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( __cfaabi_dbg_ctx ); |
---|
25 | /* paranoid */ assert( this.pipe[0] != -1 ); |
---|
26 | /* paranoid */ assert( this.pipe[1] != -1 ); |
---|
27 | |
---|
28 | CONNECTION: |
---|
29 | for() { |
---|
30 | int fd = take(wait_connect); |
---|
31 | if (fd < 0) break; |
---|
32 | |
---|
33 | printf("New connection %d, waiting for requests\n", fd); |
---|
34 | REQUEST: |
---|
35 | for() { |
---|
36 | bool closed; |
---|
37 | HttpCode code; |
---|
38 | const char * file; |
---|
39 | size_t name_size; |
---|
40 | |
---|
41 | // Read the http request |
---|
42 | size_t len = options.socket.buflen; |
---|
43 | char buffer[len]; |
---|
44 | printf("Reading request\n"); |
---|
45 | [code, closed, file, name_size] = http_read(fd, buffer, len); |
---|
46 | |
---|
47 | // if we are done, break out of the loop |
---|
48 | if( closed ) { |
---|
49 | printf("Connection closed\n"); |
---|
50 | continue CONNECTION; |
---|
51 | } |
---|
52 | |
---|
53 | // If this wasn't a request retrun 400 |
---|
54 | if( code != OK200 ) { |
---|
55 | printf("Invalid Request : %d\n", code_val(code)); |
---|
56 | answer_error(fd, code); |
---|
57 | continue REQUEST; |
---|
58 | } |
---|
59 | |
---|
60 | printf("Request for file %.*s\n", (int)name_size, file); |
---|
61 | |
---|
62 | // Get the fd from the file cache |
---|
63 | int ans_fd; |
---|
64 | size_t count; |
---|
65 | [ans_fd, count] = get_file( file, name_size ); |
---|
66 | |
---|
67 | // If we can't find the file, return 404 |
---|
68 | if( ans_fd < 0 ) { |
---|
69 | printf("File Not Found\n"); |
---|
70 | answer_error(fd, E404); |
---|
71 | continue REQUEST; |
---|
72 | } |
---|
73 | |
---|
74 | // Send the header |
---|
75 | answer_header(fd, count); |
---|
76 | |
---|
77 | // Send the desired file |
---|
78 | sendfile( this.pipe, fd, ans_fd, count); |
---|
79 | |
---|
80 | printf("File sent\n"); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | //============================================================================================= |
---|
86 | // Acceptor Thread |
---|
87 | //============================================================================================= |
---|
88 | void ?{}( Acceptor & this, int sockfd, struct sockaddr * addr, socklen_t * addrlen, int flags ) { |
---|
89 | ((thread&)this){ "Acceptor Thread", *options.clopts.instance }; |
---|
90 | this.sockfd = sockfd; |
---|
91 | this.addr = addr; |
---|
92 | this.addrlen = addrlen; |
---|
93 | this.flags = flags; |
---|
94 | } |
---|
95 | |
---|
96 | void main( Acceptor & this ) { |
---|
97 | for() { |
---|
98 | int ret = cfa_accept4( this.[sockfd, addr, addrlen, flags] ); |
---|
99 | if(ret < 0) { |
---|
100 | if( errno == ECONNABORTED ) break; |
---|
101 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
102 | } |
---|
103 | |
---|
104 | printf("New connection accepted\n"); |
---|
105 | put( wait_connect, ret ); |
---|
106 | } |
---|
107 | } |
---|