Java Code Examples for org.apache.hc.core5.http.protocol.HttpContext#setAttribute()

The following examples show how to use org.apache.hc.core5.http.protocol.HttpContext#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApacheDockerHttpClientImpl.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Override
public Response execute(Request request) {
    HttpContext context = new BasicHttpContext();
    HttpUriRequestBase httpUriRequest = new HttpUriRequestBase(request.method(), URI.create(request.path()));
    httpUriRequest.setScheme(host.getSchemeName());
    httpUriRequest.setAuthority(new URIAuthority(host.getHostName(), host.getPort()));

    request.headers().forEach(httpUriRequest::addHeader);

    InputStream body = request.body();
    if (body != null) {
        httpUriRequest.setEntity(new InputStreamEntity(body, null));
    }

    if (request.hijackedInput() != null) {
        context.setAttribute(HijackingHttpRequestExecutor.HIJACKED_INPUT_ATTRIBUTE, request.hijackedInput());
        httpUriRequest.setHeader("Upgrade", "tcp");
        httpUriRequest.setHeader("Connection", "Upgrade");
    }

    try {
        CloseableHttpResponse response = httpClient.execute(host, httpUriRequest, context);

        return new ApacheResponse(httpUriRequest, response);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: HijackingHttpRequestExecutor.java    From docker-java with Apache License 2.0 4 votes vote down vote up
private ClassicHttpResponse executeHijacked(
    ClassicHttpRequest request,
    HttpClientConnection conn,
    HttpContext context,
    InputStream hijackedInput
) throws HttpException, IOException {
    try {
        context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
        final ProtocolVersion transportVersion = request.getVersion();
        if (transportVersion != null) {
            context.setProtocolVersion(transportVersion);
        }

        conn.sendRequestHeader(request);
        conn.sendRequestEntity(request);
        conn.flush();

        ClassicHttpResponse response = conn.receiveResponseHeader();
        if (response.getCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
            conn.terminateRequest(request);
            throw new ProtocolException("Expected 101 Switching Protocols, got: " + new StatusLine(response));
        }

        Thread thread = new Thread(() -> {
            try {
                BasicClassicHttpRequest fakeRequest = new BasicClassicHttpRequest("POST", "/");
                fakeRequest.setHeader(HttpHeaders.CONTENT_LENGTH, Long.MAX_VALUE);
                fakeRequest.setEntity(new HijackedEntity(hijackedInput));
                conn.sendRequestEntity(fakeRequest);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        thread.setName("docker-java-httpclient5-hijacking-stream-" + System.identityHashCode(request));
        thread.setDaemon(true);
        thread.start();

        // 101 -> 200
        response.setCode(200);
        conn.receiveResponseEntity(response);
        return response;

    } catch (final HttpException | IOException | RuntimeException ex) {
        Closer.closeQuietly(conn);
        throw ex;
    }
}