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);
}
}
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());
}
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());
}
}
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());
}
}
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));
}
}
Aggregations