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

Last change on this file since 336 was 336, checked in by Nishi, on Oct 15, 2024 at 3:07:25 AM

trying to add nextstep support

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