Consolidating WebSocketPolicy.setMax*Size() into single setMaxMessageSize()

This commit is contained in:
Joakim Erdfelt 2013-01-18 09:27:56 -07:00
parent 99c34d5227
commit 2e216dddb7
17 changed files with 59 additions and 110 deletions

View File

@ -35,33 +35,26 @@ public class WebSocketPolicy
return new WebSocketPolicy(WebSocketBehavior.SERVER);
}
/**
* The maximum allowed payload size (validated in both directions)
* <p>
* Default: 65536 (64K)
*/
private int maxPayloadSize = 64 * KB;
/**
* The maximum size of a text message during parsing/generating.
* <p>
* Default: 16384 (16 K)
* Default: 65536 (64 K)
*/
private int maxTextMessageSize = 64 * KB;
/**
* The maximum size of a binary message during parsing/generating.
* <p>
* Default: -1 (no validation)
*/
private int maxBinaryMessageSize = 64 * KB;
private long maxMessageSize = 64 * KB;
/**
* The time in ms (milliseconds) that a websocket may be idle before closing.
* <p>
* Default: 300000 (ms)
*/
private int idleTimeout = 300000;
private long idleTimeout = 300000;
/**
* The size of the input (read from network layer) buffer size.
* <p>
* Default: 4096 (4 K)
*/
private int inputBufferSize = 4 * KB;
/**
* Behavior of the websockets
@ -73,35 +66,14 @@ public class WebSocketPolicy
this.behavior = behavior;
}
public void assertValidBinaryMessageSize(int requestedSize)
public void assertValidMessageSize(int requestedSize)
{
if (maxBinaryMessageSize > 0)
if (maxMessageSize > 0)
{
// validate it
if (requestedSize > maxBinaryMessageSize)
if (requestedSize > maxMessageSize)
{
throw new MessageTooLargeException("Requested binary message size [" + requestedSize + "] exceeds maximum size [" + maxBinaryMessageSize + "]");
}
}
}
public void assertValidPayloadLength(int payloadLength)
{
// validate to buffer sizes
if (payloadLength > maxPayloadSize)
{
throw new MessageTooLargeException("Requested payload length [" + payloadLength + "] exceeds maximum size [" + maxPayloadSize + "]");
}
}
public void assertValidTextMessageSize(int requestedSize)
{
if (maxTextMessageSize > 0)
{
// validate it
if (requestedSize > maxTextMessageSize)
{
throw new MessageTooLargeException("Requested text message size [" + requestedSize + "] exceeds maximum size [" + maxTextMessageSize + "]");
throw new MessageTooLargeException("Requested message size [" + requestedSize + "] exceeds maximum size [" + maxMessageSize + "]");
}
}
}
@ -110,9 +82,8 @@ public class WebSocketPolicy
{
WebSocketPolicy clone = new WebSocketPolicy(this.behavior);
clone.idleTimeout = this.idleTimeout;
clone.maxPayloadSize = this.maxPayloadSize;
clone.maxBinaryMessageSize = this.maxBinaryMessageSize;
clone.maxTextMessageSize = this.maxTextMessageSize;
clone.maxMessageSize = this.maxMessageSize;
clone.inputBufferSize = this.inputBufferSize;
return clone;
}
@ -121,47 +92,33 @@ public class WebSocketPolicy
return behavior;
}
public int getIdleTimeout()
public long getIdleTimeout()
{
return idleTimeout;
}
public int getMaxBinaryMessageSize()
public int getInputBufferSize()
{
return maxBinaryMessageSize;
return inputBufferSize;
}
public int getMaxPayloadSize()
public long getMaxMessageSize()
{
return maxPayloadSize;
return maxMessageSize;
}
public int getMaxTextMessageSize()
{
return maxTextMessageSize;
}
public void setIdleTimeout(int idleTimeout)
public void setIdleTimeout(long idleTimeout)
{
this.idleTimeout = idleTimeout;
}
public void setMaxBinaryMessageSize(int maxBinaryMessageSize)
public void setInputBufferSize(int inputBufferSize)
{
this.maxBinaryMessageSize = maxBinaryMessageSize;
this.inputBufferSize = inputBufferSize;
}
public void setMaxPayloadSize(int maxPayloadSize)
public void setMaxMessageSize(long maxMessageSize)
{
if (maxPayloadSize < 0)
{
throw new IllegalStateException("Cannot have payload size be a negative number");
}
this.maxPayloadSize = maxPayloadSize;
}
public void setMaxTextMessageSize(int maxTextMessageSize)
{
this.maxTextMessageSize = maxTextMessageSize;
this.maxMessageSize = maxMessageSize;
}
}

View File

@ -33,11 +33,9 @@ import java.lang.annotation.Target;
{ ElementType.TYPE })
public @interface WebSocket
{
int maxBinarySize() default -2;
int maxBufferSize() default -2;
int inputBufferSize() default -2;
int maxIdleTime() default -2;
int maxTextSize() default -2;
int maxMessageSize() default -2;
}

View File

@ -97,7 +97,7 @@ public class Parser
// OMG! Sanity Check! DO NOT WANT! Won't anyone think of the memory!
throw new MessageTooLargeException("[int-sane!] cannot handle payload lengths larger than " + Integer.MAX_VALUE);
}
policy.assertValidPayloadLength((int)len);
policy.assertValidMessageSize((int)len);
switch (frame.getOpCode())
{
@ -540,7 +540,6 @@ public class Parser
{
if (payload == null)
{
getPolicy().assertValidPayloadLength(payloadLength);
frame.assertValid();
payload = bufferPool.acquire(payloadLength,false);
BufferUtil.clearToFill(payload);

View File

@ -49,13 +49,13 @@ public class AnnotatedEventDriver extends EventDriver
WebSocket anno = websocket.getClass().getAnnotation(WebSocket.class);
// Setup the policy
if (anno.maxBinarySize() > 0)
if (anno.maxMessageSize() > 0)
{
this.policy.setMaxBinaryMessageSize(anno.maxBinarySize());
this.policy.setMaxMessageSize(anno.maxMessageSize());
}
if (anno.maxTextSize() > 0)
if (anno.maxMessageSize() > 0)
{
this.policy.setMaxTextMessageSize(anno.maxTextSize());
this.policy.setInputBufferSize(anno.inputBufferSize());
}
if (anno.maxIdleTime() > 0)
{

View File

@ -140,7 +140,7 @@ public abstract class AbstractWebSocketConnection extends AbstractConnection imp
public AbstractWebSocketConnection(EndPoint endp, Executor executor, Scheduler scheduler, WebSocketPolicy policy, ByteBufferPool bufferPool)
{
super(endp,executor,EXECUTE_ONFILLABLE); // TODO review if this is best. Specially with MUX
super(endp,executor,EXECUTE_ONFILLABLE); // TODO review if this is best. Specifically with MUX
this.policy = policy;
this.bufferPool = bufferPool;
this.generator = new Generator(policy,bufferPool);
@ -151,6 +151,7 @@ public abstract class AbstractWebSocketConnection extends AbstractConnection imp
this.ioState = new IOState();
this.ioState.setState(ConnectionState.CONNECTING);
this.writeBytes = new WriteBytesProvider(generator,new FlushCallback());
this.setInputBufferSize(policy.getInputBufferSize());
}
@Override

View File

@ -67,7 +67,7 @@ public class MessageInputStream extends InputStream implements MessageAppender
return;
}
driver.getPolicy().assertValidBinaryMessageSize(size + payload.remaining());
driver.getPolicy().assertValidMessageSize(size + payload.remaining());
size += payload.remaining();
synchronized (buf)

View File

@ -61,7 +61,7 @@ public class MessageReader extends Reader implements MessageAppender
return;
}
driver.getPolicy().assertValidTextMessageSize(size + payload.remaining());
driver.getPolicy().assertValidMessageSize(size + payload.remaining());
size += payload.remaining();
synchronized (utf)

View File

@ -54,7 +54,7 @@ public class SimpleBinaryMessage implements MessageAppender
return;
}
onEvent.getPolicy().assertValidBinaryMessageSize(size + payload.remaining());
onEvent.getPolicy().assertValidMessageSize(size + payload.remaining());
size += payload.remaining();
BufferUtil.writeTo(payload,out);

View File

@ -53,7 +53,7 @@ public class SimpleTextMessage implements MessageAppender
return;
}
onEvent.getPolicy().assertValidTextMessageSize(size + payload.remaining());
onEvent.getPolicy().assertValidMessageSize(size + payload.remaining());
size += payload.remaining();
// allow for fast fail of BAD utf (incomplete utf will trigger on messageComplete)

View File

@ -25,7 +25,7 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
/**
* Example EchoSocket using Annotations.
*/
@WebSocket(maxTextSize = 64 * 1024)
@WebSocket(maxMessageSize = 64 * 1024)
public class AnnotatedEchoSocket
{
@OnWebSocketMessage

View File

@ -38,7 +38,7 @@ public class TextPayloadParserTest
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
// Artificially small buffer/payload
policy.setMaxPayloadSize(1024);
policy.setMaxMessageSize(1024);
byte utf[] = new byte[2048];
Arrays.fill(utf,(byte)'a');
@ -88,7 +88,7 @@ public class TextPayloadParserTest
buf.flip();
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setMaxPayloadSize(100000);
policy.setMaxMessageSize(100000);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);

