Merge pull request #1991 from esdrubal/seq_test_fix
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index 8ef558d72fd997d284c76319ee04b76159d0b7e6..3862b3e4eff56284f11f2d1f768ce139c75e2c02 100644 (file)
@@ -234,7 +234,8 @@ namespace System.Net
                                return;
                        }
 
-                       pending.WaitOne ();
+                       if (!pending.WaitOne (ReadTimeout))
+                               throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
                        lock (locker) {
                                if (totalRead >= contentLength)
                                        return;
@@ -592,6 +593,14 @@ namespace System.Net
                        if (result.EndCalled)
                                return;
 
+                       if (sendChunked) {
+                               lock (locker) {
+                                       pendingWrites--;
+                                       if (pendingWrites <= 0)
+                                               pending.Set ();
+                               }
+                       }
+
                        result.EndCalled = true;
                        if (result.AsyncWriteAll) {
                                result.WaitUntilComplete ();
@@ -605,14 +614,6 @@ namespace System.Net
 
                        if (result.GotException)
                                throw result.Exception;
-
-                       if (sendChunked) {
-                               lock (locker) {
-                                       pendingWrites--;
-                                       if (pendingWrites == 0)
-                                               pending.Set ();
-                               }
-                       }
                }
                
                public override void Write (byte [] buffer, int offset, int size)
@@ -653,7 +654,8 @@ namespace System.Net
                        if (setInternalLength && !no_writestream && writeBuffer != null)
                                request.InternalContentLength = writeBuffer.Length;
 
-                       if (!(sendChunked || request.ContentLength > -1 || no_writestream || webdav))
+                       bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1);
+                       if (!(sendChunked || has_content || no_writestream || webdav))
                                return false;
 
                        headersSent = true;
@@ -775,7 +777,9 @@ namespace System.Net
                                if (disposed)
                                        return;
                                disposed = true;
-                               pending.WaitOne ();
+                               if (!pending.WaitOne (WriteTimeout)) {
+                                       throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
+                               }
                                byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
                                string err_msg = null;
                                cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);