HttpRequestContext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 : HttpRequestContextBase
  34. {
  35. HttpListenerContext ctx;
  36. public HttpRequestContext (
  37. HttpReplyChannel channel,
  38. Message msg, HttpListenerContext ctx)
  39. : base (channel, msg)
  40. {
  41. if (ctx == null)
  42. throw new ArgumentNullException ("ctx");
  43. this.ctx = ctx;
  44. }
  45. public override void Abort ()
  46. {
  47. ctx.Response.Abort ();
  48. }
  49. protected override void ProcessReply (Message msg, TimeSpan timeout)
  50. {
  51. if (msg == null)
  52. throw new ArgumentNullException ("msg");
  53. // FIXME: probably in WebHttpBinding land, there should
  54. // be some additional code (probably IErrorHandler) that
  55. // treats DestinationUnreachable (and possibly any other)
  56. // errors as HTTP 400 or something appropriate.
  57. // I originally rewrote the HTTP status here, but it
  58. // was wrong.
  59. // FIXME: should this be done here?
  60. if (Channel.MessageVersion.Addressing.Equals (AddressingVersion.None))
  61. msg.Headers.Action = null; // prohibited
  62. MemoryStream ms = new MemoryStream ();
  63. Channel.Encoder.WriteMessage (msg, ms);
  64. ctx.Response.ContentType = Channel.Encoder.ContentType;
  65. string pname = HttpResponseMessageProperty.Name;
  66. bool suppressEntityBody = false;
  67. if (msg.Properties.ContainsKey (pname)) {
  68. HttpResponseMessageProperty hp = (HttpResponseMessageProperty) msg.Properties [pname];
  69. string contentType = hp.Headers ["Content-Type"];
  70. if (contentType != null)
  71. ctx.Response.ContentType = contentType;
  72. ctx.Response.Headers.Add (hp.Headers);
  73. if (hp.StatusCode != default (HttpStatusCode))
  74. ctx.Response.StatusCode = (int) hp.StatusCode;
  75. ctx.Response.StatusDescription = hp.StatusDescription;
  76. if (hp.SuppressEntityBody)
  77. suppressEntityBody = true;
  78. }
  79. if (msg.IsFault)
  80. ctx.Response.StatusCode = 500;
  81. if (!suppressEntityBody) {
  82. ctx.Response.ContentLength64 = ms.Length;
  83. ctx.Response.OutputStream.Write (ms.GetBuffer (), 0, (int) ms.Length);
  84. ctx.Response.OutputStream.Flush ();
  85. }
  86. }
  87. public override void Close (TimeSpan timeout)
  88. {
  89. ctx.Response.Close ();
  90. }
  91. }
  92. internal abstract class HttpRequestContextBase : RequestContext
  93. {
  94. Message request;
  95. HttpReplyChannel channel;
  96. public HttpRequestContextBase (
  97. HttpReplyChannel channel,
  98. Message request)
  99. {
  100. if (channel == null)
  101. throw new ArgumentNullException ("channel");
  102. if (request == null)
  103. throw new ArgumentNullException ("request");
  104. this.channel = channel;
  105. this.request = request;
  106. }
  107. public override Message RequestMessage {
  108. get { return request; }
  109. }
  110. public HttpReplyChannel Channel {
  111. get { return channel; }
  112. }
  113. protected abstract void ProcessReply (Message msg, TimeSpan timeout);
  114. public override IAsyncResult BeginReply (
  115. Message msg, AsyncCallback callback, object state)
  116. {
  117. return BeginReply (msg,
  118. channel.DefaultSendTimeout,
  119. callback, state);
  120. }
  121. Action<Message,TimeSpan> reply_delegate;
  122. public override IAsyncResult BeginReply (
  123. Message msg, TimeSpan timeout,
  124. AsyncCallback callback, object state)
  125. {
  126. if (reply_delegate == null)
  127. reply_delegate = new Action<Message,TimeSpan> (Reply);
  128. return reply_delegate.BeginInvoke (msg, timeout, callback, state);
  129. }
  130. public override void EndReply (IAsyncResult result)
  131. {
  132. if (result == null)
  133. throw new ArgumentNullException ("result");
  134. if (reply_delegate == null)
  135. throw new InvalidOperationException ("reply operation has not started");
  136. reply_delegate.EndInvoke (result);
  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. public override void Close ()
  147. {
  148. Close (Channel.DefaultSendTimeout);
  149. }
  150. }
  151. }