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()); 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(); } }
private IResponse Invoke(UrlBuilder url, HttpMethod method, System.Net.Http.HttpContent content, IBindingSession session, long?offset, long?length, IDictionary <string, string> headers) { if (Logger.IsDebugEnabled) { Logger.Debug("HTTP: " + method.ToString() + " " + url.ToString()); } IWindowsAuthenticationProvider authProvider = session.GetAuthenticationProvider() as IWindowsAuthenticationProvider; HttpClient httpClient = session.GetValue(InvokerHttpClient) as HttpClient; if (httpClient == null) { lock (invokerLock) { httpClient = session.GetValue(InvokerHttpClient) as HttpClient; if (httpClient == null) { HttpBaseProtocolFilter httpClientFilter = new HttpBaseProtocolFilter(); // redirects httpClientFilter.AllowAutoRedirect = false; // compression string compressionFlag = session.GetValue(SessionParameter.Compression) as string; if (compressionFlag != null && compressionFlag.ToLowerInvariant().Equals("true")) { httpClientFilter.AutomaticDecompression = true; } // authentication httpClientFilter.AllowUI = false; // authentication provider if (authProvider != null) { authProvider.PrepareHttpClientFilter(httpClientFilter); } // create HttpClient httpClient = new HttpClient(httpClientFilter); session.PutValue(InvokerHttpClient, httpClient); } } } HttpRequestMessage request = new HttpRequestMessage(method, new Uri(url.ToString())); // set additional headers string userAgent = session.GetValue(SessionParameter.UserAgent) as string; request.Headers.UserAgent.Add(HttpProductInfoHeaderValue.Parse(userAgent ?? ClientVersion.UserAgent)); if (headers != null) { foreach (KeyValuePair <string, string> header in headers) { request.Headers.TryAppendWithoutValidation(header.Key, header.Value); } } // range if (offset != null && length != null) { long longOffset = offset.Value < 0 ? 0 : offset.Value; if (length.Value > 0) { request.Headers.Add(new KeyValuePair <string, string>("Range", "bytes=" + longOffset + "-" + (longOffset + length.Value - 1))); } else { request.Headers.Add(new KeyValuePair <string, string>("Range", "bytes=" + longOffset + "-")); } } else if (offset != null && offset.Value > 0) { request.Headers.Add(new KeyValuePair <string, string>("Range", "bytes=" + offset.Value + "-")); } // content if (content != null) { request.Content = new ConvertedHttpContent(content); if (request.Content.Headers.ContentLength == null) { request.Headers.TransferEncoding.TryParseAdd("chunked"); } } // authentication provider if (authProvider != null) { authProvider.PrepareHttpRequestMessage(request); } // timeouts int timeout = session.GetValue(SessionParameter.ConnectTimeout, -2); WindowsResponse response; try { Task <HttpResponseMessage> task = Send(httpClient, request, timeout); if (task.IsFaulted) { throw task.Exception; } else { HttpResponseMessage httpResponseMessage = task.Result; if (authProvider != null) { authProvider.HandleResponse(httpResponseMessage); } response = new WindowsResponse(httpResponseMessage); } } catch (Exception e) { throw new CmisConnectionException("Cannot access " + url + ": " + e.Message, e); } return(response); }