HttpReplyChannel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // HttpReplyChannel.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2010 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Net;
  33. using System.ServiceModel;
  34. using System.Text;
  35. using System.Threading;
  36. namespace System.ServiceModel.Channels.Http
  37. {
  38. internal class HttpReplyChannel : InternalReplyChannelBase
  39. {
  40. HttpChannelListener<IReplyChannel> source;
  41. RequestContext reqctx;
  42. public HttpReplyChannel (HttpChannelListener<IReplyChannel> listener)
  43. : base (listener)
  44. {
  45. this.source = listener;
  46. }
  47. public MessageEncoder Encoder {
  48. get { return source.MessageEncoder; }
  49. }
  50. internal MessageVersion MessageVersion {
  51. get { return source.MessageEncoder.MessageVersion; }
  52. }
  53. public override RequestContext ReceiveRequest (TimeSpan timeout)
  54. {
  55. RequestContext ctx;
  56. if (!TryReceiveRequest (timeout, out ctx))
  57. throw new TimeoutException ();
  58. return ctx;
  59. }
  60. protected override void OnOpen (TimeSpan timeout)
  61. {
  62. }
  63. protected override void OnAbort ()
  64. {
  65. AbortConnections (TimeSpan.Zero);
  66. base.OnAbort (); // FIXME: remove it. The base is wrong. But it is somehow required to not block some tests.
  67. }
  68. public override bool CancelAsync (TimeSpan timeout)
  69. {
  70. AbortConnections (timeout);
  71. // FIXME: this wait is sort of hack (because it should not be required), but without it some tests are blocked.
  72. // This hack even had better be moved to base.CancelAsync().
  73. if (CurrentAsyncResult != null)
  74. CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.FromMilliseconds (300));
  75. return base.CancelAsync (timeout);
  76. }
  77. void AbortConnections (TimeSpan timeout)
  78. {
  79. if (reqctx != null)
  80. reqctx.Close (timeout);
  81. }
  82. bool close_started;
  83. protected override void OnClose (TimeSpan timeout)
  84. {
  85. if (close_started)
  86. return;
  87. close_started = true;
  88. DateTime start = DateTime.Now;
  89. // FIXME: consider timeout
  90. AbortConnections (timeout - (DateTime.Now - start));
  91. base.OnClose (timeout - (DateTime.Now - start));
  92. }
  93. protected string GetHeaderItem (string raw)
  94. {
  95. if (raw == null || raw.Length == 0)
  96. return raw;
  97. switch (raw [0]) {
  98. case '\'':
  99. case '"':
  100. if (raw [raw.Length - 1] == raw [0])
  101. return raw.Substring (1, raw.Length - 2);
  102. // FIXME: is it simply an error?
  103. break;
  104. }
  105. return raw;
  106. }
  107. protected HttpRequestMessageProperty CreateRequestProperty (HttpContextInfo ctxi)
  108. {
  109. var query = ctxi.Request.Url.Query;
  110. var prop = new HttpRequestMessageProperty ();
  111. prop.Method = ctxi.Request.HttpMethod;
  112. prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
  113. // FIXME: prop.SuppressEntityBody
  114. prop.Headers.Add (ctxi.Request.Headers);
  115. return prop;
  116. }
  117. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  118. {
  119. context = null;
  120. HttpContextInfo ctxi;
  121. if (!source.ListenerManager.TryDequeueRequest (source.ChannelDispatcher, timeout, out ctxi))
  122. return false;
  123. if (ctxi == null)
  124. return true; // returning true, yet context is null. This happens at closing phase.
  125. Message msg = null;
  126. if (ctxi.Request.HttpMethod == "POST") {
  127. msg = CreatePostMessage (ctxi);
  128. if (msg == null)
  129. return false;
  130. } else if (ctxi.Request.HttpMethod == "GET")
  131. msg = Message.CreateMessage (MessageVersion.None, null); // HTTP GET-based request
  132. if (msg.Headers.To == null)
  133. msg.Headers.To = ctxi.Request.Url;
  134. msg.Properties.Add ("Via", LocalAddress.Uri);
  135. msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctxi));
  136. context = new HttpRequestContext (this, ctxi, msg);
  137. reqctx = context;
  138. return true;
  139. }
  140. protected Message CreatePostMessage (HttpContextInfo ctxi)
  141. {
  142. if (ctxi.Response.StatusCode != 200) { // it's already invalid.
  143. ctxi.Close ();
  144. return null;
  145. }
  146. if (!Encoder.IsContentTypeSupported (ctxi.Request.ContentType)) {
  147. ctxi.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
  148. ctxi.Response.StatusDescription = String.Format (
  149. "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctxi.Request.ContentType);
  150. ctxi.Close ();
  151. return null;
  152. }
  153. // FIXME: supply maxSizeOfHeaders.
  154. int maxSizeOfHeaders = 0x10000;
  155. #if false // FIXME: enable it, once duplex callback test gets passed.
  156. Stream stream = ctxi.Request.InputStream;
  157. if (source.Source.TransferMode == TransferMode.Buffered) {
  158. if (ctxi.Request.ContentLength64 <= 0)
  159. throw new ArgumentException ("This HTTP channel is configured to use buffered mode, and thus expects Content-Length sent to the listener");
  160. long size = 0;
  161. var ms = new MemoryStream ();
  162. var buf = new byte [0x1000];
  163. while (size < ctxi.Request.ContentLength64) {
  164. if ((size += stream.Read (buf, 0, 0x1000)) > source.Source.MaxBufferSize)
  165. throw new QuotaExceededException ("Message quota exceeded");
  166. ms.Write (buf, 0, (int) (size - ms.Length));
  167. }
  168. ms.Position = 0;
  169. stream = ms;
  170. }
  171. var msg = Encoder.ReadMessage (
  172. stream, maxSizeOfHeaders, ctxi.Request.ContentType);
  173. #else
  174. var msg = Encoder.ReadMessage (
  175. ctxi.Request.InputStream, maxSizeOfHeaders, ctxi.Request.ContentType);
  176. #endif
  177. if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
  178. MessageVersion.Addressing.Equals (AddressingVersion.None)) {
  179. string action = GetHeaderItem (ctxi.Request.Headers ["SOAPAction"]);
  180. if (action != null) {
  181. if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
  182. action = action.Substring (1, action.Length - 2);
  183. msg.Headers.Action = action;
  184. }
  185. }
  186. return msg;
  187. }
  188. public override bool WaitForRequest (TimeSpan timeout)
  189. {
  190. throw new NotImplementedException ();
  191. }
  192. }
  193. }