View File

@ -451,7 +451,7 @@ public class TestABCase1_1
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
policy.setMaxMessageSize(length);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
@ -488,7 +488,7 @@ public class TestABCase1_1
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
policy.setMaxMessageSize(length);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);

View File

@ -466,7 +466,7 @@ public class TestABCase1_2
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
policy.setMaxMessageSize(length);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
@ -503,7 +503,7 @@ public class TestABCase1_2
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
policy.setMaxMessageSize(length);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);

View File

@ -36,9 +36,7 @@ public class ABServlet extends WebSocketServlet
// Test cases 9.x uses BIG frame sizes, let policy handle them.
int bigFrameSize = 20 * MBYTE;
factory.getPolicy().setMaxPayloadSize(bigFrameSize);
factory.getPolicy().setMaxTextMessageSize(bigFrameSize);
factory.getPolicy().setMaxBinaryMessageSize(bigFrameSize);
factory.getPolicy().setMaxMessageSize(bigFrameSize);
factory.register(ABSocket.class);
}

View File

@ -87,9 +87,7 @@ public class Fuzzer
int bigMessageSize = 20 * MBYTE;
policy.setMaxPayloadSize(bigMessageSize);
policy.setMaxTextMessageSize(bigMessageSize);
policy.setMaxBinaryMessageSize(bigMessageSize);
policy.setMaxMessageSize(bigMessageSize);
this.client = new BlockheadClient(policy,testcase.getServer().getServerUri());
this.generator = testcase.getLaxGenerator();

