WebSocket++  0.8.0-dev
C++ websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Namespaces | Classes | Typedefs | Functions | Variables
websocketpp::frame Namespace Reference

Data structures and utility functions for manipulating WebSocket frames. More...

Namespaces

 limits
 Constants related to frame and payload limits.
 
 opcode
 Constants and utility functions related to WebSocket opcodes.
 

Classes

struct  basic_header
 The constant size component of a WebSocket frame header. More...
 
struct  extended_header
 The variable size component of a WebSocket frame header. More...
 
union  uint16_converter
 Two byte conversion union. More...
 
union  uint32_converter
 Four byte conversion union. More...
 
union  uint64_converter
 Eight byte conversion union. More...
 

Typedefs

typedef uint32_converter masking_key_type
 

Functions

bool get_fin (basic_header const &h)
 Check whether the frame's FIN bit is set. More...
 
void set_fin (basic_header &h, bool value)
 Set the frame's FIN bit. More...
 
bool get_rsv1 (const basic_header &h)
 check whether the frame's RSV1 bit is set More...
 
void set_rsv1 (basic_header &h, bool value)
 Set the frame's RSV1 bit. More...
 
bool get_rsv2 (const basic_header &h)
 check whether the frame's RSV2 bit is set More...
 
void set_rsv2 (basic_header &h, bool value)
 Set the frame's RSV2 bit. More...
 
bool get_rsv3 (const basic_header &h)
 check whether the frame's RSV3 bit is set More...
 
void set_rsv3 (basic_header &h, bool value)
 Set the frame's RSV3 bit. More...
 
opcode::value get_opcode (const basic_header &h)
 Extract opcode from basic header. More...
 
bool get_masked (basic_header const &h)
 check whether the frame is masked More...
 
void set_masked (basic_header &h, bool value)
 Set the frame's MASK bit. More...
 
uint8_t get_basic_size (const basic_header &h)
 Extracts the raw payload length specified in the basic header. More...
 
size_t get_header_len (basic_header const &h)
 Calculates the full length of the header based on the first bytes. More...
 
unsigned int get_masking_key_offset (const basic_header &h)
 Calculate the offset location of the masking key within the extended header. More...
 
std::string write_header (basic_header const &, extended_header const &)
 
masking_key_type get_masking_key (const basic_header &h, const extended_header &e)
 Extract the masking key from a frame header. More...
 
uint16_t get_extended_size (const extended_header &e)
 Extract the extended size field from an extended header. More...
 
uint64_t get_jumbo_size (const extended_header &e)
 Extract the jumbo size field from an extended header. More...
 
uint64_t get_payload_size (const basic_header &h, const extended_header &e)
 Extract the full payload size field from a WebSocket header. More...
 
size_t prepare_masking_key (const masking_key_type &key)
 Extract a masking key into a value the size of a machine word. More...
 
size_t circshift_prepared_key (size_t prepared_key, size_t offset)
 circularly shifts the supplied prepared masking key by offset bytes More...
 
template<typename input_iter , typename output_iter >
void byte_mask (input_iter first, input_iter last, output_iter result, masking_key_type const &key, size_t key_offset)
 Byte by byte mask/unmask. More...
 
template<typename iter_type >
void byte_mask (iter_type b, iter_type e, masking_key_type const &key, size_t key_offset)
 Byte by byte mask/unmask (in place) More...
 
void word_mask_exact (uint8_t *input, uint8_t *output, size_t length, const masking_key_type &key)
 Exact word aligned mask/unmask. More...
 
void word_mask_exact (uint8_t *data, size_t length, const masking_key_type &key)
 Exact word aligned mask/unmask (in place) More...
 
