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