Search in sources :

Example 6 with InvalidHandshakeException

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

the class Draft method translateHandshakeHttp.

public static HandshakeBuilder translateHandshakeHttp(ByteBuffer buf, Role role) throws InvalidHandshakeException, IncompleteHandshakeException {
    HandshakeBuilder handshake;
    String line = readStringLine(buf);
    if (line == null) {
        throw new IncompleteHandshakeException(buf.capacity() + 128);
    }
    // eg. HTTP/1.1 101 Switching the Protocols
    String[] firstLineTokens = line.split(" ", 3);
    if (firstLineTokens.length != 3) {
        throw new InvalidHandshakeException();
    }
    if (role == Role.CLIENT) {
        // translating/parsing the response from the SERVER
        handshake = new HandshakeImpl1Server();
        ServerHandshakeBuilder serverhandshake = (ServerHandshakeBuilder) handshake;
        serverhandshake.setHttpStatus(Short.parseShort(firstLineTokens[1]));
        serverhandshake.setHttpStatusMessage(firstLineTokens[2]);
    } else {
        // translating/parsing the request from the CLIENT
        ClientHandshakeBuilder clienthandshake = new HandshakeImpl1Client();
        clienthandshake.setResourceDescriptor(firstLineTokens[1]);
        handshake = clienthandshake;
    }
    line = readStringLine(buf);
    while (line != null && line.length() > 0) {
        String[] pair = line.split(":", 2);
        if (pair.length != 2) {
            throw new InvalidHandshakeException("not an http header");
        }
        handshake.put(pair[0], pair[1].replaceFirst("^ +", ""));
        line = readStringLine(buf);
    }
    if (line == null) {
        throw new IncompleteHandshakeException();
    }
    return handshake;
}
Also used : HandshakeImpl1Server(org.java_websocket.handshake.HandshakeImpl1Server) IncompleteHandshakeException(org.java_websocket.exceptions.IncompleteHandshakeException) ClientHandshakeBuilder(org.java_websocket.handshake.ClientHandshakeBuilder) HandshakeBuilder(org.java_websocket.handshake.HandshakeBuilder) ServerHandshakeBuilder(org.java_websocket.handshake.ServerHandshakeBuilder) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException) ServerHandshakeBuilder(org.java_websocket.handshake.ServerHandshakeBuilder) HandshakeImpl1Client(org.java_websocket.handshake.HandshakeImpl1Client) ClientHandshakeBuilder(org.java_websocket.handshake.ClientHandshakeBuilder)

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