| 201 | } |
| 202 | |
| 203 | bool websocket_client::parseheader(std::string& data) |
| 204 | { |
| 205 | if (data.size() < 4) { |
| 206 | /* Not enough data to form a frame yet */ |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | unsigned char opcode = data[0]; |
| 211 | switch (opcode & ~WS_FINBIT) { |
| 212 | case OP_CONTINUATION: |
| 213 | case OP_TEXT: |
| 214 | case OP_BINARY: |
| 215 | case OP_PING: |
| 216 | case OP_PONG: { |
| 217 | unsigned char len1 = data[1]; |
| 218 | unsigned int payloadstartoffset = 2; |
| 219 | |
| 220 | if (len1 & WS_MASKBIT) { |
| 221 | len1 &= ~WS_MASKBIT; |
| 222 | payloadstartoffset += 2; |
| 223 | /* We don't handle masked data, because discord doesn't send it */ |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /* 6 bit ("small") length frame */ |
| 228 | uint64_t len = len1; |
| 229 | |
| 230 | if (len1 == WS_PAYLOAD_LENGTH_MAGIC_LARGE) { |
| 231 | /* 24 bit ("large") length frame */ |
| 232 | if (data.length() < 8) { |
| 233 | /* We don't have a complete header yet */ |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | unsigned char len2 = (unsigned char)data[2]; |
| 238 | unsigned char len3 = (unsigned char)data[3]; |
| 239 | len = (len2 << 8) | len3; |
| 240 | |
| 241 | payloadstartoffset += 2; |
| 242 | } else if (len1 == WS_PAYLOAD_LENGTH_MAGIC_HUGE) { |
| 243 | /* 64 bit ("huge") length frame */ |
| 244 | if (data.length() < 10) { |
| 245 | /* We don't have a complete header yet */ |
| 246 | return false; |
| 247 | } |
| 248 | len = 0; |
| 249 | for (int v = 2, shift = 56; v < 10; ++v, shift -= 8) { |
| 250 | unsigned char l = (unsigned char)data[v]; |
| 251 | len |= (uint64_t)(l & 0xff) << shift; |
| 252 | } |
| 253 | payloadstartoffset += 8; |
| 254 | } |
| 255 | |
| 256 | if (data.length() < payloadstartoffset + len) { |
| 257 | /* We don't have a complete frame yet */ |
| 258 | return false; |
| 259 | } |
| 260 |
no test coverage detected