Implemented parser and generator for PING frame.
This commit is contained in:
parent
6f3f7f5334
commit
81538c9b59
|
@ -20,7 +20,18 @@ package org.eclipse.jetty.http2.frames;
|
|||
|
||||
public enum FrameType
|
||||
{
|
||||
DATA(0), HEADERS(1), PRIORITY(2), RST_STREAM(3);
|
||||
DATA(0),
|
||||
HEADERS(1),
|
||||
PRIORITY(2),
|
||||
RST_STREAM(3),
|
||||
SETTINGS(4),
|
||||
PUSH_PROMISE(5),
|
||||
PING(6),
|
||||
GO_AWAY(7),
|
||||
WINDOW_UPDATE(8),
|
||||
CONTINUATION(9),
|
||||
ALTSVC(10),
|
||||
BLOCKED(11);
|
||||
|
||||
private final int type;
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.frames;
|
||||
|
||||
public class PingFrame
|
||||
{
|
||||
private final byte[] payload;
|
||||
private final boolean reply;
|
||||
|
||||
public PingFrame(byte[] payload, boolean reply)
|
||||
{
|
||||
this.payload = payload;
|
||||
this.reply = reply;
|
||||
}
|
||||
|
||||
public byte[] getPayload()
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
|
||||
public boolean isReply()
|
||||
{
|
||||
return reply;
|
||||
}
|
||||
}
|
|
@ -36,6 +36,20 @@ public class Generator
|
|||
this.byteBufferPool = byteBufferPool;
|
||||
}
|
||||
|
||||
public Result generatePing(byte[] payload, boolean reply)
|
||||
{
|
||||
Result result = new Result(byteBufferPool);
|
||||
|
||||
ByteBuffer header = generateHeader(FrameType.PING, 8, reply ? 0x01 : 0x00, 0);
|
||||
|
||||
header.put(payload);
|
||||
|
||||
BufferUtil.flipToFlush(header, 0);
|
||||
result.add(header, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Result generatePriority(int streamId, int dependentStreamId, int weight, boolean exclusive)
|
||||
{
|
||||
if (streamId < 0)
|
||||
|
|
|
@ -21,6 +21,7 @@ package org.eclipse.jetty.http2.parser;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.http2.frames.DataFrame;
|
||||
import org.eclipse.jetty.http2.frames.PingFrame;
|
||||
import org.eclipse.jetty.http2.frames.PriorityFrame;
|
||||
import org.eclipse.jetty.http2.frames.ResetFrame;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
|
@ -41,19 +42,24 @@ public abstract class BodyParser
|
|||
|
||||
public abstract Result parse(ByteBuffer buffer);
|
||||
|
||||
protected boolean hasFlag(int bit)
|
||||
{
|
||||
return headerParser.hasFlag(bit);
|
||||
}
|
||||
|
||||
protected boolean isPaddingHigh()
|
||||
{
|
||||
return headerParser.isPaddingHigh();
|
||||
return headerParser.hasFlag(0x10);
|
||||
}
|
||||
|
||||
protected boolean isPaddingLow()
|
||||
{
|
||||
return headerParser.isPaddingLow();
|
||||
return headerParser.hasFlag(0x8);
|
||||
}
|
||||
|
||||
protected boolean isEndStream()
|
||||
{
|
||||
return headerParser.isEndStream();
|
||||
return headerParser.hasFlag(0x1);
|
||||
}
|
||||
|
||||
protected int getStreamId()
|
||||
|
@ -75,7 +81,7 @@ public abstract class BodyParser
|
|||
{
|
||||
try
|
||||
{
|
||||
return listener.onDataFrame(frame);
|
||||
return listener.onData(frame);
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
|
@ -88,7 +94,7 @@ public abstract class BodyParser
|
|||
{
|
||||
try
|
||||
{
|
||||
return listener.onPriorityFrame(frame);
|
||||
return listener.onPriority(frame);
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
|
@ -101,7 +107,20 @@ public abstract class BodyParser
|
|||
{
|
||||
try
|
||||
{
|
||||
return listener.onResetFrame(frame);
|
||||
return listener.onReset(frame);
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
LOG.info("Failure while notifying listener " + listener, x);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean notifyPingFrame(PingFrame frame)
|
||||
{
|
||||
try
|
||||
{
|
||||
return listener.onPing(frame);
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
|
|
|
@ -121,19 +121,9 @@ public class HeaderParser
|
|||
return type;
|
||||
}
|
||||
|
||||
public boolean isPaddingHigh()
|
||||
public boolean hasFlag(int bit)
|
||||
{
|
||||
return (flags & 0x10) == 0x10;
|
||||
}
|
||||
|
||||
public boolean isPaddingLow()
|
||||
{
|
||||
return (flags & 0x08) == 0x08;
|
||||
}
|
||||
|
||||
public boolean isEndStream()
|
||||
{
|
||||
return (flags & 0x01) == 0x01;
|
||||
return (flags & bit) == bit;
|
||||
}
|
||||
|
||||
public int getStreamId()
|
||||
|
|
|
@ -22,13 +22,14 @@ import java.nio.ByteBuffer;
|
|||
|
||||
import org.eclipse.jetty.http2.frames.DataFrame;
|
||||
import org.eclipse.jetty.http2.frames.FrameType;
|
||||
import org.eclipse.jetty.http2.frames.PingFrame;
|
||||
import org.eclipse.jetty.http2.frames.PriorityFrame;
|
||||
import org.eclipse.jetty.http2.frames.ResetFrame;
|
||||
|
||||
public class Parser
|
||||
{
|
||||
private final HeaderParser headerParser = new HeaderParser();
|
||||
private final BodyParser[] bodyParsers = new BodyParser[4];
|
||||
private final BodyParser[] bodyParsers = new BodyParser[FrameType.values().length];
|
||||
private State state = State.HEADER;
|
||||
private BodyParser bodyParser;
|
||||
|
||||
|
@ -37,6 +38,7 @@ public class Parser
|
|||
bodyParsers[FrameType.DATA.getType()] = new DataBodyParser(headerParser, listener);
|
||||
bodyParsers[FrameType.PRIORITY.getType()] = new PriorityBodyParser(headerParser, listener);
|
||||
bodyParsers[FrameType.RST_STREAM.getType()] = new ResetBodyParser(headerParser, listener);
|
||||
bodyParsers[FrameType.PING.getType()] = new PingBodyParser(headerParser, listener);
|
||||
}
|
||||
|
||||
private void reset()
|
||||
|
@ -86,28 +88,36 @@ public class Parser
|
|||
|
||||
public interface Listener
|
||||
{
|
||||
public boolean onDataFrame(DataFrame frame);
|
||||
public boolean onData(DataFrame frame);
|
||||
|
||||
public boolean onPriorityFrame(PriorityFrame frame);
|
||||
public boolean onPriority(PriorityFrame frame);
|
||||
|
||||
public boolean onResetFrame(ResetFrame frame);
|
||||
public boolean onReset(ResetFrame frame);
|
||||
|
||||
public boolean onPing(PingFrame frame);
|
||||
|
||||
public static class Adapter implements Listener
|
||||
{
|
||||
@Override
|
||||
public boolean onDataFrame(DataFrame frame)
|
||||
public boolean onData(DataFrame frame)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPriorityFrame(PriorityFrame frame)
|
||||
public boolean onPriority(PriorityFrame frame)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onResetFrame(ResetFrame frame)
|
||||
public boolean onReset(ResetFrame frame)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPing(PingFrame frame)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.parser;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.http2.frames.PingFrame;
|
||||
|
||||
public class PingBodyParser extends BodyParser
|
||||
{
|
||||
private State state = State.PAYLOAD;
|
||||
private int cursor;
|
||||
private byte[] payload;
|
||||
|
||||
public PingBodyParser(HeaderParser headerParser, Parser.Listener listener)
|
||||
{
|
||||
super(headerParser, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset()
|
||||
{
|
||||
super.reset();
|
||||
state = State.PAYLOAD;
|
||||
cursor = 0;
|
||||
payload = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result parse(ByteBuffer buffer)
|
||||
{
|
||||
while (buffer.hasRemaining())
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case PAYLOAD:
|
||||
{
|
||||
payload = new byte[8];
|
||||
if (buffer.remaining() >= 8)
|
||||
{
|
||||
buffer.get(payload);
|
||||
return onPing(payload);
|
||||
}
|
||||
else
|
||||
{
|
||||
state = State.PAYLOAD_BYTES;
|
||||
cursor = 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PAYLOAD_BYTES:
|
||||
{
|
||||
payload[8 - cursor] = buffer.get();
|
||||
--cursor;
|
||||
if (cursor == 0)
|
||||
{
|
||||
return onPing(payload);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.PENDING;
|
||||
}
|
||||
|
||||
private Result onPing(byte[] payload)
|
||||
{
|
||||
PingFrame frame = new PingFrame(payload, hasFlag(0x1));
|
||||
reset();
|
||||
return notifyPingFrame(frame) ? Result.ASYNC : Result.COMPLETE;
|
||||
}
|
||||
|
||||
private enum State
|
||||
{
|
||||
PAYLOAD, PAYLOAD_BYTES
|
||||
}
|
||||
}
|
|
@ -124,7 +124,7 @@ public class DataGenerateParseTest
|
|||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onDataFrame(DataFrame frame)
|
||||
public boolean onData(DataFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
|
@ -152,7 +152,7 @@ public class DataGenerateParseTest
|
|||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onDataFrame(DataFrame frame)
|
||||
public boolean onData(DataFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.frames;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.eclipse.jetty.http2.generator.Generator;
|
||||
import org.eclipse.jetty.http2.parser.Parser;
|
||||
import org.eclipse.jetty.io.ByteBufferPool;
|
||||
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PingGenerateParseTest
|
||||
{
|
||||
private final ByteBufferPool byteBufferPool = new MappedByteBufferPool();
|
||||
|
||||
@Test
|
||||
public void testGenerateParse() throws Exception
|
||||
{
|
||||
Generator generator = new Generator(byteBufferPool);
|
||||
|
||||
byte[] payload = new byte[8];
|
||||
new Random().nextBytes(payload);
|
||||
|
||||
// Iterate a few times to be sure generator and parser are properly reset.
|
||||
final List<PingFrame> frames = new ArrayList<>();
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
Generator.Result result = generator.generatePing(payload, true);
|
||||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onPing(PingFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
frames.clear();
|
||||
for (ByteBuffer buffer : result.getByteBuffers())
|
||||
{
|
||||
while (buffer.hasRemaining())
|
||||
{
|
||||
parser.parse(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, frames.size());
|
||||
PingFrame frame = frames.get(0);
|
||||
Assert.assertArrayEquals(payload, frame.getPayload());
|
||||
Assert.assertTrue(frame.isReply());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateParseOneByteAtATime() throws Exception
|
||||
{
|
||||
Generator generator = new Generator(byteBufferPool);
|
||||
|
||||
byte[] payload = new byte[8];
|
||||
new Random().nextBytes(payload);
|
||||
|
||||
final List<PingFrame> frames = new ArrayList<>();
|
||||
Generator.Result result = generator.generatePing(payload, true);
|
||||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onPing(PingFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
for (ByteBuffer buffer : result.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());
|
||||
}
|
||||
}
|
|
@ -33,7 +33,7 @@ public class PriorityGenerateParseTest
|
|||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onPriorityFrame(PriorityFrame frame)
|
||||
public boolean onPriority(PriorityFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
|
@ -73,7 +73,7 @@ public class PriorityGenerateParseTest
|
|||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onPriorityFrame(PriorityFrame frame)
|
||||
public boolean onPriority(PriorityFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
|
|
|
@ -31,7 +31,7 @@ public class ResetGenerateParseTest
|
|||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onResetFrame(ResetFrame frame)
|
||||
public boolean onReset(ResetFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
|
@ -67,7 +67,7 @@ public class ResetGenerateParseTest
|
|||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onResetFrame(ResetFrame frame)
|
||||
public boolean onReset(ResetFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue