blob: 88a54c4a593d5e3450018b06a58f5769543b664a [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
Christopher Faulet545fbba2021-09-28 09:36:25 +020096 * "chunked" encoding to perform some checks (it must be the last encoding for
97 * the request and must not be performed twice for any message). The
98 * H1_MF_TE_CHUNKED is set if a valid "chunked" encoding is found. The
99 * H1_MF_TE_OTHER flag is set if any other encoding is found. The H1_MF_XFER_ENC
100 * flag is always set. The H1_MF_CHNK is set when "chunked" encoding is the last
101 * one. Note that transfer codings are case-insensitive (cf RFC7230#4). This
102 * function returns <0 if a error is found, 0 if the whole header can be dropped
103 * (not used yet), or >0 if the value can be indexed.
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200104 */
Christopher Faulet545fbba2021-09-28 09:36:25 +0200105int h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value)
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200106{
107 char *e, *n;
108 struct ist word;
109
110 h1m->flags |= H1_MF_XFER_ENC;
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200111
112 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100113 e = istend(value);
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200114
115 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500116 /* skip leading delimiter and blanks */
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200117 if (HTTP_IS_LWS(*word.ptr))
118 continue;
119
120 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
121 word.len = n - word.ptr;
122
123 /* trim trailing blanks */
124 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
125 word.len--;
126
127 h1m->flags &= ~H1_MF_CHNK;
Christopher Faulet545fbba2021-09-28 09:36:25 +0200128 if (isteqi(word, ist("chunked"))) {
129 if (h1m->flags & H1_MF_TE_CHUNKED) {
130 /* cf RFC7230#3.3.1 : A sender MUST NOT apply
131 * chunked more than once to a message body
132 * (i.e., chunking an already chunked message is
133 * not allowed)
134 */
135 goto fail;
136 }
137 h1m->flags |= (H1_MF_TE_CHUNKED|H1_MF_CHNK);
138 }
139 else {
140 if ((h1m->flags & (H1_MF_RESP|H1_MF_TE_CHUNKED)) == H1_MF_TE_CHUNKED) {
141 /* cf RFC7230#3.3.1 : If any transfer coding
142 * other than chunked is applied to a request
143 * payload body, the sender MUST apply chunked
144 * as the final transfer coding to ensure that
145 * the message is properly framed.
146 */
147 goto fail;
148 }
149 h1m->flags |= H1_MF_TE_OTHER;
150 }
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200151
152 word.ptr = n;
153 }
Christopher Faulet545fbba2021-09-28 09:36:25 +0200154
155 return 1;
156 fail:
157 return -1;
Willy Tarreau2557f6a2018-09-14 16:34:47 +0200158}
159
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200160/* Validate the authority and the host header value for CONNECT method. If there
161 * is hast header, its value is normalized. 0 is returned on success, -1 if the
162 * authority is invalid and -2 if the host is invalid.
163 */
164static int h1_validate_connect_authority(struct ist authority, struct ist *host_hdr)
165{
166 struct ist uri_host, uri_port, host, host_port;
167
168 if (!isttest(authority))
169 goto invalid_authority;
170 uri_host = authority;
171 uri_port = http_get_host_port(authority);
Christopher Faulet75348c22022-11-22 10:27:54 +0100172 if (!istlen(uri_port))
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200173 goto invalid_authority;
174 uri_host.len -= (istlen(uri_port) + 1);
175
176 if (!host_hdr || !isttest(*host_hdr))
177 goto end;
178
179 /* Get the port of the host header value, if any */
180 host = *host_hdr;
181 host_port = http_get_host_port(*host_hdr);
Christopher Faulet75348c22022-11-22 10:27:54 +0100182 if (isttest(host_port))
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200183 host.len -= (istlen(host_port) + 1);
Christopher Faulet75348c22022-11-22 10:27:54 +0100184
185 if (istlen(host_port)) {
Christopher Faulet3f5fbe92022-07-05 14:50:17 +0200186 if (!isteqi(host, uri_host) || !isteq(host_port, uri_port))
187 goto invalid_host;
188 if (http_is_default_port(IST_NULL, uri_port))
189 *host_hdr = host; /* normalize */
190 }
191 else {
192 if (!http_is_default_port(IST_NULL, uri_port) || !isteqi(host, uri_host))
193 goto invalid_host;
194 }
195
196 end:
197 return 0;
198
199 invalid_authority:
200 return -1;
201
202 invalid_host:
203 return -2;
204}
205
Christopher Faulete16ffb02022-11-22 10:04:16 +0100206
207/* Validate the authority and the host header value for non-CONNECT method, when
208 * an absolute-URI is detected but when it does not exactly match the host
209 * value. The idea is to detect default port (http or https). authority and host
210 * are defined here. 0 is returned on success, -1 if the host is does not match
211 * the authority.
212 */
213static int h1_validate_mismatch_authority(struct ist scheme, struct ist authority, struct ist host_hdr)
214{
215 struct ist uri_host, uri_port, host, host_port;
216
217 if (!isttest(scheme))
218 goto mismatch;
219
220 uri_host = authority;
221 uri_port = http_get_host_port(authority);
222 if (isttest(uri_port))
223 uri_host.len -= (istlen(uri_port) + 1);
224
225 host = host_hdr;
226 host_port = http_get_host_port(host_hdr);
227 if (isttest(host_port))
228 host.len -= (istlen(host_port) + 1);
229
230 if (!isttest(uri_port) && !isttest(host_port)) {
231 /* No port on both: we already know the authority does not match
232 * the host value
233 */
234 goto mismatch;
235 }
236 else if (isttest(uri_port) && !http_is_default_port(scheme, uri_port)) {
237 /* here there is no port for the host value and the port for the
238 * authority is not the default one
239 */
240 goto mismatch;
241 }
242 else if (isttest(host_port) && !http_is_default_port(scheme, host_port)) {
243 /* here there is no port for the authority and the port for the
244 * host value is not the default one
245 */
246 goto mismatch;
247 }
248 else {
249 /* the authority or the host value contain a default port and
250 * there is no port on the other value
251 */
252 if (!isteqi(uri_host, host))
253 goto mismatch;
254 }
255
256 return 0;
257
258 mismatch:
259 return -1;
260}
261
262
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200263/* Parse the Connection: header of an HTTP/1 request, looking for "close",
264 * "keep-alive", and "upgrade" values, and updating h1m->flags according to
265 * what was found there. Note that flags are only added, not removed, so the
266 * function is safe for being called multiple times if multiple occurrences
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100267 * are found. If the flag H1_MF_CLEAN_CONN_HDR, the header value is cleaned
268 * up from "keep-alive" and "close" values. To do so, the header value is
269 * rewritten in place and its length is updated.
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200270 */
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100271void h1_parse_connection_header(struct h1m *h1m, struct ist *value)
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200272{
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100273 char *e, *n, *p;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200274 struct ist word;
275
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100276 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
277 p = value->ptr;
278 e = value->ptr + value->len;
279 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
280 value->len = 0;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200281
282 while (++word.ptr < e) {
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500283 /* skip leading delimiter and blanks */
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200284 if (HTTP_IS_LWS(*word.ptr))
285 continue;
286
287 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
288 word.len = n - word.ptr;
289
290 /* trim trailing blanks */
291 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
292 word.len--;
293
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100294 if (isteqi(word, ist("keep-alive"))) {
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200295 h1m->flags |= H1_MF_CONN_KAL;
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100296 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
297 goto skip_val;
298 }
299 else if (isteqi(word, ist("close"))) {
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200300 h1m->flags |= H1_MF_CONN_CLO;
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100301 if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
302 goto skip_val;
303 }
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200304 else if (isteqi(word, ist("upgrade")))
305 h1m->flags |= H1_MF_CONN_UPG;
306
Christopher Fauleta51ebb72019-03-29 15:03:13 +0100307 if (h1m->flags & H1_MF_CLEAN_CONN_HDR) {
308 if (value->ptr + value->len == p) {
309 /* no rewrite done till now */
310 value->len = n - value->ptr;
311 }
312 else {
313 if (value->len)
314 value->ptr[value->len++] = ',';
315 istcat(value, word, e - value->ptr);
316 }
317 }
318
319 skip_val:
320 word.ptr = p = n;
Willy Tarreau98f5cf72018-09-13 14:15:58 +0200321 }
322}
323
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +0100324/* Parse the Upgrade: header of an HTTP/1 request.
325 * If "websocket" is found, set H1_MF_UPG_WEBSOCKET flag
326 */
327void h1_parse_upgrade_header(struct h1m *h1m, struct ist value)
328{
329 char *e, *n;
330 struct ist word;
331
332 h1m->flags &= ~H1_MF_UPG_WEBSOCKET;
333
334 word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100335 e = istend(value);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +0100336
337 while (++word.ptr < e) {
338 /* skip leading delimiter and blanks */
339 if (HTTP_IS_LWS(*word.ptr))
340 continue;
341
342 n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
343 word.len = n - word.ptr;
344
345 /* trim trailing blanks */
346 while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
347 word.len--;
348
349 if (isteqi(word, ist("websocket")))
350 h1m->flags |= H1_MF_UPG_WEBSOCKET;
351
352 word.ptr = n;
353 }
354}
355
Willy Tarreau538746a2018-12-11 10:59:20 +0100356/* Macros used in the HTTP/1 parser, to check for the expected presence of
357 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
358 */
359
360/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
361 * <bad>.
362 */
363#define EXPECT_LF_HERE(ptr, bad, state, where) \
364 do { \
365 if (unlikely(*(ptr) != '\n')) { \
366 state = (where); \
367 goto bad; \
368 } \
369 } while (0)
370
371/* Increments pointer <ptr>, continues to label <more> if it's still below
372 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
373 * of buffer was reached.
374 */
375#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
376 do { \
377 if (likely(++(ptr) < (end))) \
378 goto more; \
379 else { \
380 state = (where); \
381 goto stop; \
382 } \
383 } while (0)
384
Willy Tarreau794f9af2017-07-26 09:07:47 +0200385/* This function parses a contiguous HTTP/1 headers block starting at <start>
386 * and ending before <stop>, at once, and converts it a list of (name,value)
387 * pairs representing header fields into the array <hdr> of size <hdr_num>,
388 * whose last entry will have an empty name and an empty value. If <hdr_num> is
Willy Tarreau4433c082018-09-11 15:33:32 +0200389 * too small to represent the whole message, an error is returned. Some
390 * protocol elements such as content-length and transfer-encoding will be
Willy Tarreau5384aac2018-09-11 16:04:48 +0200391 * parsed and stored into h1m as well. <hdr> may be null, in which case only
392 * the parsing state will be updated. This may be used to restart the parsing
393 * where it stopped for example.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200394 *
395 * For now it's limited to the response. If the header block is incomplete,
396 * 0 is returned, waiting to be called again with more data to try it again.
Willy Tarreau4433c082018-09-11 15:33:32 +0200397 * The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE,
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200398 * and h1m->next to zero on the first call, the parser will do the rest. If
399 * an incomplete message is seen, the caller only needs to present h1m->state
400 * and h1m->next again, with an empty header list so that the parser can start
401 * again. In this case, it will detect that it interrupted a previous session
402 * and will first look for the end of the message before reparsing it again and
403 * indexing it at the same time. This ensures that incomplete messages fed 1
404 * character at a time are never processed entirely more than exactly twice,
405 * and that there is no need to store all the internal state and pre-parsed
406 * headers or start line between calls.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200407 *
Willy Tarreaua41393f2018-09-11 15:34:50 +0200408 * A pointer to a start line descriptor may be passed in <slp>, in which case
409 * the parser will fill it with whatever it found.
410 *
Willy Tarreau794f9af2017-07-26 09:07:47 +0200411 * The code derived from the main HTTP/1 parser above but was simplified and
412 * optimized to process responses produced or forwarded by haproxy. The caller
413 * is responsible for ensuring that the message doesn't wrap, and should ensure
414 * it is complete to avoid having to retry the operation after a failed
415 * attempt. The message is not supposed to be invalid, which is why a few
416 * properties such as the character set used in the header field names are not
417 * checked. In case of an unparsable response message, a negative value will be
418 * returned with h1m->err_pos and h1m->err_state matching the location and
419 * state where the error was met. Leading blank likes are tolerated but not
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100420 * recommended. If flag H1_MF_HDRS_ONLY is set in h1m->flags, only headers are
421 * parsed and the start line is skipped. It is not required to set h1m->state
422 * nor h1m->next in this case.
Willy Tarreau794f9af2017-07-26 09:07:47 +0200423 *
424 * This function returns :
425 * -1 in case of error. In this case, h1m->err_state is filled (if h1m is
Willy Tarreau801250e2018-09-11 11:45:04 +0200426 * set) with the state the error occurred in and h1m->err_pos with the
Willy Tarreau794f9af2017-07-26 09:07:47 +0200427 * the position relative to <start>
428 * -2 if the output is full (hdr_num reached). err_state and err_pos also
429 * indicate where it failed.
430 * 0 in case of missing data.
431 * > 0 on success, it then corresponds to the number of bytes read since
432 * <start> so that the caller can go on with the payload.
433 */
434int h1_headers_to_hdr_list(char *start, const char *stop,
435 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +0200436 struct h1m *h1m, union h1_sl *slp)
Willy Tarreau794f9af2017-07-26 09:07:47 +0200437{
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200438 enum h1m_state state;
439 register char *ptr;
440 register const char *end;
441 unsigned int hdr_count;
442 unsigned int skip; /* number of bytes skipped at the beginning */
443 unsigned int sol; /* start of line */
444 unsigned int col; /* position of the colon */
445 unsigned int eol; /* end of line */
446 unsigned int sov; /* start of value */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200447 union h1_sl sl;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200448 int skip_update;
449 int restarting;
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200450 int host_idx;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200451 struct ist n, v; /* header name and value during parsing */
452
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200453 skip = 0; // do it only once to keep track of the leading CRLF.
454
455 try_again:
456 hdr_count = sol = col = eol = sov = 0;
Willy Tarreaua41393f2018-09-11 15:34:50 +0200457 sl.st.status = 0;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200458 skip_update = restarting = 0;
Christopher Faulet497ab4f2019-10-11 09:01:44 +0200459 host_idx = -1;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200460
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100461 if (h1m->flags & H1_MF_HDRS_ONLY) {
462 state = H1_MSG_HDR_FIRST;
463 h1m->next = 0;
464 }
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100465 else {
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100466 state = h1m->state;
Christopher Faulet68b1bbd2019-01-04 16:06:48 +0100467 if (h1m->state != H1_MSG_RQBEFORE && h1m->state != H1_MSG_RPBEFORE)
468 restarting = 1;
469 }
Willy Tarreau0f8fb6b2019-01-04 10:48:03 +0100470
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200471 ptr = start + h1m->next;
472 end = stop;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200473
Willy Tarreau794f9af2017-07-26 09:07:47 +0200474 if (unlikely(ptr >= end))
475 goto http_msg_ood;
476
Willy Tarreau4c34c0e2018-09-11 16:20:30 +0200477 /* don't update output if hdr is NULL or if we're restarting */
478 if (!hdr || restarting)
Willy Tarreau5384aac2018-09-11 16:04:48 +0200479 skip_update = 1;
480
Willy Tarreau794f9af2017-07-26 09:07:47 +0200481 switch (state) {
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200482 case H1_MSG_RQBEFORE:
483 http_msg_rqbefore:
484 if (likely(HTTP_IS_TOKEN(*ptr))) {
485 /* we have a start of message, we may have skipped some
486 * heading CRLF. Skip them now.
487 */
488 skip += ptr - start;
489 start = ptr;
490
491 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200492 sl.rq.m.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200493 hdr_count = 0;
494 state = H1_MSG_RQMETH;
495 goto http_msg_rqmeth;
496 }
497
498 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
499 state = H1_MSG_RQBEFORE;
500 goto http_msg_invalid;
501 }
502
503 if (unlikely(*ptr == '\n'))
504 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
505 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
506 /* stop here */
507
508 case H1_MSG_RQBEFORE_CR:
509 http_msg_rqbefore_cr:
510 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
511 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
512 /* stop here */
513
514 case H1_MSG_RQMETH:
515 http_msg_rqmeth:
516 if (likely(HTTP_IS_TOKEN(*ptr)))
517 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
518
519 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200520 sl.rq.m.len = ptr - sl.rq.m.ptr;
521 sl.rq.meth = find_http_meth(start, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200522 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
523 }
524
525 if (likely(HTTP_IS_CRLF(*ptr))) {
526 /* HTTP 0.9 request */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200527 sl.rq.m.len = ptr - sl.rq.m.ptr;
528 sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200529 http_msg_req09_uri:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200530 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200531 http_msg_req09_uri_e:
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200532 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200533 http_msg_req09_ver:
Tim Duesterhus77508502022-03-15 13:11:06 +0100534 sl.rq.v = ist2(ptr, 0);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200535 goto http_msg_rqline_eol;
536 }
537 state = H1_MSG_RQMETH;
538 goto http_msg_invalid;
539
540 case H1_MSG_RQMETH_SP:
541 http_msg_rqmeth_sp:
542 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200543 sl.rq.u.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200544 goto http_msg_rquri;
545 }
546 if (likely(HTTP_IS_SPHT(*ptr)))
547 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
548 /* so it's a CR/LF, meaning an HTTP 0.9 request */
549 goto http_msg_req09_uri;
550
551 case H1_MSG_RQURI:
552 http_msg_rquri:
Willy Tarreau02ac9502020-02-21 16:31:22 +0100553#ifdef HA_UNALIGNED_LE
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200554 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
555 while (ptr <= end - sizeof(int)) {
556 int x = *(int *)ptr - 0x21212121;
557 if (x & 0x80808080)
558 break;
559
560 x -= 0x5e5e5e5e;
561 if (!(x & 0x80808080))
562 break;
563
564 ptr += sizeof(int);
565 }
566#endif
567 if (ptr >= end) {
568 state = H1_MSG_RQURI;
569 goto http_msg_ood;
570 }
571 http_msg_rquri2:
572 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
573 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
574
575 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200576 sl.rq.u.len = ptr - sl.rq.u.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200577 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
578 }
579 if (likely((unsigned char)*ptr >= 128)) {
580 /* non-ASCII chars are forbidden unless option
581 * accept-invalid-http-request is enabled in the frontend.
582 * In any case, we capture the faulty char.
583 */
584 if (h1m->err_pos < -1)
585 goto invalid_char;
586 if (h1m->err_pos == -1)
587 h1m->err_pos = ptr - start + skip;
588 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
589 }
590
591 if (likely(HTTP_IS_CRLF(*ptr))) {
592 /* so it's a CR/LF, meaning an HTTP 0.9 request */
593 goto http_msg_req09_uri_e;
594 }
595
596 /* OK forbidden chars, 0..31 or 127 */
597 invalid_char:
598 state = H1_MSG_RQURI;
599 goto http_msg_invalid;
600
601 case H1_MSG_RQURI_SP:
602 http_msg_rquri_sp:
603 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200604 sl.rq.v.ptr = ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200605 goto http_msg_rqver;
606 }
607 if (likely(HTTP_IS_SPHT(*ptr)))
608 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
609 /* so it's a CR/LF, meaning an HTTP 0.9 request */
610 goto http_msg_req09_ver;
611
612
613 case H1_MSG_RQVER:
614 http_msg_rqver:
615 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
616 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
617
618 if (likely(HTTP_IS_CRLF(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200619 sl.rq.v.len = ptr - sl.rq.v.ptr;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200620 http_msg_rqline_eol:
621 /* We have seen the end of line. Note that we do not
622 * necessarily have the \n yet, but at least we know that we
623 * have EITHER \r OR \n, otherwise the request would not be
624 * complete. We can then record the request length and return
625 * to the caller which will be able to register it.
626 */
627
628 if (likely(!skip_update)) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200629 if ((sl.rq.v.len == 8) &&
630 (*(sl.rq.v.ptr + 5) > '1' ||
631 (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200632 h1m->flags |= H1_MF_VER_11;
633
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200634 if (unlikely(hdr_count >= hdr_num)) {
635 state = H1_MSG_RQVER;
636 goto http_output_full;
637 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200638 if (!(h1m->flags & H1_MF_NO_PHDR))
639 http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200640
641 if (unlikely(hdr_count >= hdr_num)) {
642 state = H1_MSG_RQVER;
643 goto http_output_full;
644 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200645 if (!(h1m->flags & H1_MF_NO_PHDR))
646 http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200647 }
648
649 sol = ptr - start;
650 if (likely(*ptr == '\r'))
651 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
652 goto http_msg_rqline_end;
653 }
654
655 /* neither an HTTP_VER token nor a CRLF */
656 state = H1_MSG_RQVER;
657 goto http_msg_invalid;
658
659 case H1_MSG_RQLINE_END:
660 http_msg_rqline_end:
661 /* check for HTTP/0.9 request : no version information
662 * available. sol must point to the first of CR or LF. However
663 * since we don't save these elements between calls, if we come
664 * here from a restart, we don't necessarily know. Thus in this
665 * case we simply start over.
666 */
667 if (restarting)
668 goto restart;
669
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200670 if (unlikely(sl.rq.v.len == 0))
Willy Tarreauc2ab9f52018-09-11 17:57:05 +0200671 goto http_msg_last_lf;
672
673 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
674 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
675 /* stop here */
676
677 /*
678 * Common states below
679 */
Willy Tarreau801250e2018-09-11 11:45:04 +0200680 case H1_MSG_RPBEFORE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200681 http_msg_rpbefore:
682 if (likely(HTTP_IS_TOKEN(*ptr))) {
683 /* we have a start of message, we may have skipped some
684 * heading CRLF. Skip them now.
685 */
686 skip += ptr - start;
687 start = ptr;
688
689 sol = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200690 sl.st.v.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200691 hdr_count = 0;
Willy Tarreau801250e2018-09-11 11:45:04 +0200692 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200693 goto http_msg_rpver;
694 }
695
696 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200697 state = H1_MSG_RPBEFORE;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200698 goto http_msg_invalid;
699 }
700
701 if (unlikely(*ptr == '\n'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200702 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
703 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 +0200704 /* stop here */
705
Willy Tarreau801250e2018-09-11 11:45:04 +0200706 case H1_MSG_RPBEFORE_CR:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200707 http_msg_rpbefore_cr:
Willy Tarreau801250e2018-09-11 11:45:04 +0200708 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
709 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200710 /* stop here */
711
Willy Tarreau801250e2018-09-11 11:45:04 +0200712 case H1_MSG_RPVER:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200713 http_msg_rpver:
714 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200715 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200716
717 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200718 sl.st.v.len = ptr - sl.st.v.ptr;
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200719
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200720 if ((sl.st.v.len == 8) &&
721 (*(sl.st.v.ptr + 5) > '1' ||
722 (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
Willy Tarreauba5fbca2018-09-13 11:32:51 +0200723 h1m->flags |= H1_MF_VER_11;
724
Willy Tarreau801250e2018-09-11 11:45:04 +0200725 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 +0200726 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200727 state = H1_MSG_RPVER;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200728 goto http_msg_invalid;
729
Willy Tarreau801250e2018-09-11 11:45:04 +0200730 case H1_MSG_RPVER_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200731 http_msg_rpver_sp:
732 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200733 sl.st.status = 0;
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200734 sl.st.c.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200735 goto http_msg_rpcode;
736 }
737 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200738 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 +0200739 /* so it's a CR/LF, this is invalid */
Willy Tarreau801250e2018-09-11 11:45:04 +0200740 state = H1_MSG_RPVER_SP;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200741 goto http_msg_invalid;
742
Willy Tarreau801250e2018-09-11 11:45:04 +0200743 case H1_MSG_RPCODE:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200744 http_msg_rpcode:
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100745 if (likely(HTTP_IS_DIGIT(*ptr))) {
Willy Tarreaua41393f2018-09-11 15:34:50 +0200746 sl.st.status = sl.st.status * 10 + *ptr - '0';
Willy Tarreau801250e2018-09-11 11:45:04 +0200747 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200748 }
749
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100750 if (unlikely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200751 state = H1_MSG_RPCODE;
Willy Tarreau1b4cf9b2017-11-09 11:15:45 +0100752 goto http_msg_invalid;
753 }
754
Willy Tarreau794f9af2017-07-26 09:07:47 +0200755 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200756 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau801250e2018-09-11 11:45:04 +0200757 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 +0200758 }
759
760 /* so it's a CR/LF, so there is no reason phrase */
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200761 sl.st.c.len = ptr - sl.st.c.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200762
763 http_msg_rsp_reason:
Tim Duesterhus77508502022-03-15 13:11:06 +0100764 sl.st.r = ist2(ptr, 0);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200765 goto http_msg_rpline_eol;
766
Willy Tarreau801250e2018-09-11 11:45:04 +0200767 case H1_MSG_RPCODE_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200768 http_msg_rpcode_sp:
769 if (likely(!HTTP_IS_LWS(*ptr))) {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200770 sl.st.r.ptr = ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200771 goto http_msg_rpreason;
772 }
773 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200774 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 +0200775 /* so it's a CR/LF, so there is no reason phrase */
776 goto http_msg_rsp_reason;
777
Willy Tarreau801250e2018-09-11 11:45:04 +0200778 case H1_MSG_RPREASON:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200779 http_msg_rpreason:
780 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200781 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200782 sl.st.r.len = ptr - sl.st.r.ptr;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200783 http_msg_rpline_eol:
784 /* We have seen the end of line. Note that we do not
785 * necessarily have the \n yet, but at least we know that we
786 * have EITHER \r OR \n, otherwise the response would not be
787 * complete. We can then record the response length and return
788 * to the caller which will be able to register it.
789 */
790
Willy Tarreau5384aac2018-09-11 16:04:48 +0200791 if (likely(!skip_update)) {
792 if (unlikely(hdr_count >= hdr_num)) {
793 state = H1_MSG_RPREASON;
794 goto http_output_full;
795 }
Christopher Faulet25da9e32018-10-08 15:50:15 +0200796 if (!(h1m->flags & H1_MF_NO_PHDR))
797 http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200798 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200799
800 sol = ptr - start;
801 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200802 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 +0200803 goto http_msg_rpline_end;
804
Willy Tarreau801250e2018-09-11 11:45:04 +0200805 case H1_MSG_RPLINE_END:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200806 http_msg_rpline_end:
807 /* sol must point to the first of CR or LF. */
Willy Tarreau801250e2018-09-11 11:45:04 +0200808 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
809 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 +0200810 /* stop here */
811
Willy Tarreau801250e2018-09-11 11:45:04 +0200812 case H1_MSG_HDR_FIRST:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200813 http_msg_hdr_first:
814 sol = ptr - start;
815 if (likely(!HTTP_IS_CRLF(*ptr))) {
816 goto http_msg_hdr_name;
817 }
818
819 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200820 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 +0200821 goto http_msg_last_lf;
822
Willy Tarreau801250e2018-09-11 11:45:04 +0200823 case H1_MSG_HDR_NAME:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200824 http_msg_hdr_name:
825 /* assumes sol points to the first char */
826 if (likely(HTTP_IS_TOKEN(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200827 if (!skip_update) {
828 /* turn it to lower case if needed */
829 if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
Willy Tarreauf278eec2020-07-05 21:46:32 +0200830 *ptr = tolower((unsigned char)*ptr);
Christopher Faulet2912f872018-09-19 14:01:04 +0200831 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200832 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 +0200833 }
834
835 if (likely(*ptr == ':')) {
836 col = ptr - start;
Willy Tarreaua8598a22023-02-09 21:36:54 +0100837 if (col <= sol) {
838 state = H1_MSG_HDR_NAME;
839 goto http_msg_invalid;
840 }
Willy Tarreau801250e2018-09-11 11:45:04 +0200841 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 +0200842 }
843
Willy Tarreau9aec3052018-09-12 09:20:40 +0200844 if (likely(h1m->err_pos < -1) || *ptr == '\n') {
Willy Tarreau801250e2018-09-11 11:45:04 +0200845 state = H1_MSG_HDR_NAME;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200846 goto http_msg_invalid;
847 }
848
Willy Tarreau9aec3052018-09-12 09:20:40 +0200849 if (h1m->err_pos == -1) /* capture the error pointer */
850 h1m->err_pos = ptr - start + skip; /* >= 0 now */
851
852 /* and we still accept this non-token character */
Willy Tarreau801250e2018-09-11 11:45:04 +0200853 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 +0200854
Willy Tarreau801250e2018-09-11 11:45:04 +0200855 case H1_MSG_HDR_L1_SP:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200856 http_msg_hdr_l1_sp:
857 /* assumes sol points to the first char */
858 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200859 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 +0200860
861 /* header value can be basically anything except CR/LF */
862 sov = ptr - start;
863
864 if (likely(!HTTP_IS_CRLF(*ptr))) {
865 goto http_msg_hdr_val;
866 }
867
868 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200869 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 +0200870 goto http_msg_hdr_l1_lf;
871
Willy Tarreau801250e2018-09-11 11:45:04 +0200872 case H1_MSG_HDR_L1_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200873 http_msg_hdr_l1_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200874 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
875 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 +0200876
Willy Tarreau801250e2018-09-11 11:45:04 +0200877 case H1_MSG_HDR_L1_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200878 http_msg_hdr_l1_lws:
879 if (likely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200880 if (!skip_update) {
881 /* replace HT,CR,LF with spaces */
882 for (; start + sov < ptr; sov++)
883 start[sov] = ' ';
884 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200885 goto http_msg_hdr_l1_sp;
886 }
887 /* we had a header consisting only in spaces ! */
888 eol = sov;
889 goto http_msg_complete_header;
890
Willy Tarreau801250e2018-09-11 11:45:04 +0200891 case H1_MSG_HDR_VAL:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200892 http_msg_hdr_val:
893 /* assumes sol points to the first char, and sov
894 * points to the first character of the value.
895 */
896
897 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
898 * and lower. In fact since most of the time is spent in the loop, we
899 * also remove the sign bit test so that bytes 0x8e..0x0d break the
900 * loop, but we don't care since they're very rare in header values.
901 */
Willy Tarreau02ac9502020-02-21 16:31:22 +0100902#ifdef HA_UNALIGNED_LE64
Willy Tarreau794f9af2017-07-26 09:07:47 +0200903 while (ptr <= end - sizeof(long)) {
904 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
905 goto http_msg_hdr_val2;
906 ptr += sizeof(long);
907 }
908#endif
Willy Tarreau02ac9502020-02-21 16:31:22 +0100909#ifdef HA_UNALIGNED_LE
Willy Tarreau794f9af2017-07-26 09:07:47 +0200910 while (ptr <= end - sizeof(int)) {
911 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
912 goto http_msg_hdr_val2;
913 ptr += sizeof(int);
914 }
915#endif
916 if (ptr >= end) {
Willy Tarreau801250e2018-09-11 11:45:04 +0200917 state = H1_MSG_HDR_VAL;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200918 goto http_msg_ood;
919 }
920 http_msg_hdr_val2:
921 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau801250e2018-09-11 11:45:04 +0200922 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 +0200923
924 eol = ptr - start;
925 /* Note: we could also copy eol into ->eoh so that we have the
926 * real header end in case it ends with lots of LWS, but is this
927 * really needed ?
928 */
929 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +0200930 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 +0200931 goto http_msg_hdr_l2_lf;
932
Willy Tarreau801250e2018-09-11 11:45:04 +0200933 case H1_MSG_HDR_L2_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200934 http_msg_hdr_l2_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +0200935 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
936 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 +0200937
Willy Tarreau801250e2018-09-11 11:45:04 +0200938 case H1_MSG_HDR_L2_LWS:
Willy Tarreau794f9af2017-07-26 09:07:47 +0200939 http_msg_hdr_l2_lws:
940 if (unlikely(HTTP_IS_SPHT(*ptr))) {
Christopher Faulet2912f872018-09-19 14:01:04 +0200941 if (!skip_update) {
942 /* LWS: replace HT,CR,LF with spaces */
943 for (; start + eol < ptr; eol++)
944 start[eol] = ' ';
945 }
Willy Tarreau794f9af2017-07-26 09:07:47 +0200946 goto http_msg_hdr_val;
947 }
948 http_msg_complete_header:
949 /*
950 * It was a new header, so the last one is finished. Assumes
951 * <sol> points to the first char of the name, <col> to the
952 * colon, <sov> points to the first character of the value and
953 * <eol> to the first CR or LF so we know how the line ends. We
954 * will trim spaces around the value. It's possible to do it by
955 * adjusting <eol> and <sov> which are no more used after this.
956 * We can add the header field to the list.
957 */
Christopher Faulet2912f872018-09-19 14:01:04 +0200958 if (likely(!skip_update)) {
959 while (sov < eol && HTTP_IS_LWS(start[sov]))
960 sov++;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200961
Christopher Faulet2912f872018-09-19 14:01:04 +0200962 while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
963 eol--;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200964
965
Christopher Faulet2912f872018-09-19 14:01:04 +0200966 n = ist2(start + sol, col - sol);
967 v = ist2(start + sov, eol - sov);
Willy Tarreau794f9af2017-07-26 09:07:47 +0200968
Christopher Faulet2912f872018-09-19 14:01:04 +0200969 do {
970 int ret;
Willy Tarreau794f9af2017-07-26 09:07:47 +0200971
Christopher Faulet2912f872018-09-19 14:01:04 +0200972 if (unlikely(hdr_count >= hdr_num)) {
973 state = H1_MSG_HDR_L2_LWS;
974 goto http_output_full;
975 }
Willy Tarreau5384aac2018-09-11 16:04:48 +0200976
Christopher Faulet2912f872018-09-19 14:01:04 +0200977 if (isteqi(n, ist("transfer-encoding"))) {
Christopher Faulet545fbba2021-09-28 09:36:25 +0200978 ret = h1_parse_xfer_enc_header(h1m, v);
979 if (ret < 0) {
980 state = H1_MSG_HDR_L2_LWS;
981 ptr = v.ptr; /* Set ptr on the error */
982 goto http_msg_invalid;
983 }
984 else if (ret == 0) {
985 /* skip it */
986 break;
987 }
Christopher Faulet2912f872018-09-19 14:01:04 +0200988 }
989 else if (isteqi(n, ist("content-length"))) {
990 ret = h1_parse_cont_len_header(h1m, &v);
Willy Tarreau73373ab2018-09-14 17:11:33 +0200991
Christopher Faulet2912f872018-09-19 14:01:04 +0200992 if (ret < 0) {
993 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +0100994 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet2912f872018-09-19 14:01:04 +0200995 goto http_msg_invalid;
996 }
997 else if (ret == 0) {
998 /* skip it */
999 break;
1000 }
Willy Tarreau73373ab2018-09-14 17:11:33 +02001001 }
Christopher Faulet2912f872018-09-19 14:01:04 +02001002 else if (isteqi(n, ist("connection"))) {
Christopher Fauleta51ebb72019-03-29 15:03:13 +01001003 h1_parse_connection_header(h1m, &v);
1004 if (!v.len) {
1005 /* skip it */
1006 break;
1007 }
Willy Tarreau73373ab2018-09-14 17:11:33 +02001008 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001009 else if (isteqi(n, ist("upgrade"))) {
1010 h1_parse_upgrade_header(h1m, v);
1011 }
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001012 else if (!(h1m->flags & H1_MF_RESP) && isteqi(n, ist("host"))) {
1013 if (host_idx == -1)
Christopher Faulet497ab4f2019-10-11 09:01:44 +02001014 host_idx = hdr_count;
1015 else {
1016 if (!isteqi(v, hdr[host_idx].v)) {
1017 state = H1_MSG_HDR_L2_LWS;
Christopher Faulet17034782020-01-06 13:41:01 +01001018 ptr = v.ptr; /* Set ptr on the error */
Christopher Faulet497ab4f2019-10-11 09:01:44 +02001019 goto http_msg_invalid;
1020 }
1021 /* if the same host, skip it */
1022 break;
1023 }
1024 }
Willy Tarreau2ea6bb52018-09-14 16:28:15 +02001025
Christopher Faulet2912f872018-09-19 14:01:04 +02001026 http_set_hdr(&hdr[hdr_count++], n, v);
1027 } while (0);
1028 }
Willy Tarreau794f9af2017-07-26 09:07:47 +02001029
1030 sol = ptr - start;
Christopher Faulet2912f872018-09-19 14:01:04 +02001031
Willy Tarreau794f9af2017-07-26 09:07:47 +02001032 if (likely(!HTTP_IS_CRLF(*ptr)))
1033 goto http_msg_hdr_name;
1034
1035 if (likely(*ptr == '\r'))
Willy Tarreau801250e2018-09-11 11:45:04 +02001036 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 +02001037 goto http_msg_last_lf;
1038
Willy Tarreau801250e2018-09-11 11:45:04 +02001039 case H1_MSG_LAST_LF:
Willy Tarreau794f9af2017-07-26 09:07:47 +02001040 http_msg_last_lf:
Willy Tarreau801250e2018-09-11 11:45:04 +02001041 EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
Willy Tarreau794f9af2017-07-26 09:07:47 +02001042 ptr++;
1043 /* <ptr> now points to the first byte of payload. If needed sol
1044 * still points to the first of either CR or LF of the empty
1045 * line ending the headers block.
1046 */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001047 if (likely(!skip_update)) {
1048 if (unlikely(hdr_count >= hdr_num)) {
1049 state = H1_MSG_LAST_LF;
1050 goto http_output_full;
1051 }
Christopher Fauletff08a922018-09-25 13:59:46 +02001052 http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
Willy Tarreau794f9af2017-07-26 09:07:47 +02001053 }
Willy Tarreau001823c2018-09-12 17:25:32 +02001054
1055 /* reaching here we've parsed the whole message. We may detect
1056 * that we were already continuing an interrupted parsing pass
1057 * so we were silently looking for the end of message not
1058 * updating anything before deciding to parse it fully at once.
1059 * It's guaranteed that we won't match this test twice in a row
1060 * since restarting will turn zero.
1061 */
1062 if (restarting)
1063 goto restart;
1064
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001065
1066 if (!(h1m->flags & (H1_MF_HDRS_ONLY|H1_MF_RESP))) {
1067 struct http_uri_parser parser = http_uri_parser_init(sl.rq.u);
Christopher Faulete16ffb02022-11-22 10:04:16 +01001068 struct ist scheme, authority;
1069 int ret;
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001070
Christopher Faulete16ffb02022-11-22 10:04:16 +01001071 scheme = http_parse_scheme(&parser);
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001072 authority = http_parse_authority(&parser, 1);
1073 if (sl.rq.meth == HTTP_METH_CONNECT) {
1074 struct ist *host = ((host_idx != -1) ? &hdr[host_idx].v : NULL);
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001075
1076 ret = h1_validate_connect_authority(authority, host);
1077 if (ret < 0) {
1078 if (h1m->err_pos < -1) {
1079 state = H1_MSG_LAST_LF;
Willy Tarreau55d2e852022-10-04 08:02:03 +02001080 /* WT: gcc seems to see a path where sl.rq.u.ptr was used
1081 * uninitialized, but it doesn't know that the function is
1082 * called with initial states making this impossible.
1083 */
1084 ALREADY_CHECKED(sl.rq.u.ptr);
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001085 ptr = ((ret == -1) ? sl.rq.u.ptr : host->ptr); /* Set ptr on the error */
1086 goto http_msg_invalid;
1087 }
1088 if (h1m->err_pos == -1) /* capture the error pointer */
1089 h1m->err_pos = ((ret == -1) ? sl.rq.u.ptr : host->ptr) - start + skip; /* >= 0 now */
1090 }
1091 }
1092 else if (host_idx != -1 && istlen(authority)) {
1093 struct ist host = hdr[host_idx].v;
1094
1095 /* For non-CONNECT method, the authority must match the host header value */
1096 if (!isteqi(authority, host)) {
Christopher Faulete16ffb02022-11-22 10:04:16 +01001097 ret = h1_validate_mismatch_authority(scheme, authority, host);
1098 if (ret < 0) {
1099 if (h1m->err_pos < -1) {
1100 state = H1_MSG_LAST_LF;
1101 ptr = host.ptr; /* Set ptr on the error */
1102 goto http_msg_invalid;
1103 }
1104 if (h1m->err_pos == -1) /* capture the error pointer */
1105 h1m->err_pos = v.ptr - start + skip; /* >= 0 now */
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001106 }
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001107 }
Christopher Faulet3f5fbe92022-07-05 14:50:17 +02001108 }
1109 }
1110
Willy Tarreau2557f6a2018-09-14 16:34:47 +02001111 state = H1_MSG_DATA;
1112 if (h1m->flags & H1_MF_XFER_ENC) {
1113 if (h1m->flags & H1_MF_CLEN) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02001114 /* T-E + C-L: force close and remove C-L */
1115 h1m->flags |= H1_MF_CONN_CLO;
Willy Tarreau2557f6a2018-09-14 16:34:47 +02001116 h1m->flags &= ~H1_MF_CLEN;
1117 hdr_count = http_del_hdr(hdr, ist("content-length"));
1118 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02001119 else if (!(h1m->flags & H1_MF_VER_11)) {
1120 /* T-E + HTTP/1.0: force close */
1121 h1m->flags |= H1_MF_CONN_CLO;
1122 }
Willy Tarreau2557f6a2018-09-14 16:34:47 +02001123
1124 if (h1m->flags & H1_MF_CHNK)
1125 state = H1_MSG_CHUNK_SIZE;
1126 else if (!(h1m->flags & H1_MF_RESP)) {
1127 /* cf RFC7230#3.3.3 : transfer-encoding in
1128 * request without chunked encoding is invalid.
1129 */
1130 goto http_msg_invalid;
1131 }
1132 }
1133
Willy Tarreau794f9af2017-07-26 09:07:47 +02001134 break;
1135
1136 default:
1137 /* impossible states */
1138 goto http_msg_invalid;
1139 }
1140
Willy Tarreau001823c2018-09-12 17:25:32 +02001141 /* Now we've left the headers state and are either in H1_MSG_DATA or
1142 * H1_MSG_CHUNK_SIZE.
Willy Tarreau794f9af2017-07-26 09:07:47 +02001143 */
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001144
Willy Tarreau5384aac2018-09-11 16:04:48 +02001145 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001146 *slp = sl;
1147
Willy Tarreau4433c082018-09-11 15:33:32 +02001148 h1m->state = state;
1149 h1m->next = ptr - start + skip;
1150 return h1m->next;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001151
1152 http_msg_ood:
1153 /* out of data at <ptr> during state <state> */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001154 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001155 *slp = sl;
1156
Willy Tarreau4433c082018-09-11 15:33:32 +02001157 h1m->state = state;
1158 h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001159 return 0;
1160
1161 http_msg_invalid:
1162 /* invalid message, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001163 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001164 *slp = sl;
1165
Willy Tarreau4433c082018-09-11 15:33:32 +02001166 h1m->err_state = h1m->state = state;
1167 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001168 return -1;
1169
1170 http_output_full:
1171 /* no more room to store the current header, error at <ptr> */
Willy Tarreau5384aac2018-09-11 16:04:48 +02001172 if (slp && !skip_update)
Willy Tarreaua41393f2018-09-11 15:34:50 +02001173 *slp = sl;
1174
Willy Tarreau4433c082018-09-11 15:33:32 +02001175 h1m->err_state = h1m->state = state;
1176 h1m->err_pos = h1m->next = ptr - start + skip;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001177 return -2;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001178
1179 restart:
Christopher Faulet02c89332021-12-01 18:01:48 +01001180 h1m->flags &= H1_MF_RESTART_MASK;
Christopher Faulet84f06532019-09-03 16:05:31 +02001181 h1m->curr_len = h1m->body_len = h1m->next = 0;
Willy Tarreauc2ab9f52018-09-11 17:57:05 +02001182 if (h1m->flags & H1_MF_RESP)
1183 h1m->state = H1_MSG_RPBEFORE;
1184 else
1185 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreau4c34c0e2018-09-11 16:20:30 +02001186 goto try_again;
Willy Tarreau794f9af2017-07-26 09:07:47 +02001187}
1188
Willy Tarreau2510f702017-10-31 17:14:16 +01001189/* This function performs a very minimal parsing of the trailers block present
Willy Tarreauf40e6822018-06-14 16:52:02 +02001190 * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of
Willy Tarreau7314be82018-06-14 13:32:50 +02001191 * bytes to delete to skip the trailers. It may return 0 if it's missing some
1192 * input data, or < 0 in case of parse error (in which case the caller may have
1193 * to decide how to proceed, possibly eating everything).
Willy Tarreau2510f702017-10-31 17:14:16 +01001194 */
Willy Tarreauf40e6822018-06-14 16:52:02 +02001195int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max)
Willy Tarreau2510f702017-10-31 17:14:16 +01001196{
Willy Tarreauf40e6822018-06-14 16:52:02 +02001197 const char *stop = b_peek(buf, ofs + max);
1198 int count = ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +01001199
1200 while (1) {
1201 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau7314be82018-06-14 13:32:50 +02001202 const char *start = b_peek(buf, count);
Willy Tarreau2510f702017-10-31 17:14:16 +01001203 const char *ptr = start;
Willy Tarreau2510f702017-10-31 17:14:16 +01001204
1205 /* scan current line and stop at LF or CRLF */
1206 while (1) {
1207 if (ptr == stop)
1208 return 0;
1209
1210 if (*ptr == '\n') {
1211 if (!p1)
1212 p1 = ptr;
1213 p2 = ptr;
1214 break;
1215 }
1216
1217 if (*ptr == '\r') {
1218 if (p1)
1219 return -1;
1220 p1 = ptr;
1221 }
1222
Willy Tarreau7314be82018-06-14 13:32:50 +02001223 ptr = b_next(buf, ptr);
Willy Tarreau2510f702017-10-31 17:14:16 +01001224 }
1225
1226 /* after LF; point to beginning of next line */
Willy Tarreau7314be82018-06-14 13:32:50 +02001227 p2 = b_next(buf, p2);
1228 count += b_dist(buf, start, p2);
Willy Tarreau2510f702017-10-31 17:14:16 +01001229
1230 /* LF/CRLF at beginning of line => end of trailers at p2.
1231 * Everything was scheduled for forwarding, there's nothing left
1232 * from this message. */
1233 if (p1 == start)
1234 break;
1235 /* OK, next line then */
1236 }
Willy Tarreauf40e6822018-06-14 16:52:02 +02001237 return count - ofs;
Willy Tarreau2510f702017-10-31 17:14:16 +01001238}
Amaury Denoyellec1938232020-12-11 17:53:03 +01001239
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001240/* Generate a random key for a WebSocket Handshake in respect with rfc6455
1241 * The key is 128-bits long encoded as a base64 string in <key_out> parameter
1242 * (25 bytes long).
1243 */
1244void h1_generate_random_ws_input_key(char key_out[25])
1245{
1246 /* generate a random websocket key */
1247 const uint64_t rand1 = ha_random64(), rand2 = ha_random64();
1248 char key[16];
1249
1250 memcpy(key, &rand1, 8);
1251 memcpy(&key[8], &rand2, 8);
1252 a2base64(key, 16, key_out, 25);
1253}
1254
Amaury Denoyellec1938232020-12-11 17:53:03 +01001255#define H1_WS_KEY_SUFFIX_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
1256
1257/*
1258 * Calculate the WebSocket handshake response key from <key_in>. Following the
1259 * rfc6455, <key_in> must be 24 bytes longs. The result is stored in <key_out>
1260 * as a 29 bytes long string.
1261 */
1262void h1_calculate_ws_output_key(const char *key, char *result)
1263{
1264 blk_SHA_CTX sha1_ctx;
1265 char hash_in[60], hash_out[20];
1266
1267 /* concatenate the key with a fixed suffix */
1268 memcpy(hash_in, key, 24);
1269 memcpy(&hash_in[24], H1_WS_KEY_SUFFIX_GUID, 36);
1270
1271 /* sha1 the result */
1272 blk_SHA1_Init(&sha1_ctx);
1273 blk_SHA1_Update(&sha1_ctx, hash_in, 60);
1274 blk_SHA1_Final((unsigned char *)hash_out, &sha1_ctx);
1275
1276 /* encode in base64 the hash */
1277 a2base64(hash_out, 20, result, 29);
1278}