Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Activation / HostedHttpTransportManager.cs / 1 / HostedHttpTransportManager.cs
//---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Activation { using System.Collections.Generic; using System.ServiceModel; using System.ServiceModel.Channels; using System.Diagnostics; using System.Globalization; using System.Net; using System.ServiceModel.Diagnostics; using System.Web; using System.Web.Hosting; using System.Security.Principal; using System.Threading; using System.Security; using System.Security.Permissions; class HostedHttpTransportManager : HttpTransportManager { internal HostedHttpTransportManager(BaseUriWithWildcard baseAddress) { this.ListenUri = baseAddress.BaseAddress; this.HostNameComparisonMode = baseAddress.HostNameComparisonMode; } internal override bool IsCompatible(HttpChannelListener factory) { return true; } internal override void OnClose() { // empty } internal override void OnOpen() { // empty } internal override string Scheme { get { return this.ListenUri.Scheme; } } static bool canTraceConnectionInformation = true; public void TraceConnectionInformation(HostedHttpRequestAsyncResult result) { if (result != null && DiagnosticUtility.ShouldTraceInformation && canTraceConnectionInformation) { try { IServiceProvider provider = (IServiceProvider)result.Application.Context; HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); string localAddress = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", workerRequest.GetLocalAddress(), workerRequest.GetLocalPort()); string remoteAddress = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", workerRequest.GetRemoteAddress(), workerRequest.GetRemotePort()); TraceUtility.TraceHttpConnectionInformation(localAddress, remoteAddress, this); } catch (SecurityException e) { canTraceConnectionInformation = false; // not re-throwing on purpose if (DiagnosticUtility.ShouldTraceWarning) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); } } } } ////// Critical - calls getters with LinkDemands in ASP .NET objects /// Safe - only returns the activity, doesn't leak the ASP .NET objects /// [SecurityCritical, SecurityTreatAsSafe] public ServiceModelActivity CreateReceiveBytesActivity(HostedHttpRequestAsyncResult result) { ServiceModelActivity retval = null; if (result != null && DiagnosticUtility.ShouldUseActivity) { IServiceProvider provider = (IServiceProvider)result.Application.Context; retval = ServiceModelActivity.CreateBoundedActivity(GetRequestTraceIdentifier(provider)); ServiceModelActivity.Start(retval, SR.GetString(SR.ActivityReceiveBytes, result.RequestUri.ToString()), ActivityType.ReceiveBytes); } return retval; } ////// Critical - uses the HttpWorkerRequest to get the trace identifier, which Demands UnamangedCode /// Safe - only returns the trace id, doesn't leak the HttpWorkerRequest /// [SecurityCritical, SecurityTreatAsSafe] [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] static Guid GetRequestTraceIdentifier(IServiceProvider provider) { return ((HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest))).RequestTraceIdentifier; } internal void HttpContextReceived(HostedHttpRequestAsyncResult result) { using (DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.BoundOperation(this.Activity) : null) { using (this.CreateReceiveBytesActivity(result)) { this.TraceConnectionInformation(result); HttpChannelListener listener; if (base.TryLookupUri(result.RequestUri, result.GetHttpMethod(), this.HostNameComparisonMode, out listener)) { HostedHttpContext hostedContext = (HostedHttpContext)HttpRequestContext.CreateContext(listener, result); listener.HttpContextReceived(hostedContext, null); return; } if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.HttpChannelMessageReceiveFailed, new StringTraceRecord("IsRecycling", ServiceHostingEnvironment.IsRecycling.ToString(CultureInfo.CurrentCulture)), this, null); } if (ServiceHostingEnvironment.IsRecycling) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new EndpointNotFoundException(SR.GetString(SR.Hosting_ListenerNotFoundForActivationInRecycling, result.RequestUri.ToString()))); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new EndpointNotFoundException(SR.GetString(SR.Hosting_ListenerNotFoundForActivation, result.RequestUri.ToString()))); } } } } } sealed class HostingMessageProperty { const string name = "webhost"; ////// Critical - keeps track of impersonated user, caller must use with care and call Dispose at the appropriate time /// [SecurityCritical] HostedImpersonationContext impersonationContext; ////// Critical - stores a SecurityCritical helper class that controls HttpContext.Current with an elevation /// need to ensure that HostedThreadData is constructed and used properly /// [SecurityCritical] HostedThreadData currentThreadData; ////// Critical - sets impersonation context from an arbitrary source, caller must guard /// [SecurityCritical] internal HostingMessageProperty(HostedHttpRequestAsyncResult result) { if (!ServiceHostingEnvironment.IsHosted) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.Hosting_ProcessNotExecutingUnderHostedContext, "HostingMessageProperty"))); if (ServiceHostingEnvironment.AspNetCompatibilityEnabled) { if (result.ImpersonationContext != null && result.ImpersonationContext.IsImpersonated) { this.impersonationContext = result.ImpersonationContext; this.impersonationContext.AddRef(); } currentThreadData = result.HostedThreadData; } } static internal string Name { get { return name; } } internal HostedImpersonationContext ImpersonationContext { ////// Critical - keeps track of impersonated user, caller must use with care /// Safe - safe for Get, individual members of HostedImpersonationContext are protected /// [SecurityCritical, SecurityTreatAsSafe] get { return impersonationContext; } } ////// Critical - delegates to a SecurityCritical method in HostedThreadData. caller must ensure that function is called appropriately /// and result is guarded and Dispose()'d correctly. /// [SecurityCritical] internal IDisposable ApplyIntegrationContext() { if (ServiceHostingEnvironment.AspNetCompatibilityEnabled) { return currentThreadData.CreateContext(); } return null; } ////// Critical - clean up impersonationContext, which is critical /// Safe - doesn't leak anything /// [SecurityCritical, SecurityTreatAsSafe] public void Close() { if (impersonationContext != null) { impersonationContext.Release(); impersonationContext = null; } } } ////// Critical - captures HttpContext.Current on construction, then can apply that state at a later time and reset on Dispose /// whole thing is critical because where it was initially constructed can be used to control HttpContext.set_Current later /// and HttpContext.set_Current requires an elevation /// [SecurityCritical(SecurityCriticalScope.Everything)] class HostedThreadData { CultureInfo cultureInfo; CultureInfo uiCultureInfo; HttpContext httpContext; public HostedThreadData() { this.cultureInfo = CultureInfo.CurrentCulture; this.uiCultureInfo = CultureInfo.CurrentUICulture; this.httpContext = HttpContext.Current; } public IDisposable CreateContext() { return new HostedAspNetContext(this); } [SecurityPermission(SecurityAction.Assert, Unrestricted = true)] static void UnsafeApplyData(HostedThreadData data) { // We set the CallContext.HostContext directly instead of setting HttpContext.Current because // the latter uses a demand instead of a link demand, which is very expensive in partial trust. System.Runtime.Remoting.Messaging.CallContext.HostContext = data.httpContext; Thread currentThread = Thread.CurrentThread; if (currentThread.CurrentCulture != data.cultureInfo) { currentThread.CurrentCulture = data.cultureInfo; } if (currentThread.CurrentUICulture != data.uiCultureInfo) { currentThread.CurrentUICulture = data.uiCultureInfo; } } class HostedAspNetContext : IDisposable { HostedThreadData oldData; public HostedAspNetContext(HostedThreadData newData) { oldData = new HostedThreadData(); HostedThreadData.UnsafeApplyData(newData); } public void Dispose() { HostedThreadData.UnsafeApplyData(oldData); } } } } // 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
- DbExpressionVisitor.cs
- Util.cs
- DBConnectionString.cs
- ParallelEnumerable.cs
- TableCell.cs
- Stroke.cs
- IisTraceWebEventProvider.cs
- MachineKey.cs
- XamlFilter.cs
- FastEncoderWindow.cs
- PhysicalFontFamily.cs
- InvalidPropValue.cs
- MSHTMLHost.cs
- PriorityQueue.cs
- DelimitedListTraceListener.cs
- StylusSystemGestureEventArgs.cs
- DbDataSourceEnumerator.cs
- DataGridViewRowHeaderCell.cs
- DrawingGroup.cs
- DefaultPropertyAttribute.cs
- AutoGeneratedFieldProperties.cs
- KnownIds.cs
- XamlFilter.cs
- RegexParser.cs
- LateBoundChannelParameterCollection.cs
- StringReader.cs
- XPathQilFactory.cs
- BamlTreeMap.cs
- ItemCollection.cs
- CodeNamespace.cs
- WsdlExporter.cs
- RayMeshGeometry3DHitTestResult.cs
- SafeRegistryHandle.cs
- CaseKeyBox.ViewModel.cs
- DecimalAnimation.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- AutomationProperty.cs
- BitmapPalettes.cs
- ClusterRegistryConfigurationProvider.cs
- ProcessHostMapPath.cs
- TextServicesContext.cs
- IgnoreDeviceFilterElement.cs
- ListView.cs
- ImageConverter.cs
- SearchForVirtualItemEventArgs.cs
- safelink.cs
- TagMapInfo.cs
- HttpConfigurationContext.cs
- AsyncPostBackErrorEventArgs.cs
- FeatureSupport.cs
- TextDecorationCollection.cs
- InProcStateClientManager.cs
- MenuItemBindingCollection.cs
- Win32MouseDevice.cs
- ConsoleCancelEventArgs.cs
- SmtpAuthenticationManager.cs
- ComboBoxDesigner.cs
- HttpServerProtocol.cs
- GraphicsContext.cs
- SafeNativeMethods.cs
- GridSplitterAutomationPeer.cs
- MSHTMLHostUtil.cs
- PrimitiveCodeDomSerializer.cs
- Rect3D.cs
- JoinElimination.cs
- ToolStripItemClickedEventArgs.cs
- TreeWalkHelper.cs
- TraceEventCache.cs
- SocketAddress.cs
- ValidatorCollection.cs
- ToolBarButtonDesigner.cs
- BaseDataListPage.cs
- Documentation.cs
- _Connection.cs
- WindowsGraphics.cs
- PrintDialog.cs
- ExpressionBuilderContext.cs
- RotateTransform3D.cs
- HtmlInputRadioButton.cs
- DisposableCollectionWrapper.cs
- ListenerSingletonConnectionReader.cs
- ListBox.cs
- InstanceLockQueryResult.cs
- ReaderWriterLock.cs
- SiteMapDataSourceView.cs
- unitconverter.cs
- TextEditorThreadLocalStore.cs
- Serializer.cs
- AssemblyCollection.cs
- FileDialogPermission.cs
- TranslateTransform.cs
- FormsAuthenticationUserCollection.cs
- CommandDesigner.cs
- SafeCoTaskMem.cs
- AttributeProviderAttribute.cs
- RootBrowserWindow.cs
- UpdateRecord.cs
- SystemShuttingDownException.cs
- PropertyEntry.cs
- Stylus.cs