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 = Int64.MaxValue;
99 contentLength = Int64.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 == Int64.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;
237 if (!pending.WaitOne (ReadTimeout))
238 throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
240 if (totalRead >= contentLength)
244 int diff = readBufferSize - readBufferOffset;
247 if (contentLength == Int64.MaxValue) {
248 MemoryStream ms = new MemoryStream ();
249 byte [] buffer = null;
250 if (readBuffer != null && diff > 0) {
251 ms.Write (readBuffer, readBufferOffset, diff);
252 if (readBufferSize >= 8192)
257 buffer = new byte [8192];
260 while ((read = cnc.Read (request, buffer, 0, buffer.Length)) != 0)
261 ms.Write (buffer, 0, read);
264 new_size = (int) ms.Length;
265 contentLength = new_size;
267 new_size = (int) (contentLength - totalRead);
268 b = new byte [new_size];
269 if (readBuffer != null && diff > 0) {
273 Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
276 int remaining = new_size - diff;
278 while (remaining > 0 && r != 0) {
279 r = cnc.Read (request, b, diff, remaining);
286 readBufferOffset = 0;
287 readBufferSize = new_size;
289 nextReadCalled = true;
295 void WriteCallbackWrapper (IAsyncResult r)
297 WebAsyncResult result = r as WebAsyncResult;
298 if (result != null && result.AsyncWriteAll)
301 if (r.AsyncState != null) {
302 result = (WebAsyncResult) r.AsyncState;
303 result.InnerAsyncResult = r;
304 result.DoCallback ();
313 void ReadCallbackWrapper (IAsyncResult r)
315 WebAsyncResult result;
316 if (r.AsyncState != null) {
317 result = (WebAsyncResult) r.AsyncState;
318 result.InnerAsyncResult = r;
319 result.DoCallback ();
328 public override int Read (byte [] buffer, int offset, int size)
330 AsyncCallback cb = cb_wrapper;
331 WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
332 if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
333 nextReadCalled = true;
335 throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
338 return EndRead (res);
341 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
342 AsyncCallback cb, object state)
345 throw new NotSupportedException ("this stream does not allow reading");
348 throw new ArgumentNullException ("buffer");
350 int length = buffer.Length;
351 if (offset < 0 || length < offset)
352 throw new ArgumentOutOfRangeException ("offset");
353 if (size < 0 || (length - offset) < size)
354 throw new ArgumentOutOfRangeException ("size");
361 WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
362 result.AsyncObject = request;
363 if (totalRead >= contentLength) {
364 result.SetCompleted (true, -1);
365 result.DoCallback ();
369 int remaining = readBufferSize - readBufferOffset;
371 int copy = (remaining > size) ? size : remaining;
372 Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
373 readBufferOffset += copy;
377 if (size == 0 || totalRead >= contentLength) {
378 result.SetCompleted (true, copy);
379 result.DoCallback ();
382 result.NBytes = copy;
388 if (contentLength != Int64.MaxValue && contentLength - totalRead < size)
389 size = (int)(contentLength - totalRead);
392 cnc.ReadAsync (request, buffer, offset, size, result);
394 result.SetCompleted (true, result.NBytes);
395 result.DoCallback ();
400 public override int EndRead (IAsyncResult r)
402 WebAsyncResult result = (WebAsyncResult) r;
403 int nb = result.NBytes;
405 if (result.EndCalled)
406 return (nb >= 0) ? nb : 0;
407 result.EndCalled = true;
411 if (pendingReads == 0)
415 if (result.GotException) {
416 nextReadCalled = true;
418 throw result.Exception;
424 totalRead += result.NBytes;
426 contentLength = totalRead;
429 if (totalRead >= contentLength && !nextReadCalled)
432 return (nb >= 0) ? nb : 0;
435 void WriteAsyncCB (IAsyncResult r)
437 WebAsyncResult result = (WebAsyncResult) r.AsyncState;
438 result.InnerAsyncResult = null;
441 cnc.EndWrite (request, true, r);
442 result.SetCompleted (false, 0);
445 WebConnection.InitRead (cnc);
447 } catch (Exception e) {
449 nextReadCalled = true;
451 if (e is System.Net.Sockets.SocketException)
452 e = new IOException ("Error writing request", e);
453 result.SetCompleted (false, e);
456 if (allowBuffering && !sendChunked && request.ContentLength > 0 && totalWritten == request.ContentLength)
457 complete_request_written = true;
459 result.DoCallback ();
462 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
463 AsyncCallback cb, object state)
466 throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
469 throw new NotSupportedException ("this stream does not allow writing");
472 throw new ArgumentNullException ("buffer");
474 int length = buffer.Length;
475 if (offset < 0 || length < offset)
476 throw new ArgumentOutOfRangeException ("offset");
477 if (size < 0 || (length - offset) < size)
478 throw new ArgumentOutOfRangeException ("size");
487 WebAsyncResult result = new WebAsyncResult (cb, state);
488 AsyncCallback callback = new AsyncCallback (WriteAsyncCB);
491 requestWritten = true;
493 string cSize = String.Format ("{0:X}\r\n", size);
494 byte[] head = Encoding.ASCII.GetBytes (cSize);
495 int chunkSize = 2 + size + head.Length;
496 byte[] newBuffer = new byte [chunkSize];
497 Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
498 Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
499 Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
501 if (allowBuffering) {
502 if (writeBuffer == null)
503 writeBuffer = new MemoryStream ();
504 writeBuffer.Write (buffer, offset, size);
505 totalWritten += size;
512 CheckWriteOverflow (request.ContentLength, totalWritten, size);
514 if (allowBuffering) {
515 if (writeBuffer == null)
516 writeBuffer = new MemoryStream ();
517 writeBuffer.Write (buffer, offset, size);
518 totalWritten += size;
520 if (request.ContentLength <= 0 || totalWritten < request.ContentLength) {
521 result.SetCompleted (true, 0);
522 result.DoCallback ();
526 result.AsyncWriteAll = true;
527 requestWritten = true;
528 buffer = writeBuffer.GetBuffer ();
530 size = (int)totalWritten;
535 result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
536 if (result.InnerAsyncResult == null) {
537 if (!result.IsCompleted)
538 result.SetCompleted (true, 0);
539 result.DoCallback ();
541 } catch (Exception) {
544 result.SetCompleted (true, 0);
545 result.DoCallback ();
547 totalWritten += size;
551 void CheckWriteOverflow (long contentLength, long totalWritten, long size)
553 if (contentLength == -1)
556 long avail = contentLength - totalWritten;
559 nextReadCalled = true;
561 throw new ProtocolViolationException (
562 "The number of bytes to be written is greater than " +
563 "the specified ContentLength.");
567 public override void EndWrite (IAsyncResult r)
570 throw new ArgumentNullException ("r");
572 WebAsyncResult result = r as WebAsyncResult;
574 throw new ArgumentException ("Invalid IAsyncResult");
576 if (result.EndCalled)
582 if (pendingWrites <= 0)
587 result.EndCalled = true;
588 if (result.AsyncWriteAll) {
589 result.WaitUntilComplete ();
590 if (result.GotException)
591 throw result.Exception;
595 if (allowBuffering && !sendChunked)
598 if (result.GotException)
599 throw result.Exception;
602 public override void Write (byte [] buffer, int offset, int size)
604 AsyncCallback cb = cb_wrapper;
605 WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
606 if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
608 nextReadCalled = true;
610 throw new IOException ("Write timed out.");
616 public override void Flush ()
620 internal void SetHeadersAsync (bool setInternalLength, SimpleAsyncCallback callback)
622 SimpleAsyncResult.Run (r => SetHeadersAsync (r, setInternalLength), callback);
625 bool SetHeadersAsync (SimpleAsyncResult result, bool setInternalLength)
630 string method = request.Method;
631 bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
633 bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
634 method == "COPY" || method == "MOVE" || method == "LOCK" ||
637 if (setInternalLength && !no_writestream && writeBuffer != null)
638 request.InternalContentLength = writeBuffer.Length;
640 bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1);
641 if (!(sendChunked || has_content || no_writestream || webdav))
645 headers = request.GetRequestHeaders ();
647 var innerResult = cnc.BeginWrite (request, headers, 0, headers.Length, r => {
649 cnc.EndWrite (request, true, r);
652 WebConnection.InitRead (cnc);
654 var cl = request.ContentLength;
655 if (!sendChunked && cl == 0)
656 requestWritten = true;
657 result.SetCompleted (false);
658 } catch (WebException e) {
659 result.SetCompleted (false, e);
660 } catch (Exception e) {
661 result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
665 return innerResult != null;
668 internal bool RequestWritten {
669 get { return requestWritten; }
672 internal SimpleAsyncResult WriteRequestAsync (SimpleAsyncCallback callback)
674 var result = WriteRequestAsync (callback);
676 if (!WriteRequestAsync (result))
677 result.SetCompleted (true);
678 } catch (Exception ex) {
679 result.SetCompleted (true, ex);
684 internal bool WriteRequestAsync (SimpleAsyncResult result)
689 requestWritten = true;
690 if (sendChunked || !allowBuffering || writeBuffer == null)
693 // Keep the call for a potential side-effect of GetBuffer
694 var bytes = writeBuffer.GetBuffer ();
695 var length = (int)writeBuffer.Length;
696 if (request.ContentLength != -1 && request.ContentLength < length) {
697 nextReadCalled = true;
699 throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
700 WebExceptionStatus.ServerProtocolViolation, null);
703 SetHeadersAsync (true, inner => {
704 if (inner.GotException) {
705 result.SetCompleted (inner.CompletedSynchronously, inner.Exception);
709 if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) {
710 result.SetCompleted (inner.CompletedSynchronously);
716 WebConnection.InitRead (cnc);
720 complete_request_written = true;
721 result.SetCompleted (inner.CompletedSynchronously);
725 cnc.BeginWrite (request, bytes, 0, length, r => {
727 complete_request_written = cnc.EndWrite (request, false, r);
728 result.SetCompleted (false);
729 } catch (Exception exc) {
730 result.SetCompleted (false, exc);
738 internal void InternalClose ()
743 internal bool GetResponseOnClose {
747 public override void Close ()
749 if (GetResponseOnClose) {
753 var response = (HttpWebResponse)request.GetResponse ();
763 if (!pending.WaitOne (WriteTimeout)) {
764 throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
766 byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
767 string err_msg = null;
768 cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
773 if (!nextReadCalled) {
775 // If we have not read all the contents
776 if (!nextReadCalled) {
777 nextReadCalled = true;
782 } else if (!allowBuffering) {
783 complete_request_written = true;
786 WebConnection.InitRead (cnc);
791 if (disposed || requestWritten)
794 long length = request.ContentLength;
796 if (!sendChunked && length != -1 && totalWritten != length) {
797 IOException io = new IOException ("Cannot close the stream until all bytes are written");
798 nextReadCalled = true;
800 throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
803 // Commented out the next line to fix xamarin bug #1512
808 internal void KillBuffer ()
813 public override long Seek (long a, SeekOrigin b)
815 throw new NotSupportedException ();
818 public override void SetLength (long a)
820 throw new NotSupportedException ();
823 public override bool CanSeek {
824 get { return false; }
827 public override bool CanRead {
828 get { return !disposed && isRead; }
831 public override bool CanWrite {
832 get { return !disposed && !isRead; }
835 public override long Length {
838 throw new NotSupportedException ();
839 return stream_length;
843 public override long Position {
844 get { throw new NotSupportedException (); }
845 set { throw new NotSupportedException (); }