Files
moloapp/src/google/live/ws_frame.cpp
T

133 lines
4.2 KiB
C++
Raw Normal View History

#include "google/live/ws_frame.h"
#include "google/live/caps.h"
#include "google/live/ws_error.h"
namespace molo::google::live {
using K = WsError::Kind;
std::string encode_frame(bool fin, WsOpcode opcode, const std::string& payload,
const std::uint8_t* mask_key) {
std::string out;
out.push_back((char)((fin ? 0x80 : 0x00) | (std::uint8_t)opcode));
std::uint8_t mask_bit = mask_key ? 0x80 : 0x00;
std::size_t n = payload.size();
if (n < 126) {
out.push_back((char)(mask_bit | n));
} else if (n <= 0xFFFF) {
out.push_back((char)(mask_bit | 126));
out.push_back((char)(n >> 8));
out.push_back((char)(n & 0xFF));
} else {
out.push_back((char)(mask_bit | 127));
for (int shift = 56; shift >= 0; shift -= 8)
out.push_back((char)(((std::uint64_t)n >> shift) & 0xFF));
}
if (mask_key) {
out.append(reinterpret_cast<const char*>(mask_key), 4);
for (std::size_t i = 0; i < n; ++i)
out.push_back((char)((std::uint8_t)payload[i] ^ mask_key[i & 3]));
} else {
out += payload;
}
return out;
}
bool valid_utf8(const std::string& s) {
std::size_t i = 0, n = s.size();
while (i < n) {
std::uint8_t c = (std::uint8_t)s[i];
std::size_t len;
std::uint32_t cp;
if (c < 0x80) {
++i;
continue;
} else if (c >= 0xC2 && c <= 0xDF) {
len = 2;
cp = c & 0x1F;
} else if (c >= 0xE0 && c <= 0xEF) {
len = 3;
cp = c & 0x0F;
} else if (c >= 0xF0 && c <= 0xF4) {
len = 4;
cp = c & 0x07;
} else {
return false;
}
if (i + len > n) return false;
for (std::size_t k = 1; k < len; ++k) {
std::uint8_t cc = (std::uint8_t)s[i + k];
if ((cc & 0xC0) != 0x80) return false;
cp = (cp << 6) | (cc & 0x3F);
}
if (len == 3 && (cp < 0x800 || (cp >= 0xD800 && cp <= 0xDFFF))) return false;
if (len == 4 && (cp < 0x10000 || cp > 0x10FFFF)) return false;
i += len;
}
return true;
}
void FrameDecoder::feed(const char* data, std::size_t size) {
if (pos_ > 0 && pos_ == buf_.size()) {
buf_.clear();
pos_ = 0;
} else if (pos_ > LiveCaps::kReadChunk) {
buf_.erase(0, pos_);
pos_ = 0;
}
buf_.append(data, size);
}
std::optional<WsFrame> FrameDecoder::next() {
const std::size_t avail = buf_.size() - pos_;
if (avail < 2) return std::nullopt;
const auto* p = reinterpret_cast<const std::uint8_t*>(buf_.data()) + pos_;
bool fin = p[0] & 0x80;
if (p[0] & 0x70) throw WsError(K::Protocol, "reserved bits set");
std::uint8_t op = p[0] & 0x0F;
bool known = op == 0 || op == 1 || op == 2 || (op >= 8 && op <= 10);
if (!known) throw WsError(K::Protocol, "unknown opcode");
bool masked = p[1] & 0x80;
if (masked != expect_masked_)
throw WsError(K::Protocol, expect_masked_ ? "unmasked client frame"
: "masked server frame");
std::uint64_t len = p[1] & 0x7F;
std::size_t header = 2;
if (len == 126) {
if (avail < 4) return std::nullopt;
len = ((std::uint64_t)p[2] << 8) | p[3];
header = 4;
if (len < 126) throw WsError(K::Protocol, "non-minimal length");
} else if (len == 127) {
if (avail < 10) return std::nullopt;
len = 0;
for (int i = 0; i < 8; ++i) len = (len << 8) | p[2 + i];
header = 10;
if (len >> 63) throw WsError(K::Protocol, "length high bit set");
if (len <= 0xFFFF) throw WsError(K::Protocol, "non-minimal length");
}
bool control = op >= 8;
if (control) {
if (!fin) throw WsError(K::Protocol, "fragmented control frame");
if (len > LiveCaps::kMaxControlPayload)
throw WsError(K::Protocol, "control payload over 125 bytes");
}
if (len > max_payload_) throw WsError(K::TooBig, "frame payload over cap");
std::size_t mask_len = masked ? 4 : 0;
if (avail < header + mask_len + len) return std::nullopt;
WsFrame f;
f.fin = fin;
f.opcode = op;
const std::uint8_t* key = p + header;
const std::uint8_t* body = p + header + mask_len;
f.payload.assign(reinterpret_cast<const char*>(body), (std::size_t)len);
if (masked)
for (std::size_t i = 0; i < f.payload.size(); ++i)
f.payload[i] = (char)((std::uint8_t)f.payload[i] ^ key[i & 3]);
pos_ += header + mask_len + (std::size_t)len;
return f;
}
} // namespace molo::google::live