WebConnectionStream.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. long totalWritten;
  47. bool nextReadCalled;
  48. int pendingReads;
  49. int pendingWrites;
  50. ManualResetEvent pending;
  51. bool allowBuffering;
  52. bool sendChunked;
  53. MemoryStream writeBuffer;
  54. bool requestWritten;
  55. byte [] headers;
  56. bool disposed;
  57. bool headersSent;
  58. object locker = new object ();
  59. bool initRead;
  60. bool read_eof;
  61. bool complete_request_written;
  62. int read_timeout;
  63. int write_timeout;
  64. public WebConnectionStream (WebConnection cnc)
  65. {
  66. isRead = true;
  67. pending = new ManualResetEvent (true);
  68. this.request = cnc.Data.request;
  69. read_timeout = request.ReadWriteTimeout;
  70. write_timeout = read_timeout;
  71. this.cnc = cnc;
  72. string contentType = cnc.Data.Headers ["Transfer-Encoding"];
  73. bool chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
  74. string clength = cnc.Data.Headers ["Content-Length"];
  75. if (!chunkedRead && clength != null && clength != "") {
  76. try {
  77. contentLength = Int32.Parse (clength);
  78. if (contentLength == 0 && !IsNtlmAuth ()) {
  79. ReadAll ();
  80. }
  81. } catch {
  82. contentLength = Int32.MaxValue;
  83. }
  84. } else {
  85. contentLength = Int32.MaxValue;
  86. }
  87. }
  88. public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
  89. {
  90. read_timeout = request.ReadWriteTimeout;
  91. write_timeout = read_timeout;
  92. isRead = false;
  93. this.cnc = cnc;
  94. this.request = request;
  95. allowBuffering = request.InternalAllowBuffering;
  96. sendChunked = request.SendChunked;
  97. if (sendChunked)
  98. pending = new ManualResetEvent (true);
  99. else if (allowBuffering)
  100. writeBuffer = new MemoryStream ();
  101. }
  102. bool IsNtlmAuth ()
  103. {
  104. bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
  105. string header_name = (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate";
  106. string authHeader = cnc.Data.Headers [header_name];
  107. return (authHeader != null && authHeader.IndexOf ("NTLM") != -1);
  108. }
  109. internal void CheckResponseInBuffer ()
  110. {
  111. if (contentLength > 0 && (readBufferSize - readBufferOffset) >= contentLength) {
  112. if (!IsNtlmAuth ())
  113. ReadAll ();
  114. }
  115. }
  116. internal HttpWebRequest Request {
  117. get { return request; }
  118. }
  119. internal WebConnection Connection {
  120. get { return cnc; }
  121. }
  122. #if NET_2_0
  123. public override bool CanTimeout {
  124. get { return true; }
  125. }
  126. #endif
  127. #if NET_2_0
  128. public override
  129. #endif
  130. int ReadTimeout {
  131. get {
  132. return read_timeout;
  133. }
  134. set {
  135. if (value < -1)
  136. throw new ArgumentOutOfRangeException ("value");
  137. read_timeout = value;
  138. }
  139. }
  140. #if NET_2_0
  141. public override
  142. #endif
  143. int WriteTimeout {
  144. get {
  145. return write_timeout;
  146. }
  147. set {
  148. if (value < -1)
  149. throw new ArgumentOutOfRangeException ("value");
  150. write_timeout = value;
  151. }
  152. }
  153. internal bool CompleteRequestWritten {
  154. get { return complete_request_written; }
  155. }
  156. internal bool SendChunked {
  157. set { sendChunked = value; }
  158. }
  159. internal byte [] ReadBuffer {
  160. set { readBuffer = value; }
  161. }
  162. internal int ReadBufferOffset {
  163. set { readBufferOffset = value;}
  164. }
  165. internal int ReadBufferSize {
  166. set { readBufferSize = value; }
  167. }
  168. internal byte[] WriteBuffer {
  169. get { return writeBuffer.GetBuffer (); }
  170. }
  171. internal int WriteBufferLength {
  172. get { return writeBuffer != null ? (int) writeBuffer.Length : (-1); }
  173. }
  174. internal void ForceCompletion ()
  175. {
  176. if (!nextReadCalled) {
  177. if (contentLength == Int32.MaxValue)
  178. contentLength = 0;
  179. nextReadCalled = true;
  180. cnc.NextRead ();
  181. }
  182. }
  183. internal void CheckComplete ()
  184. {
  185. bool nrc = nextReadCalled;
  186. if (!nrc && readBufferSize - readBufferOffset == contentLength) {
  187. nextReadCalled = true;
  188. cnc.NextRead ();
  189. }
  190. }
  191. internal void ReadAll ()
  192. {
  193. if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
  194. if (isRead && !nextReadCalled) {
  195. nextReadCalled = true;
  196. cnc.NextRead ();
  197. }
  198. return;
  199. }
  200. pending.WaitOne ();
  201. lock (locker) {
  202. if (totalRead >= contentLength)
  203. return;
  204. byte [] b = null;
  205. int diff = readBufferSize - readBufferOffset;
  206. int new_size;
  207. if (contentLength == Int32.MaxValue) {
  208. MemoryStream ms = new MemoryStream ();
  209. byte [] buffer = null;
  210. if (readBuffer != null && diff > 0) {
  211. ms.Write (readBuffer, readBufferOffset, diff);
  212. if (readBufferSize >= 8192)
  213. buffer = readBuffer;
  214. }
  215. if (buffer == null)
  216. buffer = new byte [8192];
  217. int read;
  218. while ((read = cnc.Read (request, buffer, 0, buffer.Length)) != 0)
  219. ms.Write (buffer, 0, read);
  220. b = ms.GetBuffer ();
  221. new_size = (int) ms.Length;
  222. contentLength = new_size;
  223. } else {
  224. new_size = contentLength - totalRead;
  225. b = new byte [new_size];
  226. if (readBuffer != null && diff > 0) {
  227. if (diff > new_size)
  228. diff = new_size;
  229. Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
  230. }
  231. int remaining = new_size - diff;
  232. int r = -1;
  233. while (remaining > 0 && r != 0) {
  234. r = cnc.Read (request, b, diff, remaining);
  235. remaining -= r;
  236. diff += r;
  237. }
  238. }
  239. readBuffer = b;
  240. readBufferOffset = 0;
  241. readBufferSize = new_size;
  242. totalRead = 0;
  243. nextReadCalled = true;
  244. }
  245. cnc.NextRead ();
  246. }
  247. void WriteCallbackWrapper (IAsyncResult r)
  248. {
  249. WebAsyncResult result = r as WebAsyncResult;
  250. if (result != null && result.AsyncWriteAll)
  251. return;
  252. if (r.AsyncState != null) {
  253. result = (WebAsyncResult) r.AsyncState;
  254. result.InnerAsyncResult = r;
  255. result.DoCallback ();
  256. } else {
  257. EndWrite (r);
  258. }
  259. }
  260. void ReadCallbackWrapper (IAsyncResult r)
  261. {
  262. WebAsyncResult result;
  263. if (r.AsyncState != null) {
  264. result = (WebAsyncResult) r.AsyncState;
  265. result.InnerAsyncResult = r;
  266. result.DoCallback ();
  267. } else {
  268. EndRead (r);
  269. }
  270. }
  271. public override int Read (byte [] buffer, int offset, int size)
  272. {
  273. if (!isRead)
  274. throw new NotSupportedException ("this stream does not allow reading");
  275. if (totalRead >= contentLength)
  276. return 0;
  277. AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
  278. WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
  279. if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
  280. nextReadCalled = true;
  281. cnc.Close (true);
  282. throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
  283. }
  284. return EndRead (res);
  285. }
  286. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  287. AsyncCallback cb, object state)
  288. {
  289. if (!isRead)
  290. throw new NotSupportedException ("this stream does not allow reading");
  291. if (buffer == null)
  292. throw new ArgumentNullException ("buffer");
  293. int length = buffer.Length;
  294. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  295. throw new ArgumentOutOfRangeException ();
  296. lock (locker) {
  297. pendingReads++;
  298. pending.Reset ();
  299. }
  300. WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
  301. if (totalRead >= contentLength) {
  302. result.SetCompleted (true, -1);
  303. result.DoCallback ();
  304. return result;
  305. }
  306. int remaining = readBufferSize - readBufferOffset;
  307. if (remaining > 0) {
  308. int copy = (remaining > size) ? size : remaining;
  309. Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
  310. readBufferOffset += copy;
  311. offset += copy;
  312. size -= copy;
  313. totalRead += copy;
  314. if (size == 0 || totalRead >= contentLength) {
  315. result.SetCompleted (true, copy);
  316. result.DoCallback ();
  317. return result;
  318. }
  319. result.NBytes = copy;
  320. }
  321. if (cb != null)
  322. cb = new AsyncCallback (ReadCallbackWrapper);
  323. if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
  324. size = contentLength - totalRead;
  325. if (!read_eof) {
  326. result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
  327. } else {
  328. result.SetCompleted (true, result.NBytes);
  329. result.DoCallback ();
  330. }
  331. return result;
  332. }
  333. public override int EndRead (IAsyncResult r)
  334. {
  335. WebAsyncResult result = (WebAsyncResult) r;
  336. if (result.EndCalled) {
  337. int xx = result.NBytes;
  338. return (xx >= 0) ? xx : 0;
  339. }
  340. result.EndCalled = true;
  341. if (!result.IsCompleted) {
  342. int nbytes = -1;
  343. try {
  344. nbytes = cnc.EndRead (request, result);
  345. } catch (Exception exc) {
  346. lock (locker) {
  347. pendingReads--;
  348. if (pendingReads == 0)
  349. pending.Set ();
  350. }
  351. nextReadCalled = true;
  352. cnc.Close (true);
  353. result.SetCompleted (false, exc);
  354. throw;
  355. }
  356. if (nbytes < 0) {
  357. nbytes = 0;
  358. read_eof = true;
  359. }
  360. totalRead += nbytes;
  361. result.SetCompleted (false, nbytes + result.NBytes);
  362. result.DoCallback ();
  363. if (nbytes == 0)
  364. contentLength = totalRead;
  365. }
  366. lock (locker) {
  367. pendingReads--;
  368. if (pendingReads == 0)
  369. pending.Set ();
  370. }
  371. if (totalRead >= contentLength && !nextReadCalled)
  372. ReadAll ();
  373. int nb = result.NBytes;
  374. return (nb >= 0) ? nb : 0;
  375. }
  376. void WriteRequestAsyncCB (IAsyncResult r)
  377. {
  378. WebAsyncResult result = (WebAsyncResult) r.AsyncState;
  379. try {
  380. cnc.EndWrite2 (request, r);
  381. result.SetCompleted (false, 0);
  382. if (!initRead) {
  383. initRead = true;
  384. WebConnection.InitRead (cnc);
  385. }
  386. } catch (Exception e) {
  387. KillBuffer ();
  388. nextReadCalled = true;
  389. cnc.Close (true);
  390. if (e is System.Net.Sockets.SocketException)
  391. e = new IOException ("Error writing request", e);
  392. result.SetCompleted (false, e);
  393. }
  394. complete_request_written = true;
  395. result.DoCallback ();
  396. }
  397. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  398. AsyncCallback cb, object state)
  399. {
  400. if (request.Aborted)
  401. throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
  402. if (isRead)
  403. throw new NotSupportedException ("this stream does not allow writing");
  404. if (buffer == null)
  405. throw new ArgumentNullException ("buffer");
  406. int length = buffer.Length;
  407. if (size < 0 || offset < 0 || length < offset || length - offset < size)
  408. throw new ArgumentOutOfRangeException ();
  409. if (sendChunked) {
  410. lock (locker) {
  411. pendingWrites++;
  412. pending.Reset ();
  413. }
  414. }
  415. WebAsyncResult result = new WebAsyncResult (cb, state);
  416. if (!sendChunked)
  417. CheckWriteOverflow (request.ContentLength, totalWritten, size);
  418. if (allowBuffering && !sendChunked) {
  419. if (writeBuffer == null)
  420. writeBuffer = new MemoryStream ();
  421. writeBuffer.Write (buffer, offset, size);
  422. totalWritten += size;
  423. if (request.ContentLength > 0 && totalWritten == request.ContentLength) {
  424. try {
  425. result.AsyncWriteAll = true;
  426. result.InnerAsyncResult = WriteRequestAsync (new AsyncCallback (WriteRequestAsyncCB), result);
  427. if (result.InnerAsyncResult == null) {
  428. if (!result.IsCompleted)
  429. result.SetCompleted (true, 0);
  430. result.DoCallback ();
  431. }
  432. } catch (Exception exc) {
  433. result.SetCompleted (true, exc);
  434. result.DoCallback ();
  435. }
  436. } else {
  437. result.SetCompleted (true, 0);
  438. result.DoCallback ();
  439. }
  440. return result;
  441. }
  442. AsyncCallback callback = null;
  443. if (cb != null)
  444. callback = new AsyncCallback (WriteCallbackWrapper);
  445. if (sendChunked) {
  446. WriteRequest ();
  447. string cSize = String.Format ("{0:X}\r\n", size);
  448. byte [] head = Encoding.ASCII.GetBytes (cSize);
  449. int chunkSize = 2 + size + head.Length;
  450. byte [] newBuffer = new byte [chunkSize];
  451. Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
  452. Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
  453. Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
  454. buffer = newBuffer;
  455. offset = 0;
  456. size = chunkSize;
  457. }
  458. result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
  459. totalWritten += size;
  460. return result;
  461. }
  462. void CheckWriteOverflow (long contentLength, long totalWritten, long size)
  463. {
  464. if (contentLength == -1)
  465. return;
  466. long avail = contentLength - totalWritten;
  467. if (size > avail) {
  468. KillBuffer ();
  469. nextReadCalled = true;
  470. cnc.Close (true);
  471. throw new ProtocolViolationException (
  472. "The number of bytes to be written is greater than " +
  473. "the specified ContentLength.");
  474. }
  475. }
  476. public override void EndWrite (IAsyncResult r)
  477. {
  478. if (r == null)
  479. throw new ArgumentNullException ("r");
  480. WebAsyncResult result = r as WebAsyncResult;
  481. if (result == null)
  482. throw new ArgumentException ("Invalid IAsyncResult");
  483. if (result.EndCalled)
  484. return;
  485. result.EndCalled = true;
  486. if (result.AsyncWriteAll) {
  487. result.WaitUntilComplete ();
  488. if (result.GotException)
  489. throw result.Exception;
  490. return;
  491. }
  492. if (allowBuffering && !sendChunked)
  493. return;
  494. if (result.GotException)
  495. throw result.Exception;
  496. try {
  497. cnc.EndWrite (request, result.InnerAsyncResult);
  498. result.SetCompleted (false, 0);
  499. } catch (Exception e) {
  500. result.SetCompleted (false, e);
  501. throw;
  502. } finally {
  503. if (sendChunked) {
  504. lock (locker) {
  505. pendingWrites--;
  506. if (pendingWrites == 0)
  507. pending.Set ();
  508. }
  509. }
  510. }
  511. }
  512. public override void Write (byte [] buffer, int offset, int size)
  513. {
  514. if (isRead)
  515. throw new NotSupportedException ("This stream does not allow writing");
  516. AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
  517. WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
  518. if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
  519. KillBuffer ();
  520. nextReadCalled = true;
  521. cnc.Close (true);
  522. throw new IOException ("Write timed out.");
  523. }
  524. EndWrite (res);
  525. }
  526. public override void Flush ()
  527. {
  528. }
  529. internal void SetHeaders (byte [] buffer, int offset, int size)
  530. {
  531. if (headersSent)
  532. return;
  533. headers = new byte [size];
  534. Buffer.BlockCopy (buffer, offset, headers, 0, size);
  535. long cl = request.ContentLength;
  536. string method = request.Method;
  537. bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
  538. method == "TRACE" || method == "DELETE");
  539. if (sendChunked || cl > -1 || no_writestream) {
  540. WriteHeaders ();
  541. if (!initRead) {
  542. initRead = true;
  543. WebConnection.InitRead (cnc);
  544. }
  545. if (!sendChunked && cl == 0)
  546. Close ();
  547. }
  548. }
  549. internal bool RequestWritten {
  550. get { return requestWritten; }
  551. }
  552. IAsyncResult WriteRequestAsync (AsyncCallback cb, object state)
  553. {
  554. requestWritten = true;
  555. byte [] bytes = writeBuffer.GetBuffer ();
  556. int length = (int) writeBuffer.Length;
  557. writeBuffer = null;
  558. // Headers already written to the stream
  559. return (length > 0) ? cnc.BeginWrite (request, bytes, 0, length, cb, state) : null;
  560. }
  561. void WriteHeaders ()
  562. {
  563. if (headersSent)
  564. return;
  565. headersSent = true;
  566. if (!cnc.Write (request, headers, 0, headers.Length))
  567. throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
  568. }
  569. internal void WriteRequest ()
  570. {
  571. if (requestWritten)
  572. return;
  573. if (sendChunked) {
  574. requestWritten = true;
  575. return;
  576. }
  577. if (!allowBuffering || writeBuffer == null)
  578. return;
  579. byte [] bytes = writeBuffer.GetBuffer ();
  580. int length = (int) writeBuffer.Length;
  581. writeBuffer = null;
  582. if (request.ContentLength != -1 && request.ContentLength < length) {
  583. nextReadCalled = true;
  584. cnc.Close (true);
  585. throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
  586. WebExceptionStatus.ServerProtocolViolation, null);
  587. }
  588. if (!headersSent) {
  589. bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
  590. method == "TRACE" || method == "DELETE");
  591. if (!no_writestream)
  592. request.InternalContentLength = length;
  593. request.SendRequestHeaders ();
  594. }
  595. WriteHeaders ();
  596. if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
  597. return;
  598. IAsyncResult result = null;
  599. if (length > 0)
  600. result = cnc.BeginWrite (request, bytes, 0, length, null, null);
  601. if (!initRead) {
  602. initRead = true;
  603. WebConnection.InitRead (cnc);
  604. }
  605. if (length > 0)
  606. complete_request_written = cnc.EndWrite (request, result);
  607. else
  608. complete_request_written = true;
  609. }
  610. internal void InternalClose ()
  611. {
  612. disposed = true;
  613. }
  614. public override void Close ()
  615. {
  616. if (sendChunked) {
  617. if (disposed)
  618. return;
  619. disposed = true;
  620. pending.WaitOne ();
  621. byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
  622. cnc.Write (request, chunk, 0, chunk.Length);
  623. return;
  624. }
  625. if (isRead) {
  626. if (!nextReadCalled) {
  627. CheckComplete ();
  628. // If we have not read all the contents
  629. if (!nextReadCalled) {
  630. nextReadCalled = true;
  631. cnc.Close (true);
  632. }
  633. }
  634. return;
  635. } else if (!allowBuffering) {
  636. complete_request_written = true;
  637. if (!initRead) {
  638. initRead = true;
  639. WebConnection.InitRead (cnc);
  640. }
  641. return;
  642. }
  643. if (disposed || requestWritten)
  644. return;
  645. long length = request.ContentLength;
  646. if (!sendChunked && length != -1 && totalWritten != length) {
  647. IOException io = new IOException ("Cannot close the stream until all bytes are written");
  648. nextReadCalled = true;
  649. cnc.Close (true);
  650. throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
  651. }
  652. WriteRequest ();
  653. disposed = true;
  654. }
  655. internal void KillBuffer ()
  656. {
  657. writeBuffer = null;
  658. }
  659. public override long Seek (long a, SeekOrigin b)
  660. {
  661. throw new NotSupportedException ();
  662. }
  663. public override void SetLength (long a)
  664. {
  665. throw new NotSupportedException ();
  666. }
  667. public override bool CanSeek {
  668. get { return false; }
  669. }
  670. public override bool CanRead {
  671. get { return isRead; }
  672. }
  673. public override bool CanWrite {
  674. get { return !isRead; }
  675. }
  676. public override long Length {
  677. get { throw new NotSupportedException (); }
  678. }
  679. public override long Position {
  680. get { throw new NotSupportedException (); }
  681. set { throw new NotSupportedException (); }
  682. }
  683. }
  684. }