WebConnectionStream.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. totalRead += copy;
  145. readBufferOffset += copy;
  146. offset += copy;
  147. size -= copy;
  148. if (size == 0 || totalRead >= contentLength) {
  149. result.SetCompleted (true, copy);
  150. result.DoCallback ();
  151. return result;
  152. }
  153. }
  154. result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, null, null);
  155. return result;
  156. }
  157. public override int EndRead (IAsyncResult r)
  158. {
  159. WebAsyncResult result = (WebAsyncResult) r;
  160. int nbytes = -1;
  161. if (result.IsCompleted) {
  162. nbytes = result.NBytes;
  163. } else {
  164. nbytes = cnc.EndRead (result.InnerAsyncResult);
  165. lock (this) {
  166. pendingReads--;
  167. if (pendingReads == 0)
  168. pending.Set ();
  169. }
  170. result.SetCompleted (false, nbytes);
  171. totalRead += nbytes;
  172. }
  173. if (totalRead >= contentLength && !nextReadCalled) {
  174. nextReadCalled = true;
  175. cnc.NextRead ();
  176. }
  177. return nbytes;
  178. }
  179. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  180. AsyncCallback cb, object state)
  181. {
  182. if (isRead)
  183. throw new NotSupportedException ("this stream does not allow writing");
  184. if (buffer == null)
  185. throw new ArgumentNullException ("buffer");
  186. int length = buffer.Length;
  187. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  188. throw new ArgumentOutOfRangeException ();
  189. WebAsyncResult result = new WebAsyncResult (cb, state);
  190. if (allowBuffering) {
  191. writeBuffer.Write (buffer, offset, size);
  192. result.SetCompleted (true, 0);
  193. result.DoCallback ();
  194. } else {
  195. result.InnerAsyncResult = cnc.BeginWrite (buffer, offset, size, cb, state);
  196. if (result.InnerAsyncResult == null)
  197. throw new WebException ("Aborted");
  198. }
  199. return result;
  200. }
  201. public override void EndWrite (IAsyncResult r)
  202. {
  203. if (r == null)
  204. throw new ArgumentNullException ("r");
  205. if (allowBuffering)
  206. return;
  207. WebAsyncResult result = r as WebAsyncResult;
  208. if (result == null)
  209. throw new ArgumentException ("Invalid IAsyncResult");
  210. cnc.EndWrite (result.InnerAsyncResult);
  211. return;
  212. }
  213. public override void Write (byte [] buffer, int offset, int size)
  214. {
  215. if (isRead)
  216. throw new NotSupportedException ("this stream does not allow writing");
  217. IAsyncResult res = BeginWrite (buffer, offset, size, null, null);
  218. EndWrite (res);
  219. }
  220. public override void Flush ()
  221. {
  222. }
  223. internal void SetHeaders (byte [] buffer, int offset, int size)
  224. {
  225. if (!allowBuffering) {
  226. Write (buffer, offset, size);
  227. } else {
  228. headers = new byte [size];
  229. Buffer.BlockCopy (buffer, offset, headers, 0, size);
  230. }
  231. }
  232. internal void WriteRequest ()
  233. {
  234. if (!allowBuffering || writeBuffer == null || requestWritten)
  235. return;
  236. byte [] bytes = writeBuffer.GetBuffer ();
  237. int length = (int) writeBuffer.Length;
  238. if (request.ContentLength != -1 && request.ContentLength < length) {
  239. throw new ProtocolViolationException ("Specified Content-Length is less than the " +
  240. "number of bytes to write");
  241. }
  242. request.InternalContentLength = length;
  243. request.SendRequestHeaders ();
  244. if (!cnc.WaitForContinue (headers, 0, headers.Length))
  245. return; // the error is reported in WebConnection (timeout or 417 or whatever)
  246. cnc.Write (bytes, 0, length);
  247. requestWritten = true;
  248. cnc.dataAvailable.Set ();
  249. }
  250. public override void Close ()
  251. {
  252. if (!allowBuffering)
  253. return;
  254. // may be ReadAll is isRead?
  255. long length = request.ContentLength;
  256. if (length != -1 && length > writeBuffer.Length)
  257. throw new IOException ("Cannot close the stream until all bytes are written");
  258. WriteRequest ();
  259. }
  260. internal void ResetWriteBuffer ()
  261. {
  262. if (!allowBuffering)
  263. return;
  264. writeBuffer = new MemoryStream ();
  265. requestWritten = false;
  266. }
  267. public override long Seek (long a, SeekOrigin b)
  268. {
  269. throw new NotSupportedException ();
  270. }
  271. public override void SetLength (long a)
  272. {
  273. throw new NotSupportedException ();
  274. }
  275. public override bool CanSeek {
  276. get { return false; }
  277. }
  278. public override bool CanRead {
  279. get { return isRead && (contentLength == Int32.MaxValue || totalRead < contentLength); }
  280. }
  281. public override bool CanWrite {
  282. get { return !isRead; }
  283. }
  284. public override long Length {
  285. get { throw new NotSupportedException (); }
  286. }
  287. public override long Position {
  288. get { throw new NotSupportedException (); }
  289. set { throw new NotSupportedException (); }
  290. }
  291. }
  292. }