Subversion Repositories Tewi

Rev

Rev 312 | Rev 335 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 nishi 1
/* $Id: http.c 315 2024-10-14 10:01:02Z nishi $ */
2
 
3
#define SOURCE
4
 
43 nishi 5
#include "../config.h"
6
 
16 nishi 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 nishi 16
#include <string.h>
16 nishi 17
 
312 nishi 18
#ifdef __OS2__
19
#include <sys/time.h>
20
#include <types.h>
21
#include <tcpustd.h>
22
#endif
23
 
315 nishi 24
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__))
240 nishi 25
#ifdef USE_WINSOCK1
26
#include <winsock.h>
27
#else
17 nishi 28
#include <winsock2.h>
240 nishi 29
#endif
315 nishi 30
#elif defined(__NETWARE__)
31
#include <sys/socket.h>
17 nishi 32
#else
118 nishi 33
#ifdef USE_POLL
187 nishi 34
#ifdef __PPU__
35
#include <net/poll.h>
36
#else
118 nishi 37
#include <poll.h>
187 nishi 38
#endif
118 nishi 39
#else
17 nishi 40
#include <sys/select.h>
41
#endif
118 nishi 42
#endif
17 nishi 43
 
16 nishi 44
void 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 nishi 47
	if(req->query != NULL) free(req->query);
16 nishi 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 nishi 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 nishi 62
}
63
 
64
int 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 nishi 68
	char* header;
69
	int nl;
294 nishi 70
	bool bad;
118 nishi 71
 
72
#ifdef USE_POLL
73
	struct pollfd pollfds[1];
74
	pollfds[0].fd = sock;
75
	pollfds[0].events = POLLIN | POLLPRI;
76
#else
16 nishi 77
	fd_set fds;
118 nishi 78
#endif
16 nishi 79
 
294 nishi 80
	bad = false;
16 nishi 81
 
82
	cbuf[1] = 0;
83
 
84
	req->method = NULL;
85
	req->path = NULL;
20 nishi 86
	req->query = NULL;
16 nishi 87
	req->headers = NULL;
88
	req->body = NULL;
89
	req->version = NULL;
90
 
212 nishi 91
	header = malloc(1);
16 nishi 92
	header[0] = 0;
212 nishi 93
	nl = 0;
16 nishi 94
 
95
	while(1) {
212 nishi 96
		int i;
97
		int len;
213 nishi 98
		int n;
118 nishi 99
#ifndef USE_POLL
212 nishi 100
		struct timeval tv;
16 nishi 101
		FD_ZERO(&fds);
102
		FD_SET(sock, &fds);
103
		tv.tv_sec = 5;
104
		tv.tv_usec = 0;
118 nishi 105
#endif
43 nishi 106
#ifndef NO_SSL
23 nishi 107
		if(ssl == NULL || !SSL_has_pending(ssl)) {
43 nishi 108
#endif
118 nishi 109
#ifdef USE_POLL
212 nishi 110
			n = poll(pollfds, 1, 5000);
118 nishi 111
#else
88 nishi 112
#ifdef __HAIKU__
212 nishi 113
		n = select(32, &fds, NULL, NULL, &tv);
88 nishi 114
#else
212 nishi 115
		n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
88 nishi 116
#endif
118 nishi 117
#endif
22 nishi 118
			if(n <= 0) {
147 nishi 119
				cm_log("HTTP", "Timeout, disconnecting");
22 nishi 120
				free(header);
121
				tw_free_request(req);
122
				return -1;
123
			}
43 nishi 124
#ifndef NO_SSL
22 nishi 125
		}
43 nishi 126
#endif
212 nishi 127
		len = tw_read(ssl, sock, buffer, 512);
70 nishi 128
		if(len <= 0) {
64 nishi 129
			bad = true;
130
			break;
131
		}
16 nishi 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 nishi 144
					char* tmp;
16 nishi 145
					if(req->method == NULL) {
146
						req->method = malloc(1);
147
						req->method[0] = 0;
148
					}
149
					cbuf[0] = c;
212 nishi 150
					tmp = req->method;
16 nishi 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 nishi 164
					char* tmp;
16 nishi 165
					if(req->path == NULL) {
166
						req->path = malloc(1);
167
						req->path[0] = 0;
168
					}
169
					cbuf[0] = c;
212 nishi 170
					tmp = req->path;
16 nishi 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 nishi 182
						int j;
183
						char* p;
184
						int incr;
16 nishi 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 nishi 191
						p = malloc(1);
16 nishi 192
						p[0] = 0;
193
						for(j = 0; req->path[j] != 0; j++) {
212 nishi 194
							char* tmp;
16 nishi 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 nishi 203
							tmp = p;
16 nishi 204
							p = cm_strcat(tmp, cbuf);
205
							free(tmp);
206
						}
207
						free(req->path);
208
						req->path = p;
209
 
212 nishi 210
						incr = 0;
16 nishi 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 nishi 216
								char* pth;
16 nishi 217
								cbuf[0] = oldc;
218
								req->path[j] = 0;
219
 
212 nishi 220
								pth = req->path + incr;
16 nishi 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 nishi 254
					char* tmp;
16 nishi 255
					if(req->version == NULL) {
256
						req->version = malloc(1);
257
						req->version[0] = 0;
258
					}
259
					cbuf[0] = c;
212 nishi 260
					tmp = req->version;
16 nishi 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 nishi 271
						int j;
16 nishi 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 nishi 278
								char* kv;
279
								char* vv;
280
								char** old;
281
								int k;
16 nishi 282
								header[j] = 0;
283
								j++;
284
								for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
285
									;
212 nishi 286
								kv = header;
287
								vv = header + j;
16 nishi 288
 
212 nishi 289
								old = req->headers;
16 nishi 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 nishi 309
					char* tmp;
16 nishi 310
					nl = 0;
311
					cbuf[0] = c;
212 nishi 312
					tmp = header;
16 nishi 313
					header = cm_strcat(tmp, cbuf);
314
					free(tmp);
315
				}
316
			}
317
		}
318
	}
319
getout:
320
	free(header);
321
	if(bad) {
322
		tw_free_request(req);
323
		return 1;
324
	}
212 nishi 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 nishi 338
		}
212 nishi 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 nishi 353
				result = cm_strcat(tmp, cbuf);
354
				free(tmp);
355
			}
20 nishi 356
		}
212 nishi 357
		free(req->path);
358
		req->path = result;
213 nishi 359
 
212 nishi 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 nishi 369
 
212 nishi 370
				pth = req->path + incr;
213 nishi 371
 
212 nishi 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 nishi 380
					}
212 nishi 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 nishi 390
				}
213 nishi 391
 
212 nishi 392
				incr = j + 1;
393
				if(oldc == 0) break;
69 nishi 394
			}
395
		}
212 nishi 396
		free(req->path);
397
		req->path = p;
398
		return 0;
69 nishi 399
	}
16 nishi 400
}