Search in sources :

Example 96 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class HTTP2Test method testRequestNoContentResponseNoContent.

@Test
public void testRequestNoContentResponseNoContent() throws Exception {
    start(new EmptyHttpServlet());
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final CountDownLatch latch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            Assert.assertTrue(stream.getId() > 0);
            Assert.assertTrue(frame.isEndStream());
            Assert.assertEquals(stream.getId(), frame.getStreamId());
            Assert.assertTrue(frame.getMetaData().isResponse());
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(200, response.getStatus());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 97 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class HTTP2Test method testRequestNoContentResponseContent.

@Test
public void testRequestNoContentResponseContent() throws Exception {
    final byte[] content = "Hello World!".getBytes(StandardCharsets.UTF_8);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getOutputStream().write(content);
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final CountDownLatch latch = new CountDownLatch(2);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            Assert.assertTrue(stream.getId() > 0);
            Assert.assertFalse(frame.isEndStream());
            Assert.assertEquals(stream.getId(), frame.getStreamId());
            Assert.assertTrue(frame.getMetaData().isResponse());
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(200, response.getStatus());
            latch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            Assert.assertTrue(frame.isEndStream());
            Assert.assertEquals(ByteBuffer.wrap(content), frame.getData());
            callback.succeeded();
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 98 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class HTTP2Test method testMultipleRequests.

@Test
public void testMultipleRequests() throws Exception {
    final String downloadBytes = "X-Download";
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            int download = request.getIntHeader(downloadBytes);
            byte[] content = new byte[download];
            new Random().nextBytes(content);
            response.getOutputStream().write(content);
        }
    });
    int requests = 20;
    Session session = newClient(new Session.Listener.Adapter());
    Random random = new Random();
    HttpFields fields = new HttpFields();
    fields.putLongField(downloadBytes, random.nextInt(128 * 1024));
    fields.put("User-Agent", "HTTP2Client/" + Jetty.VERSION);
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final CountDownLatch latch = new CountDownLatch(requests);
    for (int i = 0; i < requests; ++i) {
        session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

            @Override
            public void onData(Stream stream, DataFrame frame, Callback callback) {
                callback.succeeded();
                if (frame.isEndStream())
                    latch.countDown();
            }
        });
    }
    Assert.assertTrue(latch.await(requests, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) Random(java.util.Random) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 99 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class HTTP2Test method testInvalidAPIUsageOnClient.

@Test
public void testInvalidAPIUsageOnClient() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            Callback.Completable completable = new Callback.Completable();
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, false), completable);
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    if (frame.isEndStream()) {
                        completable.thenRun(() -> {
                            DataFrame endFrame = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
                            stream.data(endFrame, Callback.NOOP);
                        });
                    }
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(metaData, null, false);
    Promise.Completable<Stream> completable = new Promise.Completable<>();
    CountDownLatch completeLatch = new CountDownLatch(2);
    session.newStream(frame, completable, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                completeLatch.countDown();
        }
    });
    Stream stream = completable.get(5, TimeUnit.SECONDS);
    long sleep = 1000;
    DataFrame data1 = new DataFrame(stream.getId(), ByteBuffer.allocate(1024), false) {

        @Override
        public ByteBuffer getData() {
            sleep(2 * sleep);
            return super.getData();
        }
    };
    DataFrame data2 = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
    new Thread(() -> {
        // The first data() call is legal, but slow.
        stream.data(data1, new Callback() {

            @Override
            public void succeeded() {
                stream.data(data2, NOOP);
            }
        });
    }).start();
    // Wait for the first data() call to happen.
    sleep(sleep);
    // This data call is illegal because it does not
    // wait for the previous callback to complete.
    stream.data(data2, new Callback() {

        @Override
        public void failed(Throwable x) {
            if (x instanceof WritePendingException) {
                // Expected.
                completeLatch.countDown();
            }
        }
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WritePendingException(java.nio.channels.WritePendingException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

MetaData (org.eclipse.jetty.http.MetaData)99 HttpFields (org.eclipse.jetty.http.HttpFields)87 Test (org.junit.Test)79 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)75 CountDownLatch (java.util.concurrent.CountDownLatch)64 Stream (org.eclipse.jetty.http2.api.Stream)61 Session (org.eclipse.jetty.http2.api.Session)59 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)57 FuturePromise (org.eclipse.jetty.util.FuturePromise)51 ByteBuffer (java.nio.ByteBuffer)39 HttpServletResponse (javax.servlet.http.HttpServletResponse)36 Promise (org.eclipse.jetty.util.Promise)36 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)35 Callback (org.eclipse.jetty.util.Callback)30 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)29 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)26 IOException (java.io.IOException)24 HashMap (java.util.HashMap)24 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 ServletException (javax.servlet.ServletException)23