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 #if MONO_SECURITY_ALIAS
36 extern alias MonoSecurity;
37 using MonoSecurity::Mono.Security.Interface;
39 using Mono.Security.Interface;
44 using System.Collections;
45 using System.Configuration;
46 using System.Globalization;
49 using System.Net.Cache;
50 using System.Net.Sockets;
51 using System.Net.Security;
52 using System.Runtime.Remoting.Messaging;
53 using System.Runtime.Serialization;
54 using System.Security.Cryptography.X509Certificates;
56 using System.Threading;
57 using Mono.Net.Security;
62 public class HttpWebRequest : WebRequest, ISerializable {
66 bool allowAutoRedirect = true;
67 bool allowBuffering = true;
68 X509CertificateCollection certificates;
69 string connectionGroup;
70 bool haveContentLength;
71 long contentLength = -1;
72 HttpContinueDelegate continueDelegate;
73 CookieContainer cookieContainer;
74 ICredentials credentials;
78 WebHeaderCollection webHeaders;
79 bool keepAlive = true;
80 int maxAutoRedirect = 50;
81 string mediaType = String.Empty;
82 string method = "GET";
83 string initialMethod = "GET";
84 bool pipelined = true;
87 Version version = HttpVersion.Version11;
89 Version actualVersion;
92 ServicePoint servicePoint;
95 WebConnectionStream writeStream;
96 HttpWebResponse webResponse;
97 WebAsyncResult asyncWrite;
98 WebAsyncResult asyncRead;
99 EventHandler abortHandler;
101 bool gotRequestStream;
105 int bodyBufferLength;
106 bool getResponseCalled;
108 object locker = new object ();
109 bool finished_reading;
110 internal WebConnection WebConnection;
111 DecompressionMethods auto_decomp;
112 int maxResponseHeadersLength;
113 static int defaultMaxResponseHeadersLength;
114 int readWriteTimeout = 300000; // ms
115 IMonoTlsProvider tlsProvider;
117 MonoTlsSettings tlsSettings;
119 ServerCertValidationCallback certValidationCallback;
126 AuthorizationState auth_state, proxy_auth_state;
130 internal Action<Stream> ResendContentFactory;
133 static HttpWebRequest ()
135 defaultMaxResponseHeadersLength = 64 * 1024;
137 NetConfig config = ConfigurationSettings.GetConfig ("system.net/settings") as NetConfig;
138 if (config != null) {
139 int x = config.MaxResponseHeadersLength;
143 defaultMaxResponseHeadersLength = x;
153 HttpWebRequest (Uri uri)
155 this.requestUri = uri;
156 this.actualUri = uri;
157 this.proxy = InternalDefaultWebProxy;
158 this.webHeaders = new WebHeaderCollection (WebHeaderCollectionType.HttpWebRequest);
160 ResetAuthorization ();
164 internal HttpWebRequest (Uri uri, IMonoTlsProvider tlsProvider, MonoTlsSettings settings = null)
167 this.tlsProvider = tlsProvider;
168 this.tlsSettings = settings;
172 [Obsolete ("Serialization is obsoleted for this type", false)]
173 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
175 SerializationInfo info = serializationInfo;
177 requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
178 actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
179 allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
180 allowBuffering = info.GetBoolean ("allowBuffering");
181 certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
182 connectionGroup = info.GetString ("connectionGroup");
183 contentLength = info.GetInt64 ("contentLength");
184 webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
185 keepAlive = info.GetBoolean ("keepAlive");
186 maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
187 mediaType = info.GetString ("mediaType");
188 method = info.GetString ("method");
189 initialMethod = info.GetString ("initialMethod");
190 pipelined = info.GetBoolean ("pipelined");
191 version = (Version) info.GetValue ("version", typeof (Version));
192 proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
193 sendChunked = info.GetBoolean ("sendChunked");
194 timeout = info.GetInt32 ("timeout");
195 redirects = info.GetInt32 ("redirects");
196 host = info.GetString ("host");
197 ResetAuthorization ();
200 void ResetAuthorization ()
202 auth_state = new AuthorizationState (this, false);
203 proxy_auth_state = new AuthorizationState (this, true);
208 void SetSpecialHeaders(string HeaderName, string value) {
209 value = WebHeaderCollection.CheckBadChars(value, true);
210 webHeaders.RemoveInternal(HeaderName);
211 if (value.Length != 0) {
212 webHeaders.AddInternal(HeaderName, value);
216 public string Accept {
217 get { return webHeaders ["Accept"]; }
219 CheckRequestStarted ();
220 SetSpecialHeaders ("Accept", value);
225 get { return actualUri; }
226 internal set { actualUri = value; } // Used by Ftp+proxy
229 public virtual bool AllowAutoRedirect {
230 get { return allowAutoRedirect; }
231 set { this.allowAutoRedirect = value; }
234 public virtual bool AllowWriteStreamBuffering {
235 get { return allowBuffering; }
236 set { allowBuffering = value; }
239 public virtual bool AllowReadStreamBuffering {
240 get { return false; }
243 throw new InvalidOperationException ();
247 static Exception GetMustImplement ()
249 return new NotImplementedException ();
252 public DecompressionMethods AutomaticDecompression
258 CheckRequestStarted ();
263 internal bool InternalAllowBuffering {
265 return allowBuffering && MethodWithBuffer;
269 bool MethodWithBuffer {
271 return method != "HEAD" && method != "GET" &&
272 method != "MKCOL" && method != "CONNECT" &&
277 internal IMonoTlsProvider TlsProvider {
278 get { return tlsProvider; }
282 internal MonoTlsSettings TlsSettings {
283 get { return tlsSettings; }
287 public X509CertificateCollection ClientCertificates {
289 if (certificates == null)
290 certificates = new X509CertificateCollection ();
295 throw new ArgumentNullException ("value");
296 certificates = value;
300 public string Connection {
301 get { return webHeaders ["Connection"]; }
303 CheckRequestStarted ();
305 if (string.IsNullOrEmpty (value)) {
306 webHeaders.RemoveInternal ("Connection");
310 string val = value.ToLowerInvariant ();
311 if (val.Contains ("keep-alive") || val.Contains ("close"))
312 throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
315 value = value + ", Keep-Alive";
317 webHeaders.CheckUpdate ("Connection", value);
321 public override string ConnectionGroupName {
322 get { return connectionGroup; }
323 set { connectionGroup = value; }
326 public override long ContentLength {
327 get { return contentLength; }
329 CheckRequestStarted ();
331 throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
333 contentLength = value;
334 haveContentLength = true;
338 internal long InternalContentLength {
339 set { contentLength = value; }
342 internal bool ThrowOnError { get; set; }
344 public override string ContentType {
345 get { return webHeaders ["Content-Type"]; }
347 SetSpecialHeaders ("Content-Type", value);
351 public HttpContinueDelegate ContinueDelegate {
352 get { return continueDelegate; }
353 set { continueDelegate = value; }
357 public CookieContainer CookieContainer {
358 get { return cookieContainer; }
359 set { cookieContainer = value; }
362 public override ICredentials Credentials {
363 get { return credentials; }
364 set { credentials = value; }
366 public DateTime Date {
368 string date = webHeaders ["Date"];
370 return DateTime.MinValue;
371 return DateTime.ParseExact (date, "r", CultureInfo.InvariantCulture).ToLocalTime ();
374 SetDateHeaderHelper ("Date", value);
378 void SetDateHeaderHelper(string headerName, DateTime dateTime) {
379 if (dateTime == DateTime.MinValue)
380 SetSpecialHeaders(headerName, null); // remove header
382 SetSpecialHeaders(headerName, HttpProtocolUtils.date2string(dateTime));
387 public static new RequestCachePolicy DefaultCachePolicy
390 throw GetMustImplement ();
393 throw GetMustImplement ();
399 public static int DefaultMaximumErrorResponseLength
402 throw GetMustImplement ();
405 throw GetMustImplement ();
409 public string Expect {
410 get { return webHeaders ["Expect"]; }
412 CheckRequestStarted ();
415 val = val.Trim ().ToLower ();
417 if (val == null || val.Length == 0) {
418 webHeaders.RemoveInternal ("Expect");
422 if (val == "100-continue")
423 throw new ArgumentException ("100-Continue cannot be set with this property.",
426 webHeaders.CheckUpdate ("Expect", value);
431 public bool HaveResponse {
432 get { return haveResponse; }
435 public override WebHeaderCollection Headers {
436 get { return webHeaders; }
438 CheckRequestStarted ();
440 WebHeaderCollection webHeaders = value;
441 WebHeaderCollection newWebHeaders = new WebHeaderCollection(WebHeaderCollectionType.HttpWebRequest);
443 // Copy And Validate -
444 // Handle the case where their object tries to change
445 // name, value pairs after they call set, so therefore,
446 // we need to clone their headers.
449 foreach (String headerName in webHeaders.AllKeys ) {
450 newWebHeaders.Add(headerName,webHeaders[headerName]);
453 this.webHeaders = newWebHeaders;
461 return actualUri.Authority;
466 throw new ArgumentNullException ("value");
468 if (!CheckValidHost (actualUri.Scheme, value))
469 throw new ArgumentException ("Invalid host: " + value);
475 static bool CheckValidHost (string scheme, string val)
483 int idx = val.IndexOf ('/');
488 if (IPAddress.TryParse (val, out ipaddr))
491 string u = scheme + "://" + val + "/";
492 return Uri.IsWellFormedUriString (u, UriKind.Absolute);
495 public DateTime IfModifiedSince {
497 string str = webHeaders ["If-Modified-Since"];
501 return MonoHttpDate.Parse (str);
502 } catch (Exception) {
507 CheckRequestStarted ();
509 webHeaders.SetInternal ("If-Modified-Since",
510 value.ToUniversalTime ().ToString ("r", null));
511 // TODO: check last param when using different locale
515 public bool KeepAlive {
524 public int MaximumAutomaticRedirections {
525 get { return maxAutoRedirect; }
528 throw new ArgumentException ("Must be > 0", "value");
530 maxAutoRedirect = value;
534 [MonoTODO ("Use this")]
535 public int MaximumResponseHeadersLength {
536 get { return maxResponseHeadersLength; }
537 set { maxResponseHeadersLength = value; }
540 [MonoTODO ("Use this")]
541 public static int DefaultMaximumResponseHeadersLength {
542 get { return defaultMaxResponseHeadersLength; }
543 set { defaultMaxResponseHeadersLength = value; }
546 public int ReadWriteTimeout {
547 get { return readWriteTimeout; }
550 throw new InvalidOperationException ("The request has already been sent.");
553 throw new ArgumentOutOfRangeException ("value", "Must be >= -1");
555 readWriteTimeout = value;
560 public int ContinueTimeout {
561 get { throw new NotImplementedException (); }
562 set { throw new NotImplementedException (); }
565 public string MediaType {
566 get { return mediaType; }
572 public override string Method {
573 get { return this.method; }
575 if (value == null || value.Trim () == "")
576 throw new ArgumentException ("not a valid method");
578 method = value.ToUpperInvariant ();
579 if (method != "HEAD" && method != "GET" && method != "POST" && method != "PUT" &&
580 method != "DELETE" && method != "CONNECT" && method != "TRACE" &&
587 public bool Pipelined {
588 get { return pipelined; }
589 set { pipelined = value; }
592 public override bool PreAuthenticate {
593 get { return preAuthenticate; }
594 set { preAuthenticate = value; }
597 public Version ProtocolVersion {
598 get { return version; }
600 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
601 throw new ArgumentException ("value");
603 force_version = true;
608 public override IWebProxy Proxy {
609 get { return proxy; }
611 CheckRequestStarted ();
613 servicePoint = null; // we may need a new one
618 public string Referer {
619 get { return webHeaders ["Referer"]; }
621 CheckRequestStarted ();
622 if (value == null || value.Trim().Length == 0) {
623 webHeaders.RemoveInternal ("Referer");
626 webHeaders.SetInternal ("Referer", value);
630 public override Uri RequestUri {
631 get { return requestUri; }
634 public bool SendChunked {
635 get { return sendChunked; }
637 CheckRequestStarted ();
642 public ServicePoint ServicePoint {
643 get { return GetServicePoint (); }
646 internal ServicePoint ServicePointNoLock {
647 get { return servicePoint; }
649 public virtual bool SupportsCookieContainer {
651 // The managed implementation supports the cookie container
652 // it is only Silverlight that returns false here
656 public override int Timeout {
657 get { return timeout; }
660 throw new ArgumentOutOfRangeException ("value");
666 public string TransferEncoding {
667 get { return webHeaders ["Transfer-Encoding"]; }
669 CheckRequestStarted ();
672 val = val.Trim ().ToLower ();
674 if (val == null || val.Length == 0) {
675 webHeaders.RemoveInternal ("Transfer-Encoding");
679 if (val == "chunked")
680 throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
683 throw new ArgumentException ("SendChunked must be True", "value");
685 webHeaders.CheckUpdate ("Transfer-Encoding", value);
689 public override bool UseDefaultCredentials
691 get { return CredentialCache.DefaultCredentials == Credentials; }
692 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
695 public string UserAgent {
696 get { return webHeaders ["User-Agent"]; }
697 set { webHeaders.SetInternal ("User-Agent", value); }
700 bool unsafe_auth_blah;
701 public bool UnsafeAuthenticatedConnectionSharing
703 get { return unsafe_auth_blah; }
704 set { unsafe_auth_blah = value; }
707 internal bool GotRequestStream {
708 get { return gotRequestStream; }
711 internal bool ExpectContinue {
712 get { return expectContinue; }
713 set { expectContinue = value; }
716 internal Uri AuthUri {
717 get { return actualUri; }
720 internal bool ProxyQuery {
721 get { return servicePoint.UsesProxy && !servicePoint.UseConnect; }
724 internal ServerCertValidationCallback ServerCertValidationCallback {
725 get { return certValidationCallback; }
728 public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
730 if (certValidationCallback == null)
732 return certValidationCallback.ValidationCallback;
737 certValidationCallback = null;
739 certValidationCallback = new ServerCertValidationCallback (value);
745 internal ServicePoint GetServicePoint ()
748 if (hostChanged || servicePoint == null) {
749 servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
757 public void AddRange (int range)
759 AddRange ("bytes", (long) range);
762 public void AddRange (int from, int to)
764 AddRange ("bytes", (long) from, (long) to);
767 public void AddRange (string rangeSpecifier, int range)
769 AddRange (rangeSpecifier, (long) range);
772 public void AddRange (string rangeSpecifier, int from, int to)
774 AddRange (rangeSpecifier, (long) from, (long) to);
777 void AddRange (long range)
779 AddRange ("bytes", (long) range);
783 void AddRange (long from, long to)
785 AddRange ("bytes", from, to);
789 void AddRange (string rangeSpecifier, long range)
791 if (rangeSpecifier == null)
792 throw new ArgumentNullException ("rangeSpecifier");
793 if (!WebHeaderCollection.IsValidToken (rangeSpecifier))
794 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
796 string r = webHeaders ["Range"];
798 r = rangeSpecifier + "=";
800 string old_specifier = r.Substring (0, r.IndexOf ('='));
801 if (String.Compare (old_specifier, rangeSpecifier, StringComparison.OrdinalIgnoreCase) != 0)
802 throw new InvalidOperationException ("A different range specifier is already in use");
806 string n = range.ToString (CultureInfo.InvariantCulture);
811 webHeaders.ChangeInternal ("Range", r);
815 void AddRange (string rangeSpecifier, long from, long to)
817 if (rangeSpecifier == null)
818 throw new ArgumentNullException ("rangeSpecifier");
819 if (!WebHeaderCollection.IsValidToken (rangeSpecifier))
820 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
821 if (from > to || from < 0)
822 throw new ArgumentOutOfRangeException ("from");
824 throw new ArgumentOutOfRangeException ("to");
826 string r = webHeaders ["Range"];
828 r = rangeSpecifier + "=";
832 r = String.Format ("{0}{1}-{2}", r, from, to);
833 webHeaders.ChangeInternal ("Range", r);
837 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
840 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
842 bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD" ||
844 if (method == null || !send)
845 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
847 if (contentLength == -1 && !sendChunked && !allowBuffering && KeepAlive)
848 throw new ProtocolViolationException ("Content-Length not set");
850 string transferEncoding = TransferEncoding;
851 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
852 throw new ProtocolViolationException ("SendChunked should be true.");
856 if (getResponseCalled)
857 throw new InvalidOperationException ("The operation cannot be performed once the request has been submitted.");
859 if (asyncWrite != null) {
860 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
861 "method while a previous call is still in progress.");
864 asyncWrite = new WebAsyncResult (this, callback, state);
865 initialMethod = method;
867 if (writeStream != null) {
868 asyncWrite.SetCompleted (true, writeStream);
869 asyncWrite.DoCallback ();
874 gotRequestStream = true;
875 WebAsyncResult result = asyncWrite;
879 servicePoint = GetServicePoint ();
880 abortHandler = servicePoint.SendRequest (this, connectionGroup);
886 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
888 if (asyncResult == null)
889 throw new ArgumentNullException ("asyncResult");
891 WebAsyncResult result = asyncResult as WebAsyncResult;
893 throw new ArgumentException ("Invalid IAsyncResult");
896 result.WaitUntilComplete ();
898 Exception e = result.Exception;
902 return result.WriteStream;
905 public override Stream GetRequestStream()
907 IAsyncResult asyncResult = asyncWrite;
908 if (asyncResult == null) {
909 asyncResult = BeginGetRequestStream (null, null);
910 asyncWrite = (WebAsyncResult) asyncResult;
913 if (!asyncResult.IsCompleted && !asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
915 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
918 return EndGetRequestStream (asyncResult);
922 public Stream GetRequestStream (out TransportContext context)
924 throw new NotImplementedException ();
927 bool CheckIfForceWrite (SimpleAsyncResult result)
929 if (writeStream == null || writeStream.RequestWritten || !InternalAllowBuffering)
931 if (contentLength < 0 && writeStream.CanWrite == true && writeStream.WriteBufferLength < 0)
934 if (contentLength < 0 && writeStream.WriteBufferLength >= 0)
935 InternalContentLength = writeStream.WriteBufferLength;
937 // This will write the POST/PUT if the write stream already has the expected
938 // amount of bytes in it (ContentLength) (bug #77753) or if the write stream
939 // contains data and it has been closed already (xamarin bug #1512).
941 if (writeStream.WriteBufferLength == contentLength || (contentLength == -1 && writeStream.CanWrite == false))
942 return writeStream.WriteRequestAsync (result);
947 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
950 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
953 throw new ProtocolViolationException ("Method is null.");
955 string transferEncoding = TransferEncoding;
956 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
957 throw new ProtocolViolationException ("SendChunked should be true.");
959 Monitor.Enter (locker);
960 getResponseCalled = true;
961 if (asyncRead != null && !haveResponse) {
962 Monitor.Exit (locker);
963 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
964 "method while a previous call is still in progress.");
967 asyncRead = new WebAsyncResult (this, callback, state);
968 WebAsyncResult aread = asyncRead;
969 initialMethod = method;
971 SimpleAsyncResult.RunWithLock (locker, CheckIfForceWrite, inner => {
972 var synch = inner.CompletedSynchronouslyPeek;
974 if (inner.GotException) {
975 aread.SetCompleted (synch, inner.Exception);
981 Exception saved = saved_exc;
982 if (webResponse != null) {
984 aread.SetCompleted (synch, webResponse);
986 aread.SetCompleted (synch, saved);
990 } else if (saved != null) {
991 aread.SetCompleted (synch, saved);
1003 servicePoint = GetServicePoint ();
1004 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1005 } catch (Exception ex) {
1006 aread.SetCompleted (synch, ex);
1007 aread.DoCallback ();
1014 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
1016 if (asyncResult == null)
1017 throw new ArgumentNullException ("asyncResult");
1019 WebAsyncResult result = asyncResult as WebAsyncResult;
1021 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
1023 if (!result.WaitUntilComplete (timeout, false)) {
1025 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
1028 if (result.GotException)
1029 throw result.Exception;
1031 return result.Response;
1034 public Stream EndGetRequestStream (IAsyncResult asyncResult, out TransportContext transportContext)
1036 transportContext = null;
1037 return EndGetRequestStream (asyncResult);
1040 public override WebResponse GetResponse()
1042 WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
1043 return EndGetResponse (result);
1046 internal bool FinishedReading {
1047 get { return finished_reading; }
1048 set { finished_reading = value; }
1051 internal bool Aborted {
1052 get { return Interlocked.CompareExchange (ref aborted, 0, 0) == 1; }
1055 public override void Abort ()
1057 if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
1060 if (haveResponse && finished_reading)
1063 haveResponse = true;
1064 if (abortHandler != null) {
1066 abortHandler (this, EventArgs.Empty);
1067 } catch (Exception) {}
1068 abortHandler = null;
1071 if (asyncWrite != null) {
1072 WebAsyncResult r = asyncWrite;
1073 if (!r.IsCompleted) {
1075 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
1076 r.SetCompleted (false, wexc);
1083 if (asyncRead != null) {
1084 WebAsyncResult r = asyncRead;
1085 if (!r.IsCompleted) {
1087 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
1088 r.SetCompleted (false, wexc);
1095 if (writeStream != null) {
1097 writeStream.Close ();
1102 if (webResponse != null) {
1104 webResponse.Close ();
1110 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
1111 StreamingContext streamingContext)
1113 GetObjectData (serializationInfo, streamingContext);
1116 protected override void GetObjectData (SerializationInfo serializationInfo,
1117 StreamingContext streamingContext)
1119 SerializationInfo info = serializationInfo;
1121 info.AddValue ("requestUri", requestUri, typeof (Uri));
1122 info.AddValue ("actualUri", actualUri, typeof (Uri));
1123 info.AddValue ("allowAutoRedirect", allowAutoRedirect);
1124 info.AddValue ("allowBuffering", allowBuffering);
1125 info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
1126 info.AddValue ("connectionGroup", connectionGroup);
1127 info.AddValue ("contentLength", contentLength);
1128 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
1129 info.AddValue ("keepAlive", keepAlive);
1130 info.AddValue ("maxAutoRedirect", maxAutoRedirect);
1131 info.AddValue ("mediaType", mediaType);
1132 info.AddValue ("method", method);
1133 info.AddValue ("initialMethod", initialMethod);
1134 info.AddValue ("pipelined", pipelined);
1135 info.AddValue ("version", version, typeof (Version));
1136 info.AddValue ("proxy", proxy, typeof (IWebProxy));
1137 info.AddValue ("sendChunked", sendChunked);
1138 info.AddValue ("timeout", timeout);
1139 info.AddValue ("redirects", redirects);
1140 info.AddValue ("host", host);
1143 void CheckRequestStarted ()
1146 throw new InvalidOperationException ("request started");
1149 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
1151 if (continueDelegate != null)
1152 continueDelegate (statusCode, headers);
1155 void RewriteRedirectToGet ()
1158 webHeaders.RemoveInternal ("Transfer-Encoding");
1159 sendChunked = false;
1162 bool Redirect (WebAsyncResult result, HttpStatusCode code, WebResponse response)
1166 string uriString = null;
1168 case HttpStatusCode.Ambiguous: // 300
1169 e = new WebException ("Ambiguous redirect.");
1171 case HttpStatusCode.MovedPermanently: // 301
1172 case HttpStatusCode.Redirect: // 302
1173 if (method == "POST")
1174 RewriteRedirectToGet ();
1176 case HttpStatusCode.TemporaryRedirect: // 307
1178 case HttpStatusCode.SeeOther: //303
1179 RewriteRedirectToGet ();
1181 case HttpStatusCode.NotModified: // 304
1183 case HttpStatusCode.UseProxy: // 305
1184 e = new NotImplementedException ("Proxy support not available.");
1186 case HttpStatusCode.Unused: // 306
1188 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
1192 if (method != "GET" && !InternalAllowBuffering && (writeStream.WriteBufferLength > 0 || contentLength > 0))
1193 e = new WebException ("The request requires buffering data to succeed.", null, WebExceptionStatus.ProtocolError, webResponse);
1198 if (AllowWriteStreamBuffering || method == "GET")
1201 uriString = webResponse.Headers ["Location"];
1203 if (uriString == null)
1204 throw new WebException ("No Location header found for " + (int) code,
1205 WebExceptionStatus.ProtocolError);
1207 Uri prev = actualUri;
1209 actualUri = new Uri (actualUri, uriString);
1210 } catch (Exception) {
1211 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
1212 uriString, (int) code),
1213 WebExceptionStatus.ProtocolError);
1216 hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
1220 string GetHeaders ()
1222 bool continue100 = false;
1225 webHeaders.ChangeInternal ("Transfer-Encoding", "chunked");
1226 webHeaders.RemoveInternal ("Content-Length");
1227 } else if (contentLength != -1) {
1228 if (auth_state.NtlmAuthState == NtlmAuthState.Challenge || proxy_auth_state.NtlmAuthState == NtlmAuthState.Challenge) {
1229 // We don't send any body with the NTLM Challenge request.
1230 if (haveContentLength || gotRequestStream || contentLength > 0)
1231 webHeaders.SetInternal ("Content-Length", "0");
1233 webHeaders.RemoveInternal ("Content-Length");
1235 if (contentLength > 0)
1238 if (haveContentLength || gotRequestStream || contentLength > 0)
1239 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
1241 webHeaders.RemoveInternal ("Transfer-Encoding");
1243 webHeaders.RemoveInternal ("Content-Length");
1246 if (actualVersion == HttpVersion.Version11 && continue100 &&
1247 servicePoint.SendContinue) { // RFC2616 8.2.3
1248 webHeaders.ChangeInternal ("Expect" , "100-continue");
1249 expectContinue = true;
1251 webHeaders.RemoveInternal ("Expect");
1252 expectContinue = false;
1255 bool proxy_query = ProxyQuery;
1256 string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
1257 webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
1258 Version proto_version = servicePoint.ProtocolVersion;
1259 bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
1261 if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
1262 if (webHeaders[connectionHeader] == null
1263 || webHeaders[connectionHeader].IndexOf ("keep-alive", StringComparison.OrdinalIgnoreCase) == -1)
1264 webHeaders.ChangeInternal (connectionHeader, "keep-alive");
1265 } else if (!keepAlive && version == HttpVersion.Version11) {
1266 webHeaders.ChangeInternal (connectionHeader, "close");
1269 webHeaders.SetInternal ("Host", Host);
1270 if (cookieContainer != null) {
1271 string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
1272 if (cookieHeader != "")
1273 webHeaders.ChangeInternal ("Cookie", cookieHeader);
1275 webHeaders.RemoveInternal ("Cookie");
1278 string accept_encoding = null;
1279 if ((auto_decomp & DecompressionMethods.GZip) != 0)
1280 accept_encoding = "gzip";
1281 if ((auto_decomp & DecompressionMethods.Deflate) != 0)
1282 accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
1283 if (accept_encoding != null)
1284 webHeaders.ChangeInternal ("Accept-Encoding", accept_encoding);
1286 if (!usedPreAuth && preAuthenticate)
1287 DoPreAuthenticate ();
1289 return webHeaders.ToString ();
1292 void DoPreAuthenticate ()
1294 bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1295 ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
1296 Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
1300 webHeaders.RemoveInternal ("Proxy-Authorization");
1301 webHeaders.RemoveInternal ("Authorization");
1302 string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
1303 webHeaders [authHeader] = auth.Message;
1307 internal void SetWriteStreamError (WebExceptionStatus status, Exception exc)
1312 WebAsyncResult r = asyncWrite;
1320 msg = "Error: " + status;
1321 wex = new WebException (msg, status);
1323 wex = exc as WebException;
1325 msg = String.Format ("Error: {0} ({1})", status, exc.Message);
1326 wex = new WebException (msg, status, WebExceptionInternalStatus.RequestFatal, exc);
1329 r.SetCompleted (false, wex);
1334 internal byte[] GetRequestHeaders ()
1336 StringBuilder req = new StringBuilder ();
1339 query = actualUri.PathAndQuery;
1341 query = String.Format ("{0}://{1}{2}", actualUri.Scheme,
1343 actualUri.PathAndQuery);
1346 if (!force_version && servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
1347 actualVersion = servicePoint.ProtocolVersion;
1349 actualVersion = version;
1352 req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
1353 actualVersion.Major, actualVersion.Minor);
1354 req.Append (GetHeaders ());
1355 string reqstr = req.ToString ();
1356 return Encoding.UTF8.GetBytes (reqstr);
1359 internal void SetWriteStream (WebConnectionStream stream)
1364 writeStream = stream;
1365 if (bodyBuffer != null) {
1366 webHeaders.RemoveInternal ("Transfer-Encoding");
1367 contentLength = bodyBufferLength;
1368 writeStream.SendChunked = false;
1371 writeStream.SetHeadersAsync (false, result => {
1372 if (result.GotException) {
1373 SetWriteStreamError (result.Exception);
1379 SetWriteStreamInner (inner => {
1380 if (inner.GotException) {
1381 SetWriteStreamError (inner.Exception);
1385 if (asyncWrite != null) {
1386 asyncWrite.SetCompleted (inner.CompletedSynchronouslyPeek, writeStream);
1387 asyncWrite.DoCallback ();
1394 void SetWriteStreamInner (SimpleAsyncCallback callback)
1396 SimpleAsyncResult.Run (result => {
1397 if (bodyBuffer != null) {
1398 // The body has been written and buffered. The request "user"
1399 // won't write it again, so we must do it.
1400 if (auth_state.NtlmAuthState != NtlmAuthState.Challenge && proxy_auth_state.NtlmAuthState != NtlmAuthState.Challenge) {
1401 // FIXME: this is a blocking call on the thread pool that could lead to thread pool exhaustion
1402 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1404 writeStream.Close ();
1406 } else if (MethodWithBuffer) {
1407 if (getResponseCalled && !writeStream.RequestWritten)
1408 return writeStream.WriteRequestAsync (result);
1415 void SetWriteStreamError (Exception exc)
1417 WebException wexc = exc as WebException;
1419 SetWriteStreamError (wexc.Status, wexc);
1421 SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
1424 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1429 string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1430 WebAsyncResult r = asyncRead;
1435 if (e is WebException) {
1436 wexc = (WebException) e;
1438 wexc = new WebException (msg, e, status, null);
1441 if (!r.IsCompleted) {
1442 r.SetCompleted (false, wexc);
1444 } else if (r == asyncWrite) {
1447 haveResponse = true;
1451 haveResponse = true;
1457 void CheckSendError (WebConnectionData data)
1459 // Got here, but no one called GetResponse
1460 int status = data.StatusCode;
1461 if (status < 400 || status == 401 || status == 407)
1464 if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1465 // The request has not been completely sent and we got here!
1466 // We should probably just close and cause an error in any case,
1467 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse);
1468 if (allowBuffering || sendChunked || writeStream.totalWritten >= contentLength) {
1469 webResponse.ReadAll ();
1471 writeStream.IgnoreIOErrors = true;
1476 bool HandleNtlmAuth (WebAsyncResult r)
1478 bool isProxy = webResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired;
1479 if ((isProxy ? proxy_auth_state.NtlmAuthState : auth_state.NtlmAuthState) == NtlmAuthState.None)
1482 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1484 WebConnection cnc = wce.Connection;
1485 cnc.PriorityRequest = this;
1486 ICredentials creds = (!isProxy || proxy == null) ? credentials : proxy.Credentials;
1487 if (creds != null) {
1488 cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM");
1489 cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah;
1493 finished_reading = false;
1494 haveResponse = false;
1495 webResponse.ReadAll ();
1500 internal void SetResponseData (WebConnectionData data)
1504 if (data.stream != null)
1505 data.stream.Close ();
1509 WebException wexc = null;
1511 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1512 } catch (Exception e) {
1513 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null);
1514 if (data.stream != null)
1515 data.stream.Close ();
1518 if (wexc == null && (method == "POST" || method == "PUT")) {
1519 CheckSendError (data);
1520 if (saved_exc != null)
1521 wexc = (WebException) saved_exc;
1524 WebAsyncResult r = asyncRead;
1526 bool forced = false;
1527 if (r == null && webResponse != null) {
1528 // This is a forced completion (302, 204)...
1530 r = new WebAsyncResult (null, null);
1531 r.SetCompleted (false, webResponse);
1536 haveResponse = true;
1538 r.SetCompleted (false, wexc);
1543 bool isProxy = ProxyQuery && proxy != null && !proxy.IsBypassed (actualUri);
1547 redirected = CheckFinalStatus (r);
1549 if ((isProxy ? proxy_auth_state.IsNtlmAuthenticated : auth_state.IsNtlmAuthenticated) &&
1550 webResponse != null && (int)webResponse.StatusCode < 400) {
1551 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1553 WebConnection cnc = wce.Connection;
1554 cnc.NtlmAuthenticated = true;
1558 // clear internal buffer so that it does not
1559 // hold possible big buffer (bug #397627)
1560 if (writeStream != null)
1561 writeStream.KillBuffer ();
1563 haveResponse = true;
1564 r.SetCompleted (false, webResponse);
1568 sendChunked = false;
1569 webHeaders.RemoveInternal ("Transfer-Encoding");
1572 if (webResponse != null) {
1573 if (HandleNtlmAuth (r))
1575 webResponse.Close ();
1577 finished_reading = false;
1578 haveResponse = false;
1581 servicePoint = GetServicePoint ();
1582 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1584 } catch (WebException wexc2) {
1587 haveResponse = true;
1589 r.SetCompleted (false, wexc2);
1592 } catch (Exception ex) {
1593 wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null);
1596 haveResponse = true;
1598 r.SetCompleted (false, wexc);
1606 struct AuthorizationState
1608 readonly HttpWebRequest request;
1609 readonly bool isProxy;
1611 NtlmAuthState ntlm_auth_state;
1613 public bool IsCompleted {
1614 get { return isCompleted; }
1617 public NtlmAuthState NtlmAuthState {
1618 get { return ntlm_auth_state; }
1621 public bool IsNtlmAuthenticated {
1622 get { return isCompleted && ntlm_auth_state != NtlmAuthState.None; }
1625 public AuthorizationState (HttpWebRequest request, bool isProxy)
1627 this.request = request;
1628 this.isProxy = isProxy;
1629 isCompleted = false;
1630 ntlm_auth_state = NtlmAuthState.None;
1633 public bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1635 isCompleted = false;
1636 if (code == HttpStatusCode.Unauthorized && request.credentials == null)
1639 // FIXME: This should never happen!
1640 if (isProxy != (code == HttpStatusCode.ProxyAuthenticationRequired))
1643 if (isProxy && (request.proxy == null || request.proxy.Credentials == null))
1646 string [] authHeaders = response.Headers.GetValues (isProxy ? "Proxy-Authenticate" : "WWW-Authenticate");
1647 if (authHeaders == null || authHeaders.Length == 0)
1650 ICredentials creds = (!isProxy) ? request.credentials : request.proxy.Credentials;
1651 Authorization auth = null;
1652 foreach (string authHeader in authHeaders) {
1653 auth = AuthenticationManager.Authenticate (authHeader, request, creds);
1659 request.webHeaders [isProxy ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1660 isCompleted = auth.Complete;
1661 bool is_ntlm = (auth.ModuleAuthenticationType == "NTLM");
1663 ntlm_auth_state = (NtlmAuthState)((int) ntlm_auth_state + 1);
1667 public void Reset ()
1669 isCompleted = false;
1670 ntlm_auth_state = NtlmAuthState.None;
1671 request.webHeaders.RemoveInternal (isProxy ? "Proxy-Authorization" : "Authorization");
1674 public override string ToString ()
1676 return string.Format ("{0}AuthState [{1}:{2}]", isProxy ? "Proxy" : "", isCompleted, ntlm_auth_state);
1680 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1682 bool isProxy = code == HttpStatusCode.ProxyAuthenticationRequired;
1683 return isProxy ? proxy_auth_state.CheckAuthorization (response, code) : auth_state.CheckAuthorization (response, code);
1686 // Returns true if redirected
1687 bool CheckFinalStatus (WebAsyncResult result)
1689 if (result.GotException) {
1691 throw result.Exception;
1694 Exception throwMe = result.Exception;
1696 HttpWebResponse resp = result.Response;
1697 WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1698 HttpStatusCode code = 0;
1699 if (throwMe == null && webResponse != null) {
1700 code = webResponse.StatusCode;
1701 if ((!auth_state.IsCompleted && code == HttpStatusCode.Unauthorized && credentials != null) ||
1702 (ProxyQuery && !proxy_auth_state.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) {
1703 if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1704 // Keep the written body, so it can be rewritten in the retry
1705 if (MethodWithBuffer) {
1706 if (AllowWriteStreamBuffering) {
1707 if (writeStream.WriteBufferLength > 0) {
1708 bodyBuffer = writeStream.WriteBuffer;
1709 bodyBufferLength = writeStream.WriteBufferLength;
1716 // Buffering is not allowed but we have alternative way to get same content (we
1717 // need to resent it due to NTLM Authentication).
1719 if (ResendContentFactory != null) {
1720 using (var ms = new MemoryStream ()) {
1721 ResendContentFactory (ms);
1722 bodyBuffer = ms.ToArray ();
1723 bodyBufferLength = bodyBuffer.Length;
1727 } else if (method != "PUT" && method != "POST") {
1735 writeStream.InternalClose ();
1737 webResponse.Close ();
1741 throw new WebException ("This request requires buffering " +
1742 "of data for authentication or " +
1743 "redirection to be sucessful.");
1748 if ((int) code >= 400) {
1749 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1750 (int) code, webResponse.StatusDescription);
1751 throwMe = new WebException (err, null, protoError, webResponse);
1752 webResponse.ReadAll ();
1753 } else if ((int) code == 304 && allowAutoRedirect) {
1754 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1755 (int) code, webResponse.StatusDescription);
1756 throwMe = new WebException (err, null, protoError, webResponse);
1757 } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
1758 throwMe = new WebException ("Max. redirections exceeded.", null,
1759 protoError, webResponse);
1760 webResponse.ReadAll ();
1765 if (throwMe == null) {
1768 if (allowAutoRedirect && c >= 300) {
1769 b = Redirect (result, code, webResponse);
1770 if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1771 bodyBuffer = writeStream.WriteBuffer;
1772 bodyBufferLength = writeStream.WriteBufferLength;
1774 if (b && !unsafe_auth_blah) {
1775 auth_state.Reset ();
1776 proxy_auth_state.Reset ();
1780 if (resp != null && c >= 300 && c != 304)
1789 if (writeStream != null) {
1790 writeStream.InternalClose ();
1799 internal bool ReuseConnection {
1804 internal WebConnection StoredConnection;
1806 #region referencesource
1807 internal static StringBuilder GenerateConnectionGroup(string connectionGroupName, bool unsafeConnectionGroup, bool isInternalGroup)
1809 StringBuilder connectionLine = new StringBuilder(connectionGroupName);
1811 connectionLine.Append(unsafeConnectionGroup ? "U>" : "S>");
1813 if (isInternalGroup)
1815 connectionLine.Append("I>");
1818 return connectionLine;