summaryrefslogtreecommitdiffhomepage
path: root/request.c
blob: 15034eeceb22138bc468811ab11d19cf096da67c (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "request.h"

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <lauxlib.h>
#include <lua.h>

#include "http.h"

static const int REQUEST_DATA_SIZE = 4096;

/// Allocates and initializes request structure.
/// \return Pointer to initialized request or NULL in case of an error
/// \see /free_request
struct request * new_request(lua_State * L)
{
	struct request * request = malloc(sizeof(struct request));

	if (NULL == request)
	{
		return NULL;
	}

	memset(request, 0, sizeof(struct request));
	request->step = parse_step_method;

	request->data = malloc(REQUEST_DATA_SIZE);

	if (NULL == request->data)
	{
		free(request);
		return NULL;
	}

	request->lua = lua_newthread(L);
	request->reference = luaL_ref(L, LUA_REGISTRYINDEX);

	return request;
}

/// Releases memory used by the request and the request itself.
/// \param request Request to free
/// \see /new_request
void free_request(lua_State * L, struct request * request)
{
	if (NULL != request->data)
	{
		free(request->data);
	}

	luaL_unref(L, LUA_REGISTRYINDEX, request->reference);

	free(request);
}

/// Collects request between calls to `poll`.
/// \param fd Client socket
/// \param request Pointer to current request context of handled client
/// \return Number of bytes parsed, -1 if an error occured or 0 if expects more data
int parse_request(lua_State * L, const int fd, struct request ** request)
{
	if (NULL == *request)
	{
		*request = new_request(L);
		if (NULL == *request)
		{
			return -1;
		}
	}

	// TODO: Expand buffer until EAGAIN or arbitrary limit
	int length = read(fd, (*request)->data, REQUEST_DATA_SIZE - 1);

	if (0 == length || (-1 == length && EWOULDBLOCK != errno && EAGAIN != errno))
	{
		return -1; // TODO: Handle errors properly
	}

	(*request)->data[length] = 0;
	(*request)->length = length;

	return (*request)->step(*request);
}


/// Progresses the readout of the request until delimiter is found.
/// \param request Request to process
/// \param delimiter Character that marks the end of the readout
/// \return Position of the delimiter or 0 if the deliminter could not be found
static int read_until_char(struct request * request, const char delimiter)
{
	char character;
	while (request->position < request->length)
	{
		character = request->data[request->position];
		if (delimiter == character)
		{
			return request->position;
		}
		request->position++;
	}
	return 0;
}

/// Progresses the readout of the request until non-whitespace character is found.
/// \param request Request to process
/// \return Position of the first non-whitespace character or 0 if it could not be found
/// \see isalpha(3)
static int read_until_word(struct request * request)
{
	char character;
	while (request->position < request->length)
	{
		character = request->data[request->position];
		if (!isspace(character))
		{
			return request->position;
		}
		request->position++;
	}
	return 0;
}

/// Progresses the readout of the request until line separator is found.
/// \param request Request to process
/// \return Position of the first byte of the separator or 0 if line separator could not be found
static int read_rest_of_line(struct request * request)
{
	while (request->position < request->length - 1)
	{
		if ('\r' == request->data[request->position] && '\n' == request->data[request->position + 1])
		{
			return request->position;
		}
		else if ('\r' == request->data[request->position + 1])
		{
			request->position += 1;
		}
		else
		{
			request->position += 2;
		}
	}
	return 0;
}

#define PROCEED_TO(_x) \
	request->temp.start = 0; \
	request->temp.length = 0; \
	request->step = _x; \
	return _x(request)

/// Parses method field of the request.
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
int parse_step_method(struct request * request)
{
	// TODO: Consider reworking read_* functions to be more aware of word boundaries.
	request->temp.length = read_until_char(request, ' ');

	if (0 == request->temp.length)
	{
		return 0;
	}

	lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);

	PROCEED_TO(parse_step_path);
}

