blob: 56a6c9b969e8f1c10e9236bee94c2cb43f68fde8 [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
94
95
96/* Pseudo header types (max 255). */
97enum htx_phdr_type {
98 HTX_PHDR_UNKNOWN = 0,
99 HTX_PHDR_SIZE,
100};
101
102/* HTTP block's type (max 15). */
103enum htx_blk_type {
104 HTX_BLK_REQ_SL = 0, /* Request start-line */
105 HTX_BLK_RES_SL = 1, /* Response start-line */
106 HTX_BLK_HDR = 2, /* header name/value block */
107 HTX_BLK_PHDR = 3, /* pseudo header block */
108 HTX_BLK_EOH = 4, /* end-of-headers block */
109 HTX_BLK_DATA = 5, /* data block */
110 HTX_BLK_EOD = 6, /* end-of-data block */
111 HTX_BLK_TLR = 7, /* trailer name/value block */
112 HTX_BLK_EOM = 8, /* end-of-message block */
113 /* 9 .. 13 unused */
114 HTX_BLK_OOB = 14, /* Out of band block, don't alter the parser */
115 HTX_BLK_UNUSED = 15, /* unused/removed block */
116};
117
118/* One HTTP block descriptor */
119struct htx_blk {
120 uint32_t addr; /* relative storage address of a data block */
121 uint32_t info; /* information about data stored */
122};
123
124struct htx_ret {
125 int32_t ret;
126 struct htx_blk *blk;
127};
128
129struct htx_sl {
130 unsigned int flags; /* HTX_SL_F_* */
131 union {
132 struct {
133 enum http_meth_t meth; /* method */
134 } req;
135 struct {
136 uint16_t status; /* status code */
137 } res;
138 } info;
139
140 /* XXX 2 bytes unused */
141
142 unsigned int len[3]; /* length of differnt parts of the start-line */
143 char l[0];
144};
145
146/* Internal representation of an HTTP message */
147struct htx {
148 uint32_t size; /* the array size, in bytes, used to store the HTTP message itself */
149 uint32_t data; /* the data size, in bytes. To known to total size used by all allocated
150 * blocks (blocks and their contents), you need to add size used by blocks,
151 * i.e. [ used * sizeof(struct htx_blk *) ] */
152
153 uint32_t used; /* number of blocks in use */
154 uint32_t tail; /* last inserted block */
155 uint32_t front; /* block's position of the first content before the blocks table */
156 uint32_t wrap; /* the position were the blocks table wraps, if any */
157
158 uint64_t extra; /* known bytes amount remaining to receive */
159 uint32_t flags; /* HTX_FL_* */
160
161 int32_t sl_off; /* Offset of the start-line of the HTTP message relatively to the beginning the
162 data block. -1 if unset */
163
164 struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
165};
166
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200167
168extern struct htx htx_empty;
169
170struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
171struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
172struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
173
174struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100175 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200176struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
177 enum htx_blk_type mark);
178
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100179struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
180 const struct ist p1, const struct ist p2, const struct ist p3);
181struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
182 const struct ist p2, const struct ist p3);
183
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200184struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200186
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200187struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
Willy Tarreau52610e92019-01-03 18:26:17 +0100188struct 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 +0200189struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
190struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr, const struct ist value);
191struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
192struct htx_blk *htx_add_data(struct htx *htx, const struct ist data);
193struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr);
194struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100195struct 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 +0200196
Christopher Fauletc59ff232018-12-03 13:58:44 +0100197int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
198int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
199int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
200int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
201int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200202
Christopher Faulet570d1612018-11-26 11:13:57 +0100203/* Functions and macros to get parts of the start-line or legnth of these
204 * parts
205 */
206#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
207
208#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
209#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
210#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
211#define HTX_SL_P1_PTR(sl) ((sl)->l)
212#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
213#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
214
215#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
216#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
217#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
218#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
219#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
220#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
221
222#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
223#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
224#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
225#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
226#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
227#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
228
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100229static inline const struct ist htx_sl_p1(const struct htx_sl *sl)
230{
231 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
232}
Christopher Faulet570d1612018-11-26 11:13:57 +0100233
234static inline const struct ist htx_sl_p2(const struct htx_sl *sl)
235{
236 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
237}
238
239static inline const struct ist htx_sl_p3(const struct htx_sl *sl)
240{
241 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
242}
243
Christopher Faulet570d1612018-11-26 11:13:57 +0100244static inline const struct ist htx_sl_req_meth(const struct htx_sl *sl)
245{
246 return htx_sl_p1(sl);
247}
248
249static inline const struct ist htx_sl_req_uri(const struct htx_sl *sl)
250{
251 return htx_sl_p2(sl);
252}
253
254static inline const struct ist htx_sl_req_vsn(const struct htx_sl *sl)
255{
256 return htx_sl_p3(sl);
257}
258
259
260static inline const struct ist htx_sl_res_vsn(const struct htx_sl *sl)
261{
262 return htx_sl_p1(sl);
263}
264
265static inline const struct ist htx_sl_res_code(const struct htx_sl *sl)
266{
267 return htx_sl_p2(sl);
268}
269
270static inline const struct ist htx_sl_res_reason(const struct htx_sl *sl)
271{
272 return htx_sl_p3(sl);
273}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200274
Christopher Faulet54483df2018-11-26 15:05:52 +0100275/* Returns the HTX start-line if set, otherwise it returns NULL. */
276static inline struct htx_sl *htx_get_stline(struct htx *htx)
277{
278 struct htx_sl *sl = NULL;
279
280 if (htx->sl_off != -1)
281 sl = ((void *)htx->blocks + htx->sl_off);
282
283 return sl;
284}
285
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200286/* Returns the array index of a block given its position <pos> */
287static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
288{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100289 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200290}
291
292/* Returns the position of the block <blk> */
293static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
294{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100295 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200296}
297
298/* Returns the block at the position <pos> */
299static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
300{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100301 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200302}
303
304/* Returns the type of the block <blk> */
305static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
306{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100307 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200308}
309
310/* Returns the pseudo-header type of the block <blk>. If it's not a
311 * pseudo-header, HTX_PHDR_UNKNOWN is returned.
312 */
313static inline enum htx_phdr_type htx_get_blk_phdr(const struct htx_blk *blk)
314{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100315 enum htx_blk_type type = htx_get_blk_type(blk);
316 enum htx_phdr_type phdr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200317
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100318 switch (type) {
319 case HTX_BLK_PHDR:
320 phdr = (blk->info & 0xff);
321 return phdr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200322
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100323 default:
324 /* Not a pseudo-header */
325 return HTX_PHDR_UNKNOWN;
326 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200327}
328
329/* Returns the size of the block <blk>, depending of its type */
330static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
331{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100332 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200333
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100334 switch (type) {
335 case HTX_BLK_HDR:
336 /* name.length + value.length */
337 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
338 case HTX_BLK_PHDR:
339 /* value.length */
340 return ((blk->info >> 8) & 0xfffff);
341 default:
342 /* value.length */
343 return (blk->info & 0xfffffff);
344 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200345}
346
347/* Returns the position of the oldest entry (head).
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_head(const struct htx *htx)
354{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100355 if (!htx->used)
356 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200357
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100358 return (((htx->tail + 1U < htx->used) ? htx->wrap : 0) + htx->tail + 1U - htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200359}
360
361/* Returns the oldest HTX block (head) if the HTX message is not
362 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100363 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200364static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
365{
366 int32_t head = htx_get_head(htx);
367
368 return ((head == -1) ? NULL : htx_get_blk(htx, head));
369}
370
371/* Returns the type of the oldest HTX block (head) if the HTX message is not
372 * empty. Otherwise it returns HTX_BLK_UNUSED.
373 */
374static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
375{
376 struct htx_blk *blk = htx_get_head_blk(htx);
377
378 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
379}
380
381/* Returns the position of the newest entry (tail).
382 *
383 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
384 * store on unsigned 32-bits integer, but it is impossible to have so much
385 * blocks to overflow a 32-bits signed integer !
386 */
387static inline int32_t htx_get_tail(const struct htx *htx)
388{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100389 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200390}
391
392/* Returns the newest HTX block (tail) if the HTX message is not
393 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100394 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200395static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
396{
397 int32_t tail = htx_get_tail(htx);
398
399 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
400}
401
402/* Returns the type of the newest HTX block (tail) if the HTX message is not
403 * empty. Otherwise it returns HTX_BLK_UNUSED.
404 */
405static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
406{
407 struct htx_blk *blk = htx_get_tail_blk(htx);
408
409 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
410}
411
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800412/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200413 * the message is empty or if <pos> is the position of the head, -1 returned.
414 *
415 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
416 * store on unsigned 32-bits integer, but it is impossible to have so much
417 * blocks to overflow a 32-bits signed integer !
418 */
419static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
420{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100421 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200422
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100423 head = htx_get_head(htx);
424 if (head == -1 || pos == head)
425 return -1;
426 if (!pos)
427 return (htx->wrap - 1);
428 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200429}
430
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100431/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
432 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100433 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100434static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
435 const struct htx_blk *blk)
436{
437 int32_t pos;
438
439 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
440 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
441}
442
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800443/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200444 * the message is empty or if <pos> is the position of the tail, -1 returned.
445 *
446 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
447 * store on unsigned 32-bits integer, but it is impossible to have so much
448 * blocks to overflow a 32-bits signed integer !
449 */
450static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
451{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100452 if (!htx->used)
453 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200454
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100455 if (pos == htx->tail)
456 return -1;
457 pos++;
458 if (pos >= htx->wrap)
459 pos = 0;
460 return pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200461
462}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100463
464/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
465 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100466 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100467static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
468 const struct htx_blk *blk)
469{
470 int32_t pos;
471
472 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
473 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
474}
475
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200476static inline int32_t htx_find_front(const struct htx *htx)
477{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100478 int32_t front, pos;
479 uint32_t addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200480
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100481 if (!htx->used)
482 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200483
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100484 front = -1;
485 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
486 struct htx_blk *blk = htx_get_blk(htx, pos);
487 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200488
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
490 front = pos;
491 addr = blk->addr;
492 }
493 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200494
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100495 return front;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200496}
497
Christopher Faulet14e88252018-11-22 11:28:18 +0100498/* Returns the HTX block containing data with the <offset>, relatively to the
499 * beginning of the HTX message <htx>. It returns an htx_ret. if the HTX block is
500 * not found, htx_ret.blk is set to NULL. Otherwise, it points to the right HTX
501 * block and htx_ret.ret is set to the remaining offset inside the block.
502 */
503static inline struct htx_ret htx_find_blk(const struct htx *htx, uint32_t offset)
504{
505 int32_t pos;
506
507 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
508 struct htx_blk *blk = htx_get_blk(htx, pos);
509 uint32_t sz = htx_get_blksz(blk);
510
511 if (offset < sz)
512 return (struct htx_ret){ .blk = blk, .ret = offset };
513 offset -= sz;
514 }
515
516 return (struct htx_ret){ .blk = NULL };
517}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200518/* Changes the size of the value. It is the caller responsibility to change the
519 * value itself, make sure there is enough space and update allocated value.
520 */
521static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
522{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100523 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100525 switch (type) {
526 case HTX_BLK_HDR:
527 case HTX_BLK_PHDR:
528 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
529 break;
530 case HTX_BLK_REQ_SL:
531 case HTX_BLK_RES_SL:
532 case HTX_BLK_DATA:
533 case HTX_BLK_TLR:
534 case HTX_BLK_OOB:
535 blk->info = (type << 28) + vlen;
536 break;
537 default:
538 /* Unexpected case */
539 break;
540 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200541}
542
543/* Returns the data pointer of the block <blk> */
544static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
545{
546 return ((void *)htx->blocks + blk->addr);
547}
548
549/* Returns the name of the block <blk>, only if it is a header. Otherwise it
550 * returns an empty name.
551 */
552static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
553{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100554 enum htx_blk_type type = htx_get_blk_type(blk);
555 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100557 switch (type) {
558 case HTX_BLK_HDR:
559 ret.ptr = htx_get_blk_ptr(htx, blk);
560 ret.len = blk->info & 0xff;
561 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200562
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100563 default:
564 return ist("");
565 }
566 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200567}
568
Christopher Faulet54483df2018-11-26 15:05:52 +0100569
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200570/* Returns the value of the block <blk>, depending on its type. If there is no
571 * value, an empty one is retruned.
572 */
573static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
574{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100575 enum htx_blk_type type = htx_get_blk_type(blk);
576 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200577
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 switch (type) {
579 case HTX_BLK_HDR:
580 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
581 ret.len = (blk->info >> 8) & 0xfffff;
582 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200583
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100584 case HTX_BLK_PHDR:
585 ret.ptr = htx_get_blk_ptr(htx, blk);
586 ret.len = (blk->info >> 8) & 0xfffff;
587 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200588
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100589 case HTX_BLK_REQ_SL:
590 case HTX_BLK_RES_SL:
591 case HTX_BLK_DATA:
592 case HTX_BLK_TLR:
593 case HTX_BLK_OOB:
594 ret.ptr = htx_get_blk_ptr(htx, blk);
595 ret.len = blk->info & 0xfffffff;
596 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200597
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100598 default:
599 return ist("");
600 }
601 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200602}
603
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100604/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
605 * address and its length are adjusted, and the htx's total data count is
606 * updated. This is used to mark that part of some data were transfered
607 * from a DATA block without removing this DATA block. No sanity check is
608 * performed, the caller is reponsible for doing this exclusively on DATA
609 * blocks, and never removing more than the block's size.
610 */
611static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
612{
613 blk->addr += n;
614 blk->info -= n;
615 htx->data -= n;
616}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617
618/* Returns the space used by metadata in <htx>. */
619static inline uint32_t htx_meta_space(const struct htx *htx)
620{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100621 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200622}
623
624/* Returns the space used (data + metadata) in <htx> */
625static inline uint32_t htx_used_space(const struct htx *htx)
626{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100627 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200628}
629
630/* Returns the free space in <htx> */
631static inline uint32_t htx_free_space(const struct htx *htx)
632{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100633 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200634}
635
Christopher Faulet8564c1f2019-01-07 13:48:55 +0100636/* Returns the maximum space usable for data in <htx>. This is in fact the
637 * maximum sice for a uniq block to fill the HTX message. */
638static inline uint32_t htx_max_data_space(const struct htx *htx)
639{
640 if (!htx->size)
641 return 0;
642 return (htx->size - sizeof(htx->blocks[0]));
643}
644
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200645/* Returns the maximum size available to store some data in <htx> if a new block
646 * is reserved.
647 */
648static inline uint32_t htx_free_data_space(const struct htx *htx)
649{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100650 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200651
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100652 if (free < sizeof(htx->blocks[0]))
653 return 0;
654 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200655}
656
657/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
658static inline int htx_almost_full(const struct htx *htx)
659{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100660 if (!htx->size || htx_free_space(htx) < htx->size / 4)
661 return 1;
662 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200663}
664
665static inline void htx_reset(struct htx *htx)
666{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100667 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668 htx->extra = 0;
669 htx->flags = HTX_FL_NONE;
Christopher Faulet54483df2018-11-26 15:05:52 +0100670 htx->sl_off = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671}
672
Willy Tarreau3906e222018-12-05 07:56:25 +0100673/* returns the available room for raw data in buffer <buf> once HTX overhead is
674 * taken into account (one HTX header and two blocks). The purpose is to figure
675 * the optimal fill length to avoid copies.
676 */
677static inline size_t buf_room_for_htx_data(const struct buffer *buf)
678{
679 size_t room;
680
681 room = b_room(buf);
682 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
683 room = 0;
684 else
685 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
686
687 return room;
688}
689
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100690
691/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
692 * function does not update to the buffer. */
693static 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
709 * manipulate the HTX message to update <buf> accordingly.
710 *
711 * If the caller can call htxbuf() function to avoir any update of the
712 * buffer.
713 */
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{
725 if (!htx->used) {
726 htx_reset(htx);
727 b_set_data(buf, 0);
728 }
729 else
730 b_set_data(buf, b_size(buf));
731}
732
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733/* Returns 1 if the message is empty, otherwise it returns 0. */
734static inline int htx_is_empty(const struct htx *htx)
735{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100736 return (!htx || !htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200737}
738
739/* Returns 1 if the message is not empty, otherwise it returns 0. */
740static inline int htx_is_not_empty(const struct htx *htx)
741{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100742 return (htx && htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200743}
744
745/* For debugging purpose */
746static inline const char *htx_blk_type_str(enum htx_blk_type type)
747{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100748 switch (type) {
749 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
750 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
751 case HTX_BLK_HDR: return "HTX_BLK_HDR";
752 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
753 case HTX_BLK_EOH: return "HTX_BLK_EOH";
754 case HTX_BLK_DATA: return "HTX_BLK_DATA";
755 case HTX_BLK_EOD: return "HTX_BLK_EOD";
756 case HTX_BLK_TLR: return "HTX_BLK_TLR";
757 case HTX_BLK_EOM: return "HTX_BLK_EOM";
758 case HTX_BLK_OOB: return "HTX_BLK_OOB";
759 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
760 default: return "HTX_BLK_???";
761 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762}
763
764static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
765{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100766 switch (phdr) {
767 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
768 default: return "HTX_PHDR_???";
769 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200770}
771
772static inline void htx_dump(struct htx *htx)
773{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100774 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200775
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100776 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
777 htx, htx->size, htx->data, htx->used,
778 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100779 (unsigned long long)htx->extra);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100780 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
781 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200782
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100783 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100784 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100785 struct htx_blk *blk = htx_get_blk(htx, pos);
786 enum htx_blk_type type = htx_get_blk_type(blk);
787 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
788 uint32_t sz = htx_get_blksz(blk);
789 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200790
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100791 n = htx_get_blk_name(htx, blk);
792 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200793
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100794 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100796 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
797 pos, htx_blk_type_str(type), sz, blk->addr,
798 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
799 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
800 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200801 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100802 else if (type == HTX_BLK_HDR)
803 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
804 pos, htx_blk_type_str(type), sz, blk->addr,
805 (int)n.len, n.ptr,
806 (int)v.len, v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100808 else if (type == HTX_BLK_PHDR)
809 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
810 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
811 (int)v.len, v.ptr);
812 else
813 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
814 pos, htx_blk_type_str(type), sz, blk->addr,
815 (!v.len ? "\t<empty>" : ""));
816 }
817 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200818}
819
Willy Tarreaub96b77e2018-12-11 10:22:41 +0100820#endif /* _COMMON_HTX_H */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821
822/*
823 * Local variables:
824 * c-indent-level: 8
825 * c-basic-offset: 8
826 * End:
827 */