Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index a99303383128882595f3f9d97dcebc6221dc5028..ff370e9e8ba78d1e6f6275a49b9f6cb34570def0 100644 (file)
@@ -44,9 +44,10 @@ namespace System.Net
                byte [] readBuffer;
                int readBufferOffset;
                int readBufferSize;
+               int stream_length; // -1 when CL not present
                int contentLength;
                int totalRead;
-               long totalWritten;
+               internal long totalWritten;
                bool nextReadCalled;
                int pendingReads;
                int pendingWrites;
@@ -65,9 +66,16 @@ namespace System.Net
                int read_timeout;
                int write_timeout;
                AsyncCallback cb_wrapper; // Calls to ReadCallbackWrapper or WriteCallbacWrapper
+               internal bool IgnoreIOErrors;
 
                public WebConnectionStream (WebConnection cnc)
                {
+                       if (cnc.Data == null)
+                               throw new InvalidOperationException ("cnc.Data was not initialized");
+                       if (cnc.Data.Headers == null)
+                               throw new InvalidOperationException ("cnc.Data.Headers was not initialized");
+                       if (cnc.Data.request == null)
+                               throw new InvalidOperationException ("cnc.Data.request was not initialized");
                        isRead = true;
                        cb_wrapper = new AsyncCallback (ReadCallbackWrapper);
                        pending = new ManualResetEvent (true);
@@ -76,7 +84,7 @@ namespace System.Net
                        write_timeout = read_timeout;
                        this.cnc = cnc;
                        string contentType = cnc.Data.Headers ["Transfer-Encoding"];
-                       bool chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
+                       bool chunkedRead = (contentType != null && contentType.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
                        string clength = cnc.Data.Headers ["Content-Length"];
                        if (!chunkedRead && clength != null && clength != "") {
                                try {
@@ -90,6 +98,10 @@ namespace System.Net
                        } else {
                                contentLength = Int32.MaxValue;
                        }
+
+                       // Negative numbers?
+                       if (!Int32.TryParse (clength, out stream_length))
+                               stream_length = -1;
                }
 
                public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
@@ -113,7 +125,7 @@ namespace System.Net
                        bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
                        string header_name = (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate";
                        string authHeader = cnc.Data.Headers [header_name];
-                       return (authHeader != null && authHeader.IndexOf ("NTLM") != -1);
+                       return (authHeader != null && authHeader.IndexOf ("NTLM", StringComparison.Ordinal) != -1);
                }
 
                internal void CheckResponseInBuffer ()
@@ -131,16 +143,11 @@ namespace System.Net
                internal WebConnection Connection {
                        get { return cnc; }
                }
-#if NET_2_0
                public override bool CanTimeout {
                        get { return true; }
                }
-#endif
 
-#if NET_2_0
-               public override
-#endif
-               int ReadTimeout {
+               public override int ReadTimeout {
                        get {
                                return read_timeout;
                        }
@@ -152,10 +159,7 @@ namespace System.Net
                        }
                }
 
-#if NET_2_0
-               public override
-#endif
-               int WriteTimeout {
+               public override int WriteTimeout {
                        get {
                                return write_timeout;
                        }
@@ -292,7 +296,10 @@ namespace System.Net
                                result.InnerAsyncResult = r;
                                result.DoCallback ();
                        } else {
-                               EndWrite (r);
+                               try {
+                                       EndWrite (r);
+                               } catch {
+                               }
                        }
                }
 
@@ -530,7 +537,14 @@ namespace System.Net
                                size = chunkSize;
                        }
 
-                       result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
+                       try {
+                               result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
+                       } catch (Exception) {
+                               if (!IgnoreIOErrors)
+                                       throw;
+                               result.SetCompleted (true, 0);
+                               result.DoCallback ();
+                       }
                        totalWritten += size;
                        return result;
                }
@@ -582,9 +596,13 @@ namespace System.Net
                                result.SetCompleted (false, 0);
                                result.DoCallback ();
                        } catch (Exception e) {
-                               result.SetCompleted (false, e);
+                               if (IgnoreIOErrors)
+                                       result.SetCompleted (false, 0);
+                               else
+                                       result.SetCompleted (false, e);
                                result.DoCallback ();
-                               throw;
+                               if (!IgnoreIOErrors)
+                                       throw;
                        } finally {
                                if (sendChunked) {
                                        lock (locker) {
@@ -623,8 +641,11 @@ namespace System.Net
                        long cl = request.ContentLength;
                        string method = request.Method;
                        bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
-                                               method == "TRACE" || method == "DELETE");
-                       if (sendChunked || cl > -1 || no_writestream) {
+                                               method == "TRACE");
+                       bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
+                                      method == "COPY" || method == "MOVE" || method == "LOCK" ||
+                                      method == "UNLOCK");
+                       if (sendChunked || cl > -1 || no_writestream || webdav) {
                                WriteHeaders ();
                                if (!initRead) {
                                        initRead = true;
@@ -683,7 +704,7 @@ namespace System.Net
                        if (!headersSent) {
                                string method = request.Method;
                                bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
-                                                       method == "TRACE" || method == "DELETE");
+                                                       method == "TRACE");
                                if (!no_writestream)
                                        request.InternalContentLength = length;
                                request.SendRequestHeaders (true);
@@ -756,7 +777,8 @@ namespace System.Net
                                throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
                        }
 
-                       WriteRequest ();
+                       // Commented out the next line to fix xamarin bug #1512
+                       //WriteRequest ();
                        disposed = true;
                }
 
@@ -788,7 +810,11 @@ namespace System.Net
                }
 
                public override long Length {
-                       get { throw new NotSupportedException (); }
+                       get {
+                               if (!isRead)
+                                       throw new NotSupportedException ();
+                               return stream_length;
+                       }
                }
 
                public override long Position {