blob: 3a6c1c3309d677a54564dfd2c91adbb090e574c2 [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>
Amaury Denoyellec1938232020-12-11 17:53:03 +010014
15#include <import/sha1.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Amaury Denoyellec1938232020-12-11 17:53:03 +010018#include <haproxy/base64.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020019#include <haproxy/h1.h>
Willy Tarreau0017be02020-06-02 19:25:28 +020020#include <haproxy/http-hdr.h>
Amaury Denoyelleaad333a2020-12-11 17:53:07 +010021#include <haproxy/tools.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020022
Willy Tarreau73373ab2018-09-14 17:11:33 +020023/* Parse the Content-Length header field of an HTTP/1 request. The function
24 * checks all possible occurrences of a comma-delimited value, and verifies
25 * if any of them doesn't match a previous value. It returns <0 if a value
26 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
27 * if the value can be indexed (first one). In the last case, the value might
28 * be adjusted and the caller must only add the updated value.
29 */
30int h1_parse_cont_len_header(struct h1m *h1m, struct ist *value)
31{
32 char *e, *n;
33 long long cl;
34 int not_first = !!(h1m->flags & H1_MF_CLEN);
35 struct ist word;
36
37 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
38 e = value->ptr + value->len;
39
40 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +050041 /* skip leading delimiter and blanks */
Willy Tarreau73373ab2018-09-14 17:11:33 +020042 if (unlikely(HTTP_IS_LWS(*word.ptr)))
43 continue;
44
45 /* digits only now */
46 for (cl = 0, n = word.ptr; n < e; n++) {
47 unsigned int c = *n - '0';
48 if (unlikely(c > 9)) {
49 /* non-digit */
50 if (unlikely(n == word.ptr)) // spaces only
51 goto fail;
52 break;
53 }
54 if (unlikely(cl > ULLONG_MAX / 10ULL))
55 goto fail; /* multiply overflow */
56 cl = cl * 10ULL;
57 if (unlikely(cl + c < cl))
58 goto fail; /* addition overflow */
59 cl = cl + c;
60 }
61
62 /* keep a copy of the exact cleaned value */
63 word.len = n - word.ptr;
64
65 /* skip trailing LWS till next comma or EOL */
66 for (; n < e; n++) {
67 if (!HTTP_IS_LWS(*n)) {
68 if (unlikely(*n != ','))
69 goto fail;
70 break;
71 }
72 }
73
74 /* if duplicate, must be equal */
75 if (h1m->flags & H1_MF_CLEN && cl != h1m->body_len)
76 goto fail;
77
78 /* OK, store this result as the one to be indexed */
79 h1m->flags |= H1_MF_CLEN;
80 h1m->curr_len = h1m->body_len = cl;
81 *value = word;
82 word.ptr = n;
83 }
84 /* here we've reached the end with a single value or a series of
85 * identical values, all matching previous series if any. The last
86 * parsed value was sent back into <value>. We just have to decide
87 * if this occurrence has to be indexed (it's the first one) or
88 * silently skipped (it's not the first one)
89 */
90 return !not_first;
91 fail:
92 return -1;
93}
94
Willy Tarreau2557f6a2018-09-14 16:34:47 +020095/* Parse the Transfer-Encoding: header field of an HTTP/1 request, looking for
96 * "chunked" being the last value, and setting H1_MF_CHNK in h1m->flags only in
97 * this case. Any other token found or any empty header field found will reset
98 * this flag, so that it accurately represents the token's presence at the last
99 * position. The H1_MF_XFER_ENC flag is always set. Note that transfer codings
100 * are case-insensitive (cf RFC7230#4).
101 */
102void h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value)
103{
104 char *e, *n;
105 struct ist word;
106
107 h1m->flags |= H1_MF_XFER_ENC;
108 h1m->flags &= ~H1_MF_CHNK;
109
110 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
111 e = value.ptr + value.len;
112
113 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500114 /* skip leading delimiter and blanks */
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200115 if (HTTP_IS_LWS(*word.ptr))
116 continue;
117
118 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
119 word.len = n - word.ptr;
120
121 /* trim trailing blanks */
122 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
123 word.len--;
124
125 h1m->flags &= ~H1_MF_CHNK;
126 if (isteqi(word, ist("chunked")))
127 h1m->flags |= H1_MF_CHNK;
128
129 word.ptr = n;
130 }
131}
132
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200133/* Parse the Connection: header of an HTTP/1 request, looking for "close",
134 * "keep-alive", and "upgrade" values, and updating h1m->flags according to
135 * what was found there. Note that flags are only added, not removed, so the
136 * function is safe for being called multiple times if multiple occurrences
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100137 * are found. If the flag H1_MF_CLEAN_CONN_HDR, the header value is cleaned
138 * up from "keep-alive" and "close" values. To do so, the header value is
139 * rewritten in place and its length is updated.
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200140 */
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100141void h1_parse_connection_header(struct h1m *h1m, struct ist *value)
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200142{
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100143 char *e, *n, *p;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200144 struct ist word;
145
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100146 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
147 p = value->ptr;
148 e = value->ptr + value->len;
149 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
150 value->len = 0;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200151
152 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500153 /* skip leading delimiter and blanks */
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200154 if (HTTP_IS_LWS(*word.ptr))
155 continue;
156
157 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
158 word.len = n - word.ptr;
159
160 /* trim trailing blanks */
161 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
162 word.len--;
163
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100164 if (isteqi(word, ist("keep-alive"))) {
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200165 h1m->flags |= H1_MF_CONN_KAL;
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100166 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
167 goto skip_val;
168 }
169 else if (isteqi(word, ist("close"))) {
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200170 h1m->flags |= H1_MF_CONN_CLO;
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100171 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
172 goto skip_val;
173 }
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200174 else if (isteqi(word, ist("upgrade")))
175 h1m->flags |= H1_MF_CONN_UPG;
176
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100177 if (h1m->flags & H1_MF_CLEAN_CONN_HDR) {
178 if (value->ptr + value->len == p) {
179 /* no rewrite done till now */
180 value->len = n - value->ptr;
181 }
182 else {
183 if (value->len)
184 value->ptr[value->len++] = ',';
185 istcat(value, word, e - value->ptr);
186 }
187 }
188
189 skip_val:
190 word.ptr = p = n;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200191 }
192}
193
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +0100194/* Parse the Upgrade: header of an HTTP/1 request.
195 * If "websocket" is found, set H1_MF_UPG_WEBSOCKET flag
196 */
197void h1_parse_upgrade_header(struct h1m *h1m, struct ist value)
198{
199 char *e, *n;
200 struct ist word;
201
202 h1m->flags &= ~H1_MF_UPG_WEBSOCKET;
203
204 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
205 e = value.ptr + value.len;
206
207 while (++word.ptr < e) {
208 /* skip leading delimiter and blanks */
209 if (HTTP_IS_LWS(*word.ptr))
210 continue;
211
212 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
213 word.len = n - word.ptr;
214
215 /* trim trailing blanks */
216 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
217 word.len--;
218
219 if (isteqi(word, ist("websocket")))
220 h1m->flags |= H1_MF_UPG_WEBSOCKET;
221
222 word.ptr = n;
223 }
224}
225
Willy Tarreau538746a2018-12-11 10:59:20 +0100226/* Macros used in the HTTP/1 parser, to check for the expected presence of
227 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
228 */
229
230/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
231 * <bad>.
232 */
233#define EXPECT_LF_HERE(ptr, bad, state, where) \
234 do { \
235 if (unlikely(*(ptr) != '\n')) { \
236 state = (where); \
237 goto bad; \
238 } \
239 } while (0)
240
241/* Increments pointer <ptr>, continues to label <more> if it's still below
242 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
243 * of buffer was reached.
244 */
245#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
246 do { \
247 if (likely(++(ptr) < (end))) \
248 goto more; \
249 else { \
250 state = (where); \
251 goto stop; \
252 } \
253 } while (0)
254
Willy Tarreau794f9af2017-07-26 09:07:47 +0200255/* This function parses a contiguous HTTP/1 headers block starting at <start>
256 * and ending before <stop>, at once, and converts it a list of (name,value)
257 * pairs representing header fields into the array <hdr> of size <hdr_num>,
258 * whose last entry will have an empty name and an empty value. If <hdr_num> is
Willy Tarreau4433c082018-09-11 15:33:32 +0200259 * too small to represent the whole message, an error is returned. Some
260 * protocol elements such as content-length and transfer-encoding will be
Willy Tarreau5384aac2018-09-11 16:04:48 +0200261 * parsed and stored into h1m as well. <hdr> may be null, in which case only
262 * the parsing state will be updated. This may be used to restart the parsing
263 * where it stopped for example.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200264 *
265 * For now it's limited to the response. If the header block is incomplete,
266 * 0 is returned, waiting to be called again with more data to try it again.
Willy Tarreau4433c082018-09-11 15:33:32 +0200267 * The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE,
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200268 * and h1m->next to zero on the first call, the parser will do the rest. If
269 * an incomplete message is seen, the caller only needs to present h1m->state
270 * and h1m->next again, with an empty header list so that the parser can start
271 * again. In this case, it will detect that it interrupted a previous session
272 * and will first look for the end of the message before reparsing it again and
273 * indexing it at the same time. This ensures that incomplete messages fed 1
274 * character at a time are never processed entirely more than exactly twice,
275 * and that there is no need to store all the internal state and pre-parsed
276 * headers or start line between calls.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200277 *
Willy Tarreaua41393f2018-09-11 15:34:50 +0200278 * A pointer to a start line descriptor may be passed in <slp>, in which case
279 * the parser will fill it with whatever it found.
280 *
Willy Tarreau794f9af2017-07-26 09:07:47 +0200281 * The code derived from the main HTTP/1 parser above but was simplified and
282 * optimized to process responses produced or forwarded by haproxy. The caller
283 * is responsible for ensuring that the message doesn't wrap, and should ensure
284 * it is complete to avoid having to retry the operation after a failed
285 * attempt. The message is not supposed to be invalid, which is why a few
286 * properties such as the character set used in the header field names are not
287 * checked. In case of an unparsable response message, a negative value will be
288 * returned with h1m->err_pos and h1m->err_state matching the location and
289 * state where the error was met. Leading blank likes are tolerated but not
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100290 * recommended. If flag H1_MF_HDRS_ONLY is set in h1m->flags, only headers are
291 * parsed and the start line is skipped. It is not required to set h1m->state
292 * nor h1m->next in this case.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200293 *
294 * This function returns :
295 * -1 in case of error. In this case, h1m->err_state is filled (if h1m is
Willy Tarreau801250e2018-09-11 11:45:04 +0200296 * set) with the state the error occurred in and h1m->err_pos with the
Willy Tarreau794f9af2017-07-26 09:07:47 +0200297 * the position relative to <start>
298 * -2 if the output is full (hdr_num reached). err_state and err_pos also
299 * indicate where it failed.
300 * 0 in case of missing data.
301 * > 0 on success, it then corresponds to the number of bytes read since
302 * <start> so that the caller can go on with the payload.
303 */
304int h1_headers_to_hdr_list(char *start, const char *stop,
305 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +0200306 struct h1m *h1m, union h1_sl *slp)
Willy Tarreau794f9af2017-07-26 09:07:47 +0200307{
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200308 enum h1m_state state;
309 register char *ptr;
310 register const char *end;
311 unsigned int hdr_count;
312 unsigned int skip; /* number of bytes skipped at the beginning */
313 unsigned int sol; /* start of line */
314 unsigned int col; /* position of the colon */
315 unsigned int eol; /* end of line */
316 unsigned int sov; /* start of value */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200317 union h1_sl sl;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200318 int skip_update;
319 int restarting;
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200320 int host_idx;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200321 struct ist n, v; /* header name and value during parsing */
322
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200323 skip = 0; // do it only once to keep track of the leading CRLF.
324
325 try_again:
326 hdr_count = sol = col = eol = sov = 0;
Willy Tarreaua41393f2018-09-11 15:34:50 +0200327 sl.st.status = 0;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200328 skip_update = restarting = 0;
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200329 host_idx = -1;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200330
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100331 if (h1m->flags & H1_MF_HDRS_ONLY) {
332 state = H1_MSG_HDR_FIRST;
333 h1m->next = 0;
334 }
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100335 else {
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100336 state = h1m->state;
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100337 if (h1m->state != H1_MSG_RQBEFORE && h1m->state != H1_MSG_RPBEFORE)
338 restarting = 1;
339 }
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100340
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200341 ptr = start + h1m->next;
342 end = stop;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200343
Willy Tarreau794f9af2017-07-26 09:07:47 +0200344 if (unlikely(ptr >= end))
345 goto http_msg_ood;
346
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200347 /* don't update output if hdr is NULL or if we're restarting */
348 if (!hdr || restarting)
Willy Tarreau5384aac2018-09-11 16:04:48 +0200349 skip_update = 1;
350
Willy Tarreau794f9af2017-07-26 09:07:47 +0200351 switch (state) {
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200352 case H1_MSG_RQBEFORE:
353 http_msg_rqbefore:
354 if (likely(HTTP_IS_TOKEN(*ptr))) {
355 /* we have a start of message, we may have skipped some
356 * heading CRLF. Skip them now.
357 */
358 skip += ptr - start;
359 start = ptr;
360
361 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200362 sl.rq.m.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200363 hdr_count = 0;
364 state = H1_MSG_RQMETH;
365 goto http_msg_rqmeth;
366 }
367
368 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
369 state = H1_MSG_RQBEFORE;
370 goto http_msg_invalid;
371 }
372
373 if (unlikely(*ptr == '\n'))
374 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
375 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
376 /* stop here */
377
378 case H1_MSG_RQBEFORE_CR:
379 http_msg_rqbefore_cr:
380 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
381 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
382 /* stop here */
383
384 case H1_MSG_RQMETH:
385 http_msg_rqmeth:
386 if (likely(HTTP_IS_TOKEN(*ptr)))
387 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
388
389 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200390 sl.rq.m.len = ptr - sl.rq.m.ptr;
391 sl.rq.meth = find_http_meth(start, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200392 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
393 }
394
395 if (likely(HTTP_IS_CRLF(*ptr))) {
396 /* HTTP 0.9 request */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200397 sl.rq.m.len = ptr - sl.rq.m.ptr;
398 sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200399 http_msg_req09_uri:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200400 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200401 http_msg_req09_uri_e:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200402 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200403 http_msg_req09_ver:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200404 sl.rq.v.ptr = ptr;
405 sl.rq.v.len = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200406 goto http_msg_rqline_eol;
407 }
408 state = H1_MSG_RQMETH;
409 goto http_msg_invalid;
410
411 case H1_MSG_RQMETH_SP:
412 http_msg_rqmeth_sp:
413 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200414 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200415 goto http_msg_rquri;
416 }
417 if (likely(HTTP_IS_SPHT(*ptr)))
418 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
419 /* so it's a CR/LF, meaning an HTTP 0.9 request */
420 goto http_msg_req09_uri;
421
422 case H1_MSG_RQURI:
423 http_msg_rquri:
Willy Tarreau02ac9502020-02-21 16:31:22 +0100424#ifdef HA_UNALIGNED_LE
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200425 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
426 while (ptr <= end - sizeof(int)) {
427 int x = *(int *)ptr - 0x21212121;
428 if (x & 0x80808080)
429 break;
430
431 x -= 0x5e5e5e5e;
432 if (!(x & 0x80808080))
433 break;
434
435 ptr += sizeof(int);
436 }
437#endif
438 if (ptr >= end) {
439 state = H1_MSG_RQURI;
440 goto http_msg_ood;
441 }
442 http_msg_rquri2:
443 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
444 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
445
446 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200447 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200448 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
449 }
450 if (likely((unsigned char)*ptr >= 128)) {
451 /* non-ASCII chars are forbidden unless option
452 * accept-invalid-http-request is enabled in the frontend.
453 * In any case, we capture the faulty char.
454 */
455 if (h1m->err_pos < -1)
456 goto invalid_char;
457 if (h1m->err_pos == -1)
458 h1m->err_pos = ptr - start + skip;
459 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
460 }
461
462 if (likely(HTTP_IS_CRLF(*ptr))) {
463 /* so it's a CR/LF, meaning an HTTP 0.9 request */
464 goto http_msg_req09_uri_e;
465 }
466
467 /* OK forbidden chars, 0..31 or 127 */
468 invalid_char:
469 state = H1_MSG_RQURI;
470 goto http_msg_invalid;
471
472 case H1_MSG_RQURI_SP:
473 http_msg_rquri_sp:
474 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200475 sl.rq.v.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200476 goto http_msg_rqver;
477 }
478 if (likely(HTTP_IS_SPHT(*ptr)))
479 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
480 /* so it's a CR/LF, meaning an HTTP 0.9 request */
481 goto http_msg_req09_ver;
482
483
484 case H1_MSG_RQVER:
485 http_msg_rqver:
486 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
487 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
488
489 if (likely(HTTP_IS_CRLF(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200490 sl.rq.v.len = ptr - sl.rq.v.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200491 http_msg_rqline_eol:
492 /* We have seen the end of line. Note that we do not
493 * necessarily have the \n yet, but at least we know that we
494 * have EITHER \r OR \n, otherwise the request would not be
495 * complete. We can then record the request length and return
496 * to the caller which will be able to register it.
497 */
498
499 if (likely(!skip_update)) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200500 if ((sl.rq.v.len == 8) &&
501 (*(sl.rq.v.ptr + 5) > '1' ||
502 (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200503 h1m->flags |= H1_MF_VER_11;
504
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200505 if (unlikely(hdr_count >= hdr_num)) {
506 state = H1_MSG_RQVER;
507 goto http_output_full;
508 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200509 if (!(h1m->flags & H1_MF_NO_PHDR))
510 http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200511
512 if (unlikely(hdr_count >= hdr_num)) {
513 state = H1_MSG_RQVER;
514 goto http_output_full;
515 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200516 if (!(h1m->flags & H1_MF_NO_PHDR))
517 http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200518 }
519
520 sol = ptr - start;
521 if (likely(*ptr == '\r'))
522 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
523 goto http_msg_rqline_end;
524 }
525
526 /* neither an HTTP_VER token nor a CRLF */
527 state = H1_MSG_RQVER;
528 goto http_msg_invalid;
529
530 case H1_MSG_RQLINE_END:
531 http_msg_rqline_end:
532 /* check for HTTP/0.9 request : no version information
533 * available. sol must point to the first of CR or LF. However
534 * since we don't save these elements between calls, if we come
535 * here from a restart, we don't necessarily know. Thus in this
536 * case we simply start over.
537 */
538 if (restarting)
539 goto restart;
540
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200541 if (unlikely(sl.rq.v.len == 0))
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200542 goto http_msg_last_lf;
543
544 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
545 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
546 /* stop here */
547
548 /*
549 * Common states below
550 */
Willy Tarreau801250e2018-09-11 11:45:04 +0200551 case H1_MSG_RPBEFORE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200552 http_msg_rpbefore:
553 if (likely(HTTP_IS_TOKEN(*ptr))) {
554 /* we have a start of message, we may have skipped some
555 * heading CRLF. Skip them now.
556 */
557 skip += ptr - start;
558 start = ptr;
559
560 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200561 sl.st.v.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200562 hdr_count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +0200563 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200564 goto http_msg_rpver;
565 }
566
567 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200568 state = H1_MSG_RPBEFORE;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200569 goto http_msg_invalid;
570 }
571
572 if (unlikely(*ptr == '\n'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200573 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
574 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 +0200575 /* stop here */
576
Willy Tarreau801250e2018-09-11 11:45:04 +0200577 case H1_MSG_RPBEFORE_CR:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200578 http_msg_rpbefore_cr:
Willy Tarreau801250e2018-09-11 11:45:04 +0200579 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
580 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200581 /* stop here */
582
Willy Tarreau801250e2018-09-11 11:45:04 +0200583 case H1_MSG_RPVER:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200584 http_msg_rpver:
585 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200586 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200587
588 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200589 sl.st.v.len = ptr - sl.st.v.ptr;
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200590
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200591 if ((sl.st.v.len == 8) &&
592 (*(sl.st.v.ptr + 5) > '1' ||
593 (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200594 h1m->flags |= H1_MF_VER_11;
595
Willy Tarreau801250e2018-09-11 11:45:04 +0200596 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 +0200597 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200598 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200599 goto http_msg_invalid;
600
Willy Tarreau801250e2018-09-11 11:45:04 +0200601 case H1_MSG_RPVER_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200602 http_msg_rpver_sp:
603 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200604 sl.st.status = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200605 sl.st.c.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200606 goto http_msg_rpcode;
607 }
608 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200609 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 +0200610 /* so it's a CR/LF, this is invalid */
Willy Tarreau801250e2018-09-11 11:45:04 +0200611 state = H1_MSG_RPVER_SP;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200612 goto http_msg_invalid;
613
Willy Tarreau801250e2018-09-11 11:45:04 +0200614 case H1_MSG_RPCODE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200615 http_msg_rpcode:
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100616 if (likely(HTTP_IS_DIGIT(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200617 sl.st.status = sl.st.status * 10 + *ptr - '0';
Willy Tarreau801250e2018-09-11 11:45:04 +0200618 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200619 }
620
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100621 if (unlikely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200622 state = H1_MSG_RPCODE;
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100623 goto http_msg_invalid;
624 }
625
Willy Tarreau794f9af2017-07-26 09:07:47 +0200626 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200627 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau801250e2018-09-11 11:45:04 +0200628 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 +0200629 }
630
631 /* so it's a CR/LF, so there is no reason phrase */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200632 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200633
634 http_msg_rsp_reason:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200635 sl.st.r.ptr = ptr;
636 sl.st.r.len = 0;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200637 goto http_msg_rpline_eol;
638
Willy Tarreau801250e2018-09-11 11:45:04 +0200639 case H1_MSG_RPCODE_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200640 http_msg_rpcode_sp:
641 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200642 sl.st.r.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200643 goto http_msg_rpreason;
644 }
645 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200646 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 +0200647 /* so it's a CR/LF, so there is no reason phrase */
648 goto http_msg_rsp_reason;
649
Willy Tarreau801250e2018-09-11 11:45:04 +0200650 case H1_MSG_RPREASON:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200651 http_msg_rpreason:
652 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200653 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200654 sl.st.r.len = ptr - sl.st.r.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200655 http_msg_rpline_eol:
656 /* We have seen the end of line. Note that we do not
657 * necessarily have the \n yet, but at least we know that we
658 * have EITHER \r OR \n, otherwise the response would not be
659 * complete. We can then record the response length and return
660 * to the caller which will be able to register it.
661 */
662
Willy Tarreau5384aac2018-09-11 16:04:48 +0200663 if (likely(!skip_update)) {
664 if (unlikely(hdr_count >= hdr_num)) {
665 state = H1_MSG_RPREASON;
666 goto http_output_full;
667 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200668 if (!(h1m->flags & H1_MF_NO_PHDR))
669 http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200670 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200671
672 sol = ptr - start;
673 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200674 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 +0200675 goto http_msg_rpline_end;
676
Willy Tarreau801250e2018-09-11 11:45:04 +0200677 case H1_MSG_RPLINE_END:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200678 http_msg_rpline_end:
679 /* sol must point to the first of CR or LF. */
Willy Tarreau801250e2018-09-11 11:45:04 +0200680 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
681 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 +0200682 /* stop here */
683
Willy Tarreau801250e2018-09-11 11:45:04 +0200684 case H1_MSG_HDR_FIRST:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200685 http_msg_hdr_first:
686 sol = ptr - start;
687 if (likely(!HTTP_IS_CRLF(*ptr))) {
688 goto http_msg_hdr_name;
689 }
690
691 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200692 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 +0200693 goto http_msg_last_lf;
694
Willy Tarreau801250e2018-09-11 11:45:04 +0200695 case H1_MSG_HDR_NAME:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200696 http_msg_hdr_name:
697 /* assumes sol points to the first char */
698 if (likely(HTTP_IS_TOKEN(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200699 if (!skip_update) {
700 /* turn it to lower case if needed */
701 if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
Willy Tarreauf278eec2020-07-05 21:46:32 +0200702 *ptr = tolower((unsigned char)*ptr);
Christopher Faulet2912f872018-09-19 14:01:04 +0200703 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200704 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 +0200705 }
706
707 if (likely(*ptr == ':')) {
708 col = ptr - start;
Willy Tarreau801250e2018-09-11 11:45:04 +0200709 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 +0200710 }
711
Willy Tarreau9aec3052018-09-12 09:20:40 +0200712 if (likely(h1m->err_pos < -1) || *ptr == '\n') {
Willy Tarreau801250e2018-09-11 11:45:04 +0200713 state = H1_MSG_HDR_NAME;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200714 goto http_msg_invalid;
715 }
716
Willy Tarreau9aec3052018-09-12 09:20:40 +0200717 if (h1m->err_pos == -1) /* capture the error pointer */
718 h1m->err_pos = ptr - start + skip; /* >= 0 now */
719
720 /* and we still accept this non-token character */
Willy Tarreau801250e2018-09-11 11:45:04 +0200721 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 +0200722
Willy Tarreau801250e2018-09-11 11:45:04 +0200723 case H1_MSG_HDR_L1_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200724 http_msg_hdr_l1_sp:
725 /* assumes sol points to the first char */
726 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200727 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 +0200728
729 /* header value can be basically anything except CR/LF */
730 sov = ptr - start;
731
732 if (likely(!HTTP_IS_CRLF(*ptr))) {
733 goto http_msg_hdr_val;
734 }
735
736 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200737 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 +0200738 goto http_msg_hdr_l1_lf;
739
Willy Tarreau801250e2018-09-11 11:45:04 +0200740 case H1_MSG_HDR_L1_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200741 http_msg_hdr_l1_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200742 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
743 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 +0200744
Willy Tarreau801250e2018-09-11 11:45:04 +0200745 case H1_MSG_HDR_L1_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200746 http_msg_hdr_l1_lws:
747 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200748 if (!skip_update) {
749 /* replace HT,CR,LF with spaces */
750 for (; start + sov < ptr; sov++)
751 start[sov] = ' ';
752 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200753 goto http_msg_hdr_l1_sp;
754 }
755 /* we had a header consisting only in spaces ! */
756 eol = sov;
757 goto http_msg_complete_header;
758
Willy Tarreau801250e2018-09-11 11:45:04 +0200759 case H1_MSG_HDR_VAL:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200760 http_msg_hdr_val:
761 /* assumes sol points to the first char, and sov
762 * points to the first character of the value.
763 */
764
765 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
766 * and lower. In fact since most of the time is spent in the loop, we
767 * also remove the sign bit test so that bytes 0x8e..0x0d break the
768 * loop, but we don't care since they're very rare in header values.
769 */
Willy Tarreau02ac9502020-02-21 16:31:22 +0100770#ifdef HA_UNALIGNED_LE64
Willy Tarreau794f9af2017-07-26 09:07:47 +0200771 while (ptr <= end - sizeof(long)) {
772 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
773 goto http_msg_hdr_val2;
774 ptr += sizeof(long);
775 }
776#endif
Willy Tarreau02ac9502020-02-21 16:31:22 +0100777#ifdef HA_UNALIGNED_LE
Willy Tarreau794f9af2017-07-26 09:07:47 +0200778 while (ptr <= end - sizeof(int)) {
779 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
780 goto http_msg_hdr_val2;
781 ptr += sizeof(int);
782 }
783#endif
784 if (ptr >= end) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200785 state = H1_MSG_HDR_VAL;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200786 goto http_msg_ood;
787 }
788 http_msg_hdr_val2:
789 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200790 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 +0200791
792 eol = ptr - start;
793 /* Note: we could also copy eol into ->eoh so that we have the
794 * real header end in case it ends with lots of LWS, but is this
795 * really needed ?
796 */
797 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200798 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 +0200799 goto http_msg_hdr_l2_lf;
800
Willy Tarreau801250e2018-09-11 11:45:04 +0200801 case H1_MSG_HDR_L2_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200802 http_msg_hdr_l2_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200803 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
804 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 +0200805
Willy Tarreau801250e2018-09-11 11:45:04 +0200806 case H1_MSG_HDR_L2_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200807 http_msg_hdr_l2_lws:
808 if (unlikely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200809 if (!skip_update) {
810 /* LWS: replace HT,CR,LF with spaces */
811 for (; start + eol < ptr; eol++)
812 start[eol] = ' ';
813 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200814 goto http_msg_hdr_val;
815 }
816 http_msg_complete_header:
817 /*
818 * It was a new header, so the last one is finished. Assumes
819 * <sol> points to the first char of the name, <col> to the
820 * colon, <sov> points to the first character of the value and
821 * <eol> to the first CR or LF so we know how the line ends. We
822 * will trim spaces around the value. It's possible to do it by
823 * adjusting <eol> and <sov> which are no more used after this.
824 * We can add the header field to the list.
825 */
Christopher Faulet2912f872018-09-19 14:01:04 +0200826 if (likely(!skip_update)) {
827 while (sov < eol && HTTP_IS_LWS(start[sov]))
828 sov++;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200829
Christopher Faulet2912f872018-09-19 14:01:04 +0200830 while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
831 eol--;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200832
833
Christopher Faulet2912f872018-09-19 14:01:04 +0200834 n = ist2(start + sol, col - sol);
835 v = ist2(start + sov, eol - sov);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200836
Christopher Faulet2912f872018-09-19 14:01:04 +0200837 do {
838 int ret;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200839
Christopher Faulet2912f872018-09-19 14:01:04 +0200840 if (unlikely(hdr_count >= hdr_num)) {
841 state = H1_MSG_HDR_L2_LWS;
842 goto http_output_full;
843 }
Willy Tarreau5384aac2018-09-11 16:04:48 +0200844
Christopher Faulet2912f872018-09-19 14:01:04 +0200845 if (isteqi(n, ist("transfer-encoding"))) {
846 h1_parse_xfer_enc_header(h1m, v);
847 }
848 else if (isteqi(n, ist("content-length"))) {
849 ret = h1_parse_cont_len_header(h1m, &v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200850
Christopher Faulet2912f872018-09-19 14:01:04 +0200851 if (ret < 0) {
852 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +0100853 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet2912f872018-09-19 14:01:04 +0200854 goto http_msg_invalid;
855 }
856 else if (ret == 0) {
857 /* skip it */
858 break;
859 }
Willy Tarreau73373ab2018-09-14 17:11:33 +0200860 }
Christopher Faulet2912f872018-09-19 14:01:04 +0200861 else if (isteqi(n, ist("connection"))) {
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100862 h1_parse_connection_header(h1m, &v);
863 if (!v.len) {
864 /* skip it */
865 break;
866 }
Willy Tarreau73373ab2018-09-14 17:11:33 +0200867 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +0100868 else if (isteqi(n, ist("upgrade"))) {
869 h1_parse_upgrade_header(h1m, v);
870 }
Christopher Faulet7032a3f2020-05-04 09:01:45 +0200871 else if (!(h1m->flags & (H1_MF_HDRS_ONLY|H1_MF_RESP)) && isteqi(n, ist("host"))) {
Christopher Faulet531b83e2019-10-11 13:34:22 +0200872 if (host_idx == -1) {
873 struct ist authority;
874
875 authority = http_get_authority(sl.rq.u, 1);
876 if (authority.len && !isteqi(v, authority)) {
877 if (h1m->err_pos < -1) {
878 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +0100879 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet531b83e2019-10-11 13:34:22 +0200880 goto http_msg_invalid;
881 }
882 if (h1m->err_pos == -1) /* capture the error pointer */
Christopher Faulet17034782020-01-06 13:41:01 +0100883 h1m->err_pos = v.ptr - start + skip; /* >= 0 now */
Christopher Faulet531b83e2019-10-11 13:34:22 +0200884 }
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200885 host_idx = hdr_count;
Christopher Faulet531b83e2019-10-11 13:34:22 +0200886 }
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200887 else {
888 if (!isteqi(v, hdr[host_idx].v)) {
889 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +0100890 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200891 goto http_msg_invalid;
892 }
893 /* if the same host, skip it */
894 break;
895 }
896 }
Willy Tarreau2ea6bb52018-09-14 16:28:15 +0200897
Christopher Faulet2912f872018-09-19 14:01:04 +0200898 http_set_hdr(&hdr[hdr_count++], n, v);
899 } while (0);
900 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200901
902 sol = ptr - start;
Christopher Faulet2912f872018-09-19 14:01:04 +0200903
Willy Tarreau794f9af2017-07-26 09:07:47 +0200904 if (likely(!HTTP_IS_CRLF(*ptr)))
905 goto http_msg_hdr_name;
906
907 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200908 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 +0200909 goto http_msg_last_lf;
910
Willy Tarreau801250e2018-09-11 11:45:04 +0200911 case H1_MSG_LAST_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200912 http_msg_last_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200913 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200914 ptr++;
915 /* <ptr> now points to the first byte of payload. If needed sol
916 * still points to the first of either CR or LF of the empty
917 * line ending the headers block.
918 */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200919 if (likely(!skip_update)) {
920 if (unlikely(hdr_count >= hdr_num)) {
921 state = H1_MSG_LAST_LF;
922 goto http_output_full;
923 }
Christopher Fauletff08a922018-09-25 13:59:46 +0200924 http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
Willy Tarreau794f9af2017-07-26 09:07:47 +0200925 }
Willy Tarreau001823c2018-09-12 17:25:32 +0200926
927 /* reaching here we've parsed the whole message. We may detect
928 * that we were already continuing an interrupted parsing pass
929 * so we were silently looking for the end of message not
930 * updating anything before deciding to parse it fully at once.
931 * It's guaranteed that we won't match this test twice in a row
932 * since restarting will turn zero.
933 */
934 if (restarting)
935 goto restart;
936
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200937 state = H1_MSG_DATA;
938 if (h1m->flags & H1_MF_XFER_ENC) {
939 if (h1m->flags & H1_MF_CLEN) {
940 h1m->flags &= ~H1_MF_CLEN;
941 hdr_count = http_del_hdr(hdr, ist("content-length"));
942 }
943
944 if (h1m->flags & H1_MF_CHNK)
945 state = H1_MSG_CHUNK_SIZE;
946 else if (!(h1m->flags & H1_MF_RESP)) {
947 /* cf RFC7230#3.3.3 : transfer-encoding in
948 * request without chunked encoding is invalid.
949 */
950 goto http_msg_invalid;
951 }
952 }
953
Willy Tarreau794f9af2017-07-26 09:07:47 +0200954 break;
955
956 default:
957 /* impossible states */
958 goto http_msg_invalid;
959 }
960
Willy Tarreau001823c2018-09-12 17:25:32 +0200961 /* Now we've left the headers state and are either in H1_MSG_DATA or
962 * H1_MSG_CHUNK_SIZE.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200963 */
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200964
Willy Tarreau5384aac2018-09-11 16:04:48 +0200965 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200966 *slp = sl;
967
Willy Tarreau4433c082018-09-11 15:33:32 +0200968 h1m->state = state;
969 h1m->next = ptr - start + skip;
970 return h1m->next;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200971
972 http_msg_ood:
973 /* out of data at <ptr> during state <state> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200974 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200975 *slp = sl;
976
Willy Tarreau4433c082018-09-11 15:33:32 +0200977 h1m->state = state;
978 h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200979 return 0;
980
981 http_msg_invalid:
982 /* invalid message, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200983 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200984 *slp = sl;
985
Willy Tarreau4433c082018-09-11 15:33:32 +0200986 h1m->err_state = h1m->state = state;
987 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200988 return -1;
989
990 http_output_full:
991 /* no more room to store the current header, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +0200992 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +0200993 *slp = sl;
994
Willy Tarreau4433c082018-09-11 15:33:32 +0200995 h1m->err_state = h1m->state = state;
996 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200997 return -2;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200998
999 restart:
Christopher Faulet84f06532019-09-03 16:05:31 +02001000 h1m->flags &= ~(H1_MF_VER_11|H1_MF_CLEN|H1_MF_XFER_ENC|H1_MF_CHNK|H1_MF_CONN_KAL|H1_MF_CONN_CLO|H1_MF_CONN_UPG);
1001 h1m->curr_len = h1m->body_len = h1m->next = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +02001002 if (h1m->flags & H1_MF_RESP)
1003 h1m->state = H1_MSG_RPBEFORE;
1004 else
1005 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001006 goto try_again;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001007}
1008
Willy Tarreau2510f702017-10-31 17:14:16 +01001009/* This function performs a very minimal parsing of the trailers block present
Willy Tarreauf40e6822018-06-14 16:52:02 +02001010 * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of
Willy Tarreau7314be82018-06-14 13:32:50 +02001011 * bytes to delete to skip the trailers. It may return 0 if it's missing some
1012 * input data, or < 0 in case of parse error (in which case the caller may have
1013 * to decide how to proceed, possibly eating everything).
Willy Tarreau2510f702017-10-31 17:14:16 +01001014 */
Willy Tarreauf40e6822018-06-14 16:52:02 +02001015int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max)
Willy Tarreau2510f702017-10-31 17:14:16 +01001016{
Willy Tarreauf40e6822018-06-14 16:52:02 +02001017 const char *stop = b_peek(buf, ofs + max);
1018 int count = ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +01001019
1020 while (1) {
1021 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau7314be82018-06-14 13:32:50 +02001022 const char *start = b_peek(buf, count);
Willy Tarreau2510f702017-10-31 17:14:16 +01001023 const char *ptr = start;
Willy Tarreau2510f702017-10-31 17:14:16 +01001024
1025 /* scan current line and stop at LF or CRLF */
1026 while (1) {
1027 if (ptr == stop)
1028 return 0;
1029
1030 if (*ptr == '\n') {
1031 if (!p1)
1032 p1 = ptr;
1033 p2 = ptr;
1034 break;
1035 }
1036
1037 if (*ptr == '\r') {
1038 if (p1)
1039 return -1;
1040 p1 = ptr;
1041 }
1042
Willy Tarreau7314be82018-06-14 13:32:50 +02001043 ptr = b_next(buf, ptr);
Willy Tarreau2510f702017-10-31 17:14:16 +01001044 }
1045
1046 /* after LF; point to beginning of next line */
Willy Tarreau7314be82018-06-14 13:32:50 +02001047 p2 = b_next(buf, p2);
1048 count += b_dist(buf, start, p2);
Willy Tarreau2510f702017-10-31 17:14:16 +01001049
1050 /* LF/CRLF at beginning of line => end of trailers at p2.
1051 * Everything was scheduled for forwarding, there's nothing left
1052 * from this message. */
1053 if (p1 == start)
1054 break;
1055 /* OK, next line then */
1056 }
Willy Tarreauf40e6822018-06-14 16:52:02 +02001057 return count - ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +01001058}
Amaury Denoyellec1938232020-12-11 17:53:03 +01001059
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001060/* Generate a random key for a WebSocket Handshake in respect with rfc6455
1061 * The key is 128-bits long encoded as a base64 string in <key_out> parameter
1062 * (25 bytes long).
1063 */
1064void h1_generate_random_ws_input_key(char key_out[25])
1065{
1066 /* generate a random websocket key */
1067 const uint64_t rand1 = ha_random64(), rand2 = ha_random64();
1068 char key[16];
1069
1070 memcpy(key, &rand1, 8);
1071 memcpy(&key[8], &rand2, 8);
1072 a2base64(key, 16, key_out, 25);
1073}
1074
Amaury Denoyellec1938232020-12-11 17:53:03 +01001075#define H1_WS_KEY_SUFFIX_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
1076
1077/*
1078 * Calculate the WebSocket handshake response key from <key_in>. Following the
1079 * rfc6455, <key_in> must be 24 bytes longs. The result is stored in <key_out>
1080 * as a 29 bytes long string.
1081 */
1082void h1_calculate_ws_output_key(const char *key, char *result)
1083{
1084 blk_SHA_CTX sha1_ctx;
1085 char hash_in[60], hash_out[20];
1086
1087 /* concatenate the key with a fixed suffix */
1088 memcpy(hash_in, key, 24);
1089 memcpy(&hash_in[24], H1_WS_KEY_SUFFIX_GUID, 36);
1090
1091 /* sha1 the result */
1092 blk_SHA1_Init(&sha1_ctx);
1093 blk_SHA1_Update(&sha1_ctx, hash_in, 60);
1094 blk_SHA1_Final((unsigned char *)hash_out, &sha1_ctx);
1095
1096 /* encode in base64 the hash */
1097 a2base64(hash_out, 20, result, 29);
1098}