Search in sources :

Example 1 with PingFrame

use of org.eclipse.jetty.http2.frames.PingFrame in project jetty.project by eclipse.

the class HTTP2Session method onPing.

@Override
public void onPing(PingFrame frame) {
    if (LOG.isDebugEnabled())
        LOG.debug("Received {}", frame);
    if (frame.isReply()) {
        notifyPing(this, frame);
    } else {
        PingFrame reply = new PingFrame(frame.getPayload(), true);
        control(null, Callback.NOOP, reply);
    }
}
Also used : PingFrame(org.eclipse.jetty.http2.frames.PingFrame)

Example 2 with PingFrame

use of org.eclipse.jetty.http2.frames.PingFrame in project jetty.project by eclipse.

the class PingGenerateParseTest method testPayloadAsLong.

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

        @Override
        public void onPing(PingFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    PingFrame ping = new PingFrame(System.nanoTime(), true);
    generator.generate(lease, ping);
    for (ByteBuffer buffer : lease.getByteBuffers()) {
        while (buffer.hasRemaining()) {
            parser.parse(buffer);
        }
    }
    Assert.assertEquals(1, frames.size());
    PingFrame pong = frames.get(0);
    Assert.assertEquals(ping.getPayloadAsLong(), pong.getPayloadAsLong());
    Assert.assertTrue(pong.isReply());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ArrayList(java.util.ArrayList) PingGenerator(org.eclipse.jetty.http2.generator.PingGenerator) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Example 3 with PingFrame

use of org.eclipse.jetty.http2.frames.PingFrame in project jetty.project by eclipse.

the class PingGenerateParseTest method testGenerateParseOneByteAtATime.

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

        @Override
        public void onPing(PingFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    byte[] payload = new byte[8];
    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.generatePing(lease, payload, true);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        PingFrame frame = frames.get(0);
        Assert.assertArrayEquals(payload, frame.getPayload());
        Assert.assertTrue(frame.isReply());
    }
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ArrayList(java.util.ArrayList) PingGenerator(org.eclipse.jetty.http2.generator.PingGenerator) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) Random(java.util.Random) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Example 4 with PingFrame

use of org.eclipse.jetty.http2.frames.PingFrame in project jetty.project by eclipse.

the class PrefaceTest method testClientPrefaceReplySentAfterServerPreface.

@Test
public void testClientPrefaceReplySentAfterServerPreface() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 128);
            return settings;
        }

        @Override
        public void onPing(Session session, PingFrame frame) {
            session.close(ErrorCode.NO_ERROR.code, null, Callback.NOOP);
        }
    });
    ByteBufferPool byteBufferPool = client.getByteBufferPool();
    try (SocketChannel socket = SocketChannel.open()) {
        socket.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
        Generator generator = new Generator(byteBufferPool);
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        Map<Integer, Integer> clientSettings = new HashMap<>();
        clientSettings.put(SettingsFrame.ENABLE_PUSH, 0);
        generator.control(lease, new SettingsFrame(clientSettings, false));
        // The PING frame just to make sure the client stops reading.
        generator.control(lease, new PingFrame(true));
        List<ByteBuffer> buffers = lease.getByteBuffers();
        socket.write(buffers.toArray(new ByteBuffer[buffers.size()]));
        Queue<SettingsFrame> settings = new ArrayDeque<>();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                settings.offer(frame);
            }
        }, 4096, 8192);
        ByteBuffer buffer = byteBufferPool.acquire(1024, true);
        while (true) {
            BufferUtil.clearToFill(buffer);
            int read = socket.read(buffer);
            BufferUtil.flipToFlush(buffer, 0);
            if (read < 0)
                break;
            parser.parse(buffer);
        }
        Assert.assertEquals(2, settings.size());
        SettingsFrame frame1 = settings.poll();
        Assert.assertFalse(frame1.isReply());
        SettingsFrame frame2 = settings.poll();
        Assert.assertTrue(frame2.isReply());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SocketChannel(java.nio.channels.SocketChannel) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) ByteBuffer(java.nio.ByteBuffer) ArrayDeque(java.util.ArrayDeque) EndPoint(org.eclipse.jetty.io.EndPoint) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) HashMap(java.util.HashMap) Map(java.util.Map) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 5 with PingFrame

use of org.eclipse.jetty.http2.frames.PingFrame in project jetty.project by eclipse.

the class HTTP2ServerTest method testBadPingWrongPayload.

@Test
public void testBadPingWrongPayload() throws Exception {
    startServer(new HttpServlet() {
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    generator.control(lease, new PingFrame(new byte[8], false));
    // Modify the length of the frame to a wrong one.
    lease.getByteBuffers().get(2).putShort(0, (short) 7);
    final CountDownLatch latch = new CountDownLatch(1);
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onGoAway(GoAwayFrame frame) {
                Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR.code, frame.getError());
                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) HashMap(java.util.HashMap) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) 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) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 ByteBuffer (java.nio.ByteBuffer)6 PingFrame (org.eclipse.jetty.http2.frames.PingFrame)6 Parser (org.eclipse.jetty.http2.parser.Parser)6 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)6 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)4 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)3 SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)3 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)3 PingGenerator (org.eclipse.jetty.http2.generator.PingGenerator)3 OutputStream (java.io.OutputStream)2 Socket (java.net.Socket)2 HttpServlet (javax.servlet.http.HttpServlet)2 Session (org.eclipse.jetty.http2.api.Session)2 GoAwayFrame (org.eclipse.jetty.http2.frames.GoAwayFrame)2