ResponseStream.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.Net.ResponseStream
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // Copyright (c) 2005 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. #if NET_2_0
  29. using System.IO;
  30. using System.Net.Sockets;
  31. using System.Text;
  32. using System.Runtime.InteropServices;
  33. namespace System.Net {
  34. // FIXME: Does this buffer the response until Close?
  35. // What happens when we set content-length to X and write X-1 bytes then close?
  36. // what if we don't set content-length at all?
  37. class ResponseStream : NetworkStream
  38. {
  39. HttpListenerResponse response;
  40. bool ignore_errors;
  41. bool disposed;
  42. bool trailer_sent;
  43. internal ResponseStream (Socket sock, HttpListenerResponse response, bool ignore_errors) :
  44. base (sock, false)
  45. {
  46. this.response = response;
  47. this.ignore_errors = ignore_errors;
  48. }
  49. public override bool CanRead {
  50. get { return false; }
  51. }
  52. public override bool CanSeek {
  53. get { return false; }
  54. }
  55. public override bool CanWrite {
  56. get { return true; }
  57. }
  58. public override long Length {
  59. get { throw new NotSupportedException (); }
  60. }
  61. public override long Position {
  62. get { throw new NotSupportedException (); }
  63. set { throw new NotSupportedException (); }
  64. }
  65. public override void Close ()
  66. {
  67. if (disposed == false) {
  68. disposed = true;
  69. if (response.SendChunked && !trailer_sent) {
  70. WriteChunkSize (0, true);
  71. trailer_sent = true;
  72. }
  73. response.Close ();
  74. }
  75. }
  76. public override void Flush ()
  77. {
  78. }
  79. static byte [] crlf = new byte [] { 13, 10 };
  80. void WriteChunkSize (int size, bool final)
  81. {
  82. string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
  83. byte [] b = Encoding.ASCII.GetBytes (str);
  84. base.Write (b, 0, b.Length);
  85. }
  86. internal void InternalWrite (byte [] buffer, int offset, int count)
  87. {
  88. if (ignore_errors) {
  89. try {
  90. base.Write (buffer, offset, count);
  91. } catch { }
  92. } else {
  93. base.Write (buffer, offset, count);
  94. }
  95. }
  96. public override void Write (byte [] buffer, int offset, int count)
  97. {
  98. if (disposed)
  99. throw new ObjectDisposedException (GetType ().ToString ());
  100. if (response.HeadersSent == false)
  101. response.SendHeaders ();
  102. bool chunked = response.SendChunked;
  103. try {
  104. if (chunked)
  105. WriteChunkSize (count, false);
  106. } catch { }
  107. InternalWrite (buffer, offset, count);
  108. try {
  109. if (chunked)
  110. base.Write (crlf, 0, 2);
  111. } catch { }
  112. }
  113. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
  114. AsyncCallback cback, object state)
  115. {
  116. if (disposed)
  117. throw new ObjectDisposedException (GetType ().ToString ());
  118. if (response.HeadersSent == false)
  119. response.SendHeaders ();
  120. try {
  121. if (response.SendChunked)
  122. WriteChunkSize (count, false);
  123. } catch { }
  124. return base.BeginWrite (buffer, offset, count, cback, state);
  125. }
  126. public override void EndWrite (IAsyncResult ares)
  127. {
  128. if (disposed)
  129. throw new ObjectDisposedException (GetType ().ToString ());
  130. if (ignore_errors) {
  131. try {
  132. base.EndWrite (ares);
  133. if (response.SendChunked)
  134. base.Write (crlf, 0, 2);
  135. } catch { }
  136. } else {
  137. base.EndWrite (ares);
  138. if (response.SendChunked)
  139. base.Write (crlf, 0, 2);
  140. }
  141. }
  142. public override int Read ([In,Out] byte[] buffer, int offset, int count)
  143. {
  144. throw new NotSupportedException ();
  145. }
  146. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  147. AsyncCallback cback, object state)
  148. {
  149. throw new NotSupportedException ();
  150. }
  151. public override int EndRead (IAsyncResult ares)
  152. {
  153. throw new NotSupportedException ();
  154. }
  155. public override long Seek (long offset, SeekOrigin origin)
  156. {
  157. throw new NotSupportedException ();
  158. }
  159. public override void SetLength (long value)
  160. {
  161. throw new NotSupportedException ();
  162. }
  163. }
  164. }
  165. #endif