blob: e2d4e15a116473a50912861a59718a767d4a5c15 [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 */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200106 HTX_BLK_EOT = 7, /* end-of-trailers block */
107 HTX_BLK_EOM = 8, /* end-of-message block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200108 /* 8 .. 14 unused */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100109 HTX_BLK_UNUSED = 15, /* unused/removed block */
110};
111
112/* One HTTP block descriptor */
113struct htx_blk {
114 uint32_t addr; /* relative storage address of a data block */
115 uint32_t info; /* information about data stored */
116};
117
118struct htx_ret {
119 int32_t ret;
120 struct htx_blk *blk;
121};
122
123struct htx_sl {
124 unsigned int flags; /* HTX_SL_F_* */
125 union {
126 struct {
127 enum http_meth_t meth; /* method */
128 } req;
129 struct {
130 uint16_t status; /* status code */
131 } res;
132 } info;
133
134 /* XXX 2 bytes unused */
135
Christopher Faulet05c083c2019-05-15 14:56:47 +0200136 int32_t hdrs_bytes; /* Bytes held by all headers from this start-line
137 * to the corresponding EOH. -1 if unknown */
138
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100139 unsigned int len[3]; /* length of differnt parts of the start-line */
140 char l[0];
141};
142
143/* Internal representation of an HTTP message */
144struct htx {
145 uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */
146 uint32_t data; /* the data size, in bytes. To known to total size used by all allocated
147 * blocks (blocks and their contents), you need to add size used by blocks,
148 * i.e. [ used * sizeof(struct htx_blk *) ] */
149
150 uint32_t used; /* number of blocks in use */
151 uint32_t tail; /* last inserted block */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200152 uint32_t head; /* older inserted block */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100153 uint32_t front; /* block's position of the first content before the blocks table */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100154
155 uint64_t extra; /* known bytes amount remaining to receive */
156 uint32_t flags; /* HTX_FL_* */
157
Christopher Faulet29f17582019-05-23 11:03:26 +0200158 int32_t first; /* position of the first block to (re)start the analyse. -1 if unset. */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100159
160 struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
161};
162
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200163
164extern struct htx htx_empty;
165
166struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
167struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
168struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100169void htx_truncate(struct htx *htx, uint32_t offset);
Christopher Faulet549822f2019-02-25 10:23:19 +0100170struct htx_ret htx_drain(struct htx *htx, uint32_t max);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200171
172struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100173 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200174struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
175 enum htx_blk_type mark);
176
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
178 const struct ist p1, const struct ist p2, const struct ist p3);
179struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
180 const struct ist p2, const struct ist p3);
181
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200182struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100183 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200184
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200185struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200186struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value);
Willy Tarreau52610e92019-01-03 18:26:17 +0100187struct 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 +0200188struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200189struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200190struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200191struct htx_blk *htx_add_data_atonce(struct htx *htx, const struct ist data);
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200192size_t htx_add_data(struct htx *htx, const struct ist data);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100193struct 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 +0200194
Christopher Fauletc59ff232018-12-03 13:58:44 +0100195int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
196int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
197int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
198int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200199
Christopher Faulet570d1612018-11-26 11:13:57 +0100200/* Functions and macros to get parts of the start-line or legnth of these
201 * parts
202 */
203#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
204
205#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
206#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
207#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
208#define HTX_SL_P1_PTR(sl) ((sl)->l)
209#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
210#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
211
212#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
213#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
214#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
215#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
216#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
217#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
218
219#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
220#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
221#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
222#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
223#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
224#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
225
Willy Tarreau8de1df92019-04-15 21:27:18 +0200226static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100227{
228 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
229}
Christopher Faulet570d1612018-11-26 11:13:57 +0100230
Willy Tarreau8de1df92019-04-15 21:27:18 +0200231static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100232{
233 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
234}
235
Willy Tarreau8de1df92019-04-15 21:27:18 +0200236static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100237{
238 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
239}
240
Willy Tarreau8de1df92019-04-15 21:27:18 +0200241static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100242{
243 return htx_sl_p1(sl);
244}
245
Willy Tarreau8de1df92019-04-15 21:27:18 +0200246static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100247{
248 return htx_sl_p2(sl);
249}
250
Willy Tarreau8de1df92019-04-15 21:27:18 +0200251static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100252{
253 return htx_sl_p3(sl);
254}
255
256
Willy Tarreau8de1df92019-04-15 21:27:18 +0200257static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100258{
259 return htx_sl_p1(sl);
260}
261
Willy Tarreau8de1df92019-04-15 21:27:18 +0200262static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100263{
264 return htx_sl_p2(sl);
265}
266
Willy Tarreau8de1df92019-04-15 21:27:18 +0200267static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100268{
269 return htx_sl_p3(sl);
270}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200271
272/* Returns the array index of a block given its position <pos> */
273static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
274{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100275 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200276}
277
278/* Returns the position of the block <blk> */
279static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
280{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100281 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200282}
283
284/* Returns the block at the position <pos> */
285static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
286{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100287 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200288}
289
290/* Returns the type of the block <blk> */
291static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
292{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100293 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200294}
295
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200296/* Returns the size of the block <blk>, depending of its type */
297static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
298{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100299 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200300
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100301 switch (type) {
302 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200303 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100304 /* name.length + value.length */
305 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100306 default:
307 /* value.length */
308 return (blk->info & 0xfffffff);
309 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200310}
311
Christopher Faulet28f29c72019-04-30 17:55:45 +0200312/* Returns the wrap position, ie the position where the blocks table wraps.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200313 *
314 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
315 * store on unsigned 32-bits integer, but it is impossible to have so much
316 * blocks to overflow a 32-bits signed integer !
317 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200318static inline int32_t htx_get_wrap(const struct htx *htx)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200319{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100320 if (!htx->used)
321 return -1;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200322 return ((htx->tail >= htx->head)
323 ? (htx->used + htx->head)
324 : (htx->used - 1) + (htx->head - htx->tail));
325}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200326
Christopher Faulet28f29c72019-04-30 17:55:45 +0200327/* Returns the position of the oldest entry (head).
328 *
329 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
330 * store on unsigned 32-bits integer, but it is impossible to have so much
331 * blocks to overflow a 32-bits signed integer !
332 */
333static inline int32_t htx_get_head(const struct htx *htx)
334{
335 return (htx->used ? htx->head : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200336}
337
338/* Returns the oldest HTX block (head) if the HTX message is not
339 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100340 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200341static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
342{
343 int32_t head = htx_get_head(htx);
344
345 return ((head == -1) ? NULL : htx_get_blk(htx, head));
346}
347
348/* Returns the type of the oldest HTX block (head) if the HTX message is not
349 * empty. Otherwise it returns HTX_BLK_UNUSED.
350 */
351static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
352{
353 struct htx_blk *blk = htx_get_head_blk(htx);
354
355 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
356}
357
358/* Returns the position of the newest entry (tail).
359 *
360 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
361 * store on unsigned 32-bits integer, but it is impossible to have so much
362 * blocks to overflow a 32-bits signed integer !
363 */
364static inline int32_t htx_get_tail(const struct htx *htx)
365{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100366 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200367}
368
369/* Returns the newest HTX block (tail) if the HTX message is not
370 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100371 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200372static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
373{
374 int32_t tail = htx_get_tail(htx);
375
376 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
377}
378
379/* Returns the type of the newest HTX block (tail) if the HTX message is not
380 * empty. Otherwise it returns HTX_BLK_UNUSED.
381 */
382static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
383{
384 struct htx_blk *blk = htx_get_tail_blk(htx);
385
386 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
387}
388
Christopher Faulet29f17582019-05-23 11:03:26 +0200389/* Returns the position of the first block in the HTX message <htx>. If unset,
390 * or if <htx> is empty, -1 is returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200391 *
392 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
393 * store on unsigned 32-bits integer, but it is impossible to have so much
394 * blocks to overflow a 32-bits signed integer !
395 */
396static inline int32_t htx_get_first(const struct htx *htx)
397{
Christopher Faulet29f17582019-05-23 11:03:26 +0200398 if (!htx->used)
399 return -1;
400 return htx->first;
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200401}
402
Christopher Faulet29f17582019-05-23 11:03:26 +0200403/* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
404 * empty, NULL returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200405 */
406static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
407{
408 int32_t pos;
409
410 pos = htx_get_first(htx);
411 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
412}
413
414/* Returns the type of the first block in the HTX message <htx>. If unset or if
415 * <htx> is empty, HTX_BLK_UNUSED is returned.
416 */
417static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
418{
419 struct htx_blk *blk = htx_get_first_blk(htx);
420
421 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
422}
423
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800424/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200425 * the message is empty or if <pos> is the position of the head, -1 returned.
426 *
427 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
428 * store on unsigned 32-bits integer, but it is impossible to have so much
429 * blocks to overflow a 32-bits signed integer !
430 */
431static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
432{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100433 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200434
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100435 head = htx_get_head(htx);
436 if (head == -1 || pos == head)
437 return -1;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200438 if (!pos) {
439 /* htx_get_wrap() is always greater than 1 here */
440 return (htx_get_wrap(htx) - 1);
441 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100442 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200443}
444
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100445/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
446 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100447 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100448static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
449 const struct htx_blk *blk)
450{
451 int32_t pos;
452
453 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
454 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
455}
456
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800457/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200458 * the message is empty or if <pos> is the position of the tail, -1 returned.
459 *
460 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
461 * store on unsigned 32-bits integer, but it is impossible to have so much
462 * blocks to overflow a 32-bits signed integer !
463 */
464static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
465{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200466 if (!htx->used || pos == htx->tail)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100467 return -1;
468 pos++;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200469 if (pos == htx_get_wrap(htx))
470 return 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100471 return pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200472
473}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100474
475/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
476 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100477 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100478static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
479 const struct htx_blk *blk)
480{
481 int32_t pos;
482
483 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
484 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
485}
486
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200487static inline int32_t htx_find_front(const struct htx *htx)
488{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 int32_t front, pos;
490 uint32_t addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200491
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100492 if (!htx->used)
493 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200494
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100495 front = -1;
496 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
497 struct htx_blk *blk = htx_get_blk(htx, pos);
498 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200499
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100500 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
501 front = pos;
502 addr = blk->addr;
503 }
504 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200505
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100506 return front;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200507}
508
509/* Changes the size of the value. It is the caller responsibility to change the
510 * value itself, make sure there is enough space and update allocated value.
511 */
512static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
513{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200515
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100516 switch (type) {
517 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200518 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100519 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
520 break;
521 case HTX_BLK_REQ_SL:
522 case HTX_BLK_RES_SL:
523 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100524 blk->info = (type << 28) + vlen;
525 break;
526 default:
527 /* Unexpected case */
528 break;
529 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200530}
531
532/* Returns the data pointer of the block <blk> */
533static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
534{
535 return ((void *)htx->blocks + blk->addr);
536}
537
538/* Returns the name of the block <blk>, only if it is a header. Otherwise it
539 * returns an empty name.
540 */
541static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
542{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100543 enum htx_blk_type type = htx_get_blk_type(blk);
544 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200545
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100546 switch (type) {
547 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200548 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100549 ret.ptr = htx_get_blk_ptr(htx, blk);
550 ret.len = blk->info & 0xff;
551 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200552
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100553 default:
554 return ist("");
555 }
556 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557}
558
Christopher Faulet54483df2018-11-26 15:05:52 +0100559
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200560/* Returns the value of the block <blk>, depending on its type. If there is no
561 * value, an empty one is retruned.
562 */
563static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
564{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100565 enum htx_blk_type type = htx_get_blk_type(blk);
566 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200567
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100568 switch (type) {
569 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200570 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100571 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
572 ret.len = (blk->info >> 8) & 0xfffff;
573 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200574
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100575 case HTX_BLK_REQ_SL:
576 case HTX_BLK_RES_SL:
577 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 ret.ptr = htx_get_blk_ptr(htx, blk);
579 ret.len = blk->info & 0xfffffff;
580 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200581
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100582 default:
583 return ist("");
584 }
585 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586}
587
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100588/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
589 * address and its length are adjusted, and the htx's total data count is
590 * updated. This is used to mark that part of some data were transfered
591 * from a DATA block without removing this DATA block. No sanity check is
592 * performed, the caller is reponsible for doing this exclusively on DATA
593 * blocks, and never removing more than the block's size.
594 */
595static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
596{
597 blk->addr += n;
598 blk->info -= n;
599 htx->data -= n;
600}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200601
602/* Returns the space used by metadata in <htx>. */
603static inline uint32_t htx_meta_space(const struct htx *htx)
604{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100605 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606}
607
608/* Returns the space used (data + metadata) in <htx> */
609static inline uint32_t htx_used_space(const struct htx *htx)
610{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100611 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612}
613
614/* Returns the free space in <htx> */
615static inline uint32_t htx_free_space(const struct htx *htx)
616{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100617 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618}
619
Christopher Faulet8564c1f2019-01-07 13:48:55 +0100620/* Returns the maximum space usable for data in <htx>. This is in fact the
621 * maximum sice for a uniq block to fill the HTX message. */
622static inline uint32_t htx_max_data_space(const struct htx *htx)
623{
624 if (!htx->size)
625 return 0;
626 return (htx->size - sizeof(htx->blocks[0]));
627}
628
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200629/* Returns the maximum size available to store some data in <htx> if a new block
630 * is reserved.
631 */
632static inline uint32_t htx_free_data_space(const struct htx *htx)
633{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100634 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200635
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100636 if (free < sizeof(htx->blocks[0]))
637 return 0;
638 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639}
640
Christopher Faulet2ae35042019-05-16 11:16:39 +0200641/* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
642 * set to -1 to have no limit.
643 */
644static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
645{
646 uint32_t free = htx_free_space(htx);
647
648 if (max != -1 && free > max)
649 free = max;
650 if (free < sizeof(htx->blocks[0]))
651 return 0;
652 return (free - sizeof(htx->blocks[0]));
653}
654
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200655/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
656static inline int htx_almost_full(const struct htx *htx)
657{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100658 if (!htx->size || htx_free_space(htx) < htx->size / 4)
659 return 1;
660 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200661}
662
663static inline void htx_reset(struct htx *htx)
664{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200665 htx->data = htx->used = htx->tail = htx->head = htx->front = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200666 htx->extra = 0;
667 htx->flags = HTX_FL_NONE;
Christopher Faulet29f17582019-05-23 11:03:26 +0200668 htx->first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200669}
670
Willy Tarreau3906e222018-12-05 07:56:25 +0100671/* returns the available room for raw data in buffer <buf> once HTX overhead is
672 * taken into account (one HTX header and two blocks). The purpose is to figure
673 * the optimal fill length to avoid copies.
674 */
675static inline size_t buf_room_for_htx_data(const struct buffer *buf)
676{
677 size_t room;
678
679 room = b_room(buf);
680 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
681 room = 0;
682 else
683 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
684
685 return room;
686}
687
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100688
689/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Willy Tarreau245d1892019-01-31 07:21:42 +0100690 * function does not update to the buffer.
691 * Note that it always returns a valid pointer, either to an initialized buffer
692 * or to the empty buffer.
693 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100694static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200695{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100696 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200697
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100698 if (b_is_null(buf))
699 return &htx_empty;
700 htx = ((struct htx *)(buf->area));
701 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100702 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100703 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100704 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200705 return htx;
706}
707
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100708/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
709 * full. It is the caller responsibility to call htx_to_buf() when it finish to
Willy Tarreau245d1892019-01-31 07:21:42 +0100710 * manipulate the HTX message to update <buf> accordingly. The returned pointer
711 * is always valid.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100712 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100713 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100714 */
715static inline struct htx *htx_from_buf(struct buffer *buf)
716{
717 struct htx *htx = htxbuf(buf);
718
719 b_set_data(buf, b_size(buf));
720 return htx;
721}
722
723/* Upate <buf> accordingly to the HTX message <htx> */
724static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
725{
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200726 if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100727 htx_reset(htx);
728 b_set_data(buf, 0);
729 }
730 else
731 b_set_data(buf, b_size(buf));
732}
733
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100734/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
735 * illegal to call this with htx == NULL.
736 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200737static inline int htx_is_empty(const struct htx *htx)
738{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100739 return !htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200740}
741
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100742/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
743 * is illegal to call this with htx == NULL.
744 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200745static inline int htx_is_not_empty(const struct htx *htx)
746{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100747 return htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200748}
749
750/* For debugging purpose */
751static inline const char *htx_blk_type_str(enum htx_blk_type type)
752{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100753 switch (type) {
754 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
755 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
756 case HTX_BLK_HDR: return "HTX_BLK_HDR";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100757 case HTX_BLK_EOH: return "HTX_BLK_EOH";
758 case HTX_BLK_DATA: return "HTX_BLK_DATA";
759 case HTX_BLK_EOD: return "HTX_BLK_EOD";
760 case HTX_BLK_TLR: return "HTX_BLK_TLR";
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200761 case HTX_BLK_EOT: return "HTX_BLK_EOT";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100762 case HTX_BLK_EOM: return "HTX_BLK_EOM";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100763 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
764 default: return "HTX_BLK_???";
765 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200766}
767
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200768static inline void htx_dump(struct htx *htx)
769{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100770 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200771
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100772 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Faulet28f29c72019-04-30 17:55:45 +0200773 htx, htx->size, htx->data, htx->used, (htx->tail >= htx->head) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100774 (unsigned long long)htx->extra);
Christopher Faulet29f17582019-05-23 11:03:26 +0200775 fprintf(stderr, "\tfirst=%d - head=%u, tail=%u - front=%u\n",
776 htx->first, htx->head, htx->tail, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200777
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100778 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100779 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100780 struct htx_blk *blk = htx_get_blk(htx, pos);
781 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100782 uint32_t sz = htx_get_blksz(blk);
783 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200784
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100785 n = htx_get_blk_name(htx, blk);
786 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100788 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200789 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100790 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
791 pos, htx_blk_type_str(type), sz, blk->addr,
792 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
793 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
794 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200796 else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100797 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
798 pos, htx_blk_type_str(type), sz, blk->addr,
799 (int)n.len, n.ptr,
800 (int)v.len, v.ptr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100801 else
802 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
803 pos, htx_blk_type_str(type), sz, blk->addr,
804 (!v.len ? "\t<empty>" : ""));
805 }
806 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807}
808
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100809#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200810
811/*
812 * Local variables:
813 * c-indent-level: 8
814 * c-basic-offset: 8
815 * End:
816 */