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