Search in sources :

Example 1 with InvalidFrameException

use of org.java_websocket.exceptions.InvalidFrameException in project quorrabot by GloriousEggroll.

the class Draft_10 method translateSingleFrame.

public Framedata translateSingleFrame(ByteBuffer buffer) throws IncompleteException, InvalidDataException {
    int maxpacketsize = buffer.remaining();
    int realpacketsize = 2;
    if (maxpacketsize < realpacketsize) {
        throw new IncompleteException(realpacketsize);
    }
    byte b1 = buffer.get();
    boolean FIN = b1 >> 8 != 0;
    byte rsv = (byte) ((b1 & ~(byte) 128) >> 4);
    if (rsv != 0) {
        throw new InvalidFrameException("bad rsv " + rsv);
    }
    byte b2 = buffer.get();
    boolean MASK = (b2 & -128) != 0;
    int payloadlength = (byte) (b2 & ~(byte) 128);
    Opcode optcode = toOpcode((byte) (b1 & 15));
    if (!FIN) {
        if (optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING) {
            throw new InvalidFrameException("control frames may no be fragmented");
        }
    }
    if (payloadlength >= 0 && payloadlength <= 125) {
    } else {
        if (optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING) {
            throw new InvalidFrameException("more than 125 octets");
        }
        if (payloadlength == 126) {
            // additional length bytes
            realpacketsize += 2;
            if (maxpacketsize < realpacketsize) {
                throw new IncompleteException(realpacketsize);
            }
            byte[] sizebytes = new byte[3];
            sizebytes[1] = buffer.get();
            sizebytes[2] = buffer.get();
            payloadlength = new BigInteger(sizebytes).intValue();
        } else {
            // additional length bytes
            realpacketsize += 8;
            if (maxpacketsize < realpacketsize) {
                throw new IncompleteException(realpacketsize);
            }
            byte[] bytes = new byte[8];
            for (int i = 0; i < 8; i++) {
                bytes[i] = buffer.get();
            }
            long length = new BigInteger(bytes).longValue();
            if (length > Integer.MAX_VALUE) {
                throw new LimitExedeedException("Payloadsize is to big...");
            } else {
                payloadlength = (int) length;
            }
        }
    }
    // int maskskeystart = foff + realpacketsize;
    realpacketsize += (MASK ? 4 : 0);
    // int payloadstart = foff + realpacketsize;
    realpacketsize += payloadlength;
    if (maxpacketsize < realpacketsize) {
        throw new IncompleteException(realpacketsize);
    }
    ByteBuffer payload = ByteBuffer.allocate(checkAlloc(payloadlength));
    if (MASK) {
        byte[] maskskey = new byte[4];
        buffer.get(maskskey);
        for (int i = 0; i < payloadlength; i++) {
            payload.put((byte) ((byte) buffer.get() ^ (byte) maskskey[i % 4]));
        }
    } else {
        payload.put(buffer.array(), buffer.position(), payload.limit());
        buffer.position(buffer.position() + payload.limit());
    }
    FrameBuilder frame;
    if (optcode == Opcode.CLOSING) {
        frame = new CloseFrameBuilder();
    } else {
        frame = new FramedataImpl1();
        frame.setFin(FIN);
        frame.setOptcode(optcode);
    }
    payload.flip();
    frame.setPayload(payload);
    return frame;
}
Also used : Opcode(org.java_websocket.framing.Framedata.Opcode) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException) ByteBuffer(java.nio.ByteBuffer) LimitExedeedException(org.java_websocket.exceptions.LimitExedeedException) BigInteger(java.math.BigInteger) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) FrameBuilder(org.java_websocket.framing.FrameBuilder) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Example 2 with InvalidFrameException

use of org.java_websocket.exceptions.InvalidFrameException in project quorrabot by GloriousEggroll.

the class Draft_75 method translateRegularFrame.

