WebConnectionStream.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. throw;
  353. }
  354. if (nbytes < 0) {
  355. nbytes = 0;
  356. read_eof = true;
  357. }
  358. totalRead += nbytes;
  359. result.SetCompleted (false, nbytes + result.NBytes);
  360. result.DoCallback ();
  361. if (nbytes == 0)
  362. contentLength = totalRead;
  363. }
  364. lock (locker) {
  365. pendingReads--;
  366. if (pendingReads == 0)
  367. pending.Set ();
  368. }
  369. if (totalRead >= contentLength && !nextReadCalled)
  370. ReadAll ();
  371. int nb = result.NBytes;
  372. return (nb >= 0) ? nb : 0;
  373. }
  374. void WriteRequestAsyncCB (IAsyncResult r)
  375. {
  376. WebAsyncResult result = (WebAsyncResult) r.AsyncState;
  377. try {
  378. cnc.EndWrite2 (request, r);
  379. result.SetCompleted (false, 0);
  380. if (!initRead) {
  381. initRead = true;
  382. WebConnection.InitRead (cnc);
  383. }
  384. } catch (Exception e) {
  385. KillBuffer ();
  386. nextReadCalled = true;
  387. cnc.Close (true);
  388. if (e is System.Net.Sockets.SocketException)
  389. e = new IOException ("Error writing request", e);
  390. result.SetCompleted (false, e);
  391. }
  392. complete_request_written = true;
  393. result.DoCallback ();
  394. }
  395. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  396. AsyncCallback cb, object state)
  397. {
  398. if (request.Aborted)
  399. throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
  400. if (isRead)
  401. throw new NotSupportedException ("this stream does not allow writing");
  402. if (buffer == null)
  403. throw new ArgumentNullException ("buffer");
  404. int length = buffer.Length;
  405. if (offset < 0 || length < offset)
  406. throw new ArgumentOutOfRangeException ("offset");
  407. if (size < 0 || (length - offset) < size)
  408. throw new ArgumentOutOfRangeException ("size");
  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. AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
  515. WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
  516. if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
  517. KillBuffer ();
  518. nextReadCalled = true;
  519. cnc.Close (true);
  520. throw new IOException ("Write timed out.");
  521. }
  522. EndWrite (res);
  523. }
  524. public override void Flush ()
  525. {
  526. }
  527. internal void SetHeaders (byte [] buffer)
  528. {
  529. if (headersSent)
  530. return;
  531. headers = buffer;
  532. long cl = request.ContentLength;
  533. string method = request.Method;
  534. bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
  535. method == "TRACE" || method == "DELETE");
  536. if (sendChunked || cl > -1 || no_writestream) {
  537. WriteHeaders ();
  538. if (!initRead) {
  539. initRead = true;
  540. WebConnection.InitRead (cnc);
  541. }
  542. if (!sendChunked && cl == 0)
  543. requestWritten = true;
  544. }
  545. }
  546. internal bool RequestWritten {
  547. get { return requestWritten; }
  548. }
  549. IAsyncResult WriteRequestAsync (AsyncCallback cb, object state)
  550. {
  551. requestWritten = true;
  552. byte [] bytes = writeBuffer.GetBuffer ();
  553. int length = (int) writeBuffer.Length;
  554. // Headers already written to the stream
  555. return (length > 0) ? cnc.BeginWrite (request, bytes, 0, length, cb, state) : null;
  556. }
  557. void WriteHeaders ()
  558. {
  559. if (headersSent)
  560. return;
  561. headersSent = true;
  562. string err_msg = null;
  563. if (!cnc.Write (request, headers, 0, headers.Length, ref err_msg))
  564. throw new WebException ("Error writing request: " + err_msg, null, WebExceptionStatus.SendFailure, null);
  565. }
  566. internal void WriteRequest ()
  567. {
  568. if (requestWritten)
  569. return;
  570. requestWritten = true;
  571. if (sendChunked)
  572. return;
  573. if (!allowBuffering || writeBuffer == null)
  574. return;
  575. byte [] bytes = writeBuffer.GetBuffer ();
  576. int length = (int) writeBuffer.Length;
  577. if (request.ContentLength != -1 && request.ContentLength < length) {
  578. nextReadCalled = true;
  579. cnc.Close (true);
  580. throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
  581. WebExceptionStatus.ServerProtocolViolation, null);
  582. }
  583. if (!headersSent) {
  584. string method = request.Method;
  585. bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
  586. method == "TRACE" || method == "DELETE");
  587. if (!no_writestream)
  588. request.InternalContentLength = length;
  589. request.SendRequestHeaders ();
  590. }
  591. WriteHeaders ();
  592. if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
  593. return;
  594. IAsyncResult result = null;
  595. if (length > 0)
  596. result = cnc.BeginWrite (request, bytes, 0, length, null, null);
  597. if (!initRead) {
  598. initRead = true;
  599. WebConnection.InitRead (cnc);
  600. }
  601. if (length > 0)
  602. complete_request_written = cnc.EndWrite (request, result);
  603. else
  604. complete_request_written = true;
  605. }
  606. internal void InternalClose ()
  607. {
  608. disposed = true;
  609. }
  610. public override void Close ()
  611. {
  612. if (sendChunked) {
  613. if (disposed)
  614. return;
  615. disposed = true;
  616. pending.WaitOne ();
  617. byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
  618. string err_msg = null;
  619. cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
  620. return;
  621. }
  622. if (isRead) {
  623. if (!nextReadCalled) {
  624. CheckComplete ();
  625. // If we have not read all the contents
  626. if (!nextReadCalled) {
  627. nextReadCalled = true;
  628. cnc.Close (true);
  629. }
  630. }
  631. return;
  632. } else if (!allowBuffering) {
  633. complete_request_written = true;
  634. if (!initRead) {
  635. initRead = true;
  636. WebConnection.InitRead (cnc);
  637. }
  638. return;
  639. }
  640. if (disposed || requestWritten)
  641. return;
  642. long length = request.ContentLength;
  643. if (!sendChunked && length != -1 && totalWritten != length) {
  644. IOException io = new IOException ("Cannot close the stream until all bytes are written");
  645. nextReadCalled = true;
  646. cnc.Close (true);
  647. throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
  648. }
  649. WriteRequest ();
  650. disposed = true;
  651. }
  652. internal void KillBuffer ()
  653. {
  654. writeBuffer = null;
  655. }
  656. public override long Seek (long a, SeekOrigin b)
  657. {
  658. throw new NotSupportedException ();
  659. }
  660. public override void SetLength (long a)
  661. {
  662. throw new NotSupportedException ();
  663. }
  664. public override bool CanSeek {
  665. get { return false; }
  666. }
  667. public override bool CanRead {
  668. get { return !disposed && isRead; }
  669. }
  670. public override bool CanWrite {
  671. get { return !disposed && !isRead; }
  672. }
  673. public override long Length {
  674. get { throw new NotSupportedException (); }
  675. }
  676. public override long Position {
  677. get { throw new NotSupportedException (); }
  678. set { throw new NotSupportedException (); }
  679. }
  680. }
  681. }