We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- using System;
- using System.Collections;
- using System.IO;
- using System.Reflection;
- using System.Runtime.Serialization.Formatters;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Http;
- using System.Runtime.Remoting.Messaging;
- using System.Runtime.Remoting.Metadata;
- using System.Security;
- using System.Security.Permissions;
- using System.Globalization;
- namespace System.Runtime.Remoting.Channels
- {
-
-
-
-
-
- public class BinaryClientFormatterSinkProvider : IClientFormatterSinkProvider
- {
- private IClientChannelSinkProvider _next;
-
-
- private bool _includeVersioning = true;
- private bool _strictBinding = false;
-
-
- public BinaryClientFormatterSinkProvider()
- {
- }
-
-
- public BinaryClientFormatterSinkProvider(IDictionary properties, ICollection providerData)
- {
-
- if (properties != null) {
- foreach (DictionaryEntry entry in properties) {
- string keyStr = entry.Key.ToString();
- switch (keyStr) {
- case "includeVersions":
- _includeVersioning = Convert.ToBoolean(entry.Value, CultureInfo.InvariantCulture);
- break;
- case "strictBinding":
- _strictBinding = Convert.ToBoolean(entry.Value, CultureInfo.InvariantCulture);
- break;
- default:
-
- break;
- }
- }
- }
-
-
- CoreChannel.VerifyNoProviderData(this.GetType().Name, providerData);
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public IClientChannelSink CreateSink(IChannelSender channel, string url, object remoteChannelData)
- {
- IClientChannelSink nextSink = null;
- if (_next != null) {
- nextSink = _next.CreateSink(channel, url, remoteChannelData);
- if (nextSink == null)
- return null;
- }
-
- SinkChannelProtocol protocol = CoreChannel.DetermineChannelProtocol(channel);
-
- BinaryClientFormatterSink sink = new BinaryClientFormatterSink(nextSink);
- sink.IncludeVersioning = _includeVersioning;
- sink.StrictBinding = _strictBinding;
- sink.ChannelProtocol = protocol;
- return sink;
- }
-
- public IClientChannelSinkProvider Next {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get { return _next; }
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- set { _next = value; }
- }
- }
-
-
- public class BinaryClientFormatterSink : IClientFormatterSink
- {
- private IClientChannelSink _nextSink = null;
-
- private bool _includeVersioning = true;
-
- private bool _strictBinding = false;
-
- private SinkChannelProtocol _channelProtocol = SinkChannelProtocol.Other;
-
-
- public BinaryClientFormatterSink(IClientChannelSink nextSink)
- {
- _nextSink = nextSink;
- }
-
- internal bool IncludeVersioning {
- set { _includeVersioning = value; }
- }
-
- internal bool StrictBinding {
- set { _strictBinding = value; }
- }
-
- internal SinkChannelProtocol ChannelProtocol {
- set { _channelProtocol = value; }
- }
-
-
-
- public IMessageSink NextSink {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get {
- throw new NotSupportedException();
- }
- }
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public IMessage SyncProcessMessage(IMessage msg)
- {
- IMethodCallMessage mcm = msg as IMethodCallMessage;
- IMessage retMsg;
-
- try {
-
- ITransportHeaders headers;
- Stream requestStream;
- SerializeMessage(msg, out headers, out requestStream);
-
-
- Stream returnStream;
- ITransportHeaders returnHeaders;
- _nextSink.ProcessMessage(msg, headers, requestStream, out returnHeaders, out returnStream);
- if (returnHeaders == null)
- throw new ArgumentNullException("returnHeaders");
-
-
- retMsg = DeserializeMessage(mcm, returnHeaders, returnStream);
- }
- catch (Exception e) {
- retMsg = new ReturnMessage(e, mcm);
- }
- catch {
- retMsg = new ReturnMessage(new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException")), mcm);
- }
-
- return retMsg;
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
- {
- IMethodCallMessage mcm = (IMethodCallMessage)msg;
- IMessage retMsg;
-
- try {
-
- ITransportHeaders headers;
- Stream requestStream;
- SerializeMessage(msg, out headers, out requestStream);
-
-
- ClientChannelSinkStack sinkStack = new ClientChannelSinkStack(replySink);
- sinkStack.Push(this, msg);
- _nextSink.AsyncProcessRequest(sinkStack, msg, headers, requestStream);
- }
- catch (Exception e) {
- retMsg = new ReturnMessage(e, mcm);
- if (replySink != null)
- replySink.SyncProcessMessage(retMsg);
- }
- catch {
- retMsg = new ReturnMessage(new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException")), mcm);
- if (replySink != null)
- replySink.SyncProcessMessage(retMsg);
- }
-
- return null;
- }
-
-
-
- private void SerializeMessage(IMessage msg, out ITransportHeaders headers, out Stream stream)
- {
- BaseTransportHeaders requestHeaders = new BaseTransportHeaders();
- headers = requestHeaders;
-
-
- requestHeaders.ContentType = CoreChannel.BinaryMimeType;
- if (_channelProtocol == SinkChannelProtocol.Http)
- headers["__RequestVerb"] = "POST";
-
- bool bMemStream = false;
- stream = _nextSink.GetRequestStream(msg, headers);
- if (stream == null) {
- stream = new ChunkedMemoryStream(CoreChannel.BufferPool);
- bMemStream = true;
- }
- CoreChannel.SerializeBinaryMessage(msg, stream, _includeVersioning);
- if (bMemStream)
- stream.Position = 0;
- }
-
-
-
- private IMessage DeserializeMessage(IMethodCallMessage mcm, ITransportHeaders headers, Stream stream)
- {
-
- IMessage retMsg = CoreChannel.DeserializeBinaryResponseMessage(stream, mcm, _strictBinding);
-
- stream.Close();
- return retMsg;
- }
-
-
-
-
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public void ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream)
- {
-
- throw new NotSupportedException();
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg, ITransportHeaders headers, Stream stream)
- {
-
- throw new NotSupportedException();
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public void AsyncProcessResponse(IClientResponseChannelSinkStack sinkStack, object state, ITransportHeaders headers, Stream stream)
- {
-
- IMethodCallMessage mcm = (IMethodCallMessage)state;
- IMessage retMsg = DeserializeMessage(mcm, headers, stream);
- sinkStack.DispatchReplyMessage(retMsg);
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public Stream GetRequestStream(IMessage msg, ITransportHeaders headers)
- {
-
- throw new NotSupportedException();
- }
-
-
- public IClientChannelSink NextChannelSink {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get { return _nextSink; }
- }
-
-
- public IDictionary Properties {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get { return null; }
- }
-
-
-
-
-
- }
-
-
-
-
-
-
-
- public class BinaryServerFormatterSinkProvider : IServerFormatterSinkProvider
- {
- private IServerChannelSinkProvider _next = null;
-
-
- private bool _includeVersioning = true;
- private bool _strictBinding = false;
- private TypeFilterLevel _formatterSecurityLevel = TypeFilterLevel.Low;
-
- public BinaryServerFormatterSinkProvider()
- {
- }
-
-
- public BinaryServerFormatterSinkProvider(IDictionary properties, ICollection providerData)
- {
-
- if (properties != null) {
- foreach (DictionaryEntry entry in properties) {
- string keyStr = entry.Key.ToString();
- switch (keyStr) {
- case "includeVersions":
- _includeVersioning = Convert.ToBoolean(entry.Value, CultureInfo.InvariantCulture);
- break;
- case "strictBinding":
- _strictBinding = Convert.ToBoolean(entry.Value, CultureInfo.InvariantCulture);
- break;
- case "typeFilterLevel":
- _formatterSecurityLevel = (TypeFilterLevel)Enum.Parse(typeof(TypeFilterLevel), (string)entry.Value);
- break;
- default:
-
- break;
- }
- }
- }
-
-
- CoreChannel.VerifyNoProviderData(this.GetType().Name, providerData);
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public void GetChannelData(IChannelDataStore channelData)
- {
- }
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public IServerChannelSink CreateSink(IChannelReceiver channel)
- {
- if (null == channel) {
- throw new ArgumentNullException("channel");
- }
-
- IServerChannelSink nextSink = null;
- if (_next != null)
- nextSink = _next.CreateSink(channel);
-
- BinaryServerFormatterSink.Protocol protocol = BinaryServerFormatterSink.Protocol.Other;
-
-
- string uri = channel.GetUrlsForUri("")[0];
- if (String.Compare("http", 0, uri, 0, 4, StringComparison.OrdinalIgnoreCase) == 0)
- protocol = BinaryServerFormatterSink.Protocol.Http;
-
- BinaryServerFormatterSink sink = new BinaryServerFormatterSink(protocol, nextSink, channel);
- sink.TypeFilterLevel = _formatterSecurityLevel;
- sink.IncludeVersioning = _includeVersioning;
- sink.StrictBinding = _strictBinding;
- return sink;
- }
-
- public IServerChannelSinkProvider Next {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get { return _next; }
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- set { _next = value; }
- }
-
- [System.Runtime.InteropServices.ComVisible(false)]
- public TypeFilterLevel TypeFilterLevel {
- get { return _formatterSecurityLevel; }
-
- set { _formatterSecurityLevel = value; }
- }
- }
-
-
- public class BinaryServerFormatterSink : IServerChannelSink
- {
- [Serializable()]
- public enum Protocol
- {
- Http,
-
- Other
- }
-
- private IServerChannelSink _nextSink;
-
-
-
- private Protocol _protocol;
-
- private IChannelReceiver _receiver;
-
- private bool _includeVersioning = true;
-
- private bool _strictBinding = false;
-
- private TypeFilterLevel _formatterSecurityLevel = TypeFilterLevel.Full;
- private string lastUri = null;
-
- public BinaryServerFormatterSink(Protocol protocol, IServerChannelSink nextSink, IChannelReceiver receiver)
- {
- if (receiver == null)
- throw new ArgumentNullException("receiver");
-
- _nextSink = nextSink;
- _protocol = protocol;
- _receiver = receiver;
- }
-
-
- internal bool IncludeVersioning {
- set { _includeVersioning = value; }
- }
-
- internal bool StrictBinding {
- set { _strictBinding = value; }
- }
-
- [System.Runtime.InteropServices.ComVisible(false)]
- public TypeFilterLevel TypeFilterLevel {
- get { return _formatterSecurityLevel; }
-
- set { _formatterSecurityLevel = value; }
- }
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public ServerProcessing ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
- {
- if (requestMsg != null) {
-
- return _nextSink.ProcessMessage(sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
- }
-
- if (requestHeaders == null)
- throw new ArgumentNullException("requestHeaders");
-
- BaseTransportHeaders wkRequestHeaders = requestHeaders as BaseTransportHeaders;
-
- ServerProcessing processing;
-
- responseHeaders = null;
- responseStream = null;
-
- string verb = null;
- string contentType = null;
-
- bool bCanServiceRequest = true;
-
-
- string contentTypeHeader = null;
- if (wkRequestHeaders != null)
- contentTypeHeader = wkRequestHeaders.ContentType;
- else
- contentTypeHeader = requestHeaders["Content-Type"] as string;
- if (contentTypeHeader != null) {
- string charsetValue;
- HttpChannelHelper.ParseContentType(contentTypeHeader, out contentType, out charsetValue);
- }
-
-
- if ((contentType != null) && (String.CompareOrdinal(contentType, CoreChannel.BinaryMimeType) != 0)) {
- bCanServiceRequest = false;
- }
-
-
- if (_protocol == Protocol.Http) {
- verb = (string)requestHeaders["__RequestVerb"];
- if (!verb.Equals("POST") && !verb.Equals("M-POST"))
- bCanServiceRequest = false;
- }
-
-
- if (!bCanServiceRequest) {
-
- if (_nextSink != null) {
- return _nextSink.ProcessMessage(sinkStack, null, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
- }
- else {
-
- if (_protocol == Protocol.Http) {
-
- responseHeaders = new TransportHeaders();
- responseHeaders["__HttpStatusCode"] = "400";
- responseHeaders["__HttpReasonPhrase"] = "Bad Request";
- responseStream = null;
- responseMsg = null;
- return ServerProcessing.Complete;
- }
- else {
-
- throw new RemotingException(CoreChannel.GetResourceString("Remoting_Channels_InvalidRequestFormat"));
- }
- }
- }
-
-
- try {
- string objectUri = null;
-
- bool bIsCustomErrorEnabled = true;
- object oIsCustomErrorEnabled = requestHeaders["__CustomErrorsEnabled"];
- if (oIsCustomErrorEnabled != null && oIsCustomErrorEnabled is bool) {
- bIsCustomErrorEnabled = (bool)oIsCustomErrorEnabled;
- }
- CallContext.SetData("__CustomErrorsEnabled", bIsCustomErrorEnabled);
-
- if (wkRequestHeaders != null)
- objectUri = wkRequestHeaders.RequestUri;
- else
- objectUri = (string)requestHeaders[CommonTransportKeys.RequestUri];
-
- if (objectUri != lastUri && RemotingServices.GetServerTypeForUri(objectUri) == null)
- throw new RemotingException(CoreChannel.GetResourceString("Remoting_ChnlSink_UriNotPublished"));
- else
- lastUri = objectUri;
-
- PermissionSet currentPermissionSet = null;
- if (this.TypeFilterLevel != TypeFilterLevel.Full) {
- currentPermissionSet = new PermissionSet(PermissionState.None);
- currentPermissionSet.SetPermission(new SecurityPermission(SecurityPermissionFlag.SerializationFormatter));
- }
-
- try {
- if (currentPermissionSet != null)
- currentPermissionSet.PermitOnly();
-
-
- requestMsg = CoreChannel.DeserializeBinaryRequestMessage(objectUri, requestStream, _strictBinding, this.TypeFilterLevel);
- }
- finally {
- if (currentPermissionSet != null)
- CodeAccessPermission.RevertPermitOnly();
- }
- requestStream.Close();
-
- if (requestMsg == null) {
- throw new RemotingException(CoreChannel.GetResourceString("Remoting_DeserializeMessage"));
- }
-
-
-
- sinkStack.Push(this, null);
- processing = _nextSink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
-
- if (responseStream != null) {
- throw new RemotingException(CoreChannel.GetResourceString("Remoting_ChnlSink_WantNullResponseStream"));
- }
-
- switch (processing) {
- case ServerProcessing.Complete:
-
-
- {
- if (responseMsg == null)
- throw new RemotingException(CoreChannel.GetResourceString("Remoting_DispatchMessage"));
-
- sinkStack.Pop(this);
-
- SerializeResponse(sinkStack, responseMsg, ref responseHeaders, out responseStream);
- break;
- }
- break;
- case ServerProcessing.OneWay:
-
-
- {
- sinkStack.Pop(this);
- break;
- }
- break;
- case ServerProcessing.Async:
-
-
- {
- sinkStack.Store(this, null);
- break;
- }
- break;
-
- }
-
- }
- catch (Exception e) {
- processing = ServerProcessing.Complete;
- responseMsg = new ReturnMessage(e, (IMethodCallMessage)(requestMsg == null ? new ErrorMessage() : requestMsg));
-
-
- CallContext.SetData("__ClientIsClr", true);
- responseStream = (MemoryStream)CoreChannel.SerializeBinaryMessage(responseMsg, _includeVersioning);
- CallContext.FreeNamedDataSlot("__ClientIsClr");
- responseStream.Position = 0;
- responseHeaders = new TransportHeaders();
-
- if (_protocol == Protocol.Http) {
- responseHeaders["Content-Type"] = CoreChannel.BinaryMimeType;
- }
- }
- catch {
- processing = ServerProcessing.Complete;
- responseMsg = new ReturnMessage(new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException")), (IMethodCallMessage)(requestMsg == null ? new ErrorMessage() : requestMsg));
-
-
- CallContext.SetData("__ClientIsClr", true);
- responseStream = (MemoryStream)CoreChannel.SerializeBinaryMessage(responseMsg, _includeVersioning);
- CallContext.FreeNamedDataSlot("__ClientIsClr");
- responseStream.Position = 0;
- responseHeaders = new TransportHeaders();
-
- if (_protocol == Protocol.Http) {
- responseHeaders["Content-Type"] = CoreChannel.BinaryMimeType;
- }
- }
- finally {
- CallContext.FreeNamedDataSlot("__CustomErrorsEnabled");
- }
-
- return processing;
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public void AsyncProcessResponse(IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers, Stream stream)
- {
- SerializeResponse(sinkStack, msg, ref headers, out stream);
- sinkStack.AsyncProcessResponse(msg, headers, stream);
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- private void SerializeResponse(IServerResponseChannelSinkStack sinkStack, IMessage msg, ref ITransportHeaders headers, out Stream stream)
- {
- BaseTransportHeaders responseHeaders = new BaseTransportHeaders();
- if (headers != null) {
-
- foreach (DictionaryEntry entry in headers) {
- responseHeaders[entry.Key] = entry.Value;
- }
- }
- headers = responseHeaders;
-
- if (_protocol == Protocol.Http) {
- responseHeaders.ContentType = CoreChannel.BinaryMimeType;
- }
-
- bool bMemStream = false;
- stream = sinkStack.GetResponseStream(msg, headers);
- if (stream == null) {
- stream = new ChunkedMemoryStream(CoreChannel.BufferPool);
- bMemStream = true;
- }
-
- bool bBashUrl = CoreChannel.SetupUrlBashingForIisSslIfNecessary();
- try {
- CallContext.SetData("__ClientIsClr", true);
- CoreChannel.SerializeBinaryMessage(msg, stream, _includeVersioning);
- }
- finally {
- CallContext.FreeNamedDataSlot("__ClientIsClr");
- CoreChannel.CleanupUrlBashingForIisSslIfNecessary(bBashUrl);
- }
-
- if (bMemStream)
- stream.Position = 0;
- }
-
-
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- public Stream GetResponseStream(IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers)
- {
-
-
- throw new NotSupportedException();
- }
-
-
- public IServerChannelSink NextChannelSink {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get { return _nextSink; }
- }
-
-
- public IDictionary Properties {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure, Infrastructure = true)]
- get { return null; }<