use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.
the class Draft method continuousFrame.
public List<Framedata> continuousFrame(Opcode op, ByteBuffer buffer, boolean fin) {
if (op != Opcode.BINARY && op != Opcode.TEXT && op != Opcode.TEXT) {
throw new IllegalArgumentException("Only Opcode.BINARY or Opcode.TEXT are allowed");
}
if (continuousFrameType != null) {
continuousFrameType = Opcode.CONTINUOUS;
} else {
continuousFrameType = op;
}
FrameBuilder bui = new FramedataImpl1(continuousFrameType);
try {
bui.setPayload(buffer);
} catch (InvalidDataException e) {
// can only happen when one builds close frames(Opcode.Close)
throw new RuntimeException(e);
}
bui.setFin(fin);
if (fin) {
continuousFrameType = null;
} else {
continuousFrameType = op;
}
return Collections.singletonList((Framedata) bui);
}
use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.
the class Draft_10 method createFrames.
@Override
public List<Framedata> createFrames(String text, boolean mask) {
FrameBuilder curframe = new FramedataImpl1();
try {
curframe.setPayload(ByteBuffer.wrap(Charsetfunctions.utf8Bytes(text)));
} catch (InvalidDataException e) {
throw new NotSendableException(e);
}
curframe.setFin(true);
curframe.setOptcode(Opcode.TEXT);
curframe.setTransferemasked(mask);
return Collections.singletonList((Framedata) curframe);
}
use of org.java_websocket.exceptions.InvalidDataException 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.InvalidDataException in project quorrabot by GloriousEggroll.
the class WebSocketImpl method close.
private void close(int code, String message, boolean remote) {
if (readystate != READYSTATE.CLOSING && readystate != READYSTATE.CLOSED) {
if (readystate == READYSTATE.OPEN) {
if (code == CloseFrame.ABNORMAL_CLOSE) {
assert (remote == false);
readystate = READYSTATE.CLOSING;
flushAndClose(code, message, false);
return;
}
if (draft.getCloseHandshakeType() != CloseHandshakeType.NONE) {
try {
if (!remote) {
try {
wsl.onWebsocketCloseInitiated(this, code, message);
} catch (RuntimeException e) {
wsl.onWebsocketError(this, e);
}
}
sendFrame(new CloseFrameBuilder(code, message));
} catch (InvalidDataException e) {
wsl.onWebsocketError(this, e);
flushAndClose(CloseFrame.ABNORMAL_CLOSE, "generated frame is invalid", false);
}
}
flushAndClose(code, message, remote);
} else if (code == CloseFrame.FLASHPOLICY) {
assert (remote);
flushAndClose(CloseFrame.FLASHPOLICY, message, true);
} else {
flushAndClose(CloseFrame.NEVER_CONNECTED, message, false);
}
if (// this endpoint found a PROTOCOL_ERROR
code == CloseFrame.PROTOCOL_ERROR) {
flushAndClose(code, message, remote);
}
readystate = READYSTATE.CLOSING;
tmpHandshakeBytes = null;
return;
}
}
use of org.java_websocket.exceptions.InvalidDataException 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