コード例 #1
0
ファイル: HttpJsonRequest.cs プロジェクト: lahma/ravendb
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            _credentials = requestParams.Credentials;
            Url          = requestParams.Url;
            Method       = requestParams.Method;

            this.factory = factory;
            owner        = requestParams.Owner;
            conventions  = requestParams.Convention;

            if (factory.httpMessageHandler != null)
            {
                handler         = factory.httpMessageHandler;
                recreateHandler = () => factory.httpMessageHandler;
            }
            else
            {
                var webRequestHandler = new WebRequestHandler
                {
                    UseDefaultCredentials = _credentials.HasCredentials() == false,
                    Credentials           = requestParams.Credentials.Credentials,
                };
                recreateHandler = () => new WebRequestHandler
                {
                    Credentials = webRequestHandler.Credentials
                };
                disableAuthentication = () =>
                {
                    webRequestHandler.Credentials           = null;
                    webRequestHandler.UseDefaultCredentials = false;
                };
                handler = webRequestHandler;
            }

            httpClient = new HttpClient(handler);

            if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
            {
                if (Method == "POST" || Method == "PUT" || Method == "PATCH" || Method == "EVAL")
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Encoding", "gzip");
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                }

                httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            }

#if DEBUG
            if (Debugger.IsAttached)
            {
                Timeout = TimeSpan.FromMinutes(5);
            }
#endif

            headers.Add("Raven-Client-Version", ClientVersion);
            WriteMetadata(requestParams.Metadata);
            requestParams.UpdateHeaders(headers);
        }
コード例 #2
0
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            _credentials        = requestParams.DisableAuthentication == false ? requestParams.Credentials : null;
            disabledAuthRetries = requestParams.DisableAuthentication;

            Url    = requestParams.Url;
            Method = requestParams.Method;


            if (requestParams.Timeout.HasValue)
            {
                Timeout = requestParams.Timeout.Value;
            }
            else
            {
                Timeout = TimeSpan.FromSeconds(100);                 // default HttpClient timeout
#if DEBUG
                if (Debugger.IsAttached)
                {
                    Timeout = TimeSpan.FromMinutes(5);
                }
#endif
            }

            this.factory = factory;
            owner        = requestParams.Owner;
            conventions  = requestParams.Convention;

            recreateHandler = factory.httpMessageHandler ?? (
                () => new WebRequestHandler
            {
                UseDefaultCredentials = _credentials != null && _credentials.HasCredentials() == false,
                Credentials = _credentials != null ? _credentials.Credentials : null,
            }
                );

            httpClient = factory.httpClientCache.GetClient(Timeout, _credentials, recreateHandler);

            if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
            {
                if (Method == "POST" || Method == "PUT" || Method == "PATCH" || Method == "EVAL")
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Encoding", "gzip");
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                }

                if (factory.acceptGzipContent)
                {
                    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                }
            }

            headers.Add("Raven-Client-Version", ClientVersion);
            WriteMetadata(requestParams.Metadata);
            requestParams.UpdateHeaders(headers);
        }
コード例 #3
0
ファイル: HttpJsonRequest.cs プロジェクト: mynkow/ravendb
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            _credentials        = requestParams.DisableAuthentication == false ? requestParams.Credentials : null;
            disabledAuthRetries = requestParams.DisableAuthentication;

            Url    = requestParams.Url;
            Method = requestParams.Method;


            if (requestParams.Timeout.HasValue)
            {
                Timeout = requestParams.Timeout.Value;
            }
            else
            {
                Timeout = TimeSpan.FromSeconds(100); // default HttpClient timeout
#if DEBUG
                if (Debugger.IsAttached)
                {
                    Timeout = TimeSpan.FromMinutes(5);
                }
#endif
            }

            this.factory = factory;
            owner        = requestParams.Owner;
            conventions  = requestParams.Convention;

            recreateHandler = factory.httpMessageHandler ?? (
                () =>
            {
                var useDefaultCredentials = _credentials != null && _credentials.HasCredentials() == false;
                ICredentials credentialsToUse = null;
                if (_credentials != null)
                {
                    var networkCredentials = _credentials.Credentials as NetworkCredential;
                    if (networkCredentials != null && factory.authenticationScheme != null)
                    {
                        var credentialCache = new CredentialCache();
                        var uri = new Uri(requestParams.Url);
                        credentialCache.Add(new Uri(string.Format("{0}://{1}:{2}/", uri.Scheme, uri.Host, uri.Port)), factory.authenticationScheme, networkCredentials);

                        credentialsToUse = credentialCache;
                    }
                    else
                    {
                        credentialsToUse = _credentials.Credentials;
                    }
                }
#if !DNXCORE50
                var handler = new WebRequestHandler
                {
                    UseDefaultCredentials = useDefaultCredentials,
                    Credentials = credentialsToUse
                };
#else
                var handler = new WinHttpHandler
                {
                    ServerCredentials = useDefaultCredentials ? CredentialCache.DefaultCredentials : credentialsToUse
                };
#endif
                return(handler);
            }
                );

            httpClient = factory.httpClientCache.GetClient(Timeout, _credentials, recreateHandler);

            if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
            {
                if (Method == "POST" || Method == "PUT" || Method == "PATCH" || Method == "EVAL")
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Encoding", "gzip");
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                }

                if (factory.acceptGzipContent)
                {
                    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                }
            }

            headers.Add("Raven-Client-Version", ClientVersion);
            WriteMetadata(requestParams.Metadata);
            requestParams.UpdateHeaders(headers);
        }
