Search in sources :

Example 1 with InvalidHandshakeException

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

the class WebSocketClient method run.

public void run() {
    try {
        if (socket == null) {
            socket = new Socket(proxy);
        } else if (socket.isClosed()) {
            throw new IOException();
        }
        if (!socket.isBound()) {
            socket.connect(new InetSocketAddress(uri.getHost(), getPort()), connectTimeout);
        }
        istream = socket.getInputStream();
        ostream = socket.getOutputStream();
        sendHandshake();
    } catch (/*IOException | SecurityException | UnresolvedAddressException | InvalidHandshakeException | ClosedByInterruptException | SocketTimeoutException */
    Exception e) {
        onWebsocketError(engine, e);
        engine.closeConnection(CloseFrame.NEVER_CONNECTED, e.getMessage());
        return;
    }
    writeThread = new Thread(new WebsocketWriteThread());
    writeThread.start();
    byte[] rawbuffer = new byte[WebSocketImpl.RCVBUF];
    int readBytes;
    try {
        while (!isClosed() && (readBytes = istream.read(rawbuffer)) != -1) {
            engine.decode(ByteBuffer.wrap(rawbuffer, 0, readBytes));
        }
        engine.eot();
    } catch (IOException e) {
        engine.eot();
    } catch (RuntimeException e) {
        // this catch case covers internal errors only and indicates a bug in this websocket implementation
        onError(e);
        engine.closeConnection(CloseFrame.ABNORMAL_CLOSE, e.getMessage());
    }
    assert (socket.isClosed());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Socket(java.net.Socket) WebSocket(org.java_websocket.WebSocket) IOException(java.io.IOException) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException) NotYetConnectedException(java.nio.channels.NotYetConnectedException)

Example 2 with InvalidHandshakeException

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

the class WebSocketAdapter method getFlashPolicy.

/**
     * Gets the XML string that should be returned if a client requests a Flash
     * security policy.
     *
     * The default implementation allows access from all remote domains, but
     * only on the port that this WebSocketServer is listening on.
     *
     * This is specifically implemented for gitime's WebSocket client for Flash:
     * http://github.com/gimite/web-socket-js
     *
     * @return An XML String that comforts to Flash's security policy. You MUST
     * not include the null char at the end, it is appended automatically.
     * @throws InvalidDataException thrown when some data that is required to
     * generate the flash-policy like the websocket local port could not be
     * obtained e.g because the websocket is not connected.
     */
@Override
public String getFlashPolicy(WebSocket conn) throws InvalidDataException {
    InetSocketAddress adr = conn.getLocalSocketAddress();
    if (null == adr) {
        throw new InvalidHandshakeException("socket not bound");
    }
    StringBuffer sb = new StringBuffer(90);
    sb.append("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"");
    sb.append(adr.getPort());
    sb.append("\" /></cross-domain-policy>\0");
    return sb.toString();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException)

Example 3 with InvalidHandshakeException

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

the class WebSocketImpl method startHandshake.

public void startHandshake(ClientHandshakeBuilder handshakedata) throws InvalidHandshakeException {
    assert (readystate != READYSTATE.CONNECTING) : "shall only be called once";
    // Store the Handshake Request we are about to send
    this.handshakerequest = draft.postProcessHandshakeRequestAsClient(handshakedata);
    resourceDescriptor = handshakedata.getResourceDescriptor();
    assert (resourceDescriptor != null);
    // Notify Listener
    try {
        wsl.onWebsocketHandshakeSentAsClient(this, this.handshakerequest);
    } catch (InvalidDataException e) {
        // Stop if the client code throws an exception
        throw new InvalidHandshakeException("Handshake data rejected by client.");
    } catch (RuntimeException e) {
        wsl.onWebsocketError(this, e);
        throw new InvalidHandshakeException("rejected because of" + e);
    }
    // Send
    write(draft.createHandshake(this.handshakerequest, role));
}
Also used : InvalidDataException(org.java_websocket.exceptions.InvalidDataException) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException)

Example 4 with InvalidHandshakeException

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

the class Draft_76 method getPart.

