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

Last change on this file since 315 was 315, checked in by Nishi, on Oct 14, 2024 at 7:01:02 PM

wip

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