source: benchmark/io/http/protocol.cfa @ ee59ede

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

Improved error handling in server

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