blob: 3fd8fbab19dc0224b999180bbd47e6817aade435 [file] [log] [blame]
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001/*
2 * include/proto/hx.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 _PROTO_HTX_H
23#define _PROTO_HTX_H
24
25#include <common/buf.h>
26#include <common/config.h>
27#include <common/standard.h>
28#include <common/http-hdr.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020029#include <types/htx.h>
30
31extern struct htx htx_empty;
32
33struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
34struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
35struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
36
37struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010038 const struct ist old, const struct ist new);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020039struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
40 enum htx_blk_type mark);
41
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010042struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
43 const struct ist p1, const struct ist p2, const struct ist p3);
44struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
45 const struct ist p2, const struct ist p3);
46
Christopher Fauleta3d2a162018-10-22 08:59:39 +020047struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010048 const struct ist name, const struct ist value);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020049
Christopher Fauleta3d2a162018-10-22 08:59:39 +020050struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
51struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
52struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr, const struct ist value);
53struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
54struct htx_blk *htx_add_data(struct htx *htx, const struct ist data);
55struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr);
56struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob);
Christopher Faulet24ed8352018-11-22 11:20:43 +010057struct 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 +020058
Christopher Fauletc59ff232018-12-03 13:58:44 +010059int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
60int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
61int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk);
62int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked);
63int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Faulet570d1612018-11-26 11:13:57 +010065/* Functions and macros to get parts of the start-line or legnth of these
66 * parts
67 */
68#define HTX_SL_LEN(sl) ((sl)->len[0] + (sl)->len[1] + (sl)->len[2])
69
70#define HTX_SL_P1_LEN(sl) ((sl)->len[0])
71#define HTX_SL_P2_LEN(sl) ((sl)->len[1])
72#define HTX_SL_P3_LEN(sl) ((sl)->len[2])
73#define HTX_SL_P1_PTR(sl) ((sl)->l)
74#define HTX_SL_P2_PTR(sl) (HTX_SL_P1_PTR(sl) + HTX_SL_P1_LEN(sl))
75#define HTX_SL_P3_PTR(sl) (HTX_SL_P2_PTR(sl) + HTX_SL_P2_LEN(sl))
76
77#define HTX_SL_REQ_MLEN(sl) HTX_SL_P1_LEN(sl)
78#define HTX_SL_REQ_ULEN(sl) HTX_SL_P2_LEN(sl)
79#define HTX_SL_REQ_VLEN(sl) HTX_SL_P3_LEN(sl)
80#define HTX_SL_REQ_MPTR(sl) HTX_SL_P1_PTR(sl)
81#define HTX_SL_REQ_UPTR(sl) HTX_SL_P2_PTR(sl)
82#define HTX_SL_REQ_VPTR(sl) HTX_SL_P3_PTR(sl)
83
84#define HTX_SL_RES_VLEN(sl) HTX_SL_P1_LEN(sl)
85#define HTX_SL_RES_CLEN(sl) HTX_SL_P2_LEN(sl)
86#define HTX_SL_RES_RLEN(sl) HTX_SL_P3_LEN(sl)
87#define HTX_SL_RES_VPTR(sl) HTX_SL_P1_PTR(sl)
88#define HTX_SL_RES_CPTR(sl) HTX_SL_P2_PTR(sl)
89#define HTX_SL_RES_RPTR(sl) HTX_SL_P3_PTR(sl)
90
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010091 static inline const struct ist htx_sl_p1(const struct htx_sl *sl)
92 {
93 return ist2(HTX_SL_P1_PTR(sl), HTX_SL_P1_LEN(sl));
94 }
Christopher Faulet570d1612018-11-26 11:13:57 +010095
96static inline const struct ist htx_sl_p2(const struct htx_sl *sl)
97{
98 return ist2(HTX_SL_P2_PTR(sl), HTX_SL_P2_LEN(sl));
99}
100
101static inline const struct ist htx_sl_p3(const struct htx_sl *sl)
102{
103 return ist2(HTX_SL_P3_PTR(sl), HTX_SL_P3_LEN(sl));
104}
105
106
107static inline const struct ist htx_sl_req_meth(const struct htx_sl *sl)
108{
109 return htx_sl_p1(sl);
110}
111
112static inline const struct ist htx_sl_req_uri(const struct htx_sl *sl)
113{
114 return htx_sl_p2(sl);
115}
116
117static inline const struct ist htx_sl_req_vsn(const struct htx_sl *sl)
118{
119 return htx_sl_p3(sl);
120}
121
122
123static inline const struct ist htx_sl_res_vsn(const struct htx_sl *sl)
124{
125 return htx_sl_p1(sl);
126}
127
128static inline const struct ist htx_sl_res_code(const struct htx_sl *sl)
129{
130 return htx_sl_p2(sl);
131}
132
133static inline const struct ist htx_sl_res_reason(const struct htx_sl *sl)
134{
135 return htx_sl_p3(sl);
136}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200137
Christopher Faulet54483df2018-11-26 15:05:52 +0100138/* Returns the HTX start-line if set, otherwise it returns NULL. */
139static inline struct htx_sl *htx_get_stline(struct htx *htx)
140{
141 struct htx_sl *sl = NULL;
142
143 if (htx->sl_off != -1)
144 sl = ((void *)htx->blocks + htx->sl_off);
145
146 return sl;
147}
148
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200149/* Returns the array index of a block given its position <pos> */
150static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
151{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200153}
154
155/* Returns the position of the block <blk> */
156static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
157{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100158 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200159}
160
161/* Returns the block at the position <pos> */
162static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
163{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100164 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200165}
166
167/* Returns the type of the block <blk> */
168static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
169{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100170 return (blk->info >> 28);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200171}
172
173/* Returns the pseudo-header type of the block <blk>. If it's not a
174 * pseudo-header, HTX_PHDR_UNKNOWN is returned.
175 */
176static inline enum htx_phdr_type htx_get_blk_phdr(const struct htx_blk *blk)
177{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100178 enum htx_blk_type type = htx_get_blk_type(blk);
179 enum htx_phdr_type phdr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200180
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100181 switch (type) {
182 case HTX_BLK_PHDR:
183 phdr = (blk->info & 0xff);
184 return phdr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200185
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100186 default:
187 /* Not a pseudo-header */
188 return HTX_PHDR_UNKNOWN;
189 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200190}
191
192/* Returns the size of the block <blk>, depending of its type */
193static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
194{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100195 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200196
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100197 switch (type) {
198 case HTX_BLK_HDR:
199 /* name.length + value.length */
200 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
201 case HTX_BLK_PHDR:
202 /* value.length */
203 return ((blk->info >> 8) & 0xfffff);
204 default:
205 /* value.length */
206 return (blk->info & 0xfffffff);
207 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200208}
209
210/* Returns the position of the oldest entry (head).
211 *
212 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
213 * store on unsigned 32-bits integer, but it is impossible to have so much
214 * blocks to overflow a 32-bits signed integer !
215 */
216static inline int32_t htx_get_head(const struct htx *htx)
217{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100218 if (!htx->used)
219 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200220
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100221 return (((htx->tail + 1U < htx->used) ? htx->wrap : 0) + htx->tail + 1U - htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200222}
223
224/* Returns the oldest HTX block (head) if the HTX message is not
225 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100226 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200227static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
228{
229 int32_t head = htx_get_head(htx);
230
231 return ((head == -1) ? NULL : htx_get_blk(htx, head));
232}
233
234/* Returns the type of the oldest HTX block (head) if the HTX message is not
235 * empty. Otherwise it returns HTX_BLK_UNUSED.
236 */
237static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
238{
239 struct htx_blk *blk = htx_get_head_blk(htx);
240
241 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
242}
243
244/* Returns the position of the newest entry (tail).
245 *
246 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
247 * store on unsigned 32-bits integer, but it is impossible to have so much
248 * blocks to overflow a 32-bits signed integer !
249 */
250static inline int32_t htx_get_tail(const struct htx *htx)
251{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100252 return (htx->used ? htx->tail : -1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200253}
254
255/* Returns the newest HTX block (tail) if the HTX message is not
256 * empty. Otherwise it returns NULL.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100257 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200258static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
259{
260 int32_t tail = htx_get_tail(htx);
261
262 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
263}
264
265/* Returns the type of the newest HTX block (tail) if the HTX message is not
266 * empty. Otherwise it returns HTX_BLK_UNUSED.
267 */
268static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
269{
270 struct htx_blk *blk = htx_get_tail_blk(htx);
271
272 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
273}
274
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800275/* Returns the position of block immediately before the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200276 * the message is empty or if <pos> is the position of the head, -1 returned.
277 *
278 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
279 * store on unsigned 32-bits integer, but it is impossible to have so much
280 * blocks to overflow a 32-bits signed integer !
281 */
282static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
283{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100284 int32_t head;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200285
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100286 head = htx_get_head(htx);
287 if (head == -1 || pos == head)
288 return -1;
289 if (!pos)
290 return (htx->wrap - 1);
291 return (pos - 1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200292}
293
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100294/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
295 * head, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100296 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100297static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
298 const struct htx_blk *blk)
299{
300 int32_t pos;
301
302 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
303 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
304}
305
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800306/* Returns the position of block immediately after the one pointed by <pos>. If
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200307 * the message is empty or if <pos> is the position of the tail, -1 returned.
308 *
309 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
310 * store on unsigned 32-bits integer, but it is impossible to have so much
311 * blocks to overflow a 32-bits signed integer !
312 */
313static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
314{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100315 if (!htx->used)
316 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200317
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100318 if (pos == htx->tail)
319 return -1;
320 pos++;
321 if (pos >= htx->wrap)
322 pos = 0;
323 return pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200324
325}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100326
327/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
328 * tail, NULL returned.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100329 */
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100330static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
331 const struct htx_blk *blk)
332{
333 int32_t pos;
334
335 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
336 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
337}
338
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200339static inline int32_t htx_find_front(const struct htx *htx)
340{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100341 int32_t front, pos;
342 uint32_t addr = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200343
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100344 if (!htx->used)
345 return -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200346
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100347 front = -1;
348 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
349 struct htx_blk *blk = htx_get_blk(htx, pos);
350 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200351
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100352 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
353 front = pos;
354 addr = blk->addr;
355 }
356 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200357
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100358 return front;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200359}
360
Christopher Faulet14e88252018-11-22 11:28:18 +0100361/* Returns the HTX block containing data with the <offset>, relatively to the
362 * beginning of the HTX message <htx>. It returns an htx_ret. if the HTX block is
363 * not found, htx_ret.blk is set to NULL. Otherwise, it points to the right HTX
364 * block and htx_ret.ret is set to the remaining offset inside the block.
365 */
366static inline struct htx_ret htx_find_blk(const struct htx *htx, uint32_t offset)
367{
368 int32_t pos;
369
370 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
371 struct htx_blk *blk = htx_get_blk(htx, pos);
372 uint32_t sz = htx_get_blksz(blk);
373
374 if (offset < sz)
375 return (struct htx_ret){ .blk = blk, .ret = offset };
376 offset -= sz;
377 }
378
379 return (struct htx_ret){ .blk = NULL };
380}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200381/* Changes the size of the value. It is the caller responsibility to change the
382 * value itself, make sure there is enough space and update allocated value.
383 */
384static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
385{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100386 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200387
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100388 switch (type) {
389 case HTX_BLK_HDR:
390 case HTX_BLK_PHDR:
391 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
392 break;
393 case HTX_BLK_REQ_SL:
394 case HTX_BLK_RES_SL:
395 case HTX_BLK_DATA:
396 case HTX_BLK_TLR:
397 case HTX_BLK_OOB:
398 blk->info = (type << 28) + vlen;
399 break;
400 default:
401 /* Unexpected case */
402 break;
403 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200404}
405
406/* Returns the data pointer of the block <blk> */
407static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
408{
409 return ((void *)htx->blocks + blk->addr);
410}
411
412/* Returns the name of the block <blk>, only if it is a header. Otherwise it
413 * returns an empty name.
414 */
415static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
416{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100417 enum htx_blk_type type = htx_get_blk_type(blk);
418 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200419
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100420 switch (type) {
421 case HTX_BLK_HDR:
422 ret.ptr = htx_get_blk_ptr(htx, blk);
423 ret.len = blk->info & 0xff;
424 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200425
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100426 default:
427 return ist("");
428 }
429 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200430}
431
Christopher Faulet54483df2018-11-26 15:05:52 +0100432
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200433/* Returns the value of the block <blk>, depending on its type. If there is no
434 * value, an empty one is retruned.
435 */
436static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
437{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100438 enum htx_blk_type type = htx_get_blk_type(blk);
439 struct ist ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200440
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100441 switch (type) {
442 case HTX_BLK_HDR:
443 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
444 ret.len = (blk->info >> 8) & 0xfffff;
445 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200446
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100447 case HTX_BLK_PHDR:
448 ret.ptr = htx_get_blk_ptr(htx, blk);
449 ret.len = (blk->info >> 8) & 0xfffff;
450 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200451
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100452 case HTX_BLK_REQ_SL:
453 case HTX_BLK_RES_SL:
454 case HTX_BLK_DATA:
455 case HTX_BLK_TLR:
456 case HTX_BLK_OOB:
457 ret.ptr = htx_get_blk_ptr(htx, blk);
458 ret.len = blk->info & 0xfffffff;
459 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200460
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100461 default:
462 return ist("");
463 }
464 return ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200465}
466
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100467/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
468 * address and its length are adjusted, and the htx's total data count is
469 * updated. This is used to mark that part of some data were transfered
470 * from a DATA block without removing this DATA block. No sanity check is
471 * performed, the caller is reponsible for doing this exclusively on DATA
472 * blocks, and never removing more than the block's size.
473 */
474static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
475{
476 blk->addr += n;
477 blk->info -= n;
478 htx->data -= n;
479}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200480
481/* Returns the space used by metadata in <htx>. */
482static inline uint32_t htx_meta_space(const struct htx *htx)
483{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100484 return (htx->used * sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200485}
486
487/* Returns the space used (data + metadata) in <htx> */
488static inline uint32_t htx_used_space(const struct htx *htx)
489{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100490 return (htx->data + htx_meta_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200491}
492
493/* Returns the free space in <htx> */
494static inline uint32_t htx_free_space(const struct htx *htx)
495{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100496 return (htx->size - htx_used_space(htx));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200497}
498
499/* Returns the maximum size available to store some data in <htx> if a new block
500 * is reserved.
501 */
502static inline uint32_t htx_free_data_space(const struct htx *htx)
503{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100504 uint32_t free = htx_free_space(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200505
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100506 if (free < sizeof(htx->blocks[0]))
507 return 0;
508 return (free - sizeof(htx->blocks[0]));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200509}
510
511/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
512static inline int htx_almost_full(const struct htx *htx)
513{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 if (!htx->size || htx_free_space(htx) < htx->size / 4)
515 return 1;
516 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200517}
518
519static inline void htx_reset(struct htx *htx)
520{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100521 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200522 htx->extra = 0;
523 htx->flags = HTX_FL_NONE;
Christopher Faulet54483df2018-11-26 15:05:52 +0100524 htx->sl_off = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200525}
526
Willy Tarreau3906e222018-12-05 07:56:25 +0100527/* returns the available room for raw data in buffer <buf> once HTX overhead is
528 * taken into account (one HTX header and two blocks). The purpose is to figure
529 * the optimal fill length to avoid copies.
530 */
531static inline size_t buf_room_for_htx_data(const struct buffer *buf)
532{
533 size_t room;
534
535 room = b_room(buf);
536 if (room <= sizeof(struct htx) + 2 * sizeof(struct htx_blk))
537 room = 0;
538 else
539 room -= sizeof(struct htx) + 2 * sizeof(struct htx_blk);
540
541 return room;
542}
543
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100544
545/* Returns an HTX message using the buffer <buf>. Unlike htx_from_buf(), this
546 * function does not update to the buffer. */
547static inline struct htx *htxbuf(const struct buffer *buf)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200548{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100549 struct htx *htx;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200550
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100551 if (b_is_null(buf))
552 return &htx_empty;
553 htx = ((struct htx *)(buf->area));
554 if (!b_data(buf)) {
Willy Tarreau8ae42352018-12-05 09:47:34 +0100555 htx->size = buf->size - sizeof(*htx);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100556 htx_reset(htx);
Willy Tarreau8ae42352018-12-05 09:47:34 +0100557 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200558 return htx;
559}
560
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100561/* Returns an HTX message using the buffer <buf>. <buf> is updated to appear as
562 * full. It is the caller responsibility to call htx_to_buf() when it finish to
563 * manipulate the HTX message to update <buf> accordingly.
564 *
565 * If the caller can call htxbuf() function to avoir any update of the
566 * buffer.
567 */
568static inline struct htx *htx_from_buf(struct buffer *buf)
569{
570 struct htx *htx = htxbuf(buf);
571
572 b_set_data(buf, b_size(buf));
573 return htx;
574}
575
576/* Upate <buf> accordingly to the HTX message <htx> */
577static inline void htx_to_buf(struct htx *htx, struct buffer *buf)
578{
579 if (!htx->used) {
580 htx_reset(htx);
581 b_set_data(buf, 0);
582 }
583 else
584 b_set_data(buf, b_size(buf));
585}
586
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200587/* Returns 1 if the message is empty, otherwise it returns 0. */
588static inline int htx_is_empty(const struct htx *htx)
589{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100590 return (!htx || !htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200591}
592
593/* Returns 1 if the message is not empty, otherwise it returns 0. */
594static inline int htx_is_not_empty(const struct htx *htx)
595{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100596 return (htx && htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200597}
598
599/* For debugging purpose */
600static inline const char *htx_blk_type_str(enum htx_blk_type type)
601{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100602 switch (type) {
603 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
604 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
605 case HTX_BLK_HDR: return "HTX_BLK_HDR";
606 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
607 case HTX_BLK_EOH: return "HTX_BLK_EOH";
608 case HTX_BLK_DATA: return "HTX_BLK_DATA";
609 case HTX_BLK_EOD: return "HTX_BLK_EOD";
610 case HTX_BLK_TLR: return "HTX_BLK_TLR";
611 case HTX_BLK_EOM: return "HTX_BLK_EOM";
612 case HTX_BLK_OOB: return "HTX_BLK_OOB";
613 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
614 default: return "HTX_BLK_???";
615 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200616}
617
618static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
619{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100620 switch (phdr) {
621 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
622 default: return "HTX_PHDR_???";
623 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200624}
625
626static inline void htx_dump(struct htx *htx)
627{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100628 int32_t pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200629
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100630 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
631 htx, htx->size, htx->data, htx->used,
632 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100633 (unsigned long long)htx->extra);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100634 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
635 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100637 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet570d1612018-11-26 11:13:57 +0100638 struct htx_sl *sl;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100639 struct htx_blk *blk = htx_get_blk(htx, pos);
640 enum htx_blk_type type = htx_get_blk_type(blk);
641 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
642 uint32_t sz = htx_get_blksz(blk);
643 struct ist n, v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200644
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100645 n = htx_get_blk_name(htx, blk);
646 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100648 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200649 sl = htx_get_blk_ptr(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100650 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
651 pos, htx_blk_type_str(type), sz, blk->addr,
652 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
653 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
654 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200655 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100656 else if (type == HTX_BLK_HDR)
657 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
658 pos, htx_blk_type_str(type), sz, blk->addr,
659 (int)n.len, n.ptr,
660 (int)v.len, v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200661
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100662 else if (type == HTX_BLK_PHDR)
663 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
664 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
665 (int)v.len, v.ptr);
666 else
667 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
668 pos, htx_blk_type_str(type), sz, blk->addr,
669 (!v.len ? "\t<empty>" : ""));
670 }
671 fprintf(stderr, "\n");
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200672}
673
674#endif /* _PROTO_HTX_H */
675
676/*
677 * Local variables:
678 * c-indent-level: 8
679 * c-basic-offset: 8
680 * End:
681 */