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 | extern "C" { |
---|
15 | // extern ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); |
---|
16 | extern ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags); |
---|
17 | } |
---|
18 | |
---|
19 | ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count) { |
---|
20 | return splice(in_fd, offset, out_fd, 0p, count, 0); |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | //============================================================================================= |
---|
25 | // Worker Thread |
---|
26 | //============================================================================================= |
---|
27 | void ?{}( Worker & this ) { |
---|
28 | ((thread&)this){ "Server Worker Thread", *options.the_cluster }; |
---|
29 | int ret = pipe(this.pipe); |
---|
30 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
31 | } |
---|
32 | |
---|
33 | void main( Worker & this ) { |
---|
34 | CONNECTION: |
---|
35 | while( int fd = take(wait_connect); fd >= 0) { |
---|
36 | printf("New connection, waiting for requests\n"); |
---|
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 = 1024; |
---|
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\n"); |
---|
59 | answer_error(fd, code); |
---|
60 | continue REQUEST; |
---|
61 | } |
---|
62 | |
---|
63 | printf("Request for file %.*s\n", 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 | } |
---|
87 | |
---|
88 | //============================================================================================= |
---|
89 | // Acceptor Thread |
---|
90 | //============================================================================================= |
---|
91 | void ?{}( Acceptor & this, int sockfd, struct sockaddr * addr, socklen_t * addrlen, int flags ) { |
---|
92 | ((thread&)this){ "Acceptor Thread", *options.the_cluster }; |
---|
93 | this.sockfd = sockfd; |
---|
94 | this.addr = addr; |
---|
95 | this.addrlen = addrlen; |
---|
96 | this.flags = flags; |
---|
97 | } |
---|
98 | |
---|
99 | void main( Acceptor & this ) { |
---|
100 | for() { |
---|
101 | int ret = cfa_accept4( this.[sockfd, addr, addrlen, flags] ); |
---|
102 | if(ret < 0) { |
---|
103 | if( errno == ECONNABORTED ) break; |
---|
104 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
105 | } |
---|
106 | |
---|
107 | printf("New connection accepted\n"); |
---|
108 | put( wait_connect, ret ); |
---|
109 | } |
---|
110 | } |
---|