Convert blocking operations in HttpWebRequest and SslClientStream to non-blocking...
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index 132ceacbbce43079f87f0a3e6a823faca37a1f8e..5b1b85a7b8531049073d4dfaa5cd080bd828a8f5 100644 (file)
@@ -68,18 +68,24 @@ namespace System.Net
                AsyncCallback cb_wrapper; // Calls to ReadCallbackWrapper or WriteCallbacWrapper
                internal bool IgnoreIOErrors;
 
-               public WebConnectionStream (WebConnection cnc)
-               {
+               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"];
+                       string contentType = data.Headers ["Transfer-Encoding"];
                        bool chunkedRead = (contentType != null && contentType.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
-                       string clength = cnc.Data.Headers ["Content-Length"];
+                       string clength = data.Headers ["Content-Length"];
                        if (!chunkedRead && clength != null && clength != "") {
                                try {
                                        contentLength = Int32.Parse (clength);
@@ -137,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;
                        }
@@ -158,10 +159,7 @@ namespace System.Net
                        }
                }
 
-#if NET_2_0
-               public override
-#endif
-               int WriteTimeout {
+               public override int WriteTimeout {
                        get {
                                return write_timeout;
                        }
@@ -634,7 +632,7 @@ namespace System.Net
                {
                }
 
-               internal void SetHeaders (byte [] buffer)
+               internal void SetHeadersAsync (byte[] buffer, WebAsyncResult result)
                {
                        if (headersSent)
                                return;
@@ -643,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 {
@@ -668,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)
@@ -703,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;