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