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

Last change on this file since 336 was 336, checked in by Nishi, on Oct 15, 2024 at 3:07:25 AM

trying to add nextstep support

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