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
|
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "buffer.h"
#include "istream.h"
#include "str.h"
#include "str-sanitize.h"
#include "http-parser.h"
#include "http-header.h"
#include "http-header-parser.h"
enum http_header_parse_state {
HTTP_HEADER_PARSE_STATE_INIT = 0,
HTTP_HEADER_PARSE_STATE_NAME,
HTTP_HEADER_PARSE_STATE_COLON,
HTTP_HEADER_PARSE_STATE_OWS,
HTTP_HEADER_PARSE_STATE_CONTENT,
HTTP_HEADER_PARSE_STATE_CR,
HTTP_HEADER_PARSE_STATE_LF,
HTTP_HEADER_PARSE_STATE_NEW_LINE,
HTTP_HEADER_PARSE_STATE_EOH
};
struct http_header_parser {
struct istream *input;
struct http_header_limits limits;
enum http_header_parse_flags flags;
uoff_t size, field_size;
unsigned int field_count;
const unsigned char *begin, *cur, *end;
const char *error;
string_t *name;
buffer_t *value_buf;
enum http_header_parse_state state;
};
struct http_header_parser *
http_header_parser_init(struct istream *input,
const struct http_header_limits *limits,
enum http_header_parse_flags flags)
{
struct http_header_parser *parser;
parser = i_new(struct http_header_parser, 1);
parser->input = input;
if (limits != NULL)
parser->limits = *limits;
if (parser->limits.max_size == 0)
parser->limits.max_size = UOFF_T_MAX;
if (parser->limits.max_field_size == 0)
parser->limits.max_field_size = UOFF_T_MAX;
if (parser->limits.max_fields == 0)
parser->limits.max_fields = (unsigned int)-1;
parser->flags = flags;
parser->name = str_new(default_pool, 128);
parser->value_buf = buffer_create_dynamic(default_pool, 4096);
return parser;
}
void http_header_parser_deinit(struct http_header_parser **_parser)
{
struct http_header_parser *parser = *_parser;
*_parser = NULL;
//i_stream_skip(ctx->input, ctx->skip);
buffer_free(&parser->value_buf);
str_free(&parser->name);
i_free(parser);
}
void http_header_parser_reset(struct http_header_parser *parser)
{
parser->state = HTTP_HEADER_PARSE_STATE_INIT;
parser->size = 0;
parser->field_size = 0;
parser->field_count = 0;
}
static int http_header_parse_name(struct http_header_parser *parser)
{
const unsigned char *first = parser->cur;
/* field-name = token
token = 1*tchar
*/
while (parser->cur < parser->end && http_char_is_token(*parser->cur))
parser->cur++;
str_append_data(parser->name, first, parser->cur-first);
if (parser->cur == parser->end)
return 0;
if (str_len(parser->name) == 0) {
parser->error = "Empty header field name";
return -1;
}
return 1;
}
static int http_header_parse_ows(struct http_header_parser *parser)
{
/* OWS = *( SP / HTAB )
; "optional" whitespace
*/
while (parser->cur < parser->end &&
(*parser->cur == ' ' || *parser->cur == '\t'))
parser->cur++;
return (parser->cur == parser->end ? 0 : 1);
}
static int http_header_parse_content(struct http_header_parser *parser)
{
const unsigned char *first;
/* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
field-vchar = VCHAR / obs-text
*/
do {
first = parser->cur;
while (parser->cur < parser->end && http_char_is_text(*parser->cur)) {
parser->cur++;
}
buffer_append(parser->value_buf, first, parser->cur-first);
if ((parser->flags & HTTP_HEADER_PARSE_FLAG_STRICT) != 0)
break;
/* We'll be lenient here to accommodate for some bad servers. We just
drop offending characters */
while (parser->cur < parser->end && !http_char_is_text(*parser->cur) &&
(*parser->cur != '\r' && *parser->cur != '\n'))
parser->cur++;
} while (parser->cur < parser->end &&
(*parser->cur != '\r' && *parser->cur != '\n'));
if (parser->cur == parser->end)
return 0;
return 1;
}
static inline const char *_chr_sanitize(unsigned char c)
{
if (c >= 0x20 && c < 0x7F)
return t_strdup_printf("'%c'", c);
return t_strdup_printf("0x%02x", c);
}
static int http_header_parse(struct http_header_parser *parser)
{
int ret;
/* RFC 7230, Section 3.2: Header Fields
'header' = *( header-field CRLF ) CRLF
; Actually part of HTTP-message syntax
header-field = field-name ":" OWS field-value OWS
field-name = token
field-value = *( field-content / obs-fold )
field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
field-vchar = VCHAR / obs-text
obs-fold = CRLF 1*( SP / HTAB )
; obsolete line folding
; see Section 3.2.4
*/
for (;;) {
switch (parser->state) {
case HTTP_HEADER_PARSE_STATE_INIT:
buffer_set_used_size(parser->value_buf, 0);
str_truncate(parser->name, 0);
if (*parser->cur == '\r') {
/* last CRLF */
parser->cur++;
parser->state = HTTP_HEADER_PARSE_STATE_EOH;
if (parser->cur == parser->end)
return 0;
break;
} else if (*parser->cur == '\n') {
/* last LF */
parser->state = HTTP_HEADER_PARSE_STATE_EOH;
break;
}
/* next line */
parser->state = HTTP_HEADER_PARSE_STATE_NAME;
/* fall through */
case HTTP_HEADER_PARSE_STATE_NAME:
if ((ret=http_header_parse_name(parser)) <= 0)
return ret;
parser->state = HTTP_HEADER_PARSE_STATE_COLON;
/* fall through */
case HTTP_HEADER_PARSE_STATE_COLON:
if (*parser->cur != ':') {
parser->error = t_strdup_printf
("Expected ':' after header field name '%s', but found %s",
str_sanitize(str_c(parser->name),64),
_chr_sanitize(*parser->cur));
return -1;
}
parser->cur++;
if (str_len(parser->name) == 0) {
parser->error = "Empty header field name";
return -1;
}
if (++parser->field_count > parser->limits.max_fields) {
parser->error = "Excessive number of header fields";
return -1;
}
parser->state = HTTP_HEADER_PARSE_STATE_OWS;
/* fall through */
case HTTP_HEADER_PARSE_STATE_OWS:
if ((ret=http_header_parse_ows(parser)) <= 0)
return ret;
parser->state = HTTP_HEADER_PARSE_STATE_CONTENT;
/* fall through */
case HTTP_HEADER_PARSE_STATE_CONTENT:
if ((ret=http_header_parse_content(parser)) <= 0)
return ret;
parser->state = HTTP_HEADER_PARSE_STATE_CR;
/* fall through */
case HTTP_HEADER_PARSE_STATE_CR:
if (*parser->cur == '\r') {
parser->cur++;
} else if (*parser->cur != '\n') {
parser->error = t_strdup_printf
("Invalid character %s in content of header field '%s'",
_chr_sanitize(*parser->cur),
str_sanitize(str_c(parser->name),64));
return -1;
}
parser->state = HTTP_HEADER_PARSE_STATE_LF;
if (parser->cur == parser->end)
return 0;
/* fall through */
case HTTP_HEADER_PARSE_STATE_LF:
if (*parser->cur != '\n') {
parser->error = t_strdup_printf
("Expected LF after CR at end of header field '%s', but found %s",
str_sanitize(str_c(parser->name),64),
_chr_sanitize(*parser->cur));
return -1;
}
parser->cur++;
parser->state = HTTP_HEADER_PARSE_STATE_NEW_LINE;
if (parser->cur == parser->end)
return 0;
/* fall through */
case HTTP_HEADER_PARSE_STATE_NEW_LINE:
if (*parser->cur == ' ' || *parser->cur == '\t') {
/* obs-fold */
buffer_append_c(parser->value_buf, ' ');
parser->state = HTTP_HEADER_PARSE_STATE_OWS;
break;
}
/* next header line */
parser->state = HTTP_HEADER_PARSE_STATE_INIT;
return 1;
case HTTP_HEADER_PARSE_STATE_EOH:
if (*parser->cur != '\n') {
parser->error = t_strdup_printf
("Encountered stray CR at beginning of header line, followed by %s",
_chr_sanitize(*parser->cur));
return -1;
}
/* header fully parsed */
parser->cur++;
return 1;
default:
i_unreached();
}
}
i_unreached();
return -1;
}
int http_header_parse_next_field(struct http_header_parser *parser,
const char **name_r, const unsigned char **data_r, size_t *size_r,
const char **error_r)
{
const uoff_t max_size = parser->limits.max_size;
const uoff_t max_field_size = parser->limits.max_field_size;
const unsigned char *data;
size_t size;
int ret;
*error_r = NULL;
while ((ret=i_stream_read_more(parser->input, &parser->begin, &size)) > 0) {
/* check header size limits */
if (parser->size >= max_size) {
*error_r = "Excessive header size";
return -1;
}
if (parser->field_size > max_field_size) {
*error_r = "Excessive header field size";
return -1;
}
/* don't parse beyond header size limits */
if (size > (max_size - parser->size))
size = max_size - parser->size;
if (size > (max_field_size - parser->field_size)) {
size = max_field_size - parser->field_size;
size = (size == 0 ? 1 : size); /* need to parse one more byte */
}
parser->cur = parser->begin;
parser->end = parser->cur + size;
if ((ret=http_header_parse(parser)) < 0) {
*error_r = parser->error;
return -1;
}
i_stream_skip(parser->input, parser->cur - parser->begin);
parser->size += parser->cur - parser->begin;
parser->field_size += parser->cur - parser->begin;
if (ret == 1) {
parser->field_size = 0;
if (parser->state != HTTP_HEADER_PARSE_STATE_EOH) {
data = buffer_get_data(parser->value_buf, &size);
/* trim trailing OWS */
while (size > 0 &&
(data[size-1] == ' ' || data[size-1] == '\t'))
size--;
*name_r = str_c(parser->name);
*data_r = data;
*size_r = size;
parser->state = HTTP_HEADER_PARSE_STATE_INIT;
} else {
*name_r = NULL;
*data_r = NULL;
}
return 1;
}
}
i_assert(ret != -2);
if (ret < 0) {
i_assert(parser->input->eof);
if (parser->input->stream_errno == 0)
*error_r = "Premature end of input";
else
*error_r = t_strdup_printf("Stream error: %s",
i_stream_get_error(parser->input));
}
return ret;
}
|