blob: cd7db0526e48120ff023d2be448a10fb1d54e79a [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
97/* Pseudo header types (max 255). */
98enum htx_phdr_type {
99 HTX_PHDR_UNKNOWN = 0,
100 HTX_PHDR_SIZE,
101};
102
103/* HTTP block's type (max 15). */
104enum htx_blk_type {
105 HTX_BLK_REQ_SL = 0, /* Request start-line */
106 HTX_BLK_RES_SL = 1, /* Response start-line */
107 HTX_BLK_HDR = 2, /* header name/value block */
108 HTX_BLK_PHDR = 3, /* pseudo header block */
109 HTX_BLK_EOH = 4, /* end-of-headers block */
110 HTX_BLK_DATA = 5, /* data block */
111 HTX_BLK_EOD = 6, /* end-of-data block */
112 HTX_BLK_TLR = 7, /* trailer name/value block */
113 HTX_BLK_EOM = 8, /* end-of-message block */
114 /* 9 .. 13 unused */
115 HTX_BLK_OOB = 14, /* Out of band block, don't alter the parser */
116 HTX_BLK_UNUSED = 15, /* unused/removed block */
117};
118
119/* One HTTP block descriptor */
120struct htx_blk {
121 uint32_t addr; /* relative storage address of a data block */
122 uint32_t info; /* information about data stored */
123};
124
125struct htx_ret {
126 int32_t ret;
127 struct htx_blk *blk;
128};
129
130struct htx_sl {
131 unsigned int flags; /* HTX_SL_F_* */
132 union {
133 struct {
134 enum http_meth_t meth; /* method */
135 } req;
136 struct {
137 uint16_t status; /* status code */
138 } res;
139 } info;
140
141 /* XXX 2 bytes unused */
142
143 unsigned int len[3]; /* length of differnt parts of the start-line */
144 char l[0];
145};
146
147/* Internal representation of an HTTP message */
148struct htx {
149 uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */
150 uint32_t data; /* the data size, in bytes. To known to total size used by all allocated
151 * blocks (blocks and their contents), you need to add size used by blocks,
152 * i.e. [ used * sizeof(struct htx_blk *) ] */
153
154 uint32_t used; /* number of blocks in use */
155 uint32_t tail; /* last inserted block */
156 uint32_t front; /* block's position of the first content before the blocks table */
157 uint32_t wrap; /* the position were the blocks table wraps, if any */
158
159 uint64_t extra; /* known bytes amount remaining to receive */
160 uint32_t flags; /* HTX_FL_* */
161
162 int32_t sl_off; /* Offset of the start-line of the HTTP message relatively to the beginning the
163 data block. -1 if unset */
164
165 struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
166};
167
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200168
169extern struct htx htx_empty;
170
171struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
172struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
173struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100174void htx_truncate(struct htx *htx, uint32_t offset);
Christopher Faulet549822f2019-02-25 10:23:19 +0100175struct htx_ret htx_drain(struct htx *htx, uint32_t max);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200176
177struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100178 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200179struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
180 enum htx_blk_type mark);
181
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100182struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
183 const struct ist p1, const struct ist p2, const struct ist p3);
184struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
185 const struct ist p2, const struct ist p3);
186
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200187struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100188 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200189
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200190struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Willy Tarreau52610e92019-01-03 18:26:17 +0100191struct 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 +0200192struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
193struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr, const struct ist value);
194struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
195struct htx_blk *htx_add_data(struct htx *htx, const struct ist data);
196struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr);
197struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100198struct 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 +0200199
Christopher Fauletc59ff232018-12-03 13:58:44 +0100200int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
201int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
202int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
203int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
204int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200205
Christopher Faulet570d1612018-11-26 11:13:57 +0100206/* Functions and macros to get parts of the start-line or legnth of these
207 * parts
208 */
209#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
210
211#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
212#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
213#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
214#define HTX_SL_P1_PTR(sl) ((sl)->l)
215#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
216#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
217
218#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
219#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
220#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
221#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
222#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
223#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
224
225#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
226#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
227#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
228#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
229#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
230#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
231
Willy Tarreau8de1df92019-04-15 21:27:18 +0200232static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100233{
234 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
235}
Christopher Faulet570d1612018-11-26 11:13:57 +0100236
Willy Tarreau8de1df92019-04-15 21:27:18 +0200237static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100238{
239 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
240}
241
Willy Tarreau8de1df92019-04-15 21:27:18 +0200242static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100243{
244 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
245}
246
Willy Tarreau8de1df92019-04-15 21:27:18 +0200247static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100248{
249 return htx_sl_p1(sl);
250}
251
Willy Tarreau8de1df92019-04-15 21:27:18 +0200252static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100253{
254 return htx_sl_p2(sl);
255}
256
Willy Tarreau8de1df92019-04-15 21:27:18 +0200257static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100258{
259 return htx_sl_p3(sl);
260}
261
262
Willy Tarreau8de1df92019-04-15 21:27:18 +0200263static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100264{
265 return htx_sl_p1(sl);
266}
267
Willy Tarreau8de1df92019-04-15 21:27:18 +0200268static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100269{
270 return htx_sl_p2(sl);
271}
272
Willy Tarreau8de1df92019-04-15 21:27:18 +0200273static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100274{
275 return htx_sl_p3(sl);
276}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200277
Christopher Faulet54483df2018-11-26 15:05:52 +0100278/* Returns the HTX start-line if set, otherwise it returns NULL. */
279static inline struct htx_sl *htx_get_stline(struct htx *htx)
280{
281 struct htx_sl *sl = NULL;
282
Christopher Faulet3a4d1be2019-04-17 15:08:42 +0200283 if (htx->used && htx->sl_off != -1)
Christopher Faulet54483df2018-11-26 15:05:52 +0100284 sl = ((void *)htx->blocks + htx->sl_off);
285
286 return sl;
287}
288
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200289/* Returns the array index of a block given its position <pos> */
290static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
291{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100292 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200293}
294
295/* Returns the position of the block <blk> */
296static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
297{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100298 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200299}
300
301/* Returns the block at the position <pos> */
302static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
303{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100304 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200305}
306
307/* Returns the type of the block <blk> */
308static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
309{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100310 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200311}
312
313/* Returns the pseudo-header type of the block <blk>. If it's not a
314 * pseudo-header, HTX_PHDR_UNKNOWN is returned.
315 */
316static inline enum htx_phdr_type htx_get_blk_phdr(const struct htx_blk *blk)
317{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100318 enum htx_blk_type type = htx_get_blk_type(blk);
319 enum htx_phdr_type phdr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200320
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100321 switch (type) {
322 case HTX_BLK_PHDR:
323 phdr = (blk->info & 0xff);
324 return phdr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200325
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100326 default:
327 /* Not a pseudo-header */
328 return HTX_PHDR_UNKNOWN;
329 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200330}
331
332/* Returns the size of the block <blk>, depending of its type */
333static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
334{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100335 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200336
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100337 switch (type) {
338 case HTX_BLK_HDR:
339 /* name.length + value.length */
340 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
341 case HTX_BLK_PHDR:
342 /* value.length */
343 return ((blk->info >> 8) & 0xfffff);
344 default:
345 /* value.length */
346 return (blk->info & 0xfffffff);
347 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200348}
349
350/* Returns the position of the oldest entry (head).
351 *
352 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
353 * store on unsigned 32-bits integer, but it is impossible to have so much
354 * blocks to overflow a 32-bits signed integer !
355 */
356static inline int32_t htx_get_head(const struct htx *htx)
357{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100358 if (!htx->used)
359 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200360
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100361 return (((htx->tail + 1U < htx->used) ? htx->wrap : 0) + htx->tail + 1U - htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200362}
363
364/* Returns the oldest HTX block (head) if the HTX message is not
365 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100366 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200367static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
368{
369 int32_t head = htx_get_head(htx);
370
371 return ((head == -1) ? NULL : htx_get_blk(htx, head));
372}
373
374/* Returns the type of the oldest HTX block (head) if the HTX message is not
375 * empty. Otherwise it returns HTX_BLK_UNUSED.
376 */
377static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
378{
379 struct htx_blk *blk = htx_get_head_blk(htx);
380
381 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
382}
383
384/* Returns the position of the newest entry (tail).
385 *
386 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
387 * store on unsigned 32-bits integer, but it is impossible to have so much
388 * blocks to overflow a 32-bits signed integer !
389 */
390static inline int32_t htx_get_tail(const struct htx *htx)
391{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100392 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200393}
394
395/* Returns the newest HTX block (tail) if the HTX message is not
396 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100397 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200398static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
399{
400 int32_t tail = htx_get_tail(htx);
401
402 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
403}
404
405/* Returns the type of the newest HTX block (tail) if the HTX message is not
406 * empty. Otherwise it returns HTX_BLK_UNUSED.
407 */
408static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
409{
410 struct htx_blk *blk = htx_get_tail_blk(htx);
411
412 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
413}
414
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800415/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200416 * the message is empty or if <pos> is the position of the head, -1 returned.
417 *
418 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
419 * store on unsigned 32-bits integer, but it is impossible to have so much
420 * blocks to overflow a 32-bits signed integer !
421 */
422static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
423{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100424 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200425
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100426 head = htx_get_head(htx);
427 if (head == -1 || pos == head)
428 return -1;
429 if (!pos)
430 return (htx->wrap - 1);
431 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200432}
433
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100434/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
435 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100436 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100437static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
438 const struct htx_blk *blk)
439{
440 int32_t pos;
441
442 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
443 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
444}
445
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800446/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200447 * the message is empty or if <pos> is the position of the tail, -1 returned.
448 *
449 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
450 * store on unsigned 32-bits integer, but it is impossible to have so much
451 * blocks to overflow a 32-bits signed integer !
452 */
453static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
454{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100455 if (!htx->used)
456 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200457
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100458 if (pos == htx->tail)
459 return -1;
460 pos++;
461 if (pos >= htx->wrap)
462 pos = 0;
463 return pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200464
465}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100466
467/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
468 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100469 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100470static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
471 const struct htx_blk *blk)
472{
473 int32_t pos;
474
475 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
476 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
477}
478
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200479static inline int32_t htx_find_front(const struct htx *htx)
480{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100481 int32_t front, pos;
482 uint32_t addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200483
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100484 if (!htx->used)
485 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200486
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100487 front = -1;
488 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
489 struct htx_blk *blk = htx_get_blk(htx, pos);
490 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200491
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100492 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
493 front = pos;
494 addr = blk->addr;
495 }
496 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200497
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100498 return front;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200499}
500
Christopher Faulet14e88252018-11-22 11:28:18 +0100501/* Returns the HTX block containing data with the <offset>, relatively to the
502 * beginning of the HTX message <htx>. It returns an htx_ret. if the HTX block is
503 * not found, htx_ret.blk is set to NULL. Otherwise, it points to the right HTX
504 * block and htx_ret.ret is set to the remaining offset inside the block.
505 */
506static inline struct htx_ret htx_find_blk(const struct htx *htx, uint32_t offset)
507{
508 int32_t pos;
509
510 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
511 struct htx_blk *blk = htx_get_blk(htx, pos);
512 uint32_t sz = htx_get_blksz(blk);
513
514 if (offset < sz)
515 return (struct htx_ret){ .blk = blk, .ret = offset };
516 offset -= sz;
517 }
518
519 return (struct htx_ret){ .blk = NULL };
520}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200521/* Changes the size of the value. It is the caller responsibility to change the
522 * value itself, make sure there is enough space and update allocated value.
523 */
524static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
525{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100526 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200527
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100528 switch (type) {
529 case HTX_BLK_HDR:
530 case HTX_BLK_PHDR:
531 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
532 break;
533 case HTX_BLK_REQ_SL:
534 case HTX_BLK_RES_SL:
535 case HTX_BLK_DATA:
536 case HTX_BLK_TLR:
537 case HTX_BLK_OOB:
538 blk->info = (type << 28) + vlen;
539 break;
540 default:
541 /* Unexpected case */
542 break;
543 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200544}
545
546/* Returns the data pointer of the block <blk> */
547static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
548{
549 return ((void *)htx->blocks + blk->addr);
550}
551
552/* Returns the name of the block <blk>, only if it is a header. Otherwise it
553 * returns an empty name.
554 */
555static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
556{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100557 enum htx_blk_type type = htx_get_blk_type(blk);
558 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200559
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100560 switch (type) {
561 case HTX_BLK_HDR:
562 ret.ptr = htx_get_blk_ptr(htx, blk);
563 ret.len = blk->info & 0xff;
564 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200565
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100566 default:
567 return ist("");
568 }
569 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200570}
571
Christopher Faulet54483df2018-11-26 15:05:52 +0100572
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200573/* Returns the value of the block <blk>, depending on its type. If there is no
574 * value, an empty one is retruned.
575 */
576static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
577{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 enum htx_blk_type type = htx_get_blk_type(blk);
579 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200580
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100581 switch (type) {
582 case HTX_BLK_HDR:
583 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
584 ret.len = (blk->info >> 8) & 0xfffff;
585 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100587 case HTX_BLK_PHDR:
588 ret.ptr = htx_get_blk_ptr(htx, blk);
589 ret.len = (blk->info >> 8) & 0xfffff;
590 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200591
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100592 case HTX_BLK_REQ_SL:
593 case HTX_BLK_RES_SL:
594 case HTX_BLK_DATA:
595 case HTX_BLK_TLR:
596 case HTX_BLK_OOB:
597 ret.ptr = htx_get_blk_ptr(htx, blk);
598 ret.len = blk->info & 0xfffffff;
599 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 default:
602 return ist("");
603 }
604 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200605}
606
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100607/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
608 * address and its length are adjusted, and the htx's total data count is
609 * updated. This is used to mark that part of some data were transfered
610 * from a DATA block without removing this DATA block. No sanity check is
611 * performed, the caller is reponsible for doing this exclusively on DATA
612 * blocks, and never removing more than the block's size.
613 */
614static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
615{
616 blk->addr += n;
617 blk->info -= n;
618 htx->data -= n;
619}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620
621/* Returns the space used by metadata in <htx>. */
622static inline uint32_t htx_meta_space(const struct htx *htx)
623{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100624 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625}
626
627/* Returns the space used (data + metadata) in <htx> */
628static inline uint32_t htx_used_space(const struct htx *htx)
629{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100630 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631}
632
633/* Returns the free space in <htx> */
634static inline uint32_t htx_free_space(const struct htx *htx)
635{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100636 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637}
638
Christopher Faulet8564c1f2019-01-07 13:48:55 +0100639/* Returns the maximum space usable for data in <htx>. This is in fact the
640 * maximum sice for a uniq block to fill the HTX message. */
641static inline uint32_t htx_max_data_space(const struct htx *htx)
642{
643 if (!htx->size)
644 return 0;
645 return (htx->size - sizeof(htx->blocks[0]));
646}
647
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200648/* Returns the maximum size available to store some data in <htx> if a new block
649 * is reserved.
650 */
651static inline uint32_t htx_free_data_space(const struct htx *htx)
652{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100653 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200654
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100655 if (free < sizeof(htx->blocks[0]))
656 return 0;
657 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200658}
659
660/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
661static inline int htx_almost_full(const struct htx *htx)
662{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100663 if (!htx->size || htx_free_space(htx) < htx->size / 4)
664 return 1;
665 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200666}
667
668static inline void htx_reset(struct htx *htx)
669{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100670 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671 htx->extra = 0;
672 htx->flags = HTX_FL_NONE;
Christopher Faulet54483df2018-11-26 15:05:52 +0100673 htx->sl_off = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200674}
675
Willy Tarreau3906e222018-12-05 07:56:25 +0100676/* returns the available room for raw data in buffer <buf> once HTX overhead is
677 * taken into account (one HTX header and two blocks). The purpose is to figure
678 * the optimal fill length to avoid copies.
679 */
680static inline size_t buf_room_for_htx_data(const struct buffer *buf)
681{
682 size_t room;
683
684 room = b_room(buf);
685 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
686 room = 0;
687 else
688 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
689
690 return room;
691}
692
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100693
694/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Willy Tarreau245d1892019-01-31 07:21:42 +0100695 * function does not update to the buffer.
696 * Note that it always returns a valid pointer, either to an initialized buffer
697 * or to the empty buffer.
698 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100699static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100701 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200702
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100703 if (b_is_null(buf))
704 return &htx_empty;
705 htx = ((struct htx *)(buf->area));
706 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100707 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100708 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100709 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200710 return htx;
711}
712
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100713/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
714 * full. It is the caller responsibility to call htx_to_buf() when it finish to
Willy Tarreau245d1892019-01-31 07:21:42 +0100715 * manipulate the HTX message to update <buf> accordingly. The returned pointer
716 * is always valid.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100717 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100718 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100719 */
720static inline struct htx *htx_from_buf(struct buffer *buf)
721{
722 struct htx *htx = htxbuf(buf);
723
724 b_set_data(buf, b_size(buf));
725 return htx;
726}
727
728/* Upate <buf> accordingly to the HTX message <htx> */
729static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
730{
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200731 if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100732 htx_reset(htx);
733 b_set_data(buf, 0);
734 }
735 else
736 b_set_data(buf, b_size(buf));
737}
738
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100739/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
740 * illegal to call this with htx == NULL.
741 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200742static inline int htx_is_empty(const struct htx *htx)
743{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100744 return !htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200745}
746
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100747/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
748 * is illegal to call this with htx == NULL.
749 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200750static inline int htx_is_not_empty(const struct htx *htx)
751{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100752 return htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753}
754
755/* For debugging purpose */
756static inline const char *htx_blk_type_str(enum htx_blk_type type)
757{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100758 switch (type) {
759 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
760 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
761 case HTX_BLK_HDR: return "HTX_BLK_HDR";
762 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
763 case HTX_BLK_EOH: return "HTX_BLK_EOH";
764 case HTX_BLK_DATA: return "HTX_BLK_DATA";
765 case HTX_BLK_EOD: return "HTX_BLK_EOD";
766 case HTX_BLK_TLR: return "HTX_BLK_TLR";
767 case HTX_BLK_EOM: return "HTX_BLK_EOM";
768 case HTX_BLK_OOB: return "HTX_BLK_OOB";
769 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
770 default: return "HTX_BLK_???";
771 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772}
773
774static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
775{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100776 switch (phdr) {
777 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
778 default: return "HTX_PHDR_???";
779 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200780}
781
782static inline void htx_dump(struct htx *htx)
783{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100784 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200785
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100786 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
787 htx, htx->size, htx->data, htx->used,
788 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100789 (unsigned long long)htx->extra);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100790 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
791 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200792
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100793 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100794 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100795 struct htx_blk *blk = htx_get_blk(htx, pos);
796 enum htx_blk_type type = htx_get_blk_type(blk);
797 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
798 uint32_t sz = htx_get_blksz(blk);
799 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200800
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100801 n = htx_get_blk_name(htx, blk);
802 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100804 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100806 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
807 pos, htx_blk_type_str(type), sz, blk->addr,
808 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
809 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
810 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100812 else if (type == HTX_BLK_HDR)
813 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
814 pos, htx_blk_type_str(type), sz, blk->addr,
815 (int)n.len, n.ptr,
816 (int)v.len, v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100818 else if (type == HTX_BLK_PHDR)
819 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
820 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
821 (int)v.len, v.ptr);
822 else
823 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
824 pos, htx_blk_type_str(type), sz, blk->addr,
825 (!v.len ? "\t<empty>" : ""));
826 }
827 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200828}
829
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100830#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831
832/*
833 * Local variables:
834 * c-indent-level: 8
835 * c-basic-offset: 8
836 * End:
837 */