2 // System.Net.HttpWebRequest
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.Configuration;
37 using System.Globalization;
40 using System.Net.Cache;
41 using System.Net.Sockets;
42 using System.Runtime.Remoting.Messaging;
43 using System.Runtime.Serialization;
44 using System.Security.Cryptography.X509Certificates;
46 using System.Threading;
51 public class HttpWebRequest : WebRequest, ISerializable {
55 bool allowAutoRedirect = true;
56 bool allowBuffering = true;
57 X509CertificateCollection certificates;
58 string connectionGroup;
59 bool haveContentLength;
60 long contentLength = -1;
61 HttpContinueDelegate continueDelegate;
62 CookieContainer cookieContainer;
63 ICredentials credentials;
67 WebHeaderCollection webHeaders;
68 bool keepAlive = true;
69 int maxAutoRedirect = 50;
70 string mediaType = String.Empty;
71 string method = "GET";
72 string initialMethod = "GET";
73 bool pipelined = true;
76 Version version = HttpVersion.Version11;
78 Version actualVersion;
81 ServicePoint servicePoint;
84 WebConnectionStream writeStream;
85 HttpWebResponse webResponse;
86 WebAsyncResult asyncWrite;
87 WebAsyncResult asyncRead;
88 EventHandler abortHandler;
90 bool gotRequestStream;
95 bool getResponseCalled;
97 object locker = new object ();
98 bool finished_reading;
99 internal WebConnection WebConnection;
100 DecompressionMethods auto_decomp;
101 int maxResponseHeadersLength;
102 static int defaultMaxResponseHeadersLength;
103 int readWriteTimeout = 300000; // ms
110 AuthorizationState auth_state, proxy_auth_state;
114 static HttpWebRequest ()
116 defaultMaxResponseHeadersLength = 64 * 1024;
118 NetConfig config = ConfigurationSettings.GetConfig ("system.net/settings") as NetConfig;
119 if (config != null) {
120 int x = config.MaxResponseHeadersLength;
124 defaultMaxResponseHeadersLength = x;
134 HttpWebRequest (Uri uri)
136 this.requestUri = uri;
137 this.actualUri = uri;
138 this.proxy = GlobalProxySelection.Select;
139 this.webHeaders = new WebHeaderCollection (WebHeaderCollection.HeaderInfo.Request);
141 ResetAuthorization ();
144 [Obsolete ("Serialization is obsoleted for this type", false)]
145 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
147 SerializationInfo info = serializationInfo;
149 requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
150 actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
151 allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
152 allowBuffering = info.GetBoolean ("allowBuffering");
153 certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
154 connectionGroup = info.GetString ("connectionGroup");
155 contentLength = info.GetInt64 ("contentLength");
156 webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
157 keepAlive = info.GetBoolean ("keepAlive");
158 maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
159 mediaType = info.GetString ("mediaType");
160 method = info.GetString ("method");
161 initialMethod = info.GetString ("initialMethod");
162 pipelined = info.GetBoolean ("pipelined");
163 version = (Version) info.GetValue ("version", typeof (Version));
164 proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
165 sendChunked = info.GetBoolean ("sendChunked");
166 timeout = info.GetInt32 ("timeout");
167 redirects = info.GetInt32 ("redirects");
168 host = info.GetString ("host");
169 ResetAuthorization ();
172 void ResetAuthorization ()
174 auth_state = new AuthorizationState (this, false);
175 proxy_auth_state = new AuthorizationState (this, true);
180 public string Accept {
181 get { return webHeaders ["Accept"]; }
183 CheckRequestStarted ();
184 webHeaders.RemoveAndAdd ("Accept", value);
189 get { return actualUri; }
190 internal set { actualUri = value; } // Used by Ftp+proxy
193 public bool AllowAutoRedirect {
194 get { return allowAutoRedirect; }
195 set { this.allowAutoRedirect = value; }
198 public bool AllowWriteStreamBuffering {
199 get { return allowBuffering; }
200 set { allowBuffering = value; }
204 public virtual bool AllowReadStreamBuffering {
205 get { return allowBuffering; }
206 set { allowBuffering = value; }
210 static Exception GetMustImplement ()
212 return new NotImplementedException ();
215 public DecompressionMethods AutomaticDecompression
221 CheckRequestStarted ();
226 internal bool InternalAllowBuffering {
228 return (allowBuffering && (method != "HEAD" && method != "GET" &&
229 method != "MKCOL" && method != "CONNECT" &&
234 public X509CertificateCollection ClientCertificates {
236 if (certificates == null)
237 certificates = new X509CertificateCollection ();
243 throw GetMustImplement ();
247 public string Connection {
248 get { return webHeaders ["Connection"]; }
250 CheckRequestStarted ();
252 if (string.IsNullOrEmpty (value)) {
253 webHeaders.RemoveInternal ("Connection");
257 string val = value.ToLowerInvariant ();
258 if (val.Contains ("keep-alive") || val.Contains ("close"))
259 throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
262 value = value + ", Keep-Alive";
264 webHeaders.RemoveAndAdd ("Connection", value);
268 public override string ConnectionGroupName {
269 get { return connectionGroup; }
270 set { connectionGroup = value; }
273 public override long ContentLength {
274 get { return contentLength; }
276 CheckRequestStarted ();
278 throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
280 contentLength = value;
281 haveContentLength = true;
285 internal long InternalContentLength {
286 set { contentLength = value; }
289 internal bool ThrowOnError { get; set; }
291 public override string ContentType {
292 get { return webHeaders ["Content-Type"]; }
294 if (value == null || value.Trim().Length == 0) {
295 webHeaders.RemoveInternal ("Content-Type");
298 webHeaders.RemoveAndAdd ("Content-Type", value);
302 public HttpContinueDelegate ContinueDelegate {
303 get { return continueDelegate; }
304 set { continueDelegate = value; }
310 public CookieContainer CookieContainer {
311 get { return cookieContainer; }
312 set { cookieContainer = value; }
315 public override ICredentials Credentials {
316 get { return credentials; }
317 set { credentials = value; }
320 public DateTime Date {
322 string date = webHeaders ["Date"];
324 return DateTime.MinValue;
325 return DateTime.ParseExact (date, "r", CultureInfo.InvariantCulture).ToLocalTime ();
328 if (value.Equals (DateTime.MinValue))
329 webHeaders.RemoveInternal ("Date");
331 webHeaders.RemoveAndAdd ("Date", value.ToUniversalTime ().ToString ("r", CultureInfo.InvariantCulture));
338 public static new RequestCachePolicy DefaultCachePolicy
341 throw GetMustImplement ();
344 throw GetMustImplement ();
350 public static int DefaultMaximumErrorResponseLength
353 throw GetMustImplement ();
356 throw GetMustImplement ();
360 public string Expect {
361 get { return webHeaders ["Expect"]; }
363 CheckRequestStarted ();
366 val = val.Trim ().ToLower ();
368 if (val == null || val.Length == 0) {
369 webHeaders.RemoveInternal ("Expect");
373 if (val == "100-continue")
374 throw new ArgumentException ("100-Continue cannot be set with this property.",
376 webHeaders.RemoveAndAdd ("Expect", value);
383 public bool HaveResponse {
384 get { return haveResponse; }
387 public override WebHeaderCollection Headers {
388 get { return webHeaders; }
390 CheckRequestStarted ();
391 WebHeaderCollection newHeaders = new WebHeaderCollection (WebHeaderCollection.HeaderInfo.Request);
392 int count = value.Count;
393 for (int i = 0; i < count; i++)
394 newHeaders.Add (value.GetKey (i), value.Get (i));
396 webHeaders = newHeaders;
408 return actualUri.Authority;
413 throw new ArgumentNullException ("value");
415 if (!CheckValidHost (actualUri.Scheme, value))
416 throw new ArgumentException ("Invalid host: " + value);
422 static bool CheckValidHost (string scheme, string val)
430 int idx = val.IndexOf ('/');
435 if (IPAddress.TryParse (val, out ipaddr))
438 string u = scheme + "://" + val + "/";
439 return Uri.IsWellFormedUriString (u, UriKind.Absolute);
442 public DateTime IfModifiedSince {
444 string str = webHeaders ["If-Modified-Since"];
448 return MonoHttpDate.Parse (str);
449 } catch (Exception) {
454 CheckRequestStarted ();
456 webHeaders.SetInternal ("If-Modified-Since",
457 value.ToUniversalTime ().ToString ("r", null));
458 // TODO: check last param when using different locale
462 public bool KeepAlive {
471 public int MaximumAutomaticRedirections {
472 get { return maxAutoRedirect; }
475 throw new ArgumentException ("Must be > 0", "value");
477 maxAutoRedirect = value;
481 [MonoTODO ("Use this")]
482 public int MaximumResponseHeadersLength {
483 get { return maxResponseHeadersLength; }
484 set { maxResponseHeadersLength = value; }
487 [MonoTODO ("Use this")]
488 public static int DefaultMaximumResponseHeadersLength {
489 get { return defaultMaxResponseHeadersLength; }
490 set { defaultMaxResponseHeadersLength = value; }
493 public int ReadWriteTimeout {
494 get { return readWriteTimeout; }
497 throw new InvalidOperationException ("The request has already been sent.");
500 throw new ArgumentOutOfRangeException ("value", "Must be >= -1");
502 readWriteTimeout = value;
508 public int ContinueTimeout {
509 get { throw new NotImplementedException (); }
510 set { throw new NotImplementedException (); }
514 public string MediaType {
515 get { return mediaType; }
521 public override string Method {
522 get { return this.method; }
524 if (value == null || value.Trim () == "")
525 throw new ArgumentException ("not a valid method");
527 method = value.ToUpperInvariant ();
528 if (method != "HEAD" && method != "GET" && method != "POST" && method != "PUT" &&
529 method != "DELETE" && method != "CONNECT" && method != "TRACE" &&
536 public bool Pipelined {
537 get { return pipelined; }
538 set { pipelined = value; }
541 public override bool PreAuthenticate {
542 get { return preAuthenticate; }
543 set { preAuthenticate = value; }
546 public Version ProtocolVersion {
547 get { return version; }
549 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
550 throw new ArgumentException ("value");
552 force_version = true;
557 public override IWebProxy Proxy {
558 get { return proxy; }
560 CheckRequestStarted ();
562 servicePoint = null; // we may need a new one
566 public string Referer {
567 get { return webHeaders ["Referer"]; }
569 CheckRequestStarted ();
570 if (value == null || value.Trim().Length == 0) {
571 webHeaders.RemoveInternal ("Referer");
574 webHeaders.SetInternal ("Referer", value);
578 public override Uri RequestUri {
579 get { return requestUri; }
582 public bool SendChunked {
583 get { return sendChunked; }
585 CheckRequestStarted ();
590 public ServicePoint ServicePoint {
591 get { return GetServicePoint (); }
594 internal ServicePoint ServicePointNoLock {
595 get { return servicePoint; }
598 public virtual bool SupportsCookieContainer {
600 // The managed implementation supports the cookie container
601 // it is only Silverlight that returns false here
606 public override int Timeout {
607 get { return timeout; }
610 throw new ArgumentOutOfRangeException ("value");
616 public string TransferEncoding {
617 get { return webHeaders ["Transfer-Encoding"]; }
619 CheckRequestStarted ();
622 val = val.Trim ().ToLower ();
624 if (val == null || val.Length == 0) {
625 webHeaders.RemoveInternal ("Transfer-Encoding");
629 if (val == "chunked")
630 throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
633 throw new ArgumentException ("SendChunked must be True", "value");
635 webHeaders.RemoveAndAdd ("Transfer-Encoding", value);
639 public override bool UseDefaultCredentials
641 get { return CredentialCache.DefaultCredentials == Credentials; }
642 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
645 public string UserAgent {
646 get { return webHeaders ["User-Agent"]; }
647 set { webHeaders.SetInternal ("User-Agent", value); }
650 bool unsafe_auth_blah;
651 public bool UnsafeAuthenticatedConnectionSharing
653 get { return unsafe_auth_blah; }
654 set { unsafe_auth_blah = value; }
657 internal bool GotRequestStream {
658 get { return gotRequestStream; }
661 internal bool ExpectContinue {
662 get { return expectContinue; }
663 set { expectContinue = value; }
666 internal Uri AuthUri {
667 get { return actualUri; }
670 internal bool ProxyQuery {
671 get { return servicePoint.UsesProxy && !servicePoint.UseConnect; }
676 internal ServicePoint GetServicePoint ()
679 if (hostChanged || servicePoint == null) {
680 servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
688 public void AddRange (int range)
690 AddRange ("bytes", (long) range);
693 public void AddRange (int from, int to)
695 AddRange ("bytes", (long) from, (long) to);
698 public void AddRange (string rangeSpecifier, int range)
700 AddRange (rangeSpecifier, (long) range);
703 public void AddRange (string rangeSpecifier, int from, int to)
705 AddRange (rangeSpecifier, (long) from, (long) to);
712 void AddRange (long range)
714 AddRange ("bytes", (long) range);
722 void AddRange (long from, long to)
724 AddRange ("bytes", from, to);
732 void AddRange (string rangeSpecifier, long range)
734 if (rangeSpecifier == null)
735 throw new ArgumentNullException ("rangeSpecifier");
736 if (!WebHeaderCollection.IsHeaderValue (rangeSpecifier))
737 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
739 string r = webHeaders ["Range"];
741 r = rangeSpecifier + "=";
743 string old_specifier = r.Substring (0, r.IndexOf ('='));
744 if (String.Compare (old_specifier, rangeSpecifier, StringComparison.OrdinalIgnoreCase) != 0)
745 throw new InvalidOperationException ("A different range specifier is already in use");
749 string n = range.ToString (CultureInfo.InvariantCulture);
754 webHeaders.RemoveAndAdd ("Range", r);
762 void AddRange (string rangeSpecifier, long from, long to)
764 if (rangeSpecifier == null)
765 throw new ArgumentNullException ("rangeSpecifier");
766 if (!WebHeaderCollection.IsHeaderValue (rangeSpecifier))
767 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
768 if (from > to || from < 0)
769 throw new ArgumentOutOfRangeException ("from");
771 throw new ArgumentOutOfRangeException ("to");
773 string r = webHeaders ["Range"];
775 r = rangeSpecifier + "=";
779 r = String.Format ("{0}{1}-{2}", r, from, to);
780 webHeaders.RemoveAndAdd ("Range", r);
784 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
787 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
789 bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD" ||
791 if (method == null || !send)
792 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
794 if (contentLength == -1 && !sendChunked && !allowBuffering && KeepAlive)
795 throw new ProtocolViolationException ("Content-Length not set");
797 string transferEncoding = TransferEncoding;
798 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
799 throw new ProtocolViolationException ("SendChunked should be true.");
803 if (getResponseCalled)
804 throw new InvalidOperationException ("The operation cannot be performed once the request has been submitted.");
806 if (asyncWrite != null) {
807 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
808 "method while a previous call is still in progress.");
811 asyncWrite = new WebAsyncResult (this, callback, state);
812 initialMethod = method;
814 if (writeStream != null) {
815 asyncWrite.SetCompleted (true, writeStream);
816 asyncWrite.DoCallback ();
821 gotRequestStream = true;
822 WebAsyncResult result = asyncWrite;
826 servicePoint = GetServicePoint ();
827 abortHandler = servicePoint.SendRequest (this, connectionGroup);
833 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
835 if (asyncResult == null)
836 throw new ArgumentNullException ("asyncResult");
838 WebAsyncResult result = asyncResult as WebAsyncResult;
840 throw new ArgumentException ("Invalid IAsyncResult");
843 result.WaitUntilComplete ();
845 Exception e = result.Exception;
849 return result.WriteStream;
852 public override Stream GetRequestStream()
854 IAsyncResult asyncResult = asyncWrite;
855 if (asyncResult == null) {
856 asyncResult = BeginGetRequestStream (null, null);
857 asyncWrite = (WebAsyncResult) asyncResult;
860 if (!asyncResult.IsCompleted && !asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
862 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
865 return EndGetRequestStream (asyncResult);
868 WebAsyncResult CheckIfForceWrite (AsyncCallback callback, object state)
870 if (writeStream == null || writeStream.RequestWritten || !InternalAllowBuffering)
873 if (contentLength < 0 && writeStream.CanWrite == true && writeStream.WriteBufferLength < 0)
876 if (contentLength < 0 && writeStream.WriteBufferLength >= 0)
877 InternalContentLength = writeStream.WriteBufferLength;
879 if (contentLength < 0 && writeStream.CanWrite == true)
883 // This will write the POST/PUT if the write stream already has the expected
884 // amount of bytes in it (ContentLength) (bug #77753) or if the write stream
885 // contains data and it has been closed already (xamarin bug #1512).
887 if (writeStream.WriteBufferLength == contentLength || (contentLength == -1 && writeStream.CanWrite == false))
888 return writeStream.WriteRequestAsync (callback, state);
893 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
896 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
899 throw new ProtocolViolationException ("Method is null.");
901 string transferEncoding = TransferEncoding;
902 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
903 throw new ProtocolViolationException ("SendChunked should be true.");
905 Monitor.Enter (locker);
906 getResponseCalled = true;
907 if (asyncRead != null && !haveResponse) {
908 Monitor.Exit (locker);
909 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
910 "method while a previous call is still in progress.");
913 asyncRead = new WebAsyncResult (this, callback, state);
914 WebAsyncResult aread = asyncRead;
915 initialMethod = method;
917 aread.InnerAsyncResult = CheckIfForceWrite (GetResponseAsyncCB, aread);
918 if (aread.InnerAsyncResult == null)
919 GetResponseAsyncCB2 (aread);
921 Monitor.Exit (locker);
925 void GetResponseAsyncCB (IAsyncResult ar)
927 var result = (WebAsyncResult)ar;
928 var innerResult = (WebAsyncResult)result.InnerAsyncResult;
929 result.InnerAsyncResult = null;
931 if (innerResult != null && innerResult.GotException) {
932 asyncRead.SetCompleted (true, innerResult.Exception);
933 asyncRead.DoCallback ();
937 Monitor.Enter (locker);
938 GetResponseAsyncCB2 ((WebAsyncResult)ar);
941 void GetResponseAsyncCB2 (WebAsyncResult aread)
944 Exception saved = saved_exc;
945 if (webResponse != null) {
946 Monitor.Exit (locker);
948 aread.SetCompleted (true, webResponse);
950 aread.SetCompleted (true, saved);
954 } else if (saved != null) {
955 Monitor.Exit (locker);
956 aread.SetCompleted (true, saved);
965 servicePoint = GetServicePoint ();
966 abortHandler = servicePoint.SendRequest (this, connectionGroup);
969 Monitor.Exit (locker);
972 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
974 if (asyncResult == null)
975 throw new ArgumentNullException ("asyncResult");
977 WebAsyncResult result = asyncResult as WebAsyncResult;
979 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
981 if (!result.WaitUntilComplete (timeout, false)) {
983 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
986 if (result.GotException)
987 throw result.Exception;
989 return result.Response;
993 public Stream EndGetRequestStream (IAsyncResult asyncResult, out TransportContext transportContext)
995 transportContext = null;
996 return EndGetRequestStream (asyncResult);
1000 public override WebResponse GetResponse()
1002 WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
1003 return EndGetResponse (result);
1006 internal bool FinishedReading {
1007 get { return finished_reading; }
1008 set { finished_reading = value; }
1011 internal bool Aborted {
1012 get { return Interlocked.CompareExchange (ref aborted, 0, 0) == 1; }
1015 public override void Abort ()
1017 if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
1020 if (haveResponse && finished_reading)
1023 haveResponse = true;
1024 if (abortHandler != null) {
1026 abortHandler (this, EventArgs.Empty);
1027 } catch (Exception) {}
1028 abortHandler = null;
1031 if (asyncWrite != null) {
1032 WebAsyncResult r = asyncWrite;
1033 if (!r.IsCompleted) {
1035 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
1036 r.SetCompleted (false, wexc);
1043 if (asyncRead != null) {
1044 WebAsyncResult r = asyncRead;
1045 if (!r.IsCompleted) {
1047 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
1048 r.SetCompleted (false, wexc);
1055 if (writeStream != null) {
1057 writeStream.Close ();
1062 if (webResponse != null) {
1064 webResponse.Close ();
1070 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
1071 StreamingContext streamingContext)
1073 GetObjectData (serializationInfo, streamingContext);
1076 protected override void GetObjectData (SerializationInfo serializationInfo,
1077 StreamingContext streamingContext)
1079 SerializationInfo info = serializationInfo;
1081 info.AddValue ("requestUri", requestUri, typeof (Uri));
1082 info.AddValue ("actualUri", actualUri, typeof (Uri));
1083 info.AddValue ("allowAutoRedirect", allowAutoRedirect);
1084 info.AddValue ("allowBuffering", allowBuffering);
1085 info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
1086 info.AddValue ("connectionGroup", connectionGroup);
1087 info.AddValue ("contentLength", contentLength);
1088 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
1089 info.AddValue ("keepAlive", keepAlive);
1090 info.AddValue ("maxAutoRedirect", maxAutoRedirect);
1091 info.AddValue ("mediaType", mediaType);
1092 info.AddValue ("method", method);
1093 info.AddValue ("initialMethod", initialMethod);
1094 info.AddValue ("pipelined", pipelined);
1095 info.AddValue ("version", version, typeof (Version));
1096 info.AddValue ("proxy", proxy, typeof (IWebProxy));
1097 info.AddValue ("sendChunked", sendChunked);
1098 info.AddValue ("timeout", timeout);
1099 info.AddValue ("redirects", redirects);
1100 info.AddValue ("host", host);
1103 void CheckRequestStarted ()
1106 throw new InvalidOperationException ("request started");
1109 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
1111 if (continueDelegate != null)
1112 continueDelegate (statusCode, headers);
1115 void RewriteRedirectToGet ()
1118 webHeaders.RemoveInternal ("Transfer-Encoding");
1119 sendChunked = false;
1122 bool Redirect (WebAsyncResult result, HttpStatusCode code, WebResponse response)
1126 string uriString = null;
1128 case HttpStatusCode.Ambiguous: // 300
1129 e = new WebException ("Ambiguous redirect.");
1131 case HttpStatusCode.MovedPermanently: // 301
1132 case HttpStatusCode.Redirect: // 302
1133 if (method == "POST")
1134 RewriteRedirectToGet ();
1136 case HttpStatusCode.TemporaryRedirect: // 307
1138 case HttpStatusCode.SeeOther: //303
1139 RewriteRedirectToGet ();
1141 case HttpStatusCode.NotModified: // 304
1143 case HttpStatusCode.UseProxy: // 305
1144 e = new NotImplementedException ("Proxy support not available.");
1146 case HttpStatusCode.Unused: // 306
1148 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
1152 if (method != "GET" && !InternalAllowBuffering)
1153 e = new WebException ("The request requires buffering data to succeed.", null, WebExceptionStatus.ProtocolError, webResponse);
1159 uriString = webResponse.Headers ["Location"];
1161 if (uriString == null)
1162 throw new WebException ("No Location header found for " + (int) code,
1163 WebExceptionStatus.ProtocolError);
1165 Uri prev = actualUri;
1167 actualUri = new Uri (actualUri, uriString);
1168 } catch (Exception) {
1169 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
1170 uriString, (int) code),
1171 WebExceptionStatus.ProtocolError);
1174 hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
1178 string GetHeaders ()
1180 bool continue100 = false;
1183 webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
1184 webHeaders.RemoveInternal ("Content-Length");
1185 } else if (contentLength != -1) {
1186 if (auth_state.NtlmAuthState == NtlmAuthState.Challenge || proxy_auth_state.NtlmAuthState == NtlmAuthState.Challenge) {
1187 // We don't send any body with the NTLM Challenge request.
1188 if (haveContentLength || gotRequestStream || contentLength > 0)
1189 webHeaders.SetInternal ("Content-Length", "0");
1191 webHeaders.RemoveInternal ("Content-Length");
1193 if (contentLength > 0)
1196 if (haveContentLength || gotRequestStream || contentLength > 0)
1197 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
1199 webHeaders.RemoveInternal ("Transfer-Encoding");
1201 webHeaders.RemoveInternal ("Content-Length");
1204 if (actualVersion == HttpVersion.Version11 && continue100 &&
1205 servicePoint.SendContinue) { // RFC2616 8.2.3
1206 webHeaders.RemoveAndAdd ("Expect" , "100-continue");
1207 expectContinue = true;
1209 webHeaders.RemoveInternal ("Expect");
1210 expectContinue = false;
1213 bool proxy_query = ProxyQuery;
1214 string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
1215 webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
1216 Version proto_version = servicePoint.ProtocolVersion;
1217 bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
1219 if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
1220 if (webHeaders[connectionHeader] == null
1221 || webHeaders[connectionHeader].IndexOf ("keep-alive", StringComparison.OrdinalIgnoreCase) == -1)
1222 webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
1223 } else if (!keepAlive && version == HttpVersion.Version11) {
1224 webHeaders.RemoveAndAdd (connectionHeader, "close");
1227 webHeaders.SetInternal ("Host", Host);
1228 if (cookieContainer != null) {
1229 string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
1230 if (cookieHeader != "")
1231 webHeaders.RemoveAndAdd ("Cookie", cookieHeader);
1233 webHeaders.RemoveInternal ("Cookie");
1236 string accept_encoding = null;
1237 if ((auto_decomp & DecompressionMethods.GZip) != 0)
1238 accept_encoding = "gzip";
1239 if ((auto_decomp & DecompressionMethods.Deflate) != 0)
1240 accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
1241 if (accept_encoding != null)
1242 webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);
1244 if (!usedPreAuth && preAuthenticate)
1245 DoPreAuthenticate ();
1247 return webHeaders.ToString ();
1250 void DoPreAuthenticate ()
1252 bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1253 ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
1254 Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
1258 webHeaders.RemoveInternal ("Proxy-Authorization");
1259 webHeaders.RemoveInternal ("Authorization");
1260 string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
1261 webHeaders [authHeader] = auth.Message;
1265 internal void SetWriteStreamError (WebExceptionStatus status, Exception exc)
1270 WebAsyncResult r = asyncWrite;
1278 msg = "Error: " + status;
1279 wex = new WebException (msg, status);
1281 msg = String.Format ("Error: {0} ({1})", status, exc.Message);
1282 wex = new WebException (msg, exc, status);
1284 r.SetCompleted (false, wex);
1289 internal byte[] GetRequestHeaders ()
1291 StringBuilder req = new StringBuilder ();
1294 query = actualUri.PathAndQuery;
1296 query = String.Format ("{0}://{1}{2}", actualUri.Scheme,
1298 actualUri.PathAndQuery);
1301 if (!force_version && servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
1302 actualVersion = servicePoint.ProtocolVersion;
1304 actualVersion = version;
1307 req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
1308 actualVersion.Major, actualVersion.Minor);
1309 req.Append (GetHeaders ());
1310 string reqstr = req.ToString ();
1311 return Encoding.UTF8.GetBytes (reqstr);
1314 internal void SetWriteStream (WebConnectionStream stream)
1319 writeStream = stream;
1320 if (bodyBuffer != null) {
1321 webHeaders.RemoveInternal ("Transfer-Encoding");
1322 contentLength = bodyBufferLength;
1323 writeStream.SendChunked = false;
1327 var result = writeStream.SetHeadersAsync (false, SetWriteStreamCB, null);
1329 SetWriteStreamCB (null);
1330 } catch (Exception exc) {
1331 SetWriteStreamErrorCB (exc);
1335 void SetWriteStreamErrorCB (Exception exc)
1337 WebException wexc = exc as WebException;
1339 SetWriteStreamError (wexc.Status, wexc);
1341 SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
1344 void SetWriteStreamCB (IAsyncResult ar)
1346 WebAsyncResult result = ar as WebAsyncResult;
1348 if (result != null && result.Exception != null) {
1349 SetWriteStreamErrorCB (result.Exception);
1355 WebAsyncResult writeRequestResult = null;
1357 if (bodyBuffer != null) {
1358 // The body has been written and buffered. The request "user"
1359 // won't write it again, so we must do it.
1360 if (auth_state.NtlmAuthState != NtlmAuthState.Challenge && proxy_auth_state.NtlmAuthState != NtlmAuthState.Challenge) {
1361 // FIXME: this is a blocking call on the thread pool that could lead to thread pool exhaustion
1362 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1364 writeStream.Close ();
1366 } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
1367 method != "TRACE") {
1368 if (getResponseCalled && !writeStream.RequestWritten)
1369 writeRequestResult = writeStream.WriteRequestAsync (SetWriteStreamCB2, null);
1372 if (writeRequestResult == null)
1373 SetWriteStreamCB2 (null);
1376 void SetWriteStreamCB2 (IAsyncResult ar)
1378 var result = (WebAsyncResult)ar;
1379 if (result != null && result.GotException) {
1380 SetWriteStreamErrorCB (result.Exception);
1384 if (asyncWrite != null) {
1385 asyncWrite.SetCompleted (false, writeStream);
1386 asyncWrite.DoCallback ();
1391 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1396 string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1397 WebAsyncResult r = asyncRead;
1402 if (e is WebException) {
1403 wexc = (WebException) e;
1405 wexc = new WebException (msg, e, status, null);
1408 if (!r.IsCompleted) {
1409 r.SetCompleted (false, wexc);
1411 } else if (r == asyncWrite) {
1414 haveResponse = true;
1418 haveResponse = true;
1424 void CheckSendError (WebConnectionData data)
1426 // Got here, but no one called GetResponse
1427 int status = data.StatusCode;
1428 if (status < 400 || status == 401 || status == 407)
1431 if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1432 // The request has not been completely sent and we got here!
1433 // We should probably just close and cause an error in any case,
1434 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse);
1435 if (allowBuffering || sendChunked || writeStream.totalWritten >= contentLength) {
1436 webResponse.ReadAll ();
1438 writeStream.IgnoreIOErrors = true;
1443 bool HandleNtlmAuth (WebAsyncResult r)
1445 bool isProxy = webResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired;
1446 if ((isProxy ? proxy_auth_state.NtlmAuthState : auth_state.NtlmAuthState) == NtlmAuthState.None)
1449 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1451 WebConnection cnc = wce.Connection;
1452 cnc.PriorityRequest = this;
1453 ICredentials creds = !isProxy ? credentials : proxy.Credentials;
1454 if (creds != null) {
1455 cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM");
1456 cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah;
1460 finished_reading = false;
1461 haveResponse = false;
1462 webResponse.ReadAll ();
1467 internal void SetResponseData (WebConnectionData data)
1471 if (data.stream != null)
1472 data.stream.Close ();
1476 WebException wexc = null;
1478 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1479 } catch (Exception e) {
1480 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null);
1481 if (data.stream != null)
1482 data.stream.Close ();
1485 if (wexc == null && (method == "POST" || method == "PUT")) {
1486 CheckSendError (data);
1487 if (saved_exc != null)
1488 wexc = (WebException) saved_exc;
1491 WebAsyncResult r = asyncRead;
1493 bool forced = false;
1494 if (r == null && webResponse != null) {
1495 // This is a forced completion (302, 204)...
1497 r = new WebAsyncResult (null, null);
1498 r.SetCompleted (false, webResponse);
1503 haveResponse = true;
1505 r.SetCompleted (false, wexc);
1510 bool isProxy = ProxyQuery && !proxy.IsBypassed (actualUri);
1514 redirected = CheckFinalStatus (r);
1516 if ((isProxy ? proxy_auth_state.IsNtlmAuthenticated : auth_state.IsNtlmAuthenticated) &&
1517 webResponse != null && (int)webResponse.StatusCode < 400) {
1518 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1520 WebConnection cnc = wce.Connection;
1521 cnc.NtlmAuthenticated = true;
1525 // clear internal buffer so that it does not
1526 // hold possible big buffer (bug #397627)
1527 if (writeStream != null)
1528 writeStream.KillBuffer ();
1530 haveResponse = true;
1531 r.SetCompleted (false, webResponse);
1535 sendChunked = false;
1536 webHeaders.RemoveInternal ("Transfer-Encoding");
1539 if (webResponse != null) {
1540 if (HandleNtlmAuth (r))
1542 webResponse.Close ();
1544 finished_reading = false;
1545 haveResponse = false;
1548 servicePoint = GetServicePoint ();
1549 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1551 } catch (WebException wexc2) {
1554 haveResponse = true;
1556 r.SetCompleted (false, wexc2);
1559 } catch (Exception ex) {
1560 wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null);
1563 haveResponse = true;
1565 r.SetCompleted (false, wexc);
1573 struct AuthorizationState
1575 readonly HttpWebRequest request;
1576 readonly bool isProxy;
1578 NtlmAuthState ntlm_auth_state;
1580 public bool IsCompleted {
1581 get { return isCompleted; }
1584 public NtlmAuthState NtlmAuthState {
1585 get { return ntlm_auth_state; }
1588 public bool IsNtlmAuthenticated {
1589 get { return isCompleted && ntlm_auth_state != NtlmAuthState.None; }
1592 public AuthorizationState (HttpWebRequest request, bool isProxy)
1594 this.request = request;
1595 this.isProxy = isProxy;
1596 isCompleted = false;
1597 ntlm_auth_state = NtlmAuthState.None;
1600 public bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1602 isCompleted = false;
1603 if (code == HttpStatusCode.Unauthorized && request.credentials == null)
1606 // FIXME: This should never happen!
1607 if (isProxy != (code == HttpStatusCode.ProxyAuthenticationRequired))
1610 if (isProxy && (request.proxy == null || request.proxy.Credentials == null))
1613 string [] authHeaders = response.Headers.GetValues_internal (isProxy ? "Proxy-Authenticate" : "WWW-Authenticate", false);
1614 if (authHeaders == null || authHeaders.Length == 0)
1617 ICredentials creds = (!isProxy) ? request.credentials : request.proxy.Credentials;
1618 Authorization auth = null;
1619 foreach (string authHeader in authHeaders) {
1620 auth = AuthenticationManager.Authenticate (authHeader, request, creds);
1626 request.webHeaders [isProxy ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1627 isCompleted = auth.Complete;
1628 bool is_ntlm = (auth.Module.AuthenticationType == "NTLM");
1630 ntlm_auth_state = (NtlmAuthState)((int) ntlm_auth_state + 1);
1634 public void Reset ()
1636 isCompleted = false;
1637 ntlm_auth_state = NtlmAuthState.None;
1638 request.webHeaders.RemoveInternal (isProxy ? "Proxy-Authorization" : "Authorization");
1641 public override string ToString ()
1643 return string.Format ("{0}AuthState [{1}:{2}]", isProxy ? "Proxy" : "", isCompleted, ntlm_auth_state);
1647 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1649 bool isProxy = code == HttpStatusCode.ProxyAuthenticationRequired;
1650 return isProxy ? proxy_auth_state.CheckAuthorization (response, code) : auth_state.CheckAuthorization (response, code);
1653 // Returns true if redirected
1654 bool CheckFinalStatus (WebAsyncResult result)
1656 if (result.GotException) {
1658 throw result.Exception;
1661 Exception throwMe = result.Exception;
1663 HttpWebResponse resp = result.Response;
1664 WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1665 HttpStatusCode code = 0;
1666 if (throwMe == null && webResponse != null) {
1667 code = webResponse.StatusCode;
1668 if ((!auth_state.IsCompleted && code == HttpStatusCode.Unauthorized && credentials != null) ||
1669 (ProxyQuery && !proxy_auth_state.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) {
1670 if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1671 // Keep the written body, so it can be rewritten in the retry
1672 if (InternalAllowBuffering) {
1673 if (writeStream.WriteBufferLength > 0) {
1674 bodyBuffer = writeStream.WriteBuffer;
1675 bodyBufferLength = writeStream.WriteBufferLength;
1678 } else if (method != "PUT" && method != "POST") {
1686 writeStream.InternalClose ();
1688 webResponse.Close ();
1692 throw new WebException ("This request requires buffering " +
1693 "of data for authentication or " +
1694 "redirection to be sucessful.");
1699 if ((int) code >= 400) {
1700 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1701 (int) code, webResponse.StatusDescription);
1702 throwMe = new WebException (err, null, protoError, webResponse);
1703 webResponse.ReadAll ();
1704 } else if ((int) code == 304 && allowAutoRedirect) {
1705 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1706 (int) code, webResponse.StatusDescription);
1707 throwMe = new WebException (err, null, protoError, webResponse);
1708 } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
1709 throwMe = new WebException ("Max. redirections exceeded.", null,
1710 protoError, webResponse);
1711 webResponse.ReadAll ();
1716 if (throwMe == null) {
1719 if (allowAutoRedirect && c >= 300) {
1720 b = Redirect (result, code, webResponse);
1721 if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1722 bodyBuffer = writeStream.WriteBuffer;
1723 bodyBufferLength = writeStream.WriteBufferLength;
1725 if (b && !unsafe_auth_blah) {
1726 auth_state.Reset ();
1727 proxy_auth_state.Reset ();
1731 if (resp != null && c >= 300 && c != 304)
1740 if (writeStream != null) {
1741 writeStream.InternalClose ();
1750 internal bool ReuseConnection {
1755 internal WebConnection StoredConnection;