source: benchmark/io/http/protocol.cfa @ 35ea4f3

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

Added missing EPIPE handling

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[0aec496]1#include "protocol.hfa"
2
[c82af9f]3#define _GNU_SOURCE
4extern "C" {
5        #include <fcntl.h>
6}
[8c43d05]7
8#include <fstream.hfa>
[0aec496]9#include <iofwd.hfa>
10
11#include <assert.h>
12// #include <stdio.h> // Don't use stdio.h, too slow to compile
13extern "C" {
14      int snprintf ( char * s, size_t n, const char * format, ... );
[8c43d05]15        // #include <linux/io_uring.h>
[0aec496]16}
17#include <string.h>
18#include <errno.h>
19
[d11d6eb]20#include "options.hfa"
[0aec496]21
[c3ee5f3]22const char * volatile date = 0p;
[390fb02]23
[0aec496]24const char * http_msgs[] = {
[390fb02]25        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n",
26        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
27        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
[ee59ede]28        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
[390fb02]29        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
30        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
[0aec496]31};
32
[2ecbd7b]33_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
34
35const int http_codes[] = {
36        200,
37        400,
38        404,
[ee59ede]39        408,
[2ecbd7b]40        413,
41        414,
42};
43
44_Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
45
46int code_val(HttpCode code) {
47        return http_codes[code];
48}
[0aec496]49
50static inline int answer( int fd, const char * it, int len) {
51        while(len > 0) {
52                // Call write
[ba77750]53                int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
[7270432]54                // int ret = write(fd, it, len);
[ee59ede]55                if( ret < 0 ) {
56                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
57                        if( errno == EAGAIN || errno == EWOULDBLOCK) return -EAGAIN;
58
59                        abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) );
60                }
[0aec496]61
62                // update it/len
63                it  += ret;
64                len -= ret;
65        }
66        return 0;
67}
68
69int answer_error( int fd, HttpCode code ) {
70        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
71        int idx = (int)code;
72        return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
73}
74
75int answer_header( int fd, size_t size ) {
76        const char * fmt = http_msgs[OK200];
[390fb02]77        int len = 200;
[0aec496]78        char buffer[len];
[390fb02]79        len = snprintf(buffer, len, fmt, date, size);
[0aec496]80        return answer( fd, buffer, len );
81}
82
[ba77750]83int answer_plain( int fd, char buffer[], size_t size ) {
84        int ret = answer_header(fd, size);
85        if( ret < 0 ) return ret;
86        return answer(fd, buffer, size);
87}
88
[7270432]89int answer_empty( int fd ) {
90        return answer_header(fd, 0);
91}
92
[ba77750]93
[ece0e80]94[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
[0aec496]95        char * it = buffer;
96        size_t count = len - 1;
97        int rlen = 0;
98        READ:
99        for() {
[ece0e80]100                int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
[c3ee5f3]101                // int ret = read(fd, (void*)it, count);
[d11d6eb]102                if(ret == 0 ) return [OK200, true, 0, 0];
[0aec496]103                if(ret < 0 ) {
104                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
[ee59ede]105                        if( errno == ECONNRESET ) return [E408, true, 0, 0];
[1dbc3e10]106                        if( errno == EPIPE ) return [E408, true, 0, 0];
[0aec496]107                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
108                }
109                it[ret + 1] = '\0';
110                rlen += ret;
111
112                if( strstr( it, "\r\n\r\n" ) ) break;
113
114                it += ret;
115                count -= ret;
116
[d11d6eb]117                if( count < 1 ) return [E414, false, 0, 0];
[0aec496]118        }
119
[8c43d05]120        if( options.log ) {
121                write(sout, buffer, rlen);
122                sout | nl;
123        }
[0aec496]124
125        it = buffer;
126        int ret = memcmp(it, "GET /", 5);
[d11d6eb]127        if( ret != 0 ) return [E400, false, 0, 0];
[0aec496]128        it += 5;
129
130        char * end = strstr( it, " " );
131        return [OK200, false, it, end - it];
[c82af9f]132}
133
[ee59ede]134int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
[7270432]135        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
[c82af9f]136        off_t offset = 0;
137        ssize_t ret;
138        SPLICE1: while(count > 0) {
[7270432]139                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0, -1`s, 0p, 0p);
140                // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags);
[c82af9f]141                if( ret < 0 ) {
142                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
[ee59ede]143                        if( errno == ECONNRESET ) return -ECONNRESET;
144                        if( errno == EPIPE ) return -EPIPE;
[c82af9f]145                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
146                }
147
148                count -= ret;
149                offset += ret;
150                size_t in_pipe = ret;
151                SPLICE2: while(in_pipe > 0) {
[7270432]152                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0, -1`s, 0p, 0p);
153                        // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags);
[c82af9f]154                        if( ret < 0 ) {
155                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
[ee59ede]156                                if( errno == ECONNRESET ) return -ECONNRESET;
157                                if( errno == EPIPE ) return -EPIPE;
[c82af9f]158                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
159                        }
160                        in_pipe -= ret;
161                }
162
163        }
[ee59ede]164        return count;
[c3ee5f3]165}
166
167//=============================================================================================
168
169#include <clock.hfa>
170#include <time.hfa>
171#include <thread.hfa>
172
173struct date_buffer {
174        char buff[100];
175};
176
177thread DateFormater {
178        int idx;
179        date_buffer buffers[2];
180};
181
182void ?{}( DateFormater & this ) {
[ece0e80]183        ((thread&)this){ "Server Date Thread", *options.clopts.instance };
[c3ee5f3]184        this.idx = 0;
185        memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
186        memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
187}
188
189void main(DateFormater & this) {
190        LOOP: for() {
191                waitfor( ^?{} : this) {
192                        break LOOP;
193                }
194                or else {}
195
196                Time now = getTimeNsec();
[ece0e80]197
[c3ee5f3]198                strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
199
200                char * next = this.buffers[this.idx].buff;
201                __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST);
202                this.idx = (this.idx + 1) % 2;
203
204                sleep(1`s);
205        }
206}
207
208//=============================================================================================
209DateFormater * the_date_formatter;
210
211void init_protocol(void) {
212        the_date_formatter = alloc();
213        (*the_date_formatter){};
214}
215
216void deinit_protocol(void) {
217        ^(*the_date_formatter){};
218        free( the_date_formatter );
[0aec496]219}
Note: See TracBrowser for help on using the repository browser.