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
|
/*
* blogc: A blog compiler.
* Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the BSD License.
* See the file LICENSE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <event2/event.h>
#include <event2/http.h>
#include <event2/buffer.h>
#include <magic.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "../common/utils.h"
/**
* this mapping is used to declare "supported" file types, that are forced over
* whatever detected by libmagic, but we will still use the charset provided by
* libmagic anyway. it also helps detecting index files when the client asks
* for a directory.
*/
static const struct content_type_map {
const char *mimetype;
const char *extension;
const char *index;
} content_types[] = {
{"text/html", "html", "index.html"},
{"text/html", "htm", "index.htm"},
{"text/xml", "xml", "index.xml"},
{"text/plain", "txt", "index.txt"},
{"text/css", "css", NULL},
{"application/javascript", "js", NULL},
{NULL, NULL, NULL}
};
static magic_t magic_all = NULL;
static magic_t magic_charset = NULL;
static const char*
get_extension(const char *filename)
{
const char *ext = NULL;
unsigned int i;
for (i = strlen(filename); i > 0; i--) {
if (filename[i] == '.') {
ext = filename + i + 1;
break;
}
}
if (i == 0)
return NULL;
return ext;
}
static char*
guess_content_type(const char *filename, int fd)
{
int newfd;
// try "supported" types first, and just use libmagic for charset
const char *extension = get_extension(filename);
if (extension == NULL)
goto libmagic;
const char *supported = NULL;
for (unsigned int i = 0; content_types[i].extension != NULL; i++)
if (0 == strcmp(content_types[i].extension, extension))
supported = content_types[i].mimetype;
if (supported != NULL) {
newfd = dup(fd);
if (-1 != newfd) {
const char* charset = magic_descriptor(magic_charset, newfd);
close(newfd);
if (charset != NULL)
return sb_strdup_printf("%s; charset=%s", supported, charset);
}
return sb_strdup(supported);
}
libmagic:
// fallback to use libmagic for everything
newfd = dup(fd);
if (-1 != newfd) {
const char* content_type = magic_descriptor(magic_all, newfd);
close(newfd);
if (content_type != NULL)
return sb_strdup(content_type);
}
return sb_strdup("application/octet-stream");
}
static void
handler(struct evhttp_request *request, void *ptr)
{
const char *root = ptr;
const char *uri = evhttp_request_get_uri(request);
struct evhttp_uri *decoded_uri = evhttp_uri_parse(uri);
if (decoded_uri == NULL) {
evhttp_send_error(request, 400, "Bad request");
return;
}
const char *path = evhttp_uri_get_path(decoded_uri);
if (path == NULL)
path = "/";
char *decoded_path = evhttp_uridecode(path, 0, NULL);
if (decoded_path == NULL) {
evhttp_send_error(request, 400, "Bad request");
goto point1;
}
char *abs_path = sb_strdup_printf("%s/%s", root, decoded_path);
char *real_path = realpath(abs_path, NULL);
free(abs_path);
if (real_path == NULL) {
evhttp_send_error(request, 404, "Not found");
goto point2;
}
char *real_root = realpath(root, NULL);
if (real_root == NULL) {
evhttp_send_error(request, 500, "Internal server error");
goto point3;
}
if (0 != strncmp(real_root, real_path, strlen(real_root))) {
evhttp_send_error(request, 404, "Not found");
goto point4;
}
struct stat st;
if (0 > stat(real_path, &st)) {
evhttp_send_error(request, 404, "Not found");
goto point4;
}
bool add_slash = false;
if (S_ISDIR(st.st_mode)) {
char *found = NULL;
for (unsigned int i = 0; content_types[i].mimetype != NULL; i++) {
if (content_types[i].index == NULL)
continue;
char *f = sb_strdup_printf("%s/%s", real_path,
content_types[i].index);
if (0 == access(f, F_OK)) {
found = sb_strdup(f);
break;
}
free(f);
}
if (found == NULL) {
evhttp_send_error(request, 403, "Forbidden");
goto point4;
}
size_t path_len = strlen(path);
if (path_len > 0 && path[path_len - 1] != '/')
add_slash = true;
free(real_path);
real_path = found;
}
int fd;
if ((fd = open(real_path, O_RDONLY)) < 0) {
evhttp_send_error(request, 500, "Internal server error");
goto point4;
}
char *type = guess_content_type(real_path, fd);
if (fstat(fd, &st) < 0) {
evhttp_send_error(request, 500, "Internal server error");
goto point5;
}
struct evkeyvalq *headers = evhttp_request_get_output_headers(request);
if (add_slash) {
char *tmp = sb_strdup_printf("%s/", path);
evhttp_add_header(headers, "Location", tmp);
free(tmp);
// production webservers usually returns 301 in such cases, but 302 is
// better for development/testing.
evhttp_send_reply(request, 302, "Found", NULL);
goto point5;
}
evhttp_add_header(headers, "Content-Type", type);
char *content_length = sb_strdup_printf("%zu", st.st_size);
evhttp_add_header(headers, "Content-Length", content_length);
free(content_length);
struct evbuffer *evb = evbuffer_new();
evbuffer_add_file(evb, fd, 0, st.st_size);
evhttp_send_reply(request, 200, "OK", evb);
point5:
free(type);
point4:
free(real_root);
point3:
free(real_path);
point2:
free(decoded_path);
point1:
evhttp_uri_free(decoded_uri);
}
static int
runserver(const char *address, unsigned short port, const char *root)
{
struct event_base *base = event_base_new();
if (base == NULL) {
fprintf(stderr, "error: failed to initialize event base\n");
return 1;
}
struct evhttp *http = evhttp_new(base);
if (http == NULL) {
fprintf(stderr, "error: failed to initialize HTTP server\n");
return 1;
}
evhttp_set_gencb(http, handler, (char*) root);
evhttp_set_allowed_methods(http, EVHTTP_REQ_GET | EVHTTP_REQ_HEAD);
if (0 != evhttp_bind_socket(http, address, port)) {
fprintf(stderr, "error: failed to bind socket to %s:%d\n", address,
port);
return 1;
}
fprintf(stderr, " * Running on http://%s:%d/\n", address, port);
event_base_dispatch(base);
return 0;
}
static void
print_help(void)
{
printf(
"usage:\n"
" blogc-runserver [-h] [-v] [-t HOST] [-p PORT] DOCROOT\n"
" - A simple HTTP server to test blogc websites.\n"
"\n"
"positional arguments:\n"
" DOCROOT document root directory\n"
"\n"
"optional arguments:\n"
" -h show this help message and exit\n"
" -v show version and exit\n"
" -t HOST set server listen address (default: 127.0.0.1)\n"
" -p PORT set server listen port (default: 8080)\n");
}
static void
print_usage(void)
{
printf("usage: blogc-runserver [-h] [-v] [-t HOST] [-p PORT] DOCROOT\n");
}
int
main(int argc, char **argv)
{
signal(SIGPIPE, SIG_IGN);
int rv = 0;
char *host = NULL;
char *docroot = NULL;
unsigned short port = 8080;
unsigned int args = 0;
for (unsigned int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'h':
print_help();
goto cleanup;
case 'v':
printf("%s\n", PACKAGE_STRING);
goto cleanup;
case 't':
if (argv[i][2] != '\0')
host = sb_strdup(argv[i] + 2);
else
host = sb_strdup(argv[++i]);
break;
case 'p':
if (argv[i][2] != '\0')
port = strtoul(argv[i] + 2, NULL, 10);
else
port = strtoul(argv[++i], NULL, 10);
break;
default:
print_usage();
fprintf(stderr, "blogc-runserver: error: invalid "
"argument: -%c\n", argv[i][1]);
rv = 2;
goto cleanup;
}
}
else {
if (args > 0) {
print_usage();
fprintf(stderr, "blogc-runserver: error: only one positional "
"argument allowed\n");
rv = 2;
goto cleanup;
}
args++;
docroot = sb_strdup(argv[i]);
}
}
if (docroot == NULL) {
print_usage();
fprintf(stderr, "blogc-runserver: error: document root directory "
"required\n");
rv = 2;
goto cleanup;
}
if (host == NULL)
host = sb_strdup("127.0.0.1");
magic_all = magic_open(MAGIC_MIME);
magic_charset = magic_open(MAGIC_MIME_ENCODING);
if (magic_all == NULL || magic_charset == NULL) {
fprintf(stderr, "error: failed to initialize libmagic\n");
rv = 1;
goto cleanup;
}
if ((0 != magic_load(magic_all, NULL)) ||
(0 != magic_load(magic_charset, NULL)))
{
fprintf(stderr, "error: failed to load libmagic data\n");
magic_close(magic_all);
magic_close(magic_charset);
rv = 1;
goto cleanup;
}
rv = runserver(host, port, docroot);
magic_close(magic_all);
magic_close(magic_charset);
cleanup:
free(host);
free(docroot);
return rv;
}
|