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