Search in sources :

Example 6 with IncomingEntityDetails

use of org.apache.hc.core5.http.impl.IncomingEntityDetails in project httpcomponents-core by apache.

the class ClientPushH2StreamHandler method consumeHeader.

@Override
public void consumeHeader(final List<Header> headers, final boolean endStream) throws HttpException, IOException {
    if (responseState == MessageState.HEADERS) {
        Asserts.notNull(request, "Request");
        Asserts.notNull(exchangeHandler, "Exchange handler");
        final HttpResponse response = DefaultH2ResponseConverter.INSTANCE.convert(headers);
        final EntityDetails entityDetails = endStream ? null : new IncomingEntityDetails(request, -1);
        context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
        httpProcessor.process(response, entityDetails, context);
        connMetrics.incrementResponseCount();
        exchangeHandler.consumePromise(request, response, entityDetails, context);
        if (endStream) {
            responseState = MessageState.COMPLETE;
            exchangeHandler.streamEnd(null);
        } else {
            responseState = MessageState.BODY;
        }
    } else {
        throw new ProtocolException("Unexpected message headers");
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) EntityDetails(org.apache.hc.core5.http.EntityDetails) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails) HttpResponse(org.apache.hc.core5.http.HttpResponse) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails)

Example 7 with IncomingEntityDetails

use of org.apache.hc.core5.http.impl.IncomingEntityDetails 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)

Aggregations

IncomingEntityDetails (org.apache.hc.core5.http.impl.IncomingEntityDetails)4 EntityDetails (org.apache.hc.core5.http.EntityDetails)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3 HeaderGroup (org.apache.hc.core5.http.message.HeaderGroup)3 Test (org.junit.jupiter.api.Test)3 Header (org.apache.hc.core5.http.Header)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 MessageHeaders (org.apache.hc.core5.http.MessageHeaders)2 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)2 HashSet (java.util.HashSet)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 StatusLine (org.apache.hc.core5.http.message.StatusLine)1 AsyncResponseProducer (org.apache.hc.core5.http.nio.AsyncResponseProducer)1 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)1 ContentDecoder (org.apache.hc.core5.http.nio.ContentDecoder)1 BasicResponseProducer (org.apache.hc.core5.http.nio.support.BasicResponseProducer)1 ImmediateResponseExchangeHandler (org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler)1 H2StreamResetException (org.apache.hc.core5.http2.H2StreamResetException)1