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 if (totalRead >= contentLength) {
363 result.SetCompleted (true, -1);
364 result.DoCallback ();
368 int remaining = readBufferSize - readBufferOffset;
370 int copy = (remaining > size) ? size : remaining;
371 Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
372 readBufferOffset += copy;
376 if (size == 0 || totalRead >= contentLength) {
377 result.SetCompleted (true, copy);
378 result.DoCallback ();
381 result.NBytes = copy;
387 if (contentLength != Int64.MaxValue && contentLength - totalRead < size)
388 size = (int)(contentLength - totalRead);
391 result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
393 result.SetCompleted (true, result.NBytes);
394 result.DoCallback ();
399 public override int EndRead (IAsyncResult r)
401 WebAsyncResult result = (WebAsyncResult) r;
402 if (result.EndCalled) {
403 int xx = result.NBytes;
404 return (xx >= 0) ? xx : 0;
407 result.EndCalled = true;
409 if (!result.IsCompleted) {
412 nbytes = cnc.EndRead (request, result);
413 } catch (Exception exc) {
416 if (pendingReads == 0)
420 nextReadCalled = true;
422 result.SetCompleted (false, exc);
423 result.DoCallback ();
433 result.SetCompleted (false, nbytes + result.NBytes);
434 result.DoCallback ();
436 contentLength = totalRead;
441 if (pendingReads == 0)
445 if (totalRead >= contentLength && !nextReadCalled)
448 int nb = result.NBytes;
449 return (nb >= 0) ? nb : 0;
452 void WriteAsyncCB (IAsyncResult r)
454 WebAsyncResult result = (WebAsyncResult) r.AsyncState;
455 result.InnerAsyncResult = null;
458 cnc.EndWrite (request, true, r);
459 result.SetCompleted (false, 0);
462 WebConnection.InitRead (cnc);
464 } catch (Exception e) {
466 nextReadCalled = true;
468 if (e is System.Net.Sockets.SocketException)
469 e = new IOException ("Error writing request", e);
470 result.SetCompleted (false, e);
473 if (allowBuffering && !sendChunked && request.ContentLength > 0 && totalWritten == request.ContentLength)
474 complete_request_written = true;
476 result.DoCallback ();
479 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
480 AsyncCallback cb, object state)
483 throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
486 throw new NotSupportedException ("this stream does not allow writing");
489 throw new ArgumentNullException ("buffer");
491 int length = buffer.Length;
492 if (offset < 0 || length < offset)
493 throw new ArgumentOutOfRangeException ("offset");
494 if (size < 0 || (length - offset) < size)
495 throw new ArgumentOutOfRangeException ("size");
504 WebAsyncResult result = new WebAsyncResult (cb, state);
505 AsyncCallback callback = new AsyncCallback (WriteAsyncCB);
508 requestWritten = true;
510 string cSize = String.Format ("{0:X}\r\n", size);
511 byte[] head = Encoding.ASCII.GetBytes (cSize);
512 int chunkSize = 2 + size + head.Length;
513 byte[] newBuffer = new byte [chunkSize];
514 Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
515 Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
516 Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
518 if (allowBuffering) {
519 if (writeBuffer == null)
520 writeBuffer = new MemoryStream ();
521 writeBuffer.Write (buffer, offset, size);
522 totalWritten += size;
529 CheckWriteOverflow (request.ContentLength, totalWritten, size);
531 if (allowBuffering) {
532 if (writeBuffer == null)
533 writeBuffer = new MemoryStream ();
534 writeBuffer.Write (buffer, offset, size);
535 totalWritten += size;
537 if (request.ContentLength <= 0 || totalWritten < request.ContentLength) {
538 result.SetCompleted (true, 0);
539 result.DoCallback ();
543 result.AsyncWriteAll = true;
544 requestWritten = true;
545 buffer = writeBuffer.GetBuffer ();
547 size = (int)totalWritten;
552 result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
553 if (result.InnerAsyncResult == null) {
554 if (!result.IsCompleted)
555 result.SetCompleted (true, 0);
556 result.DoCallback ();
558 } catch (Exception) {
561 result.SetCompleted (true, 0);
562 result.DoCallback ();
564 totalWritten += size;
568 void CheckWriteOverflow (long contentLength, long totalWritten, long size)
570 if (contentLength == -1)
573 long avail = contentLength - totalWritten;
576 nextReadCalled = true;
578 throw new ProtocolViolationException (
579 "The number of bytes to be written is greater than " +
580 "the specified ContentLength.");
584 public override void EndWrite (IAsyncResult r)
587 throw new ArgumentNullException ("r");
589 WebAsyncResult result = r as WebAsyncResult;
591 throw new ArgumentException ("Invalid IAsyncResult");
593 if (result.EndCalled)
599 if (pendingWrites <= 0)
604 result.EndCalled = true;
605 if (result.AsyncWriteAll) {
606 result.WaitUntilComplete ();
607 if (result.GotException)
608 throw result.Exception;
612 if (allowBuffering && !sendChunked)
615 if (result.GotException)
616 throw result.Exception;
619 public override void Write (byte [] buffer, int offset, int size)
621 AsyncCallback cb = cb_wrapper;
622 WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
623 if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
625 nextReadCalled = true;
627 throw new IOException ("Write timed out.");
633 public override void Flush ()
637 internal void SetHeadersAsync (bool setInternalLength, SimpleAsyncCallback callback)
639 SimpleAsyncResult.Run (r => SetHeadersAsync (r, setInternalLength), callback);
642 bool SetHeadersAsync (SimpleAsyncResult result, bool setInternalLength)
647 string method = request.Method;
648 bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
650 bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
651 method == "COPY" || method == "MOVE" || method == "LOCK" ||
654 if (setInternalLength && !no_writestream && writeBuffer != null)
655 request.InternalContentLength = writeBuffer.Length;
657 bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1);
658 if (!(sendChunked || has_content || no_writestream || webdav))
662 headers = request.GetRequestHeaders ();
664 var innerResult = cnc.BeginWrite (request, headers, 0, headers.Length, r => {
666 cnc.EndWrite (request, true, r);
669 WebConnection.InitRead (cnc);
671 var cl = request.ContentLength;
672 if (!sendChunked && cl == 0)
673 requestWritten = true;
674 result.SetCompleted (false);
675 } catch (WebException e) {
676 result.SetCompleted (false, e);
677 } catch (Exception e) {
678 result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
682 return innerResult != null;
685 internal bool RequestWritten {
686 get { return requestWritten; }
689 internal SimpleAsyncResult WriteRequestAsync (SimpleAsyncCallback callback)
691 var result = WriteRequestAsync (callback);
693 if (!WriteRequestAsync (result))
694 result.SetCompleted (true);
695 } catch (Exception ex) {
696 result.SetCompleted (true, ex);
701 internal bool WriteRequestAsync (SimpleAsyncResult result)
706 requestWritten = true;
707 if (sendChunked || !allowBuffering || writeBuffer == null)
710 // Keep the call for a potential side-effect of GetBuffer
711 var bytes = writeBuffer.GetBuffer ();
712 var length = (int)writeBuffer.Length;
713 if (request.ContentLength != -1 && request.ContentLength < length) {
714 nextReadCalled = true;
716 throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
717 WebExceptionStatus.ServerProtocolViolation, null);
720 SetHeadersAsync (true, inner => {
721 if (inner.GotException) {
722 result.SetCompleted (inner.CompletedSynchronously, inner.Exception);
726 if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) {
727 result.SetCompleted (inner.CompletedSynchronously);
733 WebConnection.InitRead (cnc);
737 complete_request_written = true;
738 result.SetCompleted (inner.CompletedSynchronously);
742 cnc.BeginWrite (request, bytes, 0, length, r => {
744 complete_request_written = cnc.EndWrite (request, false, r);
745 result.SetCompleted (false);
746 } catch (Exception exc) {
747 result.SetCompleted (false, exc);
755 internal void InternalClose ()
760 internal bool GetResponseOnClose {
764 public override void Close ()
766 if (GetResponseOnClose) {
770 var response = (HttpWebResponse)request.GetResponse ();
780 if (!pending.WaitOne (WriteTimeout)) {
781 throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
783 byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
784 string err_msg = null;
785 cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
790 if (!nextReadCalled) {
792 // If we have not read all the contents
793 if (!nextReadCalled) {
794 nextReadCalled = true;
799 } else if (!allowBuffering) {
800 complete_request_written = true;
803 WebConnection.InitRead (cnc);
808 if (disposed || requestWritten)
811 long length = request.ContentLength;
813 if (!sendChunked && length != -1 && totalWritten != length) {
814 IOException io = new IOException ("Cannot close the stream until all bytes are written");
815 nextReadCalled = true;
817 throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
820 // Commented out the next line to fix xamarin bug #1512
825 internal void KillBuffer ()
830 public override long Seek (long a, SeekOrigin b)
832 throw new NotSupportedException ();
835 public override void SetLength (long a)
837 throw new NotSupportedException ();
840 public override bool CanSeek {
841 get { return false; }
844 public override bool CanRead {
845 get { return !disposed && isRead; }
848 public override bool CanWrite {
849 get { return !disposed && !isRead; }
852 public override long Length {
855 throw new NotSupportedException ();
856 return stream_length;
860 public override long Position {
861 get { throw new NotSupportedException (); }
862 set { throw new NotSupportedException (); }