WebConnectionStream.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // System.Net.WebConnectionStream
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.IO;
  10. using System.Threading;
  11. namespace System.Net
  12. {
  13. class WebConnectionStream : Stream
  14. {
  15. bool isRead;
  16. WebConnection cnc;
  17. HttpWebRequest request;
  18. byte [] readBuffer;
  19. int readBufferOffset;
  20. int readBufferSize;
  21. int contentLength;
  22. int totalRead;
  23. bool nextReadCalled;
  24. int pendingReads;
  25. ManualResetEvent pending;
  26. bool allowBuffering;
  27. bool sendChunked;
  28. MemoryStream writeBuffer;
  29. bool requestWritten;
  30. byte [] headers;
  31. public WebConnectionStream (WebConnection cnc)
  32. {
  33. isRead = true;
  34. pending = new ManualResetEvent (true);
  35. this.cnc = cnc;
  36. try {
  37. contentLength = Int32.Parse (cnc.Data.Headers ["Content-Length"]);
  38. } catch {
  39. contentLength = Int32.MaxValue;
  40. }
  41. }
  42. public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
  43. {
  44. isRead = false;
  45. this.cnc = cnc;
  46. this.request = request;
  47. allowBuffering = request.InternalAllowBuffering;
  48. sendChunked = request.SendChunked;
  49. if (allowBuffering)
  50. writeBuffer = new MemoryStream ();
  51. }
  52. internal byte [] ReadBuffer {
  53. set { readBuffer = value; }
  54. }
  55. internal int ReadBufferOffset {
  56. set { readBufferOffset = value;}
  57. }
  58. internal int ReadBufferSize {
  59. set { readBufferSize = value; }
  60. }
  61. internal void CheckComplete ()
  62. {
  63. if (readBufferSize - readBufferOffset == contentLength) {
  64. nextReadCalled = true;
  65. cnc.NextRead ();
  66. }
  67. }
  68. internal void ReadAll ()
  69. {
  70. if (!isRead || totalRead >= contentLength || nextReadCalled)
  71. return;
  72. pending.WaitOne ();
  73. lock (this) {
  74. if (totalRead >= contentLength)
  75. return;
  76. byte [] b = null;
  77. int diff = readBufferSize - readBufferOffset;
  78. int new_size;
  79. if (contentLength == Int32.MaxValue) {
  80. MemoryStream ms = new MemoryStream ();
  81. if (readBuffer != null && diff > 0)
  82. ms.Write (readBuffer, readBufferOffset, diff);
  83. byte [] buffer = new byte [2048];
  84. int read;
  85. while ((read = cnc.Read (buffer, 0, 2048)) != 0)
  86. ms.Write (buffer, 0, read);
  87. b = ms.GetBuffer ();
  88. new_size = (int) ms.Length;
  89. } else {
  90. new_size = contentLength - totalRead;
  91. b = new byte [new_size];
  92. if (readBuffer != null && diff > 0)
  93. Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
  94. int remaining = new_size - diff;
  95. int r = -1;
  96. while (remaining > 0 && r != 0) {
  97. r = cnc.Read (b, diff, remaining);
  98. remaining -= r;
  99. diff += r;
  100. }
  101. }
  102. readBuffer = b;
  103. readBufferOffset = 0;
  104. readBufferSize = new_size;
  105. contentLength = new_size;
  106. totalRead = 0;
  107. nextReadCalled = true;
  108. }
  109. cnc.NextRead ();
  110. }
  111. public override int Read (byte [] buffer, int offset, int size)
  112. {
  113. if (!isRead)
  114. throw new NotSupportedException ("this stream does not allow reading");
  115. if (totalRead >= contentLength)
  116. return 0;
  117. IAsyncResult res = BeginRead (buffer, offset, size, null, null);
  118. return EndRead (res);
  119. }
  120. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  121. AsyncCallback cb, object state)
  122. {
  123. if (!isRead)
  124. throw new NotSupportedException ("this stream does not allow reading");
  125. if (buffer == null)
  126. throw new ArgumentNullException ("buffer");
  127. int length = buffer.Length;
  128. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  129. throw new ArgumentOutOfRangeException ();
  130. lock (this) {
  131. pendingReads++;
  132. pending.Reset ();
  133. }
  134. WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
  135. if (totalRead >= contentLength) {
  136. result.SetCompleted (true, 0);
  137. result.DoCallback ();
  138. return result;
  139. }
  140. int remaining = readBufferSize - readBufferOffset;
  141. if (remaining > 0) {
  142. int copy = (remaining > size) ? size : remaining;
  143. Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
  144. readBufferOffset += copy;
  145. offset += copy;
  146. size -= copy;
  147. if (size == 0 || totalRead >= contentLength) {
  148. totalRead += copy;
  149. result.SetCompleted (true, copy);
  150. result.DoCallback ();
  151. return result;
  152. }
  153. result.NBytes = copy;
  154. }
  155. result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, null, null);
  156. return result;
  157. }
  158. public override int EndRead (IAsyncResult r)
  159. {
  160. WebAsyncResult result = (WebAsyncResult) r;
  161. int nbytes = -1;
  162. if (result.IsCompleted) {
  163. nbytes = result.NBytes;
  164. } else {
  165. nbytes = cnc.EndRead (result.InnerAsyncResult);
  166. lock (this) {
  167. pendingReads--;
  168. if (pendingReads == 0)
  169. pending.Set ();
  170. }
  171. nbytes += result.NBytes; // partially filled from the read buffer
  172. result.SetCompleted (false, nbytes);
  173. totalRead += nbytes;
  174. }
  175. if (totalRead >= contentLength && !nextReadCalled) {
  176. nextReadCalled = true;
  177. cnc.NextRead ();
  178. }
  179. return nbytes;
  180. }
  181. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  182. AsyncCallback cb, object state)
  183. {
  184. if (isRead)
  185. throw new NotSupportedException ("this stream does not allow writing");
  186. if (buffer == null)
  187. throw new ArgumentNullException ("buffer");
  188. int length = buffer.Length;
  189. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  190. throw new ArgumentOutOfRangeException ();
  191. WebAsyncResult result = new WebAsyncResult (cb, state);
  192. if (allowBuffering) {
  193. writeBuffer.Write (buffer, offset, size);
  194. result.SetCompleted (true, 0);
  195. result.DoCallback ();
  196. } else {
  197. result.InnerAsyncResult = cnc.BeginWrite (buffer, offset, size, cb, state);
  198. if (result.InnerAsyncResult == null)
  199. throw new WebException ("Aborted");
  200. }
  201. return result;
  202. }
  203. public override void EndWrite (IAsyncResult r)
  204. {
  205. if (r == null)
  206. throw new ArgumentNullException ("r");
  207. if (allowBuffering)
  208. return;
  209. WebAsyncResult result = r as WebAsyncResult;
  210. if (result == null)
  211. throw new ArgumentException ("Invalid IAsyncResult");
  212. cnc.EndWrite (result.InnerAsyncResult);
  213. return;
  214. }
  215. public override void Write (byte [] buffer, int offset, int size)
  216. {
  217. if (isRead)
  218. throw new NotSupportedException ("this stream does not allow writing");
  219. IAsyncResult res = BeginWrite (buffer, offset, size, null, null);
  220. EndWrite (res);
  221. }
  222. public override void Flush ()
  223. {
  224. }
  225. internal void SetHeaders (byte [] buffer, int offset, int size)
  226. {
  227. if (!allowBuffering) {
  228. Write (buffer, offset, size);
  229. } else {
  230. headers = new byte [size];
  231. Buffer.BlockCopy (buffer, offset, headers, 0, size);
  232. }
  233. }
  234. internal void WriteRequest ()
  235. {
  236. if (!allowBuffering || writeBuffer == null || requestWritten)
  237. return;
  238. byte [] bytes = writeBuffer.GetBuffer ();
  239. int length = (int) writeBuffer.Length;
  240. if (request.ContentLength != -1 && request.ContentLength < length) {
  241. throw new ProtocolViolationException ("Specified Content-Length is less than the " +
  242. "number of bytes to write");
  243. }
  244. request.InternalContentLength = length;
  245. request.SendRequestHeaders ();
  246. cnc.WaitForContinue (headers, 0, headers.Length);
  247. if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
  248. return;
  249. cnc.Write (bytes, 0, length);
  250. requestWritten = true;
  251. cnc.dataAvailable.Set ();
  252. }
  253. public override void Close ()
  254. {
  255. if (!allowBuffering)
  256. return;
  257. // may be ReadAll is isRead?
  258. long length = request.ContentLength;
  259. if (length != -1 && length > writeBuffer.Length)
  260. throw new IOException ("Cannot close the stream until all bytes are written");
  261. WriteRequest ();
  262. }
  263. internal void ResetWriteBuffer ()
  264. {
  265. if (!allowBuffering)
  266. return;
  267. writeBuffer = new MemoryStream ();
  268. requestWritten = false;
  269. }
  270. public override long Seek (long a, SeekOrigin b)
  271. {
  272. throw new NotSupportedException ();
  273. }
  274. public override void SetLength (long a)
  275. {
  276. throw new NotSupportedException ();
  277. }
  278. public override bool CanSeek {
  279. get { return false; }
  280. }
  281. public override bool CanRead {
  282. get { return isRead && (contentLength == Int32.MaxValue || totalRead < contentLength); }
  283. }
  284. public override bool CanWrite {
  285. get { return !isRead; }
  286. }
  287. public override long Length {
  288. get { throw new NotSupportedException (); }
  289. }
  290. public override long Position {
  291. get { throw new NotSupportedException (); }
  292. set { throw new NotSupportedException (); }
  293. }
  294. }
  295. }