Implemented parser and generator for WINDOW_UPDATE frame.
This commit is contained in:
parent
7a347e267f
commit
54577057bb
|
@ -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 WindowUpdateFrame
|
||||
{
|
||||
private final int streamId;
|
||||
private final int windowDelta;
|
||||
|
||||
public WindowUpdateFrame(int streamId, int windowDelta)
|
||||
{
|
||||
this.streamId = streamId;
|
||||
this.windowDelta = windowDelta;
|
||||
}
|
||||
|
||||
public int getStreamId()
|
||||
{
|
||||
return streamId;
|
||||
}
|
||||
|
||||
public int getWindowDelta()
|
||||
{
|
||||
return windowDelta;
|
||||
}
|
||||
}
|
|
@ -36,44 +36,44 @@ public class Generator
|
|||
this.byteBufferPool = byteBufferPool;
|
||||
}
|
||||
|
||||
public Result generateGoAway(int lastStreamId, int error, byte[] payload)
|
||||
public Result generateData(int streamId, int paddingLength, ByteBuffer data, boolean last, boolean compress)
|
||||
{
|
||||
if (lastStreamId < 0)
|
||||
throw new IllegalArgumentException("Invalid last stream id: " + lastStreamId);
|
||||
if (streamId < 0)
|
||||
throw new IllegalArgumentException("Invalid stream id: " + streamId);
|
||||
// Leave space for at least one byte of content.
|
||||
if (paddingLength > DataFrame.MAX_LENGTH - 3)
|
||||
throw new IllegalArgumentException("Invalid padding length: " + paddingLength);
|
||||
|
||||
int paddingBytes = paddingLength > 0xFF ? 2 : paddingLength > 0 ? 1 : 0;
|
||||
|
||||
// TODO: here we should compress the data, and then reason on the data length !
|
||||
|
||||
int dataLength = data.remaining();
|
||||
|
||||
Result result = new Result(byteBufferPool);
|
||||
|
||||
int length = 4 + 4 + (payload != null ? payload.length : 0);
|
||||
ByteBuffer header = generateHeader(FrameType.GO_AWAY, length, 0, 0);
|
||||
|
||||
header.putInt(lastStreamId);
|
||||
header.putInt(error);
|
||||
|
||||
if (payload != null)
|
||||
// Can we fit just one frame ?
|
||||
if (dataLength + paddingBytes + paddingLength <= DataFrame.MAX_LENGTH)
|
||||
{
|
||||
header.put(payload);
|
||||
generateData(result, streamId, paddingBytes, paddingLength, data, last, compress);
|
||||
}
|
||||
else
|
||||
{
|
||||
int dataBytesPerFrame = DataFrame.MAX_LENGTH - paddingBytes - paddingLength;
|
||||
int frames = dataLength / dataBytesPerFrame;
|
||||
if (frames * dataBytesPerFrame != dataLength)
|
||||
{
|
||||
++frames;
|
||||
}
|
||||
int limit = data.limit();
|
||||
for (int i = 1; i <= frames; ++i)
|
||||
{
|
||||
data.limit(Math.min(dataBytesPerFrame * i, limit));
|
||||
ByteBuffer slice = data.slice();
|
||||
data.position(data.limit());
|
||||
generateData(result, streamId, paddingBytes, paddingLength, slice, i == frames && last, compress);
|
||||
}
|
||||
}
|
||||
|
||||
BufferUtil.flipToFlush(header, 0);
|
||||
result.add(header, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Result generatePing(byte[] payload, boolean reply)
|
||||
{
|
||||
if (payload.length != 8)
|
||||
throw new IllegalArgumentException("Invalid payload length: " + payload.length);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -118,44 +118,63 @@ public class Generator
|
|||
return result;
|
||||
}
|
||||
|
||||
public Result generateData(int streamId, int paddingLength, ByteBuffer data, boolean last, boolean compress)
|
||||
public Result generatePing(byte[] payload, boolean reply)
|
||||
{
|
||||
if (streamId < 0)
|
||||
throw new IllegalArgumentException("Invalid stream id: " + streamId);
|
||||
// Leave space for at least one byte of content.
|
||||
if (paddingLength > DataFrame.MAX_LENGTH - 3)
|
||||
throw new IllegalArgumentException("Invalid padding length: " + paddingLength);
|
||||
|
||||
int paddingBytes = paddingLength > 0xFF ? 2 : paddingLength > 0 ? 1 : 0;
|
||||
|
||||
// TODO: here we should compress the data, and then reason on the data length !
|
||||
|
||||
int dataLength = data.remaining();
|
||||
if (payload.length != 8)
|
||||
throw new IllegalArgumentException("Invalid payload length: " + payload.length);
|
||||
|
||||
Result result = new Result(byteBufferPool);
|
||||
|
||||
// Can we fit just one frame ?
|
||||
if (dataLength + paddingBytes + paddingLength <= DataFrame.MAX_LENGTH)
|
||||
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 generateGoAway(int lastStreamId, int error, byte[] payload)
|
||||
{
|
||||
if (lastStreamId < 0)
|
||||
throw new IllegalArgumentException("Invalid last stream id: " + lastStreamId);
|
||||
|
||||
Result result = new Result(byteBufferPool);
|
||||
|
||||
int length = 4 + 4 + (payload != null ? payload.length : 0);
|
||||
ByteBuffer header = generateHeader(FrameType.GO_AWAY, length, 0, 0);
|
||||
|
||||
header.putInt(lastStreamId);
|
||||
header.putInt(error);
|
||||
|
||||
if (payload != null)
|
||||
{
|
||||
generateData(result, streamId, paddingBytes, paddingLength, data, last, compress);
|
||||
}
|
||||
else
|
||||
{
|
||||
int dataBytesPerFrame = DataFrame.MAX_LENGTH - paddingBytes - paddingLength;
|
||||
int frames = dataLength / dataBytesPerFrame;
|
||||
if (frames * dataBytesPerFrame != dataLength)
|
||||
{
|
||||
++frames;
|
||||
}
|
||||
int limit = data.limit();
|
||||
for (int i = 1; i <= frames; ++i)
|
||||
{
|
||||
data.limit(Math.min(dataBytesPerFrame * i, limit));
|
||||
ByteBuffer slice = data.slice();
|
||||
data.position(data.limit());
|
||||
generateData(result, streamId, paddingBytes, paddingLength, slice, i == frames && last, compress);
|
||||
}
|
||||
header.put(payload);
|
||||
}
|
||||
|
||||
BufferUtil.flipToFlush(header, 0);
|
||||
result.add(header, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Result generateWindowUpdate(int streamId, int windowUpdate)
|
||||
{
|
||||
if (streamId < 0)
|
||||
throw new IllegalArgumentException("Invalid stream id: " + streamId);
|
||||
if (windowUpdate < 0)
|
||||
throw new IllegalArgumentException("Invalid window update: " + windowUpdate);
|
||||
|
||||
Result result = new Result(byteBufferPool);
|
||||
|
||||
ByteBuffer header = generateHeader(FrameType.WINDOW_UPDATE, 4, 0, streamId);
|
||||
|
||||
header.putInt(windowUpdate);
|
||||
|
||||
BufferUtil.flipToFlush(header, 0);
|
||||
result.add(header, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.eclipse.jetty.http2.frames.GoAwayFrame;
|
|||
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.http2.frames.WindowUpdateFrame;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
|
@ -143,6 +144,19 @@ public abstract class BodyParser
|
|||
}
|
||||
}
|
||||
|
||||
protected boolean notifyWindowUpdate(WindowUpdateFrame frame)
|
||||
{
|
||||
try
|
||||
{
|
||||
return listener.onWindowUpdate(frame);
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
LOG.info("Failure while notifying listener " + listener, x);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Result
|
||||
{
|
||||
PENDING, ASYNC, COMPLETE
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.eclipse.jetty.http2.frames.GoAwayFrame;
|
|||
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.http2.frames.WindowUpdateFrame;
|
||||
|
||||
public class Parser
|
||||
{
|
||||
|
@ -41,6 +42,7 @@ public class Parser
|
|||
bodyParsers[FrameType.RST_STREAM.getType()] = new ResetBodyParser(headerParser, listener);
|
||||
bodyParsers[FrameType.PING.getType()] = new PingBodyParser(headerParser, listener);
|
||||
bodyParsers[FrameType.GO_AWAY.getType()] = new GoAwayBodyParser(headerParser, listener);
|
||||
bodyParsers[FrameType.WINDOW_UPDATE.getType()] = new WindowUpdateBodyParser(headerParser, listener);
|
||||
}
|
||||
|
||||
private void reset()
|
||||
|
@ -100,6 +102,8 @@ public class Parser
|
|||
|
||||
public boolean onGoAway(GoAwayFrame frame);
|
||||
|
||||
public boolean onWindowUpdate(WindowUpdateFrame frame);
|
||||
|
||||
public static class Adapter implements Listener
|
||||
{
|
||||
@Override
|
||||
|
@ -131,6 +135,12 @@ public class Parser
|
|||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onWindowUpdate(WindowUpdateFrame frame)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.WindowUpdateFrame;
|
||||
|
||||
public class WindowUpdateBodyParser extends BodyParser
|
||||
{
|
||||
private State state = State.WINDOW_DELTA;
|
||||
private int cursor;
|
||||
private int windowDelta;
|
||||
|
||||
public WindowUpdateBodyParser(HeaderParser headerParser, Parser.Listener listener)
|
||||
{
|
||||
super(headerParser, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset()
|
||||
{
|
||||
super.reset();
|
||||
state = State.WINDOW_DELTA;
|
||||
cursor = 0;
|
||||
windowDelta = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result parse(ByteBuffer buffer)
|
||||
{
|
||||
while (buffer.hasRemaining())
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case WINDOW_DELTA:
|
||||
{
|
||||
if (buffer.remaining() >= 4)
|
||||
{
|
||||
windowDelta = buffer.getInt() & 0x7F_FF_FF_FF;
|
||||
return onWindowUpdate(windowDelta);
|
||||
}
|
||||
else
|
||||
{
|
||||
state = State.WINDOW_DELTA_BYTES;
|
||||
cursor = 4;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WINDOW_DELTA_BYTES:
|
||||
{
|
||||
byte currByte = buffer.get();
|
||||
--cursor;
|
||||
windowDelta += (currByte & 0xFF) << 8 * cursor;
|
||||
if (cursor == 0)
|
||||
{
|
||||
windowDelta &= 0x7F_FF_FF_FF;
|
||||
return onWindowUpdate(windowDelta);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.PENDING;
|
||||
}
|
||||
|
||||
private Result onWindowUpdate(int windowDelta)
|
||||
{
|
||||
WindowUpdateFrame frame = new WindowUpdateFrame(getStreamId(), windowDelta);
|
||||
reset();
|
||||
return notifyWindowUpdate(frame) ? Result.ASYNC : Result.COMPLETE;
|
||||
}
|
||||
|
||||
private enum State
|
||||
{
|
||||
WINDOW_DELTA, WINDOW_DELTA_BYTES
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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 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 WindowUpdateGenerateParseTest
|
||||
{
|
||||
private final ByteBufferPool byteBufferPool = new MappedByteBufferPool();
|
||||
|
||||
@Test
|
||||
public void testGenerateParse() throws Exception
|
||||
{
|
||||
Generator generator = new Generator(byteBufferPool);
|
||||
|
||||
int streamId = 13;
|
||||
int windowUpdate = 17;
|
||||
|
||||
// Iterate a few times to be sure generator and parser are properly reset.
|
||||
final List<WindowUpdateFrame> frames = new ArrayList<>();
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
Generator.Result result = generator.generateWindowUpdate(streamId, windowUpdate);
|
||||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onWindowUpdate(WindowUpdateFrame frame)
|
||||
{
|
||||
frames.add(frame);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
frames.clear();
|
||||
for (ByteBuffer buffer : result.getByteBuffers())
|
||||
{
|
||||
while (buffer.hasRemaining())
|
||||
{
|
||||
parser.parse(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, frames.size());
|
||||
WindowUpdateFrame frame = frames.get(0);
|
||||
Assert.assertEquals(streamId, frame.getStreamId());
|
||||
Assert.assertEquals(windowUpdate, frame.getWindowDelta());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateParseOneByteAtATime() throws Exception
|
||||
{
|
||||
Generator generator = new Generator(byteBufferPool);
|
||||
|
||||
int streamId = 13;
|
||||
int windowUpdate = 17;
|
||||
|
||||
final List<WindowUpdateFrame> frames = new ArrayList<>();
|
||||
Generator.Result result = generator.generateWindowUpdate(streamId, windowUpdate);
|
||||
Parser parser = new Parser(new Parser.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public boolean onWindowUpdate(WindowUpdateFrame 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());
|
||||
WindowUpdateFrame frame = frames.get(0);
|
||||
Assert.assertEquals(streamId, frame.getStreamId());
|
||||
Assert.assertEquals(windowUpdate, frame.getWindowDelta());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue