Merge branch 'jetty-9.1' of ssh://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project into jetty-9.1
This commit is contained in:
commit
b884fabba6
|
@ -489,7 +489,8 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
|
||||||
@Override
|
@Override
|
||||||
public void onStreamException(StreamException x)
|
public void onStreamException(StreamException x)
|
||||||
{
|
{
|
||||||
notifyOnException(listener, x);
|
// TODO: rename to onFailure
|
||||||
|
notifyOnException(listener, x); //TODO: notify StreamFrameListener if exists?
|
||||||
rst(new RstInfo(x.getStreamId(), x.getStreamStatus()), new Callback.Adapter());
|
rst(new RstInfo(x.getStreamId(), x.getStreamStatus()), new Callback.Adapter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -804,7 +805,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
|
||||||
if (listener != null)
|
if (listener != null)
|
||||||
{
|
{
|
||||||
LOG.debug("Invoking callback with {} on listener {}", x, listener);
|
LOG.debug("Invoking callback with {} on listener {}", x, listener);
|
||||||
listener.onException(x);
|
listener.onFailure(this, x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception xx)
|
catch (Exception xx)
|
||||||
|
|
|
@ -111,7 +111,9 @@ public class StandardStream extends IdleTimeout implements IStream
|
||||||
@Override
|
@Override
|
||||||
protected void onIdleExpired(TimeoutException timeout)
|
protected void onIdleExpired(TimeoutException timeout)
|
||||||
{
|
{
|
||||||
listener.onFailure(timeout);
|
StreamFrameListener listener = this.listener;
|
||||||
|
if (listener != null)
|
||||||
|
listener.onFailure(this, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -115,9 +115,10 @@ public interface SessionFrameListener extends EventListener
|
||||||
* SPDY session.</p>
|
* SPDY session.</p>
|
||||||
* <p>Examples of such conditions are invalid frames received, corrupted headers compression state, etc.</p>
|
* <p>Examples of such conditions are invalid frames received, corrupted headers compression state, etc.</p>
|
||||||
*
|
*
|
||||||
|
* @param session the session
|
||||||
* @param x the exception that caused the event processing failure
|
* @param x the exception that caused the event processing failure
|
||||||
*/
|
*/
|
||||||
public void onException(Throwable x);
|
public void onFailure(Session session, Throwable x);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -154,7 +155,7 @@ public interface SessionFrameListener extends EventListener
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onException(Throwable x)
|
public void onFailure(Session session, Throwable x)
|
||||||
{
|
{
|
||||||
logger.info("", x);
|
logger.info("", x);
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ public interface StreamFrameListener extends EventListener
|
||||||
* <p>Callback invoked when a push syn has been received on a stream.</p>
|
* <p>Callback invoked when a push syn has been received on a stream.</p>
|
||||||
*
|
*
|
||||||
* @param stream the push stream just created
|
* @param stream the push stream just created
|
||||||
* @param pushInfo
|
* @param pushInfo the push metadata
|
||||||
* @return a listener for stream events or null if there is no interest in being notified of stream events
|
* @return a listener for stream events or null if there is no interest in being notified of stream events
|
||||||
*/
|
*/
|
||||||
public StreamFrameListener onPush(Stream stream, PushInfo pushInfo);
|
public StreamFrameListener onPush(Stream stream, PushInfo pushInfo);
|
||||||
|
@ -71,9 +71,10 @@ public interface StreamFrameListener extends EventListener
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Callback invoked on errors.</p>
|
* <p>Callback invoked on errors.</p>
|
||||||
* @param x
|
* @param stream the stream
|
||||||
|
* @param x the failure
|
||||||
*/
|
*/
|
||||||
public void onFailure(Throwable x);
|
public void onFailure(Stream stream, Throwable x);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Empty implementation of {@link StreamFrameListener}</p>
|
* <p>Empty implementation of {@link StreamFrameListener}</p>
|
||||||
|
@ -102,7 +103,7 @@ public interface StreamFrameListener extends EventListener
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,8 @@ public abstract class ControlFrameParser
|
||||||
private short type;
|
private short type;
|
||||||
private byte flags;
|
private byte flags;
|
||||||
private int length;
|
private int length;
|
||||||
private ControlFrameBodyParser parser;
|
private ControlFrameBodyParser bodyParser;
|
||||||
|
private int bytesToSkip = 0;
|
||||||
|
|
||||||
public ControlFrameParser(CompressionFactory.Decompressor decompressor)
|
public ControlFrameParser(CompressionFactory.Decompressor decompressor)
|
||||||
{
|
{
|
||||||
|
@ -66,6 +67,12 @@ public abstract class ControlFrameParser
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void skip(int bytesToSkip)
|
||||||
|
{
|
||||||
|
state = State.SKIP;
|
||||||
|
this.bytesToSkip = bytesToSkip;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean parse(ByteBuffer buffer)
|
public boolean parse(ByteBuffer buffer)
|
||||||
{
|
{
|
||||||
while (buffer.hasRemaining())
|
while (buffer.hasRemaining())
|
||||||
|
@ -140,9 +147,9 @@ public abstract class ControlFrameParser
|
||||||
|
|
||||||
// SPEC v3, 2.2.1: unrecognized control frames must be ignored
|
// SPEC v3, 2.2.1: unrecognized control frames must be ignored
|
||||||
if (controlFrameType == null)
|
if (controlFrameType == null)
|
||||||
parser = unknownParser;
|
bodyParser = unknownParser;
|
||||||
else
|
else
|
||||||
parser = parsers.get(controlFrameType);
|
bodyParser = parsers.get(controlFrameType);
|
||||||
|
|
||||||
state = State.BODY;
|
state = State.BODY;
|
||||||
|
|
||||||
|
@ -153,13 +160,29 @@ public abstract class ControlFrameParser
|
||||||
}
|
}
|
||||||
case BODY:
|
case BODY:
|
||||||
{
|
{
|
||||||
if (parser.parse(buffer))
|
if (bodyParser.parse(buffer))
|
||||||
{
|
{
|
||||||
reset();
|
reset();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case SKIP:
|
||||||
|
{
|
||||||
|
int remaining = buffer.remaining();
|
||||||
|
if (remaining >= bytesToSkip)
|
||||||
|
{
|
||||||
|
buffer.position(buffer.position() + bytesToSkip);
|
||||||
|
reset();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer.position(buffer.limit());
|
||||||
|
bytesToSkip = bytesToSkip - remaining;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
|
@ -169,7 +192,7 @@ public abstract class ControlFrameParser
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reset()
|
void reset()
|
||||||
{
|
{
|
||||||
state = State.VERSION;
|
state = State.VERSION;
|
||||||
cursor = 0;
|
cursor = 0;
|
||||||
|
@ -177,13 +200,14 @@ public abstract class ControlFrameParser
|
||||||
type = 0;
|
type = 0;
|
||||||
flags = 0;
|
flags = 0;
|
||||||
length = 0;
|
length = 0;
|
||||||
parser = null;
|
bodyParser = null;
|
||||||
|
bytesToSkip = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void onControlFrame(ControlFrame frame);
|
protected abstract void onControlFrame(ControlFrame frame);
|
||||||
|
|
||||||
private enum State
|
private enum State
|
||||||
{
|
{
|
||||||
VERSION, VERSION_BYTES, TYPE, TYPE_BYTES, FLAGS, LENGTH, BODY
|
VERSION, VERSION_BYTES, TYPE, TYPE_BYTES, FLAGS, LENGTH, BODY, SKIP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,14 @@ public class Parser
|
||||||
{
|
{
|
||||||
for (Listener listener : listeners)
|
for (Listener listener : listeners)
|
||||||
{
|
{
|
||||||
listener.onStreamException(x);
|
try
|
||||||
|
{
|
||||||
|
listener.onStreamException(x);
|
||||||
|
}
|
||||||
|
catch (Exception xx)
|
||||||
|
{
|
||||||
|
logger.debug("Could not notify listener " + listener, xx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,49 +137,52 @@ public class Parser
|
||||||
|
|
||||||
public void parse(ByteBuffer buffer)
|
public void parse(ByteBuffer buffer)
|
||||||
{
|
{
|
||||||
|
logger.debug("Parsing {} bytes", buffer.remaining());
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.debug("Parsing {} bytes", buffer.remaining());
|
|
||||||
while (buffer.hasRemaining())
|
while (buffer.hasRemaining())
|
||||||
{
|
{
|
||||||
switch (state)
|
try
|
||||||
{
|
{
|
||||||
case CONTROL_BIT:
|
switch (state)
|
||||||
{
|
{
|
||||||
// We must only peek the first byte and not advance the buffer
|
case CONTROL_BIT:
|
||||||
// because the 7 least significant bits may be relevant in data frames
|
{
|
||||||
int currByte = buffer.get(buffer.position());
|
// We must only peek the first byte and not advance the buffer
|
||||||
boolean isControlFrame = (currByte & 0x80) == 0x80;
|
// because the 7 least significant bits may be relevant in data frames
|
||||||
state = isControlFrame ? State.CONTROL_FRAME : State.DATA_FRAME;
|
int currByte = buffer.get(buffer.position());
|
||||||
break;
|
boolean isControlFrame = (currByte & 0x80) == 0x80;
|
||||||
}
|
state = isControlFrame ? State.CONTROL_FRAME : State.DATA_FRAME;
|
||||||
case CONTROL_FRAME:
|
break;
|
||||||
{
|
}
|
||||||
if (controlFrameParser.parse(buffer))
|
case CONTROL_FRAME:
|
||||||
reset();
|
{
|
||||||
break;
|
if (controlFrameParser.parse(buffer))
|
||||||
}
|
reset();
|
||||||
case DATA_FRAME:
|
break;
|
||||||
{
|
}
|
||||||
if (dataFrameParser.parse(buffer))
|
case DATA_FRAME:
|
||||||
reset();
|
{
|
||||||
break;
|
if (dataFrameParser.parse(buffer))
|
||||||
}
|
reset();
|
||||||
default:
|
break;
|
||||||
{
|
}
|
||||||
throw new IllegalStateException();
|
default:
|
||||||
|
{
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (StreamException x)
|
||||||
|
{
|
||||||
|
notifyStreamException(x);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SessionException x)
|
catch (SessionException x)
|
||||||
{
|
{
|
||||||
notifySessionException(x);
|
notifySessionException(x);
|
||||||
}
|
}
|
||||||
catch (StreamException x)
|
|
||||||
{
|
|
||||||
notifyStreamException(x);
|
|
||||||
}
|
|
||||||
catch (Throwable x)
|
catch (Throwable x)
|
||||||
{
|
{
|
||||||
notifySessionException(new SessionException(SessionStatus.PROTOCOL_ERROR, x));
|
notifySessionException(new SessionException(SessionStatus.PROTOCOL_ERROR, x));
|
||||||
|
@ -227,5 +237,4 @@ public class Parser
|
||||||
{
|
{
|
||||||
CONTROL_BIT, CONTROL_FRAME, DATA_FRAME
|
CONTROL_BIT, CONTROL_FRAME, DATA_FRAME
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,8 +85,31 @@ public class SynStreamBodyParser extends ControlFrameBodyParser
|
||||||
{
|
{
|
||||||
// Now we know the streamId, we can do the version check
|
// Now we know the streamId, we can do the version check
|
||||||
// and if it is wrong, issue a RST_STREAM
|
// and if it is wrong, issue a RST_STREAM
|
||||||
checkVersion(controlFrameParser.getVersion(), streamId);
|
try
|
||||||
|
{
|
||||||
|
checkVersion(controlFrameParser.getVersion(), streamId);
|
||||||
|
}
|
||||||
|
catch (StreamException e)
|
||||||
|
{
|
||||||
|
// We've already read 4 bytes of the streamId which are part of controlFrameParser.getLength
|
||||||
|
// so we need to substract those from the bytesToSkip.
|
||||||
|
int bytesToSkip = controlFrameParser.getLength() - 4;
|
||||||
|
int remaining = buffer.remaining();
|
||||||
|
if (remaining >= bytesToSkip)
|
||||||
|
{
|
||||||
|
buffer.position(buffer.position() + bytesToSkip);
|
||||||
|
controlFrameParser.reset();
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int bytesToSkipInNextBuffer = bytesToSkip - remaining;
|
||||||
|
buffer.position(buffer.limit());
|
||||||
|
controlFrameParser.skip(bytesToSkipInNextBuffer);
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
if (buffer.remaining() >= 4)
|
if (buffer.remaining() >= 4)
|
||||||
{
|
{
|
||||||
associatedStreamId = buffer.getInt() & 0x7F_FF_FF_FF;
|
associatedStreamId = buffer.getInt() & 0x7F_FF_FF_FF;
|
||||||
|
@ -122,8 +145,6 @@ public class SynStreamBodyParser extends ControlFrameBodyParser
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
slot = (short)(currByte & 0xFF);
|
slot = (short)(currByte & 0xFF);
|
||||||
if (slot < 0)
|
|
||||||
throw new StreamException(streamId, StreamStatus.INVALID_CREDENTIALS);
|
|
||||||
cursor = 0;
|
cursor = 0;
|
||||||
state = State.HEADERS;
|
state = State.HEADERS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -152,7 +152,7 @@ public class StandardStreamTest
|
||||||
stream.setStreamFrameListener(new StreamFrameListener.Adapter()
|
stream.setStreamFrameListener(new StreamFrameListener.Adapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
assertThat("exception is a TimeoutException", x, is(instanceOf(TimeoutException.class)));
|
assertThat("exception is a TimeoutException", x, is(instanceOf(TimeoutException.class)));
|
||||||
onFailCalledLatch.countDown();
|
onFailCalledLatch.countDown();
|
||||||
|
@ -173,7 +173,7 @@ public class StandardStreamTest
|
||||||
stream.setStreamFrameListener(new StreamFrameListener.Adapter()
|
stream.setStreamFrameListener(new StreamFrameListener.Adapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
assertThat("exception is a TimeoutException", x, is(instanceOf(TimeoutException.class)));
|
assertThat("exception is a TimeoutException", x, is(instanceOf(TimeoutException.class)));
|
||||||
onFailCalledLatch.countDown();
|
onFailCalledLatch.countDown();
|
||||||
|
|
|
@ -0,0 +1,287 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2013 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.spdy.parser;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.zip.ZipException;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||||
|
import org.eclipse.jetty.spdy.CompressionFactory;
|
||||||
|
import org.eclipse.jetty.spdy.StreamException;
|
||||||
|
import org.eclipse.jetty.spdy.api.SPDY;
|
||||||
|
import org.eclipse.jetty.spdy.api.SynInfo;
|
||||||
|
import org.eclipse.jetty.spdy.frames.ControlFrame;
|
||||||
|
import org.eclipse.jetty.spdy.frames.SynStreamFrame;
|
||||||
|
import org.eclipse.jetty.spdy.generator.Generator;
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
import org.eclipse.jetty.util.Fields;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
public class BrokenFrameTest
|
||||||
|
{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidHeaderNameLength() throws Exception
|
||||||
|
{
|
||||||
|
Fields headers = new Fields();
|
||||||
|
headers.add("broken", "header");
|
||||||
|
SynStreamFrame frame = new SynStreamFrame(SPDY.V2, SynInfo.FLAG_CLOSE, 1, 0, (byte)0, (short)0, headers);
|
||||||
|
Generator generator = new Generator(new MappedByteBufferPool(), new NoCompressionCompressionFactory.NoCompressionCompressor());
|
||||||
|
|
||||||
|
ByteBuffer bufferWithBrokenHeaderNameLength = generator.control(frame);
|
||||||
|
// Break the header name length to provoke the Parser to throw a StreamException
|
||||||
|
bufferWithBrokenHeaderNameLength.put(21, (byte)0);
|
||||||
|
|
||||||
|
ByteBuffer bufferWithValidSynStreamFrame = generator.control(frame);
|
||||||
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithBrokenHeaderNameLength));
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithValidSynStreamFrame));
|
||||||
|
|
||||||
|
byte concatenatedFramesByteArray[] = outputStream.toByteArray();
|
||||||
|
ByteBuffer concatenatedBuffer = BufferUtil.toBuffer(concatenatedFramesByteArray);
|
||||||
|
|
||||||
|
final CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
Parser parser = new Parser(new NoCompressionCompressionFactory.NoCompressionDecompressor());
|
||||||
|
parser.addListener(new Parser.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onControlFrame(ControlFrame frame)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStreamException(StreamException x)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
parser.parse(concatenatedBuffer);
|
||||||
|
|
||||||
|
assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidVersion() throws Exception
|
||||||
|
{
|
||||||
|
Fields headers = new Fields();
|
||||||
|
headers.add("good", "header");
|
||||||
|
headers.add("another","header");
|
||||||
|
SynStreamFrame frame = new SynStreamFrame(SPDY.V2, SynInfo.FLAG_CLOSE, 1, 0, (byte)0, (short)0, headers);
|
||||||
|
Generator generator = new Generator(new MappedByteBufferPool(), new NoCompressionCompressionFactory.NoCompressionCompressor());
|
||||||
|
|
||||||
|
ByteBuffer bufferWithBrokenVersion = generator.control(frame);
|
||||||
|
// Break the header name length to provoke the Parser to throw a StreamException
|
||||||
|
bufferWithBrokenVersion.put(1, (byte)4);
|
||||||
|
|
||||||
|
ByteBuffer bufferWithValidSynStreamFrame = generator.control(frame);
|
||||||
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithBrokenVersion));
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithValidSynStreamFrame));
|
||||||
|
|
||||||
|
byte concatenatedFramesByteArray[] = outputStream.toByteArray();
|
||||||
|
ByteBuffer concatenatedBuffer = BufferUtil.toBuffer(concatenatedFramesByteArray);
|
||||||
|
|
||||||
|
final CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
Parser parser = new Parser(new NoCompressionCompressionFactory.NoCompressionDecompressor());
|
||||||
|
parser.addListener(new Parser.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onControlFrame(ControlFrame frame)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStreamException(StreamException x)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
parser.parse(concatenatedBuffer);
|
||||||
|
|
||||||
|
assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidVersionWithSplitBuffer() throws Exception
|
||||||
|
{
|
||||||
|
Fields headers = new Fields();
|
||||||
|
headers.add("good", "header");
|
||||||
|
headers.add("another","header");
|
||||||
|
SynStreamFrame frame = new SynStreamFrame(SPDY.V2, SynInfo.FLAG_CLOSE, 1, 0, (byte)0, (short)0, headers);
|
||||||
|
Generator generator = new Generator(new MappedByteBufferPool(), new NoCompressionCompressionFactory.NoCompressionCompressor());
|
||||||
|
|
||||||
|
ByteBuffer bufferWithBrokenVersion = generator.control(frame);
|
||||||
|
// Break the header name length to provoke the Parser to throw a StreamException
|
||||||
|
bufferWithBrokenVersion.put(1, (byte)4);
|
||||||
|
|
||||||
|
ByteBuffer bufferWithValidSynStreamFrame = generator.control(frame);
|
||||||
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithBrokenVersion));
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithValidSynStreamFrame));
|
||||||
|
|
||||||
|
byte concatenatedFramesByteArray[] = outputStream.toByteArray();
|
||||||
|
ByteBuffer concatenatedBuffer1 = BufferUtil.toBuffer(Arrays.copyOfRange(concatenatedFramesByteArray,0,20));
|
||||||
|
ByteBuffer concatenatedBuffer2 = BufferUtil.toBuffer(Arrays.copyOfRange(concatenatedFramesByteArray,20,
|
||||||
|
concatenatedFramesByteArray.length));
|
||||||
|
|
||||||
|
final CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
Parser parser = new Parser(new NoCompressionCompressionFactory.NoCompressionDecompressor());
|
||||||
|
parser.addListener(new Parser.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onControlFrame(ControlFrame frame)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStreamException(StreamException x)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
parser.parse(concatenatedBuffer1);
|
||||||
|
parser.parse(concatenatedBuffer2);
|
||||||
|
|
||||||
|
assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidVersionAndGoodFrameSplitInThreeBuffers() throws Exception
|
||||||
|
{
|
||||||
|
Fields headers = new Fields();
|
||||||
|
headers.add("good", "header");
|
||||||
|
headers.add("another","header");
|
||||||
|
SynStreamFrame frame = new SynStreamFrame(SPDY.V2, SynInfo.FLAG_CLOSE, 1, 0, (byte)0, (short)0, headers);
|
||||||
|
Generator generator = new Generator(new MappedByteBufferPool(), new NoCompressionCompressionFactory.NoCompressionCompressor());
|
||||||
|
|
||||||
|
ByteBuffer bufferWithBrokenVersion = generator.control(frame);
|
||||||
|
// Break the header name length to provoke the Parser to throw a StreamException
|
||||||
|
bufferWithBrokenVersion.put(1, (byte)4);
|
||||||
|
|
||||||
|
ByteBuffer bufferWithValidSynStreamFrame = generator.control(frame);
|
||||||
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithBrokenVersion));
|
||||||
|
outputStream.write(BufferUtil.toArray(bufferWithValidSynStreamFrame));
|
||||||
|
|
||||||
|
byte concatenatedFramesByteArray[] = outputStream.toByteArray();
|
||||||
|
ByteBuffer concatenatedBuffer1 = BufferUtil.toBuffer(Arrays.copyOfRange(concatenatedFramesByteArray,0,20));
|
||||||
|
ByteBuffer concatenatedBuffer2 = BufferUtil.toBuffer(Arrays.copyOfRange(concatenatedFramesByteArray,20, 30));
|
||||||
|
ByteBuffer concatenatedBuffer3 = BufferUtil.toBuffer(Arrays.copyOfRange(concatenatedFramesByteArray,30,
|
||||||
|
concatenatedFramesByteArray.length));
|
||||||
|
|
||||||
|
final CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
Parser parser = new Parser(new NoCompressionCompressionFactory.NoCompressionDecompressor());
|
||||||
|
parser.addListener(new Parser.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onControlFrame(ControlFrame frame)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStreamException(StreamException x)
|
||||||
|
{
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
parser.parse(concatenatedBuffer1);
|
||||||
|
parser.parse(concatenatedBuffer2);
|
||||||
|
parser.parse(concatenatedBuffer3);
|
||||||
|
|
||||||
|
assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class NoCompressionCompressionFactory implements CompressionFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Compressor newCompressor()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Decompressor newDecompressor()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NoCompressionCompressor implements Compressor
|
||||||
|
{
|
||||||
|
|
||||||
|
private byte[] input;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInput(byte[] input)
|
||||||
|
{
|
||||||
|
this.input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDictionary(byte[] dictionary)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compress(byte[] output)
|
||||||
|
{
|
||||||
|
System.arraycopy(input, 0, output, 0, input.length);
|
||||||
|
return input.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NoCompressionDecompressor implements Decompressor
|
||||||
|
{
|
||||||
|
private byte[] input;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDictionary(byte[] dictionary)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInput(byte[] input)
|
||||||
|
{
|
||||||
|
this.input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int decompress(byte[] output) throws ZipException
|
||||||
|
{
|
||||||
|
System.arraycopy(input, 0, output, 0, input.length);
|
||||||
|
return input.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -57,7 +57,7 @@ public class HttpClientTransportOverSPDY implements HttpClientTransport
|
||||||
SessionFrameListener.Adapter listener = new SessionFrameListener.Adapter()
|
SessionFrameListener.Adapter listener = new SessionFrameListener.Adapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onException(Throwable x)
|
public void onFailure(Session session, Throwable x)
|
||||||
{
|
{
|
||||||
// TODO: is this correct ?
|
// TODO: is this correct ?
|
||||||
// TODO: if I get a stream error (e.g. invalid response headers)
|
// TODO: if I get a stream error (e.g. invalid response headers)
|
||||||
|
|
|
@ -141,7 +141,7 @@ public class HttpReceiverOverSPDY extends HttpReceiver implements StreamFrameLis
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
HttpExchange exchange = getHttpExchange();
|
HttpExchange exchange = getHttpExchange();
|
||||||
if (exchange == null)
|
if (exchange == null)
|
||||||
|
|
|
@ -156,7 +156,7 @@ public class HTTPSPDYServerConnectionFactory extends SPDYServerConnectionFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
LOG.debug(x);
|
LOG.debug(x);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.spdy.server.http;
|
package org.eclipse.jetty.spdy.server.http;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -133,7 +132,7 @@ public class HttpTransportOverSPDY implements HttpTransport
|
||||||
StreamException exception = new StreamException(stream.getId(), StreamStatus.PROTOCOL_ERROR,
|
StreamException exception = new StreamException(stream.getId(), StreamStatus.PROTOCOL_ERROR,
|
||||||
"Stream already committed!");
|
"Stream already committed!");
|
||||||
callback.failed(exception);
|
callback.failed(exception);
|
||||||
LOG.warn("Committed response twice.", exception);
|
LOG.debug("Committed response twice.", exception);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sendReply(info, !hasContent ? callback : new Callback.Adapter()
|
sendReply(info, !hasContent ? callback : new Callback.Adapter()
|
||||||
|
|
|
@ -171,7 +171,7 @@ public class SPDYProxyEngine extends ProxyEngine implements StreamFrameListener
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
LOG.debug(x);
|
LOG.debug(x);
|
||||||
}
|
}
|
||||||
|
@ -275,7 +275,7 @@ public class SPDYProxyEngine extends ProxyEngine implements StreamFrameListener
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
LOG.debug(x);
|
LOG.debug(x);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ package org.eclipse.jetty.spdy.server.http;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.eclipse.jetty.http.HttpGenerator;
|
import org.eclipse.jetty.http.HttpGenerator;
|
||||||
import org.eclipse.jetty.http.HttpStatus;
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
|
@ -37,8 +39,6 @@ import org.eclipse.jetty.spdy.http.HTTPSPDYHeader;
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
import org.eclipse.jetty.util.Callback;
|
import org.eclipse.jetty.util.Callback;
|
||||||
import org.eclipse.jetty.util.Fields;
|
import org.eclipse.jetty.util.Fields;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
|
||||||
import org.eclipse.jetty.util.log.StdErrLog;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
@ -251,22 +251,29 @@ public class HttpTransportOverSPDYTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVerifyThatAStreamIsNotCommittedTwice() throws IOException
|
public void testVerifyThatAStreamIsNotCommittedTwice() throws IOException, InterruptedException
|
||||||
{
|
{
|
||||||
((StdErrLog)Log.getLogger(HttpTransportOverSPDY.class)).setHideStacks(true);
|
final CountDownLatch failedCalledLatch = new CountDownLatch(1);
|
||||||
ByteBuffer content = createRandomByteBuffer();
|
ByteBuffer content = createRandomByteBuffer();
|
||||||
boolean lastContent = false;
|
boolean lastContent = false;
|
||||||
|
|
||||||
httpTransportOverSPDY.send(responseInfo,content,lastContent, callback);
|
httpTransportOverSPDY.send(responseInfo, content, lastContent, callback);
|
||||||
ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
|
ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
|
||||||
verify(stream, times(1)).reply(replyInfoCaptor.capture(), any(Callback.class));
|
verify(stream, times(1)).reply(replyInfoCaptor.capture(), any(Callback.class));
|
||||||
assertThat("ReplyInfo close is false", replyInfoCaptor.getValue().isClose(), is(false));
|
assertThat("ReplyInfo close is false", replyInfoCaptor.getValue().isClose(), is(false));
|
||||||
|
|
||||||
httpTransportOverSPDY.send(HttpGenerator.RESPONSE_500_INFO, null,true, new Callback.Adapter());
|
httpTransportOverSPDY.send(HttpGenerator.RESPONSE_500_INFO, null, true, new Callback.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void failed(Throwable x)
|
||||||
|
{
|
||||||
|
failedCalledLatch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
verify(stream, times(1)).data(any(DataInfo.class), any(Callback.class));
|
verify(stream, times(1)).data(any(DataInfo.class), any(Callback.class));
|
||||||
|
|
||||||
((StdErrLog)Log.getLogger(HttpTransportOverSPDY.class)).setHideStacks(false);
|
assertThat("callback.failed has been called", failedCalledLatch.await(5, TimeUnit.SECONDS), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ByteBuffer createRandomByteBuffer()
|
private ByteBuffer createRandomByteBuffer()
|
||||||
|
|
|
@ -1496,7 +1496,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest
|
||||||
new StreamFrameListener.Adapter()
|
new StreamFrameListener.Adapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
assertThat("we got a TimeoutException", x, instanceOf(TimeoutException.class));
|
assertThat("we got a TimeoutException", x, instanceOf(TimeoutException.class));
|
||||||
timeoutReceivedLatch.countDown();
|
timeoutReceivedLatch.countDown();
|
||||||
|
@ -1535,7 +1535,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest
|
||||||
new StreamFrameListener.Adapter()
|
new StreamFrameListener.Adapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
assertThat("we got a TimeoutException", x, instanceOf(TimeoutException.class));
|
assertThat("we got a TimeoutException", x, instanceOf(TimeoutException.class));
|
||||||
timeoutReceivedLatch.countDown();
|
timeoutReceivedLatch.countDown();
|
||||||
|
@ -1580,7 +1580,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest
|
||||||
new StreamFrameListener.Adapter()
|
new StreamFrameListener.Adapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable x)
|
public void onFailure(Stream stream, Throwable x)
|
||||||
{
|
{
|
||||||
assertThat("we got a TimeoutException", x, instanceOf(TimeoutException.class));
|
assertThat("we got a TimeoutException", x, instanceOf(TimeoutException.class));
|
||||||
timeoutReceivedLatch.countDown();
|
timeoutReceivedLatch.countDown();
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import org.eclipse.jetty.io.MappedByteBufferPool;
|
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||||
import org.eclipse.jetty.spdy.StandardCompressionFactory;
|
import org.eclipse.jetty.spdy.StandardCompressionFactory;
|
||||||
import org.eclipse.jetty.spdy.api.SPDY;
|
import org.eclipse.jetty.spdy.api.SPDY;
|
||||||
|
import org.eclipse.jetty.spdy.api.Session;
|
||||||
import org.eclipse.jetty.spdy.api.Stream;
|
import org.eclipse.jetty.spdy.api.Stream;
|
||||||
import org.eclipse.jetty.spdy.api.StreamFrameListener;
|
import org.eclipse.jetty.spdy.api.StreamFrameListener;
|
||||||
import org.eclipse.jetty.spdy.api.StreamStatus;
|
import org.eclipse.jetty.spdy.api.StreamStatus;
|
||||||
|
@ -58,7 +59,7 @@ public class UnsupportedVersionTest extends AbstractTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onException(Throwable x)
|
public void onFailure(Session session, Throwable x)
|
||||||
{
|
{
|
||||||
// Suppress exception logging for this test
|
// Suppress exception logging for this test
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,4 +81,9 @@ public interface Extension extends IncomingFrames, OutgoingFrames
|
||||||
* the next outgoing extension
|
* the next outgoing extension
|
||||||
*/
|
*/
|
||||||
public void setNextOutgoingFrames(OutgoingFrames nextOutgoing);
|
public void setNextOutgoingFrames(OutgoingFrames nextOutgoing);
|
||||||
|
|
||||||
|
// TODO: Extension should indicate if it requires boundary of fragments to be preserved
|
||||||
|
|
||||||
|
// TODO: Extension should indicate if it uses the Extension data field of frame for its own reasons.
|
||||||
|
|
||||||
}
|
}
|
|
@ -96,8 +96,12 @@ public class ExtensionConfig
|
||||||
{
|
{
|
||||||
str.append(';');
|
str.append(';');
|
||||||
str.append(param);
|
str.append(param);
|
||||||
str.append('=');
|
String value = parameters.get(param);
|
||||||
QuoteUtil.quoteIfNeeded(str,parameters.get(param),";=");
|
if (value != null)
|
||||||
|
{
|
||||||
|
str.append('=');
|
||||||
|
QuoteUtil.quoteIfNeeded(str,value,";=");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return str.toString();
|
return str.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2013 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.websocket.common.extensions.compress;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compression Method
|
|
||||||
*/
|
|
||||||
public interface CompressionMethod
|
|
||||||
{
|
|
||||||
public interface Process
|
|
||||||
{
|
|
||||||
public void begin();
|
|
||||||
|
|
||||||
public void end();
|
|
||||||
|
|
||||||
public void input(ByteBuffer input);
|
|
||||||
|
|
||||||
public boolean isDone();
|
|
||||||
|
|
||||||
public ByteBuffer process();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Process compress();
|
|
||||||
|
|
||||||
public Process decompress();
|
|
||||||
}
|
|
|
@ -1,260 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2013 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.websocket.common.extensions.compress;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.zip.DataFormatException;
|
|
||||||
import java.util.zip.Deflater;
|
|
||||||
import java.util.zip.Inflater;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
|
||||||
import org.eclipse.jetty.util.TypeUtil;
|
|
||||||
import org.eclipse.jetty.util.log.Log;
|
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
|
||||||
import org.eclipse.jetty.websocket.api.BadPayloadException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deflate Compression Method
|
|
||||||
*/
|
|
||||||
public class DeflateCompressionMethod implements CompressionMethod
|
|
||||||
{
|
|
||||||
private static class DeflaterProcess implements CompressionMethod.Process
|
|
||||||
{
|
|
||||||
private static final boolean BFINAL_HACK = Boolean.parseBoolean(System.getProperty("jetty.websocket.bfinal.hack","true"));
|
|
||||||
|
|
||||||
private final Deflater deflater;
|
|
||||||
private int bufferSize = DEFAULT_BUFFER_SIZE;
|
|
||||||
|
|
||||||
public DeflaterProcess(boolean nowrap)
|
|
||||||
{
|
|
||||||
deflater = new Deflater(Deflater.BEST_COMPRESSION,nowrap);
|
|
||||||
deflater.setStrategy(Deflater.DEFAULT_STRATEGY);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void begin()
|
|
||||||
{
|
|
||||||
deflater.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void end()
|
|
||||||
{
|
|
||||||
deflater.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void input(ByteBuffer input)
|
|
||||||
{
|
|
||||||
if (LOG.isDebugEnabled())
|
|
||||||
{
|
|
||||||
LOG.debug("input: {}",BufferUtil.toDetailString(input));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the data that is uncompressed to the deflater
|
|
||||||
byte raw[] = BufferUtil.toArray(input);
|
|
||||||
deflater.setInput(raw,0,raw.length);
|
|
||||||
deflater.finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return deflater.finished();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteBuffer process()
|
|
||||||
{
|
|
||||||
// prepare the output buffer
|
|
||||||
ByteBuffer buf = ByteBuffer.allocate(bufferSize);
|
|
||||||
BufferUtil.clearToFill(buf);
|
|
||||||
|
|
||||||
while (!deflater.finished())
|
|
||||||
{
|
|
||||||
byte out[] = new byte[bufferSize];
|
|
||||||
int len = deflater.deflate(out,0,out.length,Deflater.SYNC_FLUSH);
|
|
||||||
|
|
||||||
if (LOG.isDebugEnabled())
|
|
||||||
{
|
|
||||||
LOG.debug("Deflater: finished={}, needsInput={}, len={}",deflater.finished(),deflater.needsInput(),len);
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.put(out,0,len);
|
|
||||||
}
|
|
||||||
BufferUtil.flipToFlush(buf,0);
|
|
||||||
|
|
||||||
if (BFINAL_HACK)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Per the spec, it says that BFINAL 1 or 0 are allowed.
|
|
||||||
*
|
|
||||||
* However, Java always uses BFINAL 1, whereas the browsers Chromium and Safari fail to decompress when it encounters BFINAL 1.
|
|
||||||
*
|
|
||||||
* This hack will always set BFINAL 0
|
|
||||||
*/
|
|
||||||
byte b0 = buf.get(0);
|
|
||||||
if ((b0 & 1) != 0) // if BFINAL 1
|
|
||||||
{
|
|
||||||
buf.put(0,(b0 ^= 1)); // flip bit to BFINAL 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBufferSize(int bufferSize)
|
|
||||||
{
|
|
||||||
this.bufferSize = bufferSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class InflaterProcess implements CompressionMethod.Process
|
|
||||||
{
|
|
||||||
/** Tail Bytes per Spec */
|
|
||||||
private static final byte[] TAIL = new byte[]
|
|
||||||
{ 0x00, 0x00, (byte)0xFF, (byte)0xFF };
|
|
||||||
private final Inflater inflater;
|
|
||||||
private int bufferSize = DEFAULT_BUFFER_SIZE;
|
|
||||||
|
|
||||||
public InflaterProcess(boolean nowrap) {
|
|
||||||
inflater = new Inflater(nowrap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void begin()
|
|
||||||
{
|
|
||||||
inflater.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void end()
|
|
||||||
{
|
|
||||||
inflater.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void input(ByteBuffer input)
|
|
||||||
{
|
|
||||||
if (LOG.isDebugEnabled())
|
|
||||||
{
|
|
||||||
LOG.debug("inflate: {}",BufferUtil.toDetailString(input));
|
|
||||||
LOG.debug("Input Data: {}",TypeUtil.toHexString(BufferUtil.toArray(input)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the data that is compressed (+ TAIL) to the inflater
|
|
||||||
int len = input.remaining() + 4;
|
|
||||||
byte raw[] = new byte[len];
|
|
||||||
int inlen = input.remaining();
|
|
||||||
input.slice().get(raw,0,inlen);
|
|
||||||
System.arraycopy(TAIL,0,raw,inlen,TAIL.length);
|
|
||||||
inflater.setInput(raw,0,raw.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return (inflater.getRemaining() <= 0) || inflater.finished();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ByteBuffer process()
|
|
||||||
{
|
|
||||||
// Establish place for inflated data
|
|
||||||
byte buf[] = new byte[bufferSize];
|
|
||||||
try
|
|
||||||
{
|
|
||||||
int inflated = inflater.inflate(buf);
|
|
||||||
if (inflated == 0)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteBuffer ret = BufferUtil.toBuffer(buf,0,inflated);
|
|
||||||
|
|
||||||
if (LOG.isDebugEnabled())
|
|
||||||
{
|
|
||||||
LOG.debug("uncompressed={}",BufferUtil.toDetailString(ret));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
catch (DataFormatException e)
|
|
||||||
{
|
|
||||||
LOG.warn(e);
|
|
||||||
throw new BadPayloadException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBufferSize(int bufferSize)
|
|
||||||
{
|
|
||||||
this.bufferSize = bufferSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final int DEFAULT_BUFFER_SIZE = 61*1024;
|
|
||||||
|
|
||||||
private static final Logger LOG = Log.getLogger(DeflateCompressionMethod.class);
|
|
||||||
|
|
||||||
private int bufferSize = 64 * 1024;
|
|
||||||
private final DeflaterProcess compress;
|
|
||||||
private final InflaterProcess decompress;
|
|
||||||
|
|
||||||
public DeflateCompressionMethod()
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Specs specify that head/tail of deflate are not to be present.
|
|
||||||
*
|
|
||||||
* So lets not use the wrapped format of bytes.
|
|
||||||
*
|
|
||||||
* Setting nowrap to true prevents the Deflater from writing the head/tail bytes and the Inflater from expecting the head/tail bytes.
|
|
||||||
*/
|
|
||||||
boolean nowrap = true;
|
|
||||||
|
|
||||||
this.compress = new DeflaterProcess(nowrap);
|
|
||||||
this.decompress = new InflaterProcess(nowrap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Process compress()
|
|
||||||
{
|
|
||||||
return compress;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Process decompress()
|
|
||||||
{
|
|
||||||
return decompress;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBufferSize()
|
|
||||||
{
|
|
||||||
return bufferSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBufferSize(int size)
|
|
||||||
{
|
|
||||||
if (size < 64)
|
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("Buffer Size [" + size + "] cannot be less than 64 bytes");
|
|
||||||
}
|
|
||||||
this.bufferSize = size;
|
|
||||||
this.compress.setBufferSize(bufferSize);
|
|
||||||
this.decompress.setBufferSize(bufferSize);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,142 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2013 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.websocket.common.extensions.compress;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.websocket.api.WriteCallback;
|
|
||||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
|
||||||
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
|
||||||
import org.eclipse.jetty.websocket.common.OpCode;
|
|
||||||
import org.eclipse.jetty.websocket.common.extensions.AbstractExtension;
|
|
||||||
import org.eclipse.jetty.websocket.common.frames.DataFrame;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Per Message Deflate Compression extension for WebSocket.
|
|
||||||
* <p>
|
|
||||||
* Attempts to follow <a href="https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-01">draft-ietf-hybi-permessage-compression-01</a>
|
|
||||||
*/
|
|
||||||
public class MessageDeflateCompressionExtension extends AbstractExtension
|
|
||||||
{
|
|
||||||
private CompressionMethod method;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName()
|
|
||||||
{
|
|
||||||
return "permessage-deflate";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void incomingFrame(Frame frame)
|
|
||||||
{
|
|
||||||
if (OpCode.isControlFrame(frame.getOpCode()) || !frame.isRsv1())
|
|
||||||
{
|
|
||||||
// Cannot modify incoming control frames or ones with RSV1 set.
|
|
||||||
nextIncomingFrame(frame);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteBuffer data = frame.getPayload();
|
|
||||||
method.decompress().input(data);
|
|
||||||
while (!method.decompress().isDone())
|
|
||||||
{
|
|
||||||
ByteBuffer uncompressed = method.decompress().process();
|
|
||||||
if (uncompressed == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
DataFrame out = new DataFrame(frame);
|
|
||||||
out.setPayload(uncompressed);
|
|
||||||
if (!method.decompress().isDone())
|
|
||||||
{
|
|
||||||
out.setFin(false);
|
|
||||||
}
|
|
||||||
out.setRsv1(false); // Unset RSV1 on decompressed frame
|
|
||||||
nextIncomingFrame(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset only at the end of a message.
|
|
||||||
if (frame.isFin())
|
|
||||||
{
|
|
||||||
method.decompress().end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates use of RSV1 flag for indicating deflation is in use.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isRsv1User()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void outgoingFrame(Frame frame, WriteCallback callback)
|
|
||||||
{
|
|
||||||
if (OpCode.isControlFrame(frame.getOpCode()))
|
|
||||||
{
|
|
||||||
// skip, cannot compress control frames.
|
|
||||||
nextOutgoingFrame(frame,callback);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteBuffer data = frame.getPayload();
|
|
||||||
// deflate data
|
|
||||||
method.compress().input(data);
|
|
||||||
while (!method.compress().isDone())
|
|
||||||
{
|
|
||||||
ByteBuffer buf = method.compress().process();
|
|
||||||
DataFrame out = new DataFrame(frame);
|
|
||||||
out.setPayload(buf);
|
|
||||||
out.setRsv1(true);
|
|
||||||
if (!method.compress().isDone())
|
|
||||||
{
|
|
||||||
out.setFin(false);
|
|
||||||
// no callback for start/middle frames
|
|
||||||
nextOutgoingFrame(out,null);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// pass through callback to last frame
|
|
||||||
nextOutgoingFrame(out,callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset only at end of message
|
|
||||||
if (frame.isFin())
|
|
||||||
{
|
|
||||||
method.compress().end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setConfig(ExtensionConfig config)
|
|
||||||
{
|
|
||||||
super.setConfig(config);
|
|
||||||
method = new DeflateCompressionMethod();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString()
|
|
||||||
{
|
|
||||||
return String.format("%s[method=%s]",this.getClass().getSimpleName(),method);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,253 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2013 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.websocket.common.extensions.compress;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.zip.DataFormatException;
|
||||||
|
import java.util.zip.Deflater;
|
||||||
|
import java.util.zip.Inflater;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
import org.eclipse.jetty.util.log.Log;
|
||||||
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
import org.eclipse.jetty.websocket.api.BadPayloadException;
|
||||||
|
import org.eclipse.jetty.websocket.api.WriteCallback;
|
||||||
|
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||||
|
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
||||||
|
import org.eclipse.jetty.websocket.common.OpCode;
|
||||||
|
import org.eclipse.jetty.websocket.common.extensions.AbstractExtension;
|
||||||
|
import org.eclipse.jetty.websocket.common.frames.DataFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per Message Deflate Compression extension for WebSocket.
|
||||||
|
* <p>
|
||||||
|
* Attempts to follow <a href="https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-12">draft-ietf-hybi-permessage-compression-12</a>
|
||||||
|
*/
|
||||||
|
public class PerMessageDeflateExtension extends AbstractExtension
|
||||||
|
{
|
||||||
|
private static final boolean BFINAL_HACK = Boolean.parseBoolean(System.getProperty("jetty.websocket.bfinal.hack","true"));
|
||||||
|
private static final Logger LOG = Log.getLogger(PerMessageDeflateExtension.class);
|
||||||
|
|
||||||
|
private static final int OVERHEAD = 64;
|
||||||
|
/** Tail Bytes per Spec */
|
||||||
|
private static final byte[] TAIL = new byte[]
|
||||||
|
{ 0x00, 0x00, (byte)0xFF, (byte)0xFF };
|
||||||
|
private int bufferSize = 64 * 1024;
|
||||||
|
private Deflater compressor;
|
||||||
|
private Inflater decompressor;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName()
|
||||||
|
{
|
||||||
|
return "permessage-deflate";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void incomingFrame(Frame frame)
|
||||||
|
{
|
||||||
|
if (OpCode.isControlFrame(frame.getOpCode()) || !frame.isRsv1())
|
||||||
|
{
|
||||||
|
// Cannot modify incoming control frames or ones with RSV1 set.
|
||||||
|
nextIncomingFrame(frame);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!frame.hasPayload())
|
||||||
|
{
|
||||||
|
// no payload? nothing to do.
|
||||||
|
nextIncomingFrame(frame);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prime the decompressor
|
||||||
|
ByteBuffer payload = frame.getPayload();
|
||||||
|
int inlen = payload.remaining();
|
||||||
|
byte compressed[] = new byte[inlen + TAIL.length];
|
||||||
|
payload.get(compressed,0,inlen);
|
||||||
|
System.arraycopy(TAIL,0,compressed,inlen,TAIL.length);
|
||||||
|
decompressor.setInput(compressed,0,compressed.length);
|
||||||
|
|
||||||
|
// Perform decompression
|
||||||
|
while (decompressor.getRemaining() > 0 && !decompressor.finished())
|
||||||
|
{
|
||||||
|
DataFrame out = new DataFrame(frame);
|
||||||
|
out.setRsv1(false); // Unset RSV1
|
||||||
|
byte outbuf[] = new byte[Math.min(inlen * 2,bufferSize)];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int len = decompressor.inflate(outbuf);
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
if (decompressor.needsInput())
|
||||||
|
{
|
||||||
|
throw new BadPayloadException("Unable to inflate frame, not enough input on frame");
|
||||||
|
}
|
||||||
|
if (decompressor.needsDictionary())
|
||||||
|
{
|
||||||
|
throw new BadPayloadException("Unable to inflate frame, frame erroneously says it needs a dictionary");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
out.setPayload(ByteBuffer.wrap(outbuf,0,len));
|
||||||
|
}
|
||||||
|
nextIncomingFrame(out);
|
||||||
|
}
|
||||||
|
catch (DataFormatException e)
|
||||||
|
{
|
||||||
|
LOG.warn(e);
|
||||||
|
throw new BadPayloadException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates use of RSV1 flag for indicating deflation is in use.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isRsv1User()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void outgoingFrame(Frame frame, WriteCallback callback)
|
||||||
|
{
|
||||||
|
if (OpCode.isControlFrame(frame.getOpCode()))
|
||||||
|
{
|
||||||
|
// skip, cannot compress control frames.
|
||||||
|
nextOutgoingFrame(frame,callback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!frame.hasPayload())
|
||||||
|
{
|
||||||
|
// pass through, nothing to do
|
||||||
|
nextOutgoingFrame(frame,callback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (LOG.isDebugEnabled())
|
||||||
|
{
|
||||||
|
LOG.debug("outgoingFrame({}, {}) - {}",OpCode.name(frame.getOpCode()),callback != null?callback.getClass().getSimpleName():"<null>",
|
||||||
|
BufferUtil.toDetailString(frame.getPayload()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prime the compressor
|
||||||
|
byte uncompressed[] = BufferUtil.toArray(frame.getPayload());
|
||||||
|
|
||||||
|
// Perform the compression
|
||||||
|
if (!compressor.finished())
|
||||||
|
{
|
||||||
|
compressor.setInput(uncompressed,0,uncompressed.length);
|
||||||
|
byte compressed[] = new byte[uncompressed.length + OVERHEAD];
|
||||||
|
|
||||||
|
while (!compressor.needsInput())
|
||||||
|
{
|
||||||
|
int len = compressor.deflate(compressed,0,compressed.length,Deflater.SYNC_FLUSH);
|
||||||
|
ByteBuffer outbuf = getBufferPool().acquire(len,true);
|
||||||
|
BufferUtil.clearToFill(outbuf);
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
outbuf.put(compressed,0,len - 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferUtil.flipToFlush(outbuf,0);
|
||||||
|
|
||||||
|
if (len > 0 && BFINAL_HACK)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Per the spec, it says that BFINAL 1 or 0 are allowed.
|
||||||
|
*
|
||||||
|
* However, Java always uses BFINAL 1, whereas the browsers Chromium and Safari fail to decompress when it encounters BFINAL 1.
|
||||||
|
*
|
||||||
|
* This hack will always set BFINAL 0
|
||||||
|
*/
|
||||||
|
byte b0 = outbuf.get(0);
|
||||||
|
if ((b0 & 1) != 0) // if BFINAL 1
|
||||||
|
{
|
||||||
|
outbuf.put(0,(b0 ^= 1)); // flip bit to BFINAL 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DataFrame out = new DataFrame(frame);
|
||||||
|
out.setRsv1(true);
|
||||||
|
out.setPooledBuffer(true);
|
||||||
|
out.setPayload(outbuf);
|
||||||
|
|
||||||
|
if (!compressor.needsInput())
|
||||||
|
{
|
||||||
|
// this is fragmented
|
||||||
|
out.setFin(false);
|
||||||
|
nextOutgoingFrame(out,null); // non final frames have no callback
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// pass through the callback
|
||||||
|
nextOutgoingFrame(out,callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setConfig(final ExtensionConfig config)
|
||||||
|
{
|
||||||
|
ExtensionConfig negotiated = new ExtensionConfig(config.getName());
|
||||||
|
|
||||||
|
boolean nowrap = true;
|
||||||
|
compressor = new Deflater(Deflater.BEST_COMPRESSION,nowrap);
|
||||||
|
compressor.setStrategy(Deflater.DEFAULT_STRATEGY);
|
||||||
|
|
||||||
|
decompressor = new Inflater(nowrap);
|
||||||
|
|
||||||
|
for (String key : config.getParameterKeys())
|
||||||
|
{
|
||||||
|
key = key.trim();
|
||||||
|
String value = config.getParameter(key,null);
|
||||||
|
switch(key) {
|
||||||
|
case "c2s_max_window_bits":
|
||||||
|
negotiated.setParameter("s2c_max_window_bits",value);
|
||||||
|
break;
|
||||||
|
case "c2s_no_context_takeover":
|
||||||
|
negotiated.setParameter("s2c_no_context_takeover",value);
|
||||||
|
break;
|
||||||
|
case "s2c_max_window_bits":
|
||||||
|
negotiated.setParameter("c2s_max_window_bits",value);
|
||||||
|
break;
|
||||||
|
case "s2c_no_context_takeover":
|
||||||
|
negotiated.setParameter("c2s_no_context_takeover",value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
super.setConfig(negotiated);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
StringBuilder str = new StringBuilder();
|
||||||
|
str.append(this.getClass().getSimpleName());
|
||||||
|
str.append('[');
|
||||||
|
str.append(']');
|
||||||
|
return str.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension
|
org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension
|
||||||
org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension
|
org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension
|
||||||
org.eclipse.jetty.websocket.common.extensions.compress.XWebkitDeflateFrameExtension
|
org.eclipse.jetty.websocket.common.extensions.compress.XWebkitDeflateFrameExtension
|
||||||
org.eclipse.jetty.websocket.common.extensions.compress.MessageDeflateCompressionExtension
|
org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension
|
||||||
org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension
|
org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension
|
|
@ -18,15 +18,14 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.websocket.common.extensions;
|
package org.eclipse.jetty.websocket.common.extensions;
|
||||||
|
|
||||||
import org.eclipse.jetty.websocket.common.extensions.compress.DeflateCompressionMethodTest;
|
|
||||||
import org.eclipse.jetty.websocket.common.extensions.compress.MessageCompressionExtensionTest;
|
|
||||||
import org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtensionTest;
|
import org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtensionTest;
|
||||||
|
import org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtensionTest;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Suite;
|
import org.junit.runners.Suite;
|
||||||
|
|
||||||
@RunWith(Suite.class)
|
@RunWith(Suite.class)
|
||||||
@Suite.SuiteClasses(
|
@Suite.SuiteClasses(
|
||||||
{ ExtensionStackTest.class, DeflateCompressionMethodTest.class, MessageCompressionExtensionTest.class, FragmentExtensionTest.class,
|
{ ExtensionStackTest.class, PerMessageDeflateExtensionTest.class, FragmentExtensionTest.class,
|
||||||
IdentityExtensionTest.class, DeflateFrameExtensionTest.class })
|
IdentityExtensionTest.class, DeflateFrameExtensionTest.class })
|
||||||
public class AllTests
|
public class AllTests
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,26 +21,19 @@ package org.eclipse.jetty.websocket.common.extensions.compress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.log.Log;
|
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
|
||||||
import org.eclipse.jetty.websocket.api.WriteCallback;
|
import org.eclipse.jetty.websocket.api.WriteCallback;
|
||||||
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
||||||
import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
|
import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
|
||||||
import org.eclipse.jetty.websocket.common.Hex;
|
import org.eclipse.jetty.websocket.common.Hex;
|
||||||
import org.eclipse.jetty.websocket.common.OpCode;
|
|
||||||
|
|
||||||
public class CapturedHexPayloads implements OutgoingFrames
|
public class CapturedHexPayloads implements OutgoingFrames
|
||||||
{
|
{
|
||||||
private static final Logger LOG = Log.getLogger(CapturedHexPayloads.class);
|
|
||||||
private List<String> captured = new ArrayList<>();
|
private List<String> captured = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void outgoingFrame(Frame frame, WriteCallback callback)
|
public void outgoingFrame(Frame frame, WriteCallback callback)
|
||||||
{
|
{
|
||||||
String hexPayload = Hex.asHex(frame.getPayload());
|
String hexPayload = Hex.asHex(frame.getPayload());
|
||||||
LOG.debug("outgoingFrame({}: \"{}\", {})",
|
|
||||||
OpCode.name(frame.getOpCode()),
|
|
||||||
hexPayload, callback!=null?callback.getClass().getSimpleName():"<null>");
|
|
||||||
captured.add(hexPayload);
|
captured.add(hexPayload);
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,221 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2013 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.websocket.common.extensions.compress;
|
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
|
||||||
import org.eclipse.jetty.util.StringUtil;
|
|
||||||
import org.eclipse.jetty.util.TypeUtil;
|
|
||||||
import org.eclipse.jetty.util.log.Log;
|
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the Deflate Compression Method in use by several extensions.
|
|
||||||
*/
|
|
||||||
public class DeflateCompressionMethodTest
|
|
||||||
{
|
|
||||||
private static final Logger LOG = Log.getLogger(DeflateCompressionMethodTest.class);
|
|
||||||
|
|
||||||
private void assertRoundTrip(CompressionMethod method, CharSequence msg)
|
|
||||||
{
|
|
||||||
String expected = msg.toString();
|
|
||||||
|
|
||||||
ByteBuffer orig = BufferUtil.toBuffer(expected,StringUtil.__UTF8_CHARSET);
|
|
||||||
|
|
||||||
LOG.debug("orig: {}",BufferUtil.toDetailString(orig));
|
|
||||||
|
|
||||||
// compress
|
|
||||||
method.compress().begin();
|
|
||||||
method.compress().input(orig);
|
|
||||||
ByteBuffer compressed = method.compress().process();
|
|
||||||
LOG.debug("compressed: {}",BufferUtil.toDetailString(compressed));
|
|
||||||
Assert.assertThat("Compress.isDone",method.compress().isDone(),is(true));
|
|
||||||
method.compress().end();
|
|
||||||
|
|
||||||
// decompress
|
|
||||||
ByteBuffer decompressed = ByteBuffer.allocate(msg.length());
|
|
||||||
LOG.debug("decompressed(a): {}",BufferUtil.toDetailString(decompressed));
|
|
||||||
method.decompress().begin();
|
|
||||||
method.decompress().input(compressed);
|
|
||||||
while (!method.decompress().isDone())
|
|
||||||
{
|
|
||||||
ByteBuffer window = method.decompress().process();
|
|
||||||
BufferUtil.put(window,decompressed);
|
|
||||||
}
|
|
||||||
BufferUtil.flipToFlush(decompressed,0);
|
|
||||||
LOG.debug("decompressed(f): {}",BufferUtil.toDetailString(decompressed));
|
|
||||||
method.decompress().end();
|
|
||||||
|
|
||||||
// validate
|
|
||||||
String actual = BufferUtil.toUTF8String(decompressed);
|
|
||||||
Assert.assertThat("Message Size",actual.length(),is(msg.length()));
|
|
||||||
Assert.assertEquals("Message Contents",expected,actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test decompression with 2 buffers. First buffer is normal, second relies on back buffers created from first.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testFollowupBackDistance()
|
|
||||||
{
|
|
||||||
// The Sample (Compressed) Data
|
|
||||||
byte buf1[] = TypeUtil.fromHexString("2aC9Cc4dB50200"); // DEFLATE -> "time:"
|
|
||||||
byte buf2[] = TypeUtil.fromHexString("2a01110000"); // DEFLATE -> "time:"
|
|
||||||
|
|
||||||
// Setup Compression Method
|
|
||||||
CompressionMethod method = new DeflateCompressionMethod();
|
|
||||||
|
|
||||||
// Decompressed Data Holder
|
|
||||||
ByteBuffer decompressed = ByteBuffer.allocate(32);
|
|
||||||
BufferUtil.flipToFill(decompressed);
|
|
||||||
|
|
||||||
// Perform Decompress on Buf 1
|
|
||||||
BufferUtil.clearToFill(decompressed);
|
|
||||||
// IGNORE method.decompress().begin();
|
|
||||||
method.decompress().input(ByteBuffer.wrap(buf1));
|
|
||||||
while (!method.decompress().isDone())
|
|
||||||
{
|
|
||||||
ByteBuffer window = method.decompress().process();
|
|
||||||
BufferUtil.put(window,decompressed);
|
|
||||||
}
|
|
||||||
BufferUtil.flipToFlush(decompressed,0);
|
|
||||||
LOG.debug("decompressed[1]: {}",BufferUtil.toDetailString(decompressed));
|
|
||||||
// IGNORE method.decompress().end();
|
|
||||||
|
|
||||||
// Perform Decompress on Buf 2
|
|
||||||
BufferUtil.clearToFill(decompressed);
|
|
||||||
// IGNORE method.decompress().begin();
|
|
||||||
method.decompress().input(ByteBuffer.wrap(buf2));
|
|
||||||
while (!method.decompress().isDone())
|
|
||||||
{
|
|
||||||
ByteBuffer window = method.decompress().process();
|
|
||||||
BufferUtil.put(window,decompressed);
|
|
||||||
}
|
|
||||||
BufferUtil.flipToFlush(decompressed,0);
|
|
||||||
LOG.debug("decompressed[2]: {}",BufferUtil.toDetailString(decompressed));
|
|
||||||
// IGNORE method.decompress().end();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test a large payload (a payload length over 65535 bytes).
|
|
||||||
*
|
|
||||||
* Round Trip (RT) Compress then Decompress
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testRTLarge()
|
|
||||||
{
|
|
||||||
// large sized message
|
|
||||||
StringBuilder msg = new StringBuilder();
|
|
||||||
for (int i = 0; i < 5000; i++)
|
|
||||||
{
|
|
||||||
msg.append("0123456789ABCDEF ");
|
|
||||||
}
|
|
||||||
msg.append('X'); // so we can see the end in our debugging
|
|
||||||
|
|
||||||
// ensure that test remains sane
|
|
||||||
Assert.assertThat("Large Payload Length",msg.length(),greaterThan(0xFF_FF));
|
|
||||||
|
|
||||||
// Setup Compression Method
|
|
||||||
CompressionMethod method = new DeflateCompressionMethod();
|
|
||||||
|
|
||||||
// Test round trip
|
|
||||||
assertRoundTrip(method,msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test many small payloads (each payload length less than 126 bytes).
|
|
||||||
*
|
|
||||||
* Round Trip (RT) Compress then Decompress
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testRTManySmall()
|
|
||||||
{
|
|
||||||
// Quote
|
|
||||||
List<String> quote = new ArrayList<>();
|
|
||||||
quote.add("No amount of experimentation can ever prove me right;");
|
|
||||||
quote.add("a single experiment can prove me wrong.");
|
|
||||||
quote.add("-- Albert Einstein");
|
|
||||||
|
|
||||||
// Setup Compression Method
|
|
||||||
CompressionMethod method = new DeflateCompressionMethod();
|
|
||||||
|
|
||||||
for (String msg : quote)
|
|
||||||
{
|
|
||||||
// Test round trip
|
|
||||||
assertRoundTrip(method,msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test a medium payload (a payload length between 126 - 65535 bytes).
|
|
||||||
*
|
|
||||||
* Round Trip (RT) Compress then Decompress
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testRTMedium()
|
|
||||||
{
|
|
||||||
// medium sized message
|
|
||||||
StringBuilder msg = new StringBuilder();
|
|
||||||
for (int i = 0; i < 1000; i++)
|
|
||||||
{
|
|
||||||
msg.append("0123456789ABCDEF ");
|
|
||||||
}
|
|
||||||
msg.append('X'); // so we can see the end in our debugging
|
|
||||||
|
|
||||||
// ensure that test remains sane
|
|
||||||
Assert.assertThat("Medium Payload Length",msg.length(),allOf(greaterThanOrEqualTo(0x7E),lessThanOrEqualTo(0xFF_FF)));
|
|
||||||
|
|
||||||
// Setup Compression Method
|
|
||||||
CompressionMethod method = new DeflateCompressionMethod();
|
|
||||||
|
|
||||||
// Test round trip
|
|
||||||
assertRoundTrip(method, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test a small payload (a payload length less than 126 bytes).
|
|
||||||
*
|
|
||||||
* Round Trip (RT) Compress then Decompress
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testRTSmall()
|
|
||||||
{
|
|
||||||
// Quote
|
|
||||||
StringBuilder quote = new StringBuilder();
|
|
||||||
quote.append("No amount of experimentation can ever prove me right;\n");
|
|
||||||
quote.append("a single experiment can prove me wrong.\n");
|
|
||||||
quote.append("-- Albert Einstein");
|
|
||||||
|
|
||||||
// ensure that test remains sane
|
|
||||||
Assert.assertThat("Small Payload Length",quote.length(),lessThan(0x7E));
|
|
||||||
|
|
||||||
// Setup Compression Method
|
|
||||||
CompressionMethod method = new DeflateCompressionMethod();
|
|
||||||
|
|
||||||
// Test round trip
|
|
||||||
assertRoundTrip(method,quote);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,7 +23,7 @@ import static org.hamcrest.Matchers.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.jetty.io.MappedByteBufferPool;
|
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||||
|
@ -34,27 +34,77 @@ import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||||
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
||||||
import org.eclipse.jetty.websocket.common.ByteBufferAssert;
|
import org.eclipse.jetty.websocket.common.ByteBufferAssert;
|
||||||
|
import org.eclipse.jetty.websocket.common.Hex;
|
||||||
import org.eclipse.jetty.websocket.common.IncomingFramesCapture;
|
import org.eclipse.jetty.websocket.common.IncomingFramesCapture;
|
||||||
import org.eclipse.jetty.websocket.common.OpCode;
|
import org.eclipse.jetty.websocket.common.OpCode;
|
||||||
import org.eclipse.jetty.websocket.common.OutgoingFramesCapture;
|
import org.eclipse.jetty.websocket.common.OutgoingFramesCapture;
|
||||||
|
import org.eclipse.jetty.websocket.common.Parser;
|
||||||
|
import org.eclipse.jetty.websocket.common.UnitParser;
|
||||||
import org.eclipse.jetty.websocket.common.WebSocketFrame;
|
import org.eclipse.jetty.websocket.common.WebSocketFrame;
|
||||||
import org.eclipse.jetty.websocket.common.extensions.compress.CompressionMethod.Process;
|
|
||||||
import org.eclipse.jetty.websocket.common.frames.PingFrame;
|
import org.eclipse.jetty.websocket.common.frames.PingFrame;
|
||||||
import org.eclipse.jetty.websocket.common.frames.TextFrame;
|
import org.eclipse.jetty.websocket.common.frames.TextFrame;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TestName;
|
||||||
|
|
||||||
public class MessageCompressionExtensionTest
|
public class PerMessageDeflateExtensionTest
|
||||||
{
|
{
|
||||||
|
@Rule
|
||||||
|
public TestName testname = new TestName();
|
||||||
|
|
||||||
|
private void assertIncoming(byte[] raw, String... expectedTextDatas)
|
||||||
|
{
|
||||||
|
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
|
||||||
|
|
||||||
|
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||||
|
ext.setBufferPool(new MappedByteBufferPool());
|
||||||
|
ext.setPolicy(policy);
|
||||||
|
|
||||||
|
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate; c2s_max_window_bits");
|
||||||
|
ext.setConfig(config);
|
||||||
|
|
||||||
|
// Setup capture of incoming frames
|
||||||
|
IncomingFramesCapture capture = new IncomingFramesCapture();
|
||||||
|
|
||||||
|
// Wire up stack
|
||||||
|
ext.setNextIncomingFrames(capture);
|
||||||
|
|
||||||
|
Parser parser = new UnitParser(policy);
|
||||||
|
parser.configureFromExtensions(Collections.singletonList(ext));
|
||||||
|
parser.setIncomingFramesHandler(ext);
|
||||||
|
|
||||||
|
parser.parse(ByteBuffer.wrap(raw));
|
||||||
|
|
||||||
|
int len = expectedTextDatas.length;
|
||||||
|
capture.assertFrameCount(len);
|
||||||
|
capture.assertHasFrame(OpCode.TEXT,len);
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
WebSocketFrame actual = capture.getFrames().get(i);
|
||||||
|
String prefix = "Frame[" + i + "]";
|
||||||
|
Assert.assertThat(prefix + ".opcode",actual.getOpCode(),is(OpCode.TEXT));
|
||||||
|
Assert.assertThat(prefix + ".fin",actual.isFin(),is(true));
|
||||||
|
Assert.assertThat(prefix + ".rsv1",actual.isRsv1(),is(false)); // RSV1 should be unset at this point
|
||||||
|
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
|
||||||
|
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
|
||||||
|
|
||||||
|
ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i],StringUtil.__UTF8_CHARSET);
|
||||||
|
Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining()));
|
||||||
|
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void assertDraftExample(String hexStr, String expectedStr)
|
private void assertDraftExample(String hexStr, String expectedStr)
|
||||||
{
|
{
|
||||||
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
|
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
|
||||||
|
|
||||||
// Setup extension
|
// Setup extension
|
||||||
MessageDeflateCompressionExtension ext = new MessageDeflateCompressionExtension();
|
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||||
ext.setBufferPool(new MappedByteBufferPool());
|
ext.setBufferPool(new MappedByteBufferPool());
|
||||||
ext.setPolicy(policy);
|
ext.setPolicy(policy);
|
||||||
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||||
ext.setConfig(config);
|
ext.setConfig(config);
|
||||||
|
|
||||||
// Setup capture of incoming frames
|
// Setup capture of incoming frames
|
||||||
|
@ -91,6 +141,54 @@ public class MessageCompressionExtensionTest
|
||||||
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
|
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertDraft12Example(String hexStrCompleteFrame, String... expectedStrs)
|
||||||
|
{
|
||||||
|
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
|
||||||
|
|
||||||
|
// Setup extension
|
||||||
|
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||||
|
ext.setBufferPool(new MappedByteBufferPool());
|
||||||
|
ext.setPolicy(policy);
|
||||||
|
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
|
||||||
|
ext.setConfig(config);
|
||||||
|
|
||||||
|
// Setup capture of incoming frames
|
||||||
|
IncomingFramesCapture capture = new IncomingFramesCapture();
|
||||||
|
|
||||||
|
// Wire up stack
|
||||||
|
ext.setNextIncomingFrames(capture);
|
||||||
|
|
||||||
|
// Receive frame
|
||||||
|
String hex = hexStrCompleteFrame.replaceAll("\\s*0x","");
|
||||||
|
byte net[] = TypeUtil.fromHexString(hex);
|
||||||
|
|
||||||
|
Parser parser = new UnitParser(policy);
|
||||||
|
parser.configureFromExtensions(Collections.singletonList(ext));
|
||||||
|
parser.setIncomingFramesHandler(ext);
|
||||||
|
parser.parse(ByteBuffer.wrap(net));
|
||||||
|
|
||||||
|
// Verify captured frames.
|
||||||
|
int expectedCount = expectedStrs.length;
|
||||||
|
capture.assertFrameCount(expectedCount);
|
||||||
|
capture.assertHasFrame(OpCode.TEXT,expectedCount);
|
||||||
|
|
||||||
|
for (int i = 0; i < expectedCount; i++)
|
||||||
|
{
|
||||||
|
WebSocketFrame actual = capture.getFrames().pop();
|
||||||
|
|
||||||
|
String prefix = String.format("frame[%d]",i);
|
||||||
|
Assert.assertThat(prefix + ".opcode",actual.getOpCode(),is(OpCode.TEXT));
|
||||||
|
Assert.assertThat(prefix + ".fin",actual.isFin(),is(true));
|
||||||
|
Assert.assertThat(prefix + ".rsv1",actual.isRsv1(),is(false)); // RSV1 should be unset at this point
|
||||||
|
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
|
||||||
|
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
|
||||||
|
|
||||||
|
ByteBuffer expected = BufferUtil.toBuffer(expectedStrs[i],StringUtil.__UTF8_CHARSET);
|
||||||
|
Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining()));
|
||||||
|
ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-01.
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-01.
|
||||||
*/
|
*/
|
||||||
|
@ -103,6 +201,88 @@ public class MessageCompressionExtensionTest
|
||||||
assertDraftExample(hex.toString(),"Hello");
|
assertDraftExample(hex.toString(),"Hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-12. Section 8.2.3.1
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDraft12_Hello_UnCompressedBlock()
|
||||||
|
{
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
// basic, 1 block, compressed with 0 compression level (aka, uncompressed).
|
||||||
|
hex.append("0xc1 0x07 0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00");
|
||||||
|
assertDraft12Example(hex.toString(),"Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-12. Section 8.2.3.2
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDraft12_Hello_NoSharingLZ77SlidingWindow()
|
||||||
|
{
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
// message 1
|
||||||
|
hex.append("0xc1 0x07"); // (HEADER added for this test)
|
||||||
|
hex.append("0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00");
|
||||||
|
// message 2
|
||||||
|
hex.append("0xc1 0x07"); // (HEADER added for this test)
|
||||||
|
hex.append("0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00");
|
||||||
|
assertDraft12Example(hex.toString(),"Hello","Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-12. Section 8.2.3.2
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDraft12_Hello_SharingLZ77SlidingWindow()
|
||||||
|
{
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
// message 1
|
||||||
|
hex.append("0xc1 0x07"); // (HEADER added for this test)
|
||||||
|
hex.append("0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00");
|
||||||
|
// message 2
|
||||||
|
hex.append("0xc1 0x05"); // (HEADER added for this test)
|
||||||
|
hex.append("0xf2 0x00 0x11 0x00 0x00");
|
||||||
|
assertDraft12Example(hex.toString(),"Hello","Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-12. Section 8.2.3.3
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDraft12_Hello_NoCompressionBlock()
|
||||||
|
{
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
// basic, 1 block, compressed with no compression.
|
||||||
|
hex.append("0xc1 0x0b 0x00 0x05 0x00 0xfa 0xff 0x48 0x65 0x6c 0x6c 0x6f 0x00");
|
||||||
|
assertDraft12Example(hex.toString(),"Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-12. Section 8.2.3.4
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDraft12_Hello_Bfinal1()
|
||||||
|
{
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
// basic, 1 block, compressed with BFINAL set to 1.
|
||||||
|
hex.append("0xc1 0x08"); // (HEADER added for this test)
|
||||||
|
hex.append("0xf3 0x48 0xcd 0xc9 0xc9 0x07 0x00 0x00");
|
||||||
|
assertDraft12Example(hex.toString(),"Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-12. Section 8.2.3.5
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDraft12_Hello_TwoDeflateBlocks()
|
||||||
|
{
|
||||||
|
StringBuilder hex = new StringBuilder();
|
||||||
|
hex.append("0xc1 0x0d"); // (HEADER added for this test)
|
||||||
|
// 2 deflate blocks
|
||||||
|
hex.append("0xf2 0x48 0x05 0x00 0x00 0x00 0xff 0xff 0xca 0xc9 0xc9 0x07 0x00");
|
||||||
|
assertDraft12Example(hex.toString(),"Hello");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-01.
|
* Decode payload example as seen in draft-ietf-hybi-permessage-compression-01.
|
||||||
*/
|
*/
|
||||||
|
@ -159,12 +339,24 @@ public class MessageCompressionExtensionTest
|
||||||
assertDraftExample(hex.toString(),"HelloHello");
|
assertDraftExample(hex.toString(),"HelloHello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPyWebSocket_ToraToraTora()
|
||||||
|
{
|
||||||
|
// Captured from Pywebsocket (r781) - "tora" sent 3 times.
|
||||||
|
String tora1 = "c186b0c7fe48" + "9a0ed102b4c7";
|
||||||
|
String tora2 = "c185ccb6cb50" + "e6b7a950cc";
|
||||||
|
String tora3 = "c1847b9aac69" + "79fbac69";
|
||||||
|
byte rawbuf[] = Hex.asByteArray(tora1 + tora2 + tora3);
|
||||||
|
assertIncoming(rawbuf,"tora","tora","tora");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Incoming PING (Control Frame) should pass through extension unmodified
|
* Incoming PING (Control Frame) should pass through extension unmodified
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testIncomingPing() {
|
public void testIncomingPing()
|
||||||
MessageDeflateCompressionExtension ext = new MessageDeflateCompressionExtension();
|
{
|
||||||
|
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||||
ext.setBufferPool(new MappedByteBufferPool());
|
ext.setBufferPool(new MappedByteBufferPool());
|
||||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||||
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
||||||
|
@ -201,7 +393,7 @@ public class MessageCompressionExtensionTest
|
||||||
@Test
|
@Test
|
||||||
public void testIncomingUncompressedFrames()
|
public void testIncomingUncompressedFrames()
|
||||||
{
|
{
|
||||||
MessageDeflateCompressionExtension ext = new MessageDeflateCompressionExtension();
|
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||||
ext.setBufferPool(new MappedByteBufferPool());
|
ext.setBufferPool(new MappedByteBufferPool());
|
||||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||||
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
||||||
|
@ -250,83 +442,13 @@ public class MessageCompressionExtensionTest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Verify that outgoing text frames are compressed.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testOutgoingFrames() throws IOException
|
|
||||||
{
|
|
||||||
MessageDeflateCompressionExtension ext = new MessageDeflateCompressionExtension();
|
|
||||||
ext.setBufferPool(new MappedByteBufferPool());
|
|
||||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
|
||||||
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
|
||||||
ext.setConfig(config);
|
|
||||||
|
|
||||||
// Setup capture of outgoing frames
|
|
||||||
OutgoingFramesCapture capture = new OutgoingFramesCapture();
|
|
||||||
|
|
||||||
// Wire up stack
|
|
||||||
ext.setNextOutgoingFrames(capture);
|
|
||||||
|
|
||||||
// Quote
|
|
||||||
List<String> quote = new ArrayList<>();
|
|
||||||
quote.add("No amount of experimentation can ever prove me right;");
|
|
||||||
quote.add("a single experiment can prove me wrong.");
|
|
||||||
quote.add("-- Albert Einstein");
|
|
||||||
|
|
||||||
// Expected compressed parts
|
|
||||||
List<ByteBuffer> expectedBuffers = new ArrayList<>();
|
|
||||||
CompressionMethod method = new DeflateCompressionMethod();
|
|
||||||
for(String part: quote) {
|
|
||||||
Process process = method.compress();
|
|
||||||
process.begin();
|
|
||||||
process.input(BufferUtil.toBuffer(part,StringUtil.__UTF8_CHARSET));
|
|
||||||
expectedBuffers.add(process.process());
|
|
||||||
process.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write quote as separate frames
|
|
||||||
for (String section : quote)
|
|
||||||
{
|
|
||||||
Frame frame = new TextFrame().setPayload(section);
|
|
||||||
ext.outgoingFrame(frame,null);
|
|
||||||
}
|
|
||||||
|
|
||||||
int len = quote.size();
|
|
||||||
capture.assertFrameCount(len);
|
|
||||||
capture.assertHasFrame(OpCode.TEXT,len);
|
|
||||||
|
|
||||||
String prefix;
|
|
||||||
LinkedList<WebSocketFrame> frames = capture.getFrames();
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
prefix = "Frame[" + i + "]";
|
|
||||||
WebSocketFrame actual = frames.get(i);
|
|
||||||
|
|
||||||
// Validate Frame
|
|
||||||
Assert.assertThat(prefix + ".opcode",actual.getOpCode(),is(OpCode.TEXT));
|
|
||||||
Assert.assertThat(prefix + ".fin",actual.isFin(),is(true));
|
|
||||||
Assert.assertThat(prefix + ".rsv1",actual.isRsv1(),is(true));
|
|
||||||
Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false));
|
|
||||||
Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false));
|
|
||||||
|
|
||||||
// Validate Payload
|
|
||||||
ByteBuffer expected = expectedBuffers.get(i);
|
|
||||||
// Decompress payload
|
|
||||||
ByteBuffer compressed = actual.getPayload().slice();
|
|
||||||
|
|
||||||
Assert.assertThat(prefix + ".payloadLength",compressed.remaining(),is(expected.remaining()));
|
|
||||||
ByteBufferAssert.assertEquals(prefix + ".payload",expected,compressed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outgoing PING (Control Frame) should pass through extension unmodified
|
* Outgoing PING (Control Frame) should pass through extension unmodified
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOutgoingPing() throws IOException
|
public void testOutgoingPing() throws IOException
|
||||||
{
|
{
|
||||||
MessageDeflateCompressionExtension ext = new MessageDeflateCompressionExtension();
|
PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
|
||||||
ext.setBufferPool(new MappedByteBufferPool());
|
ext.setBufferPool(new MappedByteBufferPool());
|
||||||
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
ext.setPolicy(WebSocketPolicy.newServerPolicy());
|
||||||
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
ExtensionConfig config = ExtensionConfig.parse("permessage-compress");
|
|
@ -19,7 +19,7 @@
|
||||||
package org.eclipse.jetty.websocket.server.helper;
|
package org.eclipse.jetty.websocket.server.helper;
|
||||||
|
|
||||||
import org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension;
|
import org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension;
|
||||||
import org.eclipse.jetty.websocket.common.extensions.compress.MessageDeflateCompressionExtension;
|
import org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension;
|
||||||
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
||||||
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public class EchoServlet extends WebSocketServlet
|
||||||
{
|
{
|
||||||
// Setup some extensions we want to test against
|
// Setup some extensions we want to test against
|
||||||
factory.getExtensionFactory().register("x-webkit-deflate-frame",DeflateFrameExtension.class);
|
factory.getExtensionFactory().register("x-webkit-deflate-frame",DeflateFrameExtension.class);
|
||||||
factory.getExtensionFactory().register("permessage-compress",MessageDeflateCompressionExtension.class);
|
factory.getExtensionFactory().register("permessage-compress",PerMessageDeflateExtension.class);
|
||||||
|
|
||||||
// Setup the desired Socket to use for all incoming upgrade requests
|
// Setup the desired Socket to use for all incoming upgrade requests
|
||||||
factory.register(EchoSocket.class);
|
factory.register(EchoSocket.class);
|
||||||
|
|
Loading…
Reference in New Issue