Search in sources :

Example 6 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 7 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 8 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)

Example 9 with MetaData

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

Example 10 with MetaData

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

the class HpackPerfTest method encodeStories.

private void encodeStories(ByteBuffer buffer, Map<String, Object>[] stories, String type) throws Exception {
    for (Map<String, Object> story : stories) {
        if (type.equals(story.get("context"))) {
            HpackEncoder encoder = new HpackEncoder(_maxDynamicTableSize, _maxDynamicTableSize);
            // System.err.println(story);
            Object[] cases = (Object[]) story.get("cases");
            for (Object c : cases) {
                // System.err.println("  "+c);
                Object[] headers = (Object[]) ((Map<String, Object>) c).get("headers");
                // System.err.println("    "+headers);
                HttpFields fields = new HttpFields();
                for (Object header : headers) {
                    Map<String, String> h = (Map<String, String>) header;
                    Map.Entry<String, String> e = h.entrySet().iterator().next();
                    fields.add(e.getKey(), e.getValue());
                    _unencodedSize += e.getKey().length() + e.getValue().length();
                }
                BufferUtil.clearToFill(buffer);
                encoder.encode(buffer, new MetaData(HttpVersion.HTTP_2, fields));
                BufferUtil.flipToFlush(buffer, 0);
                _encodedSize += buffer.remaining();
            }
        }
    }
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Map(java.util.Map)

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