Search in sources :

Example 1 with ImmediateResponseExchangeHandler

use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.

the class ServerH2StreamHandler method handle.

@Override
public void handle(final HttpException ex, final boolean endStream) throws HttpException, IOException {
    if (done.get()) {
        throw ex;
    }
    switch(requestState) {
        case HEADERS:
            requestState = endStream ? MessageState.COMPLETE : MessageState.BODY;
            if (!responseCommitted.get()) {
                final AsyncResponseProducer responseProducer = new BasicResponseProducer(ServerSupport.toStatusCode(ex), ServerSupport.toErrorMessage(ex));
                exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
                exchangeHandler.handleRequest(null, null, responseChannel, context);
            } else {
                throw ex;
            }
            break;
        case BODY:
            responseState = MessageState.COMPLETE;
        default:
            throw ex;
    }
}
Also used : AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) ImmediateResponseExchangeHandler(org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer)

Example 2 with ImmediateResponseExchangeHandler

use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.

the class Http1IntegrationTest method testPostIdentityTransferOutOfSequenceResponse.

@Test
public void testPostIdentityTransferOutOfSequenceResponse() throws Exception {
    server.register("/hello", () -> new ImmediateResponseExchangeHandler(500, "Go away"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    final int reqNo = 5;
    for (int i = 0; i < reqNo; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
        final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/hello"), new MultiLineEntityProducer("Hello", 16 * i)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        streamEndpoint.close();
        Assertions.assertNotNull(result);
        final HttpResponse response = result.getHead();
        final String entity = result.getBody();
        Assertions.assertNotNull(response);
        Assertions.assertEquals(500, response.getCode());
        Assertions.assertEquals("Go away", entity);
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) ImmediateResponseExchangeHandler(org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Example 3 with ImmediateResponseExchangeHandler

use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.

the class ServerHttp1StreamHandler method terminateExchange.

void terminateExchange(final HttpException ex) throws HttpException, IOException {
    if (done.get() || requestState != MessageState.HEADERS) {
        throw new ProtocolException("Unexpected message head");
    }
    receivedRequest = null;
    requestState = MessageState.COMPLETE;
    final HttpResponse response = new BasicHttpResponse(ServerSupport.toStatusCode(ex));
    response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
    final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, ServerSupport.toErrorMessage(ex));
    exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
    exchangeHandler.handleRequest(null, null, responseChannel, context);
}
Also used : AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) ImmediateResponseExchangeHandler(org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer)

Example 4 with ImmediateResponseExchangeHandler

use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.

the class ServerH2StreamHandler method consumeHeader.

@Override
public void consumeHeader(final List<Header> headers, final boolean endStream) throws HttpException, IOException {
    if (done.get()) {
        throw new ProtocolException("Unexpected message headers");
    }
    switch(requestState) {
        case HEADERS:
            requestState = endStream ? MessageState.COMPLETE : MessageState.BODY;
            final HttpRequest request = DefaultH2RequestConverter.INSTANCE.convert(headers);
            final EntityDetails requestEntityDetails = endStream ? null : new IncomingEntityDetails(request, -1);
            final AsyncServerExchangeHandler handler;
            try {
                handler = exchangeHandlerFactory != null ? exchangeHandlerFactory.create(request, context) : null;
            } catch (final ProtocolException ex) {
                throw new H2StreamResetException(H2Error.PROTOCOL_ERROR, ex.getMessage());
            }
            if (handler == null) {
                throw new H2StreamResetException(H2Error.REFUSED_STREAM, "Stream refused");
            }
            exchangeHandler = handler;
            context.setProtocolVersion(HttpVersion.HTTP_2);
            context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
            try {
                httpProcessor.process(request, requestEntityDetails, context);
                connMetrics.incrementRequestCount();
                receivedRequest = request;
                exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
            } catch (final HttpException ex) {
                if (!responseCommitted.get()) {
                    final AsyncResponseProducer responseProducer = new BasicResponseProducer(ServerSupport.toStatusCode(ex), ServerSupport.toErrorMessage(ex));
                    exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
                    exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
                } else {
                    throw ex;
                }
            }
            break;
        case BODY:
            responseState = MessageState.COMPLETE;
            exchangeHandler.streamEnd(headers);
            break;
        default:
            throw new ProtocolException("Unexpected message headers");
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) ProtocolException(org.apache.hc.core5.http.ProtocolException) EntityDetails(org.apache.hc.core5.http.EntityDetails) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails) H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails) HttpException(org.apache.hc.core5.http.HttpException) ImmediateResponseExchangeHandler(org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)

Example 5 with ImmediateResponseExchangeHandler

use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.

the class ServerHttp1StreamHandler method consumeHeader.

void consumeHeader(final HttpRequest request, final EntityDetails requestEntityDetails) throws HttpException, IOException {
    if (done.get() || requestState != MessageState.HEADERS) {
        throw new ProtocolException("Unexpected message head");
    }
    receivedRequest = request;
    requestState = requestEntityDetails == null ? MessageState.COMPLETE : MessageState.BODY;
    AsyncServerExchangeHandler handler;
    try {
        handler = exchangeHandlerFactory.create(request, context);
    } catch (final MisdirectedRequestException ex) {
        handler = new ImmediateResponseExchangeHandler(HttpStatus.SC_MISDIRECTED_REQUEST, ex.getMessage());
    } catch (final HttpException ex) {
        handler = new ImmediateResponseExchangeHandler(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
    }
    if (handler == null) {
        handler = new ImmediateResponseExchangeHandler(HttpStatus.SC_NOT_FOUND, "Cannot handle request");
    }
    exchangeHandler = handler;
    final ProtocolVersion transportVersion = request.getVersion();
    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
        throw new UnsupportedHttpVersionException(transportVersion);
    }
    context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
    context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
    try {
        httpProcessor.process(request, requestEntityDetails, context);
        exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
    } catch (final HttpException ex) {
        if (!responseCommitted.get()) {
            final HttpResponse response = new BasicHttpResponse(ServerSupport.toStatusCode(ex));
            response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
            final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, ServerSupport.toErrorMessage(ex));
            exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
            exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
        } else {
            throw ex;
        }
    }
}
Also used : AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpException(org.apache.hc.core5.http.HttpException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) MisdirectedRequestException(org.apache.hc.core5.http.MisdirectedRequestException) ImmediateResponseExchangeHandler(org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)

Aggregations

ImmediateResponseExchangeHandler (org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler)5 AsyncResponseProducer (org.apache.hc.core5.http.nio.AsyncResponseProducer)4 BasicResponseProducer (org.apache.hc.core5.http.nio.support.BasicResponseProducer)4 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 HttpException (org.apache.hc.core5.http.HttpException)2 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)2 InetSocketAddress (java.net.InetSocketAddress)1 EntityDetails (org.apache.hc.core5.http.EntityDetails)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 Message (org.apache.hc.core5.http.Message)1 MisdirectedRequestException (org.apache.hc.core5.http.MisdirectedRequestException)1 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)1 UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)1 IncomingEntityDetails (org.apache.hc.core5.http.impl.IncomingEntityDetails)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 DefaultHttpProcessor (org.apache.hc.core5.http.protocol.DefaultHttpProcessor)1 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)1