use of org.apache.hc.core5.http.nio.ResponseChannel in project httpcomponents-core by apache.
the class ReactiveFullDuplexServerExample method main.
public static void main(final String[] args) throws Exception {
int port = 8080;
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
final HttpAsyncServer server = AsyncServerBootstrap.bootstrap().setIOReactorConfig(config).setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
System.out.println(connection.getRemoteAddress() + " " + new RequestLine(request));
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
System.out.println(connection.getRemoteAddress() + " " + new StatusLine(response));
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
if (keepAlive) {
System.out.println(connection.getRemoteAddress() + " exchange completed (connection kept alive)");
} else {
System.out.println(connection.getRemoteAddress() + " exchange completed (connection closed)");
}
}
}).register("/echo", () -> new ReactiveServerExchangeHandler((request, entityDetails, responseChannel, context, requestBody, responseBodyFuture) -> {
if (new BasicHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE).equals(request.getHeader(HttpHeaders.EXPECT))) {
responseChannel.sendInformation(new BasicHttpResponse(100), context);
}
responseChannel.sendResponse(new BasicHttpResponse(200), new BasicEntityDetails(-1, ContentType.APPLICATION_OCTET_STREAM), context);
// Simply using the request publisher as the response publisher will
// cause the server to echo the request body.
responseBodyFuture.execute(requestBody);
})).create();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("HTTP server shutting down");
server.close(CloseMode.GRACEFUL);
}));
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port), URIScheme.HTTP);
final ListenerEndpoint listenerEndpoint = future.get();
System.out.print("Listening on " + listenerEndpoint.getAddress());
server.awaitShutdown(TimeValue.ofDays(Long.MAX_VALUE));
}
use of org.apache.hc.core5.http.nio.ResponseChannel in project httpcomponents-core by apache.
the class ServerPushH2StreamHandler method produceOutput.
@Override
public void produceOutput() throws HttpException, IOException {
switch(responseState) {
case IDLE:
responseState = MessageState.HEADERS;
pushProducer.produceResponse(new ResponseChannel() {
@Override
public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
commitInformation(response);
}
@Override
public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
commitResponse(response, entityDetails);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
commitPromise(promise, pushProducer);
}
}, context);
break;
case BODY:
pushProducer.produce(dataChannel);
break;
}
}
use of org.apache.hc.core5.http.nio.ResponseChannel 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.ResponseChannel 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