예제 #1
0
        private IResponse Invoke(UrlBuilder url, HttpMethod method, HttpContent content, IBindingSession session,
                                 long?offset, long?length, IDictionary <string, string> headers)
        {
            if (Logger.IsDebugEnabled)
            {
                Logger.Debug("HTTP: " + method.ToString() + " " + url.ToString());
            }

            IPortableAuthenticationProvider authProvider = session.GetAuthenticationProvider() as IPortableAuthenticationProvider;

            HttpClient httpClient = session.GetValue(InvokerHttpClient) as HttpClient;

            if (httpClient == null)
            {
                lock (invokerLock)
                {
                    httpClient = session.GetValue(InvokerHttpClient) as HttpClient;
                    if (httpClient == null)
                    {
                        // create a HTTP client handler
                        HttpClientHandler httpClientHandler = null;

                        if (authProvider != null)
                        {
                            httpClientHandler = authProvider.CreateHttpClientHandler();
                        }
                        if (httpClientHandler == null)
                        {
                            httpClientHandler = new HttpClientHandler();
                        }

                        // redirects
                        if (httpClientHandler.SupportsRedirectConfiguration)
                        {
                            httpClientHandler.AllowAutoRedirect = false;
                        }

                        // compression
                        if (httpClientHandler.SupportsAutomaticDecompression)
                        {
                            string compressionFlag = session.GetValue(SessionParameter.Compression) as string;
                            if (compressionFlag != null && compressionFlag.ToLowerInvariant().Equals("true"))
                            {
                                httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                            }
                        }

                        // authentication
                        httpClientHandler.PreAuthenticate       = true;
                        httpClientHandler.UseDefaultCredentials = false;

                        // authentication provider
                        if (authProvider != null)
                        {
                            authProvider.PrepareHttpClientHandler(httpClientHandler);
                        }

                        // create HttpClient
                        httpClient = new HttpClient(httpClientHandler, true);

                        // timeouts
                        int connectTimeout = session.GetValue(SessionParameter.ConnectTimeout, -2);
                        if (connectTimeout >= -1)
                        {
                            httpClient.Timeout = TimeSpan.FromMilliseconds(connectTimeout);
                        }

                        session.PutValue(InvokerHttpClient, httpClient);
                    }
                }
            }

            HttpRequestMessage request = new HttpRequestMessage(method, url.ToString());

            // set additional headers

            string userAgent = session.GetValue(SessionParameter.UserAgent) as string;

            request.Headers.UserAgent.Add(ProductInfoHeaderValue.Parse(userAgent ?? ClientVersion.UserAgent));

            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    request.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
            }

            // range
            if (offset != null && length != null)
            {
                long longOffset = offset.Value < 0 ? 0 : offset.Value;
                if (length.Value > 0)
                {
                    request.Headers.Range = new RangeHeaderValue(longOffset, longOffset + length.Value - 1);
                }
                else
                {
                    request.Headers.Range = new RangeHeaderValue(longOffset, null);
                }
            }
            else if (offset != null && offset.Value > 0)
            {
                request.Headers.Range = new RangeHeaderValue(offset, null);
            }

            // content
            if (content != null)
            {
                request.Headers.TransferEncodingChunked = true;
                request.Content = content;
            }

            // authentication provider
            if (authProvider != null)
            {
                authProvider.PrepareHttpRequestMessage(request);
            }

            Response response;

            try
            {
                Task <HttpResponseMessage> task = Send(httpClient, request);
                if (task.IsFaulted)
                {
                    throw task.Exception;
                }
                else
                {
                    HttpResponseMessage httpResponseMessage = task.Result;

                    if (authProvider != null)
                    {
                        authProvider.HandleResponse(httpResponseMessage);
                    }
                    response = new Response(httpResponseMessage);
                }
            }
            catch (Exception e)
            {
                throw new CmisConnectionException("Cannot access " + url + ": " + e.Message, e);
            }


            return(response);
        }
