WebConnectionStream.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. } catch {
  75. contentLength = Int32.MaxValue;
  76. }
  77. } else {
  78. contentLength = Int32.MaxValue;
  79. }
  80. }
  81. public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
  82. {
  83. isRead = false;
  84. this.cnc = cnc;
  85. this.request = request;
  86. allowBuffering = request.InternalAllowBuffering;
  87. sendChunked = request.SendChunked;
  88. if (allowBuffering) {
  89. writeBuffer = new MemoryStream ();
  90. max_buffer_size = request.ContentLength;
  91. } else {
  92. max_buffer_size = -1;
  93. }
  94. if (sendChunked)
  95. pending = new ManualResetEvent (true);
  96. }
  97. #if NET_2_0
  98. public override bool CanTimeout {
  99. get { return true; }
  100. }
  101. #endif
  102. internal bool CompleteRequestWritten {
  103. get { return complete_request_written; }
  104. }
  105. internal bool SendChunked {
  106. set { sendChunked = value; }
  107. }
  108. internal byte [] ReadBuffer {
  109. set { readBuffer = value; }
  110. }
  111. internal int ReadBufferOffset {
  112. set { readBufferOffset = value;}
  113. }
  114. internal int ReadBufferSize {
  115. set { readBufferSize = value; }
  116. }
  117. internal byte[] WriteBuffer {
  118. get { return writeBuffer.GetBuffer (); }
  119. }
  120. internal int WriteBufferLength {
  121. get { return (int) writeBuffer.Length; }
  122. }
  123. internal void ForceCompletion ()
  124. {
  125. nextReadCalled = true;
  126. cnc.NextRead ();
  127. }
  128. internal void CheckComplete ()
  129. {
  130. bool nrc = nextReadCalled;
  131. if (!nrc && readBufferSize - readBufferOffset == contentLength) {
  132. nextReadCalled = true;
  133. cnc.NextRead ();
  134. }
  135. }
  136. internal void ReadAll ()
  137. {
  138. if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
  139. if (isRead && !nextReadCalled) {
  140. nextReadCalled = true;
  141. cnc.NextRead ();
  142. }
  143. return;
  144. }
  145. pending.WaitOne ();
  146. lock (locker) {
  147. if (totalRead >= contentLength)
  148. return;
  149. byte [] b = null;
  150. int diff = readBufferSize - readBufferOffset;
  151. int new_size;
  152. if (contentLength == Int32.MaxValue) {
  153. MemoryStream ms = new MemoryStream ();
  154. byte [] buffer = null;
  155. if (readBuffer != null && diff > 0) {
  156. ms.Write (readBuffer, readBufferOffset, diff);
  157. if (readBufferSize >= 8192)
  158. buffer = readBuffer;
  159. }
  160. if (buffer == null)
  161. buffer = new byte [8192];
  162. int read;
  163. while ((read = cnc.Read (buffer, 0, buffer.Length)) != 0)
  164. ms.Write (buffer, 0, read);
  165. b = ms.GetBuffer ();
  166. new_size = (int) ms.Length;
  167. contentLength = new_size;
  168. } else {
  169. new_size = contentLength - totalRead;
  170. b = new byte [new_size];
  171. if (readBuffer != null && diff > 0) {
  172. if (diff > new_size)
  173. diff = new_size;
  174. Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
  175. }
  176. int remaining = new_size - diff;
  177. int r = -1;
  178. while (remaining > 0 && r != 0) {
  179. r = cnc.Read (b, diff, remaining);
  180. remaining -= r;
  181. diff += r;
  182. }
  183. }
  184. readBuffer = b;
  185. readBufferOffset = 0;
  186. readBufferSize = new_size;
  187. totalRead = 0;
  188. nextReadCalled = true;
  189. }
  190. cnc.NextRead ();
  191. }
  192. void WriteCallbackWrapper (IAsyncResult r)
  193. {
  194. WebAsyncResult result;
  195. if (r.AsyncState != null) {
  196. result = (WebAsyncResult) r.AsyncState;
  197. result.InnerAsyncResult = r;
  198. result.DoCallback ();
  199. } else {
  200. EndWrite (r);
  201. }
  202. }
  203. void ReadCallbackWrapper (IAsyncResult r)
  204. {
  205. WebAsyncResult result;
  206. if (r.AsyncState != null) {
  207. result = (WebAsyncResult) r.AsyncState;
  208. result.InnerAsyncResult = r;
  209. result.DoCallback ();
  210. } else {
  211. EndRead (r);
  212. }
  213. }
  214. public override int Read (byte [] buffer, int offset, int size)
  215. {
  216. if (!isRead)
  217. throw new NotSupportedException ("this stream does not allow reading");
  218. if (totalRead >= contentLength)
  219. return 0;
  220. AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
  221. WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
  222. if (!res.IsCompleted && !res.WaitUntilComplete (request.ReadWriteTimeout, false)) {
  223. nextReadCalled = true;
  224. cnc.Close (true);
  225. throw new WebException ("The operation has timed out.",
  226. WebExceptionStatus.Timeout);
  227. }
  228. return EndRead (res);
  229. }
  230. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  231. AsyncCallback cb, object state)
  232. {
  233. if (!isRead)
  234. throw new NotSupportedException ("this stream does not allow reading");
  235. if (buffer == null)
  236. throw new ArgumentNullException ("buffer");
  237. int length = buffer.Length;
  238. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  239. throw new ArgumentOutOfRangeException ();
  240. lock (locker) {
  241. pendingReads++;
  242. pending.Reset ();
  243. }
  244. WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
  245. if (totalRead >= contentLength) {
  246. result.SetCompleted (true, -1);
  247. result.DoCallback ();
  248. return result;
  249. }
  250. int remaining = readBufferSize - readBufferOffset;
  251. if (remaining > 0) {
  252. int copy = (remaining > size) ? size : remaining;
  253. Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
  254. readBufferOffset += copy;
  255. offset += copy;
  256. size -= copy;
  257. totalRead += copy;
  258. if (size == 0 || totalRead >= contentLength) {
  259. result.SetCompleted (true, copy);
  260. result.DoCallback ();
  261. return result;
  262. }
  263. result.NBytes = copy;
  264. }
  265. if (cb != null)
  266. cb = new AsyncCallback (ReadCallbackWrapper);
  267. if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
  268. size = contentLength - totalRead;
  269. if (!read_eof) {
  270. result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, cb, result);
  271. } else {
  272. result.SetCompleted (true, result.NBytes);
  273. result.DoCallback ();
  274. }
  275. return result;
  276. }
  277. public override int EndRead (IAsyncResult r)
  278. {
  279. WebAsyncResult result = (WebAsyncResult) r;
  280. if (result.EndCalled) {
  281. int xx = result.NBytes;
  282. return (xx >= 0) ? xx : 0;
  283. }
  284. result.EndCalled = true;
  285. if (!result.IsCompleted) {
  286. int nbytes = -1;
  287. try {
  288. nbytes = cnc.EndRead (result);
  289. } catch (Exception exc) {
  290. lock (locker) {
  291. pendingReads--;
  292. if (pendingReads == 0)
  293. pending.Set ();
  294. }
  295. nextReadCalled = true;
  296. cnc.Close (true);
  297. result.SetCompleted (false, exc);
  298. throw;
  299. }
  300. if (nbytes < 0) {
  301. nbytes = 0;
  302. read_eof = true;
  303. }
  304. totalRead += nbytes;
  305. result.SetCompleted (false, nbytes + result.NBytes);
  306. result.DoCallback ();
  307. if (nbytes == 0)
  308. contentLength = totalRead;
  309. }
  310. lock (locker) {
  311. pendingReads--;
  312. if (pendingReads == 0)
  313. pending.Set ();
  314. }
  315. if (totalRead >= contentLength && !nextReadCalled)
  316. ReadAll ();
  317. int nb = result.NBytes;
  318. return (nb >= 0) ? nb : 0;
  319. }
  320. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  321. AsyncCallback cb, object state)
  322. {
  323. if (isRead)
  324. throw new NotSupportedException ("this stream does not allow writing");
  325. if (buffer == null)
  326. throw new ArgumentNullException ("buffer");
  327. int length = buffer.Length;
  328. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  329. throw new ArgumentOutOfRangeException ();
  330. if (sendChunked) {
  331. lock (locker) {
  332. pendingWrites++;
  333. pending.Reset ();
  334. }
  335. }
  336. WebAsyncResult result = new WebAsyncResult (cb, state);
  337. if (allowBuffering) {
  338. if (max_buffer_size >= 0) {
  339. long avail = max_buffer_size - writeBuffer.Length;
  340. if (size > avail) {
  341. if (requestWritten)
  342. throw new ProtocolViolationException (
  343. "The number of bytes to be written is greater than " +
  344. "the specified ContentLength.");
  345. }
  346. }
  347. writeBuffer.Write (buffer, offset, size);
  348. if (!sendChunked) {
  349. result.SetCompleted (true, 0);
  350. result.DoCallback ();
  351. return result;
  352. }
  353. }
  354. AsyncCallback callback = null;
  355. if (cb != null)
  356. callback = new AsyncCallback (WriteCallbackWrapper);
  357. if (sendChunked) {
  358. WriteRequest ();
  359. string cSize = String.Format ("{0:X}\r\n", size);
  360. byte [] head = Encoding.ASCII.GetBytes (cSize);
  361. int chunkSize = 2 + size + head.Length;
  362. byte [] newBuffer = new byte [chunkSize];
  363. Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
  364. Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
  365. Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
  366. buffer = newBuffer;
  367. offset = 0;
  368. size = chunkSize;
  369. }
  370. result.InnerAsyncResult = cnc.BeginWrite (buffer, offset, size, callback, result);
  371. return result;
  372. }
  373. public override void EndWrite (IAsyncResult r)
  374. {
  375. if (r == null)
  376. throw new ArgumentNullException ("r");
  377. WebAsyncResult result = r as WebAsyncResult;
  378. if (result == null)
  379. throw new ArgumentException ("Invalid IAsyncResult");
  380. if (result.EndCalled)
  381. return;
  382. result.EndCalled = true;
  383. if (allowBuffering && !sendChunked)
  384. return;
  385. if (result.GotException)
  386. throw result.Exception;
  387. try {
  388. cnc.EndWrite (result.InnerAsyncResult);
  389. result.SetCompleted (false, 0);
  390. } catch (Exception e) {
  391. result.SetCompleted (false, e);
  392. }
  393. if (sendChunked) {
  394. lock (locker) {
  395. pendingWrites--;
  396. if (pendingWrites == 0)
  397. pending.Set ();
  398. }
  399. }
  400. }
  401. public override void Write (byte [] buffer, int offset, int size)
  402. {
  403. if (isRead)
  404. throw new NotSupportedException ("This stream does not allow writing");
  405. AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
  406. WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
  407. if (!res.IsCompleted && !res.WaitUntilComplete (request.ReadWriteTimeout, false)) {
  408. nextReadCalled = true;
  409. cnc.Close (true);
  410. throw new IOException ("Write timed out.");
  411. }
  412. EndWrite (res);
  413. }
  414. public override void Flush ()
  415. {
  416. }
  417. internal void SetHeaders (byte [] buffer, int offset, int size)
  418. {
  419. if (headersSent)
  420. return;
  421. if (!allowBuffering || sendChunked) {
  422. headersSent = true;
  423. if (!cnc.Connected)
  424. throw new WebException ("Not connected", null, WebExceptionStatus.SendFailure, null);
  425. if (!cnc.Write (buffer, offset, size))
  426. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  427. if (!initRead) {
  428. initRead = true;
  429. WebConnection.InitRead (cnc);
  430. }
  431. } else {
  432. headers = new byte [size];
  433. Buffer.BlockCopy (buffer, offset, headers, 0, size);
  434. }
  435. }
  436. internal bool RequestWritten {
  437. get { return requestWritten; }
  438. }
  439. internal void WriteRequest ()
  440. {
  441. if (requestWritten)
  442. return;
  443. if (sendChunked) {
  444. request.SendRequestHeaders ();
  445. requestWritten = true;
  446. return;
  447. }
  448. if (!allowBuffering || writeBuffer == null)
  449. return;
  450. byte [] bytes = writeBuffer.GetBuffer ();
  451. int length = (int) writeBuffer.Length;
  452. if (request.ContentLength != -1 && request.ContentLength < length) {
  453. throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
  454. WebExceptionStatus.ServerProtocolViolation, null);
  455. }
  456. request.InternalContentLength = length;
  457. request.SendRequestHeaders ();
  458. requestWritten = true;
  459. //
  460. // For small requests, make a copy, it will reduce the traffic, for large
  461. // requests, the NoDelay bit on the socket should take effect (set in WebConnection).
  462. //
  463. if (headers.Length + length < 8192){
  464. byte[] b = new byte [headers.Length + length];
  465. Buffer.BlockCopy (headers, 0, b, 0, headers.Length);
  466. Buffer.BlockCopy (bytes, 0, b, headers.Length, length);
  467. if (!cnc.Write (b, 0, b.Length))
  468. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  469. headersSent = true;
  470. complete_request_written = true;
  471. if (!initRead) {
  472. initRead = true;
  473. WebConnection.InitRead (cnc);
  474. }
  475. } else {
  476. if (!cnc.Write (headers, 0, headers.Length))
  477. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  478. headersSent = true;
  479. if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
  480. return;
  481. IAsyncResult result = null;
  482. if (length > 0)
  483. result = cnc.BeginWrite (bytes, 0, length, null, null);
  484. if (!initRead) {
  485. initRead = true;
  486. WebConnection.InitRead (cnc);
  487. }
  488. if (length > 0)
  489. complete_request_written = cnc.EndWrite (result);
  490. else
  491. complete_request_written = true;
  492. }
  493. }
  494. internal void InternalClose ()
  495. {
  496. disposed = true;
  497. }
  498. internal void ForceCloseConnection ()
  499. {
  500. if (!disposed) {
  501. disposed = true;
  502. cnc.Close (true);
  503. }
  504. }
  505. public override void Close ()
  506. {
  507. if (sendChunked) {
  508. pending.WaitOne ();
  509. byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
  510. cnc.Write (chunk, 0, chunk.Length);
  511. return;
  512. }
  513. if (isRead) {
  514. if (!nextReadCalled) {
  515. CheckComplete ();
  516. // If we have not read all the contents
  517. if (!nextReadCalled) {
  518. nextReadCalled = true;
  519. cnc.Close (true);
  520. }
  521. }
  522. return;
  523. } else if (!allowBuffering) {
  524. complete_request_written = true;
  525. if (!initRead) {
  526. initRead = true;
  527. WebConnection.InitRead (cnc);
  528. }
  529. return;
  530. }
  531. if (disposed)
  532. return;
  533. long length = request.ContentLength;
  534. if (length != -1 && length > writeBuffer.Length)
  535. throw new IOException ("Cannot close the stream until all bytes are written");
  536. WriteRequest ();
  537. disposed = true;
  538. }
  539. public override long Seek (long a, SeekOrigin b)
  540. {
  541. throw new NotSupportedException ();
  542. }
  543. public override void SetLength (long a)
  544. {
  545. throw new NotSupportedException ();
  546. }
  547. public override bool CanSeek {
  548. get { return false; }
  549. }
  550. public override bool CanRead {
  551. get { return isRead; }
  552. }
  553. public override bool CanWrite {
  554. get { return !isRead; }
  555. }
  556. public override long Length {
  557. get { throw new NotSupportedException (); }
  558. }
  559. public override long Position {
  560. get { throw new NotSupportedException (); }
  561. set { throw new NotSupportedException (); }
  562. }
  563. }
  564. }