1 | #include "worker.hfa" |
---|
2 | |
---|
3 | #include <errno.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <string.h> |
---|
6 | #include <unistd.h> |
---|
7 | |
---|
8 | #include <iofwd.hfa> |
---|
9 | |
---|
10 | #include "options.hfa" |
---|
11 | #include "protocol.hfa" |
---|
12 | #include "filecache.hfa" |
---|
13 | |
---|
14 | //============================================================================================= |
---|
15 | // Worker Thread |
---|
16 | //============================================================================================= |
---|
17 | void ?{}( Worker & this ) { |
---|
18 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance }; |
---|
19 | this.pipe[0] = -1; |
---|
20 | this.pipe[1] = -1; |
---|
21 | } |
---|
22 | |
---|
23 | void main( Worker & this ) { |
---|
24 | park(); |
---|
25 | /* paranoid */ assert( this.pipe[0] != -1 ); |
---|
26 | /* paranoid */ assert( this.pipe[1] != -1 ); |
---|
27 | |
---|
28 | CONNECTION: |
---|
29 | for() { |
---|
30 | printf("=== Accepting connection ===\n"); |
---|
31 | int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p ); |
---|
32 | if(fd < 0) { |
---|
33 | if( errno == ECONNABORTED ) break; |
---|
34 | if( errno == EINVAL ) break; |
---|
35 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
36 | } |
---|
37 | |
---|
38 | printf("=== New connection %d, waiting for requests ===\n", fd); |
---|
39 | REQUEST: |
---|
40 | for() { |
---|
41 | bool closed; |
---|
42 | HttpCode code; |
---|
43 | const char * file; |
---|
44 | size_t name_size; |
---|
45 | |
---|
46 | // Read the http request |
---|
47 | size_t len = options.socket.buflen; |
---|
48 | char buffer[len]; |
---|
49 | printf("=== Reading request ===\n"); |
---|
50 | [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel); |
---|
51 | |
---|
52 | // if we are done, break out of the loop |
---|
53 | if( closed ) { |
---|
54 | printf("=== Connection closed ===\n"); |
---|
55 | continue CONNECTION; |
---|
56 | } |
---|
57 | |
---|
58 | // If this wasn't a request retrun 400 |
---|
59 | if( code != OK200 ) { |
---|
60 | printf("=== Invalid Request : %d ===\n", code_val(code)); |
---|
61 | answer_error(fd, code); |
---|
62 | continue REQUEST; |
---|
63 | } |
---|
64 | |
---|
65 | printf("=== Request for file %.*s ===\n", (int)name_size, file); |
---|
66 | |
---|
67 | // Get the fd from the file cache |
---|
68 | int ans_fd; |
---|
69 | size_t count; |
---|
70 | [ans_fd, count] = get_file( file, name_size ); |
---|
71 | |
---|
72 | // If we can't find the file, return 404 |
---|
73 | if( ans_fd < 0 ) { |
---|
74 | printf("=== File Not Found ===\n"); |
---|
75 | answer_error(fd, E404); |
---|
76 | continue REQUEST; |
---|
77 | } |
---|
78 | |
---|
79 | // Send the header |
---|
80 | answer_header(fd, count); |
---|
81 | |
---|
82 | // Send the desired file |
---|
83 | sendfile( this.pipe, fd, ans_fd, count); |
---|
84 | |
---|
85 | printf("=== File sent ===\n"); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|