Search in sources :

Example 46 with HttpField

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

the class HeadersGenerateParseTest method testGenerateParseOneByteAtATime.

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

        @Override
        public void onHeaders(HeadersFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        int streamId = 13;
        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);
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        PriorityFrame priorityFrame = new PriorityFrame(streamId, 3 * streamId, 200, true);
        generator.generateHeaders(lease, streamId, metaData, priorityFrame, true);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            buffer = buffer.slice();
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        HeadersFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertTrue(frame.isEndStream());
        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));
        }
        PriorityFrame priority = frame.getPriority();
        Assert.assertNotNull(priority);
        Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
        Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
        Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
        Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ArrayList(java.util.ArrayList) HeadersGenerator(org.eclipse.jetty.http2.generator.HeadersGenerator) 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 47 with HttpField

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

the class PushPromiseGenerateParseTest method testGenerateParse.

@Test
public void testGenerateParse() 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(buffer);
            }
        }
        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 48 with HttpField

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

the class HpackContextTest method testIndexes.

@Test
public void testIndexes() {
    // Only enough space for 5 entries
    HpackContext ctx = new HpackContext(38 * 5);
    HttpField methodPost = new HttpField(":method", "POST");
    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") };
    Entry[] entry = new Entry[100];
    // Lookup the index of a static field
    assertEquals(0, ctx.size());
    assertEquals(":authority", ctx.get(1).getHttpField().getName());
    assertEquals(3, ctx.index(ctx.get(methodPost)));
    assertEquals(methodPost, ctx.get(3).getHttpField());
    assertEquals("www-authenticate", ctx.get(61).getHttpField().getName());
    assertEquals(null, ctx.get(62));
    // Add a single entry  
    entry[0] = ctx.add(field[0]);
    // Check new entry is 62 
    assertEquals(1, ctx.size());
    assertEquals(62, ctx.index(entry[0]));
    assertEquals(entry[0], ctx.get(62));
    // and statics still OK
    assertEquals(":authority", ctx.get(1).getHttpField().getName());
    assertEquals(3, ctx.index(ctx.get(methodPost)));
    assertEquals(methodPost, ctx.get(3).getHttpField());
    assertEquals("www-authenticate", ctx.get(61).getHttpField().getName());
    assertEquals(null, ctx.get(62 + ctx.size()));
    // Add 4 more entries
    for (int i = 1; i <= 4; i++) entry[i] = ctx.add(field[i]);
    // Check newest entry is at 62 oldest at 66
    assertEquals(5, ctx.size());
    int index = 66;
    for (int i = 0; i <= 4; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // and statics still OK
    assertEquals(":authority", ctx.get(1).getHttpField().getName());
    assertEquals(3, ctx.index(ctx.get(methodPost)));
    assertEquals(methodPost, ctx.get(3).getHttpField());
    assertEquals("www-authenticate", ctx.get(61).getHttpField().getName());
    assertEquals(null, ctx.get(62 + ctx.size()));
    // add 1 more entry and this should cause an eviction!
    entry[5] = ctx.add(field[5]);
    // Check newest entry is at 1 oldest at 5
    index = 66;
    for (int i = 1; i <= 5; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // check entry 0 evicted
    assertNull(ctx.get(field[0]));
    assertEquals(0, ctx.index(entry[0]));
    // and statics still OK
    assertEquals(":authority", ctx.get(1).getHttpField().getName());
    assertEquals(3, ctx.index(ctx.get(methodPost)));
    assertEquals(methodPost, ctx.get(3).getHttpField());
    assertEquals("www-authenticate", ctx.get(61).getHttpField().getName());
    assertEquals(null, ctx.get(62 + ctx.size()));
    // Add 4 more entries
    for (int i = 6; i <= 9; i++) entry[i] = ctx.add(field[i]);
    // Check newest entry is at 1 oldest at 5
    index = 66;
    for (int i = 5; i <= 9; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // check entry 0-4 evicted
    for (int i = 0; i <= 4; i++) {
        assertNull(ctx.get(field[i]));
        assertEquals(0, ctx.index(entry[i]));
    }
    // Add new entries enough so that array queue will wrap
    for (int i = 10; i <= 52; i++) entry[i] = ctx.add(new HttpField("n" + i, "v" + i));
    index = 66;
    for (int i = 48; i <= 52; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
}
Also used : Entry(org.eclipse.jetty.http2.hpack.HpackContext.Entry) HttpField(org.eclipse.jetty.http.HttpField) Test(org.junit.Test)

Example 49 with HttpField

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

the class HpackContextTest method testJustRight.

@Test
public void testJustRight() {
    HpackContext ctx = new HpackContext(38);
    HttpField field = new HttpField("foo", "bar");
    Entry entry = ctx.add(field);
    Assert.assertNotNull(entry);
    Assert.assertThat(entry.toString(), Matchers.startsWith("{D,0,foo: bar,"));
}
Also used : Entry(org.eclipse.jetty.http2.hpack.HpackContext.Entry) HttpField(org.eclipse.jetty.http.HttpField) Test(org.junit.Test)

Example 50 with HttpField

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

the class HpackContextTest method testResize.

@Test
public void testResize() {
    // Only enough space for 5 entries
    HpackContext ctx = new HpackContext(38 * 5);
    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") };
    Entry[] entry = new Entry[field.length];
    // Add 5 entries
    for (int i = 0; i <= 4; i++) entry[i] = ctx.add(field[i]);
    assertEquals(5, ctx.size());
    // check indexes
    int index = 66;
    for (int i = 0; i <= 4; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // resize so that only 2 entries may be held
    ctx.resize(38 * 2);
    assertEquals(2, ctx.size());
    // check indexes
    index = 63;
    for (int i = 3; i <= 4; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // resize so that 6.5 entries may be held
    ctx.resize(38 * 6 + 19);
    assertEquals(2, ctx.size());
    // check indexes
    index = 63;
    for (int i = 3; i <= 4; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // Add 5 entries
    for (int i = 5; i <= 9; i++) entry[i] = ctx.add(field[i]);
    assertEquals(6, ctx.size());
    // check indexes
    index = 67;
    for (int i = 4; i <= 9; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // resize so that only 100 entries may be held
    ctx.resize(38 * 100);
    assertEquals(6, ctx.size());
    // check indexes
    index = 67;
    for (int i = 4; i <= 9; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
    // add 50 fields
    for (int i = 0; i < 50; i++) ctx.add(new HttpField("n" + i, "v" + i));
    // check indexes
    index = 67 + 50;
    for (int i = 4; i <= 9; i++) {
        assertEquals(index, ctx.index(entry[i]));
        assertEquals(entry[i], ctx.get(index));
        index--;
    }
}
Also used : Entry(org.eclipse.jetty.http2.hpack.HpackContext.Entry) 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