コード例 #4
0
        private async Task <AsyncOperationResult <T> > TryExecuteOperationAsync <T>(string url, string counterStoreName, Func <string, string, Task <T> > operation, bool avoidThrowing, OperationCredentials credentials, CancellationToken cancellationToken)
        {
            var  tryWithPrimaryCredentials = failureCounters.IsFirstFailure(url);
            bool shouldTryAgain            = false;

            try
            {
                cancellationToken.ThrowCancellationIfNotDefault(); //canceling the task here potentially will stop the recursion
                var result = await operation(url, counterStoreName).ConfigureAwait(false);

                failureCounters.ResetFailureCount(url);
                return(new AsyncOperationResult <T>
                {
                    Result = result,
                    Success = true
                });
            }
            catch (Exception e)
            {
                var ae = e as AggregateException;
                ErrorResponseException errorResponseException;
                if (ae != null)
                {
                    errorResponseException = ae.ExtractSingleInnerException() as ErrorResponseException;
                }
                else
                {
                    errorResponseException = e as ErrorResponseException;
                }
                if (tryWithPrimaryCredentials && credentials.HasCredentials() && errorResponseException != null)
                {
                    failureCounters.IncrementFailureCount(url);

                    if (errorResponseException.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        shouldTryAgain = true;
                    }
                }

                if (shouldTryAgain == false)
                {
                    if (avoidThrowing == false)
                    {
                        throw;
                    }

                    bool wasTimeout;
                    var  isServerDown = HttpConnectionHelper.IsServerDown(e, out wasTimeout);

                    if (e.Data.Contains(Constants.RequestFailedExceptionMarker) && isServerDown)
                    {
                        return(new AsyncOperationResult <T>
                        {
                            Success = false,
                            WasTimeout = wasTimeout,
                            Error = e
                        });
                    }

                    if (isServerDown)
                    {
                        return(new AsyncOperationResult <T>
                        {
                            Success = false,
                            WasTimeout = wasTimeout,
                            Error = e
                        });
                    }
                    throw;
                }
            }
            return(await TryExecuteOperationAsync(url, counterStoreName, operation, avoidThrowing, credentials, cancellationToken).ConfigureAwait(false));
        }
コード例 #5
0
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            _credentials        = requestParams.DisableAuthentication == false ? requestParams.Credentials : null;
            disabledAuthRetries = requestParams.DisableAuthentication;

            Url    = requestParams.Url;
            Method = requestParams.Method;

            if (requestParams.Timeout.HasValue)
            {
                Timeout = requestParams.Timeout.Value;
            }
            else
            {
                Timeout = DefaultHttpClientTimeout;
#if DEBUG
                if (Debugger.IsAttached)
                {
                    Timeout = TimeSpan.FromMinutes(5);
                }
#endif
            }

            this.factory      = factory;
            owner             = requestParams.Owner;
            conventions       = requestParams.Convention;
            requestTimeMetric = requestParams.RequestTimeMetric;

            recreateHandler = factory.httpMessageHandler ?? (
                () =>
            {
                var useDefaultCredentials = _credentials != null && _credentials.HasCredentials() == false;
                ICredentials credentialsToUse = null;
                if (_credentials != null)
                {
                    var networkCredentials = _credentials.Credentials as NetworkCredential;
                    if (networkCredentials != null && factory.authenticationScheme != null)
                    {
                        var credentialCache = new CredentialCache();
                        var uri = new Uri(requestParams.Url);
                        credentialCache.Add(new Uri(string.Format("{0}://{1}:{2}/", uri.Scheme, uri.Host, uri.Port)), factory.authenticationScheme, networkCredentials);

                        credentialsToUse = credentialCache;
                    }
                    else
                    {
                        credentialsToUse = _credentials.Credentials;
                    }
                }
                var handler = new HttpClientHandler
                {
                };
                return(handler);
            }
                );

            httpClient = factory.httpClientCache.GetClient(Timeout, _credentials, recreateHandler);

            var isNotGet = Method == HttpMethods.Post || Method == HttpMethods.Put || Method == HttpMethods.Patch || Method == HttpMethods.Eval;
            if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
            {
                if (isNotGet)
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Encoding", "gzip");
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                }

                if (factory.acceptGzipContent)
                {
                    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                }
            }

            if (requestParams.Etag.HasValue)
            {
                var etag = requestParams.Etag.Value.ToString();
                if (isNotGet)
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("If-Match", etag);
                }
                else
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("If-None-Match", etag);
                }
            }

            headers.Add("Raven-Client-Version", ClientVersion);
            requestParams.UpdateHeaders(headers);
        }