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