Search in sources :

Example 1 with Generator

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");
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) OutgoingNetworkBytesCapture(org.eclipse.jetty.websocket.common.test.OutgoingNetworkBytesCapture) Generator(org.eclipse.jetty.websocket.common.Generator) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 2 with Generator

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();
    }
}
Also used : LeakTrackingBufferPoolRule(org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Generator(org.eclipse.jetty.websocket.common.Generator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Generator

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);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Generator(org.eclipse.jetty.websocket.common.Generator) Before(org.junit.Before)

Example 4 with Generator

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);
        }
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) File(java.io.File) Generator(org.eclipse.jetty.websocket.common.Generator)

Example 5 with Generator

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();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) LeakTrackingByteBufferPool(org.eclipse.jetty.io.LeakTrackingByteBufferPool) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Generator(org.eclipse.jetty.websocket.common.Generator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Generator (org.eclipse.jetty.websocket.common.Generator)8 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)5 ByteBuffer (java.nio.ByteBuffer)4 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)4 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)3 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)3 Test (org.junit.Test)3 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)2 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)2 OutgoingNetworkBytesCapture (org.eclipse.jetty.websocket.common.test.OutgoingNetworkBytesCapture)2 Ignore (org.junit.Ignore)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 LeakTrackingByteBufferPool (org.eclipse.jetty.io.LeakTrackingByteBufferPool)1 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)1 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)1 LeakTrackingBufferPoolRule (org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule)1 Before (org.junit.Before)1