1 | /* $Id: http.c 361 2024-10-17 00:37:10Z nishi $ */
|
---|
2 |
|
---|
3 | #define SOURCE
|
---|
4 |
|
---|
5 | #include "../config.h"
|
---|
6 |
|
---|
7 | #include "tw_http.h"
|
---|
8 |
|
---|
9 | #include "tw_server.h"
|
---|
10 |
|
---|
11 | #include <cm_log.h>
|
---|
12 | #include <cm_string.h>
|
---|
13 |
|
---|
14 | #include <stdbool.h>
|
---|
15 | #include <stdlib.h>
|
---|
16 | #include <string.h>
|
---|
17 |
|
---|
18 | #ifdef __OS2__
|
---|
19 | #include <sys/time.h>
|
---|
20 | #include <types.h>
|
---|
21 | #include <tcpustd.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__) && !defined(__DOS__))
|
---|
25 | #ifdef USE_WINSOCK1
|
---|
26 | #include <winsock.h>
|
---|
27 | #else
|
---|
28 | #include <winsock2.h>
|
---|
29 | #endif
|
---|
30 | #elif defined(__NETWARE__)
|
---|
31 | #include <sys/bsdskt.h>
|
---|
32 | #include <sys/socket.h>
|
---|
33 | #else
|
---|
34 | #ifdef USE_POLL
|
---|
35 | #ifdef __PPU__
|
---|
36 | #include <net/poll.h>
|
---|
37 | #else
|
---|
38 | #include <poll.h>
|
---|
39 | #endif
|
---|
40 | #else
|
---|
41 | #ifdef __NeXT__
|
---|
42 | #include <sys/socket.h>
|
---|
43 | #include <time.h>
|
---|
44 | #include <sys/time.h>
|
---|
45 | #else
|
---|
46 | #include <sys/select.h>
|
---|
47 | #endif
|
---|
48 | #endif
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | void tw_free_request(struct tw_http_request* req) {
|
---|
52 | if(req->method != NULL) free(req->method);
|
---|
53 | if(req->path != NULL) free(req->path);
|
---|
54 | if(req->query != NULL) free(req->query);
|
---|
55 | if(req->headers != NULL) {
|
---|
56 | int i;
|
---|
57 | for(i = 0; req->headers[i] != NULL; i++) free(req->headers[i]);
|
---|
58 | free(req->headers);
|
---|
59 | }
|
---|
60 | if(req->body != NULL) free(req->body);
|
---|
61 | if(req->version != NULL) free(req->version);
|
---|
62 |
|
---|
63 | req->method = NULL;
|
---|
64 | req->path = NULL;
|
---|
65 | req->query = NULL;
|
---|
66 | req->headers = NULL;
|
---|
67 | req->body = NULL;
|
---|
68 | req->version = NULL;
|
---|
69 | }
|
---|
70 |
|
---|
71 | int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
|
---|
72 | char buffer[512];
|
---|
73 | char cbuf[2];
|
---|
74 | int phase = 0;
|
---|
75 | char* header;
|
---|
76 | int nl;
|
---|
77 | bool bad;
|
---|
78 |
|
---|
79 | #ifdef USE_POLL
|
---|
80 | struct pollfd pollfds[1];
|
---|
81 | pollfds[0].fd = sock;
|
---|
82 | pollfds[0].events = POLLIN | POLLPRI;
|
---|
83 | #else
|
---|
84 | fd_set fds;
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | bad = false;
|
---|
88 |
|
---|
89 | cbuf[1] = 0;
|
---|
90 |
|
---|
91 | req->method = NULL;
|
---|
92 | req->path = NULL;
|
---|
93 | req->query = NULL;
|
---|
94 | req->headers = NULL;
|
---|
95 | req->body = NULL;
|
---|
96 | req->version = NULL;
|
---|
97 |
|
---|
98 | header = malloc(1);
|
---|
99 | header[0] = 0;
|
---|
100 | nl = 0;
|
---|
101 |
|
---|
102 | while(1) {
|
---|
103 | int i;
|
---|
104 | int len;
|
---|
105 | int n;
|
---|
106 | #ifndef USE_POLL
|
---|
107 | struct timeval tv;
|
---|
108 | FD_ZERO(&fds);
|
---|
109 | FD_SET(sock, &fds);
|
---|
110 | tv.tv_sec = 5;
|
---|
111 | tv.tv_usec = 0;
|
---|
112 | #endif
|
---|
113 | #ifndef NO_SSL
|
---|
114 | if(ssl == NULL || !SSL_has_pending(ssl)) {
|
---|
115 | #endif
|
---|
116 | #ifdef USE_POLL
|
---|
117 | n = poll(pollfds, 1, 5000);
|
---|
118 | #else
|
---|
119 | #ifdef __HAIKU__
|
---|
120 | n = select(32, &fds, NULL, NULL, &tv);
|
---|
121 | #else
|
---|
122 | n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
|
---|
123 | #endif
|
---|
124 | #endif
|
---|
125 | if(n <= 0) {
|
---|
126 | cm_log("HTTP", "Timeout, disconnecting");
|
---|
127 | free(header);
|
---|
128 | tw_free_request(req);
|
---|
129 | return -1;
|
---|
130 | }
|
---|
131 | #ifndef NO_SSL
|
---|
132 | }
|
---|
133 | #endif
|
---|
134 | len = tw_read(ssl, sock, buffer, 512);
|
---|
135 | if(len <= 0) {
|
---|
136 | bad = true;
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | for(i = 0; i < len; i++) {
|
---|
140 | char c = buffer[i];
|
---|
141 | if(phase == 0) {
|
---|
142 | if(c == ' ') {
|
---|
143 | if(req->method == NULL) {
|
---|
144 | tw_free_request(req);
|
---|
145 | bad = true;
|
---|
146 | goto getout;
|
---|
147 | } else {
|
---|
148 | phase++;
|
---|
149 | }
|
---|
150 | } else {
|
---|
151 | char* tmp;
|
---|
152 | if(req->method == NULL) {
|
---|
153 | req->method = malloc(1);
|
---|
154 | req->method[0] = 0;
|
---|
155 | }
|
---|
156 | cbuf[0] = c;
|
---|
157 | tmp = req->method;
|
---|
158 | req->method = cm_strcat(tmp, cbuf);
|
---|
159 | free(tmp);
|
---|
160 | }
|
---|
161 | } else if(phase == 1) {
|
---|
162 | if(c == ' ') {
|
---|
163 | if(req->path == NULL) {
|
---|
164 | tw_free_request(req);
|
---|
165 | bad = true;
|
---|
166 | goto getout;
|
---|
167 | } else {
|
---|
168 | phase++;
|
---|
169 | }
|
---|
170 | } else {
|
---|
171 | char* tmp;
|
---|
172 | if(req->path == NULL) {
|
---|
173 | req->path = malloc(1);
|
---|
174 | req->path[0] = 0;
|
---|
175 | }
|
---|
176 | cbuf[0] = c;
|
---|
177 | tmp = req->path;
|
---|
178 | req->path = cm_strcat(tmp, cbuf);
|
---|
179 | free(tmp);
|
---|
180 | }
|
---|
181 | } else if(phase == 2) {
|
---|
182 | if(c == '\n') {
|
---|
183 | if(req->version == NULL) {
|
---|
184 | tw_free_request(req);
|
---|
185 | bad = true;
|
---|
186 | goto getout;
|
---|
187 | } else {
|
---|
188 | /* We have Method, Path, Version now */
|
---|
189 | int j;
|
---|
190 | char* p;
|
---|
191 | int incr;
|
---|
192 | if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
|
---|
193 | cm_log("HTTP", "Bad HTTP Version");
|
---|
194 | bad = true;
|
---|
195 | goto getout;
|
---|
196 | }
|
---|
197 |
|
---|
198 | p = malloc(1);
|
---|
199 | p[0] = 0;
|
---|
200 | for(j = 0; req->path[j] != 0; j++) {
|
---|
201 | char* tmp;
|
---|
202 | if(req->path[j] == '/') {
|
---|
203 | cbuf[0] = '/';
|
---|
204 | for(; req->path[j] != 0 && req->path[j] == '/'; j++)
|
---|
205 | ;
|
---|
206 | j--;
|
---|
207 | } else {
|
---|
208 | cbuf[0] = req->path[j];
|
---|
209 | }
|
---|
210 | tmp = p;
|
---|
211 | p = cm_strcat(tmp, cbuf);
|
---|
212 | free(tmp);
|
---|
213 | }
|
---|
214 | free(req->path);
|
---|
215 | req->path = p;
|
---|
216 |
|
---|
217 | incr = 0;
|
---|
218 | p = malloc(1);
|
---|
219 | p[0] = 0;
|
---|
220 | for(j = 0;; j++) {
|
---|
221 | if(req->path[j] == '/' || req->path[j] == 0) {
|
---|
222 | char oldc = req->path[j];
|
---|
223 | char* pth;
|
---|
224 | cbuf[0] = oldc;
|
---|
225 | req->path[j] = 0;
|
---|
226 |
|
---|
227 | pth = req->path + incr;
|
---|
228 |
|
---|
229 | if(strcmp(pth, "..") == 0) {
|
---|
230 | int k;
|
---|
231 | if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
|
---|
232 | for(k = strlen(p) - 1; k >= 0; k--) {
|
---|
233 | if(p[k] == '/') {
|
---|
234 | p[k + 1] = 0;
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | if(strlen(p) == 0) {
|
---|
239 | free(p);
|
---|
240 | p = cm_strdup("/");
|
---|
241 | }
|
---|
242 | } else if(strcmp(pth, ".") == 0) {
|
---|
243 | } else {
|
---|
244 | char* tmp = p;
|
---|
245 | p = cm_strcat3(tmp, pth, cbuf);
|
---|
246 | free(tmp);
|
---|
247 | }
|
---|
248 |
|
---|
249 | incr = j + 1;
|
---|
250 | if(oldc == 0) break;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | free(req->path);
|
---|
254 | req->path = p;
|
---|
255 |
|
---|
256 | cm_log("HTTP", "Request: %s %s %s", req->method, req->path, req->version);
|
---|
257 |
|
---|
258 | phase++;
|
---|
259 | }
|
---|
260 | } else if(c != '\r') {
|
---|
261 | char* tmp;
|
---|
262 | if(req->version == NULL) {
|
---|
263 | req->version = malloc(1);
|
---|
264 | req->version[0] = 0;
|
---|
265 | }
|
---|
266 | cbuf[0] = c;
|
---|
267 | tmp = req->version;
|
---|
268 | req->version = cm_strcat(tmp, cbuf);
|
---|
269 | free(tmp);
|
---|
270 | }
|
---|
271 | } else if(phase == 3) {
|
---|
272 | if(c == '\n') {
|
---|
273 | nl++;
|
---|
274 | if(nl == 2) {
|
---|
275 | phase++;
|
---|
276 | goto getout;
|
---|
277 | } else {
|
---|
278 | int j;
|
---|
279 | if(req->headers == NULL) {
|
---|
280 | req->headers = malloc(sizeof(*req->headers));
|
---|
281 | req->headers[0] = NULL;
|
---|
282 | }
|
---|
283 | for(j = 0; header[j] != 0; j++) {
|
---|
284 | if(header[j] == ':') {
|
---|
285 | char* kv;
|
---|
286 | char* vv;
|
---|
287 | char** old;
|
---|
288 | int k;
|
---|
289 | header[j] = 0;
|
---|
290 | j++;
|
---|
291 | for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
|
---|
292 | ;
|
---|
293 | kv = header;
|
---|
294 | vv = header + j;
|
---|
295 |
|
---|
296 | old = req->headers;
|
---|
297 | for(k = 0; old[k] != NULL; k++)
|
---|
298 | ;
|
---|
299 | req->headers = malloc(sizeof(*req->headers) * (k + 3));
|
---|
300 | for(k = 0; old[k] != NULL; k++) req->headers[k] = old[k];
|
---|
301 | req->headers[k] = cm_strdup(kv);
|
---|
302 | req->headers[k + 1] = cm_strdup(vv);
|
---|
303 | req->headers[k + 2] = NULL;
|
---|
304 | free(old);
|
---|
305 |
|
---|
306 | cm_log("HTTP", "Header: %s: %s", kv, vv);
|
---|
307 |
|
---|
308 | break;
|
---|
309 | }
|
---|
310 | }
|
---|
311 | free(header);
|
---|
312 | header = malloc(1);
|
---|
313 | header[0] = 0;
|
---|
314 | }
|
---|
315 | } else if(c != '\r') {
|
---|
316 | char* tmp;
|
---|
317 | nl = 0;
|
---|
318 | cbuf[0] = c;
|
---|
319 | tmp = header;
|
---|
320 | header = cm_strcat(tmp, cbuf);
|
---|
321 | free(tmp);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|
326 | getout:
|
---|
327 | free(header);
|
---|
328 | if(bad) {
|
---|
329 | tw_free_request(req);
|
---|
330 | return 1;
|
---|
331 | }
|
---|
332 | {
|
---|
333 | char* result = malloc(1);
|
---|
334 | int i;
|
---|
335 | int j;
|
---|
336 | int incr;
|
---|
337 | char* p;
|
---|
338 | result[0] = 0;
|
---|
339 | for(i = 0; req->path[i] != 0; i++) {
|
---|
340 | if(req->path[i] == '?') {
|
---|
341 | req->path[i] = 0;
|
---|
342 | req->query = cm_strdup(req->path + i + 1);
|
---|
343 | break;
|
---|
344 | }
|
---|
345 | }
|
---|
346 | for(i = 0; req->path[i] != 0; i++) {
|
---|
347 | if(req->path[i] == '%') {
|
---|
348 | if(req->path[i + 1] == 0) continue;
|
---|
349 | cbuf[0] = cm_hex(req->path + i + 1, 2);
|
---|
350 | if(cbuf[0] != '\\') {
|
---|
351 | char* tmp = result;
|
---|
352 | result = cm_strcat(tmp, cbuf);
|
---|
353 | free(tmp);
|
---|
354 | }
|
---|
355 | i += 2;
|
---|
356 | } else if(req->path[i] != '\\') {
|
---|
357 | char* tmp;
|
---|
358 | cbuf[0] = req->path[i];
|
---|
359 | tmp = result;
|
---|
360 | result = cm_strcat(tmp, cbuf);
|
---|
361 | free(tmp);
|
---|
362 | }
|
---|
363 | }
|
---|
364 | free(req->path);
|
---|
365 | req->path = result;
|
---|
366 |
|
---|
367 | incr = 0;
|
---|
368 | p = malloc(1);
|
---|
369 | p[0] = 0;
|
---|
370 | for(j = 0;; j++) {
|
---|
371 | if(req->path[j] == '/' || req->path[j] == 0) {
|
---|
372 | char oldc = req->path[j];
|
---|
373 | char* pth;
|
---|
374 | cbuf[0] = oldc;
|
---|
375 | req->path[j] = 0;
|
---|
376 |
|
---|
377 | pth = req->path + incr;
|
---|
378 |
|
---|
379 | if(strcmp(pth, "..") == 0) {
|
---|
380 | int k;
|
---|
381 | if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
|
---|
382 | for(k = strlen(p) - 1; k >= 0; k--) {
|
---|
383 | if(p[k] == '/') {
|
---|
384 | p[k + 1] = 0;
|
---|
385 | break;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | if(strlen(p) == 0) {
|
---|
389 | free(p);
|
---|
390 | p = cm_strdup("/");
|
---|
391 | }
|
---|
392 | } else if(strcmp(pth, ".") == 0) {
|
---|
393 | } else {
|
---|
394 | char* tmp = p;
|
---|
395 | p = cm_strcat3(tmp, pth, cbuf);
|
---|
396 | free(tmp);
|
---|
397 | }
|
---|
398 |
|
---|
399 | incr = j + 1;
|
---|
400 | if(oldc == 0) break;
|
---|
401 | }
|
---|
402 | }
|
---|
403 | free(req->path);
|
---|
404 | req->path = p;
|
---|
405 | return 0;
|
---|
406 | }
|
---|
407 | }
|
---|