Moving maskers from core to client, where it belongs

This commit is contained in:
Joakim Erdfelt 2012-09-17 14:38:27 -07:00
parent e0e8708932
commit e46617ad8a
9 changed files with 75 additions and 44 deletions

View File

@ -26,6 +26,7 @@ import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.masks.Masker;
import org.eclipse.jetty.websocket.driver.WebSocketEventDriver;
public interface WebSocketClient
@ -36,6 +37,8 @@ public interface WebSocketClient
public WebSocketClientFactory getFactory();
public Masker getMasker();
public WebSocketPolicy getPolicy();
public UpgradeRequest getUpgradeRequest();
@ -45,4 +48,6 @@ public interface WebSocketClient
public WebSocketEventDriver getWebSocket();
public URI getWebSocketUri();
public void setMasker(Masker masker);
}

View File

@ -31,6 +31,8 @@ import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.WebSocketClientFactory;
import org.eclipse.jetty.websocket.client.masks.Masker;
import org.eclipse.jetty.websocket.client.masks.RandomMasker;
import org.eclipse.jetty.websocket.driver.WebSocketEventDriver;
/**
@ -53,6 +55,7 @@ public class IWebSocketClient extends FutureCallback<UpgradeResponse> implements
private WebSocketConnection connection;
private ClientUpgradeRequest upgradeRequest;
private ClientUpgradeResponse upgradeResponse;
private Masker masker;
public IWebSocketClient(WebSocketClientFactory factory, WebSocketEventDriver websocket)
{
@ -62,6 +65,7 @@ public class IWebSocketClient extends FutureCallback<UpgradeResponse> implements
this.policy = factory.getPolicy();
this.websocket = websocket;
this.upgradeRequest = new ClientUpgradeRequest();
this.masker = new RandomMasker();
}
@Override
@ -157,6 +161,12 @@ public class IWebSocketClient extends FutureCallback<UpgradeResponse> implements
return factory;
}
@Override
public Masker getMasker()
{
return masker;
}
/*
* (non-Javadoc)
*
@ -212,6 +222,12 @@ public class IWebSocketClient extends FutureCallback<UpgradeResponse> implements
return websocketUri;
}
@Override
public void setMasker(Masker masker)
{
this.masker = masker;
}
public void setUpgradeResponse(ClientUpgradeResponse response)
{
this.upgradeResponse = response;

View File

@ -21,9 +21,12 @@ package org.eclipse.jetty.websocket.client.internal.io;
import java.util.concurrent.Executor;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.client.WebSocketClientFactory;
import org.eclipse.jetty.websocket.client.internal.IWebSocketClient;
import org.eclipse.jetty.websocket.client.masks.Masker;
import org.eclipse.jetty.websocket.io.AbstractWebSocketConnection;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
/**
* Client side WebSocket physical connection.
@ -32,6 +35,7 @@ public class WebSocketClientConnection extends AbstractWebSocketConnection
{
private final WebSocketClientFactory factory;
private final IWebSocketClient client;
private final Masker masker;
private boolean connected;
public WebSocketClientConnection(EndPoint endp, Executor executor, IWebSocketClient client)
@ -40,6 +44,7 @@ public class WebSocketClientConnection extends AbstractWebSocketConnection
this.client = client;
this.factory = client.getFactory();
this.connected = false;
this.masker = client.getMasker();
}
public IWebSocketClient getClient()
@ -52,7 +57,7 @@ public class WebSocketClientConnection extends AbstractWebSocketConnection
{
super.onClose();
factory.sessionClosed(getSession());
}
};
@Override
public void onOpen()
@ -64,4 +69,11 @@ public class WebSocketClientConnection extends AbstractWebSocketConnection
}
super.onOpen();
}
@Override
public <C> void output(C context, Callback<C> callback, WebSocketFrame frame)
{
masker.setMask(frame);
super.output(context,callback,frame);
}
}

View File

@ -16,28 +16,31 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.masks;
package org.eclipse.jetty.websocket.client.masks;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
public class FixedMasker implements Masker
{
private final byte[] _mask;
private final byte[] mask;
public FixedMasker()
{
this(new byte[]{(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff});
this(new byte[]
{ (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff });
}
public FixedMasker(byte[] mask)
{
_mask=new byte[4];
this.mask = new byte[4];
// Copy to avoid that external code keeps a reference
// to the array parameter to modify masking on-the-fly
System.arraycopy(mask, 0, _mask, 0, 4);
System.arraycopy(mask,0,mask,0,4);
}
@Override
public void genMask(byte[] mask)
public void setMask(WebSocketFrame frame)
{
System.arraycopy(_mask, 0, mask, 0, 4);
frame.setMask(mask);
}
}

View File

@ -16,9 +16,11 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.masks;
package org.eclipse.jetty.websocket.client.masks;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
public interface Masker
{
void genMask(byte[] mask);
void setMask(WebSocketFrame frame);
}

View File

@ -16,13 +16,15 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.masks;
package org.eclipse.jetty.websocket.client.masks;
import java.util.Random;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
public class RandomMasker implements Masker
{
private final Random _random;
private final Random random;
public RandomMasker()
{
@ -31,15 +33,14 @@ public class RandomMasker implements Masker
public RandomMasker(Random random)
{
_random=random;
this.random = random;
}
@Override
public void genMask(byte[] mask)
public void setMask(WebSocketFrame frame)
{
// The assumption is that this code is always called
// with an external lock held to prevent concurrent access
// Otherwise we need to synchronize on the _random.
_random.nextBytes(mask);
byte mask[] = new byte[4];
random.nextBytes(mask);
frame.setMask(mask);
}
}

View File

@ -16,13 +16,25 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.masks;
package org.eclipse.jetty.websocket.client.masks;
import java.util.Arrays;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
public class ZeroMasker implements Masker
{
@Override
public void genMask(byte[] mask)
private final byte mask[];
public ZeroMasker()
{
mask[0]=mask[1]=mask[2]=mask[3]=0;
this.mask = new byte[4];
Arrays.fill(mask,(byte)0);
}
@Override
public void setMask(WebSocketFrame frame)
{
frame.setMask(mask);
}
}

View File

@ -18,7 +18,6 @@
package org.eclipse.jetty.websocket.api;
import org.eclipse.jetty.websocket.masks.Masker;
/**
* Settings for WebSocket operations.
@ -82,11 +81,6 @@ public class WebSocketPolicy
*/
private int idleTimeout = 300000;
/**
* The implementation for masking
*/
private Masker masker = null;
/**
* Behavior of the websockets
*/
@ -134,7 +128,6 @@ public class WebSocketPolicy
{
WebSocketPolicy clone = new WebSocketPolicy(this.behavior);
clone.autoFragment = this.autoFragment;
clone.masker = this.masker;
clone.idleTimeout = this.idleTimeout;
clone.bufferSize = this.bufferSize;
clone.maxPayloadSize = this.maxPayloadSize;
@ -158,11 +151,6 @@ public class WebSocketPolicy
return idleTimeout;
}
public Masker getMasker()
{
return masker;
}
public int getMaxBinaryMessageSize()
{
return maxBinaryMessageSize;
@ -198,11 +186,6 @@ public class WebSocketPolicy
this.idleTimeout = idleTimeout;
}
public void setMasker(Masker masker)
{
this.masker = masker;
}
public void setMaxBinaryMessageSize(int maxBinaryMessageSize)
{
this.maxBinaryMessageSize = maxBinaryMessageSize;

View File

@ -21,13 +21,12 @@ package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.masks.FixedMasker;
import org.eclipse.jetty.websocket.masks.RandomMasker;
import org.eclipse.jetty.websocket.protocol.Generator;
import org.eclipse.jetty.websocket.protocol.IncomingFramesCapture;
import org.eclipse.jetty.websocket.protocol.OpCode;
@ -78,8 +77,6 @@ public class GeneratorParserRoundtripTest
public void testParserAndGeneratorMasked() throws Exception
{
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setMasker(new RandomMasker());
ByteBufferPool bufferPool = new MappedByteBufferPool();
Generator gen = new Generator(policy,bufferPool);
Parser parser = new Parser(policy);
@ -96,7 +93,7 @@ public class GeneratorParserRoundtripTest
// Add masking
byte mask[] = new byte[4];
new FixedMasker().genMask(mask);
Arrays.fill(mask,(byte)0xFF);
frame.setMask(mask);
// Generate Buffer