Search in sources :

Example 26 with ProtocolException

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

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

the class H2RequestContent method process.

@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
    Args.notNull(context, "HTTP context");
    final ProtocolVersion ver = context.getProtocolVersion();
    if (ver.getMajor() < 2) {
        super.process(request, entity, context);
    } else if (entity != null) {
        final String method = request.getMethod();
        if (Method.TRACE.isSame(method)) {
            throw new ProtocolException("TRACE request may not enclose an entity");
        }
        MessageSupport.addContentTypeHeader(request, entity);
        MessageSupport.addContentEncodingHeader(request, entity);
        MessageSupport.addTrailerHeader(request, entity);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 28 with ProtocolException

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

Example 29 with ProtocolException

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

the class HttpRequestExecutor method execute.

/**
 * Sends the request and obtain a response.
 *
 * @param request   the request to execute.
 * @param conn      the connection over which to execute the request.
 * @param informationCallback   callback to execute upon receipt of information status (1xx).
 *                              May be null.
 * @param context the context
 * @return  the response to the request.
 *
 * @throws IOException in case of an I/O error.
 * @throws HttpException in case of HTTP protocol violation or a processing
 *   problem.
 */
public ClassicHttpResponse execute(final ClassicHttpRequest request, final HttpClientConnection conn, final HttpResponseInformationCallback informationCallback, final HttpContext context) throws IOException, HttpException {
    Args.notNull(request, "HTTP request");
    Args.notNull(conn, "Client connection");
    Args.notNull(context, "HTTP context");
    try {
        context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
        conn.sendRequestHeader(request);
        if (streamListener != null) {
            streamListener.onRequestHead(conn, request);
        }
        boolean expectContinue = false;
        final HttpEntity entity = request.getEntity();
        if (entity != null) {
            final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
            expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
            if (!expectContinue) {
                conn.sendRequestEntity(request);
            }
        }
        conn.flush();
        ClassicHttpResponse response = null;
        while (response == null) {
            if (expectContinue) {
                if (conn.isDataAvailable(this.waitForContinue)) {
                    response = conn.receiveResponseHeader();
                    if (streamListener != null) {
                        streamListener.onResponseHead(conn, response);
                    }
                    final int status = response.getCode();
                    if (status == HttpStatus.SC_CONTINUE) {
                        // discard 100-continue
                        response = null;
                        conn.sendRequestEntity(request);
                    } else if (status < HttpStatus.SC_SUCCESS) {
                        if (informationCallback != null) {
                            informationCallback.execute(response, conn, context);
                        }
                        response = null;
                        continue;
                    } else if (status >= HttpStatus.SC_CLIENT_ERROR) {
                        conn.terminateRequest(request);
                    } else {
                        conn.sendRequestEntity(request);
                    }
                } else {
                    conn.sendRequestEntity(request);
                }
                conn.flush();
                expectContinue = false;
            } else {
                response = conn.receiveResponseHeader();
                if (streamListener != null) {
                    streamListener.onResponseHead(conn, response);
                }
                final int status = response.getCode();
                if (status < HttpStatus.SC_INFORMATIONAL) {
                    throw new ProtocolException("Invalid response: " + new StatusLine(response));
                }
                if (status < HttpStatus.SC_SUCCESS) {
                    if (informationCallback != null && status != HttpStatus.SC_CONTINUE) {
                        informationCallback.execute(response, conn, context);
                    }
                    response = null;
                }
            }
        }
        if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) {
            conn.receiveResponseEntity(response);
        }
        return response;
    } catch (final HttpException | IOException | RuntimeException ex) {
        Closer.closeQuietly(conn);
        throw ex;
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) StatusLine(org.apache.hc.core5.http.message.StatusLine) ProtocolException(org.apache.hc.core5.http.ProtocolException) HttpEntity(org.apache.hc.core5.http.HttpEntity) Header(org.apache.hc.core5.http.Header) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException)

Aggregations

ProtocolException (org.apache.hc.core5.http.ProtocolException)28 HttpResponse (org.apache.hc.core5.http.HttpResponse)10 Header (org.apache.hc.core5.http.Header)9 EntityDetails (org.apache.hc.core5.http.EntityDetails)8 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)8 URISyntaxException (java.net.URISyntaxException)6 HttpException (org.apache.hc.core5.http.HttpException)6 HttpRequest (org.apache.hc.core5.http.HttpRequest)6 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)6 IOException (java.io.IOException)5 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)5 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)5 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 URIAuthority (org.apache.hc.core5.net.URIAuthority)4 InetSocketAddress (java.net.InetSocketAddress)3 URI (java.net.URI)3 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)3 Message (org.apache.hc.core5.http.Message)3