Convert blocking operations in HttpWebRequest and SslClientStream to non-blocking...
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index bafd6e73259da84bd673ebb43c1d6ff324f8cb91..5b1b85a7b8531049073d4dfaa5cd080bd828a8f5 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;
@@ -64,18 +65,27 @@ namespace System.Net
                bool complete_request_written;
                int read_timeout;
                int write_timeout;
-
-               public WebConnectionStream (WebConnection cnc)
-               {
+               AsyncCallback cb_wrapper; // Calls to ReadCallbackWrapper or WriteCallbacWrapper
+               internal bool IgnoreIOErrors;
+
+               public WebConnectionStream (WebConnection cnc, WebConnectionData data)
+               {          
+                       if (data == null)
+                               throw new InvalidOperationException ("data was not initialized");
+                       if (data.Headers == null)
+                               throw new InvalidOperationException ("data.Headers was not initialized");
+                       if (data.request == null)
+                               throw new InvalidOperationException ("data.request was not initialized");
                        isRead = true;
+                       cb_wrapper = new AsyncCallback (ReadCallbackWrapper);
                        pending = new ManualResetEvent (true);
-                       this.request = cnc.Data.request;
+                       this.request = data.request;
                        read_timeout = request.ReadWriteTimeout;
                        write_timeout = read_timeout;
                        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"];
+                       string contentType = data.Headers ["Transfer-Encoding"];
+                       bool chunkedRead = (contentType != null && contentType.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
+                       string clength = data.Headers ["Content-Length"];
                        if (!chunkedRead && clength != null && clength != "") {
                                try {
                                        contentLength = Int32.Parse (clength);
@@ -88,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)
@@ -95,6 +109,7 @@ namespace System.Net
                        read_timeout = request.ReadWriteTimeout;
                        write_timeout = read_timeout;
                        isRead = false;
+                       cb_wrapper = new AsyncCallback (WriteCallbackWrapper);
                        this.cnc = cnc;
                        this.request = request;
                        allowBuffering = request.InternalAllowBuffering;
@@ -110,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 ()
@@ -128,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;
                        }
@@ -149,10 +159,7 @@ namespace System.Net
                        }
                }
 
-#if NET_2_0
-               public override
-#endif
-               int WriteTimeout {
+               public override int WriteTimeout {
                        get {
                                return write_timeout;
                        }
@@ -289,7 +296,10 @@ namespace System.Net
                                result.InnerAsyncResult = r;
                                result.DoCallback ();
                        } else {
-                               EndWrite (r);
+                               try {
+                                       EndWrite (r);
+                               } catch {
+                               }
                        }
                }
 
@@ -301,13 +311,16 @@ namespace System.Net
                                result.InnerAsyncResult = r;
                                result.DoCallback ();
                        } else {
-                               EndRead (r);
+                               try {
+                                       EndRead (r);
+                               } catch {
+                               }
                        }
                }
 
                public override int Read (byte [] buffer, int offset, int size)
                {
-                       AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
+                       AsyncCallback cb = cb_wrapper;
                        WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
                        if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
                                nextReadCalled = true;
@@ -362,7 +375,7 @@ namespace System.Net
                        }
 
                        if (cb != null)
-                               cb = new AsyncCallback (ReadCallbackWrapper);
+                               cb = cb_wrapper;
 
                        if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
                                size = contentLength - totalRead;
@@ -506,7 +519,7 @@ namespace System.Net
 
                        AsyncCallback callback = null;
                        if (cb != null)
-                               callback = new AsyncCallback (WriteCallbackWrapper);
+                               callback = cb_wrapper;
 
                        if (sendChunked) {
                                WriteRequest ();
@@ -524,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;
                }
@@ -576,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) {
@@ -592,7 +616,7 @@ namespace System.Net
                
                public override void Write (byte [] buffer, int offset, int size)
                {
-                       AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
+                       AsyncCallback cb = cb_wrapper;
                        WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
                        if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
                                KillBuffer ();
@@ -608,7 +632,7 @@ namespace System.Net
                {
                }
 
-               internal void SetHeaders (byte [] buffer)
+               internal void SetHeadersAsync (byte[] buffer, WebAsyncResult result)
                {
                        if (headersSent)
                                return;
@@ -617,16 +641,49 @@ 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) {
-                               WriteHeaders ();
+                                               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) {
+
+                               headersSent = true;
+
+                               try {
+                                       result.InnerAsyncResult = cnc.BeginWrite (request, headers, 0, headers.Length, new AsyncCallback(SetHeadersCB), result);
+                                       if (result.InnerAsyncResult == null) {
+                                               // when does BeginWrite return null? Is the case when the request is aborted?
+                                               if (!result.IsCompleted)
+                                                       result.SetCompleted (true, 0);
+                                               result.DoCallback ();
+                                       }
+                               } catch (Exception exc) {
+                                       result.SetCompleted (true, exc);
+                                       result.DoCallback ();
+                               }
+                       }
+               }
+
+               void SetHeadersCB (IAsyncResult r)
+               {
+                       WebAsyncResult result = (WebAsyncResult) r.AsyncState;
+                       result.InnerAsyncResult = null;
+                       try {
+                               cnc.EndWrite2 (request, r);
+                               result.SetCompleted (false, 0);
                                if (!initRead) {
                                        initRead = true;
                                        WebConnection.InitRead (cnc);
                                }
+                               long cl = request.ContentLength;
                                if (!sendChunked && cl == 0)
                                        requestWritten = true;
+                       } catch (WebException e) {
+                               result.SetCompleted (false, e);
+                       } catch (Exception e) {
+                               result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
                        }
+                       result.DoCallback ();
                }
 
                internal bool RequestWritten {
@@ -642,17 +699,6 @@ namespace System.Net
                        return (length > 0) ? cnc.BeginWrite (request, bytes, 0, length, cb, state) : null;
                }
 
-               void WriteHeaders ()
-               {
-                       if (headersSent)
-                               return;
-
-                       headersSent = true;
-                       string err_msg = null;
-                       if (!cnc.Write (request, headers, 0, headers.Length, ref err_msg))
-                               throw new WebException ("Error writing request: " + err_msg, null, WebExceptionStatus.SendFailure, null);
-               }
-
                internal void WriteRequest ()
                {
                        if (requestWritten)
@@ -677,12 +723,18 @@ 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);
+
+                               byte[] requestHeaders = request.GetRequestHeaders ();
+                               WebAsyncResult ar = new WebAsyncResult (null, null);
+                               SetHeadersAsync (requestHeaders, ar);
+                               ar.AsyncWaitHandle.WaitOne ();
+                               if (ar.Exception != null)
+                                       throw ar.Exception;
                        }
-                       WriteHeaders ();
+
                        if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
                                return;
                                
@@ -750,7 +802,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;
                }
 
@@ -782,7 +835,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 {