Search in sources :

Example 1 with AbstractConnection

use of org.eclipse.jetty.io.AbstractConnection in project jetty.project by eclipse.

the class HttpClientTest method testCONNECTWithHTTP10.

@Test
public void testCONNECTWithHTTP10() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        startClient();
        String host = "localhost";
        int port = server.getLocalPort();
        Request request = client.newRequest(host, port).method(HttpMethod.CONNECT).version(HttpVersion.HTTP_1_0);
        FuturePromise<Connection> promise = new FuturePromise<>();
        client.getDestination("http", host, port).newConnection(promise);
        Connection connection = promise.get(5, TimeUnit.SECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        try (Socket socket = server.accept()) {
            InputStream input = socket.getInputStream();
            consume(input, false);
            // HTTP/1.0 response, the client must not close the connection.
            String httpResponse = "" + "HTTP/1.0 200 OK\r\n" + "\r\n";
            OutputStream output = socket.getOutputStream();
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            ContentResponse response = listener.get(5, TimeUnit.SECONDS);
            Assert.assertEquals(200, response.getStatus());
            // Because the tunnel was successful, this connection will be
            // upgraded to an SslConnection, so it will not be fill interested.
            // This test doesn't upgrade, so it needs to restore the fill interest.
            ((AbstractConnection) connection).fillInterested();
            // Test that I can send another request on the same connection.
            request = client.newRequest(host, port);
            listener = new FutureResponseListener(request);
            connection.send(request, listener);
            consume(input, false);
            httpResponse = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            listener.get(5, TimeUnit.SECONDS);
        }
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) ServerSocket(java.net.ServerSocket) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 2 with AbstractConnection

use of org.eclipse.jetty.io.AbstractConnection in project jetty.project by eclipse.

the class ForwardProxyServerTest method testRequestTarget.

@Test
public void testRequestTarget() throws Exception {
    startServer(new AbstractConnectionFactory("http/1.1") {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            return new AbstractConnection(endPoint, connector.getExecutor()) {

                @Override
                public void onOpen() {
                    super.onOpen();
                    fillInterested();
                }

                @Override
                public void onFillable() {
                    try {
                        // When using TLS, multiple reads are required.
                        ByteBuffer buffer = BufferUtil.allocate(1024);
                        int filled = 0;
                        while (filled == 0) filled = getEndPoint().fill(buffer);
                        Utf8StringBuilder builder = new Utf8StringBuilder();
                        builder.append(buffer);
                        String request = builder.toString();
                        // ProxyServlet will receive an absolute URI from
                        // the client, and convert it to a relative URI.
                        // The ConnectHandler won't modify what the client
                        // sent, which must be a relative URI.
                        Assert.assertThat(request.length(), Matchers.greaterThan(0));
                        if (serverSslContextFactory == null)
                            Assert.assertFalse(request.contains("http://"));
                        else
                            Assert.assertFalse(request.contains("https://"));
                        String response = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
                        getEndPoint().write(Callback.NOOP, ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
                    } catch (Throwable x) {
                        x.printStackTrace();
                        close();
                    }
                }
            };
        }
    });
    startProxy();
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        ContentResponse response = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(serverSslContextFactory == null ? "http" : "https").method(HttpMethod.GET).path("/test").send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    } finally {
        httpClient.stop();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) AbstractConnectionFactory(org.eclipse.jetty.server.AbstractConnectionFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) HttpClient(org.eclipse.jetty.client.HttpClient) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.io.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with AbstractConnection

use of org.eclipse.jetty.io.AbstractConnection in project jetty.project by eclipse.

the class SslConnectionTest method testSslConnectionClosedBeforeFill.

@Test
public void testSslConnectionClosedBeforeFill() throws Exception {
    File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.start();
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.start();
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    SSLEngine sslEngine = sslContextFactory.newSSLEngine();
    sslEngine.setUseClientMode(false);
    SslConnection sslConnection = new SslConnection(byteBufferPool, threadPool, endPoint, sslEngine);
    EndPoint sslEndPoint = sslConnection.getDecryptedEndPoint();
    sslEndPoint.setConnection(new AbstractConnection(sslEndPoint, threadPool) {

        @Override
        public void onFillable() {
        }
    });
    // There are no bytes in the endPoint, so we fill zero.
    // However, this will trigger state changes in SSLEngine
    // that will later cause it to throw ISE("Internal error").
    sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
    // Close the connection before filling.
    sslEndPoint.shutdownOutput();
    // Put some bytes in the endPoint to trigger
    // the required state changes in SSLEngine.
    byte[] bytes = new byte[] { 0x16, 0x03, 0x03, 0x00, 0x00 };
    endPoint.addInput(ByteBuffer.wrap(bytes));
    // reads from the EndPoint.
    try {
        sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
        Assert.fail();
    } catch (SSLHandshakeException x) {
    // Expected.
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SSLEngine(javax.net.ssl.SSLEngine) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) EndPoint(org.eclipse.jetty.io.EndPoint) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) File(java.io.File) Test(org.junit.Test)

Aggregations

AbstractConnection (org.eclipse.jetty.io.AbstractConnection)3 EndPoint (org.eclipse.jetty.io.EndPoint)3 Test (org.junit.Test)3 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 SSLEngine (javax.net.ssl.SSLEngine)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 Connection (org.eclipse.jetty.client.api.Connection)1 Request (org.eclipse.jetty.client.api.Request)1 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)1 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)1 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)1