blob: 3c5c0fa01e477c8d36ec0e8c296ef7b43b092b86 [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
Christopher Faulet14e88252018-11-22 11:28:18 +0100281/* Returns the HTX block containing data with the <offset>, relatively to the
282 * beginning of the HTX message <htx>. It returns an htx_ret. if the HTX block is
283 * not found, htx_ret.blk is set to NULL. Otherwise, it points to the right HTX
284 * block and htx_ret.ret is set to the remaining offset inside the block.
285 */
286static inline struct htx_ret htx_find_blk(const struct htx *htx, uint32_t offset)
287{
288 int32_t pos;
289
290 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
291 struct htx_blk *blk = htx_get_blk(htx, pos);
292 uint32_t sz = htx_get_blksz(blk);
293
294 if (offset < sz)
295 return (struct htx_ret){ .blk = blk, .ret = offset };
296 offset -= sz;
297 }
298
299 return (struct htx_ret){ .blk = NULL };
300}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200301/* Changes the size of the value. It is the caller responsibility to change the
302 * value itself, make sure there is enough space and update allocated value.
303 */
304static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
305{
306 enum htx_blk_type type = htx_get_blk_type(blk);
307
308 switch (type) {
309 case HTX_BLK_HDR:
310 case HTX_BLK_PHDR:
311 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
312 break;
313 case HTX_BLK_REQ_SL:
314 case HTX_BLK_RES_SL:
315 case HTX_BLK_DATA:
316 case HTX_BLK_TLR:
317 case HTX_BLK_OOB:
318 blk->info = (type << 28) + vlen;
319 break;
320 default:
321 /* Unexpected case */
322 break;
323 }
324}
325
326/* Returns the data pointer of the block <blk> */
327static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
328{
329 return ((void *)htx->blocks + blk->addr);
330}
331
332/* Returns the name of the block <blk>, only if it is a header. Otherwise it
333 * returns an empty name.
334 */
335static inline struct ist htx_get_blk_name(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);
343 ret.len = blk->info & 0xff;
344 break;
345
346 default:
347 return ist("");
348 }
349 return ret;
350}
351
352/* Returns the value of the block <blk>, depending on its type. If there is no
353 * value, an empty one is retruned.
354 */
355static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
356{
357 enum htx_blk_type type = htx_get_blk_type(blk);
358 struct ist ret;
359
360 switch (type) {
361 case HTX_BLK_HDR:
362 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
363 ret.len = (blk->info >> 8) & 0xfffff;
364 break;
365
366 case HTX_BLK_PHDR:
367 ret.ptr = htx_get_blk_ptr(htx, blk);
368 ret.len = (blk->info >> 8) & 0xfffff;
369 break;
370
371 case HTX_BLK_REQ_SL:
372 case HTX_BLK_RES_SL:
373 case HTX_BLK_DATA:
374 case HTX_BLK_TLR:
375 case HTX_BLK_OOB:
376 ret.ptr = htx_get_blk_ptr(htx, blk);
377 ret.len = blk->info & 0xfffffff;
378 break;
379
380 default:
381 return ist("");
382 }
383 return ret;
384}
385
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100386/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
387 * address and its length are adjusted, and the htx's total data count is
388 * updated. This is used to mark that part of some data were transfered
389 * from a DATA block without removing this DATA block. No sanity check is
390 * performed, the caller is reponsible for doing this exclusively on DATA
391 * blocks, and never removing more than the block's size.
392 */
393static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
394{
395 blk->addr += n;
396 blk->info -= n;
397 htx->data -= n;
398}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200399
400/* Returns the space used by metadata in <htx>. */
401static inline uint32_t htx_meta_space(const struct htx *htx)
402{
403 return (htx->used * sizeof(htx->blocks[0]));
404}
405
406/* Returns the space used (data + metadata) in <htx> */
407static inline uint32_t htx_used_space(const struct htx *htx)
408{
409 return (htx->data + htx_meta_space(htx));
410}
411
412/* Returns the free space in <htx> */
413static inline uint32_t htx_free_space(const struct htx *htx)
414{
415 return (htx->size - htx_used_space(htx));
416}
417
418/* Returns the maximum size available to store some data in <htx> if a new block
419 * is reserved.
420 */
421static inline uint32_t htx_free_data_space(const struct htx *htx)
422{
423 uint32_t free = htx_free_space(htx);
424
425 if (free < sizeof(htx->blocks[0]))
426 return 0;
427 return (free - sizeof(htx->blocks[0]));
428}
429
430/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
431static inline int htx_almost_full(const struct htx *htx)
432{
433 if (!htx->size || htx_free_space(htx) < htx->size / 4)
434 return 1;
435 return 0;
436}
437
438static inline void htx_reset(struct htx *htx)
439{
440 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
441 htx->extra = 0;
442 htx->flags = HTX_FL_NONE;
443}
444
445/* Returns an HTX message using the buffer <buf>. */
446static inline struct htx *htx_from_buf(struct buffer *buf)
447{
448 struct htx *htx;
449
450 if (b_is_null(buf))
451 return &htx_empty;
452 htx = (struct htx *)(buf->area);
453 htx->size = buf->size - sizeof(*htx);
454 if (!b_data(buf))
455 htx_reset(htx);
456 return htx;
457}
458
459/* Returns 1 if the message is empty, otherwise it returns 0. */
460static inline int htx_is_empty(const struct htx *htx)
461{
462 return (!htx || !htx->used);
463}
464
465/* Returns 1 if the message is not empty, otherwise it returns 0. */
466static inline int htx_is_not_empty(const struct htx *htx)
467{
468 return (htx && htx->used);
469}
470
471/* For debugging purpose */
472static inline const char *htx_blk_type_str(enum htx_blk_type type)
473{
474 switch (type) {
475 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
476 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
477 case HTX_BLK_HDR: return "HTX_BLK_HDR";
478 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
479 case HTX_BLK_EOH: return "HTX_BLK_EOH";
480 case HTX_BLK_DATA: return "HTX_BLK_DATA";
481 case HTX_BLK_EOD: return "HTX_BLK_EOD";
482 case HTX_BLK_TLR: return "HTX_BLK_TLR";
483 case HTX_BLK_EOM: return "HTX_BLK_EOM";
484 case HTX_BLK_OOB: return "HTX_BLK_OOB";
485 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
486 default: return "HTX_BLK_???";
487 };
488}
489
490static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
491{
492 switch (phdr) {
493 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
494 default: return "HTX_PHDR_???";
495 }
496}
497
498static inline void htx_dump(struct htx *htx)
499{
500 int32_t pos;
501
Willy Tarreaua7280a12018-11-26 19:41:40 +0100502 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200503 htx, htx->size, htx->data, htx->used,
504 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100505 (unsigned long long)htx->extra);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200506 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
507 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
508
509 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
510 union htx_sl *sl;
511 struct htx_blk *blk = htx_get_blk(htx, pos);
512 enum htx_blk_type type = htx_get_blk_type(blk);
513 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
514 uint32_t sz = htx_get_blksz(blk);
515 struct ist n, v;
516
517 n = htx_get_blk_name(htx, blk);
518 v = htx_get_blk_value(htx, blk);
519
520 if (type == HTX_BLK_REQ_SL) {
521 sl = htx_get_blk_ptr(htx, blk);
522 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
523 pos, htx_blk_type_str(type), sz, blk->addr,
524 (int)sl->rq.m_len, sl->rq.l,
525 (int)sl->rq.u_len, sl->rq.l + sl->rq.m_len,
526 (int)sl->rq.v_len, sl->rq.l + sl->rq.m_len + sl->rq.u_len);
527 }
528 else if (type == HTX_BLK_RES_SL) {
529 sl = htx_get_blk_ptr(htx, blk);
530 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
531 pos, htx_blk_type_str(type), sz, blk->addr,
532 (int)sl->st.v_len, sl->st.l,
533 (int)sl->st.c_len, sl->st.l + sl->st.v_len,
534 (int)sl->st.r_len, sl->st.l + sl->rq.v_len + sl->st.c_len);
535 }
536 else if (type == HTX_BLK_HDR)
537 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
538 pos, htx_blk_type_str(type), sz, blk->addr,
539 (int)n.len, n.ptr,
540 (int)v.len, v.ptr);
541
542 else if (type == HTX_BLK_PHDR)
543 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
544 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
545 (int)v.len, v.ptr);
546 else
547 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
548 pos, htx_blk_type_str(type), sz, blk->addr,
549 (!v.len ? "\t<empty>" : ""));
550 }
551 fprintf(stderr, "\n");
552}
553
554#endif /* _PROTO_HTX_H */
555
556/*
557 * Local variables:
558 * c-indent-level: 8
559 * c-basic-offset: 8
560 * End:
561 */