| 1 | #include "worker.hfa"
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <errno.h>
 | 
|---|
| 4 | #include <stdio.h>
 | 
|---|
| 5 | #include <string.h>
 | 
|---|
| 6 | #include <unistd.h>
 | 
|---|
| 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 ) {
 | 
|---|
| 18 |         ((thread&)this){ "Server Worker Thread", *options.clopts.instance };
 | 
|---|
| 19 |         this.pipe[0] = -1;
 | 
|---|
| 20 |         this.pipe[1] = -1;
 | 
|---|
| 21 | }
 | 
|---|
| 22 | 
 | 
|---|
| 23 | void main( Worker & this ) {
 | 
|---|
| 24 |         park();
 | 
|---|
| 25 |         /* paranoid */ assert( this.pipe[0] != -1 );
 | 
|---|
| 26 |         /* paranoid */ assert( this.pipe[1] != -1 );
 | 
|---|
| 27 | 
 | 
|---|
| 28 |         CONNECTION:
 | 
|---|
| 29 |         for() {
 | 
|---|
| 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 |                 }
 | 
|---|
| 35 | 
 | 
|---|
| 36 |                 printf("New connection %d, waiting for requests\n", fd);
 | 
|---|
| 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
 | 
|---|
| 45 |                         size_t len = options.socket.buflen;
 | 
|---|
| 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 ) {
 | 
|---|
| 58 |                                 printf("Invalid Request : %d\n", code_val(code));
 | 
|---|
| 59 |                                 answer_error(fd, code);
 | 
|---|
| 60 |                                 continue REQUEST;
 | 
|---|
| 61 |                         }
 | 
|---|
| 62 | 
 | 
|---|
| 63 |                         printf("Request for file %.*s\n", (int)name_size, file);
 | 
|---|
| 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
 | 
|---|
| 81 |                         sendfile( this.pipe, fd, ans_fd, count);
 | 
|---|
| 82 | 
 | 
|---|
| 83 |                         printf("File sent\n");
 | 
|---|
| 84 |                 }
 | 
|---|
| 85 |         }
 | 
|---|
| 86 | }
 | 
|---|