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