Unbreak the build
[mono.git] / mcs / class / System / System.Net / FileWebRequest.cs
index 87b3bbdaaabec6aa45579def13e53be1038b7ed1..08c2d00c075b9294fe7e5872e8a5d11ddf619eda 100644 (file)
@@ -4,27 +4,27 @@
 // Author:\r
 //   Lawrence Pit (loz@cable.a2000.nl)\r
 //\r
-
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-// 
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-// 
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
+\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining\r
+// a copy of this software and associated documentation files (the\r
+// "Software"), to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify, merge, publish,\r
+// distribute, sublicense, and/or sell copies of the Software, and to\r
+// permit persons to whom the Software is furnished to do so, subject to\r
+// the following conditions:\r
+// \r
+// The above copyright notice and this permission notice shall be\r
+// included in all copies or substantial portions of the Software.\r
+// \r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
 \r
 using System;\r
 using System.Collections;\r
@@ -43,7 +43,11 @@ namespace System.Net
                \r
                private ICredentials credentials;\r
                private string connectionGroup;\r
+               private long contentLength;\r
+               private FileAccess fileAccess = FileAccess.Read;\r
                private string method = "GET";\r
+               private IWebProxy proxy;\r
+               private bool preAuthenticate;\r
                private int timeout = 100000;\r
                \r
                private Stream requestStream;\r
@@ -57,40 +61,44 @@ namespace System.Net
                internal FileWebRequest (Uri uri) \r
                { \r
                        this.uri = uri;\r
-                       this.webHeaders = new WebHeaderCollection ();\r
-               }               \r
+                       this.webHeaders = new WebHeaderCollection ();\r
+               }\r
                \r
+#if NET_2_0\r
+               [Obsolete ("Serialization is obsoleted for this type", false)]\r
+#endif\r
                protected FileWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) \r
                {\r
                        SerializationInfo info = serializationInfo;\r
-\r
-                       method = info.GetString ("method");\r
+                       webHeaders = (WebHeaderCollection) info.GetValue ("headers", typeof (WebHeaderCollection));\r
+                       proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));\r
                        uri = (Uri) info.GetValue ("uri", typeof (Uri));\r
+                       connectionGroup = info.GetString ("connectionGroupName");\r
+                       method = info.GetString ("method");\r
+                       contentLength = info.GetInt64 ("contentLength");\r
                        timeout = info.GetInt32 ("timeout");\r
-                       connectionGroup = info.GetString ("connectionGroup");\r
-                       webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));\r
+                       fileAccess = (FileAccess) info.GetValue ("fileAccess", typeof (FileAccess));\r
+                       preAuthenticate = info.GetBoolean ("preauthenticate");\r
                }\r
                \r
                // Properties\r
                \r
                // currently not used according to spec\r
