HttpRequestContext.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // HttpRequestContext.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.IO;
  29. using System.Net;
  30. using System.Threading;
  31. namespace System.ServiceModel.Channels
  32. {
  33. internal class HttpRequestContext : RequestContext
  34. {
  35. class HttpRequestContextAsyncResult : IAsyncResult
  36. {
  37. AutoResetEvent wait;
  38. AsyncCallback callback;
  39. object state;
  40. bool done, waiting;
  41. TimeSpan timeout;
  42. Exception error;
  43. public HttpRequestContextAsyncResult (
  44. HttpRequestContext context,
  45. Message msg,
  46. TimeSpan timeout,
  47. AsyncCallback callback,
  48. object state)
  49. {
  50. this.timeout = timeout;
  51. this.wait = new AutoResetEvent (false);
  52. ThreadStart ts = delegate () {
  53. try {
  54. context.ProcessReply (msg, timeout);
  55. if (callback != null)
  56. callback (this);
  57. } catch (Exception ex) {
  58. error = ex;
  59. } finally {
  60. done = true;
  61. wait.Set ();
  62. }
  63. };
  64. Thread t = new Thread (ts);
  65. t.Start ();
  66. }
  67. public WaitHandle AsyncWaitHandle {
  68. get { return wait; }
  69. }
  70. public object AsyncState {
  71. get { return state; }
  72. }
  73. public bool CompletedSynchronously {
  74. get { return done && !waiting; }
  75. }
  76. public bool IsCompleted {
  77. get { return done; }
  78. }
  79. public void WaitEnd ()
  80. {
  81. if (!done) {
  82. waiting = true;
  83. wait.WaitOne (timeout, true);
  84. }
  85. if (error != null)
  86. throw error;
  87. }
  88. }
  89. Message msg;
  90. HttpListenerContext ctx;
  91. HttpReplyChannel channel;
  92. public HttpRequestContext (
  93. HttpReplyChannel channel,
  94. Message msg, HttpListenerContext ctx)
  95. {
  96. if (channel == null)
  97. throw new ArgumentNullException ("channel");
  98. if (msg == null)
  99. throw new ArgumentNullException ("msg");
  100. if (ctx == null)
  101. throw new ArgumentNullException ("ctx");
  102. this.channel = channel;
  103. this.msg = msg;
  104. this.ctx = ctx;
  105. }
  106. public override Message RequestMessage {
  107. get { return msg; }
  108. }
  109. public HttpReplyChannel Channel {
  110. get { return channel; }
  111. }
  112. public override void Abort ()
  113. {
  114. ctx.Response.Abort ();
  115. }
  116. public override IAsyncResult BeginReply (
  117. Message msg, AsyncCallback callback, object state)
  118. {
  119. return BeginReply (msg,
  120. channel.DefaultSendTimeout,
  121. callback, state);
  122. }
  123. public override IAsyncResult BeginReply (
  124. Message msg, TimeSpan timeout,
  125. AsyncCallback callback, object state)
  126. {
  127. return new HttpRequestContextAsyncResult (this, msg, timeout, callback, state);
  128. }
  129. public override void EndReply (IAsyncResult result)
  130. {
  131. if (result == null)
  132. throw new ArgumentNullException ("result");
  133. HttpRequestContextAsyncResult r = result as HttpRequestContextAsyncResult;
  134. if (r == null)
  135. throw new InvalidOperationException ("Wrong IAsyncResult");
  136. r.WaitEnd ();
  137. }
  138. public override void Reply (Message msg)
  139. {
  140. Reply (msg, channel.DefaultSendTimeout);
  141. }
  142. public override void Reply (Message msg, TimeSpan timeout)
  143. {
  144. ProcessReply (msg, timeout);
  145. }
  146. protected virtual void ProcessReply (Message msg, TimeSpan timeout)
  147. {
  148. if (msg == null)
  149. throw new ArgumentNullException ("msg");
  150. MemoryStream ms = new MemoryStream ();
  151. channel.Encoder.WriteMessage (msg, ms);
  152. ctx.Response.ContentType = channel.Encoder.ContentType;
  153. string pname = HttpResponseMessageProperty.Name;
  154. bool suppressEntityBody = false;
  155. if (msg.Properties.ContainsKey (pname)) {
  156. HttpResponseMessageProperty hp = (HttpResponseMessageProperty) msg.Properties [pname];
  157. string contentType = hp.Headers ["Content-Type"];
  158. if (contentType != null)
  159. ctx.Response.ContentType = contentType;
  160. ctx.Response.Headers.Add (hp.Headers);
  161. if (hp.StatusCode != default (HttpStatusCode))
  162. ctx.Response.StatusCode = (int) hp.StatusCode;
  163. ctx.Response.StatusDescription = hp.StatusDescription;
  164. if (hp.SuppressEntityBody)
  165. suppressEntityBody = true;
  166. }
  167. if (!suppressEntityBody) {
  168. ctx.Response.ContentLength64 = ms.Length;
  169. ctx.Response.OutputStream.Write (ms.GetBuffer (), 0, (int) ms.Length);
  170. ctx.Response.OutputStream.Flush ();
  171. }
  172. }
  173. public override void Close ()
  174. {
  175. Close (channel.DefaultSendTimeout);
  176. }
  177. public override void Close (TimeSpan timeout)
  178. {
  179. // FIXME: use timeout
  180. ctx.Response.Close ();
  181. }
  182. }
  183. }