Search in sources :

Example 1 with HttpDestination

use of org.eclipse.jetty.client.HttpDestination in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2 method doStart.

@Override
protected void doStart() throws Exception {
    if (!client.isStarted()) {
        client.setExecutor(httpClient.getExecutor());
        client.setScheduler(httpClient.getScheduler());
        client.setByteBufferPool(httpClient.getByteBufferPool());
        client.setConnectTimeout(httpClient.getConnectTimeout());
        client.setIdleTimeout(httpClient.getIdleTimeout());
        client.setInputBufferSize(httpClient.getResponseBufferSize());
    }
    addBean(client);
    super.doStart();
    this.connectionFactory = new HTTP2ClientConnectionFactory();
    client.setClientConnectionFactory((endPoint, context) -> {
        HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
        return destination.getClientConnectionFactory().newConnection(endPoint, context);
    });
}
Also used : HTTP2ClientConnectionFactory(org.eclipse.jetty.http2.client.HTTP2ClientConnectionFactory) HttpDestination(org.eclipse.jetty.client.HttpDestination)

Example 2 with HttpDestination

use of org.eclipse.jetty.client.HttpDestination in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP method newConnection.

@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
    @SuppressWarnings("unchecked") Promise<Connection> promise = (Promise<Connection>) context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
    HttpConnectionOverHTTP connection = newHttpConnection(endPoint, destination, promise);
    if (LOG.isDebugEnabled())
        LOG.debug("Created {}", connection);
    return customize(connection, context);
}
Also used : Promise(org.eclipse.jetty.util.Promise) Connection(org.eclipse.jetty.client.api.Connection) HttpDestination(org.eclipse.jetty.client.HttpDestination)

Example 3 with HttpDestination

use of org.eclipse.jetty.client.HttpDestination in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testClientStopsServerDoesNotCloseClientCloses.

@Test
public void testClientStopsServerDoesNotCloseClientCloses() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        List<Session> sessions = new ArrayList<>();
        HTTP2Client h2Client = new HTTP2Client();
        HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client) {

            @Override
            protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
                sessions.add(session);
                return super.newHttpConnection(destination, session);
            }
        }, null);
        QueuedThreadPool clientExecutor = new QueuedThreadPool();
        clientExecutor.setName("client");
        client.setExecutor(clientExecutor);
        client.start();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.OK_200)
                resultLatch.countDown();
        });
        ByteBufferPool byteBufferPool = new MappedByteBufferPool();
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        Generator generator = new Generator(byteBufferPool);
        try (Socket socket = server.accept()) {
            socket.setSoTimeout(1000);
            OutputStream output = socket.getOutputStream();
            InputStream input = socket.getInputStream();
            ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() {

                @Override
                public void onHeaders(HeadersFrame request) {
                    // Server's preface.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
                    // Reply to client's SETTINGS.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), true));
                    // Response.
                    MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true);
                    generator.control(lease, response);
                    try {
                        // Write the frames.
                        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
                    } catch (Throwable x) {
                        x.printStackTrace();
                    }
                }
            }, 4096, 8192);
            byte[] bytes = new byte[1024];
            while (true) {
                try {
                    int read = input.read(bytes);
                    if (read < 0)
                        Assert.fail();
                    parser.parse(ByteBuffer.wrap(bytes, 0, read));
                } catch (SocketTimeoutException x) {
                    break;
                }
            }
            Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
            // The client will send a GO_AWAY, but the server will not close.
            client.stop();
            // Give some time to process the stop/close operations.
            Thread.sleep(1000);
            Assert.assertTrue(h2Client.getBeans(Session.class).isEmpty());
            for (Session session : sessions) {
                Assert.assertTrue(session.isClosed());
                Assert.assertTrue(((HTTP2Session) session).isDisconnected());
            }
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SocketTimeoutException(java.net.SocketTimeoutException) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 4 with HttpDestination

use of org.eclipse.jetty.client.HttpDestination in project jetty.project by eclipse.

the class HttpClientTransportOverFCGI method newConnection.

@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
    @SuppressWarnings("unchecked") Promise<Connection> promise = (Promise<Connection>) context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
    HttpConnectionOverFCGI connection = newHttpConnection(endPoint, destination, promise);
    if (LOG.isDebugEnabled())
        LOG.debug("Created {}", connection);
    return customize(connection, context);
}
Also used : Promise(org.eclipse.jetty.util.Promise) Connection(org.eclipse.jetty.client.api.Connection) HttpDestination(org.eclipse.jetty.client.HttpDestination)

Example 5 with HttpDestination

use of org.eclipse.jetty.client.HttpDestination in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testLastStreamId.

@Test
public void testLastStreamId() throws Exception {
    prepareServer(new RawHTTP2ServerConnectionFactory(new HttpConfiguration(), new ServerSessionListener.Adapter() {

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
            return settings;
        }

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            if (HttpMethod.HEAD.is(request.getMethod())) {
                stream.getSession().close(ErrorCode.REFUSED_STREAM_ERROR.code, null, Callback.NOOP);
            } else {
                MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            }
            return null;
        }
    }));
    server.start();
    CountDownLatch latch = new CountDownLatch(2);
    AtomicInteger lastStream = new AtomicInteger();
    AtomicReference<Stream> streamRef = new AtomicReference<>();
    CountDownLatch streamLatch = new CountDownLatch(1);
    client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client()) {

        @Override
        protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
            return new HttpConnectionOverHTTP2(destination, session) {

                @Override
                protected HttpChannelOverHTTP2 newHttpChannel(boolean push) {
                    return new HttpChannelOverHTTP2(getHttpDestination(), this, getSession(), push) {

                        @Override
                        public void setStream(Stream stream) {
                            super.setStream(stream);
                            streamRef.set(stream);
                            streamLatch.countDown();
                        }
                    };
                }
            };
        }

        @Override
        protected void onClose(HttpConnectionOverHTTP2 connection, GoAwayFrame frame) {
            super.onClose(connection, frame);
            lastStream.set(frame.getLastStreamId());
            latch.countDown();
        }
    }, null);
    QueuedThreadPool clientExecutor = new QueuedThreadPool();
    clientExecutor.setName("client");
    client.setExecutor(clientExecutor);
    client.start();
    // Prime the connection to allow client and server prefaces to be exchanged.
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).path("/zero").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    org.eclipse.jetty.client.api.Request request = client.newRequest("localhost", connector.getLocalPort()).method(HttpMethod.HEAD).path("/one");
    request.send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(streamLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Stream stream = streamRef.get();
    Assert.assertNotNull(stream);
    Assert.assertEquals(lastStream.get(), stream.getId());
}
Also used : HashMap(java.util.HashMap) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

HttpDestination (org.eclipse.jetty.client.HttpDestination)5 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpClient (org.eclipse.jetty.client.HttpClient)2 Connection (org.eclipse.jetty.client.api.Connection)2 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)2 HttpFields (org.eclipse.jetty.http.HttpFields)2 MetaData (org.eclipse.jetty.http.MetaData)2 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)2 Session (org.eclipse.jetty.http2.api.Session)2 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)2 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)2 Promise (org.eclipse.jetty.util.Promise)2 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)2 Test (org.junit.Test)2 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1