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

Last change on this file since 311 was 294, checked in by Nishi, on Oct 9, 2024 at 7:51:28 PM

c89

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