use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class FileErrorPageHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.addDefaultResponseListener(new DefaultResponseListener() {
@Override
public boolean handleDefaultResponse(final HttpServerExchange exchange) {
Set<Integer> codes = responseCodes;
if (!exchange.isResponseStarted() && codes.contains(exchange.getStatusCode())) {
serveFile(exchange);
return true;
}
return false;
}
});
next.handleRequest(exchange);
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class ProxyHandler method handleRequest.
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final ProxyClient.ProxyTarget target = proxyClient.findTarget(exchange);
if (target == null) {
log.debugf("No proxy target for request to %s", exchange.getRequestURL());
next.handleRequest(exchange);
return;
}
if (exchange.isResponseStarted()) {
// we can't proxy a request that has already started, this is basically a server configuration error
UndertowLogger.REQUEST_LOGGER.cannotProxyStartedRequest(exchange);
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
return;
}
final long timeout = maxRequestTime > 0 ? System.currentTimeMillis() + maxRequestTime : 0;
int maxRetries = maxConnectionRetries;
if (target instanceof ProxyClient.MaxRetriesProxyTarget) {
maxRetries = Math.max(maxRetries, ((ProxyClient.MaxRetriesProxyTarget) target).getMaxRetries());
}
final ProxyClientHandler clientHandler = new ProxyClientHandler(exchange, target, timeout, maxRetries, idempotentRequestPredicate);
if (timeout > 0) {
final XnioExecutor.Key key = WorkerUtils.executeAfter(exchange.getIoThread(), new Runnable() {
@Override
public void run() {
clientHandler.cancel(exchange);
}
}, maxRequestTime, TimeUnit.MILLISECONDS);
exchange.putAttachment(TIMEOUT_KEY, key);
exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
key.remove();
nextListener.proceed();
}
});
}
exchange.dispatch(exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : exchange.getIoThread(), clientHandler);
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class Context method handleRequest.
/**
* Handle a proxy request for this context.
*
* @param target the proxy target
* @param exchange the http server exchange
* @param callback the proxy callback
* @param timeout the timeout
* @param timeUnit the time unit
* @param exclusive whether this connection is exclusive
*/
void handleRequest(final ModClusterProxyTarget target, final HttpServerExchange exchange, final ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit, boolean exclusive) {
if (addRequest()) {
exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
requestDone();
nextListener.proceed();
}
});
node.getConnectionPool().connect(target, exchange, callback, timeout, timeUnit, exclusive);
} else {
callback.failed(exchange);
}
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class H2CUpgradeResetTestCase method beforeClass.
/**
* Initializes the server with the H2C handler and adds the echo handler to
* manage the requests.
* @throws IOException Some error
*/
@BeforeClass
public static void beforeClass() throws IOException {
final PathHandler path = new PathHandler().addExactPath(ECHO_PATH, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
sendEchoResponse(exchange);
}
});
server = Undertow.builder().addHttpListener(DefaultServer.getHostPort() + 1, DefaultServer.getHostAddress(), new Http2UpgradeHandler(path)).setSocketOption(Options.REUSE_ADDRESSES, true).build();
server.start();
// Create xnio worker
final Xnio xnio = Xnio.getInstance();
final XnioWorker xnioWorker = xnio.createWorker(null, OptionMap.builder().set(Options.WORKER_IO_THREADS, 8).set(Options.TCP_NODELAY, true).set(Options.KEEP_ALIVE, true).getMap());
worker = xnioWorker;
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class HttpClientTestCase method beforeClass.
@BeforeClass
public static void beforeClass() throws IOException {
// Create xnio worker
final Xnio xnio = Xnio.getInstance();
final XnioWorker xnioWorker = xnio.createWorker(null, DEFAULT_OPTIONS);
worker = xnioWorker;
DefaultServer.setRootHandler(new PathHandler().addExactPath(MESSAGE, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
sendMessage(exchange);
}
}).addExactPath(READTIMEOUT, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.setStatusCode(StatusCodes.OK);
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, 5 + "");
StreamSinkChannel responseChannel = exchange.getResponseChannel();
responseChannel.write(ByteBuffer.wrap(new byte[] { 'a', 'b', 'c' }));
responseChannel.flush();
try {
// READ_TIMEOUT set as 600ms on the client side
// On the server side intentionally sleep 2000ms
// to make READ_TIMEOUT happening at client side
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
responseChannel.write(ByteBuffer.wrap(new byte[] { 'd', 'e' }));
responseChannel.close();
}
}).addExactPath(POST, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() {
@Override
public void handle(HttpServerExchange exchange, String message) {
exchange.getResponseSender().send(message);
}
});
}
}));
}
Aggregations