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