Search in sources :

Example 21 with Undertow

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) {
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) RoutingHandler(io.undertow.server.RoutingHandler) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Undertow(io.undertow.Undertow) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 22 with Undertow

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) {
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Undertow(io.undertow.Undertow) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 23 with Undertow

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();
}
Also used : PortInUseException(org.springframework.boot.web.server.PortInUseException) Undertow(io.undertow.Undertow)

Example 24 with Undertow

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();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Undertow(io.undertow.Undertow)

Example 25 with Undertow

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;
}
Also used : HttpHandler(io.undertow.server.HttpHandler) HashMap(java.util.HashMap) Resource(io.undertow.server.handlers.resource.Resource) PathHandler(io.undertow.server.handlers.PathHandler) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) Date(java.util.Date) HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) Undertow(io.undertow.Undertow)

Aggregations

Undertow (io.undertow.Undertow)55 HttpHandler (io.undertow.server.HttpHandler)27 HttpServerExchange (io.undertow.server.HttpServerExchange)22 PathHandler (io.undertow.server.handlers.PathHandler)16 IOException (java.io.IOException)13 Test (org.junit.Test)11 ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)10 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)10 DeploymentManager (io.undertow.servlet.api.DeploymentManager)9 URI (java.net.URI)9 TestHttpClient (io.undertow.testutils.TestHttpClient)6 ServletException (javax.servlet.ServletException)6 HttpGet (org.apache.http.client.methods.HttpGet)6 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 XnioWorker (org.xnio.XnioWorker)5 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)4 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)4 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)4 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)4