PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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

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