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

Last change on this file since 396 was 380, checked in by Nishi, on Oct 17, 2024 at 7:24:11 PM

trying to fix bsdi port

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