source: Main/trunk/Server/http.c@ 312

Last change on this file since 312 was 312, checked in by Nishi, on Oct 14, 2024 at 3:17:37 AM

works on os2

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