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