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 | #include <linux/io_uring.h> |
---|
14 | } |
---|
15 | #include <string.h> |
---|
16 | #include <errno.h> |
---|
17 | |
---|
18 | #include "options.hfa" |
---|
19 | |
---|
20 | const char * volatile date = 0p; |
---|
21 | |
---|
22 | const char * http_msgs[] = { |
---|
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", |
---|
28 | }; |
---|
29 | |
---|
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 | } |
---|
45 | |
---|
46 | static inline int answer( int fd, const char * it, int len) { |
---|
47 | while(len > 0) { |
---|
48 | // Call write |
---|
49 | int ret = write(fd, it, len); |
---|
50 | if( ret < 0 ) { if( errno != EAGAIN && errno != EWOULDBLOCK) abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
51 | |
---|
52 | // update it/len |
---|
53 | it += ret; |
---|
54 | len -= ret; |
---|
55 | } |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | int answer_error( int fd, HttpCode code ) { |
---|
60 | /* paranoid */ assert( code < KNOWN_CODES && code != OK200 ); |
---|
61 | int idx = (int)code; |
---|
62 | return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) ); |
---|
63 | } |
---|
64 | |
---|
65 | int answer_header( int fd, size_t size ) { |
---|
66 | const char * fmt = http_msgs[OK200]; |
---|
67 | int len = 200; |
---|
68 | char buffer[len]; |
---|
69 | len = snprintf(buffer, len, fmt, date, size); |
---|
70 | return answer( fd, buffer, len ); |
---|
71 | } |
---|
72 | |
---|
73 | [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) { |
---|
74 | char * it = buffer; |
---|
75 | size_t count = len - 1; |
---|
76 | int rlen = 0; |
---|
77 | READ: |
---|
78 | for() { |
---|
79 | int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p); |
---|
80 | // int ret = read(fd, (void*)it, count); |
---|
81 | if(ret == 0 ) return [OK200, true, 0, 0]; |
---|
82 | if(ret < 0 ) { |
---|
83 | if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ; |
---|
84 | // if( errno == EINVAL ) return [E400, true, 0, 0]; |
---|
85 | abort( "read error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
86 | } |
---|
87 | it[ret + 1] = '\0'; |
---|
88 | rlen += ret; |
---|
89 | |
---|
90 | if( strstr( it, "\r\n\r\n" ) ) break; |
---|
91 | |
---|
92 | it += ret; |
---|
93 | count -= ret; |
---|
94 | |
---|
95 | if( count < 1 ) return [E414, false, 0, 0]; |
---|
96 | } |
---|
97 | |
---|
98 | printf("%.*s\n", rlen, buffer); |
---|
99 | |
---|
100 | it = buffer; |
---|
101 | int ret = memcmp(it, "GET /", 5); |
---|
102 | if( ret != 0 ) return [E400, false, 0, 0]; |
---|
103 | it += 5; |
---|
104 | |
---|
105 | char * end = strstr( it, " " ); |
---|
106 | return [OK200, false, it, end - it]; |
---|
107 | } |
---|
108 | |
---|
109 | void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) { |
---|
110 | off_t offset = 0; |
---|
111 | ssize_t ret; |
---|
112 | SPLICE1: while(count > 0) { |
---|
113 | ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p); |
---|
114 | if( ret < 0 ) { |
---|
115 | if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1; |
---|
116 | abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
117 | } |
---|
118 | |
---|
119 | count -= ret; |
---|
120 | offset += ret; |
---|
121 | size_t in_pipe = ret; |
---|
122 | SPLICE2: while(in_pipe > 0) { |
---|
123 | ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p); |
---|
124 | if( ret < 0 ) { |
---|
125 | if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2; |
---|
126 | abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
127 | } |
---|
128 | in_pipe -= ret; |
---|
129 | } |
---|
130 | |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | //============================================================================================= |
---|
135 | |
---|
136 | #include <clock.hfa> |
---|
137 | #include <time.hfa> |
---|
138 | #include <thread.hfa> |
---|
139 | |
---|
140 | struct date_buffer { |
---|
141 | char buff[100]; |
---|
142 | }; |
---|
143 | |
---|
144 | thread DateFormater { |
---|
145 | int idx; |
---|
146 | date_buffer buffers[2]; |
---|
147 | }; |
---|
148 | |
---|
149 | void ?{}( DateFormater & this ) { |
---|
150 | ((thread&)this){ "Server Date Thread", *options.clopts.instance }; |
---|
151 | this.idx = 0; |
---|
152 | memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) ); |
---|
153 | memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) ); |
---|
154 | } |
---|
155 | |
---|
156 | void main(DateFormater & this) { |
---|
157 | LOOP: for() { |
---|
158 | waitfor( ^?{} : this) { |
---|
159 | break LOOP; |
---|
160 | } |
---|
161 | or else {} |
---|
162 | |
---|
163 | Time now = getTimeNsec(); |
---|
164 | |
---|
165 | strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now ); |
---|
166 | |
---|
167 | char * next = this.buffers[this.idx].buff; |
---|
168 | __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST); |
---|
169 | this.idx = (this.idx + 1) % 2; |
---|
170 | |
---|
171 | sleep(1`s); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | //============================================================================================= |
---|
176 | DateFormater * the_date_formatter; |
---|
177 | |
---|
178 | void init_protocol(void) { |
---|
179 | the_date_formatter = alloc(); |
---|
180 | (*the_date_formatter){}; |
---|
181 | } |
---|
182 | |
---|
183 | void deinit_protocol(void) { |
---|
184 | ^(*the_date_formatter){}; |
---|
185 | free( the_date_formatter ); |
---|
186 | } |
---|