This repository was archived by the owner on Apr 9, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhttp.c
More file actions
582 lines (488 loc) · 16.3 KB
/
Copy pathhttp.c
File metadata and controls
582 lines (488 loc) · 16.3 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "file.h"
#include "redir.h"
#include "http.h"
#include "config.h"
#define BUFSIZE 4096
/********** Internal types **********/
/**
* HTTP method in an HTTP request.
*/
enum HttpMethod {
HTTP_METHOD_UNKNOWN, ///< Unknown verb, used to indicate a non-HTTP message
HTTP_GET, ///< HTTP GET request
HTTP_HEAD, ///< HTTP HEAD request
HTTP_POST, ///< HTTP POST request
HTTP_DELETE, ///< HTTP DELETE request
HTCPCP_BREW, ///< HTCPCP BREW request (I'm a teapot!)
};
/**
* HTTP status code in an HTTP response.
*/
enum HttpStatusCode {
HTTP_STATUS_UNKNOWN, ///< Unknown HTTP status code
HTTP_STATUS_OK, ///< HTTP 200
HTTP_STATUS_NO_CONTENT, ///< HTTP 204
HTTP_STATUS_MOVED_PERMANENTLY, ///< HTTP 301
HTTP_STATUS_FOUND, ///< HTTP 302
HTTP_STATUS_BAD_REQUEST, ///< HTTP 400
HTTP_STATUS_FORBIDDEN, ///< HTTP 403
HTTP_STATUS_NOT_FOUND, ///< HTTP 404
HTTP_STATUS_METHOD_NOT_ALLOWED, ///< HTTP 405
HTTP_STATUS_INTERNAL_SERVER_ERROR, ///< HTTP 500
HTCPCP_STATUS_I_AM_A_TEAPOT, ///< HTCPCP 418 :)
};
/**
* HTTP header indicator, mainly for use in http_extract_header().
*/
enum RequestHeader {
HTTP_HOST, ///< "Host: "
HTTP_CONTENT_TYPE, ///< "Content-Type: "
HTTP_HEADER_CONTENT_LENGTH, ///< "Content-Length: "
HTTP_HEADER_EXPECT, ///< "Expect: "
};
/**
* An HTTP request entity.
*/
struct HttpRequest {
// Request line
enum HttpMethod method;
char *path;
char *version;
// Header fields
char *host;
char *content_type;
size_t content_length;
char *expect;
// Content
uint8_t *content;
};
/**
* An HTTP response entity.
*/
struct HttpResponse {
// Status line
enum HttpStatusCode status_code;
// Header fields
char *content_type;
size_t content_length;
char *connection;
char *location;
char *allow;
// Content
uint8_t *content;
};
/********** Internal states (variables) **********/
/**
* HTTP version. This implementation sticks to HTTP/1.1, so there is no way to
* change this string...
*/
#define HTTP_VERSION "HTTP/1.1"
/**
* Hyper Text Coffee Pot Control Protocol version.
*/
#define HTCPCP_VERSION "HTCPCP/1.0"
/* String constants of HTTP contents for internal use */
static const char *http_status_ok = HTTP_VERSION " 200 OK";
static const char *http_status_no_content = HTTP_VERSION " 204 No Content";
static const char *http_status_moved_permanently = HTTP_VERSION " 301 Moved Permanently";
static const char *http_status_found = HTTP_VERSION " 302 Found";
static const char *http_status_bad_request = HTTP_VERSION " 400 Bad Request";
static const char *http_status_forbidden = HTTP_VERSION " 403 Forbidden";
static const char *http_status_not_found = HTTP_VERSION " 404 Not Found";
static const char *http_status_method_not_allowed = HTTP_VERSION " 405 Method Not Allowed";
static const char *http_status_internal_server_error = HTTP_VERSION " 500 Server Internal Error";
static const char *http_get = "GET";
static const char *http_head = "HEAD";
static const char *http_post = "POST";
static const char *http_delete = "DELETE";
static const char *htcpcp_status_i_am_a_teapot = HTCPCP_VERSION " 418 I'm a teapot";
static const char *htcpcp_brew = "BREW";
static const char *http_header_host = "Host: ";
static const char *http_header_content_type = "Content-Type: ";
static const char *http_header_content_length = "Content-Length: ";
static const char *http_header_expect = "Expect: ";
static const char *http_status_not_found_html =
"<!DOCTYPE html>\r\n"
"<html>\r\n"
"<head>\r\n"
" <meta charset=\"utf-8\" />\r\n"
" <title>404 Not Found</title>\r\n"
"</head>\r\n"
"<body style=\"text-align: center\">\r\n"
" <h1>404 Not Found</h1>\r\n"
" <hr />\r\n"
" <p>" TEAPOT_NAME "/" TEAPOT_VERSION "</p>\r\n"
"</body>\r\n"
"</html>\r\n";
/********** Private APIs **********/
/**
* Convert string into integer.
*/
static int toInteger(char a[]) {
int c = '\0', sign = 0, offset = 0, n = 0;
if (a[0] == '-') sign = -1;
if (sign == -1) offset = 1;
else offset = 0;
n = 0;
for (c = offset; a[c] != '\0'; c++) {
n = n * 10 + a[c] - '0';
}
if (sign == -1) n = -n;
return n;
}
/**
* Convert enumeration HttpMethod to string.
*
* @param method [in] HttpMethod to convert.
* @return A string constant representing the HTTP method. The caller should not
* free the string!
*/
static const char *http_method_to_string(enum HttpMethod method)
{
const char *ret = NULL;
switch (method) {
case HTTP_GET:
ret = http_get;
break;
case HTTP_HEAD:
ret = http_head;
break;
case HTTP_POST:
ret = http_post;
break;
case HTTP_DELETE:
ret = http_delete;
break;
case HTCPCP_BREW:
ret = htcpcp_brew;
break;
case HTTP_METHOD_UNKNOWN: // fall through
// default: // <- clang thinks this is unnecessary...
break; // Remain ret to be NULL
}
return ret;
}
/**
* Convert enumeration HttpStatusCode to string.
*
* @param method [in] HttpStatusCode to convert.
* @return A string constant representing the HTTP status. The caller should not
* free the string!
*/
static const char *http_status_to_string(enum HttpStatusCode status)
{
const char *ret = NULL;
switch (status) {
case HTTP_STATUS_OK:
ret = http_status_ok;
break;
case HTTP_STATUS_NO_CONTENT:
ret = http_status_no_content;
break;
case HTTP_STATUS_MOVED_PERMANENTLY:
ret = http_status_moved_permanently;
break;
case HTTP_STATUS_FOUND:
ret = http_status_found;
break;
case HTTP_STATUS_BAD_REQUEST:
ret = http_status_bad_request;
break;
case HTTP_STATUS_FORBIDDEN:
ret = http_status_forbidden;
break;
case HTTP_STATUS_NOT_FOUND:
ret = http_status_not_found;
break;
case HTTP_STATUS_METHOD_NOT_ALLOWED:
ret = http_status_method_not_allowed;
break;
case HTTP_STATUS_INTERNAL_SERVER_ERROR:
ret = http_status_internal_server_error;
break;
case HTCPCP_STATUS_I_AM_A_TEAPOT:
ret = htcpcp_status_i_am_a_teapot;
break;
case HTTP_STATUS_UNKNOWN: // fall through
// default:
break; // Remain ret to be NULL
}
return ret;
}
static enum HttpMethod http_extract_request_method(const char *req)
{
if (!req)
return HTTP_METHOD_UNKNOWN;
enum HttpMethod ret = HTTP_METHOD_UNKNOWN;
if (g_str_has_prefix(req, "GET"))
ret = HTTP_GET;
else if (g_str_has_prefix(req, "HEAD"))
ret = HTTP_HEAD;
else if (g_str_has_prefix(req, "POST"))
ret = HTTP_POST;
else if (g_str_has_prefix(req, "DELETE"))
ret = HTTP_DELETE;
else if (g_str_has_prefix(req, "BREW"))
ret = HTCPCP_BREW;
return ret;
}
static char *http_extract_request_path(const char *req)
{
if (!req)
return NULL;
char *buffer = g_malloc(BUFSIZE);
sscanf(req, "%*s %4095s", buffer); // XXX: Hardcoded buffer size
char *ret = g_strdup(buffer);
g_free(buffer);
return ret;
}
static char *http_extract_request_version(const char *req)
{
if (!req)
return NULL;
char *buffer = g_malloc(BUFSIZE);
sscanf(req, "%*s %*s %4095s", buffer); // XXX: Hardcoded buffer size
char *ret = g_strdup(buffer);
g_free(buffer);
return ret;
}
static char *http_extract_header(const char *http, enum RequestHeader header)
{
if (!http)
return NULL;
char *buffer = g_malloc0(BUFSIZE);
const char *line = NULL;
switch (header) {
case HTTP_HOST:
line = g_strstr_len(http, -1, http_header_host);
if (line) {
sscanf(line, "%*s %4095s", buffer);
}
break;
case HTTP_CONTENT_TYPE:
line = g_strstr_len(http, -1, http_header_content_type);
if (line) {
sscanf(line, "%*s %4095s", buffer);
}
break;
case HTTP_HEADER_CONTENT_LENGTH:
line = g_strstr_len(http, -1, http_header_content_length);
if (line) {
sscanf(line, "%*s %4095s", buffer);
}
break;
case HTTP_HEADER_EXPECT:
line = g_strstr_len(http, -1, http_header_expect);
if (line) {
sscanf(line, "%*s %4095s", buffer);
}
break;
// default: // <- clang thinks this is unnecessary...
// g_warning("%s:%d %s: unexpected header %d", __FILE__, __LINE__, __func__, header);
// break;
}
char *ret = g_strdup(buffer);
g_free(buffer);
return ret;
}
static uint8_t *http_extract_content(const char *http)
{
if (!http)
return NULL;
const char *content_start = g_strstr_len(http, -1, "\n\r\n");
if (!content_start)
return NULL;
char *content_length_str = http_extract_header(http, HTTP_HEADER_CONTENT_LENGTH);
size_t content_length = (size_t)toInteger(content_length_str);
uint8_t *ret = g_malloc(content_length);
memcpy(ret, content_start + strlen("\n\r\n"), content_length);
g_free(content_length_str);
return ret;
}
/**
* Parse an HTTP string into `struct HttpRequest` for future processing.
*
* @param http [in] The HTTP string.
* @return A `struct HttpRequest`.
*/
static struct HttpRequest teapot_http_request_parse(const char *http)
{
struct HttpRequest request;
// Request line
request.method = http_extract_request_method(http);
request.path = http_extract_request_path(http);
request.version = http_extract_request_version(http);
// Header fields
request.host = http_extract_header(http, HTTP_HOST);
request.content_type = http_extract_header(http, HTTP_CONTENT_TYPE);
request.content_length = (size_t)toInteger(http_extract_header(http, HTTP_HEADER_CONTENT_LENGTH));
request.expect = http_extract_header(http, HTTP_HEADER_EXPECT);
// Content
request.content = http_extract_content(http);
return request;
}
/**
* Convert a `struct HttpResponse` to an HTTP string for sending.
*
* @param response [in] The `struct HttpResponse` to convert.
* @return A NUL-terminated string of HTTP response.
*/
static char *teapot_http_response_construct(size_t *size, const struct HttpResponse response)
{
size_t response_size = 0;
char buffer[16]; // < For store the content-length
// Calculate the buffer size to allocate
response_size += strlen(http_status_to_string(response.status_code)) + strlen("\n");
if (response.content_type) {
response_size += strlen("Content-Type: ") + strlen(response.content_type) + strlen("\n");
}
if (response.content_length) {
// Convert the integer content_length into string
snprintf(buffer, 16, "%zu", response.content_length);
response_size += strlen("Content-Length: ") + strlen(buffer) + strlen("\n");
}
if (response.connection) {
response_size += strlen("Connection: ") + strlen(response.connection) + strlen("\n");
}
if (response.location) {
response_size += strlen("Location: ") + strlen(response.location) + strlen("\n");
}
if (response.allow) {
response_size += strlen("Allow: ") + strlen(response.allow) + strlen("\n");
}
response_size += strlen("\r\n");
if (response.content) {
response_size += response.content_length;
}
*size = response_size;
g_debug("Payload length %zu", response_size);
char *output = g_malloc(response_size + 1);
// NOTE: This is required for strcat() to work properly!
output[0] = '\0';
// The first line
strcat(output, http_status_to_string(response.status_code));
// Header
if (response.content_type) {
strcat(output, "\nContent-Type: ");
strcat(output, response.content_type);
}
if (response.content_length) {
strcat(output, "\nContent-Length: ");
strcat(output, buffer);
}
if (response.connection) {
strcat(output, "\nConnection: ");
strcat(output, response.connection);
}
if (response.location) {
strcat(output, "\nLocation: ");
strcat(output, response.location);
}
if (response.allow) {
strcat(output, "\nAllow: ");
strcat(output, response.allow);
}
strcat(output, "\n\r\n");
// Content
// NOTE: the content may not be ASCII string...
if (response.content) {
memcpy(output + strlen(output), response.content, response.content_length);
}
return output;
}
/********** Public APIs **********/
char *teapot_http_process(size_t *size, const char *input)
{
// get the request
struct HttpRequest request = teapot_http_request_parse(input);
// All the information sent by client is storing in request now.
struct HttpResponse response;
// --- Predefine the connection of the response ---
response.connection = "close";
// below variables may be changed later
response.content_type = NULL;
response.content_length = 0;
response.location = NULL;
response.allow = NULL;
response.content = NULL;
// ------------------------------------------------------------
struct TeapotFile *file = NULL;
switch (request.method) {
case HTTP_GET:
// Do you want to direct to a new location? ->> 3XX response
if (teapot_redir_301_query(request.path) != NULL) {
// If the new location is not temporart ->> HTTP 301
response.status_code = HTTP_STATUS_MOVED_PERMANENTLY;
response.location = teapot_redir_301_query(request.path);
break;
} else if (teapot_redir_302_query(request.path) != NULL) {
// If the new location is temporart ->> HTTP 302
response.status_code = HTTP_STATUS_FOUND;
response.location = teapot_redir_302_query(request.path);
break;
}
file = teapot_file_read(request.path, 0, TEAPOT_FILE_READ_RANGE_FULL);
if (file == NULL) { // If the file does not exist.
response.status_code = HTTP_STATUS_NOT_FOUND; ///< HTTP 404
response.content_type = "text/html; charset=utf8";
response.content_length = strlen(http_status_not_found_html);
response.content = http_status_not_found_html;
} else {
response.status_code = HTTP_STATUS_OK; ///< HTTP 200
response.content_type = file -> content_type;
response.content_length = file -> size;
response.content = file -> content;
}
break;
case HTTP_HEAD:
if (teapot_redir_301_query(request.path) != NULL) {
// If the new location is not temporart ->> HTTP 301
response.status_code = HTTP_STATUS_MOVED_PERMANENTLY;
response.location = teapot_redir_301_query(request.path);
break;
} else if (teapot_redir_302_query(request.path) != NULL) {
// If the new location is temporart ->> HTTP 302
response.status_code = HTTP_STATUS_FOUND;
response.location = teapot_redir_302_query(request.path);
break;
}
file = teapot_file_read(request.path, 0, TEAPOT_FILE_READ_RANGE_FULL);
if (file == NULL) { // If the file does not exist.
response.status_code = HTTP_STATUS_NOT_FOUND; ///< HTTP 404
} else {
response.status_code = HTTP_STATUS_OK; ///< HTTP 200
response.content_type = file -> content_type;
response.content_length = 0;
response.content = NULL;
}
break;
case HTTP_POST:
if (teapot_file_write(request.content, request.content_length, request.path)) {
response.status_code = HTTP_STATUS_NO_CONTENT;
} else {
response.status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR; // FIXME
}
break;
case HTTP_DELETE:
// TODO
break;
case HTCPCP_BREW:
response.status_code = HTCPCP_STATUS_I_AM_A_TEAPOT;
break;
case HTTP_METHOD_UNKNOWN:
// default:
response.status_code = HTTP_STATUS_METHOD_NOT_ALLOWED; ///< HTTP 405
response.allow = "GET HEAD POST DELETE";
break;
}
size_t response_size = 0;
char *response_str = teapot_http_response_construct(&response_size, response);
// char *response_str = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nHello world!";
*size = response_size;
teapot_file_free(file);
return response_str;
}