[WebConnection] Fix race condition between Close and BeginWrite (#4693)
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index 9086ed3e60f0634b022115646cd1cabc3619a8ce..96a2b3bf42fc4cc506ad4e6fd056e2c6168d36b6 100644 (file)
@@ -45,8 +45,8 @@ namespace System.Net
                int readBufferOffset;
                int readBufferSize;
                int stream_length; // -1 when CL not present
-               int contentLength;
-               int totalRead;
+               long contentLength;
+               long totalRead;
                internal long totalWritten;
                bool nextReadCalled;
                int pendingReads;
@@ -93,10 +93,10 @@ namespace System.Net
                                                ReadAll ();
                                        }
                                } catch {
-                                       contentLength = Int32.MaxValue;
+                                       contentLength = Int64.MaxValue;
                                }
                        } else {
-                               contentLength = Int32.MaxValue;
+                               contentLength = Int64.MaxValue;
                        }
 
                        // Negative numbers?
@@ -208,7 +208,7 @@ namespace System.Net
                internal void ForceCompletion ()
                {
                        if (!nextReadCalled) {
-                               if (contentLength == Int32.MaxValue)
+                               if (contentLength == Int64.MaxValue)
                                        contentLength = 0;
                                nextReadCalled = true;
                                cnc.NextRead ();
@@ -244,7 +244,7 @@ namespace System.Net
                                int diff = readBufferSize - readBufferOffset;
                                int new_size;
 
-                               if (contentLength == Int32.MaxValue) {
+                               if (contentLength == Int64.MaxValue) {
                                        MemoryStream ms = new MemoryStream ();
                                        byte [] buffer = null;
                                        if (readBuffer != null && diff > 0) {
@@ -264,7 +264,7 @@ namespace System.Net
                                        new_size = (int) ms.Length;
                                        contentLength = new_size;
                                } else {
-                                       new_size = contentLength - totalRead;
+                                       new_size = (int) (contentLength - totalRead);
                                        b = new byte [new_size];
                                        if (readBuffer != null && diff > 0) {
                                                if (diff > new_size)
@@ -384,8 +384,8 @@ namespace System.Net
                        if (cb != null)
                                cb = cb_wrapper;
 
-                       if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
-                               size = contentLength - totalRead;
+                       if (contentLength != Int64.MaxValue && contentLength - totalRead < size)
+                               size = (int)(contentLength - totalRead);
 
                        if (!read_eof) {
                                result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
@@ -459,7 +459,7 @@ namespace System.Net
                                result.SetCompleted (false, 0);
                                if (!initRead) {
                                        initRead = true;
-                                       WebConnection.InitRead (cnc);
+                                       cnc.InitRead ();
                                }
                        } catch (Exception e) {
                                KillBuffer ();
@@ -480,7 +480,7 @@ namespace System.Net
                                                        AsyncCallback cb, object state)
                {
                        if (request.Aborted)
-                               throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
+                               throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
 
                        if (isRead)
                                throw new NotSupportedException ("this stream does not allow writing");
@@ -644,29 +644,18 @@ namespace System.Net
                        if (headersSent)
                                return false;
 
-                       bool webdav = false;
-                       bool writestream = false;
-
-                       switch (request.Method) {
-                       case "PROPFIND":
-                       case "PROPPATCH":
-                       case "MKCOL":
-                       case "COPY":
-                       case "MOVE":
-                       case "LOCK":
-                       case "UNLOCK":
-                               webdav = true;
-                               break;
-                       case "POST":
-                       case "PUT":
-                               writestream = true;
-                               break;
-                       }
-
-                       if (setInternalLength && writestream && writeBuffer != null)
+                       string method = request.Method;
+                       bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
+                                             method == "TRACE");
+                       bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
+                                     method == "COPY" || method == "MOVE" || method == "LOCK" ||
+                                     method == "UNLOCK");
+
+                       if (setInternalLength && !no_writestream && writeBuffer != null)
                                request.InternalContentLength = writeBuffer.Length;
 
-                       if (!(sendChunked || request.ContentLength > -1 || !writestream || webdav))
+                       bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1);
+                       if (!(sendChunked || has_content || no_writestream || webdav))
                                return false;
 
                        headersSent = true;
@@ -677,7 +666,7 @@ namespace System.Net
                                        cnc.EndWrite (request, true, r);
                                        if (!initRead) {
                                                initRead = true;
-                                               WebConnection.InitRead (cnc);
+                                               cnc.InitRead ();
                                        }
                                        var cl = request.ContentLength;
                                        if (!sendChunked && cl == 0)
@@ -686,7 +675,7 @@ namespace System.Net
                                } catch (WebException e) {
                                        result.SetCompleted (false, e);
                                } catch (Exception e) {
-                                       result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
+                                       result.SetCompleted (false, new WebException ("Error writing headers", WebExceptionStatus.SendFailure, WebExceptionInternalStatus.RequestFatal, e));
                                }
                        }, null);
 
@@ -730,23 +719,23 @@ namespace System.Net
 
                        SetHeadersAsync (true, inner => {
                                if (inner.GotException) {
-                                       result.SetCompleted (inner.CompletedSynchronously, inner.Exception);
+                                       result.SetCompleted (inner.CompletedSynchronouslyPeek, inner.Exception);
                                        return;
                                }
 
                                if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) {
-                                       result.SetCompleted (inner.CompletedSynchronously);
+                                       result.SetCompleted (inner.CompletedSynchronouslyPeek);
                                        return;
                                }
 
                                if (!initRead) {
                                        initRead = true;
-                                       WebConnection.InitRead (cnc);
+                                       cnc.InitRead ();
                                }
 
                                if (length == 0) {
                                        complete_request_written = true;
-                                       result.SetCompleted (inner.CompletedSynchronously);
+                                       result.SetCompleted (inner.CompletedSynchronouslyPeek);
                                        return;
                                }
 
@@ -811,7 +800,7 @@ namespace System.Net
                                complete_request_written = true;
                                if (!initRead) {
                                        initRead = true;
-                                       WebConnection.InitRead (cnc);
+                                       cnc.InitRead ();
                                }
                                return;
                        }
@@ -825,7 +814,7 @@ namespace System.Net
                                IOException io = new IOException ("Cannot close the stream until all bytes are written");
                                nextReadCalled = true;
                                cnc.Close (true);
-                               throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
+                               throw new WebException ("Request was cancelled.", WebExceptionStatus.RequestCanceled, WebExceptionInternalStatus.RequestFatal, io);
                        }
 
                        // Commented out the next line to fix xamarin bug #1512