blob: e7d8b80f67a31569ba2d1d9aeefec28208be7870 [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 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200152
153 uint32_t tail_addr; /* start address of the free space in front of the the blocks table */
154 uint32_t head_addr; /* start address of the free space at the beginning */
155 uint32_t end_addr; /* end address of the free space at the beginning */
156
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100157
158 uint64_t extra; /* known bytes amount remaining to receive */
159 uint32_t flags; /* HTX_FL_* */
160
Christopher Faulet29f17582019-05-23 11:03:26 +0200161 int32_t first; /* position of the first block to (re)start the analyse. -1 if unset. */
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100162
163 struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
164};
165
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200166
167extern struct htx htx_empty;
168
169struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
170struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
171struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100172void htx_truncate(struct htx *htx, uint32_t offset);
Christopher Faulet549822f2019-02-25 10:23:19 +0100173struct htx_ret htx_drain(struct htx *htx, uint32_t max);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200174
175struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100176 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200177struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
178 enum htx_blk_type mark);
179
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100180struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
181 const struct ist p1, const struct ist p2, const struct ist p3);
182struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
183 const struct ist p2, const struct ist p3);
184
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200185struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100186 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200187
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200188struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200189struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value);
Willy Tarreau52610e92019-01-03 18:26:17 +0100190struct 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 +0200191struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200192struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200193struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200194struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data);
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200195size_t htx_add_data(struct htx *htx, const struct ist data);
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200196struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data);
Christopher Faulet86fcf6d2019-06-11 10:41:19 +0200197void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200198
Christopher Fauletc59ff232018-12-03 13:58:44 +0100199int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
200int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
201int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
202int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200203
Christopher Faulet570d1612018-11-26 11:13:57 +0100204/* Functions and macros to get parts of the start-line or legnth of these
205 * parts
206 */
207#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
208
209#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
210#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
211#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
212#define HTX_SL_P1_PTR(sl) ((sl)->l)
213#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
214#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
215
216#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
217#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
218#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
219#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
220#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
221#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
222
223#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
224#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
225#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
226#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
227#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
228#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
229
Willy Tarreau8de1df92019-04-15 21:27:18 +0200230static inline struct ist htx_sl_p1(const struct htx_sl *sl)
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100231{
232 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
233}
Christopher Faulet570d1612018-11-26 11:13:57 +0100234
Willy Tarreau8de1df92019-04-15 21:27:18 +0200235static inline struct ist htx_sl_p2(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100236{
237 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
238}
239
Willy Tarreau8de1df92019-04-15 21:27:18 +0200240static inline struct ist htx_sl_p3(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100241{
242 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
243}
244
Willy Tarreau8de1df92019-04-15 21:27:18 +0200245static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100246{
247 return htx_sl_p1(sl);
248}
249
Willy Tarreau8de1df92019-04-15 21:27:18 +0200250static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100251{
252 return htx_sl_p2(sl);
253}
254
Willy Tarreau8de1df92019-04-15 21:27:18 +0200255static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100256{
257 return htx_sl_p3(sl);
258}
259
260
Willy Tarreau8de1df92019-04-15 21:27:18 +0200261static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100262{
263 return htx_sl_p1(sl);
264}
265
Willy Tarreau8de1df92019-04-15 21:27:18 +0200266static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100267{
268 return htx_sl_p2(sl);
269}
270
Willy Tarreau8de1df92019-04-15 21:27:18 +0200271static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
Christopher Faulet570d1612018-11-26 11:13:57 +0100272{
273 return htx_sl_p3(sl);
274}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200275
276/* Returns the array index of a block given its position <pos> */
277static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
278{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100279 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200280}
281
282/* Returns the position of the block <blk> */
283static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
284{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100285 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200286}
287
288/* Returns the block at the position <pos> */
289static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
290{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100291 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200292}
293
294/* Returns the type of the block <blk> */
295static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
296{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100297 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200298}
299
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200300/* Returns the size of the block <blk>, depending of its type */
301static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
302{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100303 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200304
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100305 switch (type) {
306 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200307 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100308 /* name.length + value.length */
309 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100310 default:
311 /* value.length */
312 return (blk->info & 0xfffffff);
313 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200314}
315
Christopher Faulet28f29c72019-04-30 17:55:45 +0200316/* Returns the position of the oldest entry (head).
317 *
318 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
319 * store on unsigned 32-bits integer, but it is impossible to have so much
320 * blocks to overflow a 32-bits signed integer !
321 */
322static inline int32_t htx_get_head(const struct htx *htx)
323{
324 return (htx->used ? htx->head : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200325}
326
327/* Returns the oldest HTX block (head) if the HTX message is not
328 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100329 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200330static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
331{
332 int32_t head = htx_get_head(htx);
333
334 return ((head == -1) ? NULL : htx_get_blk(htx, head));
335}
336
337/* Returns the type of the oldest HTX block (head) if the HTX message is not
338 * empty. Otherwise it returns HTX_BLK_UNUSED.
339 */
340static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
341{
342 struct htx_blk *blk = htx_get_head_blk(htx);
343
344 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
345}
346
347/* Returns the position of the newest entry (tail).
348 *
349 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
350 * store on unsigned 32-bits integer, but it is impossible to have so much
351 * blocks to overflow a 32-bits signed integer !
352 */
353static inline int32_t htx_get_tail(const struct htx *htx)
354{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100355 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200356}
357
358/* Returns the newest HTX block (tail) if the HTX message is not
359 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100360 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200361static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
362{
363 int32_t tail = htx_get_tail(htx);
364
365 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
366}
367
368/* Returns the type of the newest HTX block (tail) if the HTX message is not
369 * empty. Otherwise it returns HTX_BLK_UNUSED.
370 */
371static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
372{
373 struct htx_blk *blk = htx_get_tail_blk(htx);
374
375 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
376}
377
Christopher Faulet29f17582019-05-23 11:03:26 +0200378/* Returns the position of the first block in the HTX message <htx>. If unset,
379 * or if <htx> is empty, -1 is returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200380 *
381 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
382 * store on unsigned 32-bits integer, but it is impossible to have so much
383 * blocks to overflow a 32-bits signed integer !
384 */
385static inline int32_t htx_get_first(const struct htx *htx)
386{
Christopher Faulet29f17582019-05-23 11:03:26 +0200387 if (!htx->used)
388 return -1;
389 return htx->first;
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200390}
391
Christopher Faulet29f17582019-05-23 11:03:26 +0200392/* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
393 * empty, NULL returned.
Christopher Fauleta3ad6b12019-05-13 11:36:27 +0200394 */
395static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
396{
397 int32_t pos;
398
399 pos = htx_get_first(htx);
400 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
401}
402
403/* Returns the type of the first block in the HTX message <htx>. If unset or if
404 * <htx> is empty, HTX_BLK_UNUSED is returned.
405 */
406static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
407{
408 struct htx_blk *blk = htx_get_first_blk(htx);
409
410 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
411}
412
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800413/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200414 * the message is empty or if <pos> is the position of the head, -1 returned.
415 *
416 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
417 * store on unsigned 32-bits integer, but it is impossible to have so much
418 * blocks to overflow a 32-bits signed integer !
419 */
420static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
421{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100422 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200423
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100424 head = htx_get_head(htx);
425 if (head == -1 || pos == head)
426 return -1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100427 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200428}
429
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100430/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
431 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100432 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100433static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
434 const struct htx_blk *blk)
435{
436 int32_t pos;
437
438 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
439 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
440}
441
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800442/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200443 * the message is empty or if <pos> is the position of the tail, -1 returned.
444 *
445 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
446 * store on unsigned 32-bits integer, but it is impossible to have so much
447 * blocks to overflow a 32-bits signed integer !
448 */
449static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
450{
Christopher Faulet28f29c72019-04-30 17:55:45 +0200451 if (!htx->used || pos == htx->tail)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100452 return -1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200453 return (pos + 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200454
455}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100456
457/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
458 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100459 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100460static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
461 const struct htx_blk *blk)
462{
463 int32_t pos;
464
465 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
466 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
467}
468
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200469/* Changes the size of the value. It is the caller responsibility to change the
470 * value itself, make sure there is enough space and update allocated value.
471 */
472static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
473{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100474 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200475
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100476 switch (type) {
477 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200478 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100479 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
480 break;
481 case HTX_BLK_REQ_SL:
482 case HTX_BLK_RES_SL:
483 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100484 blk->info = (type << 28) + vlen;
485 break;
486 default:
487 /* Unexpected case */
488 break;
489 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200490}
491
492/* Returns the data pointer of the block <blk> */
493static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
494{
495 return ((void *)htx->blocks + blk->addr);
496}
497
498/* Returns the name of the block <blk>, only if it is a header. Otherwise it
499 * returns an empty name.
500 */
501static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
502{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100503 enum htx_blk_type type = htx_get_blk_type(blk);
504 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200505
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100506 switch (type) {
507 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200508 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100509 ret.ptr = htx_get_blk_ptr(htx, blk);
510 ret.len = blk->info & 0xff;
511 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 default:
514 return ist("");
515 }
516 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200517}
518
Christopher Faulet54483df2018-11-26 15:05:52 +0100519
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200520/* Returns the value of the block <blk>, depending on its type. If there is no
521 * value, an empty one is retruned.
522 */
523static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
524{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100525 enum htx_blk_type type = htx_get_blk_type(blk);
526 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200527
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100528 switch (type) {
529 case HTX_BLK_HDR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200530 case HTX_BLK_TLR:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100531 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
532 ret.len = (blk->info >> 8) & 0xfffff;
533 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200534
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100535 case HTX_BLK_REQ_SL:
536 case HTX_BLK_RES_SL:
537 case HTX_BLK_DATA:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100538 ret.ptr = htx_get_blk_ptr(htx, blk);
539 ret.len = blk->info & 0xfffffff;
540 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200541
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100542 default:
543 return ist("");
544 }
545 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200546}
547
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100548/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
549 * address and its length are adjusted, and the htx's total data count is
550 * updated. This is used to mark that part of some data were transfered
551 * from a DATA block without removing this DATA block. No sanity check is
552 * performed, the caller is reponsible for doing this exclusively on DATA
553 * blocks, and never removing more than the block's size.
554 */
555static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
556{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200557 if (blk->addr == htx->end_addr)
558 htx->end_addr += n;
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100559 blk->addr += n;
560 blk->info -= n;
561 htx->data -= n;
562}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200563
564/* Returns the space used by metadata in <htx>. */
565static inline uint32_t htx_meta_space(const struct htx *htx)
566{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100567 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200568}
569
570/* Returns the space used (data + metadata) in <htx> */
571static inline uint32_t htx_used_space(const struct htx *htx)
572{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100573 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200574}
575
576/* Returns the free space in <htx> */
577static inline uint32_t htx_free_space(const struct htx *htx)
578{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100579 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200580}
581
Christopher Faulet8564c1f2019-01-07 13:48:55 +0100582/* Returns the maximum space usable for data in <htx>. This is in fact the
583 * maximum sice for a uniq block to fill the HTX message. */
584static inline uint32_t htx_max_data_space(const struct htx *htx)
585{
586 if (!htx->size)
587 return 0;
588 return (htx->size - sizeof(htx->blocks[0]));
589}
590
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200591/* Returns the maximum size available to store some data in <htx> if a new block
592 * is reserved.
593 */
594static inline uint32_t htx_free_data_space(const struct htx *htx)
595{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100596 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200597
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100598 if (free < sizeof(htx->blocks[0]))
599 return 0;
600 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200601}
602
Christopher Faulet2ae35042019-05-16 11:16:39 +0200603/* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
604 * set to -1 to have no limit.
605 */
606static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
607{
608 uint32_t free = htx_free_space(htx);
609
610 if (max != -1 && free > max)
611 free = max;
612 if (free < sizeof(htx->blocks[0]))
613 return 0;
614 return (free - sizeof(htx->blocks[0]));
615}
616
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
618static inline int htx_almost_full(const struct htx *htx)
619{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100620 if (!htx->size || htx_free_space(htx) < htx->size / 4)
621 return 1;
622 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200623}
624
625static inline void htx_reset(struct htx *htx)
626{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200627 htx->data = htx->used = htx->tail = htx->head = 0;
628 htx->tail_addr = htx->head_addr = htx->end_addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200629 htx->extra = 0;
630 htx->flags = HTX_FL_NONE;
Christopher Faulet29f17582019-05-23 11:03:26 +0200631 htx->first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200632}
633
Willy Tarreau3906e222018-12-05 07:56:25 +0100634/* returns the available room for raw data in buffer <buf> once HTX overhead is
635 * taken into account (one HTX header and two blocks). The purpose is to figure
636 * the optimal fill length to avoid copies.
637 */
638static inline size_t buf_room_for_htx_data(const struct buffer *buf)
639{
640 size_t room;
641
642 room = b_room(buf);
643 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
644 room = 0;
645 else
646 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
647
648 return room;
649}
650
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100651
652/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
Willy Tarreau245d1892019-01-31 07:21:42 +0100653 * function does not update to the buffer.
654 * Note that it always returns a valid pointer, either to an initialized buffer
655 * or to the empty buffer.
656 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100657static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200658{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100659 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200660
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100661 if (b_is_null(buf))
662 return &htx_empty;
663 htx = ((struct htx *)(buf->area));
664 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100665 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100666 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100667 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668 return htx;
669}
670
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100671/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
672 * full. It is the caller responsibility to call htx_to_buf() when it finish to
Willy Tarreau245d1892019-01-31 07:21:42 +0100673 * manipulate the HTX message to update <buf> accordingly. The returned pointer
674 * is always valid.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100675 *
Willy Tarreau245d1892019-01-31 07:21:42 +0100676 * The caller can call htxbuf() function to avoid any update of the buffer.
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100677 */
678static inline struct htx *htx_from_buf(struct buffer *buf)
679{
680 struct htx *htx = htxbuf(buf);
681
682 b_set_data(buf, b_size(buf));
683 return htx;
684}
685
686/* Upate <buf> accordingly to the HTX message <htx> */
687static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
688{
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200689 if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100690 htx_reset(htx);
691 b_set_data(buf, 0);
692 }
693 else
694 b_set_data(buf, b_size(buf));
695}
696
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100697/* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
698 * illegal to call this with htx == NULL.
699 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700static inline int htx_is_empty(const struct htx *htx)
701{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100702 return !htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703}
704
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100705/* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
706 * is illegal to call this with htx == NULL.
707 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200708static inline int htx_is_not_empty(const struct htx *htx)
709{
Willy Tarreaue5fcfbe2019-01-31 07:31:18 +0100710 return htx->used;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200711}
712
713/* For debugging purpose */
714static inline const char *htx_blk_type_str(enum htx_blk_type type)
715{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100716 switch (type) {
717 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
718 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
719 case HTX_BLK_HDR: return "HTX_BLK_HDR";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100720 case HTX_BLK_EOH: return "HTX_BLK_EOH";
721 case HTX_BLK_DATA: return "HTX_BLK_DATA";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100722 case HTX_BLK_TLR: return "HTX_BLK_TLR";
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200723 case HTX_BLK_EOT: return "HTX_BLK_EOT";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100724 case HTX_BLK_EOM: return "HTX_BLK_EOM";
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100725 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
726 default: return "HTX_BLK_???";
727 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200728}
729
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200730static inline void htx_dump(struct htx *htx)
731{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100732 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100734 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Fauletd7884d32019-06-11 10:40:43 +0200735 htx, htx->size, htx->data, htx->used, (!htx->head_addr) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100736 (unsigned long long)htx->extra);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200737 fprintf(stderr, "\tfirst=%d - head=%u, tail=%u\n",
738 htx->first, htx->head, htx->tail);
739 fprintf(stderr, "\ttail_addr=%d - head_addr=%u, end_addr=%u\n",
740 htx->tail_addr, htx->head_addr, htx->end_addr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200741
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100742 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100743 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100744 struct htx_blk *blk = htx_get_blk(htx, pos);
745 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100746 uint32_t sz = htx_get_blksz(blk);
747 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200748
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100749 n = htx_get_blk_name(htx, blk);
750 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200751
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100752 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100754 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
755 pos, htx_blk_type_str(type), sz, blk->addr,
756 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
757 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
758 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200759 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200760 else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100761 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
762 pos, htx_blk_type_str(type), sz, blk->addr,
763 (int)n.len, n.ptr,
764 (int)v.len, v.ptr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100765 else
766 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
767 pos, htx_blk_type_str(type), sz, blk->addr,
768 (!v.len ? "\t<empty>" : ""));
769 }
770 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200771}
772
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100773#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200774
775/*
776 * Local variables:
777 * c-indent-level: 8
778 * c-basic-offset: 8
779 * End:
780 */