1 | #include "worker.hfa" |
---|
2 | |
---|
3 | #include <errno.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <string.h> |
---|
6 | #include <unistd.h> |
---|
7 | |
---|
8 | #include <fstream.hfa> |
---|
9 | #include <iofwd.hfa> |
---|
10 | |
---|
11 | #include "options.hfa" |
---|
12 | #include "protocol.hfa" |
---|
13 | #include "filecache.hfa" |
---|
14 | |
---|
15 | //============================================================================================= |
---|
16 | // Worker Thread |
---|
17 | //============================================================================================= |
---|
18 | void ?{}( Worker & this ) { |
---|
19 | size_t cli = rand() % options.clopts.cltr_cnt; |
---|
20 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance[cli], 512000 }; |
---|
21 | options.clopts.thrd_cnt[cli]++; |
---|
22 | this.pipe[0] = -1; |
---|
23 | this.pipe[1] = -1; |
---|
24 | this.done = false; |
---|
25 | } |
---|
26 | |
---|
27 | extern "C" { |
---|
28 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
29 | } |
---|
30 | |
---|
31 | void main( Worker & this ) { |
---|
32 | park(); |
---|
33 | /* paranoid */ assert( this.pipe[0] != -1 ); |
---|
34 | /* paranoid */ assert( this.pipe[1] != -1 ); |
---|
35 | |
---|
36 | CONNECTION: |
---|
37 | for() { |
---|
38 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
39 | int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], CFA_IO_LAZY ); |
---|
40 | if(fd < 0) { |
---|
41 | if( errno == ECONNABORTED ) break; |
---|
42 | if( this.done && (errno == EINVAL || errno == EBADF) ) break; |
---|
43 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
44 | } |
---|
45 | if(this.done) break; |
---|
46 | |
---|
47 | if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; |
---|
48 | REQUEST: |
---|
49 | for() { |
---|
50 | bool closed; |
---|
51 | HttpCode code; |
---|
52 | const char * file; |
---|
53 | size_t name_size; |
---|
54 | |
---|
55 | // Read the http request |
---|
56 | size_t len = options.socket.buflen; |
---|
57 | char buffer[len]; |
---|
58 | if( options.log ) sout | "=== Reading request ==="; |
---|
59 | [code, closed, file, name_size] = http_read(fd, buffer, len); |
---|
60 | |
---|
61 | // if we are done, break out of the loop |
---|
62 | if( closed ) break REQUEST; |
---|
63 | |
---|
64 | // If this wasn't a request retrun 400 |
---|
65 | if( code != OK200 ) { |
---|
66 | sout | "=== Invalid Request :" | code_val(code) | "==="; |
---|
67 | answer_error(fd, code); |
---|
68 | continue REQUEST; |
---|
69 | } |
---|
70 | |
---|
71 | if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) { |
---|
72 | if( options.log ) sout | "=== Request for /plaintext ==="; |
---|
73 | |
---|
74 | int ret = answer_plaintext(fd); |
---|
75 | if( ret == -ECONNRESET ) break REQUEST; |
---|
76 | |
---|
77 | if( options.log ) sout | "=== Answer sent ==="; |
---|
78 | continue REQUEST; |
---|
79 | } |
---|
80 | |
---|
81 | if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) { |
---|
82 | if( options.log ) sout | "=== Request for /ping ==="; |
---|
83 | |
---|
84 | // Send the header |
---|
85 | int ret = answer_empty(fd); |
---|
86 | if( ret == -ECONNRESET ) break REQUEST; |
---|
87 | |
---|
88 | if( options.log ) sout | "=== Answer sent ==="; |
---|
89 | continue REQUEST; |
---|
90 | } |
---|
91 | |
---|
92 | if( options.log ) { |
---|
93 | sout | "=== Request for file " | nonl; |
---|
94 | write(sout, file, name_size); |
---|
95 | sout | " ==="; |
---|
96 | } |
---|
97 | |
---|
98 | if( !options.file_cache.path ) { |
---|
99 | if( options.log ) { |
---|
100 | sout | "=== File Not Found (" | nonl; |
---|
101 | write(sout, file, name_size); |
---|
102 | sout | ") ==="; |
---|
103 | } |
---|
104 | answer_error(fd, E405); |
---|
105 | continue REQUEST; |
---|
106 | } |
---|
107 | |
---|
108 | // Get the fd from the file cache |
---|
109 | int ans_fd; |
---|
110 | size_t count; |
---|
111 | [ans_fd, count] = get_file( file, name_size ); |
---|
112 | |
---|
113 | // If we can't find the file, return 404 |
---|
114 | if( ans_fd < 0 ) { |
---|
115 | if( options.log ) { |
---|
116 | sout | "=== File Not Found (" | nonl; |
---|
117 | write(sout, file, name_size); |
---|
118 | sout | ") ==="; |
---|
119 | } |
---|
120 | answer_error(fd, E404); |
---|
121 | continue REQUEST; |
---|
122 | } |
---|
123 | |
---|
124 | // Send the desired file |
---|
125 | int ret = answer_sendfile( this.pipe, fd, ans_fd, count); |
---|
126 | if( ret == -ECONNRESET ) break REQUEST; |
---|
127 | |
---|
128 | if( options.log ) sout | "=== Answer sent ==="; |
---|
129 | } |
---|
130 | |
---|
131 | if( options.log ) sout | "=== Connection closed ==="; |
---|
132 | continue CONNECTION; |
---|
133 | } |
---|
134 | } |
---|