private static byte[] getPart(String key) throws InvalidHandshakeException {
    try {
        long keyNumber = Long.parseLong(key.replaceAll("[^0-9]", ""));
        long keySpace = key.split(" ").length - 1;
        if (keySpace == 0) {
            throw new InvalidHandshakeException("invalid Sec-WebSocket-Key (/key2/)");
        }
        long part = new Long(keyNumber / keySpace);
        return new byte[] { (byte) (part >> 24), (byte) ((part << 8) >> 24), (byte) ((part << 16) >> 24), (byte) ((part << 24) >> 24) };
    } catch (NumberFormatException e) {
        throw new InvalidHandshakeException("invalid Sec-WebSocket-Key (/key1/ or /key2/)");
    }
}
Also used : InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException)

Example 5 with InvalidHandshakeException

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

the class WebSocketImpl method decodeHandshake.

/**
     * Returns whether the handshake phase has is completed. In case of a broken
     * handshake this will be never the case.
	 *
     */
private boolean decodeHandshake(ByteBuffer socketBufferNew) {
    ByteBuffer socketBuffer;
    if (tmpHandshakeBytes.capacity() == 0) {
        socketBuffer = socketBufferNew;
    } else {
        if (tmpHandshakeBytes.remaining() < socketBufferNew.remaining()) {
            ByteBuffer buf = ByteBuffer.allocate(tmpHandshakeBytes.capacity() + socketBufferNew.remaining());
            tmpHandshakeBytes.flip();
            buf.put(tmpHandshakeBytes);
            tmpHandshakeBytes = buf;
        }
        tmpHandshakeBytes.put(socketBufferNew);
        tmpHandshakeBytes.flip();
        socketBuffer = tmpHandshakeBytes;
    }
    socketBuffer.mark();
    try {
        if (draft == null) {
            HandshakeState isflashedgecase = isFlashEdgeCase(socketBuffer);
            if (isflashedgecase == HandshakeState.MATCHED) {
                try {
                    write(ByteBuffer.wrap(Charsetfunctions.utf8Bytes(wsl.getFlashPolicy(this))));
                    close(CloseFrame.FLASHPOLICY, "");
                } catch (InvalidDataException e) {
                    close(CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true);
                }
                return false;
            }
        }
        HandshakeState handshakestate = null;
        try {
            if (role == Role.SERVER) {
                if (draft == null) {
                    for (Draft d : knownDrafts) {
                        d = d.copyInstance();
                        try {
                            d.setParseMode(role);
                            socketBuffer.reset();
                            Handshakedata tmphandshake = d.translateHandshake(socketBuffer);
                            if (tmphandshake instanceof ClientHandshake == false) {
                                flushAndClose(CloseFrame.PROTOCOL_ERROR, "wrong http function", false);
                                return false;
                            }
                            ClientHandshake handshake = (ClientHandshake) tmphandshake;
                            handshakestate = d.acceptHandshakeAsServer(handshake);
                            if (handshakestate == HandshakeState.MATCHED) {
                                resourceDescriptor = handshake.getResourceDescriptor();
                                ServerHandshakeBuilder response;
                                try {
                                    response = wsl.onWebsocketHandshakeReceivedAsServer(this, d, handshake);
                                } catch (InvalidDataException e) {
                                    flushAndClose(e.getCloseCode(), e.getMessage(), false);
                                    return false;
                                } catch (RuntimeException e) {
                                    wsl.onWebsocketError(this, e);
                                    flushAndClose(CloseFrame.NEVER_CONNECTED, e.getMessage(), false);
                                    return false;
                                }
                                write(d.createHandshake(d.postProcessHandshakeResponseAsServer(handshake, response), role));
                                draft = d;
                                open(handshake);
                                return true;
                            }
                        } catch (InvalidHandshakeException e) {
                        // go on with an other draft
                        }
                    }
                    if (draft == null) {
                        close(CloseFrame.PROTOCOL_ERROR, "no draft matches");
                    }
                    return false;
                } else {
                    // special case for multiple step handshakes
                    Handshakedata tmphandshake = draft.translateHandshake(socketBuffer);
                    if (tmphandshake instanceof ClientHandshake == false) {
                        flushAndClose(CloseFrame.PROTOCOL_ERROR, "wrong http function", false);
                        return false;
                    }
                    ClientHandshake handshake = (ClientHandshake) tmphandshake;
                    handshakestate = draft.acceptHandshakeAsServer(handshake);
                    if (handshakestate == HandshakeState.MATCHED) {
                        open(handshake);
                        return true;
                    } else {
                        close(CloseFrame.PROTOCOL_ERROR, "the handshake did finaly not match");
                    }
                    return false;
                }
            } else if (role == Role.CLIENT) {
                draft.setParseMode(role);
                Handshakedata tmphandshake = draft.translateHandshake(socketBuffer);
                if (tmphandshake instanceof ServerHandshake == false) {
                    flushAndClose(CloseFrame.PROTOCOL_ERROR, "wrong http function", false);
                    return false;
                }
                ServerHandshake handshake = (ServerHandshake) tmphandshake;
                handshakestate = draft.acceptHandshakeAsClient(handshakerequest, handshake);
                if (handshakestate == HandshakeState.MATCHED) {
                    try {
                        wsl.onWebsocketHandshakeReceivedAsClient(this, handshakerequest, handshake);
                    } catch (InvalidDataException e) {
                        flushAndClose(e.getCloseCode(), e.getMessage(), false);
                        return false;
                    } catch (RuntimeException e) {
                        wsl.onWebsocketError(this, e);
                        flushAndClose(CloseFrame.NEVER_CONNECTED, e.getMessage(), false);
                        return false;
                    }
                    open(handshake);
                    return true;
                } else {
                    close(CloseFrame.PROTOCOL_ERROR, "draft " + draft + " refuses handshake");
                }
            }
        } catch (InvalidHandshakeException e) {
            close(e);
        }
    } catch (IncompleteHandshakeException e) {
        if (tmpHandshakeBytes.capacity() == 0) {
            socketBuffer.reset();
            int newsize = e.getPreferedSize();
            if (newsize == 0) {
                newsize = socketBuffer.capacity() + 16;
            } else {
                assert (e.getPreferedSize() >= socketBuffer.remaining());
            }
            tmpHandshakeBytes = ByteBuffer.allocate(newsize);
            tmpHandshakeBytes.put(socketBufferNew);
        // tmpHandshakeBytes.flip();
        } else {
            tmpHandshakeBytes.position(tmpHandshakeBytes.limit());
            tmpHandshakeBytes.limit(tmpHandshakeBytes.capacity());
        }
    }
    return false;
}
Also used : HandshakeState(org.java_websocket.drafts.Draft.HandshakeState) Draft(org.java_websocket.drafts.Draft) ServerHandshake(org.java_websocket.handshake.ServerHandshake) Handshakedata(org.java_websocket.handshake.Handshakedata) IncompleteHandshakeException(org.java_websocket.exceptions.IncompleteHandshakeException) InvalidDataException(org.java_websocket.exceptions.InvalidDataException) ServerHandshakeBuilder(org.java_websocket.handshake.ServerHandshakeBuilder) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException) ClientHandshake(org.java_websocket.handshake.ClientHandshake) ByteBuffer(java.nio.ByteBuffer)

Aggregations

InvalidHandshakeException (org.java_websocket.exceptions.InvalidHandshakeException)6 InetSocketAddress (java.net.InetSocketAddress)2 IncompleteHandshakeException (org.java_websocket.exceptions.IncompleteHandshakeException)2 InvalidDataException (org.java_websocket.exceptions.InvalidDataException)2 ServerHandshakeBuilder (org.java_websocket.handshake.ServerHandshakeBuilder)2 IOException (java.io.IOException)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 WebSocket (org.java_websocket.WebSocket)1 Draft (org.java_websocket.drafts.Draft)1 HandshakeState (org.java_websocket.drafts.Draft.HandshakeState)1 ClientHandshake (org.java_websocket.handshake.ClientHandshake)1 ClientHandshakeBuilder (org.java_websocket.handshake.ClientHandshakeBuilder)1 HandshakeBuilder (org.java_websocket.handshake.HandshakeBuilder)1 HandshakeImpl1Client (org.java_websocket.handshake.HandshakeImpl1Client)1 HandshakeImpl1Server (org.java_websocket.handshake.HandshakeImpl1Server)1 Handshakedata (org.java_websocket.handshake.Handshakedata)1 ServerHandshake (org.java_websocket.handshake.ServerHandshake)1