source: benchmark/io/http/protocol.cfa @ 1dbc3e10

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

Added missing EPIPE handling

  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include "protocol.hfa"
2
3#define _GNU_SOURCE
4extern "C" {
5        #include <fcntl.h>
6}
7
8#include <fstream.hfa>
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, ... );
15        // #include <linux/io_uring.h>
16}
17#include <string.h>
18#include <errno.h>
19
20#include "options.hfa"
21
22const char * volatile date = 0p;
23
24const char * http_msgs[] = {
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",
28        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
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",
31};
32
33_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
34
35const int http_codes[] = {
36        200,
37        400,
38        404,
39        408,
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}
49
50static inline int answer( int fd, const char * it, int len) {
51        while(len > 0) {
52                // Call write
53                int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
54                // int ret = write(fd, it, len);
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                }
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];
77        int len = 200;
78        char buffer[len];
79        len = snprintf(buffer, len, fmt, date, size);
80        return answer( fd, buffer, len );
81}
82
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
89int answer_empty( int fd ) {
90        return answer_header(fd, 0);
91}
92
93
94[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
95        char * it = buffer;
96        size_t count = len - 1;
97        int rlen = 0;
98        READ:
99        for() {
100                int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
101                // int ret = read(fd, (void*)it, count);
102                if(ret == 0 ) return [OK200, true, 0, 0];
103                if(ret < 0 ) {
104                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
105                        if( errno == ECONNRESET ) return [E408, true, 0, 0];
106                        if( errno == EPIPE ) return [E408, true, 0, 0];
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
117                if( count < 1 ) return [E414, false, 0, 0];
118        }
119
120        if( options.log ) {
121                write(sout, buffer, rlen);
122                sout | nl;
123        }
124
125        it = buffer;
126        int ret = memcmp(it, "GET /", 5);
127        if( ret != 0 ) return [E400, false, 0, 0];
128        it += 5;
129
130        char * end = strstr( it, " " );
131        return [OK200, false, it, end - it];
132}
133
134int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
135        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
136        off_t offset = 0;
137        ssize_t ret;
138        SPLICE1: while(count > 0) {
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);
141                if( ret < 0 ) {
142                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
143                        if( errno == ECONNRESET ) return -ECONNRESET;
144                        if( errno == EPIPE ) return -EPIPE;
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) {
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);
154                        if( ret < 0 ) {
155                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
156                                if( errno == ECONNRESET ) return -ECONNRESET;
157                                if( errno == EPIPE ) return -EPIPE;
158                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
159                        }
160                        in_pipe -= ret;
161                }
162
163        }
164        return count;
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 ) {
183        ((thread&)this){ "Server Date Thread", *options.clopts.instance };
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();
197
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 );
219}
Note: See TracBrowser for help on using the repository browser.