blob: 37a144269742e5059f5ef4ec5262cb3281ac4212 [file] [log] [blame]
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02001/*
2 * HTTP/1 protocol analyzer
3 *
4 * Copyright 2000-2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau794f9af2017-07-26 09:07:47 +020013#include <ctype.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020014#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau794f9af2017-07-26 09:07:47 +020016#include <common/http-hdr.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020017
Willy Tarreau188e2302018-06-15 11:11:53 +020018#include <proto/channel.h>
Willy Tarreau8740c8b2017-09-21 10:22:25 +020019#include <proto/hdr_idx.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020020
Willy Tarreau73373ab2018-09-14 17:11:33 +020021/* Parse the Content-Length header field of an HTTP/1 request. The function
22 * checks all possible occurrences of a comma-delimited value, and verifies
23 * if any of them doesn't match a previous value. It returns <0 if a value
24 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
25 * if the value can be indexed (first one). In the last case, the value might
26 * be adjusted and the caller must only add the updated value.
27 */
28int h1_parse_cont_len_header(struct h1m *h1m, struct ist *value)
29{
30 char *e, *n;
31 long long cl;
32 int not_first = !!(h1m->flags & H1_MF_CLEN);
33 struct ist word;
34
35 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
36 e = value->ptr + value->len;
37
38 while (++word.ptr < e) {
39 /* skip leading delimitor and blanks */
40 if (unlikely(HTTP_IS_LWS(*word.ptr)))
41 continue;
42
43 /* digits only now */
44 for (cl = 0, n = word.ptr; n < e; n++) {
45 unsigned int c = *n - '0';
46 if (unlikely(c > 9)) {
47 /* non-digit */
48 if (unlikely(n == word.ptr)) // spaces only
49 goto fail;
50 break;
51 }
52 if (unlikely(cl > ULLONG_MAX / 10ULL))
53 goto fail; /* multiply overflow */
54 cl = cl * 10ULL;
55 if (unlikely(cl + c < cl))
56 goto fail; /* addition overflow */
57 cl = cl + c;
58 }
59
60 /* keep a copy of the exact cleaned value */
61 word.len = n - word.ptr;
62
63 /* skip trailing LWS till next comma or EOL */
64 for (; n < e; n++) {
65 if (!HTTP_IS_LWS(*n)) {
66 if (unlikely(*n != ','))
67 goto fail;
68 break;
69 }
70 }
71
72 /* if duplicate, must be equal */
73 if (h1m->flags & H1_MF_CLEN && cl != h1m->body_len)
74 goto fail;
75
76 /* OK, store this result as the one to be indexed */
77 h1m->flags |= H1_MF_CLEN;
78 h1m->curr_len = h1m->body_len = cl;
79 *value = word;
80 word.ptr = n;
81 }
82 /* here we've reached the end with a single value or a series of
83 * identical values, all matching previous series if any. The last
84 * parsed value was sent back into <value>. We just have to decide
85 * if this occurrence has to be indexed (it's the first one) or
86 * silently skipped (it's not the first one)
87 */
88 return !not_first;
89 fail:
90 return -1;
91}
92
Willy Tarreau2557f6a2018-09-14 16:34:47 +020093/* Parse the Transfer-Encoding: header field of an HTTP/1 request, looking for
94 * "chunked" being the last value, and setting H1_MF_CHNK in h1m->flags only in
95 * this case. Any other token found or any empty header field found will reset
96 * this flag, so that it accurately represents the token's presence at the last
97 * position. The H1_MF_XFER_ENC flag is always set. Note that transfer codings
98 * are case-insensitive (cf RFC7230#4).
99 */
100void h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value)
101{
102 char *e, *n;
103 struct ist word;
104
105 h1m->flags |= H1_MF_XFER_ENC;
106 h1m->flags &= ~H1_MF_CHNK;
107
108 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
109 e = value.ptr + value.len;
110
111 while (++word.ptr < e) {
112 /* skip leading delimitor and blanks */
113 if (HTTP_IS_LWS(*word.ptr))
114 continue;
115
116 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
117 word.len = n - word.ptr;
118
119 /* trim trailing blanks */
120 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
121 word.len--;
122
123 h1m->flags &= ~H1_MF_CHNK;
124 if (isteqi(word, ist("chunked")))
125 h1m->flags |= H1_MF_CHNK;
126
127 word.ptr = n;
128 }
129}
130
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200131/* Parse the Connection: header of an HTTP/1 request, looking for "close",
132 * "keep-alive", and "upgrade" values, and updating h1m->flags according to
133 * what was found there. Note that flags are only added, not removed, so the
134 * function is safe for being called multiple times if multiple occurrences
135 * are found.
136 */
137void h1_parse_connection_header(struct h1m *h1m, struct ist value)
138{
139 char *e, *n;
140 struct ist word;
141
142 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
143 e = value.ptr + value.len;
144
145 while (++word.ptr < e) {
146 /* skip leading delimitor and blanks */
147 if (HTTP_IS_LWS(*word.ptr))
148 continue;
149
150 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
151 word.len = n - word.ptr;
152
153 /* trim trailing blanks */
154 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
155 word.len--;
156
157 if (isteqi(word, ist("keep-alive")))
158 h1m->flags |= H1_MF_CONN_KAL;
159 else if (isteqi(word, ist("close")))
160 h1m->flags |= H1_MF_CONN_CLO;
161 else if (isteqi(word, ist("upgrade")))
162 h1m->flags |= H1_MF_CONN_UPG;
163
164 word.ptr = n;
165 }
166}
167
Willy Tarreau538746a2018-12-11 10:59:20 +0100168/* Macros used in the HTTP/1 parser, to check for the expected presence of
169 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
170 */
171
172/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
173 * <bad>.
174 */
175#define EXPECT_LF_HERE(ptr, bad, state, where) \
176 do { \
177 if (unlikely(*(ptr) != '\n')) { \
178 state = (where); \
179 goto bad; \
180 } \
181 } while (0)
182
183/* Increments pointer <ptr>, continues to label <more> if it's still below
184 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
185 * of buffer was reached.
186 */
187#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
188 do { \
189 if (likely(++(ptr) < (end))) \
190 goto more; \
191 else { \
192 state = (where); \
193 goto stop; \
194 } \
195 } while (0)
196
Willy Tarreau794f9af2017-07-26 09:07:47 +0200197/* This function parses a contiguous HTTP/1 headers block starting at <start>
198 * and ending before <stop>, at once, and converts it a list of (name,value)
199 * pairs representing header fields into the array <hdr> of size <hdr_num>,
200 * whose last entry will have an empty name and an empty value. If <hdr_num> is
Willy Tarreau4433c082018-09-11 15:33:32 +0200201 * too small to represent the whole message, an error is returned. Some
202 * protocol elements such as content-length and transfer-encoding will be
Willy Tarreau5384aac2018-09-11 16:04:48 +0200203 * parsed and stored into h1m as well. <hdr> may be null, in which case only
204 * the parsing state will be updated. This may be used to restart the parsing
205 * where it stopped for example.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200206 *
207 * For now it's limited to the response. If the header block is incomplete,
208 * 0 is returned, waiting to be called again with more data to try it again.
Willy Tarreau4433c082018-09-11 15:33:32 +0200209 * The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE,
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200210 * and h1m->next to zero on the first call, the parser will do the rest. If
211 * an incomplete message is seen, the caller only needs to present h1m->state
212 * and h1m->next again, with an empty header list so that the parser can start
213 * again. In this case, it will detect that it interrupted a previous session
214 * and will first look for the end of the message before reparsing it again and
215 * indexing it at the same time. This ensures that incomplete messages fed 1
216 * character at a time are never processed entirely more than exactly twice,
217 * and that there is no need to store all the internal state and pre-parsed
218 * headers or start line between calls.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200219 *
Willy Tarreaua41393f2018-09-11 15:34:50 +0200220 * A pointer to a start line descriptor may be passed in <slp>, in which case
221 * the parser will fill it with whatever it found.
222 *
Willy Tarreau794f9af2017-07-26 09:07:47 +0200223 * The code derived from the main HTTP/1 parser above but was simplified and
224 * optimized to process responses produced or forwarded by haproxy. The caller
225 * is responsible for ensuring that the message doesn't wrap, and should ensure
226 * it is complete to avoid having to retry the operation after a failed
227 * attempt. The message is not supposed to be invalid, which is why a few
228 * properties such as the character set used in the header field names are not
229 * checked. In case of an unparsable response message, a negative value will be
230 * returned with h1m->err_pos and h1m->err_state matching the location and
231 * state where the error was met. Leading blank likes are tolerated but not
232 * recommended.
233 *
234 * This function returns :
235 * -1 in case of error. In this case, h1m->err_state is filled (if h1m is
Willy Tarreau801250e2018-09-11 11:45:04 +0200236 * set) with the state the error occurred in and h1m->err_pos with the
Willy Tarreau794f9af2017-07-26 09:07:47 +0200237 * the position relative to <start>
238 * -2 if the output is full (hdr_num reached). err_state and err_pos also
239 * indicate where it failed.
240 * 0 in case of missing data.
241 * > 0 on success, it then corresponds to the number of bytes read since
242 * <start> so that the caller can go on with the payload.
243 */
244int h1_headers_to_hdr_list(char *start, const char *stop,
245 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +0200246 struct h1m *h1m, union h1_sl *slp)
Willy Tarreau794f9af2017-07-26 09:07:47 +0200247{
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200248 enum h1m_state state;
249 register char *ptr;
250 register const char *end;
251 unsigned int hdr_count;
252 unsigned int skip; /* number of bytes skipped at the beginning */
253 unsigned int sol; /* start of line */
254 unsigned int col; /* position of the colon */
255 unsigned int eol; /* end of line */
256 unsigned int sov; /* start of value */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200257 union h1_sl sl;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200258 int skip_update;
259 int restarting;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200260 struct ist n, v; /* header name and value during parsing */
261
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200262 skip = 0; // do it only once to keep track of the leading CRLF.
263
264 try_again:
265 hdr_count = sol = col = eol = sov = 0;
Willy Tarreaua41393f2018-09-11 15:34:50 +0200266 sl.st.status = 0;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200267 skip_update = restarting = 0;
268
269 ptr = start + h1m->next;
270 end = stop;
271 state = h1m->state;
272
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200273 if (state != H1_MSG_RQBEFORE && state != H1_MSG_RPBEFORE)
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200274 restarting = 1;
275
Willy Tarreau794f9af2017-07-26 09:07:47 +0200276 if (unlikely(ptr >= end))
277 goto http_msg_ood;
278
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200279 /* don't update output if hdr is NULL or if we're restarting */
280 if (!hdr || restarting)
Willy Tarreau5384aac2018-09-11 16:04:48 +0200281 skip_update = 1;
282
Willy Tarreau794f9af2017-07-26 09:07:47 +0200283 switch (state) {
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200284 case H1_MSG_RQBEFORE:
285 http_msg_rqbefore:
286 if (likely(HTTP_IS_TOKEN(*ptr))) {
287 /* we have a start of message, we may have skipped some
288 * heading CRLF. Skip them now.
289 */
290 skip += ptr - start;
291 start = ptr;
292
293 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200294 sl.rq.m.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200295 hdr_count = 0;
296 state = H1_MSG_RQMETH;
297 goto http_msg_rqmeth;
298 }
299
300 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
301 state = H1_MSG_RQBEFORE;
302 goto http_msg_invalid;
303 }
304
305 if (unlikely(*ptr == '\n'))
306 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
307 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
308 /* stop here */
309
310 case H1_MSG_RQBEFORE_CR:
311 http_msg_rqbefore_cr:
312 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
313 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
314 /* stop here */
315
316 case H1_MSG_RQMETH:
317 http_msg_rqmeth:
318 if (likely(HTTP_IS_TOKEN(*ptr)))
319 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
320
321 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200322 sl.rq.m.len = ptr - sl.rq.m.ptr;
323 sl.rq.meth = find_http_meth(start, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200324 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
325 }
326
327 if (likely(HTTP_IS_CRLF(*ptr))) {
328 /* HTTP 0.9 request */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200329 sl.rq.m.len = ptr - sl.rq.m.ptr;
330 sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200331 http_msg_req09_uri:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200332 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200333 http_msg_req09_uri_e:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200334 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200335 http_msg_req09_ver:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200336 sl.rq.v.ptr = ptr;
337 sl.rq.v.len = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200338 goto http_msg_rqline_eol;
339 }
340 state = H1_MSG_RQMETH;
341 goto http_msg_invalid;
342
343 case H1_MSG_RQMETH_SP:
344 http_msg_rqmeth_sp:
345 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200346 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200347 goto http_msg_rquri;
348 }
349 if (likely(HTTP_IS_SPHT(*ptr)))
350 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
351 /* so it's a CR/LF, meaning an HTTP 0.9 request */
352 goto http_msg_req09_uri;
353
354 case H1_MSG_RQURI:
355 http_msg_rquri:
356#if defined(__x86_64__) || \
357 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
358 defined(__ARM_ARCH_7A__)
359 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
360 while (ptr <= end - sizeof(int)) {
361 int x = *(int *)ptr - 0x21212121;
362 if (x & 0x80808080)
363 break;
364
365 x -= 0x5e5e5e5e;
366 if (!(x & 0x80808080))
367 break;
368
369 ptr += sizeof(int);
370 }
371#endif
372 if (ptr >= end) {
373 state = H1_MSG_RQURI;
374 goto http_msg_ood;
375 }
376 http_msg_rquri2:
377 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
378 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
379
380 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200381 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200382 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
383 }
384 if (likely((unsigned char)*ptr >= 128)) {
385 /* non-ASCII chars are forbidden unless option
386 * accept-invalid-http-request is enabled in the frontend.
387 * In any case, we capture the faulty char.
388 */
389 if (h1m->err_pos < -1)
390 goto invalid_char;
391 if (h1m->err_pos == -1)
392 h1m->err_pos = ptr - start + skip;
393 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
394 }
395
396 if (likely(HTTP_IS_CRLF(*ptr))) {
397 /* so it's a CR/LF, meaning an HTTP 0.9 request */
398 goto http_msg_req09_uri_e;
399 }
400
401 /* OK forbidden chars, 0..31 or 127 */
402 invalid_char:
403 state = H1_MSG_RQURI;
404 goto http_msg_invalid;
405
406 case H1_MSG_RQURI_SP:
407 http_msg_rquri_sp:
408 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200409 sl.rq.v.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200410 goto http_msg_rqver;
411 }
412 if (likely(HTTP_IS_SPHT(*ptr)))
413 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
414 /* so it's a CR/LF, meaning an HTTP 0.9 request */
415 goto http_msg_req09_ver;
416
417
418 case H1_MSG_RQVER:
419 http_msg_rqver:
420 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
421 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
422
423 if (likely(HTTP_IS_CRLF(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200424 sl.rq.v.len = ptr - sl.rq.v.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200425 http_msg_rqline_eol:
426 /* We have seen the end of line. Note that we do not
427 * necessarily have the \n yet, but at least we know that we
428 * have EITHER \r OR \n, otherwise the request would not be
429 * complete. We can then record the request length and return
430 * to the caller which will be able to register it.
431 */
432
433 if (likely(!skip_update)) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200434 if ((sl.rq.v.len == 8) &&
435 (*(sl.rq.v.ptr + 5) > '1' ||
436 (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200437 h1m->flags |= H1_MF_VER_11;
438
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200439 if (unlikely(hdr_count >= hdr_num)) {
440 state = H1_MSG_RQVER;
441 goto http_output_full;
442 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200443 if (!(h1m->flags & H1_MF_NO_PHDR))
444 http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200445
446 if (unlikely(hdr_count >= hdr_num)) {
447 state = H1_MSG_RQVER;
448 goto http_output_full;
449 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200450 if (!(h1m->flags & H1_MF_NO_PHDR))
451 http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200452 }
453
454 sol = ptr - start;
455 if (likely(*ptr == '\r'))
456 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
457 goto http_msg_rqline_end;
458 }
459
460 /* neither an HTTP_VER token nor a CRLF */
461 state = H1_MSG_RQVER;
462 goto http_msg_invalid;
463
464 case H1_MSG_RQLINE_END:
465 http_msg_rqline_end:
466 /* check for HTTP/0.9 request : no version information
467 * available. sol must point to the first of CR or LF. However
468 * since we don't save these elements between calls, if we come
469 * here from a restart, we don't necessarily know. Thus in this
470 * case we simply start over.
471 */
472 if (restarting)
473 goto restart;
474
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200475 if (unlikely(sl.rq.v.len == 0))
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200476 goto http_msg_last_lf;
477
478 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
479 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
480 /* stop here */
481
482 /*
483 * Common states below
484 */
Willy Tarreau801250e2018-09-11 11:45:04 +0200485 case H1_MSG_RPBEFORE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200486 http_msg_rpbefore:
487 if (likely(HTTP_IS_TOKEN(*ptr))) {
488 /* we have a start of message, we may have skipped some
489 * heading CRLF. Skip them now.
490 */
491 skip += ptr - start;
492 start = ptr;
493
494 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200495 sl.st.v.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200496 hdr_count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +0200497 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200498 goto http_msg_rpver;
499 }
500
501 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200502 state = H1_MSG_RPBEFORE;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200503 goto http_msg_invalid;
504 }
505
506 if (unlikely(*ptr == '\n'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200507 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
508 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore_cr, http_msg_ood, state, H1_MSG_RPBEFORE_CR);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200509 /* stop here */
510
Willy Tarreau801250e2018-09-11 11:45:04 +0200511 case H1_MSG_RPBEFORE_CR:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200512 http_msg_rpbefore_cr:
Willy Tarreau801250e2018-09-11 11:45:04 +0200513 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
514 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200515 /* stop here */
516
Willy Tarreau801250e2018-09-11 11:45:04 +0200517 case H1_MSG_RPVER:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200518 http_msg_rpver:
519 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200520 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200521
522 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200523 sl.st.v.len = ptr - sl.st.v.ptr;
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200524
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200525 if ((sl.st.v.len == 8) &&
526 (*(sl.st.v.ptr + 5) > '1' ||
527 (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200528 h1m->flags |= H1_MF_VER_11;
529
Willy Tarreau801250e2018-09-11 11:45:04 +0200530 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, H1_MSG_RPVER_SP);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200531 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200532 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200533 goto http_msg_invalid;
534
Willy Tarreau801250e2018-09-11 11:45:04 +0200535 case H1_MSG_RPVER_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200536 http_msg_rpver_sp:
537 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200538 sl.st.status = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200539 sl.st.c.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200540 goto http_msg_rpcode;
541 }
542 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200543 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, H1_MSG_RPVER_SP);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200544 /* so it's a CR/LF, this is invalid */
Willy Tarreau801250e2018-09-11 11:45:04 +0200545 state = H1_MSG_RPVER_SP;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200546 goto http_msg_invalid;
547
Willy Tarreau801250e2018-09-11 11:45:04 +0200548 case H1_MSG_RPCODE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200549 http_msg_rpcode:
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100550 if (likely(HTTP_IS_DIGIT(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200551 sl.st.status = sl.st.status * 10 + *ptr - '0';
Willy Tarreau801250e2018-09-11 11:45:04 +0200552 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200553 }
554
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100555 if (unlikely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200556 state = H1_MSG_RPCODE;
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100557 goto http_msg_invalid;
558 }
559
Willy Tarreau794f9af2017-07-26 09:07:47 +0200560 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200561 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau801250e2018-09-11 11:45:04 +0200562 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, H1_MSG_RPCODE_SP);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200563 }
564
565 /* so it's a CR/LF, so there is no reason phrase */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200566 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200567
568 http_msg_rsp_reason:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200569 sl.st.r.ptr = ptr;
570 sl.st.r.len = 0;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200571 goto http_msg_rpline_eol;
572
Willy Tarreau801250e2018-09-11 11:45:04 +0200573 case H1_MSG_RPCODE_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200574 http_msg_rpcode_sp:
575 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200576 sl.st.r.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200577 goto http_msg_rpreason;
578 }
579 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200580 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, H1_MSG_RPCODE_SP);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200581 /* so it's a CR/LF, so there is no reason phrase */
582 goto http_msg_rsp_reason;
583
Willy Tarreau801250e2018-09-11 11:45:04 +0200584 case H1_MSG_RPREASON:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200585 http_msg_rpreason:
586 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200587 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200588 sl.st.r.len = ptr - sl.st.r.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200589 http_msg_rpline_eol:
590 /* We have seen the end of line. Note that we do not
591 * necessarily have the \n yet, but at least we know that we
592 * have EITHER \r OR \n, otherwise the response would not be
593 * complete. We can then record the response length and return
594 * to the caller which will be able to register it.
595 */
596
Willy Tarreau5384aac2018-09-11 16:04:48 +0200597 if (likely(!skip_update)) {
598 if (unlikely(hdr_count >= hdr_num)) {
599 state = H1_MSG_RPREASON;
600 goto http_output_full;
601 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200602 if (!(h1m->flags & H1_MF_NO_PHDR))
603 http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200604 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200605
606 sol = ptr - start;
607 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200608 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpline_end, http_msg_ood, state, H1_MSG_RPLINE_END);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200609 goto http_msg_rpline_end;
610
Willy Tarreau801250e2018-09-11 11:45:04 +0200611 case H1_MSG_RPLINE_END:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200612 http_msg_rpline_end:
613 /* sol must point to the first of CR or LF. */
Willy Tarreau801250e2018-09-11 11:45:04 +0200614 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
615 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200616 /* stop here */
617
Willy Tarreau801250e2018-09-11 11:45:04 +0200618 case H1_MSG_HDR_FIRST:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200619 http_msg_hdr_first:
620 sol = ptr - start;
621 if (likely(!HTTP_IS_CRLF(*ptr))) {
622 goto http_msg_hdr_name;
623 }
624
625 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200626 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200627 goto http_msg_last_lf;
628
Willy Tarreau801250e2018-09-11 11:45:04 +0200629 case H1_MSG_HDR_NAME:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200630 http_msg_hdr_name:
631 /* assumes sol points to the first char */
632 if (likely(HTTP_IS_TOKEN(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200633 if (!skip_update) {
634 /* turn it to lower case if needed */
635 if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
636 *ptr = tolower(*ptr);
637 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200638 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, H1_MSG_HDR_NAME);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200639 }
640
641 if (likely(*ptr == ':')) {
642 col = ptr - start;
Willy Tarreau801250e2018-09-11 11:45:04 +0200643 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, H1_MSG_HDR_L1_SP);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200644 }
645
Willy Tarreau9aec3052018-09-12 09:20:40 +0200646 if (likely(h1m->err_pos < -1) || *ptr == '\n') {
Willy Tarreau801250e2018-09-11 11:45:04 +0200647 state = H1_MSG_HDR_NAME;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200648 goto http_msg_invalid;
649 }
650
Willy Tarreau9aec3052018-09-12 09:20:40 +0200651 if (h1m->err_pos == -1) /* capture the error pointer */
652 h1m->err_pos = ptr - start + skip; /* >= 0 now */
653
654 /* and we still accept this non-token character */
Willy Tarreau801250e2018-09-11 11:45:04 +0200655 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, H1_MSG_HDR_NAME);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200656
Willy Tarreau801250e2018-09-11 11:45:04 +0200657 case H1_MSG_HDR_L1_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200658 http_msg_hdr_l1_sp:
659 /* assumes sol points to the first char */
660 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200661 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, H1_MSG_HDR_L1_SP);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200662
663 /* header value can be basically anything except CR/LF */
664 sov = ptr - start;
665
666 if (likely(!HTTP_IS_CRLF(*ptr))) {
667 goto http_msg_hdr_val;
668 }
669
670 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200671 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lf, http_msg_ood, state, H1_MSG_HDR_L1_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200672 goto http_msg_hdr_l1_lf;
673
Willy Tarreau801250e2018-09-11 11:45:04 +0200674 case H1_MSG_HDR_L1_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200675 http_msg_hdr_l1_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200676 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
677 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lws, http_msg_ood, state, H1_MSG_HDR_L1_LWS);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200678
Willy Tarreau801250e2018-09-11 11:45:04 +0200679 case H1_MSG_HDR_L1_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200680 http_msg_hdr_l1_lws:
681 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200682 if (!skip_update) {
683 /* replace HT,CR,LF with spaces */
684 for (; start + sov < ptr; sov++)
685 start[sov] = ' ';
686 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200687 goto http_msg_hdr_l1_sp;
688 }
689 /* we had a header consisting only in spaces ! */
690 eol = sov;
691 goto http_msg_complete_header;
692
Willy Tarreau801250e2018-09-11 11:45:04 +0200693 case H1_MSG_HDR_VAL:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200694 http_msg_hdr_val:
695 /* assumes sol points to the first char, and sov
696 * points to the first character of the value.
697 */
698
699 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
700 * and lower. In fact since most of the time is spent in the loop, we
701 * also remove the sign bit test so that bytes 0x8e..0x0d break the
702 * loop, but we don't care since they're very rare in header values.
703 */
704#if defined(__x86_64__)
705 while (ptr <= end - sizeof(long)) {
706 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
707 goto http_msg_hdr_val2;
708 ptr += sizeof(long);
709 }
710#endif
711#if defined(__x86_64__) || \
712 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
713 defined(__ARM_ARCH_7A__)
714 while (ptr <= end - sizeof(int)) {
715 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
716 goto http_msg_hdr_val2;
717 ptr += sizeof(int);
718 }
719#endif
720 if (ptr >= end) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200721 state = H1_MSG_HDR_VAL;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200722 goto http_msg_ood;
723 }
724 http_msg_hdr_val2:
725 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200726 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_val2, http_msg_ood, state, H1_MSG_HDR_VAL);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200727
728 eol = ptr - start;
729 /* Note: we could also copy eol into ->eoh so that we have the
730 * real header end in case it ends with lots of LWS, but is this
731 * really needed ?
732 */
733 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200734 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lf, http_msg_ood, state, H1_MSG_HDR_L2_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200735 goto http_msg_hdr_l2_lf;
736
Willy Tarreau801250e2018-09-11 11:45:04 +0200737 case H1_MSG_HDR_L2_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200738 http_msg_hdr_l2_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200739 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
740 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lws, http_msg_ood, state, H1_MSG_HDR_L2_LWS);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200741
Willy Tarreau801250e2018-09-11 11:45:04 +0200742 case H1_MSG_HDR_L2_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200743 http_msg_hdr_l2_lws:
744 if (unlikely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200745 if (!skip_update) {
746 /* LWS: replace HT,CR,LF with spaces */
747 for (; start + eol < ptr; eol++)
748 start[eol] = ' ';
749 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200750 goto http_msg_hdr_val;
751 }
752 http_msg_complete_header:
753 /*
754 * It was a new header, so the last one is finished. Assumes
755 * <sol> points to the first char of the name, <col> to the
756 * colon, <sov> points to the first character of the value and
757 * <eol> to the first CR or LF so we know how the line ends. We
758 * will trim spaces around the value. It's possible to do it by
759 * adjusting <eol> and <sov> which are no more used after this.
760 * We can add the header field to the list.
761 */
Christopher Faulet2912f872018-09-19 14:01:04 +0200762 if (likely(!skip_update)) {
763 while (sov < eol && HTTP_IS_LWS(start[sov]))
764 sov++;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200765
Christopher Faulet2912f872018-09-19 14:01:04 +0200766 while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
767 eol--;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200768
769
Christopher Faulet2912f872018-09-19 14:01:04 +0200770 n = ist2(start + sol, col - sol);
771 v = ist2(start + sov, eol - sov);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200772
Christopher Faulet2912f872018-09-19 14:01:04 +0200773 do {
774 int ret;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200775
Christopher Faulet2912f872018-09-19 14:01:04 +0200776 if (unlikely(hdr_count >= hdr_num)) {
777 state = H1_MSG_HDR_L2_LWS;
778 goto http_output_full;
779 }
Willy Tarreau5384aac2018-09-11 16:04:48 +0200780
Christopher Faulet2912f872018-09-19 14:01:04 +0200781 if (isteqi(n, ist("transfer-encoding"))) {
782 h1_parse_xfer_enc_header(h1m, v);
783 }
784 else if (isteqi(n, ist("content-length"))) {
785 ret = h1_parse_cont_len_header(h1m, &v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200786
Christopher Faulet2912f872018-09-19 14:01:04 +0200787 if (ret < 0) {
788 state = H1_MSG_HDR_L2_LWS;
789 goto http_msg_invalid;
790 }
791 else if (ret == 0) {
792 /* skip it */
793 break;
794 }
Willy Tarreau73373ab2018-09-14 17:11:33 +0200795 }
Christopher Faulet2912f872018-09-19 14:01:04 +0200796 else if (isteqi(n, ist("connection"))) {
797 h1_parse_connection_header(h1m, v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200798 }
Willy Tarreau2ea6bb52018-09-14 16:28:15 +0200799
Christopher Faulet2912f872018-09-19 14:01:04 +0200800 http_set_hdr(&hdr[hdr_count++], n, v);
801 } while (0);
802 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200803
804 sol = ptr - start;
Christopher Faulet2912f872018-09-19 14:01:04 +0200805
Willy Tarreau794f9af2017-07-26 09:07:47 +0200806 if (likely(!HTTP_IS_CRLF(*ptr)))
807 goto http_msg_hdr_name;
808
809 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200810 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200811 goto http_msg_last_lf;
812
Willy Tarreau801250e2018-09-11 11:45:04 +0200813 case H1_MSG_LAST_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200814 http_msg_last_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200815 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200816 ptr++;
817 /* <ptr> now points to the first byte of payload. If needed sol
818 * still points to the first of either CR or LF of the empty
819 * line ending the headers block.
820 */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200821 if (likely(!skip_update)) {
822 if (unlikely(hdr_count >= hdr_num)) {
823 state = H1_MSG_LAST_LF;
824 goto http_output_full;
825 }
Christopher Fauletff08a922018-09-25 13:59:46 +0200826 http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
Willy Tarreau794f9af2017-07-26 09:07:47 +0200827 }
Willy Tarreau001823c2018-09-12 17:25:32 +0200828
829 /* reaching here we've parsed the whole message. We may detect
830 * that we were already continuing an interrupted parsing pass
831 * so we were silently looking for the end of message not
832 * updating anything before deciding to parse it fully at once.
833 * It's guaranteed that we won't match this test twice in a row
834 * since restarting will turn zero.
835 */
836 if (restarting)
837 goto restart;
838
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200839 state = H1_MSG_DATA;
840 if (h1m->flags & H1_MF_XFER_ENC) {
841 if (h1m->flags & H1_MF_CLEN) {
842 h1m->flags &= ~H1_MF_CLEN;
843 hdr_count = http_del_hdr(hdr, ist("content-length"));
844 }
845
846 if (h1m->flags & H1_MF_CHNK)
847 state = H1_MSG_CHUNK_SIZE;
848 else if (!(h1m->flags & H1_MF_RESP)) {
849 /* cf RFC7230#3.3.3 : transfer-encoding in
850 * request without chunked encoding is invalid.
851 */
852 goto http_msg_invalid;
853 }
854 }
855
Willy Tarreau794f9af2017-07-26 09:07:47 +0200856 break;
857
858 default:
859 /* impossible states */
860 goto http_msg_invalid;
861 }
862
Willy Tarreau001823c2018-09-12 17:25:32 +0200863 /* Now we've left the headers state and are either in H1_MSG_DATA or
864 * H1_MSG_CHUNK_SIZE.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200865 */
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200866
Willy Tarreau5384aac2018-09-11 16:04:48 +0200867 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200868 *slp = sl;
869
Willy Tarreau4433c082018-09-11 15:33:32 +0200870 h1m->state = state;
871 h1m->next = ptr - start + skip;
872 return h1m->next;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200873
874 http_msg_ood:
875 /* out of data at <ptr> during state <state> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200876 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200877 *slp = sl;
878
Willy Tarreau4433c082018-09-11 15:33:32 +0200879 h1m->state = state;
880 h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200881 return 0;
882
883 http_msg_invalid:
884 /* invalid message, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200885 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200886 *slp = sl;
887
Willy Tarreau4433c082018-09-11 15:33:32 +0200888 h1m->err_state = h1m->state = state;
889 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200890 return -1;
891
892 http_output_full:
893 /* no more room to store the current header, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200894 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200895 *slp = sl;
896
Willy Tarreau4433c082018-09-11 15:33:32 +0200897 h1m->err_state = h1m->state = state;
898 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200899 return -2;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200900
901 restart:
902 h1m->next = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200903 if (h1m->flags & H1_MF_RESP)
904 h1m->state = H1_MSG_RPBEFORE;
905 else
906 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200907 goto try_again;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200908}
909
Willy Tarreau2510f702017-10-31 17:14:16 +0100910/* This function performs a very minimal parsing of the trailers block present
Willy Tarreauf40e6822018-06-14 16:52:02 +0200911 * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of
Willy Tarreau7314be82018-06-14 13:32:50 +0200912 * bytes to delete to skip the trailers. It may return 0 if it's missing some
913 * input data, or < 0 in case of parse error (in which case the caller may have
914 * to decide how to proceed, possibly eating everything).
Willy Tarreau2510f702017-10-31 17:14:16 +0100915 */
Willy Tarreauf40e6822018-06-14 16:52:02 +0200916int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max)
Willy Tarreau2510f702017-10-31 17:14:16 +0100917{
Willy Tarreauf40e6822018-06-14 16:52:02 +0200918 const char *stop = b_peek(buf, ofs + max);
919 int count = ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +0100920
921 while (1) {
922 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau7314be82018-06-14 13:32:50 +0200923 const char *start = b_peek(buf, count);
Willy Tarreau2510f702017-10-31 17:14:16 +0100924 const char *ptr = start;
Willy Tarreau2510f702017-10-31 17:14:16 +0100925
926 /* scan current line and stop at LF or CRLF */
927 while (1) {
928 if (ptr == stop)
929 return 0;
930
931 if (*ptr == '\n') {
932 if (!p1)
933 p1 = ptr;
934 p2 = ptr;
935 break;
936 }
937
938 if (*ptr == '\r') {
939 if (p1)
940 return -1;
941 p1 = ptr;
942 }
943
Willy Tarreau7314be82018-06-14 13:32:50 +0200944 ptr = b_next(buf, ptr);
Willy Tarreau2510f702017-10-31 17:14:16 +0100945 }
946
947 /* after LF; point to beginning of next line */
Willy Tarreau7314be82018-06-14 13:32:50 +0200948 p2 = b_next(buf, p2);
949 count += b_dist(buf, start, p2);
Willy Tarreau2510f702017-10-31 17:14:16 +0100950
951 /* LF/CRLF at beginning of line => end of trailers at p2.
952 * Everything was scheduled for forwarding, there's nothing left
953 * from this message. */
954 if (p1 == start)
955 break;
956 /* OK, next line then */
957 }
Willy Tarreauf40e6822018-06-14 16:52:02 +0200958 return count - ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +0100959}