[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> |
---|
[7f0ac12] | 10 | #include <mutex_stmt.hfa> |
---|
[0aec496] | 11 | |
---|
| 12 | #include "options.hfa" |
---|
| 13 | #include "protocol.hfa" |
---|
| 14 | #include "filecache.hfa" |
---|
| 15 | |
---|
[7f0ac12] | 16 | void ?{}( sendfile_stats_t & this ) { |
---|
| 17 | this.calls = 0; |
---|
| 18 | this.tries = 0; |
---|
| 19 | this.header = 0; |
---|
| 20 | this.splcin = 0; |
---|
| 21 | this.splcot = 0; |
---|
| 22 | for(i; zipf_cnts) { |
---|
| 23 | this.avgrd[i].calls = 0; |
---|
| 24 | this.avgrd[i].bytes = 0; |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
[0aec496] | 28 | //============================================================================================= |
---|
[7f0ac12] | 29 | // Generic connection handling |
---|
[0aec496] | 30 | //============================================================================================= |
---|
[7f0ac12] | 31 | static void handle_connection( connection & this, volatile int & fd, char * buffer, size_t len, io_future_t * f ) { |
---|
| 32 | REQUEST: |
---|
| 33 | for() { |
---|
| 34 | bool closed; |
---|
| 35 | HttpCode code; |
---|
| 36 | const char * file; |
---|
| 37 | size_t name_size; |
---|
| 38 | |
---|
| 39 | // Read the http request |
---|
| 40 | if( options.log ) sout | "=== Reading request ==="; |
---|
| 41 | [code, closed, file, name_size] = http_read(fd, buffer, len, f); |
---|
| 42 | f = 0p; |
---|
| 43 | |
---|
| 44 | // if we are done, break out of the loop |
---|
| 45 | if( closed ) break REQUEST; |
---|
| 46 | |
---|
| 47 | // If this wasn't a request retrun 400 |
---|
| 48 | if( code != OK200 ) { |
---|
| 49 | sout | "=== Invalid Request :" | code_val(code) | "==="; |
---|
| 50 | answer_error(fd, code); |
---|
| 51 | continue REQUEST; |
---|
| 52 | } |
---|
[ef3c383] | 53 | |
---|
[7f0ac12] | 54 | if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) { |
---|
| 55 | if( options.log ) sout | "=== Request for /plaintext ==="; |
---|
| 56 | |
---|
| 57 | int ret = answer_plaintext(fd); |
---|
| 58 | if( ret == -ECONNRESET ) break REQUEST; |
---|
| 59 | |
---|
| 60 | if( options.log ) sout | "=== Answer sent ==="; |
---|
| 61 | continue REQUEST; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) { |
---|
| 65 | if( options.log ) sout | "=== Request for /ping ==="; |
---|
| 66 | |
---|
| 67 | // Send the header |
---|
| 68 | int ret = answer_empty(fd); |
---|
| 69 | if( ret == -ECONNRESET ) break REQUEST; |
---|
| 70 | |
---|
| 71 | if( options.log ) sout | "=== Answer sent ==="; |
---|
| 72 | continue REQUEST; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | if( options.log ) { |
---|
| 76 | sout | "=== Request for file " | nonl; |
---|
| 77 | write(sout, file, name_size); |
---|
| 78 | sout | " ==="; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | if( !options.file_cache.path ) { |
---|
| 82 | if( options.log ) { |
---|
| 83 | sout | "=== File Not Found (" | nonl; |
---|
| 84 | write(sout, file, name_size); |
---|
| 85 | sout | ") ==="; |
---|
| 86 | } |
---|
| 87 | answer_error(fd, E405); |
---|
| 88 | continue REQUEST; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // Get the fd from the file cache |
---|
| 92 | int ans_fd; |
---|
| 93 | size_t count; |
---|
| 94 | [ans_fd, count] = get_file( file, name_size ); |
---|
| 95 | |
---|
| 96 | // If we can't find the file, return 404 |
---|
| 97 | if( ans_fd < 0 ) { |
---|
| 98 | if( options.log ) { |
---|
| 99 | sout | "=== File Not Found (" | nonl; |
---|
| 100 | write(sout, file, name_size); |
---|
| 101 | sout | ") ==="; |
---|
| 102 | } |
---|
| 103 | answer_error(fd, E404); |
---|
| 104 | continue REQUEST; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // Send the desired file |
---|
| 108 | int ret = answer_sendfile( this.pipe, fd, ans_fd, count, this.stats.sendfile ); |
---|
| 109 | if( ret == -ECONNRESET ) break REQUEST; |
---|
| 110 | |
---|
| 111 | if( options.log ) sout | "=== Answer sent ==="; |
---|
[ef3c383] | 112 | } |
---|
[481ee28] | 113 | } |
---|
| 114 | |
---|
[7f0ac12] | 115 | //============================================================================================= |
---|
| 116 | // Self Accepting Worker Thread |
---|
| 117 | //============================================================================================= |
---|
| 118 | void ?{}( AcceptWorker & this ) { |
---|
[8c58e73] | 119 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 }; |
---|
| 120 | options.clopts.thrd_cnt++; |
---|
[7f0ac12] | 121 | this.done = false; |
---|
[0aec496] | 122 | } |
---|
| 123 | |
---|
[7f0ac12] | 124 | void main( AcceptWorker & this ) { |
---|
[e235429] | 125 | park(); |
---|
[7f0ac12] | 126 | /* paranoid */ assert( this.conn.pipe[0] != -1 ); |
---|
| 127 | /* paranoid */ assert( this.conn.pipe[1] != -1 ); |
---|
[e95a117] | 128 | for() { |
---|
[8c43d05] | 129 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
[7f0ac12] | 130 | int fd = cfa_accept4( this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY ); |
---|
[8e3034d] | 131 | if(fd < 0) { |
---|
| 132 | if( errno == ECONNABORTED ) break; |
---|
[ee59ede] | 133 | if( this.done && (errno == EINVAL || errno == EBADF) ) break; |
---|
[8e3034d] | 134 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 135 | } |
---|
[4f762d3] | 136 | if(this.done) break; |
---|
[e95a117] | 137 | |
---|
[8c43d05] | 138 | if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; |
---|
[7f0ac12] | 139 | size_t len = options.socket.buflen; |
---|
| 140 | char buffer[len]; |
---|
| 141 | handle_connection( this.conn, fd, buffer, len, 0p ); |
---|
[0aec496] | 142 | |
---|
[7f0ac12] | 143 | if( options.log ) sout | "=== Connection closed ==="; |
---|
| 144 | } |
---|
| 145 | } |
---|
[0aec496] | 146 | |
---|
[561dd26] | 147 | |
---|
[7f0ac12] | 148 | //============================================================================================= |
---|
| 149 | // Channel Worker Thread |
---|
| 150 | //============================================================================================= |
---|
| 151 | void ?{}( ChannelWorker & this ) { |
---|
[8c58e73] | 152 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 }; |
---|
| 153 | options.clopts.thrd_cnt++; |
---|
[7f0ac12] | 154 | this.done = false; |
---|
| 155 | } |
---|
[561dd26] | 156 | |
---|
[7f0ac12] | 157 | void main( ChannelWorker & this ) { |
---|
| 158 | park(); |
---|
| 159 | /* paranoid */ assert( this.conn.pipe[0] != -1 ); |
---|
| 160 | /* paranoid */ assert( this.conn.pipe[1] != -1 ); |
---|
| 161 | for() { |
---|
| 162 | size_t len = options.socket.buflen; |
---|
| 163 | char buffer[len]; |
---|
| 164 | PendingRead p; |
---|
| 165 | p.in.buf = (void*)buffer; |
---|
| 166 | p.in.len = len; |
---|
| 167 | push(*this.queue, &p); |
---|
[561dd26] | 168 | |
---|
[7f0ac12] | 169 | if( options.log ) sout | "=== Waiting new connection ==="; |
---|
| 170 | handle_connection( this.conn, p.out.fd, buffer, len, &p.f ); |
---|
[561dd26] | 171 | |
---|
[7f0ac12] | 172 | if( options.log ) sout | "=== Connection closed ==="; |
---|
| 173 | if(this.done) break; |
---|
| 174 | } |
---|
| 175 | } |
---|
[0aec496] | 176 | |
---|
[7f0ac12] | 177 | extern "C" { |
---|
| 178 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
| 179 | } |
---|
[97748ee] | 180 | |
---|
[7f0ac12] | 181 | void ?{}( Acceptor & this ) { |
---|
[8c58e73] | 182 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 }; |
---|
| 183 | options.clopts.thrd_cnt++; |
---|
[7f0ac12] | 184 | this.done = false; |
---|
| 185 | } |
---|
[b57db73] | 186 | |
---|
[7f0ac12] | 187 | void main( Acceptor & this ) { |
---|
| 188 | park(); |
---|
| 189 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
| 190 | for() { |
---|
| 191 | int fd = accept4(this.sockfd, this.[addr, addrlen, flags]); |
---|
| 192 | if(fd < 0) { |
---|
| 193 | if( errno == EWOULDBLOCK) { |
---|
| 194 | yield(); |
---|
| 195 | continue; |
---|
[97748ee] | 196 | } |
---|
[7f0ac12] | 197 | if( errno == ECONNABORTED ) break; |
---|
| 198 | if( this.done && (errno == EINVAL || errno == EBADF) ) break; |
---|
| 199 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 200 | } |
---|
| 201 | if(this.done) return; |
---|
[97748ee] | 202 | |
---|
[7f0ac12] | 203 | if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; |
---|
[97748ee] | 204 | |
---|
[7f0ac12] | 205 | if(fd) { |
---|
| 206 | PendingRead * p = 0p; |
---|
| 207 | for() { |
---|
| 208 | if(this.done) return; |
---|
| 209 | p = pop(*this.queue); |
---|
| 210 | if(p) break; |
---|
| 211 | yield(); |
---|
| 212 | }; |
---|
| 213 | |
---|
| 214 | p->out.fd = fd; |
---|
| 215 | async_recv(p->f, p->out.fd, p->in.buf, p->in.len, 0, CFA_IO_LAZY); |
---|
[0aec496] | 216 | } |
---|
[ee59ede] | 217 | |
---|
[7f0ac12] | 218 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
[0aec496] | 219 | } |
---|
[7f0ac12] | 220 | } |
---|