HttpStandaloneReplyChannel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // HttpStandaloneReplyChannel.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 HttpStandaloneReplyChannel : HttpReplyChannel
  39. {
  40. HttpStandaloneChannelListener<IReplyChannel> source;
  41. RequestContext reqctx;
  42. public HttpStandaloneReplyChannel (HttpStandaloneChannelListener<IReplyChannel> listener)
  43. : base (listener)
  44. {
  45. this.source = listener;
  46. }
  47. protected override void OnAbort ()
  48. {
  49. AbortConnections (TimeSpan.Zero);
  50. base.OnAbort (); // FIXME: remove it. The base is wrong. But it is somehow required to not block some tests.
  51. }
  52. public override bool CancelAsync (TimeSpan timeout)
  53. {
  54. AbortConnections (timeout);
  55. // FIXME: this wait is sort of hack (because it should not be required), but without it some tests are blocked.
  56. // This hack even had better be moved to base.CancelAsync().
  57. if (CurrentAsyncResult != null)
  58. CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.FromMilliseconds (300));
  59. return base.CancelAsync (timeout);
  60. }
  61. void AbortConnections (TimeSpan timeout)
  62. {
  63. if (reqctx != null)
  64. reqctx.Close (timeout);
  65. }
  66. bool close_started;
  67. protected override void OnClose (TimeSpan timeout)
  68. {
  69. if (close_started)
  70. return;
  71. close_started = true;
  72. DateTime start = DateTime.Now;
  73. // FIXME: consider timeout
  74. AbortConnections (timeout - (DateTime.Now - start));
  75. base.OnClose (timeout - (DateTime.Now - start));
  76. }
  77. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  78. {
  79. context = null;
  80. HttpContextInfo ctxi;
  81. if (!source.ListenerManager.TryDequeueRequest (source.ChannelDispatcher, timeout, out ctxi))
  82. return false;
  83. if (ctxi == null)
  84. return true; // returning true, yet context is null. This happens at closing phase.
  85. var ctx = ((HttpStandaloneContextInfo) ctxi).Source;
  86. if (ctx.Response.StatusCode != 200) { // it's already invalid.
  87. ctx.Response.Close ();
  88. return false;
  89. }
  90. // FIXME: supply maxSizeOfHeaders.
  91. int maxSizeOfHeaders = 0x10000;
  92. Message msg = null;
  93. if (ctx.Request.HttpMethod == "POST") {
  94. if (!Encoder.IsContentTypeSupported (ctx.Request.ContentType)) {
  95. ctx.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
  96. ctx.Response.StatusDescription = String.Format (
  97. "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctx.Request.ContentType);
  98. ctx.Response.Close ();
  99. return false;
  100. }
  101. msg = Encoder.ReadMessage (
  102. ctx.Request.InputStream, maxSizeOfHeaders);
  103. if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
  104. MessageVersion.Addressing.Equals (AddressingVersion.None)) {
  105. string action = GetHeaderItem (ctx.Request.Headers ["SOAPAction"]);
  106. if (action != null) {
  107. if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
  108. action = action.Substring (1, action.Length - 2);
  109. msg.Headers.Action = action;
  110. }
  111. }
  112. } else if (ctx.Request.HttpMethod == "GET") {
  113. msg = Message.CreateMessage (MessageVersion.None, null); // HTTP GET-based request
  114. }
  115. if (msg.Headers.To == null)
  116. msg.Headers.To = ctx.Request.Url;
  117. msg.Properties.Add ("Via", LocalAddress.Uri);
  118. msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctx.Request.HttpMethod, ctx.Request.Url.Query, ctx.Request.Headers));
  119. context = new HttpStandaloneRequestContext (this, msg, ctx);
  120. reqctx = context;
  121. return true;
  122. }
  123. public override bool WaitForRequest (TimeSpan timeout)
  124. {
  125. throw new NotImplementedException ();
  126. }
  127. }
  128. internal abstract class HttpReplyChannel : InternalReplyChannelBase
  129. {
  130. HttpStandaloneChannelListener<IReplyChannel> source;
  131. protected HttpReplyChannel (HttpStandaloneChannelListener<IReplyChannel> listener)
  132. : base (listener)
  133. {
  134. this.source = listener;
  135. }
  136. public MessageEncoder Encoder {
  137. get { return source.MessageEncoder; }
  138. }
  139. internal MessageVersion MessageVersion {
  140. get { return source.MessageEncoder.MessageVersion; }
  141. }
  142. public override RequestContext ReceiveRequest (TimeSpan timeout)
  143. {
  144. RequestContext ctx;
  145. if (!TryReceiveRequest (timeout, out ctx))
  146. throw new TimeoutException ();
  147. return ctx;
  148. }
  149. protected override void OnOpen (TimeSpan timeout)
  150. {
  151. }
  152. protected string GetHeaderItem (string raw)
  153. {
  154. if (raw == null || raw.Length == 0)
  155. return raw;
  156. switch (raw [0]) {
  157. case '\'':
  158. case '"':
  159. if (raw [raw.Length - 1] == raw [0])
  160. return raw.Substring (1, raw.Length - 2);
  161. // FIXME: is it simply an error?
  162. break;
  163. }
  164. return raw;
  165. }
  166. protected HttpRequestMessageProperty CreateRequestProperty (string method, string query, NameValueCollection headers)
  167. {
  168. var prop = new HttpRequestMessageProperty ();
  169. prop.Method = method;
  170. prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
  171. // FIXME: prop.SuppressEntityBody
  172. prop.Headers.Add (headers);
  173. return prop;
  174. }
  175. }
  176. }