Convert blocking operations in HttpWebRequest and SslClientStream to non-blocking...
[mono.git] / mcs / class / System / System.Net / HttpWebRequest.cs
index 6b71f21243118bcd28e6b49a3200f5eaab448a0b..6aee5af52bd3bda083667b07cc29d699d178f597 100644 (file)
@@ -46,12 +46,8 @@ using System.Threading;
 
 namespace System.Net 
 {
-#if MOONLIGHT
-       internal class HttpWebRequest : WebRequest, ISerializable {
-#else
        [Serializable]
        public class HttpWebRequest : WebRequest, ISerializable {
-#endif
                Uri requestUri;
                Uri actualUri;
                bool hostChanged;
@@ -194,6 +190,13 @@ namespace System.Net
                        get { return allowBuffering; }
                        set { allowBuffering = value; }
                }
+               
+#if NET_4_5
+               public virtual bool AllowReadStreamBuffering {
+                       get { return allowBuffering; }
+                       set { allowBuffering = value; }
+               }
+#endif
 
                static Exception GetMustImplement ()
                {
@@ -291,6 +294,9 @@ namespace System.Net
                        set { continueDelegate = value; }
                }
                
+#if NET_4_5
+               virtual
+#endif
                public CookieContainer CookieContainer {
                        get { return cookieContainer; }
                        set { cookieContainer = value; }
@@ -316,6 +322,8 @@ namespace System.Net
                        }
                }
 #endif
+
+#if !NET_2_1
                [MonoTODO]
                public static new RequestCachePolicy DefaultCachePolicy
                {
@@ -326,6 +334,7 @@ namespace System.Net
                                throw GetMustImplement ();
                        }
                }
+#endif
                
                [MonoTODO]
                public static int DefaultMaximumErrorResponseLength
@@ -358,6 +367,9 @@ namespace System.Net
                        }
                }
                
+#if NET_4_5
+               virtual
+#endif
                public bool HaveResponse {
                        get { return haveResponse; }
                }
@@ -409,6 +421,10 @@ namespace System.Net
                        if (idx >= 0)
                                return false;
 
+                       IPAddress ipaddr;
+                       if (IPAddress.TryParse (val, out ipaddr))
+                               return true;
+
                        string u = scheme + "://" + val + "/";
                        return Uri.IsWellFormedUriString (u, UriKind.Absolute);
                }
@@ -477,6 +493,14 @@ namespace System.Net
                        }
                }
                
+#if NET_4_5
+               [MonoTODO]
+               public int ContinueTimeout {
+                       get { throw new NotImplementedException (); }
+                       set { throw new NotImplementedException (); }
+               }
+#endif
+               
                public string MediaType {
                        get { return mediaType; }
                        set { 
@@ -560,9 +584,9 @@ namespace System.Net
                internal ServicePoint ServicePointNoLock {
                        get { return servicePoint; }
                }
-#if NET_4_5 || MOBILE
+#if NET_4_0
                [MonoTODO ("for portable library support")]
-               public bool SupportsCookieContainer { 
+               public virtual bool SupportsCookieContainer { 
                        get {
                                throw new NotImplementedException ();
                        }
@@ -835,10 +859,10 @@ namespace System.Net
                        if (writeStream == null || writeStream.RequestWritten || !InternalAllowBuffering)
                                return;
 #if NET_4_0
-                       if (contentLength < 0 && writeStream.CanWrite == true && writeStream.WriteBufferLength <= 0)
+                       if (contentLength < 0 && writeStream.CanWrite == true && writeStream.WriteBufferLength < 0)
                                return;
 
-                       if (contentLength < 0 && writeStream.WriteBufferLength > 0)
+                       if (contentLength < 0 && writeStream.WriteBufferLength >= 0)
                                InternalContentLength = writeStream.WriteBufferLength;
 #else
                        if (contentLength < 0 && writeStream.CanWrite == true)
@@ -1139,7 +1163,9 @@ namespace System.Net
                        bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
 
                        if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
-                               webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
+                               if (webHeaders[connectionHeader] == null
+                                   || webHeaders[connectionHeader].IndexOf ("keep-alive", StringComparison.OrdinalIgnoreCase) == -1)
+                                       webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
                        } else if (!keepAlive && version == HttpVersion.Version11) {
                                webHeaders.RemoveAndAdd (connectionHeader, "close");
                        }
@@ -1206,7 +1232,7 @@ namespace System.Net
                        }
                }
 
-               internal void SendRequestHeaders (bool propagate_error)
+               internal byte[] GetRequestHeaders ()
                {
                        StringBuilder req = new StringBuilder ();
                        string query;
@@ -1228,18 +1254,7 @@ namespace System.Net
                                                                actualVersion.Major, actualVersion.Minor);
                        req.Append (GetHeaders ());
                        string reqstr = req.ToString ();
-                       byte [] bytes = Encoding.UTF8.GetBytes (reqstr);
-                       try {
-                               writeStream.SetHeaders (bytes);
-                       } catch (WebException wexc) {
-                               SetWriteStreamError (wexc.Status, wexc);
-                               if (propagate_error)
-                                       throw;
-                       } catch (Exception exc) {
-                               SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
-                               if (propagate_error)
-                                       throw;
-                       }
+                       return Encoding.UTF8.GetBytes (reqstr);
                }
 
                internal void SetWriteStream (WebConnectionStream stream)
@@ -1254,14 +1269,32 @@ namespace System.Net
                                writeStream.SendChunked = false;
                        }
 
-                       SendRequestHeaders (false);
+                       byte[] requestHeaders = GetRequestHeaders ();
+                       WebAsyncResult result = new WebAsyncResult (new AsyncCallback (SetWriteStreamCB), null);
+                       writeStream.SetHeadersAsync (requestHeaders, result);
+               }
 
+               void SetWriteStreamCB(IAsyncResult ar)
+               {
+                       WebAsyncResult result = ar as WebAsyncResult;
+
+                       if (result.Exception != null) {
+                               WebException wexc = result.Exception as WebException;
+                               if (wexc != null) {
+                                       SetWriteStreamError (wexc.Status, wexc);
+                                       return;
+                               }
+                               SetWriteStreamError (WebExceptionStatus.SendFailure, result.Exception);
+                               return;
+                       }
+               
                        haveRequest = true;
-                       
+
                        if (bodyBuffer != null) {
                                // The body has been written and buffered. The request "user"
                                // won't write it again, so we must do it.
                                if (ntlm_auth_state != NtlmAuthState.Challenge) {
+                                       // FIXME: this is a blocking call on the thread pool that could lead to thread pool exhaustion
                                        writeStream.Write (bodyBuffer, 0, bodyBufferLength);
                                        bodyBuffer = null;
                                        writeStream.Close ();
@@ -1269,11 +1302,12 @@ namespace System.Net
                        } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
                                        method != "TRACE") {
                                if (getResponseCalled && !writeStream.RequestWritten)
+                                       // FIXME: this is a blocking call on the thread pool that could lead to thread pool exhaustion
                                        writeStream.WriteRequest ();
                        }
 
                        if (asyncWrite != null) {
-                               asyncWrite.SetCompleted (false, stream);
+                               asyncWrite.SetCompleted (false, writeStream);
                                asyncWrite.DoCallback ();
                                asyncWrite = null;
                        }
@@ -1581,6 +1615,13 @@ namespace System.Net
 
                        throw throwMe;
                }
+
+               internal bool ReuseConnection {
+                       get;
+                       set;
+               }
+
+               internal WebConnection StoredConnection;
        }
 }