PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/stable-1.2.0/Client/Helper.cs

#
C# | 148 lines | 108 code | 17 blank | 23 comment | 13 complexity | 828e3b7643ddb86a2a739295bbc9e240 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright>
  3. // Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4. //
  5. // This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7. // </copyright>
  8. //-----------------------------------------------------------------------
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.Diagnostics.CodeAnalysis;
  14. namespace Web.Management.PHP
  15. {
  16. internal static class Helper
  17. {
  18. internal static bool Browse(string url)
  19. {
  20. if (url != null &&
  21. (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) ||
  22. url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  23. {
  24. try
  25. {
  26. Process.Start(url);
  27. return true;
  28. }
  29. catch
  30. {
  31. }
  32. }
  33. return false;
  34. }
  35. internal static string EnsureTrailingSlash(string path)
  36. {
  37. if (!path.EndsWith("/", StringComparison.Ordinal))
  38. {
  39. path = path + "/";
  40. }
  41. return path;
  42. }
  43. internal static string GetURLFromBinding(string serverName, string bindingProtocol, string bindingInformation)
  44. {
  45. string ipAddress = String.Empty;
  46. string port = String.Empty;
  47. string hostHeader = String.Empty;
  48. string[] values = bindingInformation.Split(':');
  49. if (values.Length == 3)
  50. {
  51. ipAddress = values[0];
  52. port = values[1];
  53. hostHeader = values[2];
  54. }
  55. else
  56. {
  57. // if it's a ipv6 address, the bindingInformation can look like:
  58. //"[3ffe:8311:ffff:f70f:0:5efe:172.30.188.251]:80:hostheader"
  59. if (values.Length > 2)
  60. {
  61. // look for the last delimiter for the hostheader
  62. int lastOne = bindingInformation.LastIndexOf(':');
  63. // look for the last delimiter for the port
  64. string tempString = bindingInformation.Substring(0, lastOne);
  65. int secondLastOne = tempString.LastIndexOf(':');
  66. // everything before that colon is the ip address
  67. ipAddress = bindingInformation.Substring(0, secondLastOne);
  68. port = values[values.Length - 2];
  69. hostHeader = values[values.Length - 1];
  70. }
  71. }
  72. if (port == "80")
  73. {
  74. port = null;
  75. }
  76. // check if the binding has a hostheader, if it does than just use that
  77. // as the ip address
  78. if (!string.IsNullOrEmpty(hostHeader))
  79. {
  80. ipAddress = hostHeader;
  81. }
  82. else
  83. {
  84. // host header wasnt set, so lets see if there is any specified ip address
  85. // if there is, then just use that as the ip address.
  86. if (string.IsNullOrEmpty(ipAddress))
  87. {
  88. ipAddress = serverName;
  89. }
  90. else
  91. {
  92. if (String.Equals(ipAddress, "*", StringComparison.OrdinalIgnoreCase))
  93. {
  94. ipAddress = serverName;
  95. }
  96. }
  97. }
  98. if (port != null)
  99. {
  100. return bindingProtocol + "://" + ipAddress + ":" + port + "/";
  101. }
  102. return bindingProtocol + "://" + ipAddress + "/";
  103. }
  104. /// <summary>
  105. /// returns a list of URLs based on the bindings provided and the server name.
  106. ///
  107. /// </summary>
  108. /// <param name="bindings">Arraylist of string[] {bindingProtocol, bindingInformation}</param>
  109. ///
  110. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  111. internal static List<string> GetUrlListFromBindings(string serverName, ArrayList bindings, string relativePath)
  112. {
  113. List<string> urls = new List<string>();
  114. foreach (string[] b in bindings)
  115. {
  116. string url = Helper.GetURLFromBinding(serverName, (string)b[0], (string)b[1]);
  117. try
  118. {
  119. Uri siteURI = new Uri(url);
  120. Uri uri = new Uri(siteURI, relativePath);
  121. string absoluteURL = uri.AbsoluteUri;
  122. urls.Add(EnsureTrailingSlash(absoluteURL));
  123. }
  124. catch
  125. {
  126. }
  127. }
  128. return urls;
  129. }
  130. }
  131. }