use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ProxyProtocolTest method test_PROXY_GET_v2.
@Test
public void test_PROXY_GET_v2() throws Exception {
startServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
Assert.assertEquals("10.0.0.4", request.getRemoteAddr());
Assert.assertEquals(33824, request.getRemotePort());
Assert.assertEquals("10.0.0.4", request.getLocalAddr());
Assert.assertEquals(8888, request.getLocalPort());
} catch (Throwable th) {
th.printStackTrace();
response.setStatus(500);
}
baseRequest.setHandled(true);
}
});
String request1 = "0D0A0D0A000D0A515549540A211100140A0000040A000004842022B82000050000000000";
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
channel.write(ByteBuffer.wrap(TypeUtil.fromHexString(request1)));
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 AbstractHandlerMBean method getObjectContextBasis.
/* ------------------------------------------------------------ */
@Override
public String getObjectContextBasis() {
if (_managed != null) {
String basis = null;
if (_managed instanceof ContextHandler) {
ContextHandler handler = (ContextHandler) _managed;
String context = getContextName(handler);
if (context == null)
context = handler.getDisplayName();
if (context != null)
return context;
} else if (_managed instanceof AbstractHandler) {
AbstractHandler handler = (AbstractHandler) _managed;
Server server = handler.getServer();
if (server != null) {
ContextHandler context = AbstractHandlerContainer.findContainerOf(server, ContextHandler.class, handler);
if (context != null)
basis = getContextName(context);
}
}
if (basis != null)
return basis;
}
return super.getObjectContextBasis();
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ConnectionOpenCloseTest method testOpenClose.
@Slow
@Test
public void testOpenClose() throws Exception {
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throw new IllegalStateException();
}
});
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());
Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
socket.shutdownOutput();
Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
String response = IO.toString(socket.getInputStream());
Assert.assertEquals(0, response.length());
// Wait some time to see if the callbacks are called too many times
TimeUnit.MILLISECONDS.sleep(200);
Assert.assertEquals(2, callbacks.get());
}
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ConnectionOpenCloseTest method testSSLOpenRequestClose.
@Slow
@Test
public void testSSLOpenRequestClose() throws Exception {
SslContextFactory sslContextFactory = new SslContextFactory();
File keystore = MavenTestingUtils.getTestResourceFile("keystore");
sslContextFactory.setKeyStoreResource(Resource.newResource(keystore));
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.setKeyManagerPassword("keypwd");
server.addBean(sslContextFactory);
server.removeConnector(connector);
connector = new ServerConnector(server, sslContextFactory);
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);
}
});
server.start();
final AtomicInteger callbacks = new AtomicInteger();
final CountDownLatch openLatch = new CountDownLatch(2);
final CountDownLatch closeLatch = new CountDownLatch(2);
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();
}
});
Socket socket = sslContextFactory.getSslContext().getSocketFactory().createSocket("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);
Assert.assertEquals(200, response.getStatus());
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(4, callbacks.get());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientTest method testRequestWithResponseContentChunked.
private void testRequestWithResponseContentChunked(int length) throws Exception {
final byte[] chunk1 = new byte[length];
final byte[] chunk2 = new byte[length];
Random random = new Random();
random.nextBytes(chunk1);
random.nextBytes(chunk2);
byte[] bytes = new byte[chunk1.length + chunk2.length];
System.arraycopy(chunk1, 0, bytes, 0, chunk1.length);
System.arraycopy(chunk2, 0, bytes, chunk1.length, chunk2.length);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
output.flush();
output.write(chunk2);
}
});
org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
FutureResponseListener listener = new FutureResponseListener(request, 2 * length);
request.timeout(10, TimeUnit.SECONDS).send(listener);
ContentResponse response = listener.get();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
Aggregations