MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / SendHandshake

Function SendHandshake

tools/websocket_to_posix_proxy/src/main.cpp:54–76  ·  view source on GitHub ↗

Sends WebSocket handshake back to the given WebSocket connection.

Source from the content-addressed store, hash-verified

52
53// Sends WebSocket handshake back to the given WebSocket connection.
54void 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.

Callers 1

connection_threadFunction · 0.85

Calls 9

GetHttpHeaderFunction · 0.85
strcatFunction · 0.85
printfFunction · 0.85
SHA1Function · 0.85
strlenFunction · 0.85
on_errorFunction · 0.85
base64_encodeFunction · 0.70
strstrFunction · 0.50
sendFunction · 0.50

Tested by

no test coverage detected