Search in sources :

Example 91 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class SimpleParserTestCase method testEncodedSlashAllowed.

@Test
public void testEncodedSlashAllowed() throws BadRequestException {
    byte[] in = "GET /somepath%2fotherPath HTTP/1.1\r\n\r\n".getBytes();
    final ParseState context = new ParseState(10);
    HttpServerExchange result = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.create(UndertowOptions.ALLOW_ENCODED_SLASH, true)).handle(ByteBuffer.wrap(in), context, result);
    Assert.assertSame(Methods.GET, result.getRequestMethod());
    Assert.assertEquals("/somepath/otherPath", result.getRequestPath());
    Assert.assertEquals("/somepath%2fotherPath", result.getRequestURI());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 92 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class SimpleParserTestCase method testColonSlashInFullURL.

@Test
public void testColonSlashInFullURL() throws BadRequestException {
    byte[] in = "GET http://foo.com/a/http://myurl.com/b/c HTTP/1.1\r\n\r\n".getBytes();
    final ParseState context = new ParseState(10);
    HttpServerExchange result = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.create(UndertowOptions.ALLOW_ENCODED_SLASH, true)).handle(ByteBuffer.wrap(in), context, result);
    Assert.assertSame(Methods.GET, result.getRequestMethod());
    Assert.assertEquals("/a/http://myurl.com/b/c", result.getRequestPath());
    Assert.assertEquals("http://foo.com/a/http://myurl.com/b/c", result.getRequestURI());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 93 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class SimpleParserTestCase method testMultiLevelMatrixParameters.

@Test
public void testMultiLevelMatrixParameters() throws BadRequestException {
    byte[] in = "GET /some;p1=v1/path;p2=v2?q1=v3 HTTP/1.1\r\n\r\n".getBytes();
    ParseState context = new ParseState(10);
    HttpServerExchange result = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.create(UndertowOptions.ALLOW_ENCODED_SLASH, true)).handle(ByteBuffer.wrap(in), context, result);
    Assert.assertSame(Methods.GET, result.getRequestMethod());
    Assert.assertEquals("/some;p1=v1/path;p2=v2", result.getRequestURI());
    Assert.assertEquals("/some/path", result.getRequestPath());
    Assert.assertEquals("q1=v3", result.getQueryString());
    Assert.assertEquals("v1", result.getPathParameters().get("p1").getFirst());
    Assert.assertEquals("v2", result.getPathParameters().get("p2").getFirst());
    Assert.assertEquals("v3", result.getQueryParameters().get("q1").getFirst());
    Assert.assertSame(Protocols.HTTP_1_1, result.getProtocol());
    Assert.assertFalse(result.isHostIncludedInRequestURI());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 94 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class HTTP2ViaUpgradeWithUnEncodedURLCharactersTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    int port = DefaultServer.getHostPort("default");
    server = Undertow.builder().addHttpListener(port + 1, DefaultServer.getHostAddress("default")).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setServerOption(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, true).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(Handlers.header(new Http2UpgradeHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            exchange.getResponseSender().send(message);
        }
    }, "h2c", "h2c-17"), Headers.SEC_WEB_SOCKET_ACCEPT_STRING, // work around Netty bug, it assumes that every upgrade request that does not have this header is an old style websocket upgrade
    "fake")).build();
    server.start();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) URISyntaxException(java.net.URISyntaxException) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 95 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class InMemorySessionTestCase method inMemorySessionTest.

@Test
public void inMemorySessionTest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        final SessionCookieConfig sessionConfig = new SessionCookieConfig();
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager(""), sessionConfig);
        handler.setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
                Session session = manager.getSession(exchange, sessionConfig);
                if (session == null) {
                    session = manager.createSession(exchange, sessionConfig);
                    session.setAttribute(COUNT, 0);
                }
                Integer count = (Integer) session.getAttribute(COUNT);
                exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
                session.setAttribute(COUNT, ++count);
            }
        });
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) SessionManager(io.undertow.server.session.SessionManager) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)277 HttpHandler (io.undertow.server.HttpHandler)127 Test (org.junit.Test)109 IOException (java.io.IOException)90 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)44 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 SessionConfig (io.undertow.server.session.SessionConfig)16 Map (java.util.Map)16 Sender (io.undertow.io.Sender)15 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 URI (java.net.URI)13 HeaderValues (io.undertow.util.HeaderValues)12