HttpRequestContext.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // HttpRequestContext.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.IO;
  29. using System.Net;
  30. using System.Threading;
  31. namespace System.ServiceModel.Channels.Http
  32. {
  33. internal class HttpRequestContext : RequestContext
  34. {
  35. public HttpRequestContext (HttpReplyChannel channel, HttpContextInfo context, Message request)
  36. {
  37. if (channel == null)
  38. throw new ArgumentNullException ("channel");
  39. if (context == null)
  40. throw new ArgumentNullException ("context");
  41. if (request == null)
  42. throw new ArgumentNullException ("request");
  43. this.channel = channel;
  44. this.context = context;
  45. this.request = request;
  46. }
  47. Message request;
  48. HttpReplyChannel channel;
  49. HttpContextInfo context;
  50. public override Message RequestMessage {
  51. get { return request; }
  52. }
  53. public HttpReplyChannel Channel {
  54. get { return channel; }
  55. }
  56. public HttpContextInfo Context {
  57. get { return context; }
  58. }
  59. public override IAsyncResult BeginReply (
  60. Message msg, AsyncCallback callback, object state)
  61. {
  62. return BeginReply (msg,
  63. Channel.DefaultSendTimeout,
  64. callback, state);
  65. }
  66. Action<Message,TimeSpan> reply_delegate;
  67. public override IAsyncResult BeginReply (
  68. Message msg, TimeSpan timeout,
  69. AsyncCallback callback, object state)
  70. {
  71. if (reply_delegate == null)
  72. reply_delegate = new Action<Message,TimeSpan> (Reply);
  73. return reply_delegate.BeginInvoke (msg, timeout, callback, state);
  74. }
  75. public override void EndReply (IAsyncResult result)
  76. {
  77. if (result == null)
  78. throw new ArgumentNullException ("result");
  79. if (reply_delegate == null)
  80. throw new InvalidOperationException ("reply operation has not started");
  81. reply_delegate.EndInvoke (result);
  82. }
  83. public override void Reply (Message msg)
  84. {
  85. Reply (msg, Channel.DefaultSendTimeout);
  86. }
  87. public override void Reply (Message msg, TimeSpan timeout)
  88. {
  89. InternalReply (msg, timeout);
  90. }
  91. public override void Abort ()
  92. {
  93. InternalAbort ();
  94. }
  95. public override void Close ()
  96. {
  97. Close (Channel.DefaultSendTimeout);
  98. }
  99. public override void Close (TimeSpan timeout)
  100. {
  101. InternalClose (timeout);
  102. }
  103. // implementation internals
  104. protected virtual void InternalAbort ()
  105. {
  106. Context.Abort ();
  107. }
  108. protected virtual void InternalClose (TimeSpan timeout)
  109. {
  110. Context.Close ();
  111. }
  112. protected virtual void InternalReply (Message msg, TimeSpan timeout)
  113. {
  114. if (msg == null)
  115. throw new ArgumentNullException ("msg");
  116. Logger.LogMessage (MessageLogSourceKind.TransportSend, ref msg, Channel.Source.Source.MaxReceivedMessageSize);
  117. MemoryStream ms = new MemoryStream ();
  118. Channel.Encoder.WriteMessage (msg, ms);
  119. Context.Response.ContentType = Channel.Encoder.ContentType;
  120. string pname = HttpResponseMessageProperty.Name;
  121. bool suppressEntityBody = false;
  122. if (msg.Properties.ContainsKey (pname)) {
  123. HttpResponseMessageProperty hp = (HttpResponseMessageProperty) msg.Properties [pname];
  124. string contentType = hp.Headers ["Content-Type"];
  125. if (contentType != null)
  126. Context.Response.ContentType = contentType;
  127. Context.Response.Headers.Add (hp.Headers);
  128. if (hp.StatusCode != default (HttpStatusCode))
  129. Context.Response.StatusCode = (int) hp.StatusCode;
  130. Context.Response.StatusDescription = hp.StatusDescription;
  131. if (hp.SuppressEntityBody)
  132. suppressEntityBody = true;
  133. }
  134. if (msg.IsFault)
  135. Context.Response.StatusCode = 500;
  136. if (!suppressEntityBody) {
  137. Context.Response.SetLength (ms.Length);
  138. Context.Response.OutputStream.Write (ms.GetBuffer (), 0, (int) ms.Length);
  139. Context.Response.OutputStream.Flush ();
  140. }
  141. else
  142. Context.Response.SuppressContent = true;
  143. }
  144. }
  145. }