/// <exception cref="System.IO.IOException"></exception> public virtual void Upgrade(ManagedHttpClientConnection conn, HttpHost host, HttpContext context) { HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); Lookup <ConnectionSocketFactory> registry = GetSocketFactoryRegistry(clientContext ); ConnectionSocketFactory sf = registry.Lookup(host.GetSchemeName()); if (sf == null) { throw new UnsupportedSchemeException(host.GetSchemeName() + " protocol is not supported" ); } if (!(sf is LayeredConnectionSocketFactory)) { throw new UnsupportedSchemeException(host.GetSchemeName() + " protocol does not support connection upgrade" ); } LayeredConnectionSocketFactory lsf = (LayeredConnectionSocketFactory)sf; System.Net.Sockets.Socket sock = conn.GetSocket(); int port = this.schemePortResolver.Resolve(host); sock = lsf.CreateLayeredSocket(sock, host.GetHostName(), port, context); conn.Bind(sock); }
public virtual void AuthSucceeded(HttpHost authhost, AuthScheme authScheme, HttpContext context) { Args.NotNull(authhost, "Host"); Args.NotNull(authScheme, "Auth scheme"); Args.NotNull(context, "HTTP context"); HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); if (IsCachable(authScheme)) { AuthCache authCache = clientContext.GetAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); clientContext.SetAuthCache(authCache); } if (this.log.IsDebugEnabled()) { this.log.Debug("Caching '" + authScheme.GetSchemeName() + "' auth scheme for " + authhost); } authCache.Put(authhost, authScheme); } }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="Apache.Http.Client.ClientProtocolException"></exception> protected internal override CloseableHttpResponse DoExecute(HttpHost target, IHttpRequest request, HttpContext context) { Args.NotNull(target, "Target host"); Args.NotNull(request, "HTTP request"); HttpExecutionAware execAware = null; if (request is HttpExecutionAware) { execAware = (HttpExecutionAware)request; } try { HttpRequestWrapper wrapper = HttpRequestWrapper.Wrap(request); HttpClientContext localcontext = ((HttpClientContext)HttpClientContext.Adapt(context != null ? context : new BasicHttpContext())); HttpRoute route = new HttpRoute(target); RequestConfig config = null; if (request is Configurable) { config = ((Configurable)request).GetConfig(); } if (config != null) { localcontext.SetRequestConfig(config); } return(this.requestExecutor.Execute(route, wrapper, localcontext, execAware)); } catch (HttpException httpException) { throw new ClientProtocolException(httpException); } }
public virtual object GetUserToken(HttpContext context) { HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); Principal userPrincipal = null; AuthState targetAuthState = clientContext.GetTargetAuthState(); if (targetAuthState != null) { userPrincipal = GetAuthPrincipal(targetAuthState); if (userPrincipal == null) { AuthState proxyAuthState = clientContext.GetProxyAuthState(); userPrincipal = GetAuthPrincipal(proxyAuthState); } } if (userPrincipal == null) { HttpConnection conn = clientContext.GetConnection(); if (conn.IsOpen() && conn is ManagedHttpClientConnection) { SSLSession sslsession = ((ManagedHttpClientConnection)conn).GetSSLSession(); if (sslsession != null) { userPrincipal = sslsession.GetLocalPrincipal(); } } } return(userPrincipal); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="Apache.Http.Client.ClientProtocolException"></exception> protected internal override CloseableHttpResponse DoExecute(HttpHost target, IHttpRequest request, HttpContext context) { Args.NotNull(request, "HTTP request"); HttpExecutionAware execAware = null; if (request is HttpExecutionAware) { execAware = (HttpExecutionAware)request; } try { HttpRequestWrapper wrapper = HttpRequestWrapper.Wrap(request); HttpClientContext localcontext = ((HttpClientContext)HttpClientContext.Adapt(context != null ? context : new BasicHttpContext())); RequestConfig config = null; if (request is Configurable) { config = ((Configurable)request).GetConfig(); } if (config == null) { HttpParams @params = request.GetParams(); if (@params is HttpParamsNames) { if (!((HttpParamsNames)@params).GetNames().IsEmpty()) { config = HttpClientParamConfig.GetRequestConfig(@params); } } else { config = HttpClientParamConfig.GetRequestConfig(@params); } } if (config != null) { localcontext.SetRequestConfig(config); } SetupContext(localcontext); HttpRoute route = DetermineRoute(target, wrapper, localcontext); return(this.execChain.Execute(route, wrapper, localcontext, execAware)); } catch (HttpException httpException) { throw new ClientProtocolException(httpException); } }
/// <summary> /// Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine /// if the given method should be retried. /// </summary> /// <remarks> /// Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine /// if the given method should be retried. /// </remarks> public virtual bool RetryRequest(IOException exception, int executionCount, HttpContext context) { Args.NotNull(exception, "Exception parameter"); Args.NotNull(context, "HTTP context"); if (executionCount > this.retryCount) { // Do not retry if over max retry count return(false); } if (this.nonRetriableClasses.Contains(exception.GetType())) { return(false); } else { foreach (Type rejectException in this.nonRetriableClasses) { if (rejectException.IsInstanceOfType(exception)) { return(false); } } } HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); IHttpRequest request = clientContext.GetRequest(); if (RequestIsAborted(request)) { return(false); } if (HandleAsIdempotent(request)) { // Retry if the request is considered idempotent return(true); } if (!clientContext.IsRequestSent() || this.requestSentRetryEnabled) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return(true); } // otherwise do not retry return(false); }
public virtual void AuthFailed(HttpHost authhost, AuthScheme authScheme, HttpContext context) { Args.NotNull(authhost, "Host"); Args.NotNull(context, "HTTP context"); HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); AuthCache authCache = clientContext.GetAuthCache(); if (authCache != null) { if (this.log.IsDebugEnabled()) { this.log.Debug("Clearing cached auth scheme for " + authhost); } authCache.Remove(authhost); } }
/// <exception cref="Apache.Http.HttpException"></exception> public virtual HttpRoute DetermineRoute(HttpHost host, IHttpRequest request, HttpContext context) { Args.NotNull(host, "Target host"); Args.NotNull(request, "Request"); HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); RequestConfig config = clientContext.GetRequestConfig(); IPAddress local = config.GetLocalAddress(); HttpHost proxy = config.GetProxy(); if (proxy == null) { proxy = DetermineProxy(host, request, context); } HttpHost target; if (host.GetPort() <= 0) { try { target = new HttpHost(host.GetHostName(), this.schemePortResolver.Resolve(host), host.GetSchemeName()); } catch (UnsupportedSchemeException ex) { throw new HttpException(ex.Message); } } else { target = host; } bool secure = Sharpen.Runtime.EqualsIgnoreCase(target.GetSchemeName(), "https"); if (proxy == null) { return(new HttpRoute(target, local, secure)); } else { return(new HttpRoute(target, local, proxy, secure)); } }
/// <exception cref="Apache.Http.Auth.MalformedChallengeException"></exception> public virtual Queue <AuthOption> Select(IDictionary <string, Header> challenges, HttpHost authhost, HttpResponse response, HttpContext context) { Args.NotNull(challenges, "Map of auth challenges"); Args.NotNull(authhost, "Host"); Args.NotNull(response, "HTTP response"); Args.NotNull(context, "HTTP context"); HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); Queue <AuthOption> options = new List <AuthOption>(); Lookup <AuthSchemeProvider> registry = clientContext.GetAuthSchemeRegistry(); if (registry == null) { this.log.Debug("Auth scheme registry not set in the context"); return(options); } CredentialsProvider credsProvider = clientContext.GetCredentialsProvider(); if (credsProvider == null) { this.log.Debug("Credentials provider not set in the context"); return(options); } RequestConfig config = clientContext.GetRequestConfig(); ICollection <string> authPrefs = GetPreferredAuthSchemes(config); if (authPrefs == null) { authPrefs = DefaultSchemePriority; } if (this.log.IsDebugEnabled()) { this.log.Debug("Authentication schemes in the order of preference: " + authPrefs); } foreach (string id in authPrefs) { Header challenge = challenges.Get(id.ToLower(CultureInfo.InvariantCulture)); if (challenge != null) { AuthSchemeProvider authSchemeProvider = registry.Lookup(id); if (authSchemeProvider == null) { if (this.log.IsWarnEnabled()) { this.log.Warn("Authentication scheme " + id + " not supported"); } // Try again continue; } AuthScheme authScheme = authSchemeProvider.Create(context); authScheme.ProcessChallenge(challenge); AuthScope authScope = new AuthScope(authhost.GetHostName(), authhost.GetPort(), authScheme .GetRealm(), authScheme.GetSchemeName()); Credentials credentials = credsProvider.GetCredentials(authScope); if (credentials != null) { options.AddItem(new AuthOption(authScheme, credentials)); } } else { if (this.log.IsDebugEnabled()) { this.log.Debug("Challenge for " + id + " authentication scheme not available"); } } } // Try again return(options); }
//end of switch /// <exception cref="Apache.Http.ProtocolException"></exception> public virtual URI GetLocationURI(IHttpRequest request, HttpResponse response, HttpContext context) { Args.NotNull(request, "HTTP request"); Args.NotNull(response, "HTTP response"); Args.NotNull(context, "HTTP context"); HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); //get the location header to find out where to redirect to Header locationHeader = response.GetFirstHeader("location"); if (locationHeader == null) { // got a redirect response, but no location header throw new ProtocolException("Received redirect response " + response.GetStatusLine () + " but no location header"); } string location = locationHeader.GetValue(); if (this.log.IsDebugEnabled()) { this.log.Debug("Redirect requested to location '" + location + "'"); } RequestConfig config = clientContext.GetRequestConfig(); URI uri = CreateLocationURI(location); // rfc2616 demands the location value be a complete URI // Location = "Location" ":" absoluteURI try { if (!uri.IsAbsolute()) { if (!config.IsRelativeRedirectsAllowed()) { throw new ProtocolException("Relative redirect location '" + uri + "' not allowed" ); } // Adjust location URI HttpHost target = clientContext.GetTargetHost(); Asserts.NotNull(target, "Target host"); URI requestURI = new URI(request.GetRequestLine().GetUri()); URI absoluteRequestURI = URIUtils.RewriteURI(requestURI, target, false); uri = URIUtils.Resolve(absoluteRequestURI, uri); } } catch (URISyntaxException ex) { throw new ProtocolException(ex.Message, ex); } RedirectLocations redirectLocations = (RedirectLocations)clientContext.GetAttribute (HttpClientContext.RedirectLocations); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); context.SetAttribute(HttpClientContext.RedirectLocations, redirectLocations); } if (!config.IsCircularRedirectsAllowed()) { if (redirectLocations.Contains(uri)) { throw new CircularRedirectException("Circular redirect to '" + uri + "'"); } } redirectLocations.Add(uri); return(uri); }