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 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). */
97 enum htx_phdr_type {
98 HTX_PHDR_UNKNOWN = 0,
99 HTX_PHDR_SIZE,
100 };
101
102 /* HTTP block's type (max 15). */
103 enum 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 */
119 struct htx_blk {
120 uint32_t addr; /* relative storage address of a data block */
121 uint32_t info; /* information about data stored */
122 };
123
124 struct htx_ret {
125 int32_t ret;
126 struct htx_blk *blk;
127 };
128
129 struct 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 */
147 struct 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
167
168 extern struct htx htx_empty;
169
170 struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
171 struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
172 struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
173 void htx_truncate(struct htx *htx, uint32_t offset);
174 struct htx_ret htx_drain(struct htx *htx, uint32_t max);
175
176 struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
177 const struct ist old, const struct ist new);
178 struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
179 enum htx_blk_type mark);
180
181 struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
182 const struct ist p1, const struct ist p2, const struct ist p3);
183 struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
184 const struct ist p2, const struct ist p3);
185
186 struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
187 const struct ist name, const struct ist value);
188
189 struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
190 struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
191 struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
192 struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr, const struct ist value);
193 struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
194 struct htx_blk *htx_add_data(struct htx *htx, const struct ist data);
195 struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr);
196 struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob);
197 struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data);
198
199 int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
200 int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
201 int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
202 int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
203 int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk);
204
205 /* Functions and macros to get parts of the start-line or legnth of these
206 * parts
207 */
208 #define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
209
210 #define HTX_SL_P1_LEN(sl) ((sl)->len[0])
211 #define HTX_SL_P2_LEN(sl) ((sl)->len[1])
212 #define HTX_SL_P3_LEN(sl) ((sl)->len[2])
213 #define HTX_SL_P1_PTR(sl) ((sl)->l)
214 #define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
215 #define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
216
217 #define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
218 #define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
219 #define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
220 #define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
221 #define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
222 #define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
223
224 #define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
225 #define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
226 #define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
227 #define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
228 #define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
229 #define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
230
htx_sl_p1(const struct htx_sl * sl)231 static inline const struct ist htx_sl_p1(const struct htx_sl *sl)
232 {
233 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
234 }
235
htx_sl_p2(const struct htx_sl * sl)236 static inline const struct ist htx_sl_p2(const struct htx_sl *sl)
237 {
238 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
239 }
240
htx_sl_p3(const struct htx_sl * sl)241 static inline const struct ist htx_sl_p3(const struct htx_sl *sl)
242 {
243 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
244 }
245
htx_sl_req_meth(const struct htx_sl * sl)246 static inline const struct ist htx_sl_req_meth(const struct htx_sl *sl)
247 {
248 return htx_sl_p1(sl);
249 }
250
htx_sl_req_uri(const struct htx_sl * sl)251 static inline const struct ist htx_sl_req_uri(const struct htx_sl *sl)
252 {
253 return htx_sl_p2(sl);
254 }
255
htx_sl_req_vsn(const struct htx_sl * sl)256 static inline const struct ist htx_sl_req_vsn(const struct htx_sl *sl)
257 {
258 return htx_sl_p3(sl);
259 }
260
261
htx_sl_res_vsn(const struct htx_sl * sl)262 static inline const struct ist htx_sl_res_vsn(const struct htx_sl *sl)
263 {
264 return htx_sl_p1(sl);
265 }
266
htx_sl_res_code(const struct htx_sl * sl)267 static inline const struct ist htx_sl_res_code(const struct htx_sl *sl)
268 {
269 return htx_sl_p2(sl);
270 }
271
htx_sl_res_reason(const struct htx_sl * sl)272 static inline const struct ist htx_sl_res_reason(const struct htx_sl *sl)
273 {
274 return htx_sl_p3(sl);
275 }
276
277 /* Returns the HTX start-line if set, otherwise it returns NULL. */
htx_get_stline(struct htx * htx)278 static inline struct htx_sl *htx_get_stline(struct htx *htx)
279 {
280 struct htx_sl *sl = NULL;
281
282 if (htx->used && htx->sl_off != -1)
283 sl = ((void *)htx->blocks + htx->sl_off);
284
285 return sl;
286 }
287
288 /* Returns the array index of a block given its position <pos> */
htx_pos_to_idx(const struct htx * htx,uint32_t pos)289 static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
290 {
291 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
292 }
293
294 /* Returns the position of the block <blk> */
htx_get_blk_pos(const struct htx * htx,const struct htx_blk * blk)295 static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
296 {
297 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
298 }
299
300 /* Returns the block at the position <pos> */
htx_get_blk(const struct htx * htx,uint32_t pos)301 static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
302 {
303 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
304 }
305
306 /* Returns the type of the block <blk> */
htx_get_blk_type(const struct htx_blk * blk)307 static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
308 {
309 return (blk->info >> 28);
310 }
311
312 /* Returns the pseudo-header type of the block <blk>. If it's not a
313 * pseudo-header, HTX_PHDR_UNKNOWN is returned.
314 */
htx_get_blk_phdr(const struct htx_blk * blk)315 static inline enum htx_phdr_type htx_get_blk_phdr(const struct htx_blk *blk)
316 {
317 enum htx_blk_type type = htx_get_blk_type(blk);
318 enum htx_phdr_type phdr;
319
320 switch (type) {
321 case HTX_BLK_PHDR:
322 phdr = (blk->info & 0xff);
323 return phdr;
324
325 default:
326 /* Not a pseudo-header */
327 return HTX_PHDR_UNKNOWN;
328 }
329 }
330
331 /* Returns the size of the block <blk>, depending of its type */
htx_get_blksz(const struct htx_blk * blk)332 static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
333 {
334 enum htx_blk_type type = htx_get_blk_type(blk);
335
336 switch (type) {
337 case HTX_BLK_HDR:
338 /* name.length + value.length */
339 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
340 case HTX_BLK_PHDR:
341 /* value.length */
342 return ((blk->info >> 8) & 0xfffff);
343 default:
344 /* value.length */
345 return (blk->info & 0xfffffff);
346 }
347 }
348
349 /* Returns the position of the oldest entry (head).
350 *
351 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
352 * store on unsigned 32-bits integer, but it is impossible to have so much
353 * blocks to overflow a 32-bits signed integer !
354 */
htx_get_head(const struct htx * htx)355 static inline int32_t htx_get_head(const struct htx *htx)
356 {
357 if (!htx->used)
358 return -1;
359
360 return (((htx->tail + 1U < htx->used) ? htx->wrap : 0) + htx->tail + 1U - htx->used);
361 }
362
363 /* Returns the oldest HTX block (head) if the HTX message is not
364 * empty. Otherwise it returns NULL.
365 */
htx_get_head_blk(const struct htx * htx)366 static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
367 {
368 int32_t head = htx_get_head(htx);
369
370 return ((head == -1) ? NULL : htx_get_blk(htx, head));
371 }
372
373 /* Returns the type of the oldest HTX block (head) if the HTX message is not
374 * empty. Otherwise it returns HTX_BLK_UNUSED.
375 */
htx_get_head_type(const struct htx * htx)376 static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
377 {
378 struct htx_blk *blk = htx_get_head_blk(htx);
379
380 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
381 }
382
383 /* Returns the position of the newest entry (tail).
384 *
385 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
386 * store on unsigned 32-bits integer, but it is impossible to have so much
387 * blocks to overflow a 32-bits signed integer !
388 */
htx_get_tail(const struct htx * htx)389 static inline int32_t htx_get_tail(const struct htx *htx)
390 {
391 return (htx->used ? htx->tail : -1);
392 }
393
394 /* Returns the newest HTX block (tail) if the HTX message is not
395 * empty. Otherwise it returns NULL.
396 */
htx_get_tail_blk(const struct htx * htx)397 static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
398 {
399 int32_t tail = htx_get_tail(htx);
400
401 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
402 }
403
404 /* Returns the type of the newest HTX block (tail) if the HTX message is not
405 * empty. Otherwise it returns HTX_BLK_UNUSED.
406 */
htx_get_tail_type(const struct htx * htx)407 static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
408 {
409 struct htx_blk *blk = htx_get_tail_blk(htx);
410
411 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
412 }
413
414 /* Returns the position of block immediately before the one pointed by <pos>. If
415 * the message is empty or if <pos> is the position of the head, -1 returned.
416 *
417 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
418 * store on unsigned 32-bits integer, but it is impossible to have so much
419 * blocks to overflow a 32-bits signed integer !
420 */
htx_get_prev(const struct htx * htx,uint32_t pos)421 static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
422 {
423 int32_t head;
424
425 head = htx_get_head(htx);
426 if (head == -1 || pos == head)
427 return -1;
428 if (!pos)
429 return (htx->wrap - 1);
430 return (pos - 1);
431 }
432
433 /* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
434 * head, NULL returned.
435 */
htx_get_prev_blk(const struct htx * htx,const struct htx_blk * blk)436 static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
437 const struct htx_blk *blk)
438 {
439 int32_t pos;
440
441 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
442 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
443 }
444
445 /* Returns the position of block immediately after the one pointed by <pos>. If
446 * the message is empty or if <pos> is the position of the tail, -1 returned.
447 *
448 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
449 * store on unsigned 32-bits integer, but it is impossible to have so much
450 * blocks to overflow a 32-bits signed integer !
451 */
htx_get_next(const struct htx * htx,uint32_t pos)452 static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
453 {
454 if (!htx->used)
455 return -1;
456
457 if (pos == htx->tail)
458 return -1;
459 pos++;
460 if (pos >= htx->wrap)
461 pos = 0;
462 return pos;
463
464 }
465
466 /* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
467 * tail, NULL returned.
468 */
htx_get_next_blk(const struct htx * htx,const struct htx_blk * blk)469 static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
470 const struct htx_blk *blk)
471 {
472 int32_t pos;
473
474 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
475 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
476 }
477
htx_find_front(const struct htx * htx)478 static inline int32_t htx_find_front(const struct htx *htx)
479 {
480 int32_t front, pos;
481 uint32_t addr = 0;
482
483 if (!htx->used)
484 return -1;
485
486 front = -1;
487 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
488 struct htx_blk *blk = htx_get_blk(htx, pos);
489 enum htx_blk_type type = htx_get_blk_type(blk);
490
491 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
492 front = pos;
493 addr = blk->addr;
494 }
495 }
496
497 return front;
498 }
499
500 /* Returns the HTX block containing data with the <offset>, relatively to the
501 * beginning of the HTX message <htx>. It returns an htx_ret. if the HTX block is
502 * not found, htx_ret.blk is set to NULL. Otherwise, it points to the right HTX
503 * block and htx_ret.ret is set to the remaining offset inside the block.
504 */
htx_find_blk(const struct htx * htx,uint32_t offset)505 static inline struct htx_ret htx_find_blk(const struct htx *htx, uint32_t offset)
506 {
507 int32_t pos;
508
509 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
510 struct htx_blk *blk = htx_get_blk(htx, pos);
511 uint32_t sz = htx_get_blksz(blk);
512
513 if (offset < sz)
514 return (struct htx_ret){ .blk = blk, .ret = offset };
515 offset -= sz;
516 }
517
518 return (struct htx_ret){ .blk = NULL };
519 }
520 /* Changes the size of the value. It is the caller responsibility to change the
521 * value itself, make sure there is enough space and update allocated value.
522 */
htx_set_blk_value_len(struct htx_blk * blk,uint32_t vlen)523 static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
524 {
525 enum htx_blk_type type = htx_get_blk_type(blk);
526
527 switch (type) {
528 case HTX_BLK_HDR:
529 case HTX_BLK_PHDR:
530 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
531 break;
532 case HTX_BLK_REQ_SL:
533 case HTX_BLK_RES_SL:
534 case HTX_BLK_DATA:
535 case HTX_BLK_TLR:
536 case HTX_BLK_OOB:
537 blk->info = (type << 28) + vlen;
538 break;
539 default:
540 /* Unexpected case */
541 break;
542 }
543 }
544
545 /* Returns the data pointer of the block <blk> */
htx_get_blk_ptr(const struct htx * htx,const struct htx_blk * blk)546 static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
547 {
548 return ((void *)htx->blocks + blk->addr);
549 }
550
551 /* Returns the name of the block <blk>, only if it is a header. Otherwise it
552 * returns an empty name.
553 */
htx_get_blk_name(const struct htx * htx,const struct htx_blk * blk)554 static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
555 {
556 enum htx_blk_type type = htx_get_blk_type(blk);
557 struct ist ret;
558
559 switch (type) {
560 case HTX_BLK_HDR:
561 ret.ptr = htx_get_blk_ptr(htx, blk);
562 ret.len = blk->info & 0xff;
563 break;
564
565 default:
566 return ist("");
567 }
568 return ret;
569 }
570
571
572 /* Returns the value of the block <blk>, depending on its type. If there is no
573 * value, an empty one is retruned.
574 */
htx_get_blk_value(const struct htx * htx,const struct htx_blk * blk)575 static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
576 {
577 enum htx_blk_type type = htx_get_blk_type(blk);
578 struct ist ret;
579
580 switch (type) {
581 case HTX_BLK_HDR:
582 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
583 ret.len = (blk->info >> 8) & 0xfffff;
584 break;
585
586 case HTX_BLK_PHDR:
587 ret.ptr = htx_get_blk_ptr(htx, blk);
588 ret.len = (blk->info >> 8) & 0xfffff;
589 break;
590
591 case HTX_BLK_REQ_SL:
592 case HTX_BLK_RES_SL:
593 case HTX_BLK_DATA:
594 case HTX_BLK_TLR:
595 case HTX_BLK_OOB:
596 ret.ptr = htx_get_blk_ptr(htx, blk);
597 ret.len = blk->info & 0xfffffff;
598 break;
599
600 default:
601 return ist("");
602 }
603 return ret;
604 }
605
606 /* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
607 * address and its length are adjusted, and the htx's total data count is
608 * updated. This is used to mark that part of some data were transfered
609 * from a DATA block without removing this DATA block. No sanity check is
610 * performed, the caller is reponsible for doing this exclusively on DATA
611 * blocks, and never removing more than the block's size.
612 */
htx_cut_data_blk(struct htx * htx,struct htx_blk * blk,uint32_t n)613 static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
614 {
615 blk->addr += n;
616 blk->info -= n;
617 htx->data -= n;
618 }
619
620 /* Returns the space used by metadata in <htx>. */
htx_meta_space(const struct htx * htx)621 static inline uint32_t htx_meta_space(const struct htx *htx)
622 {
623 return (htx->used * sizeof(htx->blocks[0]));
624 }
625
626 /* Returns the space used (data + metadata) in <htx> */
htx_used_space(const struct htx * htx)627 static inline uint32_t htx_used_space(const struct htx *htx)
628 {
629 return (htx->data + htx_meta_space(htx));
630 }
631
632 /* Returns the free space in <htx> */
htx_free_space(const struct htx * htx)633 static inline uint32_t htx_free_space(const struct htx *htx)
634 {
635 return (htx->size - htx_used_space(htx));
636 }
637
638 /* Returns the maximum space usable for data in <htx>. This is in fact the
639 * maximum sice for a uniq block to fill the HTX message. */
htx_max_data_space(const struct htx * htx)640 static inline uint32_t htx_max_data_space(const struct htx *htx)
641 {
642 if (!htx->size)
643 return 0;
644 return (htx->size - sizeof(htx->blocks[0]));
645 }
646
647 /* Returns the maximum size available to store some data in <htx> if a new block
648 * is reserved.
649 */
htx_free_data_space(const struct htx * htx)650 static inline uint32_t htx_free_data_space(const struct htx *htx)
651 {
652 uint32_t free = htx_free_space(htx);
653
654 if (free < sizeof(htx->blocks[0]))
655 return 0;
656 return (free - sizeof(htx->blocks[0]));
657 }
658
659 /* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
htx_almost_full(const struct htx * htx)660 static inline int htx_almost_full(const struct htx *htx)
661 {
662 if (!htx->size || htx_free_space(htx) < htx->size / 4)
663 return 1;
664 return 0;
665 }
666
htx_reset(struct htx * htx)667 static inline void htx_reset(struct htx *htx)
668 {
669 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
670 htx->extra = 0;
671 htx->flags = HTX_FL_NONE;
672 htx->sl_off = -1;
673 }
674
675 /* returns the available room for raw data in buffer <buf> once HTX overhead is
676 * taken into account (one HTX header and two blocks). The purpose is to figure
677 * the optimal fill length to avoid copies.
678 */
buf_room_for_htx_data(const struct buffer * buf)679 static inline size_t buf_room_for_htx_data(const struct buffer *buf)
680 {
681 size_t room;
682
683 room = b_room(buf);
684 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
685 room = 0;
686 else
687 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
688
689 return room;
690 }
691
692
693 /* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
694 * function does not update to the buffer.
695 * Note that it always returns a valid pointer, either to an initialized buffer
696 * or to the empty buffer.
697 */
htxbuf(const struct buffer * buf)698 static inline struct htx *htxbuf(const struct buffer *buf)
699 {
700 struct htx *htx;
701
702 if (b_is_null(buf))
703 return &htx_empty;
704 htx = ((struct htx *)(buf->area));
705 if (!b_data(buf)) {
706 htx->size = buf->size - sizeof(*htx);
707 htx_reset(htx);
708 }
709 return htx;
710 }
711
712 /* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
713 * full. It is the caller responsibility to call htx_to_buf() when it finish to
714 * manipulate the HTX message to update <buf> accordingly. The returned pointer
715 * is always valid.
716 *
717 * The caller can call htxbuf() function to avoid any update of the buffer.
718 */
htx_from_buf(struct buffer * buf)719 static inline struct htx *htx_from_buf(struct buffer *buf)
720 {
721 struct htx *htx = htxbuf(buf);
722
723 b_set_data(buf, b_size(buf));
724 return htx;
725 }
726
727 /* Upate <buf> accordingly to the HTX message <htx> */
htx_to_buf(struct htx * htx,struct buffer * buf)728 static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
729 {
730 if (!htx->used && !(htx->flags & HTX_FL_PARSING_ERROR)) {
731 htx_reset(htx);
732 b_set_data(buf, 0);
733 }
734 else
735 b_set_data(buf, b_size(buf));
736 }
737
738 /* Returns 1 if the message is empty, otherwise it returns 0. Note that it is
739 * illegal to call this with htx == NULL.
740 */
htx_is_empty(const struct htx * htx)741 static inline int htx_is_empty(const struct htx *htx)
742 {
743 return !htx->used;
744 }
745
746 /* Returns 1 if the message is not empty, otherwise it returns 0. Note that it
747 * is illegal to call this with htx == NULL.
748 */
htx_is_not_empty(const struct htx * htx)749 static inline int htx_is_not_empty(const struct htx *htx)
750 {
751 return htx->used;
752 }
753
754 /* For debugging purpose */
htx_blk_type_str(enum htx_blk_type type)755 static inline const char *htx_blk_type_str(enum htx_blk_type type)
756 {
757 switch (type) {
758 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
759 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
760 case HTX_BLK_HDR: return "HTX_BLK_HDR";
761 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
762 case HTX_BLK_EOH: return "HTX_BLK_EOH";
763 case HTX_BLK_DATA: return "HTX_BLK_DATA";
764 case HTX_BLK_EOD: return "HTX_BLK_EOD";
765 case HTX_BLK_TLR: return "HTX_BLK_TLR";
766 case HTX_BLK_EOM: return "HTX_BLK_EOM";
767 case HTX_BLK_OOB: return "HTX_BLK_OOB";
768 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
769 default: return "HTX_BLK_???";
770 };
771 }
772
htx_blk_phdr_str(enum htx_phdr_type phdr)773 static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
774 {
775 switch (phdr) {
776 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
777 default: return "HTX_PHDR_???";
778 }
779 }
780
htx_dump(struct htx * htx)781 static inline void htx_dump(struct htx *htx)
782 {
783 int32_t pos;
784
785 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
786 htx, htx->size, htx->data, htx->used,
787 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
788 (unsigned long long)htx->extra);
789 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
790 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
791
792 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
793 struct htx_sl *sl;
794 struct htx_blk *blk = htx_get_blk(htx, pos);
795 enum htx_blk_type type = htx_get_blk_type(blk);
796 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
797 uint32_t sz = htx_get_blksz(blk);
798 struct ist n, v;
799
800 n = htx_get_blk_name(htx, blk);
801 v = htx_get_blk_value(htx, blk);
802
803 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
804 sl = htx_get_blk_ptr(htx, blk);
805 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
806 pos, htx_blk_type_str(type), sz, blk->addr,
807 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
808 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
809 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
810 }
811 else if (type == HTX_BLK_HDR)
812 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
813 pos, htx_blk_type_str(type), sz, blk->addr,
814 (int)n.len, n.ptr,
815 (int)v.len, v.ptr);
816
817 else if (type == HTX_BLK_PHDR)
818 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
819 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
820 (int)v.len, v.ptr);
821 else
822 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
823 pos, htx_blk_type_str(type), sz, blk->addr,
824 (!v.len ? "\t<empty>" : ""));
825 }
826 fprintf(stderr, "\n");
827 }
828
829 #endif /* _COMMON_HTX_H */
830
831 /*
832 * Local variables:
833 * c-indent-level: 8
834 * c-basic-offset: 8
835 * End:
836 */
837