Search in sources :

Example 16 with HTTP2Client

use of org.eclipse.jetty.http2.client.HTTP2Client 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)

Example 17 with HTTP2Client

use of org.eclipse.jetty.http2.client.HTTP2Client in project jetty.project by eclipse.

the class AbstractTest method prepareClient.

protected void prepareClient() throws Exception {
    client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client()), null);
    QueuedThreadPool clientExecutor = new QueuedThreadPool();
    clientExecutor.setName("client");
    client.setExecutor(clientExecutor);
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client)

Aggregations

HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)10 HttpClient (org.eclipse.jetty.client.HttpClient)8 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)8 Session (org.eclipse.jetty.http2.api.Session)7 HttpFields (org.eclipse.jetty.http.HttpFields)6 MetaData (org.eclipse.jetty.http.MetaData)6 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)6 Test (org.junit.Test)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Stream (org.eclipse.jetty.http2.api.Stream)5 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)4 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)4 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)4 Server (org.eclipse.jetty.server.Server)4 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 Callback (org.eclipse.jetty.util.Callback)4 FuturePromise (org.eclipse.jetty.util.FuturePromise)4 InetSocketAddress (java.net.InetSocketAddress)3 Executor (java.util.concurrent.Executor)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3