2 // System.Net.FileWebRequest
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
9 // Permission is hereby granted, free of charge, to any person obtaining
\r
10 // a copy of this software and associated documentation files (the
\r
11 // "Software"), to deal in the Software without restriction, including
\r
12 // without limitation the rights to use, copy, modify, merge, publish,
\r
13 // distribute, sublicense, and/or sell copies of the Software, and to
\r
14 // permit persons to whom the Software is furnished to do so, subject to
\r
15 // the following conditions:
\r
17 // The above copyright notice and this permission notice shall be
\r
18 // included in all copies or substantial portions of the Software.
\r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
30 using System.Collections;
\r
32 using System.Runtime.Serialization;
\r
33 using System.Runtime.Remoting.Messaging;
\r
34 using System.Threading;
\r
36 namespace System.Net
\r
39 public class FileWebRequest : WebRequest, ISerializable
\r
42 private WebHeaderCollection webHeaders;
\r
44 private ICredentials credentials;
\r
45 private string connectionGroup;
\r
46 private long contentLength;
\r
47 private FileAccess fileAccess = FileAccess.Read;
\r
48 private string method = "GET";
\r
49 private IWebProxy proxy;
\r
50 private bool preAuthenticate;
\r
51 private int timeout = 100000;
\r
53 private Stream requestStream;
\r
54 private FileWebResponse webResponse;
\r
55 private AutoResetEvent requestEndEvent;
\r
56 private bool requesting;
\r
57 private bool asyncResponding;
\r
61 internal FileWebRequest (Uri uri)
\r
64 this.webHeaders = new WebHeaderCollection ();
\r
68 [Obsolete ("Serialization is obsoleted for this type", false)]
\r
70 protected FileWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
72 SerializationInfo info = serializationInfo;
\r
73 webHeaders = (WebHeaderCollection) info.GetValue ("headers", typeof (WebHeaderCollection));
\r
74 proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
\r
75 uri = (Uri) info.GetValue ("uri", typeof (Uri));
\r
76 connectionGroup = info.GetString ("connectionGroupName");
\r
77 method = info.GetString ("method");
\r
78 contentLength = info.GetInt64 ("contentLength");
\r
79 timeout = info.GetInt32 ("timeout");
\r
80 fileAccess = (FileAccess) info.GetValue ("fileAccess", typeof (FileAccess));
\r
81 preAuthenticate = info.GetBoolean ("preauthenticate");
\r
86 // currently not used according to spec
\r
87 public override string ConnectionGroupName {
\r
88 get { return connectionGroup; }
\r
89 set { connectionGroup = value; }
\r
92 public override long ContentLength {
\r
93 get { return contentLength; }
\r
97 throw new ArgumentException ("The Content-Length value must be greater than or equal to zero.", "value");
\r
99 throw new ArgumentException ("value");
\r
101 contentLength = value;
\r
105 public override string ContentType {
\r
106 get { return webHeaders ["Content-Type"]; }
\r
107 set { webHeaders ["Content-Type"] = value; }
\r
110 public override ICredentials Credentials {
\r
111 get { return credentials; }
\r
112 set { credentials = value; }
\r
115 public override WebHeaderCollection Headers {
\r
116 get { return webHeaders; }
\r
119 // currently not used according to spec
\r
120 public override string Method {
\r
121 get { return this.method; }
\r
123 if (value == null || value.Length == 0)
\r
125 throw new ArgumentException ("Cannot set null or blank "
\r
126 + "methods on request.", "value");
\r
128 throw new ArgumentException ("Cannot set null or blank "
\r
129 + "methods on request.");
\r
131 this.method = value;
\r
135 // currently not used according to spec
\r
136 public override bool PreAuthenticate {
\r
137 get { return preAuthenticate; }
\r
138 set { preAuthenticate = value; }
\r
141 // currently not used according to spec
\r
142 public override IWebProxy Proxy {
\r
143 get { return proxy; }
\r
144 set { proxy = value; }
\r
147 public override Uri RequestUri {
\r
148 get { return this.uri; }
\r
151 public override int Timeout {
\r
152 get { return timeout; }
\r
156 throw new ArgumentOutOfRangeException ("Timeout can be "
\r
157 + "only set to 'System.Threading.Timeout.Infinite' "
\r
158 + "or a value >= 0.");
\r
160 throw new ArgumentOutOfRangeException ("value");
\r
167 public override bool UseDefaultCredentials
\r
170 throw new NotSupportedException ();
\r
173 throw new NotSupportedException ();
\r
180 private delegate Stream GetRequestStreamCallback ();
\r
181 private delegate WebResponse GetResponseCallback ();
\r
184 static Exception GetMustImplement ()
\r
186 return new NotImplementedException ();
\r
189 /* LAMESPEC: Docs suggest this was present in 1.1 and
\r
190 * 1.0 profiles, but the masterinfos say otherwise
\r
193 public override void Abort ()
\r
195 throw GetMustImplement ();
\r
199 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
\r
201 if (string.Compare ("GET", method, true) == 0 ||
\r
202 string.Compare ("HEAD", method, true) == 0 ||
\r
203 string.Compare ("CONNECT", method, true) == 0)
\r
204 throw new ProtocolViolationException ("Cannot send a content-body with this verb-type.");
\r
206 if (asyncResponding || webResponse != null)
\r
207 throw new InvalidOperationException ("This operation cannot be performed after the request has been submitted.");
\r
209 throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");
\r
212 GetRequestStreamCallback c = new GetRequestStreamCallback (this.GetRequestStreamInternal);
\r
213 return c.BeginInvoke (callback, state);
\r
216 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
\r
218 if (asyncResult == null)
\r
219 throw new ArgumentNullException ("asyncResult");
\r
220 if (!asyncResult.IsCompleted)
\r
221 asyncResult.AsyncWaitHandle.WaitOne ();
\r
222 AsyncResult async = (AsyncResult) asyncResult;
\r
223 GetRequestStreamCallback cb = (GetRequestStreamCallback) async.AsyncDelegate;
\r
224 return cb.EndInvoke (asyncResult);
\r
227 public override Stream GetRequestStream()
\r
229 IAsyncResult asyncResult = BeginGetRequestStream (null, null);
\r
230 if (!(asyncResult.AsyncWaitHandle.WaitOne (timeout, false))) {
\r
231 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
\r
233 return EndGetRequestStream (asyncResult);
\r
236 internal Stream GetRequestStreamInternal ()
\r
238 this.requestStream = new FileWebStream (
\r
243 return this.requestStream;
\r
246 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
\r
249 if (asyncResponding)
\r
250 throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");
\r
251 asyncResponding = true;
\r
253 GetResponseCallback c = new GetResponseCallback (this.GetResponseInternal);
\r
254 return c.BeginInvoke (callback, state);
\r
257 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
\r
259 if (asyncResult == null)
\r
260 throw new ArgumentNullException ("asyncResult");
\r
261 if (!asyncResult.IsCompleted)
\r
262 asyncResult.AsyncWaitHandle.WaitOne ();
\r
263 AsyncResult async = (AsyncResult) asyncResult;
\r
264 GetResponseCallback cb = (GetResponseCallback) async.AsyncDelegate;
\r
265 FileWebResponse webResponse = (FileWebResponse) cb.EndInvoke(asyncResult);
\r
266 asyncResponding = false;
\r
267 if (webResponse.HasError)
\r
268 throw webResponse.Error;
\r
269 return webResponse;
\r
272 public override WebResponse GetResponse ()
\r
274 IAsyncResult asyncResult = BeginGetResponse (null, null);
\r
275 if (!(asyncResult.AsyncWaitHandle.WaitOne (timeout, false))) {
\r
276 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
\r
278 return EndGetResponse (asyncResult);
\r
281 WebResponse GetResponseInternal ()
\r
283 if (webResponse != null)
\r
284 return webResponse;
\r
287 requestEndEvent = new AutoResetEvent (false);
\r
290 if (requestEndEvent != null) {
\r
291 requestEndEvent.WaitOne ();
\r
293 FileStream fileStream = null;
\r
295 fileStream = new FileWebStream (this, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
296 this.webResponse = new FileWebResponse (this.uri, fileStream);
\r
297 } catch (Exception ex) {
\r
298 this.webResponse = new FileWebResponse (this.uri, new WebException (ex.Message, ex));
\r
300 return this.webResponse;
\r
303 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
305 GetObjectData (serializationInfo, streamingContext);
\r
311 void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
313 SerializationInfo info = serializationInfo;
\r
314 info.AddValue ("headers", webHeaders, typeof (WebHeaderCollection));
\r
315 info.AddValue ("proxy", proxy, typeof (IWebProxy));
\r
316 info.AddValue ("uri", uri, typeof (Uri));
\r
317 info.AddValue ("connectionGroupName", connectionGroup);
\r
318 info.AddValue ("method", method);
\r
319 info.AddValue ("contentLength", contentLength);
\r
320 info.AddValue ("timeout", timeout);
\r
321 info.AddValue ("fileAccess", fileAccess);
\r
323 info.AddValue ("preauthenticate", false);
\r
325 info.AddValue ("preauthenticate", preAuthenticate);
\r
329 internal void Close ()
\r
331 // already done in class below
\r
332 // if (requestStream != null) {
\r
333 // requestStream.Close ();
\r
337 requesting = false;
\r
338 if (requestEndEvent != null)
\r
339 requestEndEvent.Set ();
\r
340 // requestEndEvent = null;
\r
344 // to catch the Close called on the FileStream
\r
345 internal class FileWebStream : FileStream
\r
347 FileWebRequest webRequest;
\r
349 internal FileWebStream (FileWebRequest webRequest,
\r
353 : base (webRequest.RequestUri.LocalPath,
\r
354 mode, access, share)
\r
356 this.webRequest = webRequest;
\r
359 public override void Close()
\r
362 FileWebRequest req = webRequest;
\r