blob: cb998b99e6db69cef65cbe0e1ab28e88957b0c30 [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 */
Christopher Faulet54b5e212019-06-04 10:08:28 +0200104 HTX_BLK_TLR = 5, /* trailer name/value block */
105 HTX_BLK_EOT = 6, /* end-of-trailers block */
106 HTX_BLK_EOM = 7, /* end-of-message block */
Christopher Faulet39744f72019-05-24 14:54:00 +0200107 /* 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);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200185struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value);
Willy Tarreau52610e92019-01-03 18:26:17 +0100186struct 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 +0200187struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200188struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200189struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200190struct htx_blk *htx_add_data_atonce(struct htx *htx, const struct ist data);
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200191size_t htx_add_data(struct htx *htx, const struct ist data);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100192struct 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 +0200193
Christopher Fauletc59ff232018-12-03 13:58:44 +0100194int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
195int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
196int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
197int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200198
Christopher Faulet570d1612018-11-26 11:13:57 +0100199/* Functions and macros to get parts of the start-line or legnth of these
200 * parts
201 */
202#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
203
204#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
205#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
206#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
207#define HTX_SL_P1_PTR(sl) ((sl)->l)
208#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
209#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
210
211#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
212#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
213#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
214#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
215#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
216#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
217
218#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
219#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
220#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
221#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
222#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
223#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
224
Willy Tarreau8de1df92019-04-15 21:27:18 +0200225static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100226{
227 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
228}
Christopher Faulet570d1612018-11-26 11:13:57 +0100229
Willy Tarreau8de1df92019-04-15 21:27:18 +0200230static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100231{
232 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
233}
234
Willy Tarreau8de1df92019-04-15 21:27:18 +0200235static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100236{
237 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
238}
239
Willy Tarreau8de1df92019-04-15 21:27:18 +0200240static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100241{
242 return htx_sl_p1(sl);
243}
244
Willy Tarreau8de1df92019-04-15 21:27:18 +0200245static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100246{
247 return htx_sl_p2(sl);
248}
249
Willy Tarreau8de1df92019-04-15 21:27:18 +0200250static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100251{
252 return htx_sl_p3(sl);
253}
254
255
Willy Tarreau8de1df92019-04-15 21:27:18 +0200256static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100257{
258 return htx_sl_p1(sl);
259}
260
Willy Tarreau8de1df92019-04-15 21:27:18 +0200261static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100262{
263 return htx_sl_p2(sl);
264}
265
Willy Tarreau8de1df92019-04-15 21:27:18 +0200266static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100267{
268 return htx_sl_p3(sl);
269}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200270
271/* Returns the array index of a block given its position <pos> */
272static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
273{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100274 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200275}
276
277/* Returns the position of the block <blk> */
278static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
279{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100280 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200281}
282
283/* Returns the block at the position <pos> */
284static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
285{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100286 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200287}
288
289/* Returns the type of the block <blk> */
290static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
291{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100292 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200293}
294
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200295/* Returns the size of the block <blk>, depending of its type */
296static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
297{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100298 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200299
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100300 switch (type) {
301 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200302 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100303 /* name.length + value.length */
304 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100305 default:
306 /* value.length */
307 return (blk->info & 0xfffffff);
308 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200309}
310
Christopher Faulet28f29c72019-04-30 17:55:45 +0200311/* Returns the wrap position, ie the position where the blocks table wraps.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200312 *
313 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
314 * store on unsigned 32-bits integer, but it is impossible to have so much
315 * blocks to overflow a 32-bits signed integer !
316 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200317static inline int32_t htx_get_wrap(const struct htx *htx)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200318{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100319 if (!htx->used)
320 return -1;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200321 return ((htx->tail >= htx->head)
322 ? (htx->used + htx->head)
323 : (htx->used - 1) + (htx->head - htx->tail));
324}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200325
Christopher Faulet28f29c72019-04-30 17:55:45 +0200326/* Returns the position of the oldest entry (head).
327 *
328 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
329 * store on unsigned 32-bits integer, but it is impossible to have so much
330 * blocks to overflow a 32-bits signed integer !
331 */
332static inline int32_t htx_get_head(const struct htx *htx)
333{
334 return (htx->used ? htx->head : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200335}
336
337/* Returns the oldest HTX block (head) if the HTX message is not
338 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100339 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200340static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
341{
342 int32_t head = htx_get_head(htx);
343
344 return ((head == -1) ? NULL : htx_get_blk(htx, head));
345}
346
347/* Returns the type of the oldest HTX block (head) if the HTX message is not
348 * empty. Otherwise it returns HTX_BLK_UNUSED.
349 */
350static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
351{
352 struct htx_blk *blk = htx_get_head_blk(htx);
353
354 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
355}
356
357/* Returns the position of the newest entry (tail).
358 *
359 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
360 * store on unsigned 32-bits integer, but it is impossible to have so much
361 * blocks to overflow a 32-bits signed integer !
362 */
363static inline int32_t htx_get_tail(const struct htx *htx)
364{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100365 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200366}
367
368/* Returns the newest HTX block (tail) if the HTX message is not
369 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100370 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200371static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
372{
373 int32_t tail = htx_get_tail(htx);
374
375 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
376}
377
378/* Returns the type of the newest HTX block (tail) if the HTX message is not
379 * empty. Otherwise it returns HTX_BLK_UNUSED.
380 */
381static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
382{
383 struct htx_blk *blk = htx_get_tail_blk(htx);
384
385 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
386}
387
Christopher Faulet29f17582019-05-23 11:03:26 +0200388/* Returns the position of the first block in the HTX message <htx>. If unset,
389 * or if <htx> is empty, -1 is returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200390 *
391 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
392 * store on unsigned 32-bits integer, but it is impossible to have so much
393 * blocks to overflow a 32-bits signed integer !
394 */
395static inline int32_t htx_get_first(const struct htx *htx)
396{
Christopher Faulet29f17582019-05-23 11:03:26 +0200397 if (!htx->used)
398 return -1;
399 return htx->first;
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200400}
401
Christopher Faulet29f17582019-05-23 11:03:26 +0200402/* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
403 * empty, NULL returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200404 */
405static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
406{
407 int32_t pos;
408
409 pos = htx_get_first(htx);
410 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
411}
412
413/* Returns the type of the first block in the HTX message <htx>. If unset or if
414 * <htx> is empty, HTX_BLK_UNUSED is returned.
415 */
416static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
417{
418 struct htx_blk *blk = htx_get_first_blk(htx);
419
420 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
421}
422
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800423/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200424 * the message is empty or if <pos> is the position of the head, -1 returned.
425 *
426 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
427 * store on unsigned 32-bits integer, but it is impossible to have so much
428 * blocks to overflow a 32-bits signed integer !
429 */
430static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
431{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100432 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200433
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100434 head = htx_get_head(htx);
435 if (head == -1 || pos == head)
436 return -1;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200437 if (!pos) {
438 /* htx_get_wrap() is always greater than 1 here */
439 return (htx_get_wrap(htx) - 1);
440 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100441 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200442}
443
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100444/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
445 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100446 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100447static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
448 const struct htx_blk *blk)
449{
450 int32_t pos;
451
452 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
453 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
454}
455
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800456/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200457 * the message is empty or if <pos> is the position of the tail, -1 returned.
458 *
459 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
460 * store on unsigned 32-bits integer, but it is impossible to have so much
461 * blocks to overflow a 32-bits signed integer !
462 */
463static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
464{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200465 if (!htx->used || pos == htx->tail)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100466 return -1;
467 pos++;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200468 if (pos == htx_get_wrap(htx))
469 return 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100470 return pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200471
472}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100473
474/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
475 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100476 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100477static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
478 const struct htx_blk *blk)
479{
480 int32_t pos;
481
482 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
483 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
484}
485
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200486static inline int32_t htx_find_front(const struct htx *htx)
487{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100488 int32_t front, pos;
489 uint32_t addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200490
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100491 if (!htx->used)
492 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200493
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100494 front = -1;
495 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
496 struct htx_blk *blk = htx_get_blk(htx, pos);
497 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200498
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100499 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
500 front = pos;
501 addr = blk->addr;
502 }
503 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200504
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100505 return front;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200506}
507
508/* Changes the size of the value. It is the caller responsibility to change the
509 * value itself, make sure there is enough space and update allocated value.
510 */
511static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
512{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200514
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100515 switch (type) {
516 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200517 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100518 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
519 break;
520 case HTX_BLK_REQ_SL:
521 case HTX_BLK_RES_SL:
522 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100523 blk->info = (type << 28) + vlen;
524 break;
525 default:
526 /* Unexpected case */
527 break;
528 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200529}
530
531/* Returns the data pointer of the block <blk> */
532static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
533{
534 return ((void *)htx->blocks + blk->addr);
535}
536
537/* Returns the name of the block <blk>, only if it is a header. Otherwise it
538 * returns an empty name.
539 */
540static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
541{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100542 enum htx_blk_type type = htx_get_blk_type(blk);
543 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200544
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100545 switch (type) {
546 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200547 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100548 ret.ptr = htx_get_blk_ptr(htx, blk);
549 ret.len = blk->info & 0xff;
550 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200551
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100552 default:
553 return ist("");
554 }
555 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556}
557
Christopher Faulet54483df2018-11-26 15:05:52 +0100558
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200559/* Returns the value of the block <blk>, depending on its type. If there is no
560 * value, an empty one is retruned.
561 */
562static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
563{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100564 enum htx_blk_type type = htx_get_blk_type(blk);
565 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100567 switch (type) {
568 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200569 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100570 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
571 ret.len = (blk->info >> 8) & 0xfffff;
572 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200573
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 case HTX_BLK_REQ_SL:
575 case HTX_BLK_RES_SL:
576 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100577 ret.ptr = htx_get_blk_ptr(htx, blk);
578 ret.len = blk->info & 0xfffffff;
579 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200580
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100581 default:
582 return ist("");
583 }
584 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200585}
586
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100587/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
588 * address and its length are adjusted, and the htx's total data count is
589 * updated. This is used to mark that part of some data were transfered
590 * from a DATA block without removing this DATA block. No sanity check is
591 * performed, the caller is reponsible for doing this exclusively on DATA
592 * blocks, and never removing more than the block's size.
593 */
594static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
595{
596 blk->addr += n;
597 blk->info -= n;
598 htx->data -= n;
599}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
601/* Returns the space used by metadata in <htx>. */
602static inline uint32_t htx_meta_space(const struct htx *htx)
603{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100604 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200605}
606
607/* Returns the space used (data + metadata) in <htx> */
608static inline uint32_t htx_used_space(const struct htx *htx)
609{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100610 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200611}
612
613/* Returns the free space in <htx> */
614static inline uint32_t htx_free_space(const struct htx *htx)
615{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100616 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617}
618
Christopher Faulet8564c1f2019-01-07 13:48:55 +0100619/* Returns the maximum space usable for data in <htx>. This is in fact the
620 * maximum sice for a uniq block to fill the HTX message. */
621static inline uint32_t htx_max_data_space(const struct htx *htx)
622{
623 if (!htx->size)
624 return 0;
625 return (htx->size - sizeof(htx->blocks[0]));
626}
627
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200628/* Returns the maximum size available to store some data in <htx> if a new block
629 * is reserved.
630 */
631static inline uint32_t htx_free_data_space(const struct htx *htx)
632{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100633 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200634
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100635 if (free < sizeof(htx->blocks[0]))
636 return 0;
637 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200638}
639
Christopher Faulet2ae35042019-05-16 11:16:39 +0200640/* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
641 * set to -1 to have no limit.
642 */
643static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
644{
645 uint32_t free = htx_free_space(htx);
646
647 if (max != -1 && free > max)
648 free = max;
649 if (free < sizeof(htx->blocks[0]))
650 return 0;
651 return (free - sizeof(htx->blocks[0]));
652}
653
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200654/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
655static inline int htx_almost_full(const struct htx *htx)
656{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100657 if (!htx->size || htx_free_space(htx) < htx->size / 4)
658 return 1;
659 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200660}
661
662static inline void htx_reset(struct htx *htx)
663{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200664 htx->data = htx->used = htx->tail = htx->head = htx->front = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665 htx->extra = 0;
666 htx->flags = HTX_FL_NONE;
Christopher Faulet29f17582019-05-23 11:03:26 +0200667 htx->first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668}
669
Willy Tarreau3906e222018-12-05 07:56:25 +0100670/* returns the available room for raw data in buffer <buf> once HTX overhead is
671 * taken into account (one HTX header and two blocks). The purpose is to figure
672 * the optimal fill length to avoid copies.
673 */
674static inline size_t buf_room_for_htx_data(const struct buffer *buf)
675{
676 size_t room;
677
678 room = b_room(buf);
679 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
680 room = 0;
681 else
682 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
683
684 return room;
685}
686
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100687
688/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Willy Tarreau245d1892019-01-31 07:21:42 +0100689 * function does not update to the buffer.
690 * Note that it always returns a valid pointer, either to an initialized buffer
691 * or to the empty buffer.
692 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100693static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100695 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200696
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100697 if (b_is_null(buf))
698 return &htx_empty;
699 htx = ((struct htx *)(buf->area));
700 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100701 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100702 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100703 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200704 return htx;
705}
706
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100707/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
708 * full. It is the caller responsibility to call htx_to_buf() when it finish to
Willy Tarreau245d1892019-01-31 07:21:42 +0100709 * manipulate the HTX message to update <buf> accordingly. The returned pointer
710 * is always valid.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100711 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100712 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100713 */
714static inline struct htx *htx_from_buf(struct buffer *buf)
715{
716 struct htx *htx = htxbuf(buf);
717
718 b_set_data(buf, b_size(buf));
719 return htx;
720}
721
722/* Upate <buf> accordingly to the HTX message <htx> */
723static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
724{
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200725 if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100726 htx_reset(htx);
727 b_set_data(buf, 0);
728 }
729 else
730 b_set_data(buf, b_size(buf));
731}
732
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100733/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
734 * illegal to call this with htx == NULL.
735 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736static inline int htx_is_empty(const struct htx *htx)
737{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100738 return !htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200739}
740
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100741/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
742 * is illegal to call this with htx == NULL.
743 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200744static inline int htx_is_not_empty(const struct htx *htx)
745{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100746 return htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747}
748
749/* For debugging purpose */
750static inline const char *htx_blk_type_str(enum htx_blk_type type)
751{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100752 switch (type) {
753 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
754 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
755 case HTX_BLK_HDR: return "HTX_BLK_HDR";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100756 case HTX_BLK_EOH: return "HTX_BLK_EOH";
757 case HTX_BLK_DATA: return "HTX_BLK_DATA";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100758 case HTX_BLK_TLR: return "HTX_BLK_TLR";
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200759 case HTX_BLK_EOT: return "HTX_BLK_EOT";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100760 case HTX_BLK_EOM: return "HTX_BLK_EOM";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100761 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
762 default: return "HTX_BLK_???";
763 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200764}
765
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200766static inline void htx_dump(struct htx *htx)
767{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100768 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200769
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100770 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Faulet28f29c72019-04-30 17:55:45 +0200771 htx, htx->size, htx->data, htx->used, (htx->tail >= htx->head) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100772 (unsigned long long)htx->extra);
Christopher Faulet29f17582019-05-23 11:03:26 +0200773 fprintf(stderr, "\tfirst=%d - head=%u, tail=%u - front=%u\n",
774 htx->first, htx->head, htx->tail, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200775
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100776 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100777 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100778 struct htx_blk *blk = htx_get_blk(htx, pos);
779 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100780 uint32_t sz = htx_get_blksz(blk);
781 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200782
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100783 n = htx_get_blk_name(htx, blk);
784 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200785
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100786 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100788 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
789 pos, htx_blk_type_str(type), sz, blk->addr,
790 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
791 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
792 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200793 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200794 else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100795 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
796 pos, htx_blk_type_str(type), sz, blk->addr,
797 (int)n.len, n.ptr,
798 (int)v.len, v.ptr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100799 else
800 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
801 pos, htx_blk_type_str(type), sz, blk->addr,
802 (!v.len ? "\t<empty>" : ""));
803 }
804 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805}
806
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100807#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200808
809/*
810 * Local variables:
811 * c-indent-level: 8
812 * c-basic-offset: 8
813 * End:
814 */