[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 | |
---|
| 8 | #include <iofwd.hfa> |
---|
| 9 | |
---|
| 10 | #include "options.hfa" |
---|
| 11 | #include "protocol.hfa" |
---|
| 12 | #include "filecache.hfa" |
---|
| 13 | |
---|
| 14 | //============================================================================================= |
---|
| 15 | // Worker Thread |
---|
| 16 | //============================================================================================= |
---|
| 17 | void ?{}( Worker & this ) { |
---|
[2ecbd7b] | 18 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance }; |
---|
[d9c2284] | 19 | this.pipe[0] = -1; |
---|
| 20 | this.pipe[1] = -1; |
---|
[481ee28] | 21 | this.done = false; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | extern "C" { |
---|
| 25 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
[0aec496] | 26 | } |
---|
| 27 | |
---|
| 28 | void main( Worker & this ) { |
---|
[e235429] | 29 | park(); |
---|
[d9c2284] | 30 | /* paranoid */ assert( this.pipe[0] != -1 ); |
---|
| 31 | /* paranoid */ assert( this.pipe[1] != -1 ); |
---|
| 32 | |
---|
[0aec496] | 33 | CONNECTION: |
---|
[e95a117] | 34 | for() { |
---|
[481ee28] | 35 | if( options.log ) printf("=== Accepting connection ===\n"); |
---|
[ece0e80] | 36 | int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p ); |
---|
[481ee28] | 37 | // int fd = accept4( this.[sockfd, addr, addrlen, flags] ); |
---|
[8e3034d] | 38 | if(fd < 0) { |
---|
| 39 | if( errno == ECONNABORTED ) break; |
---|
[481ee28] | 40 | if( errno == EINVAL && this.done ) break; |
---|
[8e3034d] | 41 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 42 | } |
---|
[e95a117] | 43 | |
---|
[481ee28] | 44 | if( options.log ) printf("=== New connection %d, waiting for requests ===\n", fd); |
---|
[0aec496] | 45 | REQUEST: |
---|
| 46 | for() { |
---|
| 47 | bool closed; |
---|
| 48 | HttpCode code; |
---|
| 49 | const char * file; |
---|
| 50 | size_t name_size; |
---|
| 51 | |
---|
| 52 | // Read the http request |
---|
[03ed863] | 53 | size_t len = options.socket.buflen; |
---|
[0aec496] | 54 | char buffer[len]; |
---|
[481ee28] | 55 | if( options.log ) printf("=== Reading request ===\n"); |
---|
[ece0e80] | 56 | [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel); |
---|
[0aec496] | 57 | |
---|
| 58 | // if we are done, break out of the loop |
---|
| 59 | if( closed ) { |
---|
[481ee28] | 60 | if( options.log ) printf("=== Connection closed ===\n"); |
---|
[0aec496] | 61 | continue CONNECTION; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // If this wasn't a request retrun 400 |
---|
| 65 | if( code != OK200 ) { |
---|
[481ee28] | 66 | if( options.log ) printf("=== Invalid Request : %d ===\n", 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") ))) { |
---|
[481ee28] | 72 | if( options.log ) printf("=== Request for /plaintext ===\n"); |
---|
[0aec496] | 73 | |
---|
[561dd26] | 74 | char text[] = "Hello, World!\n"; |
---|
[0aec496] | 75 | |
---|
[561dd26] | 76 | // Send the header |
---|
| 77 | answer_plain(fd, text, sizeof(text)); |
---|
| 78 | } |
---|
| 79 | else { |
---|
[481ee28] | 80 | if( options.log ) printf("=== Request for file %.*s ===\n", (int)name_size, file); |
---|
[561dd26] | 81 | |
---|
| 82 | // Get the fd from the file cache |
---|
| 83 | int ans_fd; |
---|
| 84 | size_t count; |
---|
| 85 | [ans_fd, count] = get_file( file, name_size ); |
---|
| 86 | |
---|
| 87 | // If we can't find the file, return 404 |
---|
| 88 | if( ans_fd < 0 ) { |
---|
[481ee28] | 89 | if( options.log ) printf("=== File Not Found ===\n"); |
---|
[561dd26] | 90 | answer_error(fd, E404); |
---|
| 91 | continue REQUEST; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // Send the header |
---|
| 95 | answer_header(fd, count); |
---|
| 96 | |
---|
| 97 | // Send the desired file |
---|
| 98 | sendfile( this.pipe, fd, ans_fd, count); |
---|
[0aec496] | 99 | } |
---|
| 100 | |
---|
[481ee28] | 101 | if( options.log ) printf("=== Answer sent ===\n"); |
---|
[0aec496] | 102 | } |
---|
| 103 | } |
---|
| 104 | } |
---|