use of org.apache.hc.core5.http.nio.AsyncResponseProducer 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;
}
}
}
Aggregations