2007-01-22 Miguel de Icaza <miguel@novell.com>
[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.IO;
38 using System.Net.Sockets;
39 using System.Runtime.Remoting.Messaging;
40 using System.Runtime.Serialization;
41 using System.Security.Cryptography.X509Certificates;
42 using System.Text;
43 using System.Threading;
44
45 namespace System.Net 
46 {
47         [Serializable]
48         public class HttpWebRequest : WebRequest, ISerializable
49         {
50                 Uri requestUri;
51                 Uri actualUri;
52                 bool hostChanged;
53                 bool allowAutoRedirect = true;
54                 bool allowBuffering = true;
55                 X509CertificateCollection certificates;
56                 string connectionGroup;
57                 long contentLength = -1;
58                 HttpContinueDelegate continueDelegate;
59                 CookieContainer cookieContainer;
60                 ICredentials credentials;
61                 bool haveResponse;              
62                 bool haveRequest;
63                 bool requestSent;
64                 WebHeaderCollection webHeaders = new WebHeaderCollection (true);
65                 bool keepAlive = true;
66                 int maxAutoRedirect = 50;
67                 string mediaType = String.Empty;
68                 string method = "GET";
69                 string initialMethod = "GET";
70                 bool pipelined = true;
71                 bool preAuthenticate;
72                 bool usedPreAuth;
73                 Version version = HttpVersion.Version11;
74                 Version actualVersion;
75                 IWebProxy proxy;
76                 bool sendChunked;
77                 ServicePoint servicePoint;
78                 int timeout = 100000;
79                 
80                 WebConnectionStream writeStream;
81                 HttpWebResponse webResponse;
82                 WebAsyncResult asyncWrite;
83                 WebAsyncResult asyncRead;
84                 EventHandler abortHandler;
85                 bool aborted;
86                 bool gotRequestStream;
87                 int redirects;
88                 bool expectContinue;
89                 bool authCompleted;
90                 byte[] bodyBuffer;
91                 int bodyBufferLength;
92                 bool getResponseCalled;
93                 Exception saved_exc;
94                 object locker = new object ();
95 #if NET_1_1
96                 int maxResponseHeadersLength;
97                 static int defaultMaxResponseHeadersLength;
98                 int readWriteTimeout = 300000; // ms
99                 
100                 // Constructors
101                 static HttpWebRequest ()
102                 {
103                         NetConfig config = ConfigurationSettings.GetConfig ("system.net/settings") as NetConfig;
104                         defaultMaxResponseHeadersLength = 64 * 1024;
105                         if (config != null) {
106                                 int x = config.MaxResponseHeadersLength;
107                                 if (x != -1)
108                                         x *= 64;
109
110                                 defaultMaxResponseHeadersLength = x;
111                         }
112                 }
113 #endif
114                 
115                 internal HttpWebRequest (Uri uri) 
116                 {
117                         this.requestUri = uri;
118                         this.actualUri = uri;
119                         this.proxy = GlobalProxySelection.Select;
120                 }               
121                 
122                 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
123                 {
124                         SerializationInfo info = serializationInfo;
125
126                         requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
127                         actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
128                         allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
129                         allowBuffering = info.GetBoolean ("allowBuffering");
130                         certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
131                         connectionGroup = info.GetString ("connectionGroup");
132                         contentLength = info.GetInt64 ("contentLength");
133                         webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
134                         keepAlive = info.GetBoolean ("keepAlive");
135                         maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
136                         mediaType = info.GetString ("mediaType");
137                         method = info.GetString ("method");
138                         initialMethod = info.GetString ("initialMethod");
139                         pipelined = info.GetBoolean ("pipelined");
140                         version = (Version) info.GetValue ("version", typeof (Version));
141                         proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
142                         sendChunked = info.GetBoolean ("sendChunked");
143                         timeout = info.GetInt32 ("timeout");
144                         redirects = info.GetInt32 ("redirects");
145                 }
146                 
147                 // Properties
148                 
149                 public string Accept {
150                         get { return webHeaders ["Accept"]; }
151                         set {
152                                 CheckRequestStarted ();
153                                 webHeaders.RemoveAndAdd ("Accept", value);
154                         }
155                 }
156                 
157                 public Uri Address {
158                         get { return actualUri; }
159                 }
160                 
161                 public bool AllowAutoRedirect {
162                         get { return allowAutoRedirect; }
163                         set { this.allowAutoRedirect = value; }
164                 }
165                 
166                 public bool AllowWriteStreamBuffering {
167                         get { return allowBuffering; }
168                         set { allowBuffering = value; }
169                 }
170                 
171                 internal bool InternalAllowBuffering {
172                         get {
173                                 return (allowBuffering && (method != "HEAD" && method != "GET" &&
174                                                         method != "MKCOL" && method != "CONNECT"));
175                         }
176                 }
177                 
178                 public X509CertificateCollection ClientCertificates {
179                         get {
180                                 if (certificates == null)
181                                         certificates = new X509CertificateCollection ();
182
183                                 return certificates;
184                         }
185                 }
186                 
187                 public string Connection {
188                         get { return webHeaders ["Connection"]; }
189                         set {
190                                 CheckRequestStarted ();
191                                 string val = value;
192                                 if (val != null) 
193                                         val = val.Trim ().ToLower ();
194
195                                 if (val == null || val.Length == 0) {
196                                         webHeaders.RemoveInternal ("Connection");
197                                         return;
198                                 }
199
200                                 if (val == "keep-alive" || val == "close") 
201                                         throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
202
203                                 if (keepAlive && val.IndexOf ("keep-alive") == -1)
204                                         value = value + ", Keep-Alive";
205                                 
206                                 webHeaders.RemoveAndAdd ("Connection", value);
207                         }
208                 }               
209                 
210                 public override string ConnectionGroupName { 
211                         get { return connectionGroup; }
212                         set { connectionGroup = value; }
213                 }
214                 
215                 public override long ContentLength { 
216                         get { return contentLength; }
217                         set { 
218                                 CheckRequestStarted ();
219                                 if (value < 0)
220                                         throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
221                                         
222                                 contentLength = value;
223                         }
224                 }
225                 
226                 internal long InternalContentLength {
227                         set { contentLength = value; }
228                 }
229                 
230                 public override string ContentType { 
231                         get { return webHeaders ["Content-Type"]; }
232                         set {
233                                 if (value == null || value.Trim().Length == 0) {
234                                         webHeaders.RemoveInternal ("Content-Type");
235                                         return;
236                                 }
237                                 webHeaders.RemoveAndAdd ("Content-Type", value);
238                         }
239                 }
240                 
241                 public HttpContinueDelegate ContinueDelegate {
242                         get { return continueDelegate; }
243                         set { continueDelegate = value; }
244                 }
245                 
246                 public CookieContainer CookieContainer {
247                         get { return cookieContainer; }
248                         set { cookieContainer = value; }
249                 }
250                 
251                 public override ICredentials Credentials { 
252                         get { return credentials; }
253                         set { credentials = value; }
254                 }
255                 
256                 public string Expect {
257                         get { return webHeaders ["Expect"]; }
258                         set {
259                                 CheckRequestStarted ();
260                                 string val = value;
261                                 if (val != null)
262                                         val = val.Trim ().ToLower ();
263
264                                 if (val == null || val.Length == 0) {
265                                         webHeaders.RemoveInternal ("Expect");
266                                         return;
267                                 }
268
269                                 if (val == "100-continue")
270                                         throw new ArgumentException ("100-Continue cannot be set with this property.",
271                                                                      "value");
272                                 webHeaders.RemoveAndAdd ("Expect", value);
273                         }
274                 }
275                 
276                 public bool HaveResponse {
277                         get { return haveResponse; }
278                 }
279                 
280                 public override WebHeaderCollection Headers { 
281                         get { return webHeaders; }
282                         set {
283                                 CheckRequestStarted ();
284                                 WebHeaderCollection newHeaders = new WebHeaderCollection (true);
285                                 int count = value.Count;
286                                 for (int i = 0; i < count; i++) 
287                                         newHeaders.Add (value.GetKey (i), value.Get (i));
288
289                                 webHeaders = newHeaders;
290                         }
291                 }
292                 
293                 public DateTime IfModifiedSince {
294                         get { 
295                                 string str = webHeaders ["If-Modified-Since"];
296                                 if (str == null)
297                                         return DateTime.Now;
298                                 try {
299                                         return MonoHttpDate.Parse (str);
300                                 } catch (Exception) {
301                                         return DateTime.Now;
302                                 }
303                         }
304                         set {
305                                 CheckRequestStarted ();
306                                 // rfc-1123 pattern
307                                 webHeaders.SetInternal ("If-Modified-Since", 
308                                         value.ToUniversalTime ().ToString ("r", null));
309                                 // TODO: check last param when using different locale
310                         }
311                 }
312
313                 public bool KeepAlive {         
314                         get {
315                                 return keepAlive;
316                         }
317                         set {
318                                 keepAlive = value;
319                         }
320                 }
321                 
322                 public int MaximumAutomaticRedirections {
323                         get { return maxAutoRedirect; }
324                         set {
325                                 if (value <= 0)
326                                         throw new ArgumentException ("Must be > 0", "value");
327
328                                 maxAutoRedirect = value;
329                         }                       
330                 }
331
332 #if NET_1_1
333                 [MonoTODO ("Use this")]
334                 public int MaximumResponseHeadersLength {
335                         get { return maxResponseHeadersLength; }
336                         set { maxResponseHeadersLength = value; }
337                 }
338
339                 [MonoTODO ("Use this")]
340                 public static int DefaultMaximumResponseHeadersLength {
341                         get { return defaultMaxResponseHeadersLength; }
342                         set { defaultMaxResponseHeadersLength = value; }
343                 }
344
345                 public
346 #else
347                 internal
348 #endif
349                 int ReadWriteTimeout {
350                         get { return readWriteTimeout; }
351                         set {
352                                 if (requestSent)
353                                         throw new InvalidOperationException ("The request has already been sent.");
354
355                                 if (value < -1)
356                                         throw new ArgumentOutOfRangeException ("value", "Must be >= -1");
357
358                                 readWriteTimeout = value;
359                         }
360                 }
361                 
362                 public string MediaType {
363                         get { return mediaType; }
364                         set { 
365                                 mediaType = value;
366                         }
367                 }
368                 
369                 public override string Method { 
370                         get { return this.method; }
371                         set { 
372                                 if (value == null || value.Trim () == "")
373                                         throw new ArgumentException ("not a valid method");
374
375                                 method = value;
376                         }
377                 }
378                 
379                 public bool Pipelined {
380                         get { return pipelined; }
381                         set { pipelined = value; }
382                 }               
383                 
384                 public override bool PreAuthenticate { 
385                         get { return preAuthenticate; }
386                         set { preAuthenticate = value; }
387                 }
388                 
389                 public Version ProtocolVersion {
390                         get { return version; }
391                         set { 
392                                 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
393                                         throw new ArgumentException ("value");
394
395                                 version = value; 
396                         }
397                 }
398                 
399                 public override IWebProxy Proxy { 
400                         get { return proxy; }
401                         set { 
402                                 CheckRequestStarted ();
403                                 if (value == null)
404                                         throw new ArgumentNullException ("value");
405
406                                 proxy = value;
407                                 servicePoint = null; // we may need a new one
408                         }
409                 }
410                 
411                 public string Referer {
412                         get { return webHeaders ["Referer"]; }
413                         set {
414                                 CheckRequestStarted ();
415                                 if (value == null || value.Trim().Length == 0) {
416                                         webHeaders.RemoveInternal ("Referer");
417                                         return;
418                                 }
419                                 webHeaders.SetInternal ("Referer", value);
420                         }
421                 }
422
423                 public override Uri RequestUri { 
424                         get { return requestUri; }
425                 }
426                 
427                 public bool SendChunked {
428                         get { return sendChunked; }
429                         set {
430                                 CheckRequestStarted ();
431                                 sendChunked = value;
432                         }
433                 }
434                 
435                 public ServicePoint ServicePoint {
436                         get { return GetServicePoint (); }
437                 }
438                 
439                 public override int Timeout { 
440                         get { return timeout; }
441                         set {
442                                 if (value < -1)
443                                         throw new ArgumentOutOfRangeException ("value");
444
445                                 timeout = value;
446                         }
447                 }
448                 
449                 public string TransferEncoding {
450                         get { return webHeaders ["Transfer-Encoding"]; }
451                         set {
452                                 CheckRequestStarted ();
453                                 string val = value;
454                                 if (val != null)
455                                         val = val.Trim ().ToLower ();
456
457                                 if (val == null || val.Length == 0) {
458                                         webHeaders.RemoveInternal ("Transfer-Encoding");
459                                         return;
460                                 }
461
462                                 if (val == "chunked")
463                                         throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
464
465                                 if (!sendChunked)
466                                         throw new ArgumentException ("SendChunked must be True", "value");
467
468                                 webHeaders.RemoveAndAdd ("Transfer-Encoding", value);
469                         }
470                 }
471                 
472                 public string UserAgent {
473                         get { return webHeaders ["User-Agent"]; }
474                         set { webHeaders.SetInternal ("User-Agent", value); }
475                 }
476
477 #if NET_1_1
478                 bool unsafe_auth_blah;
479                 [MonoTODO]
480                 public bool UnsafeAuthenticatedConnectionSharing
481                 {
482                         get { return unsafe_auth_blah; }
483                         set { unsafe_auth_blah = value; }
484                 }
485 #endif
486
487                 internal bool GotRequestStream {
488                         get { return gotRequestStream; }
489                 }
490
491                 internal bool ExpectContinue {
492                         get { return expectContinue; }
493                         set { expectContinue = value; }
494                 }
495                 
496                 internal Uri AuthUri {
497                         get { return actualUri; }
498                 }
499                 
500                 internal bool ProxyQuery {
501                         get { return servicePoint.UsesProxy && !servicePoint.UseConnect; }
502                 }
503                 
504                 // Methods
505                 
506                 internal ServicePoint GetServicePoint ()
507                 {
508                         lock (locker) {
509                                 if (hostChanged || servicePoint == null) {
510                                         servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
511                                         hostChanged = false;
512                                 }
513                         }
514
515                         return servicePoint;
516                 }
517                 
518                 public void AddRange (int range)
519                 {
520                         AddRange ("bytes", range);
521                 }
522                 
523                 public void AddRange (int from, int to)
524                 {
525                         AddRange ("bytes", from, to);
526                 }
527                 
528                 public void AddRange (string rangeSpecifier, int range)
529                 {
530                         if (rangeSpecifier == null)
531                                 throw new ArgumentNullException ("rangeSpecifier");
532                         string value = webHeaders ["Range"];
533                         if (value == null || value.Length == 0) 
534                                 value = rangeSpecifier + "=";
535                         else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
536                                 value += ",";
537                         else
538                                 throw new InvalidOperationException ("rangeSpecifier");
539                         webHeaders.RemoveAndAdd ("Range", value + range + "-"); 
540                 }
541                 
542                 public void AddRange (string rangeSpecifier, int from, int to)
543                 {
544                         if (rangeSpecifier == null)
545                                 throw new ArgumentNullException ("rangeSpecifier");
546                         if (from < 0 || to < 0 || from > to)
547                                 throw new ArgumentOutOfRangeException ();                       
548                         string value = webHeaders ["Range"];
549                         if (value == null || value.Length == 0) 
550                                 value = rangeSpecifier + "=";
551                         else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
552                                 value += ",";
553                         else
554                                 throw new InvalidOperationException ("rangeSpecifier");
555                         webHeaders.RemoveAndAdd ("Range", value + from + "-" + to);     
556                 }
557 #if !NET_2_0
558                 public override int GetHashCode ()
559                 {
560                         return base.GetHashCode ();
561                 }
562 #endif
563                 
564                 void CommonChecks (bool putpost)
565                 {
566                         if (method == null)
567                                 throw new ProtocolViolationException ("Method is null.");
568
569                         if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))
570                                 throw new ProtocolViolationException ("Content-Length not set");
571
572                         string transferEncoding = TransferEncoding;
573                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
574                                 throw new ProtocolViolationException ("SendChunked should be true.");
575                 }
576
577                 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
578                 {
579                         if (aborted)
580                                 throw new WebException ("The request was previosly aborted.");
581
582                         bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD");
583                         if (method == null || !send)
584                                 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
585
586                         CommonChecks (send);
587                         
588                         lock (locker)
589                         {
590                                 if (asyncWrite != null) {
591                                         throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
592                                                                 "method while a previous call is still in progress.");
593                                 }
594         
595                                 asyncWrite = new WebAsyncResult (this, callback, state);
596                                 initialMethod = method;
597                                 if (haveRequest) {
598                                         if (writeStream != null) {
599                                                 asyncWrite.SetCompleted (true, writeStream);
600                                                 asyncWrite.DoCallback ();
601                                                 return asyncWrite;
602                                         }
603                                 }
604                                 
605                                 gotRequestStream = true;
606                                 WebAsyncResult result = asyncWrite;
607                                 if (!requestSent) {
608                                         requestSent = true;
609                                         redirects = 0;
610                                         servicePoint = GetServicePoint ();
611                                         abortHandler = servicePoint.SendRequest (this, connectionGroup);
612                                 }
613                                 return result;
614                         }
615                 }
616
617                 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
618                 {
619                         if (asyncResult == null)
620                                 throw new ArgumentNullException ("asyncResult");
621
622                         WebAsyncResult result = asyncResult as WebAsyncResult;
623                         if (result == null)
624                                 throw new ArgumentException ("Invalid IAsyncResult");
625
626                         asyncWrite = result;
627                         result.WaitUntilComplete ();
628
629                         Exception e = result.Exception;
630                         if (e != null)
631                                 throw e;
632
633                         return result.WriteStream;
634                 }
635                 
636                 public override Stream GetRequestStream()
637                 {
638                         IAsyncResult asyncResult = BeginGetRequestStream (null, null);
639                         asyncWrite = (WebAsyncResult) asyncResult;
640                         if (!asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
641                                 Abort ();
642                                 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
643                         }
644
645                         return EndGetRequestStream (asyncResult);
646                 }
647
648                 void CheckIfForceWrite ()
649                 {
650                         if (writeStream == null || contentLength < 0 || !InternalAllowBuffering)
651                                 return;
652
653                         // This will write the POST/PUT if the write stream already has the expected
654                         // amount of bytes in it (ContentLength) (bug #77753).
655                         if (writeStream.WriteBufferLength == contentLength)
656                                 writeStream.WriteRequest ();
657                 }
658
659                 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
660                 {
661                         bool send = (method == "PUT" || method == "POST");
662                         if (send) {
663                                 if ((!KeepAlive || (ContentLength == -1 && !SendChunked)) && !AllowWriteStreamBuffering)
664                                         throw new ProtocolViolationException ("Content-Length not set");
665                         }
666
667                         CommonChecks (send);
668                         Monitor.Enter (this);
669                         getResponseCalled = true;
670                         if (asyncRead != null && !haveResponse) {
671                                 Monitor.Exit (this);
672                                 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
673                                                         "method while a previous call is still in progress.");
674                         }
675
676                         CheckIfForceWrite ();
677                         asyncRead = new WebAsyncResult (this, callback, state);
678                         WebAsyncResult aread = asyncRead;
679                         initialMethod = method;
680                         if (haveResponse) {
681                                 if (webResponse != null) {
682                                         Exception saved = saved_exc;
683                                         Monitor.Exit (this);
684                                         if (saved == null) {
685                                                 aread.SetCompleted (true, webResponse);
686                                         } else {
687                                                 aread.SetCompleted (true, saved);
688                                         }
689                                         aread.DoCallback ();
690                                         return aread;
691                                 }
692                         }
693                         
694                         if (!requestSent) {
695                                 requestSent = true;
696                                 redirects = 0;
697                                 servicePoint = GetServicePoint ();
698                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
699                         }
700
701                         Monitor.Exit (this);
702                         return aread;
703                 }
704                 
705                 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
706                 {
707                         if (asyncResult == null)
708                                 throw new ArgumentNullException ("asyncResult");
709
710                         WebAsyncResult result = asyncResult as WebAsyncResult;
711                         if (result == null)
712                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
713
714                         if (!result.WaitUntilComplete (timeout, false)) {
715                                 Abort ();
716                                 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
717                         }
718
719                         if (result.GotException)
720                                 throw result.Exception;
721
722                         return result.Response;
723                 }
724
725                 public override WebResponse GetResponse()
726                 {
727                         WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
728                         return EndGetResponse (result);
729                 }
730                 
731                 public override void Abort ()
732                 {
733                         haveResponse = true;
734                         aborted = true;
735                         if (asyncWrite != null) {
736                                 WebAsyncResult r = asyncWrite;
737                                 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
738                                 r.SetCompleted (false, wexc);
739                                 r.DoCallback ();
740                                 asyncWrite = null;
741                         }                       
742
743                         if (asyncRead != null) {
744                                 WebAsyncResult r = asyncRead;
745                                 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
746                                 r.SetCompleted (false, wexc);
747                                 r.DoCallback ();
748                                 asyncRead = null;
749                         }                       
750
751                         if (abortHandler != null) {
752                                 try {
753                                         abortHandler (this, EventArgs.Empty);
754                                 } catch (Exception) {}
755                                 abortHandler = null;
756                         }
757
758                         if (writeStream != null) {
759                                 try {
760                                         writeStream.Close ();
761                                         writeStream = null;
762                                 } catch {}
763                         }
764
765                         if (webResponse != null) {
766                                 try {
767                                         webResponse.Close ();
768                                         webResponse = null;
769                                 } catch {}
770                         }
771                 }               
772                 
773                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
774                                                   StreamingContext streamingContext)
775                 {
776                         SerializationInfo info = serializationInfo;
777
778                         info.AddValue ("requestUri", requestUri, typeof (Uri));
779                         info.AddValue ("actualUri", actualUri, typeof (Uri));
780                         info.AddValue ("allowAutoRedirect", allowAutoRedirect);
781                         info.AddValue ("allowBuffering", allowBuffering);
782                         info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
783                         info.AddValue ("connectionGroup", connectionGroup);
784                         info.AddValue ("contentLength", contentLength);
785                         info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
786                         info.AddValue ("keepAlive", keepAlive);
787                         info.AddValue ("maxAutoRedirect", maxAutoRedirect);
788                         info.AddValue ("mediaType", mediaType);
789                         info.AddValue ("method", method);
790                         info.AddValue ("initialMethod", initialMethod);
791                         info.AddValue ("pipelined", pipelined);
792                         info.AddValue ("version", version, typeof (Version));
793                         info.AddValue ("proxy", proxy, typeof (IWebProxy));
794                         info.AddValue ("sendChunked", sendChunked);
795                         info.AddValue ("timeout", timeout);
796                         info.AddValue ("redirects", redirects);
797                 }
798                 
799                 void CheckRequestStarted () 
800                 {
801                         if (requestSent)
802                                 throw new InvalidOperationException ("request started");
803                 }
804
805                 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
806                 {
807                         if (continueDelegate != null)
808                                 continueDelegate (statusCode, headers);
809                 }
810                 
811                 bool Redirect (WebAsyncResult result, HttpStatusCode code)
812                 {
813                         redirects++;
814                         Exception e = null;
815                         string uriString = null;
816
817                         switch (code) {
818                         case HttpStatusCode.Ambiguous: // 300
819                                 e = new WebException ("Ambiguous redirect.");
820                                 break;
821                         case HttpStatusCode.MovedPermanently: // 301
822                         case HttpStatusCode.Redirect: // 302
823                         case HttpStatusCode.TemporaryRedirect: // 307
824                                 /* MS follows the redirect for POST too
825                                 if (method != "GET" && method != "HEAD") // 10.3
826                                         return false;
827                                 */
828
829                                 uriString = webResponse.Headers ["Location"];
830                                 break;
831                         case HttpStatusCode.SeeOther: //303
832                                 method = "GET";
833                                 uriString = webResponse.Headers ["Location"];
834                                 break;
835                         case HttpStatusCode.NotModified: // 304
836                                 return false;
837                         case HttpStatusCode.UseProxy: // 305
838                                 e = new NotImplementedException ("Proxy support not available.");
839                                 break;
840                         case HttpStatusCode.Unused: // 306
841                         default:
842                                 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
843                                 break;
844                         }
845
846                         if (e != null)
847                                 throw e;
848
849                         if (uriString == null)
850                                 throw new WebException ("No Location header found for " + (int) code,
851                                                         WebExceptionStatus.ProtocolError);
852
853                         Uri prev = actualUri;
854                         try {
855                                 actualUri = new Uri (actualUri, uriString);
856                         } catch (Exception) {
857                                 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
858                                                                         uriString, (int) code),
859                                                                         WebExceptionStatus.ProtocolError);
860                         }
861
862                         hostChanged = (actualUri.Scheme != prev.Scheme || actualUri.Host != prev.Host ||
863                                         actualUri.Port != prev.Port);
864                         return true;
865                 }
866
867                 string GetHeaders ()
868                 {
869                         bool continue100 = false;
870                         if (contentLength != -1) {
871                                 if (contentLength > 0)
872                                         continue100 = true;
873                                 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
874                                 webHeaders.RemoveInternal ("Transfer-Encoding");
875                         } else if (sendChunked) {
876                                 continue100 = true;
877                                 webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
878                                 webHeaders.RemoveInternal ("Content-Length");
879                         }
880
881                         if (actualVersion == HttpVersion.Version11 && continue100 &&
882                             servicePoint.SendContinue) { // RFC2616 8.2.3
883                                 webHeaders.RemoveAndAdd ("Expect" , "100-continue");
884                                 expectContinue = true;
885                         } else {
886                                 webHeaders.RemoveInternal ("Expect");
887                                 expectContinue = false;
888                         }
889
890                         string connectionHeader = (ProxyQuery) ? "Proxy-Connection" : "Connection";
891                         webHeaders.RemoveInternal ((!ProxyQuery) ? "Proxy-Connection" : "Connection");
892                         bool spoint10 = (servicePoint.ProtocolVersion == null ||
893                                          servicePoint.ProtocolVersion == HttpVersion.Version10);
894
895                         if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
896                                 webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
897                         } else if (!keepAlive && version == HttpVersion.Version11) {
898                                 webHeaders.RemoveAndAdd (connectionHeader, "close");
899                         }
900
901                         webHeaders.SetInternal ("Host", actualUri.Authority);
902                         if (cookieContainer != null) {
903                                 string cookieHeader = cookieContainer.GetCookieHeader (requestUri);
904                                 if (cookieHeader != "")
905                                         webHeaders.SetInternal ("Cookie", cookieHeader);
906                         }
907
908                         if (!usedPreAuth && preAuthenticate)
909                                 DoPreAuthenticate ();
910
911                         return webHeaders.ToString ();
912                 }
913
914                 void DoPreAuthenticate ()
915                 {
916                         webHeaders.RemoveInternal ("Proxy-Authorization");
917                         webHeaders.RemoveInternal ("Authorization");
918                         bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
919                         ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
920                         Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
921                         if (auth == null)
922                                 return;
923
924                         string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
925                         webHeaders [authHeader] = auth.Message;
926                         usedPreAuth = true;
927                 }
928                 
929                 internal void SetWriteStreamError (WebExceptionStatus status)
930                 {
931                         if (aborted)
932                                 return;
933
934                         WebAsyncResult r = asyncWrite;
935                         if (r == null)
936                                 r = asyncRead;
937
938                         if (r != null) {
939                                 r.SetCompleted (false, new WebException ("Error: " + status, status));
940                                 r.DoCallback ();
941                         }
942                 }
943
944                 internal void SendRequestHeaders ()
945                 {
946                         StringBuilder req = new StringBuilder ();
947                         string query;
948                         if (!ProxyQuery) {
949                                 query = actualUri.PathAndQuery;
950                         } else if (actualUri.IsDefaultPort) {
951                                 query = String.Format ("{0}://{1}{2}",  actualUri.Scheme,
952                                                                         actualUri.Host,
953                                                                         actualUri.PathAndQuery);
954                         } else {
955                                 query = String.Format ("{0}://{1}:{2}{3}", actualUri.Scheme,
956                                                                            actualUri.Host,
957                                                                            actualUri.Port,
958                                                                            actualUri.PathAndQuery);
959                         }
960                         
961                         if (servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
962                                 actualVersion = servicePoint.ProtocolVersion;
963                         } else {
964                                 actualVersion = version;
965                         }
966
967                         req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
968                                                                 actualVersion.Major, actualVersion.Minor);
969                         req.Append (GetHeaders ());
970                         string reqstr = req.ToString ();
971                         byte [] bytes = Encoding.UTF8.GetBytes (reqstr);
972                         try {
973                                 writeStream.SetHeaders (bytes, 0, bytes.Length);
974                         } catch (WebException wexc) {
975                                 SetWriteStreamError (wexc.Status);
976                         } catch (Exception) {
977                                 SetWriteStreamError (WebExceptionStatus.SendFailure);
978                         }
979                 }
980
981                 internal void SetWriteStream (WebConnectionStream stream)
982                 {
983                         if (aborted)
984                                 return;
985                         
986                         writeStream = stream;
987                         if (bodyBuffer != null) {
988                                 webHeaders.RemoveInternal ("Transfer-Encoding");
989                                 contentLength = bodyBufferLength;
990                                 writeStream.SendChunked = false;
991                         }
992
993                         SendRequestHeaders ();
994
995                         haveRequest = true;
996                         
997                         if (bodyBuffer != null) {
998                                 // The body has been written and buffered. The request "user"
999                                 // won't write it again, so we must do it.
1000                                 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1001                                 bodyBuffer = null;
1002                                 writeStream.Close ();
1003                         } else if (method == "PUT" || method == "POST") {
1004                                 if (getResponseCalled && !writeStream.RequestWritten)
1005                                         writeStream.WriteRequest ();
1006                         }
1007
1008                         if (asyncWrite != null) {
1009                                 asyncWrite.SetCompleted (false, stream);
1010                                 asyncWrite.DoCallback ();
1011                                 asyncWrite = null;
1012                         }
1013                 }
1014
1015                 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1016                 {
1017                         if (aborted)
1018                                 return;
1019                         string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1020                         WebAsyncResult r = asyncRead;
1021                         if (r == null)
1022                                 r = asyncWrite;
1023
1024                         if (r != null) {
1025                                 WebException wexc;
1026                                 if (e is WebException) {
1027                                         wexc = (WebException) e;
1028                                 } else {
1029                                         wexc = new WebException (msg, e, status, null); 
1030                                 }
1031                                 r.SetCompleted (false, wexc);
1032                                 r.DoCallback ();
1033                                 asyncRead = null;
1034                                 asyncWrite = null;
1035                         }
1036                 }
1037
1038                 void CheckSendError (WebConnectionData data)
1039                 {
1040                         // Got here, but no one called GetResponse
1041                         int status = data.StatusCode;
1042                         if (status < 400 || status == 401 || status == 407)
1043                                 return;
1044
1045                         if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1046                                 // The request has not been completely sent and we got here!
1047                                 // We should probably just close and cause an error in any case,
1048                                 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse); 
1049                                 webResponse.ReadAll ();
1050                         }
1051                 }
1052
1053                 internal void SetResponseData (WebConnectionData data)
1054                 {
1055                         if (aborted) {
1056                                 if (data.stream != null)
1057                                         data.stream.Close ();
1058                                 return;
1059                         }
1060
1061                         WebException wexc = null;
1062                         try {
1063                                 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1064                                 haveResponse = true;
1065                         } catch (Exception e) {
1066                                 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null); 
1067                                 if (data.stream != null)
1068                                         data.stream.Close ();
1069                         }
1070
1071                         if (wexc == null && (method == "POST" || method == "PUT")) {
1072                                 lock (locker) {
1073                                         CheckSendError (data);
1074                                         if (saved_exc != null)
1075                                                 wexc = (WebException) saved_exc;
1076                                 }
1077                         }
1078
1079                         WebAsyncResult r = asyncRead;
1080                         if (r != null) {
1081                                 if (wexc != null) {
1082                                         r.SetCompleted (false, wexc);
1083                                         r.DoCallback ();
1084                                         return;
1085                                 }
1086
1087                                 bool redirected;
1088                                 try {
1089                                         redirected = CheckFinalStatus (r);
1090                                         if (!redirected) {
1091                                                 r.SetCompleted (false, webResponse);
1092                                                 r.DoCallback ();
1093                                         } else {
1094                                                 if (webResponse != null) {
1095                                                         webResponse.Close ();
1096                                                         webResponse = null;
1097                                                 }
1098                                                 haveResponse = false;
1099                                                 webResponse = null;
1100                                                 r.Reset ();
1101                                                 servicePoint = GetServicePoint ();
1102                                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1103                                         }
1104                                 } catch (WebException wexc2) {
1105                                         r.SetCompleted (false, wexc2);
1106                                         r.DoCallback ();
1107                                         return;
1108                                 } catch (Exception ex) {
1109                                         wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null); 
1110                                         r.SetCompleted (false, wexc);
1111                                         r.DoCallback ();
1112                                         return;
1113                                 }
1114                         }
1115                 }
1116
1117                 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1118                 {
1119                         authCompleted = false;
1120                         if (code == HttpStatusCode.Unauthorized && credentials == null)
1121                                 return false;
1122
1123                         bool isProxy = (code == HttpStatusCode.ProxyAuthenticationRequired);
1124                         if (isProxy && (proxy == null || proxy.Credentials == null))
1125                                 return false;
1126
1127                         string authHeader = response.Headers [(isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate"];
1128                         if (authHeader == null)
1129                                 return false;
1130
1131                         ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
1132                         Authorization auth = AuthenticationManager.Authenticate (authHeader, this, creds);
1133                         if (auth == null)
1134                                 return false;
1135
1136                         webHeaders [(isProxy) ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1137                         authCompleted = auth.Complete;
1138                         return true;
1139                 }
1140
1141                 // Returns true if redirected
1142                 bool CheckFinalStatus (WebAsyncResult result)
1143                 {
1144                         if (result.GotException)
1145                                 throw result.Exception;
1146
1147                         Exception throwMe = result.Exception;
1148                         bodyBuffer = null;
1149
1150                         HttpWebResponse resp = result.Response;
1151                         WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1152                         HttpStatusCode code = 0;
1153                         if (throwMe == null && webResponse != null) {
1154                                 code = webResponse.StatusCode;
1155                                 if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
1156                                      (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) {
1157                                         if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1158                                                 // Keep the written body, so it can be rewritten in the retry
1159                                                 if (InternalAllowBuffering) {
1160                                                         bodyBuffer = writeStream.WriteBuffer;
1161                                                         bodyBufferLength = writeStream.WriteBufferLength;
1162                                                         webResponse.Close ();
1163                                                         return true;
1164                                                 } else if (method != "PUT" && method != "POST") {
1165                                                         webResponse.Close ();
1166                                                         return true;
1167                                                 }
1168                                                 
1169                                                 writeStream.InternalClose ();
1170                                                 writeStream = null;
1171                                                 webResponse.Close ();
1172                                                 webResponse = null;
1173
1174                                                 throw new WebException ("This request requires buffering " +
1175                                                                         "of data for authentication or " +
1176                                                                         "redirection to be sucessful.");
1177                                         }
1178                                 }
1179
1180                                 if ((int) code >= 400) {
1181                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1182                                                                     (int) code, webResponse.StatusDescription);
1183                                         throwMe = new WebException (err, null, protoError, webResponse);
1184                                         webResponse.ReadAll ();
1185                                 } else if ((int) code == 304 && allowAutoRedirect) {
1186                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1187                                                                     (int) code, webResponse.StatusDescription);
1188                                         throwMe = new WebException (err, null, protoError, webResponse);
1189                                 } else if ((int) code >= 300 && allowAutoRedirect && redirects > maxAutoRedirect) {
1190                                         throwMe = new WebException ("Max. redirections exceeded.", null,
1191                                                                     protoError, webResponse);
1192                                         webResponse.ReadAll ();
1193                                 }
1194                         }
1195
1196                         if (throwMe == null) {
1197                                 bool b = false;
1198                                 int c = (int) code;
1199                                 if (allowAutoRedirect && c >= 300) {
1200                                         if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1201                                                 bodyBuffer = writeStream.WriteBuffer;
1202                                                 bodyBufferLength = writeStream.WriteBufferLength;
1203                                         }
1204                                         b = Redirect (result, code);
1205                                 }
1206
1207                                 if (resp != null && c >= 300 && c != 304)
1208                                         resp.ReadAll ();
1209
1210                                 return b;
1211                         }
1212
1213                         if (writeStream != null) {
1214                                 writeStream.InternalClose ();
1215                                 writeStream = null;
1216                         }
1217
1218                         webResponse = null;
1219
1220                         throw throwMe;
1221                 }
1222         }
1223 }
1224