[0aec496] | 1 | #include "protocol.hfa" |
---|
| 2 | |
---|
[c82af9f] | 3 | #define _GNU_SOURCE |
---|
| 4 | extern "C" { |
---|
| 5 | #include <fcntl.h> |
---|
| 6 | } |
---|
[0aec496] | 7 | #include <iofwd.hfa> |
---|
| 8 | |
---|
| 9 | #include <assert.h> |
---|
| 10 | // #include <stdio.h> // Don't use stdio.h, too slow to compile |
---|
| 11 | extern "C" { |
---|
| 12 | int snprintf ( char * s, size_t n, const char * format, ... ); |
---|
[d11d6eb] | 13 | #include <linux/io_uring.h> |
---|
[0aec496] | 14 | } |
---|
| 15 | #include <string.h> |
---|
| 16 | #include <errno.h> |
---|
| 17 | |
---|
[d11d6eb] | 18 | #include "options.hfa" |
---|
[0aec496] | 19 | |
---|
[c3ee5f3] | 20 | const char * volatile date = 0p; |
---|
[390fb02] | 21 | |
---|
[0aec496] | 22 | const char * http_msgs[] = { |
---|
[390fb02] | 23 | "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n", |
---|
| 24 | "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", |
---|
| 25 | "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", |
---|
| 26 | "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", |
---|
| 27 | "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", |
---|
[0aec496] | 28 | }; |
---|
| 29 | |
---|
[2ecbd7b] | 30 | _Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0]))); |
---|
| 31 | |
---|
| 32 | const int http_codes[] = { |
---|
| 33 | 200, |
---|
| 34 | 400, |
---|
| 35 | 404, |
---|
| 36 | 413, |
---|
| 37 | 414, |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | _Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0]))); |
---|
| 41 | |
---|
| 42 | int code_val(HttpCode code) { |
---|
| 43 | return http_codes[code]; |
---|
| 44 | } |
---|
[0aec496] | 45 | |
---|
| 46 | static inline int answer( int fd, const char * it, int len) { |
---|
| 47 | while(len > 0) { |
---|
| 48 | // Call write |
---|
[ba77750] | 49 | int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p); |
---|
[7270432] | 50 | // int ret = write(fd, it, len); |
---|
[0aec496] | 51 | if( ret < 0 ) { if( errno != EAGAIN && errno != EWOULDBLOCK) abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
| 52 | |
---|
| 53 | // update it/len |
---|
| 54 | it += ret; |
---|
| 55 | len -= ret; |
---|
| 56 | } |
---|
| 57 | return 0; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | int answer_error( int fd, HttpCode code ) { |
---|
| 61 | /* paranoid */ assert( code < KNOWN_CODES && code != OK200 ); |
---|
| 62 | int idx = (int)code; |
---|
| 63 | return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) ); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | int answer_header( int fd, size_t size ) { |
---|
| 67 | const char * fmt = http_msgs[OK200]; |
---|
[390fb02] | 68 | int len = 200; |
---|
[0aec496] | 69 | char buffer[len]; |
---|
[390fb02] | 70 | len = snprintf(buffer, len, fmt, date, size); |
---|
[0aec496] | 71 | return answer( fd, buffer, len ); |
---|
| 72 | } |
---|
| 73 | |
---|
[ba77750] | 74 | int answer_plain( int fd, char buffer[], size_t size ) { |
---|
| 75 | int ret = answer_header(fd, size); |
---|
| 76 | if( ret < 0 ) return ret; |
---|
| 77 | return answer(fd, buffer, size); |
---|
| 78 | } |
---|
| 79 | |
---|
[7270432] | 80 | int answer_empty( int fd ) { |
---|
| 81 | return answer_header(fd, 0); |
---|
| 82 | } |
---|
| 83 | |
---|
[ba77750] | 84 | |
---|
[ece0e80] | 85 | [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) { |
---|
[0aec496] | 86 | char * it = buffer; |
---|
| 87 | size_t count = len - 1; |
---|
| 88 | int rlen = 0; |
---|
| 89 | READ: |
---|
| 90 | for() { |
---|
[ece0e80] | 91 | int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p); |
---|
[c3ee5f3] | 92 | // int ret = read(fd, (void*)it, count); |
---|
[d11d6eb] | 93 | if(ret == 0 ) return [OK200, true, 0, 0]; |
---|
[0aec496] | 94 | if(ret < 0 ) { |
---|
| 95 | if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ; |
---|
[c3ee5f3] | 96 | // if( errno == EINVAL ) return [E400, true, 0, 0]; |
---|
[0aec496] | 97 | abort( "read error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 98 | } |
---|
| 99 | it[ret + 1] = '\0'; |
---|
| 100 | rlen += ret; |
---|
| 101 | |
---|
| 102 | if( strstr( it, "\r\n\r\n" ) ) break; |
---|
| 103 | |
---|
| 104 | it += ret; |
---|
| 105 | count -= ret; |
---|
| 106 | |
---|
[d11d6eb] | 107 | if( count < 1 ) return [E414, false, 0, 0]; |
---|
[0aec496] | 108 | } |
---|
| 109 | |
---|
[481ee28] | 110 | if( options.log ) printf("%.*s\n", rlen, buffer); |
---|
[0aec496] | 111 | |
---|
| 112 | it = buffer; |
---|
| 113 | int ret = memcmp(it, "GET /", 5); |
---|
[d11d6eb] | 114 | if( ret != 0 ) return [E400, false, 0, 0]; |
---|
[0aec496] | 115 | it += 5; |
---|
| 116 | |
---|
| 117 | char * end = strstr( it, " " ); |
---|
| 118 | return [OK200, false, it, end - it]; |
---|
[c82af9f] | 119 | } |
---|
| 120 | |
---|
| 121 | void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) { |
---|
[7270432] | 122 | unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE; |
---|
[c82af9f] | 123 | off_t offset = 0; |
---|
| 124 | ssize_t ret; |
---|
| 125 | SPLICE1: while(count > 0) { |
---|
[7270432] | 126 | ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0, -1`s, 0p, 0p); |
---|
| 127 | // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags); |
---|
[c82af9f] | 128 | if( ret < 0 ) { |
---|
| 129 | if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1; |
---|
| 130 | abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | count -= ret; |
---|
| 134 | offset += ret; |
---|
| 135 | size_t in_pipe = ret; |
---|
| 136 | SPLICE2: while(in_pipe > 0) { |
---|
[7270432] | 137 | ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0, -1`s, 0p, 0p); |
---|
| 138 | // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags); |
---|
[c82af9f] | 139 | if( ret < 0 ) { |
---|
| 140 | if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2; |
---|
| 141 | abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 142 | } |
---|
| 143 | in_pipe -= ret; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | } |
---|
[c3ee5f3] | 147 | } |
---|
| 148 | |
---|
| 149 | //============================================================================================= |
---|
| 150 | |
---|
| 151 | #include <clock.hfa> |
---|
| 152 | #include <time.hfa> |
---|
| 153 | #include <thread.hfa> |
---|
| 154 | |
---|
| 155 | struct date_buffer { |
---|
| 156 | char buff[100]; |
---|
| 157 | }; |
---|
| 158 | |
---|
| 159 | thread DateFormater { |
---|
| 160 | int idx; |
---|
| 161 | date_buffer buffers[2]; |
---|
| 162 | }; |
---|
| 163 | |
---|
| 164 | void ?{}( DateFormater & this ) { |
---|
[ece0e80] | 165 | ((thread&)this){ "Server Date Thread", *options.clopts.instance }; |
---|
[c3ee5f3] | 166 | this.idx = 0; |
---|
| 167 | memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) ); |
---|
| 168 | memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) ); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void main(DateFormater & this) { |
---|
| 172 | LOOP: for() { |
---|
| 173 | waitfor( ^?{} : this) { |
---|
| 174 | break LOOP; |
---|
| 175 | } |
---|
| 176 | or else {} |
---|
| 177 | |
---|
| 178 | Time now = getTimeNsec(); |
---|
[ece0e80] | 179 | |
---|
[c3ee5f3] | 180 | strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now ); |
---|
| 181 | |
---|
| 182 | char * next = this.buffers[this.idx].buff; |
---|
| 183 | __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST); |
---|
| 184 | this.idx = (this.idx + 1) % 2; |
---|
| 185 | |
---|
| 186 | sleep(1`s); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | //============================================================================================= |
---|
| 191 | DateFormater * the_date_formatter; |
---|
| 192 | |
---|
| 193 | void init_protocol(void) { |
---|
| 194 | the_date_formatter = alloc(); |
---|
| 195 | (*the_date_formatter){}; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void deinit_protocol(void) { |
---|
| 199 | ^(*the_date_formatter){}; |
---|
| 200 | free( the_date_formatter ); |
---|
[0aec496] | 201 | } |
---|