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