-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.c
396 lines (334 loc) · 13 KB
/
handler.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "handler.h"
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "cgi.h"
#include "flags.h"
#include "getuserdir.h"
#include "logger.h"
#include "readdirs.h"
#include "structures.h"
#include "util.h"
int current_fd;
bool close_current_connection = true;
bool debug_mode = false;
struct flags_struct *g_flags;
REQUEST *g_request;
RESPONSE *g_response;
char *g_rip;
void close_connection(int fd) {
/** Sending a tcp close connection message to the client */
(void)shutdown(fd, SHUT_RDWR);
(void)close(fd);
int logger_fd;
if (g_flags->l_flag) {
if (g_flags->d_flag) {
logger_fd = STDOUT_FILENO;
} else {
if ((logger_fd = open(g_flags->log_file_arg, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR)) == -1) {
/* Stopping the server if we don't have access to the logfile '*/
_exit(EXIT_FAILURE);
}
}
writelog(logger_fd, *g_request, *g_response, g_rip);
}
/** In debug mode there is no child process that is handling client request,
* if we exit it will cause the entire server to stop */
// if (!debug_mode) {
/** Child dies peacefully :) */
_exit(EXIT_SUCCESS);
//}
}
void createResponse(RESPONSE *response) {
(void)strncpy((*response).server, SERVER, strlen(SERVER));
(void)strncpy((*response).content_type, CONTENT_TYPE_DEFAULT, strlen(CONTENT_TYPE_DEFAULT));
get_gmt_date_str((*response).date, DATE_MAX_LEN);
(void)strncpy((*response).protocol, SUPPORTED_PROTOCOL_ONLY, strlen(SUPPORTED_PROTOCOL_ONLY));
(void)strncpy((*response).version, SUPPORTED_VERSION_ONLY, strlen(SUPPORTED_VERSION_ONLY));
get_status_verb((*response).status_code, (*response).status_verb);
}
void alarm_handler(int sig_num) {
/** The definition of the handler is supposed to have this but I have no use
* with it :( */
(void)sig_num;
/** If in the meantime there has been something going on between clien
* and server then we won't close the connection yet. */
if (close_current_connection) {
close_connection(current_fd);
}
}
void handleFirstLine(const char *separator, char *token, char *line_buffer, bool *is_first_line, bool *is_valid_request,
REQUEST *request) {
int iterator = 0;
token = strtok(line_buffer, separator);
while (token != NULL) {
if (iterator <= 2) {
(*is_valid_request) = (*is_valid_request) && create_request_frame(request, token, iterator);
} else {
/** This is a bad request brother */
(*is_valid_request) = false;
}
iterator++;
token = strtok(NULL, separator);
}
(*is_first_line) = false;
/** We need minimum three tokens ("GET", "/", "HTTP/1.0") */
if (iterator < 2) {
(*is_valid_request) = false;
}
}
/** Make sure to set the status_code and content length before calling this
* function :) */
void send_headers(int fd, bool is_valid_request, RESPONSE *response, char *response_string) {
if ((*response).status_code == 0) {
if (!is_valid_request) {
(*response).status_code = 400;
} else {
(*response).status_code = 200;
}
}
createResponse(response);
/**
* Even when the timeout is done, we shouldn't close this connection as user
* is done with his input and we are the ones pending to serve either dir
* indexing, file serving or CGI execution
*/
close_current_connection = false;
/** We stop taking anything else from client now */
(void)create_response_string(response, response_string);
g_response = response;
/** We couldn't write headers, so it is better to close the connection and
* call it a day */
if (write(fd, response_string, strlen(response_string)) < 0) {
close_connection(fd);
}
if (write(fd, "\r\n", strlen("\r\n")) < 0) {
close_connection(fd);
}
if (response->status_verb != NULL) {
if (strlen(g_request->verb) == strlen(SUPPORTED_HTTP_VERB_2)) {
if (strncmp(g_request->verb, SUPPORTED_HTTP_VERB_2, strlen(SUPPORTED_HTTP_VERB_2)) == 0) {
close_connection(fd);
}
}
}
}
void send_error(int status_code, int socket_fd, bool is_valid_request, RESPONSE *response, char *response_string) {
response->status_code = status_code;
send_headers(socket_fd, is_valid_request, response, response_string);
/** Sending the error again because clients like browsers will just show
* empty page on error if we don't. */
if (write(socket_fd, response->status_verb, strlen(response->status_verb)) < 0) {
close_connection(socket_fd);
}
if (write(socket_fd, "\r\n", strlen("\r\n")) < 0) {
close_connection(socket_fd);
}
close_connection(socket_fd);
}
/** This code has been referenced from CS631 APUE class notes apue-code/09 */
void handleConnection(int fd, struct sockaddr_in6 client, struct flags_struct flags) {
const char *rip;
char claddr[INET6_ADDRSTRLEN];
bool is_first_line = true;
char separator[2] = " ";
char *token;
bool is_valid_request = true;
ssize_t n_chars = 0;
REQUEST request;
RESPONSE response;
response.status_code = 0;
char response_string[BUFSIZ];
bzero(response_string, sizeof(response_string));
reset_request_object(&request);
reset_response_object(&response);
int number_of_headers = 0;
char line_buffer_chars[1];
bool last_char_was_next_line = false;
bool stream_done = false;
bool end_of_request = false;
if ((rip = inet_ntop(PF_INET6, &(client.sin6_addr), claddr, INET6_ADDRSTRLEN)) == NULL) {
perror("inet_ntop");
/** Something wrong with rip */
g_request = &request;
send_error(500, fd, is_valid_request, &response, response_string);
}
while (!stream_done) {
int cursor = 0;
char line_buffer[SUPPORTED_MAX_HEADER_SIZE + 1];
bzero(line_buffer, sizeof(line_buffer));
while (true) {
/** We are reading one character at a time to parse the entire
* request */
n_chars = read(fd, line_buffer_chars, 1);
/** An error has occured while reading :( */
if (n_chars < 0) {
g_request = &request;
send_error(500, fd, is_valid_request, &response, response_string);
return;
}
if (n_chars == 0) {
stream_done = true;
break;
}
if (strncmp(line_buffer_chars, "\n", 1) == 0) {
if (last_char_was_next_line) {
/** This is the end of the request. */
end_of_request = true;
}
last_char_was_next_line = true;
break;
} else {
if (strncmp(line_buffer_chars, "\r", 1) != 0) {
last_char_was_next_line = false;
line_buffer[cursor] = line_buffer_chars[0];
cursor++;
}
}
/** Client has given a header that is greater than the size we
* accept. */
if (cursor >= SUPPORTED_MAX_HEADER_SIZE) {
end_of_request = true;
response.status_code = 413;
break;
}
}
line_buffer[cursor + 1] = '\0';
number_of_headers++;
if (number_of_headers >= MAX_NUMBER_OF_HEADERS) {
end_of_request = true;
response.status_code = 413;
break;
}
if (end_of_request) {
break;
}
if (number_of_headers == 1) {
handleFirstLine(separator, token, line_buffer, &is_first_line, &is_valid_request, &request);
} else {
/** Reading the headers we don't validate or care about anything
* else except for If-Modified-Since */
token = strtok(line_buffer, separator);
if ((strlen(token) == strlen(SUPPORTED_HEADER)) && (strncmp(token, SUPPORTED_HEADER, strlen(token)) == 0)) {
token = strtok(NULL, "");
is_valid_request = is_valid_request && create_request_frame(&request, token, 3);
} else {
continue;
}
}
}
if (!is_valid_request) {
g_request = &request;
send_error(400, fd, is_valid_request, &response, response_string);
}
if (is_valid_request && (request.if_modified_str_type != 0)) {
struct tm tm;
time_t t = 0;
if (request.if_modified_str_type == 1) {
if (strptime(request.if_modified_since, "%a, %d %b %Y %T GMT", &tm) == NULL) {
perror("strptime failed");
/** This is a bad request because the regex matched but the
* values doesn't make sense, which is a mistake from client */
send_error(400, fd, is_valid_request, &response, response_string);
}
} else if (request.if_modified_str_type == 2) {
if (strptime(request.if_modified_since, "%a, %d-%b-%y %T GMT", &tm) == NULL) {
perror("strptime failed");
/** This is a bad request because the regex matched but the
* values doesn't make sense, which is a mistake from client */
send_error(400, fd, is_valid_request, &response, response_string);
}
} else if (request.if_modified_str_type == 3) {
if (strptime(request.if_modified_since, "%a %b %d %T %Y GMT", &tm) == NULL) {
perror("strptime failed");
/** This is a bad request because the regex matched but the
* values doesn't make sense, which is a mistake from client */
send_error(400, fd, is_valid_request, &response, response_string);
}
}
if ((t = mktime(&tm)) < 0) {
perror("mktime failed");
send_error(500, fd, is_valid_request, &response, response_string);
}
request.if_modified_t = t;
}
g_request = &request;
g_rip = (char *)rip;
if ((strncmp(request.path, "/cgi-bin", strlen("/cgi-bin")) == 0) && flags.c_flag) {
execute_file(request.path, fd, is_valid_request, &response, response_string, flags);
} else {
if (strncmp(request.path, "/~", strlen("/~")) == 0) {
getuserdir(request.path + 2, fd, request.if_modified_t, is_valid_request, &response, response_string);
} else {
readdirs(request.path, flags.working_dir, fd, request.if_modified_t, is_valid_request, &response, response_string);
}
}
}
/** This code has been referenced from CS631 APUE class notes apue-code/09 */
void handleSocket(int socket, struct flags_struct flags) {
int fd;
pid_t pid;
struct sockaddr_in6 client;
socklen_t length;
memset(&client, 0, sizeof(client));
length = sizeof(client);
if ((fd = accept(socket, (struct sockaddr *)&client, &length)) < 0) {
/** If accept is failing there is not much we can do apart from ignoring
* and moving on to next request because we don't even have fd to say
* something to client */
perror("accept");
return;
}
g_flags = &flags;
if (flags.d_flag) {
pid = 0;
debug_mode = true;
} else {
}
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (!pid) {
/** Child */
current_fd = fd;
if (signal(SIGALRM, alarm_handler) == SIG_ERR) {
perror("signal for SIGALRM ");
/** We need all of these to send an error. :( */
REQUEST request;
RESPONSE response;
response.status_code = 0;
char response_string[BUFSIZ];
bzero(response_string, sizeof(response_string));
reset_response_object(&response);
g_request = &request;
send_error(500, fd, true, &response, response_string);
}
if (alarm(TIMEOUT) == ((unsigned int)-1)) {
/** We need all of these to send an error. :( */
RESPONSE response;
REQUEST request;
response.status_code = 0;
char response_string[BUFSIZ];
bzero(response_string, sizeof(response_string));
reset_response_object(&response);
g_request = &request;
send_error(500, fd, true, &response, response_string);
}
handleConnection(fd, client, flags);
} else if (pid > 0) {
/** Parent */
if (flags.d_flag) {
int status = -1;
/** We don't need to worry about the status of the child.
* The purpose of this wait is to make the parent blocking until and unless this request is being served
* which is the whole point of debug mode.
*/
(void)waitpid(pid, &status, 0);
}
}
}