blob: 91a2086a33077ab20880aa8df9b29a0c1507dbfd [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);
60
61int htx_reqline_to_str(const union htx_sl *sl, struct buffer *chk);
62int htx_stline_to_str(const union htx_sl *sl, struct buffer *chk);
63int htx_hdr_to_str(const struct ist n, const struct ist v, struct buffer *chk);
64int htx_data_to_str(const struct ist data, struct buffer *chk, int chunked);
65int htx_trailer_to_str(const struct ist tlr, struct buffer *chk);
66
67
68/* Returns the array index of a block given its position <pos> */
69static inline uint32_t htx_pos_to_idx(const struct htx *htx, uint32_t pos)
70{
71 return ((htx->size / sizeof(htx->blocks[0])) - pos - 1);
72}
73
74/* Returns the position of the block <blk> */
75static inline uint32_t htx_get_blk_pos(const struct htx *htx, const struct htx_blk *blk)
76{
77 return (htx->blocks + (htx->size / sizeof(htx->blocks[0])) - blk - 1);
78}
79
80/* Returns the block at the position <pos> */
81static inline struct htx_blk *htx_get_blk(const struct htx *htx, uint32_t pos)
82{
83 return ((struct htx_blk *)(htx->blocks) + htx_pos_to_idx(htx, pos));
84}
85
86/* Returns the type of the block <blk> */
87static inline enum htx_blk_type htx_get_blk_type(const struct htx_blk *blk)
88{
89 return (blk->info >> 28);
90}
91
92/* Returns the pseudo-header type of the block <blk>. If it's not a
93 * pseudo-header, HTX_PHDR_UNKNOWN is returned.
94 */
95static inline enum htx_phdr_type htx_get_blk_phdr(const struct htx_blk *blk)
96{
97 enum htx_blk_type type = htx_get_blk_type(blk);
98 enum htx_phdr_type phdr;
99
100 switch (type) {
101 case HTX_BLK_PHDR:
102 phdr = (blk->info & 0xff);
103 return phdr;
104
105 default:
106 /* Not a pseudo-header */
107 return HTX_PHDR_UNKNOWN;
108 }
109}
110
111/* Returns the size of the block <blk>, depending of its type */
112static inline uint32_t htx_get_blksz(const struct htx_blk *blk)
113{
114 enum htx_blk_type type = htx_get_blk_type(blk);
115
116 switch (type) {
117 case HTX_BLK_HDR:
118 /* name.length + value.length */
119 return ((blk->info & 0xff) + ((blk->info >> 8) & 0xfffff));
120 case HTX_BLK_PHDR:
121 /* value.length */
122 return ((blk->info >> 8) & 0xfffff);
123 default:
124 /* value.length */
125 return (blk->info & 0xfffffff);
126 }
127}
128
129/* Returns the position of the oldest entry (head).
130 *
131 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
132 * store on unsigned 32-bits integer, but it is impossible to have so much
133 * blocks to overflow a 32-bits signed integer !
134 */
135static inline int32_t htx_get_head(const struct htx *htx)
136{
137 if (!htx->used)
138 return -1;
139
140 return (((htx->tail + 1U < htx->used) ? htx->wrap : 0) + htx->tail + 1U - htx->used);
141}
142
143/* Returns the oldest HTX block (head) if the HTX message is not
144 * empty. Otherwise it returns NULL.
145*/
146static inline struct htx_blk *htx_get_head_blk(const struct htx *htx)
147{
148 int32_t head = htx_get_head(htx);
149
150 return ((head == -1) ? NULL : htx_get_blk(htx, head));
151}
152
153/* Returns the type of the oldest HTX block (head) if the HTX message is not
154 * empty. Otherwise it returns HTX_BLK_UNUSED.
155 */
156static inline enum htx_blk_type htx_get_head_type(const struct htx *htx)
157{
158 struct htx_blk *blk = htx_get_head_blk(htx);
159
160 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
161}
162
163/* Returns the position of the newest entry (tail).
164 *
165 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
166 * store on unsigned 32-bits integer, but it is impossible to have so much
167 * blocks to overflow a 32-bits signed integer !
168 */
169static inline int32_t htx_get_tail(const struct htx *htx)
170{
171 return (htx->used ? htx->tail : -1);
172}
173
174/* Returns the newest HTX block (tail) if the HTX message is not
175 * empty. Otherwise it returns NULL.
176*/
177static inline struct htx_blk *htx_get_tail_blk(const struct htx *htx)
178{
179 int32_t tail = htx_get_tail(htx);
180
181 return ((tail == -1) ? NULL : htx_get_blk(htx, tail));
182}
183
184/* Returns the type of the newest HTX block (tail) if the HTX message is not
185 * empty. Otherwise it returns HTX_BLK_UNUSED.
186 */
187static inline enum htx_blk_type htx_get_tail_type(const struct htx *htx)
188{
189 struct htx_blk *blk = htx_get_tail_blk(htx);
190
191 return (blk ? htx_get_blk_type(blk) : HTX_BLK_UNUSED);
192}
193
194/* Returns the position of block immediatly before the one pointed by <pos>. If
195 * the message is empty or if <pos> is the position of the head, -1 returned.
196 *
197 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
198 * store on unsigned 32-bits integer, but it is impossible to have so much
199 * blocks to overflow a 32-bits signed integer !
200 */
201static inline int32_t htx_get_prev(const struct htx *htx, uint32_t pos)
202{
203 int32_t head;
204
205 head = htx_get_head(htx);
206 if (head == -1 || pos == head)
207 return -1;
208 if (!pos)
209 return (htx->wrap - 1);
210 return (pos - 1);
211}
212
213/* Returns the position of block immediatly after the one pointed by <pos>. If
214 * the message is empty or if <pos> is the position of the tail, -1 returned.
215 *
216 * An signed 32-bits integer is returned to handle -1 case. Blocks position are
217 * store on unsigned 32-bits integer, but it is impossible to have so much
218 * blocks to overflow a 32-bits signed integer !
219 */
220static inline int32_t htx_get_next(const struct htx *htx, uint32_t pos)
221{
222 if (!htx->used)
223 return -1;
224
225 if (pos == htx->tail)
226 return -1;
227 pos++;
228 if (pos >= htx->wrap)
229 pos = 0;
230 return pos;
231
232}
233static inline int32_t htx_find_front(const struct htx *htx)
234{
235 int32_t front, pos;
236 uint32_t addr = 0;
237
238 if (!htx->used)
239 return -1;
240
241 front = -1;
242 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
243 struct htx_blk *blk = htx_get_blk(htx, pos);
244 enum htx_blk_type type = htx_get_blk_type(blk);
245
246 if (type != HTX_BLK_UNUSED && blk->addr >= addr) {
247 front = pos;
248 addr = blk->addr;
249 }
250 }
251
252 return front;
253}
254
255/* Changes the size of the value. It is the caller responsibility to change the
256 * value itself, make sure there is enough space and update allocated value.
257 */
258static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
259{
260 enum htx_blk_type type = htx_get_blk_type(blk);
261
262 switch (type) {
263 case HTX_BLK_HDR:
264 case HTX_BLK_PHDR:
265 blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
266 break;
267 case HTX_BLK_REQ_SL:
268 case HTX_BLK_RES_SL:
269 case HTX_BLK_DATA:
270 case HTX_BLK_TLR:
271 case HTX_BLK_OOB:
272 blk->info = (type << 28) + vlen;
273 break;
274 default:
275 /* Unexpected case */
276 break;
277 }
278}
279
280/* Returns the data pointer of the block <blk> */
281static inline void *htx_get_blk_ptr(const struct htx *htx, const struct htx_blk *blk)
282{
283 return ((void *)htx->blocks + blk->addr);
284}
285
286/* Returns the name of the block <blk>, only if it is a header. Otherwise it
287 * returns an empty name.
288 */
289static inline struct ist htx_get_blk_name(const struct htx *htx, const struct htx_blk *blk)
290{
291 enum htx_blk_type type = htx_get_blk_type(blk);
292 struct ist ret;
293
294 switch (type) {
295 case HTX_BLK_HDR:
296 ret.ptr = htx_get_blk_ptr(htx, blk);
297 ret.len = blk->info & 0xff;
298 break;
299
300 default:
301 return ist("");
302 }
303 return ret;
304}
305
306/* Returns the value of the block <blk>, depending on its type. If there is no
307 * value, an empty one is retruned.
308 */
309static inline struct ist htx_get_blk_value(const struct htx *htx, const struct htx_blk *blk)
310{
311 enum htx_blk_type type = htx_get_blk_type(blk);
312 struct ist ret;
313
314 switch (type) {
315 case HTX_BLK_HDR:
316 ret.ptr = htx_get_blk_ptr(htx, blk) + (blk->info & 0xff);
317 ret.len = (blk->info >> 8) & 0xfffff;
318 break;
319
320 case HTX_BLK_PHDR:
321 ret.ptr = htx_get_blk_ptr(htx, blk);
322 ret.len = (blk->info >> 8) & 0xfffff;
323 break;
324
325 case HTX_BLK_REQ_SL:
326 case HTX_BLK_RES_SL:
327 case HTX_BLK_DATA:
328 case HTX_BLK_TLR:
329 case HTX_BLK_OOB:
330 ret.ptr = htx_get_blk_ptr(htx, blk);
331 ret.len = blk->info & 0xfffffff;
332 break;
333
334 default:
335 return ist("");
336 }
337 return ret;
338}
339
340
341/* Returns the space used by metadata in <htx>. */
342static inline uint32_t htx_meta_space(const struct htx *htx)
343{
344 return (htx->used * sizeof(htx->blocks[0]));
345}
346
347/* Returns the space used (data + metadata) in <htx> */
348static inline uint32_t htx_used_space(const struct htx *htx)
349{
350 return (htx->data + htx_meta_space(htx));
351}
352
353/* Returns the free space in <htx> */
354static inline uint32_t htx_free_space(const struct htx *htx)
355{
356 return (htx->size - htx_used_space(htx));
357}
358
359/* Returns the maximum size available to store some data in <htx> if a new block
360 * is reserved.
361 */
362static inline uint32_t htx_free_data_space(const struct htx *htx)
363{
364 uint32_t free = htx_free_space(htx);
365
366 if (free < sizeof(htx->blocks[0]))
367 return 0;
368 return (free - sizeof(htx->blocks[0]));
369}
370
371/* Returns 1 if the message has less than 1/4 of its capacity free, otherwise 0 */
372static inline int htx_almost_full(const struct htx *htx)
373{
374 if (!htx->size || htx_free_space(htx) < htx->size / 4)
375 return 1;
376 return 0;
377}
378
379static inline void htx_reset(struct htx *htx)
380{
381 htx->data = htx->used = htx->tail = htx->wrap = htx->front = 0;
382 htx->extra = 0;
383 htx->flags = HTX_FL_NONE;
384}
385
386/* Returns an HTX message using the buffer <buf>. */
387static inline struct htx *htx_from_buf(struct buffer *buf)
388{
389 struct htx *htx;
390
391 if (b_is_null(buf))
392 return &htx_empty;
393 htx = (struct htx *)(buf->area);
394 htx->size = buf->size - sizeof(*htx);
395 if (!b_data(buf))
396 htx_reset(htx);
397 return htx;
398}
399
400/* Returns 1 if the message is empty, otherwise it returns 0. */
401static inline int htx_is_empty(const struct htx *htx)
402{
403 return (!htx || !htx->used);
404}
405
406/* Returns 1 if the message is not empty, otherwise it returns 0. */
407static inline int htx_is_not_empty(const struct htx *htx)
408{
409 return (htx && htx->used);
410}
411
412/* For debugging purpose */
413static inline const char *htx_blk_type_str(enum htx_blk_type type)
414{
415 switch (type) {
416 case HTX_BLK_REQ_SL: return "HTX_BLK_REQ_SL";
417 case HTX_BLK_RES_SL: return "HTX_BLK_RES_SL";
418 case HTX_BLK_HDR: return "HTX_BLK_HDR";
419 case HTX_BLK_PHDR: return "HTX_BLK_PHDR";
420 case HTX_BLK_EOH: return "HTX_BLK_EOH";
421 case HTX_BLK_DATA: return "HTX_BLK_DATA";
422 case HTX_BLK_EOD: return "HTX_BLK_EOD";
423 case HTX_BLK_TLR: return "HTX_BLK_TLR";
424 case HTX_BLK_EOM: return "HTX_BLK_EOM";
425 case HTX_BLK_OOB: return "HTX_BLK_OOB";
426 case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED";
427 default: return "HTX_BLK_???";
428 };
429}
430
431static inline const char *htx_blk_phdr_str(enum htx_phdr_type phdr)
432{
433 switch (phdr) {
434 case HTX_PHDR_UNKNOWN: return "HTX_PHDR_UNKNOWN";
435 default: return "HTX_PHDR_???";
436 }
437}
438
439static inline void htx_dump(struct htx *htx)
440{
441 int32_t pos;
442
443 fprintf(stderr, "htx:%p [ size=%u - data=%u - used=%u - wrap=%s - extra=%lu]\n",
444 htx, htx->size, htx->data, htx->used,
445 (!htx->used || htx->tail+1 == htx->wrap) ? "NO" : "YES",
446 htx->extra);
447 fprintf(stderr, "\thead=%d - tail=%u - front=%u - wrap=%u\n",
448 htx_get_head(htx), htx->tail, htx->front, htx->wrap);
449
450 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
451 union htx_sl *sl;
452 struct htx_blk *blk = htx_get_blk(htx, pos);
453 enum htx_blk_type type = htx_get_blk_type(blk);
454 enum htx_phdr_type phdr = htx_get_blk_phdr(blk);
455 uint32_t sz = htx_get_blksz(blk);
456 struct ist n, v;
457
458 n = htx_get_blk_name(htx, blk);
459 v = htx_get_blk_value(htx, blk);
460
461 if (type == HTX_BLK_REQ_SL) {
462 sl = htx_get_blk_ptr(htx, blk);
463 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
464 pos, htx_blk_type_str(type), sz, blk->addr,
465 (int)sl->rq.m_len, sl->rq.l,
466 (int)sl->rq.u_len, sl->rq.l + sl->rq.m_len,
467 (int)sl->rq.v_len, sl->rq.l + sl->rq.m_len + sl->rq.u_len);
468 }
469 else if (type == HTX_BLK_RES_SL) {
470 sl = htx_get_blk_ptr(htx, blk);
471 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s %.*s %.*s\n",
472 pos, htx_blk_type_str(type), sz, blk->addr,
473 (int)sl->st.v_len, sl->st.l,
474 (int)sl->st.c_len, sl->st.l + sl->st.v_len,
475 (int)sl->st.r_len, sl->st.l + sl->rq.v_len + sl->st.c_len);
476 }
477 else if (type == HTX_BLK_HDR)
478 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s: %.*s\n",
479 pos, htx_blk_type_str(type), sz, blk->addr,
480 (int)n.len, n.ptr,
481 (int)v.len, v.ptr);
482
483 else if (type == HTX_BLK_PHDR)
484 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u\t%.*s\n",
485 pos, htx_blk_phdr_str(phdr), sz, blk->addr,
486 (int)v.len, v.ptr);
487 else
488 fprintf(stderr, "\t\t[%u] type=%-17s - size=%-6u - addr=%-6u%s\n",
489 pos, htx_blk_type_str(type), sz, blk->addr,
490 (!v.len ? "\t<empty>" : ""));
491 }
492 fprintf(stderr, "\n");
493}
494
495#endif /* _PROTO_HTX_H */
496
497/*
498 * Local variables:
499 * c-indent-level: 8
500 * c-basic-offset: 8
501 * End:
502 */