blob: 7db2059c272cd4a5d4f3b2e37bdadb510795ce38 [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
214/* Returns the position of block immediatly after the one pointed by <pos>. If
215 * the message is empty or if <pos> is the position of the tail, -1 returned.
216 *
217 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
218 * store on unsigned 32-bits integer, but it is impossible to have so much
219 * blocks to overflow a 32-bits signed integer !
220 */
221static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
222{
223 if (!htx->used)
224 return -1;
225
226 if (pos == htx->tail)
227 return -1;
228 pos++;
229 if (pos >= htx->wrap)
230 pos = 0;
231 return pos;
232
233}
234static inline int32_t htx_find_front(const struct htx *htx)
235{
236 int32_t front, pos;
237 uint32_t addr = 0;
238
239 if (!htx->used)
240 return -1;
241
242 front = -1;
243 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
244 struct htx_blk *blk = htx_get_blk(htx, pos);
245 enum htx_blk_type type = htx_get_blk_type(blk);
246
247 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
248 front = pos;
249 addr = blk->addr;
250 }
251 }
252
253 return front;
254}
255
256/* Changes the size of the value. It is the caller responsibility to change the
257 * value itself, make sure there is enough space and update allocated value.
258 */
259static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
260{
261 enum htx_blk_type type = htx_get_blk_type(blk);
262
263 switch (type) {
264 case HTX_BLK_HDR:
265 case HTX_BLK_PHDR:
266 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
267 break;
268 case HTX_BLK_REQ_SL:
269 case HTX_BLK_RES_SL:
270 case HTX_BLK_DATA:
271 case HTX_BLK_TLR:
272 case HTX_BLK_OOB:
273 blk->info = (type << 28) + vlen;
274 break;
275 default:
276 /* Unexpected case */
277 break;
278 }
279}
280
281/* Returns the data pointer of the block <blk> */
282static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
283{
284 return ((void *)htx->blocks + blk->addr);
285}
286
287/* Returns the name of the block <blk>, only if it is a header. Otherwise it
288 * returns an empty name.
289 */
290static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
291{
292 enum htx_blk_type type = htx_get_blk_type(blk);
293 struct ist ret;
294
295 switch (type) {
296 case HTX_BLK_HDR:
297 ret.ptr = htx_get_blk_ptr(htx, blk);
298 ret.len = blk->info & 0xff;
299 break;
300
301 default:
302 return ist("");
303 }
304 return ret;
305}
306
307/* Returns the value of the block <blk>, depending on its type. If there is no
308 * value, an empty one is retruned.
309 */
310static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
311{
312 enum htx_blk_type type = htx_get_blk_type(blk);
313 struct ist ret;
314
315 switch (type) {
316 case HTX_BLK_HDR:
317 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
318 ret.len = (blk->info >> 8) & 0xfffff;
319 break;
320
321 case HTX_BLK_PHDR:
322 ret.ptr = htx_get_blk_ptr(htx, blk);
323 ret.len = (blk->info >> 8) & 0xfffff;
324 break;
325
326 case HTX_BLK_REQ_SL:
327 case HTX_BLK_RES_SL:
328 case HTX_BLK_DATA:
329 case HTX_BLK_TLR:
330 case HTX_BLK_OOB:
331 ret.ptr = htx_get_blk_ptr(htx, blk);
332 ret.len = blk->info & 0xfffffff;
333 break;
334
335 default:
336 return ist("");
337 }
338 return ret;
339}
340
Willy Tarreauc01ed9f2018-11-30 14:29:31 +0100341/* Removes <n> bytes from the beginning of DATA block <blk>. The block's start
342 * address and its length are adjusted, and the htx's total data count is
343 * updated. This is used to mark that part of some data were transfered
344 * from a DATA block without removing this DATA block. No sanity check is
345 * performed, the caller is reponsible for doing this exclusively on DATA
346 * blocks, and never removing more than the block's size.
347 */
348static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
349{
350 blk->addr += n;
351 blk->info -= n;
352 htx->data -= n;
353}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200354
355/* Returns the space used by metadata in <htx>. */
356static inline uint32_t htx_meta_space(const struct htx *htx)
357{
358 return (htx->used * sizeof(htx->blocks[0]));
359}
360
361/* Returns the space used (data + metadata) in <htx> */
362static inline uint32_t htx_used_space(const struct htx *htx)
363{
364 return (htx->data + htx_meta_space(htx));
365}
366
367/* Returns the free space in <htx> */
368static inline uint32_t htx_free_space(const struct htx *htx)
369{
370 return (htx->size - htx_used_space(htx));
371}
372
373/* Returns the maximum size available to store some data in <htx> if a new block
374 * is reserved.
375 */
376static inline uint32_t htx_free_data_space(const struct htx *htx)
377{
378 uint32_t free = htx_free_space(htx);
379
380 if (free < sizeof(htx->blocks[0]))
381 return 0;
382 return (free - sizeof(htx->blocks[0]));
383}
384
385/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
386static inline int htx_almost_full(const struct htx *htx)
387{
388 if (!htx->size || htx_free_space(htx) < htx->size / 4)
389 return 1;
390 return 0;
391}
392
393static inline void htx_reset(struct htx *htx)
394{
395 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
396 htx->extra = 0;
397 htx->flags = HTX_FL_NONE;
398}
399
400/* Returns an HTX message using the buffer <buf>. */
401static inline struct htx *htx_from_buf(struct buffer *buf)
402{
403 struct htx *htx;
404
405 if (b_is_null(buf))
406 return &htx_empty;
407 htx = (struct htx *)(buf->area);
408 htx->size = buf->size - sizeof(*htx);
409 if (!b_data(buf))
410 htx_reset(htx);
411 return htx;
412}
413
414/* Returns 1 if the message is empty, otherwise it returns 0. */
415static inline int htx_is_empty(const struct htx *htx)
416{
417 return (!htx || !htx->used);
418}
419
420/* Returns 1 if the message is not empty, otherwise it returns 0. */
421static inline int htx_is_not_empty(const struct htx *htx)
422{
423 return (htx && htx->used);
424}
425
426/* For debugging purpose */
427static inline const char *htx_blk_type_str(enum htx_blk_type type)
428{
429 switch (type) {
430 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
431 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
432 case HTX_BLK_HDR: return "HTX_BLK_HDR";
433 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
434 case HTX_BLK_EOH: return "HTX_BLK_EOH";
435 case HTX_BLK_DATA: return "HTX_BLK_DATA";
436 case HTX_BLK_EOD: return "HTX_BLK_EOD";
437 case HTX_BLK_TLR: return "HTX_BLK_TLR";
438 case HTX_BLK_EOM: return "HTX_BLK_EOM";
439 case HTX_BLK_OOB: return "HTX_BLK_OOB";
440 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
441 default: return "HTX_BLK_???";
442 };
443}
444
445static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
446{
447 switch (phdr) {
448 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
449 default: return "HTX_PHDR_???";
450 }
451}
452
453static inline void htx_dump(struct htx *htx)
454{
455 int32_t pos;
456
Willy Tarreaua7280a12018-11-26 19:41:40 +0100457 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%llu]\n",
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200458 htx, htx->size, htx->data, htx->used,
459 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
Willy Tarreaua7280a12018-11-26 19:41:40 +0100460 (unsigned long long)htx->extra);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200461 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
462 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
463
464 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
465 union htx_sl *sl;
466 struct htx_blk *blk = htx_get_blk(htx, pos);
467 enum htx_blk_type type = htx_get_blk_type(blk);
468 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
469 uint32_t sz = htx_get_blksz(blk);
470 struct ist n, v;
471
472 n = htx_get_blk_name(htx, blk);
473 v = htx_get_blk_value(htx, blk);
474
475 if (type == HTX_BLK_REQ_SL) {
476 sl = htx_get_blk_ptr(htx, blk);
477 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
478 pos, htx_blk_type_str(type), sz, blk->addr,
479 (int)sl->rq.m_len, sl->rq.l,
480 (int)sl->rq.u_len, sl->rq.l + sl->rq.m_len,
481 (int)sl->rq.v_len, sl->rq.l + sl->rq.m_len + sl->rq.u_len);
482 }
483 else if (type == HTX_BLK_RES_SL) {
484 sl = htx_get_blk_ptr(htx, blk);
485 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
486 pos, htx_blk_type_str(type), sz, blk->addr,
487 (int)sl->st.v_len, sl->st.l,
488 (int)sl->st.c_len, sl->st.l + sl->st.v_len,
489 (int)sl->st.r_len, sl->st.l + sl->rq.v_len + sl->st.c_len);
490 }
491 else if (type == HTX_BLK_HDR)
492 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
493 pos, htx_blk_type_str(type), sz, blk->addr,
494 (int)n.len, n.ptr,
495 (int)v.len, v.ptr);
496
497 else if (type == HTX_BLK_PHDR)
498 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
499 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
500 (int)v.len, v.ptr);
501 else
502 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
503 pos, htx_blk_type_str(type), sz, blk->addr,
504 (!v.len ? "\t<empty>" : ""));
505 }
506 fprintf(stderr, "\n");
507}
508
509#endif /* _PROTO_HTX_H */
510
511/*
512 * Local variables:
513 * c-indent-level: 8
514 * c-basic-offset: 8
515 * End:
516 */