WebConnectionStream.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. // (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. namespace System.Net
  34. {
  35. class WebConnectionStream : Stream
  36. {
  37. static byte [] crlf = new byte [] { 13, 10 };
  38. bool isRead;
  39. WebConnection cnc;
  40. HttpWebRequest request;
  41. byte [] readBuffer;
  42. int readBufferOffset;
  43. int readBufferSize;
  44. int contentLength;
  45. int totalRead;
  46. bool nextReadCalled;
  47. int pendingReads;
  48. int pendingWrites;
  49. ManualResetEvent pending;
  50. bool allowBuffering;
  51. bool sendChunked;
  52. MemoryStream writeBuffer;
  53. bool requestWritten;
  54. byte [] headers;
  55. bool disposed;
  56. bool headersSent;
  57. object locker = new object ();
  58. bool initRead;
  59. bool read_eof;
  60. bool complete_request_written;
  61. long max_buffer_size;
  62. public WebConnectionStream (WebConnection cnc)
  63. {
  64. isRead = true;
  65. pending = new ManualResetEvent (true);
  66. this.request = cnc.Data.request;
  67. this.cnc = cnc;
  68. string contentType = cnc.Data.Headers ["Transfer-Encoding"];
  69. bool chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
  70. string clength = cnc.Data.Headers ["Content-Length"];
  71. if (!chunkedRead && clength != null && clength != "") {
  72. try {
  73. contentLength = Int32.Parse (clength);
  74. if (contentLength == 0) {
  75. ReadAll ();
  76. }
  77. } catch {
  78. contentLength = Int32.MaxValue;
  79. }
  80. } else {
  81. contentLength = Int32.MaxValue;
  82. }
  83. }
  84. public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
  85. {
  86. isRead = false;
  87. this.cnc = cnc;
  88. this.request = request;
  89. allowBuffering = request.InternalAllowBuffering;
  90. sendChunked = request.SendChunked;
  91. if (allowBuffering) {
  92. writeBuffer = new MemoryStream ();
  93. max_buffer_size = request.ContentLength;
  94. } else {
  95. max_buffer_size = -1;
  96. }
  97. if (sendChunked)
  98. pending = new ManualResetEvent (true);
  99. }
  100. internal WebConnection Connection {
  101. get { return cnc; }
  102. }
  103. #if NET_2_0
  104. public override bool CanTimeout {
  105. get { return true; }
  106. }
  107. #endif
  108. internal bool CompleteRequestWritten {
  109. get { return complete_request_written; }
  110. }
  111. internal bool SendChunked {
  112. set { sendChunked = value; }
  113. }
  114. internal byte [] ReadBuffer {
  115. set { readBuffer = value; }
  116. }
  117. internal int ReadBufferOffset {
  118. set { readBufferOffset = value;}
  119. }
  120. internal int ReadBufferSize {
  121. set { readBufferSize = value; }
  122. }
  123. internal byte[] WriteBuffer {
  124. get { return writeBuffer.GetBuffer (); }
  125. }
  126. internal int WriteBufferLength {
  127. get { return (int) writeBuffer.Length; }
  128. }
  129. internal void ForceCompletion ()
  130. {
  131. nextReadCalled = true;
  132. cnc.NextRead ();
  133. }
  134. internal void CheckComplete ()
  135. {
  136. bool nrc = nextReadCalled;
  137. if (!nrc && readBufferSize - readBufferOffset == contentLength) {
  138. nextReadCalled = true;
  139. cnc.NextRead ();
  140. }
  141. }
  142. internal void ReadAll ()
  143. {
  144. if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
  145. if (isRead && !nextReadCalled) {
  146. nextReadCalled = true;
  147. cnc.NextRead ();
  148. }
  149. return;
  150. }
  151. pending.WaitOne ();
  152. lock (locker) {
  153. if (totalRead >= contentLength)
  154. return;
  155. byte [] b = null;
  156. int diff = readBufferSize - readBufferOffset;
  157. int new_size;
  158. if (contentLength == Int32.MaxValue) {
  159. MemoryStream ms = new MemoryStream ();
  160. byte [] buffer = null;
  161. if (readBuffer != null && diff > 0) {
  162. ms.Write (readBuffer, readBufferOffset, diff);
  163. if (readBufferSize >= 8192)
  164. buffer = readBuffer;
  165. }
  166. if (buffer == null)
  167. buffer = new byte [8192];
  168. int read;
  169. while ((read = cnc.Read (buffer, 0, buffer.Length)) != 0)
  170. ms.Write (buffer, 0, read);
  171. b = ms.GetBuffer ();
  172. new_size = (int) ms.Length;
  173. contentLength = new_size;
  174. } else {
  175. new_size = contentLength - totalRead;
  176. b = new byte [new_size];
  177. if (readBuffer != null && diff > 0) {
  178. if (diff > new_size)
  179. diff = new_size;
  180. Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
  181. }
  182. int remaining = new_size - diff;
  183. int r = -1;
  184. while (remaining > 0 && r != 0) {
  185. r = cnc.Read (b, diff, remaining);
  186. remaining -= r;
  187. diff += r;
  188. }
  189. }
  190. readBuffer = b;
  191. readBufferOffset = 0;
  192. readBufferSize = new_size;
  193. totalRead = 0;
  194. nextReadCalled = true;
  195. }
  196. cnc.NextRead ();
  197. }
  198. void WriteCallbackWrapper (IAsyncResult r)
  199. {
  200. WebAsyncResult result;
  201. if (r.AsyncState != null) {
  202. result = (WebAsyncResult) r.AsyncState;
  203. result.InnerAsyncResult = r;
  204. result.DoCallback ();
  205. } else {
  206. EndWrite (r);
  207. }
  208. }
  209. void ReadCallbackWrapper (IAsyncResult r)
  210. {
  211. WebAsyncResult result;
  212. if (r.AsyncState != null) {
  213. result = (WebAsyncResult) r.AsyncState;
  214. result.InnerAsyncResult = r;
  215. result.DoCallback ();
  216. } else {
  217. EndRead (r);
  218. }
  219. }
  220. public override int Read (byte [] buffer, int offset, int size)
  221. {
  222. if (!isRead)
  223. throw new NotSupportedException ("this stream does not allow reading");
  224. if (totalRead >= contentLength)
  225. return 0;
  226. AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
  227. WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
  228. if (!res.IsCompleted && !res.WaitUntilComplete (request.ReadWriteTimeout, false)) {
  229. nextReadCalled = true;
  230. cnc.Close (true);
  231. throw new WebException ("The operation has timed out.",
  232. WebExceptionStatus.Timeout);
  233. }
  234. return EndRead (res);
  235. }
  236. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  237. AsyncCallback cb, object state)
  238. {
  239. if (!isRead)
  240. throw new NotSupportedException ("this stream does not allow reading");
  241. if (buffer == null)
  242. throw new ArgumentNullException ("buffer");
  243. int length = buffer.Length;
  244. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  245. throw new ArgumentOutOfRangeException ();
  246. lock (locker) {
  247. pendingReads++;
  248. pending.Reset ();
  249. }
  250. WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
  251. if (totalRead >= contentLength) {
  252. result.SetCompleted (true, -1);
  253. result.DoCallback ();
  254. return result;
  255. }
  256. int remaining = readBufferSize - readBufferOffset;
  257. if (remaining > 0) {
  258. int copy = (remaining > size) ? size : remaining;
  259. Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
  260. readBufferOffset += copy;
  261. offset += copy;
  262. size -= copy;
  263. totalRead += copy;
  264. if (size == 0 || totalRead >= contentLength) {
  265. result.SetCompleted (true, copy);
  266. result.DoCallback ();
  267. return result;
  268. }
  269. result.NBytes = copy;
  270. }
  271. if (cb != null)
  272. cb = new AsyncCallback (ReadCallbackWrapper);
  273. if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
  274. size = contentLength - totalRead;
  275. if (!read_eof) {
  276. result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, cb, result);
  277. } else {
  278. result.SetCompleted (true, result.NBytes);
  279. result.DoCallback ();
  280. }
  281. return result;
  282. }
  283. public override int EndRead (IAsyncResult r)
  284. {
  285. WebAsyncResult result = (WebAsyncResult) r;
  286. if (result.EndCalled) {
  287. int xx = result.NBytes;
  288. return (xx >= 0) ? xx : 0;
  289. }
  290. result.EndCalled = true;
  291. if (!result.IsCompleted) {
  292. int nbytes = -1;
  293. try {
  294. nbytes = cnc.EndRead (result);
  295. } catch (Exception exc) {
  296. lock (locker) {
  297. pendingReads--;
  298. if (pendingReads == 0)
  299. pending.Set ();
  300. }
  301. nextReadCalled = true;
  302. cnc.Close (true);
  303. result.SetCompleted (false, exc);
  304. throw;
  305. }
  306. if (nbytes < 0) {
  307. nbytes = 0;
  308. read_eof = true;
  309. }
  310. totalRead += nbytes;
  311. result.SetCompleted (false, nbytes + result.NBytes);
  312. result.DoCallback ();
  313. if (nbytes == 0)
  314. contentLength = totalRead;
  315. }
  316. lock (locker) {
  317. pendingReads--;
  318. if (pendingReads == 0)
  319. pending.Set ();
  320. }
  321. if (totalRead >= contentLength && !nextReadCalled)
  322. ReadAll ();
  323. int nb = result.NBytes;
  324. return (nb >= 0) ? nb : 0;
  325. }
  326. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  327. AsyncCallback cb, object state)
  328. {
  329. if (isRead)
  330. throw new NotSupportedException ("this stream does not allow writing");
  331. if (buffer == null)
  332. throw new ArgumentNullException ("buffer");
  333. int length = buffer.Length;
  334. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  335. throw new ArgumentOutOfRangeException ();
  336. if (sendChunked) {
  337. lock (locker) {
  338. pendingWrites++;
  339. pending.Reset ();
  340. }
  341. }
  342. WebAsyncResult result = new WebAsyncResult (cb, state);
  343. if (allowBuffering) {
  344. if (max_buffer_size >= 0) {
  345. long avail = max_buffer_size - writeBuffer.Length;
  346. if (size > avail) {
  347. if (requestWritten)
  348. throw new ProtocolViolationException (
  349. "The number of bytes to be written is greater than " +
  350. "the specified ContentLength.");
  351. }
  352. }
  353. writeBuffer.Write (buffer, offset, size);
  354. if (!sendChunked) {
  355. result.SetCompleted (true, 0);
  356. result.DoCallback ();
  357. return result;
  358. }
  359. }
  360. AsyncCallback callback = null;
  361. if (cb != null)
  362. callback = new AsyncCallback (WriteCallbackWrapper);
  363. if (sendChunked) {
  364. WriteRequest ();
  365. string cSize = String.Format ("{0:X}\r\n", size);
  366. byte [] head = Encoding.ASCII.GetBytes (cSize);
  367. int chunkSize = 2 + size + head.Length;
  368. byte [] newBuffer = new byte [chunkSize];
  369. Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
  370. Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
  371. Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
  372. buffer = newBuffer;
  373. offset = 0;
  374. size = chunkSize;
  375. }
  376. result.InnerAsyncResult = cnc.BeginWrite (buffer, offset, size, callback, result);
  377. return result;
  378. }
  379. public override void EndWrite (IAsyncResult r)
  380. {
  381. if (r == null)
  382. throw new ArgumentNullException ("r");
  383. WebAsyncResult result = r as WebAsyncResult;
  384. if (result == null)
  385. throw new ArgumentException ("Invalid IAsyncResult");
  386. if (result.EndCalled)
  387. return;
  388. result.EndCalled = true;
  389. if (allowBuffering && !sendChunked)
  390. return;
  391. if (result.GotException)
  392. throw result.Exception;
  393. try {
  394. cnc.EndWrite (result.InnerAsyncResult);
  395. result.SetCompleted (false, 0);
  396. } catch (Exception e) {
  397. result.SetCompleted (false, e);
  398. }
  399. if (sendChunked) {
  400. lock (locker) {
  401. pendingWrites--;
  402. if (pendingWrites == 0)
  403. pending.Set ();
  404. }
  405. }
  406. }
  407. public override void Write (byte [] buffer, int offset, int size)
  408. {
  409. if (isRead)
  410. throw new NotSupportedException ("This stream does not allow writing");
  411. AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
  412. WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
  413. if (!res.IsCompleted && !res.WaitUntilComplete (request.ReadWriteTimeout, false)) {
  414. nextReadCalled = true;
  415. cnc.Close (true);
  416. throw new IOException ("Write timed out.");
  417. }
  418. EndWrite (res);
  419. }
  420. public override void Flush ()
  421. {
  422. }
  423. internal void SetHeaders (byte [] buffer, int offset, int size)
  424. {
  425. if (headersSent)
  426. return;
  427. if (!allowBuffering || sendChunked) {
  428. headersSent = true;
  429. if (!cnc.Connected)
  430. throw new WebException ("Not connected", null, WebExceptionStatus.SendFailure, null);
  431. if (!cnc.Write (buffer, offset, size))
  432. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  433. if (!initRead) {
  434. initRead = true;
  435. WebConnection.InitRead (cnc);
  436. }
  437. } else {
  438. headers = new byte [size];
  439. Buffer.BlockCopy (buffer, offset, headers, 0, size);
  440. }
  441. }
  442. internal bool RequestWritten {
  443. get { return requestWritten; }
  444. }
  445. internal void WriteRequest ()
  446. {
  447. if (requestWritten)
  448. return;
  449. if (sendChunked) {
  450. request.SendRequestHeaders ();
  451. requestWritten = true;
  452. return;
  453. }
  454. if (!allowBuffering || writeBuffer == null)
  455. return;
  456. byte [] bytes = writeBuffer.GetBuffer ();
  457. int length = (int) writeBuffer.Length;
  458. if (request.ContentLength != -1 && request.ContentLength < length) {
  459. throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
  460. WebExceptionStatus.ServerProtocolViolation, null);
  461. }
  462. request.InternalContentLength = length;
  463. request.SendRequestHeaders ();
  464. requestWritten = true;
  465. //
  466. // For small requests, make a copy, it will reduce the traffic, for large
  467. // requests, the NoDelay bit on the socket should take effect (set in WebConnection).
  468. //
  469. if (headers.Length + length < 8192){
  470. byte[] b = new byte [headers.Length + length];
  471. Buffer.BlockCopy (headers, 0, b, 0, headers.Length);
  472. Buffer.BlockCopy (bytes, 0, b, headers.Length, length);
  473. if (!cnc.Write (b, 0, b.Length))
  474. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  475. headersSent = true;
  476. complete_request_written = true;
  477. if (!initRead) {
  478. initRead = true;
  479. WebConnection.InitRead (cnc);
  480. }
  481. } else {
  482. if (!cnc.Write (headers, 0, headers.Length))
  483. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  484. headersSent = true;
  485. if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
  486. return;
  487. IAsyncResult result = null;
  488. if (length > 0)
  489. result = cnc.BeginWrite (bytes, 0, length, null, null);
  490. if (!initRead) {
  491. initRead = true;
  492. WebConnection.InitRead (cnc);
  493. }
  494. if (length > 0)
  495. complete_request_written = cnc.EndWrite (result);
  496. else
  497. complete_request_written = true;
  498. }
  499. }
  500. internal void InternalClose ()
  501. {
  502. disposed = true;
  503. }
  504. internal void ForceCloseConnection ()
  505. {
  506. if (!disposed) {
  507. disposed = true;
  508. cnc.Close (true);
  509. }
  510. }
  511. public override void Close ()
  512. {
  513. if (sendChunked) {
  514. pending.WaitOne ();
  515. byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
  516. cnc.Write (chunk, 0, chunk.Length);
  517. return;
  518. }
  519. if (isRead) {
  520. if (!nextReadCalled) {
  521. CheckComplete ();
  522. // If we have not read all the contents
  523. if (!nextReadCalled) {
  524. nextReadCalled = true;
  525. cnc.Close (true);
  526. }
  527. }
  528. return;
  529. } else if (!allowBuffering) {
  530. complete_request_written = true;
  531. if (!initRead) {
  532. initRead = true;
  533. WebConnection.InitRead (cnc);
  534. }
  535. return;
  536. }
  537. if (disposed)
  538. return;
  539. long length = request.ContentLength;
  540. if (length != -1 && length > writeBuffer.Length)
  541. throw new IOException ("Cannot close the stream until all bytes are written");
  542. WriteRequest ();
  543. disposed = true;
  544. }
  545. internal void KillBuffer ()
  546. {
  547. writeBuffer = null;
  548. }
  549. public override long Seek (long a, SeekOrigin b)
  550. {
  551. throw new NotSupportedException ();
  552. }
  553. public override void SetLength (long a)
  554. {
  555. throw new NotSupportedException ();
  556. }
  557. public override bool CanSeek {
  558. get { return false; }
  559. }
  560. public override bool CanRead {
  561. get { return isRead; }
  562. }
  563. public override bool CanWrite {
  564. get { return !isRead; }
  565. }
  566. public override long Length {
  567. get { throw new NotSupportedException (); }
  568. }
  569. public override long Position {
  570. get { throw new NotSupportedException (); }
  571. set { throw new NotSupportedException (); }
  572. }
  573. }
  574. }