Search in sources :

Example 1 with Frame

use of org.eclipse.jetty.websocket.api.extensions.Frame in project jetty.project by eclipse.

the class RFC6455ExamplesParserTest method testSingleUnmasked64KByteBinaryMessage.

@Test
public void testSingleUnmasked64KByteBinaryMessage() {
    int dataSize = 1024 * 64;
    ByteBuffer buf = ByteBuffer.allocate((dataSize + 10));
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // 64 Kbytes binary message in a single unmasked frame
    buf.put(new byte[] { (byte) 0x82, 0x7F });
    // 64bit size
    buf.putLong(dataSize);
    for (int i = 0; i < dataSize; i++) {
        buf.put((byte) 0x77);
    }
    buf.flip();
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(buf);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.BINARY, 1);
    Frame bin = capture.getFrames().poll();
    Assert.assertThat("BinaryFrame.payloadLength", bin.getPayloadLength(), is(dataSize));
    ByteBuffer data = bin.getPayload();
    Assert.assertThat("BinaryFrame.payload.length", data.remaining(), is(dataSize));
    for (int i = 0; i < dataSize; i++) {
        Assert.assertThat("BinaryFrame.payload[" + i + "]", data.get(i), is((byte) 0x77));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 2 with Frame

use of org.eclipse.jetty.websocket.api.extensions.Frame in project jetty.project by eclipse.

the class WebSocketServletRFCTest method testInternalError.

/**
     * Test the requirement of responding with server terminated close code 1011 when there is an unhandled (internal server error) being produced by the
     * WebSocket POJO.
     * @throws Exception on test failure
     */
@Test
public void testInternalError() throws Exception {
    try (BlockheadClient client = new BlockheadClient(server.getServerUri());
        StacklessLogging stackless = new StacklessLogging(EventDriver.class)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        try (StacklessLogging context = new StacklessLogging(EventDriver.class)) {
            // Generate text frame
            client.write(new TextFrame().setPayload("CRASH"));
            // Read frame (hopefully close frame)
            EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
            Frame cf = frames.poll();
            CloseInfo close = new CloseInfo(cf);
            Assert.assertThat("Close Frame.status code", close.getStatusCode(), is(StatusCode.SERVER_ERROR));
        }
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 3 with Frame

use of org.eclipse.jetty.websocket.api.extensions.Frame in project jetty.project by eclipse.

the class IncomingFramesCapture method dump.

public void dump() {
    System.err.printf("Captured %d incoming frames%n", frames.size());
    int i = 0;
    for (Frame frame : frames) {
        System.err.printf("[%3d] %s%n", i++, frame);
        System.err.printf("          payload: %s%n", BufferUtil.toDetailString(frame.getPayload()));
    }
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame)

Example 4 with Frame

use of org.eclipse.jetty.websocket.api.extensions.Frame in project jetty.project by eclipse.

the class OutgoingFramesCapture method dump.

public void dump() {
    System.out.printf("Captured %d outgoing writes%n", frames.size());
    for (int i = 0; i < frames.size(); i++) {
        Frame frame = frames.get(i);
        System.out.printf("[%3d] %s%n", i, frame);
        System.out.printf("      %s%n", BufferUtil.toDetailString(frame.getPayload()));
    }
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame)

Example 5 with Frame

use of org.eclipse.jetty.websocket.api.extensions.Frame in project jetty.project by eclipse.

the class Fuzzer method send.

public void send(List<WebSocketFrame> send) throws IOException {
    Assert.assertThat("Client connected", client.isConnected(), is(true));
    LOG.debug("[{}] Sending {} frames (mode {})", testname, send.size(), sendMode);
    if ((sendMode == SendMode.BULK) || (sendMode == SendMode.SLOW)) {
        int buflen = 0;
        for (Frame f : send) {
            buflen += f.getPayloadLength() + Generator.MAX_HEADER_LENGTH;
        }
        ByteBuffer buf = ByteBuffer.allocate(buflen);
        // Generate frames
        for (WebSocketFrame f : send) {
            setClientMask(f);
            buf.put(generator.generateHeaderBytes(f));
            if (f.hasPayload()) {
                buf.put(f.getPayload());
            }
        }
        BufferUtil.flipToFlush(buf, 0);
        // Write Data Frame
        switch(sendMode) {
            case BULK:
                client.writeRaw(buf);
                break;
            case SLOW:
                client.writeRawSlowly(buf, slowSendSegmentSize);
                break;
            default:
                throw new RuntimeException("Whoops, unsupported sendMode: " + sendMode);
        }
    } else if (sendMode == SendMode.PER_FRAME) {
        for (WebSocketFrame f : send) {
            // make sure we have mask set
            f.setMask(MASK);
            // Using lax generator, generate and send
            ByteBuffer fullframe = ByteBuffer.allocate(f.getPayloadLength() + Generator.MAX_HEADER_LENGTH);
            BufferUtil.clearToFill(fullframe);
            generator.generateWholeFrame(f, fullframe);
            BufferUtil.flipToFlush(fullframe, 0);
            client.writeRaw(fullframe);
            client.flush();
        }
    }
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Frame (org.eclipse.jetty.websocket.api.extensions.Frame)46 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)40 ByteBuffer (java.nio.ByteBuffer)39 Test (org.junit.Test)37 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)28 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)24 Parser (org.eclipse.jetty.websocket.common.Parser)22 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)21 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)11 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)11 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)9 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)8 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)7 FragmentExtension (org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension)5 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)5 OutgoingFramesCapture (org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture)5 ArrayList (java.util.ArrayList)3 Generator (org.eclipse.jetty.websocket.common.Generator)3 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)3 Extension (org.eclipse.jetty.websocket.api.extensions.Extension)2