Search in sources :

Example 1 with AbstractConnectionFactory

use of org.eclipse.jetty.server.AbstractConnectionFactory 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)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 AbstractConnection (org.eclipse.jetty.io.AbstractConnection)1 Connection (org.eclipse.jetty.io.Connection)1 EndPoint (org.eclipse.jetty.io.EndPoint)1 AbstractConnectionFactory (org.eclipse.jetty.server.AbstractConnectionFactory)1 Connector (org.eclipse.jetty.server.Connector)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 Utf8StringBuilder (org.eclipse.jetty.util.Utf8StringBuilder)1 Test (org.junit.Test)1