| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- //
- // MonoWebRequestHandler.cs
- //
- // Authors:
- // Marek Safar <[email protected]>
- // Martin Baulig <[email protected]>
- //
- // Copyright (C) 2011-2018 Xamarin Inc (http://www.xamarin.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Collections.Generic;
- using System.Security.Authentication;
- using System.Security.Cryptography.X509Certificates;
- using System.Security.Principal;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Collections.Specialized;
- using System.Net.Http.Headers;
- using System.Net.Cache;
- using System.Net.Security;
- using System.Linq;
- namespace System.Net.Http
- {
- class MonoWebRequestHandler : IMonoHttpClientHandler
- {
- static long groupCounter;
- bool allowAutoRedirect;
- DecompressionMethods automaticDecompression;
- CookieContainer cookieContainer;
- ICredentials credentials;
- int maxAutomaticRedirections;
- long maxRequestContentBufferSize;
- bool preAuthenticate;
- IWebProxy proxy;
- bool useCookies;
- bool useProxy;
- SslClientAuthenticationOptions sslOptions;
- bool allowPipelining;
- RequestCachePolicy cachePolicy;
- AuthenticationLevel authenticationLevel;
- TimeSpan continueTimeout;
- TokenImpersonationLevel impersonationLevel;
- int maxResponseHeadersLength;
- int readWriteTimeout;
- RemoteCertificateValidationCallback serverCertificateValidationCallback;
- bool unsafeAuthenticatedConnectionSharing;
- bool sentRequest;
- string connectionGroupName;
- bool disposed;
- internal MonoWebRequestHandler ()
- {
- allowAutoRedirect = true;
- maxAutomaticRedirections = 50;
- maxRequestContentBufferSize = int.MaxValue;
- useCookies = true;
- useProxy = true;
- allowPipelining = true;
- authenticationLevel = AuthenticationLevel.MutualAuthRequested;
- cachePolicy = System.Net.WebRequest.DefaultCachePolicy;
- continueTimeout = TimeSpan.FromMilliseconds (350);
- impersonationLevel = TokenImpersonationLevel.Delegation;
- maxResponseHeadersLength = HttpWebRequest.DefaultMaximumResponseHeadersLength;
- readWriteTimeout = 300000;
- serverCertificateValidationCallback = null;
- unsafeAuthenticatedConnectionSharing = false;
- connectionGroupName = "HttpClientHandler" + Interlocked.Increment (ref groupCounter);
- }
- internal void EnsureModifiability ()
- {
- if (sentRequest)
- throw new InvalidOperationException (
- "This instance has already started one or more requests. " +
- "Properties can only be modified before sending the first request.");
- }
- public bool AllowAutoRedirect {
- get {
- return allowAutoRedirect;
- }
- set {
- EnsureModifiability ();
- allowAutoRedirect = value;
- }
- }
- public DecompressionMethods AutomaticDecompression {
- get {
- return automaticDecompression;
- }
- set {
- EnsureModifiability ();
- automaticDecompression = value;
- }
- }
- public CookieContainer CookieContainer {
- get {
- return cookieContainer ?? (cookieContainer = new CookieContainer ());
- }
- set {
- EnsureModifiability ();
- cookieContainer = value;
- }
- }
- public ICredentials Credentials {
- get {
- return credentials;
- }
- set {
- EnsureModifiability ();
- credentials = value;
- }
- }
- public int MaxAutomaticRedirections {
- get {
- return maxAutomaticRedirections;
- }
- set {
- EnsureModifiability ();
- if (value <= 0)
- throw new ArgumentOutOfRangeException ();
- maxAutomaticRedirections = value;
- }
- }
- public long MaxRequestContentBufferSize {
- get {
- return maxRequestContentBufferSize;
- }
- set {
- EnsureModifiability ();
- if (value < 0)
- throw new ArgumentOutOfRangeException ();
- maxRequestContentBufferSize = value;
- }
- }
- public bool PreAuthenticate {
- get {
- return preAuthenticate;
- }
- set {
- EnsureModifiability ();
- preAuthenticate = value;
- }
- }
- public IWebProxy Proxy {
- get {
- return proxy;
- }
- set {
- EnsureModifiability ();
- if (!UseProxy)
- throw new InvalidOperationException ();
- proxy = value;
- }
- }
- public virtual bool SupportsAutomaticDecompression {
- get {
- return true;
- }
- }
- public virtual bool SupportsProxy {
- get {
- return true;
- }
- }
- public virtual bool SupportsRedirectConfiguration {
- get {
- return true;
- }
- }
- public bool UseCookies {
- get {
- return useCookies;
- }
- set {
- EnsureModifiability ();
- useCookies = value;
- }
- }
- public bool UseProxy {
- get {
- return useProxy;
- }
- set {
- EnsureModifiability ();
- useProxy = value;
- }
- }
- public bool AllowPipelining {
- get { return allowPipelining; }
- set {
- EnsureModifiability ();
- allowPipelining = value;
- }
- }
- public RequestCachePolicy CachePolicy {
- get { return cachePolicy; }
- set {
- EnsureModifiability ();
- cachePolicy = value;
- }
- }
- public AuthenticationLevel AuthenticationLevel {
- get { return authenticationLevel; }
- set {
- EnsureModifiability ();
- authenticationLevel = value;
- }
- }
- [MonoTODO]
- public TimeSpan ContinueTimeout {
- get { return continueTimeout; }
- set {
- EnsureModifiability ();
- continueTimeout = value;
- }
- }
- public TokenImpersonationLevel ImpersonationLevel {
- get { return impersonationLevel; }
- set {
- EnsureModifiability ();
- impersonationLevel = value;
- }
- }
- public int MaxResponseHeadersLength {
- get { return maxResponseHeadersLength; }
- set {
- EnsureModifiability ();
- maxResponseHeadersLength = value;
- }
- }
- public int ReadWriteTimeout {
- get { return readWriteTimeout; }
- set {
- EnsureModifiability ();
- readWriteTimeout = value;
- }
- }
- public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
- get { return serverCertificateValidationCallback; }
- set {
- EnsureModifiability ();
- serverCertificateValidationCallback = value;
- }
- }
- public bool UnsafeAuthenticatedConnectionSharing {
- get { return unsafeAuthenticatedConnectionSharing; }
- set {
- EnsureModifiability ();
- unsafeAuthenticatedConnectionSharing = value;
- }
- }
- public SslClientAuthenticationOptions SslOptions {
- get => sslOptions ?? (sslOptions = new SslClientAuthenticationOptions ());
- set {
- EnsureModifiability ();
- sslOptions = value;
- }
- }
- public void Dispose ()
- {
- Dispose (true);
- }
- protected virtual void Dispose (bool disposing)
- {
- if (disposing && !disposed) {
- Volatile.Write (ref disposed, true);
- ServicePointManager.CloseConnectionGroup (connectionGroupName);
- }
- }
- bool GetConnectionKeepAlive (HttpRequestHeaders headers)
- {
- return headers.Connection.Any (l => string.Equals (l, "Keep-Alive", StringComparison.OrdinalIgnoreCase));
- }
- internal virtual HttpWebRequest CreateWebRequest (HttpRequestMessage request)
- {
- var wr = new HttpWebRequest (request.RequestUri);
- wr.ThrowOnError = false;
- wr.AllowWriteStreamBuffering = false;
- if (request.Version == HttpVersion.Version20)
- wr.ProtocolVersion = HttpVersion.Version11;
- else
- wr.ProtocolVersion = request.Version;
- wr.ConnectionGroupName = connectionGroupName;
- wr.Method = request.Method.Method;
- if (wr.ProtocolVersion == HttpVersion.Version10) {
- wr.KeepAlive = GetConnectionKeepAlive (request.Headers);
- } else {
- wr.KeepAlive = request.Headers.ConnectionClose != true;
- }
- if (allowAutoRedirect) {
- wr.AllowAutoRedirect = true;
- wr.MaximumAutomaticRedirections = maxAutomaticRedirections;
- } else {
- wr.AllowAutoRedirect = false;
- }
- wr.AutomaticDecompression = automaticDecompression;
- wr.PreAuthenticate = preAuthenticate;
- if (useCookies) {
- // It cannot be null or allowAutoRedirect won't work
- wr.CookieContainer = CookieContainer;
- }
- wr.Credentials = credentials;
- if (useProxy) {
- wr.Proxy = proxy;
- } else {
- // Disables default WebRequest.DefaultWebProxy value
- wr.Proxy = null;
- }
- wr.ServicePoint.Expect100Continue = request.Headers.ExpectContinue == true;
- // Add request headers
- var headers = wr.Headers;
- foreach (var header in request.Headers) {
- var values = header.Value;
- if (header.Key == "Host") {
- //
- // Host must be explicitly set for HttpWebRequest
- //
- wr.Host = request.Headers.Host;
- continue;
- }
- if (header.Key == "Transfer-Encoding") {
- //
- // Chunked Transfer-Encoding is set for HttpWebRequest later when Content length is checked
- //
- values = values.Where (l => l != "chunked");
- }
- var values_formated = HeaderUtils.GetSingleHeaderString (header.Key, values);
- if (values_formated == null)
- continue;
- headers.AddInternal (header.Key, values_formated);
- }
- return wr;
- }
- HttpResponseMessage CreateResponseMessage (HttpWebResponse wr, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
- {
- var response = new HttpResponseMessage (wr.StatusCode);
- response.RequestMessage = requestMessage;
- response.ReasonPhrase = wr.StatusDescription;
- #if LEGACY_HTTPCLIENT
- response.Content = new StreamContent (wr.GetResponseStream (), cancellationToken);
- #else
- response.Content = new StreamContent (wr.GetResponseStream ());
- #endif
- var headers = wr.Headers;
- for (int i = 0; i < headers.Count; ++i) {
- var key = headers.GetKey (i);
- var value = headers.GetValues (i);
- HttpHeaders item_headers;
- if (HeaderUtils.IsContentHeader (key))
- item_headers = response.Content.Headers;
- else
- item_headers = response.Headers;
- item_headers.TryAddWithoutValidation (key, value);
- }
- requestMessage.RequestUri = wr.ResponseUri;
- return response;
- }
- static bool MethodHasBody (HttpMethod method)
- {
- switch (method.Method) {
- case "HEAD":
- case "GET":
- case "MKCOL":
- case "CONNECT":
- case "TRACE":
- return false;
- default:
- return true;
- }
- }
- public async Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken)
- {
- if (disposed)
- throw new ObjectDisposedException (GetType ().ToString ());
- Volatile.Write (ref sentRequest, true);
- var wrequest = CreateWebRequest (request);
- HttpWebResponse wresponse = null;
- try {
- using (cancellationToken.Register (l => ((HttpWebRequest)l).Abort (), wrequest)) {
- var content = request.Content;
- if (content != null) {
- var headers = wrequest.Headers;
- foreach (var header in content.Headers) {
- foreach (var value in header.Value) {
- headers.AddInternal (header.Key, value);
- }
- }
- if (request.Headers.TransferEncodingChunked == true) {
- wrequest.SendChunked = true;
- } else {
- //
- // Content length has to be set because HttpWebRequest is running without buffering
- //
- var contentLength = content.Headers.ContentLength;
- if (contentLength != null) {
- wrequest.ContentLength = contentLength.Value;
- } else {
- if (MaxRequestContentBufferSize == 0)
- throw new InvalidOperationException ("The content length of the request content can't be determined. Either set TransferEncodingChunked to true, load content into buffer, or set MaxRequestContentBufferSize.");
- await content.LoadIntoBufferAsync (MaxRequestContentBufferSize).ConfigureAwait (false);
- wrequest.ContentLength = content.Headers.ContentLength.Value;
- }
- }
- wrequest.ResendContentFactory = content.CopyToAsync;
- using (var stream = await wrequest.GetRequestStreamAsync ().ConfigureAwait (false)) {
- await request.Content.CopyToAsync (stream).ConfigureAwait (false);
- }
- } else if (MethodHasBody (request.Method)) {
- // Explicitly set this to make sure we're sending a "Content-Length: 0" header.
- // This fixes the issue that's been reported on the forums:
- // http://forums.xamarin.com/discussion/17770/length-required-error-in-http-post-since-latest-release
- wrequest.ContentLength = 0;
- }
- wresponse = (HttpWebResponse)await wrequest.GetResponseAsync ().ConfigureAwait (false);
- }
- } catch (WebException we) {
- if (we.Status != WebExceptionStatus.RequestCanceled)
- throw new HttpRequestException ("An error occurred while sending the request", we);
- } catch (System.IO.IOException ex) {
- throw new HttpRequestException ("An error occurred while sending the request", ex);
- }
- if (cancellationToken.IsCancellationRequested) {
- var cancelled = new TaskCompletionSource<HttpResponseMessage> ();
- cancelled.SetCanceled ();
- return await cancelled.Task;
- }
- return CreateResponseMessage (wresponse, request, cancellationToken);
- }
- public ICredentials DefaultProxyCredentials {
- get {
- throw new NotImplementedException ();
- }
- set {
- throw new NotImplementedException ();
- }
- }
- public int MaxConnectionsPerServer {
- get {
- throw new NotImplementedException ();
- }
- set {
- throw new NotImplementedException ();
- }
- }
- public IDictionary<string, object> Properties {
- get {
- throw new NotImplementedException ();
- }
- }
- }
- }
|