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

Last change on this file since 88 was 88, checked in by Nishi, on Sep 19, 2024 at 9:59:40 PM

fix select

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