/// Parses path field of the request.
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
// TODO: Consider spliting path into an actual path and arguments in this stage
int parse_step_path(struct request * request)
{
	// TODO: Rethink interface of parse_step_* and read_* functions. After a break it looks like mess; it's a bad sign.
	if (0 >= request->temp.start)
	{
		request->temp.start = read_until_word(request);

		if (0 == request->temp.start)
		{
			return 0;
		}
	}

	const int result = read_until_char(request, ' ');

	if (0 == result)
	{
		return 0;
	}

	request->temp.length = request->position - request->temp.start;

	// TODO: Use macro for pushing temporary span. Also clear the span in that macro instead of PROCEED_TO.
	lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);

	PROCEED_TO(parse_step_version);
}

/// Parses and verifies http version field of the request.
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
// TODO: Return -1 if version is unsupported, meaning other than HTTP/1.1
int parse_step_version(struct request * request)
{
	if (0 >= request->temp.start)
	{
		request->temp.start = read_until_word(request);

		if (0 == request->temp.start)
		{
			return 0;
		}
	}

	const int result = read_rest_of_line(request);

	if (0 == result)
	{
		return 0;
	}

	request->temp.length = request->position - request->temp.start;

	lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);

	PROCEED_TO(parse_step_header_name);
}

/// Parses and verifies name of a single header from the request.
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
int parse_step_header_name(struct request * request)
{
	if (0 == lua_istable(request->lua, -1))
	{
		lua_newtable(request->lua);
	}

	if (request->position + 4 >= request->length)
	{
		return 0;
	}

	if (
		request->data[request->position] == '\r' &&
		request->data[request->position + 1] == '\n' &&
		request->data[request->position + 2] == '\r' &&
		request->data[request->position + 3] == '\n')
	{
		// Skip "\r\n\r\n", end of headers.
		request->position += 4;

		// Early return hidden in a macro!
		PROCEED_TO(parse_step_data);
	}

	if (0 >= request->temp.start)
	{
		request->temp.start = read_until_word(request);

		if (0 == request->temp.start)
		{
			return 0;
		}
	}

	int result = read_until_char(request, ':');

	if (0 == result)
	{
		printf("parse_step_header_name:no ':'\n");
		return 0;
	}

	request->temp.length = request->position - request->temp.start;

	if (0 == strncasecmp("content-length", request->data + request->temp.start, request->temp.length))
	{
		// TODO: While it shouldn't happen, use a flag instead of a magic value.
		request->expected_data_length = -1;
	}

	lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);

	// Skip ':'
	request->position++;
	PROCEED_TO(parse_step_header_value);
}

/// Parses and verifies value of a single header from the request.
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
int parse_step_header_value(struct request * request)
{
	if (0 >= request->temp.start)
	{
		request->temp.start = read_until_word(request);

		if (0 == request->temp.start)
		{
			return 0;
		}
	}

	const int result = read_rest_of_line(request);

	if (0 == result)
	{
		return 0;
	}

	request->temp.length = request->position - request->temp.start;

	if (-1 == request->expected_data_length)
	{
		*(request->data + request->temp.start + request->temp.length) = 0;
		request->expected_data_length = atoi(request->data + request->temp.start);
		*(request->data + request->temp.start + request->temp.length) = '\r';
	}

	lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);
	lua_rawset(request->lua, -3);

	PROCEED_TO(parse_step_header_name);
}

/// Makes sure that the expected data is in payload and pushes it forward to Lua.
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
int parse_step_data(struct request * request)
{
	const int bytes_left = request->length - request->position;

	if (bytes_left > request->expected_data_length)
	{
		return -1;
	}
	else if (bytes_left < request->expected_data_length)
	{
		return 0;
	}

	lua_pushlstring(request->lua, request->data + request->position, request->expected_data_length);

	if (NULL != request->data)
	{
		free(request->data);
		request->data = NULL;
	}

	return request->position;
}

// TODO: Make a table from the request.