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

Last change on this file since 284 was 240, checked in by Nishi, on Oct 3, 2024 at 2:54:55 PM

winsock1 support

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