use of io.undertow.Undertow in project undertow by undertow-io.
the class URLDecodingHandlerTestCase method testDecodeCharactersInMatchedPaths.
@Test
public void testDecodeCharactersInMatchedPaths() throws Exception {
// When UndertowOptions.DECODE_URL is disabled, the URLDecodingHandler should decode values.
Undertow undertow = Undertow.builder().setServerOption(UndertowOptions.DECODE_URL, false).addHttpListener(PORT, "0.0.0.0").setHandler(new RoutingHandler().get("/api/{pathParam}/tail", new URLDecodingHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
String matched = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY).getParameters().get("pathParam");
exchange.getResponseSender().send(matched);
}
}, "UTF-8"))).build();
undertow.start();
try {
TestHttpClient client = new TestHttpClient();
// '%253E' decodes to '%3E', which would decode to '>' if decoded twice
try (CloseableHttpResponse response = client.execute(new HttpGet("http://localhost:" + PORT + "/api/test%2Ftest+test%2Btest%20test/tail"))) {
Assert.assertEquals("test/test+test+test test", getResponseString(response));
}
} finally {
undertow.stop();
// sleep 1 s to prevent BindException (Address already in use) when restarting the server
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
}
}
use of io.undertow.Undertow in project undertow by undertow-io.
the class URLDecodingHandlerTestCase method testDoesNotDecodeByDefault.
@Test
public void testDoesNotDecodeByDefault() throws Exception {
// By default Undertow decodes upon accepting requests, see UndertowOptions.DECODE_URL.
// If this is enabled, the URLDecodingHandler should no-op
Undertow undertow = Undertow.builder().addHttpListener(PORT, "0.0.0.0").setHandler(new URLDecodingHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send(exchange.getRelativePath());
}
}, "UTF-8")).build();
undertow.start();
try {
TestHttpClient client = new TestHttpClient();
// '%253E' decodes to '%3E', which would decode to '>' if decoded twice
try (CloseableHttpResponse response = client.execute(new HttpGet("http://localhost:" + PORT + "/%253E"))) {
Assert.assertEquals("/%3E", getResponseString(response));
}
} finally {
undertow.stop();
// sleep 1 s to prevent BindException (Address already in use) when restarting the server
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
}
}
use of io.undertow.Undertow in project spring-boot by spring-projects.
the class UndertowServletWebServerFactoryTests method handleExceptionCausedByBlockedPortOnPrimaryConnector.
@Override
protected void handleExceptionCausedByBlockedPortOnPrimaryConnector(RuntimeException ex, int blockedPort) {
assertThat(ex).isInstanceOf(PortInUseException.class);
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
Undertow undertow = (Undertow) ReflectionTestUtils.getField(this.webServer, "undertow");
assertThat(undertow.getWorker()).isNull();
}
use of io.undertow.Undertow in project baseio by generallycloud.
the class TestUndertow method main.
public static void main(final String[] args) {
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(new // 设置HttpHandler回调方法
HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Hello World");
}
}).build();
server.start();
}
use of io.undertow.Undertow in project kontraktor by RuedigerMoeller.
the class Http4K method publishResourcePath.
public Http4K publishResourcePath(String hostName, String urlPath, int port, DynamicResourceManager man, boolean compress, Function<HttpServerExchange, Boolean> interceptor) {
Pair<PathHandler, Undertow> server = getServer(port, hostName);
ResourceHandler handler = new ResourceHandler(man);
if (compress) {
HttpHandler compressHandler = new EncodingHandler.Builder().build(new HashMap<>()).wrap(handler);
HttpHandler httpHandler = new HttpHandler() {
volatile byte[] zippedAggregate;
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
String requestPath = exchange.getRequestPath();
if (exchange.getRequestMethod() == Methods.GET && !man.isDevMode() && (requestPath.equals("/") || requestPath.equals("") || requestPath.equals("/index.html"))) {
HeaderMap requestHeaders = exchange.getRequestHeaders();
String lastMod = requestHeaders.get(Headers.IF_MODIFIED_SINCE, 0);
if (lastMod != null) {
Date date = DateUtils.parseDate(lastMod);
if (date != null && date.getTime() >= man.getLastModified() - 2_000) {
exchange.setResponseCode(304);
exchange.endExchange();
return;
}
}
Resource cacheEntry = man.getCacheEntry("index.html");
if (cacheEntry instanceof DynamicResourceManager.MyResource) {
if (zippedAggregate == null) {
zippedAggregate = gzip(((DynamicResourceManager.MyResource) cacheEntry).getBytes());
}
exchange.setResponseCode(200);
exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, "gzip");
exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, DateUtils.toDateString(man.getLastModifiedDate()));
Sender responseSender = exchange.getResponseSender();
responseSender.send(ByteBuffer.wrap(zippedAggregate));
} else {
zippedAggregate = null;
compressHandler.handleRequest(exchange);
}
} else {
handler.handleRequest(exchange);
}
}
};
if (interceptor != null) {
server.car().addPrefixPath(urlPath, httpExchange -> {
boolean apply = interceptor.apply(httpExchange);
if (!apply) {
httpHandler.handleRequest(httpExchange);
}
});
} else {
server.car().addPrefixPath(urlPath, httpHandler);
}
} else {
if (interceptor != null) {
server.car().addPrefixPath(urlPath, httpExchange -> {
boolean apply = interceptor.apply(httpExchange);
if (!apply) {
handler.handleRequest(httpExchange);
}
});
} else {
server.car().addPrefixPath(urlPath, handler);
}
}
return this;
}
Aggregations