-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
45 lines (33 loc) · 1.15 KB
/
Copy pathtest.c
File metadata and controls
45 lines (33 loc) · 1.15 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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "http.h"
void on_request(http_context *const context) {
assert(context != NULL);
if (http_string_cmp(context->request.path, STR_FROM("/stop-test")) == 0) {
context->response.status = 200;
http_server_stop(context->server);
} else if (http_string_cmp(context->request.path, STR_FROM("/Makefile")) == 0) {
http_context_sendfile(context, "Makefile");
return;
} else if (context->state == state_read_end) {
context->response.status = STATUS_IM_A_TEAPOT;
} else if (context->state == state_read_timeout) {
context->response.status = STATUS_REQUEST_TIMEOUT;
} else {
context->response.status = STATUS_BAD_REQUEST;
}
http_header_set(context->response.headers, STR_FROM(HTTP_CONTENT_LENGTH), STR_FROM("0"));
http_context_respond(context);
}
int main(void) {
int rc;
http_server server;
if (http_server_init(&server, "0.0.0.0", 8080, 16) == -1) {
exit(1);
}
rc = http_server_start(&server, NULL, on_request);
http_server_deinit(&server);
if (rc == -1) exit(1);
return 0;
}