2004-07-09 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.SetInternal ("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.SetInternal ("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                                 CheckRequestStarted ();
230                                 if (value == null || value.Trim().Length == 0) {
231                                         webHeaders.RemoveInternal ("Content-Type");
232                                         return;
233                                 }
234                                 webHeaders.SetInternal ("Content-Type", value);
235                         }
236                 }
237                 
238                 public HttpContinueDelegate ContinueDelegate {
239                         get { return continueDelegate; }
240                         set { continueDelegate = value; }
241                 }
242                 
243                 public CookieContainer CookieContainer {
244                         get { return cookieContainer; }
245                         set { cookieContainer = value; }
246                 }
247                 
248                 public override ICredentials Credentials { 
249                         get { return credentials; }
250                         set { credentials = value; }
251                 }
252                 
253                 public string Expect {
254                         get { return webHeaders ["Expect"]; }
255                         set {
256                                 CheckRequestStarted ();
257                                 string val = value;
258                                 if (val != null)
259                                         val = val.Trim ().ToLower ();
260
261                                 if (val == null || val.Length == 0) {
262                                         webHeaders.RemoveInternal ("Expect");
263                                         return;
264                                 }
265
266                                 if (val == "100-continue")
267                                         throw new ArgumentException ("100-Continue cannot be set with this property.",
268                                                                      "value");
269                                 webHeaders.SetInternal ("Expect", value);
270                         }
271                 }
272                 
273                 public bool HaveResponse {
274                         get { return haveResponse; }
275                 }
276                 
277                 public override WebHeaderCollection Headers { 
278                         get { return webHeaders; }
279                         set {
280                                 CheckRequestStarted ();
281                                 WebHeaderCollection newHeaders = new WebHeaderCollection (true);
282                                 int count = value.Count;
283                                 for (int i = 0; i < count; i++) 
284                                         newHeaders.Add (value.GetKey (i), value.Get (i));
285
286                                 webHeaders = newHeaders;
287                         }
288                 }
289                 
290                 public DateTime IfModifiedSince {
291                         get { 
292                                 string str = webHeaders ["If-Modified-Since"];
293                                 if (str == null)
294                                         return DateTime.Now;
295                                 try {
296                                         return MonoHttpDate.Parse (str);
297                                 } catch (Exception) {
298                                         return DateTime.Now;
299                                 }
300                         }
301                         set {
302                                 CheckRequestStarted ();
303                                 // rfc-1123 pattern
304                                 webHeaders.SetInternal ("If-Modified-Since", 
305                                         value.ToUniversalTime ().ToString ("r", null));
306                                 // TODO: check last param when using different locale
307                         }
308                 }
309
310                 public bool KeepAlive {         
311                         get {
312                                 return keepAlive;
313                         }
314                         set {
315                                 keepAlive = value;
316                         }
317                 }
318                 
319                 public int MaximumAutomaticRedirections {
320                         get { return maxAutoRedirect; }
321                         set {
322                                 if (value <= 0)
323                                         throw new ArgumentException ("Must be > 0", "value");
324
325                                 maxAutoRedirect = value;
326                         }                       
327                 }
328
329 #if NET_1_1
330                 [MonoTODO ("Use this")]
331                 public int MaximumResponseHeadersLength {
332                         get { return maxResponseHeadersLength; }
333                         set { maxResponseHeadersLength = value; }
334                 }
335
336                 [MonoTODO ("Use this")]
337                 public static int DefaultMaximumResponseHeadersLength {
338                         get { return defaultMaxResponseHeadersLength; }
339                         set { defaultMaxResponseHeadersLength = value; }
340                 }
341
342                 [MonoTODO ("Use this")]
343                 public int ReadWriteTimeout {
344                         get { return readWriteTimeout; }
345                         set { readWriteTimeout = value; }
346                 }
347 #endif
348                 
349                 public string MediaType {
350                         get { return mediaType; }
351                         set { 
352                                 mediaType = value;
353                         }
354                 }
355                 
356                 public override string Method { 
357                         get { return this.method; }
358                         set { 
359                                 if (value == null || value.Trim () == "")
360                                         throw new ArgumentException ("not a valid method");
361
362                                 method = value;
363                         }
364                 }
365                 
366                 public bool Pipelined {
367                         get { return pipelined; }
368                         set { pipelined = value; }
369                 }               
370                 
371                 public override bool PreAuthenticate { 
372                         get { return preAuthenticate; }
373                         set { preAuthenticate = value; }
374                 }
375                 
376                 public Version ProtocolVersion {
377                         get { return version; }
378                         set { 
379                                 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
380                                         throw new ArgumentException ("value");
381
382                                 version = value; 
383                         }
384                 }
385                 
386                 public override IWebProxy Proxy { 
387                         get { return proxy; }
388                         set { 
389                                 CheckRequestStarted ();
390                                 if (value == null)
391                                         throw new ArgumentNullException ("value");
392
393                                 proxy = value;
394                                 servicePoint = null; // we may need a new one
395                         }
396                 }
397                 
398                 public string Referer {
399                         get { return webHeaders ["Referer"]; }
400                         set {
401                                 CheckRequestStarted ();
402                                 if (value == null || value.Trim().Length == 0) {
403                                         webHeaders.RemoveInternal ("Referer");
404                                         return;
405                                 }
406                                 webHeaders.SetInternal ("Referer", value);
407                         }
408                 }
409
410                 public override Uri RequestUri { 
411                         get { return requestUri; }
412                 }
413                 
414                 public bool SendChunked {
415                         get { return sendChunked; }
416                         set {
417                                 CheckRequestStarted ();
418                                 sendChunked = value;
419                         }
420                 }
421                 
422                 public ServicePoint ServicePoint {
423                         get { return GetServicePoint (); }
424                 }
425                 
426                 public override int Timeout { 
427                         get { return timeout; }
428                         set {
429                                 if (value < -1)
430                                         throw new ArgumentOutOfRangeException ("value");
431
432                                 timeout = value;
433                         }
434                 }
435                 
436                 public string TransferEncoding {
437                         get { return webHeaders ["Transfer-Encoding"]; }
438                         set {
439                                 CheckRequestStarted ();
440                                 string val = value;
441                                 if (val != null)
442                                         val = val.Trim ().ToLower ();
443
444                                 if (val == null || val.Length == 0) {
445                                         webHeaders.RemoveInternal ("Transfer-Encoding");
446                                         return;
447                                 }
448
449                                 if (val == "chunked")
450                                         throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
451
452                                 if (!sendChunked)
453                                         throw new ArgumentException ("SendChunked must be True", "value");
454
455                                 webHeaders.SetInternal ("Transfer-Encoding", value);
456                         }
457                 }
458                 
459                 public string UserAgent {
460                         get { return webHeaders ["User-Agent"]; }
461                         set { webHeaders.SetInternal ("User-Agent", value); }
462                 }
463
464 #if NET_1_1
465                 [MonoTODO]
466                 public bool UnsafeAuthenticatedConnectionSharing
467                 {
468                         get { throw new NotImplementedException (); }
469                         set { throw new NotImplementedException (); }
470                 }
471 #endif
472
473                 internal bool GotRequestStream {
474                         get { return gotRequestStream; }
475                 }
476
477                 internal bool ExpectContinue {
478                         get { return expectContinue; }
479                         set { expectContinue = value; }
480                 }
481                 
482                 internal Uri AuthUri {
483                         get { return actualUri; }
484                 }
485                 
486                 internal bool ProxyQuery {
487                         get { return servicePoint.UsesProxy; }
488                 }
489                 
490                 // Methods
491                 
492                 internal ServicePoint GetServicePoint ()
493                 {
494                         if (!hostChanged && servicePoint != null)
495                                 return servicePoint;
496
497                         lock (this) {
498                                 if (hostChanged || servicePoint == null) {
499                                         servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
500                                         hostChanged = false;
501                                 }
502                         }
503
504                         return servicePoint;
505                 }
506                 
507                 public void AddRange (int range)
508                 {
509                         AddRange ("bytes", range);
510                 }
511                 
512                 public void AddRange (int from, int to)
513                 {
514                         AddRange ("bytes", from, to);
515                 }
516                 
517                 public void AddRange (string rangeSpecifier, int range)
518                 {
519                         if (rangeSpecifier == null)
520                                 throw new ArgumentNullException ("rangeSpecifier");
521                         string value = webHeaders ["Range"];
522                         if (value == null || value.Length == 0) 
523                                 value = rangeSpecifier + "=";
524                         else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
525                                 value += ",";
526                         else
527                                 throw new InvalidOperationException ("rangeSpecifier");
528                         webHeaders.SetInternal ("Range", value + range + "-");  
529                 }
530                 
531                 public void AddRange (string rangeSpecifier, int from, int to)
532                 {
533                         if (rangeSpecifier == null)
534                                 throw new ArgumentNullException ("rangeSpecifier");
535                         if (from < 0 || to < 0 || from > to)
536                                 throw new ArgumentOutOfRangeException ();                       
537                         string value = webHeaders ["Range"];
538                         if (value == null || value.Length == 0) 
539                                 value = rangeSpecifier + "=";
540                         else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
541                                 value += ",";
542                         else
543                                 throw new InvalidOperationException ("rangeSpecifier");
544                         webHeaders.SetInternal ("Range", value + from + "-" + to);      
545                 }
546                 
547                 public override int GetHashCode ()
548                 {
549                         return base.GetHashCode ();
550                 }
551                 
552                 void CommonChecks (bool putpost)
553                 {
554                         if (method == null)
555                                 throw new ProtocolViolationException ("Method is null.");
556
557                         if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))
558                                 throw new ProtocolViolationException ("Content-Length not set");
559
560                         string transferEncoding = TransferEncoding;
561                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
562                                 throw new ProtocolViolationException ("SendChunked should be true.");
563                 }
564
565                 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
566                 {
567                         if (aborted)
568                                 throw new WebException ("The request was previosly aborted.");
569
570                         bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD");
571                         if (method == null || !send)
572                                 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
573
574                         CommonChecks (send);
575                         
576                         lock (this)
577                         {
578                                 if (asyncWrite != null) {
579                                         throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
580                                                                 "method while a previous call is still in progress.");
581                                 }
582         
583                                 asyncWrite = new WebAsyncResult (this, callback, state);
584                                 initialMethod = method;
585                                 if (haveRequest) {
586                                         if (writeStream != null) {
587                                                 asyncWrite.SetCompleted (true, writeStream);
588                                                 asyncWrite.DoCallback ();
589                                                 return asyncWrite;
590                                         }
591                                 }
592                                 
593                                 gotRequestStream = true;
594                                 WebAsyncResult result = asyncWrite;
595                                 if (!requestSent) {
596                                         requestSent = true;
597                                         servicePoint = GetServicePoint ();
598                                         abortHandler = servicePoint.SendRequest (this, connectionGroup);
599                                 }
600                                 return result;
601                         }
602                 }
603
604                 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
605                 {
606                         if (asyncResult == null)
607                                 throw new ArgumentNullException ("asyncResult");
608
609                         WebAsyncResult result = asyncResult as WebAsyncResult;
610                         if (result == null)
611                                 throw new ArgumentException ("Invalid IAsyncResult");
612
613                         asyncWrite = result;
614                         result.WaitUntilComplete ();
615
616                         Exception e = result.Exception;
617                         if (e != null)
618                                 throw e;
619
620                         return result.WriteStream;
621                 }
622                 
623                 public override Stream GetRequestStream()
624                 {
625                         IAsyncResult asyncResult = BeginGetRequestStream (null, null);
626                         asyncWrite = (WebAsyncResult) asyncResult;
627                         if (!asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
628                                 Abort ();
629                                 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
630                         }
631
632                         return EndGetRequestStream (asyncResult);
633                 }
634                 
635                 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
636                 {
637                         bool send = (method == "PUT" || method == "POST");
638                         if (send) {
639                                 if ((!KeepAlive || (ContentLength == -1 && !SendChunked)) && !AllowWriteStreamBuffering)
640                                         throw new ProtocolViolationException ("Content-Length not set");
641                         }
642
643                         CommonChecks (send);
644                         Monitor.Enter (this);
645                         if (asyncRead != null && !haveResponse) {
646                                 Monitor.Exit (this);
647                                 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
648                                                         "method while a previous call is still in progress.");
649                         }
650
651                         asyncRead = new WebAsyncResult (this, callback, state);
652                         initialMethod = method;
653                         if (haveResponse) {
654                                 if (webResponse != null) {
655                                         Monitor.Exit (this);
656                                         asyncRead.SetCompleted (true, webResponse);
657                                         asyncRead.DoCallback ();
658                                         return asyncRead;
659                                 }
660                         }
661                         
662                         if (!requestSent) {
663                                 requestSent = true;
664                                 servicePoint = GetServicePoint ();
665                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
666                         }
667
668                         Monitor.Exit (this);
669                         return asyncRead;
670                 }
671                 
672                 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
673                 {
674                         if (asyncResult == null)
675                                 throw new ArgumentNullException ("asyncResult");
676
677                         WebAsyncResult result = asyncResult as WebAsyncResult;
678                         if (result == null)
679                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
680
681                         redirects = 0;
682                         bool redirected = false;
683                         asyncRead = result;
684                         do {
685                                 if (redirected) {
686                                         haveResponse = false;
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                         string host = actualUri.Host;
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.Host != host);
842                         return true;
843                 }
844
845                 string GetHeaders ()
846                 {
847                         bool continue100 = false;
848                         if (gotRequestStream && contentLength != -1) {
849                                 continue100 = true;
850                                 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
851                                 webHeaders.RemoveInternal ("Transfer-Encoding");
852                         } else if (sendChunked) {
853                                 continue100 = true;
854                                 webHeaders.SetInternal ("Transfer-Encoding", "chunked");
855                                 webHeaders.RemoveInternal ("Content-Length");
856                         }
857
858                         if (actualVersion == HttpVersion.Version11 && continue100 &&
859                             servicePoint.SendContinue) { // RFC2616 8.2.3
860                                 webHeaders.SetInternal ("Expect" , "100-continue");
861                                 expectContinue = true;
862                         } else {
863                                 webHeaders.RemoveInternal ("Expect");
864                                 expectContinue = false;
865                         }
866
867                         string connectionHeader = (ProxyQuery) ? "Proxy-Connection" : "Connection";
868                         webHeaders.RemoveInternal ((!ProxyQuery) ? "Proxy-Connection" : "Connection");
869                         bool spoint10 = (servicePoint.ProtocolVersion == null ||
870                                          servicePoint.ProtocolVersion == HttpVersion.Version10);
871
872                         if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
873                                 webHeaders.SetInternal (connectionHeader, "keep-alive");
874                         } else if (!keepAlive && version == HttpVersion.Version11) {
875                                 webHeaders.SetInternal (connectionHeader, "close");
876                         }
877
878                         webHeaders.SetInternal ("Host", actualUri.Authority);
879                         if (cookieContainer != null) {
880                                 string cookieHeader = cookieContainer.GetCookieHeader (requestUri);
881                                 if (cookieHeader != "")
882                                         webHeaders.SetInternal ("Cookie", cookieHeader);
883                         }
884
885                         if (!usedPreAuth && preAuthenticate) {
886                                 DoPreAuthenticate ();
887                         }
888                         return webHeaders.ToString ();
889                 }
890
891                 void DoPreAuthenticate ()
892                 {
893                         webHeaders.RemoveInternal ("Proxy-Authorization");
894                         webHeaders.RemoveInternal ("Authorization");
895                         bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
896                         ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
897                         Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
898                         if (auth == null)
899                                 return;
900
901                         string authHeader = (isProxy) ? "Proxy-Authorization" : "Authorization";
902                         webHeaders [authHeader] = auth.Message;
903                         usedPreAuth = true;
904                 }
905                 
906                 internal void SetWriteStreamError (WebExceptionStatus status)
907                 {
908                         if (aborted)
909                                 return;
910
911                         WebAsyncResult r = asyncWrite;
912                         if (r == null)
913                                 r = asyncRead;
914
915                         if (r != null) {
916                                 r.SetCompleted (false, new WebException ("Error: " + status, status));
917                                 r.DoCallback ();
918                         }
919                 }
920
921                 internal void SendRequestHeaders ()
922                 {
923                         StringBuilder req = new StringBuilder ();
924                         string query;
925                         if (!ProxyQuery) {
926                                 query = actualUri.PathAndQuery;
927                         } else if (actualUri.IsDefaultPort) {
928                                 query = String.Format ("{0}://{1}{2}",  actualUri.Scheme,
929                                                                         actualUri.Host,
930                                                                         actualUri.PathAndQuery);
931                         } else {
932                                 query = String.Format ("{0}://{1}:{2}{3}", actualUri.Scheme,
933                                                                            actualUri.Host,
934                                                                            actualUri.Port,
935                                                                            actualUri.PathAndQuery);
936                         }
937                         
938                         if (servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
939                                 actualVersion = servicePoint.ProtocolVersion;
940                         } else {
941                                 actualVersion = version;
942                         }
943
944                         req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
945                                                                 actualVersion.Major, actualVersion.Minor);
946                         req.Append (GetHeaders ());
947                         string reqstr = req.ToString ();
948                         byte [] bytes = Encoding.UTF8.GetBytes (reqstr);
949                         writeStream.SetHeaders (bytes, 0, bytes.Length);
950                 }
951
952                 internal void SetWriteStream (WebConnectionStream stream)
953                 {
954                         if (aborted)
955                                 return;
956                         
957                         writeStream = stream;
958                         if (bodyBuffer != null) {
959                                 webHeaders.RemoveInternal ("Transfer-Encoding");
960                                 contentLength = bodyBufferLength;
961                                 writeStream.SendChunked = false;
962                         }
963                         
964                         SendRequestHeaders ();
965
966                         haveRequest = true;
967                         
968                         if (bodyBuffer != null) {
969                                 // The body has been written and buffered. The request "user"
970                                 // won't write it again, so we must do it.
971                                 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
972                                 bodyBuffer = null;
973                                 writeStream.Close ();
974                         }
975
976                         if (asyncWrite != null) {
977                                 asyncWrite.SetCompleted (false, stream);
978                                 asyncWrite.DoCallback ();
979                                 asyncWrite = null;
980                         }
981                 }
982
983                 internal void SetResponseError (WebExceptionStatus status, Exception e)
984                 {
985                         WebAsyncResult r = asyncRead;
986                         if (r == null)
987                                 r = asyncWrite;
988
989                         if (r != null) {
990                                 WebException wexc = new WebException ("Error getting response stream: " + status, e, status, null); 
991                                 r.SetCompleted (false, wexc);
992                                 r.DoCallback ();
993                                 asyncRead = null;
994                                 asyncWrite = null;
995                         }
996                 }
997                 
998                 internal void SetResponseData (WebConnectionData data)
999                 {
1000                         if (aborted) {
1001                                 if (data.stream != null)
1002                                         data.stream.Close ();
1003                                 return;
1004                         }
1005                         
1006                         webResponse = new HttpWebResponse (actualUri, method, data, (cookieContainer != null));
1007                         haveResponse = true;
1008
1009                         WebAsyncResult r = asyncRead;
1010                         if (r != null) {
1011                                 r.SetCompleted (false, webResponse);
1012                                 r.DoCallback ();
1013                         }
1014                 }
1015
1016                 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1017                 {
1018                         authCompleted = false;
1019                         if (code == HttpStatusCode.Unauthorized && credentials == null)
1020                                 return false;
1021
1022                         bool isProxy = (code == HttpStatusCode.ProxyAuthenticationRequired);
1023                         if (isProxy && (proxy == null || proxy.Credentials == null))
1024                                 return false;
1025
1026                         string authHeader = response.Headers [(isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate"];
1027                         if (authHeader == null)
1028                                 return false;
1029
1030                         ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
1031                         Authorization auth = AuthenticationManager.Authenticate (authHeader, this, creds);
1032                         if (auth == null)
1033                                 return false;
1034
1035                         webHeaders [(isProxy) ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1036                         authCompleted = auth.Complete;
1037                         return true;
1038                 }
1039
1040                 // Returns true if redirected
1041                 bool CheckFinalStatus (WebAsyncResult result)
1042                 {
1043                         if (result.GotException)
1044                                 throw result.Exception;
1045
1046                         Exception throwMe = result.Exception;
1047                         bodyBuffer = null;
1048
1049                         HttpWebResponse resp = result.Response;
1050                         WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1051                         HttpStatusCode code = 0;
1052                         if (throwMe == null && webResponse != null) {
1053                                 code  = webResponse.StatusCode;
1054                                 if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
1055                                                         code == HttpStatusCode.ProxyAuthenticationRequired)) {
1056                                         if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1057                                                 // Keep the written body, so it can be rewritten in the retry
1058                                                 if (InternalAllowBuffering) {
1059                                                         bodyBuffer = writeStream.WriteBuffer;
1060                                                         bodyBufferLength = writeStream.WriteBufferLength;
1061                                                         return true;
1062                                                 } else if (method != "PUT" && method != "POST") {
1063                                                         return true;
1064                                                 }
1065                                                 
1066                                                 writeStream.InternalClose ();
1067                                                 writeStream = null;
1068                                                 webResponse = null;
1069                                                 throw new WebException ("This request requires buffering " +
1070                                                                         "of data for authentication or " +
1071                                                                         "redirection to be sucessful.");
1072                                         }
1073                                 }
1074
1075                                 if ((int) code >= 400) {
1076                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1077                                                                     (int) code, webResponse.StatusDescription);
1078                                         throwMe = new WebException (err, null, protoError, webResponse);
1079                                 } else if ((int) code == 304 && allowAutoRedirect) {
1080                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1081                                                                     (int) code, webResponse.StatusDescription);
1082                                         throwMe = new WebException (err, null, protoError, webResponse);
1083                                 } else if ((int) code >= 300 && allowAutoRedirect && redirects > maxAutoRedirect) {
1084                                         throwMe = new WebException ("Max. redirections exceeded.", null,
1085                                                                     protoError, webResponse);
1086                                 }
1087                         }
1088
1089                         if (throwMe == null) {
1090                                 bool b = false;
1091                                 if (allowAutoRedirect && (int) code >= 300)
1092                                         b = Redirect (result, code);
1093
1094                                 return b;
1095                         }
1096
1097                         if (writeStream != null) {
1098                                 writeStream.InternalClose ();
1099                                 writeStream = null;
1100                         }
1101
1102                         if (webResponse != null)
1103                                 webResponse = null;
1104
1105                         throw throwMe;
1106                 }
1107         }
1108 }
1109