use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class DeflateFrameExtensionTest method testGeneratedTwoFrames.
@Test
public void testGeneratedTwoFrames() throws IOException {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
DeflateFrameExtension ext = new DeflateFrameExtension();
ext.setBufferPool(bufferPool);
ext.setPolicy(policy);
ext.setConfig(new ExtensionConfig(ext.getName()));
Generator generator = new Generator(policy, bufferPool, true);
generator.configureFromExtensions(Collections.singletonList(ext));
OutgoingNetworkBytesCapture capture = new OutgoingNetworkBytesCapture(generator);
ext.setNextOutgoingFrames(capture);
ext.outgoingFrame(new TextFrame().setPayload("Hello"), null, BatchMode.OFF);
ext.outgoingFrame(new TextFrame().setPayload("There"), null, BatchMode.OFF);
capture.assertBytes(0, "c107f248cdc9c90700");
}
use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class TooFastClientTest method testUpgradeWithSmallFrames.
@Test
@Ignore("RELEASE")
public void testUpgradeWithSmallFrames() throws Exception {
BlockheadClient client = new BlockheadClient(server.getServerUri());
try {
client.connect();
// Create ByteBuffer representing the initial opening network packet from the client
ByteBuffer initialPacket = ByteBuffer.allocate(4096);
BufferUtil.clearToFill(initialPacket);
// Add upgrade request to packet
StringBuilder upgradeRequest = client.generateUpgradeRequest();
ByteBuffer upgradeBuffer = BufferUtil.toBuffer(upgradeRequest.toString(), StandardCharsets.UTF_8);
initialPacket.put(upgradeBuffer);
// Add text frames
Generator generator = new Generator(WebSocketPolicy.newClientPolicy(), new LeakTrackingBufferPoolRule("Generator"));
String msg1 = "Echo 1";
String msg2 = "This is also an echooooo!";
TextFrame frame1 = new TextFrame().setPayload(msg1);
TextFrame frame2 = new TextFrame().setPayload(msg2);
// Need to set frame mask (as these are client frames)
byte[] mask = new byte[] { 0x11, 0x22, 0x33, 0x44 };
frame1.setMask(mask);
frame2.setMask(mask);
generator.generateWholeFrame(frame1, initialPacket);
generator.generateWholeFrame(frame2, initialPacket);
// Write packet to network
BufferUtil.flipToFlush(initialPacket, 0);
client.writeRaw(initialPacket);
// Expect upgrade
client.expectUpgradeResponse();
// Read frames (hopefully text frames)
EventQueue<WebSocketFrame> frames = client.readFrames(2, 1, TimeUnit.SECONDS);
WebSocketFrame tf = frames.poll();
Assert.assertThat("Text Frame/msg1", tf.getPayloadAsUTF8(), is(msg1));
tf = frames.poll();
Assert.assertThat("Text Frame/msg2", tf.getPayloadAsUTF8(), is(msg2));
} finally {
client.close();
}
}
use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class AbstractABCase method initGenerators.
@Before
public void initGenerators() {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
strictGenerator = new Generator(policy, bufferPool, true);
laxGenerator = new Generator(policy, bufferPool, false);
}
use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class FrameCaptureExtension method setConfig.
@Override
public void setConfig(ExtensionConfig config) {
super.setConfig(config);
String cfgOutputDir = config.getParameter("output-dir", null);
if (StringUtil.isNotBlank(cfgOutputDir)) {
Path path = new File(cfgOutputDir).toPath();
if (Files.isDirectory(path) && Files.exists(path) && Files.isWritable(path)) {
this.outputDir = path;
} else {
LOG.warn("Unable to configure {}: not a valid output directory", path.toAbsolutePath().toString());
}
}
String cfgPrefix = config.getParameter("prefix", "frame");
if (StringUtil.isNotBlank(cfgPrefix)) {
this.prefix = cfgPrefix;
}
if (this.outputDir != null) {
try {
Path dir = this.outputDir.toRealPath();
// create a non-validating, read-only generator
String tstamp = String.format("%1$tY%1$tm%1$td-%1$tH%1$tM%1$tS", Calendar.getInstance());
incomingFramesPath = dir.resolve(String.format("%s-%s-incoming.dat", this.prefix, tstamp));
outgoingFramesPath = dir.resolve(String.format("%s-%s-outgoing.dat", this.prefix, tstamp));
incomingChannel = Files.newByteChannel(incomingFramesPath, CREATE, WRITE);
outgoingChannel = Files.newByteChannel(outgoingFramesPath, CREATE, WRITE);
this.generator = new Generator(WebSocketPolicy.newServerPolicy(), getBufferPool(), false, true);
} catch (IOException e) {
LOG.warn("Unable to create capture file(s)", e);
}
}
}
use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class TooFastClientTest method testUpgradeWithLargeFrame.
/**
* Test where were a client sends a HTTP Upgrade to websocket AND enough websocket frame(s)
* to completely overfill the {@link org.eclipse.jetty.io.AbstractConnection#getInputBufferSize()}
* to test a situation where the WebSocket connection opens with prefill that exceeds
* the normal input buffer sizes.
* @throws Exception on test failure
*/
@Test
@Ignore("RELEASE")
public void testUpgradeWithLargeFrame() throws Exception {
BlockheadClient client = new BlockheadClient(server.getServerUri());
try {
client.connect();
// Create ByteBuffer representing the initial opening network packet from the client
ByteBuffer initialPacket = ByteBuffer.allocate(100 * 1024);
BufferUtil.clearToFill(initialPacket);
// Add upgrade request to packet
StringBuilder upgradeRequest = client.generateUpgradeRequest();
ByteBuffer upgradeBuffer = BufferUtil.toBuffer(upgradeRequest.toString(), StandardCharsets.UTF_8);
initialPacket.put(upgradeBuffer);
// Add text frames
Generator generator = new Generator(WebSocketPolicy.newClientPolicy(), new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()));
byte[] bigMsgBytes = new byte[64 * 1024];
Arrays.fill(bigMsgBytes, (byte) 'x');
String bigMsg = new String(bigMsgBytes, StandardCharsets.UTF_8);
// Need to set frame mask (as these are client frames)
byte[] mask = new byte[] { 0x11, 0x22, 0x33, 0x44 };
TextFrame frame = new TextFrame().setPayload(bigMsg);
frame.setMask(mask);
generator.generateWholeFrame(frame, initialPacket);
// Write packet to network
BufferUtil.flipToFlush(initialPacket, 0);
client.writeRaw(initialPacket);
// Expect upgrade
client.expectUpgradeResponse();
// Read frames (hopefully text frames)
EventQueue<WebSocketFrame> frames = client.readFrames(1, 1, TimeUnit.SECONDS);
WebSocketFrame tf = frames.poll();
Assert.assertThat("Text Frame/msg1", tf.getPayloadAsUTF8(), is(bigMsg));
} finally {
client.close();
}
}
Aggregations