source: benchmark/io/http/worker.cfa @ b7664a0

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

httpforall now uses sout rather than printf

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[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
[8c43d05]8#include <fstream.hfa>
[0aec496]9#include <iofwd.hfa>
10
11#include "options.hfa"
12#include "protocol.hfa"
13#include "filecache.hfa"
14
15//=============================================================================================
16// Worker Thread
17//=============================================================================================
18void ?{}( Worker & this ) {
[2ecbd7b]19        ((thread&)this){ "Server Worker Thread", *options.clopts.instance };
[d9c2284]20        this.pipe[0] = -1;
21        this.pipe[1] = -1;
[481ee28]22        this.done = false;
23}
24
25extern "C" {
26extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
[0aec496]27}
28
29void main( Worker & this ) {
[e235429]30        park();
[d9c2284]31        /* paranoid */ assert( this.pipe[0] != -1 );
32        /* paranoid */ assert( this.pipe[1] != -1 );
33
[0aec496]34        CONNECTION:
[e95a117]35        for() {
[8c43d05]36                if( options.log ) sout | "=== Accepting connection ===";
[ece0e80]37                int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p );
[481ee28]38                // int fd = accept4( this.[sockfd, addr, addrlen, flags] );
[8e3034d]39                if(fd < 0) {
40                        if( errno == ECONNABORTED ) break;
[ee59ede]41                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
[8e3034d]42                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
43                }
[e95a117]44
[8c43d05]45                if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
[0aec496]46                REQUEST:
47                for() {
48                        bool closed;
49                        HttpCode code;
50                        const char * file;
51                        size_t name_size;
52
53                        // Read the http request
[03ed863]54                        size_t len = options.socket.buflen;
[0aec496]55                        char buffer[len];
[8c43d05]56                        if( options.log ) sout | "=== Reading request ===";
[ece0e80]57                        [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel);
[0aec496]58
59                        // if we are done, break out of the loop
[ee59ede]60                        if( closed ) break REQUEST;
[0aec496]61
62                        // If this wasn't a request retrun 400
63                        if( code != OK200 ) {
[8c43d05]64                                sout | "=== Invalid Request :" | code_val(code) | "===";
[0aec496]65                                answer_error(fd, code);
66                                continue REQUEST;
67                        }
68
[561dd26]69                        if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
[8c43d05]70                                if( options.log ) sout | "=== Request for /plaintext ===";
[0aec496]71
[561dd26]72                                char text[] = "Hello, World!\n";
[0aec496]73
[561dd26]74                                // Send the header
[ee59ede]75                                int ret = answer_plain(fd, text, sizeof(text));
76                                if( ret == -ECONNRESET ) break REQUEST;
[561dd26]77
[8c43d05]78                                if( options.log ) sout | "=== Answer sent ===";
[97748ee]79                                continue REQUEST;
80                        }
[561dd26]81
[97748ee]82                        if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) {
[8c43d05]83                                if( options.log ) sout | "=== Request for /ping ===";
[561dd26]84
85                                // Send the header
[ee59ede]86                                int ret = answer_empty(fd);
87                                if( ret == -ECONNRESET ) break REQUEST;
[561dd26]88
[8c43d05]89                                if( options.log ) sout | "=== Answer sent ===";
[97748ee]90                                continue REQUEST;
[0aec496]91                        }
92
[8c43d05]93                        if( options.log ) {
94                                sout | "=== Request for file " | nonl;
95                                write(sout, file, name_size);
96                                sout | " ===";
97                        }
[97748ee]98
99                        // Get the fd from the file cache
100                        int ans_fd;
101                        size_t count;
102                        [ans_fd, count] = get_file( file, name_size );
103
104                        // If we can't find the file, return 404
105                        if( ans_fd < 0 ) {
[8c43d05]106                                sout | "=== File Not Found (" | nonl;
107                                write(sout, file, name_size);
108                                sout | ") ===";
[97748ee]109                                answer_error(fd, E404);
110                                continue REQUEST;
111                        }
112
113                        // Send the header
[ee59ede]114                        int ret = answer_header(fd, count);
115                        if( ret == -ECONNRESET ) break REQUEST;
[97748ee]116
117                        // Send the desired file
[ee59ede]118                        ret = sendfile( this.pipe, fd, ans_fd, count);
119                        if( ret == -ECONNRESET ) break REQUEST;
[97748ee]120
[8c43d05]121                        if( options.log ) sout | "=== Answer sent ===";
[0aec496]122                }
[ee59ede]123
[8c43d05]124                if( options.log ) sout | "=== Connection closed ===";
[ee59ede]125                close(fd);
126                continue CONNECTION;
[0aec496]127        }
128}
Note: See TracBrowser for help on using the repository browser.