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 | c00d814 | 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; |
| 168 | h1m_init_res(h1m); |
| 169 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* By default, request have always a known length */ |
| 174 | h1m->flags |= H1_MF_XFER_LEN; |
| 175 | |
| 176 | if (h1sl->rq.meth == HTTP_METH_CONNECT) { |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 177 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 178 | h1m->curr_len = h1m->body_len = 0; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Christopher Faulet | 673504a | 2021-09-09 09:52:51 +0200 | [diff] [blame] | 181 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 182 | flags = h1m_htx_sl_flags(h1m); |
Christopher Faulet | 673504a | 2021-09-09 09:52:51 +0200 | [diff] [blame] | 183 | if ((flags & (HTX_SL_F_CONN_UPG|HTX_SL_F_BODYLESS)) == HTX_SL_F_CONN_UPG) { |
| 184 | int i; |
| 185 | |
| 186 | for (i = 0; hdrs[i].n.len; i++) { |
| 187 | if (isteqi(hdrs[i].n, ist("upgrade"))) |
| 188 | hdrs[i].v = IST_NULL; |
| 189 | } |
| 190 | h1m->flags &=~ H1_MF_CONN_UPG; |
| 191 | flags &= ~HTX_SL_F_CONN_UPG; |
| 192 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 193 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, uri, vsn); |
| 194 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
| 195 | goto error; |
| 196 | sl->info.req.meth = h1sl->rq.meth; |
| 197 | |
Christopher Faulet | fe451fb | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 198 | /* Check if the uri contains an authority. Also check if it contains an |
| 199 | * explicit scheme and if it is "http" or "https". */ |
| 200 | if (h1sl->rq.meth == HTTP_METH_CONNECT) |
| 201 | sl->flags |= HTX_SL_F_HAS_AUTHORITY; |
| 202 | else if (uri.len && uri.ptr[0] != '/' && uri.ptr[0] != '*') { |
| 203 | sl->flags |= (HTX_SL_F_HAS_AUTHORITY|HTX_SL_F_HAS_SCHM); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 204 | if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h') |
| 205 | sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
Amaury Denoyelle | c00d814 | 2021-07-07 10:49:27 +0200 | [diff] [blame] | 206 | |
| 207 | /* absolute-form target URI present, proceed to scheme-based |
| 208 | * normalization */ |
| 209 | http_scheme_based_normalize(htx); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 210 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 211 | |
| 212 | /* If body length cannot be determined, set htx->extra to |
| 213 | * ULLONG_MAX. This value is impossible in other cases. |
| 214 | */ |
| 215 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
| 216 | |
| 217 | end: |
| 218 | return 1; |
| 219 | error: |
| 220 | h1m->err_pos = h1m->next; |
| 221 | h1m->err_state = h1m->state; |
| 222 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* Postprocess the parsed headers for a response and convert them into an htx |
| 227 | * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't |
| 228 | * proceed. Parsing errors are reported by setting the htx flag |
| 229 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. |
| 230 | */ |
| 231 | static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx, |
| 232 | struct http_hdr *hdrs, size_t max) |
| 233 | { |
| 234 | struct htx_sl *sl; |
| 235 | struct ist vsn, status, reason; |
| 236 | unsigned int flags; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 237 | uint16_t code = 0; |
| 238 | |
| 239 | if (h1sl) { |
| 240 | /* For HTTP responses, the start-line was parsed */ |
| 241 | code = h1sl->st.status; |
| 242 | vsn = h1sl->st.v; |
| 243 | status = h1sl->st.c; |
| 244 | reason = h1sl->st.r; |
| 245 | } |
| 246 | else { |
| 247 | /* For FCGI responses, there is no start(-line but the "Status" |
| 248 | * header must be parsed, if found. |
| 249 | */ |
| 250 | int hdr; |
| 251 | |
| 252 | vsn = ((h1m->flags & H1_MF_VER_11) ? ist("HTTP/1.1") : ist("HTTP/1.0")); |
| 253 | for (hdr = 0; hdrs[hdr].n.len; hdr++) { |
| 254 | if (isteqi(hdrs[hdr].n, ist("status"))) { |
| 255 | code = http_parse_status_val(hdrs[hdr].v, &status, &reason); |
| 256 | } |
| 257 | else if (isteqi(hdrs[hdr].n, ist("location"))) { |
| 258 | code = 302; |
| 259 | status = ist("302"); |
| 260 | reason = ist("Moved Temporarily"); |
| 261 | } |
| 262 | } |
| 263 | if (!code) { |
| 264 | code = 200; |
| 265 | status = ist("200"); |
| 266 | reason = ist("OK"); |
| 267 | } |
| 268 | /* FIXME: Check the codes 1xx ? */ |
| 269 | } |
| 270 | |
| 271 | /* Be sure the message, once converted into HTX, will not exceed the max |
| 272 | * size allowed. |
| 273 | */ |
| 274 | if (h1_eval_htx_size(vsn, status, reason, hdrs) > max) { |
| 275 | if (htx_is_empty(htx)) |
| 276 | goto error; |
| 277 | h1m_init_res(h1m); |
| 278 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 279 | return 0; |
| 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; |
| 312 | error: |
| 313 | h1m->err_pos = h1m->next; |
| 314 | h1m->err_state = h1m->state; |
| 315 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /* Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if |
| 320 | * it couldn't proceed. Parsing errors are reported by setting the htx flag |
| 321 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This |
| 322 | * functions is responsible to update the parser state <h1m> and the start-line |
| 323 | * <h1sl> if not NULL. |
| 324 | * For the requests, <h1sl> must always be provided. For responses, <h1sl> may |
| 325 | * be NULL and <h1m> flags HTTP_METH_CONNECT of HTTP_METH_HEAD may be set. |
| 326 | */ |
| 327 | int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx, |
| 328 | struct buffer *srcbuf, size_t ofs, size_t max) |
| 329 | { |
| 330 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 331 | int ret = 0; |
| 332 | |
| 333 | if (!max || !b_data(srcbuf)) |
| 334 | goto end; |
| 335 | |
| 336 | /* Realing input buffer if necessary */ |
| 337 | if (b_head(srcbuf) + b_data(srcbuf) > b_wrap(srcbuf)) |
| 338 | b_slow_realign(srcbuf, trash.area, 0); |
| 339 | |
| 340 | if (!h1sl) { |
| 341 | /* If there no start-line, be sure to only parse the headers */ |
| 342 | h1m->flags |= H1_MF_HDRS_ONLY; |
| 343 | } |
| 344 | ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf), |
| 345 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, h1sl); |
| 346 | if (ret <= 0) { |
| 347 | /* Incomplete or invalid message. If the input buffer only |
| 348 | * contains headers and is full, which is detected by it being |
| 349 | * full and the offset to be zero, it's an error because |
| 350 | * headers are too large to be handled by the parser. */ |
| 351 | if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf))) |
| 352 | goto error; |
| 353 | goto end; |
| 354 | } |
| 355 | |
| 356 | /* messages headers fully parsed, do some checks to prepare the body |
| 357 | * parsing. |
| 358 | */ |
| 359 | |
| 360 | if (!(h1m->flags & H1_MF_RESP)) { |
| 361 | if (!h1_process_req_vsn(h1m, h1sl)) { |
| 362 | h1m->err_pos = h1sl->rq.v.ptr - b_head(srcbuf); |
| 363 | h1m->err_state = h1m->state; |
| 364 | goto vsn_error; |
| 365 | } |
| 366 | if (!h1_postparse_req_hdrs(h1m, h1sl, dsthtx, hdrs, max)) |
| 367 | ret = 0; |
| 368 | } |
| 369 | else { |
| 370 | if (h1sl && !h1_process_res_vsn(h1m, h1sl)) { |
| 371 | h1m->err_pos = h1sl->st.v.ptr - b_head(srcbuf); |
| 372 | h1m->err_state = h1m->state; |
| 373 | goto vsn_error; |
| 374 | } |
| 375 | if (!h1_postparse_res_hdrs(h1m, h1sl, dsthtx, hdrs, max)) |
| 376 | ret = 0; |
| 377 | } |
| 378 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 379 | /* Switch messages without any payload to DONE state */ |
| 380 | if (((h1m->flags & H1_MF_CLEN) && h1m->body_len == 0) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 381 | ((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] | 382 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 383 | dsthtx->flags |= HTX_FL_EOM; |
| 384 | } |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 385 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 386 | end: |
| 387 | return ret; |
| 388 | error: |
| 389 | h1m->err_pos = h1m->next; |
| 390 | h1m->err_state = h1m->state; |
| 391 | vsn_error: |
| 392 | dsthtx->flags |= HTX_FL_PARSING_ERROR; |
| 393 | return 0; |
| 394 | |
| 395 | } |
| 396 | |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 397 | /* Copy data from <srbuf> into an DATA block in <dsthtx>. If possible, a |
| 398 | * zero-copy is performed. It returns the number of bytes copied. |
| 399 | */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 400 | static int h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs, |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 401 | size_t count, struct buffer *htxbuf) |
| 402 | { |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 403 | struct htx *tmp_htx = *dsthtx; |
| 404 | |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 405 | /* very often with large files we'll face the following |
| 406 | * situation : |
| 407 | * - htx is empty and points to <htxbuf> |
| 408 | * - ret == srcbuf->data |
| 409 | * - srcbuf->head == sizeof(struct htx) |
| 410 | * => we can swap the buffers and place an htx header into |
| 411 | * the target buffer instead |
| 412 | */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 413 | if (unlikely(htx_is_empty(tmp_htx) && count == b_data(srcbuf) && |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 414 | !ofs && b_head_ofs(srcbuf) == sizeof(struct htx))) { |
| 415 | void *raw_area = srcbuf->area; |
| 416 | void *htx_area = htxbuf->area; |
| 417 | struct htx_blk *blk; |
| 418 | |
| 419 | srcbuf->area = htx_area; |
| 420 | htxbuf->area = raw_area; |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 421 | tmp_htx = (struct htx *)htxbuf->area; |
| 422 | tmp_htx->size = htxbuf->size - sizeof(*tmp_htx); |
| 423 | htx_reset(tmp_htx); |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 424 | b_set_data(htxbuf, b_size(htxbuf)); |
| 425 | |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 426 | blk = htx_add_blk(tmp_htx, HTX_BLK_DATA, count); |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 427 | blk->info += count; |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 428 | |
| 429 | *dsthtx = tmp_htx; |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 430 | /* nothing else to do, the old buffer now contains an |
| 431 | * empty pre-initialized HTX header |
| 432 | */ |
| 433 | return count; |
| 434 | } |
| 435 | |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 436 | return htx_add_data(*dsthtx, ist2(b_peek(srcbuf, ofs), count)); |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 439 | /* Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 440 | * couldn't proceed. Parsing errors are reported by setting the htx flags |
| 441 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This |
| 442 | * functions is responsible to update the parser state <h1m>. |
| 443 | */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 444 | int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx, |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 445 | struct buffer *srcbuf, size_t ofs, size_t max, |
| 446 | struct buffer *htxbuf) |
| 447 | { |
| 448 | size_t total = 0; |
| 449 | int32_t ret = 0; |
| 450 | |
| 451 | if (h1m->flags & H1_MF_CLEN) { |
| 452 | /* content-length: read only h2m->body_len */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 453 | ret = htx_get_max_blksz(*dsthtx, max); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 454 | if ((uint64_t)ret > h1m->curr_len) |
| 455 | ret = h1m->curr_len; |
| 456 | if (ret > b_contig_data(srcbuf, ofs)) |
| 457 | ret = b_contig_data(srcbuf, ofs); |
| 458 | if (ret) { |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 459 | int32_t try = ret; |
| 460 | |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 461 | ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 462 | h1m->curr_len -= ret; |
| 463 | max -= sizeof(struct htx_blk) + ret; |
| 464 | ofs += ret; |
| 465 | total += ret; |
| 466 | if (ret < try) |
| 467 | goto end; |
| 468 | } |
| 469 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 470 | if (!h1m->curr_len) { |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 471 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 472 | (*dsthtx)->flags |= HTX_FL_EOM; |
| 473 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 474 | } |
| 475 | else if (h1m->flags & H1_MF_CHNK) { |
| 476 | /* te:chunked : parse chunks */ |
| 477 | new_chunk: |
| 478 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
| 479 | ret = h1_skip_chunk_crlf(srcbuf, ofs, b_data(srcbuf)); |
| 480 | if (ret <= 0) |
| 481 | goto end; |
| 482 | h1m->state = H1_MSG_CHUNK_SIZE; |
| 483 | ofs += ret; |
| 484 | total += ret; |
| 485 | } |
| 486 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Christopher Faulet | 405f054 | 2021-01-27 15:17:13 +0100 | [diff] [blame] | 487 | uint64_t chksz; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 488 | |
| 489 | ret = h1_parse_chunk_size(srcbuf, ofs, b_data(srcbuf), &chksz); |
| 490 | if (ret <= 0) |
| 491 | goto end; |
| 492 | h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA); |
| 493 | h1m->curr_len = chksz; |
| 494 | h1m->body_len += chksz; |
| 495 | ofs += ret; |
| 496 | total += ret; |
| 497 | if (!h1m->curr_len) |
| 498 | goto end; |
| 499 | } |
| 500 | if (h1m->state == H1_MSG_DATA) { |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 501 | ret = htx_get_max_blksz(*dsthtx, max); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 502 | if ((uint64_t)ret > h1m->curr_len) |
| 503 | ret = h1m->curr_len; |
| 504 | if (ret > b_contig_data(srcbuf, ofs)) |
| 505 | ret = b_contig_data(srcbuf, ofs); |
| 506 | if (ret) { |
| 507 | int32_t try = ret; |
| 508 | |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 509 | ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 510 | h1m->curr_len -= ret; |
| 511 | max -= sizeof(struct htx_blk) + ret; |
| 512 | ofs += ret; |
| 513 | total += ret; |
| 514 | if (ret < try) |
| 515 | goto end; |
| 516 | } |
| 517 | if (!h1m->curr_len) { |
| 518 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 519 | goto new_chunk; |
| 520 | } |
| 521 | goto end; |
| 522 | } |
| 523 | } |
| 524 | else if (h1m->flags & H1_MF_XFER_LEN) { |
| 525 | /* XFER_LEN is set but not CLEN nor CHNK, it means there is no |
| 526 | * body. Switch the message in DONE state |
| 527 | */ |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 528 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 529 | (*dsthtx)->flags |= HTX_FL_EOM; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 530 | } |
| 531 | else { |
| 532 | /* no content length, read till SHUTW */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 533 | ret = htx_get_max_blksz(*dsthtx, max); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 534 | if (ret > b_contig_data(srcbuf, ofs)) |
| 535 | ret = b_contig_data(srcbuf, ofs); |
| 536 | if (ret) |
Christopher Faulet | cc3124c | 2019-08-12 22:42:21 +0200 | [diff] [blame] | 537 | total += h1_copy_msg_data(dsthtx, srcbuf, ofs, ret, htxbuf); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | end: |
| 541 | if (ret < 0) { |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 542 | (*dsthtx)->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 543 | h1m->err_state = h1m->state; |
| 544 | h1m->err_pos = ofs; |
| 545 | total = 0; |
| 546 | } |
| 547 | |
| 548 | /* update htx->extra, only when the body length is known */ |
| 549 | if (h1m->flags & H1_MF_XFER_LEN) |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 550 | (*dsthtx)->extra = h1m->curr_len; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 551 | return total; |
| 552 | } |
| 553 | |
| 554 | /* Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if |
| 555 | * it couldn't proceed. Parsing errors are reported by setting the htx flags |
| 556 | * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This |
| 557 | * functions is responsible to update the parser state <h1m>. |
| 558 | */ |
| 559 | int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx, |
| 560 | struct buffer *srcbuf, size_t ofs, size_t max) |
| 561 | { |
| 562 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 563 | struct h1m tlr_h1m; |
| 564 | int ret = 0; |
| 565 | |
| 566 | if (!max || !b_data(srcbuf)) |
| 567 | goto end; |
| 568 | |
| 569 | /* Realing input buffer if necessary */ |
| 570 | if (b_peek(srcbuf, ofs) > b_tail(srcbuf)) |
| 571 | b_slow_realign(srcbuf, trash.area, 0); |
| 572 | |
| 573 | tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY); |
| 574 | ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf), |
| 575 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL); |
| 576 | if (ret <= 0) { |
| 577 | /* Incomplete or invalid trailers. If the input buffer only |
| 578 | * contains trailers and is full, which is detected by it being |
| 579 | * full and the offset to be zero, it's an error because |
| 580 | * trailers are too large to be handled by the parser. */ |
| 581 | if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf))) |
| 582 | goto error; |
| 583 | goto end; |
| 584 | } |
| 585 | |
| 586 | /* messages trailers fully parsed. */ |
| 587 | if (h1_eval_htx_hdrs_size(hdrs) > max) { |
| 588 | if (htx_is_empty(dsthtx)) |
| 589 | goto error; |
| 590 | ret = 0; |
| 591 | goto end; |
| 592 | } |
| 593 | |
| 594 | if (!htx_add_all_trailers(dsthtx, hdrs)) |
| 595 | goto error; |
| 596 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 597 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 598 | dsthtx->flags |= HTX_FL_EOM; |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 599 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 600 | end: |
| 601 | return ret; |
| 602 | error: |
| 603 | h1m->err_state = h1m->state; |
| 604 | h1m->err_pos = h1m->next; |
| 605 | dsthtx->flags |= HTX_FL_PARSING_ERROR; |
| 606 | return 0; |
| 607 | } |
| 608 | |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 609 | /* Appends the H1 representation of the request line <sl> to the chunk <chk>. It |
| 610 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 611 | */ |
| 612 | int h1_format_htx_reqline(const struct htx_sl *sl, struct buffer *chk) |
| 613 | { |
| 614 | struct ist uri; |
| 615 | size_t sz = chk->data; |
| 616 | |
Christopher Faulet | fb38c91 | 2021-04-26 09:38:55 +0200 | [diff] [blame] | 617 | uri = h1_get_uri(sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 618 | if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) || |
| 619 | !chunk_memcat(chk, " ", 1) || |
| 620 | !chunk_memcat(chk, uri.ptr, uri.len) || |
| 621 | !chunk_memcat(chk, " ", 1)) |
| 622 | goto full; |
| 623 | |
| 624 | if (sl->flags & HTX_SL_F_VER_11) { |
| 625 | if (!chunk_memcat(chk, "HTTP/1.1", 8)) |
| 626 | goto full; |
| 627 | } |
| 628 | else { |
| 629 | if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl))) |
| 630 | goto full; |
| 631 | } |
| 632 | |
| 633 | if (!chunk_memcat(chk, "\r\n", 2)) |
| 634 | goto full; |
| 635 | |
| 636 | return 1; |
| 637 | |
| 638 | full: |
| 639 | chk->data = sz; |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* Appends the H1 representation of the status line <sl> to the chunk <chk>. It |
| 644 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 645 | */ |
| 646 | int h1_format_htx_stline(const struct htx_sl *sl, struct buffer *chk) |
| 647 | { |
| 648 | size_t sz = chk->data; |
| 649 | |
| 650 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
| 651 | return 0; |
| 652 | |
| 653 | if (sl->flags & HTX_SL_F_VER_11) { |
| 654 | if (!chunk_memcat(chk, "HTTP/1.1", 8)) |
| 655 | goto full; |
| 656 | } |
| 657 | else { |
| 658 | if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl))) |
| 659 | goto full; |
| 660 | } |
| 661 | if (!chunk_memcat(chk, " ", 1) || |
| 662 | !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) || |
| 663 | !chunk_memcat(chk, " ", 1) || |
| 664 | !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) || |
| 665 | !chunk_memcat(chk, "\r\n", 2)) |
| 666 | goto full; |
| 667 | |
| 668 | return 1; |
| 669 | |
| 670 | full: |
| 671 | chk->data = sz; |
| 672 | return 0; |
| 673 | } |
| 674 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 675 | /* 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] | 676 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 677 | * returns 0. |
| 678 | */ |
| 679 | int h1_format_htx_hdr(const struct ist n, const struct ist v, struct buffer *chk) |
| 680 | { |
| 681 | size_t sz = chk->data; |
| 682 | |
| 683 | if (n.len + v.len + 4 > b_room(chk)) |
| 684 | return 0; |
| 685 | |
| 686 | if (!chunk_memcat(chk, n.ptr, n.len) || |
| 687 | !chunk_memcat(chk, ": ", 2) || |
| 688 | !chunk_memcat(chk, v.ptr, v.len) || |
| 689 | !chunk_memcat(chk, "\r\n", 2)) |
| 690 | goto full; |
| 691 | |
| 692 | return 1; |
| 693 | |
| 694 | full: |
| 695 | chk->data = sz; |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* Appends the H1 representation of the data <data> to the chunk <chk>. If |
| 700 | * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if |
| 701 | * data are successfully appended, otherwise it returns 0. |
| 702 | */ |
| 703 | int h1_format_htx_data(const struct ist data, struct buffer *chk, int chunked) |
| 704 | { |
| 705 | size_t sz = chk->data; |
| 706 | |
| 707 | if (chunked) { |
| 708 | uint32_t chksz; |
| 709 | char tmp[10]; |
| 710 | char *beg, *end; |
| 711 | |
| 712 | chksz = data.len; |
| 713 | |
| 714 | beg = end = tmp+10; |
| 715 | *--beg = '\n'; |
| 716 | *--beg = '\r'; |
| 717 | do { |
| 718 | *--beg = hextab[chksz & 0xF]; |
| 719 | } while (chksz >>= 4); |
| 720 | |
| 721 | if (!chunk_memcat(chk, beg, end - beg) || |
| 722 | !chunk_memcat(chk, data.ptr, data.len) || |
| 723 | !chunk_memcat(chk, "\r\n", 2)) |
| 724 | goto full; |
| 725 | } |
| 726 | else { |
| 727 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | return 1; |
| 732 | |
| 733 | full: |
| 734 | chk->data = sz; |
| 735 | return 0; |
| 736 | } |
| 737 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 738 | /* |
| 739 | * Local variables: |
| 740 | * c-indent-level: 8 |
| 741 | * c-basic-offset: 8 |
| 742 | * End: |
| 743 | */ |