Implemented PING functionality.

This commit is contained in:
Simone Bordet 2014-06-13 17:59:17 +02:00
parent 8681511f08
commit b3aa67e0a9
4 changed files with 172 additions and 3 deletions

View File

@ -0,0 +1,58 @@
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http2.client;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.http2.api.Session;
import org.eclipse.jetty.http2.api.server.ServerSessionListener;
import org.eclipse.jetty.http2.frames.PingFrame;
import org.eclipse.jetty.util.Callback;
import org.junit.Assert;
import org.junit.Test;
public class PingTest extends AbstractTest
{
@Test
public void testPing() throws Exception
{
startServer(new ServerSessionListener.Adapter());
final byte[] payload = new byte[8];
new Random().nextBytes(payload);
final CountDownLatch latch = new CountDownLatch(1);
Session client = newClient(new Session.Listener.Adapter()
{
@Override
public void onPing(Session session, PingFrame frame)
{
Assert.assertTrue(frame.isReply());
Assert.assertArrayEquals(payload, frame.getPayload());
latch.countDown();
}
});
PingFrame frame = new PingFrame(payload, false);
client.ping(frame, Callback.Adapter.INSTANCE);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}

View File

@ -174,6 +174,15 @@ public abstract class HTTP2Session implements ISession, Parser.Listener
@Override @Override
public boolean onPing(PingFrame frame) public boolean onPing(PingFrame frame)
{ {
if (frame.isReply())
{
notifyPing(this, frame);
}
else
{
PingFrame reply = new PingFrame(frame.getPayload(), true);
ping(reply, disconnectCallback);
}
return false; return false;
} }
@ -263,7 +272,10 @@ public abstract class HTTP2Session implements ISession, Parser.Listener
@Override @Override
public void ping(PingFrame frame, Callback callback) public void ping(PingFrame frame, Callback callback)
{ {
control(null, frame, callback); if (frame.isReply())
callback.failed(new IllegalArgumentException());
else
control(null, frame, callback);
} }
@Override @Override
@ -438,6 +450,18 @@ public abstract class HTTP2Session implements ISession, Parser.Listener
} }
} }
protected void notifyPing(Session session, PingFrame frame)
{
try
{
listener.onPing(session, frame);
}
catch (Throwable x)
{
LOG.info("Failure while notifying listener " + listener, x);
}
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -50,11 +50,16 @@ public class PingBodyParser extends BodyParser
{ {
case PREPARE: case PREPARE:
{ {
int length = getBodyLength(); // SPEC: wrong streamId is treated as connection error.
if (length != 8) if (getStreamId() != 0)
{ {
return notifyConnectionFailure(ErrorCode.PROTOCOL_ERROR, "invalid_ping_frame"); return notifyConnectionFailure(ErrorCode.PROTOCOL_ERROR, "invalid_ping_frame");
} }
// SPEC: wrong body length is treated as connection error.
if (getBodyLength() != 8)
{
return notifyConnectionFailure(ErrorCode.FRAME_SIZE_ERROR, "invalid_ping_frame");
}
state = State.PAYLOAD; state = State.PAYLOAD;
break; break;
} }

View File

@ -40,12 +40,14 @@ import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http2.frames.DataFrame; import org.eclipse.jetty.http2.frames.DataFrame;
import org.eclipse.jetty.http2.frames.GoAwayFrame; import org.eclipse.jetty.http2.frames.GoAwayFrame;
import org.eclipse.jetty.http2.frames.HeadersFrame; import org.eclipse.jetty.http2.frames.HeadersFrame;
import org.eclipse.jetty.http2.frames.PingFrame;
import org.eclipse.jetty.http2.frames.SettingsFrame; import org.eclipse.jetty.http2.frames.SettingsFrame;
import org.eclipse.jetty.http2.generator.Generator; import org.eclipse.jetty.http2.generator.Generator;
import org.eclipse.jetty.http2.hpack.HpackContext; import org.eclipse.jetty.http2.hpack.HpackContext;
import org.eclipse.jetty.http2.hpack.HpackDecoder; import org.eclipse.jetty.http2.hpack.HpackDecoder;
import org.eclipse.jetty.http2.hpack.HpackEncoder; import org.eclipse.jetty.http2.hpack.HpackEncoder;
import org.eclipse.jetty.http2.hpack.MetaData; import org.eclipse.jetty.http2.hpack.MetaData;
import org.eclipse.jetty.http2.parser.ErrorCode;
import org.eclipse.jetty.http2.parser.Parser; import org.eclipse.jetty.http2.parser.Parser;
import org.eclipse.jetty.http2.parser.PrefaceParser; import org.eclipse.jetty.http2.parser.PrefaceParser;
import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ByteBufferPool;
@ -271,6 +273,86 @@ public class HTTP2ServerTest
} }
} }
@Test
public void testBadPingWrongPayload() throws Exception
{
startServer(new HttpServlet(){});
String host = "localhost";
int port = connector.getLocalPort();
PingFrame frame = new PingFrame(new byte[8], false);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, frame);
// Modify the length of the frame to a wrong one.
lease.getByteBuffers().get(0).putShort(0, (short)7);
lease.prepend(ByteBuffer.wrap(PrefaceParser.PREFACE_BYTES), false);
final CountDownLatch latch = new CountDownLatch(1);
try (Socket client = new Socket(host, port))
{
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 boolean onGoAway(GoAwayFrame frame)
{
Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR, frame.getError());
latch.countDown();
return false;
}
});
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
@Test
public void testBadPingWrongStreamId() throws Exception
{
startServer(new HttpServlet(){});
String host = "localhost";
int port = connector.getLocalPort();
PingFrame frame = new PingFrame(new byte[8], false);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, frame);
// Modify the streamId of the frame to non zero.
lease.getByteBuffers().get(0).putInt(4, 1);
lease.prepend(ByteBuffer.wrap(PrefaceParser.PREFACE_BYTES), false);
final CountDownLatch latch = new CountDownLatch(1);
try (Socket client = new Socket(host, port))
{
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 boolean onGoAway(GoAwayFrame frame)
{
Assert.assertEquals(ErrorCode.PROTOCOL_ERROR, frame.getError());
latch.countDown();
return false;
}
});
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
private void parseResponse(Socket client, Parser parser) throws IOException private void parseResponse(Socket client, Parser parser) throws IOException
{ {
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];