blob: 77d077f75b14ec5c88743e855f1de74d2517843c [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>
29
30#include <types/h1.h>
31#include <types/htx.h>
32
33extern struct htx htx_empty;
34
35struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk);
36struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz);
37struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk);
38
39struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
40 const struct ist old, const struct ist new);
41struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
42 enum htx_blk_type mark);
43
44struct htx_blk *htx_replace_reqline(struct htx *htx, struct htx_blk *blk,
45 const union h1_sl sl);
46struct htx_blk *htx_replace_resline(struct htx *htx, struct htx_blk *blk,
47 const union h1_sl sl);
48struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
49 const struct ist name, const struct ist value);
50
51struct htx_blk *htx_add_reqline(struct htx *htx, const union h1_sl sl);
52struct htx_blk *htx_add_resline(struct htx *htx, const union h1_sl sl);
53struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, const struct ist value);
54struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs);
55struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr, const struct ist value);
56struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
57struct htx_blk *htx_add_data(struct htx *htx, const struct ist data);
58struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr);
59struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob);
Christopher Faulet24ed8352018-11-22 11:20:43 +010060struct 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 +020061
62int htx_reqline_to_str(const union htx_sl *sl, struct buffer *chk);
63int htx_stline_to_str(const union htx_sl *sl, struct buffer *chk);
64int htx_hdr_to_str(const struct ist n, const struct ist v, struct buffer *chk);
65int htx_data_to_str(const struct ist data, struct buffer *chk, int chunked);
66int htx_trailer_to_str(const struct ist tlr, struct buffer *chk);
67
68
69/* Returns the array index of a block given its position <pos> */
70static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
71{
72 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
73}
74
75/* Returns the position of the block <blk> */
76static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
77{
78 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
79}
80
81/* Returns the block at the position <pos> */
82static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
83{
84 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
85}
86
87/* Returns the type of the block <blk> */
88static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
89{
90 return (blk->info >> 28);
91}
92
93/* Returns the pseudo-header type of the block <blk>. If it's not a
94 * pseudo-header, HTX_PHDR_UNKNOWN is returned.
95 */
96static inline enum htx_phdr_type htx_get_blk_phdr(const struct htx_blk *blk)
97{
98 enum htx_blk_type type = htx_get_blk_type(blk);
99 enum htx_phdr_type phdr;
100
101 switch (type) {
102 case HTX_BLK_PHDR:
103 phdr = (blk->info & 0xff);
104 return phdr;
105
106 default:
107 /* Not a pseudo-header */
108 return HTX_PHDR_UNKNOWN;
109 }
110}
111
112/* Returns the size of the block <blk>, depending of its type */
113static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
114{
115 enum htx_blk_type type = htx_get_blk_type(blk);
116
117 switch (type) {
118 case HTX_BLK_HDR:
119 /* name.length + value.length */
120 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
121 case HTX_BLK_PHDR:
122 /* value.length */
123 return ((blk->info >> 8) & 0xfffff);
124 default:
125 /* value.length */
126 return (blk->info & 0xfffffff);
127 }
128}
129
130/* Returns the position of the oldest entry (head).
131 *
132 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
133 * store on unsigned 32-bits integer, but it is impossible to have so much
134 * blocks to overflow a 32-bits signed integer !
135 */
136static inline int32_t htx_get_head(const struct htx *htx)
137{
138 if (!htx->used)
139 return -1;
140
141 return (((htx->tail + 1U < htx->used) ? htx->wrap : 0) + htx->tail + 1U - htx->used);
142}
143
144/* Returns the oldest HTX block (head) if the HTX message is not
145 * empty. Otherwise it returns NULL.
146*/
147static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
148{
149 int32_t head = htx_get_head(htx);
150
151 return ((head == -1) ? NULL : htx_get_blk(htx, head));
152}
153
154/* Returns the type of the oldest HTX block (head) if the HTX message is not
155 * empty. Otherwise it returns HTX_BLK_UNUSED.
156 */
157static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
158{
159 struct htx_blk *blk = htx_get_head_blk(htx);
160
161 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
162}
163
164/* Returns the position of the newest entry (tail).
165 *
166 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
167 * store on unsigned 32-bits integer, but it is impossible to have so much
168 * blocks to overflow a 32-bits signed integer !
169 */
170static inline int32_t htx_get_tail(const struct htx *htx)
171{
172 return (htx->used ? htx->tail : -1);
173}
174
175/* Returns the newest HTX block (tail) if the HTX message is not
176 * empty. Otherwise it returns NULL.
177*/
178static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
179{
180 int32_t tail = htx_get_tail(htx);
181
182 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
183}
184
185/* Returns the type of the newest HTX block (tail) if the HTX message is not
186 * empty. Otherwise it returns HTX_BLK_UNUSED.
187 */
188static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
189{
190 struct htx_blk *blk = htx_get_tail_blk(htx);
191
192 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
193}
194
195/* Returns the position of block immediatly before the one pointed by <pos>. If
196 * the message is empty or if <pos> is the position of the head, -1 returned.
197 *
198 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
199 * store on unsigned 32-bits integer, but it is impossible to have so much
200 * blocks to overflow a 32-bits signed integer !
201 */
202static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
203{
204 int32_t head;
205
206 head = htx_get_head(htx);
207 if (head == -1 || pos == head)
208 return -1;
209 if (!pos)
210 return (htx->wrap - 1);
211 return (pos - 1);
212}
213
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100214/* Returns the HTX block before <blk> in the HTX message <htx>. If <blk> is the
215 * head, NULL returned.
216*/
217static inline struct htx_blk *htx_get_prev_blk(const struct htx *htx,
218 const struct htx_blk *blk)
219{
220 int32_t pos;
221
222 pos = htx_get_prev(htx, htx_get_blk_pos(htx, blk));
223 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
224}
225
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200226/* Returns the position of block immediatly after the one pointed by <pos>. If
227 * the message is empty or if <pos> is the position of the tail, -1 returned.
228 *
229 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
230 * store on unsigned 32-bits integer, but it is impossible to have so much
231 * blocks to overflow a 32-bits signed integer !
232 */
233static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
234{
235 if (!htx->used)
236 return -1;
237
238 if (pos == htx->tail)
239 return -1;
240 pos++;
241 if (pos >= htx->wrap)
242 pos = 0;
243 return pos;
244
245}
Christopher Fauletd16b0a72018-11-22 11:23:23 +0100246
247/* Returns the HTX block after <blk> in the HTX message <htx>. If <blk> is the
248 * tail, NULL returned.
249*/
250static inline struct htx_blk *htx_get_next_blk(const struct htx *htx,
251 const struct htx_blk *blk)
252{
253 int32_t pos;
254
255 pos = htx_get_next(htx, htx_get_blk_pos(htx, blk));
256 return ((pos == -1) ? NULL : htx_get_blk(htx, pos));
257}
258
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200259static inline int32_t htx_find_front(const struct htx *htx)
260{
261 int32_t front, pos;
262 uint32_t addr = 0;
263
264 if (!htx->used)
265 return -1;
266
267 front = -1;
268 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
269 struct htx_blk *blk = htx_get_blk(htx, pos);
270 enum htx_blk_type type = htx_get_blk_type(blk);
271
272 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
273 front = pos;
274 addr = blk->addr;
275 }
276 }
277
278 return front;
279}
280
281/* Changes the size of the value. It is the caller responsibility to change the
282 * value itself, make sure there is enough space and update allocated value.
283 */
284static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
285{
286 enum htx_blk_type type = htx_get_blk_type(blk);
287
288 switch (type) {
289 case HTX_BLK_HDR:
290 case HTX_BLK_PHDR:
291 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
292 break;
293 case HTX_BLK_REQ_SL:
294 case HTX_BLK_RES_SL:
295 case HTX_BLK_DATA:
296 case HTX_BLK_TLR:
297 case HTX_BLK_OOB:
298 blk->info = (type << 28) + vlen;
299 break;
300 default:
301 /* Unexpected case */
302 break;
303 }
304}
305
306/* Returns the data pointer of the block <blk> */
307static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
308{
309 return ((void *)htx->blocks + blk->addr);
310}
311
312/* Returns the name of the block <blk>, only if it is a header. Otherwise it
313 * returns an empty name.
314 */
315static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
316{
317 enum htx_blk_type type = htx_get_blk_type(blk);
318 struct ist ret;
319
320 switch (type) {
321 case HTX_BLK_HDR:
322 ret.ptr = htx_get_blk_ptr(htx, blk);
323 ret.len = blk->info & 0xff;
324 break;
325
326 default:
327 return ist("");
328 }
329 return ret;
330}
331
332/* Returns the value of the block <blk>, depending on its type. If there is no
333 * value, an empty one is retruned.
334 */
335static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
336{
337 enum htx_blk_type type = htx_get_blk_type(blk);
338 struct ist ret;
339
340 switch (type) {
341 case HTX_BLK_HDR:
342 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
343 ret.len = (blk->info >> 8) & 0xfffff;
344 break;
345
346 case HTX_BLK_PHDR:
347 ret.ptr = htx_get_blk_ptr(htx, blk);
348 ret.len = (blk->info >> 8) & 0xfffff;
349 break;
350
351 case HTX_BLK_REQ_SL:
352 case HTX_BLK_RES_SL:
353 case HTX_BLK_DATA:
354 case HTX_BLK_TLR:
355 case HTX_BLK_OOB:
356 ret.ptr = htx_get_blk_ptr(htx, blk);
357 ret.len = blk->info & 0xfffffff;
358 break;
359
360 default:
361 return ist("");
362 }
363 return ret;
364}
365
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100366/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
367 * address and its length are adjusted, and the htx's total data count is
368 * updated. This is used to mark that part of some data were transfered
369 * from a DATA block without removing this DATA block. No sanity check is
370 * performed, the caller is reponsible for doing this exclusively on DATA
371 * blocks, and never removing more than the block's size.
372 */
373static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
374{
375 blk->addr += n;
376 blk->info -= n;
377 htx->data -= n;
378}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200379
380/* Returns the space used by metadata in <htx>. */
381static inline uint32_t htx_meta_space(const struct htx *htx)
382{
383 return (htx->used * sizeof(htx->blocks[0]));
384}
385
386/* Returns the space used (data + metadata) in <htx> */
387static inline uint32_t htx_used_space(const struct htx *htx)
388{
389 return (htx->data + htx_meta_space(htx));
390}
391
392/* Returns the free space in <htx> */
393static inline uint32_t htx_free_space(const struct htx *htx)
394{
395 return (htx->size - htx_used_space(htx));
396}
397
398/* Returns the maximum size available to store some data in <htx> if a new block
399 * is reserved.
400 */
401static inline uint32_t htx_free_data_space(const struct htx *htx)
402{
403 uint32_t free = htx_free_space(htx);
404
405 if (free < sizeof(htx->blocks[0]))
406 return 0;
407 return (free - sizeof(htx->blocks[0]));
408}
409
410/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
411static inline int htx_almost_full(const struct htx *htx)
412{
413 if (!htx->size || htx_free_space(htx) < htx->size / 4)
414 return 1;
415 return 0;
416}
417
418static inline void htx_reset(struct htx *htx)
419{
420 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
421 htx->extra = 0;
422 htx->flags = HTX_FL_NONE;
423}
424
425/* Returns an HTX message using the buffer <buf>. */
426static inline struct htx *htx_from_buf(struct buffer *buf)
427{
428 struct htx *htx;
429
430 if (b_is_null(buf))
431 return &htx_empty;
432 htx = (struct htx *)(buf->area);
433 htx->size = buf->size - sizeof(*htx);
434 if (!b_data(buf))
435 htx_reset(htx);
436 return htx;
437}
438
439/* Returns 1 if the message is empty, otherwise it returns 0. */
440static inline int htx_is_empty(const struct htx *htx)
441{
442 return (!htx || !htx->used);
443}
444
445/* Returns 1 if the message is not empty, otherwise it returns 0. */
446static inline int htx_is_not_empty(const struct htx *htx)
447{
448 return (htx && htx->used);
449}
450
451/* For debugging purpose */
452static inline const char *htx_blk_type_str(enum htx_blk_type type)
453{
454 switch (type) {
455 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
456 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
457 case HTX_BLK_HDR: return "HTX_BLK_HDR";
458 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
459 case HTX_BLK_EOH: return "HTX_BLK_EOH";
460 case HTX_BLK_DATA: return "HTX_BLK_DATA";
461 case HTX_BLK_EOD: return "HTX_BLK_EOD";
462 case HTX_BLK_TLR: return "HTX_BLK_TLR";
463 case HTX_BLK_EOM: return "HTX_BLK_EOM";
464 case HTX_BLK_OOB: return "HTX_BLK_OOB";
465 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
466 default: return "HTX_BLK_???";
467 };
468}
469
470static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
471{
472 switch (phdr) {
473 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
474 default: return "HTX_PHDR_???";
475 }
476}
477
478static inline void htx_dump(struct htx *htx)
479{
480 int32_t pos;
481
Willy Tarreaua7280a12018-11-26 19:41:40 +0100482 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200483 htx, htx->size, htx->data, htx->used,
484 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100485 (unsigned long long)htx->extra);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200486 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
487 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
488
489 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
490 union htx_sl *sl;
491 struct htx_blk *blk = htx_get_blk(htx, pos);
492 enum htx_blk_type type = htx_get_blk_type(blk);
493 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
494 uint32_t sz = htx_get_blksz(blk);
495 struct ist n, v;
496
497 n = htx_get_blk_name(htx, blk);
498 v = htx_get_blk_value(htx, blk);
499
500 if (type == HTX_BLK_REQ_SL) {
501 sl = htx_get_blk_ptr(htx, blk);
502 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
503 pos, htx_blk_type_str(type), sz, blk->addr,
504 (int)sl->rq.m_len, sl->rq.l,
505 (int)sl->rq.u_len, sl->rq.l + sl->rq.m_len,
506 (int)sl->rq.v_len, sl->rq.l + sl->rq.m_len + sl->rq.u_len);
507 }
508 else if (type == HTX_BLK_RES_SL) {
509 sl = htx_get_blk_ptr(htx, blk);
510 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
511 pos, htx_blk_type_str(type), sz, blk->addr,
512 (int)sl->st.v_len, sl->st.l,
513 (int)sl->st.c_len, sl->st.l + sl->st.v_len,
514 (int)sl->st.r_len, sl->st.l + sl->rq.v_len + sl->st.c_len);
515 }
516 else if (type == HTX_BLK_HDR)
517 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
518 pos, htx_blk_type_str(type), sz, blk->addr,
519 (int)n.len, n.ptr,
520 (int)v.len, v.ptr);
521
522 else if (type == HTX_BLK_PHDR)
523 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
524 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
525 (int)v.len, v.ptr);
526 else
527 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
528 pos, htx_blk_type_str(type), sz, blk->addr,
529 (!v.len ? "\t<empty>" : ""));
530 }
531 fprintf(stderr, "\n");
532}
533
534#endif /* _PROTO_HTX_H */
535
536/*
537 * Local variables:
538 * c-indent-level: 8
539 * c-basic-offset: 8
540 * End:
541 */