BaseUriWithWildcard.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / BaseUriWithWildcard.cs / 1 / BaseUriWithWildcard.cs

                            //---------------------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------------------

namespace System.ServiceModel.Channels 
{
    using System.Collections; 
    using System.Collections.Generic; 
    using System.ServiceModel;
    using System.Diagnostics; 
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Web.Hosting;
 
    [DataContract]
    sealed class BaseUriWithWildcard 
    { 
        [DataMember]
        Uri baseAddress; 

        static char[] bindingSeparator = new char[] {':'};
        const char segmentDelimiter = '/';
 
        [DataMember]
        HostNameComparisonMode hostNameComparisonMode; 
        const string plus = "+"; 
        const string star = "*";
        const int HttpUriDefaultPort = 80; 
        const int HttpsUriDefaultPort = 443;

        BaseUriWithWildcard(string protocol, int defaultPort, string binding, int segmentCount, string path)
        { 
            string[] urlParameters = binding.Split(bindingSeparator, StringSplitOptions.None);
            if (urlParameters.Length != segmentCount) 
            { 
                string sampleBinding = GetSampleBinding(protocol);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                    new UriFormatException(SR.GetString(SR.Hosting_MisformattedBinding, binding, protocol, sampleBinding)));
            }

            int currentIndex = segmentCount - 1; 
            string host = ParseHostAndHostNameComparisonMode(urlParameters[currentIndex]);
 
            int port = -1; 
            if (--currentIndex >= 0)
            { 
                string portString = urlParameters[currentIndex].Trim();
                if (!string.IsNullOrEmpty(portString) &&
                    !int.TryParse(portString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out port))
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriFormatException(SR.GetString(SR.Hosting_MisformattedPort, protocol, binding, portString)));
                } 
 
