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());
}
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());
}
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());
}
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();
}
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();
}
}
Aggregations