Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System / System.Net / WebClient.cs
1 //
2 // System.Net.WebClient
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Atsushi Enomoto (atsushi@ximian.com)
8 //      Miguel de Icaza (miguel@ximian.com)
9 //
10 // Copyright 2003 Ximian, Inc. (http://www.ximian.com)
11 // Copyright 2006, 2010 Novell, Inc. (http://www.novell.com)
12 //
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 //
34 // Notes on CancelAsync and Async methods:
35 //
36 //    WebClient.CancelAsync is implemented by calling Thread.Interrupt
37 //    in our helper thread.   The various async methods have to cancel
38 //    any ongoing requests by calling request.Abort () at that point.
39 //    In a few places (UploadDataCore, UploadValuesCore,
40 //    UploadFileCore) we catch the ThreadInterruptedException and
41 //    abort the request there.
42 //
43 //    Higher level routines (the async callbacks) also need to catch
44 //    the exception and raise the OnXXXXCompleted events there with
45 //    the "canceled" flag set to true. 
46 //
47 //    In a few other places where these helper routines are not used
48 //    (OpenReadAsync for example) catching the ThreadAbortException
49 //    also must abort the request.
50 //
51 //    The Async methods currently differ in their implementation from
52 //    the .NET implementation in that we manually catch any other
53 //    exceptions and correctly raise the OnXXXXCompleted passing the
54 //    Exception that caused the problem.   The .NET implementation
55 //    does not seem to have a mechanism to flag errors that happen
56 //    during downloads though.    We do this because we still need to
57 //    catch the exception on these helper threads, or we would
58 //    otherwise kill the application (on the 2.x profile, uncaught
59 //    exceptions in threads terminate the application).
60 //
61 using System;
62 using System.Collections.Specialized;
63 using System.ComponentModel;
64 using System.IO;
65 using System.Runtime.InteropServices;
66 using System.Runtime.Serialization;
67 using System.Text;
68 using System.Threading;
69 using System.Net.Cache;
70
71 namespace System.Net 
72 {
73         [ComVisible(true)]
74         public class WebClient : Component
75         {
76                 static readonly string urlEncodedCType = "application/x-www-form-urlencoded";
77                 static byte [] hexBytes;
78                 ICredentials credentials;
79                 WebHeaderCollection headers;
80                 WebHeaderCollection responseHeaders;
81                 Uri baseAddress;
82                 string baseString;
83                 NameValueCollection queryString;
84                 bool is_busy;
85                 bool async;
86                 Thread async_thread;
87                 Encoding encoding = Encoding.Default;
88                 IWebProxy proxy;
89                 RequestCachePolicy cache_policy;
90
91                 // Constructors
92                 static WebClient ()
93                 {
94                         hexBytes = new byte [16];
95                         int index = 0;
96                         for (int i = '0'; i <= '9'; i++, index++)
97                                 hexBytes [index] = (byte) i;
98
99                         for (int i = 'a'; i <= 'f'; i++, index++)
100                                 hexBytes [index] = (byte) i;
101                 }
102                 
103                 public WebClient ()
104                 {
105                 }
106                 
107                 // Properties
108                 
109                 public string BaseAddress {
110                         get {
111                                 if (baseString == null) {
112                                         if (baseAddress == null)
113                                                 return string.Empty;
114                                 }
115
116                                 baseString = baseAddress.ToString ();
117                                 return baseString;
118                         }
119                         
120                         set {
121                                 if (value == null || value.Length == 0) {
122                                         baseAddress = null;
123                                 } else {
124                                         baseAddress = new Uri (value);
125                                 }
126                         }
127                 }
128
129                 static Exception GetMustImplement ()
130                 {
131                         return new NotImplementedException ();
132                 }
133                 
134                 [MonoTODO ("Value can be set but is currently ignored")]
135                 public RequestCachePolicy CachePolicy
136                 {
137                         get {
138                                 throw GetMustImplement ();
139                         }
140                         set { cache_policy = value; }
141                 }
142
143                 [MonoTODO ("Value can be set but is ignored")]
144                 public bool UseDefaultCredentials
145                 {
146                         get {
147                                 throw GetMustImplement ();
148                         }
149                         set {
150                                 // This makes no sense in mono
151                         }
152                 }
153                 
154                 public ICredentials Credentials {
155                         get { return credentials; }
156                         set { credentials = value; }
157                 }
158
159                 public WebHeaderCollection Headers {
160                         get {
161                                 if (headers == null)
162                                         headers = new WebHeaderCollection ();
163
164                                 return headers;
165                         }
166                         set { headers = value; }
167                 }
168                 
169                 public NameValueCollection QueryString {
170                         get {
171                                 if (queryString == null)
172                                         queryString = new NameValueCollection ();
173
174                                 return queryString;
175                         }
176                         set { queryString = value; }
177                 }
178                 
179                 public WebHeaderCollection ResponseHeaders {
180                         get { return responseHeaders; }
181                 }
182
183                 public Encoding Encoding {
184                         get { return encoding; }
185                         set {
186                                 if (value == null)
187                                         throw new ArgumentNullException ("Encoding");
188                                 encoding = value;
189                         }
190                 }
191
192                 public IWebProxy Proxy {
193                         get { return proxy; }
194                         set { proxy = value; }
195                 }
196
197                 public bool IsBusy {
198                         get { return is_busy; } 
199                 }
200                 // Methods
201
202                 void CheckBusy ()
203                 {
204                         if (IsBusy)
205                                 throw new NotSupportedException ("WebClient does not support conccurent I/O operations.");
206                 }
207
208                 void SetBusy ()
209                 {
210                         lock (this) {
211                                 CheckBusy ();
212                                 is_busy = true;
213                         }
214                 }
215
216                 //   DownloadData
217
218                 public byte [] DownloadData (string address)
219                 {
220                         if (address == null)
221                                 throw new ArgumentNullException ("address");
222
223                         return DownloadData (CreateUri (address));
224                 }
225
226                 public byte [] DownloadData (Uri address)
227                 {
228                         if (address == null)
229                                 throw new ArgumentNullException ("address");
230
231                         try {
232                                 SetBusy ();
233                                 async = false;
234                                 return DownloadDataCore (address, null);
235                         } finally {
236                                 is_busy = false;
237                         }
238                 }
239
240                 byte [] DownloadDataCore (Uri address, object userToken)
241                 {
242                         WebRequest request = null;
243                         
244                         try {
245                                 request = SetupRequest (address);
246                                 return ReadAll (request, userToken);
247                         } catch (ThreadInterruptedException){
248                                 if (request != null)
249                                         request.Abort ();
250                                 throw;
251                         } catch (WebException) {
252                                 throw;
253                         } catch (Exception ex) {
254                                 throw new WebException ("An error occurred " +
255                                         "performing a WebClient request.", ex);
256                         }
257                 }
258
259                 //   DownloadFile
260
261                 public void DownloadFile (string address, string fileName)
262                 {
263                         if (address == null)
264                                 throw new ArgumentNullException ("address");
265
266                         DownloadFile (CreateUri (address), fileName);
267                 }
268
269                 public void DownloadFile (Uri address, string fileName)
270                 {
271                         if (address == null)
272                                 throw new ArgumentNullException ("address");
273                         if (fileName == null)
274                                 throw new ArgumentNullException ("fileName");
275
276                         try {
277                                 SetBusy ();
278                                 async = false;
279                                 DownloadFileCore (address, fileName, null);
280                         } catch (WebException) {
281                                 throw;
282                         } catch (Exception ex) {
283                                 throw new WebException ("An error occurred " +
284                                         "performing a WebClient request.", ex);
285                         } finally {
286                                 is_busy = false;
287                         }
288                 }
289
290                 void DownloadFileCore (Uri address, string fileName, object userToken)
291                 {
292                         WebRequest request = null;
293                         
294                         using (FileStream f = new FileStream (fileName, FileMode.Create)) {
295                                 try {
296                                         request = SetupRequest (address);
297                                         WebResponse response = GetWebResponse (request);
298                                         Stream st = response.GetResponseStream ();
299                                         
300                                         int cLength = (int) response.ContentLength;
301                                         int length = (cLength <= -1 || cLength > 32*1024) ? 32*1024 : cLength;
302                                         byte [] buffer = new byte [length];
303                                         
304                                         int nread = 0;
305                                         long notify_total = 0;
306                                         while ((nread = st.Read (buffer, 0, length)) != 0){
307                                                 if (async){
308                                                         notify_total += nread;
309                                                         OnDownloadProgressChanged (
310                                                                 new DownloadProgressChangedEventArgs (notify_total, response.ContentLength, userToken));
311                                                                                                       
312                                                 }
313                                                 f.Write (buffer, 0, nread);
314                                         }
315                                 } catch (ThreadInterruptedException){
316                                         if (request != null)
317                                                 request.Abort ();
318                                         throw;
319                                 }
320                         }
321                 }
322
323                 //   OpenRead
324
325                 public Stream OpenRead (string address)
326                 {
327                         if (address == null)
328                                 throw new ArgumentNullException ("address");
329                         return OpenRead (CreateUri (address));
330                 }
331
332                 public Stream OpenRead (Uri address)
333                 {
334                         if (address == null)
335                                 throw new ArgumentNullException ("address");
336
337                         WebRequest request = null;
338                         try {
339                                 SetBusy ();
340                                 async = false;
341                                 request = SetupRequest (address);
342                                 WebResponse response = GetWebResponse (request);
343                                 return response.GetResponseStream ();
344                         } catch (WebException) {
345                                 throw;
346                         } catch (Exception ex) {
347                                 throw new WebException ("An error occurred " +
348                                         "performing a WebClient request.", ex);
349                         } finally {
350                                 is_busy = false;
351                         }
352                 }
353
354                 //   OpenWrite
355
356                 public Stream OpenWrite (string address)
357                 {
358                         if (address == null)
359                                 throw new ArgumentNullException ("address");
360
361                         return OpenWrite (CreateUri (address));
362                 }
363                 
364                 public Stream OpenWrite (string address, string method)
365                 {
366                         if (address == null)
367                                 throw new ArgumentNullException ("address");
368
369                         return OpenWrite (CreateUri (address), method);
370                 }
371
372                 public Stream OpenWrite (Uri address)
373                 {
374                         return OpenWrite (address, (string) null);
375                 }
376
377                 public Stream OpenWrite (Uri address, string method)
378                 {
379                         if (address == null)
380                                 throw new ArgumentNullException ("address");
381
382                         try {
383                                 SetBusy ();
384                                 async = false;
385                                 WebRequest request = SetupRequest (address, method, true);
386                                 return request.GetRequestStream ();
387                         } catch (WebException) {
388                                 throw;
389                         } catch (Exception ex) {
390                                 throw new WebException ("An error occurred " +
391                                         "performing a WebClient request.", ex);
392                         } finally {
393                                 is_busy = false;
394                         }
395                 }
396
397                 private string DetermineMethod (Uri address, string method, bool is_upload)
398                 {
399                         if (method != null)
400                                 return method;
401
402                         if (address.Scheme == Uri.UriSchemeFtp)
403                                 return (is_upload) ? "STOR" : "RETR";
404
405                         return (is_upload) ? "POST" : "GET";
406                 }
407
408                 //   UploadData
409
410                 public byte [] UploadData (string address, byte [] data)
411                 {
412                         if (address == null)
413                                 throw new ArgumentNullException ("address");
414
415                         return UploadData (CreateUri (address), data);
416                 }
417                 
418                 public byte [] UploadData (string address, string method, byte [] data)
419                 {
420                         if (address == null)
421                                 throw new ArgumentNullException ("address");
422
423                         return UploadData (CreateUri (address), method, data);
424                 }
425
426                 public byte [] UploadData (Uri address, byte [] data)
427                 {
428                         return UploadData (address, (string) null, data);
429                 }
430
431                 public byte [] UploadData (Uri address, string method, byte [] data)
432                 {
433                         if (address == null)
434                                 throw new ArgumentNullException ("address");
435                         if (data == null)
436                                 throw new ArgumentNullException ("data");
437
438                         try {
439                                 SetBusy ();
440                                 async = false;
441                                 return UploadDataCore (address, method, data, null);
442                         } catch (WebException) {
443                                 throw;
444                         } catch (Exception ex) {
445                                 throw new WebException ("An error occurred " +
446                                         "performing a WebClient request.", ex);
447                         } finally {
448                                 is_busy = false;
449                         }
450                 }
451
452                 byte [] UploadDataCore (Uri address, string method, byte [] data, object userToken)
453                 {
454                         WebRequest request = SetupRequest (address, method, true);
455                         try {
456                                 int contentLength = data.Length;
457                                 request.ContentLength = contentLength;
458                                 using (Stream stream = request.GetRequestStream ()) {
459                                         stream.Write (data, 0, contentLength);
460                                 }
461                                 
462                                 return ReadAll (request, userToken);
463                         } catch (ThreadInterruptedException){
464                                 if (request != null)
465                                         request.Abort ();
466                                 throw;
467                         }
468                 }
469
470                 //   UploadFile
471
472                 public byte [] UploadFile (string address, string fileName)
473                 {
474                         if (address == null)
475                                 throw new ArgumentNullException ("address");
476
477                         return UploadFile (CreateUri (address), fileName);
478                 }
479
480                 public byte [] UploadFile (Uri address, string fileName)
481                 {
482                         return UploadFile (address, (string) null, fileName);
483                 }
484                 
485                 public byte [] UploadFile (string address, string method, string fileName)
486                 {
487                         return UploadFile (CreateUri (address), method, fileName);
488                 }
489
490                 public byte [] UploadFile (Uri address, string method, string fileName)
491                 {
492                         if (address == null)
493                                 throw new ArgumentNullException ("address");
494                         if (fileName == null)
495                                 throw new ArgumentNullException ("fileName");
496
497                         try {
498                                 SetBusy ();
499                                 async = false;
500                                 return UploadFileCore (address, method, fileName, null);
501                         } catch (WebException) {
502                                 throw;
503                         } catch (Exception ex) {
504                                 throw new WebException ("An error occurred " +
505                                         "performing a WebClient request.", ex);
506                         } finally {
507                                 is_busy = false;
508                         }
509                 }
510
511                 byte [] UploadFileCore (Uri address, string method, string fileName, object userToken)
512                 {
513                         string fileCType = Headers ["Content-Type"];
514                         if (fileCType != null) {
515                                 string lower = fileCType.ToLower ();
516                                 if (lower.StartsWith ("multipart/"))
517                                         throw new WebException ("Content-Type cannot be set to a multipart" +
518                                                                 " type for this request.");
519                         } else {
520                                 fileCType = "application/octet-stream";
521                         }
522
523                         string boundary = "------------" + DateTime.Now.Ticks.ToString ("x");
524                         Headers ["Content-Type"] = String.Format ("multipart/form-data; boundary={0}", boundary);
525                         Stream reqStream = null;
526                         Stream fStream = null;
527                         byte [] resultBytes = null;
528
529                         fileName = Path.GetFullPath (fileName);
530
531                         WebRequest request = null;
532                         try {
533                                 fStream = File.OpenRead (fileName);
534                                 request = SetupRequest (address, method, true);
535                                 reqStream = request.GetRequestStream ();
536                                 byte [] bytes_boundary = Encoding.ASCII.GetBytes (boundary);
537                                 reqStream.WriteByte ((byte) '-');
538                                 reqStream.WriteByte ((byte) '-');
539                                 reqStream.Write (bytes_boundary, 0, bytes_boundary.Length);
540                                 reqStream.WriteByte ((byte) '\r');
541                                 reqStream.WriteByte ((byte) '\n');
542                                 string partHeaders = String.Format ("Content-Disposition: form-data; " +
543                                                                     "name=\"file\"; filename=\"{0}\"\r\n" +
544                                                                     "Content-Type: {1}\r\n\r\n",
545                                                                     Path.GetFileName (fileName), fileCType);
546
547                                 byte [] partHeadersBytes = Encoding.UTF8.GetBytes (partHeaders);
548                                 reqStream.Write (partHeadersBytes, 0, partHeadersBytes.Length);
549                                 int nread;
550                                 byte [] buffer = new byte [4096];
551                                 while ((nread = fStream.Read (buffer, 0, 4096)) != 0)
552                                         reqStream.Write (buffer, 0, nread);
553
554                                 reqStream.WriteByte ((byte) '\r');
555                                 reqStream.WriteByte ((byte) '\n');
556                                 reqStream.WriteByte ((byte) '-');
557                                 reqStream.WriteByte ((byte) '-');
558                                 reqStream.Write (bytes_boundary, 0, bytes_boundary.Length);
559                                 reqStream.WriteByte ((byte) '-');
560                                 reqStream.WriteByte ((byte) '-');
561                                 reqStream.WriteByte ((byte) '\r');
562                                 reqStream.WriteByte ((byte) '\n');
563                                 reqStream.Close ();
564                                 reqStream = null;
565                                 resultBytes = ReadAll (request, userToken);
566                         } catch (ThreadInterruptedException){
567                                 if (request != null)
568                                         request.Abort ();
569                                 throw;
570                         } finally {
571                                 if (fStream != null)
572                                         fStream.Close ();
573
574                                 if (reqStream != null)
575                                         reqStream.Close ();
576                         }
577                         
578                         return resultBytes;
579                 }
580                 
581                 public byte[] UploadValues (string address, NameValueCollection data)
582                 {
583                         if (address == null)
584                                 throw new ArgumentNullException ("address");
585
586                         return UploadValues (CreateUri (address), data);
587                 }
588                 
589                 public byte[] UploadValues (string address, string method, NameValueCollection data)
590                 {
591                         if (address == null)
592                                 throw new ArgumentNullException ("address");
593                         return UploadValues (CreateUri (address), method, data);
594                 }
595
596                 public byte[] UploadValues (Uri address, NameValueCollection data)
597                 {
598                         return UploadValues (address, (string) null, data);
599                 }
600
601                 public byte[] UploadValues (Uri address, string method, NameValueCollection data)
602                 {
603                         if (address == null)
604                                 throw new ArgumentNullException ("address");
605                         if (data == null)
606                                 throw new ArgumentNullException ("data");
607
608                         try {
609                                 SetBusy ();
610                                 async = false;
611                                 return UploadValuesCore (address, method, data, null);
612                         } catch (WebException) {
613                                 throw;
614                         } catch (Exception ex) {
615                                 throw new WebException ("An error occurred " +
616                                         "performing a WebClient request.", ex);
617                         } finally {
618                                 is_busy = false;
619                         }
620                 }
621
622                 byte[] UploadValuesCore (Uri uri, string method, NameValueCollection data, object userToken)
623                 {
624                         string cType = Headers ["Content-Type"];
625                         if (cType != null && String.Compare (cType, urlEncodedCType, true) != 0)
626                                 throw new WebException ("Content-Type header cannot be changed from its default " +
627                                                         "value for this request.");
628
629                         Headers ["Content-Type"] = urlEncodedCType;
630                         WebRequest request = SetupRequest (uri, method, true);
631                         try {
632                                 MemoryStream tmpStream = new MemoryStream ();
633                                 foreach (string key in data) {
634                                         byte [] bytes = Encoding.UTF8.GetBytes (key);
635                                         UrlEncodeAndWrite (tmpStream, bytes);
636                                         tmpStream.WriteByte ((byte) '=');
637                                         bytes = Encoding.UTF8.GetBytes (data [key]);
638                                         UrlEncodeAndWrite (tmpStream, bytes);
639                                         tmpStream.WriteByte ((byte) '&');
640                                 }
641                                 
642                                 int length = (int) tmpStream.Length;
643                                 if (length > 0)
644                                         tmpStream.SetLength (--length); // remove trailing '&'
645                                 
646                                 byte [] buf = tmpStream.GetBuffer ();
647                                 request.ContentLength = length;
648                                 using (Stream rqStream = request.GetRequestStream ()) {
649                                         rqStream.Write (buf, 0, length);
650                                 }
651                                 tmpStream.Close ();
652                                 
653                                 return ReadAll (request, userToken);
654                         } catch (ThreadInterruptedException) {
655                                 request.Abort ();
656                                 throw;
657                         }
658                 }
659
660                 public string DownloadString (string address)
661                 {
662                         if (address == null)
663                                 throw new ArgumentNullException ("address");
664
665                         return encoding.GetString (DownloadData (CreateUri (address)));
666                 }
667
668                 public string DownloadString (Uri address)
669                 {
670                         if (address == null)
671                                 throw new ArgumentNullException ("address");
672
673                         return encoding.GetString (DownloadData (CreateUri (address)));
674                 }
675
676                 public string UploadString (string address, string data)
677                 {
678                         if (address == null)
679                                 throw new ArgumentNullException ("address");
680                         if (data == null)
681                                 throw new ArgumentNullException ("data");
682
683                         byte [] resp = UploadData (address, encoding.GetBytes (data));
684                         return encoding.GetString (resp);
685                 }
686
687                 public string UploadString (string address, string method, string data)
688                 {
689                         if (address == null)
690                                 throw new ArgumentNullException ("address");
691                         if (data == null)
692                                 throw new ArgumentNullException ("data");
693
694                         byte [] resp = UploadData (address, method, encoding.GetBytes (data));
695                         return encoding.GetString (resp);
696                 }
697
698                 public string UploadString (Uri address, string data)
699                 {
700                         if (address == null)
701                                 throw new ArgumentNullException ("address");
702                         if (data == null)
703                                 throw new ArgumentNullException ("data");
704
705                         byte [] resp = UploadData (address, encoding.GetBytes (data));
706                         return encoding.GetString (resp);
707                 }
708
709                 public string UploadString (Uri address, string method, string data)
710                 {
711                         if (address == null)
712                                 throw new ArgumentNullException ("address");
713                         if (data == null)
714                                 throw new ArgumentNullException ("data");
715
716                         byte [] resp = UploadData (address, method, encoding.GetBytes (data));
717                         return encoding.GetString (resp);
718                 }
719
720                 public event DownloadDataCompletedEventHandler DownloadDataCompleted;
721                 public event AsyncCompletedEventHandler DownloadFileCompleted;
722                 public event DownloadProgressChangedEventHandler DownloadProgressChanged;
723                 public event DownloadStringCompletedEventHandler DownloadStringCompleted;
724                 public event OpenReadCompletedEventHandler OpenReadCompleted;
725                 public event OpenWriteCompletedEventHandler OpenWriteCompleted;
726                 public event UploadDataCompletedEventHandler UploadDataCompleted;
727                 public event UploadFileCompletedEventHandler UploadFileCompleted;
728                 public event UploadProgressChangedEventHandler UploadProgressChanged;
729                 public event UploadStringCompletedEventHandler UploadStringCompleted;
730                 public event UploadValuesCompletedEventHandler UploadValuesCompleted;
731
732                 Uri CreateUri (string address)
733                 {
734                         Uri uri;
735                         try {
736                                 if (baseAddress == null)
737                                         uri = new Uri (address);
738                                 else
739                                         uri = new Uri (baseAddress, address);
740                                 return CreateUri (uri);
741                         } catch {
742                         }
743                         return new Uri (Path.GetFullPath (address));
744                 }
745
746                 Uri CreateUri (Uri address)
747                 {
748                         Uri result = address;
749                         if (baseAddress != null && !result.IsAbsoluteUri) {
750                                 try {
751                                         result = new Uri (baseAddress, result.OriginalString);
752                                 } catch {
753                                         return result; // Not much we can do here.
754                                 }
755                         }
756
757                         string query = result.Query;
758                         if (String.IsNullOrEmpty (query))
759                                 query = GetQueryString (true);
760                         UriBuilder builder = new UriBuilder (address);
761                         if (!String.IsNullOrEmpty (query))
762                                 builder.Query = query.Substring (1);
763                         return builder.Uri;
764                 }
765
766                 string GetQueryString (bool add_qmark)
767                 {
768                         if (queryString == null || queryString.Count == 0)
769                                 return null;
770
771                         StringBuilder sb = new StringBuilder ();
772                         if (add_qmark)
773                                 sb.Append ('?');
774
775                         foreach (string key in queryString)
776                                 sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
777
778                         if (sb.Length != 0)
779                                 sb.Length--; // removes last '&' or the '?' if empty.
780
781                         if (sb.Length == 0)
782                                 return null;
783
784                         return sb.ToString ();
785                 }
786
787                 WebRequest SetupRequest (Uri uri)
788                 {
789                         WebRequest request = GetWebRequest (uri);
790                         if (Proxy != null)
791                                 request.Proxy = Proxy;
792                         request.Credentials = credentials;
793
794                         // Special headers. These are properties of HttpWebRequest.
795                         // What do we do with other requests differnt from HttpWebRequest?
796                         if (headers != null && headers.Count != 0 && (request is HttpWebRequest)) {
797                                 HttpWebRequest req = (HttpWebRequest) request;
798                                 string expect = headers ["Expect"];
799                                 string contentType = headers ["Content-Type"];
800                                 string accept = headers ["Accept"];
801                                 string connection = headers ["Connection"];
802                                 string userAgent = headers ["User-Agent"];
803                                 string referer = headers ["Referer"];
804                                 headers.RemoveInternal ("Expect");
805                                 headers.RemoveInternal ("Content-Type");
806                                 headers.RemoveInternal ("Accept");
807                                 headers.RemoveInternal ("Connection");
808                                 headers.RemoveInternal ("Referer");
809                                 headers.RemoveInternal ("User-Agent");
810                                 request.Headers = headers;
811
812                                 if (expect != null && expect.Length > 0)
813                                         req.Expect = expect;
814
815                                 if (accept != null && accept.Length > 0)
816                                         req.Accept = accept;
817
818                                 if (contentType != null && contentType.Length > 0)
819                                         req.ContentType = contentType;
820
821                                 if (connection != null && connection.Length > 0)
822                                         req.Connection = connection;
823
824                                 if (userAgent != null && userAgent.Length > 0)
825                                         req.UserAgent = userAgent;
826
827                                 if (referer != null && referer.Length > 0)
828                                         req.Referer = referer;
829                         }
830
831                         responseHeaders = null;
832                         return request;
833                 }
834
835                 WebRequest SetupRequest (Uri uri, string method, bool is_upload)
836                 {
837                         WebRequest request = SetupRequest (uri);
838                         request.Method = DetermineMethod (uri, method, is_upload);
839                         return request;
840                 }
841
842                 byte [] ReadAll (WebRequest request, object userToken)
843                 {
844                         WebResponse response = GetWebResponse (request);
845                         Stream stream = response.GetResponseStream ();
846                         int length = (int) response.ContentLength;
847                         HttpWebRequest wreq = request as HttpWebRequest;
848
849                         if (length > -1 && wreq != null && (int) wreq.AutomaticDecompression != 0) {
850                                 string content_encoding = ((HttpWebResponse) response).ContentEncoding;
851                                 if (((content_encoding == "gzip" && (wreq.AutomaticDecompression & DecompressionMethods.GZip) != 0)) ||
852                                         ((content_encoding == "deflate" && (wreq.AutomaticDecompression & DecompressionMethods.Deflate) != 0)))
853                                         length = -1;
854                         }
855
856                         MemoryStream ms = null;
857                         bool nolength = (length == -1);
858                         int size = ((nolength) ? 8192 : length);
859                         if (nolength)
860                                 ms = new MemoryStream ();
861
862 //                      long total = 0;
863                         int nread = 0;
864                         int offset = 0;
865                         byte [] buffer = new byte [size];
866                         while ((nread = stream.Read (buffer, offset, size)) != 0) {
867                                 if (nolength) {
868                                         ms.Write (buffer, 0, nread);
869                                 } else {
870                                         offset += nread;
871                                         size -= nread;
872                                 }
873                                 if (async){
874 //                                      total += nread;
875                                         OnDownloadProgressChanged (new DownloadProgressChangedEventArgs (nread, length, userToken));
876                                 }
877                         }
878
879                         if (nolength)
880                                 return ms.ToArray ();
881
882                         return buffer;
883                 }
884
885                 string UrlEncode (string str)
886                 {
887                         StringBuilder result = new StringBuilder ();
888
889                         int len = str.Length;
890                         for (int i = 0; i < len; i++) {
891                                 char c = str [i];
892                                 if (c == ' ')
893                                         result.Append ('+');
894                                 else if ((c < '0' && c != '-' && c != '.') ||
895                                          (c < 'A' && c > '9') ||
896                                          (c > 'Z' && c < 'a' && c != '_') ||
897                                          (c > 'z')) {
898                                         result.Append ('%');
899                                         int idx = ((int) c) >> 4;
900                                         result.Append ((char) hexBytes [idx]);
901                                         idx = ((int) c) & 0x0F;
902                                         result.Append ((char) hexBytes [idx]);
903                                 } else {
904                                         result.Append (c);
905                                 }
906                         }
907
908                         return result.ToString ();
909                 }
910
911                 static void UrlEncodeAndWrite (Stream stream, byte [] bytes)
912                 {
913                         if (bytes == null)
914                                 return;
915
916                         int len = bytes.Length;
917                         if (len == 0)
918                                 return;
919
920                         for (int i = 0; i < len; i++) {
921                                 char c = (char) bytes [i];
922                                 if (c == ' ')
923                                         stream.WriteByte ((byte) '+');
924                                 else if ((c < '0' && c != '-' && c != '.') ||
925                                          (c < 'A' && c > '9') ||
926                                          (c > 'Z' && c < 'a' && c != '_') ||
927                                          (c > 'z')) {
928                                         stream.WriteByte ((byte) '%');
929                                         int idx = ((int) c) >> 4;
930                                         stream.WriteByte (hexBytes [idx]);
931                                         idx = ((int) c) & 0x0F;
932                                         stream.WriteByte (hexBytes [idx]);
933                                 } else {
934                                         stream.WriteByte ((byte) c);
935                                 }
936                         }
937                 }
938
939                 public void CancelAsync ()
940                 {
941                         lock (this){
942                                 if (async_thread == null)
943                                         return;
944
945                                 //
946                                 // We first flag things as done, in case the Interrupt hangs
947                                 // or the thread decides to hang in some other way inside the
948                                 // event handlers, or if we are stuck somewhere else.  This
949                                 // ensures that the WebClient object is reusable immediately
950                                 //
951                                 Thread t = async_thread;
952                                 CompleteAsync ();
953                                 t.Interrupt ();
954                         }
955                 }
956
957                 void CompleteAsync ()
958                 {
959                         lock (this){
960                                 is_busy = false;
961                                 async_thread = null;
962                         }
963                 }
964
965                 //    DownloadDataAsync
966
967                 public void DownloadDataAsync (Uri address)
968                 {
969                         DownloadDataAsync (address, null);
970                 }
971
972                 public void DownloadDataAsync (Uri address, object userToken)
973                 {
974                         if (address == null)
975                                 throw new ArgumentNullException ("address");
976                         
977                         lock (this) {
978                                 SetBusy ();
979                                 async = true;
980                                 
981                                 async_thread = new Thread (delegate (object state) {
982                                         object [] args = (object []) state;
983                                         try {
984                                                 byte [] data = DownloadDataCore ((Uri) args [0], args [1]);
985                                                 OnDownloadDataCompleted (
986                                                         new DownloadDataCompletedEventArgs (data, null, false, args [1]));
987                                         } catch (ThreadInterruptedException){
988                                                 OnDownloadDataCompleted (
989                                                         new DownloadDataCompletedEventArgs (null, null, true, args [1]));
990                                                 throw;
991                                         } catch (Exception e){
992                                                 OnDownloadDataCompleted (
993                                                         new DownloadDataCompletedEventArgs (null, e, false, args [1]));
994                                         }
995                                 });
996                                 object [] cb_args = new object [] {address, userToken};
997                                 async_thread.Start (cb_args);
998                         }
999                 }
1000
1001                 //    DownloadFileAsync
1002
1003                 public void DownloadFileAsync (Uri address, string fileName)
1004                 {
1005                         DownloadFileAsync (address, fileName, null);
1006                 }
1007
1008                 public void DownloadFileAsync (Uri address, string fileName, object userToken)
1009                 {
1010                         if (address == null)
1011                                 throw new ArgumentNullException ("address");
1012                         if (fileName == null)
1013                                 throw new ArgumentNullException ("fileName");
1014                         
1015                         lock (this) {
1016                                 SetBusy ();
1017                                 async = true;
1018
1019                                 async_thread = new Thread (delegate (object state) {
1020                                         object [] args = (object []) state;
1021                                         try {
1022                                                 DownloadFileCore ((Uri) args [0], (string) args [1], args [2]);
1023                                                 OnDownloadFileCompleted (
1024                                                         new AsyncCompletedEventArgs (null, false, args [2]));
1025                                         } catch (ThreadInterruptedException){
1026                                                 OnDownloadFileCompleted (
1027                                                         new AsyncCompletedEventArgs (null, true, args [2]));
1028                                         } catch (Exception e){
1029                                                 OnDownloadFileCompleted (
1030                                                         new AsyncCompletedEventArgs (e, false, args [2]));
1031                                         }});
1032                                 object [] cb_args = new object [] {address, fileName, userToken};
1033                                 async_thread.Start (cb_args);
1034                         }
1035                 }
1036
1037                 //    DownloadStringAsync
1038
1039                 public void DownloadStringAsync (Uri address)
1040                 {
1041                         DownloadStringAsync (address, null);
1042                 }
1043
1044                 public void DownloadStringAsync (Uri address, object userToken)
1045                 {
1046                         if (address == null)
1047                                 throw new ArgumentNullException ("address");
1048                         
1049                         lock (this) {
1050                                 SetBusy ();
1051                                 async = true;
1052
1053                                 async_thread = new Thread (delegate (object state) {
1054                                         object [] args = (object []) state;
1055                                         try {
1056                                                 string data = encoding.GetString (DownloadDataCore ((Uri) args [0], args [1]));
1057                                                 OnDownloadStringCompleted (
1058                                                         new DownloadStringCompletedEventArgs (data, null, false, args [1]));
1059                                         } catch (ThreadInterruptedException){
1060                                                 OnDownloadStringCompleted (
1061                                                         new DownloadStringCompletedEventArgs (null, null, true, args [1]));
1062                                         } catch (Exception e){
1063                                                 OnDownloadStringCompleted (
1064                                                         new DownloadStringCompletedEventArgs (null, e, false, args [1]));
1065                                         }});
1066                                 object [] cb_args = new object [] {address, userToken};
1067                                 async_thread.Start (cb_args);
1068                         }
1069                 }
1070
1071                 //    OpenReadAsync
1072
1073                 public void OpenReadAsync (Uri address)
1074                 {
1075                         OpenReadAsync (address, null);
1076                 }
1077
1078                 public void OpenReadAsync (Uri address, object userToken)
1079                 {
1080                         if (address == null)
1081                                 throw new ArgumentNullException ("address");
1082                         
1083                         lock (this) {
1084                                 SetBusy ();
1085                                 async = true;
1086
1087                                 async_thread = new Thread (delegate (object state) {
1088                                         object [] args = (object []) state;
1089                                         WebRequest request = null;
1090                                         try {
1091                                                 request = SetupRequest ((Uri) args [0]);
1092                                                 WebResponse response = GetWebResponse (request);
1093                                                 Stream stream = response.GetResponseStream ();
1094                                                 OnOpenReadCompleted (
1095                                                         new OpenReadCompletedEventArgs (stream, null, false, args [1]));
1096                                         } catch (ThreadInterruptedException){
1097                                                 if (request != null)
1098                                                         request.Abort ();
1099                                                 
1100                                                 OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, null, true, args [1]));
1101                                         } catch (Exception e){
1102                                                 OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, e, false, args [1]));
1103                                         } });
1104                                 object [] cb_args = new object [] {address, userToken};
1105                                 async_thread.Start (cb_args);
1106                         }
1107                 }
1108
1109                 //    OpenWriteAsync
1110
1111                 public void OpenWriteAsync (Uri address)
1112                 {
1113                         OpenWriteAsync (address, null);
1114                 }
1115
1116                 public void OpenWriteAsync (Uri address, string method)
1117                 {
1118                         OpenWriteAsync (address, method, null);
1119                 }
1120
1121                 public void OpenWriteAsync (Uri address, string method, object userToken)
1122                 {
1123                         if (address == null)
1124                                 throw new ArgumentNullException ("address");
1125
1126                         lock (this) {
1127                                 SetBusy ();
1128                                 async = true;
1129
1130                                 async_thread = new Thread (delegate (object state) {
1131                                         object [] args = (object []) state;
1132                                         WebRequest request = null;
1133                                         try {
1134                                                 request = SetupRequest ((Uri) args [0], (string) args [1], true);
1135                                                 Stream stream = request.GetRequestStream ();
1136                                                 OnOpenWriteCompleted (
1137                                                         new OpenWriteCompletedEventArgs (stream, null, false, args [2]));
1138                                         } catch (ThreadInterruptedException){
1139                                                 if (request != null)
1140                                                         request.Abort ();
1141                                                 OnOpenWriteCompleted (
1142                                                         new OpenWriteCompletedEventArgs (null, null, true, args [2]));
1143                                         } catch (Exception e){
1144                                                 OnOpenWriteCompleted (
1145                                                         new OpenWriteCompletedEventArgs (null, e, false, args [2]));
1146                                         }});
1147                                 object [] cb_args = new object [] {address, method, userToken};
1148                                 async_thread.Start (cb_args);
1149                         }
1150                 }
1151
1152                 //    UploadDataAsync
1153
1154                 public void UploadDataAsync (Uri address, byte [] data)
1155                 {
1156                         UploadDataAsync (address, null, data);
1157                 }
1158
1159                 public void UploadDataAsync (Uri address, string method, byte [] data)
1160                 {
1161                         UploadDataAsync (address, method, data, null);
1162                 }
1163
1164                 public void UploadDataAsync (Uri address, string method, byte [] data, object userToken)
1165                 {
1166                         if (address == null)
1167                                 throw new ArgumentNullException ("address");
1168                         if (data == null)
1169                                 throw new ArgumentNullException ("data");
1170                         
1171                         lock (this) {
1172                                 SetBusy ();
1173                                 async = true;
1174
1175                                 async_thread = new Thread (delegate (object state) {
1176                                         object [] args = (object []) state;
1177                                         byte [] data2;
1178
1179                                         try {
1180                                                 data2 = UploadDataCore ((Uri) args [0], (string) args [1], (byte []) args [2], args [3]);
1181                                         
1182                                                 OnUploadDataCompleted (
1183                                                         new UploadDataCompletedEventArgs (data2, null, false, args [3]));
1184                                         } catch (ThreadInterruptedException){
1185                                                 OnUploadDataCompleted (
1186                                                         new UploadDataCompletedEventArgs (null, null, true, args [3]));
1187                                         } catch (Exception e){
1188                                                 OnUploadDataCompleted (
1189                                                         new UploadDataCompletedEventArgs (null, e, false, args [3]));
1190                                         }});
1191                                 object [] cb_args = new object [] {address, method, data,  userToken};
1192                                 async_thread.Start (cb_args);
1193                         }
1194                 }
1195
1196                 //    UploadFileAsync
1197
1198                 public void UploadFileAsync (Uri address, string fileName)
1199                 {
1200                         UploadFileAsync (address, null, fileName);
1201                 }
1202
1203                 public void UploadFileAsync (Uri address, string method, string fileName)
1204                 {
1205                         UploadFileAsync (address, method, fileName, null);
1206                 }
1207
1208                 public void UploadFileAsync (Uri address, string method, string fileName, object userToken)
1209                 {
1210                         if (address == null)
1211                                 throw new ArgumentNullException ("address");
1212                         if (fileName == null)
1213                                 throw new ArgumentNullException ("fileName");
1214
1215                         lock (this) {
1216                                 SetBusy ();
1217                                 async = true;
1218
1219                                 async_thread = new Thread (delegate (object state) {
1220                                         object [] args = (object []) state;
1221                                         byte [] data;
1222
1223                                         try {
1224                                                 data = UploadFileCore ((Uri) args [0], (string) args [1], (string) args [2], args [3]);
1225                                                 OnUploadFileCompleted (
1226                                                         new UploadFileCompletedEventArgs (data, null, false, args [3]));
1227                                         } catch (ThreadInterruptedException){
1228                                                 OnUploadFileCompleted (
1229                                                         new UploadFileCompletedEventArgs (null, null, true, args [3]));
1230                                         } catch (Exception e){
1231                                                 OnUploadFileCompleted (
1232                                                         new UploadFileCompletedEventArgs (null, e, false, args [3]));
1233                                         }});
1234                                 object [] cb_args = new object [] {address, method, fileName,  userToken};
1235                                 async_thread.Start (cb_args);
1236                         }
1237                 }
1238
1239                 //    UploadStringAsync
1240
1241                 public void UploadStringAsync (Uri address, string data)
1242                 {
1243                         UploadStringAsync (address, null, data);
1244                 }
1245
1246                 public void UploadStringAsync (Uri address, string method, string data)
1247                 {
1248                         UploadStringAsync (address, method, data, null);
1249                 }
1250
1251                 public void UploadStringAsync (Uri address, string method, string data, object userToken)
1252                 {
1253                         if (address == null)
1254                                 throw new ArgumentNullException ("address");
1255                         if (data == null)
1256                                 throw new ArgumentNullException ("data");
1257                         
1258                         lock (this) {
1259                                 CheckBusy ();
1260                                 async = true;
1261                                 
1262                                 async_thread = new Thread (delegate (object state) {
1263                                         object [] args = (object []) state;
1264
1265                                         try {
1266                                                 string data2 = UploadString ((Uri) args [0], (string) args [1], (string) args [2]);
1267                                                 OnUploadStringCompleted (
1268                                                         new UploadStringCompletedEventArgs (data2, null, false, args [3]));
1269                                         } catch (ThreadInterruptedException){
1270                                                 OnUploadStringCompleted (
1271                                                         new UploadStringCompletedEventArgs (null, null, true, args [3]));
1272                                         } catch (Exception e){
1273                                                 OnUploadStringCompleted (
1274                                                         new UploadStringCompletedEventArgs (null, e, false, args [3]));
1275                                         }});
1276                                 object [] cb_args = new object [] {address, method, data, userToken};
1277                                 async_thread.Start (cb_args);
1278                         }
1279                 }
1280
1281                 //    UploadValuesAsync
1282
1283                 public void UploadValuesAsync (Uri address, NameValueCollection values)
1284                 {
1285                         UploadValuesAsync (address, null, values);
1286                 }
1287
1288                 public void UploadValuesAsync (Uri address, string method, NameValueCollection values)
1289                 {
1290                         UploadValuesAsync (address, method, values, null);
1291                 }
1292
1293                 public void UploadValuesAsync (Uri address, string method, NameValueCollection values, object userToken)
1294                 {
1295                         if (address == null)
1296                                 throw new ArgumentNullException ("address");
1297                         if (values == null)
1298                                 throw new ArgumentNullException ("values");
1299
1300                         lock (this) {
1301                                 CheckBusy ();
1302                                 async = true;
1303
1304                                 async_thread = new Thread (delegate (object state) {
1305                                         object [] args = (object []) state;
1306                                         try {
1307                                                 byte [] data = UploadValuesCore ((Uri) args [0], (string) args [1], (NameValueCollection) args [2], args [3]);
1308                                                 OnUploadValuesCompleted (
1309                                                         new UploadValuesCompletedEventArgs (data, null, false, args [3]));
1310                                         } catch (ThreadInterruptedException){
1311                                                 OnUploadValuesCompleted (
1312                                                         new UploadValuesCompletedEventArgs (null, null, true, args [3]));
1313                                         } catch (Exception e){
1314                                                 OnUploadValuesCompleted (
1315                                                         new UploadValuesCompletedEventArgs (null, e, false, args [3]));
1316                                         }});
1317                                 object [] cb_args = new object [] {address, method, values,  userToken};
1318                                 async_thread.Start (cb_args);
1319                         }
1320                 }
1321
1322                 protected virtual void OnDownloadDataCompleted (DownloadDataCompletedEventArgs args)
1323                 {
1324                         CompleteAsync ();
1325                         if (DownloadDataCompleted != null)
1326                                 DownloadDataCompleted (this, args);
1327                 }
1328
1329                 protected virtual void OnDownloadFileCompleted (AsyncCompletedEventArgs args)
1330                 {
1331                         CompleteAsync ();
1332                         if (DownloadFileCompleted != null)
1333                                 DownloadFileCompleted (this, args);
1334                 }
1335
1336                 protected virtual void OnDownloadProgressChanged (DownloadProgressChangedEventArgs e)
1337                 {
1338                         if (DownloadProgressChanged != null)
1339                                 DownloadProgressChanged (this, e);
1340                 }
1341
1342                 protected virtual void OnDownloadStringCompleted (DownloadStringCompletedEventArgs args)
1343                 {
1344                         CompleteAsync ();
1345                         if (DownloadStringCompleted != null)
1346                                 DownloadStringCompleted (this, args);
1347                 }
1348
1349                 protected virtual void OnOpenReadCompleted (OpenReadCompletedEventArgs args)
1350                 {
1351                         CompleteAsync ();
1352                         if (OpenReadCompleted != null)
1353                                 OpenReadCompleted (this, args);
1354                 }
1355
1356                 protected virtual void OnOpenWriteCompleted (OpenWriteCompletedEventArgs args)
1357                 {
1358                         CompleteAsync ();
1359                         if (OpenWriteCompleted != null)
1360                                 OpenWriteCompleted (this, args);
1361                 }
1362
1363                 protected virtual void OnUploadDataCompleted (UploadDataCompletedEventArgs args)
1364                 {
1365                         CompleteAsync ();
1366                         if (UploadDataCompleted != null)
1367                                 UploadDataCompleted (this, args);
1368                 }
1369
1370                 protected virtual void OnUploadFileCompleted (UploadFileCompletedEventArgs args)
1371                 {
1372                         CompleteAsync ();
1373                         if (UploadFileCompleted != null)
1374                                 UploadFileCompleted (this, args);
1375                 }
1376
1377                 protected virtual void OnUploadProgressChanged (UploadProgressChangedEventArgs e)
1378                 {
1379                         if (UploadProgressChanged != null)
1380                                 UploadProgressChanged (this, e);
1381                 }
1382
1383                 protected virtual void OnUploadStringCompleted (UploadStringCompletedEventArgs args)
1384                 {
1385                         CompleteAsync ();
1386                         if (UploadStringCompleted != null)
1387                                 UploadStringCompleted (this, args);
1388                 }
1389
1390                 protected virtual void OnUploadValuesCompleted (UploadValuesCompletedEventArgs args)
1391                 {
1392                         CompleteAsync ();
1393                         if (UploadValuesCompleted != null)
1394                                 UploadValuesCompleted (this, args);
1395                 }
1396
1397                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
1398                 {
1399                         WebResponse response = request.EndGetResponse (result);
1400                         responseHeaders = response.Headers;
1401                         return response;
1402                 }
1403
1404                 protected virtual WebRequest GetWebRequest (Uri address)
1405                 {
1406                         return WebRequest.Create (address);
1407                 }
1408
1409                 protected virtual WebResponse GetWebResponse (WebRequest request)
1410                 {
1411                         WebResponse response = request.GetResponse ();
1412                         responseHeaders = response.Headers;
1413                         return response;
1414                 }
1415
1416         }
1417 }
1418