WebConnectionStream.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
  274. WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
  275. if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
  276. nextReadCalled = true;
  277. cnc.Close (true);
  278. throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
  279. }
  280. return EndRead (res);
  281. }
  282. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  283. AsyncCallback cb, object state)
  284. {
  285. if (!isRead)
  286. throw new NotSupportedException ("this stream does not allow reading");
  287. if (buffer == null)
  288. throw new ArgumentNullException ("buffer");
  289. int length = buffer.Length;
  290. if (offset < 0 || length < offset)
  291. throw new ArgumentOutOfRangeException ("offset");
  292. if (size < 0 || (length - offset) < size)
  293. throw new ArgumentOutOfRangeException ("size");
  294. lock (locker) {
  295. pendingReads++;
  296. pending.Reset ();
  297. }
  298. WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
  299. if (totalRead >= contentLength) {
  300. result.SetCompleted (true, -1);
  301. result.DoCallback ();
  302. return result;
  303. }
  304. int remaining = readBufferSize - readBufferOffset;
  305. if (remaining > 0) {
  306. int copy = (remaining > size) ? size : remaining;
  307. Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
  308. readBufferOffset += copy;
  309. offset += copy;
  310. size -= copy;
  311. totalRead += copy;
  312. if (size == 0 || totalRead >= contentLength) {
  313. result.SetCompleted (true, copy);
  314. result.DoCallback ();
  315. return result;
  316. }
  317. result.NBytes = copy;
  318. }
  319. if (cb != null)
  320. cb = new AsyncCallback (ReadCallbackWrapper);
  321. if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
  322. size = contentLength - totalRead;
  323. if (!read_eof) {
  324. result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
  325. } else {
  326. result.SetCompleted (true, result.NBytes);
  327. result.DoCallback ();
  328. }
  329. return result;
  330. }
  331. public override int EndRead (IAsyncResult r)
  332. {
  333. WebAsyncResult result = (WebAsyncResult) r;
  334. if (result.EndCalled) {
  335. int xx = result.NBytes;
  336. return (xx >= 0) ? xx : 0;
  337. }
  338. result.EndCalled = true;
  339. if (!result.IsCompleted) {
  340. int nbytes = -1;
  341. try {
  342. nbytes = cnc.EndRead (request, result);
  343. } catch (Exception exc) {
  344. lock (locker) {
  345. pendingReads--;
  346. if (pendingReads == 0)
  347. pending.Set ();
  348. }
  349. nextReadCalled = true;
  350. cnc.Close (true);
  351. result.SetCompleted (false, exc);
  352. result.DoCallback ();
  353. throw;
  354. }
  355. if (nbytes < 0) {
  356. nbytes = 0;
  357. read_eof = true;
  358. }
  359. totalRead += nbytes;
  360. result.SetCompleted (false, nbytes + result.NBytes);
  361. result.DoCallback ();
  362. if (nbytes == 0)
  363. contentLength = totalRead;
  364. }
  365. lock (locker) {
  366. pendingReads--;
  367. if (pendingReads == 0)
  368. pending.Set ();
  369. }
  370. if (totalRead >= contentLength && !nextReadCalled)
  371. ReadAll ();
  372. int nb = result.NBytes;
  373. return (nb >= 0) ? nb : 0;
  374. }
  375. void WriteRequestAsyncCB (IAsyncResult r)
  376. {
  377. WebAsyncResult result = (WebAsyncResult) r.AsyncState;
  378. try {
  379. cnc.EndWrite2 (request, r);
  380. result.SetCompleted (false, 0);
  381. if (!initRead) {
  382. initRead = true;
  383. WebConnection.InitRead (cnc);
  384. }
  385. } catch (Exception e) {
  386. KillBuffer ();
  387. nextReadCalled = true;
  388. cnc.Close (true);
  389. if (e is System.Net.Sockets.SocketException)
  390. e = new IOException ("Error writing request", e);
  391. result.SetCompleted (false, e);
  392. }
  393. complete_request_written = true;
  394. result.DoCallback ();
  395. }
  396. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  397. AsyncCallback cb, object state)
  398. {
  399. if (request.Aborted)
  400. throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
  401. if (isRead)
  402. throw new NotSupportedException ("this stream does not allow writing");
  403. if (buffer == null)
  404. throw new ArgumentNullException ("buffer");
  405. int length = buffer.Length;
  406. if (offset < 0 || length < offset)
  407. throw new ArgumentOutOfRangeException ("offset");
  408. if (size < 0 || (length - offset) < size)
  409. throw new ArgumentOutOfRangeException ("size");
  410. if (sendChunked) {
  411. lock (locker) {
  412. pendingWrites++;
  413. pending.Reset ();
  414. }
  415. }
  416. WebAsyncResult result = new WebAsyncResult (cb, state);
  417. if (!sendChunked)
  418. CheckWriteOverflow (request.ContentLength, totalWritten, size);
  419. if (allowBuffering && !sendChunked) {
  420. if (writeBuffer == null)
  421. writeBuffer = new MemoryStream ();
  422. writeBuffer.Write (buffer, offset, size);
  423. totalWritten += size;
  424. if (request.ContentLength > 0 && totalWritten == request.ContentLength) {
  425. try {
  426. result.AsyncWriteAll = true;
  427. result.InnerAsyncResult = WriteRequestAsync (new AsyncCallback (WriteRequestAsyncCB), result);
  428. if (result.InnerAsyncResult == null) {
  429. if (!result.IsCompleted)
  430. result.SetCompleted (true, 0);
  431. result.DoCallback ();
  432. }
  433. } catch (Exception exc) {
  434. result.SetCompleted (true, exc);
  435. result.DoCallback ();
  436. }
  437. } else {
  438. result.SetCompleted (true, 0);
  439. result.DoCallback ();
  440. }
  441. return result;
  442. }
  443. AsyncCallback callback = null;
  444. if (cb != null)
  445. callback = new AsyncCallback (WriteCallbackWrapper);
  446. if (sendChunked) {
  447. WriteRequest ();
  448. string cSize = String.Format ("{0:X}\r\n", size);
  449. byte [] head = Encoding.ASCII.GetBytes (cSize);
  450. int chunkSize = 2 + size + head.Length;
  451. byte [] newBuffer = new byte [chunkSize];
  452. Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
  453. Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
  454. Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
  455. buffer = newBuffer;
  456. offset = 0;
  457. size = chunkSize;
  458. }
  459. result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
  460. totalWritten += size;
  461. return result;
  462. }
  463. void CheckWriteOverflow (long contentLength, long totalWritten, long size)
  464. {
  465. if (contentLength == -1)
  466. return;
  467. long avail = contentLength - totalWritten;
  468. if (size > avail) {
  469. KillBuffer ();
  470. nextReadCalled = true;
  471. cnc.Close (true);
  472. throw new ProtocolViolationException (
  473. "The number of bytes to be written is greater than " +
  474. "the specified ContentLength.");
  475. }
  476. }
  477. public override void EndWrite (IAsyncResult r)
  478. {
  479. if (r == null)
  480. throw new ArgumentNullException ("r");
  481. WebAsyncResult result = r as WebAsyncResult;
  482. if (result == null)
  483. throw new ArgumentException ("Invalid IAsyncResult");
  484. if (result.EndCalled)
  485. return;
  486. result.EndCalled = true;
  487. if (result.AsyncWriteAll) {
  488. result.WaitUntilComplete ();
  489. if (result.GotException)
  490. throw result.Exception;
  491. return;
  492. }
  493. if (allowBuffering && !sendChunked)
  494. return;
  495. if (result.GotException)
  496. throw result.Exception;
  497. try {
  498. cnc.EndWrite2 (request, result.InnerAsyncResult);
  499. result.SetCompleted (false, 0);
  500. result.DoCallback ();
  501. } catch (Exception e) {
  502. result.SetCompleted (false, e);
  503. result.DoCallback ();
  504. throw;
  505. } finally {
  506. if (sendChunked) {
  507. lock (locker) {
  508. pendingWrites--;
  509. if (pendingWrites == 0)
  510. pending.Set ();
  511. }
  512. }
  513. }
  514. }
  515. public override void Write (byte [] buffer, int offset, int size)
  516. {
  517. AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
  518. WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
  519. if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
  520. KillBuffer ();
  521. nextReadCalled = true;
  522. cnc.Close (true);
  523. throw new IOException ("Write timed out.");
  524. }
  525. EndWrite (res);
  526. }
  527. public override void Flush ()
  528. {
  529. }
  530. internal void SetHeaders (byte [] buffer)
  531. {
  532. if (headersSent)
  533. return;
  534. headers = buffer;
  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. requestWritten = true;
  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. // Headers already written to the stream
  558. return (length > 0) ? cnc.BeginWrite (request, bytes, 0, length, cb, state) : null;
  559. }
  560. void WriteHeaders ()
  561. {
  562. if (headersSent)
  563. return;
  564. headersSent = true;
  565. string err_msg = null;
  566. if (!cnc.Write (request, headers, 0, headers.Length, ref err_msg))
  567. throw new WebException ("Error writing request: " + err_msg, null, WebExceptionStatus.SendFailure, null);
  568. }
  569. internal void WriteRequest ()
  570. {
  571. if (requestWritten)
  572. return;
  573. requestWritten = true;
  574. if (sendChunked)
  575. return;
  576. if (!allowBuffering || writeBuffer == null)
  577. return;
  578. byte [] bytes = writeBuffer.GetBuffer ();
  579. int length = (int) writeBuffer.Length;
  580. if (request.ContentLength != -1 && request.ContentLength < length) {
  581. nextReadCalled = true;
  582. cnc.Close (true);
  583. throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
  584. WebExceptionStatus.ServerProtocolViolation, null);
  585. }
  586. if (!headersSent) {
  587. string method = request.Method;
  588. bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
  589. method == "TRACE" || method == "DELETE");
  590. if (!no_writestream)
  591. request.InternalContentLength = length;
  592. request.SendRequestHeaders (true);
  593. }
  594. WriteHeaders ();
  595. if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
  596. return;
  597. IAsyncResult result = null;
  598. if (length > 0)
  599. result = cnc.BeginWrite (request, bytes, 0, length, null, null);
  600. if (!initRead) {
  601. initRead = true;
  602. WebConnection.InitRead (cnc);
  603. }
  604. if (length > 0)
  605. complete_request_written = cnc.EndWrite (request, result);
  606. else
  607. complete_request_written = true;
  608. }
  609. internal void InternalClose ()
  610. {
  611. disposed = true;
  612. }
  613. public override void Close ()
  614. {
  615. if (sendChunked) {
  616. if (disposed)
  617. return;
  618. disposed = true;
  619. pending.WaitOne ();
  620. byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
  621. string err_msg = null;
  622. cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
  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 !disposed && isRead; }
  672. }
  673. public override bool CanWrite {
  674. get { return !disposed && !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. }