1 /*
2  * include/common/htx.h
3  * 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 
22 #ifndef _COMMON_HTX_H
23 #define _COMMON_HTX_H
24 
25 #include <stdio.h>
26 #include <common/buf.h>
27 #include <common/config.h>
28 #include <common/ist.h>
29 #include <common/http.h>
30 #include <common/http-hdr.h>
31 #include <common/standard.h>
32 
33 /*
34  * The internal representation of an HTTP message, called HTX, is a structure
35  * with useful information on the message followed by a contiguous array
36  * containing parts of the message, called blocks. A block is composed of
37  * metadata (htx_blk) and the associated payload. Blocks' metadata are stored
38  * starting from the end of the array while their payload are stored at the
39  * beginning. Blocks' metadata are often simply called blocks. it is a misuse of
40  * language that's simplify explainations.
41  *
42  *
43  *  +-----+---------------+------------------------------+--------------+
44  *  | HTX |  PAYLOADS ==> |                              | <== HTX_BLKs |
45  *  +-----+---------------+------------------------------+--------------+
46  *        ^
47  *        blocks[] (the beginning of the bocks array)
48  *
49  *
50  * The blocks part remains linear and sorted. You may think about it as an array
51  * with negative indexes. But, instead of using negative indexes, we use
52  * positive positions to identify a block. This position is then converted to a
53  * address relatively to the beginning of the blocks array.
54  *
55  *
56  *      .....--+------------------------------+-----+-----+
57  *             |                       ...    | BLK | BLK |
58  *      .....--+------------------------------+-----+-----+
59  *                                            ^     ^
60  *                            Addr of the block     Addr of the block
61  *                            at the position 1     at the position 0
62  *
63  *
64  * The payloads part is a raw space that may wrap. You never access to a block's
65  * payload directly. Instead you get a block to retrieve the address of its
66  * payload. When no more space left between blocks and payloads parts, the free
67  * space at the beginning, if any, is used.
68  *
69  *
70  *        +----------- WRAPPING ------------------------+
71  *        |                                             |
72  *        V                                             |
73  *  +-----+-------------+---------------+---------------++--------------+
74  *  | HTX | PAYLOAD ==> |               |  PAYLOADS ==X || X== HTX_BLKs |
75  *  +-----+-------------+---------------+---------------++--------------+
76  *
77  *
78  * The blocks part, on its side, never wrap. If we have no space to allocate a
79  * new block and if there is a hole at the beginning of the blocks part (so at
80  * the end of the blocks array), we move back all blocks.x
81  *
82  *
83  *    ...+--------------+----------+   blocks  ...+----------+--------------+
84  *       | X== HTX_BLKS |          |   defrag     |          | <== HTX_BLKS |
85  *    ...+--------------+----------+   =====>  ...+----------+--------------+
86  *
87  *
88  * At the end, if payload wrapping or blocks defragmenation is not enough, some
89  * free space may be get back with a full defragmenation. This way, the holes in
90  * the middle are not reusable but count in the available free space. The only
91  * way to reuse this lost space is to fully defragmenate the HTX message.
92  *
93  *                                   - * -
94  *
95  * An HTX block is as well a header as a body part or a trailer. For all these
96  * types of block, a payload is attached to the block. It can also be a mark,
97  * like the end-of-headers or end-of-message. For these blocks, there is no
98  * payload but it count for a byte. It is important to not skip it when data are
99  * forwarded. Metadata of an HTX block are composed of 2 fields :
100  *
101  *     - .info : It a 32 bits field containing the block's type on 4 bits
102  *               followed by the payload length. See below for details.
103  *
104  *     - .addr : The payload's address, if any, relatively to the beginning the
105  *               array used to store the HTX message itself.
106  *
107  * htx_blk.info representation :
108  *
109  *   0b 0000 0000 0000 0000 0000 0000 0000 0000
110  *      ---- ------------------------ ---------
111  *      type     value (1 MB max)     name length (header/trailer)
112  *           ----------------------------------
113  *                data length (256 MB max)
114  *    (body, method, path, version, status, reason)
115  *
116  *   types :
117  *     - 0000 = request  start-line
118  *     - 0001 = response start-line
119  *     - 0010 = header
120  *     - 0011 = pseudo-header ou "special" header
121  *     - 0100 = end-of-headers
122  *     - 0101 = data
123  *     - 0110 = trailer
124  *     - 0111 = end-of-trailers
125  *     - 1000 = end-of-message
126  *       ...
127  *     - 1111 = unused
128  *
129  */
130 
131 /* HTX start-line flags */
132 #define HTX_SL_F_NONE          0x00000000
133 #define HTX_SL_F_IS_RESP       0x00000001 /* It is the response start-line (unset means the request one) */
134 #define HTX_SL_F_XFER_LEN      0x00000002 /* The message xfer size can be dertermined */
135 #define HTX_SL_F_XFER_ENC      0x00000004 /* The transfer-encoding header was found in message */
136 #define HTX_SL_F_CLEN          0x00000008 /* The content-length header was found in message */
137 #define HTX_SL_F_CHNK          0x00000010 /* The message payload is chunked */
138 #define HTX_SL_F_VER_11        0x00000020 /* The message indicates version 1.1 or above */
139 #define HTX_SL_F_BODYLESS      0x00000040 /* The message has no body (content-length = 0) */
140 #define HTX_SL_F_HAS_SCHM      0x00000080 /* The scheme is explicitly specified */
141 #define HTX_SL_F_SCHM_HTTP     0x00000100 /* The scheme HTTP should be used */
142 #define HTX_SL_F_SCHM_HTTPS    0x00000200 /* The scheme HTTPS should be used */
143 
144 /* HTX flags */
145 #define HTX_FL_NONE              0x00000000
146 #define HTX_FL_PARSING_ERROR     0x00000001 /* Set when a parsing error occurred */
147 #define HTX_FL_UPGRADE           0x00000002 /* Set when an upgrade is in progress */
148 #define HTX_FL_FRAGMENTED        0x00000020 /* Set when the HTX buffer is fragmented */
149 
150 
151 /* HTX block's type (max 15). */
152 enum htx_blk_type {
153 	HTX_BLK_REQ_SL =  0, /* Request start-line */
154 	HTX_BLK_RES_SL =  1, /* Response start-line */
155 	HTX_BLK_HDR    =  2, /* header name/value block */
156 	HTX_BLK_EOH    =  3, /* end-of-headers block */
157 	HTX_BLK_DATA   =  4, /* data block */
158 	HTX_BLK_TLR    =  5, /* trailer name/value block */
159 	HTX_BLK_EOT    =  6, /* end-of-trailers block */
160 	HTX_BLK_EOM    =  7, /* end-of-message block */
161 	/* 8 .. 14 unused */
162 	HTX_BLK_UNUSED = 15, /* unused/removed block */
163 };
164 
165 /* One HTX block descriptor */
166 struct htx_blk {
167 	uint32_t addr; /* relative storage address of the block's payload */
168 	uint32_t info; /* information about the block (type, length) */
169 };
170 
171 /* Composite return value used by some HTX functions */
172 struct htx_ret {
173 	int32_t ret;         /* A numerical value */
174 	struct htx_blk *blk; /* An HTX block */
175 };
176 
177 /* HTX start-line */
178 struct htx_sl {
179 	unsigned int flags; /* HTX_SL_F_* */
180 	union {
181 		struct {
182 			enum http_meth_t meth;   /* method */
183 		} req;
184 		struct {
185 			uint16_t         status; /* status code */
186 		} res;
187 	} info;
188 
189 	/* XXX 2 bytes unused */
190 
191 	int32_t hdrs_bytes;  /* Bytes held by all headers, as seen by the mux
192 			      * during parsing, from this start-line to the
193 			      * corresponding EOH. -1 if unknown */
194 
195 	unsigned int len[3]; /* length of differnt parts of the start-line */
196 	char         l[0];
197 };
198 
199 /* Internal representation of an HTTP message */
200 struct htx {
201 	uint32_t size;   /* the array size, in bytes, used to store the HTTP message itself */
202 	uint32_t data;   /* the data size, in bytes. To known to total size used by all allocated
203 			  * blocks (blocks and their contents), you need to add size used by blocks,
204 			  * i.e. [ used * sizeof(struct htx_blk *) ] */
205 
206 	uint32_t used;   /* number of blocks in use */
207 	uint32_t tail;   /* newest inserted block. -1 if the HTX message is empty */
208 	uint32_t head;   /* oldest inserted block. -1 if the HTX message is empty */
209 
210 	uint32_t tail_addr; /* start address of the free space in front of the the blocks table */
211 	uint32_t head_addr; /* start address of the free space at the beginning */
212 	uint32_t end_addr;  /* end address of the free space at the beginning */
213 
214 
215 	uint64_t extra;  /* known bytes amount remaining to receive */
216 	uint32_t flags;  /* HTX_FL_* */
217 
218 	int32_t  first;  /* position of the first block to (re)start the analyse. -1 if unset. */
219 
220 	struct htx_blk blocks[0]; /* Blocks representing the HTTP message itself */
221 };
222 
223 
224 extern struct htx htx_empty;
225 
226 struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk, uint32_t info);
227 struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
228 struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
229 struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset);
230 void htx_truncate(struct htx *htx, uint32_t offset);
231 struct htx_ret htx_drain(struct htx *htx, uint32_t max);
232 
233 struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
234 				      const struct ist old, const struct ist new);
235 struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
236 			     enum htx_blk_type mark);
237 
238 struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
239 			      const struct ist p1, const struct ist p2, const struct ist p3);
240 struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
241 				  const struct ist p2, const struct ist p3);
242 
243 struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
244 				   const struct ist name, const struct ist value);
245 
246 struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
247 struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, const struct ist value);
248 struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
249 struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
250 struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs);
251 struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
252 struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data);
253 size_t htx_add_data(struct htx *htx, const struct ist data);
254 struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data);
255 void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref);
256 
257 int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
258 int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
259 int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
260 int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
261 
262 /* Functions and macros to get parts of the start-line or legnth of these
263  * parts. Request and response start-lines are both composed of 3 parts.
264  */
265 #define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
266 
267 #define HTX_SL_P1_LEN(sl) ((sl)->len[0])
268 #define HTX_SL_P2_LEN(sl) ((sl)->len[1])
269 #define HTX_SL_P3_LEN(sl) ((sl)->len[2])
270 #define HTX_SL_P1_PTR(sl) ((sl)->l)
271 #define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
272 #define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
273 
274 #define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
275 #define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
276 #define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
277 #define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
278 #define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
279 #define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
280 
281 #define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
282 #define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
283 #define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
284 #define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
285 #define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
286 #define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
287 
htx_sl_p1(const struct htx_sl * sl)288 static inline struct ist htx_sl_p1(const struct htx_sl *sl)
289 {
290 	return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
291 }
292 
htx_sl_p2(const struct htx_sl * sl)293 static inline struct ist htx_sl_p2(const struct htx_sl *sl)
294 {
295 	return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
296 }
297 
htx_sl_p3(const struct htx_sl * sl)298 static inline struct ist htx_sl_p3(const struct htx_sl *sl)
299 {
300 	return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
301 }
302 
htx_sl_req_meth(const struct htx_sl * sl)303 static inline struct ist htx_sl_req_meth(const struct htx_sl *sl)
304 {
305 	return htx_sl_p1(sl);
306 }
307 
htx_sl_req_uri(const struct htx_sl * sl)308 static inline struct ist htx_sl_req_uri(const struct htx_sl *sl)
309 {
310 	return htx_sl_p2(sl);
311 }
312 
htx_sl_req_vsn(const struct htx_sl * sl)313 static inline struct ist htx_sl_req_vsn(const struct htx_sl *sl)
314 {
315 	return htx_sl_p3(sl);
316 }
317 
318 
htx_sl_res_vsn(const struct htx_sl * sl)319 static inline struct ist htx_sl_res_vsn(const struct htx_sl *sl)
320 {
321 	return htx_sl_p1(sl);
322 }
323 
htx_sl_res_code(const struct htx_sl * sl)324 static inline struct ist htx_sl_res_code(const struct htx_sl *sl)
325 {
326 	return htx_sl_p2(sl);
327 }
328 
htx_sl_res_reason(const struct htx_sl * sl)329 static inline struct ist htx_sl_res_reason(const struct htx_sl *sl)
330 {
331 	return htx_sl_p3(sl);
332 }
333 
334 /* Converts a position to the corresponding relative address */
htx_pos_to_idx(const struct htx * htx,uint32_t pos)335 static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
336 {
337 	return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
338 }
339 
340 /* Returns the position of the block <blk>. It is the caller responsibility to
341  * be sure <blk> is part of <htx>. */
htx_get_blk_pos(const struct htx * htx,const struct htx_blk * blk)342 static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
343 {
344 	return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
345 }
346 
347 /* Returns the block at the position <pos>. It is the caller responsibility to
348  * be sure the block at the position <pos> exists. */
htx_get_blk(const struct htx * htx,uint32_t pos)349 static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
350 {
351 	return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
352 }
353 
354 /* Returns the type of the block <blk> */
htx_get_blk_type(const struct htx_blk * blk)355 static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
356 {
357 	return (blk->info >> 28);
358 }
359 
360 /* Returns the size of the block <blk>, depending of its type */
htx_get_blksz(const struct htx_blk * blk)361 static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
362 {
363 	enum htx_blk_type type = htx_get_blk_type(blk);
364 
365 	switch (type) {
366 		case HTX_BLK_HDR:
367 		case HTX_BLK_TLR:
368 			/*       name.length       +        value.length        */
369 			return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
370 		default:
371 			/*         value.length      */
372 			return (blk->info & 0xfffffff);
373 	}
374 }
375 
376 /* Returns the position of the oldest entry (head).
377  *
378  * An signed 32-bits integer is returned to handle -1 case. Blocks position are
379  * store on unsigned 32-bits integer, but it is impossible to have so much
380  * blocks to overflow a 32-bits signed integer !
381  */
htx_get_head(const struct htx * htx)382 static inline int32_t htx_get_head(const struct htx *htx)
383 {
384 	return (htx->used ? htx->head : -1);
385 }
386 
387 /* Returns the oldest HTX block (head) if the HTX message is not
388  * empty. Otherwise it returns NULL.
389  */
htx_get_head_blk(const struct htx * htx)390 static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
391 {
392 	int32_t head = htx_get_head(htx);
393 
394 	return ((head == -1) ? NULL : htx_get_blk(htx, head));
395 }
396 
397 /* Returns the type of the oldest HTX block (head) if the HTX message is not
398  * empty. Otherwise it returns HTX_BLK_UNUSED.
399  */
htx_get_head_type(const struct htx * htx)400 static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
401 {
402 	struct htx_blk *blk = htx_get_head_blk(htx);
403 
404 	return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
405 }
406 
407 /* Returns the position of the newest entry (tail).
408  *
409  * An signed 32-bits integer is returned to handle -1 case. Blocks position are
410  * store on unsigned 32-bits integer, but it is impossible to have so much
411  * blocks to overflow a 32-bits signed integer !
412  */
htx_get_tail(const struct htx * htx)413 static inline int32_t htx_get_tail(const struct htx *htx)
414 {
415 	return (htx->used ? htx->tail : -1);
416 }
417 
418 /* Returns the newest HTX block (tail) if the HTX message is not
419  * empty. Otherwise it returns NULL.
420  */
htx_get_tail_blk(const struct htx * htx)421 static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
422 {
423 	int32_t tail = htx_get_tail(htx);
424 
425 	return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
426 }
427 
428 /* Returns the type of the newest HTX block (tail) if the HTX message is not
429  * empty. Otherwise it returns HTX_BLK_UNUSED.
430  */
htx_get_tail_type(const struct htx * htx)431 static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
432 {
433 	struct htx_blk *blk = htx_get_tail_blk(htx);
434 
435 	return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
436 }
437 
438 /* Returns the position of the first block in the HTX message <htx>. If unset,
439  * or if <htx> is empty, -1 is returned.
440  *
441  * An signed 32-bits integer is returned to handle -1 case. Blocks position are
442  * store on unsigned 32-bits integer, but it is impossible to have so much
443  * blocks to overflow a 32-bits signed integer !
444  */
htx_get_first(const struct htx * htx)445 static inline int32_t htx_get_first(const struct htx *htx)
446 {
447 	if (!htx->used)
448 		return -1;
449 	return htx->first;
450 }
451 
452 /* Returns the first HTX block in the HTX message <htx>. If unset or if <htx> is
453  * empty, NULL returned.
454  */
htx_get_first_blk(const struct htx * htx)455 static inline struct htx_blk *htx_get_first_blk(const struct htx *htx)
456 {
457 	int32_t pos;
458 
459 	pos = htx_get_first(htx);
460 	return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
461 }
462 
463 /* Returns the type of the first block in the HTX message <htx>. If unset or if
464  * <htx> is empty, HTX_BLK_UNUSED is returned.
465  */
htx_get_first_type(const struct htx * htx)466 static inline enum htx_blk_type htx_get_first_type(const struct htx *htx)
467 {
468 	struct htx_blk *blk = htx_get_first_blk(htx);
469 
470 	return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
471 }
472 
473 /* Returns the position of block immediately before the one pointed by <pos>. If
474  * the message is empty or if <pos> is the position of the head, -1 returned.
475  *
476  * An signed 32-bits integer is returned to handle -1 case. Blocks position are
477  * store on unsigned 32-bits integer, but it is impossible to have so much
478  * blocks to overflow a 32-bits signed integer !
479  */
htx_get_prev(const struct htx * htx,uint32_t pos)480 static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
481 {
482 	int32_t head;
483 
484 	head = htx_get_head(htx);
485 	if (head == -1 || pos == head)
486 		return -1;
487 	return (pos - 1);
488 }
489 
490 /* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
491  * head, NULL returned.
492  */
htx_get_prev_blk(const struct htx * htx,const struct htx_blk * blk)493 static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
494 					       const struct htx_blk *blk)
495 {
496 	int32_t pos;
497 
498 	pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
499 	return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
500 }
501 
502 /* Returns the position of block immediately after the one pointed by <pos>. If
503  * the message is empty or if <pos> is the position of the tail, -1 returned.
504  *
505  * An signed 32-bits integer is returned to handle -1 case. Blocks position are
506  * store on unsigned 32-bits integer, but it is impossible to have so much
507  * blocks to overflow a 32-bits signed integer !
508  */
htx_get_next(const struct htx * htx,uint32_t pos)509 static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
510 {
511 	if (!htx->used || pos == htx->tail)
512 		return -1;
513 	return (pos + 1);
514 
515 }
516 
517 /* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
518  * tail, NULL returned.
519  */
htx_get_next_blk(const struct htx * htx,const struct htx_blk * blk)520 static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
521 					       const struct htx_blk *blk)
522 {
523 	int32_t pos;
524 
525 	pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
526 	return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
527 }
528 
529 /* Changes the size of the value. It is the caller responsibility to change the
530  * value itself, make sure there is enough space and update allocated
531  * value. This function updates the HTX message accordingly.
532  */
htx_change_blk_value_len(struct htx * htx,struct htx_blk * blk,uint32_t newlen)533 static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk, uint32_t newlen)
534 {
535 	enum htx_blk_type type = htx_get_blk_type(blk);
536 	uint32_t oldlen, sz;
537 	int32_t delta;
538 
539 	sz = htx_get_blksz(blk);
540 	switch (type) {
541 		case HTX_BLK_HDR:
542 		case HTX_BLK_TLR:
543 			oldlen = (blk->info >> 8) & 0xfffff;
544 			blk->info = (type << 28) + (newlen << 8) + (blk->info & 0xff);
545 			break;
546 		default:
547 			oldlen = blk->info & 0xfffffff;
548 			blk->info = (type << 28) + newlen;
549 			break;
550 	}
551 
552 	/* Update HTTP message */
553 	delta = (newlen - oldlen);
554 	htx->data += delta;
555 	if (blk->addr+sz == htx->tail_addr)
556 		htx->tail_addr += delta;
557 	else if (blk->addr+sz == htx->head_addr)
558 		htx->head_addr += delta;
559 }
560 
561 /* Changes the size of the value. It is the caller responsibility to change the
562  * value itself, make sure there is enough space and update allocated
563  * value. Unlike the function htx_change_blk_value_len(), this one does not
564  * update the HTX message. So it should be used with caution.
565  */
htx_set_blk_value_len(struct htx_blk * blk,uint32_t vlen)566 static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
567 {
568 	enum htx_blk_type type = htx_get_blk_type(blk);
569 
570 	switch (type) {
571 		case HTX_BLK_HDR:
572 		case HTX_BLK_TLR:
573 			blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
574 			break;
575 		case HTX_BLK_REQ_SL:
576 		case HTX_BLK_RES_SL:
577 		case HTX_BLK_DATA:
578 			blk->info = (type << 28) + vlen;
579 			break;
580 		default:
581 			/* Unexpected case */
582 			break;
583 	}
584 }
585 
586 /* Returns the data pointer of the block <blk> */
htx_get_blk_ptr(const struct htx * htx,const struct htx_blk * blk)587 static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
588 {
589 	return ((void *)htx->blocks + blk->addr);
590 }
591 
592 /* Returns the name of the block <blk>, only if it is a header or a
593  * trailer. Otherwise it returns an empty string.
594  */
htx_get_blk_name(const struct htx * htx,const struct htx_blk * blk)595 static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
596 {
597 	enum htx_blk_type type = htx_get_blk_type(blk);
598 	struct ist ret;
599 
600 	switch (type) {
601 		case HTX_BLK_HDR:
602 		case HTX_BLK_TLR:
603 			ret.ptr = htx_get_blk_ptr(htx, blk);
604 			ret.len = blk->info & 0xff;
605 			break;
606 
607 		default:
608 			return ist("");
609 	}
610 	return ret;
611 }
612 
613 
614 /* Returns the value of the block <blk>, depending on its type. If there is no
615  * value (for end-of blocks), an empty one is retruned.
616  */
htx_get_blk_value(const struct htx * htx,const struct htx_blk * blk)617 static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
618 {
619 	enum htx_blk_type type = htx_get_blk_type(blk);
620 	struct ist ret;
621 
622 	switch (type) {
623 		case HTX_BLK_HDR:
624 		case HTX_BLK_TLR:
625 			ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
626 			ret.len = (blk->info >> 8) & 0xfffff;
627 			break;
628 
629 		case HTX_BLK_REQ_SL:
630 		case HTX_BLK_RES_SL:
631 		case HTX_BLK_DATA:
632 			ret.ptr = htx_get_blk_ptr(htx, blk);
633 			ret.len = blk->info & 0xfffffff;
634 			break;
635 
636 		default:
637 			return ist("");
638 	}
639 	return ret;
640 }
641 
642 /* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
643  * address and its length are adjusted, and the htx's total data count is
644  * updated. This is used to mark that part of some data were transfered
645  * from a DATA block without removing this DATA block. No sanity check is
646  * performed, the caller is reponsible for doing this exclusively on DATA
647  * blocks, and never removing more than the block's size.
648  */
htx_cut_data_blk(struct htx * htx,struct htx_blk * blk,uint32_t n)649 static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
650 {
651 	if (blk->addr == htx->end_addr)
652 		htx->end_addr += n;
653 	blk->addr += n;
654 	blk->info -= n;
655 	htx->data -= n;
656 }
657 
658 /* Returns the space used by metadata in <htx>. */
htx_meta_space(const struct htx * htx)659 static inline uint32_t htx_meta_space(const struct htx *htx)
660 {
661 	return (htx->used * sizeof(htx->blocks[0]));
662 }
663 
664 /* Returns the space used (payload + metadata) in <htx> */
htx_used_space(const struct htx * htx)665 static inline uint32_t htx_used_space(const struct htx *htx)
666 {
667 	return (htx->data + htx_meta_space(htx));
668 }
669 
670 /* Returns the free space in <htx> */
htx_free_space(const struct htx * htx)671 static inline uint32_t htx_free_space(const struct htx *htx)
672 {
673 	return (htx->size - htx_used_space(htx));
674 }
675 
676 /* Returns the maximum size available to store some data in <htx> if a new block
677  * is reserved.
678  */
htx_free_data_space(const struct htx * htx)679 static inline uint32_t htx_free_data_space(const struct htx *htx)
680 {
681 	uint32_t free = htx_free_space(htx);
682 
683 	if (free < sizeof(htx->blocks[0]))
684 		return 0;
685 	return (free - sizeof(htx->blocks[0]));
686 }
687 
688 /* Returns non-zero only if the HTX message free space wraps */
htx_space_wraps(const struct htx * htx)689 static inline int htx_space_wraps(const struct htx *htx)
690 {
691 	uint32_t headroom, tailroom;
692 
693 	headroom = (htx->end_addr - htx->head_addr);
694 	tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
695 
696 	return (headroom && tailroom);
697 }
698 
699 /* Returns the maximum size for a block, not exceeding <max> bytes. <max> may be
700  * set to -1 to have no limit.
701  */
htx_get_max_blksz(const struct htx * htx,int32_t max)702 static inline uint32_t htx_get_max_blksz(const struct htx *htx, int32_t max)
703 {
704 	uint32_t free = htx_free_space(htx);
705 
706 	if (max != -1 && free > max)
707 		free = max;
708 	if (free < sizeof(htx->blocks[0]))
709 		return 0;
710 	return (free - sizeof(htx->blocks[0]));
711 }
712 
713 /* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
htx_almost_full(const struct htx * htx)714 static inline int htx_almost_full(const struct htx *htx)
715 {
716 	if (!htx->size || htx_free_space(htx) < htx->size / 4)
717 		return 1;
718 	return 0;
719 }
720 
721 /* Resets an HTX message */
htx_reset(struct htx * htx)722 static inline void htx_reset(struct htx *htx)
723 {
724 	htx->data = htx->used = htx->tail = htx->head  = 0;
725 	htx->tail_addr = htx->head_addr = htx->end_addr = 0;
726 	htx->extra = 0;
727 	htx->flags = HTX_FL_NONE;
728 	htx->first = -1;
729 }
730 
731 /* Returns the available room for raw data in buffer <buf> once HTX overhead is
732  * taken into account (one HTX header and two blocks). The purpose is to figure
733  * the optimal fill length to avoid copies.
734  */
buf_room_for_htx_data(const struct buffer * buf)735 static inline size_t buf_room_for_htx_data(const struct buffer *buf)
736 {
737 	size_t room;
738 
739 	room = b_room(buf);
740 	if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
741 		room = 0;
742 	else
743 		room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
744 
745 	return room;
746 }
747 
748 
749 /* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
750  * function does not update the buffer. So if the HTX message is updated, the
751  * caller must call htx_to_buf() to be sure to also update the underlying buffer
752  * accordingly.  Note that it always returns a valid pointer, either to an
753  * initialized buffer or to the empty buffer. This function must always be
754  * called with a buffer containing an HTX message (or an empty buffer).
755  */
htxbuf(const struct buffer * buf)756 static inline struct htx *htxbuf(const struct buffer *buf)
757 {
758 	struct htx *htx;
759 
760 	if (b_is_null(buf))
761 		return &htx_empty;
762 	htx = ((struct htx *)(buf->area));
763 	if (!b_data(buf)) {
764 		htx->size = buf->size - sizeof(*htx);
765 		htx_reset(htx);
766 	}
767 	return htx;
768 }
769 
770 /* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
771  * full. It should be used when you want to add something into the HTX message,
772  * so the call to htx_to_buf() may be skipped. But, it is the caller
773  * responsibility to call htx_to_buf() to reset <buf> if it is relevant. The
774  * returned pointer is always valid. This function must always be called with a
775  * buffer containing an HTX message (or an empty buffer).
776  *
777  * The caller can call htxbuf() function to avoid any update of the buffer.
778  */
htx_from_buf(struct buffer * buf)779 static inline struct htx *htx_from_buf(struct buffer *buf)
780 {
781 	struct htx *htx = htxbuf(buf);
782 
783 	b_set_data(buf, b_size(buf));
784 	return htx;
785 }
786 
787 /* Update <buf> accordingly to the HTX message <htx> */
htx_to_buf(struct htx * htx,struct buffer * buf)788 static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
789 {
790 	if (!htx->used && !(htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_UPGRADE))) {
791 		htx_reset(htx);
792 		b_set_data(buf, 0);
793 	}
794 	else
795 		b_set_data(buf, b_size(buf));
796 }
797 
798 /* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
799  * illegal to call this with htx == NULL.
800  */
htx_is_empty(const struct htx * htx)801 static inline int htx_is_empty(const struct htx *htx)
802 {
803 	return !htx->used;
804 }
805 
806 /* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
807  * is illegal to call this with htx == NULL.
808  */
htx_is_not_empty(const struct htx * htx)809 static inline int htx_is_not_empty(const struct htx *htx)
810 {
811 	return htx->used;
812 }
813 
htx_skip_msg_payload(struct htx * htx)814 static inline void htx_skip_msg_payload(struct htx *htx)
815 {
816 	struct htx_blk *blk = htx_get_first_blk(htx);
817 
818 	while (blk) {
819 		enum htx_blk_type type = htx_get_blk_type(blk);
820 
821 		blk = ((type > HTX_BLK_EOH && type < HTX_BLK_EOM)
822 		       ? htx_remove_blk(htx, blk)
823 		       : htx_get_next_blk(htx, blk));
824 	}
825 }
826 
827 /* For debugging purpose */
htx_blk_type_str(enum htx_blk_type type)828 static inline const char *htx_blk_type_str(enum htx_blk_type type)
829 {
830 	switch (type) {
831 		case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
832 		case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
833 		case HTX_BLK_HDR:    return "HTX_BLK_HDR";
834 		case HTX_BLK_EOH:    return "HTX_BLK_EOH";
835 		case HTX_BLK_DATA:   return "HTX_BLK_DATA";
836 		case HTX_BLK_TLR:    return "HTX_BLK_TLR";
837 		case HTX_BLK_EOT:    return "HTX_BLK_EOT";
838 		case HTX_BLK_EOM:    return "HTX_BLK_EOM";
839 		case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
840 		default:             return "HTX_BLK_???";
841 	};
842 }
843 
844 /* For debugging purpose */
htx_dump(struct htx * htx)845 static inline void htx_dump(struct htx *htx)
846 {
847 	int32_t pos;
848 
849 	fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
850 		htx, htx->size, htx->data, htx->used, (!htx->head_addr) ? "NO" : "YES",
851 		(unsigned long long)htx->extra);
852 	fprintf(stderr, "\tfirst=%d - head=%u, tail=%u\n",
853 		htx->first, htx->head, htx->tail);
854 	fprintf(stderr, "\ttail_addr=%d - head_addr=%u, end_addr=%u\n",
855 		htx->tail_addr, htx->head_addr, htx->end_addr);
856 
857 	for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
858 		struct htx_sl     *sl;
859 		struct htx_blk    *blk  = htx_get_blk(htx, pos);
860 		enum htx_blk_type  type = htx_get_blk_type(blk);
861 		uint32_t           sz   = htx_get_blksz(blk);
862 		struct ist         n, v;
863 
864 		n = htx_get_blk_name(htx, blk);
865 		v = htx_get_blk_value(htx, blk);
866 
867 		if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
868 			sl = htx_get_blk_ptr(htx, blk);
869 			fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
870 				pos, htx_blk_type_str(type), sz, blk->addr,
871 				HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
872 				HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
873 				HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
874 		}
875 		else if (type == HTX_BLK_HDR || type == HTX_BLK_TLR)
876 			fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
877 				pos, htx_blk_type_str(type), sz, blk->addr,
878 				(int)n.len, n.ptr,
879 				(int)v.len, v.ptr);
880 		else
881 			fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
882 				pos, htx_blk_type_str(type), sz, blk->addr,
883 				(!v.len ? "\t<empty>" : ""));
884 	}
885 	fprintf(stderr, "\n");
886 }
887 
888 #endif /* _COMMON_HTX_H */
889 
890 /*
891  * Local variables:
892  *  c-indent-level: 8
893  *  c-basic-offset: 8
894  * End:
895  */
896