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
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WindowsScroll.cs
- IdentityNotMappedException.cs
- WebPartVerb.cs
- SymbolType.cs
- BaseTransportHeaders.cs
- TextServicesContext.cs
- ContextMenuAutomationPeer.cs
- CustomAttributeFormatException.cs
- RequestNavigateEventArgs.cs
- MeasureData.cs
- Matrix3D.cs
- LogReserveAndAppendState.cs
- SqlBulkCopyColumnMapping.cs
- Vector3DKeyFrameCollection.cs
- OfTypeExpression.cs
- TextFormatterContext.cs
- XhtmlBasicLabelAdapter.cs
- NavigatingCancelEventArgs.cs
- GC.cs
- SchemaImporter.cs
- KeyFrames.cs
- FilteredReadOnlyMetadataCollection.cs
- BeginEvent.cs
- DeleteBookmarkScope.cs
- CodeCatchClause.cs
- KeyGestureValueSerializer.cs
- LicenseContext.cs
- RequestChannel.cs
- Label.cs
- BitmapVisualManager.cs
- SoapException.cs
- MessageBox.cs
- GenericNameHandler.cs
- ThemeInfoAttribute.cs
- TreeNode.cs
- RotateTransform.cs
- RIPEMD160Managed.cs
- ISO2022Encoding.cs
- InvalidFilterCriteriaException.cs
- InstanceDataCollection.cs
- IconHelper.cs
- Section.cs
- SqlNodeTypeOperators.cs
- ColorAnimationBase.cs
- FixUpCollection.cs
- Win32KeyboardDevice.cs
- FontFamily.cs
- SafeFileMappingHandle.cs
- TimelineCollection.cs
- TrackBarRenderer.cs
- SqlConnectionPoolGroupProviderInfo.cs
- XmlJsonWriter.cs
- Substitution.cs
- XmlEntity.cs
- ComContractElementCollection.cs
- ControlPager.cs
- SQLByte.cs
- EncryptedPackageFilter.cs
- COSERVERINFO.cs
- EventLogTraceListener.cs
- UrlUtility.cs
- HwndTarget.cs
- XmlDocumentType.cs
- MsmqIntegrationBindingCollectionElement.cs
- ColorEditor.cs
- ColorAnimation.cs
- XPathItem.cs
- ConfigurationValues.cs
- CustomAttributeSerializer.cs
- DictionaryBase.cs
- DynamicPropertyReader.cs
- WebSysDefaultValueAttribute.cs
- DetailsViewUpdatedEventArgs.cs
- ObjectDataSourceWizardForm.cs
- OleDbPermission.cs
- MetabaseServerConfig.cs
- XmlUrlResolver.cs
- SqlDataRecord.cs
- ObjectListTitleAttribute.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- WmiEventSink.cs
- FontCacheUtil.cs
- ApplicationActivator.cs
- TypeConverterHelper.cs
- ScriptMethodAttribute.cs
- NotifyCollectionChangedEventArgs.cs
- ContentPresenter.cs
- WindowsListViewGroupHelper.cs
- Point3DCollection.cs
- JavascriptCallbackBehaviorAttribute.cs
- RedistVersionInfo.cs
- SessionStateModule.cs
- KnownTypesProvider.cs
- ConnectionProviderAttribute.cs
- LocationReferenceValue.cs
- UniqueIdentifierService.cs
- PriorityQueue.cs
- OdbcException.cs
- TypeLibConverter.cs
- DataKey.cs