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