* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index d056bf21e01a51cd35c29ee88ba7eaf2cc4cc8eb..37c9eb4b8406ba486b592b2fe23d8b14f0e35562 100644 (file)
@@ -59,6 +59,9 @@ namespace System.Net
                bool headersSent;
                object locker = new object ();
                bool initRead;
+               bool read_eof;
+               bool complete_request_written;
+               long max_buffer_size;
 
                public WebConnectionStream (WebConnection cnc)
                {
@@ -66,8 +69,11 @@ namespace System.Net
                        pending = new ManualResetEvent (true);
                        this.request = cnc.Data.request;
                        this.cnc = cnc;
+                       string contentType = cnc.Data.Headers ["Transfer-Encoding"];
+                       bool chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
                        string clength = cnc.Data.Headers ["Content-Length"];
-                       if (clength != null && clength != "") {
+                       if (!chunkedRead && clength != null && clength != "") {
+
                                try {
                                        contentLength = Int32.Parse (clength);
                                } catch {
@@ -85,13 +91,21 @@ namespace System.Net
                        this.request = request;
                        allowBuffering = request.InternalAllowBuffering;
                        sendChunked = request.SendChunked;
-                       if (allowBuffering)
+                       if (allowBuffering) {
                                writeBuffer = new MemoryStream ();
+                               max_buffer_size = request.ContentLength;
+                       } else {
+                               max_buffer_size = -1;
+                       }
 
                        if (sendChunked)
                                pending = new ManualResetEvent (true);
                }
 
+               internal bool CompleteRequestWritten {
+                       get { return complete_request_written; }
+               }
+
                internal bool SendChunked {
                        set { sendChunked = value; }
                }
@@ -133,8 +147,8 @@ namespace System.Net
 
                internal void ReadAll ()
                {
-                       if (!isRead || totalRead >= contentLength || nextReadCalled) {
-                               if (!nextReadCalled) {
+                       if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
+                               if (isRead && !nextReadCalled) {
                                        nextReadCalled = true;
                                        cnc.NextRead ();
                                }
@@ -233,6 +247,7 @@ namespace System.Net
                        AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
                        WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
                        if (!res.IsCompleted && !res.WaitUntilComplete (request.ReadWriteTimeout, false)) {
+                               nextReadCalled = true;
                                cnc.Close (true);
                                throw new IOException ("Read timed out.");
                        }
@@ -287,7 +302,12 @@ namespace System.Net
                        if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
                                size = contentLength - totalRead;
 
-                       result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, cb, result);
+                       if (!read_eof) {
+                               result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, cb, result);
+                       } else {
+                               result.SetCompleted (true, result.NBytes);
+                               result.DoCallback ();
+                       }
                        return result;
                }
 
@@ -302,15 +322,31 @@ namespace System.Net
                        result.EndCalled = true;
 
                        if (!result.IsCompleted) {
-                               int nbytes = cnc.EndRead (result);
-                               bool finished = (nbytes == -1);
-                               if (finished && result.NBytes > 0)
+                               int nbytes = -1;
+                               try {
+                                       nbytes = cnc.EndRead (result);
+                               } catch (Exception exc) {
+                                       lock (locker) {
+                                               pendingReads--;
+                                               if (pendingReads == 0)
+                                                       pending.Set ();
+                                       }
+
+                                       nextReadCalled = true;
+                                       cnc.Close (true);
+                                       result.SetCompleted (false, exc);
+                                       throw;
+                               }
+
+                               if (nbytes < 0) {
                                        nbytes = 0;
+                                       read_eof = true;
+                               }
 
                                totalRead += nbytes;
                                result.SetCompleted (false, nbytes + result.NBytes);
                                result.DoCallback ();
-                               if (finished || nbytes == 0)
+                               if (nbytes == 0)
                                        contentLength = totalRead;
                        }
 
@@ -349,6 +385,15 @@ namespace System.Net
 
                        WebAsyncResult result = new WebAsyncResult (cb, state);
                        if (allowBuffering) {
+                               if (max_buffer_size >= 0) {
+                                       long avail = max_buffer_size - writeBuffer.Length;
+                                       if (size > avail) {
+                                               if (requestWritten)
+                                                       throw new ProtocolViolationException (
+                                                       "The number of bytes to be written is greater than " +
+                                                       "the specified ContentLength.");
+                                       }
+                               }
                                writeBuffer.Write (buffer, offset, size);
                                if (!sendChunked) {
                                        result.SetCompleted (true, 0);
@@ -425,6 +470,7 @@ namespace System.Net
                        AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
                        WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
                        if (!res.IsCompleted && !res.WaitUntilComplete (request.ReadWriteTimeout, false)) {
+                               nextReadCalled = true;
                                cnc.Close (true);
                                throw new IOException ("Write timed out.");
                        }
@@ -493,14 +539,34 @@ namespace System.Net
                        if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
                                return;
 
-                       cnc.Write (bytes, 0, length);
+                       IAsyncResult result = null;
+                       if (length > 0)
+                               result = cnc.BeginWrite (bytes, 0, length, null, null);
+
+                       if (!initRead) {
+                               initRead = true;
+                               WebConnection.InitRead (cnc);
+                       }
+
+                       if (length > 0) 
+                               complete_request_written = cnc.EndWrite (result);
+                       else
+                               complete_request_written = true;
                }
 
                internal void InternalClose ()
                {
                        disposed = true;
                }
-               
+
+               internal void ForceCloseConnection ()
+               {
+                       if (!disposed) {
+                               disposed = true;
+                               cnc.Close (true);
+                       }
+               }
+
                public override void Close ()
                {
                        if (sendChunked) {
@@ -514,11 +580,14 @@ namespace System.Net
                                if (!nextReadCalled) {
                                        CheckComplete ();
                                        // If we have not read all the contents
-                                       if (!nextReadCalled)
+                                       if (!nextReadCalled) {
+                                               nextReadCalled = true;
                                                cnc.Close (true);
+                                       }
                                }
                                return;
                        } else if (!allowBuffering) {
+                               complete_request_written = true;
                                if (!initRead) {
                                        initRead = true;
                                        WebConnection.InitRead (cnc);
@@ -529,17 +598,12 @@ namespace System.Net
                        if (disposed)
                                return;
 
-                       disposed = true;
-
                        long length = request.ContentLength;
                        if (length != -1 && length > writeBuffer.Length)
                                throw new IOException ("Cannot close the stream until all bytes are written");
 
                        WriteRequest ();
-                       if (!initRead) {
-                               initRead = true;
-                               WebConnection.InitRead (cnc);
-                       }
+                       disposed = true;
                }
 
                public override long Seek (long a, SeekOrigin b)