AspNetReplyChannel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // AspNetReplyChannel.cs
  3. //
  4. // Author:
  5. // Ankit Jain <[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. using System.Web;
  36. namespace System.ServiceModel.Channels
  37. {
  38. internal class AspNetReplyChannel : HttpReplyChannel
  39. {
  40. AspNetChannelListener<IReplyChannel> listener;
  41. List<HttpContext> waiting = new List<HttpContext> ();
  42. HttpContext http_context;
  43. ManualResetEvent wait;
  44. public AspNetReplyChannel (AspNetChannelListener<IReplyChannel> listener)
  45. : base (listener)
  46. {
  47. this.listener = listener;
  48. }
  49. internal void CloseContext ()
  50. {
  51. if (http_context == null)
  52. return;
  53. try {
  54. ((AspNetListenerManager) listener.ListenerManager).HttpHandler.EndRequest (listener, http_context);
  55. } finally {
  56. http_context = null;
  57. }
  58. }
  59. void ShutdownPendingRequests ()
  60. {
  61. lock (waiting)
  62. foreach (HttpContext ctx in waiting)
  63. try {
  64. ((AspNetListenerManager) listener.ListenerManager).HttpHandler.EndRequest (listener, ctx);
  65. } catch {
  66. }
  67. }
  68. protected override void OnAbort ()
  69. {
  70. ShutdownPendingRequests ();
  71. CloseContext ();
  72. }
  73. protected override void OnClose (TimeSpan timeout)
  74. {
  75. base.OnClose (timeout);
  76. ShutdownPendingRequests ();
  77. CloseContext ();
  78. }
  79. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  80. {
  81. try {
  82. return TryReceiveRequestCore (timeout, out context);
  83. } catch (Exception ex) {
  84. // FIXME: log it
  85. Console.WriteLine ("AspNetReplyChannel caught an error: " + ex);
  86. throw;
  87. }
  88. }
  89. bool TryReceiveRequestCore (TimeSpan timeout, out RequestContext context)
  90. {
  91. context = null;
  92. if (waiting.Count == 0 && !WaitForRequest (timeout))
  93. return false;
  94. lock (waiting) {
  95. if (waiting.Count > 0) {
  96. http_context = waiting [0];
  97. waiting.RemoveAt (0);
  98. }
  99. }
  100. if (http_context == null)
  101. // Though as long as this instance is used
  102. // synchronously, it should not happen.
  103. return false;
  104. if (http_context.Response.StatusCode != 200) {
  105. http_context.Response.Close ();
  106. return false;
  107. }
  108. Message msg;
  109. var req = http_context.Request;
  110. if (req.HttpMethod == "GET") {
  111. msg = Message.CreateMessage (Encoder.MessageVersion, null);
  112. } else {
  113. //FIXME: Do above stuff for HttpContext ?
  114. int maxSizeOfHeaders = 0x10000;
  115. msg = Encoder.ReadMessage (
  116. req.InputStream, maxSizeOfHeaders);
  117. if (Encoder.MessageVersion.Envelope == EnvelopeVersion.Soap11) {
  118. string action = GetHeaderItem (req.Headers ["SOAPAction"]);
  119. if (action != null)
  120. msg.Headers.Action = action;
  121. }
  122. }
  123. // FIXME: prop.SuppressEntityBody
  124. msg.Headers.To = req.Url;
  125. msg.Properties.Add ("Via", LocalAddress.Uri);
  126. msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (req.HttpMethod, req.Url.Query, req.Headers));
  127. context = new AspNetRequestContext (this, msg, http_context);
  128. return true;
  129. }
  130. /*
  131. public override bool WaitForRequest (TimeSpan timeout)
  132. {
  133. if (http_context == null)
  134. http_context = listener.HttpHandler.WaitForRequest (listener, timeout);
  135. return http_context != null;
  136. }
  137. */
  138. public override bool WaitForRequest (TimeSpan timeout)
  139. {
  140. if (wait != null)
  141. throw new InvalidOperationException ("Another wait operation is in progress");
  142. try {
  143. var wait_ = new ManualResetEvent (false);
  144. wait = wait_;
  145. listener.ListenerManager.GetHttpContextAsync (timeout, HttpContextAcquired);
  146. return wait_.WaitOne (timeout, false) && waiting.Count > 0;
  147. } finally {
  148. wait = null;
  149. }
  150. }
  151. void HttpContextAcquired (HttpContextInfo ctx)
  152. {
  153. if (wait == null)
  154. throw new InvalidOperationException ("WaitForRequest operation has not started");
  155. var sctx = (AspNetHttpContextInfo) ctx;
  156. if (State == CommunicationState.Opened && ctx != null)
  157. waiting.Add (sctx.Source);
  158. var wait_ = wait;
  159. wait = null;
  160. wait_.Set ();
  161. }
  162. }
  163. }