Subversion Repositories Tewi

Rev

Rev 361 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 nishi 1
/* $Id: http.c 380 2024-10-17 10:24:11Z 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
 
359 nishi 24
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__) && !defined(__DOS__))
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__)
361 nishi 31
#include <sys/bsdskt.h>
315 nishi 32
#include <sys/socket.h>
17 nishi 33
#else
118 nishi 34
#ifdef USE_POLL
187 nishi 35
#ifdef __PPU__
36
#include <net/poll.h>
37
#else
118 nishi 38
#include <poll.h>
187 nishi 39
#endif
118 nishi 40
#else
336 nishi 41
#ifdef __NeXT__
42
#include <sys/socket.h>
338 nishi 43
#include <time.h>
337 nishi 44
#include <sys/time.h>
336 nishi 45
#else
380 nishi 46
#ifdef __bsdi__
47
#include <sys/time.h>
48
#endif
17 nishi 49
#include <sys/select.h>
50
#endif
118 nishi 51
#endif
335 nishi 52
#endif
17 nishi 53
 
16 nishi 54
void 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 nishi 57
	if(req->query != NULL) free(req->query);
16 nishi 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 nishi 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 nishi 72
}
73
 
74
int 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 nishi 78
	char* header;
79
	int nl;
294 nishi 80
	bool bad;
118 nishi 81
 
82
#ifdef USE_POLL
83
	struct pollfd pollfds[1];
84
	pollfds[0].fd = sock;
85
	pollfds[0].events = POLLIN | POLLPRI;
86
#else
16 nishi 87
	fd_set fds;
118 nishi 88
#endif
16 nishi 89
 
294 nishi 90
	bad = false;
16 nishi 91
 
92
	cbuf[1] = 0;
93
 
94
	req->method = NULL;
95
	req->path = NULL;
20 nishi 96
	req->query = NULL;
16 nishi 97
	req->headers = NULL;
98
	req->body = NULL;
99
	req->version = NULL;
100
 
212 nishi 101
	header = malloc(1);
16 nishi 102
	header[0] = 0;
212 nishi 103
	nl = 0;
16 nishi 104
 
105
	while(1) {
212 nishi 106
		int i;
107
		int len;
213 nishi 108
		int n;
118 nishi 109
#ifndef USE_POLL
212 nishi 110
		struct timeval tv;
16 nishi 111
		FD_ZERO(&fds);
112
		FD_SET(sock, &fds);
113
		tv.tv_sec = 5;
114
		tv.tv_usec = 0;
118 nishi 115
#endif
43 nishi 116
#ifndef NO_SSL
23 nishi 117
		if(ssl == NULL || !SSL_has_pending(ssl)) {
43 nishi 118
#endif
118 nishi 119
#ifdef USE_POLL
212 nishi 120
			n = poll(pollfds, 1, 5000);
118 nishi 121
#else
88 nishi 122
#ifdef __HAIKU__
212 nishi 123
		n = select(32, &fds, NULL, NULL, &tv);
88 nishi 124
#else
212 nishi 125
		n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
88 nishi 126
#endif
118 nishi 127
#endif
22 nishi 128
			if(n <= 0) {
147 nishi 129
				cm_log("HTTP", "Timeout, disconnecting");
22 nishi 130
				free(header);
131
				tw_free_request(req);
132
				return -1;
133
			}
43 nishi 134
#ifndef NO_SSL
22 nishi 135
		}
43 nishi 136
#endif
212 nishi 137
		len = tw_read(ssl, sock, buffer, 512);
70 nishi 138
		if(len <= 0) {
64 nishi 139
			bad = true;
140
			break;
141
		}
16 nishi 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 nishi 154
					char* tmp;
16 nishi 155
					if(req->method == NULL) {
156
						req->method = malloc(1);
157
						req->method[0] = 0;
158
					}
159
					cbuf[0] = c;
212 nishi 160
					tmp = req->method;
16 nishi 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 nishi 174
					char* tmp;
16 nishi 175
					if(req->path == NULL) {
176
						req->path = malloc(1);
177
						req->path[0] = 0;
178
					}
179
					cbuf[0] = c;
212 nishi 180
					tmp = req->path;
16 nishi 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 nishi 192
						int j;
193
						char* p;
194
						int incr;
16 nishi 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 nishi 201
						p = malloc(1);
16 nishi 202
						p[0] = 0;
203
						for(j = 0; req->path[j] != 0; j++) {
212 nishi 204
							char* tmp;
16 nishi 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 nishi 213
							tmp = p;
16 nishi 214
							p = cm_strcat(tmp, cbuf);
215
							free(tmp);
216
						}
217
						free(req->path);
218
						req->path = p;
219
 
212 nishi 220
						incr = 0;
16 nishi 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 nishi 226
								char* pth;
16 nishi 227
								cbuf[0] = oldc;
228
								req->path[j] = 0;
229
 
212 nishi 230
								pth = req->path + incr;
16 nishi 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 nishi 264
					char* tmp;
16 nishi 265
					if(req->version == NULL) {
266
						req->version = malloc(1);
267
						req->version[0] = 0;
268
					}
269
					cbuf[0] = c;
212 nishi 270
					tmp = req->version;
16 nishi 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 nishi 281
						int j;
16 nishi 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 nishi 288
								char* kv;
289
								char* vv;
290
								char** old;
291
								int k;
16 nishi 292
								header[j] = 0;
293
								j++;
294
								for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
295
									;
212 nishi 296
								kv = header;
297
								vv = header + j;
16 nishi 298
 
212 nishi 299
								old = req->headers;
16 nishi 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 nishi 319
					char* tmp;
16 nishi 320
					nl = 0;
321
					cbuf[0] = c;
212 nishi 322
					tmp = header;
16 nishi 323
					header = cm_strcat(tmp, cbuf);
324
					free(tmp);
325
				}
326
			}
327
		}
328
	}
329
getout:
330
	free(header);
331
	if(bad) {
332
		tw_free_request(req);
333
		return 1;
334
	}
212 nishi 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 nishi 348
		}
212 nishi 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 nishi 363
				result = cm_strcat(tmp, cbuf);
364
				free(tmp);
365
			}
20 nishi 366
		}
212 nishi 367
		free(req->path);
368
		req->path = result;
213 nishi 369
 
212 nishi 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 nishi 379
 
212 nishi 380
				pth = req->path + incr;
213 nishi 381
 
212 nishi 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 nishi 390
					}
212 nishi 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 nishi 400
				}
213 nishi 401
 
212 nishi 402
				incr = j + 1;
403
				if(oldc == 0) break;
69 nishi 404
			}
405
		}
212 nishi 406
		free(req->path);
407
		req->path = p;
408
		return 0;
69 nishi 409
	}
16 nishi 410
}