Search in sources :

Example 6 with HttpField

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

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

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

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

the class HpackContextTest method testGetAddStaticName.

@Test
public void testGetAddStaticName() {
    HpackContext ctx = new HpackContext(4096);
    HttpField methodOther = new HttpField(":method", "OTHER");
    // Look for the field by name.  Should find static version.
    assertEquals(":method", ctx.get(":method").getHttpField().getName());
    assertTrue(ctx.get(":method").isStatic());
    // Add dynamic entry with method
    ctx.add(methodOther);
    // Look for the field by name.  Should find static version.
    assertEquals(":method", ctx.get(":method").getHttpField().getName());
    assertTrue(ctx.get(":method").isStatic());
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) Test(org.junit.Test)

Example 10 with HttpField

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

the class HpackContextTest method testTooBigAdd.

@Test
public void testTooBigAdd() {
    HpackContext ctx = new HpackContext(37);
    HttpField field = new HttpField("foo", "bar");
    Assert.assertNull(ctx.add(field));
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) Test(org.junit.Test)

Aggregations

HttpField (org.eclipse.jetty.http.HttpField)57 Test (org.junit.Test)29 HttpFields (org.eclipse.jetty.http.HttpFields)19 ByteBuffer (java.nio.ByteBuffer)17 MetaData (org.eclipse.jetty.http.MetaData)16 HostPortHttpField (org.eclipse.jetty.http.HostPortHttpField)12 ArrayList (java.util.ArrayList)8 PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)7 Entry (org.eclipse.jetty.http2.hpack.HpackContext.Entry)7 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)7 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)7 HttpHeader (org.eclipse.jetty.http.HttpHeader)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HpackEncoder (org.eclipse.jetty.http2.hpack.HpackEncoder)4 Parser (org.eclipse.jetty.http2.parser.Parser)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3