source: benchmark/io/http/worker.cfa @ 561dd26

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 561dd26 was 561dd26, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Implemented 'plaintext' workload

  • Property mode set to 100644
File size: 2.4 KB
Line 
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//=============================================================================================
17void ?{}( Worker & this ) {
18        ((thread&)this){ "Server Worker Thread", *options.clopts.instance };
19        this.pipe[0] = -1;
20        this.pipe[1] = -1;
21}
22
23void 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                printf("=== Accepting connection ===\n");
31                int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p );
32                if(fd < 0) {
33                        if( errno == ECONNABORTED ) break;
34                        if( errno == EINVAL ) break;
35                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
36                }
37
38                printf("=== New connection %d, waiting for requests ===\n", fd);
39                REQUEST:
40                for() {
41                        bool closed;
42                        HttpCode code;
43                        const char * file;
44                        size_t name_size;
45
46                        // Read the http request
47                        size_t len = options.socket.buflen;
48                        char buffer[len];
49                        printf("=== Reading request ===\n");
50                        [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel);
51
52                        // if we are done, break out of the loop
53                        if( closed ) {
54                                printf("=== Connection closed ===\n");
55                                continue CONNECTION;
56                        }
57
58                        // If this wasn't a request retrun 400
59                        if( code != OK200 ) {
60                                printf("=== Invalid Request : %d ===\n", code_val(code));
61                                answer_error(fd, code);
62                                continue REQUEST;
63                        }
64
65                        if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
66                                printf("=== Request for /plaintext ===\n");
67
68                                char text[] = "Hello, World!\n";
69
70                                // Send the header
71                                answer_plain(fd, text, sizeof(text));
72                        }
73                        else {
74                                printf("=== Request for file %.*s ===\n", (int)name_size, file);
75
76                                // Get the fd from the file cache
77                                int ans_fd;
78                                size_t count;
79                                [ans_fd, count] = get_file( file, name_size );
80
81                                // If we can't find the file, return 404
82                                if( ans_fd < 0 ) {
83                                        printf("=== File Not Found ===\n");
84                                        answer_error(fd, E404);
85                                        continue REQUEST;
86                                }
87
88                                // Send the header
89                                answer_header(fd, count);
90
91                                // Send the desired file
92                                sendfile( this.pipe, fd, ans_fd, count);
93                        }
94
95                        printf("=== Answer sent ===\n");
96                }
97        }
98}
Note: See TracBrowser for help on using the repository browser.