size_t word_mask_circ (uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
 Circular word aligned mask/unmask. More...
 
size_t word_mask_circ (uint8_t *data, size_t length, size_t prepared_key)
 Circular word aligned mask/unmask (in place) More...
 
std::string prepare_header (const basic_header &h, const extended_header &e)
 Generate a properly sized contiguous string that encodes a full frame header. More...
 
size_t byte_mask_circ (uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
 Circular byte aligned mask/unmask. More...
 
size_t byte_mask_circ (uint8_t *data, size_t length, size_t prepared_key)
 Circular byte aligned mask/unmask (in place) More...
 

Variables

static unsigned int const BASIC_HEADER_LENGTH = 2
 Minimum length of a WebSocket frame header.
 
static unsigned int const MAX_HEADER_LENGTH = 14
 Maximum length of a WebSocket header.
 
static unsigned int const MAX_EXTENDED_HEADER_LENGTH = 12
 Maximum length of the variable portion of the WebSocket header.
 
static uint8_t const BHB0_OPCODE = 0x0F
 
static uint8_t const BHB0_RSV3 = 0x10
 
static uint8_t const BHB0_RSV2 = 0x20
 
static uint8_t const BHB0_RSV1 = 0x40
 
static uint8_t const BHB0_FIN = 0x80
 
static uint8_t const BHB1_PAYLOAD = 0x7F
 
static uint8_t const BHB1_MASK = 0x80
 
static uint8_t const payload_size_code_16bit = 0x7E
 
static uint8_t const payload_size_code_64bit = 0x7F
 

Detailed Description

Data structures and utility functions for manipulating WebSocket frames.

namespace frame provides a number of data structures and utility functions for reading, writing, and manipulating binary encoded WebSocket frames.

Function Documentation

template<typename input_iter , typename output_iter >
void websocketpp::frame::byte_mask ( input_iter  first,
input_iter  last,
output_iter  result,
masking_key_type const &  key,
size_t  key_offset 
)

Byte by byte mask/unmask.

Iterator based byte by byte masking and unmasking for WebSocket payloads. Performs masking in place using the supplied key offset by the supplied offset number of bytes.

This function is simple and can be done in place on input with arbitrary lengths and does not vary based on machine word size. It is slow.

Parameters
bBeginning iterator to start masking
eEnding iterator to end masking
oBeginning iterator to store masked results
key32 bit key to mask with.
key_offsetoffset value to start masking at.

Definition at line 642 of file frame.hpp.

template<typename iter_type >
void websocketpp::frame::byte_mask ( iter_type  b,
iter_type  e,
masking_key_type const &  key,
size_t  key_offset 
)

Byte by byte mask/unmask (in place)

Iterator based byte by byte masking and unmasking for WebSocket payloads. Performs masking in place using the supplied key offset by the supplied offset number of bytes.

This function is simple and can be done in place on input with arbitrary lengths and does not vary based on machine word size. It is slow.

Parameters
bBeginning iterator to start masking
eEnding iterator to end masking
key32 bit key to mask with.
key_offsetoffset value to start masking at.

Definition at line 672 of file frame.hpp.

size_t websocketpp::frame::byte_mask_circ ( uint8_t *  input,
uint8_t *  output,
size_t  length,
size_t  prepared_key 
)
inline

Circular byte aligned mask/unmask.

Performs a circular mask/unmask in byte sized chunks using pre-prepared keys that store state between calls. Best for providing streaming masking or unmasking of small chunks at a time of a larger message. Requires that the underlying allocated size of the data buffer be a multiple of the word size. Data in the buffer after length will be overwritten only with the same values that were originally present.

word_mask returns a copy of prepared_key circularly shifted based on the length value. The returned value may be fed back into byte_mask when more data is available.

Parameters
dataCharacter buffer to mask
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 827 of file frame.hpp.

size_t websocketpp::frame::byte_mask_circ ( uint8_t *  data,
size_t  length,
size_t  prepared_key 
)
inline

Circular byte aligned mask/unmask (in place)

In place version of byte_mask_circ

See also
byte_mask_circ
Parameters
dataCharacter buffer to read from and write to
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 854 of file frame.hpp.

size_t websocketpp::frame::circshift_prepared_key ( size_t  prepared_key,
size_t  offset 
)
inline

circularly shifts the supplied prepared masking key by offset bytes

Prepared_key must be the output of prepare_masking_key with the associated restrictions on the machine word size. offset must be greater than or equal to zero and less than sizeof(size_t).

Definition at line 612 of file frame.hpp.

uint8_t websocketpp::frame::get_basic_size ( const basic_header &  h)
inline

Extracts the raw payload length specified in the basic header.

A basic WebSocket frame header contains a 7 bit value that represents the payload size. There are two reserved values that are used to indicate that the actual payload size will not fit in 7 bits and that the full payload size is included in a separate field. The values are as follows:

PAYLOAD_SIZE_CODE_16BIT (0x7E) indicates that the actual payload is less than 16 bit

PAYLOAD_SIZE_CODE_64BIT (0x7F) indicates that the actual payload is less than 63 bit

Parameters
[in]hBasic header to read value from.
Returns
The exact size encoded in h.

Definition at line 431 of file frame.hpp.

uint16_t websocketpp::frame::get_extended_size ( const extended_header &  e)
inline

Extract the extended size field from an extended header.

It is the responsibility of the caller to verify that e is a valid extended header. This function assumes that e contains an extended payload size.

Parameters
eThe extended header to extract from
Returns
The size encoded in the extended header in host byte order

Definition at line 540 of file frame.hpp.

bool websocketpp::frame::get_fin ( basic_header const &  h)
inline

Check whether the frame's FIN bit is set.

Parameters
[in]hThe basic header to extract from.
Returns
True if the header's fin bit is set.

Definition at line 321 of file frame.hpp.

size_t websocketpp::frame::get_header_len ( basic_header const &  h)
inline

Calculates the full length of the header based on the first bytes.

A WebSocket frame header always has at least two bytes. Encoded within the first two bytes is all the information necessary to calculate the full (variable) header length. get_header_len() calculates the full header length for the given two byte basic header.

Parameters
hBasic frame header to extract size from.
Returns
Full length of the extended header.

Definition at line 445 of file frame.hpp.

uint64_t websocketpp::frame::get_jumbo_size ( const extended_header &  e)
inline

Extract the jumbo size field from an extended header.

It is the responsibility of the caller to verify that e is a valid extended header. This function assumes that e contains a jumbo payload size.

Parameters
eThe extended header to extract from
Returns
The size encoded in the extended header in host byte order

Definition at line 555 of file frame.hpp.

bool websocketpp::frame::get_masked ( basic_header const &  h)
inline

check whether the frame is masked

Parameters
[in]hThe basic header to extract from.
Returns
True if the header mask bit is set.

Definition at line 402 of file frame.hpp.

masking_key_type websocketpp::frame::get_masking_key ( const basic_header &  h,
const extended_header &  e 
)
inline

Extract the masking key from a frame header.

Note that while read and written as an integer at times, this value is not an integer and should never be interpreted as one. Big and little endian machines will generate and store masking keys differently without issue as long as the integer values remain irrelivant.

Parameters
hThe basic header to extract from
eThe extended header to extract from
Returns
The masking key as an integer.

Definition at line 516 of file frame.hpp.

unsigned int websocketpp::frame::get_masking_key_offset ( const basic_header &  h)
inline

Calculate the offset location of the masking key within the extended header.

Calculate the offset location of the masking key within the extended header using information from its corresponding basic header

Parameters
hCorresponding basic header to calculate from.
Returns
byte offset of the first byte of the masking key

Definition at line 469 of file frame.hpp.

opcode::value websocketpp::frame::get_opcode ( const basic_header &  h)
inline

Extract opcode from basic header.

Parameters
[in]hThe basic header to extract from.
Returns
The opcode value of the header.

Definition at line 393 of file frame.hpp.

uint64_t websocketpp::frame::get_payload_size ( const basic_header &  h,
const extended_header &  e 
)
inline

Extract the full payload size field from a WebSocket header.

It is the responsibility of the caller to verify that h and e together represent a valid WebSocket frame header. This function assumes only that h and e are valid. It uses information in the basic header to determine where to look for the payload_size

Parameters
hThe basic header to extract from
eThe extended header to extract from
Returns
The size encoded in the combined header in host byte order.

Definition at line 573 of file frame.hpp.

bool websocketpp::frame::get_rsv1 ( const basic_header &  h)
inline

check whether the frame's RSV1 bit is set

Parameters
[in]hThe basic header to extract from.
Returns
True if the header's RSV1 bit is set.

Definition at line 339 of file frame.hpp.

bool websocketpp::frame::get_rsv2 ( const basic_header &  h)
inline

check whether the frame's RSV2 bit is set

Parameters
[in]hThe basic header to extract from.
Returns
True if the header's RSV2 bit is set.

Definition at line 357 of file frame.hpp.

bool websocketpp::frame::get_rsv3 ( const basic_header &  h)
inline

check whether the frame's RSV3 bit is set

Parameters
[in]hThe basic header to extract from.
Returns
True if the header's RSV3 bit is set.

Definition at line 375 of file frame.hpp.

std::string websocketpp::frame::prepare_header ( const basic_header &  h,
const extended_header &  e 
)
inline

Generate a properly sized contiguous string that encodes a full frame header.

Copy the basic header h and extended header e into a properly sized contiguous frame header string for the purposes of writing out to the wire.

Parameters
hThe basic header to include
eThe extended header to include
Returns
A contiguous string containing h and e

Definition at line 489 of file frame.hpp.

size_t websocketpp::frame::prepare_masking_key ( const masking_key_type &  key)
inline

Extract a masking key into a value the size of a machine word.

Machine word size must be 4 or 8.

Parameters
keyMasking key to extract from
Returns
prepared key as a machine word

Definition at line 595 of file frame.hpp.

void websocketpp::frame::set_fin ( basic_header &  h,
bool  value 
)
inline

Set the frame's FIN bit.

Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 330 of file frame.hpp.

void websocketpp::frame::set_masked ( basic_header &  h,
bool  value 
)
inline

Set the frame's MASK bit.

Parameters
[out]hHeader to set.
valueValue to set it to.

Definition at line 411 of file frame.hpp.

void websocketpp::frame::set_rsv1 ( basic_header &  h,
bool  value 
)
inline

Set the frame's RSV1 bit.

Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 348 of file frame.hpp.

void websocketpp::frame::set_rsv2 ( basic_header &  h,
bool  value 
)
inline

Set the frame's RSV2 bit.

Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 366 of file frame.hpp.

void websocketpp::frame::set_rsv3 ( basic_header &  h,
bool  value 
)
inline

Set the frame's RSV3 bit.

Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 384 of file frame.hpp.

size_t websocketpp::frame::word_mask_circ ( uint8_t *  input,
uint8_t *  output,
size_t  length,
size_t  prepared_key 
)
inline

Circular word aligned mask/unmask.

Performs a circular mask/unmask in word sized chunks using pre-prepared keys that store state between calls. Best for providing streaming masking or unmasking of small chunks at a time of a larger message. Requires that the underlying allocated size of the data buffer be a multiple of the word size. Data in the buffer after length will be overwritten only with the same values that were originally present.

Buffer based word by word masking and unmasking for WebSocket payloads. Performs masking in place using the supplied key. Casts the data buffer to an array of size_t's and performs masking word by word. The underlying buffer size must be a muliple of the word size.

word_mask returns a copy of prepared_key circularly shifted based on the length value. The returned value may be fed back into word_mask when more data is available.

input and output must both have length at least: ceil(length/sizeof(size_t))*sizeof(size_t) Exactly that many bytes will be written, although only exactly length bytes will be changed (trailing bytes will be replaced without masking)

Parameters
dataCharacter buffer to mask
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 765 of file frame.hpp.

size_t websocketpp::frame::word_mask_circ ( uint8_t *  data,
size_t  length,
size_t  prepared_key 
)
inline

Circular word aligned mask/unmask (in place)

In place version of word_mask_circ

See also
word_mask_circ
Parameters
dataCharacter buffer to read from and write to
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 802 of file frame.hpp.

void websocketpp::frame::word_mask_exact ( uint8_t *  input,
uint8_t *  output,
size_t  length,
const masking_key_type &  key 
)
inline

Exact word aligned mask/unmask.

Balanced combination of byte by byte and circular word by word masking. Best used to mask complete messages at once. Has much higher setup costs than word_mask_circ but works with exact sized buffers.

Buffer based word by word masking and unmasking for WebSocket payloads. Masking is done in word by word chunks with the remainder not divisible by the word size done byte by byte.

input and output must both be at least length bytes. Exactly length bytes will be written.

Parameters
inputbuffer to mask or unmask
outputbuffer to store the output. May be the same as input.
lengthlength of data buffer
keyMasking key to use

Definition at line 699 of file frame.hpp.

void websocketpp::frame::word_mask_exact ( uint8_t *  data,
size_t  length,
const masking_key_type &  key 
)
inline

Exact word aligned mask/unmask (in place)

In place version of word_mask_exact

See also
word_mask_exact
Parameters
databuffer to read and write from
lengthlength of data buffer
keyMasking key to use

Definition at line 728 of file frame.hpp.