blob: 38b73cd955708279de0cf3ab766976da0a5ba4d2 [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
Willy Tarreaua32f99f2023-08-09 08:32:48 +020037 word.ptr = value->ptr;
Willy Tarreau73373ab2018-09-14 17:11:33 +020038 e = value->ptr + value->len;
39
Willy Tarreaua32f99f2023-08-09 08:32:48 +020040 while (1) {
41 if (word.ptr >= e) {
42 /* empty header or empty value */
43 goto fail;
44 }
45
Ilya Shipitsin47d17182020-06-21 21:42:57 +050046 /* skip leading delimiter and blanks */
Willy Tarreaua32f99f2023-08-09 08:32:48 +020047 if (unlikely(HTTP_IS_LWS(*word.ptr))) {
48 word.ptr++;
Willy Tarreau73373ab2018-09-14 17:11:33 +020049 continue;
Willy Tarreaua32f99f2023-08-09 08:32:48 +020050 }
Willy Tarreau73373ab2018-09-14 17:11:33 +020051
52 /* digits only now */
53 for (cl = 0, n = word.ptr; n < e; n++) {
54 unsigned int c = *n - '0';
55 if (unlikely(c > 9)) {
56 /* non-digit */
57 if (unlikely(n == word.ptr)) // spaces only
58 goto fail;
59 break;
60 }
Willy Tarreauc33738c2023-08-09 11:02:34 +020061
62 if (unlikely(!cl && n > word.ptr)) {
63 /* There was a leading zero before this digit,
64 * let's trim it.
65 */
66 word.ptr = n;
67 }
68
Willy Tarreau73373ab2018-09-14 17:11:33 +020069 if (unlikely(cl > ULLONG_MAX / 10ULL))
70 goto fail; /* multiply overflow */
71 cl = cl * 10ULL;
72 if (unlikely(cl + c < cl))
73 goto fail; /* addition overflow */
74 cl = cl + c;
75 }
76
77 /* keep a copy of the exact cleaned value */
78 word.len = n - word.ptr;
79
80 /* skip trailing LWS till next comma or EOL */
81 for (; n < e; n++) {
82 if (!HTTP_IS_LWS(*n)) {
83 if (unlikely(*n != ','))
84 goto fail;
85 break;
86 }
87 }
88
89 /* if duplicate, must be equal */
90 if (h1m->flags & H1_MF_CLEN && cl != h1m->body_len)
91 goto fail;
92
93 /* OK, store this result as the one to be indexed */
94 h1m->flags |= H1_MF_CLEN;
95 h1m->curr_len = h1m->body_len = cl;
96 *value = word;
Willy Tarreaua32f99f2023-08-09 08:32:48 +020097
98 /* Now either n==e and we're done, or n points to the comma,
99 * and we skip it and continue.
100 */
101 if (n++ == e)
102 break;
103
Willy Tarreau73373ab2018-09-14 17:11:33 +0200104 word.ptr = n;
105 }
106 /* here we've reached the end with a single value or a series of
107 * identical values, all matching previous series if any. The last
108 * parsed value was sent back into <value>. We just have to decide
109 * if this occurrence has to be indexed (it's the first one) or
110 * silently skipped (it's not the first one)
111 */
112 return !not_first;
113 fail:
114 return -1;
115}
116
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200117/* Parse the Transfer-Encoding: header field of an HTTP/1 request, looking for
Christopher Faulet545fbba2021-09-28 09:36:25 +0200118 * "chunked" encoding to perform some checks (it must be the last encoding for
119 * the request and must not be performed twice for any message). The
120 * H1_MF_TE_CHUNKED is set if a valid "chunked" encoding is found. The
121 * H1_MF_TE_OTHER flag is set if any other encoding is found. The H1_MF_XFER_ENC
122 * flag is always set. The H1_MF_CHNK is set when "chunked" encoding is the last
123 * one. Note that transfer codings are case-insensitive (cf RFC7230#4). This
124 * function returns <0 if a error is found, 0 if the whole header can be dropped
125 * (not used yet), or >0 if the value can be indexed.
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200126 */
Christopher Faulet545fbba2021-09-28 09:36:25 +0200127int h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value)
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200128{
129 char *e, *n;
130 struct ist word;
131
132 h1m->flags |= H1_MF_XFER_ENC;
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200133
134 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100135 e = istend(value);
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200136
137 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500138 /* skip leading delimiter and blanks */
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200139 if (HTTP_IS_LWS(*word.ptr))
140 continue;
141
142 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
143 word.len = n - word.ptr;
144
145 /* trim trailing blanks */
146 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
147 word.len--;
148
149 h1m->flags &= ~H1_MF_CHNK;
Christopher Faulet545fbba2021-09-28 09:36:25 +0200150 if (isteqi(word, ist("chunked"))) {
151 if (h1m->flags & H1_MF_TE_CHUNKED) {
152 /* cf RFC7230#3.3.1 : A sender MUST NOT apply
153 * chunked more than once to a message body
154 * (i.e., chunking an already chunked message is
155 * not allowed)
156 */
157 goto fail;
158 }
159 h1m->flags |= (H1_MF_TE_CHUNKED|H1_MF_CHNK);
160 }
161 else {
162 if ((h1m->flags & (H1_MF_RESP|H1_MF_TE_CHUNKED)) == H1_MF_TE_CHUNKED) {
163 /* cf RFC7230#3.3.1 : If any transfer coding
164 * other than chunked is applied to a request
165 * payload body, the sender MUST apply chunked
166 * as the final transfer coding to ensure that
167 * the message is properly framed.
168 */
169 goto fail;
170 }
171 h1m->flags |= H1_MF_TE_OTHER;
172 }
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200173
174 word.ptr = n;
175 }
Christopher Faulet545fbba2021-09-28 09:36:25 +0200176
177 return 1;
178 fail:
179 return -1;
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200180}
181
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200182/* Validate the authority and the host header value for CONNECT method. If there
183 * is hast header, its value is normalized. 0 is returned on success, -1 if the
184 * authority is invalid and -2 if the host is invalid.
185 */
186static int h1_validate_connect_authority(struct ist authority, struct ist *host_hdr)
187{
188 struct ist uri_host, uri_port, host, host_port;
189
190 if (!isttest(authority))
191 goto invalid_authority;
192 uri_host = authority;
193 uri_port = http_get_host_port(authority);
Christopher Faulet75348c22022-11-22 10:27:54 +0100194 if (!istlen(uri_port))
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200195 goto invalid_authority;
196 uri_host.len -= (istlen(uri_port) + 1);
197
198 if (!host_hdr || !isttest(*host_hdr))
199 goto end;
200
201 /* Get the port of the host header value, if any */
202 host = *host_hdr;
203 host_port = http_get_host_port(*host_hdr);
Christopher Faulet75348c22022-11-22 10:27:54 +0100204 if (isttest(host_port))
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200205 host.len -= (istlen(host_port) + 1);
Christopher Faulet75348c22022-11-22 10:27:54 +0100206
207 if (istlen(host_port)) {
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200208 if (!isteqi(host, uri_host) || !isteq(host_port, uri_port))
209 goto invalid_host;
210 if (http_is_default_port(IST_NULL, uri_port))
211 *host_hdr = host; /* normalize */
212 }
213 else {
214 if (!http_is_default_port(IST_NULL, uri_port) || !isteqi(host, uri_host))
215 goto invalid_host;
216 }
217
218 end:
219 return 0;
220
221 invalid_authority:
222 return -1;
223
224 invalid_host:
225 return -2;
226}
227
Christopher Faulete16ffb02022-11-22 10:04:16 +0100228
229/* Validate the authority and the host header value for non-CONNECT method, when
230 * an absolute-URI is detected but when it does not exactly match the host
231 * value. The idea is to detect default port (http or https). authority and host
232 * are defined here. 0 is returned on success, -1 if the host is does not match
233 * the authority.
234 */
235static int h1_validate_mismatch_authority(struct ist scheme, struct ist authority, struct ist host_hdr)
236{
237 struct ist uri_host, uri_port, host, host_port;
238
239 if (!isttest(scheme))
240 goto mismatch;
241
242 uri_host = authority;
243 uri_port = http_get_host_port(authority);
244 if (isttest(uri_port))
245 uri_host.len -= (istlen(uri_port) + 1);
246
247 host = host_hdr;
248 host_port = http_get_host_port(host_hdr);
249 if (isttest(host_port))
250 host.len -= (istlen(host_port) + 1);
251
252 if (!isttest(uri_port) && !isttest(host_port)) {
253 /* No port on both: we already know the authority does not match
254 * the host value
255 */
256 goto mismatch;
257 }
258 else if (isttest(uri_port) && !http_is_default_port(scheme, uri_port)) {
259 /* here there is no port for the host value and the port for the
260 * authority is not the default one
261 */
262 goto mismatch;
263 }
264 else if (isttest(host_port) && !http_is_default_port(scheme, host_port)) {
265 /* here there is no port for the authority and the port for the
266 * host value is not the default one
267 */
268 goto mismatch;
269 }
270 else {
271 /* the authority or the host value contain a default port and
272 * there is no port on the other value
273 */
274 if (!isteqi(uri_host, host))
275 goto mismatch;
276 }
277
278 return 0;
279
280 mismatch:
281 return -1;
282}
283
284
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200285/* Parse the Connection: header of an HTTP/1 request, looking for "close",
286 * "keep-alive", and "upgrade" values, and updating h1m->flags according to
287 * what was found there. Note that flags are only added, not removed, so the
288 * function is safe for being called multiple times if multiple occurrences
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100289 * are found. If the flag H1_MF_CLEAN_CONN_HDR, the header value is cleaned
290 * up from "keep-alive" and "close" values. To do so, the header value is
291 * rewritten in place and its length is updated.
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200292 */
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100293void h1_parse_connection_header(struct h1m *h1m, struct ist *value)
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200294{
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100295 char *e, *n, *p;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200296 struct ist word;
297
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100298 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
299 p = value->ptr;
300 e = value->ptr + value->len;
301 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
302 value->len = 0;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200303
304 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500305 /* skip leading delimiter and blanks */
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200306 if (HTTP_IS_LWS(*word.ptr))
307 continue;
308
309 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
310 word.len = n - word.ptr;
311
312 /* trim trailing blanks */
313 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
314 word.len--;
315
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100316 if (isteqi(word, ist("keep-alive"))) {
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200317 h1m->flags |= H1_MF_CONN_KAL;
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100318 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
319 goto skip_val;
320 }
321 else if (isteqi(word, ist("close"))) {
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200322 h1m->flags |= H1_MF_CONN_CLO;
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100323 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
324 goto skip_val;
325 }
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200326 else if (isteqi(word, ist("upgrade")))
327 h1m->flags |= H1_MF_CONN_UPG;
328
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100329 if (h1m->flags & H1_MF_CLEAN_CONN_HDR) {
330 if (value->ptr + value->len == p) {
331 /* no rewrite done till now */
332 value->len = n - value->ptr;
333 }
334 else {
335 if (value->len)
336 value->ptr[value->len++] = ',';
337 istcat(value, word, e - value->ptr);
338 }
339 }
340
341 skip_val:
342 word.ptr = p = n;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200343 }
344}
345
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +0100346/* Parse the Upgrade: header of an HTTP/1 request.
347 * If "websocket" is found, set H1_MF_UPG_WEBSOCKET flag
348 */
349void h1_parse_upgrade_header(struct h1m *h1m, struct ist value)
350{
351 char *e, *n;
352 struct ist word;
353
354 h1m->flags &= ~H1_MF_UPG_WEBSOCKET;
355
356 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100357 e = istend(value);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +0100358
359 while (++word.ptr < e) {
360 /* skip leading delimiter and blanks */
361 if (HTTP_IS_LWS(*word.ptr))
362 continue;
363
364 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
365 word.len = n - word.ptr;
366
367 /* trim trailing blanks */
368 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
369 word.len--;
370
371 if (isteqi(word, ist("websocket")))
372 h1m->flags |= H1_MF_UPG_WEBSOCKET;
373
374 word.ptr = n;
375 }
376}
377
Willy Tarreau538746a2018-12-11 10:59:20 +0100378/* Macros used in the HTTP/1 parser, to check for the expected presence of
379 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
380 */
381
382/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
383 * <bad>.
384 */
385#define EXPECT_LF_HERE(ptr, bad, state, where) \
386 do { \
387 if (unlikely(*(ptr) != '\n')) { \
388 state = (where); \
389 goto bad; \
390 } \
391 } while (0)
392
393/* Increments pointer <ptr>, continues to label <more> if it's still below
394 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
395 * of buffer was reached.
396 */
397#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
398 do { \
399 if (likely(++(ptr) < (end))) \
400 goto more; \
401 else { \
402 state = (where); \
403 goto stop; \
404 } \
405 } while (0)
406
Willy Tarreau794f9af2017-07-26 09:07:47 +0200407/* This function parses a contiguous HTTP/1 headers block starting at <start>
408 * and ending before <stop>, at once, and converts it a list of (name,value)
409 * pairs representing header fields into the array <hdr> of size <hdr_num>,
410 * whose last entry will have an empty name and an empty value. If <hdr_num> is
Willy Tarreau4433c082018-09-11 15:33:32 +0200411 * too small to represent the whole message, an error is returned. Some
412 * protocol elements such as content-length and transfer-encoding will be
Willy Tarreau5384aac2018-09-11 16:04:48 +0200413 * parsed and stored into h1m as well. <hdr> may be null, in which case only
414 * the parsing state will be updated. This may be used to restart the parsing
415 * where it stopped for example.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200416 *
417 * For now it's limited to the response. If the header block is incomplete,
418 * 0 is returned, waiting to be called again with more data to try it again.
Willy Tarreau4433c082018-09-11 15:33:32 +0200419 * The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE,
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200420 * and h1m->next to zero on the first call, the parser will do the rest. If
421 * an incomplete message is seen, the caller only needs to present h1m->state
422 * and h1m->next again, with an empty header list so that the parser can start
423 * again. In this case, it will detect that it interrupted a previous session
424 * and will first look for the end of the message before reparsing it again and
425 * indexing it at the same time. This ensures that incomplete messages fed 1
426 * character at a time are never processed entirely more than exactly twice,
427 * and that there is no need to store all the internal state and pre-parsed
428 * headers or start line between calls.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200429 *
Willy Tarreaua41393f2018-09-11 15:34:50 +0200430 * A pointer to a start line descriptor may be passed in <slp>, in which case
431 * the parser will fill it with whatever it found.
432 *
Willy Tarreau794f9af2017-07-26 09:07:47 +0200433 * The code derived from the main HTTP/1 parser above but was simplified and
434 * optimized to process responses produced or forwarded by haproxy. The caller
435 * is responsible for ensuring that the message doesn't wrap, and should ensure
436 * it is complete to avoid having to retry the operation after a failed
437 * attempt. The message is not supposed to be invalid, which is why a few
438 * properties such as the character set used in the header field names are not
439 * checked. In case of an unparsable response message, a negative value will be
440 * returned with h1m->err_pos and h1m->err_state matching the location and
441 * state where the error was met. Leading blank likes are tolerated but not
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100442 * recommended. If flag H1_MF_HDRS_ONLY is set in h1m->flags, only headers are
443 * parsed and the start line is skipped. It is not required to set h1m->state
444 * nor h1m->next in this case.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200445 *
446 * This function returns :
447 * -1 in case of error. In this case, h1m->err_state is filled (if h1m is
Willy Tarreau801250e2018-09-11 11:45:04 +0200448 * set) with the state the error occurred in and h1m->err_pos with the
Willy Tarreau794f9af2017-07-26 09:07:47 +0200449 * the position relative to <start>
450 * -2 if the output is full (hdr_num reached). err_state and err_pos also
451 * indicate where it failed.
452 * 0 in case of missing data.
453 * > 0 on success, it then corresponds to the number of bytes read since
454 * <start> so that the caller can go on with the payload.
455 */
456int h1_headers_to_hdr_list(char *start, const char *stop,
457 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +0200458 struct h1m *h1m, union h1_sl *slp)
Willy Tarreau794f9af2017-07-26 09:07:47 +0200459{
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200460 enum h1m_state state;
461 register char *ptr;
462 register const char *end;
463 unsigned int hdr_count;
464 unsigned int skip; /* number of bytes skipped at the beginning */
465 unsigned int sol; /* start of line */
466 unsigned int col; /* position of the colon */
467 unsigned int eol; /* end of line */
468 unsigned int sov; /* start of value */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200469 union h1_sl sl;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200470 int skip_update;
471 int restarting;
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200472 int host_idx;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200473 struct ist n, v; /* header name and value during parsing */
474
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200475 skip = 0; // do it only once to keep track of the leading CRLF.
476
477 try_again:
478 hdr_count = sol = col = eol = sov = 0;
Willy Tarreaua41393f2018-09-11 15:34:50 +0200479 sl.st.status = 0;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200480 skip_update = restarting = 0;
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200481 host_idx = -1;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200482
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100483 if (h1m->flags & H1_MF_HDRS_ONLY) {
484 state = H1_MSG_HDR_FIRST;
485 h1m->next = 0;
486 }
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100487 else {
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100488 state = h1m->state;
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100489 if (h1m->state != H1_MSG_RQBEFORE && h1m->state != H1_MSG_RPBEFORE)
490 restarting = 1;
491 }
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100492
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200493 ptr = start + h1m->next;
494 end = stop;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200495
Willy Tarreau794f9af2017-07-26 09:07:47 +0200496 if (unlikely(ptr >= end))
497 goto http_msg_ood;
498
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200499 /* don't update output if hdr is NULL or if we're restarting */
500 if (!hdr || restarting)
Willy Tarreau5384aac2018-09-11 16:04:48 +0200501 skip_update = 1;
502
Willy Tarreau794f9af2017-07-26 09:07:47 +0200503 switch (state) {
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200504 case H1_MSG_RQBEFORE:
505 http_msg_rqbefore:
506 if (likely(HTTP_IS_TOKEN(*ptr))) {
507 /* we have a start of message, we may have skipped some
508 * heading CRLF. Skip them now.
509 */
510 skip += ptr - start;
511 start = ptr;
512
513 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200514 sl.rq.m.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200515 hdr_count = 0;
516 state = H1_MSG_RQMETH;
517 goto http_msg_rqmeth;
518 }
519
520 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
521 state = H1_MSG_RQBEFORE;
522 goto http_msg_invalid;
523 }
524
525 if (unlikely(*ptr == '\n'))
526 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
527 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
528 /* stop here */
529
530 case H1_MSG_RQBEFORE_CR:
531 http_msg_rqbefore_cr:
532 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
533 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
534 /* stop here */
535
536 case H1_MSG_RQMETH:
537 http_msg_rqmeth:
538 if (likely(HTTP_IS_TOKEN(*ptr)))
539 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
540
541 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200542 sl.rq.m.len = ptr - sl.rq.m.ptr;
543 sl.rq.meth = find_http_meth(start, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200544 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
545 }
546
547 if (likely(HTTP_IS_CRLF(*ptr))) {
548 /* HTTP 0.9 request */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200549 sl.rq.m.len = ptr - sl.rq.m.ptr;
550 sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200551 http_msg_req09_uri:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200552 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200553 http_msg_req09_uri_e:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200554 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200555 http_msg_req09_ver:
Tim Duesterhus77508502022-03-15 13:11:06 +0100556 sl.rq.v = ist2(ptr, 0);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200557 goto http_msg_rqline_eol;
558 }
559 state = H1_MSG_RQMETH;
560 goto http_msg_invalid;
561
562 case H1_MSG_RQMETH_SP:
563 http_msg_rqmeth_sp:
564 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200565 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200566 goto http_msg_rquri;
567 }
568 if (likely(HTTP_IS_SPHT(*ptr)))
569 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
570 /* so it's a CR/LF, meaning an HTTP 0.9 request */
571 goto http_msg_req09_uri;
572
573 case H1_MSG_RQURI:
574 http_msg_rquri:
Willy Tarreau02ac9502020-02-21 16:31:22 +0100575#ifdef HA_UNALIGNED_LE
Willy Tarreau9bf75c82023-08-08 16:17:22 +0200576 /* speedup: skip bytes not between 0x24 and 0x7e inclusive */
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200577 while (ptr <= end - sizeof(int)) {
Willy Tarreau9bf75c82023-08-08 16:17:22 +0200578 int x = *(int *)ptr - 0x24242424;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200579 if (x & 0x80808080)
580 break;
581
Willy Tarreau9bf75c82023-08-08 16:17:22 +0200582 x -= 0x5b5b5b5b;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200583 if (!(x & 0x80808080))
584 break;
585
586 ptr += sizeof(int);
587 }
588#endif
589 if (ptr >= end) {
590 state = H1_MSG_RQURI;
591 goto http_msg_ood;
592 }
593 http_msg_rquri2:
Willy Tarreau9bf75c82023-08-08 16:17:22 +0200594 if (likely((unsigned char)(*ptr - 33) <= 93)) { /* 33 to 126 included */
595 if (*ptr == '#') {
596 if (h1m->err_pos < -1) /* PR_O2_REQBUG_OK not set */
597 goto invalid_char;
598 if (h1m->err_pos == -1) /* PR_O2_REQBUG_OK set: just log */
599 h1m->err_pos = ptr - start + skip;
600 }
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200601 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
Willy Tarreau9bf75c82023-08-08 16:17:22 +0200602 }
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200603
604 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200605 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200606 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
607 }
608 if (likely((unsigned char)*ptr >= 128)) {
609 /* non-ASCII chars are forbidden unless option
610 * accept-invalid-http-request is enabled in the frontend.
611 * In any case, we capture the faulty char.
612 */
613 if (h1m->err_pos < -1)
614 goto invalid_char;
615 if (h1m->err_pos == -1)
616 h1m->err_pos = ptr - start + skip;
617 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
618 }
619
620 if (likely(HTTP_IS_CRLF(*ptr))) {
621 /* so it's a CR/LF, meaning an HTTP 0.9 request */
622 goto http_msg_req09_uri_e;
623 }
624
625 /* OK forbidden chars, 0..31 or 127 */
626 invalid_char:
627 state = H1_MSG_RQURI;
628 goto http_msg_invalid;
629
630 case H1_MSG_RQURI_SP:
631 http_msg_rquri_sp:
632 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200633 sl.rq.v.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200634 goto http_msg_rqver;
635 }
636 if (likely(HTTP_IS_SPHT(*ptr)))
637 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
638 /* so it's a CR/LF, meaning an HTTP 0.9 request */
639 goto http_msg_req09_ver;
640
641
642 case H1_MSG_RQVER:
643 http_msg_rqver:
644 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
645 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
646
647 if (likely(HTTP_IS_CRLF(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200648 sl.rq.v.len = ptr - sl.rq.v.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200649 http_msg_rqline_eol:
650 /* We have seen the end of line. Note that we do not
651 * necessarily have the \n yet, but at least we know that we
652 * have EITHER \r OR \n, otherwise the request would not be
653 * complete. We can then record the request length and return
654 * to the caller which will be able to register it.
655 */
656
657 if (likely(!skip_update)) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200658 if ((sl.rq.v.len == 8) &&
659 (*(sl.rq.v.ptr + 5) > '1' ||
660 (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200661 h1m->flags |= H1_MF_VER_11;
662
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200663 if (unlikely(hdr_count >= hdr_num)) {
664 state = H1_MSG_RQVER;
665 goto http_output_full;
666 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200667 if (!(h1m->flags & H1_MF_NO_PHDR))
668 http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200669
670 if (unlikely(hdr_count >= hdr_num)) {
671 state = H1_MSG_RQVER;
672 goto http_output_full;
673 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200674 if (!(h1m->flags & H1_MF_NO_PHDR))
675 http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200676 }
677
678 sol = ptr - start;
679 if (likely(*ptr == '\r'))
680 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
681 goto http_msg_rqline_end;
682 }
683
684 /* neither an HTTP_VER token nor a CRLF */
685 state = H1_MSG_RQVER;
686 goto http_msg_invalid;
687
688 case H1_MSG_RQLINE_END:
689 http_msg_rqline_end:
690 /* check for HTTP/0.9 request : no version information
691 * available. sol must point to the first of CR or LF. However
692 * since we don't save these elements between calls, if we come
693 * here from a restart, we don't necessarily know. Thus in this
694 * case we simply start over.
695 */
696 if (restarting)
697 goto restart;
698
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200699 if (unlikely(sl.rq.v.len == 0))
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200700 goto http_msg_last_lf;
701
702 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
703 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
704 /* stop here */
705
706 /*
707 * Common states below
708 */
Willy Tarreau801250e2018-09-11 11:45:04 +0200709 case H1_MSG_RPBEFORE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200710 http_msg_rpbefore:
711 if (likely(HTTP_IS_TOKEN(*ptr))) {
712 /* we have a start of message, we may have skipped some
713 * heading CRLF. Skip them now.
714 */
715 skip += ptr - start;
716 start = ptr;
717
718 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200719 sl.st.v.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200720 hdr_count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +0200721 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200722 goto http_msg_rpver;
723 }
724
725 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200726 state = H1_MSG_RPBEFORE;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200727 goto http_msg_invalid;
728 }
729
730 if (unlikely(*ptr == '\n'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200731 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
732 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 +0200733 /* stop here */
734
Willy Tarreau801250e2018-09-11 11:45:04 +0200735 case H1_MSG_RPBEFORE_CR:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200736 http_msg_rpbefore_cr:
Willy Tarreau801250e2018-09-11 11:45:04 +0200737 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
738 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200739 /* stop here */
740
Willy Tarreau801250e2018-09-11 11:45:04 +0200741 case H1_MSG_RPVER:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200742 http_msg_rpver:
743 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200744 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200745
746 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200747 sl.st.v.len = ptr - sl.st.v.ptr;
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200748
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200749 if ((sl.st.v.len == 8) &&
750 (*(sl.st.v.ptr + 5) > '1' ||
751 (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200752 h1m->flags |= H1_MF_VER_11;
753
Willy Tarreau801250e2018-09-11 11:45:04 +0200754 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 +0200755 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200756 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200757 goto http_msg_invalid;
758
Willy Tarreau801250e2018-09-11 11:45:04 +0200759 case H1_MSG_RPVER_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200760 http_msg_rpver_sp:
761 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200762 sl.st.status = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200763 sl.st.c.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200764 goto http_msg_rpcode;
765 }
766 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200767 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 +0200768 /* so it's a CR/LF, this is invalid */
Willy Tarreau801250e2018-09-11 11:45:04 +0200769 state = H1_MSG_RPVER_SP;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200770 goto http_msg_invalid;
771
Willy Tarreau801250e2018-09-11 11:45:04 +0200772 case H1_MSG_RPCODE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200773 http_msg_rpcode:
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100774 if (likely(HTTP_IS_DIGIT(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200775 sl.st.status = sl.st.status * 10 + *ptr - '0';
Willy Tarreau801250e2018-09-11 11:45:04 +0200776 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200777 }
778
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100779 if (unlikely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200780 state = H1_MSG_RPCODE;
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100781 goto http_msg_invalid;
782 }
783
Willy Tarreau794f9af2017-07-26 09:07:47 +0200784 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200785 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau801250e2018-09-11 11:45:04 +0200786 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 +0200787 }
788
789 /* so it's a CR/LF, so there is no reason phrase */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200790 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200791
792 http_msg_rsp_reason:
Tim Duesterhus77508502022-03-15 13:11:06 +0100793 sl.st.r = ist2(ptr, 0);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200794 goto http_msg_rpline_eol;
795
Willy Tarreau801250e2018-09-11 11:45:04 +0200796 case H1_MSG_RPCODE_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200797 http_msg_rpcode_sp:
798 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200799 sl.st.r.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200800 goto http_msg_rpreason;
801 }
802 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200803 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 +0200804 /* so it's a CR/LF, so there is no reason phrase */
805 goto http_msg_rsp_reason;
806
Willy Tarreau801250e2018-09-11 11:45:04 +0200807 case H1_MSG_RPREASON:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200808 http_msg_rpreason:
809 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200810 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200811 sl.st.r.len = ptr - sl.st.r.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200812 http_msg_rpline_eol:
813 /* We have seen the end of line. Note that we do not
814 * necessarily have the \n yet, but at least we know that we
815 * have EITHER \r OR \n, otherwise the response would not be
816 * complete. We can then record the response length and return
817 * to the caller which will be able to register it.
818 */
819
Willy Tarreau5384aac2018-09-11 16:04:48 +0200820 if (likely(!skip_update)) {
821 if (unlikely(hdr_count >= hdr_num)) {
822 state = H1_MSG_RPREASON;
823 goto http_output_full;
824 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200825 if (!(h1m->flags & H1_MF_NO_PHDR))
826 http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200827 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200828
829 sol = ptr - start;
830 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200831 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 +0200832 goto http_msg_rpline_end;
833
Willy Tarreau801250e2018-09-11 11:45:04 +0200834 case H1_MSG_RPLINE_END:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200835 http_msg_rpline_end:
836 /* sol must point to the first of CR or LF. */
Willy Tarreau801250e2018-09-11 11:45:04 +0200837 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
838 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 +0200839 /* stop here */
840
Willy Tarreau801250e2018-09-11 11:45:04 +0200841 case H1_MSG_HDR_FIRST:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200842 http_msg_hdr_first:
843 sol = ptr - start;
844 if (likely(!HTTP_IS_CRLF(*ptr))) {
845 goto http_msg_hdr_name;
846 }
847
848 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200849 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 +0200850 goto http_msg_last_lf;
851
Willy Tarreau801250e2018-09-11 11:45:04 +0200852 case H1_MSG_HDR_NAME:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200853 http_msg_hdr_name:
854 /* assumes sol points to the first char */
855 if (likely(HTTP_IS_TOKEN(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200856 if (!skip_update) {
857 /* turn it to lower case if needed */
858 if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
Willy Tarreauf278eec2020-07-05 21:46:32 +0200859 *ptr = tolower((unsigned char)*ptr);
Christopher Faulet2912f872018-09-19 14:01:04 +0200860 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200861 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 +0200862 }
863
864 if (likely(*ptr == ':')) {
865 col = ptr - start;
Willy Tarreaua8598a22023-02-09 21:36:54 +0100866 if (col <= sol) {
867 state = H1_MSG_HDR_NAME;
868 goto http_msg_invalid;
869 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200870 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 +0200871 }
872
Willy Tarreau9aec3052018-09-12 09:20:40 +0200873 if (likely(h1m->err_pos < -1) || *ptr == '\n') {
Willy Tarreau801250e2018-09-11 11:45:04 +0200874 state = H1_MSG_HDR_NAME;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200875 goto http_msg_invalid;
876 }
877
Willy Tarreau9aec3052018-09-12 09:20:40 +0200878 if (h1m->err_pos == -1) /* capture the error pointer */
879 h1m->err_pos = ptr - start + skip; /* >= 0 now */
880
881 /* and we still accept this non-token character */
Willy Tarreau801250e2018-09-11 11:45:04 +0200882 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 +0200883
Willy Tarreau801250e2018-09-11 11:45:04 +0200884 case H1_MSG_HDR_L1_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200885 http_msg_hdr_l1_sp:
886 /* assumes sol points to the first char */
887 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200888 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 +0200889
890 /* header value can be basically anything except CR/LF */
891 sov = ptr - start;
892
893 if (likely(!HTTP_IS_CRLF(*ptr))) {
894 goto http_msg_hdr_val;
895 }
896
897 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200898 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 +0200899 goto http_msg_hdr_l1_lf;
900
Willy Tarreau801250e2018-09-11 11:45:04 +0200901 case H1_MSG_HDR_L1_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200902 http_msg_hdr_l1_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200903 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
904 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 +0200905
Willy Tarreau801250e2018-09-11 11:45:04 +0200906 case H1_MSG_HDR_L1_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200907 http_msg_hdr_l1_lws:
908 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200909 if (!skip_update) {
910 /* replace HT,CR,LF with spaces */
911 for (; start + sov < ptr; sov++)
912 start[sov] = ' ';
913 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200914 goto http_msg_hdr_l1_sp;
915 }
916 /* we had a header consisting only in spaces ! */
917 eol = sov;
918 goto http_msg_complete_header;
919
Willy Tarreau801250e2018-09-11 11:45:04 +0200920 case H1_MSG_HDR_VAL:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200921 http_msg_hdr_val:
922 /* assumes sol points to the first char, and sov
923 * points to the first character of the value.
924 */
925
926 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
927 * and lower. In fact since most of the time is spent in the loop, we
928 * also remove the sign bit test so that bytes 0x8e..0x0d break the
929 * loop, but we don't care since they're very rare in header values.
930 */
Willy Tarreau02ac9502020-02-21 16:31:22 +0100931#ifdef HA_UNALIGNED_LE64
Willy Tarreau794f9af2017-07-26 09:07:47 +0200932 while (ptr <= end - sizeof(long)) {
933 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
934 goto http_msg_hdr_val2;
935 ptr += sizeof(long);
936 }
937#endif
Willy Tarreau02ac9502020-02-21 16:31:22 +0100938#ifdef HA_UNALIGNED_LE
Willy Tarreau794f9af2017-07-26 09:07:47 +0200939 while (ptr <= end - sizeof(int)) {
940 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
941 goto http_msg_hdr_val2;
942 ptr += sizeof(int);
943 }
944#endif
945 if (ptr >= end) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200946 state = H1_MSG_HDR_VAL;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200947 goto http_msg_ood;
948 }
949 http_msg_hdr_val2:
950 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200951 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 +0200952
953 eol = ptr - start;
954 /* Note: we could also copy eol into ->eoh so that we have the
955 * real header end in case it ends with lots of LWS, but is this
956 * really needed ?
957 */
958 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200959 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 +0200960 goto http_msg_hdr_l2_lf;
961
Willy Tarreau801250e2018-09-11 11:45:04 +0200962 case H1_MSG_HDR_L2_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200963 http_msg_hdr_l2_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200964 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
965 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 +0200966
Willy Tarreau801250e2018-09-11 11:45:04 +0200967 case H1_MSG_HDR_L2_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200968 http_msg_hdr_l2_lws:
969 if (unlikely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200970 if (!skip_update) {
971 /* LWS: replace HT,CR,LF with spaces */
972 for (; start + eol < ptr; eol++)
973 start[eol] = ' ';
974 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200975 goto http_msg_hdr_val;
976 }
977 http_msg_complete_header:
978 /*
979 * It was a new header, so the last one is finished. Assumes
980 * <sol> points to the first char of the name, <col> to the
981 * colon, <sov> points to the first character of the value and
982 * <eol> to the first CR or LF so we know how the line ends. We
983 * will trim spaces around the value. It's possible to do it by
984 * adjusting <eol> and <sov> which are no more used after this.
985 * We can add the header field to the list.
986 */
Christopher Faulet2912f872018-09-19 14:01:04 +0200987 if (likely(!skip_update)) {
988 while (sov < eol && HTTP_IS_LWS(start[sov]))
989 sov++;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200990
Christopher Faulet2912f872018-09-19 14:01:04 +0200991 while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
992 eol--;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200993
994
Christopher Faulet2912f872018-09-19 14:01:04 +0200995 n = ist2(start + sol, col - sol);
996 v = ist2(start + sov, eol - sov);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200997
Christopher Faulet2912f872018-09-19 14:01:04 +0200998 do {
999 int ret;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001000
Christopher Faulet2912f872018-09-19 14:01:04 +02001001 if (unlikely(hdr_count >= hdr_num)) {
1002 state = H1_MSG_HDR_L2_LWS;
1003 goto http_output_full;
1004 }
Willy Tarreau5384aac2018-09-11 16:04:48 +02001005
Christopher Faulet2912f872018-09-19 14:01:04 +02001006 if (isteqi(n, ist("transfer-encoding"))) {
Christopher Faulet545fbba2021-09-28 09:36:25 +02001007 ret = h1_parse_xfer_enc_header(h1m, v);
1008 if (ret < 0) {
1009 state = H1_MSG_HDR_L2_LWS;
1010 ptr = v.ptr; /* Set ptr on the error */
1011 goto http_msg_invalid;
1012 }
1013 else if (ret == 0) {
1014 /* skip it */
1015 break;
1016 }
Christopher Faulet2912f872018-09-19 14:01:04 +02001017 }
1018 else if (isteqi(n, ist("content-length"))) {
1019 ret = h1_parse_cont_len_header(h1m, &v);
Willy Tarreau73373ab2018-09-14 17:11:33 +02001020
Christopher Faulet2912f872018-09-19 14:01:04 +02001021 if (ret < 0) {
1022 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +01001023 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet2912f872018-09-19 14:01:04 +02001024 goto http_msg_invalid;
1025 }
1026 else if (ret == 0) {
1027 /* skip it */
1028 break;
1029 }
Willy Tarreau73373ab2018-09-14 17:11:33 +02001030 }
Christopher Faulet2912f872018-09-19 14:01:04 +02001031 else if (isteqi(n, ist("connection"))) {
Christopher Fauleta51ebb72019-03-29 15:03:13 +01001032 h1_parse_connection_header(h1m, &v);
1033 if (!v.len) {
1034 /* skip it */
1035 break;
1036 }
Willy Tarreau73373ab2018-09-14 17:11:33 +02001037 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001038 else if (isteqi(n, ist("upgrade"))) {
1039 h1_parse_upgrade_header(h1m, v);
1040 }
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001041 else if (!(h1m->flags & H1_MF_RESP) && isteqi(n, ist("host"))) {
1042 if (host_idx == -1)
Christopher Faulet497ab4f2019-10-11 09:01:44 +02001043 host_idx = hdr_count;
1044 else {
1045 if (!isteqi(v, hdr[host_idx].v)) {
1046 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +01001047 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet497ab4f2019-10-11 09:01:44 +02001048 goto http_msg_invalid;
1049 }
1050 /* if the same host, skip it */
1051 break;
1052 }
1053 }
Willy Tarreau2ea6bb52018-09-14 16:28:15 +02001054
Christopher Faulet2912f872018-09-19 14:01:04 +02001055 http_set_hdr(&hdr[hdr_count++], n, v);
1056 } while (0);
1057 }
Willy Tarreau794f9af2017-07-26 09:07:47 +02001058
1059 sol = ptr - start;
Christopher Faulet2912f872018-09-19 14:01:04 +02001060
Willy Tarreau794f9af2017-07-26 09:07:47 +02001061 if (likely(!HTTP_IS_CRLF(*ptr)))
1062 goto http_msg_hdr_name;
1063
1064 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +02001065 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 +02001066 goto http_msg_last_lf;
1067
Willy Tarreau801250e2018-09-11 11:45:04 +02001068 case H1_MSG_LAST_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +02001069 http_msg_last_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +02001070 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +02001071 ptr++;
1072 /* <ptr> now points to the first byte of payload. If needed sol
1073 * still points to the first of either CR or LF of the empty
1074 * line ending the headers block.
1075 */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001076 if (likely(!skip_update)) {
1077 if (unlikely(hdr_count >= hdr_num)) {
1078 state = H1_MSG_LAST_LF;
1079 goto http_output_full;
1080 }
Christopher Fauletff08a922018-09-25 13:59:46 +02001081 http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
Willy Tarreau794f9af2017-07-26 09:07:47 +02001082 }
Willy Tarreau001823c2018-09-12 17:25:32 +02001083
1084 /* reaching here we've parsed the whole message. We may detect
1085 * that we were already continuing an interrupted parsing pass
1086 * so we were silently looking for the end of message not
1087 * updating anything before deciding to parse it fully at once.
1088 * It's guaranteed that we won't match this test twice in a row
1089 * since restarting will turn zero.
1090 */
1091 if (restarting)
1092 goto restart;
1093
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001094
1095 if (!(h1m->flags & (H1_MF_HDRS_ONLY|H1_MF_RESP))) {
1096 struct http_uri_parser parser = http_uri_parser_init(sl.rq.u);
Christopher Faulete16ffb02022-11-22 10:04:16 +01001097 struct ist scheme, authority;
1098 int ret;
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001099
Christopher Faulete16ffb02022-11-22 10:04:16 +01001100 scheme = http_parse_scheme(&parser);
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001101 authority = http_parse_authority(&parser, 1);
1102 if (sl.rq.meth == HTTP_METH_CONNECT) {
1103 struct ist *host = ((host_idx != -1) ? &hdr[host_idx].v : NULL);
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001104
1105 ret = h1_validate_connect_authority(authority, host);
1106 if (ret < 0) {
1107 if (h1m->err_pos < -1) {
1108 state = H1_MSG_LAST_LF;
Willy Tarreau55d2e852022-10-04 08:02:03 +02001109 /* WT: gcc seems to see a path where sl.rq.u.ptr was used
1110 * uninitialized, but it doesn't know that the function is
1111 * called with initial states making this impossible.
1112 */
1113 ALREADY_CHECKED(sl.rq.u.ptr);
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001114 ptr = ((ret == -1) ? sl.rq.u.ptr : host->ptr); /* Set ptr on the error */
1115 goto http_msg_invalid;
1116 }
1117 if (h1m->err_pos == -1) /* capture the error pointer */
1118 h1m->err_pos = ((ret == -1) ? sl.rq.u.ptr : host->ptr) - start + skip; /* >= 0 now */
1119 }
1120 }
1121 else if (host_idx != -1 && istlen(authority)) {
1122 struct ist host = hdr[host_idx].v;
1123
1124 /* For non-CONNECT method, the authority must match the host header value */
1125 if (!isteqi(authority, host)) {
Christopher Faulete16ffb02022-11-22 10:04:16 +01001126 ret = h1_validate_mismatch_authority(scheme, authority, host);
1127 if (ret < 0) {
1128 if (h1m->err_pos < -1) {
1129 state = H1_MSG_LAST_LF;
1130 ptr = host.ptr; /* Set ptr on the error */
1131 goto http_msg_invalid;
1132 }
1133 if (h1m->err_pos == -1) /* capture the error pointer */
1134 h1m->err_pos = v.ptr - start + skip; /* >= 0 now */
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001135 }
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001136 }
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001137 }
1138 }
1139
Willy Tarreau2557f6a2018-09-14 16:34:47 +02001140 state = H1_MSG_DATA;
1141 if (h1m->flags & H1_MF_XFER_ENC) {
1142 if (h1m->flags & H1_MF_CLEN) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02001143 /* T-E + C-L: force close and remove C-L */
1144 h1m->flags |= H1_MF_CONN_CLO;
Willy Tarreau2557f6a2018-09-14 16:34:47 +02001145 h1m->flags &= ~H1_MF_CLEN;
1146 hdr_count = http_del_hdr(hdr, ist("content-length"));
1147 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02001148 else if (!(h1m->flags & H1_MF_VER_11)) {
1149 /* T-E + HTTP/1.0: force close */
1150 h1m->flags |= H1_MF_CONN_CLO;
1151 }
Willy Tarreau2557f6a2018-09-14 16:34:47 +02001152
1153 if (h1m->flags & H1_MF_CHNK)
1154 state = H1_MSG_CHUNK_SIZE;
1155 else if (!(h1m->flags & H1_MF_RESP)) {
1156 /* cf RFC7230#3.3.3 : transfer-encoding in
1157 * request without chunked encoding is invalid.
1158 */
1159 goto http_msg_invalid;
1160 }
1161 }
1162
Willy Tarreau794f9af2017-07-26 09:07:47 +02001163 break;
1164
1165 default:
1166 /* impossible states */
1167 goto http_msg_invalid;
1168 }
1169
Willy Tarreau001823c2018-09-12 17:25:32 +02001170 /* Now we've left the headers state and are either in H1_MSG_DATA or
1171 * H1_MSG_CHUNK_SIZE.
Willy Tarreau794f9af2017-07-26 09:07:47 +02001172 */
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001173
Willy Tarreau5384aac2018-09-11 16:04:48 +02001174 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001175 *slp = sl;
1176
Willy Tarreau4433c082018-09-11 15:33:32 +02001177 h1m->state = state;
1178 h1m->next = ptr - start + skip;
1179 return h1m->next;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001180
1181 http_msg_ood:
1182 /* out of data at <ptr> during state <state> */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001183 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001184 *slp = sl;
1185
Willy Tarreau4433c082018-09-11 15:33:32 +02001186 h1m->state = state;
1187 h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001188 return 0;
1189
1190 http_msg_invalid:
1191 /* invalid message, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001192 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001193 *slp = sl;
1194
Willy Tarreau4433c082018-09-11 15:33:32 +02001195 h1m->err_state = h1m->state = state;
1196 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001197 return -1;
1198
1199 http_output_full:
1200 /* no more room to store the current header, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001201 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001202 *slp = sl;
1203
Willy Tarreau4433c082018-09-11 15:33:32 +02001204 h1m->err_state = h1m->state = state;
1205 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001206 return -2;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001207
1208 restart:
Christopher Faulet02c89332021-12-01 18:01:48 +01001209 h1m->flags &= H1_MF_RESTART_MASK;
Christopher Faulet84f06532019-09-03 16:05:31 +02001210 h1m->curr_len = h1m->body_len = h1m->next = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +02001211 if (h1m->flags & H1_MF_RESP)
1212 h1m->state = H1_MSG_RPBEFORE;
1213 else
1214 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001215 goto try_again;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001216}
1217
Willy Tarreau2510f702017-10-31 17:14:16 +01001218/* This function performs a very minimal parsing of the trailers block present
Willy Tarreauf40e6822018-06-14 16:52:02 +02001219 * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of
Willy Tarreau7314be82018-06-14 13:32:50 +02001220 * bytes to delete to skip the trailers. It may return 0 if it's missing some
1221 * input data, or < 0 in case of parse error (in which case the caller may have
1222 * to decide how to proceed, possibly eating everything).
Willy Tarreau2510f702017-10-31 17:14:16 +01001223 */
Willy Tarreauf40e6822018-06-14 16:52:02 +02001224int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max)
Willy Tarreau2510f702017-10-31 17:14:16 +01001225{
Willy Tarreauf40e6822018-06-14 16:52:02 +02001226 const char *stop = b_peek(buf, ofs + max);
1227 int count = ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +01001228
1229 while (1) {
1230 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau7314be82018-06-14 13:32:50 +02001231 const char *start = b_peek(buf, count);
Willy Tarreau2510f702017-10-31 17:14:16 +01001232 const char *ptr = start;
Willy Tarreau2510f702017-10-31 17:14:16 +01001233
1234 /* scan current line and stop at LF or CRLF */
1235 while (1) {
1236 if (ptr == stop)
1237 return 0;
1238
1239 if (*ptr == '\n') {
1240 if (!p1)
1241 p1 = ptr;
1242 p2 = ptr;
1243 break;
1244 }
1245
1246 if (*ptr == '\r') {
1247 if (p1)
1248 return -1;
1249 p1 = ptr;
1250 }
1251
Willy Tarreau7314be82018-06-14 13:32:50 +02001252 ptr = b_next(buf, ptr);
Willy Tarreau2510f702017-10-31 17:14:16 +01001253 }
1254
1255 /* after LF; point to beginning of next line */
Willy Tarreau7314be82018-06-14 13:32:50 +02001256 p2 = b_next(buf, p2);
1257 count += b_dist(buf, start, p2);
Willy Tarreau2510f702017-10-31 17:14:16 +01001258
1259 /* LF/CRLF at beginning of line => end of trailers at p2.
1260 * Everything was scheduled for forwarding, there's nothing left
1261 * from this message. */
1262 if (p1 == start)
1263 break;
1264 /* OK, next line then */
1265 }
Willy Tarreauf40e6822018-06-14 16:52:02 +02001266 return count - ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +01001267}
Amaury Denoyellec1938232020-12-11 17:53:03 +01001268
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001269/* Generate a random key for a WebSocket Handshake in respect with rfc6455
1270 * The key is 128-bits long encoded as a base64 string in <key_out> parameter
1271 * (25 bytes long).
1272 */
1273void h1_generate_random_ws_input_key(char key_out[25])
1274{
1275 /* generate a random websocket key */
1276 const uint64_t rand1 = ha_random64(), rand2 = ha_random64();
1277 char key[16];
1278
1279 memcpy(key, &rand1, 8);
1280 memcpy(&key[8], &rand2, 8);
1281 a2base64(key, 16, key_out, 25);
1282}
1283
Amaury Denoyellec1938232020-12-11 17:53:03 +01001284#define H1_WS_KEY_SUFFIX_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
1285
1286/*
1287 * Calculate the WebSocket handshake response key from <key_in>. Following the
1288 * rfc6455, <key_in> must be 24 bytes longs. The result is stored in <key_out>
1289 * as a 29 bytes long string.
1290 */
1291void h1_calculate_ws_output_key(const char *key, char *result)
1292{
1293 blk_SHA_CTX sha1_ctx;
1294 char hash_in[60], hash_out[20];
1295
1296 /* concatenate the key with a fixed suffix */
1297 memcpy(hash_in, key, 24);
1298 memcpy(&hash_in[24], H1_WS_KEY_SUFFIX_GUID, 36);
1299
1300 /* sha1 the result */
1301 blk_SHA1_Init(&sha1_ctx);
1302 blk_SHA1_Update(&sha1_ctx, hash_in, 60);
1303 blk_SHA1_Final((unsigned char *)hash_out, &sha1_ctx);
1304
1305 /* encode in base64 the hash */
1306 a2base64(hash_out, 20, result, 29);
1307}