예제 #2
0
        private IResponse Invoke(UrlBuilder url, HttpMethod method, HttpContent content, IBindingSession session,
                                 long?offset, long?length, IDictionary <string, string> headers)
        {
            if (Logger.IsDebugEnabled)
            {
                Logger.Debug("HTTP: " + method.ToString() + " " + url.ToString());
            }

            IPortableAuthenticationProvider authProvider = session.GetAuthenticationProvider() as IPortableAuthenticationProvider;

            HttpClient httpClient = session.GetValue(InvokerHttpClient) as HttpClient;

            if (httpClient == null)
            {
                lock (invokerLock)
                {
                    httpClient = session.GetValue(InvokerHttpClient) as HttpClient;
                    if (httpClient == null)
                    {
                        // create a HTTP client handler
                        HttpClientHandler httpClientHandler = null;
                        if (authProvider != null)
                        {
                            httpClientHandler = authProvider.CreateHttpClientHandler();
                        }
                        if (httpClientHandler == null)
                        {
                            httpClientHandler = new HttpClientHandler();
                        }


                        // redirects
                        if (httpClientHandler.SupportsRedirectConfiguration)
                        {
                            httpClientHandler.AllowAutoRedirect = false;
                        }

                        // compression
                        if (httpClientHandler.SupportsAutomaticDecompression)
                        {
                            string compressionFlag = session.GetValue(SessionParameter.Compression) as string;
                            if (compressionFlag != null && compressionFlag.ToLowerInvariant().Equals("true"))
                            {
                                httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                            }
                        }

                        // authentication
                        httpClientHandler.PreAuthenticate       = true;
                        httpClientHandler.UseDefaultCredentials = false;


                        // authentication provider
                        if (authProvider != null)
                        {
                            authProvider.PrepareHttpClientHandler(httpClientHandler);
                        }
                        string                  sslProtocols            = SessionParameterDefaults.SslProtocol;
                        string                  clientCertificateOption = SessionParameterDefaults.ClientCertificateOption;
                        SslProtocols            prot                       = SslProtocols.None;
                        ClientCertificateOption clientOption               = ClientCertificateOption.Manual;
                        object                  sslProtocolsobj            = session.GetValue(SessionParameter.SslProtocols, SessionParameterDefaults.SslProtocol);
                        object                  clientCertificateOptionObj = session.GetValue(SessionParameter.ClientCertificateOption, SessionParameterDefaults.ClientCertificateOption);
                        if (sslProtocolsobj != null)
                        {
                            sslProtocols = $"{sslProtocolsobj}";
                            if (!string.IsNullOrWhiteSpace(sslProtocols))
                            {
                                if (Enum.TryParse <SslProtocols>(sslProtocols, out SslProtocols result))
                                {
                                    prot = result;
                                }
                            }
                        }
                        if (clientCertificateOptionObj != null)
                        {
                            clientCertificateOption = $"{clientCertificateOptionObj}";
                            if (!string.IsNullOrWhiteSpace(clientCertificateOption))
                            {
                                if (Enum.TryParse <ClientCertificateOption>(clientCertificateOption, out ClientCertificateOption result))
                                {
                                    clientOption = result;
                                }
                            }
                        }
                        httpClientHandler.ClientCertificateOptions = clientOption;
                        httpClientHandler.SslProtocols             = prot;
                        httpClientHandler.ServerCertificateCustomValidationCallback =
                            (httpRequestMessage, cert, cetChain, policyErrors) =>
                        {
                            return(true);
                        };
                        // create HttpClient
                        httpClient = new HttpClient(httpClientHandler, true);

                        // timeouts
                        int connectTimeout = session.GetValue(SessionParameter.ConnectTimeout, -2);
                        if (connectTimeout >= -1)
                        {
                            httpClient.Timeout = TimeSpan.FromMilliseconds(connectTimeout);
                        }
                        session.PutValue(InvokerHttpClient, httpClient);
                    }
                }
            }

            HttpRequestMessage request = new HttpRequestMessage(method, url.ToString());

            try
            {
                // set additional headers
                string userAgent = session.GetValue(SessionParameter.UserAgent) as string;
                request.Headers.UserAgent.Add(ProductInfoHeaderValue.Parse(userAgent ?? ClientVersion.UserAgent));

                if (headers != null)
                {
                    foreach (KeyValuePair <string, string> header in headers)
                    {
                        request.Headers.TryAddWithoutValidation(header.Key, header.Value);
                    }
                }

                // range
                if (offset != null && length != null)
                {
                    long longOffset = offset.Value < 0 ? 0 : offset.Value;
                    if (length.Value > 0)
                    {
                        request.Headers.Range = new RangeHeaderValue(longOffset, longOffset + length.Value - 1);
                    }
                    else
                    {
                        request.Headers.Range = new RangeHeaderValue(longOffset, null);
                    }
                }
                else if (offset != null && offset.Value > 0)
                {
                    request.Headers.Range = new RangeHeaderValue(offset, null);
                }

                // content
                if (content != null)
                {
                    request.Headers.TransferEncodingChunked = true;
                    request.Content = content;
                }

                // authentication provider
                if (authProvider != null)
                {
                    authProvider.PrepareHttpRequestMessage(request);
                }

                Response response;
                try
                {
                    Task <HttpResponseMessage> task = Send(httpClient, request);
                    if (task.IsFaulted)
                    {
                        throw task.Exception;
                    }
                    else
                    {
                        HttpResponseMessage httpResponseMessage = task.Result;

                        if (authProvider != null)
                        {
                            authProvider.HandleResponse(httpResponseMessage);
                        }
                        response = new Response(httpResponseMessage);
                    }
                }
                catch (Exception e)
                {
                    throw new CmisConnectionException("Cannot access " + url + ": " + e.Message, e);
                }


                return(response);
            }
            finally
            {
                request.Dispose();
            }
        }