[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; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | extern "C" { |
---|
| 28 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
[0aec496] | 29 | } |
---|
| 30 | |
---|
| 31 | void main( Worker & this ) { |
---|
[e235429] | 32 | park(); |
---|
[d9c2284] | 33 | /* paranoid */ assert( this.pipe[0] != -1 ); |
---|
| 34 | /* paranoid */ assert( this.pipe[1] != -1 ); |
---|
| 35 | |
---|
[0aec496] | 36 | CONNECTION: |
---|
[e95a117] | 37 | for() { |
---|
[8c43d05] | 38 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
[2cd784a] | 39 | int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], CFA_IO_LAZY ); |
---|
[8e3034d] | 40 | if(fd < 0) { |
---|
| 41 | if( errno == ECONNABORTED ) break; |
---|
[ee59ede] | 42 | if( this.done && (errno == EINVAL || errno == EBADF) ) break; |
---|
[8e3034d] | 43 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 44 | } |
---|
[4f762d3] | 45 | if(this.done) break; |
---|
[e95a117] | 46 | |
---|
[8c43d05] | 47 | if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; |
---|
[0aec496] | 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 |
---|
[03ed863] | 56 | size_t len = options.socket.buflen; |
---|
[0aec496] | 57 | char buffer[len]; |
---|
[8c43d05] | 58 | if( options.log ) sout | "=== Reading request ==="; |
---|
[4f762d3] | 59 | [code, closed, file, name_size] = http_read(fd, buffer, len); |
---|
[0aec496] | 60 | |
---|
| 61 | // if we are done, break out of the loop |
---|
[ee59ede] | 62 | if( closed ) break REQUEST; |
---|
[0aec496] | 63 | |
---|
| 64 | // If this wasn't a request retrun 400 |
---|
| 65 | if( code != OK200 ) { |
---|
[8c43d05] | 66 | sout | "=== Invalid Request :" | code_val(code) | "==="; |
---|
[0aec496] | 67 | answer_error(fd, code); |
---|
| 68 | continue REQUEST; |
---|
| 69 | } |
---|
| 70 | |
---|
[561dd26] | 71 | if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) { |
---|
[8c43d05] | 72 | if( options.log ) sout | "=== Request for /plaintext ==="; |
---|
[0aec496] | 73 | |
---|
[187fdb8] | 74 | int ret = answer_plaintext(fd); |
---|
[ee59ede] | 75 | if( ret == -ECONNRESET ) break REQUEST; |
---|
[561dd26] | 76 | |
---|
[8c43d05] | 77 | if( options.log ) sout | "=== Answer sent ==="; |
---|
[97748ee] | 78 | continue REQUEST; |
---|
| 79 | } |
---|
[561dd26] | 80 | |
---|
[97748ee] | 81 | if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) { |
---|
[8c43d05] | 82 | if( options.log ) sout | "=== Request for /ping ==="; |
---|
[561dd26] | 83 | |
---|
| 84 | // Send the header |
---|
[ee59ede] | 85 | int ret = answer_empty(fd); |
---|
| 86 | if( ret == -ECONNRESET ) break REQUEST; |
---|
[561dd26] | 87 | |
---|
[8c43d05] | 88 | if( options.log ) sout | "=== Answer sent ==="; |
---|
[97748ee] | 89 | continue REQUEST; |
---|
[0aec496] | 90 | } |
---|
| 91 | |
---|
[8c43d05] | 92 | if( options.log ) { |
---|
| 93 | sout | "=== Request for file " | nonl; |
---|
| 94 | write(sout, file, name_size); |
---|
| 95 | sout | " ==="; |
---|
| 96 | } |
---|
[97748ee] | 97 | |
---|
[b57db73] | 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 | |
---|
[97748ee] | 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 ) { |
---|
[b57db73] | 115 | if( options.log ) { |
---|
| 116 | sout | "=== File Not Found (" | nonl; |
---|
| 117 | write(sout, file, name_size); |
---|
| 118 | sout | ") ==="; |
---|
| 119 | } |
---|
[97748ee] | 120 | answer_error(fd, E404); |
---|
| 121 | continue REQUEST; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // Send the desired file |
---|
[3f39009] | 125 | int ret = answer_sendfile( this.pipe, fd, ans_fd, count); |
---|
[ee59ede] | 126 | if( ret == -ECONNRESET ) break REQUEST; |
---|
[97748ee] | 127 | |
---|
[8c43d05] | 128 | if( options.log ) sout | "=== Answer sent ==="; |
---|
[0aec496] | 129 | } |
---|
[ee59ede] | 130 | |
---|
[8c43d05] | 131 | if( options.log ) sout | "=== Connection closed ==="; |
---|
[ee59ede] | 132 | continue CONNECTION; |
---|
[0aec496] | 133 | } |
---|
| 134 | } |
---|