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