Search in sources :

Example 36 with AbstractHandler

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));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 37 with AbstractHandler

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());
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 38 with AbstractHandler

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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 39 with AbstractHandler

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();
    }
}
Also used : OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Socket(java.net.Socket) Test(org.junit.Test)

Example 40 with AbstractHandler

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)219 HttpServletRequest (javax.servlet.http.HttpServletRequest)216 HttpServletResponse (javax.servlet.http.HttpServletResponse)216 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)188 Request (org.eclipse.jetty.server.Request)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 CountDownLatch (java.util.concurrent.CountDownLatch)80 InterruptedIOException (java.io.InterruptedIOException)40 Result (org.eclipse.jetty.client.api.Result)38 InputStream (java.io.InputStream)35 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)32 Response (org.eclipse.jetty.client.api.Response)32 Request (org.eclipse.jetty.client.api.Request)29 OutputStream (java.io.OutputStream)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24