Sends WebSocket handshake back to the given WebSocket connection.
| 52 | |
| 53 | // Sends WebSocket handshake back to the given WebSocket connection. |
| 54 | void SendHandshake(int fd, const char *request) { |
| 55 | const char webSocketGlobalGuid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; // 36 characters long |
| 56 | char key[128+sizeof(webSocketGlobalGuid)]; |
| 57 | GetHttpHeader(request, "Sec-WebSocket-Key: ", key, sizeof(key)/2); |
| 58 | strcat(key, webSocketGlobalGuid); |
| 59 | |
| 60 | char sha1[21]; |
| 61 | printf("hashing key: \"%s\"\n", key); |
| 62 | SHA1(sha1, key, (int)strlen(key)); |
| 63 | |
| 64 | char handshakeMsg[] = |
| 65 | "HTTP/1.1 101 Switching Protocols\r\n" |
| 66 | "Upgrade: websocket\r\n" |
| 67 | "Connection: Upgrade\r\n" |
| 68 | "Sec-WebSocket-Accept: 0000000000000000000000000000\r\n" |
| 69 | "\r\n"; |
| 70 | |
| 71 | base64_encode(strstr(handshakeMsg, "Sec-WebSocket-Accept: ") + strlen("Sec-WebSocket-Accept: "), sha1, 20); |
| 72 | |
| 73 | int err = send(fd, handshakeMsg, (int)strlen(handshakeMsg), 0); |
| 74 | if (err < 0) on_error("Client write failed\n"); |
| 75 | printf("Sent handshake:\n%s\n", handshakeMsg); |
| 76 | } |
| 77 | |
| 78 | // Validates if the given, possibly partially received WebSocket message has |
| 79 | // enough bytes to contain a full WebSocket header. |
no test coverage detected