summaryrefslogtreecommitdiff
path: root/utils/http.c
blob: 71c8a216eee46353dfc6e6af1ed07586132ca19c (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright 2010 John-Mark Bell <jmb@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * HTTP header parsing functions
 */

#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils/http.h"

/**
 * Representation of an HTTP parameter
 */
struct http_parameter {
	struct http_parameter *next;	/**< Next parameter in list, or NULL */

	char *name;			/**< Parameter name */
	char *value;			/**< Parameter value */
};

/**
 * Determine if a character is valid for an HTTP token
 *
 * \param c  Character to consider
 * \return True if character is valid, false otherwise
 */
static bool http_is_token_char(uint8_t c)
{
	/* [ 32 - 126 ] except ()<>@,;:\"/[]?={} SP HT */

	if (c <= ' ' || 126 < c)
		return false;

	return (strchr("()<>@,;:\\\"/[]?={}", c) == NULL);
}

/**
 * Parse an HTTP token
 *
 * \param input  Pointer to current input byte. Updated on exit.
 * \param value  Pointer to location to receive on-heap token value.
 * \return NSERROR_OK on success,
 * 	   NSERROR_NOMEM on memory exhaustion
 *
 * The returned value is owned by the caller
 */
static nserror http_parse_token(const char **input, char **value)
{
	const uint8_t *start = (const uint8_t *) *input;
	const uint8_t *end;
	char *token;

	end = start;
	while (http_is_token_char(*end))
		end++;

	token = malloc(end - start + 1);
	if (token == NULL)
		return NSERROR_NOMEM;

	memcpy(token, start, end - start);
	token[end - start] = '\0';

	*value = token;
	*input = (const char *) end;

	return NSERROR_OK;
}

/**
 * Parse an HTTP quoted-string
 *
 * \param input  Pointer to current input byte. Updated on exit.
 * \param value  Pointer to location to receive on-heap string value.
 * \return NSERROR_OK on success,
 * 	   NSERROR_NOMEM on memory exhaustion
 *
 * The returned value is owned by the caller
 */
static nserror http_parse_quoted_string(const char **input, char **value)
{
	const uint8_t *start = (const uint8_t *) *input;
	const uint8_t *end;
	uint8_t c;
	char *string_value;

	/* <"> *( qdtext | quoted-pair ) <">
	 * qdtext = any TEXT except <">
	 * quoted-pair = "\" CHAR
	 * TEXT = [ HT, CR, LF, 32-126, 128-255 ]
	 * CHAR = [ 0 - 127 ]
	 *
	 * \todo TEXT may contain non 8859-1 chars encoded per RFC 2047
	 * \todo Support quoted-pairs
	 */

	if (*start == '"') {
		end = start = start + 1;

		c = *end;
		while (c == '\t' || c == '\r' || c == '\n' || 
				c == ' ' || c == '!' ||
				('#' <= c && c <= 126) || c > 127) {
			end++;
			c = *end;
		}

		if (*end != '"') {
			start--;
			end = start;
		}
	} else {
		end = start;
	}

	string_value = malloc(end - start + 1);
	if (string_value == NULL)
		return NSERROR_NOMEM;

	memcpy(string_value, start, end - start);
	string_value[end - start] = '\0';

	*value = string_value;

	if (end != start)
		*input = (const char *) end + 1;

	return NSERROR_OK;
}

/**
 * Parse an HTTP parameter
 *
 * \param input      Pointer to current input byte. Updated on exit.
 * \param parameter  Pointer to location to receive on-heap parameter.
 * \return NSERROR_OK on success,
 * 	   NSERROR_NOMEM on memory exhaustion
 *
 * The returned parameter is owned by the caller.
 */
static nserror http_parse_parameter(const char **input,
		http_parameter **parameter)
{
	const char *pos = *input;
	char *name;
	char *value;
	http_parameter *param;
	nserror error;

	/* token "=" ( token | quoted-string ) */

	error = http_parse_token(&pos, &name);
	if (error != NSERROR_OK)
		return error;

	while (*pos == ' ' || *pos == '\t')
		pos++;

	if (*pos != '=') {
		value = strdup("");
		if (value == NULL) {
			free(name);
			return NSERROR_NOMEM;
		}
	} else {
		pos++;

		while (*pos == ' ' || *pos == '\t')
			pos++;

		if (*pos == '"')
			error = http_parse_quoted_string(&pos, &value);
		else
			error = http_parse_token(&pos, &value);

		if (error != NSERROR_OK) {
			free(name);
			return error;
		}
	}

	param = malloc(sizeof(*param));
	if (param == NULL) {
		free(value);
		free(name);
		return NSERROR_NOMEM;
	}

	param->next = NULL;
	param->name = name;
	param->value = value;

	*parameter = param;
	*input = pos;

	return NSERROR_OK;
}

/**
 * Parse an HTTP parameter list
 *
 * \param input       Pointer to current input byte. Updated on exit.
 * \param parameters  Pointer to location to receive on-heap parameter list.
 * \return NSERROR_OK on success,
 * 	   NSERROR_NOMEM on memory exhaustion
 *
 * The returned parameter list is owned by the caller
 */
static nserror http_parse_parameter_list(const char **input, 
		http_parameter **parameters)
{
	const char *pos = *input;
	http_parameter *param;
	http_parameter *list = NULL;
	nserror error;

	/* 1*( ";" parameter ) */

	while (*pos == ';') {
		pos++;

		while (*pos == ' ' || *pos == '\t')
			pos++;

		error = http_parse_parameter(&pos, &param);
		if (error != NSERROR_OK) {
			while (list != NULL) {
				param = list;

				list = param->next;

				free(param->name);
				free(param->value);
				free(param);
			}
			return error;
		}

		if (list != NULL)
			param->next = list;

		list = param;

		while (*pos == ' ' || *pos == '\t')
			pos++;
	}

	*parameters = list;
	*input = pos;

	return NSERROR_OK;
}

/* See http.h for documentation */
nserror http_parse_content_type(const char *header_value, char **media_type, 
		http_parameter **parameters)
{
	const char *pos = header_value;
	char *type;
	char *subtype = NULL;
	http_parameter *params = NULL;
	char *mime;
	size_t mime_len;
	nserror error;

	/* type "/" subtype *( ";" parameter ) */

	while (*pos == ' ' || *pos == '\t')
		pos++;

	error = http_parse_token(&pos, &type);
	if (error != NSERROR_OK)
		return error;

	while (*pos == ' ' || *pos == '\t')
		pos++;

	if (*pos == '/') {
		pos++;

		while (*pos == ' ' || *pos == '\t')
			pos++;

		error = http_parse_token(&pos, &subtype);
		if (error != NSERROR_OK) {
			free(type);
			return error;
		}

		while (*pos == ' ' || *pos == '\t')
			pos++;

		if (*pos == ';') {
			error = http_parse_parameter_list(&pos, &params);
			if (error != NSERROR_OK) {
				free(subtype);
				free(type);
				return error;
			}
		}
	}

	/* <type> + <subtype> + '/' */
	mime_len = strlen(type) + (subtype != NULL ? strlen(subtype) : 0) + 1;

	mime = malloc(mime_len + 1);
	if (mime == NULL) {
		http_parameter_list_destroy(params);
		free(subtype);
		free(type);
		return NSERROR_OK;
	}

	sprintf(mime, "%s/%s", type, subtype != NULL ? subtype : "");

	free(subtype);
	free(type);

	*media_type = mime;
	*parameters = params;

	return NSERROR_OK;
}

/* See http.h for documentation */
nserror http_parameter_list_find_item(const http_parameter *list,
		const char *name, const char **value)
{
	while (list != NULL && strcasecmp(name, list->name) != 0)
		list = list->next;

	if (list == NULL)
		return NSERROR_NOT_FOUND;

	*value = list->value;

	return NSERROR_OK;
}

/* See http.h for documentation */
const http_parameter *http_parameter_list_iterate(const http_parameter *cur,
		const char **name, const char **value)
{
	if (cur == NULL)
		return NULL;

	*name = cur->name;
	*value = cur->value;

	return cur->next;
}

/* See http.h for documentation */
void http_parameter_list_destroy(http_parameter *list)
{
	while (list != NULL) {
		http_parameter *victim = list;

		list = victim->next;

		free(victim->name);
		free(victim->value);
		free(victim);
	}
}