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