Search in sources :

Example 1 with MisdirectedRequestException

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

the class RequestHandlerRegistry method resolve.

@Override
public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException {
    final URIAuthority authority = request.getAuthority();
    final String key = authority != null ? TextUtils.toLowerCase(authority.getHostName()) : null;
    final LookupRegistry<T> patternMatcher = getPatternMatcher(key);
    if (patternMatcher == null) {
        throw new MisdirectedRequestException("Not authoritative");
    }
    String path = request.getPath();
    final int i = path.indexOf('?');
    if (i != -1) {
        path = path.substring(0, i);
    }
    return patternMatcher.lookup(path);
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) MisdirectedRequestException(org.apache.hc.core5.http.MisdirectedRequestException)

Example 2 with MisdirectedRequestException

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

MisdirectedRequestException (org.apache.hc.core5.http.MisdirectedRequestException)2 HttpException (org.apache.hc.core5.http.HttpException)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)1 UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1 AsyncResponseProducer (org.apache.hc.core5.http.nio.AsyncResponseProducer)1 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)1 BasicResponseProducer (org.apache.hc.core5.http.nio.support.BasicResponseProducer)1 ImmediateResponseExchangeHandler (org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler)1 URIAuthority (org.apache.hc.core5.net.URIAuthority)1