Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions to manipulate H1 messages using the internal representation. |
| 3 | * |
| 4 | * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 13 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 14 | #include <haproxy/cfgparse.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 15 | #include <haproxy/global.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 16 | #include <haproxy/h1.h> |
Willy Tarreau | c6fe884 | 2020-06-04 09:00:02 +0200 | [diff] [blame] | 17 | #include <haproxy/h1_htx.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 18 | #include <haproxy/http.h> |
Amaury Denoyelle | 852d78c | 2021-07-07 10:49:27 +0200 | [diff] [blame] | 19 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 20 | #include <haproxy/htx.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <haproxy/tools.h> |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 22 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 23 | /* Estimate the size of the HTX headers after the parsing, including the EOH. */ |
| 24 | static size_t h1_eval_htx_hdrs_size(const struct http_hdr *hdrs) |
| 25 | { |
| 26 | size_t sz = 0; |
| 27 | int i; |
| 28 | |
| 29 | for (i = 0; hdrs[i].n.len; i++) |
| 30 | sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len; |
| 31 | sz += sizeof(struct htx_blk) + 1; |
| 32 | return sz; |
| 33 | } |
| 34 | |
| 35 | /* Estimate the size of the HTX request after the parsing. */ |
| 36 | static size_t h1_eval_htx_size(const struct ist p1, const struct ist p2, const struct ist p3, |
| 37 | const struct http_hdr *hdrs) |
| 38 | { |
| 39 | size_t sz; |
| 40 | |
| 41 | /* size of the HTX start-line */ |
| 42 | sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + p1.len + p2.len + p3.len; |
| 43 | sz += h1_eval_htx_hdrs_size(hdrs); |
| 44 | return sz; |
| 45 | } |
| 46 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 47 | /* Check the validity of the request version. If the version is valid, it |
| 48 | * returns 1. Otherwise, it returns 0. |
| 49 | */ |
| 50 | static int h1_process_req_vsn(struct h1m *h1m, union h1_sl *sl) |
| 51 | { |
| 52 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 53 | * exactly one digit "." one digit. This check may be disabled using |
| 54 | * option accept-invalid-http-request. |
| 55 | */ |
| 56 | if (h1m->err_pos == -2) { /* PR_O2_REQBUG_OK not set */ |
| 57 | if (sl->rq.v.len != 8) |
| 58 | return 0; |
| 59 | |
| 60 | if (*(sl->rq.v.ptr + 4) != '/' || |
| 61 | !isdigit((unsigned char)*(sl->rq.v.ptr + 5)) || |
| 62 | *(sl->rq.v.ptr + 6) != '.' || |
| 63 | !isdigit((unsigned char)*(sl->rq.v.ptr + 7))) |
| 64 | return 0; |
| 65 | } |
| 66 | else if (!sl->rq.v.len) { |
| 67 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 68 | |
| 69 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 70 | if (sl->rq.meth != HTTP_METH_GET) |
| 71 | return 0; |
| 72 | |
| 73 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 74 | if (!sl->rq.u.len) |
| 75 | return 0; |
| 76 | |
| 77 | /* Add HTTP version */ |
| 78 | sl->rq.v = ist("HTTP/1.0"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | if ((sl->rq.v.len == 8) && |
| 83 | ((*(sl->rq.v.ptr + 5) > '1') || |
| 84 | ((*(sl->rq.v.ptr + 5) == '1') && (*(sl->rq.v.ptr + 7) >= '1')))) |
| 85 | h1m->flags |= H1_MF_VER_11; |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | /* Check the validity of the response version. If the version is valid, it |
| 90 | * returns 1. Otherwise, it returns 0. |
| 91 | */ |
| 92 | static int h1_process_res_vsn(struct h1m *h1m, union h1_sl *sl) |
| 93 | { |
| 94 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 95 | * exactly one digit "." one digit. This check may be disabled using |
| 96 | * option accept-invalid-http-request. |
| 97 | */ |
| 98 | if (h1m->err_pos == -2) { /* PR_O2_REQBUG_OK not set */ |
| 99 | if (sl->st.v.len != 8) |
| 100 | return 0; |
| 101 | |
| 102 | if (*(sl->st.v.ptr + 4) != '/' || |
| 103 | !isdigit((unsigned char)*(sl->st.v.ptr + 5)) || |
| 104 | *(sl->st.v.ptr + 6) != '.' || |
| 105 | !isdigit((unsigned char)*(sl->st.v.ptr + 7))) |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | if ((sl->st.v.len == 8) && |
| 110 | ((*(sl->st.v.ptr + 5) > '1') || |
| 111 | ((*(sl->st.v.ptr + 5) == '1') && (*(sl->st.v.ptr + 7) >= '1')))) |
| 112 | h1m->flags |= H1_MF_VER_11; |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | /* Convert H1M flags to HTX start-line flags. */ |
| 118 | static unsigned int h1m_htx_sl_flags(struct h1m *h1m) |
| 119 | { |
| 120 | unsigned int flags = HTX_SL_F_NONE; |
| 121 | |
| 122 | if (h1m->flags & H1_MF_RESP) |
| 123 | flags |= HTX_SL_F_IS_RESP; |
| 124 | if (h1m->flags & H1_MF_VER_11) |
| 125 | flags |= HTX_SL_F_VER_11; |
| 126 | if (h1m->flags & H1_MF_XFER_ENC) |
| 127 | flags |= HTX_SL_F_XFER_ENC; |
| 128 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 129 | flags |= HTX_SL_F_XFER_LEN; |
| 130 | if (h1m->flags & H1_MF_CHNK) |
| 131 | flags |= HTX_SL_F_CHNK; |
| 132 | else if (h1m->flags & H1_MF_CLEN) { |
| 133 | flags |= HTX_SL_F_CLEN; |
| 134 | if (h1m->body_len == 0) |
| 135 | flags |= HTX_SL_F_BODYLESS; |
| 136 | } |
| 137 | else |
| 138 | flags |= HTX_SL_F_BODYLESS; |
| 139 | } |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 140 | if (h1m->flags & H1_MF_CONN_UPG) |
| 141 | flags |= HTX_SL_F_CONN_UPG; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 142 | return flags; |
| 143 | } |
| 144 | |
| 145 | /* Postprocess the parsed headers for a request and convert them into an htx |
| 146 | * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't |
| 147 | * proceed. Parsing errors are reported by setting the htx flag |
| 148 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. |
| 149 | */ |
| 150 | static int h1_postparse_req_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx, |
| 151 | struct http_hdr *hdrs, size_t max) |
| 152 | { |
| 153 | struct htx_sl *sl; |
| 154 | struct ist meth, uri, vsn; |
| 155 | unsigned int flags; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 156 | |
| 157 | /* <h1sl> is always defined for a request */ |
| 158 | meth = h1sl->rq.m; |
| 159 | uri = h1sl->rq.u; |
| 160 | vsn = h1sl->rq.v; |
| 161 | |
| 162 | /* Be sure the message, once converted into HTX, will not exceed the max |
| 163 | * size allowed. |
| 164 | */ |
| 165 | if (h1_eval_htx_size(meth, uri, vsn, hdrs) > max) { |
| 166 | if (htx_is_empty(htx)) |
| 167 | goto error; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 168 | goto output_full; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* By default, request have always a known length */ |
| 172 | h1m->flags |= H1_MF_XFER_LEN; |
| 173 | |
| 174 | if (h1sl->rq.meth == HTTP_METH_CONNECT) { |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 175 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 176 | h1m->curr_len = h1m->body_len = 0; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Christopher Faulet | 52a5ec2 | 2021-09-09 09:52:51 +0200 | [diff] [blame] | 179 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 180 | flags = h1m_htx_sl_flags(h1m); |
Christopher Faulet | 52a5ec2 | 2021-09-09 09:52:51 +0200 | [diff] [blame] | 181 | if ((flags & (HTX_SL_F_CONN_UPG|HTX_SL_F_BODYLESS)) == HTX_SL_F_CONN_UPG) { |
| 182 | int i; |
| 183 | |
| 184 | for (i = 0; hdrs[i].n.len; i++) { |
| 185 | if (isteqi(hdrs[i].n, ist("upgrade"))) |
| 186 | hdrs[i].v = IST_NULL; |
| 187 | } |
| 188 | h1m->flags &=~ H1_MF_CONN_UPG; |
| 189 | flags &= ~HTX_SL_F_CONN_UPG; |
| 190 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 191 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, uri, vsn); |
| 192 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
| 193 | goto error; |
| 194 | sl->info.req.meth = h1sl->rq.meth; |
| 195 | |
Christopher Faulet | fe451fb | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 196 | /* Check if the uri contains an authority. Also check if it contains an |
| 197 | * explicit scheme and if it is "http" or "https". */ |
| 198 | if (h1sl->rq.meth == HTTP_METH_CONNECT) |
| 199 | sl->flags |= HTX_SL_F_HAS_AUTHORITY; |
| 200 | else if (uri.len && uri.ptr[0] != '/' && uri.ptr[0] != '*') { |
| 201 | sl->flags |= (HTX_SL_F_HAS_AUTHORITY|HTX_SL_F_HAS_SCHM); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 202 | if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h') |
| 203 | sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
Amaury Denoyelle | 852d78c | 2021-07-07 10:49:27 +0200 | [diff] [blame] | 204 | |
| 205 | /* absolute-form target URI present, proceed to scheme-based |
| 206 | * normalization */ |
| 207 | http_scheme_based_normalize(htx); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 208 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 209 | |
| 210 | /* If body length cannot be determined, set htx->extra to |
| 211 | * ULLONG_MAX. This value is impossible in other cases. |
| 212 | */ |
| 213 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
| 214 | |
| 215 | end: |
| 216 | return 1; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 217 | output_full: |
| 218 | h1m_init_req(h1m); |
| 219 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 220 | return -2; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 221 | error: |
| 222 | h1m->err_pos = h1m->next; |
| 223 | h1m->err_state = h1m->state; |
| 224 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 225 | return -1; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* Postprocess the parsed headers for a response and convert them into an htx |
| 229 | * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't |
| 230 | * proceed. Parsing errors are reported by setting the htx flag |
| 231 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. |
| 232 | */ |
| 233 | static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx, |
| 234 | struct http_hdr *hdrs, size_t max) |
| 235 | { |
| 236 | struct htx_sl *sl; |
| 237 | struct ist vsn, status, reason; |
| 238 | unsigned int flags; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 239 | uint16_t code = 0; |
| 240 | |
| 241 | if (h1sl) { |
| 242 | /* For HTTP responses, the start-line was parsed */ |
| 243 | code = h1sl->st.status; |
| 244 | vsn = h1sl->st.v; |
| 245 | status = h1sl->st.c; |
| 246 | reason = h1sl->st.r; |
| 247 | } |
| 248 | else { |
| 249 | /* For FCGI responses, there is no start(-line but the "Status" |
| 250 | * header must be parsed, if found. |
| 251 | */ |
| 252 | int hdr; |
| 253 | |
| 254 | vsn = ((h1m->flags & H1_MF_VER_11) ? ist("HTTP/1.1") : ist("HTTP/1.0")); |
| 255 | for (hdr = 0; hdrs[hdr].n.len; hdr++) { |
| 256 | if (isteqi(hdrs[hdr].n, ist("status"))) { |
| 257 | code = http_parse_status_val(hdrs[hdr].v, &status, &reason); |
| 258 | } |
| 259 | else if (isteqi(hdrs[hdr].n, ist("location"))) { |
| 260 | code = 302; |
| 261 | status = ist("302"); |
| 262 | reason = ist("Moved Temporarily"); |
| 263 | } |
| 264 | } |
| 265 | if (!code) { |
| 266 | code = 200; |
| 267 | status = ist("200"); |
| 268 | reason = ist("OK"); |
| 269 | } |
| 270 | /* FIXME: Check the codes 1xx ? */ |
| 271 | } |
| 272 | |
| 273 | /* Be sure the message, once converted into HTX, will not exceed the max |
| 274 | * size allowed. |
| 275 | */ |
| 276 | if (h1_eval_htx_size(vsn, status, reason, hdrs) > max) { |
| 277 | if (htx_is_empty(htx)) |
| 278 | goto error; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 279 | goto output_full; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 282 | if (((h1m->flags & H1_MF_METH_CONNECT) && code >= 200 && code < 300) || code == 101) { |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 283 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 284 | h1m->flags |= H1_MF_XFER_LEN; |
| 285 | h1m->curr_len = h1m->body_len = 0; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 286 | } |
| 287 | else if ((h1m->flags & H1_MF_METH_HEAD) || (code >= 100 && code < 200) || |
| 288 | (code == 204) || (code == 304)) { |
| 289 | /* Responses known to have no body. */ |
| 290 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 291 | h1m->flags |= H1_MF_XFER_LEN; |
| 292 | h1m->curr_len = h1m->body_len = 0; |
| 293 | } |
| 294 | else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 295 | /* Responses with a known body length. */ |
| 296 | h1m->flags |= H1_MF_XFER_LEN; |
| 297 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 298 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 299 | flags = h1m_htx_sl_flags(h1m); |
| 300 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, vsn, status, reason); |
| 301 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
| 302 | goto error; |
| 303 | sl->info.res.status = code; |
| 304 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 305 | /* If body length cannot be determined, set htx->extra to |
| 306 | * ULLONG_MAX. This value is impossible in other cases. |
| 307 | */ |
| 308 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
| 309 | |
| 310 | end: |
| 311 | return 1; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 312 | output_full: |
| 313 | h1m_init_res(h1m); |
| 314 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 315 | return -2; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 316 | error: |
| 317 | h1m->err_pos = h1m->next; |
| 318 | h1m->err_state = h1m->state; |
| 319 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 320 | return -1; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 323 | /* Parse HTTP/1 headers. It returns the number of bytes parsed on success, 0 if |
| 324 | * headers are incomplete, -1 if an error occurred or -2 if it needs more space |
| 325 | * to proceed while the output buffer is not empty. Parsing errors are reported |
| 326 | * by setting the htx flag HTX_FL_PARSING_ERROR and filling h1m->err_pos and |
| 327 | * h1m->err_state fields. This functions is responsible to update the parser |
| 328 | * state <h1m> and the start-line <h1sl> if not NULL. For the requests, <h1sl> |
| 329 | * must always be provided. For responses, <h1sl> may be NULL and <h1m> flags |
| 330 | * HTTP_METH_CONNECT of HTTP_METH_HEAD may be set. |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 331 | */ |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 332 | int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, |
| 333 | struct buffer *srcbuf, size_t ofs, size_t max) |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 334 | { |
| 335 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 336 | int total = 0, ret = 0; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 337 | |
| 338 | if (!max || !b_data(srcbuf)) |
| 339 | goto end; |
| 340 | |
| 341 | /* Realing input buffer if necessary */ |
| 342 | if (b_head(srcbuf) + b_data(srcbuf) > b_wrap(srcbuf)) |
Christopher Faulet | 00d7cde | 2021-02-04 11:01:51 +0100 | [diff] [blame] | 343 | b_slow_realign_ofs(srcbuf, trash.area, 0); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 344 | |
| 345 | if (!h1sl) { |
| 346 | /* If there no start-line, be sure to only parse the headers */ |
| 347 | h1m->flags |= H1_MF_HDRS_ONLY; |
| 348 | } |
| 349 | ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf), |
| 350 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, h1sl); |
| 351 | if (ret <= 0) { |
| 352 | /* Incomplete or invalid message. If the input buffer only |
| 353 | * contains headers and is full, which is detected by it being |
| 354 | * full and the offset to be zero, it's an error because |
| 355 | * headers are too large to be handled by the parser. */ |
| 356 | if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf))) |
| 357 | goto error; |
| 358 | goto end; |
| 359 | } |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 360 | total = ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 361 | |
| 362 | /* messages headers fully parsed, do some checks to prepare the body |
| 363 | * parsing. |
| 364 | */ |
| 365 | |
| 366 | if (!(h1m->flags & H1_MF_RESP)) { |
| 367 | if (!h1_process_req_vsn(h1m, h1sl)) { |
| 368 | h1m->err_pos = h1sl->rq.v.ptr - b_head(srcbuf); |
| 369 | h1m->err_state = h1m->state; |
| 370 | goto vsn_error; |
| 371 | } |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 372 | ret = h1_postparse_req_hdrs(h1m, h1sl, dsthtx, hdrs, max); |
| 373 | if (ret < 0) |
| 374 | return ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 375 | } |
| 376 | else { |
| 377 | if (h1sl && !h1_process_res_vsn(h1m, h1sl)) { |
| 378 | h1m->err_pos = h1sl->st.v.ptr - b_head(srcbuf); |
| 379 | h1m->err_state = h1m->state; |
| 380 | goto vsn_error; |
| 381 | } |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 382 | ret = h1_postparse_res_hdrs(h1m, h1sl, dsthtx, hdrs, max); |
| 383 | if (ret < 0) |
| 384 | return ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 387 | /* Switch messages without any payload to DONE state */ |
| 388 | if (((h1m->flags & H1_MF_CLEN) && h1m->body_len == 0) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 389 | ((h1m->flags & (H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK)) == H1_MF_XFER_LEN)) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 390 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 391 | dsthtx->flags |= HTX_FL_EOM; |
| 392 | } |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 393 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 394 | end: |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 395 | return total; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 396 | error: |
| 397 | h1m->err_pos = h1m->next; |
| 398 | h1m->err_state = h1m->state; |
| 399 | vsn_error: |
| 400 | dsthtx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 401 | return -1; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 402 | |
| 403 | } |
| 404 | |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 405 | /* Copy data from <srbuf> into an DATA block in <dsthtx>. If possible, a |
| 406 | * zero-copy is performed. It returns the number of bytes copied. |
| 407 | */ |
Christopher Faulet | de471a4 | 2021-02-01 16:37:28 +0100 | [diff] [blame] | 408 | static size_t h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs, |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 409 | size_t count, size_t max, struct buffer *htxbuf) |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 410 | { |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 411 | struct htx *tmp_htx = *dsthtx; |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 412 | size_t block1, block2, ret = 0; |
| 413 | |
| 414 | /* Be prepared to create at least one HTX block by reserving its size |
| 415 | * and adjust <count> accordingly. |
| 416 | */ |
| 417 | max -= sizeof(struct htx_blk); |
| 418 | if (count > max) |
| 419 | count = max; |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 420 | |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 421 | /* very often with large files we'll face the following |
| 422 | * situation : |
| 423 | * - htx is empty and points to <htxbuf> |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 424 | * - count == srcbuf->data |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 425 | * - srcbuf->head == sizeof(struct htx) |
| 426 | * => we can swap the buffers and place an htx header into |
| 427 | * the target buffer instead |
| 428 | */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 429 | if (unlikely(htx_is_empty(tmp_htx) && count == b_data(srcbuf) && |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 430 | !ofs && b_head_ofs(srcbuf) == sizeof(struct htx))) { |
| 431 | void *raw_area = srcbuf->area; |
| 432 | void *htx_area = htxbuf->area; |
| 433 | struct htx_blk *blk; |
| 434 | |
| 435 | srcbuf->area = htx_area; |
| 436 | htxbuf->area = raw_area; |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 437 | tmp_htx = (struct htx *)htxbuf->area; |
| 438 | tmp_htx->size = htxbuf->size - sizeof(*tmp_htx); |
| 439 | htx_reset(tmp_htx); |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 440 | b_set_data(htxbuf, b_size(htxbuf)); |
| 441 | |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 442 | blk = htx_add_blk(tmp_htx, HTX_BLK_DATA, count); |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 443 | blk->info += count; |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 444 | |
| 445 | *dsthtx = tmp_htx; |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 446 | /* nothing else to do, the old buffer now contains an |
| 447 | * empty pre-initialized HTX header |
| 448 | */ |
| 449 | return count; |
| 450 | } |
| 451 | |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 452 | /* * First block is the copy of contiguous data starting at offset <ofs> |
| 453 | * with <count> as max. <max> is updated accordingly |
| 454 | * |
| 455 | * * Second block is the remaining (count - block1) if <max> is large |
| 456 | * enough. Another HTX block is reserved. |
| 457 | */ |
| 458 | block1 = b_contig_data(srcbuf, ofs); |
| 459 | block2 = 0; |
| 460 | if (block1 > count) |
| 461 | block1 = count; |
| 462 | max -= block1; |
| 463 | |
| 464 | if (max > sizeof(struct htx_blk)) { |
| 465 | block2 = count - block1; |
| 466 | max -= sizeof(struct htx_blk); |
| 467 | if (block2 > max) |
| 468 | block2 = max; |
| 469 | } |
| 470 | |
| 471 | ret = htx_add_data(tmp_htx, ist2(b_peek(srcbuf, ofs), block1)); |
| 472 | if (ret == block1 && block2) |
| 473 | ret += htx_add_data(tmp_htx, ist2(b_orig(srcbuf), block2)); |
| 474 | end: |
| 475 | return ret; |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Christopher Faulet | 7a835f3 | 2021-05-21 11:31:35 +0200 | [diff] [blame] | 478 | static const char hextable[] = { |
| 479 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 480 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
| 481 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 482 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 483 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 484 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 485 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 486 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
| 487 | }; |
| 488 | |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 489 | /* Generic function to parse the current HTTP chunk. It may be used to parsed |
Ilya Shipitsin | 213bb99 | 2021-06-12 15:55:27 +0500 | [diff] [blame] | 490 | * any kind of chunks, including incomplete HTTP chunks or split chunks |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 491 | * because the buffer wraps. This version tries to performed zero-copy on large |
| 492 | * chunks if possible. |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 493 | */ |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 494 | static size_t h1_parse_chunk(struct h1m *h1m, struct htx **dsthtx, |
| 495 | struct buffer *srcbuf, size_t ofs, size_t *max, |
| 496 | struct buffer *htxbuf) |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 497 | { |
| 498 | uint64_t chksz; |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 499 | size_t sz, used, lmax, total = 0; |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 500 | int ret = 0; |
| 501 | |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 502 | lmax = *max; |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 503 | switch (h1m->state) { |
| 504 | case H1_MSG_DATA: |
| 505 | new_chunk: |
| 506 | used = htx_used_space(*dsthtx); |
| 507 | |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 508 | if (b_data(srcbuf) == ofs || !lmax) |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 509 | break; |
| 510 | |
| 511 | sz = b_data(srcbuf) - ofs; |
| 512 | if (unlikely(sz > h1m->curr_len)) |
| 513 | sz = h1m->curr_len; |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 514 | sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, lmax, htxbuf); |
| 515 | lmax -= htx_used_space(*dsthtx) - used; |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 516 | ofs += sz; |
| 517 | total += sz; |
| 518 | h1m->curr_len -= sz; |
| 519 | if (h1m->curr_len) |
| 520 | break; |
| 521 | |
| 522 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 523 | /*fall through */ |
| 524 | |
| 525 | case H1_MSG_CHUNK_CRLF: |
| 526 | ret = h1_skip_chunk_crlf(srcbuf, ofs, b_data(srcbuf)); |
| 527 | if (ret <= 0) |
| 528 | break; |
| 529 | ofs += ret; |
| 530 | total += ret; |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 531 | |
| 532 | /* Don't parse next chunk to try to handle contiguous chunks if possible */ |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 533 | h1m->state = H1_MSG_CHUNK_SIZE; |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 534 | break; |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 535 | |
| 536 | case H1_MSG_CHUNK_SIZE: |
| 537 | ret = h1_parse_chunk_size(srcbuf, ofs, b_data(srcbuf), &chksz); |
| 538 | if (ret <= 0) |
| 539 | break; |
| 540 | h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA); |
| 541 | h1m->curr_len = chksz; |
| 542 | h1m->body_len += chksz; |
| 543 | ofs += ret; |
| 544 | total += ret; |
| 545 | |
| 546 | if (h1m->curr_len) { |
| 547 | h1m->state = H1_MSG_DATA; |
| 548 | goto new_chunk; |
| 549 | } |
| 550 | h1m->state = H1_MSG_TRAILERS; |
| 551 | break; |
| 552 | |
| 553 | default: |
| 554 | /* unexpected */ |
| 555 | ret = -1; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | if (ret < 0) { |
| 560 | (*dsthtx)->flags |= HTX_FL_PARSING_ERROR; |
| 561 | h1m->err_state = h1m->state; |
| 562 | h1m->err_pos = ofs; |
| 563 | total = 0; |
| 564 | } |
| 565 | |
| 566 | /* Don't forget to update htx->extra */ |
| 567 | (*dsthtx)->extra = h1m->curr_len; |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 568 | *max = lmax; |
| 569 | return total; |
| 570 | } |
| 571 | |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 572 | /* Parses full contiguous HTTP chunks. This version is optimized for small |
| 573 | * chunks and does not performed zero-copy. It must be called in |
Ilya Shipitsin | 213bb99 | 2021-06-12 15:55:27 +0500 | [diff] [blame] | 574 | * H1_MSG_CHUNK_SIZE state. Be careful if you change something in this |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 575 | * function. It is really sensitive, any change may have an impact on |
| 576 | * performance. |
| 577 | */ |
| 578 | static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx, |
| 579 | struct buffer *srcbuf, size_t ofs, size_t *max, |
| 580 | struct buffer *htxbuf) |
| 581 | { |
| 582 | char *start, *end, *dptr; |
| 583 | ssize_t dpos, ridx, save; |
| 584 | size_t lmax, total = 0; |
| 585 | uint64_t chksz; |
| 586 | struct htx_ret htxret; |
| 587 | |
| 588 | /* source info : |
| 589 | * start : pointer at <ofs> position |
| 590 | * end : pointer marking the end of data to parse |
| 591 | * ridx : the reverse index (negative) marking the parser position (end[ridx]) |
| 592 | */ |
| 593 | ridx = -b_contig_data(srcbuf, ofs); |
| 594 | if (!ridx) |
| 595 | goto out; |
| 596 | start = b_peek(srcbuf, ofs); |
| 597 | end = start - ridx; |
| 598 | |
| 599 | /* Reserve the maximum possible size for the data */ |
| 600 | htxret = htx_reserve_max_data(*dsthtx); |
| 601 | if (!htxret.blk) |
| 602 | goto out; |
| 603 | |
| 604 | /* destination info : |
| 605 | * dptr : pointer on the beginning of the data |
| 606 | * dpos : current position where to copy data |
| 607 | */ |
| 608 | dptr = htx_get_blk_ptr(*dsthtx, htxret.blk); |
| 609 | dpos = htxret.ret; |
| 610 | |
| 611 | /* Empty DATA block is not possible, thus if <dpos> is the beginning of |
| 612 | * the block, it means it is a new block. We can remove the block size |
| 613 | * from <max>. Then we must adjust it if it exceeds the free size in the |
| 614 | * block. |
| 615 | */ |
| 616 | lmax = *max; |
| 617 | if (!dpos) |
| 618 | lmax -= sizeof(struct htx_blk); |
| 619 | if (lmax > htx_get_blksz(htxret.blk) - dpos) |
| 620 | lmax = htx_get_blksz(htxret.blk) - dpos; |
| 621 | |
| 622 | while (1) { |
| 623 | /* The chunk size is in the following form, though we are only |
| 624 | * interested in the size and CRLF : |
| 625 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 626 | */ |
| 627 | chksz = 0; |
| 628 | save = ridx; /* Save the parser position to rewind if necessary */ |
| 629 | while (1) { |
| 630 | int c; |
| 631 | |
| 632 | if (!ridx) |
| 633 | goto end_parsing; |
| 634 | |
| 635 | /* Convert current character */ |
Christopher Faulet | 7a835f3 | 2021-05-21 11:31:35 +0200 | [diff] [blame] | 636 | c = hextable[(unsigned char)end[ridx]]; |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 637 | |
| 638 | /* not a hex digit anymore */ |
Christopher Faulet | bf76df1 | 2021-06-11 13:39:06 +0200 | [diff] [blame] | 639 | if (c & 0xF0) |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 640 | break; |
| 641 | |
| 642 | /* Update current chunk size */ |
| 643 | chksz = (chksz << 4) + c; |
| 644 | |
Willy Tarreau | 8f0b4e9 | 2022-01-28 09:39:24 +0100 | [diff] [blame] | 645 | if (unlikely(chksz & 0xF0000000000000ULL)) { |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 646 | /* Don't get more than 13 hexa-digit (2^52 - 1) |
| 647 | * to never fed possibly bogus values from |
| 648 | * languages that use floats for their integers |
| 649 | */ |
| 650 | goto parsing_error; |
| 651 | } |
| 652 | ++ridx; |
| 653 | } |
| 654 | |
| 655 | if (unlikely(chksz > lmax)) |
| 656 | goto end_parsing; |
| 657 | |
| 658 | if (unlikely(ridx == save)) { |
| 659 | /* empty size not allowed */ |
| 660 | goto parsing_error; |
| 661 | } |
| 662 | |
| 663 | /* Skip spaces */ |
| 664 | while (HTTP_IS_SPHT(end[ridx])) { |
| 665 | if (!++ridx) |
| 666 | goto end_parsing; |
| 667 | } |
| 668 | |
| 669 | /* Up to there, we know that at least one byte is present. Check |
| 670 | * for the end of chunk size. |
| 671 | */ |
| 672 | while (1) { |
| 673 | if (likely(end[ridx] == '\r')) { |
| 674 | /* Parse CRLF */ |
| 675 | if (!++ridx) |
| 676 | goto end_parsing; |
| 677 | if (unlikely(end[ridx] != '\n')) { |
| 678 | /* CR must be followed by LF */ |
| 679 | goto parsing_error; |
| 680 | } |
| 681 | |
| 682 | /* done */ |
| 683 | ++ridx; |
| 684 | break; |
| 685 | } |
| 686 | else if (end[ridx] == '\n') { |
| 687 | /* Parse LF only, nothing more to do */ |
| 688 | ++ridx; |
| 689 | break; |
| 690 | } |
| 691 | else if (likely(end[ridx] == ';')) { |
| 692 | /* chunk extension, ends at next CRLF */ |
| 693 | if (!++ridx) |
| 694 | goto end_parsing; |
| 695 | while (!HTTP_IS_CRLF(end[ridx])) { |
| 696 | if (!++ridx) |
| 697 | goto end_parsing; |
| 698 | } |
| 699 | /* we have a CRLF now, loop above */ |
| 700 | continue; |
| 701 | } |
| 702 | else { |
| 703 | /* all other characters are unexpected */ |
| 704 | goto parsing_error; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /* Exit if it is the last chunk */ |
| 709 | if (unlikely(!chksz)) { |
| 710 | h1m->state = H1_MSG_TRAILERS; |
| 711 | save = ridx; |
| 712 | goto end_parsing; |
| 713 | } |
| 714 | |
| 715 | /* Now check if the whole chunk is here (including the CRLF at |
Ilya Shipitsin | 213bb99 | 2021-06-12 15:55:27 +0500 | [diff] [blame] | 716 | * the end), otherwise we switch in H1_MSG_DATA state. |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 717 | */ |
| 718 | if (chksz + 2 > -ridx) { |
| 719 | h1m->curr_len = chksz; |
| 720 | h1m->body_len += chksz; |
| 721 | h1m->state = H1_MSG_DATA; |
| 722 | (*dsthtx)->extra = h1m->curr_len; |
| 723 | save = ridx; |
| 724 | goto end_parsing; |
| 725 | } |
| 726 | |
| 727 | memcpy(dptr + dpos, end + ridx, chksz); |
| 728 | h1m->body_len += chksz; |
| 729 | lmax -= chksz; |
| 730 | dpos += chksz; |
| 731 | ridx += chksz; |
| 732 | |
| 733 | /* Parse CRLF or LF (always present) */ |
| 734 | if (likely(end[ridx] == '\r')) |
| 735 | ++ridx; |
| 736 | if (end[ridx] != '\n') { |
| 737 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 738 | goto parsing_error; |
| 739 | } |
| 740 | ++ridx; |
| 741 | } |
| 742 | |
| 743 | end_parsing: |
| 744 | ridx = save; |
| 745 | |
| 746 | /* Adjust the HTX block size or remove the block if nothing was copied |
| 747 | * (Empty HTX data block are not supported). |
| 748 | */ |
| 749 | if (!dpos) |
| 750 | htx_remove_blk(*dsthtx, htxret.blk); |
| 751 | else |
| 752 | htx_change_blk_value_len(*dsthtx, htxret.blk, dpos); |
| 753 | total = end + ridx - start; |
| 754 | *max = lmax; |
| 755 | |
| 756 | out: |
| 757 | return total; |
| 758 | |
| 759 | parsing_error: |
| 760 | (*dsthtx)->flags |= HTX_FL_PARSING_ERROR; |
| 761 | h1m->err_state = h1m->state; |
| 762 | h1m->err_pos = ofs + end + ridx - start; |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | /* Parse HTTP chunks. This function relies on an optimized function to parse |
| 767 | * contiguous chunks if possible. Otherwise, when a chunk is incomplete or when |
| 768 | * the underlying buffer is wrapping, a generic function is used. |
| 769 | */ |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 770 | static size_t h1_parse_msg_chunks(struct h1m *h1m, struct htx **dsthtx, |
| 771 | struct buffer *srcbuf, size_t ofs, size_t max, |
| 772 | struct buffer *htxbuf) |
| 773 | { |
| 774 | size_t ret, total = 0; |
| 775 | |
| 776 | while (ofs < b_data(srcbuf)) { |
| 777 | ret = 0; |
| 778 | |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 779 | /* First parse full contiguous chunks. It is only possible if we |
| 780 | * are waiting for the next chunk size. |
| 781 | */ |
| 782 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
| 783 | ret = h1_parse_full_contig_chunks(h1m, dsthtx, srcbuf, ofs, &max, htxbuf); |
| 784 | /* exit on error */ |
| 785 | if (!ret && (*dsthtx)->flags & HTX_FL_PARSING_ERROR) { |
| 786 | total = 0; |
| 787 | break; |
| 788 | } |
| 789 | /* or let a chance to parse remaining data */ |
| 790 | total += ret; |
| 791 | ofs += ret; |
| 792 | ret = 0; |
| 793 | } |
| 794 | |
| 795 | /* If some data remains, try to parse it using the generic |
Ilya Shipitsin | 213bb99 | 2021-06-12 15:55:27 +0500 | [diff] [blame] | 796 | * function handling incomplete chunks and split chunks |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 797 | * because of a wrapping buffer. |
| 798 | */ |
| 799 | if (h1m->state < H1_MSG_TRAILERS && ofs < b_data(srcbuf)) { |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 800 | ret = h1_parse_chunk(h1m, dsthtx, srcbuf, ofs, &max, htxbuf); |
| 801 | total += ret; |
| 802 | ofs += ret; |
| 803 | } |
| 804 | |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 805 | /* nothing more was parsed or parsing was stopped on incomplete |
| 806 | * chunk, we can exit, handling parsing error if necessary. |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 807 | */ |
Christopher Faulet | bdcefe5 | 2021-05-21 11:05:12 +0200 | [diff] [blame] | 808 | if (!ret || h1m->state != H1_MSG_CHUNK_SIZE) { |
Christopher Faulet | 0d4c924 | 2021-05-21 10:56:24 +0200 | [diff] [blame] | 809 | if ((*dsthtx)->flags & HTX_FL_PARSING_ERROR) |
| 810 | total = 0; |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 815 | return total; |
| 816 | } |
| 817 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 818 | /* Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 819 | * couldn't proceed. Parsing errors are reported by setting the htx flags |
| 820 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This |
| 821 | * functions is responsible to update the parser state <h1m>. |
| 822 | */ |
Christopher Faulet | de471a4 | 2021-02-01 16:37:28 +0100 | [diff] [blame] | 823 | size_t h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, |
| 824 | struct buffer *srcbuf, size_t ofs, size_t max, |
| 825 | struct buffer *htxbuf) |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 826 | { |
Christopher Faulet | de471a4 | 2021-02-01 16:37:28 +0100 | [diff] [blame] | 827 | size_t sz, total = 0; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 828 | |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 829 | if (b_data(srcbuf) == ofs || !max) |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 830 | return 0; |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 831 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 832 | if (h1m->flags & H1_MF_CLEN) { |
| 833 | /* content-length: read only h2m->body_len */ |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 834 | sz = b_data(srcbuf) - ofs; |
| 835 | if (unlikely(sz > h1m->curr_len)) |
Christopher Faulet | de471a4 | 2021-02-01 16:37:28 +0100 | [diff] [blame] | 836 | sz = h1m->curr_len; |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 837 | sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, max, htxbuf); |
| 838 | h1m->curr_len -= sz; |
| 839 | (*dsthtx)->extra = h1m->curr_len; |
| 840 | total += sz; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 841 | if (!h1m->curr_len) { |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 842 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 843 | (*dsthtx)->flags |= HTX_FL_EOM; |
| 844 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 845 | } |
| 846 | else if (h1m->flags & H1_MF_CHNK) { |
| 847 | /* te:chunked : parse chunks */ |
Christopher Faulet | 140691b | 2021-02-03 11:51:24 +0100 | [diff] [blame] | 848 | total += h1_parse_msg_chunks(h1m, dsthtx, srcbuf, ofs, max, htxbuf); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 849 | } |
| 850 | else if (h1m->flags & H1_MF_XFER_LEN) { |
| 851 | /* XFER_LEN is set but not CLEN nor CHNK, it means there is no |
| 852 | * body. Switch the message in DONE state |
| 853 | */ |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 854 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 855 | (*dsthtx)->flags |= HTX_FL_EOM; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 856 | } |
| 857 | else { |
| 858 | /* no content length, read till SHUTW */ |
Christopher Faulet | f7c2044 | 2021-02-02 19:40:07 +0100 | [diff] [blame] | 859 | sz = b_data(srcbuf) - ofs; |
| 860 | sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, max, htxbuf); |
| 861 | total += sz; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 864 | return total; |
| 865 | } |
| 866 | |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 867 | /* Parse HTTP/1 trailers. It returns the number of bytes parsed on success, 0 if |
| 868 | * trailers are incomplete, -1 if an error occurred or -2 if it needs more space |
| 869 | * to proceed while the output buffer is not empty. Parsing errors are reported |
| 870 | * by setting the htx flags HTX_FL_PARSING_ERROR and filling h1m->err_pos and |
| 871 | * h1m->err_state fields. This functions is responsible to update the parser |
| 872 | * state <h1m>. |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 873 | */ |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 874 | int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx, |
| 875 | struct buffer *srcbuf, size_t ofs, size_t max) |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 876 | { |
| 877 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 878 | struct h1m tlr_h1m; |
| 879 | int ret = 0; |
| 880 | |
Christopher Faulet | ae660be | 2022-04-13 17:48:54 +0200 | [diff] [blame] | 881 | if (b_data(srcbuf) == ofs) { |
| 882 | /* Nothing to parse */ |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 883 | goto end; |
Christopher Faulet | ae660be | 2022-04-13 17:48:54 +0200 | [diff] [blame] | 884 | } |
| 885 | if (!max) { |
| 886 | /* No more room */ |
| 887 | goto output_full; |
| 888 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 889 | |
| 890 | /* Realing input buffer if necessary */ |
| 891 | if (b_peek(srcbuf, ofs) > b_tail(srcbuf)) |
Christopher Faulet | 00d7cde | 2021-02-04 11:01:51 +0100 | [diff] [blame] | 892 | b_slow_realign_ofs(srcbuf, trash.area, 0); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 893 | |
| 894 | tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY); |
| 895 | ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf), |
| 896 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL); |
| 897 | if (ret <= 0) { |
| 898 | /* Incomplete or invalid trailers. If the input buffer only |
| 899 | * contains trailers and is full, which is detected by it being |
| 900 | * full and the offset to be zero, it's an error because |
| 901 | * trailers are too large to be handled by the parser. */ |
| 902 | if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf))) |
| 903 | goto error; |
| 904 | goto end; |
| 905 | } |
| 906 | |
| 907 | /* messages trailers fully parsed. */ |
| 908 | if (h1_eval_htx_hdrs_size(hdrs) > max) { |
| 909 | if (htx_is_empty(dsthtx)) |
| 910 | goto error; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 911 | goto output_full; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | if (!htx_add_all_trailers(dsthtx, hdrs)) |
| 915 | goto error; |
| 916 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 917 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 918 | dsthtx->flags |= HTX_FL_EOM; |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 919 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 920 | end: |
| 921 | return ret; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 922 | output_full: |
| 923 | return -2; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 924 | error: |
| 925 | h1m->err_state = h1m->state; |
| 926 | h1m->err_pos = h1m->next; |
| 927 | dsthtx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 928 | return -1; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 931 | /* Appends the H1 representation of the request line <sl> to the chunk <chk>. It |
| 932 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 933 | */ |
| 934 | int h1_format_htx_reqline(const struct htx_sl *sl, struct buffer *chk) |
| 935 | { |
| 936 | struct ist uri; |
| 937 | size_t sz = chk->data; |
| 938 | |
Christopher Faulet | fb38c91 | 2021-04-26 09:38:55 +0200 | [diff] [blame] | 939 | uri = h1_get_uri(sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 940 | if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) || |
| 941 | !chunk_memcat(chk, " ", 1) || |
| 942 | !chunk_memcat(chk, uri.ptr, uri.len) || |
| 943 | !chunk_memcat(chk, " ", 1)) |
| 944 | goto full; |
| 945 | |
| 946 | if (sl->flags & HTX_SL_F_VER_11) { |
| 947 | if (!chunk_memcat(chk, "HTTP/1.1", 8)) |
| 948 | goto full; |
| 949 | } |
| 950 | else { |
| 951 | if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl))) |
| 952 | goto full; |
| 953 | } |
| 954 | |
| 955 | if (!chunk_memcat(chk, "\r\n", 2)) |
| 956 | goto full; |
| 957 | |
| 958 | return 1; |
| 959 | |
| 960 | full: |
| 961 | chk->data = sz; |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | /* Appends the H1 representation of the status line <sl> to the chunk <chk>. It |
| 966 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 967 | */ |
| 968 | int h1_format_htx_stline(const struct htx_sl *sl, struct buffer *chk) |
| 969 | { |
| 970 | size_t sz = chk->data; |
| 971 | |
| 972 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
| 973 | return 0; |
| 974 | |
| 975 | if (sl->flags & HTX_SL_F_VER_11) { |
| 976 | if (!chunk_memcat(chk, "HTTP/1.1", 8)) |
| 977 | goto full; |
| 978 | } |
| 979 | else { |
| 980 | if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl))) |
| 981 | goto full; |
| 982 | } |
| 983 | if (!chunk_memcat(chk, " ", 1) || |
| 984 | !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) || |
| 985 | !chunk_memcat(chk, " ", 1) || |
| 986 | !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) || |
| 987 | !chunk_memcat(chk, "\r\n", 2)) |
| 988 | goto full; |
| 989 | |
| 990 | return 1; |
| 991 | |
| 992 | full: |
| 993 | chk->data = sz; |
| 994 | return 0; |
| 995 | } |
| 996 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 997 | /* Appends the H1 representation of the header <n> with the value <v> to the |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 998 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 999 | * returns 0. |
| 1000 | */ |
| 1001 | int h1_format_htx_hdr(const struct ist n, const struct ist v, struct buffer *chk) |
| 1002 | { |
| 1003 | size_t sz = chk->data; |
| 1004 | |
| 1005 | if (n.len + v.len + 4 > b_room(chk)) |
| 1006 | return 0; |
| 1007 | |
| 1008 | if (!chunk_memcat(chk, n.ptr, n.len) || |
| 1009 | !chunk_memcat(chk, ": ", 2) || |
| 1010 | !chunk_memcat(chk, v.ptr, v.len) || |
| 1011 | !chunk_memcat(chk, "\r\n", 2)) |
| 1012 | goto full; |
| 1013 | |
| 1014 | return 1; |
| 1015 | |
| 1016 | full: |
| 1017 | chk->data = sz; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | /* Appends the H1 representation of the data <data> to the chunk <chk>. If |
| 1022 | * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if |
| 1023 | * data are successfully appended, otherwise it returns 0. |
| 1024 | */ |
| 1025 | int h1_format_htx_data(const struct ist data, struct buffer *chk, int chunked) |
| 1026 | { |
| 1027 | size_t sz = chk->data; |
| 1028 | |
| 1029 | if (chunked) { |
| 1030 | uint32_t chksz; |
| 1031 | char tmp[10]; |
| 1032 | char *beg, *end; |
| 1033 | |
| 1034 | chksz = data.len; |
| 1035 | |
| 1036 | beg = end = tmp+10; |
| 1037 | *--beg = '\n'; |
| 1038 | *--beg = '\r'; |
| 1039 | do { |
| 1040 | *--beg = hextab[chksz & 0xF]; |
| 1041 | } while (chksz >>= 4); |
| 1042 | |
| 1043 | if (!chunk_memcat(chk, beg, end - beg) || |
| 1044 | !chunk_memcat(chk, data.ptr, data.len) || |
| 1045 | !chunk_memcat(chk, "\r\n", 2)) |
| 1046 | goto full; |
| 1047 | } |
| 1048 | else { |
| 1049 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | return 1; |
| 1054 | |
| 1055 | full: |
| 1056 | chk->data = sz; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1060 | /* |
| 1061 | * Local variables: |
| 1062 | * c-indent-level: 8 |
| 1063 | * c-basic-offset: 8 |
| 1064 | * End: |
| 1065 | */ |