HttpReplyChannel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // HttpReplyChannel.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 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.IO;
  31. using System.Net;
  32. using System.ServiceModel;
  33. using System.Text;
  34. using System.Threading;
  35. namespace System.ServiceModel.Channels
  36. {
  37. internal class HttpReplyChannel : ReplyChannelBase
  38. {
  39. HttpChannelListener<IReplyChannel> source;
  40. List<HttpListenerContext> waiting = new List<HttpListenerContext> ();
  41. TimeSpan timeout;
  42. EndpointAddress local_address;
  43. public HttpReplyChannel (HttpChannelListener<IReplyChannel> listener,
  44. TimeSpan timeout)
  45. : base (listener)
  46. {
  47. this.source = listener;
  48. this.timeout = timeout;
  49. }
  50. public MessageEncoder Encoder {
  51. get { return source.MessageEncoder; }
  52. }
  53. // FIXME: where is it set?
  54. public override EndpointAddress LocalAddress {
  55. get { return local_address; }
  56. }
  57. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  58. {
  59. context = null;
  60. if (waiting.Count == 0 && !WaitForRequest (timeout))
  61. return false;
  62. HttpListenerContext ctx = null;
  63. lock (waiting) {
  64. if (waiting.Count > 0) {
  65. ctx = waiting [0];
  66. waiting.RemoveAt (0);
  67. }
  68. }
  69. if (ctx == null)
  70. // Though as long as this instance is used
  71. // synchronously, it should not happen.
  72. return false;
  73. // FIXME: supply maxSizeOfHeaders.
  74. int maxSizeOfHeaders = 0x10000;
  75. Message msg = null;
  76. if (ctx.Request.HttpMethod == "POST") {
  77. if (!Encoder.IsContentTypeSupported (ctx.Request.ContentType)) {
  78. ctx.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
  79. ctx.Response.StatusDescription = String.Format (
  80. "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctx.Request.ContentType);
  81. ctx.Response.Close ();
  82. return false;
  83. }
  84. msg = Encoder.ReadMessage (
  85. ctx.Request.InputStream, maxSizeOfHeaders);
  86. if (source.MessageEncoder.MessageVersion.Envelope == EnvelopeVersion.Soap11 ||
  87. source.MessageEncoder.MessageVersion.Addressing == AddressingVersion.None) {
  88. string action = GetHeaderItem (ctx.Request.Headers ["SOAPAction"]);
  89. if (action != null)
  90. msg.Headers.Action = action;
  91. }
  92. } else if (ctx.Request.HttpMethod == "GET") {
  93. msg = Message.CreateMessage (source.MessageEncoder.MessageVersion, null);
  94. }
  95. msg.Headers.To = ctx.Request.Url;
  96. HttpRequestMessageProperty prop =
  97. new HttpRequestMessageProperty ();
  98. prop.Method = ctx.Request.HttpMethod;
  99. prop.QueryString = ctx.Request.Url.Query;
  100. if (prop.QueryString.StartsWith ("?"))
  101. prop.QueryString = prop.QueryString.Substring (1);
  102. // FIXME: prop.SuppressEntityBody
  103. prop.Headers.Add (ctx.Request.Headers);
  104. msg.Properties.Add (HttpRequestMessageProperty.Name, prop);
  105. /*
  106. MessageBuffer buf = msg.CreateBufferedCopy (0x10000);
  107. msg = buf.CreateMessage ();
  108. System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter (Console.Out);
  109. w.Formatting = System.Xml.Formatting.Indented;
  110. buf.CreateMessage ().WriteMessage (w);
  111. w.Close ();
  112. */
  113. context = new HttpRequestContext (this, msg, ctx);
  114. return true;
  115. }
  116. protected string GetHeaderItem (string raw)
  117. {
  118. if (raw == null || raw.Length == 0)
  119. return raw;
  120. switch (raw [0]) {
  121. case '\'':
  122. case '"':
  123. if (raw [raw.Length - 1] == raw [0])
  124. return raw.Substring (1, raw.Length - 2);
  125. // FIXME: is it simply an error?
  126. break;
  127. }
  128. return raw;
  129. }
  130. public override IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
  135. {
  136. throw new NotImplementedException ();
  137. }
  138. public override bool WaitForRequest (TimeSpan timeout)
  139. {
  140. AutoResetEvent wait = new AutoResetEvent (false);
  141. try {
  142. source.Http.BeginGetContext (HttpContextReceived, wait);
  143. } catch (HttpListenerException e) {
  144. if (e.ErrorCode == 0x80004005) // invalid handle. Happens during shutdown.
  145. while (true) Thread.Sleep (1000); // thread is about to be terminated.
  146. throw;
  147. } catch (ObjectDisposedException) { return false; }
  148. // FIXME: we might want to take other approaches.
  149. if (timeout.Ticks > int.MaxValue)
  150. timeout = TimeSpan.FromDays (20);
  151. return wait.WaitOne (timeout, false);
  152. }
  153. void HttpContextReceived (IAsyncResult result)
  154. {
  155. if (State == CommunicationState.Closing || State == CommunicationState.Closed)
  156. return;
  157. waiting.Add (source.Http.EndGetContext (result));
  158. AutoResetEvent wait = (AutoResetEvent) result.AsyncState;
  159. wait.Set ();
  160. }
  161. public override IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. public override bool EndWaitForRequest (IAsyncResult result)
  166. {
  167. throw new NotImplementedException ();
  168. }
  169. public override RequestContext ReceiveRequest (TimeSpan timeout)
  170. {
  171. RequestContext ctx;
  172. TryReceiveRequest (timeout, out ctx);
  173. return ctx;
  174. }
  175. public override IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. public override RequestContext EndReceiveRequest (IAsyncResult result)
  180. {
  181. throw new NotImplementedException ();
  182. }
  183. protected override void OnAbort ()
  184. {
  185. foreach (HttpListenerContext ctx in waiting)
  186. ctx.Request.InputStream.Close ();
  187. }
  188. protected override void OnClose (TimeSpan timeout)
  189. {
  190. // FIXME: consider timeout
  191. foreach (HttpListenerContext ctx in waiting)
  192. ctx.Request.InputStream.Close ();
  193. }
  194. protected override void OnOpen (TimeSpan timeout)
  195. {
  196. }
  197. }
  198. }