blob: ba6604b27bd151279a264c5bc490288bb1a0fce3 [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 */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100132#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) */
Christopher Faulete21c0162019-06-14 10:08:13 +0200140#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 */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100143
144/* HTX flags */
145#define HTX_FL_NONE 0x00000000
Christopher Faulet3b219722019-06-19 13:48:09 +0200146#define HTX_FL_PARSING_ERROR 0x00000001 /* Set when a parsing error occurred */
147#define HTX_FL_UPGRADE 0x00000002 /* Set when an upgrade is in progress */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100148
149
Christopher Faulet3b219722019-06-19 13:48:09 +0200150/* HTX block's type (max 15). */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100151enum htx_blk_type {
152 HTX_BLK_REQ_SL = 0, /* Request start-line */
153 HTX_BLK_RES_SL = 1, /* Response start-line */
154 HTX_BLK_HDR = 2, /* header name/value block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200155 HTX_BLK_EOH = 3, /* end-of-headers block */
156 HTX_BLK_DATA = 4, /* data block */
Christopher Faulet54b5e212019-06-04 10:08:28 +0200157 HTX_BLK_TLR = 5, /* trailer name/value block */
158 HTX_BLK_EOT = 6, /* end-of-trailers block */
159 HTX_BLK_EOM = 7, /* end-of-message block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200160 /* 8 .. 14 unused */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100161 HTX_BLK_UNUSED = 15, /* unused/removed block */
162};
163
Christopher Faulet3b219722019-06-19 13:48:09 +0200164/* One HTX block descriptor */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100165struct htx_blk {
Christopher Faulet3b219722019-06-19 13:48:09 +0200166 uint32_t addr; /* relative storage address of the block's payload */
167 uint32_t info; /* information about the block (type, length) */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100168};
169
Christopher Faulet3b219722019-06-19 13:48:09 +0200170/* Composite return value used by some HTX functions */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100171struct htx_ret {
Christopher Faulet3b219722019-06-19 13:48:09 +0200172 int32_t ret; /* A numerical value */
173 struct htx_blk *blk; /* An HTX block */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100174};
175
Christopher Faulet3b219722019-06-19 13:48:09 +0200176/* HTX start-line */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100177struct htx_sl {
178 unsigned int flags; /* HTX_SL_F_* */
179 union {
180 struct {
181 enum http_meth_t meth; /* method */
182 } req;
183 struct {
184 uint16_t status; /* status code */
185 } res;
186 } info;
187
188 /* XXX 2 bytes unused */
189
Christopher Faulet3b219722019-06-19 13:48:09 +0200190 int32_t hdrs_bytes; /* Bytes held by all headers, as seen by the mux
191 * during parsing, from this start-line to the
192 * corresponding EOH. -1 if unknown */
Christopher Faulet05c083c2019-05-15 14:56:47 +0200193
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100194 unsigned int len[3]; /* length of differnt parts of the start-line */
195 char l[0];
196};
197
198/* Internal representation of an HTTP message */
199struct htx {
200 uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */
201 uint32_t data; /* the data size, in bytes. To known to total size used by all allocated
202 * blocks (blocks and their contents), you need to add size used by blocks,
203 * i.e. [ used * sizeof(struct htx_blk *) ] */
204
205 uint32_t used; /* number of blocks in use */
Christopher Faulet3b219722019-06-19 13:48:09 +0200206 uint32_t tail; /* newest inserted block. -1 if the HTX message is empty */
207 uint32_t head; /* oldest inserted block. -1 if the HTX message is empty */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200208
209 uint32_t tail_addr; /* start address of the free space in front of the the blocks table */
210 uint32_t head_addr; /* start address of the free space at the beginning */
211 uint32_t end_addr; /* end address of the free space at the beginning */
212
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100213
214 uint64_t extra; /* known bytes amount remaining to receive */
215 uint32_t flags; /* HTX_FL_* */
216
Christopher Faulet29f17582019-05-23 11:03:26 +0200217 int32_t first; /* position of the first block to (re)start the analyse. -1 if unset. */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100218
219 struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
220};
221
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200222
223extern struct htx htx_empty;
224
225struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
226struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
227struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100228void htx_truncate(struct htx *htx, uint32_t offset);
Christopher Faulet549822f2019-02-25 10:23:19 +0100229struct htx_ret htx_drain(struct htx *htx, uint32_t max);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200230
231struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100232 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200233struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
234 enum htx_blk_type mark);
235
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100236struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
237 const struct ist p1, const struct ist p2, const struct ist p3);
238struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
239 const struct ist p2, const struct ist p3);
240
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200241struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100242 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200243
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200244struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200245struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200246struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200247struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200248struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200249struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data);
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200250size_t htx_add_data(struct htx *htx, const struct ist data);
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200251struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data);
Christopher Faulet86fcf6d2019-06-11 10:41:19 +0200252void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200253
Christopher Fauletc59ff232018-12-03 13:58:44 +0100254int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
255int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
256int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
257int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200258
Christopher Faulet570d1612018-11-26 11:13:57 +0100259/* Functions and macros to get parts of the start-line or legnth of these
Christopher Faulet3b219722019-06-19 13:48:09 +0200260 * parts. Request and response start-lines are both composed of 3 parts.
Christopher Faulet570d1612018-11-26 11:13:57 +0100261 */
262#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
263
264#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
265#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
266#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
267#define HTX_SL_P1_PTR(sl) ((sl)->l)
268#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
269#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
270
271#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
272#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
273#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
274#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
275#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
276#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
277
278#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
279#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
280#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
281#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
282#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
283#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
284
Willy Tarreau8de1df92019-04-15 21:27:18 +0200285static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100286{
287 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
288}
Christopher Faulet570d1612018-11-26 11:13:57 +0100289
Willy Tarreau8de1df92019-04-15 21:27:18 +0200290static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100291{
292 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
293}
294
Willy Tarreau8de1df92019-04-15 21:27:18 +0200295static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100296{
297 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
298}
299
Willy Tarreau8de1df92019-04-15 21:27:18 +0200300static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100301{
302 return htx_sl_p1(sl);
303}
304
Willy Tarreau8de1df92019-04-15 21:27:18 +0200305static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100306{
307 return htx_sl_p2(sl);
308}
309
Willy Tarreau8de1df92019-04-15 21:27:18 +0200310static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100311{
312 return htx_sl_p3(sl);
313}
314
315
Willy Tarreau8de1df92019-04-15 21:27:18 +0200316static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100317{
318 return htx_sl_p1(sl);
319}
320
Willy Tarreau8de1df92019-04-15 21:27:18 +0200321static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100322{
323 return htx_sl_p2(sl);
324}
325
Willy Tarreau8de1df92019-04-15 21:27:18 +0200326static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100327{
328 return htx_sl_p3(sl);
329}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200330
Christopher Faulet3b219722019-06-19 13:48:09 +0200331/* Converts a position to the corresponding relative address */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200332static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
333{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100334 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200335}
336
Christopher Faulet3b219722019-06-19 13:48:09 +0200337/* Returns the position of the block <blk>. It is the caller responsibility to
338 * be sure <blk> is part of <htx>. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200339static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
340{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100341 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200342}
343
Christopher Faulet3b219722019-06-19 13:48:09 +0200344/* Returns the block at the position <pos>. It is the caller responsibility to
345 * be sure the block at the position <pos> exists. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200346static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
347{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100348 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200349}
350
351/* Returns the type of the block <blk> */
352static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
353{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100354 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200355}
356
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200357/* Returns the size of the block <blk>, depending of its type */
358static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
359{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100360 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200361
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100362 switch (type) {
363 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200364 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100365 /* name.length + value.length */
366 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100367 default:
368 /* value.length */
369 return (blk->info & 0xfffffff);
370 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200371}
372
Christopher Faulet28f29c72019-04-30 17:55:45 +0200373/* Returns the position of the oldest entry (head).
374 *
375 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
376 * store on unsigned 32-bits integer, but it is impossible to have so much
377 * blocks to overflow a 32-bits signed integer !
378 */
379static inline int32_t htx_get_head(const struct htx *htx)
380{
381 return (htx->used ? htx->head : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200382}
383
384/* Returns the oldest HTX block (head) if the HTX message is not
385 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100386 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200387static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
388{
389 int32_t head = htx_get_head(htx);
390
391 return ((head == -1) ? NULL : htx_get_blk(htx, head));
392}
393
394/* Returns the type of the oldest HTX block (head) if the HTX message is not
395 * empty. Otherwise it returns HTX_BLK_UNUSED.
396 */
397static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
398{
399 struct htx_blk *blk = htx_get_head_blk(htx);
400
401 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
402}
403
404/* Returns the position of the newest entry (tail).
405 *
406 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
407 * store on unsigned 32-bits integer, but it is impossible to have so much
408 * blocks to overflow a 32-bits signed integer !
409 */
410static inline int32_t htx_get_tail(const struct htx *htx)
411{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100412 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200413}
414
415/* Returns the newest HTX block (tail) if the HTX message is not
416 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100417 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200418static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
419{
420 int32_t tail = htx_get_tail(htx);
421
422 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
423}
424
425/* Returns the type of the newest HTX block (tail) if the HTX message is not
426 * empty. Otherwise it returns HTX_BLK_UNUSED.
427 */
428static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
429{
430 struct htx_blk *blk = htx_get_tail_blk(htx);
431
432 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
433}
434
Christopher Faulet29f17582019-05-23 11:03:26 +0200435/* Returns the position of the first block in the HTX message <htx>. If unset,
436 * or if <htx> is empty, -1 is returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200437 *
438 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
439 * store on unsigned 32-bits integer, but it is impossible to have so much
440 * blocks to overflow a 32-bits signed integer !
441 */
442static inline int32_t htx_get_first(const struct htx *htx)
443{
Christopher Faulet29f17582019-05-23 11:03:26 +0200444 if (!htx->used)
445 return -1;
446 return htx->first;
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200447}
448
Christopher Faulet29f17582019-05-23 11:03:26 +0200449/* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
450 * empty, NULL returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200451 */
452static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
453{
454 int32_t pos;
455
456 pos = htx_get_first(htx);
457 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
458}
459
460/* Returns the type of the first block in the HTX message <htx>. If unset or if
461 * <htx> is empty, HTX_BLK_UNUSED is returned.
462 */
463static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
464{
465 struct htx_blk *blk = htx_get_first_blk(htx);
466
467 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
468}
469
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800470/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200471 * the message is empty or if <pos> is the position of the head, -1 returned.
472 *
473 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
474 * store on unsigned 32-bits integer, but it is impossible to have so much
475 * blocks to overflow a 32-bits signed integer !
476 */
477static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
478{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100479 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200480
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100481 head = htx_get_head(htx);
482 if (head == -1 || pos == head)
483 return -1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100484 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200485}
486
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100487/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
488 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100490static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
491 const struct htx_blk *blk)
492{
493 int32_t pos;
494
495 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
496 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
497}
498
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800499/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200500 * the message is empty or if <pos> is the position of the tail, -1 returned.
501 *
502 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
503 * store on unsigned 32-bits integer, but it is impossible to have so much
504 * blocks to overflow a 32-bits signed integer !
505 */
506static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
507{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200508 if (!htx->used || pos == htx->tail)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100509 return -1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200510 return (pos + 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200511
512}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100513
514/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
515 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100516 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100517static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
518 const struct htx_blk *blk)
519{
520 int32_t pos;
521
522 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
523 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
524}
525
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526/* Changes the size of the value. It is the caller responsibility to change the
Christopher Fauletbb0efcd2019-06-18 09:37:00 +0200527 * value itself, make sure there is enough space and update allocated
528 * value. This function updates the HTX message accordingly.
529 */
530static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk, uint32_t newlen)
531{
532 enum htx_blk_type type = htx_get_blk_type(blk);
533 uint32_t oldlen, sz;
534 int32_t delta;
535
536 sz = htx_get_blksz(blk);
537 switch (type) {
538 case HTX_BLK_HDR:
539 case HTX_BLK_TLR:
540 oldlen = (blk->info >> 8) & 0xfffff;
541 blk->info = (type << 28) + (newlen << 8) + (blk->info & 0xff);
542 break;
543 default:
544 oldlen = blk->info & 0xfffffff;
545 blk->info = (type << 28) + newlen;
546 break;
547 }
548
549 /* Update HTTP message */
550 delta = (newlen - oldlen);
551 htx->data += delta;
552 if (blk->addr+sz == htx->tail_addr)
553 htx->tail_addr += delta;
554 else if (blk->addr+sz == htx->head_addr)
555 htx->head_addr += delta;
556}
557
558/* Changes the size of the value. It is the caller responsibility to change the
559 * value itself, make sure there is enough space and update allocated
560 * value. Unlike the function htx_change_blk_value_len(), this one does not
561 * update the HTX message. So it should be used with caution.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200562 */
563static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
564{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100565 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100567 switch (type) {
568 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200569 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100570 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
571 break;
572 case HTX_BLK_REQ_SL:
573 case HTX_BLK_RES_SL:
574 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100575 blk->info = (type << 28) + vlen;
576 break;
577 default:
578 /* Unexpected case */
579 break;
580 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200581}
582
583/* Returns the data pointer of the block <blk> */
584static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
585{
586 return ((void *)htx->blocks + blk->addr);
587}
588
Christopher Faulet3b219722019-06-19 13:48:09 +0200589/* Returns the name of the block <blk>, only if it is a header or a
590 * trailer. Otherwise it returns an empty string.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200591 */
592static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
593{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100594 enum htx_blk_type type = htx_get_blk_type(blk);
595 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200596
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100597 switch (type) {
598 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200599 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100600 ret.ptr = htx_get_blk_ptr(htx, blk);
601 ret.len = blk->info & 0xff;
602 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200603
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100604 default:
605 return ist("");
606 }
607 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608}
609
Christopher Faulet54483df2018-11-26 15:05:52 +0100610
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200611/* Returns the value of the block <blk>, depending on its type. If there is no
Christopher Faulet3b219722019-06-19 13:48:09 +0200612 * value (for end-of blocks), an empty one is retruned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200613 */
614static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
615{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100616 enum htx_blk_type type = htx_get_blk_type(blk);
617 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100619 switch (type) {
620 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200621 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100622 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
623 ret.len = (blk->info >> 8) & 0xfffff;
624 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100626 case HTX_BLK_REQ_SL:
627 case HTX_BLK_RES_SL:
628 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100629 ret.ptr = htx_get_blk_ptr(htx, blk);
630 ret.len = blk->info & 0xfffffff;
631 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200632
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100633 default:
634 return ist("");
635 }
636 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637}
638
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100639/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
640 * address and its length are adjusted, and the htx's total data count is
641 * updated. This is used to mark that part of some data were transfered
642 * from a DATA block without removing this DATA block. No sanity check is
643 * performed, the caller is reponsible for doing this exclusively on DATA
644 * blocks, and never removing more than the block's size.
645 */
646static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
647{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200648 if (blk->addr == htx->end_addr)
649 htx->end_addr += n;
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100650 blk->addr += n;
651 blk->info -= n;
652 htx->data -= n;
653}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200654
655/* Returns the space used by metadata in <htx>. */
656static inline uint32_t htx_meta_space(const struct htx *htx)
657{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100658 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200659}
660
Christopher Faulet3b219722019-06-19 13:48:09 +0200661/* Returns the space used (payload + metadata) in <htx> */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200662static inline uint32_t htx_used_space(const struct htx *htx)
663{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100664 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665}
666
667/* Returns the free space in <htx> */
668static inline uint32_t htx_free_space(const struct htx *htx)
669{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100670 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671}
672
673/* Returns the maximum size available to store some data in <htx> if a new block
674 * is reserved.
675 */
676static inline uint32_t htx_free_data_space(const struct htx *htx)
677{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100678 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200679
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100680 if (free < sizeof(htx->blocks[0]))
681 return 0;
682 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200683}
684
Christopher Faulet2ae35042019-05-16 11:16:39 +0200685/* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
686 * set to -1 to have no limit.
687 */
688static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
689{
690 uint32_t free = htx_free_space(htx);
691
692 if (max != -1 && free > max)
693 free = max;
694 if (free < sizeof(htx->blocks[0]))
695 return 0;
696 return (free - sizeof(htx->blocks[0]));
697}
698
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200699/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
700static inline int htx_almost_full(const struct htx *htx)
701{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100702 if (!htx->size || htx_free_space(htx) < htx->size / 4)
703 return 1;
704 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200705}
706
Christopher Faulet3b219722019-06-19 13:48:09 +0200707/* Resets an HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200708static inline void htx_reset(struct htx *htx)
709{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200710 htx->data = htx->used = htx->tail = htx->head = 0;
711 htx->tail_addr = htx->head_addr = htx->end_addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200712 htx->extra = 0;
713 htx->flags = HTX_FL_NONE;
Christopher Faulet29f17582019-05-23 11:03:26 +0200714 htx->first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200715}
716
Christopher Faulet3b219722019-06-19 13:48:09 +0200717/* Returns the available room for raw data in buffer <buf> once HTX overhead is
Willy Tarreau3906e222018-12-05 07:56:25 +0100718 * taken into account (one HTX header and two blocks). The purpose is to figure
719 * the optimal fill length to avoid copies.
720 */
721static inline size_t buf_room_for_htx_data(const struct buffer *buf)
722{
723 size_t room;
724
725 room = b_room(buf);
726 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
727 room = 0;
728 else
729 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
730
731 return room;
732}
733
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100734
735/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Christopher Faulet3b219722019-06-19 13:48:09 +0200736 * function does not update the buffer. So if the HTX message is updated, the
737 * caller must call htx_to_buf() to be sure to also update the underlying buffer
738 * accordingly. Note that it always returns a valid pointer, either to an
739 * initialized buffer or to the empty buffer. This function must always be
740 * called with a buffer containing an HTX message (or an empty buffer).
Willy Tarreau245d1892019-01-31 07:21:42 +0100741 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100742static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200743{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100744 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200745
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100746 if (b_is_null(buf))
747 return &htx_empty;
748 htx = ((struct htx *)(buf->area));
749 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100750 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100751 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100752 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753 return htx;
754}
755
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100756/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
Christopher Faulet3b219722019-06-19 13:48:09 +0200757 * full. It should be used when you want to add something into the HTX message,
758 * so the call to htx_to_buf() may be skipped. But, it is the caller
759 * responsibility to call htx_to_buf() to reset <buf> if it is relevant. The
760 * returned pointer is always valid. This function must always be called with a
761 * buffer containing an HTX message (or an empty buffer).
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100762 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100763 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100764 */
765static inline struct htx *htx_from_buf(struct buffer *buf)
766{
767 struct htx *htx = htxbuf(buf);
768
769 b_set_data(buf, b_size(buf));
770 return htx;
771}
772
Christopher Faulet3b219722019-06-19 13:48:09 +0200773/* Update <buf> accordingly to the HTX message <htx> */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100774static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
775{
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200776 if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100777 htx_reset(htx);
778 b_set_data(buf, 0);
779 }
780 else
781 b_set_data(buf, b_size(buf));
782}
783
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100784/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
785 * illegal to call this with htx == NULL.
786 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787static inline int htx_is_empty(const struct htx *htx)
788{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100789 return !htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200790}
791
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100792/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
793 * is illegal to call this with htx == NULL.
794 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795static inline int htx_is_not_empty(const struct htx *htx)
796{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100797 return htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200798}
799
800/* For debugging purpose */
801static inline const char *htx_blk_type_str(enum htx_blk_type type)
802{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100803 switch (type) {
804 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
805 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
806 case HTX_BLK_HDR: return "HTX_BLK_HDR";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100807 case HTX_BLK_EOH: return "HTX_BLK_EOH";
808 case HTX_BLK_DATA: return "HTX_BLK_DATA";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100809 case HTX_BLK_TLR: return "HTX_BLK_TLR";
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200810 case HTX_BLK_EOT: return "HTX_BLK_EOT";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100811 case HTX_BLK_EOM: return "HTX_BLK_EOM";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100812 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
813 default: return "HTX_BLK_???";
814 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200815}
816
Christopher Faulet3b219722019-06-19 13:48:09 +0200817/* For debugging purpose */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200818static inline void htx_dump(struct htx *htx)
819{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100820 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100822 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Fauletd7884d32019-06-11 10:40:43 +0200823 htx, htx->size, htx->data, htx->used, (!htx->head_addr) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100824 (unsigned long long)htx->extra);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200825 fprintf(stderr, "\tfirst=%d - head=%u, tail=%u\n",
826 htx->first, htx->head, htx->tail);
827 fprintf(stderr, "\ttail_addr=%d - head_addr=%u, end_addr=%u\n",
828 htx->tail_addr, htx->head_addr, htx->end_addr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100830 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100831 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100832 struct htx_blk *blk = htx_get_blk(htx, pos);
833 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100834 uint32_t sz = htx_get_blksz(blk);
835 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200836
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100837 n = htx_get_blk_name(htx, blk);
838 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200839
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100840 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200841 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100842 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
843 pos, htx_blk_type_str(type), sz, blk->addr,
844 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
845 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
846 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200847 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200848 else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100849 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
850 pos, htx_blk_type_str(type), sz, blk->addr,
851 (int)n.len, n.ptr,
852 (int)v.len, v.ptr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100853 else
854 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
855 pos, htx_blk_type_str(type), sz, blk->addr,
856 (!v.len ? "\t<empty>" : ""));
857 }
858 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200859}
860
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100861#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200862
863/*
864 * Local variables:
865 * c-indent-level: 8
866 * c-basic-offset: 8
867 * End:
868 */