aboutsummaryrefslogtreecommitdiff
path: root/picohttp.c
blob: 10b7d3c2ca90f70325fb684d6b7118e2440dc32a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "picohttp.h"

#include <alloca.h>
#include <string.h>

static void picohttpStatus400BadRequest(
	struct picohttpRequest *req )
{
}

static void picohttpStatus404NotFound(
	struct picohttpRequest *req )
{
}

static void picohttpStatus405MethodNotAllowed(
	struct picohttpRequest *req )
{
}

static void picohttpStatus414RequestURITooLong(
	struct picohttpRequest *req )
{
}

static void picohttpStatus500InternalServerError(
	struct picohttpRequest *req )
{
}

static void picohttpStatus501NotImplemented(
	struct picohttpRequest *req )
{
}

static void picohttpStatus505HTTPVersionNotSupported(
	struct picohttpRequest *req )
{
}

static int16_t picohttpIoSkipSpace(
	struct picohttpIoOps const * const ioops )
{
	int16_t ch;
	while(' ' == (char)(ch = picohttpIoGetch(ioops)));
	return ch;
}

static int16_t picohttpIoGetPercentCh(
	struct picohttpIoOps const * const ioops )
{
	char ch;
	int16_t chr;
	if( 0 > (chr = picohttpIoGetch(ioops)))
		return chr;

	chr |= 0x20;
	if( chr >= '0' && chr <= '9' ) {
		ch = ((chr)&0x0f);
	} else if(
	    chr >= 'a' && chr <= 'f' ) {
		ch = (((chr)&0x0f) + 9);
	}

	if( 0 > (chr = picohttpIoGetch(ioops)))
		return chr;

	chr |= 0x20;
	if( chr >= '0' && chr <= '9' ) {
		ch |= ((chr)&0x0f) << 4;
	} else if(
	    chr >= 'a' && chr <= 'f' ) {
		ch |= (((chr)&0x0f) + 9) << 4;
	}

	return ch;
}

int picohttpMatchRoute(
	struct picohttpRequest * const req,
	struct picohttpURLRoute const * const routes )
{
}

void picohttpProcessRequest(
	struct picohttpIoOps const * const ioops,
	struct picohttpURLRoute const * const routes )
{
	char *url, *var;
	struct picohttpRequest request = {0,};
	size_t url_max_length = 0;
	size_t var_max_length = 0;

	for(size_t i = 0; routes[i].urlhead; i++) {
		size_t url_length =
			strlen(routes[i].urlhead) +
			routes[i].max_urltail_len;

		if(url_length > url_max_length)
			url_max_length = url_length;

	}
	url = alloca(url_max_length+1);
	memset(url, 0, url_max_length+1);

	request.url = url;
	request.urltail = 0;
	request.ioops = ioops;
	request.method = 0;

	/* Poor mans string matching tree; trade RAM for code */
	switch( picohttpIoGetch(ioops) ) {
	case 'H': switch( picohttpIoGetch(ioops) ) {
		case 'E': switch( picohttpIoGetch(ioops) ) {
			case 'A': switch( picohttpIoGetch(ioops) ) {
				case 'D':
					request.method = PICOHTTP_METHOD_HEAD;
					break;
				}
			} break;
		} break;
	case 'G': switch( picohttpIoGetch(ioops) ) {
		case 'E': switch( picohttpIoGetch(ioops) ) {
			case 'T':
				request.method = PICOHTTP_METHOD_GET;
			} break;
		} break;
	case 'P': switch( picohttpIoGetch(ioops) ) {
		case 'O': switch( picohttpIoGetch(ioops) ) {
			case 'S': switch( picohttpIoGetch(ioops) ) {
				case 'T':
					request.method = PICOHTTP_METHOD_POST;
					break;
				}
			} break;
		} break;
	}
	if( !request.method ) {
		picohttpStatus501NotImplemented(&request);
		return;
	}

	int16_t ch;

	ch = picohttpIoSkipSpace(ioops);
	if( ch < 0 ) {
		picohttpStatus500InternalServerError(&request);
		return;
	}
	
	url[0] = (char)ch;
	/* copy url up to the first query variable; note that this is not
	 * fully compliant to RFC 6874, which permits query components in each
	 * path component (i.e. between '/'-es). */
	for(char *urliter = url+1 ;; urliter++) {
		if( urliter - url >= url_max_length ) {
			picohttpStatus414RequestURITooLong(&request);
			return;
		}
		ch = picohttpIoGetch(ioops);
		if( ch < 0 ) {
			picohttpStatus500InternalServerError(&request);
			return;
		}
		if( '?' == (char)ch || ' ' == (char)ch ) {
			break;
		}
		if( '%' == (char)ch ) {
			ch = picohttpIoGetPercentCh(ioops);
		}
		if(ch < 0) {
			picohttpStatus500InternalServerError(&request);
			return;
		}

		*urliter = (char)ch;	
	}

	if( !picohttpMatchRoute(&request, routes) ) {
		picohttpStatus404NotFound(&request);
		return;
	}
	if( !(request.route->allowed_methods & request.method) ) {
		picohttpStatus405MethodNotAllowed(&request);
		return;
	}

	if(request.route->get_vars) {
		for(size_t j = 0; request.route->get_vars[j].name; j++) {
			size_t var_length = strlen(
				request.route->get_vars[j].name );
			if( var_length > var_max_length ) {
				var_max_length = var_length;
			}
		}
	}
	var = alloca(var_max_length+1);

	while('?' == (char)ch || '&' ==(char)ch) {
		memset(var, 0, var_max_length+1);
		ch = picohttpIoGetch(ioops);

		if( ch < 0 ) {
			picohttpStatus500InternalServerError(&request);
			return;
		}

		if( '&' == (char)ch )
			continue;

		if( '%' == (char)ch ) {
			ch = picohttpIoGetPercentCh(ioops);
		}
		if(ch < 0) {
			picohttpStatus500InternalServerError(&request);
			return;
		}

		var[0] = ch;
		for(char *variter = var+1 ;; variter++) {
			if( variter - var >= var_max_length ) {
				/* variable name longer than longest accepted
				 * variable name --> skip to next variable */
				do {
					ch = picohttpIoGetch(ioops);
					if( ch < 0 ) {
						picohttpStatus500InternalServerError(&request);
						return;
					}
				} while ( '&' != ch );
				continue;
			}

			ch = picohttpIoGetch(ioops);
			if( ch < 0 ) {
				picohttpStatus500InternalServerError(&request);
				return;
			}
		}
	}

	ch = picohttpIoSkipSpace(ioops);
	if( ch < 0 ) {
		picohttpStatus500InternalServerError(&request);
		return;
	}
	for(uint8_t i = 0; i < 4; i++) {
		if("HTTP"[i] != (char)(ch = picohttpIoGetch(ioops)) ) {
			if( ch < 0 ) {
				picohttpStatus500InternalServerError(&request);
			} else {
				picohttpStatus400BadRequest(&request);
			}
			return;
		}
	}

	if( PICOHTTP_MAJORVERSION(request.httpversion) > 1 ||
	    PICOHTTP_MINORVERSION(request.httpversion) > 1 ) {
		picohttpStatus505HTTPVersionNotSupported(&request);
		return;
	}

	request.route->handler(&request);
}