use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.
the class ServerH2StreamHandler method handle.
@Override
public void handle(final HttpException ex, final boolean endStream) throws HttpException, IOException {
if (done.get()) {
throw ex;
}
switch(requestState) {
case HEADERS:
requestState = endStream ? MessageState.COMPLETE : MessageState.BODY;
if (!responseCommitted.get()) {
final AsyncResponseProducer responseProducer = new BasicResponseProducer(ServerSupport.toStatusCode(ex), ServerSupport.toErrorMessage(ex));
exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
exchangeHandler.handleRequest(null, null, responseChannel, context);
} else {
throw ex;
}
break;
case BODY:
responseState = MessageState.COMPLETE;
default:
throw ex;
}
}
use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.
the class Http1IntegrationTest method testPostIdentityTransferOutOfSequenceResponse.
@Test
public void testPostIdentityTransferOutOfSequenceResponse() throws Exception {
server.register("/hello", () -> new ImmediateResponseExchangeHandler(500, "Go away"));
final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
client.start();
final int reqNo = 5;
for (int i = 0; i < reqNo; i++) {
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/hello"), new MultiLineEntityProducer("Hello", 16 * i)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
streamEndpoint.close();
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
final String entity = result.getBody();
Assertions.assertNotNull(response);
Assertions.assertEquals(500, response.getCode());
Assertions.assertEquals("Go away", entity);
}
}
use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler in project httpcomponents-core by apache.
the class ServerHttp1StreamHandler method terminateExchange.
void terminateExchange(final HttpException ex) throws HttpException, IOException {
if (done.get() || requestState != MessageState.HEADERS) {
throw new ProtocolException("Unexpected message head");
}
receivedRequest = null;
requestState = MessageState.COMPLETE;
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(null, null, responseChannel, context);
}
use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler 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");
}
}
use of org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler 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