RequestStream.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // RequestStream.cs
  3. // Copied from System.Net.RequestStream
  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.Runtime.InteropServices;
  34. namespace WebSocketSharp.Net {
  35. class RequestStream : Stream
  36. {
  37. byte [] buffer;
  38. int offset;
  39. int length;
  40. long remaining_body;
  41. bool disposed;
  42. System.IO.Stream stream;
  43. internal RequestStream (System.IO.Stream stream, byte [] buffer, int offset, int length)
  44. : this (stream, buffer, offset, length, -1)
  45. {
  46. }
  47. internal RequestStream (System.IO.Stream stream, byte [] buffer, int offset, int length, long contentlength)
  48. {
  49. this.stream = stream;
  50. this.buffer = buffer;
  51. this.offset = offset;
  52. this.length = length;
  53. this.remaining_body = contentlength;
  54. }
  55. public override bool CanRead {
  56. get { return true; }
  57. }
  58. public override bool CanSeek {
  59. get { return false; }
  60. }
  61. public override bool CanWrite {
  62. get { return false; }
  63. }
  64. public override long Length {
  65. get { throw new NotSupportedException (); }
  66. }
  67. public override long Position {
  68. get { throw new NotSupportedException (); }
  69. set { throw new NotSupportedException (); }
  70. }
  71. public override void Close ()
  72. {
  73. disposed = true;
  74. }
  75. public override void Flush ()
  76. {
  77. }
  78. // Returns 0 if we can keep reading from the base stream,
  79. // > 0 if we read something from the buffer.
  80. // -1 if we had a content length set and we finished reading that many bytes.
  81. int FillFromBuffer (byte [] buffer, int off, int count)
  82. {
  83. if (buffer == null)
  84. throw new ArgumentNullException ("buffer");
  85. if (off < 0)
  86. throw new ArgumentOutOfRangeException ("offset", "< 0");
  87. if (count < 0)
  88. throw new ArgumentOutOfRangeException ("count", "< 0");
  89. int len = buffer.Length;
  90. if (off > len)
  91. throw new ArgumentException ("destination offset is beyond array size");
  92. if (off > len - count)
  93. throw new ArgumentException ("Reading would overrun buffer");
  94. if (this.remaining_body == 0)
  95. return -1;
  96. if (this.length == 0)
  97. return 0;
  98. int size = Math.Min (this.length, count);
  99. if (this.remaining_body > 0)
  100. size = (int) Math.Min (size, this.remaining_body);
  101. if (this.offset > this.buffer.Length - size) {
  102. size = Math.Min (size, this.buffer.Length - this.offset);
  103. }
  104. if (size == 0)
  105. return 0;
  106. Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
  107. this.offset += size;
  108. this.length -= size;
  109. if (this.remaining_body > 0)
  110. remaining_body -= size;
  111. return size;
  112. }
  113. public override int Read ([In,Out] byte[] buffer, int offset, int count)
  114. {
  115. if (disposed)
  116. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  117. // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0
  118. int nread = FillFromBuffer (buffer, offset, count);
  119. if (nread == -1) { // No more bytes available (Content-Length)
  120. return 0;
  121. } else if (nread > 0) {
  122. return nread;
  123. }
  124. nread = stream.Read (buffer, offset, count);
  125. if (nread > 0 && remaining_body > 0)
  126. remaining_body -= nread;
  127. return nread;
  128. }
  129. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  130. AsyncCallback cback, object state)
  131. {
  132. if (disposed)
  133. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  134. int nread = FillFromBuffer (buffer, offset, count);
  135. if (nread > 0 || nread == -1) {
  136. HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
  137. ares.Buffer = buffer;
  138. ares.Offset = offset;
  139. ares.Count = count;
  140. ares.Callback = cback;
  141. ares.State = state;
  142. ares.SynchRead = nread;
  143. ares.Complete ();
  144. return ares;
  145. }
  146. // Avoid reading past the end of the request to allow
  147. // for HTTP pipelining
  148. if (remaining_body >= 0 && count > remaining_body)
  149. count = (int) Math.Min (Int32.MaxValue, remaining_body);
  150. return stream.BeginRead (buffer, offset, count, cback, state);
  151. }
  152. public override int EndRead (IAsyncResult ares)
  153. {
  154. if (disposed)
  155. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  156. if (ares == null)
  157. throw new ArgumentNullException ("async_result");
  158. if (ares is HttpStreamAsyncResult) {
  159. HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
  160. if (!ares.IsCompleted)
  161. ares.AsyncWaitHandle.WaitOne ();
  162. return r.SynchRead;
  163. }
  164. // Close on exception?
  165. int nread = stream.EndRead (ares);
  166. if (remaining_body > 0 && nread > 0)
  167. remaining_body -= nread;
  168. return nread;
  169. }
  170. public override long Seek (long offset, SeekOrigin origin)
  171. {
  172. throw new NotSupportedException ();
  173. }
  174. public override void SetLength (long value)
  175. {
  176. throw new NotSupportedException ();
  177. }
  178. public override void Write (byte[] buffer, int offset, int count)
  179. {
  180. throw new NotSupportedException ();
  181. }
  182. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
  183. AsyncCallback cback, object state)
  184. {
  185. throw new NotSupportedException ();
  186. }
  187. public override void EndWrite (IAsyncResult async_result)
  188. {
  189. throw new NotSupportedException ();
  190. }
  191. }
  192. }