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