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