[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;
|
---|
[0aec496] | 21 | }
|
---|
| 22 |
|
---|
| 23 | void main( Worker & this ) {
|
---|
[e235429] | 24 | park();
|
---|
[d9c2284] | 25 | /* paranoid */ assert( this.pipe[0] != -1 );
|
---|
| 26 | /* paranoid */ assert( this.pipe[1] != -1 );
|
---|
| 27 |
|
---|
[0aec496] | 28 | CONNECTION:
|
---|
[e95a117] | 29 | for() {
|
---|
[8e3034d] | 30 | int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, 0p, 0p );
|
---|
| 31 | if(fd < 0) {
|
---|
| 32 | if( errno == ECONNABORTED ) break;
|
---|
| 33 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
|
---|
| 34 | }
|
---|
[e95a117] | 35 |
|
---|
| 36 | printf("New connection %d, waiting for requests\n", fd);
|
---|
[0aec496] | 37 | REQUEST:
|
---|
| 38 | for() {
|
---|
| 39 | bool closed;
|
---|
| 40 | HttpCode code;
|
---|
| 41 | const char * file;
|
---|
| 42 | size_t name_size;
|
---|
| 43 |
|
---|
| 44 | // Read the http request
|
---|
[03ed863] | 45 | size_t len = options.socket.buflen;
|
---|
[0aec496] | 46 | char buffer[len];
|
---|
| 47 | printf("Reading request\n");
|
---|
| 48 | [code, closed, file, name_size] = http_read(fd, buffer, len);
|
---|
| 49 |
|
---|
| 50 | // if we are done, break out of the loop
|
---|
| 51 | if( closed ) {
|
---|
| 52 | printf("Connection closed\n");
|
---|
| 53 | continue CONNECTION;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // If this wasn't a request retrun 400
|
---|
| 57 | if( code != OK200 ) {
|
---|
[2ecbd7b] | 58 | printf("Invalid Request : %d\n", code_val(code));
|
---|
[0aec496] | 59 | answer_error(fd, code);
|
---|
| 60 | continue REQUEST;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[2ecbd7b] | 63 | printf("Request for file %.*s\n", (int)name_size, file);
|
---|
[0aec496] | 64 |
|
---|
| 65 | // Get the fd from the file cache
|
---|
| 66 | int ans_fd;
|
---|
| 67 | size_t count;
|
---|
| 68 | [ans_fd, count] = get_file( file, name_size );
|
---|
| 69 |
|
---|
| 70 | // If we can't find the file, return 404
|
---|
| 71 | if( ans_fd < 0 ) {
|
---|
| 72 | printf("File Not Found\n");
|
---|
| 73 | answer_error(fd, E404);
|
---|
| 74 | continue REQUEST;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | // Send the header
|
---|
| 78 | answer_header(fd, count);
|
---|
| 79 |
|
---|
| 80 | // Send the desired file
|
---|
[c82af9f] | 81 | sendfile( this.pipe, fd, ans_fd, count);
|
---|
[0aec496] | 82 |
|
---|
| 83 | printf("File sent\n");
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|