forked from longshine/ser2nets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
488 lines (428 loc) · 14.6 KB
/
http.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* ser2nets-http - A program for allowing HTTP connection to serial ports
* Copyright (C) 2011 Longshine <longxianghe@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include "selector.h"
#include "http.h"
#include "sha1.h"
#if DEBUG
#define D(...) printf(__VA_ARGS__);
#else
#define D(...)
#endif
#define IS_WEBSOCKET(hd) hd->websocket_key[0]
#define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
#define WEBSOCKET_OPCODE_CTN 0x0
#define WEBSOCKET_OPCODE_TXT 0x1
#define WEBSOCKET_OPCODE_BIN 0x2
#define WEBSOCKET_OPCODE_CLOSE 0x8
#define WEBSOCKET_OPCODE_PING 0x9
#define WEBSOCKET_OPCODE_PONG 0xA
#define WEBSOCKET_DEFAULT_OPCODE WEBSOCKET_OPCODE_TXT
#define WEBSOCKET_HEADER_SIZE 2
#define WEBSOCKET_MASK_SIZE 4
#define WEBSOCKET_UNEXTENDED_MAX_LEN 0x7D
#define WEBSOCKET_EXT16_LENGTH 0x7E
#define WEBSOCKET_EXT16_MAX_LEN 0xFFFF
#define WEBSOCKET_EXT16_SIZE 2
#define WEBSOCKET_EXT64_LENGTH 0x7F
#define WEBSOCKET_EXT64_SIZE 8
#define WEBSOCKET_UNEXTENDED(frm) ((frm)->hdr->length < WEBSOCKET_EXT16_LENGTH)
#define WEBSOCKET_EXT16(frm) (WEBSOCKET_EXT16_LENGTH == ((frm)->hdr->length & 0xFF))
#define WEBSOCKET_EXT64(frm) (WEBSOCKET_EXT64_LENGTH == ((frm)->hdr->length & 0xFF))
#define WEBSOCKET_LENGTH(frm) ((frm)->len ? (WEBSOCKET_UNEXTENDED(frm) ? (frm)->len->len7 : (WEBSOCKET_EXT16(frm) ? ntohs((frm)->len->len16) : (frm)->len->len64)) : (frm)->hdr->length)
#define WEBSOCKET_MASKED(frm) (frm->hdr->MASK)
#define WEBSOCKET_FRAME_SIZE(frm) WEBSOCKET_LENGTH(frm) + WEBSOCKET_HEADER_SIZE \
+ (WEBSOCKET_UNEXTENDED(frm) ? 0 : (WEBSOCKET_EXT16(frm) ? WEBSOCKET_EXT16_SIZE : WEBSOCKET_EXT64_SIZE)) \
+ (WEBSOCKET_MASKED(frm) ? WEBSOCKET_MASK_SIZE : 0)
#define WEBSOCKET_GET_CLOSE_REASON(frm) (ntohs(*(unsigned short *) (frm)->payload))
#define WEBSOCKET_SET_CLOSE_REASON(frm, rsn) *(unsigned short *) (frm)->payload = htons(rsn)
#define MIN(you, me) (you < me ? you : me)
extern selector_t *ser2net_sel;
static char *http_response_header = "HTTP/1.1 200 OK\r\n" \
"Server: ser2net\r\n" \
"Content-type: text/plain\r\n" \
"\r\n";
static char *websocket_response_header = "HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade: websocket\r\n" \
"Connection: Upgrade\r\n" \
"Sec-WebSocket-Accept: %s\r\n" \
"Server: ser2net\r\n" \
"\r\n";
static int
sha1(char *src, int src_len, char *dest, int dest_len)
{
SHA1Context sha;
int i;
SHA1Reset(&sha);
SHA1Input(&sha, (const unsigned char *) src, src_len);
SHA1Result(&sha);
for (i = 0; i < 5; i++) {
*(dest++) = sha.Message_Digest[i] >> 24;
*(dest++) = sha.Message_Digest[i] >> 16;
*(dest++) = sha.Message_Digest[i] >> 8;
*(dest++) = sha.Message_Digest[i];
}
return 20;
}
static void
base64_encode(char *src, int src_len, char *dest, int dest_len)
{
static char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// Create data and output buffers
int dataBuffer[3];
int outputBuffer[4];
int pos, i;
// while there are still bytes to be processed
for (pos = 0; pos < src_len;) {
memset(dataBuffer, 0, sizeof(dataBuffer));
// Create new data buffer and populate next 3 bytes from data
for (i = 0; i < 3 && pos < src_len; i++) {
dataBuffer[i] = src[pos++] & 0xFF;
}
// Convert to data buffer Base64 character positions and
// store in output buffer
outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
outputBuffer[3] = dataBuffer[2] & 0x3f;
// If data buffer was short (i.e not 3 characters) then set
// end character indexes in data buffer to index of '=' symbol.
// This is necessary because Base64 data is always a multiple of
// 4 bytes and is basses with '=' symbols.
for (; i < 3; i++) {
outputBuffer[i + 1] = 64;
}
// Loop through output buffer and add Base64 characters to
// encoded data string for each character.
for (i = 0; i < 4; i++) {
*(dest++) = base64_chars[outputBuffer[i]];
}
}
*dest = '\0';
}
static void
ws_init_frame(ws_frame_t *frm)
{
memset(frm, 0, sizeof(ws_frame_t));
frm->hdr = (ws_hdr_t *) frm->buf;
frm->hdr->FIN = 1;
frm->hdr->opcode = WEBSOCKET_DEFAULT_OPCODE;
frm->left = -1;
}
static void
ws_set_payload_length(ws_frame_t *frm, unsigned int len)
{
frm->payload = (unsigned char *)frm->hdr + WEBSOCKET_HEADER_SIZE;
if (len <= WEBSOCKET_UNEXTENDED_MAX_LEN) {
frm->hdr->length = len;
frm->len = (ws_length_t *) (frm->payload - 1);
} else if (len <= WEBSOCKET_EXT16_MAX_LEN){
frm->hdr->length = WEBSOCKET_EXT16_LENGTH;
frm->len = (ws_length_t *) frm->payload;
frm->len->len16 = htons((unsigned short) len);
frm->payload += WEBSOCKET_EXT16_SIZE;
} else {
// TODO set extended length
}
if (WEBSOCKET_MASKED(frm)) {
frm->mask = frm->payload;
frm->payload = frm->mask + WEBSOCKET_MASK_SIZE;
}
frm->left = WEBSOCKET_LENGTH(frm);
}
static void
ws_append_payload(ws_frame_t *frm, const unsigned char *data, unsigned int len)
{
int i;
// if (!frm->payload)
// ws_set_payload_length(frm, frm->length);
/* make sure that len <= left buffer size */
if (len > frm->left)
len = frm->left;
for (i = 0; i < len; i++)
frm->payload[frm->index++] = data[i];
frm->left = WEBSOCKET_LENGTH(frm) - frm->index;
}
static void
ws_unmask(ws_frame_t *frm)
{
unsigned int i;
int len = WEBSOCKET_LENGTH(frm);
for (i = 0; i < len; i++) {
frm->payload[i] = frm->payload[i] ^ frm->mask[i % WEBSOCKET_MASK_SIZE];
}
}
#if DEBUG
static void
ws_print_frame(ws_frame_t *frm)
{
int i;
unsigned char *hdr = (unsigned char *) frm->hdr;
D("FIN: %x, RSV1: %d, RSV2: %d, RSV3: %d, opcode: %d, MASK: %d, length: %d\n",
frm->hdr->FIN, frm->hdr->RSV1, frm->hdr->RSV2, frm->hdr->RSV3,
frm->hdr->opcode, frm->hdr->MASK, WEBSOCKET_LENGTH(frm))
if (WEBSOCKET_MASKED(frm) && frm->mask) {
D("mask key:")
for (i = 0; i < WEBSOCKET_MASK_SIZE; i++) {
D(" %02x", frm->mask[i])
}
D("\n")
}
if (frm->payload) {
D("payload: %s\n", frm->payload)
}
while (*hdr) {
D("%02x ", *(hdr++))
}
D("\n")
}
#endif
static int
get_header_name(char *line, char *name_buf, int len)
{
char *colon;
/* Find the : */
colon = strchr (line, ':');
/* If there's no :, it's no header */
if (!colon)
return 0;
/* If we have space in the destination to fit this header */
if ((colon - line) < len) {
/* Copy it there */
strncpy(name_buf, line, colon - line);
name_buf[colon - line] = '\0';
/* Uppercase it all */
for (colon = name_buf; *colon; colon++)
*colon = toupper(*colon);
return 1;
}
return 0;
}
static void
prepare_response_headers(http_data_t *hd, int fd)
{
if (IS_WEBSOCKET(hd)) {
/* websocket key negotiation */
char out[128] = "", tmp[32] = "";
int len;
strcpy(out, hd->websocket_key);
strcat(out, WEBSOCKET_GUID);
len = sha1(out, strlen(out), tmp, sizeof(tmp));
base64_encode(tmp, len, out, sizeof(out));
hd->response.cursize = sprintf((char *) hd->response.buf, websocket_response_header, out);
} else {
hd->response.cursize = strlen(http_response_header);
memcpy(hd->response.buf, http_response_header, hd->response.cursize);
}
}
static void
send_ws_frame(http_data_t *hd, ws_frame_t *frm)
{
//ws_print_frame(frm);
buffer_output(&hd->response, (unsigned char *) frm->hdr, WEBSOCKET_FRAME_SIZE(frm));
hd->output_ready(hd->cb_data);
}
static void
send_ws_close_frame(http_data_t *hd)
{
ws_frame_t ws_frame;
ws_init_frame(&ws_frame);
ws_frame.hdr->opcode = WEBSOCKET_OPCODE_CLOSE;
ws_set_payload_length(&ws_frame, 2);
WEBSOCKET_SET_CLOSE_REASON(&ws_frame, 1000);
hd->state = HTTP_CLOSING;
hd->output_ready(hd->cb_data);
send_ws_frame(hd, &ws_frame);
}
void
http_init(http_data_t *hd, void *cb_data,
void (*output_ready)(void *),
int (*handle_request)(void *, unsigned char *, int))
{
hd->state = HTTP_UNCONNECTED;
buffer_init(&hd->request, hd->request_buf, sizeof(hd->request_buf));
buffer_init(&hd->response, hd->response_buf, sizeof(hd->response_buf));
hd->cb_data = cb_data;
hd->output_ready = output_ready;
hd->handle_request = handle_request;
memset(hd->websocket_key, 0, sizeof(hd->websocket_key));
ws_init_frame(&hd->ws_frame);
}
char *
http_process_response(http_data_t *hd, unsigned char *buf, int len)
{
if (hd->state != HTTP_CONNECTED)
return NULL;
if (IS_WEBSOCKET(hd)){
ws_frame_t ws_frame;
ws_init_frame(&ws_frame);
ws_set_payload_length(&ws_frame, len);
ws_append_payload(&ws_frame, buf, len);
send_ws_frame(hd, &ws_frame);
} else {
/* http mode */
buffer_output(&hd->response, buf, len);
hd->output_ready(hd->cb_data);
}
return NULL;
}
char *
http_process_request(http_data_t *hd, int fd)
{
unsigned char *ret, *buf = NULL;
char line[HTTP_BUFSIZE];
int header_len;
char header[64];
char *header_value;
int i;
hd->request.cursize = read(fd, hd->request.buf + hd->request.pos, HTTP_BUFSIZE - hd->request.pos);
if (hd->request.cursize < 0) {
/* Got an error on the read, shut down the port. */
return "tcp read error";
} else if (hd->request.cursize == 0) {
/* The other end closed the port, shut it down. */
return "tcp read close";
}
hd->request.pos += hd->request.cursize;
hd->request.buf[hd->request.pos] = '\0';
if (HTTP_UNCONNECTED == hd->state) {
hd->state = HTTP_CONNECTING;
}
if (HTTP_CONNECTING == hd->state) {
/* handshaking */
buf = hd->request.buf;
for (;;) {
ret = (unsigned char *) strchr((char *) buf, '\n');
if (ret) {
header_len = ret - buf + 1;
strncpy(line, (char *) buf, header_len);
line[header_len - 2] = '\0';
hd->request.pos -= header_len;
buf = ret + 1;
// TODO process HTTP Method
/* If there is nothing left (ie: blank line),
* the headers are over. */
if (!strcmp(line, "")) {
/* request headers over, send response headers */
prepare_response_headers(hd, fd);
hd->state = HTTP_CONNECTED;
hd->output_ready(hd->cb_data);
break;
}
if (get_header_name(line, header, sizeof(header))) {
header_value = strchr(line, ':');
if (header_value) {
header_value++;
while (header_value && *header_value == ' ')
header_value++;
}
// TODO process other headers
/* store websocket key */
if (strcmp(header, "SEC-WEBSOCKET-KEY") == 0) {
strncpy(hd->websocket_key, header_value, sizeof(hd->websocket_key) - 1);
}
}
} else {
break;
}
}
} else if (HTTP_CONNECTED == hd->state) {
if (IS_WEBSOCKET(hd)){
/* decode websocket frame */
ws_frame_t *frame = &hd->ws_frame;
unsigned char *hdr;
buf = hd->request.buf;
hdr = (unsigned char *)frame->hdr;
while (hd->request.pos > 0) {
hdr[frame->index++] = *(buf++);
hd->request.pos--;
if (frame->left > 0) {
frame->left--;
}
if (1 == frame->index) {
continue;
}
if (WEBSOCKET_HEADER_SIZE == frame->index) {
frame->payload = hdr + frame->index;
if (WEBSOCKET_UNEXTENDED(frame)) {
frame->len = (ws_length_t *) (frame->payload - 1);
frame->left = WEBSOCKET_LENGTH(frame);
if (WEBSOCKET_MASKED(frame)) {
frame->left += WEBSOCKET_MASK_SIZE;
frame->mask = frame->payload;
frame->payload += WEBSOCKET_MASK_SIZE;
}
} else if (WEBSOCKET_EXT16(frame)) {
/* the following 2 bytes as a 16-bit uint are the payload length. */
frame->len = (ws_length_t *) frame->payload;
frame->payload += WEBSOCKET_EXT16_SIZE;
} else {
/* TODO the following 8 bytes as a 64-bit unit are the payload length. */
}
} else if (WEBSOCKET_HEADER_SIZE + WEBSOCKET_EXT16_SIZE == frame->index) {
if (WEBSOCKET_EXT16(frame)) {
frame->left = WEBSOCKET_LENGTH(frame);
if (WEBSOCKET_MASKED(frame)) {
frame->left += WEBSOCKET_MASK_SIZE;
frame->mask = frame->payload;
frame->payload += WEBSOCKET_MASK_SIZE;
}
}
} else if (WEBSOCKET_HEADER_SIZE + WEBSOCKET_EXT64_SIZE == frame->index) {
if (WEBSOCKET_EXT64(frame)) {
/* TODO If 127, the following 8 bytes as a 64-bit unit are the payload length. */
}
}
if (0 == frame->left) {
/* one frame read */
if (WEBSOCKET_MASKED(frame)) {
ws_unmask(frame);
}
// ws_print_frame(frame);
if (frame->hdr->opcode == WEBSOCKET_OPCODE_CLOSE) {
//printf("close reason: %d\n", WEBSOCKET_GET_CLOSE_REASON(frame));
send_ws_close_frame(hd);
} else {
hd->handle_request(hd->cb_data, frame->payload, WEBSOCKET_LENGTH(frame));
}
ws_init_frame(frame);
}
}
} else {
/* pass http payload to dev directly */
buf = hd->request.buf;
hd->handle_request(hd->cb_data, buf, hd->request.pos);
buf += hd->request.pos;
hd->request.pos = 0;
}
}
if (buf) {
for (i = 0; i < hd->request.pos; i++) {
hd->request.buf[i] = buf[i];
}
}
hd->request.buf[hd->request.pos] = '\0';
return NULL;
}