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());
}
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();
}
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));
}
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/)");
}
}
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;
}
Aggregations