Subversion Repositories Tewi

Rev

Rev 17 | Rev 22 | 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 20 2024-09-14 09:59:15Z nishi $ */
2
 
3
#define SOURCE
4
 
5
#include "tw_http.h"
6
 
7
#include "tw_server.h"
8
 
9
#include <cm_log.h>
10
#include <cm_string.h>
11
 
12
#include <stdbool.h>
13
#include <stdlib.h>
17 nishi 14
#include <string.h>
16 nishi 15
 
17 nishi 16
#ifdef __MINGW32__
17
#include <winsock2.h>
18
#else
19
#include <sys/select.h>
20
#endif
21
 
16 nishi 22
void tw_free_request(struct tw_http_request* req) {
23
	if(req->method != NULL) free(req->method);
24
	if(req->path != NULL) free(req->path);
20 nishi 25
	if(req->query != NULL) free(req->query);
16 nishi 26
	if(req->headers != NULL) {
27
		int i;
28
		for(i = 0; req->headers[i] != NULL; i++) free(req->headers[i]);
29
		free(req->headers);
30
	}
31
	if(req->body != NULL) free(req->body);
32
	if(req->version != NULL) free(req->version);
33
}
34
 
35
int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
36
	char buffer[512];
37
	char cbuf[2];
38
	int phase = 0;
39
	fd_set fds;
40
 
41
	bool bad = false;
42
 
43
	cbuf[1] = 0;
44
 
45
	req->method = NULL;
46
	req->path = NULL;
20 nishi 47
	req->query = NULL;
16 nishi 48
	req->headers = NULL;
49
	req->body = NULL;
50
	req->version = NULL;
51
 
52
	char* header = malloc(1);
53
	header[0] = 0;
54
	int nl = 0;
55
 
