Subversion Repositories Tewi

Rev

Rev 335 | Rev 337 | 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 336 2024-10-14 18:07:25Z 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
336 nishi 40
#ifdef __NeXT__
41
#include <sys/socket.h>
42
#else
17 nishi 43
#include <sys/select.h>
44
#endif
118 nishi 45
#endif
335 nishi 46
#endif
17 nishi 47
 
16 nishi 48
void 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 nishi 51
	if(req->query != NULL) free(req->query);
16 nishi 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 nishi 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 nishi 66
}
67
 
68
int 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 nishi 72
	char* header;
73
	int nl;
294 nishi 74
	bool bad;
118 nishi 75
 
76
#ifdef USE_POLL
77
	struct pollfd pollfds[1];
78
	pollfds[0].fd = sock;
79
	pollfds[0].events = POLLIN | POLLPRI;
80
#else
16 nishi 81
	fd_set fds;
118 nishi 82
#endif
16 nishi 83
 
294 nishi 84
	bad = false;
16 nishi 85
 
86
	cbuf[1] = 0;
87
 
88
	req->method = NULL;
89
	req->path = NULL;
20 nishi 90
	req->query = NULL;
16 nishi 91
	req->headers = NULL;
92
	req->body = NULL;
93
	req->version = NULL;
94
 
212 nishi 95
	header = malloc(1);
16 nishi 96
	header[0] = 0;
212 nishi 97
	nl = 0;
16 nishi 98
 
99
	while(1) {
212 nishi 100
		int i;
101
		int len;
213 nishi 102
		int n;
118 nishi 103
#ifndef USE_POLL
212 nishi 104
		struct timeval tv;
16 nishi 105
		FD_ZERO(&fds);
106
		FD_SET(sock, &fds);
107
		tv.tv_sec = 5;
108
		tv.tv_usec = 0;
118 nishi 109
#endif
43 nishi 110
#ifndef NO_SSL
23 nishi 111
		if(ssl == NULL || !SSL_has_pending(ssl)) {
43 nishi 112
#endif
118 nishi 113
#ifdef USE_POLL
212 nishi 114
			n = poll(pollfds, 1, 5000);
118 nishi 115
#else
88 nishi 116
#ifdef __HAIKU__
212 nishi 117
		n = select(32, &fds, NULL, NULL, &tv);
88 nishi 118
#else
212 nishi 119
		n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
88 nishi 120
#endif
118 nishi 121
#endif
22 nishi 122
			if(n <= 0) {
147 nishi 123
				cm_log("HTTP", "Timeout, disconnecting");
22 nishi 124
				free(header);
125
				tw_free_request(req);
126
				return -1;
127
			}
43 nishi 128
#ifndef NO_SSL
22 nishi 129
		}
43 nishi 130
#endif
212 nishi 131
		len = tw_read(ssl, sock, buffer, 512);
70 nishi 132
		if(len <= 0) {
64 nishi 133
			bad = true;
134
			break;
135
		}
16 nishi 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 nishi 148
					char* tmp;
16 nishi 149
					if(req->method == NULL) {
150
						req->method = malloc(1);
151
						req->method[0] = 0;
152
					}
153
					cbuf[0] = c;
212 nishi 154
					tmp = req->method;
16 nishi 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 nishi 168
					char* tmp;
16 nishi 169
					if(req->path == NULL) {
170
						req->path = malloc(1);
171
						req->path[0] = 0;
172
					}
173
					cbuf[0] = c;
212 nishi 174
					tmp = req->path;
16 nishi 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 nishi 186
						int j;
187
						char* p;
188
						int incr;
16 nishi 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 nishi 195
						p = malloc(1);
16 nishi 196
						p[0] = 0;
197
						for(j = 0; req->path[j] != 0; j++) {
212 nishi 198
							char* tmp;
16 nishi 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 nishi 207
							tmp = p;
16 nishi 208
							p = cm_strcat(tmp, cbuf);
209
							free(tmp);
210
						}
211
						free(req->path);
212
						req->path = p;
213
 
212 nishi 214
						incr = 0;
16 nishi 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 nishi 220
								char* pth;
16 nishi 221
								cbuf[0] = oldc;
222
								req->path[j] = 0;
223
 
212 nishi 224
								pth = req->path + incr;
16 nishi 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 nishi 258
					char* tmp;
16 nishi 259
					if(req->version == NULL) {
260
						req->version = malloc(1);
261
						req->version[0] = 0;
262
					}
263
					cbuf[0] = c;
212 nishi 264
					tmp = req->version;
16 nishi 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 nishi 275
						int j;
16 nishi 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 nishi 282
								char* kv;
283
								char* vv;
284
								char** old;
285
								int k;
16 nishi 286
								header[j] = 0;
287
								j++;
288
								for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
289
									;
212 nishi 290
								kv = header;
291
								vv = header + j;
16 nishi 292
 
212 nishi 293
								old = req->headers;
16 nishi 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 nishi 313
					char* tmp;
16 nishi 314
					nl = 0;
315
					cbuf[0] = c;
212 nishi 316
					tmp = header;
16 nishi 317
					header = cm_strcat(tmp, cbuf);
318
					free(tmp);
319
				}
320
			}
321
		}
322
	}
323
getout:
324
	free(header);
325
	if(bad) {
326
		tw_free_request(req);
327
		return 1;
328
	}
212 nishi 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 nishi 342
		}
212 nishi 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 nishi 357
				result = cm_strcat(tmp, cbuf);
358
				free(tmp);
359
			}
20 nishi 360
		}
212 nishi 361
		free(req->path);
362
		req->path = result;
213 nishi 363
 
212 nishi 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 nishi 373
 
212 nishi 374
				pth = req->path + incr;
213 nishi 375
 
212 nishi 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 nishi 384
					}
212 nishi 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 nishi 394
				}
213 nishi 395
 
212 nishi 396
				incr = j + 1;
397
				if(oldc == 0) break;
69 nishi 398
			}
399
		}
212 nishi 400
		free(req->path);
401
		req->path = p;
402
		return 0;
69 nishi 403
	}
16 nishi 404
}