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 //bodyBufferLength = 0;
1160 //bodyBuffer = null;
1162 sendChunked = false;
1163 webHeaders.RemoveInternal ("Transfer-Encoding");
1165 uriString = webResponse.Headers ["Location"];
1167 if (uriString == null)
1168 throw new WebException ("No Location header found for " + (int) code,
1169 WebExceptionStatus.ProtocolError);
1171 Uri prev = actualUri;
1173 actualUri = new Uri (actualUri, uriString);
1174 } catch (Exception) {
1175 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
1176 uriString, (int) code),
1177 WebExceptionStatus.ProtocolError);
1180 hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
1184 string GetHeaders ()
1186 bool continue100 = false;
1189 webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
1190 webHeaders.RemoveInternal ("Content-Length");
1191 } else if (contentLength != -1) {
1192 if (auth_state.NtlmAuthState == NtlmAuthState.Challenge || proxy_auth_state.NtlmAuthState == NtlmAuthState.Challenge) {
1193 // We don't send any body with the NTLM Challenge request.
1194 webHeaders.SetInternal ("Content-Length", "0");
1196 if (contentLength > 0)
1199 if (haveContentLength || gotRequestStream || contentLength > 0)
1200 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
1202 webHeaders.RemoveInternal ("Transfer-Encoding");
1204 webHeaders.RemoveInternal ("Content-Length");
1207 if (actualVersion == HttpVersion.Version11 && continue100 &&
1208 servicePoint.SendContinue) { // RFC2616 8.2.3
1209 webHeaders.RemoveAndAdd ("Expect" , "100-continue");
1210 expectContinue = true;
1212 webHeaders.RemoveInternal ("Expect");
1213 expectContinue = false;
1216 bool proxy_query = ProxyQuery;
1217 string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
1218 webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
1219 Version proto_version = servicePoint.ProtocolVersion;
1220 bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
1222 if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
1223 if (webHeaders[connectionHeader] == null
1224 || webHeaders[connectionHeader].IndexOf ("keep-alive", StringComparison.OrdinalIgnoreCase) == -1)
1225 webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
1226 } else if (!keepAlive && version == HttpVersion.Version11) {
1227 webHeaders.RemoveAndAdd (connectionHeader, "close");
1230 webHeaders.SetInternal ("Host", Host);
1231 if (cookieContainer != null) {
1232 string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
1233 if (cookieHeader != "")
1234 webHeaders.RemoveAndAdd ("Cookie", cookieHeader);
1236 webHeaders.RemoveInternal ("Cookie");
1239 string accept_encoding = null;
1240 if ((auto_decomp & DecompressionMethods.GZip) != 0)
1241 accept_encoding = "gzip";
1242 if ((auto_decomp & DecompressionMethods.Deflate) != 0)
1243 accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
1244 if (accept_encoding != null)
1245 webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);
1247 if (!usedPreAuth && preAuthenticate)
1248 DoPreAuthenticate ();
1250 return webHeaders.ToString ();
1253 void DoPreAuthenticate ()
1255 bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1256 ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
1257 Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
1261 webHeaders.RemoveInternal ("Proxy-Authorization");
1262 webHeaders.RemoveInternal ("Authorization");
1263 string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
1264 webHeaders [authHeader] = auth.Message;
1268 internal void SetWriteStreamError (WebExceptionStatus status, Exception exc)
1273 WebAsyncResult r = asyncWrite;
1281 msg = "Error: " + status;
1282 wex = new WebException (msg, status);
1284 msg = String.Format ("Error: {0} ({1})", status, exc.Message);
1285 wex = new WebException (msg, exc, status);
1287 r.SetCompleted (false, wex);
1292 internal byte[] GetRequestHeaders ()
1294 StringBuilder req = new StringBuilder ();
1297 query = actualUri.PathAndQuery;
1299 query = String.Format ("{0}://{1}{2}", actualUri.Scheme,
1301 actualUri.PathAndQuery);
1304 if (!force_version && servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
1305 actualVersion = servicePoint.ProtocolVersion;
1307 actualVersion = version;
1310 req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
1311 actualVersion.Major, actualVersion.Minor);
1312 req.Append (GetHeaders ());
1313 string reqstr = req.ToString ();
1314 return Encoding.UTF8.GetBytes (reqstr);
1317 internal void SetWriteStream (WebConnectionStream stream)
1322 writeStream = stream;
1323 if (bodyBuffer != null) {
1324 webHeaders.RemoveInternal ("Transfer-Encoding");
1325 contentLength = bodyBufferLength;
1326 writeStream.SendChunked = false;
1330 var result = writeStream.SetHeadersAsync (false, SetWriteStreamCB, null);
1332 SetWriteStreamCB (null);
1333 } catch (Exception exc) {
1334 SetWriteStreamErrorCB (exc);
1338 void SetWriteStreamErrorCB (Exception exc)
1340 WebException wexc = exc as WebException;
1342 SetWriteStreamError (wexc.Status, wexc);
1344 SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
1347 void SetWriteStreamCB (IAsyncResult ar)
1349 WebAsyncResult result = ar as WebAsyncResult;
1351 if (result != null && result.Exception != null) {
1352 SetWriteStreamErrorCB (result.Exception);
1358 WebAsyncResult writeRequestResult = null;
1360 if (bodyBuffer != null) {
1361 // The body has been written and buffered. The request "user"
1362 // won't write it again, so we must do it.
1363 if (auth_state.NtlmAuthState != NtlmAuthState.Challenge && proxy_auth_state.NtlmAuthState != NtlmAuthState.Challenge) {
1364 // FIXME: this is a blocking call on the thread pool that could lead to thread pool exhaustion
1365 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1367 writeStream.Close ();
1369 } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
1370 method != "TRACE") {
1371 if (getResponseCalled && !writeStream.RequestWritten)
1372 writeRequestResult = writeStream.WriteRequestAsync (SetWriteStreamCB2, null);
1375 if (writeRequestResult == null)
1376 SetWriteStreamCB2 (null);
1379 void SetWriteStreamCB2 (IAsyncResult ar)
1381 var result = (WebAsyncResult)ar;
1382 if (result != null && result.GotException) {
1383 SetWriteStreamErrorCB (result.Exception);
1387 if (asyncWrite != null) {
1388 asyncWrite.SetCompleted (false, writeStream);
1389 asyncWrite.DoCallback ();
1394 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1399 string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1400 WebAsyncResult r = asyncRead;
1405 if (e is WebException) {
1406 wexc = (WebException) e;
1408 wexc = new WebException (msg, e, status, null);
1411 if (!r.IsCompleted) {
1412 r.SetCompleted (false, wexc);
1414 } else if (r == asyncWrite) {
1417 haveResponse = true;
1421 haveResponse = true;
1427 void CheckSendError (WebConnectionData data)
1429 // Got here, but no one called GetResponse
1430 int status = data.StatusCode;
1431 if (status < 400 || status == 401 || status == 407)
1434 if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1435 // The request has not been completely sent and we got here!
1436 // We should probably just close and cause an error in any case,
1437 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse);
1438 if (allowBuffering || sendChunked || writeStream.totalWritten >= contentLength) {
1439 webResponse.ReadAll ();
1441 writeStream.IgnoreIOErrors = true;
1446 bool HandleNtlmAuth (WebAsyncResult r)
1448 bool isProxy = webResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired;
1449 if ((isProxy ? proxy_auth_state.NtlmAuthState : auth_state.NtlmAuthState) == NtlmAuthState.None)
1452 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1454 WebConnection cnc = wce.Connection;
1455 cnc.PriorityRequest = this;
1456 ICredentials creds = !isProxy ? credentials : proxy.Credentials;
1457 if (creds != null) {
1458 cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM");
1459 cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah;
1463 finished_reading = false;
1464 haveResponse = false;
1465 webResponse.ReadAll ();
1470 internal void SetResponseData (WebConnectionData data)
1474 if (data.stream != null)
1475 data.stream.Close ();
1479 WebException wexc = null;
1481 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1482 } catch (Exception e) {
1483 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null);
1484 if (data.stream != null)
1485 data.stream.Close ();
1488 if (wexc == null && (method == "POST" || method == "PUT")) {
1489 CheckSendError (data);
1490 if (saved_exc != null)
1491 wexc = (WebException) saved_exc;
1494 WebAsyncResult r = asyncRead;
1496 bool forced = false;
1497 if (r == null && webResponse != null) {
1498 // This is a forced completion (302, 204)...
1500 r = new WebAsyncResult (null, null);
1501 r.SetCompleted (false, webResponse);
1506 haveResponse = true;
1508 r.SetCompleted (false, wexc);
1513 bool isProxy = ProxyQuery && !proxy.IsBypassed (actualUri);
1517 redirected = CheckFinalStatus (r);
1519 if ((isProxy ? proxy_auth_state.IsNtlmAuthenticated : auth_state.IsNtlmAuthenticated) &&
1520 webResponse != null && (int)webResponse.StatusCode < 400) {
1521 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1523 WebConnection cnc = wce.Connection;
1524 cnc.NtlmAuthenticated = true;
1528 // clear internal buffer so that it does not
1529 // hold possible big buffer (bug #397627)
1530 if (writeStream != null)
1531 writeStream.KillBuffer ();
1533 haveResponse = true;
1534 r.SetCompleted (false, webResponse);
1537 if (webResponse != null) {
1538 if (HandleNtlmAuth (r))
1540 webResponse.Close ();
1542 finished_reading = false;
1543 haveResponse = false;
1546 servicePoint = GetServicePoint ();
1547 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1549 } catch (WebException wexc2) {
1552 haveResponse = true;
1554 r.SetCompleted (false, wexc2);
1557 } catch (Exception ex) {
1558 wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null);
1561 haveResponse = true;
1563 r.SetCompleted (false, wexc);
1571 struct AuthorizationState
1573 readonly HttpWebRequest request;
1574 readonly bool isProxy;
1576 NtlmAuthState ntlm_auth_state;
1578 public bool IsCompleted {
1579 get { return isCompleted; }
1582 public NtlmAuthState NtlmAuthState {
1583 get { return ntlm_auth_state; }
1586 public bool IsNtlmAuthenticated {
1587 get { return isCompleted && ntlm_auth_state != NtlmAuthState.None; }
1590 public AuthorizationState (HttpWebRequest request, bool isProxy)
1592 this.request = request;
1593 this.isProxy = isProxy;
1594 isCompleted = false;
1595 ntlm_auth_state = NtlmAuthState.None;
1598 public bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1600 isCompleted = false;
1601 if (code == HttpStatusCode.Unauthorized && request.credentials == null)
1604 // FIXME: This should never happen!
1605 if (isProxy != (code == HttpStatusCode.ProxyAuthenticationRequired))
1608 if (isProxy && (request.proxy == null || request.proxy.Credentials == null))
1611 string [] authHeaders = response.Headers.GetValues_internal (isProxy ? "Proxy-Authenticate" : "WWW-Authenticate", false);
1612 if (authHeaders == null || authHeaders.Length == 0)
1615 ICredentials creds = (!isProxy) ? request.credentials : request.proxy.Credentials;
1616 Authorization auth = null;
1617 foreach (string authHeader in authHeaders) {
1618 auth = AuthenticationManager.Authenticate (authHeader, request, creds);
1624 request.webHeaders [isProxy ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1625 isCompleted = auth.Complete;
1626 bool is_ntlm = (auth.Module.AuthenticationType == "NTLM");
1628 ntlm_auth_state = (NtlmAuthState)((int) ntlm_auth_state + 1);
1632 public void Reset ()
1634 isCompleted = false;
1635 ntlm_auth_state = NtlmAuthState.None;
1636 request.webHeaders.RemoveInternal (isProxy ? "Proxy-Authorization" : "Authorization");
1639 public override string ToString ()
1641 return string.Format ("{0}AuthState [{1}:{2}]", isProxy ? "Proxy" : "", isCompleted, ntlm_auth_state);
1645 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1647 bool isProxy = code == HttpStatusCode.ProxyAuthenticationRequired;
1648 return isProxy ? proxy_auth_state.CheckAuthorization (response, code) : auth_state.CheckAuthorization (response, code);
1651 // Returns true if redirected
1652 bool CheckFinalStatus (WebAsyncResult result)
1654 if (result.GotException) {
1656 throw result.Exception;
1659 Exception throwMe = result.Exception;
1661 HttpWebResponse resp = result.Response;
1662 WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1663 HttpStatusCode code = 0;
1664 if (throwMe == null && webResponse != null) {
1665 code = webResponse.StatusCode;
1666 if ((!auth_state.IsCompleted && code == HttpStatusCode.Unauthorized && credentials != null) ||
1667 (ProxyQuery && !proxy_auth_state.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) {
1668 if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1669 // Keep the written body, so it can be rewritten in the retry
1670 if (InternalAllowBuffering) {
1671 // NTLM: This is to avoid sending data in the 'challenge' request
1672 // We save it in the first request (first 401), don't send anything
1673 // in the challenge request and send it in the response request along
1674 // with the buffers kept form the first request.
1675 if (auth_state.NtlmAuthState == NtlmAuthState.Challenge || proxy_auth_state.NtlmAuthState == NtlmAuthState.Challenge) {
1676 bodyBuffer = writeStream.WriteBuffer;
1677 bodyBufferLength = writeStream.WriteBufferLength;
1680 } else if (method != "PUT" && method != "POST") {
1688 writeStream.InternalClose ();
1690 webResponse.Close ();
1694 throw new WebException ("This request requires buffering " +
1695 "of data for authentication or " +
1696 "redirection to be sucessful.");
1701 if ((int) code >= 400) {
1702 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1703 (int) code, webResponse.StatusDescription);
1704 throwMe = new WebException (err, null, protoError, webResponse);
1705 webResponse.ReadAll ();
1706 } else if ((int) code == 304 && allowAutoRedirect) {
1707 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1708 (int) code, webResponse.StatusDescription);
1709 throwMe = new WebException (err, null, protoError, webResponse);
1710 } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
1711 throwMe = new WebException ("Max. redirections exceeded.", null,
1712 protoError, webResponse);
1713 webResponse.ReadAll ();
1718 if (throwMe == null) {
1721 if (allowAutoRedirect && c >= 300) {
1722 b = Redirect (result, code, webResponse);
1723 if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1724 bodyBuffer = writeStream.WriteBuffer;
1725 bodyBufferLength = writeStream.WriteBufferLength;
1727 if (b && !unsafe_auth_blah) {
1728 auth_state.Reset ();
1729 proxy_auth_state.Reset ();
1733 if (resp != null && c >= 300 && c != 304)
1742 if (writeStream != null) {
1743 writeStream.InternalClose ();
1752 internal bool ReuseConnection {
1757 internal WebConnection StoredConnection;