Search in sources :

Example 36 with Parser

use of org.eclipse.jetty.http2.parser.Parser in project jetty.project by eclipse.

the class HTTP2ServerTest method testNonISOHeader.

@Test
public void testNonISOHeader() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        startServer(new HttpServlet() {

            @Override
            protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // Invalid header name, the connection must be closed.
                response.setHeader("Euro_(€)", "42");
            }
        });
        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, null, true));
        try (Socket client = new Socket("localhost", connector.getLocalPort())) {
            OutputStream output = client.getOutputStream();
            for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
            output.flush();
            Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
            boolean closed = parseResponse(client, parser);
            Assert.assertTrue(closed);
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Socket(java.net.Socket) Test(org.junit.Test)

Example 37 with Parser

use of org.eclipse.jetty.http2.parser.Parser in project jetty.project by eclipse.

the class HTTP2ServerTest method testNoPrefaceBytes.

@Test
public void testNoPrefaceBytes() throws Exception {
    startServer(new HttpServlet() {
    });
    // No preface bytes.
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    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 CountDownLatch latch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onGoAway(GoAwayFrame frame) {
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Socket(java.net.Socket) Test(org.junit.Test)

Example 38 with Parser

use of org.eclipse.jetty.http2.parser.Parser in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestResponseContent.

@Test
public void testRequestResponseContent() throws Exception {
    final byte[] content = "Hello, world!".getBytes(StandardCharsets.UTF_8);
    final CountDownLatch latch = new CountDownLatch(4);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            latch.countDown();
            resp.getOutputStream().write(content);
        }
    });
    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, 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);
        Assert.assertArrayEquals(content, BufferUtil.toArray(responseData.getData()));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Socket(java.net.Socket) Test(org.junit.Test)

Example 39 with Parser

use of org.eclipse.jetty.http2.parser.Parser in project jetty.project by eclipse.

the class CloseTest method testServerSendsGoAwayClientDoesNotCloseServerIdleTimeout.

@Test
public void testServerSendsGoAwayClientDoesNotCloseServerIdleTimeout() throws Exception {
    final long idleTimeout = 1000;
    final AtomicReference<Session> sessionRef = new AtomicReference<>();
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            sessionRef.set(stream.getSession());
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            stream.getSession().close(ErrorCode.NO_ERROR.code, "OK", Callback.NOOP);
            return null;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    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, 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 CountDownLatch responseLatch = new CountDownLatch(1);
        final CountDownLatch closeLatch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                responseLatch.countDown();
            }

            @Override
            public void onGoAway(GoAwayFrame frame) {
                closeLatch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
        // Don't close the connection.
        // Wait for the server to idle timeout.
        Thread.sleep(2 * idleTimeout);
        // Client received the TCP FIN from server.
        Assert.assertEquals(-1, client.getInputStream().read());
        // Server is closed.
        Session session = sessionRef.get();
        Assert.assertTrue(session.isClosed());
        Assert.assertTrue(((HTTP2Session) session).isDisconnected());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) 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) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 40 with Parser

use of org.eclipse.jetty.http2.parser.Parser in project jetty.project by eclipse.

the class GoAwayGenerateParseTest method testGenerateParseOneByteAtATime.

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

        @Override
        public void onGoAway(GoAwayFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int lastStreamId = 13;
    int error = 17;
    byte[] payload = new byte[16];
    new Random().nextBytes(payload);
    // 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.generateGoAway(lease, lastStreamId, error, payload);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        GoAwayFrame frame = frames.get(0);
        Assert.assertEquals(lastStreamId, frame.getLastStreamId());
        Assert.assertEquals(error, frame.getError());
        Assert.assertArrayEquals(payload, frame.getPayload());
    }
}
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) Random(java.util.Random) GoAwayGenerator(org.eclipse.jetty.http2.generator.GoAwayGenerator) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)38 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)38 Parser (org.eclipse.jetty.http2.parser.Parser)37 Test (org.junit.Test)35 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)26 ArrayList (java.util.ArrayList)23 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)21 HttpFields (org.eclipse.jetty.http.HttpFields)18 MetaData (org.eclipse.jetty.http.MetaData)18 HashMap (java.util.HashMap)16 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)15 SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)15 OutputStream (java.io.OutputStream)14 Socket (java.net.Socket)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)13 PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Generator (org.eclipse.jetty.http2.generator.Generator)8 HttpServlet (javax.servlet.http.HttpServlet)7