blob: a6065a6e3704fa42f7a81575375bb7fe4315a505 [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/*
34 * The internal representation of an HTTP message is a contiguous array
35 * containing both the blocks (htx_blk) and their contents. Blocks are stored
36 * starting from the end of the array while their contents are stored at the
37 * beginning.
38 *
39 * As data are sent to the peer, blocks and contents are released at the
40 * edges. This free space is reused when no more space left. So blocks and
41 * contents may wrap, not necessarily the same time.
42 *
43 * An HTTP block is as well a header as a body part or a trailer part. For all
44 * these types of block, a content is attached to the block. It can also be a
45 * mark, like the end-of-headers or end-of-message. For these blocks, there is
46 * no content but it count for a byte. It is important to not skip it when data
47 * are forwarded. An HTTP block is composed of 2 fields:
48 *
49 * - .info : It a 32 bits field containing the block's type on 4 bits
50 * followed by content' length. See below for details.
51 *
52 * - .addr : The content's address, if any, relatively to the beginning the
53 * array used to store the HTTP message itself.
54 *
55 * htx_blk.info representation:
56 *
57 * 0b 0000 0000 0000 0000 0000 0000 0000 0000
58 * ---- ------------------------ ---------
59 * type value (1 MB max) name length (header)
60 * ----------------------------------
61 * data length (256 MB max)
62 * (body, method, path, version, status, reason, trailers)
63 *
64 * types:
65 * - 0000 = request start-line
66 * - 0001 = response start-line
67 * - 0010 = header
68 * - 0011 = pseudo-header ou "special" header
69 * - 0100 = end-of-headers
70 * - 0101 = data
71 * - 0110 = end-of-data
72 * - 0111 = trailer
73 * - 1000 = end-of-message
74 * ...
75 * - 1101 = out-of-band
76 * - 1110 = error
77 * - 1111 = unused
78 *
79 */
80
81/*HTX start-line flags */
82#define HTX_SL_F_NONE 0x00000000
83#define HTX_SL_F_IS_RESP 0x00000001 /* It is the response start-line (unset means the request one) */
84#define HTX_SL_F_XFER_LEN 0x00000002 /* The message xfer size can be dertermined */
85#define HTX_SL_F_XFER_ENC 0x00000004 /* The transfer-encoding header was found in message */
86#define HTX_SL_F_CLEN 0x00000008 /* The content-length header was found in message */
87#define HTX_SL_F_CHNK 0x00000010 /* The message payload is chunked */
88#define HTX_SL_F_VER_11 0x00000020 /* The message indicates version 1.1 or above */
89#define HTX_SL_F_BODYLESS 0x00000040 /* The message has no body (content-length = 0) */
90
91/* HTX flags */
92#define HTX_FL_NONE 0x00000000
93#define HTX_FL_PARSING_ERROR 0x00000001
Christopher Faulet0ef372a2019-04-08 10:57:20 +020094#define HTX_FL_UPGRADE 0x00000002
Willy Tarreaub96b77e2018-12-11 10:22:41 +010095
96
Willy Tarreaub96b77e2018-12-11 10:22:41 +010097/* HTTP block's type (max 15). */
98enum htx_blk_type {
99 HTX_BLK_REQ_SL = 0, /* Request start-line */
100 HTX_BLK_RES_SL = 1, /* Response start-line */
101 HTX_BLK_HDR = 2, /* header name/value block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200102 HTX_BLK_EOH = 3, /* end-of-headers block */
103 HTX_BLK_DATA = 4, /* data block */
104 HTX_BLK_EOD = 5, /* end-of-data block */
105 HTX_BLK_TLR = 6, /* trailer name/value block */
106 HTX_BLK_EOM = 7, /* end-of-message block */
107 /* 8 .. 14 unused */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100108 HTX_BLK_UNUSED = 15, /* unused/removed block */
109};
110
111/* One HTTP block descriptor */
112struct htx_blk {
113 uint32_t addr; /* relative storage address of a data block */
114 uint32_t info; /* information about data stored */
115};
116
117struct htx_ret {
118 int32_t ret;
119 struct htx_blk *blk;
120};
121
122struct htx_sl {
123 unsigned int flags; /* HTX_SL_F_* */
124 union {
125 struct {
126 enum http_meth_t meth; /* method */
127 } req;
128 struct {
129 uint16_t status; /* status code */
130 } res;
131 } info;
132
133 /* XXX 2 bytes unused */
134
Christopher Faulet05c083c2019-05-15 14:56:47 +0200135 int32_t hdrs_bytes; /* Bytes held by all headers from this start-line
136 * to the corresponding EOH. -1 if unknown */
137
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100138 unsigned int len[3]; /* length of differnt parts of the start-line */
139 char l[0];
140};
141
142/* Internal representation of an HTTP message */
143struct htx {
144 uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */
145 uint32_t data; /* the data size, in bytes. To known to total size used by all allocated
146 * blocks (blocks and their contents), you need to add size used by blocks,
147 * i.e. [ used * sizeof(struct htx_blk *) ] */
148
149 uint32_t used; /* number of blocks in use */
150 uint32_t tail; /* last inserted block */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200151 uint32_t head; /* older inserted block */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100152 uint32_t front; /* block's position of the first content before the blocks table */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100153
154 uint64_t extra; /* known bytes amount remaining to receive */
155 uint32_t flags; /* HTX_FL_* */
156
Christopher Faulet29f17582019-05-23 11:03:26 +0200157 int32_t first; /* position of the first block to (re)start the analyse. -1 if unset. */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100158
159 struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
160};
161
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200162
163extern struct htx htx_empty;
164
165struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
166struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
167struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100168void htx_truncate(struct htx *htx, uint32_t offset);
Christopher Faulet549822f2019-02-25 10:23:19 +0100169struct htx_ret htx_drain(struct htx *htx, uint32_t max);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200170
171struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100172 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200173struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
174 enum htx_blk_type mark);
175
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
177 const struct ist p1, const struct ist p2, const struct ist p3);
178struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
179 const struct ist p2, const struct ist p3);
180
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200181struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100182 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200183
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200184struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Willy Tarreau52610e92019-01-03 18:26:17 +0100185struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200186struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200187struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
188struct htx_blk *htx_add_data(struct htx *htx, const struct ist data);
189struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100190struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, const struct ist data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200191
Christopher Fauletc59ff232018-12-03 13:58:44 +0100192int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
193int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
194int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
195int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
196int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200197
Christopher Faulet570d1612018-11-26 11:13:57 +0100198/* Functions and macros to get parts of the start-line or legnth of these
199 * parts
200 */
201#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
202
203#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
204#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
205#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
206#define HTX_SL_P1_PTR(sl) ((sl)->l)
207#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
208#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
209
210#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
211#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
212#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
213#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
214#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
215#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
216
217#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
218#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
219#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
220#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
221#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
222#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
223
Willy Tarreau8de1df92019-04-15 21:27:18 +0200224static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100225{
226 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
227}
Christopher Faulet570d1612018-11-26 11:13:57 +0100228
Willy Tarreau8de1df92019-04-15 21:27:18 +0200229static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100230{
231 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
232}
233
Willy Tarreau8de1df92019-04-15 21:27:18 +0200234static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100235{
236 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
237}
238
Willy Tarreau8de1df92019-04-15 21:27:18 +0200239static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100240{
241 return htx_sl_p1(sl);
242}
243
Willy Tarreau8de1df92019-04-15 21:27:18 +0200244static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100245{
246 return htx_sl_p2(sl);
247}
248
Willy Tarreau8de1df92019-04-15 21:27:18 +0200249static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100250{
251 return htx_sl_p3(sl);
252}
253
254
Willy Tarreau8de1df92019-04-15 21:27:18 +0200255static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100256{
257 return htx_sl_p1(sl);
258}
259
Willy Tarreau8de1df92019-04-15 21:27:18 +0200260static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100261{
262 return htx_sl_p2(sl);
263}
264
Willy Tarreau8de1df92019-04-15 21:27:18 +0200265static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100266{
267 return htx_sl_p3(sl);
268}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200269
270/* Returns the array index of a block given its position <pos> */
271static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
272{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100273 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200274}
275
276/* Returns the position of the block <blk> */
277static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
278{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100279 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200280}
281
282/* Returns the block at the position <pos> */
283static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
284{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100285 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200286}
287
288/* Returns the type of the block <blk> */
289static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
290{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100291 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200292}
293
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200294/* Returns the size of the block <blk>, depending of its type */
295static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
296{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100297 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200298
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100299 switch (type) {
300 case HTX_BLK_HDR:
301 /* name.length + value.length */
302 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100303 default:
304 /* value.length */
305 return (blk->info & 0xfffffff);
306 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200307}
308
Christopher Faulet28f29c72019-04-30 17:55:45 +0200309/* Returns the wrap position, ie the position where the blocks table wraps.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200310 *
311 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
312 * store on unsigned 32-bits integer, but it is impossible to have so much
313 * blocks to overflow a 32-bits signed integer !
314 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200315static inline int32_t htx_get_wrap(const struct htx *htx)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200316{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100317 if (!htx->used)
318 return -1;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200319 return ((htx->tail >= htx->head)
320 ? (htx->used + htx->head)
321 : (htx->used - 1) + (htx->head - htx->tail));
322}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200323
Christopher Faulet28f29c72019-04-30 17:55:45 +0200324/* Returns the position of the oldest entry (head).
325 *
326 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
327 * store on unsigned 32-bits integer, but it is impossible to have so much
328 * blocks to overflow a 32-bits signed integer !
329 */
330static inline int32_t htx_get_head(const struct htx *htx)
331{
332 return (htx->used ? htx->head : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200333}
334
335/* Returns the oldest HTX block (head) if the HTX message is not
336 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100337 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200338static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
339{
340 int32_t head = htx_get_head(htx);
341
342 return ((head == -1) ? NULL : htx_get_blk(htx, head));
343}
344
345/* Returns the type of the oldest HTX block (head) if the HTX message is not
346 * empty. Otherwise it returns HTX_BLK_UNUSED.
347 */
348static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
349{
350 struct htx_blk *blk = htx_get_head_blk(htx);
351
352 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
353}
354
355/* Returns the position of the newest entry (tail).
356 *
357 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
358 * store on unsigned 32-bits integer, but it is impossible to have so much
359 * blocks to overflow a 32-bits signed integer !
360 */
361static inline int32_t htx_get_tail(const struct htx *htx)
362{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100363 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200364}
365
366/* Returns the newest HTX block (tail) if the HTX message is not
367 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100368 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200369static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
370{
371 int32_t tail = htx_get_tail(htx);
372
373 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
374}
375
376/* Returns the type of the newest HTX block (tail) if the HTX message is not
377 * empty. Otherwise it returns HTX_BLK_UNUSED.
378 */
379static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
380{
381 struct htx_blk *blk = htx_get_tail_blk(htx);
382
383 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
384}
385
Christopher Faulet29f17582019-05-23 11:03:26 +0200386/* Returns the position of the first block in the HTX message <htx>. If unset,
387 * or if <htx> is empty, -1 is returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200388 *
389 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
390 * store on unsigned 32-bits integer, but it is impossible to have so much
391 * blocks to overflow a 32-bits signed integer !
392 */
393static inline int32_t htx_get_first(const struct htx *htx)
394{
Christopher Faulet29f17582019-05-23 11:03:26 +0200395 if (!htx->used)
396 return -1;
397 return htx->first;
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200398}
399
Christopher Faulet29f17582019-05-23 11:03:26 +0200400/* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
401 * empty, NULL returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200402 */
403static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
404{
405 int32_t pos;
406
407 pos = htx_get_first(htx);
408 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
409}
410
411/* Returns the type of the first block in the HTX message <htx>. If unset or if
412 * <htx> is empty, HTX_BLK_UNUSED is returned.
413 */
414static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
415{
416 struct htx_blk *blk = htx_get_first_blk(htx);
417
418 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
419}
420
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800421/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200422 * the message is empty or if <pos> is the position of the head, -1 returned.
423 *
424 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
425 * store on unsigned 32-bits integer, but it is impossible to have so much
426 * blocks to overflow a 32-bits signed integer !
427 */
428static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
429{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100430 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200431
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100432 head = htx_get_head(htx);
433 if (head == -1 || pos == head)
434 return -1;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200435 if (!pos) {
436 /* htx_get_wrap() is always greater than 1 here */
437 return (htx_get_wrap(htx) - 1);
438 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100439 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200440}
441
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100442/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
443 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100444 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100445static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
446 const struct htx_blk *blk)
447{
448 int32_t pos;
449
450 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
451 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
452}
453
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800454/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200455 * the message is empty or if <pos> is the position of the tail, -1 returned.
456 *
457 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
458 * store on unsigned 32-bits integer, but it is impossible to have so much
459 * blocks to overflow a 32-bits signed integer !
460 */
461static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
462{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200463 if (!htx->used || pos == htx->tail)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100464 return -1;
465 pos++;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200466 if (pos == htx_get_wrap(htx))
467 return 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100468 return pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200469
470}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100471
472/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
473 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100474 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100475static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
476 const struct htx_blk *blk)
477{
478 int32_t pos;
479
480 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
481 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
482}
483
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200484static inline int32_t htx_find_front(const struct htx *htx)
485{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100486 int32_t front, pos;
487 uint32_t addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200488
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 if (!htx->used)
490 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200491
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100492 front = -1;
493 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
494 struct htx_blk *blk = htx_get_blk(htx, pos);
495 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200496
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100497 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
498 front = pos;
499 addr = blk->addr;
500 }
501 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200502
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100503 return front;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200504}
505
506/* Changes the size of the value. It is the caller responsibility to change the
507 * value itself, make sure there is enough space and update allocated value.
508 */
509static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
510{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100511 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 switch (type) {
514 case HTX_BLK_HDR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100515 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
516 break;
517 case HTX_BLK_REQ_SL:
518 case HTX_BLK_RES_SL:
519 case HTX_BLK_DATA:
520 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100521 blk->info = (type << 28) + vlen;
522 break;
523 default:
524 /* Unexpected case */
525 break;
526 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200527}
528
529/* Returns the data pointer of the block <blk> */
530static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
531{
532 return ((void *)htx->blocks + blk->addr);
533}
534
535/* Returns the name of the block <blk>, only if it is a header. Otherwise it
536 * returns an empty name.
537 */
538static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
539{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100540 enum htx_blk_type type = htx_get_blk_type(blk);
541 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100543 switch (type) {
544 case HTX_BLK_HDR:
545 ret.ptr = htx_get_blk_ptr(htx, blk);
546 ret.len = blk->info & 0xff;
547 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200548
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100549 default:
550 return ist("");
551 }
552 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200553}
554
Christopher Faulet54483df2018-11-26 15:05:52 +0100555
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556/* Returns the value of the block <blk>, depending on its type. If there is no
557 * value, an empty one is retruned.
558 */
559static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
560{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100561 enum htx_blk_type type = htx_get_blk_type(blk);
562 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200563
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100564 switch (type) {
565 case HTX_BLK_HDR:
566 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
567 ret.len = (blk->info >> 8) & 0xfffff;
568 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200569
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100570 case HTX_BLK_REQ_SL:
571 case HTX_BLK_RES_SL:
572 case HTX_BLK_DATA:
573 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 ret.ptr = htx_get_blk_ptr(htx, blk);
575 ret.len = blk->info & 0xfffffff;
576 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200577
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 default:
579 return ist("");
580 }
581 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200582}
583
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100584/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
585 * address and its length are adjusted, and the htx's total data count is
586 * updated. This is used to mark that part of some data were transfered
587 * from a DATA block without removing this DATA block. No sanity check is
588 * performed, the caller is reponsible for doing this exclusively on DATA
589 * blocks, and never removing more than the block's size.
590 */
591static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
592{
593 blk->addr += n;
594 blk->info -= n;
595 htx->data -= n;
596}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200597
598/* Returns the space used by metadata in <htx>. */
599static inline uint32_t htx_meta_space(const struct htx *htx)
600{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200602}
603
604/* Returns the space used (data + metadata) in <htx> */
605static inline uint32_t htx_used_space(const struct htx *htx)
606{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100607 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608}
609
610/* Returns the free space in <htx> */
611static inline uint32_t htx_free_space(const struct htx *htx)
612{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100613 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614}
615
Christopher Faulet8564c1f2019-01-07 13:48:55 +0100616/* Returns the maximum space usable for data in <htx>. This is in fact the
617 * maximum sice for a uniq block to fill the HTX message. */
618static inline uint32_t htx_max_data_space(const struct htx *htx)
619{
620 if (!htx->size)
621 return 0;
622 return (htx->size - sizeof(htx->blocks[0]));
623}
624
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625/* Returns the maximum size available to store some data in <htx> if a new block
626 * is reserved.
627 */
628static inline uint32_t htx_free_data_space(const struct htx *htx)
629{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100630 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100632 if (free < sizeof(htx->blocks[0]))
633 return 0;
634 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200635}
636
Christopher Faulet2ae35042019-05-16 11:16:39 +0200637/* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
638 * set to -1 to have no limit.
639 */
640static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
641{
642 uint32_t free = htx_free_space(htx);
643
644 if (max != -1 && free > max)
645 free = max;
646 if (free < sizeof(htx->blocks[0]))
647 return 0;
648 return (free - sizeof(htx->blocks[0]));
649}
650
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200651/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
652static inline int htx_almost_full(const struct htx *htx)
653{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100654 if (!htx->size || htx_free_space(htx) < htx->size / 4)
655 return 1;
656 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200657}
658
659static inline void htx_reset(struct htx *htx)
660{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200661 htx->data = htx->used = htx->tail = htx->head = htx->front = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200662 htx->extra = 0;
663 htx->flags = HTX_FL_NONE;
Christopher Faulet29f17582019-05-23 11:03:26 +0200664 htx->first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665}
666
Willy Tarreau3906e222018-12-05 07:56:25 +0100667/* returns the available room for raw data in buffer <buf> once HTX overhead is
668 * taken into account (one HTX header and two blocks). The purpose is to figure
669 * the optimal fill length to avoid copies.
670 */
671static inline size_t buf_room_for_htx_data(const struct buffer *buf)
672{
673 size_t room;
674
675 room = b_room(buf);
676 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
677 room = 0;
678 else
679 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
680
681 return room;
682}
683
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100684
685/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Willy Tarreau245d1892019-01-31 07:21:42 +0100686 * function does not update to the buffer.
687 * Note that it always returns a valid pointer, either to an initialized buffer
688 * or to the empty buffer.
689 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100690static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200691{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100692 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200693
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100694 if (b_is_null(buf))
695 return &htx_empty;
696 htx = ((struct htx *)(buf->area));
697 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100698 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100699 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100700 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200701 return htx;
702}
703
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100704/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
705 * full. It is the caller responsibility to call htx_to_buf() when it finish to
Willy Tarreau245d1892019-01-31 07:21:42 +0100706 * manipulate the HTX message to update <buf> accordingly. The returned pointer
707 * is always valid.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100708 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100709 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100710 */
711static inline struct htx *htx_from_buf(struct buffer *buf)
712{
713 struct htx *htx = htxbuf(buf);
714
715 b_set_data(buf, b_size(buf));
716 return htx;
717}
718
719/* Upate <buf> accordingly to the HTX message <htx> */
720static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
721{
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200722 if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100723 htx_reset(htx);
724 b_set_data(buf, 0);
725 }
726 else
727 b_set_data(buf, b_size(buf));
728}
729
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100730/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
731 * illegal to call this with htx == NULL.
732 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733static inline int htx_is_empty(const struct htx *htx)
734{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100735 return !htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736}
737
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100738/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
739 * is illegal to call this with htx == NULL.
740 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200741static inline int htx_is_not_empty(const struct htx *htx)
742{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100743 return htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200744}
745
746/* For debugging purpose */
747static inline const char *htx_blk_type_str(enum htx_blk_type type)
748{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100749 switch (type) {
750 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
751 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
752 case HTX_BLK_HDR: return "HTX_BLK_HDR";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100753 case HTX_BLK_EOH: return "HTX_BLK_EOH";
754 case HTX_BLK_DATA: return "HTX_BLK_DATA";
755 case HTX_BLK_EOD: return "HTX_BLK_EOD";
756 case HTX_BLK_TLR: return "HTX_BLK_TLR";
757 case HTX_BLK_EOM: return "HTX_BLK_EOM";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100758 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
759 default: return "HTX_BLK_???";
760 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200761}
762
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200763static inline void htx_dump(struct htx *htx)
764{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100765 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200766
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100767 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Faulet28f29c72019-04-30 17:55:45 +0200768 htx, htx->size, htx->data, htx->used, (htx->tail >= htx->head) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100769 (unsigned long long)htx->extra);
Christopher Faulet29f17582019-05-23 11:03:26 +0200770 fprintf(stderr, "\tfirst=%d - head=%u, tail=%u - front=%u\n",
771 htx->first, htx->head, htx->tail, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100773 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100774 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100775 struct htx_blk *blk = htx_get_blk(htx, pos);
776 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100777 uint32_t sz = htx_get_blksz(blk);
778 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200779
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100780 n = htx_get_blk_name(htx, blk);
781 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200782
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100783 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200784 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100785 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
786 pos, htx_blk_type_str(type), sz, blk->addr,
787 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
788 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
789 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200790 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100791 else if (type == HTX_BLK_HDR)
792 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
793 pos, htx_blk_type_str(type), sz, blk->addr,
794 (int)n.len, n.ptr,
795 (int)v.len, v.ptr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100796 else
797 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
798 pos, htx_blk_type_str(type), sz, blk->addr,
799 (!v.len ? "\t<empty>" : ""));
800 }
801 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200802}
803
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100804#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805
806/*
807 * Local variables:
808 * c-indent-level: 8
809 * c-basic-offset: 8
810 * End:
811 */