blob: 5527a473fcab5d33de75d7da498a9bc03b17aa11 [file] [log] [blame]
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001/*
Willy Tarreaub96b77e2018-12-11 10:22:41 +01002 * include/common/htx.h
Christopher Fauleta3d2a162018-10-22 08:59:39 +02003 * 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 Tarreaub96b77e2018-12-11 10:22:41 +010022#ifndef _COMMON_HTX_H
23#define _COMMON_HTX_H
Christopher Fauleta3d2a162018-10-22 08:59:39 +020024
Willy Tarreaub96b77e2018-12-11 10:22:41 +010025#include <stdio.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020026#include <common/buf.h>
27#include <common/config.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010028#include <common/ist.h>
29#include <common/http.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020030#include <common/http-hdr.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010031#include <common/standard.h>
32
33/*
Christopher Faulet3b219722019-06-19 13:48:09 +020034 * The internal representation of an HTTP message, called HTX, is a structure
35 * with useful information on the message followed by a contiguous array
36 * containing parts of the message, called blocks. A block is composed of
37 * metadata (htx_blk) and the associated payload. Blocks' metadata are stored
38 * starting from the end of the array while their payload are stored at the
39 * beginning. Blocks' metadata are often simply called blocks. it is a misuse of
40 * language that's simplify explainations.
Willy Tarreaub96b77e2018-12-11 10:22:41 +010041 *
Willy Tarreaub96b77e2018-12-11 10:22:41 +010042 *
Christopher Faulet3b219722019-06-19 13:48:09 +020043 * +-----+---------------+------------------------------+--------------+
44 * | HTX | PAYLOADS ==> | | <== HTX_BLKs |
45 * +-----+---------------+------------------------------+--------------+
46 * ^
47 * blocks[] (the beginning of the bocks array)
48 *
49 *
50 * The blocks part remains linear and sorted. You may think about it as an array
51 * with negative indexes. But, instead of using negative indexes, we use
52 * positive positions to identify a block. This position is then converted to a
53 * address relatively to the beginning of the blocks array.
54 *
55 *
56 * .....--+------------------------------+-----+-----+
57 * | ... | BLK | BLK |
58 * .....--+------------------------------+-----+-----+
59 * ^ ^
60 * Addr of the block Addr of the block
61 * at the position 1 at the position 0
62 *
63 *
64 * The payloads part is a raw space that may wrap. You never access to a block's
65 * payload directly. Instead you get a block to retrieve the address of its
66 * payload. When no more space left between blocks and payloads parts, the free
67 * space at the beginning, if any, is used.
68 *
69 *
70 * +----------- WRAPPING ------------------------+
71 * | |
72 * V |
73 * +-----+-------------+---------------+---------------++--------------+
74 * | HTX | PAYLOAD ==> | | PAYLOADS ==X || X== HTX_BLKs |
75 * +-----+-------------+---------------+---------------++--------------+
76 *
77 *
78 * The blocks part, on its side, never wrap. If we have no space to allocate a
79 * new block and if there is a hole at the beginning of the blocks part (so at
80 * the end of the blocks array), we move back all blocks.x
81 *
82 *
83 * ...+--------------+----------+ blocks ...+----------+--------------+
84 * | X== HTX_BLKS | | defrag | | <== HTX_BLKS |
85 * ...+--------------+----------+ =====> ...+----------+--------------+
86 *
87 *
88 * At the end, if payload wrapping or blocks defragmenation is not enough, some
89 * free space may be get back with a full defragmenation. This way, the holes in
90 * the middle are not reusable but count in the available free space. The only
91 * way to reuse this lost space is to fully defragmenate the HTX message.
92 *
93 * - * -
94 *
95 * An HTX block is as well a header as a body part or a trailer. For all these
96 * types of block, a payload is attached to the block. It can also be a mark,
97 * like the end-of-headers or end-of-message. For these blocks, there is no
98 * payload but it count for a byte. It is important to not skip it when data are
99 * forwarded. Metadata of an HTX block are composed of 2 fields :
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100100 *
101 * - .info : It a 32 bits field containing the block's type on 4 bits
Christopher Faulet3b219722019-06-19 13:48:09 +0200102 * followed by the payload length. See below for details.
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100103 *
Christopher Faulet3b219722019-06-19 13:48:09 +0200104 * - .addr : The payload's address, if any, relatively to the beginning the
105 * array used to store the HTX message itself.
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100106 *
Christopher Faulet3b219722019-06-19 13:48:09 +0200107 * htx_blk.info representation :
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100108 *
109 * 0b 0000 0000 0000 0000 0000 0000 0000 0000
110 * ---- ------------------------ ---------
Christopher Faulet3b219722019-06-19 13:48:09 +0200111 * type value (1 MB max) name length (header/trailer)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100112 * ----------------------------------
113 * data length (256 MB max)
Christopher Faulet3b219722019-06-19 13:48:09 +0200114 * (body, method, path, version, status, reason)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100115 *
Christopher Faulet3b219722019-06-19 13:48:09 +0200116 * types :
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100117 * - 0000 = request start-line
118 * - 0001 = response start-line
119 * - 0010 = header
120 * - 0011 = pseudo-header ou "special" header
121 * - 0100 = end-of-headers
122 * - 0101 = data
Christopher Faulet3b219722019-06-19 13:48:09 +0200123 * - 0110 = trailer
124 * - 0111 = end-of-trailers
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100125 * - 1000 = end-of-message
126 * ...
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100127 * - 1111 = unused
128 *
129 */
130
Christopher Faulet3b219722019-06-19 13:48:09 +0200131/* HTX start-line flags */
Christopher Faulet9a67c292019-10-08 14:27:52 +0200132#define HTX_SL_F_NONE 0x00000000
133#define HTX_SL_F_IS_RESP 0x00000001 /* It is the response start-line (unset means the request one) */
134#define HTX_SL_F_XFER_LEN 0x00000002 /* The message xfer size can be dertermined */
135#define HTX_SL_F_XFER_ENC 0x00000004 /* The transfer-encoding header was found in message */
136#define HTX_SL_F_CLEN 0x00000008 /* The content-length header was found in message */
137#define HTX_SL_F_CHNK 0x00000010 /* The message payload is chunked */
138#define HTX_SL_F_VER_11 0x00000020 /* The message indicates version 1.1 or above */
139#define HTX_SL_F_BODYLESS 0x00000040 /* The message has no body (content-length = 0) */
140#define HTX_SL_F_HAS_SCHM 0x00000080 /* The scheme is explicitly specified */
141#define HTX_SL_F_SCHM_HTTP 0x00000100 /* The scheme HTTP should be used */
142#define HTX_SL_F_SCHM_HTTPS 0x00000200 /* The scheme HTTPS should be used */
143#define HTX_SL_F_HAS_AUTHORITY 0x00000400 /* The request authority is explicitly specified */
144#define HTX_SL_F_NORMALIZED_URI 0x00000800 /* The received URI is normalized (an implicit absolute-uri form) */
145
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100146
147/* HTX flags */
148#define HTX_FL_NONE 0x00000000
Christopher Faulet3b219722019-06-19 13:48:09 +0200149#define HTX_FL_PARSING_ERROR 0x00000001 /* Set when a parsing error occurred */
Christopher Faulet505adfc2019-09-06 19:08:27 +0200150#define HTX_FL_PROCESSING_ERROR 0x00000002 /* Set when a processing error occurred */
151#define HTX_FL_UPGRADE 0x00000004 /* Set when an upgrade is in progress */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100152
153
Christopher Faulet3b219722019-06-19 13:48:09 +0200154/* HTX block's type (max 15). */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100155enum htx_blk_type {
156 HTX_BLK_REQ_SL = 0, /* Request start-line */
157 HTX_BLK_RES_SL = 1, /* Response start-line */
158 HTX_BLK_HDR = 2, /* header name/value block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200159 HTX_BLK_EOH = 3, /* end-of-headers block */
160 HTX_BLK_DATA = 4, /* data block */
Christopher Faulet54b5e212019-06-04 10:08:28 +0200161 HTX_BLK_TLR = 5, /* trailer name/value block */
162 HTX_BLK_EOT = 6, /* end-of-trailers block */
163 HTX_BLK_EOM = 7, /* end-of-message block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200164 /* 8 .. 14 unused */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100165 HTX_BLK_UNUSED = 15, /* unused/removed block */
166};
167
Christopher Faulet3b219722019-06-19 13:48:09 +0200168/* One HTX block descriptor */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100169struct htx_blk {
Christopher Faulet3b219722019-06-19 13:48:09 +0200170 uint32_t addr; /* relative storage address of the block's payload */
171 uint32_t info; /* information about the block (type, length) */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100172};
173
Christopher Faulet3b219722019-06-19 13:48:09 +0200174/* Composite return value used by some HTX functions */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100175struct htx_ret {
Christopher Faulet3b219722019-06-19 13:48:09 +0200176 int32_t ret; /* A numerical value */
177 struct htx_blk *blk; /* An HTX block */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100178};
179
Christopher Faulet3b219722019-06-19 13:48:09 +0200180/* HTX start-line */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100181struct htx_sl {
182 unsigned int flags; /* HTX_SL_F_* */
183 union {
184 struct {
185 enum http_meth_t meth; /* method */
186 } req;
187 struct {
188 uint16_t status; /* status code */
189 } res;
190 } info;
191
192 /* XXX 2 bytes unused */
193
Christopher Faulet3b219722019-06-19 13:48:09 +0200194 int32_t hdrs_bytes; /* Bytes held by all headers, as seen by the mux
195 * during parsing, from this start-line to the
196 * corresponding EOH. -1 if unknown */
Christopher Faulet05c083c2019-05-15 14:56:47 +0200197
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100198 unsigned int len[3]; /* length of differnt parts of the start-line */
199 char l[0];
200};
201
202/* Internal representation of an HTTP message */
203struct htx {
204 uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */
205 uint32_t data; /* the data size, in bytes. To known to total size used by all allocated
206 * blocks (blocks and their contents), you need to add size used by blocks,
207 * i.e. [ used * sizeof(struct htx_blk *) ] */
208
Christopher Faulet192c6a22019-06-11 16:32:24 +0200209 int32_t tail; /* newest inserted block. -1 if the HTX message is empty */
210 int32_t head; /* oldest inserted block. -1 if the HTX message is empty */
211 int32_t first; /* position of the first block to (re)start the analyse. -1 if unset. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200212
213 uint32_t tail_addr; /* start address of the free space in front of the the blocks table */
214 uint32_t head_addr; /* start address of the free space at the beginning */
215 uint32_t end_addr; /* end address of the free space at the beginning */
216
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100217 uint64_t extra; /* known bytes amount remaining to receive */
218 uint32_t flags; /* HTX_FL_* */
219
Christopher Faulet192c6a22019-06-11 16:32:24 +0200220 /* XXX 4 bytes unused */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100221
Christopher Faulet192c6a22019-06-11 16:32:24 +0200222 /* Blocks representing the HTTP message itself */
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200223 char blocks[0] __attribute__((aligned(8)));
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100224};
225
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200226
227extern struct htx htx_empty;
228
229struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
230struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
231struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100232void htx_truncate(struct htx *htx, uint32_t offset);
Christopher Faulet549822f2019-02-25 10:23:19 +0100233struct htx_ret htx_drain(struct htx *htx, uint32_t max);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200234
235struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100236 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200237struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
238 enum htx_blk_type mark);
239
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100240struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
241 const struct ist p1, const struct ist p2, const struct ist p3);
242struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
243 const struct ist p2, const struct ist p3);
244
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200245struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100246 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200247
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200248struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200249struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200250struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200251struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200252struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200253struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data);
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200254size_t htx_add_data(struct htx *htx, const struct ist data);
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200255struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data);
Christopher Faulet86fcf6d2019-06-11 10:41:19 +0200256void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200257
Christopher Faulet570d1612018-11-26 11:13:57 +0100258/* Functions and macros to get parts of the start-line or legnth of these
Christopher Faulet3b219722019-06-19 13:48:09 +0200259 * parts. Request and response start-lines are both composed of 3 parts.
Christopher Faulet570d1612018-11-26 11:13:57 +0100260 */
261#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
262
263#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
264#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
265#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
266#define HTX_SL_P1_PTR(sl) ((sl)->l)
267#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
268#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
269
270#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
271#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
272#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
273#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
274#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
275#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
276
277#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
278#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
279#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
280#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
281#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
282#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
283
Willy Tarreau8de1df92019-04-15 21:27:18 +0200284static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100285{
286 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
287}
Christopher Faulet570d1612018-11-26 11:13:57 +0100288
Willy Tarreau8de1df92019-04-15 21:27:18 +0200289static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100290{
291 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
292}
293
Willy Tarreau8de1df92019-04-15 21:27:18 +0200294static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100295{
296 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
297}
298
Willy Tarreau8de1df92019-04-15 21:27:18 +0200299static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100300{
301 return htx_sl_p1(sl);
302}
303
Willy Tarreau8de1df92019-04-15 21:27:18 +0200304static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100305{
306 return htx_sl_p2(sl);
307}
308
Willy Tarreau8de1df92019-04-15 21:27:18 +0200309static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100310{
311 return htx_sl_p3(sl);
312}
313
314
Willy Tarreau8de1df92019-04-15 21:27:18 +0200315static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100316{
317 return htx_sl_p1(sl);
318}
319
Willy Tarreau8de1df92019-04-15 21:27:18 +0200320static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100321{
322 return htx_sl_p2(sl);
323}
324
Willy Tarreau8de1df92019-04-15 21:27:18 +0200325static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100326{
327 return htx_sl_p3(sl);
328}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200329
Christopher Faulet3b219722019-06-19 13:48:09 +0200330/* Converts a position to the corresponding relative address */
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200331static inline uint32_t htx_pos_to_addr(const struct htx *htx, uint32_t pos)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200332{
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200333 return htx->size - (pos + 1) * sizeof(struct htx_blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200334}
335
Christopher Faulet3b219722019-06-19 13:48:09 +0200336/* Returns the position of the block <blk>. It is the caller responsibility to
337 * be sure <blk> is part of <htx>. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200338static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
339{
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200340 return ((htx->blocks + htx->size - (char *)blk) / sizeof(struct htx_blk) - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200341}
342
Christopher Faulet3b219722019-06-19 13:48:09 +0200343/* Returns the block at the position <pos>. It is the caller responsibility to
344 * be sure the block at the position <pos> exists. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200345static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
346{
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200347 return (struct htx_blk *)(htx->blocks + htx_pos_to_addr(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200348}
349
350/* Returns the type of the block <blk> */
351static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
352{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100353 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200354}
355
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200356/* Returns the size of the block <blk>, depending of its type */
357static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
358{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100359 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200360
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100361 switch (type) {
362 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200363 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100364 /* name.length + value.length */
365 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100366 default:
367 /* value.length */
368 return (blk->info & 0xfffffff);
369 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200370}
371
Christopher Faulet192c6a22019-06-11 16:32:24 +0200372/* Returns the position of the oldest entry (head). It returns a signed 32-bits
373 * integer, -1 means the HTX message is empty.
Christopher Faulet28f29c72019-04-30 17:55:45 +0200374 */
375static inline int32_t htx_get_head(const struct htx *htx)
376{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200377 return htx->head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200378}
379
380/* Returns the oldest HTX block (head) if the HTX message is not
381 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100382 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200383static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
384{
385 int32_t head = htx_get_head(htx);
386
387 return ((head == -1) ? NULL : htx_get_blk(htx, head));
388}
389
390/* Returns the type of the oldest HTX block (head) if the HTX message is not
391 * empty. Otherwise it returns HTX_BLK_UNUSED.
392 */
393static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
394{
395 struct htx_blk *blk = htx_get_head_blk(htx);
396
397 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
398}
399
Christopher Faulet192c6a22019-06-11 16:32:24 +0200400/* Returns the position of the newest entry (tail). It returns a signed 32-bits
401 * integer, -1 means the HTX message is empty.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200402 */
403static inline int32_t htx_get_tail(const struct htx *htx)
404{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200405 return htx->tail;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200406}
407
408/* Returns the newest HTX block (tail) if the HTX message is not
409 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100410 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200411static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
412{
413 int32_t tail = htx_get_tail(htx);
414
415 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
416}
417
418/* Returns the type of the newest HTX block (tail) if the HTX message is not
419 * empty. Otherwise it returns HTX_BLK_UNUSED.
420 */
421static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
422{
423 struct htx_blk *blk = htx_get_tail_blk(htx);
424
425 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
426}
427
Christopher Faulet192c6a22019-06-11 16:32:24 +0200428/* Returns the position of the first block in the HTX message <htx>. -1 means
429 * the first block is unset or the HTS is empty.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200430 */
431static inline int32_t htx_get_first(const struct htx *htx)
432{
Christopher Faulet29f17582019-05-23 11:03:26 +0200433 return htx->first;
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200434}
435
Christopher Faulet29f17582019-05-23 11:03:26 +0200436/* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
437 * empty, NULL returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200438 */
439static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
440{
441 int32_t pos;
442
443 pos = htx_get_first(htx);
444 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
445}
446
447/* Returns the type of the first block in the HTX message <htx>. If unset or if
448 * <htx> is empty, HTX_BLK_UNUSED is returned.
449 */
450static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
451{
452 struct htx_blk *blk = htx_get_first_blk(htx);
453
454 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
455}
456
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800457/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200458 * the message is empty or if <pos> is the position of the head, -1 returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200459 */
460static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
461{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200462 if (htx->head == -1 || pos == htx->head)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100463 return -1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100464 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200465}
466
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100467/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
468 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100469 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100470static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
471 const struct htx_blk *blk)
472{
473 int32_t pos;
474
475 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
476 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
477}
478
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800479/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200480 * the message is empty or if <pos> is the position of the tail, -1 returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200481 */
482static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
483{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200484 if (htx->tail == -1 || pos == htx->tail)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100485 return -1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200486 return (pos + 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200487
488}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100489
490/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
491 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100492 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100493static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
494 const struct htx_blk *blk)
495{
496 int32_t pos;
497
498 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
499 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
500}
501
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200502/* Changes the size of the value. It is the caller responsibility to change the
Christopher Fauletbb0efcd2019-06-18 09:37:00 +0200503 * value itself, make sure there is enough space and update allocated
504 * value. This function updates the HTX message accordingly.
505 */
506static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk, uint32_t newlen)
507{
508 enum htx_blk_type type = htx_get_blk_type(blk);
509 uint32_t oldlen, sz;
510 int32_t delta;
511
512 sz = htx_get_blksz(blk);
513 switch (type) {
514 case HTX_BLK_HDR:
515 case HTX_BLK_TLR:
516 oldlen = (blk->info >> 8) & 0xfffff;
517 blk->info = (type << 28) + (newlen << 8) + (blk->info & 0xff);
518 break;
519 default:
520 oldlen = blk->info & 0xfffffff;
521 blk->info = (type << 28) + newlen;
522 break;
523 }
524
525 /* Update HTTP message */
526 delta = (newlen - oldlen);
527 htx->data += delta;
528 if (blk->addr+sz == htx->tail_addr)
529 htx->tail_addr += delta;
530 else if (blk->addr+sz == htx->head_addr)
531 htx->head_addr += delta;
532}
533
534/* Changes the size of the value. It is the caller responsibility to change the
535 * value itself, make sure there is enough space and update allocated
536 * value. Unlike the function htx_change_blk_value_len(), this one does not
537 * update the HTX message. So it should be used with caution.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200538 */
539static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
540{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100541 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100543 switch (type) {
544 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200545 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100546 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
547 break;
548 case HTX_BLK_REQ_SL:
549 case HTX_BLK_RES_SL:
550 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100551 blk->info = (type << 28) + vlen;
552 break;
553 default:
554 /* Unexpected case */
555 break;
556 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557}
558
559/* Returns the data pointer of the block <blk> */
560static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
561{
562 return ((void *)htx->blocks + blk->addr);
563}
564
Christopher Faulet3b219722019-06-19 13:48:09 +0200565/* Returns the name of the block <blk>, only if it is a header or a
566 * trailer. Otherwise it returns an empty string.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200567 */
568static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
569{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100570 enum htx_blk_type type = htx_get_blk_type(blk);
571 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200572
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100573 switch (type) {
574 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200575 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100576 ret.ptr = htx_get_blk_ptr(htx, blk);
577 ret.len = blk->info & 0xff;
578 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200579
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100580 default:
581 return ist("");
582 }
583 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200584}
585
Christopher Faulet54483df2018-11-26 15:05:52 +0100586
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200587/* Returns the value of the block <blk>, depending on its type. If there is no
Christopher Faulet3b219722019-06-19 13:48:09 +0200588 * value (for end-of blocks), an empty one is retruned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200589 */
590static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
591{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100592 enum htx_blk_type type = htx_get_blk_type(blk);
593 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200594
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100595 switch (type) {
596 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200597 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100598 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
599 ret.len = (blk->info >> 8) & 0xfffff;
600 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200601
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100602 case HTX_BLK_REQ_SL:
603 case HTX_BLK_RES_SL:
604 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100605 ret.ptr = htx_get_blk_ptr(htx, blk);
606 ret.len = blk->info & 0xfffffff;
607 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100609 default:
610 return ist("");
611 }
612 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200613}
614
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100615/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
616 * address and its length are adjusted, and the htx's total data count is
617 * updated. This is used to mark that part of some data were transfered
618 * from a DATA block without removing this DATA block. No sanity check is
619 * performed, the caller is reponsible for doing this exclusively on DATA
620 * blocks, and never removing more than the block's size.
621 */
622static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
623{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200624 if (blk->addr == htx->end_addr)
625 htx->end_addr += n;
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100626 blk->addr += n;
627 blk->info -= n;
628 htx->data -= n;
629}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200630
631/* Returns the space used by metadata in <htx>. */
632static inline uint32_t htx_meta_space(const struct htx *htx)
633{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200634 if (htx->tail == -1)
635 return 0;
636
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200637 return ((htx->tail + 1 - htx->head) * sizeof(struct htx_blk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200638}
639
Christopher Faulet3b219722019-06-19 13:48:09 +0200640/* Returns the space used (payload + metadata) in <htx> */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200641static inline uint32_t htx_used_space(const struct htx *htx)
642{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100643 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200644}
645
646/* Returns the free space in <htx> */
647static inline uint32_t htx_free_space(const struct htx *htx)
648{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100649 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200650}
651
652/* Returns the maximum size available to store some data in <htx> if a new block
653 * is reserved.
654 */
655static inline uint32_t htx_free_data_space(const struct htx *htx)
656{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100657 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200658
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200659 if (free < sizeof(struct htx_blk))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100660 return 0;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200661 return (free - sizeof(struct htx_blk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200662}
663
Christopher Faulet2ae35042019-05-16 11:16:39 +0200664/* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
665 * set to -1 to have no limit.
666 */
667static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
668{
669 uint32_t free = htx_free_space(htx);
670
671 if (max != -1 && free > max)
672 free = max;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200673 if (free < sizeof(struct htx_blk))
Christopher Faulet2ae35042019-05-16 11:16:39 +0200674 return 0;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200675 return (free - sizeof(struct htx_blk));
Christopher Faulet2ae35042019-05-16 11:16:39 +0200676}
677
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200678/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
679static inline int htx_almost_full(const struct htx *htx)
680{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100681 if (!htx->size || htx_free_space(htx) < htx->size / 4)
682 return 1;
683 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200684}
685
Christopher Faulet3b219722019-06-19 13:48:09 +0200686/* Resets an HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200687static inline void htx_reset(struct htx *htx)
688{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200689 htx->tail = htx->head = htx->first = -1;
690 htx->data = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200691 htx->tail_addr = htx->head_addr = htx->end_addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692 htx->extra = 0;
693 htx->flags = HTX_FL_NONE;
694}
695
Christopher Faulet3b219722019-06-19 13:48:09 +0200696/* Returns the available room for raw data in buffer <buf> once HTX overhead is
Willy Tarreau3906e222018-12-05 07:56:25 +0100697 * taken into account (one HTX header and two blocks). The purpose is to figure
698 * the optimal fill length to avoid copies.
699 */
700static inline size_t buf_room_for_htx_data(const struct buffer *buf)
701{
702 size_t room;
703
704 room = b_room(buf);
705 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
706 room = 0;
707 else
708 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
709
710 return room;
711}
712
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100713
714/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Christopher Faulet3b219722019-06-19 13:48:09 +0200715 * function does not update the buffer. So if the HTX message is updated, the
716 * caller must call htx_to_buf() to be sure to also update the underlying buffer
717 * accordingly. Note that it always returns a valid pointer, either to an
718 * initialized buffer or to the empty buffer. This function must always be
719 * called with a buffer containing an HTX message (or an empty buffer).
Willy Tarreau245d1892019-01-31 07:21:42 +0100720 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100721static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200722{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100723 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200724
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100725 if (b_is_null(buf))
726 return &htx_empty;
727 htx = ((struct htx *)(buf->area));
728 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100729 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100730 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100731 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200732 return htx;
733}
734
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100735/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
Christopher Faulet3b219722019-06-19 13:48:09 +0200736 * full. It should be used when you want to add something into the HTX message,
737 * so the call to htx_to_buf() may be skipped. But, it is the caller
738 * responsibility to call htx_to_buf() to reset <buf> if it is relevant. The
739 * returned pointer is always valid. This function must always be called with a
740 * buffer containing an HTX message (or an empty buffer).
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100741 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100742 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100743 */
744static inline struct htx *htx_from_buf(struct buffer *buf)
745{
746 struct htx *htx = htxbuf(buf);
747
748 b_set_data(buf, b_size(buf));
749 return htx;
750}
751
Christopher Faulet3b219722019-06-19 13:48:09 +0200752/* Update <buf> accordingly to the HTX message <htx> */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100753static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
754{
Christopher Faulet505adfc2019-09-06 19:08:27 +0200755 if ((htx->head == -1) &&
756 !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100757 htx_reset(htx);
758 b_set_data(buf, 0);
759 }
760 else
761 b_set_data(buf, b_size(buf));
762}
763
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100764/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
765 * illegal to call this with htx == NULL.
766 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200767static inline int htx_is_empty(const struct htx *htx)
768{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200769 return (htx->head == -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200770}
771
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100772/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
773 * is illegal to call this with htx == NULL.
774 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200775static inline int htx_is_not_empty(const struct htx *htx)
776{
Christopher Faulet192c6a22019-06-11 16:32:24 +0200777 return (htx->head != -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200778}
779
Christopher Faulet192c6a22019-06-11 16:32:24 +0200780/* Returns the number of used blocks in the HTX message <htx>. Note that it is
781 * illegal to call this function with htx == NULL. Note also blocks of type
782 * HTX_BLK_UNUSED are part of used blocks.
783 */
784static inline int htx_nbblks(const struct htx *htx)
785{
786 return ((htx->head != -1) ? (htx->tail + 1 - htx->head) : 0);
787}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200788/* For debugging purpose */
789static inline const char *htx_blk_type_str(enum htx_blk_type type)
790{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100791 switch (type) {
792 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
793 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
794 case HTX_BLK_HDR: return "HTX_BLK_HDR";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100795 case HTX_BLK_EOH: return "HTX_BLK_EOH";
796 case HTX_BLK_DATA: return "HTX_BLK_DATA";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100797 case HTX_BLK_TLR: return "HTX_BLK_TLR";
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200798 case HTX_BLK_EOT: return "HTX_BLK_EOT";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100799 case HTX_BLK_EOM: return "HTX_BLK_EOM";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100800 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
801 default: return "HTX_BLK_???";
802 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803}
804
Christopher Faulet3b219722019-06-19 13:48:09 +0200805/* For debugging purpose */
Christopher Faulet27aa65e2019-10-01 22:03:49 +0200806static inline void htx_dump(struct buffer *chunk, const struct htx *htx, int full)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100808 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809
Christopher Faulet27aa65e2019-10-01 22:03:49 +0200810 chunk_appendf(chunk, " htx=%p(size=%u,data=%u,used=%u,wrap=%s,flags=0x%08x,extra=%llu,"
811 "first=%d,head=%d,tail=%d,tail_addr=%d,head_addr=%d,end_addr=%d)",
812 htx, htx->size, htx->data, htx_nbblks(htx), (!htx->head_addr) ? "NO" : "YES",
813 htx->flags, (unsigned long long)htx->extra, htx->first, htx->head, htx->tail,
814 htx->tail_addr, htx->head_addr, htx->end_addr);
815
816 if (!full || !htx_nbblks(htx))
817 return;
818 chunk_memcat(chunk, "\n", 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200819
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100820 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100821 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100822 struct htx_blk *blk = htx_get_blk(htx, pos);
823 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100824 uint32_t sz = htx_get_blksz(blk);
825 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200826
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100827 n = htx_get_blk_name(htx, blk);
828 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100830 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet27aa65e2019-10-01 22:03:49 +0200832 chunk_appendf(chunk, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
833 pos, htx_blk_type_str(type), sz, blk->addr,
834 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
835 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
836 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200838 else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet27aa65e2019-10-01 22:03:49 +0200839 chunk_appendf(chunk, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
840 pos, htx_blk_type_str(type), sz, blk->addr,
841 (int)n.len, n.ptr,
842 (int)v.len, v.ptr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100843 else
Christopher Faulet27aa65e2019-10-01 22:03:49 +0200844 chunk_appendf(chunk, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
845 pos, htx_blk_type_str(type), sz, blk->addr,
846 (!v.len ? "\t<empty>" : ""));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100847 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200848}
849
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100850#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200851
852/*
853 * Local variables:
854 * c-indent-level: 8
855 * c-basic-offset: 8
856 * End:
857 */