blob: 015605e40ffe9e0e72ec6229933f783f64838709 (
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
29
30
31
32
33
34
35
36
|
#include <openssl/sha.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include "./base64.c"
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;
}
|