use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ProxyProtocolTest method test_PROXY_GET_v1.
@Test
public void test_PROXY_GET_v1() throws Exception {
startServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
Assert.assertEquals("1.2.3.4", request.getRemoteAddr());
Assert.assertEquals(1111, request.getRemotePort());
Assert.assertEquals("5.6.7.8", request.getLocalAddr());
Assert.assertEquals(2222, request.getLocalPort());
} catch (Throwable th) {
th.printStackTrace();
response.setStatus(500);
}
baseRequest.setHandled(true);
}
});
String request1 = "PROXY TCP4 1.2.3.4 5.6.7.8 1111 2222\r\n";
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
channel.write(ByteBuffer.wrap(request1.getBytes(StandardCharsets.UTF_8)));
FuturePromise<Session> promise = new FuturePromise<>();
client.accept(null, channel, new Session.Listener.Adapter(), promise);
Session session = promise.get(5, TimeUnit.SECONDS);
HttpFields fields = new HttpFields();
String uri = "http://localhost:" + connector.getLocalPort() + "/";
MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
HeadersFrame frame = new HeadersFrame(metaData, null, true);
CountDownLatch latch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ConnectionOpenCloseTest method testOpenRequestClose.
@Slow
@Test
public void testOpenRequestClose() throws Exception {
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
}
});
server.start();
final AtomicInteger callbacks = new AtomicInteger();
final CountDownLatch openLatch = new CountDownLatch(1);
final CountDownLatch closeLatch = new CountDownLatch(1);
connector.addBean(new Connection.Listener.Adapter() {
@Override
public void onOpened(Connection connection) {
callbacks.incrementAndGet();
openLatch.countDown();
}
@Override
public void onClosed(Connection connection) {
callbacks.incrementAndGet();
closeLatch.countDown();
}
});
try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
socket.setSoTimeout((int) connector.getIdleTimeout());
OutputStream output = socket.getOutputStream();
output.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + connector.getLocalPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
output.flush();
InputStream inputStream = socket.getInputStream();
HttpTester.Response response = HttpTester.parseResponse(inputStream);
assertThat("Status Code", response.getStatus(), is(200));
Assert.assertEquals(-1, inputStream.read());
socket.close();
Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
// Wait some time to see if the callbacks are called too many times
TimeUnit.SECONDS.sleep(1);
Assert.assertEquals(2, callbacks.get());
}
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpConnectionTest method testEmptyFlush.
@Test
public void testEmptyFlush() throws Exception {
server.stop();
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setStatus(200);
OutputStream out = response.getOutputStream();
out.flush();
out.flush();
}
});
server.start();
String response = connector.getResponse("GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n");
assertThat(response, Matchers.containsString("200 OK"));
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HostHeaderCustomizerTest method testHostHeaderCustomizer.
@Test
public void testHostHeaderCustomizer() throws Exception {
Server server = new Server();
HttpConfiguration httpConfig = new HttpConfiguration();
final String serverName = "test_server_name";
final int serverPort = 13;
final String redirectPath = "/redirect";
httpConfig.addCustomizer(new HostHeaderCustomizer(serverName, serverPort));
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(serverName, request.getServerName());
Assert.assertEquals(serverPort, request.getServerPort());
response.sendRedirect(redirectPath);
}
});
server.start();
try {
try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
try (OutputStream output = socket.getOutputStream()) {
String request = "" + "GET / HTTP/1.0\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
HttpTester.Input input = HttpTester.from(socket.getInputStream());
HttpTester.Response response = HttpTester.parseResponse(input);
String location = response.get("location");
Assert.assertNotNull(location);
String schemePrefix = "http://";
Assert.assertTrue(location.startsWith(schemePrefix));
Assert.assertTrue(location.endsWith(redirectPath));
String hostPort = location.substring(schemePrefix.length(), location.length() - redirectPath.length());
Assert.assertEquals(serverName + ":" + serverPort, hostPort);
}
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ValidatingConnectionPoolTest method testServerClosesConnectionAfterRedirectWithoutConnectionCloseHeader.
@Test
public void testServerClosesConnectionAfterRedirectWithoutConnectionCloseHeader() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
if (target.endsWith("/redirect")) {
response.setStatus(HttpStatus.TEMPORARY_REDIRECT_307);
response.setContentLength(0);
response.setHeader(HttpHeader.LOCATION.asString(), scheme + "://localhost:" + connector.getLocalPort() + "/");
response.flushBuffer();
baseRequest.getHttpChannel().getEndPoint().shutdownOutput();
} else {
response.setStatus(HttpStatus.OK_200);
response.setContentLength(0);
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
}
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/redirect").send();
Assert.assertEquals(200, response.getStatus());
}
Aggregations