WebConnectionStream.cs 8.4 KB

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