use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class SimpleSSLTestCase method simpleSSLTestCase.
@Test
public void simpleSSLTestCase() throws IOException, GeneralSecurityException {
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(HttpString.tryFromString("scheme"), exchange.getRequestScheme());
exchange.endExchange();
}
});
DefaultServer.startSSLServer();
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] header = result.getHeaders("scheme");
Assert.assertEquals("https", header[0].getValue());
} finally {
client.getConnectionManager().shutdown();
DefaultServer.stopSSLServer();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class HTTP2ViaUpgradeTestCase 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).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.HttpHandler in project undertow by undertow-io.
the class AuthenticationTestBase method setAuthenticationChain.
@Before
public void setAuthenticationChain() {
List<AuthenticationMechanism> testMechanisms = getTestMechanisms();
if (testMechanisms == null) {
return;
}
HttpHandler current = new ResponseHandler();
current = new AuthenticationCallHandler(current);
current = new AuthenticationConstraintHandler(current);
current = new AuthenticationMechanismsHandler(current, testMechanisms);
// Ensure empty on initialisation.
auditReceiver.takeNotifications();
current = new NotificationReceiverHandler(current, Collections.<NotificationReceiver>singleton(auditReceiver));
if (cachingRequired()) {
current = new CachedAuthenticatedSessionHandler(current);
}
current = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, current);
setRootHandler(current);
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class URLRewritingSessionTestCase method setup.
@BeforeClass
public static void setup() {
final PathParameterSessionConfig sessionConfig = new PathParameterSessionConfig();
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);
} else {
Assert.assertEquals("/notamatchingpath;jsessionid=" + session.getId(), exchange.getRequestURI());
}
Integer count = (Integer) session.getAttribute(COUNT);
exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
session.setAttribute(COUNT, ++count);
for (Map.Entry<String, Deque<String>> qp : exchange.getQueryParameters().entrySet()) {
exchange.getResponseHeaders().add(new HttpString(qp.getKey()), qp.getValue().getFirst());
}
if (exchange.getQueryString().isEmpty()) {
exchange.getResponseSender().send(sessionConfig.rewriteUrl(DefaultServer.getDefaultServerURL() + "/notamatchingpath", session.getId()));
} else {
exchange.getResponseSender().send(sessionConfig.rewriteUrl(DefaultServer.getDefaultServerURL() + "/notamatchingpath?" + exchange.getQueryString(), session.getId()));
}
}
});
DefaultServer.setRootHandler(handler);
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class NameVirtualHostHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final String hostHeader = exchange.getRequestHeaders().getFirst(Headers.HOST);
if (hostHeader != null) {
String host;
if (hostHeader.contains(":")) {
//header can be in host:port format
host = hostHeader.substring(0, hostHeader.lastIndexOf(":"));
} else {
host = hostHeader;
}
//most hosts will be lowercase, so we do the host
HttpHandler handler = hosts.get(host);
if (handler != null) {
handler.handleRequest(exchange);
return;
}
//do a cache insensitive match
handler = hosts.get(host.toLowerCase(Locale.ENGLISH));
if (handler != null) {
handler.handleRequest(exchange);
return;
}
}
defaultHandler.handleRequest(exchange);
}
Aggregations