-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver-evhttp.c
363 lines (311 loc) · 8.71 KB
/
server-evhttp.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "cJSON.h"
#define DEFAULT_PORT 12345
char uri_root[512];
char doc_root[100];
char name_server[100];
unsigned short port;
static const struct table_entry {
const char *extension;
const char *content_type;
} content_type_table[] = {
{ "txt", "text/plain" },
{ "c", "text/plain" },
{ "h", "text/plain" },
{ "html", "text/html" },
{ "htm", "text/htm" },
{ "css", "text/css" },
{ "gif", "image/gif" },
{ "jpg", "image/jpeg" },
{ "jpeg", "image/jpeg" },
{ "png", "image/png" },
{ "pdf", "application/pdf" },
{ "ps", "application/postsript" },
{ NULL, NULL },
};
/* Try to guess a good content-type for 'path' */
static const char * guess_content_type(const char *path)
{
const char *last_period, *extension;
const struct table_entry *ent;
last_period = strrchr(path, '.');
if (!last_period || strchr(last_period, '/'))
goto not_found; /* no extension */
extension = last_period + 1;
for (ent = &content_type_table[0]; ent->extension; ++ent) {
if (!evutil_ascii_strcasecmp(ent->extension, extension))
return ent->content_type;
}
not_found:
return "application/misc";
}
static int encode_request_to_file_path(const char* uri, char** whole_path) {
const char *path;
char *decoded_path;
struct evhttp_uri *decoded = NULL;
struct stat st;
size_t len;
*whole_path = NULL;
decoded = evhttp_uri_parse(uri);
if (!decoded) {
printf("It's not a good URI. Sending 404.\n");
return 0;
}
/* Let's see what path the user asked for. */
path = evhttp_uri_get_path(decoded);
if (!path) {
path = "/";
}
/* We need to decode it, to see what path the user really wanted. */
decoded_path = evhttp_uridecode(path, 0, NULL);
if (decoded_path == NULL) {
return 0;
}
len = strlen(decoded_path) + strlen(doc_root) + 15;
*whole_path = (char*)malloc(len);
if (!*whole_path) {
perror("malloc");
evhttp_uri_free(decoded);
free(decoded_path);
return 0;
}
evutil_snprintf(*whole_path, len, "%s%s", doc_root, decoded_path);
if (stat(*whole_path, &st) < 0) {
evhttp_uri_free(decoded);
free(decoded_path);
return 0;
}
if (S_ISDIR(st.st_mode)) {
// Find index.html of this page
if ((*whole_path)[strlen(*whole_path) - 1] == '/') {
strcat(*whole_path, "index.html");
} else {
strcat(*whole_path, "/index.html");
}
if (stat(*whole_path, &st) < 0) {
evhttp_uri_free(decoded);
free(decoded_path);
return 0;
}
}
evhttp_uri_free(decoded);
free(decoded_path);
return st.st_size;
}
static void dump_request_cb(struct evhttp_request *req, void *arg) {
const char *cmdtype;
struct evkeyvalq *headers;
struct evkeyval *header;
struct evbuffer *buf;
switch (evhttp_request_get_command(req)) {
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
case EVHTTP_REQ_POST: cmdtype = "POST"; break;
case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
default: cmdtype = "unknown"; break;
}
printf("Received a %s request for %s\nHeaders:\n",
cmdtype, evhttp_request_get_uri(req));
headers = evhttp_request_get_input_headers(req);
for (header = headers->tqh_first; header;
header = header->next.tqe_next) {
printf(" %s: %s\n", header->key, header->value);
}
buf = evhttp_request_get_input_buffer(req);
puts("Input data: <<<");
while (evbuffer_get_length(buf)) {
int n;
char cbuf[128];
n = evbuffer_remove(buf, cbuf, sizeof(cbuf));
if (n > 0)
(void) fwrite(cbuf, 1, n, stdout);
}
puts(">>>");
evhttp_send_reply(req, 200, "OK", NULL);
}
static void send_document_cb(struct evhttp_request *req, void *arg) {
struct evbuffer *evb = NULL;
const char *uri = evhttp_request_get_uri(req);
char *whole_path = NULL;
int fd = -1, fsize;
if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
dump_request_cb(req, arg);
return;
}
printf("[GET] Request <%s>", uri);
fsize = encode_request_to_file_path(uri, &whole_path);
if (!fsize) {
goto err;
}
evb = evbuffer_new();
const char *type = guess_content_type(whole_path);
if ((fd = open(whole_path, O_RDONLY)) < 0) {
perror("open");
goto err;
}
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", type);
evbuffer_add_file(evb, fd, 0, fsize);
printf(" [200]\n");
evhttp_send_reply(req, 200, "OK", evb);
goto done;
err:
printf(" [404]\n");
evhttp_send_error(req, 404, "Document was not found");
if (fd >= 0) {
close(fd);
}
done:
if (whole_path) {
free(whole_path);
}
if (evb) {
evbuffer_free(evb);
}
}
void diplay_socket_information(struct evhttp_bound_socket *handle) {
struct sockaddr_storage ss;
evutil_socket_t fd;
ev_socklen_t socklen = sizeof(ss);
char addrbuf[128];
void *inaddr;
const char *addr;
int got_port = -1;
fd = evhttp_bound_socket_get_fd(handle);
memset(&ss, 0, sizeof(ss));
if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
perror("getsockname() failed");
exit(1);
}
if (ss.ss_family == AF_INET) {
got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
} else {
fprintf(stderr, "Weird address family %d\n", ss.ss_family);
exit(1);
}
addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf, sizeof(addrbuf));
if (addr) {
printf("Listening on %s:%d\n", addr, got_port);
evutil_snprintf(uri_root, sizeof(uri_root),
"http://%s:%d",addr,got_port);
} else {
fprintf(stderr, "evutil_inet_ntop failed\n");
exit(1);
}
}
char * read_file(char * filename) {
char *buffer = NULL;
int string_size, read_size;
FILE *handler = fopen(filename, "r");
if (handler)
{
// Seek the last byte of the file
fseek(handler, 0, SEEK_END);
// Offset from the first to the last byte, or in other words, filesize
string_size = ftell(handler);
// go back to the start of the file
rewind(handler);
// Allocate a string that can hold it all
buffer = (char*) malloc(sizeof(char) * (string_size + 1) );
// Read it all in one operation
read_size = fread(buffer, sizeof(char), string_size, handler);
// fread doesn't set it so put a \0 in the last position
// and buffer is now officially a string
buffer[string_size] = '\0';
if (string_size != read_size)
{
// Something went wrong, throw away the memory and set
// the buffer to NULL
free(buffer);
buffer = NULL;
}
// Always remember to close the file.
fclose(handler);
}
return buffer;
}
void configure() {
char * config_location = "config/config.json";
char * json_string = read_file(config_location);
cJSON * root = cJSON_Parse(json_string);
cJSON * port_item = cJSON_GetObjectItemCaseSensitive(root, "port");
cJSON * directory_item = cJSON_GetObjectItemCaseSensitive(root, "directory");
cJSON * name_server_item = cJSON_GetObjectItemCaseSensitive(root, "name_server");
int port_value = 0;
if (cJSON_IsNumber(port_item)) {
port_value = port_item->valueint;
if (port_value > 0) {
port = port_value;
} else {
port = DEFAULT_PORT;
}
}
if (cJSON_IsString(directory_item)) {
strcpy(doc_root, directory_item->valuestring);
} else {
strcpy(doc_root, "htdocs");
}
if (cJSON_IsString(name_server_item)) {
strcpy(name_server, name_server_item->valuestring);
} else {
strcpy(name_server, "localhost");
}
cJSON_Delete(root);
}
void show_license() {
char * license = read_file("LICENSE-TEXT");
printf("%s\n", license);
}
int main(int argc, char **argv) {
configure();
show_license();
struct event_base *base;
struct evhttp *http;
struct evhttp_bound_socket *handle;
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
return 1;
}
base = event_base_new();
if (!base) {
fprintf(stderr, "Couldn't create an event_base: exiting\n");
return 1;
}
http = evhttp_new(base);
if (!http) {
fprintf(stderr, "couldn't create evhttp. Exiting.\n");
return 1;
}
evhttp_set_cb(http, "/dump", dump_request_cb, NULL);
evhttp_set_gencb(http, send_document_cb, NULL);
handle = evhttp_bind_socket_with_handle(http, name_server, port);
if (!handle) {
fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
(int)port);
return 1;
}
diplay_socket_information(handle);
event_base_dispatch(base);
return 0;
}