2003-06-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Thu, 12 Jun 2003 20:00:09 +0000 (20:00 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Thu, 12 Jun 2003 20:00:09 +0000 (20:00 -0000)
* HttpWebRequest.cs: use InternalClose when we are not going to send the
rest of the request stream because of an error after sending the
headers.

* WebConnection.cs: check for completion after setting the response.
Enable reading in NextRead.

* WebConnectionStream.cs: re-fixed the count for partially buffered
reads. If the network stream returns 0 bytes, we're done.

svn path=/trunk/mcs/; revision=15351

mcs/class/System/System.Net/ChangeLog
mcs/class/System/System.Net/HttpWebRequest.cs
mcs/class/System/System.Net/WebConnection.cs
mcs/class/System/System.Net/WebConnectionStream.cs

index d6fc158ba9ebbf7b8a2d1f13e47acfdb93b9d912..f568fb569664b9436556c904c6bfb144c3bc0dcd 100644 (file)
@@ -1,3 +1,15 @@
+2003-06-12  Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * HttpWebRequest.cs: use InternalClose when we are not going to send the
+       rest of the request stream because of an error after sending the
+       headers.
+
+       * WebConnection.cs: check for completion after setting the response.
+       Enable reading in NextRead.
+
+       * WebConnectionStream.cs: re-fixed the count for partially buffered
+       reads. If the network stream returns 0 bytes, we're done.
+
 2003-06-11  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * WebClient.cs: implemented UploadFile. Fixed SetupRequest to set the
index 8e39c4bdd6621c60a17bfdcd61775b325a93e0dd..57d31fee4003453cea58fd3461368d96d65eadf9 100644 (file)
@@ -823,7 +823,7 @@ namespace System.Net
                        }
 
                        if (writeStream != null) {
-                               writeStream.Close ();
+                               writeStream.InternalClose ();
                                writeStream = null;
                        }
 
index eb2bd6ab87ac3c514cbef69a6aad02d57fd486bf..85c2e50ccc70cdd355dd5f680d6211273176afd4 100644 (file)
@@ -150,7 +150,8 @@ namespace System.Net
                        }
 
                        if (nread == 0) {
-                               Console.WriteLine ("nread == 0");
+                               Console.WriteLine ("nread == 0: may be the connection was closed?");
+                               cnc.dataAvailable.Set ();
                                return;
                        }
 
@@ -203,7 +204,6 @@ namespace System.Net
                                stream.ReadBuffer = cnc.buffer;
                                stream.ReadBufferOffset = pos;
                                stream.ReadBufferSize = nread;
-                               stream.CheckComplete ();
                        } else if (cnc.chunkStream == null) {
                                cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
                        } else {
@@ -214,6 +214,7 @@ namespace System.Net
                        cnc.prevStream = stream;
                        data.stream = stream;
                        data.request.SetResponseData (data);
+                       stream.CheckComplete ();
                }
                
                static void InitRead (object state)
@@ -344,7 +345,7 @@ namespace System.Net
                                prevStream.ReadAll ();
                                prevStream = null;
                        }
-                               
+
                        if (!busy) {
                                busy = true;
                                Monitor.Exit (this);
@@ -367,13 +368,15 @@ namespace System.Net
                                Close ();
                        }
 
+                       busy = false;
+                       dataAvailable.Set ();
+
                        if (queue.Count > 0) {
                                HttpWebRequest request = (HttpWebRequest) queue [0];
                                queue.RemoveAt (0);
                                Monitor.Exit (this);
                                SendRequest (request);
                        } else {
-                               busy = false;
                                Monitor.Exit (this);
                        }
                }
index e4e2fff894b9bdc8d7aa9d8d0c2a0e667998890a..83a014c06b910528f5816be7b1059b4bff945f0e 100644 (file)
@@ -30,6 +30,7 @@ namespace System.Net
                MemoryStream writeBuffer;
                bool requestWritten;
                byte [] headers;
+               bool disposed;
 
                public WebConnectionStream (WebConnection cnc)
                {
@@ -170,8 +171,8 @@ namespace System.Net
                                readBufferOffset += copy;
                                offset += copy;
                                size -= copy;
+                               totalRead += copy;
                                if (size == 0 || totalRead >= contentLength) {
-                                       totalRead += copy;
                                        result.SetCompleted (true, copy);
                                        result.DoCallback ();
                                        return result;
@@ -187,20 +188,18 @@ namespace System.Net
                {
                        WebAsyncResult result = (WebAsyncResult) r;
 
-                       int nbytes = -1;
-                       if (result.IsCompleted) {
-                               nbytes = result.NBytes;
-                       } else {
-                               nbytes = cnc.EndRead (result.InnerAsyncResult);
+                       if (!result.IsCompleted) {
+                               int nbytes = cnc.EndRead (result.InnerAsyncResult);
                                lock (this) {
                                        pendingReads--;
                                        if (pendingReads == 0)
                                                pending.Set ();
                                }
 
-                               nbytes += result.NBytes; // partially filled from the read buffer
-                               result.SetCompleted (false, nbytes);
+                               result.SetCompleted (false, nbytes + result.NBytes);
                                totalRead += nbytes;
+                               if (nbytes == 0)
+                                       contentLength = totalRead;
                        }
 
                        if (totalRead >= contentLength && !nextReadCalled) {
@@ -208,7 +207,7 @@ namespace System.Net
                                cnc.NextRead ();
                        }
 
-                       return nbytes;
+                       return result.NBytes;
                }
                
                public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
@@ -300,12 +299,18 @@ namespace System.Net
                        cnc.dataAvailable.Set ();
                }
 
+               internal void InternalClose ()
+               {
+                       disposed = true;
+               }
+               
                public override void Close ()
                {
-                       if (!allowBuffering)
+                       if (isRead || !allowBuffering || disposed)
                                return;
 
-                       // may be ReadAll is isRead?
+                       disposed = true;
+
                        long length = request.ContentLength;
                        if (length != -1 && length > writeBuffer.Length)
                                throw new IOException ("Cannot close the stream until all bytes are written");