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

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since a3769cc was 665edf40, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[0aec496]1#include "protocol.hfa"
2
[c82af9f]3#define _GNU_SOURCE
4extern "C" {
5        #include <fcntl.h>
6}
[8c43d05]7
[857a1c6]8#define xstr(s) str(s)
9#define str(s) #s
10
[8c43d05]11#include <fstream.hfa>
[0aec496]12#include <iofwd.hfa>
13
14#include <assert.h>
15// #include <stdio.h> // Don't use stdio.h, too slow to compile
16extern "C" {
17      int snprintf ( char * s, size_t n, const char * format, ... );
[8c43d05]18        // #include <linux/io_uring.h>
[0aec496]19}
20#include <string.h>
21#include <errno.h>
22
[d11d6eb]23#include "options.hfa"
[0aec496]24
[ed2cb3c]25#define PLAINTEXT_1WRITE
[857a1c6]26#define PLAINTEXT_MEMCPY
[ed2cb3c]27#define PLAINTEXT_NOCOPY
28
[2caed18]29struct https_msg_str {
30        char msg[512];
31        size_t len;
[0aec496]32};
33
[2caed18]34const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
35
[2ecbd7b]36_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
37
[2caed18]38const int http_codes[KNOWN_CODES] = {
[ed2cb3c]39        200,
[2ecbd7b]40        200,
41        400,
42        404,
[b57db73]43        405,
[ee59ede]44        408,
[2ecbd7b]45        413,
46        414,
47};
48
49_Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
50
51int code_val(HttpCode code) {
52        return http_codes[code];
53}
[0aec496]54
55static inline int answer( int fd, const char * it, int len) {
56        while(len > 0) {
57                // Call write
[2cd784a]58                int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
[ee59ede]59                if( ret < 0 ) {
60                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
61                        if( errno == EAGAIN || errno == EWOULDBLOCK) return -EAGAIN;
62
63                        abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) );
64                }
[0aec496]65
66                // update it/len
67                it  += ret;
68                len -= ret;
69        }
70        return 0;
71}
72
73int answer_error( int fd, HttpCode code ) {
74        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
75        int idx = (int)code;
[2caed18]76        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
[0aec496]77}
78
79int answer_header( int fd, size_t size ) {
[2caed18]80        char buffer[512];
81        char * it = buffer;
82        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
83        it += http_msgs[OK200]->len;
84        int len = http_msgs[OK200]->len;
85        len += snprintf(it, 512 - len, "%d \n\n", size);
[0aec496]86        return answer( fd, buffer, len );
87}
88
[ed2cb3c]89#if defined(PLAINTEXT_NOCOPY)
90int answer_plaintext( int fd ) {
[857a1c6]91        return answer(fd, http_msgs[OK200_PlainText]->msg, http_msgs[OK200_PlainText]->len); // +1 cause snprintf doesn't count nullterminator
92}
93#elif defined(PLAINTEXT_MEMCPY)
94#define TEXTSIZE 15
95int answer_plaintext( int fd ) {
96        char text[] = "Hello, World!\n\n";
97        char ts[] = xstr(TEXTSIZE) " \n\n";
98        _Static_assert(sizeof(text) - 1 == TEXTSIZE);
99        char buffer[512 + TEXTSIZE];
100        char * it = buffer;
101        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
102        it += http_msgs[OK200]->len;
103        int len = http_msgs[OK200]->len;
104        memcpy(it, ts, sizeof(ts) - 1);
105        it += sizeof(ts) - 1;
106        len += sizeof(ts) - 1;
107        memcpy(it, text, TEXTSIZE);
108        return answer(fd, buffer, len + TEXTSIZE);
[ed2cb3c]109}
110#elif defined(PLAINTEXT_1WRITE)
[187fdb8]111int answer_plaintext( int fd ) {
[857a1c6]112        char text[] = "Hello, World!\n\n";
[187fdb8]113        char buffer[512 + sizeof(text)];
114        char * it = buffer;
115        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
116        it += http_msgs[OK200]->len;
117        int len = http_msgs[OK200]->len;
118        int r = snprintf(it, 512 - len, "%d \n\n", sizeof(text));
119        it += r;
120        len += r;
121        memcpy(it, text, sizeof(text));
122        return answer(fd, buffer, len + sizeof(text));
123}
124#else
125int answer_plaintext( int fd ) {
[857a1c6]126        char text[] = "Hello, World!\n\n";
[187fdb8]127        int ret = answer_header(fd, sizeof(text));
[ba77750]128        if( ret < 0 ) return ret;
[187fdb8]129        return answer(fd, text, sizeof(text));
[ba77750]130}
[187fdb8]131#endif
[ba77750]132
[7270432]133int answer_empty( int fd ) {
134        return answer_header(fd, 0);
135}
136
[ba77750]137
[4f762d3]138[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
[0aec496]139        char * it = buffer;
140        size_t count = len - 1;
141        int rlen = 0;
142        READ:
143        for() {
[2cd784a]144                int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
[c3ee5f3]145                // int ret = read(fd, (void*)it, count);
[d11d6eb]146                if(ret == 0 ) return [OK200, true, 0, 0];
[0aec496]147                if(ret < 0 ) {
148                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
[ee59ede]149                        if( errno == ECONNRESET ) return [E408, true, 0, 0];
[1dbc3e10]150                        if( errno == EPIPE ) return [E408, true, 0, 0];
[0aec496]151                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
152                }
153                it[ret + 1] = '\0';
154                rlen += ret;
155
156                if( strstr( it, "\r\n\r\n" ) ) break;
157
158                it += ret;
159                count -= ret;
160
[d11d6eb]161                if( count < 1 ) return [E414, false, 0, 0];
[0aec496]162        }
163
[8c43d05]164        if( options.log ) {
165                write(sout, buffer, rlen);
166                sout | nl;
167        }
[0aec496]168
169        it = buffer;
170        int ret = memcmp(it, "GET /", 5);
[d11d6eb]171        if( ret != 0 ) return [E400, false, 0, 0];
[0aec496]172        it += 5;
173
174        char * end = strstr( it, " " );
175        return [OK200, false, it, end - it];
[c82af9f]176}
177
[ee59ede]178int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
[7270432]179        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
[c82af9f]180        off_t offset = 0;
181        ssize_t ret;
182        SPLICE1: while(count > 0) {
[2cd784a]183                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
[c82af9f]184                if( ret < 0 ) {
185                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
[ee59ede]186                        if( errno == ECONNRESET ) return -ECONNRESET;
187                        if( errno == EPIPE ) return -EPIPE;
[c82af9f]188                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
189                }
190
191                count -= ret;
192                offset += ret;
193                size_t in_pipe = ret;
194                SPLICE2: while(in_pipe > 0) {
[2cd784a]195                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
[c82af9f]196                        if( ret < 0 ) {
197                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
[ee59ede]198                                if( errno == ECONNRESET ) return -ECONNRESET;
199                                if( errno == EPIPE ) return -EPIPE;
[c82af9f]200                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
201                        }
202                        in_pipe -= ret;
203                }
204
205        }
[ee59ede]206        return count;
[c3ee5f3]207}
208
209//=============================================================================================
210
211#include <clock.hfa>
212#include <time.hfa>
213#include <thread.hfa>
214
[2caed18]215const char * original_http_msgs[] = {
216        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
[857a1c6]217        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n\n",
[2caed18]218        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
219        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
220        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
221        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
222        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
223        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
224};
225
[c3ee5f3]226struct date_buffer {
[2caed18]227        https_msg_str strs[KNOWN_CODES];
[c3ee5f3]228};
229
230thread DateFormater {
231        int idx;
232        date_buffer buffers[2];
233};
234
235void ?{}( DateFormater & this ) {
[348f81d5]236        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
[c3ee5f3]237        this.idx = 0;
[2caed18]238        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
239        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
[c3ee5f3]240}
241
242void main(DateFormater & this) {
243        LOOP: for() {
244                waitfor( ^?{} : this) {
245                        break LOOP;
246                }
247                or else {}
248
[2caed18]249
250                char buff[100];
[e54d0c3]251                Time now = timeHiRes();
[2caed18]252                strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
[2cd784a]253                sout | "Updated date to '" | buff | "'";
[ece0e80]254
[2caed18]255                for(i; KNOWN_CODES) {
256                        size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
257                        this.buffers[this.idx].strs[i].len = len;
258                }
[c3ee5f3]259
[2caed18]260                for(i; KNOWN_CODES) {
261                        https_msg_str * next = &this.buffers[this.idx].strs[i];
262                        __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
263                }
[c3ee5f3]264                this.idx = (this.idx + 1) % 2;
265
[2cd784a]266                sout | "Date thread sleeping";
267
[c3ee5f3]268                sleep(1`s);
269        }
270}
271
272//=============================================================================================
273DateFormater * the_date_formatter;
274
275void init_protocol(void) {
276        the_date_formatter = alloc();
277        (*the_date_formatter){};
278}
279
280void deinit_protocol(void) {
281        ^(*the_date_formatter){};
282        free( the_date_formatter );
[e54d0c3]283}
Note: See TracBrowser for help on using the repository browser.