| 1 | #include "protocol.hfa"
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #define _GNU_SOURCE
 | 
|---|
| 4 | extern "C" {
 | 
|---|
| 5 |         #include <fcntl.h>
 | 
|---|
| 6 | }
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #define xstr(s) str(s)
 | 
|---|
| 9 | #define str(s) #s
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <fstream.hfa>
 | 
|---|
| 12 | #include <iofwd.hfa>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include <assert.h>
 | 
|---|
| 15 | // #include <stdio.h> // Don't use stdio.h, too slow to compile
 | 
|---|
| 16 | extern "C" {
 | 
|---|
| 17 |       int snprintf ( char * s, size_t n, const char * format, ... );
 | 
|---|
| 18 |         // #include <linux/io_uring.h>
 | 
|---|
| 19 | }
 | 
|---|
| 20 | #include <string.h>
 | 
|---|
| 21 | #include <errno.h>
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "options.hfa"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #define PLAINTEXT_1WRITE
 | 
|---|
| 26 | #define PLAINTEXT_MEMCPY
 | 
|---|
| 27 | #define PLAINTEXT_NOCOPY
 | 
|---|
| 28 | 
 | 
|---|
| 29 | struct https_msg_str {
 | 
|---|
| 30 |         char msg[512];
 | 
|---|
| 31 |         size_t len;
 | 
|---|
| 32 | };
 | 
|---|
| 33 | 
 | 
|---|
| 34 | const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
 | 
|---|
| 35 | 
 | 
|---|
| 36 | _Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
 | 
|---|
| 37 | 
 | 
|---|
| 38 | const int http_codes[KNOWN_CODES] = {
 | 
|---|
| 39 |         200,
 | 
|---|
| 40 |         200,
 | 
|---|
| 41 |         400,
 | 
|---|
| 42 |         404,
 | 
|---|
| 43 |         405,
 | 
|---|
| 44 |         408,
 | 
|---|
| 45 |         413,
 | 
|---|
| 46 |         414,
 | 
|---|
| 47 | };
 | 
|---|
| 48 | 
 | 
|---|
| 49 | _Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
 | 
|---|
| 50 | 
 | 
