blob: 38d13c6c33beae02b1cf517b0af6fcd6c062d7c2 [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 }
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100275 else {
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100276 state = h1m->state;
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100277 if (h1m->state != H1_MSG_RQBEFORE && h1m->state != H1_MSG_RPBEFORE)
278 restarting = 1;
279 }
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100280
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200281 ptr = start + h1m->next;
282 end = stop;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200283
Willy Tarreau794f9af2017-07-26 09:07:47 +0200284 if (unlikely(ptr >= end))
285 goto http_msg_ood;
286
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200287 /* don't update output if hdr is NULL or if we're restarting */
288 if (!hdr || restarting)
Willy Tarreau5384aac2018-09-11 16:04:48 +0200289 skip_update = 1;
290
Willy Tarreau794f9af2017-07-26 09:07:47 +0200291 switch (state) {
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200292 case H1_MSG_RQBEFORE:
293 http_msg_rqbefore:
294 if (likely(HTTP_IS_TOKEN(*ptr))) {
295 /* we have a start of message, we may have skipped some
296 * heading CRLF. Skip them now.
297 */
298 skip += ptr - start;
299 start = ptr;
300
301 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200302 sl.rq.m.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200303 hdr_count = 0;
304 state = H1_MSG_RQMETH;
305 goto http_msg_rqmeth;
306 }
307
308 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
309 state = H1_MSG_RQBEFORE;
310 goto http_msg_invalid;
311 }
312
313 if (unlikely(*ptr == '\n'))
314 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
315 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
316 /* stop here */
317
318 case H1_MSG_RQBEFORE_CR:
319 http_msg_rqbefore_cr:
320 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
321 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
322 /* stop here */
323
324 case H1_MSG_RQMETH:
325 http_msg_rqmeth:
326 if (likely(HTTP_IS_TOKEN(*ptr)))
327 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
328
329 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200330 sl.rq.m.len = ptr - sl.rq.m.ptr;
331 sl.rq.meth = find_http_meth(start, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200332 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
333 }
334
335 if (likely(HTTP_IS_CRLF(*ptr))) {
336 /* HTTP 0.9 request */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200337 sl.rq.m.len = ptr - sl.rq.m.ptr;
338 sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200339 http_msg_req09_uri:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200340 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200341 http_msg_req09_uri_e:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200342 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200343 http_msg_req09_ver:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200344 sl.rq.v.ptr = ptr;
345 sl.rq.v.len = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200346 goto http_msg_rqline_eol;
347 }
348 state = H1_MSG_RQMETH;
349 goto http_msg_invalid;
350
351 case H1_MSG_RQMETH_SP:
352 http_msg_rqmeth_sp:
353 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200354 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200355 goto http_msg_rquri;
356 }
357 if (likely(HTTP_IS_SPHT(*ptr)))
358 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
359 /* so it's a CR/LF, meaning an HTTP 0.9 request */
360 goto http_msg_req09_uri;
361
362 case H1_MSG_RQURI:
363 http_msg_rquri:
364#if defined(__x86_64__) || \
365 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
366 defined(__ARM_ARCH_7A__)
367 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
368 while (ptr <= end - sizeof(int)) {
369 int x = *(int *)ptr - 0x21212121;
370 if (x & 0x80808080)
371 break;
372
373 x -= 0x5e5e5e5e;
374 if (!(x & 0x80808080))
375 break;
376
377 ptr += sizeof(int);
378 }
379#endif
380 if (ptr >= end) {
381 state = H1_MSG_RQURI;
382 goto http_msg_ood;
383 }
384 http_msg_rquri2:
385 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
386 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
387
388 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200389 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200390 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
391 }
392 if (likely((unsigned char)*ptr >= 128)) {
393 /* non-ASCII chars are forbidden unless option
394 * accept-invalid-http-request is enabled in the frontend.
395 * In any case, we capture the faulty char.
396 */
397 if (h1m->err_pos < -1)
398 goto invalid_char;
399 if (h1m->err_pos == -1)
400 h1m->err_pos = ptr - start + skip;
401 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
402 }
403
404 if (likely(HTTP_IS_CRLF(*ptr))) {
405 /* so it's a CR/LF, meaning an HTTP 0.9 request */
406 goto http_msg_req09_uri_e;
407 }
408
409 /* OK forbidden chars, 0..31 or 127 */
410 invalid_char:
411 state = H1_MSG_RQURI;
412 goto http_msg_invalid;
413
414 case H1_MSG_RQURI_SP:
415 http_msg_rquri_sp:
416 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200417 sl.rq.v.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200418 goto http_msg_rqver;
419 }
420 if (likely(HTTP_IS_SPHT(*ptr)))
421 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
422 /* so it's a CR/LF, meaning an HTTP 0.9 request */
423 goto http_msg_req09_ver;
424
425
426 case H1_MSG_RQVER:
427 http_msg_rqver:
428 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
429 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
430
431 if (likely(HTTP_IS_CRLF(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200432 sl.rq.v.len = ptr - sl.rq.v.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200433 http_msg_rqline_eol:
434 /* We have seen the end of line. Note that we do not
435 * necessarily have the \n yet, but at least we know that we
436 * have EITHER \r OR \n, otherwise the request would not be
437 * complete. We can then record the request length and return
438 * to the caller which will be able to register it.
439 */
440
441 if (likely(!skip_update)) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200442 if ((sl.rq.v.len == 8) &&
443 (*(sl.rq.v.ptr + 5) > '1' ||
444 (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200445 h1m->flags |= H1_MF_VER_11;
446
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200447 if (unlikely(hdr_count >= hdr_num)) {
448 state = H1_MSG_RQVER;
449 goto http_output_full;
450 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200451 if (!(h1m->flags & H1_MF_NO_PHDR))
452 http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200453
454 if (unlikely(hdr_count >= hdr_num)) {
455 state = H1_MSG_RQVER;
456 goto http_output_full;
457 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200458 if (!(h1m->flags & H1_MF_NO_PHDR))
459 http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200460 }
461
462 sol = ptr - start;
463 if (likely(*ptr == '\r'))
464 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
465 goto http_msg_rqline_end;
466 }
467
468 /* neither an HTTP_VER token nor a CRLF */
469 state = H1_MSG_RQVER;
470 goto http_msg_invalid;
471
472 case H1_MSG_RQLINE_END:
473 http_msg_rqline_end:
474 /* check for HTTP/0.9 request : no version information
475 * available. sol must point to the first of CR or LF. However
476 * since we don't save these elements between calls, if we come
477 * here from a restart, we don't necessarily know. Thus in this
478 * case we simply start over.
479 */
480 if (restarting)
481 goto restart;
482
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200483 if (unlikely(sl.rq.v.len == 0))
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200484 goto http_msg_last_lf;
485
486 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
487 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
488 /* stop here */
489
490 /*
491 * Common states below
492 */
Willy Tarreau801250e2018-09-11 11:45:04 +0200493 case H1_MSG_RPBEFORE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200494 http_msg_rpbefore:
495 if (likely(HTTP_IS_TOKEN(*ptr))) {
496 /* we have a start of message, we may have skipped some
497 * heading CRLF. Skip them now.
498 */
499 skip += ptr - start;
500 start = ptr;
501
502 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200503 sl.st.v.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200504 hdr_count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +0200505 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200506 goto http_msg_rpver;
507 }
508
509 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200510 state = H1_MSG_RPBEFORE;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200511 goto http_msg_invalid;
512 }
513
514 if (unlikely(*ptr == '\n'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200515 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
516 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 +0200517 /* stop here */
518
Willy Tarreau801250e2018-09-11 11:45:04 +0200519 case H1_MSG_RPBEFORE_CR:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200520 http_msg_rpbefore_cr:
Willy Tarreau801250e2018-09-11 11:45:04 +0200521 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
522 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200523 /* stop here */
524
Willy Tarreau801250e2018-09-11 11:45:04 +0200525 case H1_MSG_RPVER:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200526 http_msg_rpver:
527 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200528 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200529
530 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200531 sl.st.v.len = ptr - sl.st.v.ptr;
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200532
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200533 if ((sl.st.v.len == 8) &&
534 (*(sl.st.v.ptr + 5) > '1' ||
535 (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200536 h1m->flags |= H1_MF_VER_11;
537
Willy Tarreau801250e2018-09-11 11:45:04 +0200538 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 +0200539 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200540 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200541 goto http_msg_invalid;
542
Willy Tarreau801250e2018-09-11 11:45:04 +0200543 case H1_MSG_RPVER_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200544 http_msg_rpver_sp:
545 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200546 sl.st.status = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200547 sl.st.c.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200548 goto http_msg_rpcode;
549 }
550 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200551 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 +0200552 /* so it's a CR/LF, this is invalid */
Willy Tarreau801250e2018-09-11 11:45:04 +0200553 state = H1_MSG_RPVER_SP;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200554 goto http_msg_invalid;
555
Willy Tarreau801250e2018-09-11 11:45:04 +0200556 case H1_MSG_RPCODE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200557 http_msg_rpcode:
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100558 if (likely(HTTP_IS_DIGIT(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200559 sl.st.status = sl.st.status * 10 + *ptr - '0';
Willy Tarreau801250e2018-09-11 11:45:04 +0200560 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200561 }
562
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100563 if (unlikely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200564 state = H1_MSG_RPCODE;
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100565 goto http_msg_invalid;
566 }
567
Willy Tarreau794f9af2017-07-26 09:07:47 +0200568 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200569 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau801250e2018-09-11 11:45:04 +0200570 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 +0200571 }
572
573 /* so it's a CR/LF, so there is no reason phrase */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200574 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200575
576 http_msg_rsp_reason:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200577 sl.st.r.ptr = ptr;
578 sl.st.r.len = 0;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200579 goto http_msg_rpline_eol;
580
Willy Tarreau801250e2018-09-11 11:45:04 +0200581 case H1_MSG_RPCODE_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200582 http_msg_rpcode_sp:
583 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200584 sl.st.r.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200585 goto http_msg_rpreason;
586 }
587 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200588 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 +0200589 /* so it's a CR/LF, so there is no reason phrase */
590 goto http_msg_rsp_reason;
591
Willy Tarreau801250e2018-09-11 11:45:04 +0200592 case H1_MSG_RPREASON:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200593 http_msg_rpreason:
594 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200595 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200596 sl.st.r.len = ptr - sl.st.r.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200597 http_msg_rpline_eol:
598 /* We have seen the end of line. Note that we do not
599 * necessarily have the \n yet, but at least we know that we
600 * have EITHER \r OR \n, otherwise the response would not be
601 * complete. We can then record the response length and return
602 * to the caller which will be able to register it.
603 */
604
Willy Tarreau5384aac2018-09-11 16:04:48 +0200605 if (likely(!skip_update)) {
606 if (unlikely(hdr_count >= hdr_num)) {
607 state = H1_MSG_RPREASON;
608 goto http_output_full;
609 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200610 if (!(h1m->flags & H1_MF_NO_PHDR))
611 http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200612 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200613
614 sol = ptr - start;
615 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200616 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 +0200617 goto http_msg_rpline_end;
618
Willy Tarreau801250e2018-09-11 11:45:04 +0200619 case H1_MSG_RPLINE_END:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200620 http_msg_rpline_end:
621 /* sol must point to the first of CR or LF. */
Willy Tarreau801250e2018-09-11 11:45:04 +0200622 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
623 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 +0200624 /* stop here */
625
Willy Tarreau801250e2018-09-11 11:45:04 +0200626 case H1_MSG_HDR_FIRST:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200627 http_msg_hdr_first:
628 sol = ptr - start;
629 if (likely(!HTTP_IS_CRLF(*ptr))) {
630 goto http_msg_hdr_name;
631 }
632
633 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200634 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 +0200635 goto http_msg_last_lf;
636
Willy Tarreau801250e2018-09-11 11:45:04 +0200637 case H1_MSG_HDR_NAME:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200638 http_msg_hdr_name:
639 /* assumes sol points to the first char */
640 if (likely(HTTP_IS_TOKEN(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200641 if (!skip_update) {
642 /* turn it to lower case if needed */
643 if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
644 *ptr = tolower(*ptr);
645 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200646 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 +0200647 }
648
649 if (likely(*ptr == ':')) {
650 col = ptr - start;
Willy Tarreau801250e2018-09-11 11:45:04 +0200651 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 +0200652 }
653
Willy Tarreau9aec3052018-09-12 09:20:40 +0200654 if (likely(h1m->err_pos < -1) || *ptr == '\n') {
Willy Tarreau801250e2018-09-11 11:45:04 +0200655 state = H1_MSG_HDR_NAME;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200656 goto http_msg_invalid;
657 }
658
Willy Tarreau9aec3052018-09-12 09:20:40 +0200659 if (h1m->err_pos == -1) /* capture the error pointer */
660 h1m->err_pos = ptr - start + skip; /* >= 0 now */
661
662 /* and we still accept this non-token character */
Willy Tarreau801250e2018-09-11 11:45:04 +0200663 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 +0200664
Willy Tarreau801250e2018-09-11 11:45:04 +0200665 case H1_MSG_HDR_L1_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200666 http_msg_hdr_l1_sp:
667 /* assumes sol points to the first char */
668 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200669 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 +0200670
671 /* header value can be basically anything except CR/LF */
672 sov = ptr - start;
673
674 if (likely(!HTTP_IS_CRLF(*ptr))) {
675 goto http_msg_hdr_val;
676 }
677
678 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200679 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 +0200680 goto http_msg_hdr_l1_lf;
681
Willy Tarreau801250e2018-09-11 11:45:04 +0200682 case H1_MSG_HDR_L1_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200683 http_msg_hdr_l1_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200684 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
685 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 +0200686
Willy Tarreau801250e2018-09-11 11:45:04 +0200687 case H1_MSG_HDR_L1_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200688 http_msg_hdr_l1_lws:
689 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200690 if (!skip_update) {
691 /* replace HT,CR,LF with spaces */
692 for (; start + sov < ptr; sov++)
693 start[sov] = ' ';
694 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200695 goto http_msg_hdr_l1_sp;
696 }
697 /* we had a header consisting only in spaces ! */
698 eol = sov;
699 goto http_msg_complete_header;
700
Willy Tarreau801250e2018-09-11 11:45:04 +0200701 case H1_MSG_HDR_VAL:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200702 http_msg_hdr_val:
703 /* assumes sol points to the first char, and sov
704 * points to the first character of the value.
705 */
706
707 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
708 * and lower. In fact since most of the time is spent in the loop, we
709 * also remove the sign bit test so that bytes 0x8e..0x0d break the
710 * loop, but we don't care since they're very rare in header values.
711 */
712#if defined(__x86_64__)
713 while (ptr <= end - sizeof(long)) {
714 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
715 goto http_msg_hdr_val2;
716 ptr += sizeof(long);
717 }
718#endif
719#if defined(__x86_64__) || \
720 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
721 defined(__ARM_ARCH_7A__)
722 while (ptr <= end - sizeof(int)) {
723 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
724 goto http_msg_hdr_val2;
725 ptr += sizeof(int);
726 }
727#endif
728 if (ptr >= end) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200729 state = H1_MSG_HDR_VAL;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200730 goto http_msg_ood;
731 }
732 http_msg_hdr_val2:
733 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200734 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 +0200735
736 eol = ptr - start;
737 /* Note: we could also copy eol into ->eoh so that we have the
738 * real header end in case it ends with lots of LWS, but is this
739 * really needed ?
740 */
741 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200742 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 +0200743 goto http_msg_hdr_l2_lf;
744
Willy Tarreau801250e2018-09-11 11:45:04 +0200745 case H1_MSG_HDR_L2_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200746 http_msg_hdr_l2_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200747 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
748 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 +0200749
Willy Tarreau801250e2018-09-11 11:45:04 +0200750 case H1_MSG_HDR_L2_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200751 http_msg_hdr_l2_lws:
752 if (unlikely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200753 if (!skip_update) {
754 /* LWS: replace HT,CR,LF with spaces */
755 for (; start + eol < ptr; eol++)
756 start[eol] = ' ';
757 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200758 goto http_msg_hdr_val;
759 }
760 http_msg_complete_header:
761 /*
762 * It was a new header, so the last one is finished. Assumes
763 * <sol> points to the first char of the name, <col> to the
764 * colon, <sov> points to the first character of the value and
765 * <eol> to the first CR or LF so we know how the line ends. We
766 * will trim spaces around the value. It's possible to do it by
767 * adjusting <eol> and <sov> which are no more used after this.
768 * We can add the header field to the list.
769 */
Christopher Faulet2912f872018-09-19 14:01:04 +0200770 if (likely(!skip_update)) {
771 while (sov < eol && HTTP_IS_LWS(start[sov]))
772 sov++;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200773
Christopher Faulet2912f872018-09-19 14:01:04 +0200774 while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
775 eol--;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200776
777
Christopher Faulet2912f872018-09-19 14:01:04 +0200778 n = ist2(start + sol, col - sol);
779 v = ist2(start + sov, eol - sov);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200780
Christopher Faulet2912f872018-09-19 14:01:04 +0200781 do {
782 int ret;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200783
Christopher Faulet2912f872018-09-19 14:01:04 +0200784 if (unlikely(hdr_count >= hdr_num)) {
785 state = H1_MSG_HDR_L2_LWS;
786 goto http_output_full;
787 }
Willy Tarreau5384aac2018-09-11 16:04:48 +0200788
Christopher Faulet2912f872018-09-19 14:01:04 +0200789 if (isteqi(n, ist("transfer-encoding"))) {
790 h1_parse_xfer_enc_header(h1m, v);
791 }
792 else if (isteqi(n, ist("content-length"))) {
793 ret = h1_parse_cont_len_header(h1m, &v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200794
Christopher Faulet2912f872018-09-19 14:01:04 +0200795 if (ret < 0) {
796 state = H1_MSG_HDR_L2_LWS;
797 goto http_msg_invalid;
798 }
799 else if (ret == 0) {
800 /* skip it */
801 break;
802 }
Willy Tarreau73373ab2018-09-14 17:11:33 +0200803 }
Christopher Faulet2912f872018-09-19 14:01:04 +0200804 else if (isteqi(n, ist("connection"))) {
805 h1_parse_connection_header(h1m, v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200806 }
Willy Tarreau2ea6bb52018-09-14 16:28:15 +0200807
Christopher Faulet2912f872018-09-19 14:01:04 +0200808 http_set_hdr(&hdr[hdr_count++], n, v);
809 } while (0);
810 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200811
812 sol = ptr - start;
Christopher Faulet2912f872018-09-19 14:01:04 +0200813
Willy Tarreau794f9af2017-07-26 09:07:47 +0200814 if (likely(!HTTP_IS_CRLF(*ptr)))
815 goto http_msg_hdr_name;
816
817 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200818 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 +0200819 goto http_msg_last_lf;
820
Willy Tarreau801250e2018-09-11 11:45:04 +0200821 case H1_MSG_LAST_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200822 http_msg_last_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200823 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200824 ptr++;
825 /* <ptr> now points to the first byte of payload. If needed sol
826 * still points to the first of either CR or LF of the empty
827 * line ending the headers block.
828 */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200829 if (likely(!skip_update)) {
830 if (unlikely(hdr_count >= hdr_num)) {
831 state = H1_MSG_LAST_LF;
832 goto http_output_full;
833 }
Christopher Fauletff08a922018-09-25 13:59:46 +0200834 http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
Willy Tarreau794f9af2017-07-26 09:07:47 +0200835 }
Willy Tarreau001823c2018-09-12 17:25:32 +0200836
837 /* reaching here we've parsed the whole message. We may detect
838 * that we were already continuing an interrupted parsing pass
839 * so we were silently looking for the end of message not
840 * updating anything before deciding to parse it fully at once.
841 * It's guaranteed that we won't match this test twice in a row
842 * since restarting will turn zero.
843 */
844 if (restarting)
845 goto restart;
846
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200847 state = H1_MSG_DATA;
848 if (h1m->flags & H1_MF_XFER_ENC) {
849 if (h1m->flags & H1_MF_CLEN) {
850 h1m->flags &= ~H1_MF_CLEN;
851 hdr_count = http_del_hdr(hdr, ist("content-length"));
852 }
853
854 if (h1m->flags & H1_MF_CHNK)
855 state = H1_MSG_CHUNK_SIZE;
856 else if (!(h1m->flags & H1_MF_RESP)) {
857 /* cf RFC7230#3.3.3 : transfer-encoding in
858 * request without chunked encoding is invalid.
859 */
860 goto http_msg_invalid;
861 }
862 }
863
Willy Tarreau794f9af2017-07-26 09:07:47 +0200864 break;
865
866 default:
867 /* impossible states */
868 goto http_msg_invalid;
869 }
870
Willy Tarreau001823c2018-09-12 17:25:32 +0200871 /* Now we've left the headers state and are either in H1_MSG_DATA or
872 * H1_MSG_CHUNK_SIZE.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200873 */
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200874
Willy Tarreau5384aac2018-09-11 16:04:48 +0200875 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200876 *slp = sl;
877
Willy Tarreau4433c082018-09-11 15:33:32 +0200878 h1m->state = state;
879 h1m->next = ptr - start + skip;
880 return h1m->next;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200881
882 http_msg_ood:
883 /* out of data at <ptr> during state <state> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200884 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200885 *slp = sl;
886
Willy Tarreau4433c082018-09-11 15:33:32 +0200887 h1m->state = state;
888 h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200889 return 0;
890
891 http_msg_invalid:
892 /* invalid message, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200893 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200894 *slp = sl;
895
Willy Tarreau4433c082018-09-11 15:33:32 +0200896 h1m->err_state = h1m->state = state;
897 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200898 return -1;
899
900 http_output_full:
901 /* no more room to store the current header, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200902 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200903 *slp = sl;
904
Willy Tarreau4433c082018-09-11 15:33:32 +0200905 h1m->err_state = h1m->state = state;
906 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200907 return -2;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200908
909 restart:
910 h1m->next = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200911 if (h1m->flags & H1_MF_RESP)
912 h1m->state = H1_MSG_RPBEFORE;
913 else
914 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200915 goto try_again;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200916}
917
Willy Tarreau2510f702017-10-31 17:14:16 +0100918/* This function performs a very minimal parsing of the trailers block present
Willy Tarreauf40e6822018-06-14 16:52:02 +0200919 * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of
Willy Tarreau7314be82018-06-14 13:32:50 +0200920 * bytes to delete to skip the trailers. It may return 0 if it's missing some
921 * input data, or < 0 in case of parse error (in which case the caller may have
922 * to decide how to proceed, possibly eating everything).
Willy Tarreau2510f702017-10-31 17:14:16 +0100923 */
Willy Tarreauf40e6822018-06-14 16:52:02 +0200924int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max)
Willy Tarreau2510f702017-10-31 17:14:16 +0100925{
Willy Tarreauf40e6822018-06-14 16:52:02 +0200926 const char *stop = b_peek(buf, ofs + max);
927 int count = ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +0100928
929 while (1) {
930 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau7314be82018-06-14 13:32:50 +0200931 const char *start = b_peek(buf, count);
Willy Tarreau2510f702017-10-31 17:14:16 +0100932 const char *ptr = start;
Willy Tarreau2510f702017-10-31 17:14:16 +0100933
934 /* scan current line and stop at LF or CRLF */
935 while (1) {
936 if (ptr == stop)
937 return 0;
938
939 if (*ptr == '\n') {
940 if (!p1)
941 p1 = ptr;
942 p2 = ptr;
943 break;
944 }
945
946 if (*ptr == '\r') {
947 if (p1)
948 return -1;
949 p1 = ptr;
950 }
951
Willy Tarreau7314be82018-06-14 13:32:50 +0200952 ptr = b_next(buf, ptr);
Willy Tarreau2510f702017-10-31 17:14:16 +0100953 }
954
955 /* after LF; point to beginning of next line */
Willy Tarreau7314be82018-06-14 13:32:50 +0200956 p2 = b_next(buf, p2);
957 count += b_dist(buf, start, p2);
Willy Tarreau2510f702017-10-31 17:14:16 +0100958
959 /* LF/CRLF at beginning of line => end of trailers at p2.
960 * Everything was scheduled for forwarding, there's nothing left
961 * from this message. */
962 if (p1 == start)
963 break;
964 /* OK, next line then */
965 }
Willy Tarreauf40e6822018-06-14 16:52:02 +0200966 return count - ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +0100967}