Merge pull request #954 from ermshiperete/bug-xamarin-8907
[mono.git] / mcs / class / System / System.Net / HttpWebRequest.cs
1 //
2 // System.Net.HttpWebRequest
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
11 //
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 using System;
35 using System.Collections;
36 using System.Configuration;
37 using System.Globalization;
38 using System.IO;
39 using System.Net;
40 using System.Net.Cache;
41 using System.Net.Sockets;
42 using System.Runtime.Remoting.Messaging;
43 using System.Runtime.Serialization;
44 using System.Security.Cryptography.X509Certificates;
45 using System.Text;
46 using System.Threading;
47
48 namespace System.Net 
49 {
50         [Serializable]
51         public class HttpWebRequest : WebRequest, ISerializable {
52                 Uri requestUri;
53                 Uri actualUri;
54                 bool hostChanged;
55                 bool allowAutoRedirect = true;
56                 bool allowBuffering = true;
57                 X509CertificateCollection certificates;
58                 string connectionGroup;
59                 bool haveContentLength;
60                 long contentLength = -1;
61                 HttpContinueDelegate continueDelegate;
62                 CookieContainer cookieContainer;
63                 ICredentials credentials;
64                 bool haveResponse;              
65                 bool haveRequest;
66                 bool requestSent;
67                 WebHeaderCollection webHeaders;
68                 bool keepAlive = true;
69                 int maxAutoRedirect = 50;
70                 string mediaType = String.Empty;
71                 string method = "GET";
72                 string initialMethod = "GET";
73                 bool pipelined = true;
74                 bool preAuthenticate;
75                 bool usedPreAuth;
76                 Version version = HttpVersion.Version11;
77                 bool force_version;
78                 Version actualVersion;
79                 IWebProxy proxy;
80                 bool sendChunked;
81                 ServicePoint servicePoint;
82                 int timeout = 100000;
83                 
84                 WebConnectionStream writeStream;
85                 HttpWebResponse webResponse;
86                 WebAsyncResult asyncWrite;
87                 WebAsyncResult asyncRead;
88                 EventHandler abortHandler;
89                 int aborted;
90                 bool gotRequestStream;
91                 int redirects;
92                 bool expectContinue;
93                 byte[] bodyBuffer;
94                 int bodyBufferLength;
95                 bool getResponseCalled;
96                 Exception saved_exc;
97                 object locker = new object ();
98                 bool finished_reading;
99                 internal WebConnection WebConnection;
100                 DecompressionMethods auto_decomp;
101                 int maxResponseHeadersLength;
102                 static int defaultMaxResponseHeadersLength;
103                 int readWriteTimeout = 300000; // ms
104
105                 enum NtlmAuthState {
106                         None,
107                         Challenge,
108                         Response
109                 }
110                 AuthorizationState auth_state, proxy_auth_state;
111                 string host;
112
113                 // Constructors
114                 static HttpWebRequest ()
115                 {
116                         defaultMaxResponseHeadersLength = 64 * 1024;
117 #if !NET_2_1
118                         NetConfig config = ConfigurationSettings.GetConfig ("system.net/settings") as NetConfig;
119                         if (config != null) {
120                                 int x = config.MaxResponseHeadersLength;
121                                 if (x != -1)
122                                         x *= 64;
123
124                                 defaultMaxResponseHeadersLength = x;
125                         }
126 #endif
127                 }
128
129 #if NET_2_1
130                 public
131 #else
132                 internal
133 #endif
134                 HttpWebRequest (Uri uri) 
135                 {
136                         this.requestUri = uri;
137                         this.actualUri = uri;
138                         this.proxy = GlobalProxySelection.Select;
139                         this.webHeaders = new WebHeaderCollection (WebHeaderCollection.HeaderInfo.Request);
140                         ThrowOnError = true;
141                         ResetAuthorization ();
142                 }
143                 
144                 [Obsolete ("Serialization is obsoleted for this type", false)]
145                 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
146                 {
147                         SerializationInfo info = serializationInfo;
148
149                         requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
150                         actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
151                         allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
152                         allowBuffering = info.GetBoolean ("allowBuffering");
153                         certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
154                         connectionGroup = info.GetString ("connectionGroup");
155                         contentLength = info.GetInt64 ("contentLength");
156                         webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
157                         keepAlive = info.GetBoolean ("keepAlive");
158                         maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
159                         mediaType = info.GetString ("mediaType");
160                         method = info.GetString ("method");
161                         initialMethod = info.GetString ("initialMethod");
162                         pipelined = info.GetBoolean ("pipelined");
163                         version = (Version) info.GetValue ("version", typeof (Version));
164                         proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
165                         sendChunked = info.GetBoolean ("sendChunked");
166                         timeout = info.GetInt32 ("timeout");
167                         redirects = info.GetInt32 ("redirects");
168                         host = info.GetString ("host");
169                         ResetAuthorization ();
170                 }
171
172                 void ResetAuthorization ()
173                 {
174                         auth_state = new AuthorizationState (this, false);
175                         proxy_auth_state = new AuthorizationState (this, true);
176                 }
177                 
178                 // Properties
179
180                 public string Accept {
181                         get { return webHeaders ["Accept"]; }
182                         set {
183                                 CheckRequestStarted ();
184                                 webHeaders.RemoveAndAdd ("Accept", value);
185                         }
186                 }
187                 
188                 public Uri Address {
189                         get { return actualUri; }
190                         internal set { actualUri = value; } // Used by Ftp+proxy
191                 }
192                 
193                 public bool AllowAutoRedirect {
194                         get { return allowAutoRedirect; }
195                         set { this.allowAutoRedirect = value; }
196                 }
197                 
198                 public bool AllowWriteStreamBuffering {
199                         get { return allowBuffering; }
200                         set { allowBuffering = value; }
201                 }
202                 
203 #if NET_4_5
204                 public virtual bool AllowReadStreamBuffering {
205                         get { return allowBuffering; }
206                         set { allowBuffering = value; }
207                 }
208 #endif
209
210                 static Exception GetMustImplement ()
211                 {
212                         return new NotImplementedException ();
213                 }
214                 
215                 public DecompressionMethods AutomaticDecompression
216                 {
217                         get {
218                                 return auto_decomp;
219                         }
220                         set {
221                                 CheckRequestStarted ();
222                                 auto_decomp = value;
223                         }
224                 }
225                 
226                 internal bool InternalAllowBuffering {
227                         get {
228                                 return (allowBuffering && (method != "HEAD" && method != "GET" &&
229                                                         method != "MKCOL" && method != "CONNECT" &&
230                                                         method != "TRACE"));
231                         }
232                 }
233                 
234                 public X509CertificateCollection ClientCertificates {
235                         get {
236                                 if (certificates == null)
237                                         certificates = new X509CertificateCollection ();
238
239                                 return certificates;
240                         }
241                         [MonoTODO]
242                         set {
243                                 throw GetMustImplement ();
244                         }
245                 }
246                 
247                 public string Connection {
248                         get { return webHeaders ["Connection"]; }
249                         set {
250                                 CheckRequestStarted ();
251
252                                 if (string.IsNullOrEmpty (value)) {
253                                         webHeaders.RemoveInternal ("Connection");
254                                         return;
255                                 }
256
257                                 string val = value.ToLowerInvariant ();
258                                 if (val.Contains ("keep-alive") || val.Contains ("close"))
259                                         throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
260
261                                 if (keepAlive)
262                                         value = value + ", Keep-Alive";
263                                 
264                                 webHeaders.RemoveAndAdd ("Connection", value);
265                         }
266                 }               
267                 
268                 public override string ConnectionGroupName { 
269                         get { return connectionGroup; }
270                         set { connectionGroup = value; }
271                 }
272                 
273                 public override long ContentLength { 
274                         get { return contentLength; }
275                         set { 
276                                 CheckRequestStarted ();
277                                 if (value < 0)
278                                         throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
279                                         
280                                 contentLength = value;
281                                 haveContentLength = true;
282                         }
283                 }
284                 
285                 internal long InternalContentLength {
286                         set { contentLength = value; }
287                 }
288                         
289                 internal bool ThrowOnError { get; set; }
290                 
291                 public override string ContentType { 
292                         get { return webHeaders ["Content-Type"]; }
293                         set {
294                                 if (value == null || value.Trim().Length == 0) {
295                                         webHeaders.RemoveInternal ("Content-Type");
296                                         return;
297                                 }
298                                 webHeaders.RemoveAndAdd ("Content-Type", value);
299                         }
300                 }
301                 
302                 public HttpContinueDelegate ContinueDelegate {
303                         get { return continueDelegate; }
304                         set { continueDelegate = value; }
305                 }
306                 
307 #if NET_4_5
308                 virtual
309 #endif
310                 public CookieContainer CookieContainer {
311                         get { return cookieContainer; }
312                         set { cookieContainer = value; }
313                 }
314                 
315                 public override ICredentials Credentials { 
316                         get { return credentials; }
317                         set { credentials = value; }
318                 }
319 #if NET_4_0
320                 public DateTime Date {
321                         get {
322                                 string date = webHeaders ["Date"];
323                                 if (date == null)
324                                         return DateTime.MinValue;
325                                 return DateTime.ParseExact (date, "r", CultureInfo.InvariantCulture).ToLocalTime ();
326                         }
327                         set {
328                                 if (value.Equals (DateTime.MinValue))
329                                         webHeaders.RemoveInternal ("Date");
330                                 else
331                                         webHeaders.RemoveAndAdd ("Date", value.ToUniversalTime ().ToString ("r", CultureInfo.InvariantCulture));
332                         }
333                 }
334 #endif
335
336 #if !NET_2_1
337                 [MonoTODO]
338                 public static new RequestCachePolicy DefaultCachePolicy
339                 {
340                         get {
341                                 throw GetMustImplement ();
342                         }
343                         set {
344                                 throw GetMustImplement ();
345                         }
346                 }
347 #endif
348                 
349                 [MonoTODO]
350                 public static int DefaultMaximumErrorResponseLength
351                 {
352                         get {
353                                 throw GetMustImplement ();
354                         }
355                         set {
356                                 throw GetMustImplement ();
357                         }
358                 }
359                 
360                 public string Expect {
361                         get { return webHeaders ["Expect"]; }
362                         set {
363                                 CheckRequestStarted ();
364                                 string val = value;
365                                 if (val != null)
366                                         val = val.Trim ().ToLower ();
367
368                                 if (val == null || val.Length == 0) {
369                                         webHeaders.RemoveInternal ("Expect");
370                                         return;
371                                 }
372
373                                 if (val == "100-continue")
374                                         throw new ArgumentException ("100-Continue cannot be set with this property.",
375                                                                      "value");
376                                 webHeaders.RemoveAndAdd ("Expect", value);
377                         }
378                 }
379                 
380 #if NET_4_5
381                 virtual
382 #endif
383                 public bool HaveResponse {
384                         get { return haveResponse; }
385                 }
386                 
387                 public override WebHeaderCollection Headers { 
388                         get { return webHeaders; }
389                         set {
390                                 CheckRequestStarted ();
391                                 WebHeaderCollection newHeaders = new WebHeaderCollection (WebHeaderCollection.HeaderInfo.Request);
392                                 int count = value.Count;
393                                 for (int i = 0; i < count; i++) 
394                                         newHeaders.Add (value.GetKey (i), value.Get (i));
395
396                                 webHeaders = newHeaders;
397                         }
398                 }
399                 
400 #if NET_4_0
401                 public
402 #else
403                 internal
404 #endif
405                 string Host {
406                         get {
407                                 if (host == null)
408                                         return actualUri.Authority;
409                                 return host;
410                         }
411                         set {
412                                 if (value == null)
413                                         throw new ArgumentNullException ("value");
414
415                                 if (!CheckValidHost (actualUri.Scheme, value))
416                                         throw new ArgumentException ("Invalid host: " + value);
417
418                                 host = value;
419                         }
420                 }
421
422                 static bool CheckValidHost (string scheme, string val)
423                 {
424                         if (val.Length == 0)
425                                 return false;
426
427                         if (val [0] == '.')
428                                 return false;
429
430                         int idx = val.IndexOf ('/');
431                         if (idx >= 0)
432                                 return false;
433
434                         IPAddress ipaddr;
435                         if (IPAddress.TryParse (val, out ipaddr))
436                                 return true;
437
438                         string u = scheme + "://" + val + "/";
439                         return Uri.IsWellFormedUriString (u, UriKind.Absolute);
440                 }
441
442                 public DateTime IfModifiedSince {
443                         get { 
444                                 string str = webHeaders ["If-Modified-Since"];
445                                 if (str == null)
446                                         return DateTime.Now;
447                                 try {
448                                         return MonoHttpDate.Parse (str);
449                                 } catch (Exception) {
450                                         return DateTime.Now;
451                                 }
452                         }
453                         set {
454                                 CheckRequestStarted ();
455                                 // rfc-1123 pattern
456                                 webHeaders.SetInternal ("If-Modified-Since", 
457                                         value.ToUniversalTime ().ToString ("r", null));
458                                 // TODO: check last param when using different locale
459                         }
460                 }
461
462                 public bool KeepAlive {         
463                         get {
464                                 return keepAlive;
465                         }
466                         set {
467                                 keepAlive = value;
468                         }
469                 }
470                 
471                 public int MaximumAutomaticRedirections {
472                         get { return maxAutoRedirect; }
473                         set {
474                                 if (value <= 0)
475                                         throw new ArgumentException ("Must be > 0", "value");
476
477                                 maxAutoRedirect = value;
478                         }                       
479                 }
480
481                 [MonoTODO ("Use this")]
482                 public int MaximumResponseHeadersLength {
483                         get { return maxResponseHeadersLength; }
484                         set { maxResponseHeadersLength = value; }
485                 }
486
487                 [MonoTODO ("Use this")]
488                 public static int DefaultMaximumResponseHeadersLength {
489                         get { return defaultMaxResponseHeadersLength; }
490                         set { defaultMaxResponseHeadersLength = value; }
491                 }
492
493                 public  int ReadWriteTimeout {
494                         get { return readWriteTimeout; }
495                         set {
496                                 if (requestSent)
497                                         throw new InvalidOperationException ("The request has already been sent.");
498
499                                 if (value < -1)
500                                         throw new ArgumentOutOfRangeException ("value", "Must be >= -1");
501
502                                 readWriteTimeout = value;
503                         }
504                 }
505                 
506 #if NET_4_5
507                 [MonoTODO]
508                 public int ContinueTimeout {
509                         get { throw new NotImplementedException (); }
510                         set { throw new NotImplementedException (); }
511                 }
512 #endif
513                 
514                 public string MediaType {
515                         get { return mediaType; }
516                         set { 
517                                 mediaType = value;
518                         }
519                 }
520                 
521                 public override string Method { 
522                         get { return this.method; }
523                         set { 
524                                 if (value == null || value.Trim () == "")
525                                         throw new ArgumentException ("not a valid method");
526
527                                 method = value.ToUpperInvariant ();
528                                 if (method != "HEAD" && method != "GET" && method != "POST" && method != "PUT" &&
529                                         method != "DELETE" && method != "CONNECT" && method != "TRACE" &&
530                                         method != "MKCOL") {
531                                         method = value;
532                                 }
533                         }
534                 }
535                 
536                 public bool Pipelined {
537                         get { return pipelined; }
538                         set { pipelined = value; }
539                 }               
540                 
541                 public override bool PreAuthenticate { 
542                         get { return preAuthenticate; }
543                         set { preAuthenticate = value; }
544                 }
545                 
546                 public Version ProtocolVersion {
547                         get { return version; }
548                         set { 
549                                 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
550                                         throw new ArgumentException ("value");
551
552                                 force_version = true;
553                                 version = value; 
554                         }
555                 }
556                 
557                 public override IWebProxy Proxy { 
558                         get { return proxy; }
559                         set { 
560                                 CheckRequestStarted ();
561                                 proxy = value;
562                                 servicePoint = null; // we may need a new one
563                         }
564                 }
565                 
566                 public string Referer {
567                         get { return webHeaders ["Referer"]; }
568                         set {
569                                 CheckRequestStarted ();
570                                 if (value == null || value.Trim().Length == 0) {
571                                         webHeaders.RemoveInternal ("Referer");
572                                         return;
573                                 }
574                                 webHeaders.SetInternal ("Referer", value);
575                         }
576                 }
577
578                 public override Uri RequestUri { 
579                         get { return requestUri; }
580                 }
581                 
582                 public bool SendChunked {
583                         get { return sendChunked; }
584                         set {
585                                 CheckRequestStarted ();
586                                 sendChunked = value;
587                         }
588                 }
589                 
590                 public ServicePoint ServicePoint {
591                         get { return GetServicePoint (); }
592                 }
593
594                 internal ServicePoint ServicePointNoLock {
595                         get { return servicePoint; }
596                 }
597 #if NET_4_0
598                 public virtual bool SupportsCookieContainer { 
599                         get {
600                                 // The managed implementation supports the cookie container
601                                 // it is only Silverlight that returns false here
602                                 return true;
603                         }
604                 }
605 #endif
606                 public override int Timeout { 
607                         get { return timeout; }
608                         set {
609                                 if (value < -1)
610                                         throw new ArgumentOutOfRangeException ("value");
611
612                                 timeout = value;
613                         }
614                 }
615                 
616                 public string TransferEncoding {
617                         get { return webHeaders ["Transfer-Encoding"]; }
618                         set {
619                                 CheckRequestStarted ();
620                                 string val = value;
621                                 if (val != null)
622                                         val = val.Trim ().ToLower ();
623
624                                 if (val == null || val.Length == 0) {
625                                         webHeaders.RemoveInternal ("Transfer-Encoding");
626                                         return;
627                                 }
628
629                                 if (val == "chunked")
630                                         throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
631
632                                 if (!sendChunked)
633                                         throw new ArgumentException ("SendChunked must be True", "value");
634
635                                 webHeaders.RemoveAndAdd ("Transfer-Encoding", value);
636                         }
637                 }
638
639                 public override bool UseDefaultCredentials
640                 {
641                         get { return CredentialCache.DefaultCredentials == Credentials; }
642                         set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
643                 }
644                 
645                 public string UserAgent {
646                         get { return webHeaders ["User-Agent"]; }
647                         set { webHeaders.SetInternal ("User-Agent", value); }
648                 }
649
650                 bool unsafe_auth_blah;
651                 public bool UnsafeAuthenticatedConnectionSharing
652                 {
653                         get { return unsafe_auth_blah; }
654                         set { unsafe_auth_blah = value; }
655                 }
656
657                 internal bool GotRequestStream {
658                         get { return gotRequestStream; }
659                 }
660
661                 internal bool ExpectContinue {
662                         get { return expectContinue; }
663                         set { expectContinue = value; }
664                 }
665                 
666                 internal Uri AuthUri {
667                         get { return actualUri; }
668                 }
669                 
670                 internal bool ProxyQuery {
671                         get { return servicePoint.UsesProxy && !servicePoint.UseConnect; }
672                 }
673                 
674                 // Methods
675                 
676                 internal ServicePoint GetServicePoint ()
677                 {
678                         lock (locker) {
679                                 if (hostChanged || servicePoint == null) {
680                                         servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
681                                         hostChanged = false;
682                                 }
683                         }
684
685                         return servicePoint;
686                 }
687                 
688                 public void AddRange (int range)
689                 {
690                         AddRange ("bytes", (long) range);
691                 }
692                 
693                 public void AddRange (int from, int to)
694                 {
695                         AddRange ("bytes", (long) from, (long) to);
696                 }
697                 
698                 public void AddRange (string rangeSpecifier, int range)
699                 {
700                         AddRange (rangeSpecifier, (long) range);
701                 }
702                 
703                 public void AddRange (string rangeSpecifier, int from, int to)
704                 {
705                         AddRange (rangeSpecifier, (long) from, (long) to);
706                 }
707 #if NET_4_0
708                 public
709 #else
710                 internal
711 #endif
712                 void AddRange (long range)
713                 {
714                         AddRange ("bytes", (long) range);
715                 }
716
717 #if NET_4_0
718                 public
719 #else
720                 internal
721 #endif
722                 void AddRange (long from, long to)
723                 {
724                         AddRange ("bytes", from, to);
725                 }
726
727 #if NET_4_0
728                 public
729 #else
730                 internal
731 #endif
732                 void AddRange (string rangeSpecifier, long range)
733                 {
734                         if (rangeSpecifier == null)
735                                 throw new ArgumentNullException ("rangeSpecifier");
736                         if (!WebHeaderCollection.IsHeaderValue (rangeSpecifier))
737                                 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
738
739                         string r = webHeaders ["Range"];
740                         if (r == null)
741                                 r = rangeSpecifier + "=";
742                         else {
743                                 string old_specifier = r.Substring (0, r.IndexOf ('='));
744                                 if (String.Compare (old_specifier, rangeSpecifier, StringComparison.OrdinalIgnoreCase) != 0)
745                                         throw new InvalidOperationException ("A different range specifier is already in use");
746                                 r += ",";
747                         }
748
749                         string n = range.ToString (CultureInfo.InvariantCulture);
750                         if (range < 0)
751                                 r = r + "0" + n;
752                         else
753                                 r = r + n + "-";
754                         webHeaders.RemoveAndAdd ("Range", r);
755                 }
756
757 #if NET_4_0
758                 public
759 #else
760                 internal
761 #endif
762                 void AddRange (string rangeSpecifier, long from, long to)
763                 {
764                         if (rangeSpecifier == null)
765                                 throw new ArgumentNullException ("rangeSpecifier");
766                         if (!WebHeaderCollection.IsHeaderValue (rangeSpecifier))
767                                 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
768                         if (from > to || from < 0)
769                                 throw new ArgumentOutOfRangeException ("from");
770                         if (to < 0)
771                                 throw new ArgumentOutOfRangeException ("to");
772
773                         string r = webHeaders ["Range"];
774                         if (r == null)
775                                 r = rangeSpecifier + "=";
776                         else
777                                 r += ",";
778
779                         r = String.Format ("{0}{1}-{2}", r, from, to);
780                         webHeaders.RemoveAndAdd ("Range", r);
781                 }
782
783                 
784                 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
785                 {
786                         if (Aborted)
787                                 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
788
789                         bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD" ||
790                                         method == "TRACE");
791                         if (method == null || !send)
792                                 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
793
794                         if (contentLength == -1 && !sendChunked && !allowBuffering && KeepAlive)
795                                 throw new ProtocolViolationException ("Content-Length not set");
796
797                         string transferEncoding = TransferEncoding;
798                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
799                                 throw new ProtocolViolationException ("SendChunked should be true.");
800
801                         lock (locker)
802                         {
803                                 if (getResponseCalled)
804                                         throw new InvalidOperationException ("The operation cannot be performed once the request has been submitted.");
805
806                                 if (asyncWrite != null) {
807                                         throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
808                                                                 "method while a previous call is still in progress.");
809                                 }
810         
811                                 asyncWrite = new WebAsyncResult (this, callback, state);
812                                 initialMethod = method;
813                                 if (haveRequest) {
814                                         if (writeStream != null) {
815                                                 asyncWrite.SetCompleted (true, writeStream);
816                                                 asyncWrite.DoCallback ();
817                                                 return asyncWrite;
818                                         }
819                                 }
820                                 
821                                 gotRequestStream = true;
822                                 WebAsyncResult result = asyncWrite;
823                                 if (!requestSent) {
824                                         requestSent = true;
825                                         redirects = 0;
826                                         servicePoint = GetServicePoint ();
827                                         abortHandler = servicePoint.SendRequest (this, connectionGroup);
828                                 }
829                                 return result;
830                         }
831                 }
832
833                 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
834                 {
835                         if (asyncResult == null)
836                                 throw new ArgumentNullException ("asyncResult");
837
838                         WebAsyncResult result = asyncResult as WebAsyncResult;
839                         if (result == null)
840                                 throw new ArgumentException ("Invalid IAsyncResult");
841
842                         asyncWrite = result;
843                         result.WaitUntilComplete ();
844
845                         Exception e = result.Exception;
846                         if (e != null)
847                                 throw e;
848
849                         return result.WriteStream;
850                 }
851                 
852                 public override Stream GetRequestStream()
853                 {
854                         IAsyncResult asyncResult = asyncWrite;
855                         if (asyncResult == null) {
856                                 asyncResult = BeginGetRequestStream (null, null);
857                                 asyncWrite = (WebAsyncResult) asyncResult;
858                         }
859
860                         if (!asyncResult.IsCompleted && !asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
861                                 Abort ();
862                                 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
863                         }
864
865                         return EndGetRequestStream (asyncResult);
866                 }
867
868                 WebAsyncResult CheckIfForceWrite (AsyncCallback callback, object state)
869                 {
870                         if (writeStream == null || writeStream.RequestWritten || !InternalAllowBuffering)
871                                 return null;
872 #if NET_4_0
873                         if (contentLength < 0 && writeStream.CanWrite == true && writeStream.WriteBufferLength < 0)
874                                 return null;
875
876                         if (contentLength < 0 && writeStream.WriteBufferLength >= 0)
877                                 InternalContentLength = writeStream.WriteBufferLength;
878 #else
879                         if (contentLength < 0 && writeStream.CanWrite == true)
880                                 return null;
881 #endif
882
883                         // This will write the POST/PUT if the write stream already has the expected
884                         // amount of bytes in it (ContentLength) (bug #77753) or if the write stream
885                         // contains data and it has been closed already (xamarin bug #1512).
886
887                         if (writeStream.WriteBufferLength == contentLength || (contentLength == -1 && writeStream.CanWrite == false))
888                                 return writeStream.WriteRequestAsync (callback, state);
889
890                         return null;
891                 }
892
893                 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
894                 {
895                         if (Aborted)
896                                 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
897
898                         if (method == null)
899                                 throw new ProtocolViolationException ("Method is null.");
900
901                         string transferEncoding = TransferEncoding;
902                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
903                                 throw new ProtocolViolationException ("SendChunked should be true.");
904
905                         Monitor.Enter (locker);
906                         getResponseCalled = true;
907                         if (asyncRead != null && !haveResponse) {
908                                 Monitor.Exit (locker);
909                                 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
910                                                         "method while a previous call is still in progress.");
911                         }
912
913                         asyncRead = new WebAsyncResult (this, callback, state);
914                         WebAsyncResult aread = asyncRead;
915                         initialMethod = method;
916
917                         aread.InnerAsyncResult = CheckIfForceWrite (GetResponseAsyncCB, aread);
918                         if (aread.InnerAsyncResult == null)
919                                 GetResponseAsyncCB2 (aread);
920                         else
921                                 Monitor.Exit (locker);
922                         return aread;
923                 }
924
925                 void GetResponseAsyncCB (IAsyncResult ar)
926                 {
927                         var result = (WebAsyncResult)ar;
928                         var innerResult = (WebAsyncResult)result.InnerAsyncResult;
929                         result.InnerAsyncResult = null;
930
931                         if (innerResult != null && innerResult.GotException) {
932                                 asyncRead.SetCompleted (true, innerResult.Exception);
933                                 asyncRead.DoCallback ();
934                                 return;
935                         }
936
937                         Monitor.Enter (locker);
938                         GetResponseAsyncCB2 ((WebAsyncResult)ar);
939                 }
940
941                 void GetResponseAsyncCB2 (WebAsyncResult aread)
942                 {
943                         if (haveResponse) {
944                                 Exception saved = saved_exc;
945                                 if (webResponse != null) {
946                                         Monitor.Exit (locker);
947                                         if (saved == null) {
948                                                 aread.SetCompleted (true, webResponse);
949                                         } else {
950                                                 aread.SetCompleted (true, saved);
951                                         }
952                                         aread.DoCallback ();
953                                         return;
954                                 } else if (saved != null) {
955                                         Monitor.Exit (locker);
956                                         aread.SetCompleted (true, saved);
957                                         aread.DoCallback ();
958                                         return;
959                                 }
960                         }
961
962                         if (!requestSent) {
963                                 requestSent = true;
964                                 redirects = 0;
965                                 servicePoint = GetServicePoint ();
966                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
967                         }
968
969                         Monitor.Exit (locker);
970                 }
971
972                 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
973                 {
974                         if (asyncResult == null)
975                                 throw new ArgumentNullException ("asyncResult");
976
977                         WebAsyncResult result = asyncResult as WebAsyncResult;
978                         if (result == null)
979                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
980
981                         if (!result.WaitUntilComplete (timeout, false)) {
982                                 Abort ();
983                                 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
984                         }
985
986                         if (result.GotException)
987                                 throw result.Exception;
988
989                         return result.Response;
990                 }
991                 
992 #if NET_3_5
993                 public Stream EndGetRequestStream (IAsyncResult asyncResult, out TransportContext transportContext)
994                 {
995                         transportContext = null;
996                         return EndGetRequestStream (asyncResult);
997                 }
998 #endif
999
1000                 public override WebResponse GetResponse()
1001                 {
1002                         WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
1003                         return EndGetResponse (result);
1004                 }
1005                 
1006                 internal bool FinishedReading {
1007                         get { return finished_reading; }
1008                         set { finished_reading = value; }
1009                 }
1010
1011                 internal bool Aborted {
1012                         get { return Interlocked.CompareExchange (ref aborted, 0, 0) == 1; }
1013                 }
1014
1015                 public override void Abort ()
1016                 {
1017                         if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
1018                                 return;
1019
1020                         if (haveResponse && finished_reading)
1021                                 return;
1022
1023                         haveResponse = true;
1024                         if (abortHandler != null) {
1025                                 try {
1026                                         abortHandler (this, EventArgs.Empty);
1027                                 } catch (Exception) {}
1028                                 abortHandler = null;
1029                         }
1030
1031                         if (asyncWrite != null) {
1032                                 WebAsyncResult r = asyncWrite;
1033                                 if (!r.IsCompleted) {
1034                                         try {
1035                                                 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
1036                                                 r.SetCompleted (false, wexc);
1037                                                 r.DoCallback ();
1038                                         } catch {}
1039                                 }
1040                                 asyncWrite = null;
1041                         }                       
1042
1043                         if (asyncRead != null) {
1044                                 WebAsyncResult r = asyncRead;
1045                                 if (!r.IsCompleted) {
1046                                         try {
1047                                                 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
1048                                                 r.SetCompleted (false, wexc);
1049                                                 r.DoCallback ();
1050                                         } catch {}
1051                                 }
1052                                 asyncRead = null;
1053                         }                       
1054
1055                         if (writeStream != null) {
1056                                 try {
1057                                         writeStream.Close ();
1058                                         writeStream = null;
1059                                 } catch {}
1060                         }
1061
1062                         if (webResponse != null) {
1063                                 try {
1064                                         webResponse.Close ();
1065                                         webResponse = null;
1066                                 } catch {}
1067                         }
1068                 }               
1069                 
1070                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
1071                                                   StreamingContext streamingContext)
1072                 {
1073                         GetObjectData (serializationInfo, streamingContext);
1074                 }
1075
1076                 protected override void GetObjectData (SerializationInfo serializationInfo,
1077                         StreamingContext streamingContext)
1078                 {
1079                         SerializationInfo info = serializationInfo;
1080
1081                         info.AddValue ("requestUri", requestUri, typeof (Uri));
1082                         info.AddValue ("actualUri", actualUri, typeof (Uri));
1083                         info.AddValue ("allowAutoRedirect", allowAutoRedirect);
1084                         info.AddValue ("allowBuffering", allowBuffering);
1085                         info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
1086                         info.AddValue ("connectionGroup", connectionGroup);
1087                         info.AddValue ("contentLength", contentLength);
1088                         info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
1089                         info.AddValue ("keepAlive", keepAlive);
1090                         info.AddValue ("maxAutoRedirect", maxAutoRedirect);
1091                         info.AddValue ("mediaType", mediaType);
1092                         info.AddValue ("method", method);
1093                         info.AddValue ("initialMethod", initialMethod);
1094                         info.AddValue ("pipelined", pipelined);
1095                         info.AddValue ("version", version, typeof (Version));
1096                         info.AddValue ("proxy", proxy, typeof (IWebProxy));
1097                         info.AddValue ("sendChunked", sendChunked);
1098                         info.AddValue ("timeout", timeout);
1099                         info.AddValue ("redirects", redirects);
1100                         info.AddValue ("host", host);
1101                 }
1102                 
1103                 void CheckRequestStarted () 
1104                 {
1105                         if (requestSent)
1106                                 throw new InvalidOperationException ("request started");
1107                 }
1108
1109                 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
1110                 {
1111                         if (continueDelegate != null)
1112                                 continueDelegate (statusCode, headers);
1113                 }
1114
1115                 void RewriteRedirectToGet ()
1116                 {
1117                         method = "GET";
1118                         webHeaders.RemoveInternal ("Transfer-Encoding");
1119                         sendChunked = false;
1120                 }
1121                 
1122                 bool Redirect (WebAsyncResult result, HttpStatusCode code, WebResponse response)
1123                 {
1124                         redirects++;
1125                         Exception e = null;
1126                         string uriString = null;
1127                         switch (code) {
1128                         case HttpStatusCode.Ambiguous: // 300
1129                                 e = new WebException ("Ambiguous redirect.");
1130                                 break;
1131                         case HttpStatusCode.MovedPermanently: // 301
1132                         case HttpStatusCode.Redirect: // 302
1133                                 if (method == "POST")
1134                                         RewriteRedirectToGet ();
1135                                 break;
1136                         case HttpStatusCode.TemporaryRedirect: // 307
1137                                 break;
1138                         case HttpStatusCode.SeeOther: //303
1139                                 RewriteRedirectToGet ();
1140                                 break;
1141                         case HttpStatusCode.NotModified: // 304
1142                                 return false;
1143                         case HttpStatusCode.UseProxy: // 305
1144                                 e = new NotImplementedException ("Proxy support not available.");
1145                                 break;
1146                         case HttpStatusCode.Unused: // 306
1147                         default:
1148                                 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
1149                                 break;
1150                         }
1151
1152                         if (method != "GET" && !InternalAllowBuffering)
1153                                 e = new WebException ("The request requires buffering data to succeed.", null, WebExceptionStatus.ProtocolError, webResponse);
1154
1155                         if (e != null)
1156                                 throw e;
1157
1158                         contentLength = -1;
1159                         uriString = webResponse.Headers ["Location"];
1160
1161                         if (uriString == null)
1162                                 throw new WebException ("No Location header found for " + (int) code,
1163                                                         WebExceptionStatus.ProtocolError);
1164
1165                         Uri prev = actualUri;
1166                         try {
1167                                 actualUri = new Uri (actualUri, uriString);
1168                         } catch (Exception) {
1169                                 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
1170                                                                         uriString, (int) code),
1171                                                                         WebExceptionStatus.ProtocolError);
1172                         }
1173
1174                         hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
1175                         return true;
1176                 }
1177
1178                 string GetHeaders ()
1179                 {
1180                         bool continue100 = false;
1181                         if (sendChunked) {
1182                                 continue100 = true;
1183                                 webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
1184                                 webHeaders.RemoveInternal ("Content-Length");
1185                         } else if (contentLength != -1) {
1186                                 if (auth_state.NtlmAuthState == NtlmAuthState.Challenge || proxy_auth_state.NtlmAuthState == NtlmAuthState.Challenge) {
1187                                         // We don't send any body with the NTLM Challenge request.
1188                                         if (haveContentLength || gotRequestStream || contentLength > 0)
1189                                                 webHeaders.SetInternal ("Content-Length", "0");
1190                                         else
1191                                                 webHeaders.RemoveInternal ("Content-Length");
1192                                 } else {
1193                                         if (contentLength > 0)
1194                                                 continue100 = true;
1195
1196                                         if (haveContentLength || gotRequestStream || contentLength > 0)
1197                                                 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
1198                                 }
1199                                 webHeaders.RemoveInternal ("Transfer-Encoding");
1200                         } else {
1201                                 webHeaders.RemoveInternal ("Content-Length");
1202                         }
1203
1204                         if (actualVersion == HttpVersion.Version11 && continue100 &&
1205                             servicePoint.SendContinue) { // RFC2616 8.2.3
1206                                 webHeaders.RemoveAndAdd ("Expect" , "100-continue");
1207                                 expectContinue = true;
1208                         } else {
1209                                 webHeaders.RemoveInternal ("Expect");
1210                                 expectContinue = false;
1211                         }
1212
1213                         bool proxy_query = ProxyQuery;
1214                         string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
1215                         webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
1216                         Version proto_version = servicePoint.ProtocolVersion;
1217                         bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
1218
1219                         if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
1220                                 if (webHeaders[connectionHeader] == null
1221                                     || webHeaders[connectionHeader].IndexOf ("keep-alive", StringComparison.OrdinalIgnoreCase) == -1)
1222                                         webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
1223                         } else if (!keepAlive && version == HttpVersion.Version11) {
1224                                 webHeaders.RemoveAndAdd (connectionHeader, "close");
1225                         }
1226
1227                         webHeaders.SetInternal ("Host", Host);
1228                         if (cookieContainer != null) {
1229                                 string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
1230                                 if (cookieHeader != "")
1231                                         webHeaders.RemoveAndAdd ("Cookie", cookieHeader);
1232                                 else
1233                                         webHeaders.RemoveInternal ("Cookie");
1234                         }
1235
1236                         string accept_encoding = null;
1237                         if ((auto_decomp & DecompressionMethods.GZip) != 0)
1238                                 accept_encoding = "gzip";
1239                         if ((auto_decomp & DecompressionMethods.Deflate) != 0)
1240                                 accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
1241                         if (accept_encoding != null)
1242                                 webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);
1243
1244                         if (!usedPreAuth && preAuthenticate)
1245                                 DoPreAuthenticate ();
1246
1247                         return webHeaders.ToString ();
1248                 }
1249
1250                 void DoPreAuthenticate ()
1251                 {
1252                         bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1253                         ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
1254                         Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
1255                         if (auth == null)
1256                                 return;
1257
1258                         webHeaders.RemoveInternal ("Proxy-Authorization");
1259                         webHeaders.RemoveInternal ("Authorization");
1260                         string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
1261                         webHeaders [authHeader] = auth.Message;
1262                         usedPreAuth = true;
1263                 }
1264                 
1265                 internal void SetWriteStreamError (WebExceptionStatus status, Exception exc)
1266                 {
1267                         if (Aborted)
1268                                 return;
1269
1270                         WebAsyncResult r = asyncWrite;
1271                         if (r == null)
1272                                 r = asyncRead;
1273
1274                         if (r != null) {
1275                                 string msg;
1276                                 WebException wex;
1277                                 if (exc == null) {
1278                                         msg = "Error: " + status;
1279                                         wex = new WebException (msg, status);
1280                                 } else {
1281                                         msg = String.Format ("Error: {0} ({1})", status, exc.Message);
1282                                         wex = new WebException (msg, exc, status);
1283                                 }
1284                                 r.SetCompleted (false, wex);
1285                                 r.DoCallback ();
1286                         }
1287                 }
1288
1289                 internal byte[] GetRequestHeaders ()
1290                 {
1291                         StringBuilder req = new StringBuilder ();
1292                         string query;
1293                         if (!ProxyQuery) {
1294                                 query = actualUri.PathAndQuery;
1295                         } else {
1296                                 query = String.Format ("{0}://{1}{2}",  actualUri.Scheme,
1297                                                                         Host,
1298                                                                         actualUri.PathAndQuery);
1299                         }
1300                         
1301                         if (!force_version && servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
1302                                 actualVersion = servicePoint.ProtocolVersion;
1303                         } else {
1304                                 actualVersion = version;
1305                         }
1306
1307                         req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
1308                                                                 actualVersion.Major, actualVersion.Minor);
1309                         req.Append (GetHeaders ());
1310                         string reqstr = req.ToString ();
1311                         return Encoding.UTF8.GetBytes (reqstr);
1312                 }
1313
1314                 internal void SetWriteStream (WebConnectionStream stream)
1315                 {
1316                         if (Aborted)
1317                                 return;
1318                         
1319                         writeStream = stream;
1320                         if (bodyBuffer != null) {
1321                                 webHeaders.RemoveInternal ("Transfer-Encoding");
1322                                 contentLength = bodyBufferLength;
1323                                 writeStream.SendChunked = false;
1324                         }
1325
1326                         try {
1327                                 var result = writeStream.SetHeadersAsync (false, SetWriteStreamCB, null);
1328                                 if (result == null)
1329                                         SetWriteStreamCB (null);
1330                         } catch (Exception exc) {
1331                                 SetWriteStreamErrorCB (exc);
1332                         }
1333                 }
1334
1335                 void SetWriteStreamErrorCB (Exception exc)
1336                 {
1337                         WebException wexc = exc as WebException;
1338                         if (wexc != null)
1339                                 SetWriteStreamError (wexc.Status, wexc);
1340                         else
1341                                 SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
1342                 }
1343
1344                 void SetWriteStreamCB (IAsyncResult ar)
1345                 {
1346                         WebAsyncResult result = ar as WebAsyncResult;
1347
1348                         if (result != null && result.Exception != null) {
1349                                 SetWriteStreamErrorCB (result.Exception);
1350                                 return;
1351                         }
1352                 
1353                         haveRequest = true;
1354
1355                         WebAsyncResult writeRequestResult = null;
1356
1357                         if (bodyBuffer != null) {
1358                                 // The body has been written and buffered. The request "user"
1359                                 // won't write it again, so we must do it.
1360                                 if (auth_state.NtlmAuthState != NtlmAuthState.Challenge && proxy_auth_state.NtlmAuthState != NtlmAuthState.Challenge) {
1361                                         // FIXME: this is a blocking call on the thread pool that could lead to thread pool exhaustion
1362                                         writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1363                                         bodyBuffer = null;
1364                                         writeStream.Close ();
1365                                 }
1366                         } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
1367                                         method != "TRACE") {
1368                                 if (getResponseCalled && !writeStream.RequestWritten)
1369                                         writeRequestResult = writeStream.WriteRequestAsync (SetWriteStreamCB2, null);
1370                         }
1371
1372                         if (writeRequestResult == null)
1373                                 SetWriteStreamCB2 (null);
1374                 }
1375
1376                 void SetWriteStreamCB2 (IAsyncResult ar)
1377                 {
1378                         var result = (WebAsyncResult)ar;
1379                         if (result != null && result.GotException) {
1380                                 SetWriteStreamErrorCB (result.Exception);
1381                                 return;
1382                         }
1383
1384                         if (asyncWrite != null) {
1385                                 asyncWrite.SetCompleted (false, writeStream);
1386                                 asyncWrite.DoCallback ();
1387                                 asyncWrite = null;
1388                         }
1389                 }
1390
1391                 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1392                 {
1393                         if (Aborted)
1394                                 return;
1395                         lock (locker) {
1396                         string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1397                         WebAsyncResult r = asyncRead;
1398                         if (r == null)
1399                                 r = asyncWrite;
1400
1401                         WebException wexc;
1402                         if (e is WebException) {
1403                                 wexc = (WebException) e;
1404                         } else {
1405                                 wexc = new WebException (msg, e, status, null); 
1406                         }
1407                         if (r != null) {
1408                                 if (!r.IsCompleted) {
1409                                         r.SetCompleted (false, wexc);
1410                                         r.DoCallback ();
1411                                 } else if (r == asyncWrite) {
1412                                         saved_exc = wexc;
1413                                 }
1414                                 haveResponse = true;
1415                                 asyncRead = null;
1416                                 asyncWrite = null;
1417                         } else {
1418                                 haveResponse = true;
1419                                 saved_exc = wexc;
1420                         }
1421                         }
1422                 }
1423
1424                 void CheckSendError (WebConnectionData data)
1425                 {
1426                         // Got here, but no one called GetResponse
1427                         int status = data.StatusCode;
1428                         if (status < 400 || status == 401 || status == 407)
1429                                 return;
1430
1431                         if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1432                                 // The request has not been completely sent and we got here!
1433                                 // We should probably just close and cause an error in any case,
1434                                 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse); 
1435                                 if (allowBuffering || sendChunked || writeStream.totalWritten >= contentLength) {
1436                                         webResponse.ReadAll ();
1437                                 } else {
1438                                         writeStream.IgnoreIOErrors = true;
1439                                 }
1440                         }
1441                 }
1442
1443                 bool HandleNtlmAuth (WebAsyncResult r)
1444                 {
1445                         bool isProxy = webResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired;
1446                         if ((isProxy ? proxy_auth_state.NtlmAuthState : auth_state.NtlmAuthState) == NtlmAuthState.None)
1447                                 return false;
1448
1449                         WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1450                         if (wce != null) {
1451                                 WebConnection cnc = wce.Connection;
1452                                 cnc.PriorityRequest = this;
1453                                 ICredentials creds = !isProxy ? credentials : proxy.Credentials;
1454                                 if (creds != null) {
1455                                         cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM");
1456                                         cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah;
1457                                 }
1458                         }
1459                         r.Reset ();
1460                         finished_reading = false;
1461                         haveResponse = false;
1462                         webResponse.ReadAll ();
1463                         webResponse = null;
1464                         return true;
1465                 }
1466
1467                 internal void SetResponseData (WebConnectionData data)
1468                 {
1469                         lock (locker) {
1470                         if (Aborted) {
1471                                 if (data.stream != null)
1472                                         data.stream.Close ();
1473                                 return;
1474                         }
1475
1476                         WebException wexc = null;
1477                         try {
1478                                 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1479                         } catch (Exception e) {
1480                                 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null); 
1481                                 if (data.stream != null)
1482                                         data.stream.Close ();
1483                         }
1484
1485                         if (wexc == null && (method == "POST" || method == "PUT")) {
1486                                 CheckSendError (data);
1487                                 if (saved_exc != null)
1488                                         wexc = (WebException) saved_exc;
1489                         }
1490
1491                         WebAsyncResult r = asyncRead;
1492
1493                         bool forced = false;
1494                         if (r == null && webResponse != null) {
1495                                 // This is a forced completion (302, 204)...
1496                                 forced = true;
1497                                 r = new WebAsyncResult (null, null);
1498                                 r.SetCompleted (false, webResponse);
1499                         }
1500
1501                         if (r != null) {
1502                                 if (wexc != null) {
1503                                         haveResponse = true;
1504                                         if (!r.IsCompleted)
1505                                                 r.SetCompleted (false, wexc);
1506                                         r.DoCallback ();
1507                                         return;
1508                                 }
1509
1510                                 bool isProxy = ProxyQuery && !proxy.IsBypassed (actualUri);
1511
1512                                 bool redirected;
1513                                 try {
1514                                         redirected = CheckFinalStatus (r);
1515                                         if (!redirected) {
1516                                                 if ((isProxy ? proxy_auth_state.IsNtlmAuthenticated : auth_state.IsNtlmAuthenticated) &&
1517                                                                 webResponse != null && (int)webResponse.StatusCode < 400) {
1518                                                         WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1519                                                         if (wce != null) {
1520                                                                 WebConnection cnc = wce.Connection;
1521                                                                 cnc.NtlmAuthenticated = true;
1522                                                         }
1523                                                 }
1524
1525                                                 // clear internal buffer so that it does not
1526                                                 // hold possible big buffer (bug #397627)
1527                                                 if (writeStream != null)
1528                                                         writeStream.KillBuffer ();
1529
1530                                                 haveResponse = true;
1531                                                 r.SetCompleted (false, webResponse);
1532                                                 r.DoCallback ();
1533                                         } else {
1534                                                 if (sendChunked) {
1535                                                         sendChunked = false;
1536                                                         webHeaders.RemoveInternal ("Transfer-Encoding");
1537                                                 }
1538
1539                                                 if (webResponse != null) {
1540                                                         if (HandleNtlmAuth (r))
1541                                                                 return;
1542                                                         webResponse.Close ();
1543                                                 }
1544                                                 finished_reading = false;
1545                                                 haveResponse = false;
1546                                                 webResponse = null;
1547                                                 r.Reset ();
1548                                                 servicePoint = GetServicePoint ();
1549                                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1550                                         }
1551                                 } catch (WebException wexc2) {
1552                                         if (forced) {
1553                                                 saved_exc = wexc2;
1554                                                 haveResponse = true;
1555                                         }
1556                                         r.SetCompleted (false, wexc2);
1557                                         r.DoCallback ();
1558                                         return;
1559                                 } catch (Exception ex) {
1560                                         wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null); 
1561                                         if (forced) {
1562                                                 saved_exc = wexc;
1563                                                 haveResponse = true;
1564                                         }
1565                                         r.SetCompleted (false, wexc);
1566                                         r.DoCallback ();
1567                                         return;
1568                                 }
1569                         }
1570                         }
1571                 }
1572
1573                 struct AuthorizationState
1574                 {
1575                         readonly HttpWebRequest request;
1576                         readonly bool isProxy;
1577                         bool isCompleted;
1578                         NtlmAuthState ntlm_auth_state;
1579
1580                         public bool IsCompleted {
1581                                 get { return isCompleted; }
1582                         }
1583
1584                         public NtlmAuthState NtlmAuthState {
1585                                 get { return ntlm_auth_state; }
1586                         }
1587
1588                         public bool IsNtlmAuthenticated {
1589                                 get { return isCompleted && ntlm_auth_state != NtlmAuthState.None; }
1590                         }
1591
1592                         public AuthorizationState (HttpWebRequest request, bool isProxy)
1593                         {
1594                                 this.request = request;
1595                                 this.isProxy = isProxy;
1596                                 isCompleted = false;
1597                                 ntlm_auth_state = NtlmAuthState.None;
1598                         }
1599
1600                         public bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1601                         {
1602                                 isCompleted = false;
1603                                 if (code == HttpStatusCode.Unauthorized && request.credentials == null)
1604                                         return false;
1605
1606                                 // FIXME: This should never happen!
1607                                 if (isProxy != (code == HttpStatusCode.ProxyAuthenticationRequired))
1608                                         return false;
1609
1610                                 if (isProxy && (request.proxy == null || request.proxy.Credentials == null))
1611                                         return false;
1612
1613                                 string [] authHeaders = response.Headers.GetValues_internal (isProxy ? "Proxy-Authenticate" : "WWW-Authenticate", false);
1614                                 if (authHeaders == null || authHeaders.Length == 0)
1615                                         return false;
1616
1617                                 ICredentials creds = (!isProxy) ? request.credentials : request.proxy.Credentials;
1618                                 Authorization auth = null;
1619                                 foreach (string authHeader in authHeaders) {
1620                                         auth = AuthenticationManager.Authenticate (authHeader, request, creds);
1621                                         if (auth != null)
1622                                                 break;
1623                                 }
1624                                 if (auth == null)
1625                                         return false;
1626                                 request.webHeaders [isProxy ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1627                                 isCompleted = auth.Complete;
1628                                 bool is_ntlm = (auth.Module.AuthenticationType == "NTLM");
1629                                 if (is_ntlm)
1630                                         ntlm_auth_state = (NtlmAuthState)((int) ntlm_auth_state + 1);
1631                                 return true;
1632                         }
1633
1634                         public void Reset ()
1635                         {
1636                                 isCompleted = false;
1637                                 ntlm_auth_state = NtlmAuthState.None;
1638                                 request.webHeaders.RemoveInternal (isProxy ? "Proxy-Authorization" : "Authorization");
1639                         }
1640
1641                         public override string ToString ()
1642                         {
1643                                 return string.Format ("{0}AuthState [{1}:{2}]", isProxy ? "Proxy" : "", isCompleted, ntlm_auth_state);
1644                         }
1645                 }
1646
1647                 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1648                 {
1649                         bool isProxy = code == HttpStatusCode.ProxyAuthenticationRequired;
1650                         return isProxy ? proxy_auth_state.CheckAuthorization (response, code) : auth_state.CheckAuthorization (response, code);
1651                 }
1652
1653                 // Returns true if redirected
1654                 bool CheckFinalStatus (WebAsyncResult result)
1655                 {
1656                         if (result.GotException) {
1657                                 bodyBuffer = null;
1658                                 throw result.Exception;
1659                         }
1660
1661                         Exception throwMe = result.Exception;
1662
1663                         HttpWebResponse resp = result.Response;
1664                         WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1665                         HttpStatusCode code = 0;
1666                         if (throwMe == null && webResponse != null) {
1667                                 code = webResponse.StatusCode;
1668                                 if ((!auth_state.IsCompleted && code == HttpStatusCode.Unauthorized && credentials != null) ||
1669                                         (ProxyQuery && !proxy_auth_state.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) {
1670                                         if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1671                                                 // Keep the written body, so it can be rewritten in the retry
1672                                                 if (InternalAllowBuffering) {
1673                                                         if (writeStream.WriteBufferLength > 0) {
1674                                                                 bodyBuffer = writeStream.WriteBuffer;
1675                                                                 bodyBufferLength = writeStream.WriteBufferLength;
1676                                                         }
1677                                                         return true;
1678                                                 } else if (method != "PUT" && method != "POST") {
1679                                                         bodyBuffer = null;
1680                                                         return true;
1681                                                 }
1682
1683                                                 if (!ThrowOnError)
1684                                                         return false;
1685                                                         
1686                                                 writeStream.InternalClose ();
1687                                                 writeStream = null;
1688                                                 webResponse.Close ();
1689                                                 webResponse = null;
1690                                                 bodyBuffer = null;
1691                                                         
1692                                                 throw new WebException ("This request requires buffering " +
1693                                                                         "of data for authentication or " +
1694                                                                         "redirection to be sucessful.");
1695                                         }
1696                                 }
1697
1698                                 bodyBuffer = null;
1699                                 if ((int) code >= 400) {
1700                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1701                                                                     (int) code, webResponse.StatusDescription);
1702                                         throwMe = new WebException (err, null, protoError, webResponse);
1703                                         webResponse.ReadAll ();
1704                                 } else if ((int) code == 304 && allowAutoRedirect) {
1705                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1706                                                                     (int) code, webResponse.StatusDescription);
1707                                         throwMe = new WebException (err, null, protoError, webResponse);
1708                                 } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
1709                                         throwMe = new WebException ("Max. redirections exceeded.", null,
1710                                                                     protoError, webResponse);
1711                                         webResponse.ReadAll ();
1712                                 }
1713                         }
1714
1715                         bodyBuffer = null;
1716                         if (throwMe == null) {
1717                                 bool b = false;
1718                                 int c = (int) code;
1719                                 if (allowAutoRedirect && c >= 300) {
1720                                         b = Redirect (result, code, webResponse);
1721                                         if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1722                                                 bodyBuffer = writeStream.WriteBuffer;
1723                                                 bodyBufferLength = writeStream.WriteBufferLength;
1724                                         }
1725                                         if (b && !unsafe_auth_blah) {
1726                                                 auth_state.Reset ();
1727                                                 proxy_auth_state.Reset ();
1728                                         }
1729                                 }
1730
1731                                 if (resp != null && c >= 300 && c != 304)
1732                                         resp.ReadAll ();
1733
1734                                 return b;
1735                         }
1736                                 
1737                         if (!ThrowOnError)
1738                                 return false;
1739
1740                         if (writeStream != null) {
1741                                 writeStream.InternalClose ();
1742                                 writeStream = null;
1743                         }
1744
1745                         webResponse = null;
1746
1747                         throw throwMe;
1748                 }
1749
1750                 internal bool ReuseConnection {
1751                         get;
1752                         set;
1753                 }
1754
1755                 internal WebConnection StoredConnection;
1756         }
1757 }
1758