public long getKeepAliveDuration(HttpResponse response, HttpContext context) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch(NumberFormatException ignore) { } } } return -1; }
@Override public Proxy determineProxy(HttpHost host, HttpRequest request, HttpContext context, IPPool ipPool, CrawlerSession crawlerSession) { HttpClientContext httpClientContext = HttpClientContext.adapt(context); Proxy proxy = (Proxy) crawlerSession.getExtInfo(VSCRAWLER_AVPROXY_KEY); if (proxy == null) { String accessUrl = null; if (request instanceof HttpRequestWrapper || request instanceof HttpGet) { accessUrl = HttpUriRequest.class.cast(request).getURI().toString(); } if (!PoolUtil.isDungProxyEnabled(httpClientContext)) { log.info("{}不会被代理", accessUrl); return null; } proxy = ipPool.getIP(host.getHostName(), accessUrl); if (proxy == null) { return null; } crawlerSession.setExtInfo(VSCRAWLER_AVPROXY_KEY, proxy); } return proxy; }
public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException { for (int c = 1;; c++) { HttpResponse response = backend.execute(target, request, context); if (retryStrategy.retryRequest(response, c, context)) { long nextInterval = retryStrategy.getRetryInterval(); try { log.trace("Wait for " + nextInterval); Thread.sleep(nextInterval); } catch (InterruptedException e) { throw new InterruptedIOException(e.getMessage()); } } else { return response; } } }
/** * Puts a new request in queue as a new thread in pool to be executed * * @param client HttpClient to be used for request, can differ in single requests * @param contentType MIME body type, for POST and PUT requests, may be null * @param context Context of Android application, to hold the reference of request * @param httpContext HttpContext in which the request will be executed * @param responseHandler ResponseHandler or its subclass to put the response into * @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete, * HttpPost, HttpGet, HttpPut, etc. * @return RequestHandle of future request process */ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) { if (contentType != null) { uriRequest.addHeader("Content-Type", contentType); } responseHandler.setRequestHeaders(uriRequest.getAllHeaders()); responseHandler.setRequestURI(uriRequest.getURI()); Future<?> request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler)); if (context != null) { // Add request to request map List<WeakReference<Future<?>>> requestList = requestMap.get(context); if (requestList == null) { requestList = new LinkedList<WeakReference<Future<?>>>(); requestMap.put(context, requestList); } requestList.add(new WeakReference<Future<?>>(request)); // TODO: Remove dead weakrefs from requestLists? } return new RequestHandle(request); }
@Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { long timeout = Long.parseLong(value) * 1000; if (timeout > 20 * 1000) { return 20 * 1000; } else { return timeout; } } } return 5 * 1000; }
@Override public CloseableHttpResponse execute( final HttpUriRequest request, final HttpContext context) throws IOException, ClientProtocolException { Subsegment subsegment = recorder.beginSubsegment(determineTarget(request).getHostName()); return wrapHttpSupplier(subsegment, () -> { if (null != subsegment) { TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request)); } CloseableHttpResponse response = wrappedClient.execute(request, context); if (null != subsegment) { TracedResponseHandler.addResponseInformation(subsegment, response); } return response; }); }
public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING)) { request.addHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING, AsyncHttpClient .ENCODING_GZIP); } for (String header : AsyncHttpClient.access$000(this.this$0).keySet()) { if (request.containsHeader(header)) { Header overwritten = request.getFirstHeader(header); Log.d(AsyncHttpClient.LOG_TAG, String.format("Headers were overwritten! (%s | %s)" + " overwrites (%s | %s)", new Object[]{header, AsyncHttpClient.access$000 (this.this$0).get(header), overwritten.getName(), overwritten.getValue()})); request.removeHeader(overwritten); } request.addHeader(header, (String) AsyncHttpClient.access$000(this.this$0).get(header)); } }
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute("http.auth.target-scope"); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute("http.auth" + ".credentials-provider"); HttpHost targetHost = (HttpHost) context.getAttribute("http.target_host"); if (authState.getAuthScheme() == null) { Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName (), targetHost.getPort())); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
@Override public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException { Subsegment subsegment = getRecorder().beginSubsegment(TracedHttpClient.determineTarget(request).getHostName()); try { if (null != subsegment) { TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request)); } CloseableHttpResponse response = super.execute(request, context); if (null != subsegment) { TracedResponseHandler.addResponseInformation(subsegment, response); } return response; } catch (Exception e) { if (null != subsegment) { subsegment.addException(e); } throw e; } finally { if (null != subsegment) { getRecorder().endSubsegment(); } } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { final HttpRequestAttachment.Builder builder = create("Request", request.getRequestLine().getUri()) .withMethod(request.getRequestLine().getMethod()); Stream.of(request.getAllHeaders()) .forEach(header -> builder.withHeader(header.getName(), header.getValue())); if (request instanceof HttpEntityEnclosingRequest) { final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); entity.writeTo(os); final String body = new String(os.toByteArray(), StandardCharsets.UTF_8); builder.withBody(body); } final HttpRequestAttachment requestAttachment = builder.build(); processor.addAttachment(requestAttachment, renderer); }
public boolean isAuthenticationRequested( final HttpHost host, final HttpResponse response, final AuthenticationStrategy authStrategy, final AuthState authState, final HttpContext context) { if (authStrategy.isAuthenticationRequested(host, response, context)) { return true; } else { switch (authState.getState()) { case CHALLENGED: case HANDSHAKE: authState.setState(AuthProtocolState.SUCCESS); authStrategy.authSucceeded(host, authState.getAuthScheme(), context); break; case SUCCESS: break; default: authState.setState(AuthProtocolState.UNCHALLENGED); } return false; } }
@Override public Socket connectSocket( final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context ) throws IOException { Socket socket0 = socket != null ? socket : createSocket(context); if (localAddress != null) { socket0.bind(localAddress); } try { socket0.connect(remoteAddress, connectTimeout); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, host, remoteAddress.getAddress()); } return socket0; }
@Override public Socket connectSocket( final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { if (log.isDebugEnabled()) { log.debug("Connecting to {}:{}", remoteAddress.getAddress(), remoteAddress.getPort()); } Socket connectedSocket = super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context); if (connectedSocket instanceof SSLSocket) { return new SdkSslSocket((SSLSocket) connectedSocket); } return new SdkSocket(connectedSocket); }
protected HttpContext createHttpContext() { HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.SCHEME_REGISTRY, getConnectionManager().getSchemeRegistry()); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.COOKIE_STORE, getCookieStore()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; }
@Test public void testExecute_happy() throws IOException { Map<String, String> headers = new HashMap<>(); headers.put("Account-Id", "fubar"); headers.put("Content-Type", "application/json"); GenericApiGatewayResponse response = client.execute( new GenericApiGatewayRequestBuilder() .withBody(new ByteArrayInputStream("test request".getBytes())) .withHttpMethod(HttpMethodName.POST) .withHeaders(headers) .withResourcePath("/test/orders").build()); assertEquals("Wrong response body", "test payload", response.getBody()); assertEquals("Wrong response status", 200, response.getHttpResponse().getStatusCode()); Mockito.verify(mockClient, times(1)).execute(argThat(new LambdaMatcher<>( x -> (x.getMethod().equals("POST") && x.getFirstHeader("Account-Id").getValue().equals("fubar") && x.getFirstHeader("x-api-key").getValue().equals("12345") && x.getFirstHeader("Authorization").getValue().startsWith("AWS4") && x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/test/orders")))), any(HttpContext.class)); }
@Override protected HttpResponse doSendRequest(final HttpRequest request, final HttpClientConnection conn, final HttpContext context) throws IOException, HttpException { synchronized(listener) { listener.log(TranscriptListener.Type.request, request.getRequestLine().toString()); for(Header header : request.getAllHeaders()) { switch(header.getName()) { case HttpHeaders.AUTHORIZATION: case "X-Auth-Key": case "X-Auth-Token": listener.log(TranscriptListener.Type.request, String.format("%s: %s", header.getName(), StringUtils.repeat("*", Integer.min(8, StringUtils.length(header.getValue()))))); break; default: listener.log(TranscriptListener.Type.request, header.toString()); break; } } } return super.doSendRequest(request, conn, context); }
@Override public long getKeepAliveDuration( HttpResponse response, HttpContext context) { // If there's a Keep-Alive timeout directive in the response and it's // shorter than our configured max, honor that. Otherwise go with the // configured maximum. long duration = DefaultConnectionKeepAliveStrategy.INSTANCE .getKeepAliveDuration(response, context); if (0 < duration && duration < maxIdleTime) { return duration; } return maxIdleTime; }
private Response executeRequest(HttpRequestBase httpMethod, HttpContext httpContext) throws RestException { try (CloseableHttpResponse httpResponse = client.execute(httpMethod, httpContext)) { return new Response(httpResponse.getStatusLine().getStatusCode(), HTTPCUtil.getHeaders(httpResponse), HTTPCUtil.handleResponse(httpResponse)); } catch (Exception e) { httpMethod.abort(); throw new RestException(e, e.getMessage()); } }
@Override public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket(context)); if (localAddress != null) { InetSocketAddress isa = new InetSocketAddress(localAddress.getAddress(), localAddress.getPort()); sslsock.bind(isa); } sslsock.connect(remoteAddress, connectTimeout); return sslsock; }
@Override public boolean retryRequest( final IOException exception, int executionCount, final HttpContext context) { boolean retry = super.retryRequest(exception, executionCount, context); if (retry) { AWSRequestMetrics awsRequestMetrics = (AWSRequestMetrics) context .getAttribute(AWSRequestMetrics.class.getSimpleName()); if (awsRequestMetrics != null) { awsRequestMetrics.incrementCounter(Field.HttpClientRetryCount); } } return retry; }
public boolean isAuthenticationRequested( final HttpResponse response, final HttpContext context) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } int status = response.getStatusLine().getStatusCode(); return status == HttpStatus.SC_UNAUTHORIZED; }
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) { this.client = (AbstractHttpClient) Utils.notNull(client, "client"); this.context = (HttpContext) Utils.notNull(context, "context"); this.request = (HttpUriRequest) Utils.notNull(request, SocialConstants.TYPE_REQUEST); this.responseHandler = (ResponseHandlerInterface) Utils.notNull(responseHandler, "responseHandler"); }
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { final URI uri = URI.create(request.getRequestLine().getUri()); if (uri.getPath().endsWith(loginPath)) { LOG.debug("Request ends with {} so I'm not intercepting the request", loginPath); return; } Cookie loginCookie = getLoginCookie(context, loginTokenName); if (loginCookie != null) { LOG.debug("Request has cookie {}={} so I'm not intercepting the request", loginCookie.getName(), loginCookie.getValue()); return; } // get host final HttpHost host = HttpClientContext.adapt(context).getTargetHost(); // get the username and password from the credentials provider final CredentialsProvider credsProvider = HttpClientContext.adapt(context).getCredentialsProvider(); final AuthScope scope = new AuthScope(host.getHostName(), host.getPort()); final String username = credsProvider.getCredentials(scope).getUserPrincipal().getName(); final String password = credsProvider.getCredentials(scope).getPassword(); List<NameValuePair> parameters = new LinkedList<>(); parameters.add(new BasicNameValuePair("j_username", username)); parameters.add(new BasicNameValuePair("j_password", password)); HttpEntity httpEntity = new UrlEncodedFormEntity(parameters, "utf-8"); HttpPost loginPost = new HttpPost(URI.create(request.getRequestLine().getUri()).resolve(loginPath)); loginPost.setEntity(httpEntity); final CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build(); client.execute(host, loginPost, context); }
/** * Simple interface method, to enable or disable redirects. If you set * manually RedirectHandler on underlying HttpClient, effects of this method * will be canceled. * * @param enableRedirects boolean */ public void setEnableRedirects(final boolean enableRedirects) { httpClient.setRedirectHandler(new DefaultRedirectHandler() { @Override public boolean isRedirectRequested(HttpResponse response, HttpContext context) { return enableRedirects; } }); }
HttpComponentsAsyncClientHttpRequest(HttpAsyncClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) { this.httpClient = httpClient; this.httpRequest = httpRequest; this.httpContext = httpContext; }
@Override public <T> T execute(HttpHost host, HttpRequest request, ResponseHandler<? extends T> handler, HttpContext context) throws IOException { HttpResponse response = execute(host, request, context); try { return handler.handleResponse(response); } finally { consumeContentQuietly(response); } }
public void updateSecureConnection( final OperatedClientConnection conn, final HttpHost target, final HttpContext context, final HttpParams params) throws IOException { if (conn == null) { throw new IllegalArgumentException("Connection may not be null"); } if (target == null) { throw new IllegalArgumentException("Target host may not be null"); } if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } if (!conn.isOpen()) { throw new IllegalStateException("Connection must be open"); } final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); if (!(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory)) { throw new IllegalArgumentException ("Target scheme (" + schm.getName() + ") must have layered socket factory."); } SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory(); Socket sock; try { sock = lsf.createLayeredSocket( conn.getSocket(), target.getHostName(), target.getPort(), params); } catch (ConnectException ex) { throw new HttpHostConnectException(target, ex); } prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); return; } // Obtain the client connection (required) HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute( ExecutionContext.HTTP_CONNECTION); if (conn == null) { this.log.debug("HTTP connection not set in the context"); return; } HttpRoute route = conn.getRoute(); if (route.getHopCount() == 1 || route.isTunnelled()) { if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) { request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } if (route.getHopCount() == 2 && !route.isTunnelled()) { if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) { request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } }
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { Log.d("plan", "DataAndTableRequestHandler"); DataAndTableEntity entity = getDatabaseAndTable(); if (entity == null) { entity = new DataAndTableEntity(); entity.setCode(BaseEntity.FAILURE_CODE); entity.setDataList(new ArrayList<Map<String, List<SimpleNameEntity>>>()); } response.setStatusCode(200); response.setEntity(new StringEntity(ParserJson.getSafeJsonStr(entity), "utf-8")); }
private Future<Response> executeRequest(Request request, HttpRequestBase method, HttpContext context, HTTPCallback<HttpResponse> callback) { if (request.isDownload()) { HttpAsyncRequestProducer producer = HttpAsyncMethods.create(method); HttpAsyncResponseConsumer<HttpResponse> consumer = new BasicAsyncResponseConsumer(); return executeRequest(producer, consumer, context, callback); } else return executeRequest(method, context, callback); }
@Override public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn, final HttpContext context) throws IOException, HttpException { for(Header h : headers) { request.addHeader(new BasicHeader(h.getName(), h.getValue())); } return super.execute(request, conn, context); }
public void setAttribute(final String id, final Object obj) { OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); if (conn instanceof HttpContext) { ((HttpContext) conn).setAttribute(id, obj); } }
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) { this.delegate = new DefaultHttpClient(ccm, params) { @Override protected BasicHttpProcessor createHttpProcessor() { // Add interceptor to prevent making requests from main thread. BasicHttpProcessor processor = super.createHttpProcessor(); processor.addRequestInterceptor(sThreadCheckInterceptor); processor.addRequestInterceptor(new CurlLogger()); return processor; } @Override protected HttpContext createHttpContext() { // Same as DefaultHttpClient.createHttpContext() minus the // cookie store. HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; } }; }
private HttpResponse a(HttpUriRequest httpUriRequest, HttpContext httpContext) throws Exception { try { return this.c.execute(httpUriRequest, httpContext); } catch (Throwable e) { throw new Exception(e); } }
@Override protected List<String> getAuthPreferences( final HttpResponse response, final HttpContext context) { @SuppressWarnings("unchecked") List<String> authpref = (List<String>) response.getParams().getParameter( AuthPNames.PROXY_AUTH_PREF); if (authpref != null) { return authpref; } else { return super.getAuthPreferences(response, context); } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if(StringUtils.isNotBlank(token)) { request.removeHeaders(SDSSession.SDS_AUTH_TOKEN_HEADER); request.addHeader(SDSSession.SDS_AUTH_TOKEN_HEADER, token); } }
@Override public HttpResponse execute(HttpUriRequest request, HttpContext context) { requestExecuted = request; StatusLine statusLine = new BasicStatusLine( new ProtocolVersion("HTTP", 1, 1), mStatusCode, ""); HttpResponse response = new BasicHttpResponse(statusLine); response.setEntity(mResponseEntity); return response; }
@Before public void setUp() throws IOException { AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials("foo", "bar")); mockClient = Mockito.mock(SdkHttpClient.class); HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(new ByteArrayInputStream("test payload".getBytes())); resp.setEntity(entity); Mockito.doReturn(resp).when(mockClient).execute(any(HttpUriRequest.class), any(HttpContext.class)); ClientConfiguration clientConfig = new ClientConfiguration(); client = new GenericApiGatewayClientBuilder() .withClientConfiguration(clientConfig) .withCredentials(credentials) .withEndpoint("https://foobar.execute-api.us-east-1.amazonaws.com") .withRegion(Region.getRegion(Regions.fromName("us-east-1"))) .withApiKey("12345") .withHttpClient(new AmazonHttpClient(clientConfig, mockClient, null)) .build(); }
public Map<String, Header> getChallenges( final HttpResponse response, final HttpContext context) throws MalformedChallengeException { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } Header[] headers = response.getHeaders(AUTH.WWW_AUTH); return parseChallenges(headers); }
/** * Handles the following {@code Content-Encoding}s by * using the appropriate decompressor to wrap the response Entity: * <ul> * <li>gzip - see {@link GzipDecompressingEntity}</li> * <li>deflate - see {@link DeflateDecompressingEntity}</li> * <li>identity - no action needed</li> * </ul> * * @param response the response which contains the entity * @param context not currently used * * @throws HttpException if the {@code Content-Encoding} is none of the above */ public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); // It wasn't a 304 Not Modified response, 204 No Content or similar if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement codec : codecs) { String codecname = codec.getName().toLowerCase(Locale.US); if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); if (context != null) context.setAttribute(UNCOMPRESSED, true); return; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); if (context != null) context.setAttribute(UNCOMPRESSED, true); return; } else if ("identity".equals(codecname)) { /* Don't need to transform the content - no-op */ return; } else { throw new HttpException("Unsupported Content-Coding: " + codec.getName()); } } } } }