source: benchmark/io/http/protocol.cfa @ 2caed18

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 2caed18 was 2caed18, checked in by Thierry Delisle <tdelisle@…>, 8 months ago

Changed how messages is handled to avoid long printf.

  • Property mode set to 100644
File size: 6.1 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
22struct https_msg_str {
23        char msg[512];
24        size_t len;
25};
26
27const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
28
29_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
30
31const int http_codes[KNOWN_CODES] = {
32        200,
33        400,
34        404,
35        405,
36        408,
37        413,
38        414,
39};
40
41_Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
42
43int code_val(HttpCode code) {
44        return http_codes[code];
45}
46
47static inline int answer( int fd, const char * it, int len) {
48        while(len > 0) {
49                // Call write
50                int ret = cfa_send(fd, it, len, 0, 0);
51                if( ret < 0 ) {
52                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
53                        if( errno == EAGAIN || errno == EWOULDBLOCK) return -EAGAIN;
54
55                        abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) );
56                }
57
58                // update it/len
59                it  += ret;
60                len -= ret;
61        }
62        return 0;
63}
64
65int answer_error( int fd, HttpCode code ) {
66        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
67        int idx = (int)code;
68        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
69}
70
71int answer_header( int fd, size_t size ) {
72        char buffer[512];
73        char * it = buffer;
74        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
75        it += http_msgs[OK200]->len;
76        int len = http_msgs[OK200]->len;
77        len += snprintf(it, 512 - len, "%d \n\n", 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) {
93        char * it = buffer;
94        size_t count = len - 1;
95        int rlen = 0;
96        READ:
97        for() {
98                int ret = cfa_recv(fd, (void*)it, count, 0, 0);
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                        if( errno == EPIPE ) return [E408, true, 0, 0];
105                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
106                }
107                it[ret + 1] = '\0';
108                rlen += ret;
109
110                if( strstr( it, "\r\n\r\n" ) ) break;
111
112                it += ret;
113                count -= ret;
114
115                if( count < 1 ) return [E414, false, 0, 0];
116        }
117
118        if( options.log ) {
119                write(sout, buffer, rlen);
120                sout | nl;
121        }
122
123        it = buffer;
124        int ret = memcmp(it, "GET /", 5);
125        if( ret != 0 ) return [E400, false, 0, 0];
126        it += 5;
127
128        char * end = strstr( it, " " );
129        return [OK200, false, it, end - it];
130}
131
132int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
133        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
134        off_t offset = 0;
135        ssize_t ret;
136        SPLICE1: while(count > 0) {
137                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0);
138                // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags);
139                if( ret < 0 ) {
140                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
141                        if( errno == ECONNRESET ) return -ECONNRESET;
142                        if( errno == EPIPE ) return -EPIPE;
143                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
144                }
145
146                count -= ret;
147                offset += ret;
148                size_t in_pipe = ret;
149                SPLICE2: while(in_pipe > 0) {
150                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0);
151                        // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags);
152                        if( ret < 0 ) {
153                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
154                                if( errno == ECONNRESET ) return -ECONNRESET;
155                                if( errno == EPIPE ) return -EPIPE;
156                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
157                        }
158                        in_pipe -= ret;
159                }
160
161        }
162        return count;
163}
164
165//=============================================================================================
166
167#include <clock.hfa>
168#include <time.hfa>
169#include <thread.hfa>
170
171const char * original_http_msgs[] = {
172        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
173        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
174        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
175        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
176        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
177        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
178        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
179};
180
181struct date_buffer {
182        https_msg_str strs[KNOWN_CODES];
183};
184
185thread DateFormater {
186        int idx;
187        date_buffer buffers[2];
188};
189
190void ?{}( DateFormater & this ) {
191        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
192        this.idx = 0;
193        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
194        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
195}
196
197void main(DateFormater & this) {
198        LOOP: for() {
199                waitfor( ^?{} : this) {
200                        break LOOP;
201                }
202                or else {}
203
204
205                char buff[100];
206                Time now = getTimeNsec();
207                strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
208
209                for(i; KNOWN_CODES) {
210                        size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
211                        this.buffers[this.idx].strs[i].len = len;
212                }
213
214                for(i; KNOWN_CODES) {
215                        https_msg_str * next = &this.buffers[this.idx].strs[i];
216                        __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
217                }
218                this.idx = (this.idx + 1) % 2;
219
220                sleep(1`s);
221        }
222}
223
224//=============================================================================================
225DateFormater * the_date_formatter;
226
227void init_protocol(void) {
228        the_date_formatter = alloc();
229        (*the_date_formatter){};
230}
231
232void deinit_protocol(void) {
233        ^(*the_date_formatter){};
234        free( the_date_formatter );
235}
Note: See TracBrowser for help on using the repository browser.