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