source: Main/trunk/Server/http.c

Last change on this file was 380, checked in by Nishi, on Oct 17, 2024 at 7:24:11 PM

trying to fix bsdi port

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