|---|
| 51 | int code_val(HttpCode code) {
 | 
|---|
| 52 |         return http_codes[code];
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | static inline int answer( int fd, const char * it, int len) {
 | 
|---|
| 56 |         while(len > 0) {
 | 
|---|
| 57 |                 // Call write
 | 
|---|
| 58 |                 int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
 | 
|---|
| 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 |                 }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |                 // update it/len
 | 
|---|
| 67 |                 it  += ret;
 | 
|---|
| 68 |                 len -= ret;
 | 
|---|
| 69 |         }
 | 
|---|
| 70 |         return 0;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | int answer_error( int fd, HttpCode code ) {
 | 
|---|
| 74 |         /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
 | 
|---|
| 75 |         int idx = (int)code;
 | 
|---|
| 76 |         return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | int answer_header( int fd, size_t size ) {
 | 
|---|
| 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);
 | 
|---|
| 86 |         return answer( fd, buffer, len );
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | #if defined(PLAINTEXT_NOCOPY)
 | 
|---|
| 90 | int answer_plaintext( int fd ) {
 | 
|---|
| 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
 | 
|---|
| 95 | int 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);
 | 
|---|
| 109 | }
 | 
|---|
| 110 | #elif defined(PLAINTEXT_1WRITE)
 | 
|---|
| 111 | int answer_plaintext( int fd ) {
 | 
|---|
| 112 |         char text[] = "Hello, World!\n\n";
 | 
|---|
| 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
 | 
|---|
| 125 | int answer_plaintext( int fd ) {
 | 
|---|
| 126 |         char text[] = "Hello, World!\n\n";
 | 
|---|
| 127 |         int ret = answer_header(fd, sizeof(text));
 | 
|---|
| 128 |         if( ret < 0 ) return ret;
 | 
|---|
| 129 |         return answer(fd, text, sizeof(text));
 | 
|---|
| 130 | }
 | 
|---|
| 131 | #endif
 | 
|---|
| 132 | 
 | 
|---|
| 133 | int answer_empty( int fd ) {
 | 
|---|
| 134 |         return answer_header(fd, 0);
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | 
 | 
|---|
| 138 | [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
 | 
|---|
| 139 |         char * it = buffer;
 | 
|---|
| 140 |         size_t count = len - 1;
 | 
|---|
| 141 |         int rlen = 0;
 | 
|---|
| 142 |         READ:
 | 
|---|
| 143 |         for() {
 | 
|---|
| 144 |                 int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
 | 
|---|
| 145 |                 // int ret = read(fd, (void*)it, count);
 | 
|---|
| 146 |                 if(ret == 0 ) return [OK200, true, 0, 0];
 | 
|---|
| 147 |                 if(ret < 0 ) {
 | 
|---|
| 148 |                         if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
 | 
|---|
| 149 |                         if( errno == ECONNRESET ) return [E408, true, 0, 0];
 | 
|---|
| 150 |                         if( errno == EPIPE ) return [E408, true, 0, 0];
 | 
|---|
| 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 | 
 | 
|---|
| 161 |                 if( count < 1 ) return [E414, false, 0, 0];
 | 
|---|
| 162 |         }
 | 
|---|
| 163 | 
 | 
|---|
| 164 |         if( options.log ) {
 | 
|---|
| 165 |                 write(sout, buffer, rlen);
 | 
|---|
| 166 |                 sout | nl;
 | 
|---|
| 167 |         }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |         it = buffer;
 | 
|---|
| 170 |         int ret = memcmp(it, "GET /", 5);
 | 
|---|
| 171 |         if( ret != 0 ) return [E400, false, 0, 0];
 | 
|---|
| 172 |         it += 5;
 | 
|---|
| 173 | 
 | 
|---|
| 174 |         char * end = strstr( it, " " );
 | 
|---|
| 175 |         return [OK200, false, it, end - it];
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
 | 
|---|
| 179 |         unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
 | 
|---|
| 180 |         off_t offset = 0;
 | 
|---|
| 181 |         ssize_t ret;
 | 
|---|
| 182 |         SPLICE1: while(count > 0) {
 | 
|---|
| 183 |                 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
 | 
|---|
| 184 |                 if( ret < 0 ) {
 | 
|---|
| 185 |                         if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
 | 
|---|
| 186 |                         if( errno == ECONNRESET ) return -ECONNRESET;
 | 
|---|
| 187 |                         if( errno == EPIPE ) return -EPIPE;
 | 
|---|
| 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) {
 | 
|---|
| 195 |                         ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
 | 
|---|
| 196 |                         if( ret < 0 ) {
 | 
|---|
| 197 |                                 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
 | 
|---|
| 198 |                                 if( errno == ECONNRESET ) return -ECONNRESET;
 | 
|---|
| 199 |                                 if( errno == EPIPE ) return -EPIPE;
 | 
|---|
| 200 |                                 abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
 | 
|---|
| 201 |                         }
 | 
|---|
| 202 |                         in_pipe -= ret;
 | 
|---|
| 203 |                 }
 | 
|---|
| 204 | 
 | 
|---|
| 205 |         }
 | 
|---|
| 206 |         return count;
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|
| 209 | //=============================================================================================
 | 
|---|
| 210 | 
 | 
|---|
| 211 | #include <clock.hfa>
 | 
|---|
| 212 | #include <time.hfa>
 | 
|---|
| 213 | #include <thread.hfa>
 | 
|---|
| 214 | 
 | 
|---|
| 215 | const char * original_http_msgs[] = {
 | 
|---|
| 216 |         "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
 | 
|---|
| 217 |         "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n\n",
 | 
|---|
| 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 | 
 | 
|---|
| 226 | struct date_buffer {
 | 
|---|
| 227 |         https_msg_str strs[KNOWN_CODES];
 | 
|---|
| 228 | };
 | 
|---|
| 229 | 
 | 
|---|
| 230 | thread DateFormater {
 | 
|---|
| 231 |         int idx;
 | 
|---|
| 232 |         date_buffer buffers[2];
 | 
|---|
| 233 | };
 | 
|---|
| 234 | 
 | 
|---|
| 235 | void ?{}( DateFormater & this ) {
 | 
|---|
| 236 |         ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
 | 
|---|
| 237 |         this.idx = 0;
 | 
|---|
| 238 |         memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
 | 
|---|
| 239 |         memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
 | 
|---|
| 240 | }
 | 
|---|
| 241 | 
 | 
|---|
| 242 | void main(DateFormater & this) {
 | 
|---|
| 243 |         LOOP: for() {
 | 
|---|
| 244 |                 waitfor( ^?{} : this) {
 | 
|---|
| 245 |                         break LOOP;
 | 
|---|
| 246 |                 }
 | 
|---|
| 247 |                 or else {}
 | 
|---|
| 248 | 
 | 
|---|
| 249 | 
 | 
|---|
| 250 |                 char buff[100];
 | 
|---|
| 251 |                 Time now = timeHiRes();
 | 
|---|
| 252 |                 strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
 | 
|---|
| 253 |                 sout | "Updated date to '" | buff | "'";
 | 
|---|
| 254 | 
 | 
|---|
| 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 |                 }
 | 
|---|
| 259 | 
 | 
|---|
| 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 |                 }
 | 
|---|
| 264 |                 this.idx = (this.idx + 1) % 2;
 | 
|---|
| 265 | 
 | 
|---|
| 266 |                 sout | "Date thread sleeping";
 | 
|---|
| 267 | 
 | 
|---|
| 268 |                 sleep(1`s);
 | 
|---|
| 269 |         }
 | 
|---|
| 270 | }
 | 
|---|
| 271 | 
 | 
|---|
| 272 | //=============================================================================================
 | 
|---|
| 273 | DateFormater * the_date_formatter;
 | 
|---|
| 274 | 
 | 
|---|
| 275 | void init_protocol(void) {
 | 
|---|
| 276 |         the_date_formatter = alloc();
 | 
|---|
| 277 |         (*the_date_formatter){};
 | 
|---|
| 278 | }
 | 
|---|
| 279 | 
 | 
|---|
| 280 | void deinit_protocol(void) {
 | 
|---|
| 281 |         ^(*the_date_formatter){};
 | 
|---|
| 282 |         free( the_date_formatter );
 | 
|---|
| 283 | }
 | 
|---|