56
	while(1) {
57
		FD_ZERO(&fds);
58
		FD_SET(sock, &fds);
59
		struct timeval tv;
60
		tv.tv_sec = 5;
61
		tv.tv_usec = 0;
62
		int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
63
		if(n == 0) break;
64
		int len = tw_read(ssl, sock, buffer, 512);
65
		if(len <= 0) break;
66
		int i;
67
		for(i = 0; i < len; i++) {
68
			char c = buffer[i];
69
			if(phase == 0) {
70
				if(c == ' ') {
71
					if(req->method == NULL) {
72
						tw_free_request(req);
73
						bad = true;
74
						goto getout;
75
					} else {
76
						phase++;
77
					}
78
				} else {
79
					if(req->method == NULL) {
80
						req->method = malloc(1);
81
						req->method[0] = 0;
82
					}
83
					cbuf[0] = c;
84
					char* tmp = req->method;
85
					req->method = cm_strcat(tmp, cbuf);
86
					free(tmp);
87
				}
88
			} else if(phase == 1) {
89
				if(c == ' ') {
90
					if(req->path == NULL) {
91
						tw_free_request(req);
92
						bad = true;
93
						goto getout;
94
					} else {
95
						phase++;
96
					}
97
				} else {
98
					if(req->path == NULL) {
99
						req->path = malloc(1);
100
						req->path[0] = 0;
101
					}
102
					cbuf[0] = c;
103
					char* tmp = req->path;
104
					req->path = cm_strcat(tmp, cbuf);
105
					free(tmp);
106
				}
107
			} else if(phase == 2) {
108
				if(c == '\n') {
109
					if(req->version == NULL) {
110
						tw_free_request(req);
111
						bad = true;
112
						goto getout;
113
					} else {
114
						/* We have Method, Path, Version now */
115
 
116
						if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
117
							cm_log("HTTP", "Bad HTTP Version");
118
							bad = true;
119
							goto getout;
120
						}
121
 
122
						int j;
123
						char* p = malloc(1);
124
						p[0] = 0;
125
						for(j = 0; req->path[j] != 0; j++) {
126
							if(req->path[j] == '/') {
127
								cbuf[0] = '/';
128
								for(; req->path[j] != 0 && req->path[j] == '/'; j++)
129
									;
130
								j--;
131
							} else {
132
								cbuf[0] = req->path[j];
133
							}
134
							char* tmp = p;
135
							p = cm_strcat(tmp, cbuf);
136
							free(tmp);
137
						}
138
						free(req->path);
139
						req->path = p;
140
 
141
						int incr = 0;
142
						p = malloc(1);
143
						p[0] = 0;
144
						for(j = 0;; j++) {
145
							if(req->path[j] == '/' || req->path[j] == 0) {
146
								char oldc = req->path[j];
147
								cbuf[0] = oldc;
148
								req->path[j] = 0;
149
 
150
								char* pth = req->path + incr;
151
 
152
								if(strcmp(pth, "..") == 0) {
153
									int k;
154
									if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
155
									for(k = strlen(p) - 1; k >= 0; k--) {
156
										if(p[k] == '/') {
157
											p[k + 1] = 0;
158
											break;
159
										}
160
									}
161
									if(strlen(p) == 0) {
162
										free(p);
163
										p = cm_strdup("/");
164
									}
165
								} else if(strcmp(pth, ".") == 0) {
166
								} else {
167
									char* tmp = p;
168
									p = cm_strcat3(tmp, pth, cbuf);
169
									free(tmp);
170
								}
171
 
172
								incr = j + 1;
173
								if(oldc == 0) break;
174
							}
175
						}
176
						free(req->path);
177
						req->path = p;
178
 
179
						cm_log("HTTP", "Request: %s %s %s", req->method, req->path, req->version);
180
 
181
						phase++;
182
					}
183
				} else if(c != '\r') {
184
					if(req->version == NULL) {
185
						req->version = malloc(1);
186
						req->version[0] = 0;
187
					}
188
					cbuf[0] = c;
189
					char* tmp = req->version;
190
					req->version = cm_strcat(tmp, cbuf);
191
					free(tmp);
192
				}
193
			} else if(phase == 3) {
194
				if(c == '\n') {
195
					nl++;
196
					if(nl == 2) {
197
						phase++;
198
						goto getout;
199
					} else {
200
						if(req->headers == NULL) {
201
							req->headers = malloc(sizeof(*req->headers));
202
							req->headers[0] = NULL;
203
						}
204
						int j;
205
						for(j = 0; header[j] != 0; j++) {
206
							if(header[j] == ':') {
207
								header[j] = 0;
208
								j++;
209
								for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
210
									;
211
								char* kv = header;
212
								char* vv = header + j;
213
 
214
								char** old = req->headers;
215
								int k;
216
								for(k = 0; old[k] != NULL; k++)
217
									;
218
								req->headers = malloc(sizeof(*req->headers) * (k + 3));
219
								for(k = 0; old[k] != NULL; k++) req->headers[k] = old[k];
220
								req->headers[k] = cm_strdup(kv);
221
								req->headers[k + 1] = cm_strdup(vv);
222
								req->headers[k + 2] = NULL;
223
								free(old);
224
 
225
								cm_log("HTTP", "Header: %s: %s", kv, vv);
226
 
227
								break;
228
							}
229
						}
230
						free(header);
231
						header = malloc(1);
232
						header[0] = 0;
233
					}
234
				} else if(c != '\r') {
235
					nl = 0;
236
					cbuf[0] = c;
237
					char* tmp = header;
238
					header = cm_strcat(tmp, cbuf);
239
					free(tmp);
240
				}
241
			}
242
		}
243
	}
244
getout:
245
	free(header);
246
	if(bad) {
247
		tw_free_request(req);
248
		return 1;
249
	}
20 nishi 250
	char* result = malloc(1);
251
	result[0] = 0;
252
	int i;
253
	for(i = 0; req->path[i] != 0; i++) {
254
		if(req->path[i] == '?') {
255
			req->path[i] = 0;
256
			req->query = cm_strdup(req->path + i + 1);
257
			break;
258
		}
259
	}
260
	for(i = 0; req->path[i] != 0; i++) {
261
		if(req->path[i] == '%') {
262
			if(req->path[i + 1] == 0) continue;
263
			cbuf[0] = cm_hex(req->path + i + 1, 2);
264
			char* tmp = result;
265
			result = cm_strcat(tmp, cbuf);
266
			free(tmp);
267
			i += 2;
268
		} else {
269
			cbuf[0] = req->path[i];
270
			char* tmp = result;
271
			result = cm_strcat(tmp, cbuf);
272
			free(tmp);
273
		}
274
	}
275
	free(req->path);
276
	req->path = result;
16 nishi 277
	return 0;
278
}