Search in sources :

Example 1 with UnsupportedHttpVersionException

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

the class ClientHttp1StreamHandler method consumeHeader.

void consumeHeader(final HttpResponse response, final EntityDetails entityDetails) throws HttpException, IOException {
    if (done.get() || responseState != MessageState.HEADERS) {
        throw new ProtocolException("Unexpected message head");
    }
    final ProtocolVersion transportVersion = response.getVersion();
    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
        throw new UnsupportedHttpVersionException(transportVersion);
    }
    final int status = response.getCode();
    if (status < HttpStatus.SC_INFORMATIONAL) {
        throw new ProtocolException("Invalid response: " + new StatusLine(response));
    }
    if (status > HttpStatus.SC_CONTINUE && status < HttpStatus.SC_SUCCESS) {
        exchangeHandler.consumeInformation(response, context);
    } else {
        if (!connectionReuseStrategy.keepAlive(committedRequest, response, context)) {
            keepAlive = false;
        }
    }
    if (requestState == MessageState.ACK) {
        if (status == HttpStatus.SC_CONTINUE || status >= HttpStatus.SC_SUCCESS) {
            outputChannel.setSocketTimeout(timeout);
            requestState = MessageState.BODY;
            if (status < HttpStatus.SC_CLIENT_ERROR) {
                exchangeHandler.produce(internalDataChannel);
            }
        }
    }
    if (status < HttpStatus.SC_SUCCESS) {
        return;
    }
    if (requestState == MessageState.BODY) {
        if (status >= HttpStatus.SC_CLIENT_ERROR) {
            requestState = MessageState.COMPLETE;
            if (!outputChannel.abortGracefully()) {
                keepAlive = false;
            }
        }
    }
    context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
    context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
    httpProcessor.process(response, entityDetails, context);
    if (entityDetails == null && !keepAlive) {
        outputChannel.close();
    }
    exchangeHandler.consumeResponse(response, entityDetails, context);
    if (entityDetails == null) {
        responseState = MessageState.COMPLETE;
    } else {
        responseState = MessageState.BODY;
    }
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) ProtocolException(org.apache.hc.core5.http.ProtocolException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 2 with UnsupportedHttpVersionException

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

the class TestHttpService method testUnsupportedHttpVersionException.

@Test
public void testUnsupportedHttpVersionException() throws Exception {
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
    Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
    Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
    Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
    Mockito.doThrow(new UnsupportedHttpVersionException()).when(requestHandler).handle(request, response, context);
    httpservice.handleRequest(conn, context);
    final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
    Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
    final ClassicHttpResponse error = responseCaptor.getValue();
    Assertions.assertNotNull(error);
    Assertions.assertSame(request, context.getRequest());
    Assertions.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getCode());
    Mockito.verify(httprocessor).process(error, error.getEntity(), context);
    Mockito.verify(conn).sendResponseHeader(error);
    Mockito.verify(conn).sendResponseEntity(error);
    Mockito.verify(conn).close();
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) Test(org.junit.jupiter.api.Test)

Example 3 with UnsupportedHttpVersionException

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

the class DefaultBHttpClientConnection method receiveResponseHeader.

@Override
public ClassicHttpResponse receiveResponseHeader() throws HttpException, IOException {
    final SocketHolder socketHolder = ensureOpen();
    final ClassicHttpResponse response = this.responseParser.parse(this.inBuffer, socketHolder.getInputStream());
    if (response == null) {
        throw new NoHttpResponseException("The target server failed to respond");
    }
    final ProtocolVersion transportVersion = response.getVersion();
    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
        throw new UnsupportedHttpVersionException(transportVersion);
    }
    this.version = transportVersion;
    onResponseReceived(response);
    final int status = response.getCode();
    if (status < HttpStatus.SC_INFORMATIONAL) {
        throw new ProtocolException("Invalid response: " + status);
    }
    if (response.getCode() >= HttpStatus.SC_SUCCESS) {
        incrementResponseCount();
    }
    return response;
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) NoHttpResponseException(org.apache.hc.core5.http.NoHttpResponseException) ProtocolException(org.apache.hc.core5.http.ProtocolException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 4 with UnsupportedHttpVersionException

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

the class DefaultBHttpServerConnection method receiveRequestHeader.

@Override
public ClassicHttpRequest receiveRequestHeader() throws HttpException, IOException {
    final SocketHolder socketHolder = ensureOpen();
    final ClassicHttpRequest request = this.requestParser.parse(this.inBuffer, socketHolder.getInputStream());
    if (request == null) {
        return null;
    }
    final ProtocolVersion transportVersion = request.getVersion();
    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
        throw new UnsupportedHttpVersionException(transportVersion);
    }
    request.setScheme(this.scheme);
    this.version = transportVersion;
    onRequestReceived(request);
    incrementRequestCount();
    return request;
}
Also used : ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 5 with UnsupportedHttpVersionException

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

UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)9 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)8 HttpException (org.apache.hc.core5.http.HttpException)4 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)3 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Header (org.apache.hc.core5.http.Header)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 MisdirectedRequestException (org.apache.hc.core5.http.MisdirectedRequestException)1 NoHttpResponseException (org.apache.hc.core5.http.NoHttpResponseException)1 HttpServerRequestHandler (org.apache.hc.core5.http.io.HttpServerRequestHandler)1 BasicHttpServerRequestHandler (org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler)1 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)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