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

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

httpforall now only loads file if explicit path is given.

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