2 // System.Net.WebConnectionStream
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 // (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Threading;
38 class WebConnectionStream : Stream
40 static byte [] crlf = new byte [] { 13, 10 };
43 HttpWebRequest request;
47 int stream_length; // -1 when CL not present
50 internal long totalWritten;
54 ManualResetEvent pending;
57 MemoryStream writeBuffer;
62 object locker = new object ();
65 bool complete_request_written;
68 AsyncCallback cb_wrapper; // Calls to ReadCallbackWrapper or WriteCallbacWrapper
69 internal bool IgnoreIOErrors;
71 public WebConnectionStream (WebConnection cnc, WebConnectionData data)
74 throw new InvalidOperationException ("data was not initialized");
75 if (data.Headers == null)
76 throw new InvalidOperationException ("data.Headers was not initialized");
77 if (data.request == null)
78 throw new InvalidOperationException ("data.request was not initialized");
80 cb_wrapper = new AsyncCallback (ReadCallbackWrapper);
81 pending = new ManualResetEvent (true);
82 this.request = data.request;
83 read_timeout = request.ReadWriteTimeout;
84 write_timeout = read_timeout;
86 string contentType = data.Headers ["Transfer-Encoding"];
87 bool chunkedRead = (contentType != null && contentType.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
88 string clength = data.Headers ["Content-Length"];
89 if (!chunkedRead && clength != null && clength != "") {
91 contentLength = Int32.Parse (clength);
92 if (contentLength == 0 && !IsNtlmAuth ()) {
96 contentLength = Int32.MaxValue;
99 contentLength = Int32.MaxValue;
103 if (!Int32.TryParse (clength, out stream_length))
107 public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
109 read_timeout = request.ReadWriteTimeout;
110 write_timeout = read_timeout;
112 cb_wrapper = new AsyncCallback (WriteCallbackWrapper);
114 this.request = request;
115 allowBuffering = request.InternalAllowBuffering;
116 sendChunked = request.SendChunked;
118 pending = new ManualResetEvent (true);
119 else if (allowBuffering)
120 writeBuffer = new MemoryStream ();
123 bool CheckAuthHeader (string headerName)
125 var authHeader = cnc.Data.Headers [headerName];
126 return (authHeader != null && authHeader.IndexOf ("NTLM", StringComparison.Ordinal) != -1);
131 bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
132 if (isProxy && CheckAuthHeader ("Proxy-Authenticate"))
134 return CheckAuthHeader ("WWW-Authenticate");
137 internal void CheckResponseInBuffer ()
139 if (contentLength > 0 && (readBufferSize - readBufferOffset) >= contentLength) {
145 internal HttpWebRequest Request {
146 get { return request; }
149 internal WebConnection Connection {
152 public override bool CanTimeout {
156 public override int ReadTimeout {
163 throw new ArgumentOutOfRangeException ("value");
164 read_timeout = value;
168 public override int WriteTimeout {
170 return write_timeout;
175 throw new ArgumentOutOfRangeException ("value");
176 write_timeout = value;
180 internal bool CompleteRequestWritten {
181 get { return complete_request_written; }
184 internal bool SendChunked {
185 set { sendChunked = value; }
188 internal byte [] ReadBuffer {
189 set { readBuffer = value; }
192 internal int ReadBufferOffset {
193 set { readBufferOffset = value; }
196 internal int ReadBufferSize {
197 set { readBufferSize = value; }
200 internal byte[] WriteBuffer {
201 get { return writeBuffer.GetBuffer (); }
204 internal int WriteBufferLength {
205 get { return writeBuffer != null ? (int) writeBuffer.Length : (-1); }
208 internal void ForceCompletion ()
210 if (!nextReadCalled) {
211 if (contentLength == Int32.MaxValue)
213 nextReadCalled = true;
218 internal void CheckComplete ()
220 bool nrc = nextReadCalled;
221 if (!nrc && readBufferSize - readBufferOffset == contentLength) {
222 nextReadCalled = true;
227 internal void ReadAll ()
229 if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
230 if (isRead && !nextReadCalled) {
231 nextReadCalled = true;
239 if (totalRead >= contentLength)
243 int diff = readBufferSize - readBufferOffset;
246 if (contentLength == Int32.MaxValue) {
247 MemoryStream ms = new MemoryStream ();
248 byte [] buffer = null;
249 if (readBuffer != null && diff > 0) {
250 ms.Write (readBuffer, readBufferOffset, diff);
251 if (readBufferSize >= 8192)
256 buffer = new byte [8192];
259 while ((read = cnc.Read (request, buffer, 0, buffer.Length)) != 0)
260 ms.Write (buffer, 0, read);
263 new_size = (int) ms.Length;
264 contentLength = new_size;
266 new_size = contentLength - totalRead;
267 b = new byte [new_size];
268 if (readBuffer != null && diff > 0) {
272 Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
275 int remaining = new_size - diff;
277 while (remaining > 0 && r != 0) {
278 r = cnc.Read (request, b, diff, remaining);
285 readBufferOffset = 0;
286 readBufferSize = new_size;
288 nextReadCalled = true;
294 void WriteCallbackWrapper (IAsyncResult r)
296 WebAsyncResult result = r as WebAsyncResult;
297 if (result != null && result.AsyncWriteAll)
300 if (r.AsyncState != null) {
301 result = (WebAsyncResult) r.AsyncState;
302 result.InnerAsyncResult = r;
303 result.DoCallback ();
312 void ReadCallbackWrapper (IAsyncResult r)
314 WebAsyncResult result;
315 if (r.AsyncState != null) {
316 result = (WebAsyncResult) r.AsyncState;
317 result.InnerAsyncResult = r;
318 result.DoCallback ();
327 public override int Read (byte [] buffer, int offset, int size)
329 AsyncCallback cb = cb_wrapper;
330 WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
331 if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
332 nextReadCalled = true;
334 throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
337 return EndRead (res);
340 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
341 AsyncCallback cb, object state)
344 throw new NotSupportedException ("this stream does not allow reading");
347 throw new ArgumentNullException ("buffer");
349 int length = buffer.Length;
350 if (offset < 0 || length < offset)
351 throw new ArgumentOutOfRangeException ("offset");
352 if (size < 0 || (length - offset) < size)
353 throw new ArgumentOutOfRangeException ("size");
360 WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
361 if (totalRead >= contentLength) {
362 result.SetCompleted (true, -1);
363 result.DoCallback ();
367 int remaining = readBufferSize - readBufferOffset;
369 int copy = (remaining > size) ? size : remaining;
370 Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
371 readBufferOffset += copy;
375 if (size == 0 || totalRead >= contentLength) {
376 result.SetCompleted (true, copy);
377 result.DoCallback ();
380 result.NBytes = copy;
386 if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
387 size = contentLength - totalRead;
390 result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
392 result.SetCompleted (true, result.NBytes);
393 result.DoCallback ();
398 public override int EndRead (IAsyncResult r)
400 WebAsyncResult result = (WebAsyncResult) r;
401 if (result.EndCalled) {
402 int xx = result.NBytes;
403 return (xx >= 0) ? xx : 0;
406 result.EndCalled = true;
408 if (!result.IsCompleted) {
411 nbytes = cnc.EndRead (request, result);
412 } catch (Exception exc) {
415 if (pendingReads == 0)
419 nextReadCalled = true;
421 result.SetCompleted (false, exc);
422 result.DoCallback ();
432 result.SetCompleted (false, nbytes + result.NBytes);
433 result.DoCallback ();
435 contentLength = totalRead;
440 if (pendingReads == 0)
444 if (totalRead >= contentLength && !nextReadCalled)
447 int nb = result.NBytes;
448 return (nb >= 0) ? nb : 0;
451 void WriteAsyncCB (IAsyncResult r)
453 WebAsyncResult result = (WebAsyncResult) r.AsyncState;
454 result.InnerAsyncResult = null;
457 cnc.EndWrite (request, true, r);
458 result.SetCompleted (false, 0);
461 WebConnection.InitRead (cnc);
463 } catch (Exception e) {
465 nextReadCalled = true;
467 if (e is System.Net.Sockets.SocketException)
468 e = new IOException ("Error writing request", e);
469 result.SetCompleted (false, e);
472 if (allowBuffering && !sendChunked && request.ContentLength > 0 && totalWritten == request.ContentLength)
473 complete_request_written = true;
475 result.DoCallback ();
478 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
479 AsyncCallback cb, object state)
482 throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
485 throw new NotSupportedException ("this stream does not allow writing");
488 throw new ArgumentNullException ("buffer");
490 int length = buffer.Length;
491 if (offset < 0 || length < offset)
492 throw new ArgumentOutOfRangeException ("offset");
493 if (size < 0 || (length - offset) < size)
494 throw new ArgumentOutOfRangeException ("size");
503 WebAsyncResult result = new WebAsyncResult (cb, state);
504 AsyncCallback callback = new AsyncCallback (WriteAsyncCB);
507 requestWritten = true;
509 string cSize = String.Format ("{0:X}\r\n", size);
510 byte[] head = Encoding.ASCII.GetBytes (cSize);
511 int chunkSize = 2 + size + head.Length;
512 byte[] newBuffer = new byte [chunkSize];
513 Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
514 Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
515 Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
517 if (allowBuffering) {
518 if (writeBuffer == null)
519 writeBuffer = new MemoryStream ();
520 writeBuffer.Write (buffer, offset, size);
521 totalWritten += size;
528 CheckWriteOverflow (request.ContentLength, totalWritten, size);
530 if (allowBuffering) {
531 if (writeBuffer == null)
532 writeBuffer = new MemoryStream ();
533 writeBuffer.Write (buffer, offset, size);
534 totalWritten += size;
536 if (request.ContentLength <= 0 || totalWritten < request.ContentLength) {
537 result.SetCompleted (true, 0);
538 result.DoCallback ();
542 result.AsyncWriteAll = true;
543 requestWritten = true;
544 buffer = writeBuffer.GetBuffer ();
546 size = (int)totalWritten;
551 result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
552 if (result.InnerAsyncResult == null) {
553 if (!result.IsCompleted)
554 result.SetCompleted (true, 0);
555 result.DoCallback ();
557 } catch (Exception) {
560 result.SetCompleted (true, 0);
561 result.DoCallback ();
563 totalWritten += size;
567 void CheckWriteOverflow (long contentLength, long totalWritten, long size)
569 if (contentLength == -1)
572 long avail = contentLength - totalWritten;
575 nextReadCalled = true;
577 throw new ProtocolViolationException (
578 "The number of bytes to be written is greater than " +
579 "the specified ContentLength.");
583 public override void EndWrite (IAsyncResult r)
586 throw new ArgumentNullException ("r");
588 WebAsyncResult result = r as WebAsyncResult;
590 throw new ArgumentException ("Invalid IAsyncResult");
592 if (result.EndCalled)
595 result.EndCalled = true;
596 if (result.AsyncWriteAll) {
597 result.WaitUntilComplete ();
598 if (result.GotException)
599 throw result.Exception;
603 if (allowBuffering && !sendChunked)
606 if (result.GotException)
607 throw result.Exception;
612 if (pendingWrites == 0)
618 public override void Write (byte [] buffer, int offset, int size)
620 AsyncCallback cb = cb_wrapper;
621 WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
622 if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
624 nextReadCalled = true;
626 throw new IOException ("Write timed out.");
632 public override void Flush ()
636 internal void SetHeadersAsync (bool setInternalLength, SimpleAsyncCallback callback)
638 SimpleAsyncResult.Run (r => SetHeadersAsync (r, setInternalLength), callback);
641 bool SetHeadersAsync (SimpleAsyncResult result, bool setInternalLength)
646 string method = request.Method;
647 bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
649 bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
650 method == "COPY" || method == "MOVE" || method == "LOCK" ||
653 if (setInternalLength && !no_writestream && writeBuffer != null)
654 request.InternalContentLength = writeBuffer.Length;
656 if (!(sendChunked || request.ContentLength > -1 || no_writestream || webdav))
660 headers = request.GetRequestHeaders ();
662 var innerResult = cnc.BeginWrite (request, headers, 0, headers.Length, r => {
664 cnc.EndWrite (request, true, r);
667 WebConnection.InitRead (cnc);
669 var cl = request.ContentLength;
670 if (!sendChunked && cl == 0)
671 requestWritten = true;
672 result.SetCompleted (false);
673 } catch (WebException e) {
674 result.SetCompleted (false, e);
675 } catch (Exception e) {
676 result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
680 return innerResult != null;
683 internal bool RequestWritten {
684 get { return requestWritten; }
687 internal SimpleAsyncResult WriteRequestAsync (SimpleAsyncCallback callback)
689 var result = WriteRequestAsync (callback);
691 if (!WriteRequestAsync (result))
692 result.SetCompleted (true);
693 } catch (Exception ex) {
694 result.SetCompleted (true, ex);
699 internal bool WriteRequestAsync (SimpleAsyncResult result)
704 requestWritten = true;
705 if (sendChunked || !allowBuffering || writeBuffer == null)
708 // Keep the call for a potential side-effect of GetBuffer
709 var bytes = writeBuffer.GetBuffer ();
710 var length = (int)writeBuffer.Length;
711 if (request.ContentLength != -1 && request.ContentLength < length) {
712 nextReadCalled = true;
714 throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
715 WebExceptionStatus.ServerProtocolViolation, null);
718 SetHeadersAsync (true, inner => {
719 if (inner.GotException) {
720 result.SetCompleted (inner.CompletedSynchronously, inner.Exception);
724 if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) {
725 result.SetCompleted (inner.CompletedSynchronously);
731 WebConnection.InitRead (cnc);
735 complete_request_written = true;
736 result.SetCompleted (inner.CompletedSynchronously);
740 cnc.BeginWrite (request, bytes, 0, length, r => {
742 complete_request_written = cnc.EndWrite (request, false, r);
743 result.SetCompleted (false);
744 } catch (Exception exc) {
745 result.SetCompleted (false, exc);
753 internal void InternalClose ()
758 internal bool GetResponseOnClose {
762 public override void Close ()
764 if (GetResponseOnClose) {
768 var response = (HttpWebResponse)request.GetResponse ();
779 byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
780 string err_msg = null;
781 cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
786 if (!nextReadCalled) {
788 // If we have not read all the contents
789 if (!nextReadCalled) {
790 nextReadCalled = true;
795 } else if (!allowBuffering) {
796 complete_request_written = true;
799 WebConnection.InitRead (cnc);
804 if (disposed || requestWritten)
807 long length = request.ContentLength;
809 if (!sendChunked && length != -1 && totalWritten != length) {
810 IOException io = new IOException ("Cannot close the stream until all bytes are written");
811 nextReadCalled = true;
813 throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
816 // Commented out the next line to fix xamarin bug #1512
821 internal void KillBuffer ()
826 public override long Seek (long a, SeekOrigin b)
828 throw new NotSupportedException ();
831 public override void SetLength (long a)
833 throw new NotSupportedException ();
836 public override bool CanSeek {
837 get { return false; }
840 public override bool CanRead {
841 get { return !disposed && isRead; }
844 public override bool CanWrite {
845 get { return !disposed && !isRead; }
848 public override long Length {
851 throw new NotSupportedException ();
852 return stream_length;
856 public override long Position {
857 get { throw new NotSupportedException (); }
858 set { throw new NotSupportedException (); }