                if (port == defaultPort)
                { 
                    // Set to -1 so that Uri does not show it in the string.
                    port = -1;
                }
            } 

            try 
            { 
                if (path != null)
                { 
                    baseAddress = new UriBuilder(protocol, host, port, path).Uri;
                }
                else
                { 
                    baseAddress = new UriBuilder(protocol, host, port, HostingEnvironmentWrapper.ApplicationVirtualPath).Uri;
                } 
            } 
            catch (Exception exception)
            { 
                if (DiagnosticUtility.IsFatal(exception))
                {
                    throw;
                } 

                if (DiagnosticUtility.ShouldTraceError) 
                { 
                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Error);
                } 

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriFormatException(SR.GetString(SR.Hosting_MisformattedBindingData, binding,
                    protocol)));
            } 
        }
 
        static string GetSampleBinding(string protocol) 
        {
            if (string.Compare(protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) == 0) 
            {
                return ":80:";
            }
 
            if (string.Compare(protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) == 0)
            { 
                return ":443:"; 
            }
 
            if (string.Compare(protocol, Uri.UriSchemeNetTcp, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return "808:*";
            } 

            if ( (string.Compare(protocol, Uri.UriSchemeNetPipe, StringComparison.OrdinalIgnoreCase) == 0) || 
                 (string.Compare(protocol, MsmqUri.NetMsmqAddressTranslator.Scheme, StringComparison.OrdinalIgnoreCase) == 0) || 
                 (string.Compare(protocol, MsmqUri.FormatNameAddressTranslator.Scheme, StringComparison.OrdinalIgnoreCase) == 0))
            { 
                return "*";
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                    new UriFormatException(SR.GetString(SR.Hosting_NotSupportedProtocol, protocol)));
        } 
 
        internal BaseUriWithWildcard(Uri baseAddress, HostNameComparisonMode hostNameComparisonMode)
        { 
            this.baseAddress = baseAddress;
            this.hostNameComparisonMode = hostNameComparisonMode;

            // Note the Uri may contain query string for WSDL purpose. 
            // So do not check IsValid().
        } 
 
        internal static BaseUriWithWildcard CreateHttpUri(string binding)
        { 
            if (binding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding"));
            } 
            return CreateHttpUri(binding, null);
        } 
 
        static BaseUriWithWildcard CreateHttpUri(string binding, string path)
        { 
            // For http, binding format is: "::"
            // as specified in http://www.microsoft.com/resources/documentation/WindowsServ/2003/standard/proddocs/en-us/Default.asp?url=/resources/documentation/WindowsServ/2003/standard/proddocs/en-us/ref_mb_serverbindings.asp
            return new BaseUriWithWildcard(Uri.UriSchemeHttp, HttpUriDefaultPort, binding, 3, path);
        } 

        internal static BaseUriWithWildcard CreateHttpsUri(string binding) 
        { 
            if (binding == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding"));
            }

            return CreateHttpsUri(binding, null); 
        }
 
        static BaseUriWithWildcard CreateHttpsUri(string binding, string path) 
        {
            // For https, binding format is the same as http 
            return new BaseUriWithWildcard(Uri.UriSchemeHttps, HttpsUriDefaultPort, binding, 3, path);
        }

        internal static BaseUriWithWildcard CreateTcpUri(string binding) 
        {
            if (binding == null) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding"));
            } 

            return CreateTcpUri(binding, null);
        }
 
        static BaseUriWithWildcard CreateTcpUri(string binding, string path)
        { 
            // For net.tcp, binding format is: ":" 
            return new BaseUriWithWildcard(Uri.UriSchemeNetTcp, TcpUri.DefaultPort, binding, 2, path);
        } 

        internal static BaseUriWithWildcard CreatePipeUri(string binding)
        {
            if (binding == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding")); 
            } 

            return CreatePipeUri(binding, null); 
        }

        static BaseUriWithWildcard CreatePipeUri(string binding, string path)
        { 
            // For net.pipe, binding format is: ""
            return new BaseUriWithWildcard(Uri.UriSchemeNetPipe, -1, binding, 1, path); 
        } 

        internal static BaseUriWithWildcard CreateMsmqUri(string binding) 
        {
            if (binding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding")); 
            }
 
            return CreateMsmqUri(binding, null); 
        }
 
        static BaseUriWithWildcard CreateMsmqUri(string binding, string path)
        {
            return new BaseUriWithWildcard(MsmqUri.NetMsmqAddressTranslator.Scheme, -1, binding, 1, path);
        } 

        internal static BaseUriWithWildcard CreateMsmqFormatNameUri(string binding) 
        { 
            if (binding == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding"));
            }

            return CreateMsmqFormatNameUri(binding, null); 
        }
 
        static BaseUriWithWildcard CreateMsmqFormatNameUri(string binding, string path) 
        {
            return new BaseUriWithWildcard(MsmqUri.FormatNameAddressTranslator.Scheme, -1, binding, 1, path); 
        }

        internal static BaseUriWithWildcard CreateUri(string protocol, string binding, string path)
        { 
            if (protocol == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("protocol")); 
            }
 
            if (binding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("binding"));
            } 

            if (path == null) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("path"));
            } 

            if (protocol.Equals(Uri.UriSchemeHttp))
            {
                return CreateHttpUri(binding, path); 
            }
            else if (protocol.Equals(Uri.UriSchemeHttps)) 
            { 
                return CreateHttpsUri(binding, path);
            } 
            else if (protocol.Equals(Uri.UriSchemeNetTcp))
            {
                return CreateTcpUri(binding, path);
            } 
            else if (protocol.Equals(Uri.UriSchemeNetPipe))
            { 
                return CreatePipeUri(binding, path); 
            }
            else if (protocol.Equals(MsmqUri.NetMsmqAddressTranslator.Scheme)) 
            {
                return CreateMsmqUri(binding, path);
            }
            else if (protocol.Equals(MsmqUri.FormatNameAddressTranslator.Scheme)) 
            {
                return CreateMsmqFormatNameUri(binding, path); 
            } 

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriFormatException(SR.GetString(SR.Hosting_NotSupportedProtocol, binding))); 
        }

        internal Uri BaseAddress
        { 
            get { return baseAddress; }
        } 
 
        internal HostNameComparisonMode HostNameComparisonMode
        { 
            get { return hostNameComparisonMode; }
        }

        public override bool Equals(object other) 
        {
            BaseUriWithWildcard otherBaseUri = other as BaseUriWithWildcard; 
            if (otherBaseUri == null || GetHashCode() != otherBaseUri.GetHashCode() || this.HostNameComparisonMode!= otherBaseUri.HostNameComparisonMode) 
            {
                return false; 
            }
            if ((object) this.BaseAddress.Scheme != (object) otherBaseUri.BaseAddress.Scheme)
            {
                return false; 
            }
            if (this.BaseAddress.Port != otherBaseUri.BaseAddress.Port) 
            { 
                // port is a pain for tcp because Uri doesn't understand net.tcp
                if ((baseAddress.Port == TcpUri.DefaultPort && otherBaseUri.baseAddress.Port != -1) 
                    || (baseAddress.Port == -1 && otherBaseUri.baseAddress.Port != TcpUri.DefaultPort))
                {
                    return false;
                } 
            }
            if (this.HostNameComparisonMode != HostNameComparisonMode.Exact) 
            { 
                string s1 = this.BaseAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped);
                string s2 = otherBaseUri.BaseAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped); 
                return s1.Equals(s2);
            }
            return this.BaseAddress.Equals(otherBaseUri.BaseAddress);
        } 

        public override int GetHashCode() 
        { 
            return baseAddress.Port ^ baseAddress.PathAndQuery.GetHashCode() ^ (int)this.HostNameComparisonMode;
        } 

        internal bool IsBaseOf(Uri fullAddress)
        {
            if ((object) baseAddress.Scheme != (object) fullAddress.Scheme) 
            {
                return false; 
            } 

            if (baseAddress.Port != fullAddress.Port) 
            {
                return false;
            }
 
            if (this.HostNameComparisonMode == HostNameComparisonMode.Exact)
            { 
                if (string.Compare(baseAddress.Host, fullAddress.Host, StringComparison.OrdinalIgnoreCase) != 0) 
                {
                    return false; 
                }
            }
            string s1 = baseAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped);
            string s2 = fullAddress.GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped); 

            if (s1.Length > s2.Length) 
            { 
                return false;
            } 

            if (s1.Length < s2.Length &&
                s1[s1.Length - 1] != segmentDelimiter &&
                s2[s1.Length] != segmentDelimiter) 
            {
                // Matching over segments 
                return false; 
            }
 
            return string.Compare(s2, 0, s1, 0, s1.Length, StringComparison.OrdinalIgnoreCase) == 0;
        }

        [OnDeserialized] 
        internal void OnDeserialized(StreamingContext context)
        { 
            UriSchemeKeyedCollection.ValidateBaseAddress(baseAddress, "context"); 
            if (!HostNameComparisonModeHelper.IsDefined(this.HostNameComparisonMode))
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("context", SR.GetString(SR.Hosting_BaseUriDeserializedNotValid));
            }
        }
 
        string ParseHostAndHostNameComparisonMode(string host)
        { 
            if (string.IsNullOrEmpty(host) || host.Equals(star)) 
            {
                hostNameComparisonMode = HostNameComparisonMode.WeakWildcard; 
                host = DnsCache.MachineName;
            }
            else if (host.Equals(plus))
            { 
                hostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                host = DnsCache.MachineName; 
            } 
            else
            { 
                hostNameComparisonMode = HostNameComparisonMode.Exact;
            }

            return host; 
        }
 
        public override string ToString() 
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.HostNameComparisonMode, this.BaseAddress); 
        }
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK