-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.c
More file actions
590 lines (447 loc) · 12.8 KB
/
aws.c
File metadata and controls
590 lines (447 loc) · 12.8 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
583
584
585
586
587
588
589
590
// SPDX-License-Identifier: BSD-3-Clause
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/sendfile.h>
#include <sys/eventfd.h>
#include <libaio.h>
#include <errno.h>
#include <time.h>
#include "aws.h"
#include "utils/util.h"
#include "utils/debug.h"
#include "utils/sock_util.h"
#include "utils/w_epoll.h"
/* server socket file descriptor */
static int listenfd;
/* epoll file descriptor */
static int epollfd;
static io_context_t ctx;
static int aws_on_path_cb(http_parser *p, const char *buf, size_t len)
{
struct connection *conn = (struct connection *)p->data;
memcpy(conn->request_path, buf, len);
conn->request_path[len] = '\0';
conn->have_path = 1;
return 0;
}
/* Prepare the connection buffer to send the reply header. */
static void connection_prepare_send_reply_header(struct connection *conn)
{
/* Get current date and time */
char date[100];
time_t now = time(NULL);
struct tm tm;
gmtime_r(&now, &tm);
strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S GMT", &tm);
conn->send_len = snprintf(conn->send_buffer, BUFSIZ,
"HTTP/1.1 200 OK\r\n"
"Date: %s\r\n"
"Content-Length: %lu\r\n"
"Connection: close\r\n"
"\r\n",
date, conn->file_size);
}
/* Prepare the connection buffer to send the 404 header. */
static void connection_prepare_send_404(struct connection *conn)
{
/* Get current date and time */
char date[100];
time_t now = time(NULL);
struct tm tm;
gmtime_r(&now, &tm);
strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S GMT", &tm);
conn->send_len = snprintf(conn->send_buffer, BUFSIZ,
"HTTP/1.1 404 Not Found\r\n"
"Date: %s\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n"
"\r\n",
date);
}
/* Get resource type depending on request path/filename.
* Filename should point to the static or dynamic folder.
*/
static enum resource_type connection_get_resource_type(struct connection *conn)
{
if (strstr(conn->request_path, "/static/"))
return RESOURCE_TYPE_STATIC;
if (strstr(conn->request_path, "/dynamic/"))
return RESOURCE_TYPE_DYNAMIC;
return RESOURCE_TYPE_NONE;
}
/* Initialize connection structure on given socket. */
struct connection *connection_create(int sockfd)
{
struct connection *conn = malloc(sizeof(struct connection));
DIE(conn == NULL, "conn malloc");
conn->fd = -1;
memset(conn->filename, 0, BUFSIZ);
conn->eventfd = -1;
conn->sockfd = sockfd;
conn->ctx = ctx;
conn->file_size = 0;
memset(conn->recv_buffer, 0, BUFSIZ);
conn->recv_len = 0;
memset(conn->send_buffer, 0, BUFSIZ);
conn->send_len = 0;
conn->send_pos = 0;
conn->file_pos = 0;
conn->async_read_len = 0;
conn->have_path = 0;
memset(conn->request_path, 0, BUFSIZ);
conn->res_type = RESOURCE_TYPE_NONE;
conn->state = STATE_INITIAL;
http_parser_init(&conn->request_parser, HTTP_REQUEST);
conn->request_parser.data = conn;
return conn;
}
/* Start asynchronous operation (read from file).
*/
void connection_start_async_io(struct connection *conn)
{
if (conn->eventfd < 0) {
conn->eventfd = eventfd(0, EFD_NONBLOCK);
DIE(conn->eventfd < 0, "eventfd");
w_epoll_add_ptr_in(epollfd, conn->eventfd, conn);
}
io_prep_pread(&conn->iocb, conn->fd, conn->send_buffer, BUFSIZ, conn->file_pos);
io_set_eventfd(&conn->iocb, conn->eventfd);
conn->piocb[0] = &conn->iocb;
int rc = io_submit(conn->ctx, 1, conn->piocb);
DIE(rc < 0, "io_submit");
}
/* Remove connection handler. */
void connection_remove(struct connection *conn)
{
int rc;
rc = w_epoll_remove_ptr(epollfd, conn->sockfd, conn);
DIE(rc < 0, "w_epoll_remove_ptr");
/* Close fds */
if (conn->sockfd >= 0) {
rc = close(conn->sockfd);
DIE(rc < 0, "close conn sockfd");
}
if (conn->fd >= 0) {
rc = close(conn->fd);
DIE(rc < 0, "close conn fd");
}
if (conn->eventfd >= 0) {
rc = close(conn->eventfd);
DIE(rc < 0, "close conn eventfd");
}
free(conn);
}
/* Handle a new connection request on the server socket. */
void handle_new_connection(void)
{
int rc;
/* Accept new connection. */
int connectfd = accept(listenfd, NULL, NULL);
DIE(connectfd < 0, "accept");
/* Set socket to be non-blocking. */
fcntl(connectfd, F_SETFL, fcntl(connectfd, F_GETFL, 0) | O_NONBLOCK);
/* Instantiate new connection handler. */
struct connection *conn = connection_create(connectfd);
/* Add socket to epoll. */
rc = w_epoll_add_ptr_in(epollfd, connectfd, conn);
DIE(rc < 0, "w_epoll_add_ptr_in");
}
/* Receive message on socket.
* Store message in recv_buffer in struct connection.
*/
void receive_data(struct connection *conn)
{
/* Append new data to existing buffer */
ssize_t bytes_read = recv(conn->sockfd,
conn->recv_buffer + conn->recv_len,
BUFSIZ - conn->recv_len, 0);
if (bytes_read < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* No data ready yet */
return;
}
conn->state = STATE_CONNECTION_CLOSED;
return;
}
if (bytes_read == 0) {
conn->state = STATE_CONNECTION_CLOSED;
return;
}
conn->recv_len += bytes_read;
}
/* Open file and update connection fields. */
int connection_open_file(struct connection *conn)
{
conn->fd = open(conn->filename, O_RDONLY);
if (conn->fd < 0)
return -1;
struct stat stats;
int rc = fstat(conn->fd, &stats);
if (rc < 0) {
close(conn->fd);
conn->fd = -1;
return -1;
}
conn->file_size = stats.st_size;
conn->file_pos = 0;
return 0;
}
/* Complete asynchronous operation; operation returns successfully.
* Prepare socket for sending.
*/
void connection_complete_async_io(struct connection *conn)
{
struct io_event events[1];
int n = io_getevents(conn->ctx, 1, 1, events, NULL);
if (n <= 0) {
conn->state = STATE_CONNECTION_CLOSED;
return;
}
conn->async_read_len = events[0].res;
if (conn->async_read_len <= 0) {
conn->state = STATE_CONNECTION_CLOSED;
return;
}
conn->file_pos += conn->async_read_len;
conn->send_pos = 0;
uint64_t tmp;
read(conn->eventfd, &tmp, sizeof(tmp));
conn->state = STATE_SENDING_DATA;
w_epoll_update_ptr_out(epollfd, conn->sockfd, conn);
}
/* Parse the HTTP header and extract the file path. */
int parse_header(struct connection *conn)
{
/* Use mostly null settings except for on_path callback. */
http_parser_settings settings_on_path = {
.on_message_begin = 0,
.on_header_field = 0,
.on_header_value = 0,
.on_path = aws_on_path_cb,
.on_url = 0,
.on_fragment = 0,
.on_query_string = 0,
.on_body = 0,
.on_headers_complete = 0,
.on_message_complete = 0
};
return http_parser_execute(&conn->request_parser, &settings_on_path,
conn->recv_buffer, conn->recv_len);
}
/* Send static data using sendfile(2). */
enum connection_state connection_send_static(struct connection *conn)
{
size_t remaining = conn->file_size - conn->file_pos;
off_t offset = conn->file_pos;
ssize_t sent = sendfile(conn->sockfd, conn->fd, &offset, remaining);
conn->file_pos = offset;
if (sent < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return STATE_SENDING_DATA;
return STATE_CONNECTION_CLOSED;
}
if (conn->file_pos >= conn->file_size)
return STATE_CONNECTION_CLOSED;
return STATE_SENDING_DATA;
}
/* Read data asynchronously. Returns 0 on success and -1 on error. */
int connection_send_dynamic(struct connection *conn)
{
size_t remaining = conn->async_read_len - conn->send_pos;
ssize_t sent = send(conn->sockfd, conn->send_buffer + conn->send_pos, remaining, 0);
if (sent < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return 0;
return -1;
}
conn->send_pos += sent;
if (conn->send_pos >= conn->async_read_len) {
if (conn->file_pos >= conn->file_size) {
/* File sent completely */
conn->state = STATE_CONNECTION_CLOSED;
return 0;
}
/* More data to read, start next async read */
conn->state = STATE_ASYNC_ONGOING;
connection_start_async_io(conn);
}
return 0;
}
/* Handle input information: may be a new message or notification of
* completion of an asynchronous I/O operation.
*/
void handle_input(struct connection *conn)
{
switch (conn->state) {
case STATE_INITIAL:
conn->state = STATE_RECEIVING_DATA;
break;
case STATE_RECEIVING_DATA:
receive_data(conn);
if (conn->recv_len > 0) {
/* Check if we have complete HTTP header (ends with \r\n\r\n) */
if (memmem(conn->recv_buffer, conn->recv_len, "\r\n\r\n", 4)) {
parse_header(conn);
if (conn->have_path) {
conn->state = STATE_REQUEST_RECEIVED;
handle_input(conn);
}
}
}
break;
case STATE_REQUEST_RECEIVED:
/* Get filename */
snprintf(conn->filename, BUFSIZ, "%s%s",
AWS_DOCUMENT_ROOT, conn->request_path + 1);
/* Get resource type */
conn->res_type = connection_get_resource_type(conn);
int rc = connection_open_file(conn);
if (rc >= 0) {
/* Success 200 */
connection_prepare_send_reply_header(conn);
conn->send_pos = 0;
conn->state = STATE_SENDING_HEADER;
} else {
/* Error 404 */
connection_prepare_send_404(conn);
conn->send_pos = 0;
conn->state = STATE_SENDING_404;
}
w_epoll_update_ptr_out(epollfd, conn->sockfd, conn);
break;
case STATE_ASYNC_ONGOING:
connection_complete_async_io(conn);
break;
default:
printf("shouldn't get here %d\n", conn->state);
}
}
/* Handle output information: may be a new valid requests or notification of
* completion of an asynchronous I/O operation or invalid requests.
*/
void handle_output(struct connection *conn)
{
size_t remaining;
ssize_t sent;
switch (conn->state) {
case STATE_SENDING_HEADER:
remaining = conn->send_len - conn->send_pos;
sent = send(conn->sockfd, conn->send_buffer + conn->send_pos, remaining, 0);
if (sent < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
conn->state = STATE_CONNECTION_CLOSED;
return;
}
conn->send_pos += sent;
/* Header is sent */
if (conn->send_pos == conn->send_len) {
if (conn->res_type == RESOURCE_TYPE_STATIC) {
conn->state = STATE_SENDING_DATA;
w_epoll_update_ptr_out(epollfd, conn->sockfd, conn);
} else if (conn->res_type == RESOURCE_TYPE_DYNAMIC) {
conn->state = STATE_ASYNC_ONGOING;
connection_start_async_io(conn);
} else {
// 404
conn->state = STATE_CONNECTION_CLOSED;
}
}
break;
case STATE_SENDING_404:
remaining = conn->send_len - conn->send_pos;
sent = send(conn->sockfd, conn->send_buffer + conn->send_pos, remaining, 0);
if (sent < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
conn->state = STATE_CONNECTION_CLOSED;
return;
}
conn->send_pos += sent;
if (conn->send_pos == conn->send_len)
conn->state = STATE_CONNECTION_CLOSED;
break;
case STATE_SENDING_DATA:
if (conn->res_type == RESOURCE_TYPE_STATIC) {
conn->state = connection_send_static(conn);
if (conn->state == STATE_SENDING_DATA)
w_epoll_update_ptr_out(epollfd, conn->sockfd, conn);
} else if (conn->res_type == RESOURCE_TYPE_DYNAMIC) {
int rc = connection_send_dynamic(conn);
if (rc < 0)
conn->state = STATE_CONNECTION_CLOSED;
if (conn->state == STATE_SENDING_DATA)
w_epoll_update_ptr_out(epollfd, conn->sockfd, conn);
}
break;
case STATE_ASYNC_ONGOING:
/* Waiting for async I/O to complete, ignore output events */
break;
default:
ERR("Unexpected state\n");
exit(1);
}
}
/* Handle new client. There can be input and output connections.
* Take care of what happened at the end of a connection.
*/
void handle_client(uint32_t event, struct connection *conn)
{
if (event & EPOLLIN)
handle_input(conn);
if (event & EPOLLOUT)
handle_output(conn);
if (conn->state == STATE_CONNECTION_CLOSED)
connection_remove(conn);
}
int main(void)
{
int rc;
/* Initialize asynchronous operations. */
rc = io_setup(10, &ctx);
DIE(rc < 0, "io_setup");
/* Initialize multiplexing. */
epollfd = w_epoll_create();
DIE(epollfd < 0, "w_epoll_create");
/* Create server socket. */
listenfd = tcp_create_listener(AWS_LISTEN_PORT, 10);
DIE(listenfd < 0, "tcp_create_listener");
/* Add server socket to epoll object*/
rc = w_epoll_add_fd_in(epollfd, listenfd);
DIE(rc < 0, "w_epoll_add_fd_in");
dlog(LOG_INFO, "Server waiting for connections on port %d\n", AWS_LISTEN_PORT);
/* server main loop */
while (1) {
struct epoll_event rev;
/* Wait for events. */
int n = w_epoll_wait_infinite(epollfd, &rev);
DIE(n < 0, "w_epoll_wait_infinite");
/* Switch event types; consider
* - new connection requests (on server socket)
* - socket communication (on connection sockets)
*/
struct connection *conn = (struct connection *)rev.data.ptr;
if (rev.data.fd == listenfd) {
/* New connection */
handle_new_connection();
} else if (rev.data.fd == conn->eventfd) {
/* Async I/O */
handle_input(conn);
} else {
/* Socket */
handle_client(rev.events, conn);
}
}
return 0;
}