View File

@ -25,7 +25,7 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
/**
* Example Socket for echoing back Big data using the Annotation techniques along with stateless techniques.
*/
@WebSocket(maxTextSize = 64 * 1024, maxBinarySize = 64 * 1024)
@WebSocket(maxMessageSize = 64 * 1024)
public class BigEchoSocket
{
@OnWebSocketMessage

View File

@ -68,15 +68,12 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
* <dl>
* <dt>maxIdleTime</dt>
* <dd>set the time in ms that a websocket may be idle before closing<br>
* <i>Default:</i></dd>
*
* <dt>maxTextMessagesSize</dt>
* <dd>set the size in characters that a websocket may be accept before closing<br>
* <i>Default:</i></dd>
*
* <dt>maxBinaryMessagesSize</dt>
* <dt>maxMessagesSize</dt>
* <dd>set the size in bytes that a websocket may be accept before closing<br>
* <i>Default:</i></dd>
*
* <dt>inputBufferSize</dt>
* <dd>set the size in bytes of the buffer used to read raw bytes from the network layer<br>
* </dl>
*/
@SuppressWarnings("serial")
@ -105,19 +102,19 @@ public abstract class WebSocketServlet extends HttpServlet
String max = getInitParameter("maxIdleTime");
if (max != null)
{
policy.setIdleTimeout(Integer.parseInt(max));
policy.setIdleTimeout(Long.parseLong(max));
}
max = getInitParameter("maxTextMessageSize");
max = getInitParameter("maxMessageSize");
if (max != null)
{
policy.setMaxTextMessageSize(Integer.parseInt(max));
policy.setMaxMessageSize(Long.parseLong(max));
}
max = getInitParameter("maxBinaryMessageSize");
max = getInitParameter("inputBufferSize");
if (max != null)
{
policy.setMaxBinaryMessageSize(Integer.parseInt(max));
policy.setInputBufferSize(Integer.parseInt(max));
}
WebSocketServletFactory baseFactory;
@ -129,6 +126,7 @@ public abstract class WebSocketServlet extends HttpServlet
}
else
{
@SuppressWarnings("unchecked")
Class<WebSocketServletFactory> wssf = (Class<WebSocketServletFactory>)getServletContext().getClass().getClassLoader()
.loadClass("org.eclipse.jetty.websocket.server.WebSocketServerFactory");
baseFactory = wssf.newInstance();