Search in sources :

Example 46 with MetaData

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

the class TrailersTest method testTrailersSentByServer.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpFields responseFields = new HttpFields();
            responseFields.put("X-Response", "true");
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, responseFields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, false);
            stream.headers(responseFrame, new Callback() {

                @Override
                public void succeeded() {
                    HttpFields trailerFields = new HttpFields();
                    trailerFields.put("X-Trailer", "true");
                    MetaData trailer = new MetaData(HttpVersion.HTTP_2, trailerFields);
                    HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailer, null, true);
                    stream.headers(trailerFrame, NOOP);
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(request, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        private boolean responded;

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (!responded) {
                MetaData.Response response = (MetaData.Response) frame.getMetaData();
                Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
                Assert.assertTrue(response.getFields().containsKey("X-Response"));
                Assert.assertFalse(frame.isEndStream());
                responded = true;
            } else {
                MetaData trailer = frame.getMetaData();
                Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                Assert.assertTrue(frame.isEndStream());
                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) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 47 with MetaData

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

the class TrailersTest method testTrailersSentByClient.

@Test
public void testTrailersSentByClient() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            Assert.assertFalse(frame.isEndStream());
            Assert.assertTrue(request.getFields().containsKey("X-Request"));
            return new Stream.Listener.Adapter() {

                @Override
                public void onHeaders(Stream stream, HeadersFrame frame) {
                    MetaData trailer = frame.getMetaData();
                    Assert.assertTrue(frame.isEndStream());
                    Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                    latch.countDown();
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields requestFields = new HttpFields();
    requestFields.put("X-Request", "true");
    MetaData.Request request = newRequest("GET", requestFields);
    HeadersFrame requestFrame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> streamPromise = new FuturePromise<>();
    session.newStream(requestFrame, streamPromise, new Stream.Listener.Adapter());
    Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
    // Send the trailers.
    HttpFields trailerFields = new HttpFields();
    trailerFields.put("X-Trailer", "true");
    MetaData trailers = new MetaData(HttpVersion.HTTP_2, trailerFields);
    HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailers, null, true);
    stream.headers(trailerFrame, Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 48 with MetaData

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

the class PushPromiseGenerateParseTest method testGenerateParseOneByteAtATime.

@Test
public void testGenerateParseOneByteAtATime() throws Exception {
    PushPromiseGenerator generator = new PushPromiseGenerator(new HeaderGenerator(), new HpackEncoder());
    final List<PushPromiseFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onPushPromise(PushPromiseFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int promisedStreamId = 17;
    HttpFields fields = new HttpFields();
    fields.put("Accept", "text/html");
    fields.put("User-Agent", "Jetty");
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields);
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generatePushPromise(lease, streamId, promisedStreamId, metaData);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        PushPromiseFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertEquals(promisedStreamId, frame.getPromisedStreamId());
        MetaData.Request request = (MetaData.Request) frame.getMetaData();
        Assert.assertEquals(metaData.getMethod(), request.getMethod());
        Assert.assertEquals(metaData.getURI(), request.getURI());
        for (int j = 0; j < fields.size(); ++j) {
            HttpField field = fields.getField(j);
            Assert.assertTrue(request.getFields().contains(field));
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PushPromiseGenerator(org.eclipse.jetty.http2.generator.PushPromiseGenerator) ArrayList(java.util.ArrayList) HpackEncoder(org.eclipse.jetty.http2.hpack.HpackEncoder) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) MetaData(org.eclipse.jetty.http.MetaData) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) Test(org.junit.Test)

Example 49 with MetaData

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

the class HpackEncoderTest method testFieldLargerThanTable.

@Test
public void testFieldLargerThanTable() {
    HttpFields fields = new HttpFields();
    HpackEncoder encoder = new HpackEncoder(128);
    ByteBuffer buffer0 = BufferUtil.allocate(4096);
    int pos = BufferUtil.flipToFill(buffer0);
    encoder.encode(buffer0, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer0, pos);
    encoder = new HpackEncoder(128);
    fields.add(new HttpField("user-agent", "jetty/test"));
    ByteBuffer buffer1 = BufferUtil.allocate(4096);
    pos = BufferUtil.flipToFill(buffer1);
    encoder.encode(buffer1, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer1, pos);
    encoder = new HpackEncoder(128);
    fields.add(new HttpField(":path", "This is a very large field, whose size is larger than the dynamic table so it should not be indexed as it will not fit in the table ever!" + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " + "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY " + "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ "));
    ByteBuffer buffer2 = BufferUtil.allocate(4096);
    pos = BufferUtil.flipToFill(buffer2);
    encoder.encode(buffer2, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer2, pos);
    encoder = new HpackEncoder(128);
    fields.add(new HttpField("host", "somehost"));
    ByteBuffer buffer = BufferUtil.allocate(4096);
    pos = BufferUtil.flipToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, pos);
    //System.err.println(BufferUtil.toHexString(buffer0));
    //System.err.println(BufferUtil.toHexString(buffer1));
    //System.err.println(BufferUtil.toHexString(buffer2));
    //System.err.println(BufferUtil.toHexString(buffer));
    // something was encoded!
    assertThat(buffer.remaining(), Matchers.greaterThan(0));
    // check first field is static index name and dynamic index body
    assertThat((buffer.get(buffer0.remaining()) & 0xFF) >> 6, equalTo(1));
    // check first field is static index name and literal body
    assertThat((buffer.get(buffer1.remaining()) & 0xFF) >> 4, equalTo(0));
    // check first field is static index name and dynamic index body
    assertThat((buffer.get(buffer2.remaining()) & 0xFF) >> 6, equalTo(1));
    // Only first and third fields are put in the table
    HpackContext context = encoder.getHpackContext();
    assertThat(context.size(), equalTo(2));
    assertThat(context.get(HpackContext.STATIC_SIZE + 1).getHttpField().getName(), equalTo("host"));
    assertThat(context.get(HpackContext.STATIC_SIZE + 2).getHttpField().getName(), equalTo("user-agent"));
    assertThat(context.getDynamicTableSize(), equalTo(context.get(HpackContext.STATIC_SIZE + 1).getSize() + context.get(HpackContext.STATIC_SIZE + 2).getSize()));
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 50 with MetaData

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

the class HpackEncoderTest method testUnknownFieldsContextManagement.

@Test
public void testUnknownFieldsContextManagement() {
    HpackEncoder encoder = new HpackEncoder(38 * 5);
    HttpFields fields = new HttpFields();
    HttpField[] field = { new HttpField("fo0", "b0r"), new HttpField("fo1", "b1r"), new HttpField("fo2", "b2r"), new HttpField("fo3", "b3r"), new HttpField("fo4", "b4r"), new HttpField("fo5", "b5r"), new HttpField("fo6", "b6r"), new HttpField("fo7", "b7r"), new HttpField("fo8", "b8r"), new HttpField("fo9", "b9r"), new HttpField("foA", "bAr") };
    // Add 4 entries
    for (int i = 0; i <= 3; i++) fields.add(field[i]);
    // encode them
    ByteBuffer buffer = BufferUtil.allocate(4096);
    int pos = BufferUtil.flipToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, pos);
    // something was encoded!
    assertThat(buffer.remaining(), Matchers.greaterThan(0));
    // All are in the dynamic table
    Assert.assertEquals(4, encoder.getHpackContext().size());
    // encode exact same fields again!
    BufferUtil.clearToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, 0);
    // All are in the dynamic table
    Assert.assertEquals(4, encoder.getHpackContext().size());
    // Add 4 more fields
    for (int i = 4; i <= 7; i++) fields.add(field[i]);
    // encode
    BufferUtil.clearToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, 0);
    // something was encoded!
    assertThat(buffer.remaining(), Matchers.greaterThan(0));
    // max dynamic table size reached
    Assert.assertEquals(5, encoder.getHpackContext().size());
    // remove some fields
    for (int i = 0; i <= 7; i += 2) fields.remove(field[i].getName());
    // encode
    BufferUtil.clearToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, 0);
    // something was encoded!
    assertThat(buffer.remaining(), Matchers.greaterThan(0));
    // max dynamic table size reached
    Assert.assertEquals(5, encoder.getHpackContext().size());
    // remove another fields
    fields.remove(field[1].getName());
    // encode
    BufferUtil.clearToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, 0);
    // something was encoded!
    assertThat(buffer.remaining(), Matchers.greaterThan(0));
    // max dynamic table size reached
    Assert.assertEquals(5, encoder.getHpackContext().size());
    // re add the field
    fields.add(field[1]);
    // encode
    BufferUtil.clearToFill(buffer);
    encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
    BufferUtil.flipToFlush(buffer, 0);
    // something was encoded!
    assertThat(buffer.remaining(), Matchers.greaterThan(0));
    // max dynamic table size reached
    Assert.assertEquals(5, encoder.getHpackContext().size());
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ByteBuffer(java.nio.ByteBuffer) 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