Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Dispatcher / EndpointAddressMessageFilter.cs / 1 / EndpointAddressMessageFilter.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Dispatcher { using System; using System.ServiceModel.Channels; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.ServiceModel.Security; using System.Text; using System.Xml; using QName = System.ServiceModel.Dispatcher.EndpointAddressProcessor.QName; using HeaderBit = System.ServiceModel.Dispatcher.EndpointAddressProcessor.HeaderBit; public class EndpointAddressMessageFilter : MessageFilter { EndpointAddress address; bool includeHostNameInComparison; EndpointAddressMessageFilterHelper helper; EqualityComparercomparer; public EndpointAddressMessageFilter(EndpointAddress address) : this(address, false) { } public EndpointAddressMessageFilter(EndpointAddress address, bool includeHostNameInComparison) { if (address == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address"); } this.address = address; this.includeHostNameInComparison = includeHostNameInComparison; this.helper = new EndpointAddressMessageFilterHelper(this.address); if (includeHostNameInComparison) { this.comparer = HostUriComparer.Value; } else { this.comparer = NoHostUriComparer.Value; } } public EndpointAddress Address { get { return this.address; } } public bool IncludeHostNameInComparison { get { return this.includeHostNameInComparison; } } protected internal override IMessageFilterTable CreateFilterTable () { return new EndpointAddressMessageFilterTable (); } public override bool Match(MessageBuffer messageBuffer) { if (messageBuffer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageBuffer"); } Message msg = messageBuffer.CreateMessage(); try { return Match(msg); } finally { msg.Close(); } } public override bool Match(Message message) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message"); } // To #pragma warning suppress 56506 // [....], Message.Headers can never be null Uri to = message.Headers.To; Uri actingAs = this.address.Uri; if (to == null || !this.comparer.Equals(actingAs, to)) { return false; } return this.helper.Match(message); } internal Dictionary HeaderLookup { get { return this.helper.HeaderLookup; } } internal abstract class UriComparer : EqualityComparer { protected UriComparer() { } protected abstract bool CompareHost { get; } public override bool Equals(Uri u1, Uri u2) { return EndpointAddress.UriEquals(u1, u2, true /* ignoreCase */, CompareHost); } public override int GetHashCode(Uri uri) { return EndpointAddress.UriGetHashCode(uri, CompareHost); } } internal sealed class HostUriComparer : UriComparer { internal static readonly EqualityComparer Value = new HostUriComparer(); HostUriComparer() { } protected override bool CompareHost { get { return true; } } } internal sealed class NoHostUriComparer : UriComparer { internal static readonly EqualityComparer Value = new NoHostUriComparer(); NoHostUriComparer() { } protected override bool CompareHost { get { return false; } } } } internal class EndpointAddressMessageFilterHelper { EndpointAddress address; WeakReference processorPool; int size; byte[] mask; Dictionary qnameLookup; Dictionary headerLookup; internal EndpointAddressMessageFilterHelper(EndpointAddress address) { this.address = address; if (this.address.Headers.Count > 0) { CreateMask(); this.processorPool = new WeakReference(null); } else { this.qnameLookup = null; this.headerLookup = null; this.size = 0; this.mask = null; } } void CreateMask() { int nextBit = 0; string key; HeaderBit[] bits; QName qname; this.qnameLookup = new Dictionary (EndpointAddressProcessor.QNameComparer); this.headerLookup = new Dictionary (); StringBuilder builder = null; for (int i = 0; i < this.address.Headers.Count; ++i) { if (builder == null) builder = new StringBuilder(); else builder.Remove(0, builder.Length); key = this.address.Headers[i].GetComparableForm(builder); if (this.headerLookup.TryGetValue(key, out bits)) { Array.Resize(ref bits, bits.Length + 1); bits[bits.Length - 1] = new HeaderBit(nextBit++); this.headerLookup[key] = bits; } else { this.headerLookup.Add(key, new HeaderBit[] { new HeaderBit(nextBit++) }); AddressHeader parameter = this.address.Headers[i]; qname.name = parameter.Name; qname.ns = parameter.Namespace; this.qnameLookup[qname] = 1; } } if (nextBit == 0) { this.size = 0; } else { this.size = (nextBit - 1) / 8 + 1; } if (this.size > 0) { this.mask = new byte[size]; for (int i = 0; i < this.size - 1; ++i) { this.mask[i] = 0xff; } if ((nextBit % 8) == 0) { this.mask[this.size - 1] = 0xff; } else { this.mask[this.size - 1] = (byte)((1 << (nextBit % 8)) - 1); } } } internal Dictionary HeaderLookup { get { if (this.headerLookup == null) this.headerLookup = new Dictionary (); return this.headerLookup; } } EndpointAddressProcessor CreateProcessor(int length) { if (this.processorPool.Target != null) { lock (this.processorPool) { object o = this.processorPool.Target; if (o != null) { EndpointAddressProcessor p = (EndpointAddressProcessor)o; this.processorPool.Target = p.Next; p.Next = null; p.Clear(length); return p; } } } return new EndpointAddressProcessor(length); } internal bool Match(Message message) { if (this.size == 0) { return true; } EndpointAddressProcessor context = CreateProcessor(this.size); context.ProcessHeaders(message, this.qnameLookup, this.headerLookup); bool result = context.TestExact(this.mask); ReleaseProcessor(context); return result; } void ReleaseProcessor(EndpointAddressProcessor context) { lock (this.processorPool) { context.Next = this.processorPool.Target as EndpointAddressProcessor; this.processorPool.Target = context; } } } } // 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
- InstanceDataCollection.cs
- XmlComment.cs
- storepermissionattribute.cs
- TreeNodeCollectionEditor.cs
- ConsoleKeyInfo.cs
- DocumentPage.cs
- BookmarkScope.cs
- Int16.cs
- QueryableFilterRepeater.cs
- WindowsPen.cs
- MailAddress.cs
- TreeView.cs
- QueryAsyncResult.cs
- XD.cs
- DoubleAnimationClockResource.cs
- CommandID.cs
- ModelItemExtensions.cs
- PasswordRecovery.cs
- OutputCacheModule.cs
- SecurityManager.cs
- StaticDataManager.cs
- SQLBinary.cs
- FixedPageAutomationPeer.cs
- CompiledRegexRunner.cs
- DragDeltaEventArgs.cs
- PolicyStatement.cs
- Expression.cs
- BufferModesCollection.cs
- ItemCheckedEvent.cs
- Size3D.cs
- backend.cs
- Vector3DCollectionConverter.cs
- RegionInfo.cs
- DataGridView.cs
- ClientViaElement.cs
- FlowLayout.cs
- SafeFileMapViewHandle.cs
- HttpAsyncResult.cs
- FilteredReadOnlyMetadataCollection.cs
- SoapElementAttribute.cs
- HMACSHA384.cs
- ContentValidator.cs
- HostingEnvironmentSection.cs
- ExpressionSelection.cs
- TextFormatter.cs
- AnnotationHelper.cs
- UInt16.cs
- EndPoint.cs
- SqlDataSourceParameterParser.cs
- ConfigurationManagerHelper.cs
- PlatformCulture.cs
- TypedReference.cs
- SectionUpdates.cs
- SafeRightsManagementHandle.cs
- WebBrowserHelper.cs
- FacetValueContainer.cs
- PropertyDescriptorComparer.cs
- Crc32.cs
- TeredoHelper.cs
- DSASignatureFormatter.cs
- MulticastNotSupportedException.cs
- ContentElementAutomationPeer.cs
- ServerIdentity.cs
- SqlCommandSet.cs
- SqlMetaData.cs
- CodeGeneratorOptions.cs
- Win32.cs
- DataMember.cs
- InternalDuplexChannelListener.cs
- DecimalAnimationBase.cs
- UIElement3D.cs
- BitmapFrameDecode.cs
- ArrangedElement.cs
- ToolStripItemEventArgs.cs
- StorageAssociationTypeMapping.cs
- TextFormatterHost.cs
- ImpersonateTokenRef.cs
- StateItem.cs
- DtcInterfaces.cs
- PreDigestedSignedInfo.cs
- CqlErrorHelper.cs
- QuaternionIndependentAnimationStorage.cs
- RouteParametersHelper.cs
- Pair.cs
- XmlnsDictionary.cs
- Stackframe.cs
- EntityClassGenerator.cs
- TextDocumentView.cs
- AutoGeneratedFieldProperties.cs
- AsmxEndpointPickerExtension.cs
- QuaternionRotation3D.cs
- HtmlElementCollection.cs
- DbDeleteCommandTree.cs
- ExpressionParser.cs
- SymbolUsageManager.cs
- InternalResources.cs
- DataGridRowsPresenter.cs
- ModelProperty.cs
- BitVector32.cs
- SQLDouble.cs