-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhalo.c
338 lines (264 loc) · 9.24 KB
/
halo.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <janet.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "http_parser.h"
#include "sandbird.h"
JanetTable *request_table;
JanetTable *headers;
Janet prev_header_name;
JanetFunction *handler;
http_parser_settings settings;
sb_Options opt;
sb_Server *server;
int server_running = 1;
static void sig_handler(int signo) {
if (signo == SIGINT) {
server_running = 0;
}
}
void send_http_response(sb_Event *e, Janet res) {
switch (janet_type(res)) {
case JANET_TABLE:
case JANET_STRUCT:
{
const JanetKV *kvs;
int32_t kvlen, kvcap;
janet_dictionary_view(res, &kvs, &kvlen, &kvcap);
/* check for static files */
const uint8_t *file_path;
struct stat s;
int err;
Janet janet_filepath = janet_dictionary_get(kvs, kvcap, janet_ckeywordv("file"));
if(janet_checktype(janet_filepath, JANET_STRING)) {
file_path = janet_unwrap_string(janet_filepath);
/* Get file info */
err = stat((const char *)file_path, &s);
/* Does file exist? */
if (err == -1) {
sb_send_status(e->stream, 404, "Not found");
return;
}
// TODO Directories?
/* Handle file */
err = sb_send_file(e->stream, (const char *)file_path);
if (err) {
break;
} else {
return;
}
}
/* Serve a generic HTTP response */
Janet status = janet_dictionary_get(kvs, kvcap, janet_ckeywordv("status"));
Janet headers = janet_dictionary_get(kvs, kvcap, janet_ckeywordv("headers"));
Janet body = janet_dictionary_get(kvs, kvcap, janet_ckeywordv("body"));
int code;
if (janet_checktype(status, JANET_NIL))
code = 200;
else if (janet_checkint(status))
code = janet_unwrap_integer(status);
else
break;
const JanetKV *headerkvs;
int32_t headerlen, headercap;
if (janet_checktype(headers, JANET_NIL)) {
headerkvs = NULL;
headerlen = 0;
headercap = 0;
} else if (!janet_dictionary_view(headers, &headerkvs, &headerlen, &headercap)) {
break;
}
const uint8_t *body_bytes;
int32_t body_len;
if (janet_checktype(body, JANET_NIL)) {
body_bytes = NULL;
body_len = 0;
} else if (!janet_bytes_view(body, &body_bytes, &body_len)) {
break;
}
const char *code_text = http_status_str(code);
sb_send_status(e->stream, code, code_text);
for (const JanetKV *kv = janet_dictionary_next(headerkvs, headercap, NULL);
kv;
kv = janet_dictionary_next(headerkvs, headercap, kv)) {
const uint8_t *name = janet_to_string(kv->key);
int32_t header_len;
const Janet *header_items;
if (janet_indexed_view(kv->value, &header_items, &header_len)) {
for (int32_t i = 0; i < header_len; i++) {
const uint8_t *value = janet_to_string(header_items[i]);
sb_send_header(e->stream, (const char *)name, (const char *)value);
}
} else {
const uint8_t *value = janet_to_string(kv->value);
sb_send_header(e->stream, (const char *)name, (const char *)value);
}
}
if (body_len > 0) {
int length = snprintf(NULL, 0, "%d", body_len);
char str[length];
sprintf(str, "%d", body_len);
sb_send_header(e->stream, "Content-Length", str);
sb_writef(e->stream, "%s", (const char *)body_bytes);
}
}
break;
default:
sb_send_status(e->stream, 500, "Internal server error");
sb_send_header(e->stream, "Content-Type", "text/plain");
sb_writef(e->stream, "%s", "Internal Server Error");
break;
}
}
int message_begin_cb(struct http_parser *parser) {
(void)parser;
return 0;
}
int header_field_cb(struct http_parser *parser, const char *p, unsigned long len) {
(void)parser;
prev_header_name = janet_wrap_string(janet_string((uint8_t *)p, len));
return 0;
}
int header_value_cb(struct http_parser *parser, const char *p, unsigned long len) {
(void)parser;
Janet header = janet_table_get(headers, prev_header_name);
Janet value = janet_wrap_string(janet_string((uint8_t *)p, len));
switch (janet_type(header)) {
case JANET_NIL:
janet_table_put(headers, prev_header_name, value);
break;
case JANET_ARRAY:
janet_array_push(janet_unwrap_array(header), value);
break;
default: {
Janet newHeader[2] = { header, value };
janet_table_put(headers, prev_header_name, janet_wrap_array(janet_array_n(newHeader, 2)));
break;
}
}
return 0;
}
int status_cb(struct http_parser *parser, const char *p, unsigned long len) {
(void)parser;
(void)p;
(void)len;
return 0;
}
int url_cb(struct http_parser *parser, const char *p, unsigned long len) {
(void)parser;
janet_table_put(request_table, janet_ckeywordv("uri"), janet_wrap_string(janet_string((const uint8_t *)p, len)));
return 0;
}
int body_cb(struct http_parser *parser, const char *p, unsigned long len) {
(void)parser;
janet_table_put(request_table, janet_ckeywordv("body"), janet_wrap_string(janet_string((const uint8_t *)p, len)));
return 0;
}
int headers_complete_cb(http_parser *parser) {
janet_table_put(request_table, janet_ckeywordv("method"), janet_wrap_string(janet_cstring(http_method_str(parser->method))));
janet_table_put(request_table, janet_ckeywordv("headers"), janet_wrap_table(headers));
return 0;
}
int message_complete_cb(struct http_parser *parser) {
(void)parser;
return 0;
}
static int event_handler(sb_Event *e) {
if (e->type == SB_EV_REQUEST) {
request_table = janet_table(5);
headers = janet_table(20);
int nparsed = 0;
http_parser parser;
http_parser_init(&parser, HTTP_REQUEST);
nparsed = http_parser_execute(&parser, &settings, e->stream->recv_buf.s, e->stream->recv_buf.len);
// TODO while loop to parse all the streams
// keep calling e->stream->next until NULL
// multipart
//if(nparsed != e->stream->recv_buf.len) {
// parse next stream
//}
Janet jarg[1];
jarg[0] = janet_wrap_table(request_table);
Janet response;
JanetFiber *fiber = janet_fiber(handler, 64, 1, jarg);
JanetFiber *janet_vm_fiber = janet_current_fiber();
if (!janet_vm_fiber->env) {
janet_vm_fiber->env = janet_table(0);
}
fiber->env = janet_vm_fiber->env;
JanetSignal signal = janet_continue(fiber, jarg[0], &response);
if(signal != JANET_SIGNAL_OK) {
janet_stacktrace(fiber, response);
return SB_RES_CLOSE;
}
send_http_response(e, response);
}
return SB_RES_OK;
}
Janet cfun_start_server(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 3);
JanetFunction *janet_handler = janet_getfunction(argv, 0);
const uint8_t *port = janet_getstring(argv, 1);
const uint8_t *ip_address = janet_optstring(argv, argc, 2, NULL);
settings.on_message_begin = message_begin_cb;
settings.on_header_field = header_field_cb;
settings.on_header_value = header_value_cb;
settings.on_status = status_cb;
settings.on_url = url_cb;
settings.on_body = body_cb;
settings.on_headers_complete = headers_complete_cb;
settings.on_message_complete = message_complete_cb;
memset(&opt, 0, sizeof(opt));
opt.port = (char *)port;
if(ip_address != NULL) {
opt.host = (char *)ip_address;
}
opt.handler = event_handler;
handler = janet_handler;
server = sb_new_server(&opt);
if (!server) {
janet_panicf("failed to intialize server\n");
}
return janet_wrap_nil();
}
Janet cfun_poll_server(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
int32_t timeout = janet_getinteger(argv, 0);
sb_poll_server(server, timeout);
return janet_wrap_nil();
}
Janet cfun_server_running(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void)argv;
return janet_wrap_boolean(server_running);
}
Janet cfun_stop_server(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void)argv;
sb_close_server(server);
return janet_wrap_nil();
}
static const JanetReg cfuns[] = {
{"start-server", cfun_start_server, NULL},
{"poll-server", cfun_poll_server, NULL},
{"stop-server", cfun_stop_server, NULL},
{"server-running?", cfun_server_running, NULL},
{NULL, NULL, NULL}
};
extern const unsigned char *halo_lib_embed;
extern size_t halo_lib_embed_size;
JANET_MODULE_ENTRY(JanetTable *env) {
if (signal(SIGINT, sig_handler) == SIG_ERR) {
printf("\ncan't catch SIGINT\n");
}
janet_cfuns(env, "halo", cfuns);
janet_dobytes(env,
halo_lib_embed,
halo_lib_embed_size,
"halo_lib.janet",
NULL);
}