-
Notifications
You must be signed in to change notification settings - Fork 131
/
program.c
120 lines (94 loc) · 3.62 KB
/
program.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <haywire.h>
#include "opt.h"
#include "haywire.h"
#define CRLF "\r\n"
void response_complete(void* user_data)
{
}
void get_ping(http_request* request, hw_http_response* response, void* user_data)
{
hw_string status_code;
hw_string content_type_name;
hw_string content_type_value;
hw_string body;
hw_string keep_alive_name;
hw_string keep_alive_value;
hw_string route_matched_name;
hw_string route_matched_value;
hw_print_request_headers(request);
hw_print_body(request);
SETSTRING(status_code, HTTP_STATUS_200);
hw_set_response_status_code(response, &status_code);
SETSTRING(content_type_name, "Content-Type");
SETSTRING(content_type_value, "text/html");
hw_set_response_header(response, &content_type_name, &content_type_value);
body.value = request->body->value;
body.length = request->body->length;
hw_set_body(response, &body);
if (request->keep_alive)
{
SETSTRING(keep_alive_name, "Connection");
SETSTRING(keep_alive_value, "Keep-Alive");
hw_set_response_header(response, &keep_alive_name, &keep_alive_value);
}
else
{
hw_set_http_version(response, 1, 0);
}
hw_http_response_send(response, "user_data", response_complete);
}
void get_root(http_request* request, hw_http_response* response, void* user_data)
{
hw_string status_code;
hw_string content_type_name;
hw_string content_type_value;
hw_string body;
hw_string keep_alive_name;
hw_string keep_alive_value;
hw_string route_matched_name;
hw_string route_matched_value;
SETSTRING(status_code, HTTP_STATUS_200);
hw_set_response_status_code(response, &status_code);
SETSTRING(content_type_name, "Content-Type");
SETSTRING(content_type_value, "text/html");
hw_set_response_header(response, &content_type_name, &content_type_value);
SETSTRING(body, "hello world");
hw_set_body(response, &body);
if (request->keep_alive)
{
SETSTRING(keep_alive_name, "Connection");
SETSTRING(keep_alive_value, "Keep-Alive");
hw_set_response_header(response, &keep_alive_name, &keep_alive_value);
}
else
{
hw_set_http_version(response, 1, 0);
}
hw_http_response_send(response, "user_data", response_complete);
}
int main(int args, char** argsv)
{
char root_route[] = "/";
char ping_route[] = "/ping";
configuration config;
config.http_listen_address = "0.0.0.0";
struct opt_config *conf;
conf = opt_config_init();
opt_flag_int(conf, &config.http_listen_port, "port", 8000, "Port to listen on.");
opt_flag_int(conf, &config.thread_count, "threads", 0, "Number of threads to use.");
opt_flag_string(conf, &config.balancer, "balancer", "ipc", "Method to load balance threads.");
opt_flag_string(conf, &config.parser, "parser", "http_parser", "HTTP parser to use");
opt_flag_int(conf, &config.max_request_size, "max_request_size", 1048576, "Maximum request size. Defaults to 1MB.");
opt_flag_bool(conf, &config.tcp_nodelay, "tcp_nodelay", "If present, enables tcp_nodelay (i.e. disables Nagle's algorithm).");
opt_flag_int(conf, &config.listen_backlog, "listen_backlog", 0, "Maximum size of the backlog when accepting connection. Defaults to SOMAXCONN.");
args = opt_config_parse(conf, args, argsv);
hw_init_with_config(&config);
hw_http_add_route(ping_route, get_ping, NULL);
hw_http_add_route(root_route, get_root, NULL);
hw_http_open();
opt_config_free(conf);
return 0;
}