ResponseStream.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // ResponseStream.cs
  3. // Copied from System.Net.ResponseStream
  4. //
  5. // Author:
  6. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  7. //
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Text;
  34. using System.Runtime.InteropServices;
  35. namespace WebSocketSharp.Net {
  36. // FIXME: Does this buffer the response until Close?
  37. // Update: we send a single packet for the first non-chunked Write
  38. // What happens when we set content-length to X and write X-1 bytes then close?
  39. // what if we don't set content-length at all?
  40. class ResponseStream : Stream
  41. {
  42. HttpListenerResponse response;
  43. bool ignore_errors;
  44. bool disposed;
  45. bool trailer_sent;
  46. System.IO.Stream stream;
  47. internal ResponseStream (System.IO.Stream stream, HttpListenerResponse response, bool ignore_errors)
  48. {
  49. this.response = response;
  50. this.ignore_errors = ignore_errors;
  51. this.stream = stream;
  52. }
  53. public override bool CanRead {
  54. get { return false; }
  55. }
  56. public override bool CanSeek {
  57. get { return false; }
  58. }
  59. public override bool CanWrite {
  60. get { return true; }
  61. }
  62. public override long Length {
  63. get { throw new NotSupportedException (); }
  64. }
  65. public override long Position {
  66. get { throw new NotSupportedException (); }
  67. set { throw new NotSupportedException (); }
  68. }
  69. public override void Close ()
  70. {
  71. if (disposed == false) {
  72. disposed = true;
  73. byte [] bytes = null;
  74. MemoryStream ms = GetHeaders (true);
  75. bool chunked = response.SendChunked;
  76. if (ms != null) {
  77. long start = ms.Position;
  78. if (chunked && !trailer_sent) {
  79. bytes = GetChunkSizeBytes (0, true);
  80. ms.Position = ms.Length;
  81. ms.Write (bytes, 0, bytes.Length);
  82. }
  83. InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
  84. trailer_sent = true;
  85. } else if (chunked && !trailer_sent) {
  86. bytes = GetChunkSizeBytes (0, true);
  87. InternalWrite (bytes, 0, bytes.Length);
  88. trailer_sent = true;
  89. }
  90. response.Close ();
  91. }
  92. }
  93. MemoryStream GetHeaders (bool closing)
  94. {
  95. if (response.HeadersSent)
  96. return null;
  97. MemoryStream ms = new MemoryStream ();
  98. response.SendHeaders (closing, ms);
  99. return ms;
  100. }
  101. public override void Flush ()
  102. {
  103. }
  104. static byte [] crlf = new byte [] { 13, 10 };
  105. static byte [] GetChunkSizeBytes (int size, bool final)
  106. {
  107. string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
  108. return Encoding.ASCII.GetBytes (str);
  109. }
  110. internal void InternalWrite (byte [] buffer, int offset, int count)
  111. {
  112. if (ignore_errors) {
  113. try {
  114. stream.Write (buffer, offset, count);
  115. } catch { }
  116. } else {
  117. stream.Write (buffer, offset, count);
  118. }
  119. }
  120. public override void Write (byte [] buffer, int offset, int count)
  121. {
  122. if (disposed)
  123. throw new ObjectDisposedException (GetType ().ToString ());
  124. byte [] bytes = null;
  125. MemoryStream ms = GetHeaders (false);
  126. bool chunked = response.SendChunked;
  127. if (ms != null) {
  128. long start = ms.Position; // After the possible preamble for the encoding
  129. ms.Position = ms.Length;
  130. if (chunked) {
  131. bytes = GetChunkSizeBytes (count, false);
  132. ms.Write (bytes, 0, bytes.Length);
  133. }
  134. int new_count = Math.Min (count, 16384 - (int) ms.Position + (int) start);
  135. ms.Write (buffer, offset, new_count);
  136. count -= new_count;
  137. offset += new_count;
  138. InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
  139. ms.SetLength (0);
  140. ms.Capacity = 0; // 'dispose' the buffer in ms.
  141. } else if (chunked) {
  142. bytes = GetChunkSizeBytes (count, false);
  143. InternalWrite (bytes, 0, bytes.Length);
  144. }
  145. if (count > 0)
  146. InternalWrite (buffer, offset, count);
  147. if (chunked)
  148. InternalWrite (crlf, 0, 2);
  149. }
  150. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
  151. AsyncCallback cback, object state)
  152. {
  153. if (disposed)
  154. throw new ObjectDisposedException (GetType ().ToString ());
  155. byte [] bytes = null;
  156. MemoryStream ms = GetHeaders (false);
  157. bool chunked = response.SendChunked;
  158. if (ms != null) {
  159. long start = ms.Position;
  160. ms.Position = ms.Length;
  161. if (chunked) {
  162. bytes = GetChunkSizeBytes (count, false);
  163. ms.Write (bytes, 0, bytes.Length);
  164. }
  165. ms.Write (buffer, offset, count);
  166. buffer = ms.GetBuffer ();
  167. offset = (int) start;
  168. count = (int) (ms.Position - start);
  169. } else if (chunked) {
  170. bytes = GetChunkSizeBytes (count, false);
  171. InternalWrite (bytes, 0, bytes.Length);
  172. }
  173. return stream.BeginWrite (buffer, offset, count, cback, state);
  174. }
  175. public override void EndWrite (IAsyncResult ares)
  176. {
  177. if (disposed)
  178. throw new ObjectDisposedException (GetType ().ToString ());
  179. if (ignore_errors) {
  180. try {
  181. stream.EndWrite (ares);
  182. if (response.SendChunked)
  183. stream.Write (crlf, 0, 2);
  184. } catch { }
  185. } else {
  186. stream.EndWrite (ares);
  187. if (response.SendChunked)
  188. stream.Write (crlf, 0, 2);
  189. }
  190. }
  191. public override int Read ([In,Out] byte[] buffer, int offset, int count)
  192. {
  193. throw new NotSupportedException ();
  194. }
  195. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  196. AsyncCallback cback, object state)
  197. {
  198. throw new NotSupportedException ();
  199. }
  200. public override int EndRead (IAsyncResult ares)
  201. {
  202. throw new NotSupportedException ();
  203. }
  204. public override long Seek (long offset, SeekOrigin origin)
  205. {
  206. throw new NotSupportedException ();
  207. }
  208. public override void SetLength (long value)
  209. {
  210. throw new NotSupportedException ();
  211. }
  212. }
  213. }