| [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 |  | 
|---|
|  | 20 | const char * http_msgs[] = { | 
|---|
|  | 21 | "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: %zu\n\n", | 
|---|
|  | 22 | "HTTP/1.1 400 Bad Request\nContent-Type: text/plain\nContent-Length: 0\n\n", | 
|---|
|  | 23 | "HTTP/1.1 404 Not Found\nContent-Type: text/plain\nContent-Length: 0\n\n", | 
|---|
|  | 24 | "HTTP/1.1 413 Payload Too Large\nContent-Type: text/plain\nContent-Length: 0\n\n", | 
|---|
|  | 25 | "HTTP/1.1 414 URI Too Long\nContent-Type: text/plain\nContent-Length: 0\n\n", | 
|---|
|  | 26 | }; | 
|---|
|  | 27 |  | 
|---|
| [2ecbd7b] | 28 | _Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0]))); | 
|---|
|  | 29 |  | 
|---|
|  | 30 | const int http_codes[] = { | 
|---|
|  | 31 | 200, | 
|---|
|  | 32 | 400, | 
|---|
|  | 33 | 404, | 
|---|
|  | 34 | 413, | 
|---|
|  | 35 | 414, | 
|---|
|  | 36 | }; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | _Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0]))); | 
|---|
|  | 39 |  | 
|---|
|  | 40 | int code_val(HttpCode code) { | 
|---|
|  | 41 | return http_codes[code]; | 
|---|
|  | 42 | } | 
|---|
| [0aec496] | 43 |  | 
|---|
|  | 44 | static inline int answer( int fd, const char * it, int len) { | 
|---|
|  | 45 | while(len > 0) { | 
|---|
|  | 46 | // Call write | 
|---|
|  | 47 | int ret = write(fd, it, len); | 
|---|
|  | 48 | if( ret < 0 ) { if( errno != EAGAIN && errno != EWOULDBLOCK) abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) ); } | 
|---|
|  | 49 |  | 
|---|
|  | 50 | // update it/len | 
|---|
|  | 51 | it  += ret; | 
|---|
|  | 52 | len -= ret; | 
|---|
|  | 53 | } | 
|---|
|  | 54 | return 0; | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | int answer_error( int fd, HttpCode code ) { | 
|---|
|  | 58 | /* paranoid */ assert( code < KNOWN_CODES && code != OK200 ); | 
|---|
|  | 59 | int idx = (int)code; | 
|---|
|  | 60 | return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) ); | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 | int answer_header( int fd, size_t size ) { | 
|---|
|  | 64 | const char * fmt = http_msgs[OK200]; | 
|---|
|  | 65 | int len = 100; | 
|---|
|  | 66 | char buffer[len]; | 
|---|
|  | 67 | len = snprintf(buffer, len, fmt, size); | 
|---|
|  | 68 | return answer( fd, buffer, len ); | 
|---|
|  | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 | [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) { | 
|---|
|  | 72 | char * it = buffer; | 
|---|
|  | 73 | size_t count = len - 1; | 
|---|
|  | 74 | int rlen = 0; | 
|---|
|  | 75 | READ: | 
|---|
|  | 76 | for() { | 
|---|
| [d11d6eb] | 77 | int ret = cfa_read(fd, (void*)it, count, 0, -1`s, 0p, 0p); | 
|---|
|  | 78 | if(ret == 0 ) return [OK200, true, 0, 0]; | 
|---|
| [0aec496] | 79 | if(ret < 0 ) { | 
|---|
|  | 80 | if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ; | 
|---|
|  | 81 | abort( "read error: (%d) %s\n", (int)errno, strerror(errno) ); | 
|---|
|  | 82 | } | 
|---|
|  | 83 | it[ret + 1] = '\0'; | 
|---|
|  | 84 | rlen += ret; | 
|---|
|  | 85 |  | 
|---|
|  | 86 | if( strstr( it, "\r\n\r\n" ) ) break; | 
|---|
|  | 87 |  | 
|---|
|  | 88 | it += ret; | 
|---|
|  | 89 | count -= ret; | 
|---|
|  | 90 |  | 
|---|
| [d11d6eb] | 91 | if( count < 1 ) return [E414, false, 0, 0]; | 
|---|
| [0aec496] | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | printf("%.*s\n", rlen, buffer); | 
|---|
|  | 95 |  | 
|---|
|  | 96 | it = buffer; | 
|---|
|  | 97 | int ret = memcmp(it, "GET /", 5); | 
|---|
| [d11d6eb] | 98 | if( ret != 0 ) return [E400, false, 0, 0]; | 
|---|
| [0aec496] | 99 | it += 5; | 
|---|
|  | 100 |  | 
|---|
|  | 101 | char * end = strstr( it, " " ); | 
|---|
|  | 102 | return [OK200, false, it, end - it]; | 
|---|
| [c82af9f] | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) { | 
|---|
|  | 106 | off_t offset = 0; | 
|---|
|  | 107 | ssize_t ret; | 
|---|
|  | 108 | SPLICE1: while(count > 0) { | 
|---|
| [d11d6eb] | 109 | ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p); | 
|---|
| [c82af9f] | 110 | if( ret < 0 ) { | 
|---|
|  | 111 | if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1; | 
|---|
|  | 112 | abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) ); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | count -= ret; | 
|---|
|  | 116 | offset += ret; | 
|---|
|  | 117 | size_t in_pipe = ret; | 
|---|
|  | 118 | SPLICE2: while(in_pipe > 0) { | 
|---|
| [d11d6eb] | 119 | ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p); | 
|---|
| [c82af9f] | 120 | if( ret < 0 ) { | 
|---|
|  | 121 | if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2; | 
|---|
|  | 122 | abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) ); | 
|---|
|  | 123 | } | 
|---|
|  | 124 | in_pipe -= ret; | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
|  | 127 | } | 
|---|
| [0aec496] | 128 | } | 
|---|