Search in sources :

Example 66 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseConnControlEntityChunked.

@Test
public void testResponseConnControlEntityChunked() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null, true));
    final ResponseConnControl interceptor = new ResponseConnControl();
    interceptor.process(response, response.getEntity(), context);
    final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
    Assertions.assertNull(header);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 67 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseContentStatusNoContent.

@Test
public void testResponseContentStatusNoContent() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setCode(HttpStatus.SC_NO_CONTENT);
    final ResponseContent interceptor = new ResponseContent();
    interceptor.process(response, response.getEntity(), context);
    final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNull(header);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 68 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.

the class HttpService method handleRequest.

/**
 * Handles receives one HTTP request over the given connection within the
 * given execution context and sends a response back to the client.
 *
 * @param conn the active connection to the client
 * @param context the actual execution context.
 * @throws IOException in case of an I/O error.
 * @throws HttpException in case of HTTP protocol violation or a processing
 *   problem.
 */
public void handleRequest(final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException {
    final AtomicBoolean responseSubmitted = new AtomicBoolean(false);
    try {
        final ClassicHttpRequest request = conn.receiveRequestHeader();
        if (request == null) {
            conn.close();
            return;
        }
        if (streamListener != null) {
            streamListener.onRequestHead(conn, request);
        }
        conn.receiveRequestEntity(request);
        final ProtocolVersion transportVersion = request.getVersion();
        context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
        context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
        context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
        this.processor.process(request, request.getEntity(), context);
        this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() {

            @Override
            public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException {
                if (responseSubmitted.get()) {
                    throw new HttpException("Response already submitted");
                }
                if (response.getCode() >= HttpStatus.SC_SUCCESS) {
                    throw new HttpException("Invalid intermediate response");
                }
                if (streamListener != null) {
                    streamListener.onResponseHead(conn, response);
                }
                conn.sendResponseHeader(response);
                conn.flush();
            }

            @Override
            public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
                try {
                    final ProtocolVersion transportVersion = response.getVersion();
                    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
                        throw new UnsupportedHttpVersionException(transportVersion);
                    }
                    ServerSupport.validateResponse(response, response.getEntity());
                    context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
                    context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
                    processor.process(response, response.getEntity(), context);
                    responseSubmitted.set(true);
                    conn.sendResponseHeader(response);
                    if (streamListener != null) {
                        streamListener.onResponseHead(conn, response);
                    }
                    if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) {
                        conn.sendResponseEntity(response);
                    }
                    // Make sure the request content is fully consumed
                    EntityUtils.consume(request.getEntity());
                    final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context);
                    if (streamListener != null) {
                        streamListener.onExchangeComplete(conn, keepAlive);
                    }
                    if (!keepAlive) {
                        conn.close();
                    }
                    conn.flush();
                } finally {
                    response.close();
                }
            }
        }, context);
    } catch (final HttpException ex) {
        if (responseSubmitted.get()) {
            throw ex;
        }
        try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) {
            handleException(ex, errorResponse);
            errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
            context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse);
            this.processor.process(errorResponse, errorResponse.getEntity(), context);
            conn.sendResponseHeader(errorResponse);
            if (streamListener != null) {
                streamListener.onResponseHead(conn, errorResponse);
            }
            conn.sendResponseEntity(errorResponse);
            conn.close();
        }
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpServerRequestHandler(org.apache.hc.core5.http.io.HttpServerRequestHandler) BasicHttpServerRequestHandler(org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 69 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project dialogue by palantir.

the class InactivityValidationAwareConnectionKeepAliveStrategyTest method testKeepAliveHeaderWithoutTimeout.

@Test
void testKeepAliveHeaderWithoutTimeout() {
    InactivityValidationAwareConnectionKeepAliveStrategy strategy = new InactivityValidationAwareConnectionKeepAliveStrategy(manager, "name");
    BasicClassicHttpResponse response = new BasicClassicHttpResponse(200);
    response.addHeader("Keep-Alive", "max=60");
    TimeValue value = strategy.getKeepAliveDuration(response, CONTEXT);
    assertThat(value).isEqualTo(CONTEXT.getRequestConfig().getConnectionKeepAlive());
    verify(manager).setValidateAfterInactivity(eq(INITIAL_TIMEOUT));
}
Also used : TimeValue(org.apache.hc.core5.util.TimeValue) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)69 Test (org.junit.jupiter.api.Test)57 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)56 Header (org.apache.hc.core5.http.Header)34 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)29 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)19 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)14 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpEntity (org.apache.hc.core5.http.HttpEntity)12 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)11 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)10 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)10 BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)9 TimeValue (org.apache.hc.core5.util.TimeValue)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 Timeout (org.apache.hc.core5.util.Timeout)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3