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

Last change on this file since 212 was 212, checked in by Nishi, on Oct 3, 2024 at 2:44:55 AM

compiles on vc6

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