-               public override string ConnectionGroupName { \r
+               public override string ConnectionGroupName {\r
                        get { return connectionGroup; }\r
                        set { connectionGroup = value; }\r
                }\r
                \r
-               public override long ContentLength { \r
-                       get { \r
-                               try {\r
-                                       return Int64.Parse (webHeaders ["Content-Length"]); \r
-                               } catch (Exception) {\r
-                                       return 0;\r
-                               }\r
-                       }\r
-                       set { \r
+               public override long ContentLength {\r
+                       get { return contentLength; }\r
+                       set {\r
                                if (value < 0)\r
+#if NET_2_0\r
+                                       throw new ArgumentException ("The Content-Length value must be greater than or equal to zero.", "value");\r
+#else\r
                                        throw new ArgumentException ("value");\r
-                               webHeaders ["Content-Length"] = Convert.ToString (value);\r
+#endif\r
+                               contentLength =  value;\r
                        }\r
                }\r
                \r
@@ -111,19 +119,29 @@ namespace System.Net
                // currently not used according to spec\r
                public override string Method { \r
                        get { return this.method; }\r
-                       set { this.method = value; }\r
+                       set {\r
+                               if (value == null || value.Length == 0)\r
+#if NET_2_0\r
+                                       throw new ArgumentException ("Cannot set null or blank "\r
+                                               + "methods on request.", "value");\r
+#else\r
+                                       throw new ArgumentException ("Cannot set null or blank "\r
+                                               + "methods on request.");\r
+#endif\r
+                               this.method = value;\r
+                       }\r
                }\r
                \r
                // currently not used according to spec\r
                public override bool PreAuthenticate { \r
-                       get { throw new NotSupportedException (); }\r
-                       set { throw new NotSupportedException (); }\r
+                       get { return preAuthenticate; }\r
+                       set { preAuthenticate = value; }\r
                }\r
                \r
                // currently not used according to spec\r
-               public override IWebProxy Proxy { \r
-                       get { throw new NotSupportedException (); }\r
-                       set { throw new NotSupportedException (); }\r
+               public override IWebProxy Proxy {\r
+                       get { return proxy; }\r
+                       set { proxy = value; }\r
                }\r
                \r
                public override Uri RequestUri { \r
@@ -133,34 +151,57 @@ namespace System.Net
                public override int Timeout { \r
                        get { return timeout; }\r
                        set { \r
-                               if (value < 0)\r
-                                       throw new ArgumentException ("value");\r
+                               if (value < -1)\r
+#if NET_2_0\r
+                                       throw new ArgumentOutOfRangeException ("Timeout can be "\r
+                                               + "only set to 'System.Threading.Timeout.Infinite' "\r
+                                               + "or a value >= 0.");\r
+#else\r
+                                       throw new ArgumentOutOfRangeException ("value");\r
+#endif\r
                                timeout = value;\r
                        }\r
                }\r
+\r
+#if NET_2_0\r
+               public override bool UseDefaultCredentials\r
+               {\r
+                       get {\r
+                               throw new NotSupportedException ();\r
+                       }\r
+                       set {\r
+                               throw new NotSupportedException ();\r
+                       }\r
+               }\r
+#endif\r
                \r
                // Methods\r
                \r
                private delegate Stream GetRequestStreamCallback ();\r
                private delegate WebResponse GetResponseCallback ();\r
 \r
+#if NET_2_0\r
+               static Exception GetMustImplement ()\r
+               {\r
+                       return new NotImplementedException ();\r
+               }\r
+               \r
+               /* LAMESPEC: Docs suggest this was present in 1.1 and\r
+                * 1.0 profiles, but the masterinfos say otherwise\r
+                */\r
+               [MonoTODO]\r
+               public override void Abort ()\r
+               {\r
+                       throw GetMustImplement ();\r
+               }\r
+#endif\r
+\r
                public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) \r
-               {               \r
-                       if (method == null || (!method.Equals ("PUT") && !method.Equals ("POST")))\r
-                               throw new ProtocolViolationException ("Cannot send file when method is: " + this.method + ". Method must be PUT.");\r
-                       // workaround for bug 24943\r
-                       Exception e = null;\r
-                       lock (this) {\r
-                               if (asyncResponding || webResponse != null)\r
-                                       e = new InvalidOperationException ("This operation cannot be performed after the request has been submitted.");\r
-                               else if (requesting)\r
-                                       e = new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");\r
-                               else\r
-                                       requesting = true;\r
-                       }\r
-                       if (e != null)\r
-                               throw e;\r
-                       /*\r
+               {\r
+                       if (string.Compare ("GET", method, true) == 0 ||\r
+                               string.Compare ("HEAD", method, true) == 0 ||\r
+                               string.Compare ("CONNECT", method, true) == 0)\r
+                               throw new ProtocolViolationException ("Cannot send a content-body with this verb-type.");\r
                        lock (this) {\r
                                if (asyncResponding || webResponse != null)\r
                                        throw new InvalidOperationException ("This operation cannot be performed after the request has been submitted.");\r
@@ -168,9 +209,8 @@ namespace System.Net
                                        throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");\r
                                requesting = true;\r
                        }\r
-                       */\r
                        GetRequestStreamCallback c = new GetRequestStreamCallback (this.GetRequestStreamInternal);\r
-                       return c.BeginInvoke (callback, state);                 \r
+                       return c.BeginInvoke (callback, state);\r
                }\r
                \r
                public override Stream EndGetRequestStream (IAsyncResult asyncResult)\r
@@ -178,7 +218,7 @@ namespace System.Net
                        if (asyncResult == null)\r
                                throw new ArgumentNullException ("asyncResult");\r
                        if (!asyncResult.IsCompleted)\r
-                               asyncResult.AsyncWaitHandle.WaitOne ();                         \r
+                               asyncResult.AsyncWaitHandle.WaitOne ();\r
                        AsyncResult async = (AsyncResult) asyncResult;\r
                        GetRequestStreamCallback cb = (GetRequestStreamCallback) async.AsyncDelegate;\r
                        return cb.EndInvoke (asyncResult);\r
@@ -197,7 +237,7 @@ namespace System.Net
                {\r
                        this.requestStream = new FileWebStream (\r
                                                this,\r
-                                               FileMode.CreateNew,\r
+                                               FileMode.Create,\r
                                                FileAccess.Write, \r
                                                FileShare.Read);\r
                        return this.requestStream;\r
@@ -205,40 +245,30 @@ namespace System.Net
                \r
                public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)\r
                {\r
-                       // workaround for bug 24943\r
-                       Exception e = null;\r
-                       lock (this) {\r
-                               if (asyncResponding)\r
-                                       e = new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");\r
-                               else \r
-                                       asyncResponding = true;\r
-                       }\r
-                       if (e != null)\r
-                               throw e;\r
-                       /*\r
                        lock (this) {\r
                                if (asyncResponding)\r
                                        throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");\r
                                asyncResponding = true;\r
                        }\r
-                       */\r
                        GetResponseCallback c = new GetResponseCallback (this.GetResponseInternal);\r
                        return c.BeginInvoke (callback, state);\r
                }\r
-\r
+               \r
                public override WebResponse EndGetResponse (IAsyncResult asyncResult)\r
                {\r
                        if (asyncResult == null)\r
                                throw new ArgumentNullException ("asyncResult");\r
                        if (!asyncResult.IsCompleted)\r
-                               asyncResult.AsyncWaitHandle.WaitOne ();                 \r
+                               asyncResult.AsyncWaitHandle.WaitOne ();\r
                        AsyncResult async = (AsyncResult) asyncResult;\r
                        GetResponseCallback cb = (GetResponseCallback) async.AsyncDelegate;\r
-                       WebResponse webResponse = cb.EndInvoke(asyncResult);\r
+                       FileWebResponse webResponse = (FileWebResponse) cb.EndInvoke(asyncResult);\r
                        asyncResponding = false;\r
+                       if (webResponse.HasError)\r
+                               throw webResponse.Error;\r
                        return webResponse;\r
                }\r
-                               \r
+               \r
                public override WebResponse GetResponse ()\r
                {\r
                        IAsyncResult asyncResult = BeginGetResponse (null, null);\r
@@ -247,11 +277,11 @@ namespace System.Net
                        }\r
                        return EndGetResponse (asyncResult);\r
                }\r
-                       \r
+               \r
                WebResponse GetResponseInternal ()\r
                {\r
                        if (webResponse != null)\r
-                               return webResponse;                     \r
+                               return webResponse;\r
                        lock (this) {\r
                                if (requesting) {\r
                                        requestEndEvent = new AutoResetEvent (false);\r
@@ -260,24 +290,40 @@ namespace System.Net
                        if (requestEndEvent != null) {\r
                                requestEndEvent.WaitOne ();\r
                        }\r
-                       FileStream fileStream = new FileWebStream (\r
-                                                       this,\r
-                                                       FileMode.Open, \r
-                                                       FileAccess.Read, \r
-                                                       FileShare.Read);\r
-                       this.webResponse = new FileWebResponse (this.uri, fileStream);\r
-                       return (WebResponse) this.webResponse;\r
+                       FileStream fileStream = null;\r
+                       try {\r
+                               fileStream = new FileWebStream (this, FileMode.Open, FileAccess.Read, FileShare.Read);\r
+                               this.webResponse = new FileWebResponse (this.uri, fileStream);\r
+                       } catch (Exception ex) {\r
+                               this.webResponse = new FileWebResponse (this.uri, new WebException (ex.Message, ex));\r
+                       }\r
+                       return this.webResponse;\r
                }\r
                \r
                void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
                {\r
-                       SerializationInfo info = serializationInfo;\r
+                       GetObjectData (serializationInfo, streamingContext);\r
+               }\r
 \r
-                       info.AddValue ("method", method);\r
+#if NET_2_0\r
+               protected override\r
+#endif\r
+               void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
+               {\r
+                       SerializationInfo info = serializationInfo;\r
+                       info.AddValue ("headers", webHeaders, typeof (WebHeaderCollection));\r
+                       info.AddValue ("proxy", proxy, typeof (IWebProxy));\r
                        info.AddValue ("uri", uri, typeof (Uri));\r
+                       info.AddValue ("connectionGroupName", connectionGroup);\r
+                       info.AddValue ("method", method);\r
+                       info.AddValue ("contentLength", contentLength);\r
                        info.AddValue ("timeout", timeout);\r
-                       info.AddValue ("connectionGroup", connectionGroup);\r
-                       info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));\r
+                       info.AddValue ("fileAccess", fileAccess);\r
+#if NET_2_0\r
+                       info.AddValue ("preauthenticate", false);\r
+#else\r
+                       info.AddValue ("preauthenticate", preAuthenticate);\r
+#endif\r
                }\r
                \r
                internal void Close ()\r
@@ -287,7 +333,7 @@ namespace System.Net
                        //      requestStream.Close ();\r
                        // }\r
 \r
-                       lock (this) {                   \r
+                       lock (this) {\r
                                requesting = false;\r
                                if (requestEndEvent != null) \r
                                        requestEndEvent.Set ();\r
@@ -309,7 +355,7 @@ namespace System.Net
                        {\r
                                this.webRequest = webRequest;\r
                        }\r
-                                               \r
+                       \r
                        public override void Close() \r
                        {\r
                                base.Close ();\r