-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.c
More file actions
executable file
·530 lines (449 loc) · 14.3 KB
/
httpserver.c
File metadata and controls
executable file
·530 lines (449 loc) · 14.3 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
#include "libhttp.h"
#include "wq.h"
/*
* Global configuration variables.
* handle_proxy_request. Their values are set up in main() using the
*/
wq_t work_queue; // Only used by poolserver
int num_threads; // Only used by poolserver
int server_port; // Default value: 8000
char *server_files_directory;
char *server_proxy_hostname;
int server_proxy_port;
struct i_o {
int fd1;
int fd2;
};
/*
* Serves the contents the file stored at `path` to the client socket `fd`.
* It is the caller's reponsibility to ensure that the file stored at `path` exists.
*/
void serve_file(int fd, char *path) {
int file;
file = open(path, O_RDONLY);
struct stat st;
fstat(file, &st);
off_t fileLength = st.st_size;
int length = snprintf( NULL, 0, "%d", (int) fileLength );
char* str = malloc( length + 1 );
snprintf( str, length + 1, "%d", (int) fileLength );
http_start_response(fd, 200);
http_send_header(fd, "Content-Type", http_get_mime_type(path));
http_send_header(fd, "Content-Length", str );
http_end_headers(fd);
free(str);
char buffer [512];
int read_cnt = 0;
while (1)
{
read_cnt = read(file,buffer, sizeof(buffer));
if (read_cnt < 1)
{
break;
}
write(fd, buffer, read_cnt);
}
close(file);
}
void serve_directory(int fd, char *path) {
char* buf [1000];
http_format_index(buf, path);
int tmp = open(buf, O_RDONLY);
if(tmp >= 0){
serve_file(fd, buf);
} else {
http_start_response(fd, 200);
http_send_header(fd, "Content-Type", http_get_mime_type(".html"));
http_end_headers(fd);
char* body [1000];
char* part [100];
DIR *dir;
struct dirent *dp;
char * file_name;
dir = opendir(path);
while ((dp=readdir(dir)) != NULL) {
file_name = dp->d_name;
http_format_href(part, path, file_name);
strncat(body, part, strlen(part));
}
write(fd, body, strlen(body));
}
close(fd);
}
void *proxy_helper(void *input_void) {
struct i_o *in = (struct i_o *)input_void;
int fd1 = in->fd1;
int fd2 = in->fd2;
char buf[512];
while(1) {
int bytes_written = 0;
int bytes_read = 0;
bytes_read = read(fd1, buf, sizeof(buf) - 1);
buf[bytes_read] = '\0';
int writing = 0;
while(bytes_written < bytes_read) {
writing = write(fd2, &buf[bytes_written], bytes_read - bytes_written);
if(writing < 1) break;
bytes_written += writing;
}
if(bytes_read + bytes_written <= 0) {
break;
}
}
pthread_exit(NULL);
}
/*
* Reads an HTTP request from client socket (fd), and writes an HTTP response
* containing:
*
* 1) If user requested an existing file, respond with the file
* 2) If user requested a directory and index.html exists in the directory,
* send the index.html file.
* 3) If user requested a directory and index.html doesn't exist, send a list
* of files in the directory with links to each.
* 4) Send a 404 Not Found response.
*
* Closes the client socket (fd) when finished.
*/
void handle_files_request(int fd) {
struct http_request *request = http_request_parse(fd);
if (request == NULL || request->path[0] != '/') {
http_start_response(fd, 400);
http_send_header(fd, "Content-Type", "text/html");
http_end_headers(fd);
close(fd);
return;
}
if (strstr(request->path, "..") != NULL) {
http_start_response(fd, 403);
http_send_header(fd, "Content-Type", "text/html");
http_end_headers(fd);
close(fd);
return;
}
/* Remove beginning `./` */
char *path = malloc(2 + strlen(request->path) + 1);
path[0] = '.';
path[1] = '/';
memcpy(path + 2, request->path, strlen(request->path) + 1);
struct stat st;
if(stat(path, &st) == 0)
{
if (S_ISDIR(st.st_mode))
{
serve_directory(fd, path);
} else {
serve_file(fd, path);
}
} else {
http_start_response(fd, 404);
}
close(fd);
return;
}
/*
* Opens a connection to the proxy target (hostname=server_proxy_hostname and
* port=server_proxy_port) and relays traffic to/from the stream fd and the
* proxy target_fd. HTTP requests from the client (fd) should be sent to the
* proxy target (target_fd), and HTTP responses from the proxy target (target_fd)
* should be sent to the client (fd).
*
* +--------+ +------------+ +--------------+
* | client | <-> | httpserver | <-> | proxy target |
* +--------+ +------------+ +--------------+
*
* Closes client socket (fd) and proxy target fd (target_fd) when finished.
*/
void handle_proxy_request(int fd) {
/*
* The code below does a DNS lookup of server_proxy_hostname and
* opens a connection to it. Please do not modify.
*/
struct sockaddr_in target_address;
memset(&target_address, 0, sizeof(target_address));
target_address.sin_family = AF_INET;
target_address.sin_port = htons(server_proxy_port);
// Use DNS to resolve the proxy target's IP address
struct hostent *target_dns_entry = gethostbyname2(server_proxy_hostname, AF_INET);
// Create an IPv4 TCP socket to communicate with the proxy target.
int target_fd = socket(PF_INET, SOCK_STREAM, 0);
if (target_fd == -1) {
fprintf(stderr, "Failed to create a new socket: error %d: %s\n", errno, strerror(errno));
close(fd);
exit(errno);
}
if (target_dns_entry == NULL) {
fprintf(stderr, "Cannot find host: %s\n", server_proxy_hostname);
close(target_fd);
close(fd);
exit(ENXIO);
}
char *dns_address = target_dns_entry->h_addr_list[0];
// Connect to the proxy target.
memcpy(&target_address.sin_addr, dns_address, sizeof(target_address.sin_addr));
int connection_status = connect(target_fd, (struct sockaddr*) &target_address,
sizeof(target_address));
if (connection_status < 0) {
/* Dummy request parsing, just to be compliant. */
http_request_parse(fd);
http_start_response(fd, 502);
http_send_header(fd, "Content-Type", "text/html");
http_end_headers(fd);
close(target_fd);
close(fd);
return;
}
pthread_t t1;
pthread_t t2;
struct i_o *in1 = malloc(sizeof(struct i_o));
struct i_o *in2 = malloc(sizeof(struct i_o));
in1->fd1 = fd;
in1->fd2 = target_fd;
in2->fd1 = target_fd;
in2->fd2 = fd;
pthread_create(&t1, NULL, proxy_helper, (void *)in1);
pthread_create(&t2, NULL, proxy_helper, (void *)in2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
close(target_fd);
close(fd);
return;
}
#ifdef POOLSERVER
/*
* All worker threads will run this function until the server shutsdown.
* Each thread should block until a new request has been received.
* When the server accepts a new connection, a thread should be dispatched
* to send a response to the client.
*/
void *handle_clients(void *void_request_handler) {
void (*request_handler)(int) = (void (*)(int)) void_request_handler;
/* (Valgrind) Detach so thread frees its memory on completion, since we won't
* be joining on it. */
pthread_detach(pthread_self());
int fd = wq_pop(&work_queue);
request_handler(fd);
close(fd);
return 0;
/* TODO: PART 7 */
}
/*
* Creates `num_threads` amount of threads. Initializes the work queue.
*/
void init_thread_pool(int num_threads, void (*request_handler)(int)) {
/* TODO: PART 7 */
pthread_t threads[num_threads];
wq_init(&work_queue);
for (int i = 0; i < num_threads; i++) {
pthread_create(&threads[i], NULL, handle_clients, (void *)request_handler);
}
}
#endif
/*
* Opens a TCP stream socket on all interfaces with port number PORTNO. Saves
* the fd number of the server socket in *socket_number. For each accepted
* connection, calls request_handler with the accepted fd number.
*/
void serve_forever(int *socket_number, void (*request_handler)(int)) {
struct sockaddr_in server_address, client_address;
size_t client_address_length = sizeof(client_address);
int client_socket_number;
// Creates a socket for IPv4 and TCP.
*socket_number = socket(PF_INET, SOCK_STREAM, 0);
if (*socket_number == -1) {
perror("Failed to create a new socket");
exit(errno);
}
int socket_option = 1;
if (setsockopt(*socket_number, SOL_SOCKET, SO_REUSEADDR, &socket_option,
sizeof(socket_option)) == -1) {
perror("Failed to set socket options");
exit(errno);
}
// Setup arguments for bind()
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(server_port);
/*
*
*
* Given the socket created above, call bind() to give it
* an address and a port. Then, call listen() with the socket.
* An appropriate size of the backlog is 1024, though you may
* play around with this value during performance testing.
*/
bind(*socket_number, &server_address, sizeof(server_address));
listen(*socket_number, 1024);
/* PART 1 END */
printf("Listening on port %d...\n", server_port);
#ifdef POOLSERVER
/*
* The thread pool is initialized *before* the server
* begins accepting client connections.
*/
init_thread_pool(num_threads, request_handler);
#endif
while (1) {
client_socket_number = accept(*socket_number,
(struct sockaddr *) &client_address,
(socklen_t *) &client_address_length);
if (client_socket_number < 0) {
perror("Error accepting socket");
continue;
}
printf("Accepted connection from %s on port %d\n",
inet_ntoa(client_address.sin_addr),
client_address.sin_port);
#ifdef BASICSERVER
/*
* This is a single-process, single-threaded HTTP server.
* When a client connection has been accepted, the main
* process sends a response to the client. During this
* time, the server does not listen and accept connections.
* Only after a response has been sent to the client can
* the server accept a new connection.
*/
request_handler(client_socket_number);
#elif FORKSERVER
/*
*
* When a client connection has been accepted, a new
* process is spawned. This child process will send
* a response to the client. Afterwards, the child
* process should exit. During this time, the parent
* process should continue listening and accepting
* connections.
*/
pid_t child = fork();
if(child != 0) {
//parent
continue;
} else if (child == -1) {
perror("fork err");
} else {
request_handler(client_socket_number);
close(client_socket_number);
exit(NULL);
}
#elif THREADSERVER
/*
*
* When a client connection has been accepted, a new
* thread is created. This thread will send a response
* to the client. The main thread should continue
* listening and accepting connections. The main
* thread will NOT be joining with the new thread.
*/
pthread_t thread;
pthread_create(&thread, NULL, request_handler, (void *) client_socket_number);
#elif POOLSERVER
/*
*
* When a client connection has been accepted, add the
* client's socket number to the work queue. A thread
* in the thread pool will send a response to the client.
*/
init_thread_pool(num_threads, request_handler);
wq_push(&work_queue, client_socket_number);
#endif
}
shutdown(*socket_number, SHUT_RDWR);
close(*socket_number);
}
int server_fd;
void signal_callback_handler(int signum) {
printf("Caught signal %d: %s\n", signum, strsignal(signum));
printf("Closing socket %d\n", server_fd);
if (close(server_fd) < 0) perror("Failed to close server_fd (ignoring)\n");
exit(0);
}
char *USAGE =
"Usage: ./httpserver --files some_directory/ [--port 8000 --num-threads 5]\n"
" ./httpserver --proxy inst.eecs.berkeley.edu:80 [--port 8000 --num-threads 5]\n";
void exit_with_usage() {
fprintf(stderr, "%s", USAGE);
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv) {
signal(SIGINT, signal_callback_handler);
signal(SIGPIPE, SIG_IGN);
/* Default settings */
server_port = 8000;
void (*request_handler)(int) = NULL;
int i;
for (i = 1; i < argc; i++) {
if (strcmp("--files", argv[i]) == 0) {
request_handler = handle_files_request;
server_files_directory = argv[++i];
if (!server_files_directory) {
fprintf(stderr, "Expected argument after --files\n");
exit_with_usage();
}
} else if (strcmp("--proxy", argv[i]) == 0) {
request_handler = handle_proxy_request;
char *proxy_target = argv[++i];
if (!proxy_target) {
fprintf(stderr, "Expected argument after --proxy\n");
exit_with_usage();
}
char *colon_pointer = strchr(proxy_target, ':');
if (colon_pointer != NULL) {
*colon_pointer = '\0';
server_proxy_hostname = proxy_target;
server_proxy_port = atoi(colon_pointer + 1);
} else {
server_proxy_hostname = proxy_target;
server_proxy_port = 80;
}
} else if (strcmp("--port", argv[i]) == 0) {
char *server_port_string = argv[++i];
if (!server_port_string) {
fprintf(stderr, "Expected argument after --port\n");
exit_with_usage();
}
server_port = atoi(server_port_string);
} else if (strcmp("--num-threads", argv[i]) == 0) {
char *num_threads_str = argv[++i];
if (!num_threads_str || (num_threads = atoi(num_threads_str)) < 1) {
fprintf(stderr, "Expected positive integer after --num-threads\n");
exit_with_usage();
}
} else if (strcmp("--help", argv[i]) == 0) {
exit_with_usage();
} else {
fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
exit_with_usage();
}
}
if (server_files_directory == NULL && server_proxy_hostname == NULL) {
fprintf(stderr, "Please specify either \"--files [DIRECTORY]\" or \n"
" \"--proxy [HOSTNAME:PORT]\"\n");
exit_with_usage();
}
#ifdef POOLSERVER
if (num_threads < 1) {
fprintf(stderr, "Please specify \"--num-threads [N]\"\n");
exit_with_usage();
}
#endif
chdir(server_files_directory);
serve_forever(&server_fd, request_handler);
return EXIT_SUCCESS;
}