protected List<Framedata> translateRegularFrame(ByteBuffer buffer) throws InvalidDataException {
    while (buffer.hasRemaining()) {
        byte newestByte = buffer.get();
        if (newestByte == START_OF_FRAME) {
            // Beginning of Frame
            if (readingState) {
                throw new InvalidFrameException("unexpected START_OF_FRAME");
            }
            readingState = true;
        } else if (newestByte == END_OF_FRAME) {
            // End of Frame
            if (!readingState) {
                throw new InvalidFrameException("unexpected END_OF_FRAME");
            }
            // START_OF_FRAME, thus we will send 'null' as the sent message.
            if (this.currentFrame != null) {
                currentFrame.flip();
                FramedataImpl1 curframe = new FramedataImpl1();
                curframe.setPayload(currentFrame);
                curframe.setFin(true);
                curframe.setOptcode(Opcode.TEXT);
                readyframes.add(curframe);
                this.currentFrame = null;
                buffer.mark();
            }
            readingState = false;
        } else if (readingState) {
            // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
            if (currentFrame == null) {
                currentFrame = createBuffer();
            } else if (!currentFrame.hasRemaining()) {
                currentFrame = increaseBuffer(currentFrame);
            }
            currentFrame.put(newestByte);
        } else {
            return null;
        }
    }
    // if no error occurred this block will be reached
    /*if( readingState ) {
			checkAlloc(currentFrame.position()+1);
		}*/
    List<Framedata> frames = readyframes;
    readyframes = new LinkedList<Framedata>();
    return frames;
}
Also used : Framedata(org.java_websocket.framing.Framedata) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Example 3 with InvalidFrameException

use of org.java_websocket.exceptions.InvalidFrameException in project quorrabot by GloriousEggroll.

the class Draft_76 method translateFrame.

@Override
public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException {
    buffer.mark();
    List<Framedata> frames = super.translateRegularFrame(buffer);
    if (frames == null) {
        buffer.reset();
        frames = readyframes;
        readingState = true;
        if (currentFrame == null) {
            currentFrame = ByteBuffer.allocate(2);
        } else {
            throw new InvalidFrameException();
        }
        if (buffer.remaining() > currentFrame.remaining()) {
            throw new InvalidFrameException();
        } else {
            currentFrame.put(buffer);
        }
        if (!currentFrame.hasRemaining()) {
            if (Arrays.equals(currentFrame.array(), closehandshake)) {
                frames.add(new CloseFrameBuilder(CloseFrame.NORMAL));
                return frames;
            } else {
                throw new InvalidFrameException();
            }
        } else {
            readyframes = new LinkedList<Framedata>();
            return frames;
        }
    } else {
        return frames;
    }
}
Also used : Framedata(org.java_websocket.framing.Framedata) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException)

Example 4 with InvalidFrameException

use of org.java_websocket.exceptions.InvalidFrameException in project quorrabot by GloriousEggroll.

the class CloseFrameBuilder method initMessage.

private void initMessage() throws InvalidDataException {
    if (code == CloseFrame.NOCODE) {
        reason = Charsetfunctions.stringUtf8(super.getPayloadData());
    } else {
        ByteBuffer b = super.getPayloadData();
        // because stringUtf8 also creates a mark
        int mark = b.position();
        try {
            b.position(b.position() + 2);
            reason = Charsetfunctions.stringUtf8(b);
        } catch (IllegalArgumentException e) {
            throw new InvalidFrameException(e);
        } finally {
            b.position(mark);
        }
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException)

Example 5 with InvalidFrameException

use of org.java_websocket.exceptions.InvalidFrameException in project quorrabot by GloriousEggroll.

the class CloseFrameBuilder method initCloseCode.

private void initCloseCode() throws InvalidFrameException {
    code = CloseFrame.NOCODE;
    ByteBuffer payload = super.getPayloadData();
    payload.mark();
    if (payload.remaining() >= 2) {
        ByteBuffer bb = ByteBuffer.allocate(4);
        bb.position(2);
        bb.putShort(payload.getShort());
        bb.position(0);
        code = bb.getInt();
        if (code == CloseFrame.ABNORMAL_CLOSE || code == CloseFrame.TLS_ERROR || code == CloseFrame.NOCODE || code > 4999 || code < 1000 || code == 1004) {
            throw new InvalidFrameException("closecode must not be sent over the wire: " + code);
        }
    }
    payload.reset();
}
Also used : ByteBuffer(java.nio.ByteBuffer) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException)

Aggregations

InvalidFrameException (org.java_websocket.exceptions.InvalidFrameException)5 ByteBuffer (java.nio.ByteBuffer)3 CloseFrameBuilder (org.java_websocket.framing.CloseFrameBuilder)2 Framedata (org.java_websocket.framing.Framedata)2 FramedataImpl1 (org.java_websocket.framing.FramedataImpl1)2 BigInteger (java.math.BigInteger)1 LimitExedeedException (org.java_websocket.exceptions.LimitExedeedException)1 FrameBuilder (org.java_websocket.framing.FrameBuilder)1 Opcode (org.java_websocket.framing.Framedata.Opcode)1