Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 2 | * include/common/htx.h |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 3 | * This file defines everything related to the internal HTTP messages. |
| 4 | * |
| 5 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 22 | #ifndef _COMMON_HTX_H |
| 23 | #define _COMMON_HTX_H |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 26 | #include <common/buf.h> |
| 27 | #include <common/config.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 28 | #include <common/ist.h> |
| 29 | #include <common/http.h> |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 30 | #include <common/http-hdr.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 31 | #include <common/standard.h> |
| 32 | |
| 33 | /* |
| 34 | * The internal representation of an HTTP message is a contiguous array |
| 35 | * containing both the blocks (htx_blk) and their contents. Blocks are stored |
| 36 | * starting from the end of the array while their contents are stored at the |
| 37 | * beginning. |
| 38 | * |
| 39 | * As data are sent to the peer, blocks and contents are released at the |
| 40 | * edges. This free space is reused when no more space left. So blocks and |
| 41 | * contents may wrap, not necessarily the same time. |
| 42 | * |
| 43 | * An HTTP block is as well a header as a body part or a trailer part. For all |
| 44 | * these types of block, a content is attached to the block. It can also be a |
| 45 | * mark, like the end-of-headers or end-of-message. For these blocks, there is |
| 46 | * no content but it count for a byte. It is important to not skip it when data |
| 47 | * are forwarded. An HTTP block is composed of 2 fields: |
| 48 | * |
| 49 | * - .info : It a 32 bits field containing the block's type on 4 bits |
| 50 | * followed by content' length. See below for details. |
| 51 | * |
| 52 | * - .addr : The content's address, if any, relatively to the beginning the |
| 53 | * array used to store the HTTP message itself. |
| 54 | * |
| 55 | * htx_blk.info representation: |
| 56 | * |
| 57 | * 0b 0000 0000 0000 0000 0000 0000 0000 0000 |
| 58 | * ---- ------------------------ --------- |
| 59 | * type value (1 MB max) name length (header) |
| 60 | * ---------------------------------- |
| 61 | * data length (256 MB max) |
| 62 | * (body, method, path, version, status, reason, trailers) |
| 63 | * |
| 64 | * types: |
| 65 | * - 0000 = request start-line |
| 66 | * - 0001 = response start-line |
| 67 | * - 0010 = header |
| 68 | * - 0011 = pseudo-header ou "special" header |
| 69 | * - 0100 = end-of-headers |
| 70 | * - 0101 = data |
| 71 | * - 0110 = end-of-data |
| 72 | * - 0111 = trailer |
| 73 | * - 1000 = end-of-message |
| 74 | * ... |
| 75 | * - 1101 = out-of-band |
| 76 | * - 1110 = error |
| 77 | * - 1111 = unused |
| 78 | * |
| 79 | */ |
| 80 | |
| 81 | /*HTX start-line flags */ |
| 82 | #define HTX_SL_F_NONE 0x00000000 |
| 83 | #define HTX_SL_F_IS_RESP 0x00000001 /* It is the response start-line (unset means the request one) */ |
| 84 | #define HTX_SL_F_XFER_LEN 0x00000002 /* The message xfer size can be dertermined */ |
| 85 | #define HTX_SL_F_XFER_ENC 0x00000004 /* The transfer-encoding header was found in message */ |
| 86 | #define HTX_SL_F_CLEN 0x00000008 /* The content-length header was found in message */ |
| 87 | #define HTX_SL_F_CHNK 0x00000010 /* The message payload is chunked */ |
| 88 | #define HTX_SL_F_VER_11 0x00000020 /* The message indicates version 1.1 or above */ |
| 89 | #define HTX_SL_F_BODYLESS 0x00000040 /* The message has no body (content-length = 0) */ |
Christopher Faulet | e21c016 | 2019-06-14 10:08:13 +0200 | [diff] [blame] | 90 | #define HTX_SL_F_HAS_SCHM 0x00000080 /* The scheme is explicitly specified */ |
| 91 | #define HTX_SL_F_SCHM_HTTP 0x00000100 /* The scheme HTTP should be used */ |
| 92 | #define HTX_SL_F_SCHM_HTTPS 0x00000200 /* The scheme HTTPS should be used */ |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 93 | |
| 94 | /* HTX flags */ |
| 95 | #define HTX_FL_NONE 0x00000000 |
| 96 | #define HTX_FL_PARSING_ERROR 0x00000001 |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 97 | #define HTX_FL_UPGRADE 0x00000002 |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 98 | |
| 99 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 100 | /* HTTP block's type (max 15). */ |
| 101 | enum htx_blk_type { |
| 102 | HTX_BLK_REQ_SL = 0, /* Request start-line */ |
| 103 | HTX_BLK_RES_SL = 1, /* Response start-line */ |
| 104 | HTX_BLK_HDR = 2, /* header name/value block */ |
Christopher Faulet | 39744f7 | 2019-05-24 14:54:00 +0200 | [diff] [blame] | 105 | HTX_BLK_EOH = 3, /* end-of-headers block */ |
| 106 | HTX_BLK_DATA = 4, /* data block */ |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 107 | HTX_BLK_TLR = 5, /* trailer name/value block */ |
| 108 | HTX_BLK_EOT = 6, /* end-of-trailers block */ |
| 109 | HTX_BLK_EOM = 7, /* end-of-message block */ |
Christopher Faulet | 39744f7 | 2019-05-24 14:54:00 +0200 | [diff] [blame] | 110 | /* 8 .. 14 unused */ |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 111 | HTX_BLK_UNUSED = 15, /* unused/removed block */ |
| 112 | }; |
| 113 | |
| 114 | /* One HTTP block descriptor */ |
| 115 | struct htx_blk { |
| 116 | uint32_t addr; /* relative storage address of a data block */ |
| 117 | uint32_t info; /* information about data stored */ |
| 118 | }; |
| 119 | |
| 120 | struct htx_ret { |
| 121 | int32_t ret; |
| 122 | struct htx_blk *blk; |
| 123 | }; |
| 124 | |
| 125 | struct htx_sl { |
| 126 | unsigned int flags; /* HTX_SL_F_* */ |
| 127 | union { |
| 128 | struct { |
| 129 | enum http_meth_t meth; /* method */ |
| 130 | } req; |
| 131 | struct { |
| 132 | uint16_t status; /* status code */ |
| 133 | } res; |
| 134 | } info; |
| 135 | |
| 136 | /* XXX 2 bytes unused */ |
| 137 | |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 138 | int32_t hdrs_bytes; /* Bytes held by all headers from this start-line |
| 139 | * to the corresponding EOH. -1 if unknown */ |
| 140 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 141 | unsigned int len[3]; /* length of differnt parts of the start-line */ |
| 142 | char l[0]; |
| 143 | }; |
| 144 | |
| 145 | /* Internal representation of an HTTP message */ |
| 146 | struct htx { |
| 147 | uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */ |
| 148 | uint32_t data; /* the data size, in bytes. To known to total size used by all allocated |
| 149 | * blocks (blocks and their contents), you need to add size used by blocks, |
| 150 | * i.e. [ used * sizeof(struct htx_blk *) ] */ |
| 151 | |
| 152 | uint32_t used; /* number of blocks in use */ |
| 153 | uint32_t tail; /* last inserted block */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 154 | uint32_t head; /* older inserted block */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 155 | |
| 156 | uint32_t tail_addr; /* start address of the free space in front of the the blocks table */ |
| 157 | uint32_t head_addr; /* start address of the free space at the beginning */ |
| 158 | uint32_t end_addr; /* end address of the free space at the beginning */ |
| 159 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 160 | |
| 161 | uint64_t extra; /* known bytes amount remaining to receive */ |
| 162 | uint32_t flags; /* HTX_FL_* */ |
| 163 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 164 | int32_t first; /* position of the first block to (re)start the analyse. -1 if unset. */ |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 165 | |
| 166 | struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */ |
| 167 | }; |
| 168 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 169 | |
| 170 | extern struct htx htx_empty; |
| 171 | |
| 172 | struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk); |
| 173 | struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz); |
| 174 | struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk); |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 175 | void htx_truncate(struct htx *htx, uint32_t offset); |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 176 | struct htx_ret htx_drain(struct htx *htx, uint32_t max); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 177 | |
| 178 | struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 179 | const struct ist old, const struct ist new); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 180 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 181 | enum htx_blk_type mark); |
| 182 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 183 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 184 | const struct ist p1, const struct ist p2, const struct ist p3); |
| 185 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 186 | const struct ist p2, const struct ist p3); |
| 187 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 188 | struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 189 | const struct ist name, const struct ist value); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 190 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 191 | struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 192 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value); |
Willy Tarreau | 52610e9 | 2019-01-03 18:26:17 +0100 | [diff] [blame] | 193 | struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 194 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 195 | struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 196 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 197 | struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data); |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 198 | size_t htx_add_data(struct htx *htx, const struct ist data); |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 199 | struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data); |
Christopher Faulet | 86fcf6d | 2019-06-11 10:41:19 +0200 | [diff] [blame] | 200 | void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 201 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 202 | int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk); |
| 203 | int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk); |
| 204 | int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk); |
| 205 | int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 206 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 207 | /* Functions and macros to get parts of the start-line or legnth of these |
| 208 | * parts |
| 209 | */ |
| 210 | #define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2]) |
| 211 | |
| 212 | #define HTX_SL_P1_LEN(sl) ((sl)->len[0]) |
| 213 | #define HTX_SL_P2_LEN(sl) ((sl)->len[1]) |
| 214 | #define HTX_SL_P3_LEN(sl) ((sl)->len[2]) |
| 215 | #define HTX_SL_P1_PTR(sl) ((sl)->l) |
| 216 | #define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl)) |
| 217 | #define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl)) |
| 218 | |
| 219 | #define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl) |
| 220 | #define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl) |
| 221 | #define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl) |
| 222 | #define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl) |
| 223 | #define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl) |
| 224 | #define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl) |
| 225 | |
| 226 | #define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl) |
| 227 | #define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl) |
| 228 | #define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl) |
| 229 | #define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl) |
| 230 | #define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl) |
| 231 | #define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl) |
| 232 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 233 | static inline struct ist htx_sl_p1(const struct htx_sl *sl) |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 234 | { |
| 235 | return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl)); |
| 236 | } |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 237 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 238 | static inline struct ist htx_sl_p2(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 239 | { |
| 240 | return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl)); |
| 241 | } |
| 242 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 243 | static inline struct ist htx_sl_p3(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 244 | { |
| 245 | return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl)); |
| 246 | } |
| 247 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 248 | static inline struct ist htx_sl_req_meth(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 249 | { |
| 250 | return htx_sl_p1(sl); |
| 251 | } |
| 252 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 253 | static inline struct ist htx_sl_req_uri(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 254 | { |
| 255 | return htx_sl_p2(sl); |
| 256 | } |
| 257 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 258 | static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 259 | { |
| 260 | return htx_sl_p3(sl); |
| 261 | } |
| 262 | |
| 263 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 264 | static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 265 | { |
| 266 | return htx_sl_p1(sl); |
| 267 | } |
| 268 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 269 | static inline struct ist htx_sl_res_code(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 270 | { |
| 271 | return htx_sl_p2(sl); |
| 272 | } |
| 273 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 274 | static inline struct ist htx_sl_res_reason(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 275 | { |
| 276 | return htx_sl_p3(sl); |
| 277 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 278 | |
| 279 | /* Returns the array index of a block given its position <pos> */ |
| 280 | static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos) |
| 281 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 282 | return ((htx->size / sizeof(htx->blocks[0])) - pos - 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Returns the position of the block <blk> */ |
| 286 | static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk) |
| 287 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 288 | return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Returns the block at the position <pos> */ |
| 292 | static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos) |
| 293 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 294 | return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Returns the type of the block <blk> */ |
| 298 | static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk) |
| 299 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 300 | return (blk->info >> 28); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 303 | /* Returns the size of the block <blk>, depending of its type */ |
| 304 | static inline uint32_t htx_get_blksz(const struct htx_blk *blk) |
| 305 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 306 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 307 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 308 | switch (type) { |
| 309 | case HTX_BLK_HDR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 310 | case HTX_BLK_TLR: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 311 | /* name.length + value.length */ |
| 312 | return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff)); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 313 | default: |
| 314 | /* value.length */ |
| 315 | return (blk->info & 0xfffffff); |
| 316 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 319 | /* Returns the position of the oldest entry (head). |
| 320 | * |
| 321 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 322 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 323 | * blocks to overflow a 32-bits signed integer ! |
| 324 | */ |
| 325 | static inline int32_t htx_get_head(const struct htx *htx) |
| 326 | { |
| 327 | return (htx->used ? htx->head : -1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* Returns the oldest HTX block (head) if the HTX message is not |
| 331 | * empty. Otherwise it returns NULL. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 332 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 333 | static inline struct htx_blk *htx_get_head_blk(const struct htx *htx) |
| 334 | { |
| 335 | int32_t head = htx_get_head(htx); |
| 336 | |
| 337 | return ((head == -1) ? NULL : htx_get_blk(htx, head)); |
| 338 | } |
| 339 | |
| 340 | /* Returns the type of the oldest HTX block (head) if the HTX message is not |
| 341 | * empty. Otherwise it returns HTX_BLK_UNUSED. |
| 342 | */ |
| 343 | static inline enum htx_blk_type htx_get_head_type(const struct htx *htx) |
| 344 | { |
| 345 | struct htx_blk *blk = htx_get_head_blk(htx); |
| 346 | |
| 347 | return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED); |
| 348 | } |
| 349 | |
| 350 | /* Returns the position of the newest entry (tail). |
| 351 | * |
| 352 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 353 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 354 | * blocks to overflow a 32-bits signed integer ! |
| 355 | */ |
| 356 | static inline int32_t htx_get_tail(const struct htx *htx) |
| 357 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 358 | return (htx->used ? htx->tail : -1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* Returns the newest HTX block (tail) if the HTX message is not |
| 362 | * empty. Otherwise it returns NULL. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 363 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 364 | static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx) |
| 365 | { |
| 366 | int32_t tail = htx_get_tail(htx); |
| 367 | |
| 368 | return ((tail == -1) ? NULL : htx_get_blk(htx, tail)); |
| 369 | } |
| 370 | |
| 371 | /* Returns the type of the newest HTX block (tail) if the HTX message is not |
| 372 | * empty. Otherwise it returns HTX_BLK_UNUSED. |
| 373 | */ |
| 374 | static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx) |
| 375 | { |
| 376 | struct htx_blk *blk = htx_get_tail_blk(htx); |
| 377 | |
| 378 | return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED); |
| 379 | } |
| 380 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 381 | /* Returns the position of the first block in the HTX message <htx>. If unset, |
| 382 | * or if <htx> is empty, -1 is returned. |
Christopher Faulet | a3ad6b1 | 2019-05-13 11:36:27 +0200 | [diff] [blame] | 383 | * |
| 384 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 385 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 386 | * blocks to overflow a 32-bits signed integer ! |
| 387 | */ |
| 388 | static inline int32_t htx_get_first(const struct htx *htx) |
| 389 | { |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 390 | if (!htx->used) |
| 391 | return -1; |
| 392 | return htx->first; |
Christopher Faulet | a3ad6b1 | 2019-05-13 11:36:27 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 395 | /* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is |
| 396 | * empty, NULL returned. |
Christopher Faulet | a3ad6b1 | 2019-05-13 11:36:27 +0200 | [diff] [blame] | 397 | */ |
| 398 | static inline struct htx_blk *htx_get_first_blk(const struct htx *htx) |
| 399 | { |
| 400 | int32_t pos; |
| 401 | |
| 402 | pos = htx_get_first(htx); |
| 403 | return ((pos == -1) ? NULL : htx_get_blk(htx, pos)); |
| 404 | } |
| 405 | |
| 406 | /* Returns the type of the first block in the HTX message <htx>. If unset or if |
| 407 | * <htx> is empty, HTX_BLK_UNUSED is returned. |
| 408 | */ |
| 409 | static inline enum htx_blk_type htx_get_first_type(const struct htx *htx) |
| 410 | { |
| 411 | struct htx_blk *blk = htx_get_first_blk(htx); |
| 412 | |
| 413 | return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED); |
| 414 | } |
| 415 | |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 416 | /* Returns the position of block immediately before the one pointed by <pos>. If |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 417 | * the message is empty or if <pos> is the position of the head, -1 returned. |
| 418 | * |
| 419 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 420 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 421 | * blocks to overflow a 32-bits signed integer ! |
| 422 | */ |
| 423 | static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos) |
| 424 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 425 | int32_t head; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 426 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 427 | head = htx_get_head(htx); |
| 428 | if (head == -1 || pos == head) |
| 429 | return -1; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 430 | return (pos - 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 433 | /* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the |
| 434 | * head, NULL returned. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 435 | */ |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 436 | static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx, |
| 437 | const struct htx_blk *blk) |
| 438 | { |
| 439 | int32_t pos; |
| 440 | |
| 441 | pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk)); |
| 442 | return ((pos == -1) ? NULL : htx_get_blk(htx, pos)); |
| 443 | } |
| 444 | |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 445 | /* Returns the position of block immediately after the one pointed by <pos>. If |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 446 | * the message is empty or if <pos> is the position of the tail, -1 returned. |
| 447 | * |
| 448 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 449 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 450 | * blocks to overflow a 32-bits signed integer ! |
| 451 | */ |
| 452 | static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos) |
| 453 | { |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 454 | if (!htx->used || pos == htx->tail) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 455 | return -1; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 456 | return (pos + 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 457 | |
| 458 | } |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 459 | |
| 460 | /* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the |
| 461 | * tail, NULL returned. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 462 | */ |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 463 | static inline struct htx_blk *htx_get_next_blk(const struct htx *htx, |
| 464 | const struct htx_blk *blk) |
| 465 | { |
| 466 | int32_t pos; |
| 467 | |
| 468 | pos = htx_get_next(htx, htx_get_blk_pos(htx, blk)); |
| 469 | return ((pos == -1) ? NULL : htx_get_blk(htx, pos)); |
| 470 | } |
| 471 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 472 | /* Changes the size of the value. It is the caller responsibility to change the |
Christopher Faulet | 8d09dc2 | 2019-06-18 09:37:00 +0200 | [diff] [blame] | 473 | * value itself, make sure there is enough space and update allocated |
| 474 | * value. This function updates the HTX message accordingly. |
| 475 | */ |
| 476 | static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk, uint32_t newlen) |
| 477 | { |
| 478 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 479 | uint32_t oldlen, sz; |
| 480 | int32_t delta; |
| 481 | |
| 482 | sz = htx_get_blksz(blk); |
| 483 | switch (type) { |
| 484 | case HTX_BLK_HDR: |
| 485 | case HTX_BLK_TLR: |
| 486 | oldlen = (blk->info >> 8) & 0xfffff; |
| 487 | blk->info = (type << 28) + (newlen << 8) + (blk->info & 0xff); |
| 488 | break; |
| 489 | default: |
| 490 | oldlen = blk->info & 0xfffffff; |
| 491 | blk->info = (type << 28) + newlen; |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | /* Update HTTP message */ |
| 496 | delta = (newlen - oldlen); |
| 497 | htx->data += delta; |
| 498 | if (blk->addr+sz == htx->tail_addr) |
| 499 | htx->tail_addr += delta; |
| 500 | else if (blk->addr+sz == htx->head_addr) |
| 501 | htx->head_addr += delta; |
| 502 | } |
| 503 | |
| 504 | /* Changes the size of the value. It is the caller responsibility to change the |
| 505 | * value itself, make sure there is enough space and update allocated |
| 506 | * value. Unlike the function htx_change_blk_value_len(), this one does not |
| 507 | * update the HTX message. So it should be used with caution. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 508 | */ |
| 509 | static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen) |
| 510 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 511 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 512 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 513 | switch (type) { |
| 514 | case HTX_BLK_HDR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 515 | case HTX_BLK_TLR: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 516 | blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff); |
| 517 | break; |
| 518 | case HTX_BLK_REQ_SL: |
| 519 | case HTX_BLK_RES_SL: |
| 520 | case HTX_BLK_DATA: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 521 | blk->info = (type << 28) + vlen; |
| 522 | break; |
| 523 | default: |
| 524 | /* Unexpected case */ |
| 525 | break; |
| 526 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Returns the data pointer of the block <blk> */ |
| 530 | static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk) |
| 531 | { |
| 532 | return ((void *)htx->blocks + blk->addr); |
| 533 | } |
| 534 | |
| 535 | /* Returns the name of the block <blk>, only if it is a header. Otherwise it |
| 536 | * returns an empty name. |
| 537 | */ |
| 538 | static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk) |
| 539 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 540 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 541 | struct ist ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 542 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 543 | switch (type) { |
| 544 | case HTX_BLK_HDR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 545 | case HTX_BLK_TLR: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 546 | ret.ptr = htx_get_blk_ptr(htx, blk); |
| 547 | ret.len = blk->info & 0xff; |
| 548 | break; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 549 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 550 | default: |
| 551 | return ist(""); |
| 552 | } |
| 553 | return ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 556 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 557 | /* Returns the value of the block <blk>, depending on its type. If there is no |
| 558 | * value, an empty one is retruned. |
| 559 | */ |
| 560 | static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk) |
| 561 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 562 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 563 | struct ist ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 564 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 565 | switch (type) { |
| 566 | case HTX_BLK_HDR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 567 | case HTX_BLK_TLR: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 568 | ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff); |
| 569 | ret.len = (blk->info >> 8) & 0xfffff; |
| 570 | break; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 571 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 572 | case HTX_BLK_REQ_SL: |
| 573 | case HTX_BLK_RES_SL: |
| 574 | case HTX_BLK_DATA: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 575 | ret.ptr = htx_get_blk_ptr(htx, blk); |
| 576 | ret.len = blk->info & 0xfffffff; |
| 577 | break; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 578 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 579 | default: |
| 580 | return ist(""); |
| 581 | } |
| 582 | return ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Willy Tarreau | c01ed9f | 2018-11-30 14:29:31 +0100 | [diff] [blame] | 585 | /* Removes <n> bytes from the beginning of DATA block <blk>. The block's start |
| 586 | * address and its length are adjusted, and the htx's total data count is |
| 587 | * updated. This is used to mark that part of some data were transfered |
| 588 | * from a DATA block without removing this DATA block. No sanity check is |
| 589 | * performed, the caller is reponsible for doing this exclusively on DATA |
| 590 | * blocks, and never removing more than the block's size. |
| 591 | */ |
| 592 | static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n) |
| 593 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 594 | if (blk->addr == htx->end_addr) |
| 595 | htx->end_addr += n; |
Willy Tarreau | c01ed9f | 2018-11-30 14:29:31 +0100 | [diff] [blame] | 596 | blk->addr += n; |
| 597 | blk->info -= n; |
| 598 | htx->data -= n; |
| 599 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 600 | |
| 601 | /* Returns the space used by metadata in <htx>. */ |
| 602 | static inline uint32_t htx_meta_space(const struct htx *htx) |
| 603 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 604 | return (htx->used * sizeof(htx->blocks[0])); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* Returns the space used (data + metadata) in <htx> */ |
| 608 | static inline uint32_t htx_used_space(const struct htx *htx) |
| 609 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 610 | return (htx->data + htx_meta_space(htx)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* Returns the free space in <htx> */ |
| 614 | static inline uint32_t htx_free_space(const struct htx *htx) |
| 615 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 616 | return (htx->size - htx_used_space(htx)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* Returns the maximum size available to store some data in <htx> if a new block |
| 620 | * is reserved. |
| 621 | */ |
| 622 | static inline uint32_t htx_free_data_space(const struct htx *htx) |
| 623 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 624 | uint32_t free = htx_free_space(htx); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 625 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 626 | if (free < sizeof(htx->blocks[0])) |
| 627 | return 0; |
| 628 | return (free - sizeof(htx->blocks[0])); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 629 | } |
| 630 | |
Christopher Faulet | 2ae3504 | 2019-05-16 11:16:39 +0200 | [diff] [blame] | 631 | /* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be |
| 632 | * set to -1 to have no limit. |
| 633 | */ |
| 634 | static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max) |
| 635 | { |
| 636 | uint32_t free = htx_free_space(htx); |
| 637 | |
| 638 | if (max != -1 && free > max) |
| 639 | free = max; |
| 640 | if (free < sizeof(htx->blocks[0])) |
| 641 | return 0; |
| 642 | return (free - sizeof(htx->blocks[0])); |
| 643 | } |
| 644 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 645 | /* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */ |
| 646 | static inline int htx_almost_full(const struct htx *htx) |
| 647 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 648 | if (!htx->size || htx_free_space(htx) < htx->size / 4) |
| 649 | return 1; |
| 650 | return 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static inline void htx_reset(struct htx *htx) |
| 654 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 655 | htx->data = htx->used = htx->tail = htx->head = 0; |
| 656 | htx->tail_addr = htx->head_addr = htx->end_addr = 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 657 | htx->extra = 0; |
| 658 | htx->flags = HTX_FL_NONE; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 659 | htx->first = -1; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Willy Tarreau | 3906e22 | 2018-12-05 07:56:25 +0100 | [diff] [blame] | 662 | /* returns the available room for raw data in buffer <buf> once HTX overhead is |
| 663 | * taken into account (one HTX header and two blocks). The purpose is to figure |
| 664 | * the optimal fill length to avoid copies. |
| 665 | */ |
| 666 | static inline size_t buf_room_for_htx_data(const struct buffer *buf) |
| 667 | { |
| 668 | size_t room; |
| 669 | |
| 670 | room = b_room(buf); |
| 671 | if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk)) |
| 672 | room = 0; |
| 673 | else |
| 674 | room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk); |
| 675 | |
| 676 | return room; |
| 677 | } |
| 678 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 679 | |
| 680 | /* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this |
Willy Tarreau | 245d189 | 2019-01-31 07:21:42 +0100 | [diff] [blame] | 681 | * function does not update to the buffer. |
| 682 | * Note that it always returns a valid pointer, either to an initialized buffer |
| 683 | * or to the empty buffer. |
| 684 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 685 | static inline struct htx *htxbuf(const struct buffer *buf) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 686 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 687 | struct htx *htx; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 688 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 689 | if (b_is_null(buf)) |
| 690 | return &htx_empty; |
| 691 | htx = ((struct htx *)(buf->area)); |
| 692 | if (!b_data(buf)) { |
Willy Tarreau | 8ae4235 | 2018-12-05 09:47:34 +0100 | [diff] [blame] | 693 | htx->size = buf->size - sizeof(*htx); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 694 | htx_reset(htx); |
Willy Tarreau | 8ae4235 | 2018-12-05 09:47:34 +0100 | [diff] [blame] | 695 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 696 | return htx; |
| 697 | } |
| 698 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 699 | /* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as |
| 700 | * full. It is the caller responsibility to call htx_to_buf() when it finish to |
Willy Tarreau | 245d189 | 2019-01-31 07:21:42 +0100 | [diff] [blame] | 701 | * manipulate the HTX message to update <buf> accordingly. The returned pointer |
| 702 | * is always valid. |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 703 | * |
Willy Tarreau | 245d189 | 2019-01-31 07:21:42 +0100 | [diff] [blame] | 704 | * The caller can call htxbuf() function to avoid any update of the buffer. |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 705 | */ |
| 706 | static inline struct htx *htx_from_buf(struct buffer *buf) |
| 707 | { |
| 708 | struct htx *htx = htxbuf(buf); |
| 709 | |
| 710 | b_set_data(buf, b_size(buf)); |
| 711 | return htx; |
| 712 | } |
| 713 | |
| 714 | /* Upate <buf> accordingly to the HTX message <htx> */ |
| 715 | static inline void htx_to_buf(struct htx *htx, struct buffer *buf) |
| 716 | { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 717 | if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 718 | htx_reset(htx); |
| 719 | b_set_data(buf, 0); |
| 720 | } |
| 721 | else |
| 722 | b_set_data(buf, b_size(buf)); |
| 723 | } |
| 724 | |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 725 | /* Returns 1 if the message is empty, otherwise it returns 0. Note that it is |
| 726 | * illegal to call this with htx == NULL. |
| 727 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 728 | static inline int htx_is_empty(const struct htx *htx) |
| 729 | { |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 730 | return !htx->used; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 733 | /* Returns 1 if the message is not empty, otherwise it returns 0. Note that it |
| 734 | * is illegal to call this with htx == NULL. |
| 735 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 736 | static inline int htx_is_not_empty(const struct htx *htx) |
| 737 | { |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 738 | return htx->used; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* For debugging purpose */ |
| 742 | static inline const char *htx_blk_type_str(enum htx_blk_type type) |
| 743 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 744 | switch (type) { |
| 745 | case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL"; |
| 746 | case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL"; |
| 747 | case HTX_BLK_HDR: return "HTX_BLK_HDR"; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 748 | case HTX_BLK_EOH: return "HTX_BLK_EOH"; |
| 749 | case HTX_BLK_DATA: return "HTX_BLK_DATA"; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 750 | case HTX_BLK_TLR: return "HTX_BLK_TLR"; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 751 | case HTX_BLK_EOT: return "HTX_BLK_EOT"; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 752 | case HTX_BLK_EOM: return "HTX_BLK_EOM"; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 753 | case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED"; |
| 754 | default: return "HTX_BLK_???"; |
| 755 | }; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 758 | static inline void htx_dump(struct htx *htx) |
| 759 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 760 | int32_t pos; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 761 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 762 | fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n", |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 763 | htx, htx->size, htx->data, htx->used, (!htx->head_addr) ? "NO" : "YES", |
Willy Tarreau | a7280a1 | 2018-11-26 19:41:40 +0100 | [diff] [blame] | 764 | (unsigned long long)htx->extra); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 765 | fprintf(stderr, "\tfirst=%d - head=%u, tail=%u\n", |
| 766 | htx->first, htx->head, htx->tail); |
| 767 | fprintf(stderr, "\ttail_addr=%d - head_addr=%u, end_addr=%u\n", |
| 768 | htx->tail_addr, htx->head_addr, htx->end_addr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 769 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 770 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 771 | struct htx_sl *sl; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 772 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 773 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 774 | uint32_t sz = htx_get_blksz(blk); |
| 775 | struct ist n, v; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 776 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 777 | n = htx_get_blk_name(htx, blk); |
| 778 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 779 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 780 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 781 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 782 | fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n", |
| 783 | pos, htx_blk_type_str(type), sz, blk->addr, |
| 784 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 785 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 786 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 787 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 788 | else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 789 | fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n", |
| 790 | pos, htx_blk_type_str(type), sz, blk->addr, |
| 791 | (int)n.len, n.ptr, |
| 792 | (int)v.len, v.ptr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 793 | else |
| 794 | fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n", |
| 795 | pos, htx_blk_type_str(type), sz, blk->addr, |
| 796 | (!v.len ? "\t<empty>" : "")); |
| 797 | } |
| 798 | fprintf(stderr, "\n"); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 801 | #endif /* _COMMON_HTX_H */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 802 | |
| 803 | /* |
| 804 | * Local variables: |
| 805 | * c-indent-level: 8 |
| 806 | * c-basic-offset: 8 |
| 807 | * End: |
| 808 | */ |