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