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;
39 using System.Net.Cache;
40 using System.Net.Sockets;
41 using System.Runtime.Remoting.Messaging;
42 using System.Runtime.Serialization;
43 using System.Security.Cryptography.X509Certificates;
45 using System.Threading;
50 internal class HttpWebRequest : WebRequest, ISerializable {
53 public class HttpWebRequest : WebRequest, ISerializable {
58 bool allowAutoRedirect = true;
59 bool allowBuffering = true;
60 X509CertificateCollection certificates;
61 string connectionGroup;
62 long contentLength = -1;
63 HttpContinueDelegate continueDelegate;
64 CookieContainer cookieContainer;
65 ICredentials credentials;
69 WebHeaderCollection webHeaders;
70 bool keepAlive = true;
71 int maxAutoRedirect = 50;
72 string mediaType = String.Empty;
73 string method = "GET";
74 string initialMethod = "GET";
75 bool pipelined = true;
78 Version version = HttpVersion.Version11;
80 Version actualVersion;
83 ServicePoint servicePoint;
86 WebConnectionStream writeStream;
87 HttpWebResponse webResponse;
88 WebAsyncResult asyncWrite;
89 WebAsyncResult asyncRead;
90 EventHandler abortHandler;
92 bool gotRequestStream;
98 bool getResponseCalled;
100 object locker = new object ();
101 bool finished_reading;
102 internal WebConnection WebConnection;
103 DecompressionMethods auto_decomp;
104 int maxResponseHeadersLength;
105 static int defaultMaxResponseHeadersLength;
106 int readWriteTimeout = 300000; // ms
113 NtlmAuthState ntlm_auth_state;
117 static HttpWebRequest ()
119 defaultMaxResponseHeadersLength = 64 * 1024;
121 NetConfig config = ConfigurationSettings.GetConfig ("system.net/settings") as NetConfig;
122 if (config != null) {
123 int x = config.MaxResponseHeadersLength;
127 defaultMaxResponseHeadersLength = x;
137 HttpWebRequest (Uri uri)
139 this.requestUri = uri;
140 this.actualUri = uri;
141 this.proxy = GlobalProxySelection.Select;
142 this.webHeaders = new WebHeaderCollection (WebHeaderCollection.HeaderInfo.Request);
145 [Obsolete ("Serialization is obsoleted for this type", false)]
146 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
148 SerializationInfo info = serializationInfo;
150 requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
151 actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
152 allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
153 allowBuffering = info.GetBoolean ("allowBuffering");
154 certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
155 connectionGroup = info.GetString ("connectionGroup");
156 contentLength = info.GetInt64 ("contentLength");
157 webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
158 keepAlive = info.GetBoolean ("keepAlive");
159 maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
160 mediaType = info.GetString ("mediaType");
161 method = info.GetString ("method");
162 initialMethod = info.GetString ("initialMethod");
163 pipelined = info.GetBoolean ("pipelined");
164 version = (Version) info.GetValue ("version", typeof (Version));
165 proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
166 sendChunked = info.GetBoolean ("sendChunked");
167 timeout = info.GetInt32 ("timeout");
168 redirects = info.GetInt32 ("redirects");
169 host = info.GetString ("host");
174 public string Accept {
175 get { return webHeaders ["Accept"]; }
177 CheckRequestStarted ();
178 webHeaders.RemoveAndAdd ("Accept", value);
183 get { return actualUri; }
184 internal set { actualUri = value; } // Used by Ftp+proxy
187 public bool AllowAutoRedirect {
188 get { return allowAutoRedirect; }
189 set { this.allowAutoRedirect = value; }
192 public bool AllowWriteStreamBuffering {
193 get { return allowBuffering; }
194 set { allowBuffering = value; }
197 static Exception GetMustImplement ()
199 return new NotImplementedException ();
202 public DecompressionMethods AutomaticDecompression
208 CheckRequestStarted ();
213 internal bool InternalAllowBuffering {
215 return (allowBuffering && (method != "HEAD" && method != "GET" &&
216 method != "MKCOL" && method != "CONNECT" &&
221 public X509CertificateCollection ClientCertificates {
223 if (certificates == null)
224 certificates = new X509CertificateCollection ();
230 throw GetMustImplement ();
234 public string Connection {
235 get { return webHeaders ["Connection"]; }
237 CheckRequestStarted ();
239 if (string.IsNullOrEmpty (value)) {
240 webHeaders.RemoveInternal ("Connection");
244 string val = value.ToLowerInvariant ();
245 if (val.Contains ("keep-alive") || val.Contains ("close"))
246 throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
249 value = value + ", Keep-Alive";
251 webHeaders.RemoveAndAdd ("Connection", value);
255 public override string ConnectionGroupName {
256 get { return connectionGroup; }
257 set { connectionGroup = value; }
260 public override long ContentLength {
261 get { return contentLength; }
263 CheckRequestStarted ();
265 throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
267 contentLength = value;
271 internal long InternalContentLength {
272 set { contentLength = value; }
275 public override string ContentType {
276 get { return webHeaders ["Content-Type"]; }
278 if (value == null || value.Trim().Length == 0) {
279 webHeaders.RemoveInternal ("Content-Type");
282 webHeaders.RemoveAndAdd ("Content-Type", value);
286 public HttpContinueDelegate ContinueDelegate {
287 get { return continueDelegate; }
288 set { continueDelegate = value; }
291 public CookieContainer CookieContainer {
292 get { return cookieContainer; }
293 set { cookieContainer = value; }
296 public override ICredentials Credentials {
297 get { return credentials; }
298 set { credentials = value; }
301 public DateTime Date {
303 string date = webHeaders ["Date"];
305 return DateTime.MinValue;
306 return DateTime.ParseExact (date, "r", CultureInfo.InvariantCulture).ToLocalTime ();
309 webHeaders.RemoveAndAdd ("Date", value.ToUniversalTime ().ToString ("r", CultureInfo.InvariantCulture));
314 public static new RequestCachePolicy DefaultCachePolicy
317 throw GetMustImplement ();
320 throw GetMustImplement ();
325 public static int DefaultMaximumErrorResponseLength
328 throw GetMustImplement ();
331 throw GetMustImplement ();
335 public string Expect {
336 get { return webHeaders ["Expect"]; }
338 CheckRequestStarted ();
341 val = val.Trim ().ToLower ();
343 if (val == null || val.Length == 0) {
344 webHeaders.RemoveInternal ("Expect");
348 if (val == "100-continue")
349 throw new ArgumentException ("100-Continue cannot be set with this property.",
351 webHeaders.RemoveAndAdd ("Expect", value);
355 public bool HaveResponse {
356 get { return haveResponse; }
359 public override WebHeaderCollection Headers {
360 get { return webHeaders; }
362 CheckRequestStarted ();
363 WebHeaderCollection newHeaders = new WebHeaderCollection (WebHeaderCollection.HeaderInfo.Request);
364 int count = value.Count;
365 for (int i = 0; i < count; i++)
366 newHeaders.Add (value.GetKey (i), value.Get (i));
368 webHeaders = newHeaders;
380 return actualUri.Authority;
385 throw new ArgumentNullException ("value");
387 if (!CheckValidHost (actualUri.Scheme, value))
388 throw new ArgumentException ("Invalid host: " + value);
394 static bool CheckValidHost (string scheme, string val)
397 throw new ArgumentNullException ("value");
405 int idx = val.IndexOf ('/');
409 string u = scheme + "://" + val + "/";
410 return Uri.IsWellFormedUriString (u, UriKind.Absolute);
413 public DateTime IfModifiedSince {
415 string str = webHeaders ["If-Modified-Since"];
419 return MonoHttpDate.Parse (str);
420 } catch (Exception) {
425 CheckRequestStarted ();
427 webHeaders.SetInternal ("If-Modified-Since",
428 value.ToUniversalTime ().ToString ("r", null));
429 // TODO: check last param when using different locale
433 public bool KeepAlive {
442 public int MaximumAutomaticRedirections {
443 get { return maxAutoRedirect; }
446 throw new ArgumentException ("Must be > 0", "value");
448 maxAutoRedirect = value;
452 [MonoTODO ("Use this")]
453 public int MaximumResponseHeadersLength {
454 get { return maxResponseHeadersLength; }
455 set { maxResponseHeadersLength = value; }
458 [MonoTODO ("Use this")]
459 public static int DefaultMaximumResponseHeadersLength {
460 get { return defaultMaxResponseHeadersLength; }
461 set { defaultMaxResponseHeadersLength = value; }
464 public int ReadWriteTimeout {
465 get { return readWriteTimeout; }
468 throw new InvalidOperationException ("The request has already been sent.");
471 throw new ArgumentOutOfRangeException ("value", "Must be >= -1");
473 readWriteTimeout = value;
477 public string MediaType {
478 get { return mediaType; }
484 public override string Method {
485 get { return this.method; }
487 if (value == null || value.Trim () == "")
488 throw new ArgumentException ("not a valid method");
490 method = value.ToUpperInvariant ();
491 if (method != "HEAD" && method != "GET" && method != "POST" && method != "PUT" &&
492 method != "DELETE" && method != "CONNECT" && method != "TRACE" &&
499 public bool Pipelined {
500 get { return pipelined; }
501 set { pipelined = value; }
504 public override bool PreAuthenticate {
505 get { return preAuthenticate; }
506 set { preAuthenticate = value; }
509 public Version ProtocolVersion {
510 get { return version; }
512 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
513 throw new ArgumentException ("value");
515 force_version = true;
520 public override IWebProxy Proxy {
521 get { return proxy; }
523 CheckRequestStarted ();
525 servicePoint = null; // we may need a new one
529 public string Referer {
530 get { return webHeaders ["Referer"]; }
532 CheckRequestStarted ();
533 if (value == null || value.Trim().Length == 0) {
534 webHeaders.RemoveInternal ("Referer");
537 webHeaders.SetInternal ("Referer", value);
541 public override Uri RequestUri {
542 get { return requestUri; }
545 public bool SendChunked {
546 get { return sendChunked; }
548 CheckRequestStarted ();
553 public ServicePoint ServicePoint {
554 get { return GetServicePoint (); }
557 public override int Timeout {
558 get { return timeout; }
561 throw new ArgumentOutOfRangeException ("value");
567 public string TransferEncoding {
568 get { return webHeaders ["Transfer-Encoding"]; }
570 CheckRequestStarted ();
573 val = val.Trim ().ToLower ();
575 if (val == null || val.Length == 0) {
576 webHeaders.RemoveInternal ("Transfer-Encoding");
580 if (val == "chunked")
581 throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
584 throw new ArgumentException ("SendChunked must be True", "value");
586 webHeaders.RemoveAndAdd ("Transfer-Encoding", value);
590 public override bool UseDefaultCredentials
592 get { return CredentialCache.DefaultCredentials == Credentials; }
593 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
596 public string UserAgent {
597 get { return webHeaders ["User-Agent"]; }
598 set { webHeaders.SetInternal ("User-Agent", value); }
601 bool unsafe_auth_blah;
602 public bool UnsafeAuthenticatedConnectionSharing
604 get { return unsafe_auth_blah; }
605 set { unsafe_auth_blah = value; }
608 internal bool GotRequestStream {
609 get { return gotRequestStream; }
612 internal bool ExpectContinue {
613 get { return expectContinue; }
614 set { expectContinue = value; }
617 internal Uri AuthUri {
618 get { return actualUri; }
621 internal bool ProxyQuery {
622 get { return servicePoint.UsesProxy && !servicePoint.UseConnect; }
627 internal ServicePoint GetServicePoint ()
630 if (hostChanged || servicePoint == null) {
631 servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
639 public void AddRange (int range)
641 AddRange ("bytes", (long) range);
644 public void AddRange (int from, int to)
646 AddRange ("bytes", (long) from, (long) to);
649 public void AddRange (string rangeSpecifier, int range)
651 AddRange (rangeSpecifier, (long) range);
654 public void AddRange (string rangeSpecifier, int from, int to)
656 AddRange (rangeSpecifier, (long) from, (long) to);
663 void AddRange (long range)
665 AddRange ("bytes", (long) range);
673 void AddRange (long from, long to)
675 AddRange ("bytes", from, to);
683 void AddRange (string rangeSpecifier, long range)
685 if (rangeSpecifier == null)
686 throw new ArgumentNullException ("rangeSpecifier");
687 if (!WebHeaderCollection.IsHeaderValue (rangeSpecifier))
688 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
690 string r = webHeaders ["Range"];
692 r = rangeSpecifier + "=";
694 string old_specifier = r.Substring (0, r.IndexOf ('='));
695 if (String.Compare (old_specifier, rangeSpecifier, StringComparison.OrdinalIgnoreCase) != 0)
696 throw new InvalidOperationException ("A different range specifier is already in use");
700 string n = range.ToString (CultureInfo.InvariantCulture);
705 webHeaders.RemoveAndAdd ("Range", r);
713 void AddRange (string rangeSpecifier, long from, long to)
715 if (rangeSpecifier == null)
716 throw new ArgumentNullException ("rangeSpecifier");
717 if (!WebHeaderCollection.IsHeaderValue (rangeSpecifier))
718 throw new ArgumentException ("Invalid range specifier", "rangeSpecifier");
719 if (from > to || from < 0)
720 throw new ArgumentOutOfRangeException ("from");
722 throw new ArgumentOutOfRangeException ("to");
724 string r = webHeaders ["Range"];
726 r = rangeSpecifier + "=";
730 r = String.Format ("{0}{1}-{2}", r, from, to);
731 webHeaders.RemoveAndAdd ("Range", r);
735 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
738 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
740 bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD" ||
742 if (method == null || !send)
743 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
745 if (contentLength == -1 && !sendChunked && !allowBuffering && KeepAlive)
746 throw new ProtocolViolationException ("Content-Length not set");
748 string transferEncoding = TransferEncoding;
749 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
750 throw new ProtocolViolationException ("SendChunked should be true.");
754 if (asyncWrite != null) {
755 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
756 "method while a previous call is still in progress.");
759 asyncWrite = new WebAsyncResult (this, callback, state);
760 initialMethod = method;
762 if (writeStream != null) {
763 asyncWrite.SetCompleted (true, writeStream);
764 asyncWrite.DoCallback ();
769 gotRequestStream = true;
770 WebAsyncResult result = asyncWrite;
774 servicePoint = GetServicePoint ();
775 abortHandler = servicePoint.SendRequest (this, connectionGroup);
781 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
783 if (asyncResult == null)
784 throw new ArgumentNullException ("asyncResult");
786 WebAsyncResult result = asyncResult as WebAsyncResult;
788 throw new ArgumentException ("Invalid IAsyncResult");
791 result.WaitUntilComplete ();
793 Exception e = result.Exception;
797 return result.WriteStream;
800 public override Stream GetRequestStream()
802 IAsyncResult asyncResult = asyncWrite;
803 if (asyncResult == null) {
804 asyncResult = BeginGetRequestStream (null, null);
805 asyncWrite = (WebAsyncResult) asyncResult;
808 if (!asyncResult.IsCompleted && !asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
810 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
813 return EndGetRequestStream (asyncResult);
816 void CheckIfForceWrite ()
818 if (writeStream == null || writeStream.RequestWritten || (contentLength < 0 && writeStream.CanWrite == true) || !InternalAllowBuffering)
821 // This will write the POST/PUT if the write stream already has the expected
822 // amount of bytes in it (ContentLength) (bug #77753) or if the write stream
823 // contains data and it has been closed already (xamarin bug #1512).
825 if (writeStream.WriteBufferLength == contentLength || (contentLength == -1 && writeStream.CanWrite == false))
826 writeStream.WriteRequest ();
829 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
832 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
835 throw new ProtocolViolationException ("Method is null.");
837 string transferEncoding = TransferEncoding;
838 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
839 throw new ProtocolViolationException ("SendChunked should be true.");
841 Monitor.Enter (locker);
842 getResponseCalled = true;
843 if (asyncRead != null && !haveResponse) {
844 Monitor.Exit (locker);
845 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
846 "method while a previous call is still in progress.");
849 CheckIfForceWrite ();
850 asyncRead = new WebAsyncResult (this, callback, state);
851 WebAsyncResult aread = asyncRead;
852 initialMethod = method;
854 Exception saved = saved_exc;
855 if (webResponse != null) {
856 Monitor.Exit (locker);
858 aread.SetCompleted (true, webResponse);
860 aread.SetCompleted (true, saved);
864 } else if (saved != null) {
865 Monitor.Exit (locker);
866 aread.SetCompleted (true, saved);
875 servicePoint = GetServicePoint ();
876 abortHandler = servicePoint.SendRequest (this, connectionGroup);
879 Monitor.Exit (locker);
883 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
885 if (asyncResult == null)
886 throw new ArgumentNullException ("asyncResult");
888 WebAsyncResult result = asyncResult as WebAsyncResult;
890 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
892 if (!result.WaitUntilComplete (timeout, false)) {
894 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
897 if (result.GotException)
898 throw result.Exception;
900 return result.Response;
903 public override WebResponse GetResponse()
905 WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
906 return EndGetResponse (result);
909 internal bool FinishedReading {
910 get { return finished_reading; }
911 set { finished_reading = value; }
914 internal bool Aborted {
915 get { return Interlocked.CompareExchange (ref aborted, 0, 0) == 1; }
918 public override void Abort ()
920 if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
923 if (haveResponse && finished_reading)
927 if (abortHandler != null) {
929 abortHandler (this, EventArgs.Empty);
930 } catch (Exception) {}
934 if (asyncWrite != null) {
935 WebAsyncResult r = asyncWrite;
936 if (!r.IsCompleted) {
938 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
939 r.SetCompleted (false, wexc);
946 if (asyncRead != null) {
947 WebAsyncResult r = asyncRead;
948 if (!r.IsCompleted) {
950 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
951 r.SetCompleted (false, wexc);
958 if (writeStream != null) {
960 writeStream.Close ();
965 if (webResponse != null) {
967 webResponse.Close ();
973 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
974 StreamingContext streamingContext)
976 GetObjectData (serializationInfo, streamingContext);
979 protected override void GetObjectData (SerializationInfo serializationInfo,
980 StreamingContext streamingContext)
982 SerializationInfo info = serializationInfo;
984 info.AddValue ("requestUri", requestUri, typeof (Uri));
985 info.AddValue ("actualUri", actualUri, typeof (Uri));
986 info.AddValue ("allowAutoRedirect", allowAutoRedirect);
987 info.AddValue ("allowBuffering", allowBuffering);
988 info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
989 info.AddValue ("connectionGroup", connectionGroup);
990 info.AddValue ("contentLength", contentLength);
991 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
992 info.AddValue ("keepAlive", keepAlive);
993 info.AddValue ("maxAutoRedirect", maxAutoRedirect);
994 info.AddValue ("mediaType", mediaType);
995 info.AddValue ("method", method);
996 info.AddValue ("initialMethod", initialMethod);
997 info.AddValue ("pipelined", pipelined);
998 info.AddValue ("version", version, typeof (Version));
999 info.AddValue ("proxy", proxy, typeof (IWebProxy));
1000 info.AddValue ("sendChunked", sendChunked);
1001 info.AddValue ("timeout", timeout);
1002 info.AddValue ("redirects", redirects);
1003 info.AddValue ("host", host);
1006 void CheckRequestStarted ()
1009 throw new InvalidOperationException ("request started");
1012 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
1014 if (continueDelegate != null)
1015 continueDelegate (statusCode, headers);
1018 bool Redirect (WebAsyncResult result, HttpStatusCode code)
1022 string uriString = null;
1025 case HttpStatusCode.Ambiguous: // 300
1026 e = new WebException ("Ambiguous redirect.");
1028 case HttpStatusCode.MovedPermanently: // 301
1029 case HttpStatusCode.Redirect: // 302
1030 case HttpStatusCode.TemporaryRedirect: // 307
1031 /* MS follows the redirect for POST too
1032 if (method != "GET" && method != "HEAD") // 10.3
1037 bodyBufferLength = 0;
1039 if (code != HttpStatusCode.TemporaryRedirect)
1041 uriString = webResponse.Headers ["Location"];
1043 case HttpStatusCode.SeeOther: //303
1045 uriString = webResponse.Headers ["Location"];
1047 case HttpStatusCode.NotModified: // 304
1049 case HttpStatusCode.UseProxy: // 305
1050 e = new NotImplementedException ("Proxy support not available.");
1052 case HttpStatusCode.Unused: // 306
1054 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
1061 if (uriString == null)
1062 throw new WebException ("No Location header found for " + (int) code,
1063 WebExceptionStatus.ProtocolError);
1065 Uri prev = actualUri;
1067 actualUri = new Uri (actualUri, uriString);
1068 } catch (Exception) {
1069 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
1070 uriString, (int) code),
1071 WebExceptionStatus.ProtocolError);
1074 hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
1078 string GetHeaders ()
1080 bool continue100 = false;
1083 webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
1084 webHeaders.RemoveInternal ("Content-Length");
1085 } else if (contentLength != -1) {
1086 if (ntlm_auth_state != NtlmAuthState.Challenge) {
1087 if (contentLength > 0)
1090 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
1092 webHeaders.SetInternal ("Content-Length", "0");
1094 webHeaders.RemoveInternal ("Transfer-Encoding");
1096 webHeaders.RemoveInternal ("Content-Length");
1099 if (actualVersion == HttpVersion.Version11 && continue100 &&
1100 servicePoint.SendContinue) { // RFC2616 8.2.3
1101 webHeaders.RemoveAndAdd ("Expect" , "100-continue");
1102 expectContinue = true;
1104 webHeaders.RemoveInternal ("Expect");
1105 expectContinue = false;
1108 bool proxy_query = ProxyQuery;
1109 string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
1110 webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
1111 Version proto_version = servicePoint.ProtocolVersion;
1112 bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
1114 if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
1115 webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
1116 } else if (!keepAlive && version == HttpVersion.Version11) {
1117 webHeaders.RemoveAndAdd (connectionHeader, "close");
1120 webHeaders.SetInternal ("Host", Host);
1121 if (cookieContainer != null) {
1122 string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
1123 if (cookieHeader != "")
1124 webHeaders.SetInternal ("Cookie", cookieHeader);
1127 string accept_encoding = null;
1128 if ((auto_decomp & DecompressionMethods.GZip) != 0)
1129 accept_encoding = "gzip";
1130 if ((auto_decomp & DecompressionMethods.Deflate) != 0)
1131 accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
1132 if (accept_encoding != null)
1133 webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);
1135 if (!usedPreAuth && preAuthenticate)
1136 DoPreAuthenticate ();
1138 return webHeaders.ToString ();
1141 void DoPreAuthenticate ()
1143 bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1144 ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
1145 Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
1149 webHeaders.RemoveInternal ("Proxy-Authorization");
1150 webHeaders.RemoveInternal ("Authorization");
1151 string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
1152 webHeaders [authHeader] = auth.Message;
1156 internal void SetWriteStreamError (WebExceptionStatus status, Exception exc)
1161 WebAsyncResult r = asyncWrite;
1169 msg = "Error: " + status;
1170 wex = new WebException (msg, status);
1172 msg = String.Format ("Error: {0} ({1})", status, exc.Message);
1173 wex = new WebException (msg, exc, status);
1175 r.SetCompleted (false, wex);
1180 internal void SendRequestHeaders (bool propagate_error)
1182 StringBuilder req = new StringBuilder ();
1185 query = actualUri.PathAndQuery;
1187 query = String.Format ("{0}://{1}{2}", actualUri.Scheme,
1189 actualUri.PathAndQuery);
1192 if (!force_version && servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
1193 actualVersion = servicePoint.ProtocolVersion;
1195 actualVersion = version;
1198 req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
1199 actualVersion.Major, actualVersion.Minor);
1200 req.Append (GetHeaders ());
1201 string reqstr = req.ToString ();
1202 byte [] bytes = Encoding.UTF8.GetBytes (reqstr);
1204 writeStream.SetHeaders (bytes);
1205 } catch (WebException wexc) {
1206 SetWriteStreamError (wexc.Status, wexc);
1207 if (propagate_error)
1209 } catch (Exception exc) {
1210 SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
1211 if (propagate_error)
1216 internal void SetWriteStream (WebConnectionStream stream)
1221 writeStream = stream;
1222 if (bodyBuffer != null) {
1223 webHeaders.RemoveInternal ("Transfer-Encoding");
1224 contentLength = bodyBufferLength;
1225 writeStream.SendChunked = false;
1228 SendRequestHeaders (false);
1232 if (bodyBuffer != null) {
1233 // The body has been written and buffered. The request "user"
1234 // won't write it again, so we must do it.
1235 if (ntlm_auth_state != NtlmAuthState.Challenge) {
1236 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1238 writeStream.Close ();
1240 } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
1241 method != "TRACE") {
1242 if (getResponseCalled && !writeStream.RequestWritten)
1243 writeStream.WriteRequest ();
1246 if (asyncWrite != null) {
1247 asyncWrite.SetCompleted (false, stream);
1248 asyncWrite.DoCallback ();
1253 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1258 string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1259 WebAsyncResult r = asyncRead;
1264 if (e is WebException) {
1265 wexc = (WebException) e;
1267 wexc = new WebException (msg, e, status, null);
1270 if (!r.IsCompleted) {
1271 r.SetCompleted (false, wexc);
1273 } else if (r == asyncWrite) {
1276 haveResponse = true;
1280 haveResponse = true;
1286 void CheckSendError (WebConnectionData data)
1288 // Got here, but no one called GetResponse
1289 int status = data.StatusCode;
1290 if (status < 400 || status == 401 || status == 407)
1293 if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1294 // The request has not been completely sent and we got here!
1295 // We should probably just close and cause an error in any case,
1296 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse);
1297 if (allowBuffering || sendChunked || writeStream.totalWritten >= contentLength) {
1298 webResponse.ReadAll ();
1300 writeStream.IgnoreIOErrors = true;
1305 void HandleNtlmAuth (WebAsyncResult r)
1307 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1309 WebConnection cnc = wce.Connection;
1310 cnc.PriorityRequest = this;
1311 bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1312 ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
1313 if (creds != null) {
1314 cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM");
1315 cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah;
1319 finished_reading = false;
1320 haveResponse = false;
1321 webResponse.ReadAll ();
1325 internal void SetResponseData (WebConnectionData data)
1329 if (data.stream != null)
1330 data.stream.Close ();
1334 WebException wexc = null;
1336 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1337 } catch (Exception e) {
1338 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null);
1339 if (data.stream != null)
1340 data.stream.Close ();
1343 if (wexc == null && (method == "POST" || method == "PUT")) {
1344 CheckSendError (data);
1345 if (saved_exc != null)
1346 wexc = (WebException) saved_exc;
1349 WebAsyncResult r = asyncRead;
1351 bool forced = false;
1352 if (r == null && webResponse != null) {
1353 // This is a forced completion (302, 204)...
1355 r = new WebAsyncResult (null, null);
1356 r.SetCompleted (false, webResponse);
1361 haveResponse = true;
1363 r.SetCompleted (false, wexc);
1370 redirected = CheckFinalStatus (r);
1372 if (ntlm_auth_state != NtlmAuthState.None && authCompleted && webResponse != null
1373 && (int)webResponse.StatusCode < 400) {
1374 WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1376 WebConnection cnc = wce.Connection;
1377 cnc.NtlmAuthenticated = true;
1381 // clear internal buffer so that it does not
1382 // hold possible big buffer (bug #397627)
1383 if (writeStream != null)
1384 writeStream.KillBuffer ();
1386 haveResponse = true;
1387 r.SetCompleted (false, webResponse);
1390 if (webResponse != null) {
1391 if (ntlm_auth_state != NtlmAuthState.None) {
1395 webResponse.Close ();
1397 finished_reading = false;
1398 haveResponse = false;
1401 servicePoint = GetServicePoint ();
1402 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1404 } catch (WebException wexc2) {
1407 haveResponse = true;
1409 r.SetCompleted (false, wexc2);
1412 } catch (Exception ex) {
1413 wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null);
1416 haveResponse = true;
1418 r.SetCompleted (false, wexc);
1426 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1428 authCompleted = false;
1429 if (code == HttpStatusCode.Unauthorized && credentials == null)
1432 bool isProxy = (code == HttpStatusCode.ProxyAuthenticationRequired);
1433 if (isProxy && (proxy == null || proxy.Credentials == null))
1436 string [] authHeaders = response.Headers.GetValues ( (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate");
1437 if (authHeaders == null || authHeaders.Length == 0)
1440 ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
1441 Authorization auth = null;
1442 foreach (string authHeader in authHeaders) {
1443 auth = AuthenticationManager.Authenticate (authHeader, this, creds);
1449 webHeaders [(isProxy) ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1450 authCompleted = auth.Complete;
1451 bool is_ntlm = (auth.Module.AuthenticationType == "NTLM");
1453 ntlm_auth_state = (NtlmAuthState)((int) ntlm_auth_state + 1);
1457 // Returns true if redirected
1458 bool CheckFinalStatus (WebAsyncResult result)
1460 if (result.GotException) {
1462 throw result.Exception;
1465 Exception throwMe = result.Exception;
1467 HttpWebResponse resp = result.Response;
1468 WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1469 HttpStatusCode code = 0;
1470 if (throwMe == null && webResponse != null) {
1471 code = webResponse.StatusCode;
1472 if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
1473 (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) {
1474 if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1475 // Keep the written body, so it can be rewritten in the retry
1476 if (InternalAllowBuffering) {
1477 // NTLM: This is to avoid sending data in the 'challenge' request
1478 // We save it in the first request (first 401), don't send anything
1479 // in the challenge request and send it in the response request along
1480 // with the buffers kept form the first request.
1481 if (ntlm_auth_state != NtlmAuthState.Response) {
1482 bodyBuffer = writeStream.WriteBuffer;
1483 bodyBufferLength = writeStream.WriteBufferLength;
1486 } else if (method != "PUT" && method != "POST") {
1491 writeStream.InternalClose ();
1493 webResponse.Close ();
1497 throw new WebException ("This request requires buffering " +
1498 "of data for authentication or " +
1499 "redirection to be sucessful.");
1504 if ((int) code >= 400) {
1505 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1506 (int) code, webResponse.StatusDescription);
1507 throwMe = new WebException (err, null, protoError, webResponse);
1508 webResponse.ReadAll ();
1509 } else if ((int) code == 304 && allowAutoRedirect) {
1510 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1511 (int) code, webResponse.StatusDescription);
1512 throwMe = new WebException (err, null, protoError, webResponse);
1513 } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
1514 throwMe = new WebException ("Max. redirections exceeded.", null,
1515 protoError, webResponse);
1516 webResponse.ReadAll ();
1521 if (throwMe == null) {
1524 if (allowAutoRedirect && c >= 300) {
1525 if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1526 bodyBuffer = writeStream.WriteBuffer;
1527 bodyBufferLength = writeStream.WriteBufferLength;
1529 b = Redirect (result, code);
1532 if (resp != null && c >= 300 && c != 304)
1538 if (writeStream != null) {
1539 writeStream.InternalClose ();