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

Last change on this file since 335 was 335, checked in by Nishi, on Oct 15, 2024 at 3:06:21 AM

trying to add nextstep support

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