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