Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3584 sourcerer 1
/*
2
 * Copyright 2010 John-Mark Bell 
3
 *
4
 * This file is part of NetSurf, http://www.netsurf-browser.org/
5
 *
6
 * NetSurf is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; version 2 of the License.
9
 *
10
 * NetSurf is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see .
17
 */
18
 
19
#include 
20
#include 
21
#include 
22
#include 
23
 
24
#include "utils/http/primitives.h"
25
 
26
/**
27
 * Skip past linear whitespace in input
28
 *
29
 * \param input  Pointer to current input byte. Updated on exit.
30
 */
31
void http__skip_LWS(const char **input)
32
{
33
	const char *pos = *input;
34
 
35
	while (*pos == ' ' || *pos == '\t')
36
		pos++;
37
 
38
	*input = pos;
39
}
40
 
41
/**
42
 * Determine if a character is valid for an HTTP token
43
 *
44
 * \param c  Character to consider
45
 * \return True if character is valid, false otherwise
46
 */
47
static bool http_is_token_char(uint8_t c)
48
{
49
	/* [ 32 - 126 ] except ()<>@,;:\"/[]?={} SP HT */
50
 
51
	if (c <= ' ' || 126 < c)
52
		return false;
53
 
54
	return (strchr("()<>@,;:\\\"/[]?={}", c) == NULL);
55
}
56
 
57
/**
58
 * Parse an HTTP token
59
 *
60
 * \param input  Pointer to current input byte. Updated on exit.
61
 * \param value  Pointer to location to receive on-heap token value.
62
 * \return NSERROR_OK on success,
63
 * 	   NSERROR_NOMEM on memory exhaustion,
64
 * 	   NSERROR_NOT_FOUND if no token could be parsed
65
 *
66
 * The returned value is owned by the caller
67
 */
68
nserror http__parse_token(const char **input, lwc_string **value)
69
{
70
	const uint8_t *start = (const uint8_t *) *input;
71
	const uint8_t *end;
72
	lwc_string *token;
73
 
74
	end = start;
75
	while (http_is_token_char(*end))
76
		end++;
77
 
78
	if (end == start)
79
		return NSERROR_NOT_FOUND;
80
 
81
	if (lwc_intern_string((const char *) start,
82
			end - start, &token) != lwc_error_ok)
83
		return NSERROR_NOMEM;
84
 
85
	*value = token;
86
	*input = (const char *) end;
87
 
88
	return NSERROR_OK;
89
}
90
 
91
/**
92
 * Parse an HTTP quoted-string
93
 *
94
 * \param input  Pointer to current input byte. Updated on exit.
95
 * \param value  Pointer to location to receive on-heap string value.
96
 * \return NSERROR_OK on success,
97
 * 	   NSERROR_NOMEM on memory exhaustion,
98
 * 	   NSERROR_NOT_FOUND if no string could be parsed
99
 *
100
 * The returned value is owned by the caller
101
 */
102
nserror http__parse_quoted_string(const char **input, lwc_string **value)
103
{
104
	const uint8_t *start = (const uint8_t *) *input;
105
	const uint8_t *end;
106
	uint8_t c;
107
	lwc_string *string_value;
108
 
109
	/* <"> *( qdtext | quoted-pair ) <">
110
	 * qdtext = any TEXT except <">
111
	 * quoted-pair = "\" CHAR
112
	 * TEXT = [ HT, CR, LF, 32-126, 128-255 ]
113
	 * CHAR = [ 0 - 127 ]
114
	 *
115
	 * \todo TEXT may contain non 8859-1 chars encoded per RFC 2047
116
	 * \todo Support quoted-pairs
117
	 */
118
 
119
	if (*start != '"')
120
		return NSERROR_NOT_FOUND;
121
 
122
	end = start = start + 1;
123
 
124
	c = *end;
125
	while (c == '\t' || c == '\r' || c == '\n' ||
126
			c == ' ' || c == '!' ||
127
			('#' <= c && c <= 126) || c > 127) {
128
		end++;
129
		c = *end;
130
	}
131
 
132
	if (*end != '"')
133
		return NSERROR_NOT_FOUND;
134
 
135
	if (lwc_intern_string((const char *) start, end - start,
136
			&string_value) != lwc_error_ok)
137
		return NSERROR_NOMEM;
138
 
139
	*value = string_value;
140
 
141
	*input = (const char *) end + 1;
142
 
143
	return NSERROR_OK;
144
}
145