78 std::vector<std::string_view> lines = absl::StrSplit(request,
"\r\n");
80 const std::vector<std::string_view> first = absl::StrSplit(lines[0],
' ');
81 if (first.size() != 3 || first[0] !=
"GET" ||
82 !absl::StartsWith(first[2],
"HTTP/1.1") || first[1].empty() ||
84 return absl::InvalidArgumentError(
"not an HTTP/1.1 GET");
87 bool connection =
false;
89 Handshake handshake{.path = std::string(first[1]), .key =
""};
90 for (std::size_t i = 1; i < lines.size(); ++i) {
91 const std::size_t colon = lines[i].find(
':');
92 if (colon == std::string_view::npos) {
95 const std::string_view name =
96 absl::StripAsciiWhitespace(lines[i].substr(0, colon));
97 const std::string_view value =
98 absl::StripAsciiWhitespace(lines[i].substr(colon + 1));
99 if (absl::EqualsIgnoreCase(name,
"Upgrade")) {
100 upgrade = HasToken(value,
"websocket");
101 }
else if (absl::EqualsIgnoreCase(name,
"Connection")) {
102 connection = HasToken(value,
"upgrade");
103 }
else if (absl::EqualsIgnoreCase(name,
"Sec-WebSocket-Version")) {
104 version = value ==
"13";
105 }
else if (absl::EqualsIgnoreCase(name,
"Sec-WebSocket-Key")) {
106 handshake.
key = std::string(value);
109 if (!upgrade || !connection) {
110 return absl::InvalidArgumentError(
"not an upgrade to websocket");
113 return absl::InvalidArgumentError(
"not WebSocket version 13");
117 if (!absl::Base64Unescape(handshake.
key, &bytes) || bytes.size() != 16) {
118 return absl::InvalidArgumentError(
"no Sec-WebSocket-Key of 16 bytes");
124 const std::string input = absl::StrCat(key, kGuid);
125 std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
126 unsigned int size = 0;
127 EVP_Digest(input.data(), input.size(), digest.data(), &size, EVP_sha1(),
129 return absl::Base64Escape(std::string(digest.begin(), digest.begin() + size));
158 static_cast<char>((fin ? 0x80 : 0) |
static_cast<uint8_t
>(opcode)));
159 AppendLength(frame, payload.size(),
true);
160 const std::array<char, 4> key = {
161 static_cast<char>(mask >> 24),
static_cast<char>((mask >> 16) & 0xFF),
162 static_cast<char>((mask >> 8) & 0xFF),
static_cast<char>(mask & 0xFF)};
163 frame.append(key.data(), key.size());
164 for (std::size_t i = 0; i < payload.size(); ++i) {
165 frame.push_back(
static_cast<char>(payload[i] ^ key[i % 4]));
184 while (status_.ok()) {
185 if (buffer_.size() < 2) {
188 const auto byte = [&](std::size_t i) {
189 return static_cast<uint8_t
>(buffer_[i]);
191 const bool fin = (byte(0) & 0x80) != 0;
192 if ((
byte(0) & 0x70) != 0) {
193 return Fail(
"a frame uses bits no extension was agreed for");
195 const auto opcode =
static_cast<Opcode>(byte(0) & 0x0F);
196 const bool control = (byte(0) & 0x08) != 0;
206 return Fail(
"a frame with an opcode the protocol doesn't have");
208 if ((
byte(1) & 0x80) == 0) {
209 return Fail(
"a client's frame has to be masked");
211 std::size_t header = 2;
212 uint64_t size = byte(1) & 0x7F;
215 if (buffer_.size() < header) {
218 size = (uint64_t{byte(2)} << 8) |
byte(3);
219 }
else if (size == 127) {
221 if (buffer_.size() < header) {
225 for (std::size_t i = 2; i < 10; ++i) {
226 size = (size << 8) |
byte(i);
229 if (control && (!fin || size > kMaxControl)) {
230 return Fail(
"a control frame has to be whole, and 125 bytes at most");
233 if (!control && fragments_.size() + size > max_) {
234 return Fail(absl::StrCat(
"a message over ", max_,
" bytes"));
236 if (buffer_.size() < header + 4 + size) {
239 const std::string_view mask(buffer_.data() + header, 4);
240 std::string payload(buffer_, header + 4, size);
241 for (std::size_t i = 0; i < payload.size(); ++i) {
242 payload[i] =
static_cast<char>(payload[i] ^ mask[i % 4]);
244 buffer_.erase(0, header + 4 + size);
246 return Message{.opcode = opcode, .payload = std::move(payload)};
250 return Fail(
"a continuation of no message");
252 }
else if (fragmented_) {
253 return Fail(
"a new message before the last one's end");
255 fragmented_ = opcode;
257 fragments_.append(payload);
259 Message message{.opcode = *fragmented_, .payload = std::move(fragments_)};