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