/
http_websocket_server.c
229 lines (192 loc) · 6.83 KB
/
http_websocket_server.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
int http_websocket_server_init(struct http_websocket_server_t *self_p,
struct socket_t *socket_p)
{
ASSERTN(self_p != NULL, EINVAL)
ASSERTN(socket_p != NULL, EINVAL)
self_p->socket_p = socket_p;
return (0);
}
int http_websocket_server_handshake(struct http_websocket_server_t *self_p,
struct http_server_request_t *request_p)
{
ASSERTN(self_p != NULL, EINVAL)
ASSERTN(request_p != NULL, EINVAL)
char buf[160];
char accept_key[29];
const char *key_p;
struct sha1_t sha;
uint8_t hash[20];
size_t size;
static const char secret[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
/* Only the GET action is supported. */
if (request_p->action != http_server_request_action_get_t) {
return (-1);
}
/* Sec-Websocket-Key is required. */
if (request_p->headers.sec_websocket_key.present == 0) {
log_object_print(NULL,
LOG_DEBUG,
FSTR("Missing HTTP header field: Sec-Websocket-Key\r\n"));
return (-1);
}
/* Calculate the accept key. */
key_p = request_p->headers.sec_websocket_key.value;
size = strlen(key_p);
memcpy(buf, key_p, size);
memcpy(&buf[size], secret, sizeof(secret));
sha1_init(&sha);
sha1_update(&sha, buf, size + sizeof(secret) - 1);
sha1_digest(&sha, hash);
base64_encode(accept_key, hash, sizeof(hash));
accept_key[sizeof(accept_key) - 1] = '\0';
/* Format and write the websocket handshake response to the
client. */
size = std_sprintf(buf,
FSTR("HTTP/1.1 101 Switching Protocols\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: %s\r\n"
"\r\n"),
accept_key);
if (socket_write(self_p->socket_p, buf, size) != size) {
return (-EIO);
}
return (0);
}
ssize_t http_websocket_server_read(struct http_websocket_server_t *self_p,
int *type_p,
void *buf_p,
size_t size)
{
ASSERTN(self_p != NULL, EINVAL)
ASSERTN(buf_p != NULL, EINVAL)
ASSERTN(size > 0, EINVAL)
uint8_t buf[16], *b_p = buf_p;
size_t payload_left, left = size, n;
int fin = 0;
while (fin == 0) {
/* Read the next frame. */
if (socket_read(self_p->socket_p, buf, 2) != 2) {
return (-EIO);
}
fin = (buf[0] & INET_HTTP_WEBSOCKET_FIN);
payload_left = (buf[1] & ~INET_HTTP_WEBSOCKET_MASK);
if (payload_left == 126) {
if (socket_read(self_p->socket_p, &buf[2], 2) != 2) {
return (-EIO);
}
payload_left = ((uint32_t)(buf[2]) << 8 | buf[3]);
} else if (payload_left == 127) {
if (socket_read(self_p->socket_p, &buf[2], 8) != 8) {
return (-EIO);
}
payload_left = ((uint32_t)(buf[6]) << 24
| (uint32_t)(buf[7]) << 16
| (uint32_t)(buf[8]) << 8
| buf[9]);
}
/* Read the mask. */
if (buf[1] & INET_HTTP_WEBSOCKET_MASK) {
if (socket_read(self_p->socket_p, buf, 4) != 4) {
return (-EIO);
}
}
/* Read the payload. */
while ((payload_left > 0) && (left > 0)) {
if (payload_left < left) {
n = payload_left;
} else {
n = left;
}
if (socket_read(self_p->socket_p, b_p, n) != n) {
return (-1);
}
b_p += n;
left -= n;
payload_left -= n;
}
/* Discard leftover data. */
while (payload_left > 0) {
if (socket_read(self_p->socket_p, buf, 1) != 1) {
return (-1);
}
payload_left--;
}
}
return (size - left);
}
ssize_t http_websocket_server_write(struct http_websocket_server_t *self_p,
int type,
const void *buf_p,
uint32_t size)
{
ASSERTN(self_p != NULL, EINVAL)
ASSERTN(buf_p != NULL, EINVAL)
ASSERTN(size > 0, EINVAL)
const uint8_t masking_key[4] = { 0x00, 0x00, 0x00, 0x00 };
uint8_t header[16];
size_t header_size = 2;
header[0] = (INET_HTTP_WEBSOCKET_FIN | type);
if (size < 126) {
header[1] = (INET_HTTP_WEBSOCKET_MASK | size);
} else if (size < 65536) {
header[1] = (INET_HTTP_WEBSOCKET_MASK | 126);
header[2] = ((size >> 8) & 0xff);
header[3] = ((size >> 0) & 0xff);
header_size += 2;
} else {
header[1] = (INET_HTTP_WEBSOCKET_MASK | 127);
header[2] = 0;
header[3] = 0;
header[4] = 0;
header[5] = 0;
header[6] = ((size >> 24) & 0xff);
header[7] = ((size >> 16) & 0xff);
header[8] = ((size >> 8) & 0xff);
header[9] = ((size >> 0) & 0xff);
header_size += 8;
}
header[header_size + 0] = masking_key[0];
header[header_size + 1] = masking_key[1];
header[header_size + 2] = masking_key[2];
header[header_size + 3] = masking_key[3];
header_size += 4;
if (socket_write(self_p->socket_p,
header,
header_size) != header_size) {
return (-EIO);
}
if (socket_write(self_p->socket_p, buf_p, size) != size) {
return (-EIO);
}
return (size);
}