Search in sources :

Example 41 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method testHTTP10WithKeepAliveAndNoContentLength.

@Test
public void testHTTP10WithKeepAliveAndNoContentLength() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Send the headers at this point, then write the content
            response.flushBuffer();
            response.getOutputStream().print("TEST");
        }
    });
    FuturePromise<Connection> promise = new FuturePromise<>();
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    destination.newConnection(promise);
    try (Connection connection = promise.get(5, TimeUnit.SECONDS)) {
        long timeout = 5000;
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(destination.getScheme()).version(HttpVersion.HTTP_1_0).header(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString()).timeout(timeout, TimeUnit.MILLISECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        ContentResponse response = listener.get(2 * timeout, TimeUnit.MILLISECONDS);
        Assert.assertEquals(200, response.getStatus());
        // The parser notifies end-of-content and therefore the CompleteListener
        // before closing the connection, so we need to wait before checking
        // that the connection is closed to avoid races.
        Thread.sleep(1000);
        Assert.assertTrue(connection.isClosed());
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 42 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method test_GET_WithParameters_ResponseWithContent.

@Test
public void test_GET_WithParameters_ResponseWithContent() throws Exception {
    final String paramName1 = "a";
    final String paramName2 = "b";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setCharacterEncoding("UTF-8");
            ServletOutputStream output = response.getOutputStream();
            String paramValue1 = request.getParameter(paramName1);
            output.write(paramValue1.getBytes(StandardCharsets.UTF_8));
            String paramValue2 = request.getParameter(paramName2);
            Assert.assertEquals("", paramValue2);
            output.write("empty".getBytes(StandardCharsets.UTF_8));
            baseRequest.setHandled(true);
        }
    });
    String value1 = "€";
    String paramValue1 = URLEncoder.encode(value1, "UTF-8");
    String query = paramName1 + "=" + paramValue1 + "&" + paramName2;
    ContentResponse response = client.GET(scheme + "://localhost:" + connector.getLocalPort() + "/?" + query);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    String content = new String(response.getContent(), StandardCharsets.UTF_8);
    Assert.assertEquals(value1 + "empty", content);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletOutputStream(javax.servlet.ServletOutputStream) 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)

Example 43 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class SslBytesServerTest method init.

@Before
public void init() throws Exception {
    threadPool = Executors.newCachedThreadPool();
    server = new Server();
    File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
    sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    HttpConnectionFactory httpFactory = new HttpConnectionFactory() {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            return configure(new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {

                @Override
                protected HttpParser newHttpParser(HttpCompliance compliance) {
                    return new HttpParser(newRequestHandler(), getHttpConfiguration().getRequestHeaderSize(), compliance) {

                        @Override
                        public boolean parseNext(ByteBuffer buffer) {
                            httpParses.incrementAndGet();
                            return super.parseNext(buffer);
                        }
                    };
                }

                @Override
                protected boolean onReadTimeout() {
                    final Runnable idleHook = SslBytesServerTest.this.idleHook;
                    if (idleHook != null)
                        idleHook.run();
                    return super.onReadTimeout();
                }
            }, connector, endPoint);
        }
    };
    httpFactory.getHttpConfiguration().addCustomizer(new SecureRequestCustomizer());
    SslConnectionFactory sslFactory = new SslConnectionFactory(sslContextFactory, httpFactory.getProtocol()) {

        @Override
        protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) {
            return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine) {

                @Override
                protected DecryptedEndPoint newDecryptedEndPoint() {
                    return new DecryptedEndPoint() {

                        @Override
                        public int fill(ByteBuffer buffer) throws IOException {
                            sslFills.incrementAndGet();
                            return super.fill(buffer);
                        }

                        @Override
                        public boolean flush(ByteBuffer... appOuts) throws IOException {
                            sslFlushes.incrementAndGet();
                            return super.flush(appOuts);
                        }
                    };
                }
            };
        }
    };
    ServerConnector connector = new ServerConnector(server, null, null, null, 1, 1, sslFactory, httpFactory) {

        @Override
        protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
            ChannelEndPoint endp = super.newEndPoint(channel, selectSet, key);
            serverEndPoint.set(endp);
            return endp;
        }
    };
    connector.setIdleTimeout(idleTimeout);
    connector.setPort(0);
    server.addConnector(connector);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
            try {
                request.setHandled(true);
                String contentLength = request.getHeader("Content-Length");
                if (contentLength != null) {
                    int length = Integer.parseInt(contentLength);
                    ServletInputStream input = httpRequest.getInputStream();
                    ServletOutputStream output = httpResponse.getOutputStream();
                    byte[] buffer = new byte[32 * 1024];
                    while (length > 0) {
                        int read = input.read(buffer);
                        if (read < 0)
                            throw new EOFException();
                        length -= read;
                        if (target.startsWith("/echo"))
                            output.write(buffer, 0, read);
                    }
                }
            } catch (IOException x) {
                if (!(target.endsWith("suppress_exception")))
                    throw x;
            }
        }
    });
    server.start();
    serverPort = connector.getLocalPort();
    sslContext = sslContextFactory.getSslContext();
    proxy = new SimpleProxy(threadPool, "localhost", serverPort);
    proxy.start();
    logger.info("proxy:{} <==> server:{}", proxy.getPort(), serverPort);
}
Also used : ManagedSelector(org.eclipse.jetty.io.ManagedSelector) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) SocketChannel(java.nio.channels.SocketChannel) Server(org.eclipse.jetty.server.Server) HttpConnection(org.eclipse.jetty.server.HttpConnection) ChannelEndPoint(org.eclipse.jetty.io.ChannelEndPoint) ServletOutputStream(javax.servlet.ServletOutputStream) SSLEngine(javax.net.ssl.SSLEngine) EndPoint(org.eclipse.jetty.io.EndPoint) ChannelEndPoint(org.eclipse.jetty.io.ChannelEndPoint) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) EOFException(java.io.EOFException) HttpParser(org.eclipse.jetty.http.HttpParser) SelectionKey(java.nio.channels.SelectionKey) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpCompliance(org.eclipse.jetty.http.HttpCompliance) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) File(java.io.File) Before(org.junit.Before)

Example 44 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientURITest method testPathWithQuery.

@Test
public void testPathWithQuery() throws Exception {
    String name = "a";
    String value = "1";
    final String query = name + "=" + value;
    final String path = "/path";
    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);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    String pathQuery = path + "?" + query;
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(pathQuery);
    Assert.assertEquals(path, request.getPath());
    Assert.assertEquals(query, request.getQuery());
    Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
    Fields params = request.getParams();
    Assert.assertEquals(1, params.getSize());
    Assert.assertEquals(value, params.get(name).getValue());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Example 45 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientURITest method testPathWithParam.

@Test
public void testPathWithParam() throws Exception {
    String name = "a";
    String value = "1";
    final String query = name + "=" + value;
    final String path = "/path";
    String pathQuery = path + "?" + query;
    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);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path).param(name, value);
    Assert.assertEquals(path, request.getPath());
    Assert.assertEquals(query, request.getQuery());
    Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
    Fields params = request.getParams();
    Assert.assertEquals(1, params.getSize());
    Assert.assertEquals(value, params.get(name).getValue());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) 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