summaryrefslogtreecommitdiff
path: root/src/server/ws_key.c
blob: ca66b81d0f1f12f747590b6d6f52f32c200a5084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
static const char *GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

void websocket_accept_key(const char *client_key, char *output) {
  char buf[256];
  snprintf(buf, sizeof(buf), "%s%s", client_key, GUID);

  unsigned char hash[SHA_DIGEST_LENGTH];
  SHA1((unsigned char *)buf, strlen(buf), hash);
  base64_encode(hash, SHA_DIGEST_LENGTH, output);
}

char *find_websocket_key(char *req) {
    char *p = strstr(req, "Sec-WebSocket-Key:");

    if (!p) return NULL;

    p += strlen("Sec-WebSocket-Key:");

    while (*p == ' ') p++;

    static char key[128];
    int i = 0;
    while (*p && *p != '\r' && *p != '\n')
        key[i++] = *p++;

    key[i] = 0;
    return key;
}