blob: f9e8c9c0ffb327ad72c3206e07261b572e2cb1ff [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
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100232 * recommended. If flag H1_MF_HDRS_ONLY is set in h1m->flags, only headers are
233 * parsed and the start line is skipped. It is not required to set h1m->state
234 * nor h1m->next in this case.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200235 *
236 * This function returns :
237 * -1 in case of error. In this case, h1m->err_state is filled (if h1m is
Willy Tarreau801250e2018-09-11 11:45:04 +0200238 * set) with the state the error occurred in and h1m->err_pos with the
Willy Tarreau794f9af2017-07-26 09:07:47 +0200239 * the position relative to <start>
240 * -2 if the output is full (hdr_num reached). err_state and err_pos also
241 * indicate where it failed.
242 * 0 in case of missing data.
243 * > 0 on success, it then corresponds to the number of bytes read since
244 * <start> so that the caller can go on with the payload.
245 */
246int h1_headers_to_hdr_list(char *start, const char *stop,
247 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +0200248 struct h1m *h1m, union h1_sl *slp)
Willy Tarreau794f9af2017-07-26 09:07:47 +0200249{
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200250 enum h1m_state state;
251 register char *ptr;
252 register const char *end;
253 unsigned int hdr_count;
254 unsigned int skip; /* number of bytes skipped at the beginning */
255 unsigned int sol; /* start of line */
256 unsigned int col; /* position of the colon */
257 unsigned int eol; /* end of line */
258 unsigned int sov; /* start of value */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200259 union h1_sl sl;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200260 int skip_update;
261 int restarting;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200262 struct ist n, v; /* header name and value during parsing */
263
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200264 skip = 0; // do it only once to keep track of the leading CRLF.
265
266 try_again:
267 hdr_count = sol = col = eol = sov = 0;
Willy Tarreaua41393f2018-09-11 15:34:50 +0200268 sl.st.status = 0;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200269 skip_update = restarting = 0;
270
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100271 if (h1m->flags & H1_MF_HDRS_ONLY) {
272 state = H1_MSG_HDR_FIRST;
273 h1m->next = 0;
274 }
275 else if (h1m->state == H1_MSG_RQBEFORE || h1m->state == H1_MSG_RPBEFORE)
276 state = h1m->state;
277 else
278 restarting = 1;
279
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200280 ptr = start + h1m->next;
281 end = stop;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200282
Willy Tarreau794f9af2017-07-26 09:07:47 +0200283 if (unlikely(ptr >= end))
284 goto http_msg_ood;
285
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200286 /* don't update output if hdr is NULL or if we're restarting */
287 if (!hdr || restarting)
Willy Tarreau5384aac2018-09-11 16:04:48 +0200288 skip_update = 1;
289
Willy Tarreau794f9af2017-07-26 09:07:47 +0200290 switch (state) {
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200291 case H1_MSG_RQBEFORE:
292 http_msg_rqbefore:
293 if (likely(HTTP_IS_TOKEN(*ptr))) {
294 /* we have a start of message, we may have skipped some
295 * heading CRLF. Skip them now.
296 */
297 skip += ptr - start;
298 start = ptr;
299
300 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200301 sl.rq.m.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200302 hdr_count = 0;
303 state = H1_MSG_RQMETH;
304 goto http_msg_rqmeth;
305 }
306
307 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
308 state = H1_MSG_RQBEFORE;
309 goto http_msg_invalid;
310 }
311
312 if (unlikely(*ptr == '\n'))
313 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
314 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
315 /* stop here */
316
317 case H1_MSG_RQBEFORE_CR:
318 http_msg_rqbefore_cr:
319 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
320 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
321 /* stop here */
322
323 case H1_MSG_RQMETH:
324 http_msg_rqmeth:
325 if (likely(HTTP_IS_TOKEN(*ptr)))
326 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
327
328 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200329 sl.rq.m.len = ptr - sl.rq.m.ptr;
330 sl.rq.meth = find_http_meth(start, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200331 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
332 }
333
334 if (likely(HTTP_IS_CRLF(*ptr))) {
335 /* HTTP 0.9 request */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200336 sl.rq.m.len = ptr - sl.rq.m.ptr;
337 sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200338 http_msg_req09_uri:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200339 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200340 http_msg_req09_uri_e:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200341 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200342 http_msg_req09_ver:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200343 sl.rq.v.ptr = ptr;
344 sl.rq.v.len = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200345 goto http_msg_rqline_eol;
346 }
347 state = H1_MSG_RQMETH;
348 goto http_msg_invalid;
349
350 case H1_MSG_RQMETH_SP:
351 http_msg_rqmeth_sp:
352 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200353 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200354 goto http_msg_rquri;
355 }
356 if (likely(HTTP_IS_SPHT(*ptr)))
357 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
358 /* so it's a CR/LF, meaning an HTTP 0.9 request */
359 goto http_msg_req09_uri;
360
361 case H1_MSG_RQURI:
362 http_msg_rquri:
363#if defined(__x86_64__) || \
364 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
365 defined(__ARM_ARCH_7A__)
366 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
367 while (ptr <= end - sizeof(int)) {
368 int x = *(int *)ptr - 0x21212121;
369 if (x & 0x80808080)
370 break;
371
372 x -= 0x5e5e5e5e;
373 if (!(x & 0x80808080))
374 break;
375
376 ptr += sizeof(int);
377 }
378#endif
379 if (ptr >= end) {
380 state = H1_MSG_RQURI;
381 goto http_msg_ood;
382 }
383 http_msg_rquri2:
384 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
385 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
386
387 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200388 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200389 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
390 }
391 if (likely((unsigned char)*ptr >= 128)) {
392 /* non-ASCII chars are forbidden unless option
393 * accept-invalid-http-request is enabled in the frontend.
394 * In any case, we capture the faulty char.
395 */
396 if (h1m->err_pos < -1)
397 goto invalid_char;
398 if (h1m->err_pos == -1)
399 h1m->err_pos = ptr - start + skip;
400 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
401 }
402
403 if (likely(HTTP_IS_CRLF(*ptr))) {
404 /* so it's a CR/LF, meaning an HTTP 0.9 request */
405 goto http_msg_req09_uri_e;
406 }
407
408 /* OK forbidden chars, 0..31 or 127 */
409 invalid_char:
410 state = H1_MSG_RQURI;
411 goto http_msg_invalid;
412
413 case H1_MSG_RQURI_SP:
414 http_msg_rquri_sp:
415 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200416 sl.rq.v.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200417 goto http_msg_rqver;
418 }
419 if (likely(HTTP_IS_SPHT(*ptr)))
420 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
421 /* so it's a CR/LF, meaning an HTTP 0.9 request */
422 goto http_msg_req09_ver;
423
424
425 case H1_MSG_RQVER:
426 http_msg_rqver:
427 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
428 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
429
430 if (likely(HTTP_IS_CRLF(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200431 sl.rq.v.len = ptr - sl.rq.v.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200432 http_msg_rqline_eol:
433 /* We have seen the end of line. Note that we do not
434 * necessarily have the \n yet, but at least we know that we
435 * have EITHER \r OR \n, otherwise the request would not be
436 * complete. We can then record the request length and return
437 * to the caller which will be able to register it.
438 */
439
440 if (likely(!skip_update)) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200441 if ((sl.rq.v.len == 8) &&
442 (*(sl.rq.v.ptr + 5) > '1' ||
443 (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200444 h1m->flags |= H1_MF_VER_11;
445
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200446 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(":method"), sl.rq.m);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200452
453 if (unlikely(hdr_count >= hdr_num)) {
454 state = H1_MSG_RQVER;
455 goto http_output_full;
456 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200457 if (!(h1m->flags & H1_MF_NO_PHDR))
458 http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200459 }
460
461 sol = ptr - start;
462 if (likely(*ptr == '\r'))
463 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
464 goto http_msg_rqline_end;
465 }
466
467 /* neither an HTTP_VER token nor a CRLF */
468 state = H1_MSG_RQVER;
469 goto http_msg_invalid;
470
471 case H1_MSG_RQLINE_END:
472 http_msg_rqline_end:
473 /* check for HTTP/0.9 request : no version information
474 * available. sol must point to the first of CR or LF. However
475 * since we don't save these elements between calls, if we come
476 * here from a restart, we don't necessarily know. Thus in this
477 * case we simply start over.
478 */
479 if (restarting)
480 goto restart;
481
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200482 if (unlikely(sl.rq.v.len == 0))
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200483 goto http_msg_last_lf;
484
485 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
486 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
487 /* stop here */
488
489 /*
490 * Common states below
491 */
Willy Tarreau801250e2018-09-11 11:45:04 +0200492 case H1_MSG_RPBEFORE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200493 http_msg_rpbefore:
494 if (likely(HTTP_IS_TOKEN(*ptr))) {
495 /* we have a start of message, we may have skipped some
496 * heading CRLF. Skip them now.
497 */
498 skip += ptr - start;
499 start = ptr;
500
501 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200502 sl.st.v.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200503 hdr_count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +0200504 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200505 goto http_msg_rpver;
506 }
507
508 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200509 state = H1_MSG_RPBEFORE;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200510 goto http_msg_invalid;
511 }
512
513 if (unlikely(*ptr == '\n'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200514 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
515 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 +0200516 /* stop here */
517
Willy Tarreau801250e2018-09-11 11:45:04 +0200518 case H1_MSG_RPBEFORE_CR:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200519 http_msg_rpbefore_cr:
Willy Tarreau801250e2018-09-11 11:45:04 +0200520 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
521 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200522 /* stop here */
523
Willy Tarreau801250e2018-09-11 11:45:04 +0200524 case H1_MSG_RPVER:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200525 http_msg_rpver:
526 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200527 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200528
529 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200530 sl.st.v.len = ptr - sl.st.v.ptr;
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200531
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200532 if ((sl.st.v.len == 8) &&
533 (*(sl.st.v.ptr + 5) > '1' ||
534 (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200535 h1m->flags |= H1_MF_VER_11;
536
Willy Tarreau801250e2018-09-11 11:45:04 +0200537 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 +0200538 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200539 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200540 goto http_msg_invalid;
541
Willy Tarreau801250e2018-09-11 11:45:04 +0200542 case H1_MSG_RPVER_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200543 http_msg_rpver_sp:
544 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200545 sl.st.status = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200546 sl.st.c.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200547 goto http_msg_rpcode;
548 }
549 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200550 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 +0200551 /* so it's a CR/LF, this is invalid */
Willy Tarreau801250e2018-09-11 11:45:04 +0200552 state = H1_MSG_RPVER_SP;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200553 goto http_msg_invalid;
554
Willy Tarreau801250e2018-09-11 11:45:04 +0200555 case H1_MSG_RPCODE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200556 http_msg_rpcode:
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100557 if (likely(HTTP_IS_DIGIT(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200558 sl.st.status = sl.st.status * 10 + *ptr - '0';
Willy Tarreau801250e2018-09-11 11:45:04 +0200559 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200560 }
561
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100562 if (unlikely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200563 state = H1_MSG_RPCODE;
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100564 goto http_msg_invalid;
565 }
566
Willy Tarreau794f9af2017-07-26 09:07:47 +0200567 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200568 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau801250e2018-09-11 11:45:04 +0200569 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 +0200570 }
571
572 /* so it's a CR/LF, so there is no reason phrase */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200573 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200574
575 http_msg_rsp_reason:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200576 sl.st.r.ptr = ptr;
577 sl.st.r.len = 0;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200578 goto http_msg_rpline_eol;
579
Willy Tarreau801250e2018-09-11 11:45:04 +0200580 case H1_MSG_RPCODE_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200581 http_msg_rpcode_sp:
582 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200583 sl.st.r.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200584 goto http_msg_rpreason;
585 }
586 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200587 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 +0200588 /* so it's a CR/LF, so there is no reason phrase */
589 goto http_msg_rsp_reason;
590
Willy Tarreau801250e2018-09-11 11:45:04 +0200591 case H1_MSG_RPREASON:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200592 http_msg_rpreason:
593 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200594 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200595 sl.st.r.len = ptr - sl.st.r.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200596 http_msg_rpline_eol:
597 /* We have seen the end of line. Note that we do not
598 * necessarily have the \n yet, but at least we know that we
599 * have EITHER \r OR \n, otherwise the response would not be
600 * complete. We can then record the response length and return
601 * to the caller which will be able to register it.
602 */
603
Willy Tarreau5384aac2018-09-11 16:04:48 +0200604 if (likely(!skip_update)) {
605 if (unlikely(hdr_count >= hdr_num)) {
606 state = H1_MSG_RPREASON;
607 goto http_output_full;
608 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200609 if (!(h1m->flags & H1_MF_NO_PHDR))
610 http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200611 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200612
613 sol = ptr - start;
614 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200615 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 +0200616 goto http_msg_rpline_end;
617
Willy Tarreau801250e2018-09-11 11:45:04 +0200618 case H1_MSG_RPLINE_END:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200619 http_msg_rpline_end:
620 /* sol must point to the first of CR or LF. */
Willy Tarreau801250e2018-09-11 11:45:04 +0200621 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
622 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 +0200623 /* stop here */
624
Willy Tarreau801250e2018-09-11 11:45:04 +0200625 case H1_MSG_HDR_FIRST:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200626 http_msg_hdr_first:
627 sol = ptr - start;
628 if (likely(!HTTP_IS_CRLF(*ptr))) {
629 goto http_msg_hdr_name;
630 }
631
632 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200633 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 +0200634 goto http_msg_last_lf;
635
Willy Tarreau801250e2018-09-11 11:45:04 +0200636 case H1_MSG_HDR_NAME:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200637 http_msg_hdr_name:
638 /* assumes sol points to the first char */
639 if (likely(HTTP_IS_TOKEN(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200640 if (!skip_update) {
641 /* turn it to lower case if needed */
642 if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
643 *ptr = tolower(*ptr);
644 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200645 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 +0200646 }
647
648 if (likely(*ptr == ':')) {
649 col = ptr - start;
Willy Tarreau801250e2018-09-11 11:45:04 +0200650 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 +0200651 }
652
Willy Tarreau9aec3052018-09-12 09:20:40 +0200653 if (likely(h1m->err_pos < -1) || *ptr == '\n') {
Willy Tarreau801250e2018-09-11 11:45:04 +0200654 state = H1_MSG_HDR_NAME;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200655 goto http_msg_invalid;
656 }
657
Willy Tarreau9aec3052018-09-12 09:20:40 +0200658 if (h1m->err_pos == -1) /* capture the error pointer */
659 h1m->err_pos = ptr - start + skip; /* >= 0 now */
660
661 /* and we still accept this non-token character */
Willy Tarreau801250e2018-09-11 11:45:04 +0200662 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 +0200663
Willy Tarreau801250e2018-09-11 11:45:04 +0200664 case H1_MSG_HDR_L1_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200665 http_msg_hdr_l1_sp:
666 /* assumes sol points to the first char */
667 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200668 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 +0200669
670 /* header value can be basically anything except CR/LF */
671 sov = ptr - start;
672
673 if (likely(!HTTP_IS_CRLF(*ptr))) {
674 goto http_msg_hdr_val;
675 }
676
677 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200678 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 +0200679 goto http_msg_hdr_l1_lf;
680
Willy Tarreau801250e2018-09-11 11:45:04 +0200681 case H1_MSG_HDR_L1_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200682 http_msg_hdr_l1_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200683 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
684 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 +0200685
Willy Tarreau801250e2018-09-11 11:45:04 +0200686 case H1_MSG_HDR_L1_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200687 http_msg_hdr_l1_lws:
688 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200689 if (!skip_update) {
690 /* replace HT,CR,LF with spaces */
691 for (; start + sov < ptr; sov++)
692 start[sov] = ' ';
693 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200694 goto http_msg_hdr_l1_sp;
695 }
696 /* we had a header consisting only in spaces ! */
697 eol = sov;
698 goto http_msg_complete_header;
699
Willy Tarreau801250e2018-09-11 11:45:04 +0200700 case H1_MSG_HDR_VAL:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200701 http_msg_hdr_val:
702 /* assumes sol points to the first char, and sov
703 * points to the first character of the value.
704 */
705
706 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
707 * and lower. In fact since most of the time is spent in the loop, we
708 * also remove the sign bit test so that bytes 0x8e..0x0d break the
709 * loop, but we don't care since they're very rare in header values.
710 */
711#if defined(__x86_64__)
712 while (ptr <= end - sizeof(long)) {
713 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
714 goto http_msg_hdr_val2;
715 ptr += sizeof(long);
716 }
717#endif
718#if defined(__x86_64__) || \
719 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
720 defined(__ARM_ARCH_7A__)
721 while (ptr <= end - sizeof(int)) {
722 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
723 goto http_msg_hdr_val2;
724 ptr += sizeof(int);
725 }
726#endif
727 if (ptr >= end) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200728 state = H1_MSG_HDR_VAL;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200729 goto http_msg_ood;
730 }
731 http_msg_hdr_val2:
732 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200733 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 +0200734
735 eol = ptr - start;
736 /* Note: we could also copy eol into ->eoh so that we have the
737 * real header end in case it ends with lots of LWS, but is this
738 * really needed ?
739 */
740 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200741 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 +0200742 goto http_msg_hdr_l2_lf;
743
Willy Tarreau801250e2018-09-11 11:45:04 +0200744 case H1_MSG_HDR_L2_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200745 http_msg_hdr_l2_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200746 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
747 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 +0200748
Willy Tarreau801250e2018-09-11 11:45:04 +0200749 case H1_MSG_HDR_L2_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200750 http_msg_hdr_l2_lws:
751 if (unlikely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200752 if (!skip_update) {
753 /* LWS: replace HT,CR,LF with spaces */
754 for (; start + eol < ptr; eol++)
755 start[eol] = ' ';
756 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200757 goto http_msg_hdr_val;
758 }
759 http_msg_complete_header:
760 /*
761 * It was a new header, so the last one is finished. Assumes
762 * <sol> points to the first char of the name, <col> to the
763 * colon, <sov> points to the first character of the value and
764 * <eol> to the first CR or LF so we know how the line ends. We
765 * will trim spaces around the value. It's possible to do it by
766 * adjusting <eol> and <sov> which are no more used after this.
767 * We can add the header field to the list.
768 */
Christopher Faulet2912f872018-09-19 14:01:04 +0200769 if (likely(!skip_update)) {
770 while (sov < eol && HTTP_IS_LWS(start[sov]))
771 sov++;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200772
Christopher Faulet2912f872018-09-19 14:01:04 +0200773 while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
774 eol--;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200775
776
Christopher Faulet2912f872018-09-19 14:01:04 +0200777 n = ist2(start + sol, col - sol);
778 v = ist2(start + sov, eol - sov);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200779
Christopher Faulet2912f872018-09-19 14:01:04 +0200780 do {
781 int ret;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200782
Christopher Faulet2912f872018-09-19 14:01:04 +0200783 if (unlikely(hdr_count >= hdr_num)) {
784 state = H1_MSG_HDR_L2_LWS;
785 goto http_output_full;
786 }
Willy Tarreau5384aac2018-09-11 16:04:48 +0200787
Christopher Faulet2912f872018-09-19 14:01:04 +0200788 if (isteqi(n, ist("transfer-encoding"))) {
789 h1_parse_xfer_enc_header(h1m, v);
790 }
791 else if (isteqi(n, ist("content-length"))) {
792 ret = h1_parse_cont_len_header(h1m, &v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200793
Christopher Faulet2912f872018-09-19 14:01:04 +0200794 if (ret < 0) {
795 state = H1_MSG_HDR_L2_LWS;
796 goto http_msg_invalid;
797 }
798 else if (ret == 0) {
799 /* skip it */
800 break;
801 }
Willy Tarreau73373ab2018-09-14 17:11:33 +0200802 }
Christopher Faulet2912f872018-09-19 14:01:04 +0200803 else if (isteqi(n, ist("connection"))) {
804 h1_parse_connection_header(h1m, v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200805 }
Willy Tarreau2ea6bb52018-09-14 16:28:15 +0200806
Christopher Faulet2912f872018-09-19 14:01:04 +0200807 http_set_hdr(&hdr[hdr_count++], n, v);
808 } while (0);
809 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200810
811 sol = ptr - start;
Christopher Faulet2912f872018-09-19 14:01:04 +0200812
Willy Tarreau794f9af2017-07-26 09:07:47 +0200813 if (likely(!HTTP_IS_CRLF(*ptr)))
814 goto http_msg_hdr_name;
815
816 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200817 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 +0200818 goto http_msg_last_lf;
819
Willy Tarreau801250e2018-09-11 11:45:04 +0200820 case H1_MSG_LAST_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200821 http_msg_last_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200822 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200823 ptr++;
824 /* <ptr> now points to the first byte of payload. If needed sol
825 * still points to the first of either CR or LF of the empty
826 * line ending the headers block.
827 */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200828 if (likely(!skip_update)) {
829 if (unlikely(hdr_count >= hdr_num)) {
830 state = H1_MSG_LAST_LF;
831 goto http_output_full;
832 }
Christopher Fauletff08a922018-09-25 13:59:46 +0200833 http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
Willy Tarreau794f9af2017-07-26 09:07:47 +0200834 }
Willy Tarreau001823c2018-09-12 17:25:32 +0200835
836 /* reaching here we've parsed the whole message. We may detect
837 * that we were already continuing an interrupted parsing pass
838 * so we were silently looking for the end of message not
839 * updating anything before deciding to parse it fully at once.
840 * It's guaranteed that we won't match this test twice in a row
841 * since restarting will turn zero.
842 */
843 if (restarting)
844 goto restart;
845
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200846 state = H1_MSG_DATA;
847 if (h1m->flags & H1_MF_XFER_ENC) {
848 if (h1m->flags & H1_MF_CLEN) {
849 h1m->flags &= ~H1_MF_CLEN;
850 hdr_count = http_del_hdr(hdr, ist("content-length"));
851 }
852
853 if (h1m->flags & H1_MF_CHNK)
854 state = H1_MSG_CHUNK_SIZE;
855 else if (!(h1m->flags & H1_MF_RESP)) {
856 /* cf RFC7230#3.3.3 : transfer-encoding in
857 * request without chunked encoding is invalid.
858 */
859 goto http_msg_invalid;
860 }
861 }
862
Willy Tarreau794f9af2017-07-26 09:07:47 +0200863 break;
864
865 default:
866 /* impossible states */
867 goto http_msg_invalid;
868 }
869
Willy Tarreau001823c2018-09-12 17:25:32 +0200870 /* Now we've left the headers state and are either in H1_MSG_DATA or
871 * H1_MSG_CHUNK_SIZE.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200872 */
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200873
Willy Tarreau5384aac2018-09-11 16:04:48 +0200874 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200875 *slp = sl;
876
Willy Tarreau4433c082018-09-11 15:33:32 +0200877 h1m->state = state;
878 h1m->next = ptr - start + skip;
879 return h1m->next;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200880
881 http_msg_ood:
882 /* out of data at <ptr> during state <state> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200883 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200884 *slp = sl;
885
Willy Tarreau4433c082018-09-11 15:33:32 +0200886 h1m->state = state;
887 h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200888 return 0;
889
890 http_msg_invalid:
891 /* invalid message, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200892 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200893 *slp = sl;
894
Willy Tarreau4433c082018-09-11 15:33:32 +0200895 h1m->err_state = h1m->state = state;
896 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200897 return -1;
898
899 http_output_full:
900 /* no more room to store the current header, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200901 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200902 *slp = sl;
903
Willy Tarreau4433c082018-09-11 15:33:32 +0200904 h1m->err_state = h1m->state = state;
905 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200906 return -2;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200907
908 restart:
909 h1m->next = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200910 if (h1m->flags & H1_MF_RESP)
911 h1m->state = H1_MSG_RPBEFORE;
912 else
913 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200914 goto try_again;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200915}
916
Willy Tarreau2510f702017-10-31 17:14:16 +0100917/* This function performs a very minimal parsing of the trailers block present
Willy Tarreauf40e6822018-06-14 16:52:02 +0200918 * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of
Willy Tarreau7314be82018-06-14 13:32:50 +0200919 * bytes to delete to skip the trailers. It may return 0 if it's missing some
920 * input data, or < 0 in case of parse error (in which case the caller may have
921 * to decide how to proceed, possibly eating everything).
Willy Tarreau2510f702017-10-31 17:14:16 +0100922 */
Willy Tarreauf40e6822018-06-14 16:52:02 +0200923int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max)
Willy Tarreau2510f702017-10-31 17:14:16 +0100924{
Willy Tarreauf40e6822018-06-14 16:52:02 +0200925 const char *stop = b_peek(buf, ofs + max);
926 int count = ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +0100927
928 while (1) {
929 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau7314be82018-06-14 13:32:50 +0200930 const char *start = b_peek(buf, count);
Willy Tarreau2510f702017-10-31 17:14:16 +0100931 const char *ptr = start;
Willy Tarreau2510f702017-10-31 17:14:16 +0100932
933 /* scan current line and stop at LF or CRLF */
934 while (1) {
935 if (ptr == stop)
936 return 0;
937
938 if (*ptr == '\n') {
939 if (!p1)
940 p1 = ptr;
941 p2 = ptr;
942 break;
943 }
944
945 if (*ptr == '\r') {
946 if (p1)
947 return -1;
948 p1 = ptr;
949 }
950
Willy Tarreau7314be82018-06-14 13:32:50 +0200951 ptr = b_next(buf, ptr);
Willy Tarreau2510f702017-10-31 17:14:16 +0100952 }
953
954 /* after LF; point to beginning of next line */
Willy Tarreau7314be82018-06-14 13:32:50 +0200955 p2 = b_next(buf, p2);
956 count += b_dist(buf, start, p2);
Willy Tarreau2510f702017-10-31 17:14:16 +0100957
958 /* LF/CRLF at beginning of line => end of trailers at p2.
959 * Everything was scheduled for forwarding, there's nothing left
960 * from this message. */
961 if (p1 == start)
962 break;
963 /* OK, next line then */
964 }
Willy Tarreauf40e6822018-06-14 16:52:02 +0200965 return count - ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +0100966}