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
|
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "net.h"
#include "str.h"
#include "strescape.h"
#include "http-url.h"
#include "http-parser.h"
/*
Character definitions:
tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
/ "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
/ DIGIT / ALPHA
; any VCHAR, except special
special = "(" / ")" / "<" / ">" / "@" / ","
/ ";" / ":" / "\" / DQUOTE / "/" / "["
/ "]" / "?" / "=" / "{" / "}"
qdtext = OWS / %x21 / %x23-5B / %x5D-7E / obs-text
qdtext-nf = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
ctext = OWS / %x21-27 / %x2A-5B / %x5D-7E / obs-text
obs-text = %x80-FF
OWS = *( SP / HTAB )
VCHAR = %x21-7E
't68char' = ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/"
'text' = ( HTAB / SP / VCHAR / obs-text )
Character bit mappings:
(1<<0) => ALPHA / DIGIT / "-" / "." / "_" / "~" / "+"
(1<<1) => "!" / "#" / "$" / "%" / "&" / "'" / "*" / "^" / "`" / "|"
(1<<2) => special
(1<<3) => %x21 / %x2A-5B / %x5D-7E
(1<<4) => %x23-29
(1<<5) => %x22-27
(1<<6) => HTAB / SP / obs-text
(1<<7) => "/"
*/
const unsigned char _http_token_char_mask = (1<<0)|(1<<1);
const unsigned char _http_value_char_mask = (1<<0)|(1<<1)|(1<<2);
const unsigned char _http_text_char_mask = (1<<0)|(1<<1)|(1<<2)|(1<<6);
const unsigned char _http_qdtext_char_mask = (1<<3)|(1<<4)|(1<<6);
const unsigned char _http_ctext_char_mask = (1<<3)|(1<<5)|(1<<6);
const unsigned char _http_token68_char_mask = (1<<0)|(1<<7);
const unsigned char _http_char_lookup[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
64, 10, 36, 50, 50, 50, 50, 50, 20, 20, 10, 9, 12, 9, 9, 140, // 20
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, // 30
12, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 40
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 4, 12, 10, 9, // 50
10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 60
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 10, 12, 9, 0, // 70
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 80
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 90
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // A0
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // B0
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // C0
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // D0
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // E0
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // F0
};
/*
* HTTP value parsing
*/
void http_parser_init(struct http_parser *parser,
const unsigned char *data, size_t size)
{
i_zero(parser);
parser->begin = data;
parser->cur = data;
parser->end = data + size;
}
void http_parse_ows(struct http_parser *parser)
{
/* OWS = *( SP / HTAB ) */
if (parser->cur >= parser->end)
return;
while (parser->cur < parser->end &&
(parser->cur[0] == ' ' || parser->cur[0] == '\t')) {
parser->cur++;
}
}
int http_parser_skip_token(struct http_parser *parser)
{
/* token = 1*tchar */
if (parser->cur >= parser->end || !http_char_is_token(*parser->cur))
return 0;
parser->cur++;
while (parser->cur < parser->end && http_char_is_token(*parser->cur))
parser->cur++;
return 1;
}
int http_parse_token(struct http_parser *parser, const char **token_r)
{
const unsigned char *first = parser->cur;
int ret;
if ((ret=http_parser_skip_token(parser)) <= 0)
return ret;
*token_r = t_strndup(first, parser->cur - first);
return 1;
}
int http_parse_token_list_next(struct http_parser *parser,
const char **token_r)
{
/* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-21;
Appendix B:
For compatibility with legacy list rules, recipients SHOULD accept
empty list elements. In other words, consumers would follow the list
productions:
#element => [ ( "," / element ) *( OWS "," [ OWS element ] ) ]
1#element => *( "," OWS ) element *( OWS "," [ OWS element ] )
*/
for (;;) {
if (http_parse_token(parser, token_r) > 0)
break;
http_parse_ows(parser);
if (parser->cur >= parser->end || parser->cur[0] != ',')
return 0;
parser->cur++;
http_parse_ows(parser);
}
return 1;
}
int http_parse_quoted_string(struct http_parser *parser, const char **str_r)
{
string_t *str;
/* quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'['
/ %x5D-7E ; ']'-'~'
/ obs-text
quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
obs-text = %x80-FF
*/
/* DQUOTE */
if (parser->cur >= parser->end || parser->cur[0] != '"')
return 0;
parser->cur++;
/* *( qdtext / quoted-pair ) */
str = t_str_new(256);
for (;;) {
const unsigned char *first;
/* *qdtext */
first = parser->cur;
while (parser->cur < parser->end && http_char_is_qdtext(*parser->cur))
parser->cur++;
if (parser->cur >= parser->end)
return -1;
str_append_data(str, first, parser->cur - first);
/* DQUOTE */
if (*parser->cur == '"') {
parser->cur++;
break;
/* "\" */
} else if (*parser->cur == '\\') {
parser->cur++;
if (parser->cur >= parser->end || !http_char_is_text(*parser->cur))
return -1;
str_append_c(str, *parser->cur);
parser->cur++;
/* ERROR */
} else {
return -1;
}
}
*str_r = str_c(str);
return 1;
}
int http_parse_token_or_qstring(struct http_parser *parser,
const char **word_r)
{
if (parser->cur >= parser->end)
return 0;
if (parser->cur[0] == '"')
return http_parse_quoted_string(parser, word_r);
return http_parse_token(parser, word_r);
}
|