Package org.microproxy.http
Class WebSocketFrame
java.lang.Object
org.microproxy.http.WebSocketFrame
A WebSocket frame (RFC 6455 section 5.2). Frames the proxy relays after an upgrade are shown to
HttpFilters.webSocketFrameReceived(WebSocketFrame, boolean) and HttpFilters.filterWebSocketFrame(WebSocketFrame, boolean); the latter may return
a frame built with text(String), binary(byte[]), withPayload(byte[])
and the like to send instead. Frames are immutable.
Built frames carry an unmasked payload. The proxy masks a frame when it sends it towards the
server and leaves it unmasked towards the client, as RFC 6455 requires (toWire(boolean)).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe opcode for a binary message.static final intThe opcode for closing the WebSocket connection.static final intThe opcode for a continuation of a fragmented message.static final intThe opcode for a PING control frame.static final intThe opcode for a PONG control frame.static final intThe opcode for a text message. -
Constructor Summary
ConstructorsConstructorDescriptionWebSocketFrame(byte[] header, byte[] wirePayload, long payloadLength) Creates a frame from wire data, retaining the supplied arrays without copying. -
Method Summary
Modifier and TypeMethodDescriptionstatic WebSocketFramebinary(byte[] data) A final binary frame.static WebSocketFrameA close frame with a status code (RFC 6455 section 7.4) and a reason.booleanisBinary()Checks the frame opcode.booleanisClose()Checks the frame opcode.booleanChecks the frame opcode.booleanisFinal()Checks whether this frame ends the message.booleanisMasked()Whether the payload is masked (always true for client-to-server frames).booleanisPing()Checks the frame opcode.booleanisPong()Checks the frame opcode.booleanisText()Checks the frame opcode.booleanWhether the payload was too large to buffer, in which casepayload()is null.static WebSocketFrameof(boolean fin, int rsv, int opcode, byte[] payload) A frame with the given fields and an unmasked payload.intopcode()Returns the frame opcode.byte[]payload()The unmasked application payload, ornullifisTruncated().The payload decoded as UTF-8 (meaningful for text frames), or null if truncated.longReturns the declared payload length, even when the payload was not buffered.byte[]rawBytes()The frame exactly as on the wire: header plus (masked) payload, or just the header if truncated.intrsv()The RSV1-3 bits (bit 2 = RSV1, used by permessage-deflate).static WebSocketFrameA final text frame.toString()byte[]toWire(boolean masked) The frame encoded for sending: masked with a fresh random key whenmasked(frames towards a server), unmasked otherwise.withPayload(byte[] payload) This frame's FIN bit, RSV bits and opcode with a new payload.This frame's FIN bit, RSV bits and opcode with a new payload, encoded as UTF-8.
-
Field Details
-
OPCODE_CONTINUATION
public static final int OPCODE_CONTINUATIONThe opcode for a continuation of a fragmented message.- See Also:
-
OPCODE_TEXT
public static final int OPCODE_TEXTThe opcode for a text message.- See Also:
-
OPCODE_BINARY
public static final int OPCODE_BINARYThe opcode for a binary message.- See Also:
-
OPCODE_CLOSE
public static final int OPCODE_CLOSEThe opcode for closing the WebSocket connection.- See Also:
-
OPCODE_PING
public static final int OPCODE_PINGThe opcode for a PING control frame.- See Also:
-
OPCODE_PONG
public static final int OPCODE_PONGThe opcode for a PONG control frame.- See Also:
-
-
Constructor Details
-
WebSocketFrame
public WebSocketFrame(byte[] header, byte[] wirePayload, long payloadLength) Creates a frame from wire data, retaining the supplied arrays without copying.- Parameters:
header- the frame header as on the wire (2-14 bytes)wirePayload- the payload as on the wire (still masked), ornullif the frame was too large to bufferpayloadLength- the payload length declared in the header
-
-
Method Details
-
of
A frame with the given fields and an unmasked payload.- Parameters:
fin- whether this is the final frame of the messagersv- the RSV1-3 bits (0 unless an extension defines them)opcode- the four-bit frame opcodepayload- the unmasked payload bytes, copied into the frame- Returns:
- a frame with the supplied flags and payload
-
text
A final text frame.- Parameters:
text- the text to encode as UTF-8- Returns:
- a final text frame
-
binary
A final binary frame.- Parameters:
data- the binary payload bytes, copied into the frame- Returns:
- a final binary frame
-
close
A close frame with a status code (RFC 6455 section 7.4) and a reason.- Parameters:
code- the status code to encode in two octetsreason- the close reason to encode as UTF-8- Returns:
- a final close frame containing the status and reason
-
withPayload
This frame's FIN bit, RSV bits and opcode with a new payload.- Parameters:
payload- the replacement unmasked payload, copied into the new frame- Returns:
- a new frame preserving FIN, RSV bits, and opcode
-
withText
This frame's FIN bit, RSV bits and opcode with a new payload, encoded as UTF-8.- Parameters:
text- the replacement payload text to encode as UTF-8- Returns:
- a new frame preserving FIN, RSV bits, and opcode
-
isFinal
public boolean isFinal()Checks whether this frame ends the message.- Returns:
- whether the FIN bit is set
-
rsv
public int rsv()The RSV1-3 bits (bit 2 = RSV1, used by permessage-deflate).- Returns:
- the three reserved bits, with RSV1 in bit 2
-
opcode
public int opcode()Returns the frame opcode.- Returns:
- the unsigned four-bit opcode
-
isText
public boolean isText()Checks the frame opcode.- Returns:
- whether the opcode is OPCODE_TEXT
-
isBinary
public boolean isBinary()Checks the frame opcode.- Returns:
- whether the opcode is OPCODE_BINARY
-
isContinuation
public boolean isContinuation()Checks the frame opcode.- Returns:
- whether the opcode is OPCODE_CONTINUATION
-
isClose
public boolean isClose()Checks the frame opcode.- Returns:
- whether the opcode is OPCODE_CLOSE
-
isPing
public boolean isPing()Checks the frame opcode.- Returns:
- whether the opcode is OPCODE_PING
-
isPong
public boolean isPong()Checks the frame opcode.- Returns:
- whether the opcode is OPCODE_PONG
-
isMasked
public boolean isMasked()Whether the payload is masked (always true for client-to-server frames).- Returns:
- whether the frame header sets the MASK bit
-
payloadLength
public long payloadLength()Returns the declared payload length, even when the payload was not buffered.- Returns:
- the payload length declared by the wire header, in octets
-
isTruncated
public boolean isTruncated()Whether the payload was too large to buffer, in which casepayload()is null.- Returns:
- whether the payload was omitted from the buffered frame
-
payload
public byte[] payload()The unmasked application payload, ornullifisTruncated().- Returns:
- a copy of the unmasked payload, or null if truncated
-
payloadAsText
The payload decoded as UTF-8 (meaningful for text frames), or null if truncated.- Returns:
- the UTF-8-decoded payload, or null if truncated
-
toWire
public byte[] toWire(boolean masked) The frame encoded for sending: masked with a fresh random key whenmasked(frames towards a server), unmasked otherwise.- Parameters:
masked- whether to mask the payload with a fresh random key- Returns:
- the encoded header and payload
- Throws:
IllegalStateException- if the frame is truncated
-
rawBytes
public byte[] rawBytes()The frame exactly as on the wire: header plus (masked) payload, or just the header if truncated.- Returns:
- a new array containing the original header and any buffered wire payload
-
toString
-