-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
98 lines (77 loc) · 2.68 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gc.h>
#include <sanic.h>
enum sanic_middleware_action http_version_filter(struct sanic_http_request *req, struct sanic_http_response *res) {
char version[4];
bzero(version, 4);
strcpy(version, req->version + 5);
char *err;
double version_num = strtod(version, &err);
if (version == err) {
res->status = 400;
return ACTION_STOP;
}
if (version_num >= 2) {
res->status = 505;
return ACTION_STOP;
}
return ACTION_PASS;
}
#ifdef SANIC_USE_CLANG_BLOCKS
int main() {
sanic_init();
sanic_log_level = LEVEL_DEBUG;
sanic_use_middleware(^enum sanic_middleware_action(struct sanic_http_request *req, struct sanic_http_response *res) {
return http_version_filter(req, res);
});
sanic_use_middleware(^enum sanic_middleware_action(struct sanic_http_request *req, struct sanic_http_response *res) {
if (strcmp(req->path, "/teapot") == 0) {
res->status = 418;
return ACTION_STOP;
}
return ACTION_PASS;
});
sanic_http_on_get("/", ^void(struct sanic_http_request *req, struct sanic_http_response *res) {
res->response_body = GC_STRDUP("<h1>Hello, World!</h1>");
sanic_http_header_insert(res, &(struct sanic_http_param) {
.key = "Hotel",
.value = "Trivago",
});
});
sanic_http_on_get("/people/{:name}", ^void(struct sanic_http_request *req, struct sanic_http_response *res) {
char *name = sanic_path_params_get(req, "name");
res->response_body = GC_MALLOC_ATOMIC(18 + strlen(name));
sprintf(res->response_body, "<h1>Hello, %s!</h1>", name);
});
return sanic_http_serve(8080);
}
#else
void handle_index(struct sanic_http_request *req, struct sanic_http_response *res) {
res->response_body = GC_STRDUP("<h1>Hello, World!</h1>");
}
void handle_get_person(struct sanic_http_request *req, struct sanic_http_response *res) {
char *name = sanic_path_params_get(req, "name");
char *html_template = "<h1>Hello, %s!</h1>";
res->response_body = GC_malloc_atomic(strlen(html_template) + strlen(name) - 1);
bzero(res->response_body, strlen(html_template) + strlen(name) - 1);
sprintf(res->response_body, html_template, name);
}
enum sanic_middleware_action teapot_filter(struct sanic_http_request *req, struct sanic_http_response *res) {
if (strcmp(req->path, "/teapot") == 0) {
res->status = 418;
return ACTION_STOP;
}
return ACTION_PASS;
}
int main() {
sanic_init();
sanic_log_level = LEVEL_ERROR;
//sanic_use_middleware(http_version_filter);
//sanic_use_middleware(teapot_filter);
sanic_http_on_get("/", handle_index);
//sanic_http_on_get("/people/{:name}", handle_get_person);
return sanic_http_serve(8080);
}
#endif