summaryrefslogtreecommitdiff
path: root/utils/http/primitives.c
blob: 1e24d2a0d18aac4bc205c04c894b0a2dd7c64d9e (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
/*
 * 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/>.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "netsurf/inttypes.h"
#include "utils/http/primitives.h"

/**
 * Skip past linear whitespace in input
 *
 * \param input  Pointer to current input byte. Updated on exit.
 */
void http__skip_LWS(const char **input)
{
	const char *pos = *input;

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

	*input = pos;
}

/**
 * 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,
 * 	   NSERROR_NOT_FOUND if no token could be parsed
 *
 * The returned value is owned by the caller
 */
nserror http__parse_token(const char **input, lwc_string **value)
{
	const uint8_t *start = (const uint8_t *) *input;
	const uint8_t *end;
	lwc_string *token;

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

	if (end == start)
		return NSERROR_NOT_FOUND;

	if (lwc_intern_string((const char *) start, 
			end - start, &token) != lwc_error_ok)
		return NSERROR_NOMEM;

	*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,
 * 	   NSERROR_NOT_FOUND if no string could be parsed
 *
 * The returned value is owned by the caller
 */
nserror http__parse_quoted_string(const char **input, lwc_string **value)
{
	const uint8_t *start = (const uint8_t *) *input;
	const uint8_t *end;
	uint8_t c;
	lwc_string *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 != '"')
		return NSERROR_NOT_FOUND;

	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 != '"')
		return NSERROR_NOT_FOUND;

	if (lwc_intern_string((const char *) start, end - start, 
			&string_value) != lwc_error_ok)
		return NSERROR_NOMEM;

	*value = string_value;

	*input = (const char *) end + 1;

	return NSERROR_OK;
}