SoapServerFormatterSink.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // System.Runtime.Remoting.Channels.SoapServerFormatterSink.cs
  3. //
  4. // Authors: Duncan Mak ([email protected])
  5. // Jean-Marc Andre ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Runtime.Remoting.Messaging;
  33. using System.Runtime.Serialization;
  34. using System.Runtime.Serialization.Formatters;
  35. using System.Runtime.Serialization.Formatters.Soap;
  36. using System.Runtime.InteropServices;
  37. namespace System.Runtime.Remoting.Channels {
  38. /// <summary>
  39. // The formatter sink that uses SoapFormatter
  40. /// </summary>
  41. // <remarks>
  42. // The formatter sink deserializes the message from the channel sink
  43. // and passes the result to the remoting infrastructure
  44. // </remark>
  45. //
  46. public class SoapServerFormatterSink : IServerChannelSink, IChannelSinkBase
  47. {
  48. IServerChannelSink next_sink;
  49. IChannelReceiver _receiver;
  50. private SoapCore _soapCore = SoapCore.DefaultInstance;
  51. public SoapServerFormatterSink (SoapServerFormatterSink.Protocol protocol,
  52. IServerChannelSink nextSink,
  53. IChannelReceiver receiver)
  54. {
  55. this.next_sink = nextSink;
  56. _receiver = receiver;
  57. }
  58. internal SoapCore SoapCore
  59. {
  60. get { return _soapCore; }
  61. set { _soapCore = value; }
  62. }
  63. /// <summary>
  64. // Gets the next channel sink in the channel sink chain
  65. // </summary>
  66. /// <value>
  67. // The next channel sink in the sink chain
  68. // </value>
  69. public IServerChannelSink NextChannelSink {
  70. get {
  71. return next_sink;
  72. }
  73. }
  74. public IDictionary Properties {
  75. get {
  76. return null;
  77. }
  78. }
  79. #if NET_1_1
  80. [ComVisible(false)]
  81. public TypeFilterLevel TypeFilterLevel
  82. {
  83. get { return _soapCore.TypeFilterLevel; }
  84. set
  85. {
  86. IDictionary props = (IDictionary) ((ICloneable)_soapCore.Properties).Clone ();
  87. props ["typeFilterLevel"] = value;
  88. _soapCore = new SoapCore (this, props, SoapServerFormatterSinkProvider.AllowedProperties);
  89. }
  90. }
  91. #endif
  92. public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
  93. IMessage msg, ITransportHeaders headers, Stream stream)
  94. {
  95. ITransportHeaders responseHeaders = new TransportHeaders();
  96. if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);
  97. if(stream == null) stream = new MemoryStream();
  98. SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;
  99. SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);
  100. _soapCore.Serializer.Serialize(stream, soapMessage, null);
  101. if(stream is MemoryStream) stream.Position = 0;
  102. sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);
  103. }
  104. public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
  105. IMessage msg, ITransportHeaders headers)
  106. {
  107. // this method shouldn't be called
  108. throw new NotSupportedException ();
  109. }
  110. public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
  111. IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
  112. out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
  113. {
  114. // Check whether the request was already processed by another
  115. // formatter sink and pass the request to the next sink if so.
  116. if (requestMsg != null)
  117. return next_sink.ProcessMessage (sinkStack,
  118. requestMsg,
  119. requestHeaders,
  120. requestStream,
  121. out responseMsg,
  122. out responseHeaders,
  123. out responseStream);
  124. // Check whether the request is suitable for this formatter
  125. // and pass the request to the next sink if not.
  126. // Note that a null content-type is handled as suitable,
  127. // otherwise no other sink will be able to handle the request.
  128. string contentType = requestHeaders["Content-Type"] as string;
  129. if (contentType != null && !contentType.StartsWith ("text/xml")) {
  130. try {
  131. return next_sink.ProcessMessage (sinkStack,
  132. requestMsg,
  133. requestHeaders,
  134. requestStream,
  135. out responseMsg,
  136. out responseHeaders,
  137. out responseStream);
  138. } catch {
  139. // Let this formatter handle the exception.
  140. }
  141. }
  142. responseMsg = null;
  143. responseHeaders = null;
  144. responseStream = null;
  145. ServerProcessing sp;
  146. SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();
  147. sinkStack.Push(this, soapMsgFormatter);
  148. try {
  149. string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
  150. string uri;
  151. _receiver.Parse(url, out uri);
  152. if(uri == null) uri = url;
  153. Type serverType = RemotingServices.GetServerTypeForUri(uri);
  154. if (serverType == null) throw new RemotingException ("No receiver for uri " + uri);
  155. SoapFormatter fm = _soapCore.GetSafeDeserializer ();
  156. SoapMessage soapMessage = soapMsgFormatter.CreateSoapMessage (true);
  157. fm.TopObject = soapMessage;
  158. requestStream.Position = 0;
  159. fm.Deserialize(requestStream);
  160. requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);
  161. sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
  162. if(sp == ServerProcessing.Complete) {
  163. if(responseMsg != null && responseStream == null) {
  164. object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
  165. responseStream = new MemoryStream();
  166. _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
  167. }
  168. }
  169. }
  170. catch(Exception e)
  171. {
  172. responseMsg = (IMethodReturnMessage)new ReturnMessage(e, (IMethodCallMessage)requestMsg);
  173. object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
  174. responseStream = new MemoryStream();
  175. _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
  176. sp = ServerProcessing.Complete;
  177. }
  178. if (sp == ServerProcessing.Complete)
  179. sinkStack.Pop(this);
  180. return sp;
  181. }
  182. [Serializable]
  183. public enum Protocol
  184. {
  185. Http = 0,
  186. Other = 1,
  187. }
  188. }
  189. }