X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem%2FSystem.Net%2FFileWebRequest.cs;h=08c2d00c075b9294fe7e5872e8a5d11ddf619eda;hb=69da587d6ae81fcbdcedac1334aae4ec766abee0;hp=87b3bbdaaabec6aa45579def13e53be1038b7ed1;hpb=0abc2e6270020edc4a5b4c66f93b4ae582815f20;p=mono.git diff --git a/mcs/class/System/System.Net/FileWebRequest.cs b/mcs/class/System/System.Net/FileWebRequest.cs index 87b3bbdaaab..08c2d00c075 100644 --- a/mcs/class/System/System.Net/FileWebRequest.cs +++ b/mcs/class/System/System.Net/FileWebRequest.cs @@ -4,27 +4,27 @@ // Author: // 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. -// + +// +// 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; @@ -43,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; @@ -57,40 +61,44 @@ namespace System.Net internal FileWebRequest (Uri uri) { this.uri = uri; - this.webHeaders = new WebHeaderCollection (); - } + this.webHeaders = new WebHeaderCollection (); + } +#if NET_2_0 + [Obsolete ("Serialization is obsoleted for this type", false)] +#endif 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) +#if NET_2_0 + throw new ArgumentException ("The Content-Length value must be greater than or equal to zero.", "value"); +#else throw new ArgumentException ("value"); - webHeaders ["Content-Length"] = Convert.ToString (value); +#endif + contentLength = value; } } @@ -111,19 +119,29 @@ 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) +#if NET_2_0 + throw new ArgumentException ("Cannot set null or blank " + + "methods on request.", "value"); +#else + throw new ArgumentException ("Cannot set null or blank " + + "methods on request."); +#endif + 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 { @@ -133,34 +151,57 @@ namespace System.Net public override int Timeout { get { return timeout; } set { - if (value < 0) - throw new ArgumentException ("value"); + if (value < -1) +#if NET_2_0 + throw new ArgumentOutOfRangeException ("Timeout can be " + + "only set to 'System.Threading.Timeout.Infinite' " + + "or a value >= 0."); +#else + throw new ArgumentOutOfRangeException ("value"); +#endif timeout = value; } } + +#if NET_2_0 + public override bool UseDefaultCredentials + { + get { + throw new NotSupportedException (); + } + set { + throw new NotSupportedException (); + } + } +#endif // Methods private delegate Stream GetRequestStreamCallback (); private delegate WebResponse GetResponseCallback (); +#if NET_2_0 + 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 (); + } +#endif + 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."); @@ -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."); requesting = true; } - */ GetRequestStreamCallback c = new GetRequestStreamCallback (this.GetRequestStreamInternal); - return c.BeginInvoke (callback, state); + return c.BeginInvoke (callback, state); } public override Stream EndGetRequestStream (IAsyncResult asyncResult) @@ -178,7 +218,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); @@ -197,7 +237,7 @@ namespace System.Net { this.requestStream = new FileWebStream ( this, - FileMode.CreateNew, + FileMode.Create, FileAccess.Write, FileShare.Read); return this.requestStream; @@ -205,40 +245,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); @@ -247,11 +277,11 @@ namespace System.Net } return EndGetResponse (asyncResult); } - + WebResponse GetResponseInternal () { if (webResponse != null) - return webResponse; + return webResponse; lock (this) { if (requesting) { requestEndEvent = new AutoResetEvent (false); @@ -260,24 +290,40 @@ 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); +#if NET_2_0 + protected override +#endif + 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); +#if NET_2_0 + info.AddValue ("preauthenticate", false); +#else + info.AddValue ("preauthenticate", preAuthenticate); +#endif } internal void Close () @@ -287,7 +333,7 @@ namespace System.Net // requestStream.Close (); // } - lock (this) { + lock (this) { requesting = false; if (requestEndEvent != null) requestEndEvent.Set (); @@ -309,7 +355,7 @@ namespace System.Net { this.webRequest = webRequest; } - + public override void Close() { base.Close ();