Search in sources :

Example 81 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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 82 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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 83 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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 84 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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)

Example 85 with HttpFields

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

the class HpackEncoderTest method testNeverIndexSetCookie.

@Test
public void testNeverIndexSetCookie() {
    HpackEncoder encoder = new HpackEncoder(38 * 5);
    ByteBuffer buffer = BufferUtil.allocate(4096);
    HttpFields fields = new HttpFields();
    fields.put("set-cookie", "some cookie value");
    // 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));
    // empty dynamic table
    Assert.assertEquals(0, encoder.getHpackContext().size());
    // encode again
    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));
    // empty dynamic table
    Assert.assertEquals(0, encoder.getHpackContext().size());
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)172 Test (org.junit.Test)142 MetaData (org.eclipse.jetty.http.MetaData)117 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)105 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)93 Session (org.eclipse.jetty.http2.api.Session)89 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)79 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)59 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)46 IOException (java.io.IOException)42 ServletException (javax.servlet.ServletException)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32