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) */ |
| 90 | |
| 91 | /* HTX flags */ |
| 92 | #define HTX_FL_NONE 0x00000000 |
| 93 | #define HTX_FL_PARSING_ERROR 0x00000001 |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 94 | #define HTX_FL_UPGRADE 0x00000002 |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 95 | |
| 96 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 97 | /* HTTP block's type (max 15). */ |
| 98 | enum htx_blk_type { |
| 99 | HTX_BLK_REQ_SL = 0, /* Request start-line */ |
| 100 | HTX_BLK_RES_SL = 1, /* Response start-line */ |
| 101 | HTX_BLK_HDR = 2, /* header name/value block */ |
Christopher Faulet | 39744f7 | 2019-05-24 14:54:00 +0200 | [diff] [blame] | 102 | HTX_BLK_EOH = 3, /* end-of-headers block */ |
| 103 | HTX_BLK_DATA = 4, /* data block */ |
| 104 | HTX_BLK_EOD = 5, /* end-of-data block */ |
| 105 | HTX_BLK_TLR = 6, /* trailer name/value block */ |
| 106 | HTX_BLK_EOM = 7, /* end-of-message block */ |
| 107 | /* 8 .. 14 unused */ |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 108 | HTX_BLK_UNUSED = 15, /* unused/removed block */ |
| 109 | }; |
| 110 | |
| 111 | /* One HTTP block descriptor */ |
| 112 | struct htx_blk { |
| 113 | uint32_t addr; /* relative storage address of a data block */ |
| 114 | uint32_t info; /* information about data stored */ |
| 115 | }; |
| 116 | |
| 117 | struct htx_ret { |
| 118 | int32_t ret; |
| 119 | struct htx_blk *blk; |
| 120 | }; |
| 121 | |
| 122 | struct htx_sl { |
| 123 | unsigned int flags; /* HTX_SL_F_* */ |
| 124 | union { |
| 125 | struct { |
| 126 | enum http_meth_t meth; /* method */ |
| 127 | } req; |
| 128 | struct { |
| 129 | uint16_t status; /* status code */ |
| 130 | } res; |
| 131 | } info; |
| 132 | |
| 133 | /* XXX 2 bytes unused */ |
| 134 | |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 135 | int32_t hdrs_bytes; /* Bytes held by all headers from this start-line |
| 136 | * to the corresponding EOH. -1 if unknown */ |
| 137 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 138 | unsigned int len[3]; /* length of differnt parts of the start-line */ |
| 139 | char l[0]; |
| 140 | }; |
| 141 | |
| 142 | /* Internal representation of an HTTP message */ |
| 143 | struct htx { |
| 144 | uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */ |
| 145 | uint32_t data; /* the data size, in bytes. To known to total size used by all allocated |
| 146 | * blocks (blocks and their contents), you need to add size used by blocks, |
| 147 | * i.e. [ used * sizeof(struct htx_blk *) ] */ |
| 148 | |
| 149 | uint32_t used; /* number of blocks in use */ |
| 150 | uint32_t tail; /* last inserted block */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 151 | uint32_t head; /* older inserted block */ |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 152 | uint32_t front; /* block's position of the first content before the blocks table */ |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 153 | |
| 154 | uint64_t extra; /* known bytes amount remaining to receive */ |
| 155 | uint32_t flags; /* HTX_FL_* */ |
| 156 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 157 | 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] | 158 | |
| 159 | struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */ |
| 160 | }; |
| 161 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 162 | |
| 163 | extern struct htx htx_empty; |
| 164 | |
| 165 | struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk); |
| 166 | struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz); |
| 167 | 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] | 168 | void htx_truncate(struct htx *htx, uint32_t offset); |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 169 | struct htx_ret htx_drain(struct htx *htx, uint32_t max); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 170 | |
| 171 | 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] | 172 | const struct ist old, const struct ist new); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 173 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 174 | enum htx_blk_type mark); |
| 175 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 176 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 177 | const struct ist p1, const struct ist p2, const struct ist p3); |
| 178 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 179 | const struct ist p2, const struct ist p3); |
| 180 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 181 | 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] | 182 | const struct ist name, const struct ist value); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 183 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 184 | struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value); |
Willy Tarreau | 52610e9 | 2019-01-03 18:26:17 +0100 | [diff] [blame] | 185 | 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] | 186 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 187 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type); |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 188 | struct htx_blk *htx_add_data_atonce(struct htx *htx, const struct ist data); |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 189 | size_t htx_add_data(struct htx *htx, const struct ist data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 190 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 191 | struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, const struct ist data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 192 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 193 | int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk); |
| 194 | int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk); |
| 195 | int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk); |
| 196 | int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked); |
| 197 | int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 198 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 199 | /* Functions and macros to get parts of the start-line or legnth of these |
| 200 | * parts |
| 201 | */ |
| 202 | #define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2]) |
| 203 | |
| 204 | #define HTX_SL_P1_LEN(sl) ((sl)->len[0]) |
| 205 | #define HTX_SL_P2_LEN(sl) ((sl)->len[1]) |
| 206 | #define HTX_SL_P3_LEN(sl) ((sl)->len[2]) |
| 207 | #define HTX_SL_P1_PTR(sl) ((sl)->l) |
| 208 | #define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl)) |
| 209 | #define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl)) |
| 210 | |
| 211 | #define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl) |
| 212 | #define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl) |
| 213 | #define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl) |
| 214 | #define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl) |
| 215 | #define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl) |
| 216 | #define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl) |
| 217 | |
| 218 | #define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl) |
| 219 | #define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl) |
| 220 | #define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl) |
| 221 | #define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl) |
| 222 | #define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl) |
| 223 | #define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl) |
| 224 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 225 | static inline struct ist htx_sl_p1(const struct htx_sl *sl) |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 226 | { |
| 227 | return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl)); |
| 228 | } |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 229 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 230 | static inline struct ist htx_sl_p2(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 231 | { |
| 232 | return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl)); |
| 233 | } |
| 234 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 235 | static inline struct ist htx_sl_p3(const struct htx_sl *sl) |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 236 | { |
| 237 | return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl)); |
| 238 | } |
| 239 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 240 | 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] | 241 | { |
| 242 | return htx_sl_p1(sl); |
| 243 | } |
| 244 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 245 | 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] | 246 | { |
| 247 | return htx_sl_p2(sl); |
| 248 | } |
| 249 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 250 | 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] | 251 | { |
| 252 | return htx_sl_p3(sl); |
| 253 | } |
| 254 | |
| 255 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | return htx_sl_p1(sl); |
| 259 | } |
| 260 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 261 | 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] | 262 | { |
| 263 | return htx_sl_p2(sl); |
| 264 | } |
| 265 | |
Willy Tarreau | 8de1df9 | 2019-04-15 21:27:18 +0200 | [diff] [blame] | 266 | 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] | 267 | { |
| 268 | return htx_sl_p3(sl); |
| 269 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 270 | |
| 271 | /* Returns the array index of a block given its position <pos> */ |
| 272 | static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos) |
| 273 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 274 | return ((htx->size / sizeof(htx->blocks[0])) - pos - 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* Returns the position of the block <blk> */ |
| 278 | static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk) |
| 279 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 280 | return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* Returns the block at the position <pos> */ |
| 284 | static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos) |
| 285 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 286 | return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* Returns the type of the block <blk> */ |
| 290 | static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk) |
| 291 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 292 | return (blk->info >> 28); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 295 | /* Returns the size of the block <blk>, depending of its type */ |
| 296 | static inline uint32_t htx_get_blksz(const struct htx_blk *blk) |
| 297 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 298 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 299 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 300 | switch (type) { |
| 301 | case HTX_BLK_HDR: |
| 302 | /* name.length + value.length */ |
| 303 | return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff)); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 304 | default: |
| 305 | /* value.length */ |
| 306 | return (blk->info & 0xfffffff); |
| 307 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 310 | /* Returns the wrap position, ie the position where the blocks table wraps. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 311 | * |
| 312 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 313 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 314 | * blocks to overflow a 32-bits signed integer ! |
| 315 | */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 316 | static inline int32_t htx_get_wrap(const struct htx *htx) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 317 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 318 | if (!htx->used) |
| 319 | return -1; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 320 | return ((htx->tail >= htx->head) |
| 321 | ? (htx->used + htx->head) |
| 322 | : (htx->used - 1) + (htx->head - htx->tail)); |
| 323 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 324 | |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 325 | /* Returns the position of the oldest entry (head). |
| 326 | * |
| 327 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 328 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 329 | * blocks to overflow a 32-bits signed integer ! |
| 330 | */ |
| 331 | static inline int32_t htx_get_head(const struct htx *htx) |
| 332 | { |
| 333 | return (htx->used ? htx->head : -1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Returns the oldest HTX block (head) if the HTX message is not |
| 337 | * empty. Otherwise it returns NULL. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 338 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 339 | static inline struct htx_blk *htx_get_head_blk(const struct htx *htx) |
| 340 | { |
| 341 | int32_t head = htx_get_head(htx); |
| 342 | |
| 343 | return ((head == -1) ? NULL : htx_get_blk(htx, head)); |
| 344 | } |
| 345 | |
| 346 | /* Returns the type of the oldest HTX block (head) if the HTX message is not |
| 347 | * empty. Otherwise it returns HTX_BLK_UNUSED. |
| 348 | */ |
| 349 | static inline enum htx_blk_type htx_get_head_type(const struct htx *htx) |
| 350 | { |
| 351 | struct htx_blk *blk = htx_get_head_blk(htx); |
| 352 | |
| 353 | return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED); |
| 354 | } |
| 355 | |
| 356 | /* Returns the position of the newest entry (tail). |
| 357 | * |
| 358 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 359 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 360 | * blocks to overflow a 32-bits signed integer ! |
| 361 | */ |
| 362 | static inline int32_t htx_get_tail(const struct htx *htx) |
| 363 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 364 | return (htx->used ? htx->tail : -1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* Returns the newest HTX block (tail) if the HTX message is not |
| 368 | * empty. Otherwise it returns NULL. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 369 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 370 | static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx) |
| 371 | { |
| 372 | int32_t tail = htx_get_tail(htx); |
| 373 | |
| 374 | return ((tail == -1) ? NULL : htx_get_blk(htx, tail)); |
| 375 | } |
| 376 | |
| 377 | /* Returns the type of the newest HTX block (tail) if the HTX message is not |
| 378 | * empty. Otherwise it returns HTX_BLK_UNUSED. |
| 379 | */ |
| 380 | static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx) |
| 381 | { |
| 382 | struct htx_blk *blk = htx_get_tail_blk(htx); |
| 383 | |
| 384 | return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED); |
| 385 | } |
| 386 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 387 | /* Returns the position of the first block in the HTX message <htx>. If unset, |
| 388 | * or if <htx> is empty, -1 is returned. |
Christopher Faulet | a3ad6b1 | 2019-05-13 11:36:27 +0200 | [diff] [blame] | 389 | * |
| 390 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 391 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 392 | * blocks to overflow a 32-bits signed integer ! |
| 393 | */ |
| 394 | static inline int32_t htx_get_first(const struct htx *htx) |
| 395 | { |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 396 | if (!htx->used) |
| 397 | return -1; |
| 398 | return htx->first; |
Christopher Faulet | a3ad6b1 | 2019-05-13 11:36:27 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 401 | /* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is |
| 402 | * empty, NULL returned. |
Christopher Faulet | a3ad6b1 | 2019-05-13 11:36:27 +0200 | [diff] [blame] | 403 | */ |
| 404 | static inline struct htx_blk *htx_get_first_blk(const struct htx *htx) |
| 405 | { |
| 406 | int32_t pos; |
| 407 | |
| 408 | pos = htx_get_first(htx); |
| 409 | return ((pos == -1) ? NULL : htx_get_blk(htx, pos)); |
| 410 | } |
| 411 | |
| 412 | /* Returns the type of the first block in the HTX message <htx>. If unset or if |
| 413 | * <htx> is empty, HTX_BLK_UNUSED is returned. |
| 414 | */ |
| 415 | static inline enum htx_blk_type htx_get_first_type(const struct htx *htx) |
| 416 | { |
| 417 | struct htx_blk *blk = htx_get_first_blk(htx); |
| 418 | |
| 419 | return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED); |
| 420 | } |
| 421 | |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 422 | /* 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] | 423 | * the message is empty or if <pos> is the position of the head, -1 returned. |
| 424 | * |
| 425 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 426 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 427 | * blocks to overflow a 32-bits signed integer ! |
| 428 | */ |
| 429 | static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos) |
| 430 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 431 | int32_t head; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 432 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 433 | head = htx_get_head(htx); |
| 434 | if (head == -1 || pos == head) |
| 435 | return -1; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 436 | if (!pos) { |
| 437 | /* htx_get_wrap() is always greater than 1 here */ |
| 438 | return (htx_get_wrap(htx) - 1); |
| 439 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 440 | return (pos - 1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 443 | /* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the |
| 444 | * head, NULL returned. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 445 | */ |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 446 | static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx, |
| 447 | const struct htx_blk *blk) |
| 448 | { |
| 449 | int32_t pos; |
| 450 | |
| 451 | pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk)); |
| 452 | return ((pos == -1) ? NULL : htx_get_blk(htx, pos)); |
| 453 | } |
| 454 | |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 455 | /* 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] | 456 | * the message is empty or if <pos> is the position of the tail, -1 returned. |
| 457 | * |
| 458 | * An signed 32-bits integer is returned to handle -1 case. Blocks position are |
| 459 | * store on unsigned 32-bits integer, but it is impossible to have so much |
| 460 | * blocks to overflow a 32-bits signed integer ! |
| 461 | */ |
| 462 | static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos) |
| 463 | { |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 464 | if (!htx->used || pos == htx->tail) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 465 | return -1; |
| 466 | pos++; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 467 | if (pos == htx_get_wrap(htx)) |
| 468 | return 0; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 469 | return pos; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 470 | |
| 471 | } |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 472 | |
| 473 | /* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the |
| 474 | * tail, NULL returned. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 475 | */ |
Christopher Faulet | d16b0a7 | 2018-11-22 11:23:23 +0100 | [diff] [blame] | 476 | static inline struct htx_blk *htx_get_next_blk(const struct htx *htx, |
| 477 | const struct htx_blk *blk) |
| 478 | { |
| 479 | int32_t pos; |
| 480 | |
| 481 | pos = htx_get_next(htx, htx_get_blk_pos(htx, blk)); |
| 482 | return ((pos == -1) ? NULL : htx_get_blk(htx, pos)); |
| 483 | } |
| 484 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 485 | static inline int32_t htx_find_front(const struct htx *htx) |
| 486 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 487 | int32_t front, pos; |
| 488 | uint32_t addr = 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 489 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 490 | if (!htx->used) |
| 491 | return -1; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 492 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 493 | front = -1; |
| 494 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 495 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 496 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 497 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 498 | if (type != HTX_BLK_UNUSED && blk->addr >= addr) { |
| 499 | front = pos; |
| 500 | addr = blk->addr; |
| 501 | } |
| 502 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 503 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 504 | return front; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* Changes the size of the value. It is the caller responsibility to change the |
| 508 | * value itself, make sure there is enough space and update allocated value. |
| 509 | */ |
| 510 | static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen) |
| 511 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 512 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 513 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 514 | switch (type) { |
| 515 | case HTX_BLK_HDR: |
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: |
| 521 | case HTX_BLK_TLR: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 522 | blk->info = (type << 28) + vlen; |
| 523 | break; |
| 524 | default: |
| 525 | /* Unexpected case */ |
| 526 | break; |
| 527 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* Returns the data pointer of the block <blk> */ |
| 531 | static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk) |
| 532 | { |
| 533 | return ((void *)htx->blocks + blk->addr); |
| 534 | } |
| 535 | |
| 536 | /* Returns the name of the block <blk>, only if it is a header. Otherwise it |
| 537 | * returns an empty name. |
| 538 | */ |
| 539 | static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk) |
| 540 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 541 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 542 | struct ist ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 543 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 544 | switch (type) { |
| 545 | case HTX_BLK_HDR: |
| 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: |
| 567 | ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff); |
| 568 | ret.len = (blk->info >> 8) & 0xfffff; |
| 569 | break; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 570 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 571 | case HTX_BLK_REQ_SL: |
| 572 | case HTX_BLK_RES_SL: |
| 573 | case HTX_BLK_DATA: |
| 574 | case HTX_BLK_TLR: |
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 | { |
| 594 | blk->addr += n; |
| 595 | blk->info -= n; |
| 596 | htx->data -= n; |
| 597 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 598 | |
| 599 | /* Returns the space used by metadata in <htx>. */ |
| 600 | static inline uint32_t htx_meta_space(const struct htx *htx) |
| 601 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 602 | return (htx->used * sizeof(htx->blocks[0])); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* Returns the space used (data + metadata) in <htx> */ |
| 606 | static inline uint32_t htx_used_space(const struct htx *htx) |
| 607 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 608 | return (htx->data + htx_meta_space(htx)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* Returns the free space in <htx> */ |
| 612 | static inline uint32_t htx_free_space(const struct htx *htx) |
| 613 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 614 | return (htx->size - htx_used_space(htx)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Christopher Faulet | 8564c1f | 2019-01-07 13:48:55 +0100 | [diff] [blame] | 617 | /* Returns the maximum space usable for data in <htx>. This is in fact the |
| 618 | * maximum sice for a uniq block to fill the HTX message. */ |
| 619 | static inline uint32_t htx_max_data_space(const struct htx *htx) |
| 620 | { |
| 621 | if (!htx->size) |
| 622 | return 0; |
| 623 | return (htx->size - sizeof(htx->blocks[0])); |
| 624 | } |
| 625 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 626 | /* Returns the maximum size available to store some data in <htx> if a new block |
| 627 | * is reserved. |
| 628 | */ |
| 629 | static inline uint32_t htx_free_data_space(const struct htx *htx) |
| 630 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 631 | uint32_t free = htx_free_space(htx); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 632 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 633 | if (free < sizeof(htx->blocks[0])) |
| 634 | return 0; |
| 635 | return (free - sizeof(htx->blocks[0])); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Christopher Faulet | 2ae3504 | 2019-05-16 11:16:39 +0200 | [diff] [blame] | 638 | /* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be |
| 639 | * set to -1 to have no limit. |
| 640 | */ |
| 641 | static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max) |
| 642 | { |
| 643 | uint32_t free = htx_free_space(htx); |
| 644 | |
| 645 | if (max != -1 && free > max) |
| 646 | free = max; |
| 647 | if (free < sizeof(htx->blocks[0])) |
| 648 | return 0; |
| 649 | return (free - sizeof(htx->blocks[0])); |
| 650 | } |
| 651 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 652 | /* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */ |
| 653 | static inline int htx_almost_full(const struct htx *htx) |
| 654 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 655 | if (!htx->size || htx_free_space(htx) < htx->size / 4) |
| 656 | return 1; |
| 657 | return 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static inline void htx_reset(struct htx *htx) |
| 661 | { |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 662 | htx->data = htx->used = htx->tail = htx->head = htx->front = 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 663 | htx->extra = 0; |
| 664 | htx->flags = HTX_FL_NONE; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 665 | htx->first = -1; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Willy Tarreau | 3906e22 | 2018-12-05 07:56:25 +0100 | [diff] [blame] | 668 | /* returns the available room for raw data in buffer <buf> once HTX overhead is |
| 669 | * taken into account (one HTX header and two blocks). The purpose is to figure |
| 670 | * the optimal fill length to avoid copies. |
| 671 | */ |
| 672 | static inline size_t buf_room_for_htx_data(const struct buffer *buf) |
| 673 | { |
| 674 | size_t room; |
| 675 | |
| 676 | room = b_room(buf); |
| 677 | if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk)) |
| 678 | room = 0; |
| 679 | else |
| 680 | room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk); |
| 681 | |
| 682 | return room; |
| 683 | } |
| 684 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 685 | |
| 686 | /* 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] | 687 | * function does not update to the buffer. |
| 688 | * Note that it always returns a valid pointer, either to an initialized buffer |
| 689 | * or to the empty buffer. |
| 690 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 691 | static inline struct htx *htxbuf(const struct buffer *buf) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 692 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 693 | struct htx *htx; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 694 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 695 | if (b_is_null(buf)) |
| 696 | return &htx_empty; |
| 697 | htx = ((struct htx *)(buf->area)); |
| 698 | if (!b_data(buf)) { |
Willy Tarreau | 8ae4235 | 2018-12-05 09:47:34 +0100 | [diff] [blame] | 699 | htx->size = buf->size - sizeof(*htx); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 700 | htx_reset(htx); |
Willy Tarreau | 8ae4235 | 2018-12-05 09:47:34 +0100 | [diff] [blame] | 701 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 702 | return htx; |
| 703 | } |
| 704 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 705 | /* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as |
| 706 | * 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] | 707 | * manipulate the HTX message to update <buf> accordingly. The returned pointer |
| 708 | * is always valid. |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 709 | * |
Willy Tarreau | 245d189 | 2019-01-31 07:21:42 +0100 | [diff] [blame] | 710 | * 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] | 711 | */ |
| 712 | static inline struct htx *htx_from_buf(struct buffer *buf) |
| 713 | { |
| 714 | struct htx *htx = htxbuf(buf); |
| 715 | |
| 716 | b_set_data(buf, b_size(buf)); |
| 717 | return htx; |
| 718 | } |
| 719 | |
| 720 | /* Upate <buf> accordingly to the HTX message <htx> */ |
| 721 | static inline void htx_to_buf(struct htx *htx, struct buffer *buf) |
| 722 | { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 723 | if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 724 | htx_reset(htx); |
| 725 | b_set_data(buf, 0); |
| 726 | } |
| 727 | else |
| 728 | b_set_data(buf, b_size(buf)); |
| 729 | } |
| 730 | |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 731 | /* Returns 1 if the message is empty, otherwise it returns 0. Note that it is |
| 732 | * illegal to call this with htx == NULL. |
| 733 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 734 | static inline int htx_is_empty(const struct htx *htx) |
| 735 | { |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 736 | return !htx->used; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 739 | /* Returns 1 if the message is not empty, otherwise it returns 0. Note that it |
| 740 | * is illegal to call this with htx == NULL. |
| 741 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 742 | static inline int htx_is_not_empty(const struct htx *htx) |
| 743 | { |
Willy Tarreau | e5fcfbe | 2019-01-31 07:31:18 +0100 | [diff] [blame] | 744 | return htx->used; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* For debugging purpose */ |
| 748 | static inline const char *htx_blk_type_str(enum htx_blk_type type) |
| 749 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 750 | switch (type) { |
| 751 | case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL"; |
| 752 | case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL"; |
| 753 | case HTX_BLK_HDR: return "HTX_BLK_HDR"; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 754 | case HTX_BLK_EOH: return "HTX_BLK_EOH"; |
| 755 | case HTX_BLK_DATA: return "HTX_BLK_DATA"; |
| 756 | case HTX_BLK_EOD: return "HTX_BLK_EOD"; |
| 757 | case HTX_BLK_TLR: return "HTX_BLK_TLR"; |
| 758 | case HTX_BLK_EOM: return "HTX_BLK_EOM"; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 759 | case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED"; |
| 760 | default: return "HTX_BLK_???"; |
| 761 | }; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 764 | static inline void htx_dump(struct htx *htx) |
| 765 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 766 | int32_t pos; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 767 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 768 | fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n", |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 769 | htx, htx->size, htx->data, htx->used, (htx->tail >= htx->head) ? "NO" : "YES", |
Willy Tarreau | a7280a1 | 2018-11-26 19:41:40 +0100 | [diff] [blame] | 770 | (unsigned long long)htx->extra); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 771 | fprintf(stderr, "\tfirst=%d - head=%u, tail=%u - front=%u\n", |
| 772 | htx->first, htx->head, htx->tail, htx->front); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 773 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 774 | 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] | 775 | struct htx_sl *sl; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 776 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 777 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 778 | uint32_t sz = htx_get_blksz(blk); |
| 779 | struct ist n, v; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 780 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 781 | n = htx_get_blk_name(htx, blk); |
| 782 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 783 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 784 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 785 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 786 | fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n", |
| 787 | pos, htx_blk_type_str(type), sz, blk->addr, |
| 788 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 789 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 790 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 791 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 792 | else if (type == HTX_BLK_HDR) |
| 793 | fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n", |
| 794 | pos, htx_blk_type_str(type), sz, blk->addr, |
| 795 | (int)n.len, n.ptr, |
| 796 | (int)v.len, v.ptr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 797 | else |
| 798 | fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n", |
| 799 | pos, htx_blk_type_str(type), sz, blk->addr, |
| 800 | (!v.len ? "\t<empty>" : "")); |
| 801 | } |
| 802 | fprintf(stderr, "\n"); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 805 | #endif /* _COMMON_HTX_H */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * Local variables: |
| 809 | * c-indent-level: 8 |
| 810 | * c-basic-offset: 8 |
| 811 | * End: |
| 812 | */ |