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