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