Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
index fb7222672eb9fe569279cae20fa1c5e9da617797..ff370e9e8ba78d1e6f6275a49b9f6cb34570def0 100644 (file)
@@ -44,6 +44,7 @@ namespace System.Net
                byte [] readBuffer;
                int readBufferOffset;
                int readBufferSize;
+               int stream_length; // -1 when CL not present
                int contentLength;
                int totalRead;
                internal long totalWritten;
@@ -69,6 +70,12 @@ namespace System.Net
 
                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);
@@ -91,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)
@@ -132,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;
                        }
@@ -153,10 +159,7 @@ namespace System.Net
                        }
                }
 
-#if NET_2_0
-               public override
-#endif
-               int WriteTimeout {
+               public override int WriteTimeout {
                        get {
                                return write_timeout;
                        }
@@ -638,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;
@@ -698,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);
@@ -804,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 {