-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
310 lines (258 loc) · 8.63 KB
/
http.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
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <dirent.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "utils/log.h"
#include "utils/time.h"
#include "utils/vec.h"
#include "utils/sync.h"
#include "utils/html.h"
#include "utils/compress.h"
#include "utils/profile.h"
#include "http/response.h"
#include "http/request.h"
response_t serve_error(enum StatusCode c) {
response_t r = resp_new(c);
resp_add_hdr(&r, "Content-Type", "text/html");
char errt[256];
sprintf(errt, "Error %d", c);
html_t html = html_new();
html_body_add(&html.body, html_h1_new(errt));
html_body_add(&html.body, html_p_new("Hey! Don't do that."));
char* msg = html_render(&html);
resp_add_content(&r, msg, strlen(msg));
return r;
}
vec_t paths;
hashmap_t path_redirs;
char* get_file_ext(char* filename) {
char* ext = strtok(filename, ".");
char* next;
while ((next = strtok(NULL, "."))) ext = next;
return ext;
}
response_t serve_file(request_t* req) {
char path[128+8] = "./public";
strcat(path, req->path);
int fd = open(path, O_RDONLY); // TODO handle
response_t r = resp_new(OK);
struct stat st;
fstat(fd, &st);
char* fbuf = malloc(st.st_size); // TODO what if file larger than memory?
for (size_t i = 0; read(fd, fbuf+(i*4096), 4096) > 0; i++);
char* ext = get_file_ext(req->path);
resp_set_ctype(&r, ext);
bool ok = true;
char* mtype = (char*)ext_to_mtype(ext);
char* hdr;
if ((hdr = hashmap_get(&req->headers, "Accept"))) {
ok = false;
vec_t mtypes = hdr_parse_accept(hdr);
for (int j = 0; j < mtypes.vec_sz; j++) {
struct req_mimetype* a_mtype = vec_get(&mtypes, j);
// TODO doesn't handle stuff like image/* (is that even allowed?)
if (strcmp(a_mtype->item, mtype) == 0 || strcmp(a_mtype->item, "*/*") == 0) {
ok = true;
break;
}
}
}
char* buf = fbuf; // buffer to be written
size_t bufsize = st.st_size;
if (ok && (hdr = hashmap_get(&req->headers, "Accept-Encoding"))) {
ok = false;
vec_t mtypes = hdr_parse_accept(hdr); // abuse of this func
for (int j = 0; j < mtypes.vec_sz; j++) {
struct req_mimetype* a_mtype = vec_get(&mtypes, j);
if (strcmp(a_mtype->item, "gzip") == 0) {
resp_add_hdr(&r, "Content-Encoding", "gzip");
buf = gzip_compress(buf, &bufsize);
ok = true;
break;
} else if (strcmp(a_mtype->item, "deflate") == 0) {
resp_add_hdr(&r, "Content-Encoding", "deflate");
buf = zlib_compress(buf, &bufsize);
ok = true;
break;
} else if (strcmp(a_mtype->item, "identity") == 0) {
ok = true;
break;
}
}
}
/* if (!ok) return serve_error(NotAcceptable); */
char datebuf[64];
time_to_str(st.st_mtim.tv_sec, datebuf);
resp_add_hdr(&r, "Last-Modified", datebuf);
resp_add_content(&r, buf, bufsize);
free(buf);
return r;
}
// TODO actually respect requests
// TODO list dirs
response_t make_response (request_t* req, int pfd) {
if (req_parse(req) < 0) {
log_error("Failed to parse incoming request.");
return serve_error(BadRequest);
}
if (hashmap_get(&req->headers, "Profile") != 0x0) {
int profile = true;
if (ptrace(PTRACE_TRACEME, NULL) < 0) {
log_error("PTRACE_TRACME failed with err %d", errno);
}
if (write(pfd, "profile", 8) != 8) {
log_error("write err");
}
usleep(100);
}
log_info("Got request %s %s", method_name(req->method), req->path);
char* mapped_path = hashmap_get(&path_redirs, req->path);
if (mapped_path != NULL) {
strcpy(req->path, mapped_path);
}
if (vec_check(&paths, req->path, (sv_cmp_t)strcmp) == -1) {
return serve_error(NotFound);
}
log_debug("method = %d", req->method);
switch (req->method) {
case GET: return serve_file(req);
default: return serve_error(MethodNotAllowed);
}
}
double diff_timespec(const struct timespec *time1, const struct timespec *time0) {
return (time1->tv_sec - time0->tv_sec)
+ (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
}
void* handle_conn(int ns, int pfd) {
char msgbuf[1024] = {0};
while(true) {
struct timespec before, after, tdiff;
if (recv(ns, msgbuf, 1024, 0) == 0) {
log_info("Closing connection.");
return 0x0;
}
clock_gettime(CLOCK_MONOTONIC, &before);
if (msgbuf[0] != 0) {
request_t req = req_new(msgbuf, 1024);
response_t r = make_response(&req, pfd);
send(ns, r.content, r.sz, 0);
free(r.content);
write(pfd, "stop", 5);
clock_gettime(CLOCK_MONOTONIC, &after);
log_info("Handled request in %f sec", diff_timespec(&after, &before));
req_free(&req);
}
}
}
int list_files_sv(vec_t* sv, char* base) {
char path[MAX_PATH_LEN] = "/";
struct dirent* dp;
DIR* dir = opendir(base);
if (!dir) return -1;
while ((dp = readdir(dir)) != NULL) {
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
strcpy(path+1, base);
strcat(path, "/");
strcat(path, dp->d_name);
vec_push(sv, strchr(path+1, '/'));
list_files_sv(sv, path+1);
}
}
closedir(dir);
return 0;
}
channel_t listen_chan;
void* listen_for_conns(void* ctxt) {
int s = *(int*)ctxt;
int namelen;
struct sockaddr_in client;
while (true) {
listen(s, 1);
int ns = accept(s, (struct sockaddr*)&client, (socklen_t*)&namelen);
log_info("Handling connection from %s", inet_ntoa(client.sin_addr));
channel_push(&listen_chan, &ns);
}
}
struct conn_ctxt {
pid_t pid;
int fd;
};
// TODO some sort of DDOS protection idk
int main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) die("socket");
int portnum = 8080;
struct sockaddr_in name;
name.sin_family = AF_INET;
name.sin_port = htons(portnum);
name.sin_addr.s_addr = htonl(INADDR_ANY);
while(bind(s, (struct sockaddr*)&name, sizeof(name)) < 0) {
portnum += 1;
name.sin_port = htons(portnum);
}
paths = vec_new(MAX_PATH_LEN);
path_redirs = hashmap_new(MAX_PATH_LEN, MAX_PATH_LEN);
path_redirs.vark = true;
hashmap_set(&path_redirs, "/", "/index.html");
list_files_sv(&paths, "public");
int nlen;
getsockname(s, (struct sockaddr*)&name, (socklen_t*)&nlen);
log_debug("Open on port %d.", htons(name.sin_port));
listen_chan = channel_new(sizeof(int));
pthread_t lthread;
pthread_create(<hread, NULL, listen_for_conns, &s);
int namelen;
struct sockaddr_in client;
vec_t conns = vec_new(sizeof(struct conn_ctxt));
while (true) {
int* ptr = channel_try_recv(&listen_chan);
if (ptr != NULL) {
int pfds[2];
pipe(pfds);
int retval = fcntl(pfds[0], F_SETFL, fcntl(pfds[0], F_GETFL) | O_NONBLOCK);
pid_t pid = fork();
if (pid == 0) {
handle_conn(*ptr, pfds[1]);
close(pfds[0]);
close(pfds[1]);
exit(0);
}
struct conn_ctxt ctxt = {.fd = pfds[0], .pid = pid };
vec_push(&conns, &ctxt);
}
for (int i = 0; i < conns.vec_sz; i++) {
struct conn_ctxt* ctxt = vec_get(&conns, i);
char msg[8] = {0};
if (read(ctxt->fd, &msg, 8) != 8 || strcmp(msg, "profile") != 0) continue;
struct profile_node prof_res = profile_res_new();
while(true) {
vec_t stack = profile(ctxt->pid);
profile_proc_stack(&prof_res, &stack);
usleep(3);
if (read(ctxt->fd, &msg, 5) == 5 && strcmp(msg, "stop") == 0) break;
}
profile_dump(&prof_res, -1);
}
}
}
// meta todos:
// - TODO true dependencyless (no zlib, no pthread)
// - TODO HTML parsing maybe but that makes me want to cry
// - TODO nice ways of dynamically *generating* html
// - TODO dynamically profile requests
// - TODO some kinda config file parsing