X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem%2FSystem.Net%2FFileWebRequest.cs;h=618c09ee145cb5ed92053efb2ab6dcc07b15a423;hb=1ad39faaa0022f44e6409dac11a087197791eb3d;hp=84234017670b49d3b4d5c6f2c0d875855ebe0aa4;hpb=1b462e1ef964186146dd5d0bded78da61acf07f5;p=mono.git diff --git a/mcs/class/System/System.Net/FileWebRequest.cs b/mcs/class/System/System.Net/FileWebRequest.cs index 84234017670..618c09ee145 100644 --- a/mcs/class/System/System.Net/FileWebRequest.cs +++ b/mcs/class/System/System.Net/FileWebRequest.cs @@ -5,6 +5,27 @@ // Lawrence Pit (loz@cable.a2000.nl) // +// +// 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. +// + using System; using System.Collections; using System.IO; @@ -22,7 +43,11 @@ namespace System.Net private ICredentials credentials; private string connectionGroup; + private long contentLength; + private FileAccess fileAccess = FileAccess.Read; private string method = "GET"; + private IWebProxy proxy; + private bool preAuthenticate; private int timeout = 100000; private Stream requestStream; @@ -36,40 +61,38 @@ namespace System.Net internal FileWebRequest (Uri uri) { this.uri = uri; - this.webHeaders = new WebHeaderCollection (); - } + this.webHeaders = new WebHeaderCollection (); + } + [Obsolete ("Serialization is obsoleted for this type", false)] protected FileWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) { SerializationInfo info = serializationInfo; - - method = info.GetString ("method"); + webHeaders = (WebHeaderCollection) info.GetValue ("headers", typeof (WebHeaderCollection)); + proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy)); uri = (Uri) info.GetValue ("uri", typeof (Uri)); + connectionGroup = info.GetString ("connectionGroupName"); + method = info.GetString ("method"); + contentLength = info.GetInt64 ("contentLength"); timeout = info.GetInt32 ("timeout"); - connectionGroup = info.GetString ("connectionGroup"); - webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection)); + fileAccess = (FileAccess) info.GetValue ("fileAccess", typeof (FileAccess)); + preAuthenticate = info.GetBoolean ("preauthenticate"); } // Properties // currently not used according to spec - public override string ConnectionGroupName { + public override string ConnectionGroupName { get { return connectionGroup; } set { connectionGroup = value; } } - public override long ContentLength { - get { - try { - return Int64.Parse (webHeaders ["Content-Length"]); - } catch (Exception) { - return 0; - } - } - set { + public override long ContentLength { + get { return contentLength; } + set { if (value < 0) - throw new ArgumentException ("value"); - webHeaders ["Content-Length"] = Convert.ToString (value); + throw new ArgumentException ("The Content-Length value must be greater than or equal to zero.", "value"); + contentLength = value; } } @@ -90,19 +113,24 @@ namespace System.Net // currently not used according to spec public override string Method { get { return this.method; } - set { this.method = value; } + set { + if (value == null || value.Length == 0) + throw new ArgumentException ("Cannot set null or blank " + + "methods on request.", "value"); + this.method = value; + } } // currently not used according to spec public override bool PreAuthenticate { - get { throw new NotSupportedException (); } - set { throw new NotSupportedException (); } + get { return preAuthenticate; } + set { preAuthenticate = value; } } // currently not used according to spec - public override IWebProxy Proxy { - get { throw new NotSupportedException (); } - set { throw new NotSupportedException (); } + public override IWebProxy Proxy { + get { return proxy; } + set { proxy = value; } } public override Uri RequestUri { @@ -112,34 +140,48 @@ namespace System.Net public override int Timeout { get { return timeout; } set { - if (value < 0) - throw new ArgumentException ("value"); + if (value < -1) + throw new ArgumentOutOfRangeException ("Timeout can be " + + "only set to 'System.Threading.Timeout.Infinite' " + + "or a value >= 0."); timeout = value; } } - + + public override bool UseDefaultCredentials + { + get { + throw new NotSupportedException (); + } + set { + throw new NotSupportedException (); + } + } // Methods private delegate Stream GetRequestStreamCallback (); private delegate WebResponse GetResponseCallback (); + static Exception GetMustImplement () + { + return new NotImplementedException (); + } + + /* LAMESPEC: Docs suggest this was present in 1.1 and + * 1.0 profiles, but the masterinfos say otherwise + */ + [MonoTODO] + public override void Abort () + { + throw GetMustImplement (); + } + public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) - { - if (method == null || (!method.Equals ("PUT") && !method.Equals ("POST"))) - throw new ProtocolViolationException ("Cannot send file when method is: " + this.method + ". Method must be PUT."); - // workaround for bug 24943 - Exception e = null; - lock (this) { - if (asyncResponding || webResponse != null) - e = new InvalidOperationException ("This operation cannot be performed after the request has been submitted."); - else if (requesting) - e = new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress."); - else - requesting = true; - } - if (e != null) - throw e; - /* + { + if (string.Compare ("GET", method, true) == 0 || + string.Compare ("HEAD", method, true) == 0 || + string.Compare ("CONNECT", method, true) == 0) + throw new ProtocolViolationException ("Cannot send a content-body with this verb-type."); lock (this) { if (asyncResponding || webResponse != null) throw new InvalidOperationException ("This operation cannot be performed after the request has been submitted."); @@ -147,9 +189,8 @@ namespace System.Net throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress."); requesting = true; } - */ GetRequestStreamCallback c = new GetRequestStreamCallback (this.GetRequestStreamInternal); - return c.BeginInvoke (callback, state); + return c.BeginInvoke (callback, state); } public override Stream EndGetRequestStream (IAsyncResult asyncResult) @@ -157,7 +198,7 @@ namespace System.Net if (asyncResult == null) throw new ArgumentNullException ("asyncResult"); if (!asyncResult.IsCompleted) - asyncResult.AsyncWaitHandle.WaitOne (); + asyncResult.AsyncWaitHandle.WaitOne (); AsyncResult async = (AsyncResult) asyncResult; GetRequestStreamCallback cb = (GetRequestStreamCallback) async.AsyncDelegate; return cb.EndInvoke (asyncResult); @@ -176,7 +217,7 @@ namespace System.Net { this.requestStream = new FileWebStream ( this, - FileMode.CreateNew, + FileMode.Create, FileAccess.Write, FileShare.Read); return this.requestStream; @@ -184,40 +225,30 @@ namespace System.Net public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state) { - // workaround for bug 24943 - Exception e = null; - lock (this) { - if (asyncResponding) - e = new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress."); - else - asyncResponding = true; - } - if (e != null) - throw e; - /* lock (this) { if (asyncResponding) throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress."); asyncResponding = true; } - */ GetResponseCallback c = new GetResponseCallback (this.GetResponseInternal); return c.BeginInvoke (callback, state); } - + public override WebResponse EndGetResponse (IAsyncResult asyncResult) { if (asyncResult == null) throw new ArgumentNullException ("asyncResult"); if (!asyncResult.IsCompleted) - asyncResult.AsyncWaitHandle.WaitOne (); + asyncResult.AsyncWaitHandle.WaitOne (); AsyncResult async = (AsyncResult) asyncResult; GetResponseCallback cb = (GetResponseCallback) async.AsyncDelegate; - WebResponse webResponse = cb.EndInvoke(asyncResult); + FileWebResponse webResponse = (FileWebResponse) cb.EndInvoke(asyncResult); asyncResponding = false; + if (webResponse.HasError) + throw webResponse.Error; return webResponse; } - + public override WebResponse GetResponse () { IAsyncResult asyncResult = BeginGetResponse (null, null); @@ -226,11 +257,11 @@ namespace System.Net } return EndGetResponse (asyncResult); } - + WebResponse GetResponseInternal () { if (webResponse != null) - return webResponse; + return webResponse; lock (this) { if (requesting) { requestEndEvent = new AutoResetEvent (false); @@ -239,24 +270,33 @@ namespace System.Net if (requestEndEvent != null) { requestEndEvent.WaitOne (); } - FileStream fileStream = new FileWebStream ( - this, - FileMode.Open, - FileAccess.Read, - FileShare.Read); - this.webResponse = new FileWebResponse (this.uri, fileStream); - return (WebResponse) this.webResponse; + FileStream fileStream = null; + try { + fileStream = new FileWebStream (this, FileMode.Open, FileAccess.Read, FileShare.Read); + this.webResponse = new FileWebResponse (this.uri, fileStream); + } catch (Exception ex) { + this.webResponse = new FileWebResponse (this.uri, new WebException (ex.Message, ex)); + } + return this.webResponse; } void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext) { - SerializationInfo info = serializationInfo; + GetObjectData (serializationInfo, streamingContext); + } - info.AddValue ("method", method); + protected override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext) + { + SerializationInfo info = serializationInfo; + info.AddValue ("headers", webHeaders, typeof (WebHeaderCollection)); + info.AddValue ("proxy", proxy, typeof (IWebProxy)); info.AddValue ("uri", uri, typeof (Uri)); + info.AddValue ("connectionGroupName", connectionGroup); + info.AddValue ("method", method); + info.AddValue ("contentLength", contentLength); info.AddValue ("timeout", timeout); - info.AddValue ("connectionGroup", connectionGroup); - info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection)); + info.AddValue ("fileAccess", fileAccess); + info.AddValue ("preauthenticate", false); } internal void Close () @@ -266,7 +306,7 @@ namespace System.Net // requestStream.Close (); // } - lock (this) { + lock (this) { requesting = false; if (requestEndEvent != null) requestEndEvent.Set (); @@ -288,7 +328,7 @@ namespace System.Net { this.webRequest = webRequest; } - + public override void Close() { base.Close ();