Normalize line endings.
[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 [] realBoundary = Encoding.ASCII.GetBytes ("--" + boundary + "\r\n");
537                                 reqStream.Write (realBoundary, 0, realBoundary.Length);
538                                 string partHeaders = String.Format ("Content-Disposition: form-data; " +
539                                                                     "name=\"file\"; filename=\"{0}\"\r\n" +
540                                                                     "Content-Type: {1}\r\n\r\n",
541                                                                     Path.GetFileName (fileName), fileCType);
542
543                                 byte [] partHeadersBytes = Encoding.UTF8.GetBytes (partHeaders);
544                                 reqStream.Write (partHeadersBytes, 0, partHeadersBytes.Length);
545                                 int nread;
546                                 byte [] buffer = new byte [4096];
547                                 while ((nread = fStream.Read (buffer, 0, 4096)) != 0)
548                                         reqStream.Write (buffer, 0, nread);
549
550                                 reqStream.WriteByte ((byte) '\r');
551                                 reqStream.WriteByte ((byte) '\n');
552                                 reqStream.Write (realBoundary, 0, realBoundary.Length);
553                                 reqStream.Close ();
554                                 reqStream = null;
555                                 resultBytes = ReadAll (request, userToken);
556                         } catch (ThreadInterruptedException){
557                                 if (request != null)
558                                         request.Abort ();
559                                 throw;
560                         } finally {
561                                 if (fStream != null)
562                                         fStream.Close ();
563
564                                 if (reqStream != null)
565                                         reqStream.Close ();
566                         }
567                         
568                         return resultBytes;
569                 }
570                 
571                 public byte[] UploadValues (string address, NameValueCollection data)
572                 {
573                         if (address == null)
574                                 throw new ArgumentNullException ("address");
575
576                         return UploadValues (CreateUri (address), data);
577                 }
578                 
579                 public byte[] UploadValues (string address, string method, NameValueCollection data)
580                 {
581                         if (address == null)
582                                 throw new ArgumentNullException ("address");
583                         return UploadValues (CreateUri (address), method, data);
584                 }
585
586                 public byte[] UploadValues (Uri address, NameValueCollection data)
587                 {
588                         return UploadValues (address, (string) null, data);
589                 }
590
591                 public byte[] UploadValues (Uri address, string method, NameValueCollection data)
592                 {
593                         if (address == null)
594                                 throw new ArgumentNullException ("address");
595                         if (data == null)
596                                 throw new ArgumentNullException ("data");
597
598                         try {
599                                 SetBusy ();
600                                 async = false;
601                                 return UploadValuesCore (address, method, data, null);
602                         } catch (WebException) {
603                                 throw;
604                         } catch (Exception ex) {
605                                 throw new WebException ("An error occurred " +
606                                         "performing a WebClient request.", ex);
607                         } finally {
608                                 is_busy = false;
609                         }
610                 }
611
612                 byte[] UploadValuesCore (Uri uri, string method, NameValueCollection data, object userToken)
613                 {
614                         string cType = Headers ["Content-Type"];
615                         if (cType != null && String.Compare (cType, urlEncodedCType, true) != 0)
616                                 throw new WebException ("Content-Type header cannot be changed from its default " +
617                                                         "value for this request.");
618
619                         Headers ["Content-Type"] = urlEncodedCType;
620                         WebRequest request = SetupRequest (uri, method, true);
621                         try {
622                                 MemoryStream tmpStream = new MemoryStream ();
623                                 foreach (string key in data) {
624                                         byte [] bytes = Encoding.UTF8.GetBytes (key);
625                                         UrlEncodeAndWrite (tmpStream, bytes);
626                                         tmpStream.WriteByte ((byte) '=');
627                                         bytes = Encoding.UTF8.GetBytes (data [key]);
628                                         UrlEncodeAndWrite (tmpStream, bytes);
629                                         tmpStream.WriteByte ((byte) '&');
630                                 }
631                                 
632                                 int length = (int) tmpStream.Length;
633                                 if (length > 0)
634                                         tmpStream.SetLength (--length); // remove trailing '&'
635                                 
636                                 byte [] buf = tmpStream.GetBuffer ();
637                                 request.ContentLength = length;
638                                 using (Stream rqStream = request.GetRequestStream ()) {
639                                         rqStream.Write (buf, 0, length);
640                                 }
641                                 tmpStream.Close ();
642                                 
643                                 return ReadAll (request, userToken);
644                         } catch (ThreadInterruptedException) {
645                                 request.Abort ();
646                                 throw;
647                         }
648                 }
649
650                 public string DownloadString (string address)
651                 {
652                         if (address == null)
653                                 throw new ArgumentNullException ("address");
654
655                         return encoding.GetString (DownloadData (CreateUri (address)));
656                 }
657
658                 public string DownloadString (Uri address)
659                 {
660                         if (address == null)
661                                 throw new ArgumentNullException ("address");
662
663                         return encoding.GetString (DownloadData (CreateUri (address)));
664                 }
665
666                 public string UploadString (string address, string data)
667                 {
668                         if (address == null)
669                                 throw new ArgumentNullException ("address");
670                         if (data == null)
671                                 throw new ArgumentNullException ("data");
672
673                         byte [] resp = UploadData (address, encoding.GetBytes (data));
674                         return encoding.GetString (resp);
675                 }
676
677                 public string UploadString (string address, string method, string data)
678                 {
679                         if (address == null)
680                                 throw new ArgumentNullException ("address");
681                         if (data == null)
682                                 throw new ArgumentNullException ("data");
683
684                         byte [] resp = UploadData (address, method, encoding.GetBytes (data));
685                         return encoding.GetString (resp);
686                 }
687
688                 public string UploadString (Uri address, string data)
689                 {
690                         if (address == null)
691                                 throw new ArgumentNullException ("address");
692                         if (data == null)
693                                 throw new ArgumentNullException ("data");
694
695                         byte [] resp = UploadData (address, encoding.GetBytes (data));
696                         return encoding.GetString (resp);
697                 }
698
699                 public string UploadString (Uri address, string method, string data)
700                 {
701                         if (address == null)
702                                 throw new ArgumentNullException ("address");
703                         if (data == null)
704                                 throw new ArgumentNullException ("data");
705
706                         byte [] resp = UploadData (address, method, encoding.GetBytes (data));
707                         return encoding.GetString (resp);
708                 }
709
710                 public event DownloadDataCompletedEventHandler DownloadDataCompleted;
711                 public event AsyncCompletedEventHandler DownloadFileCompleted;
712                 public event DownloadProgressChangedEventHandler DownloadProgressChanged;
713                 public event DownloadStringCompletedEventHandler DownloadStringCompleted;
714                 public event OpenReadCompletedEventHandler OpenReadCompleted;
715                 public event OpenWriteCompletedEventHandler OpenWriteCompleted;
716                 public event UploadDataCompletedEventHandler UploadDataCompleted;
717                 public event UploadFileCompletedEventHandler UploadFileCompleted;
718                 public event UploadProgressChangedEventHandler UploadProgressChanged;
719                 public event UploadStringCompletedEventHandler UploadStringCompleted;
720                 public event UploadValuesCompletedEventHandler UploadValuesCompleted;
721
722                 Uri CreateUri (string address)
723                 {
724                         Uri uri;
725                         try {
726                                 if (baseAddress == null)
727                                         uri = new Uri (address);
728                                 else
729                                         uri = new Uri (baseAddress, address);
730                                 return CreateUri (uri);
731                         } catch {
732                         }
733                         return new Uri (Path.GetFullPath (address));
734                 }
735
736                 Uri CreateUri (Uri address)
737                 {
738                         Uri result = address;
739                         if (baseAddress != null && !result.IsAbsoluteUri) {
740                                 try {
741                                         result = new Uri (baseAddress, result.OriginalString);
742                                 } catch {
743                                         return result; // Not much we can do here.
744                                 }
745                         }
746
747                         string query = result.Query;
748                         if (String.IsNullOrEmpty (query))
749                                 query = GetQueryString (true);
750                         UriBuilder builder = new UriBuilder (address);
751                         if (!String.IsNullOrEmpty (query))
752                                 builder.Query = query.Substring (1);
753                         return builder.Uri;
754                 }
755
756                 string GetQueryString (bool add_qmark)
757                 {
758                         if (queryString == null || queryString.Count == 0)
759                                 return null;
760
761                         StringBuilder sb = new StringBuilder ();
762                         if (add_qmark)
763                                 sb.Append ('?');
764
765                         foreach (string key in queryString)
766                                 sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
767
768                         if (sb.Length != 0)
769                                 sb.Length--; // removes last '&' or the '?' if empty.
770
771                         if (sb.Length == 0)
772                                 return null;
773
774                         return sb.ToString ();
775                 }
776
777                 WebRequest SetupRequest (Uri uri)
778                 {
779                         WebRequest request = GetWebRequest (uri);
780                         if (Proxy != null)
781                                 request.Proxy = Proxy;
782                         request.Credentials = credentials;
783
784                         // Special headers. These are properties of HttpWebRequest.
785                         // What do we do with other requests differnt from HttpWebRequest?
786                         if (headers != null && headers.Count != 0 && (request is HttpWebRequest)) {
787                                 HttpWebRequest req = (HttpWebRequest) request;
788                                 string expect = headers ["Expect"];
789                                 string contentType = headers ["Content-Type"];
790                                 string accept = headers ["Accept"];
791                                 string connection = headers ["Connection"];
792                                 string userAgent = headers ["User-Agent"];
793                                 string referer = headers ["Referer"];
794                                 headers.RemoveInternal ("Expect");
795                                 headers.RemoveInternal ("Content-Type");
796                                 headers.RemoveInternal ("Accept");
797                                 headers.RemoveInternal ("Connection");
798                                 headers.RemoveInternal ("Referer");
799                                 headers.RemoveInternal ("User-Agent");
800                                 request.Headers = headers;
801
802                                 if (expect != null && expect.Length > 0)
803                                         req.Expect = expect;
804
805                                 if (accept != null && accept.Length > 0)
806                                         req.Accept = accept;
807
808                                 if (contentType != null && contentType.Length > 0)
809                                         req.ContentType = contentType;
810
811                                 if (connection != null && connection.Length > 0)
812                                         req.Connection = connection;
813
814                                 if (userAgent != null && userAgent.Length > 0)
815                                         req.UserAgent = userAgent;
816
817                                 if (referer != null && referer.Length > 0)
818                                         req.Referer = referer;
819                         }
820
821                         responseHeaders = null;
822                         return request;
823                 }
824
825                 WebRequest SetupRequest (Uri uri, string method, bool is_upload)
826                 {
827                         WebRequest request = SetupRequest (uri);
828                         request.Method = DetermineMethod (uri, method, is_upload);
829                         return request;
830                 }
831
832                 byte [] ReadAll (WebRequest request, object userToken)
833                 {
834                         WebResponse response = GetWebResponse (request);
835                         Stream stream = response.GetResponseStream ();
836                         int length = (int) response.ContentLength;
837                         HttpWebRequest wreq = request as HttpWebRequest;
838
839                         if (length > -1 && wreq != null && (int) wreq.AutomaticDecompression != 0) {
840                                 string content_encoding = ((HttpWebResponse) response).ContentEncoding;
841                                 if (((content_encoding == "gzip" && (wreq.AutomaticDecompression & DecompressionMethods.GZip) != 0)) ||
842                                         ((content_encoding == "deflate" && (wreq.AutomaticDecompression & DecompressionMethods.Deflate) != 0)))
843                                         length = -1;
844                         }
845
846                         MemoryStream ms = null;
847                         bool nolength = (length == -1);
848                         int size = ((nolength) ? 8192 : length);
849                         if (nolength)
850                                 ms = new MemoryStream ();
851
852 //                      long total = 0;
853                         int nread = 0;
854                         int offset = 0;
855                         byte [] buffer = new byte [size];
856                         while ((nread = stream.Read (buffer, offset, size)) != 0) {
857                                 if (nolength) {
858                                         ms.Write (buffer, 0, nread);
859                                 } else {
860                                         offset += nread;
861                                         size -= nread;
862                                 }
863                                 if (async){
864 //                                      total += nread;
865                                         OnDownloadProgressChanged (new DownloadProgressChangedEventArgs (nread, length, userToken));
866                                 }
867                         }
868
869                         if (nolength)
870                                 return ms.ToArray ();
871
872                         return buffer;
873                 }
874
875                 string UrlEncode (string str)
876                 {
877                         StringBuilder result = new StringBuilder ();
878
879                         int len = str.Length;
880                         for (int i = 0; i < len; i++) {
881                                 char c = str [i];
882                                 if (c == ' ')
883                                         result.Append ('+');
884                                 else if ((c < '0' && c != '-' && c != '.') ||
885                                          (c < 'A' && c > '9') ||
886                                          (c > 'Z' && c < 'a' && c != '_') ||
887                                          (c > 'z')) {
888                                         result.Append ('%');
889                                         int idx = ((int) c) >> 4;
890                                         result.Append ((char) hexBytes [idx]);
891                                         idx = ((int) c) & 0x0F;
892                                         result.Append ((char) hexBytes [idx]);
893                                 } else {
894                                         result.Append (c);
895                                 }
896                         }
897
898                         return result.ToString ();
899                 }
900
901                 static void UrlEncodeAndWrite (Stream stream, byte [] bytes)
902                 {
903                         if (bytes == null)
904                                 return;
905
906                         int len = bytes.Length;
907                         if (len == 0)
908                                 return;
909
910                         for (int i = 0; i < len; i++) {
911                                 char c = (char) bytes [i];
912                                 if (c == ' ')
913                                         stream.WriteByte ((byte) '+');
914                                 else if ((c < '0' && c != '-' && c != '.') ||
915                                          (c < 'A' && c > '9') ||
916                                          (c > 'Z' && c < 'a' && c != '_') ||
917                                          (c > 'z')) {
918                                         stream.WriteByte ((byte) '%');
919                                         int idx = ((int) c) >> 4;
920                                         stream.WriteByte (hexBytes [idx]);
921                                         idx = ((int) c) & 0x0F;
922                                         stream.WriteByte (hexBytes [idx]);
923                                 } else {
924                                         stream.WriteByte ((byte) c);
925                                 }
926                         }
927                 }
928
929                 public void CancelAsync ()
930                 {
931                         lock (this){
932                                 if (async_thread == null)
933                                         return;
934
935                                 //
936                                 // We first flag things as done, in case the Interrupt hangs
937                                 // or the thread decides to hang in some other way inside the
938                                 // event handlers, or if we are stuck somewhere else.  This
939                                 // ensures that the WebClient object is reusable immediately
940                                 //
941                                 Thread t = async_thread;
942                                 CompleteAsync ();
943                                 t.Interrupt ();
944                         }
945                 }
946
947                 void CompleteAsync ()
948                 {
949                         lock (this){
950                                 is_busy = false;
951                                 async_thread = null;
952                         }
953                 }
954
955                 //    DownloadDataAsync
956
957                 public void DownloadDataAsync (Uri address)
958                 {
959                         DownloadDataAsync (address, null);
960                 }
961
962                 public void DownloadDataAsync (Uri address, object userToken)
963                 {
964                         if (address == null)
965                                 throw new ArgumentNullException ("address");
966                         
967                         lock (this) {
968                                 SetBusy ();
969                                 async = true;
970                                 
971                                 async_thread = new Thread (delegate (object state) {
972                                         object [] args = (object []) state;
973                                         try {
974                                                 byte [] data = DownloadDataCore ((Uri) args [0], args [1]);
975                                                 OnDownloadDataCompleted (
976                                                         new DownloadDataCompletedEventArgs (data, null, false, args [1]));
977                                         } catch (ThreadInterruptedException){
978                                                 OnDownloadDataCompleted (
979                                                         new DownloadDataCompletedEventArgs (null, null, true, args [1]));
980                                                 throw;
981                                         } catch (Exception e){
982                                                 OnDownloadDataCompleted (
983                                                         new DownloadDataCompletedEventArgs (null, e, false, args [1]));
984                                         }
985                                 });
986                                 object [] cb_args = new object [] {address, userToken};
987                                 async_thread.Start (cb_args);
988                         }
989                 }
990
991                 //    DownloadFileAsync
992
993                 public void DownloadFileAsync (Uri address, string fileName)
994                 {
995                         DownloadFileAsync (address, fileName, null);
996                 }
997
998                 public void DownloadFileAsync (Uri address, string fileName, object userToken)
999                 {
1000                         if (address == null)
1001                                 throw new ArgumentNullException ("address");
1002                         if (fileName == null)
1003                                 throw new ArgumentNullException ("fileName");
1004                         
1005                         lock (this) {
1006                                 SetBusy ();
1007                                 async = true;
1008
1009                                 async_thread = new Thread (delegate (object state) {
1010                                         object [] args = (object []) state;
1011                                         try {
1012                                                 DownloadFileCore ((Uri) args [0], (string) args [1], args [2]);
1013                                                 OnDownloadFileCompleted (
1014                                                         new AsyncCompletedEventArgs (null, false, args [2]));
1015                                         } catch (ThreadInterruptedException){
1016                                                 OnDownloadFileCompleted (
1017                                                         new AsyncCompletedEventArgs (null, true, args [2]));
1018                                         } catch (Exception e){
1019                                                 OnDownloadFileCompleted (
1020                                                         new AsyncCompletedEventArgs (e, false, args [2]));
1021                                         }});
1022                                 object [] cb_args = new object [] {address, fileName, userToken};
1023                                 async_thread.Start (cb_args);
1024                         }
1025                 }
1026
1027                 //    DownloadStringAsync
1028
1029                 public void DownloadStringAsync (Uri address)
1030                 {
1031                         DownloadStringAsync (address, null);
1032                 }
1033
1034                 public void DownloadStringAsync (Uri address, object userToken)
1035                 {
1036                         if (address == null)
1037                                 throw new ArgumentNullException ("address");
1038                         
1039                         lock (this) {
1040                                 SetBusy ();
1041                                 async = true;
1042
1043                                 async_thread = new Thread (delegate (object state) {
1044                                         object [] args = (object []) state;
1045                                         try {
1046                                                 string data = encoding.GetString (DownloadDataCore ((Uri) args [0], args [1]));
1047                                                 OnDownloadStringCompleted (
1048                                                         new DownloadStringCompletedEventArgs (data, null, false, args [1]));
1049                                         } catch (ThreadInterruptedException){
1050                                                 OnDownloadStringCompleted (
1051                                                         new DownloadStringCompletedEventArgs (null, null, true, args [1]));
1052                                         } catch (Exception e){
1053                                                 OnDownloadStringCompleted (
1054                                                         new DownloadStringCompletedEventArgs (null, e, false, args [1]));
1055                                         }});
1056                                 object [] cb_args = new object [] {address, userToken};
1057                                 async_thread.Start (cb_args);
1058                         }
1059                 }
1060
1061                 //    OpenReadAsync
1062
1063                 public void OpenReadAsync (Uri address)
1064                 {
1065                         OpenReadAsync (address, null);
1066                 }
1067
1068                 public void OpenReadAsync (Uri address, object userToken)
1069                 {
1070                         if (address == null)
1071                                 throw new ArgumentNullException ("address");
1072                         
1073                         lock (this) {
1074                                 SetBusy ();
1075                                 async = true;
1076
1077                                 async_thread = new Thread (delegate (object state) {
1078                                         object [] args = (object []) state;
1079                                         WebRequest request = null;
1080                                         try {
1081                                                 request = SetupRequest ((Uri) args [0]);
1082                                                 WebResponse response = GetWebResponse (request);
1083                                                 Stream stream = response.GetResponseStream ();
1084                                                 OnOpenReadCompleted (
1085                                                         new OpenReadCompletedEventArgs (stream, null, false, args [1]));
1086                                         } catch (ThreadInterruptedException){
1087                                                 if (request != null)
1088                                                         request.Abort ();
1089                                                 
1090                                                 OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, null, true, args [1]));
1091                                         } catch (Exception e){
1092                                                 OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, e, false, args [1]));
1093                                         } });
1094                                 object [] cb_args = new object [] {address, userToken};
1095                                 async_thread.Start (cb_args);
1096                         }
1097                 }
1098
1099                 //    OpenWriteAsync
1100
1101                 public void OpenWriteAsync (Uri address)
1102                 {
1103                         OpenWriteAsync (address, null);
1104                 }
1105
1106                 public void OpenWriteAsync (Uri address, string method)
1107                 {
1108                         OpenWriteAsync (address, method, null);
1109                 }
1110
1111                 public void OpenWriteAsync (Uri address, string method, object userToken)
1112                 {
1113                         if (address == null)
1114                                 throw new ArgumentNullException ("address");
1115
1116                         lock (this) {
1117                                 SetBusy ();
1118                                 async = true;
1119
1120                                 async_thread = new Thread (delegate (object state) {
1121                                         object [] args = (object []) state;
1122                                         WebRequest request = null;
1123                                         try {
1124                                                 request = SetupRequest ((Uri) args [0], (string) args [1], true);
1125                                                 Stream stream = request.GetRequestStream ();
1126                                                 OnOpenWriteCompleted (
1127                                                         new OpenWriteCompletedEventArgs (stream, null, false, args [2]));
1128                                         } catch (ThreadInterruptedException){
1129                                                 if (request != null)
1130                                                         request.Abort ();
1131                                                 OnOpenWriteCompleted (
1132                                                         new OpenWriteCompletedEventArgs (null, null, true, args [2]));
1133                                         } catch (Exception e){
1134                                                 OnOpenWriteCompleted (
1135                                                         new OpenWriteCompletedEventArgs (null, e, false, args [2]));
1136                                         }});
1137                                 object [] cb_args = new object [] {address, method, userToken};
1138                                 async_thread.Start (cb_args);
1139                         }
1140                 }
1141
1142                 //    UploadDataAsync
1143
1144                 public void UploadDataAsync (Uri address, byte [] data)
1145                 {
1146                         UploadDataAsync (address, null, data);
1147                 }
1148
1149                 public void UploadDataAsync (Uri address, string method, byte [] data)
1150                 {
1151                         UploadDataAsync (address, method, data, null);
1152                 }
1153
1154                 public void UploadDataAsync (Uri address, string method, byte [] data, object userToken)
1155                 {
1156                         if (address == null)
1157                                 throw new ArgumentNullException ("address");
1158                         if (data == null)
1159                                 throw new ArgumentNullException ("data");
1160                         
1161                         lock (this) {
1162                                 SetBusy ();
1163                                 async = true;
1164
1165                                 async_thread = new Thread (delegate (object state) {
1166                                         object [] args = (object []) state;
1167                                         byte [] data2;
1168
1169                                         try {
1170                                                 data2 = UploadDataCore ((Uri) args [0], (string) args [1], (byte []) args [2], args [3]);
1171                                         
1172                                                 OnUploadDataCompleted (
1173                                                         new UploadDataCompletedEventArgs (data2, null, false, args [3]));
1174                                         } catch (ThreadInterruptedException){
1175                                                 OnUploadDataCompleted (
1176                                                         new UploadDataCompletedEventArgs (null, null, true, args [3]));
1177                                         } catch (Exception e){
1178                                                 OnUploadDataCompleted (
1179                                                         new UploadDataCompletedEventArgs (null, e, false, args [3]));
1180                                         }});
1181                                 object [] cb_args = new object [] {address, method, data,  userToken};
1182                                 async_thread.Start (cb_args);
1183                         }
1184                 }
1185
1186                 //    UploadFileAsync
1187
1188                 public void UploadFileAsync (Uri address, string fileName)
1189                 {
1190                         UploadFileAsync (address, null, fileName);
1191                 }
1192
1193                 public void UploadFileAsync (Uri address, string method, string fileName)
1194                 {
1195                         UploadFileAsync (address, method, fileName, null);
1196                 }
1197
1198                 public void UploadFileAsync (Uri address, string method, string fileName, object userToken)
1199                 {
1200                         if (address == null)
1201                                 throw new ArgumentNullException ("address");
1202                         if (fileName == null)
1203                                 throw new ArgumentNullException ("fileName");
1204
1205                         lock (this) {
1206                                 SetBusy ();
1207                                 async = true;
1208
1209                                 async_thread = new Thread (delegate (object state) {
1210                                         object [] args = (object []) state;
1211                                         byte [] data;
1212
1213                                         try {
1214                                                 data = UploadFileCore ((Uri) args [0], (string) args [1], (string) args [2], args [3]);
1215                                                 OnUploadFileCompleted (
1216                                                         new UploadFileCompletedEventArgs (data, null, false, args [3]));
1217                                         } catch (ThreadInterruptedException){
1218                                                 OnUploadFileCompleted (
1219                                                         new UploadFileCompletedEventArgs (null, null, true, args [3]));
1220                                         } catch (Exception e){
1221                                                 OnUploadFileCompleted (
1222                                                         new UploadFileCompletedEventArgs (null, e, false, args [3]));
1223                                         }});
1224                                 object [] cb_args = new object [] {address, method, fileName,  userToken};
1225                                 async_thread.Start (cb_args);
1226                         }
1227                 }
1228
1229                 //    UploadStringAsync
1230
1231                 public void UploadStringAsync (Uri address, string data)
1232                 {
1233                         UploadStringAsync (address, null, data);
1234                 }
1235
1236                 public void UploadStringAsync (Uri address, string method, string data)
1237                 {
1238                         UploadStringAsync (address, method, data, null);
1239                 }
1240
1241                 public void UploadStringAsync (Uri address, string method, string data, object userToken)
1242                 {
1243                         if (address == null)
1244                                 throw new ArgumentNullException ("address");
1245                         if (data == null)
1246                                 throw new ArgumentNullException ("data");
1247                         
1248                         lock (this) {
1249                                 CheckBusy ();
1250                                 async = true;
1251                                 
1252                                 async_thread = new Thread (delegate (object state) {
1253                                         object [] args = (object []) state;
1254
1255                                         try {
1256                                                 string data2 = UploadString ((Uri) args [0], (string) args [1], (string) args [2]);
1257                                                 OnUploadStringCompleted (
1258                                                         new UploadStringCompletedEventArgs (data2, null, false, args [3]));
1259                                         } catch (ThreadInterruptedException){
1260                                                 OnUploadStringCompleted (
1261                                                         new UploadStringCompletedEventArgs (null, null, true, args [3]));
1262                                         } catch (Exception e){
1263                                                 OnUploadStringCompleted (
1264                                                         new UploadStringCompletedEventArgs (null, e, false, args [3]));
1265                                         }});
1266                                 object [] cb_args = new object [] {address, method, data, userToken};
1267                                 async_thread.Start (cb_args);
1268                         }
1269                 }
1270
1271                 //    UploadValuesAsync
1272
1273                 public void UploadValuesAsync (Uri address, NameValueCollection values)
1274                 {
1275                         UploadValuesAsync (address, null, values);
1276                 }
1277
1278                 public void UploadValuesAsync (Uri address, string method, NameValueCollection values)
1279                 {
1280                         UploadValuesAsync (address, method, values, null);
1281                 }
1282
1283                 public void UploadValuesAsync (Uri address, string method, NameValueCollection values, object userToken)
1284                 {
1285                         if (address == null)
1286                                 throw new ArgumentNullException ("address");
1287                         if (values == null)
1288                                 throw new ArgumentNullException ("values");
1289
1290                         lock (this) {
1291                                 CheckBusy ();
1292                                 async = true;
1293
1294                                 async_thread = new Thread (delegate (object state) {
1295                                         object [] args = (object []) state;
1296                                         try {
1297                                                 byte [] data = UploadValuesCore ((Uri) args [0], (string) args [1], (NameValueCollection) args [2], args [3]);
1298                                                 OnUploadValuesCompleted (
1299                                                         new UploadValuesCompletedEventArgs (data, null, false, args [3]));
1300                                         } catch (ThreadInterruptedException){
1301                                                 OnUploadValuesCompleted (
1302                                                         new UploadValuesCompletedEventArgs (null, null, true, args [3]));
1303                                         } catch (Exception e){
1304                                                 OnUploadValuesCompleted (
1305                                                         new UploadValuesCompletedEventArgs (null, e, false, args [3]));
1306                                         }});
1307                                 object [] cb_args = new object [] {address, method, values,  userToken};
1308                                 async_thread.Start (cb_args);
1309                         }
1310                 }
1311
1312                 protected virtual void OnDownloadDataCompleted (DownloadDataCompletedEventArgs args)
1313                 {
1314                         CompleteAsync ();
1315                         if (DownloadDataCompleted != null)
1316                                 DownloadDataCompleted (this, args);
1317                 }
1318
1319                 protected virtual void OnDownloadFileCompleted (AsyncCompletedEventArgs args)
1320                 {
1321                         CompleteAsync ();
1322                         if (DownloadFileCompleted != null)
1323                                 DownloadFileCompleted (this, args);
1324                 }
1325
1326                 protected virtual void OnDownloadProgressChanged (DownloadProgressChangedEventArgs e)
1327                 {
1328                         if (DownloadProgressChanged != null)
1329                                 DownloadProgressChanged (this, e);
1330                 }
1331
1332                 protected virtual void OnDownloadStringCompleted (DownloadStringCompletedEventArgs args)
1333                 {
1334                         CompleteAsync ();
1335                         if (DownloadStringCompleted != null)
1336                                 DownloadStringCompleted (this, args);
1337                 }
1338
1339                 protected virtual void OnOpenReadCompleted (OpenReadCompletedEventArgs args)
1340                 {
1341                         CompleteAsync ();
1342                         if (OpenReadCompleted != null)
1343                                 OpenReadCompleted (this, args);
1344                 }
1345
1346                 protected virtual void OnOpenWriteCompleted (OpenWriteCompletedEventArgs args)
1347                 {
1348                         CompleteAsync ();
1349                         if (OpenWriteCompleted != null)
1350                                 OpenWriteCompleted (this, args);
1351                 }
1352
1353                 protected virtual void OnUploadDataCompleted (UploadDataCompletedEventArgs args)
1354                 {
1355                         CompleteAsync ();
1356                         if (UploadDataCompleted != null)
1357                                 UploadDataCompleted (this, args);
1358                 }
1359
1360                 protected virtual void OnUploadFileCompleted (UploadFileCompletedEventArgs args)
1361                 {
1362                         CompleteAsync ();
1363                         if (UploadFileCompleted != null)
1364                                 UploadFileCompleted (this, args);
1365                 }
1366
1367                 protected virtual void OnUploadProgressChanged (UploadProgressChangedEventArgs e)
1368                 {
1369                         if (UploadProgressChanged != null)
1370                                 UploadProgressChanged (this, e);
1371                 }
1372
1373                 protected virtual void OnUploadStringCompleted (UploadStringCompletedEventArgs args)
1374                 {
1375                         CompleteAsync ();
1376                         if (UploadStringCompleted != null)
1377                                 UploadStringCompleted (this, args);
1378                 }
1379
1380                 protected virtual void OnUploadValuesCompleted (UploadValuesCompletedEventArgs args)
1381                 {
1382                         CompleteAsync ();
1383                         if (UploadValuesCompleted != null)
1384                                 UploadValuesCompleted (this, args);
1385                 }
1386
1387                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
1388                 {
1389                         WebResponse response = request.EndGetResponse (result);
1390                         responseHeaders = response.Headers;
1391                         return response;
1392                 }
1393
1394                 protected virtual WebRequest GetWebRequest (Uri address)
1395                 {
1396                         return WebRequest.Create (address);
1397                 }
1398
1399                 protected virtual WebResponse GetWebResponse (WebRequest request)
1400                 {
1401                         WebResponse response = request.GetResponse ();
1402                         responseHeaders = response.Headers;
1403                         return response;
1404                 }
1405
1406         }
1407 }
1408