Search in sources :

Example 41 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class WindowUpdateGenerateParseTest method testGenerateParseOneByteAtATime.

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

        @Override
        public void onWindowUpdate(WindowUpdateFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int windowUpdate = 17;
    // 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.generateWindowUpdate(lease, streamId, windowUpdate);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        WindowUpdateFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertEquals(windowUpdate, frame.getWindowDelta());
    }
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) WindowUpdateGenerator(org.eclipse.jetty.http2.generator.WindowUpdateGenerator) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Example 42 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class WindowUpdateGenerateParseTest method testGenerateParse.

@Test
public void testGenerateParse() throws Exception {
    WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator());
    final List<WindowUpdateFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onWindowUpdate(WindowUpdateFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int windowUpdate = 17;
    // 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.generateWindowUpdate(lease, streamId, windowUpdate);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(buffer);
            }
        }
    }
    Assert.assertEquals(1, frames.size());
    WindowUpdateFrame frame = frames.get(0);
    Assert.assertEquals(streamId, frame.getStreamId());
    Assert.assertEquals(windowUpdate, frame.getWindowDelta());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) WindowUpdateGenerator(org.eclipse.jetty.http2.generator.WindowUpdateGenerator) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Example 43 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2CServerTest method testHTTP_2_0_Direct.

@Test
public void testHTTP_2_0_Direct() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    byteBufferPool = new MappedByteBufferPool();
    generator = new Generator(byteBufferPool);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:" + connector.getLocalPort()), "/test", HttpVersion.HTTP_2, new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        final AtomicReference<HeadersFrame> headersRef = new AtomicReference<>();
        final AtomicReference<DataFrame> dataRef = new AtomicReference<>();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                latch.countDown();
            }

            @Override
            public void onHeaders(HeadersFrame frame) {
                headersRef.set(frame);
                latch.countDown();
            }

            @Override
            public void onData(DataFrame frame) {
                dataRef.set(frame);
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        HeadersFrame response = headersRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        DataFrame responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        String s = BufferUtil.toString(responseData.getData());
        assertThat(s, containsString("Hello from Jetty using HTTP/2.0"));
        assertThat(s, containsString("uri=/test"));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 44 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2CServerTest method testHTTP_1_1_Upgrade.

@Test
public void testHTTP_1_1_Upgrade() throws Exception {
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET /one HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: something, else, upgrade, HTTP2-Settings\r\n" + "Upgrade: h2c\r\n" + "HTTP2-Settings: \r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
        output.flush();
        InputStream input = client.getInputStream();
        Utf8StringBuilder upgrade = new Utf8StringBuilder();
        int crlfs = 0;
        while (true) {
            int read = input.read();
            if (read == '\r' || read == '\n')
                ++crlfs;
            else
                crlfs = 0;
            upgrade.append((byte) read);
            if (crlfs == 4)
                break;
        }
        assertTrue(upgrade.toString().startsWith("HTTP/1.1 101 "));
        byteBufferPool = new MappedByteBufferPool();
        generator = new Generator(byteBufferPool);
        final AtomicReference<HeadersFrame> headersRef = new AtomicReference<>();
        final AtomicReference<DataFrame> dataRef = new AtomicReference<>();
        final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>(new CountDownLatch(2));
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                headersRef.set(frame);
                latchRef.get().countDown();
            }

            @Override
            public void onData(DataFrame frame) {
                dataRef.set(frame);
                latchRef.get().countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
        HeadersFrame response = headersRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        DataFrame responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        String content = BufferUtil.toString(responseData.getData());
        // The upgrade request is seen as HTTP/1.1.
        assertThat(content, containsString("Hello from Jetty using HTTP/1.1"));
        assertThat(content, containsString("uri=/one"));
        // Send a HTTP/2 request.
        headersRef.set(null);
        dataRef.set(null);
        latchRef.set(new CountDownLatch(2));
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:" + connector.getLocalPort()), "/two", HttpVersion.HTTP_2, new HttpFields());
        generator.control(lease, new HeadersFrame(3, metaData, null, true));
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        output.flush();
        parseResponse(client, parser);
        Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
        response = headersRef.get();
        Assert.assertNotNull(response);
        responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        content = BufferUtil.toString(responseData.getData());
        assertThat(content, containsString("Hello from Jetty using HTTP/2.0"));
        assertThat(content, containsString("uri=/two"));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) InputStream(java.io.InputStream) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Parser(org.eclipse.jetty.http2.parser.Parser) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 45 with ByteBufferPool

use of org.eclipse.jetty.io.ByteBufferPool in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithPriorityWithContinuationFramesWithEmptyHeadersFrame.

@Test
public void testRequestWithPriorityWithContinuationFramesWithEmptyHeadersFrame() throws Exception {
    PriorityFrame priority = new PriorityFrame(1, 13, 200, true);
    testRequestWithContinuationFrames(null, () -> {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = newRequest("GET", new HttpFields());
        generator.control(lease, new HeadersFrame(1, metaData, priority, true));
        // Take the HeadersFrame header and set the length to just the priority frame.
        List<ByteBuffer> buffers = lease.getByteBuffers();
        ByteBuffer headersFrameHeader = buffers.get(2);
        headersFrameHeader.put(0, (byte) 0);
        headersFrameHeader.putShort(1, (short) PriorityFrame.PRIORITY_LENGTH);
        // Insert a CONTINUATION frame header for the body of the HEADERS frame.
        lease.insert(3, buffers.get(4).slice(), false);
        return lease;
    });
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HashMap(java.util.HashMap) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Test(org.junit.Test)

Aggregations

ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)72 ByteBuffer (java.nio.ByteBuffer)53 Test (org.junit.Test)51 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)40 Parser (org.eclipse.jetty.http2.parser.Parser)37 HttpFields (org.eclipse.jetty.http.HttpFields)29 MetaData (org.eclipse.jetty.http.MetaData)24 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)21 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)21 PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)20 SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)20 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)19 OutputStream (java.io.OutputStream)16 Socket (java.net.Socket)16 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)14 IOException (java.io.IOException)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 EndPoint (org.eclipse.jetty.io.EndPoint)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8