blob: a57c57d15f9eda8e1ea1364a5f63f0a50308183e [file] [log] [blame]
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001/*
2 * internal HTTP message
3 *
4 * Copyright 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/chunk.h>
14#include <proto/htx.h>
15
16struct htx htx_empty = { .size = 0, .data = 0, .used = 0 };
17
18/* Defragments an HTTP message, removing unused blocks and unwrapping blocks and
19 * their contents. A temporary message is used to do so. This function never
20 * fails. if <blk> is not NULL, we replace it by the new block address, after
21 * the defragmentation. The new <blk> is returned.
22 */
23/* TODO: merge data blocks into one */
24struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk)
25{
26 struct buffer *chunk = get_trash_chunk();
27 struct htx *tmp = htx_from_buf(chunk);
28 struct htx_blk *newblk, *oldblk;
29 uint32_t new, old;
30 uint32_t addr, blksz;
31
32 if (!htx->used)
33 return NULL;
34
35 new = 0;
36 addr = 0;
37 tmp->size = htx->size;
38
39 /* start from the head */
40 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
41 oldblk = htx_get_blk(htx, old);
42 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED) {
43 htx->used--;
44 continue;
45 }
46
47 newblk = htx_get_blk(tmp, new);
48 newblk->addr = addr;
49 newblk->info = oldblk->info;
50 blksz = htx_get_blksz(oldblk);
51
52 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
53 new++;
54 addr += blksz;
55
Christopher Faulet54483df2018-11-26 15:05:52 +010056 /* update the start-line offset */
57 if (htx->sl_off == oldblk->addr)
58 htx->sl_off = addr;
59
Christopher Fauleta3d2a162018-10-22 08:59:39 +020060 /* if <blk> is defined, set its new location */
61 if (blk != NULL && blk == oldblk)
62 blk = newblk;
63 } while (new < htx->used);
64
65 htx->wrap = htx->used;
66 htx->front = htx->tail = new - 1;
67 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
68
69 return blk;
70}
71
72/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
73 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
74 * block is returned and the HTTP message is updated. Space for this new block
75 * is reserved in the HTTP message. But it is the caller responsibility to set
76 * right info in the block to reflect the stored data.
77 */
78static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
79{
80 struct htx_blk *blk, *prevblk, *headblk, *frtblk;
81 uint32_t used;
82 uint32_t tail;
83 uint32_t prev;
84 uint32_t wrap;
85 uint32_t head;
86 int32_t headroom, tailroom;
87
88 if (blksz > htx_free_data_space(htx))
89 return NULL; /* full */
90
91 if (!htx->used) {
92 /* Empty message */
93 htx->front = htx->tail = 0;
94 htx->wrap = htx->used = 1;
95 blk = htx_get_blk(htx, htx->tail);
96 blk->addr = 0;
97 htx->data = blksz;
98 return blk;
99 }
100
101 used = htx->used + 1;
102 tail = htx->tail + 1;
103 prev = htx->tail;
104 wrap = htx->wrap;
105 head = htx_get_head(htx);
106
107 if (tail == wrap) {
108 frtblk = htx_get_blk(htx, htx->front);
109
110 /* Blocks don't wrap for now. We either need to push the new one
111 * on top of others or to defragement the table. */
112 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap+1) >= frtblk->addr + htx_get_blksz(frtblk))
113 wrap++;
114 else if (tail >= used) /* There is hole at the beginning */
115 tail = 0;
116 else {
117 /* No more room, tail hits data. We have to realign the
118 * whole message. */
119 goto defrag;
120 }
121 }
122 else if (used >= wrap) {
123 /* We have hit the tail, we need to reorganize the blocks. */
124 goto defrag;
125 }
126
127 /* Now we have updated tail, used and wrap, we know that there is some
128 * available room at least from the protocol's perspective. This space
129 * is split in two areas :
130 *
131 * 1: the space between the beginning of the blocks table and the
132 * front data's address. This space will only be used if data don't
133 * wrap yet.
134
135 * 2: If the previous tail was the front block, the space between the
136 * beginning of the message and the head data's address.
137 * Otherwise, the space between the tail data's address and the
138 * tail's one.
139 */
140 prevblk = htx_get_blk(htx, prev);
141 headblk = htx_get_blk(htx, head);
142 if (prevblk->addr >= headblk->addr) {
143 /* the area was contiguous */
144 frtblk = htx_get_blk(htx, htx->front);
145 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap) - (frtblk->addr + htx_get_blksz(frtblk));
146 headroom = headblk->addr;
147
148 if (tailroom >= (int32_t)blksz) {
149 /* install upfront and update ->front */
150 blk = htx_get_blk(htx, tail);
151 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
152 htx->front = tail;
153 }
154 else if (headroom >= (int32_t)blksz) {
155 blk = htx_get_blk(htx, tail);
156 blk->addr = 0;
157 }
158 else {
159 /* need to defragment the table before inserting upfront */
160 goto defrag;
161 }
162 }
163 else {
164 /* it's already wrapped so we can't store anything in the tailroom */
165 headroom = headblk->addr - (prevblk->addr + htx_get_blksz(prevblk));
166
167 if (headroom >= (int32_t)blksz) {
168 blk = htx_get_blk(htx, tail);
169 blk->addr = prevblk->addr + htx_get_blksz(prevblk);
170 }
171 else {
172 defrag:
173 /* need to defragment the table before inserting upfront */
174 htx_defrag(htx, NULL);
175 frtblk = htx_get_blk(htx, htx->front);
176 wrap = htx->wrap + 1;
177 tail = htx->tail + 1;
178 used = htx->used + 1;
179 blk = htx_get_blk(htx, tail);
180 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
181 htx->front = tail;
182 }
183 }
184
185 htx->wrap = wrap;
186 htx->tail = tail;
187 htx->used = used;
188 htx->data += blksz;
189 return blk;
190}
191
192/* Adds a new block of type <type> in the HTTP message <htx>. Its content size
193 * is passed but it is the caller responsibility to do the copy.
194 */
195struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
196{
197 struct htx_blk *blk;
198
199 blk = htx_reserve_nxblk(htx, blksz);
200 if (!blk)
201 return NULL;
202
203 blk->info = (type << 28);
204 return blk;
205}
206
207/* Removes the block <blk> from the HTTP message <htx>. The function returns the
208 * block following <blk> or NULL if <blk> is the last block or the last
209 * inserted one.
210 */
211struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
212{
Christopher Faulet54483df2018-11-26 15:05:52 +0100213 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200214 uint32_t next, head, pos;
215
Christopher Faulet54483df2018-11-26 15:05:52 +0100216 if (type != HTX_BLK_UNUSED) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200217 /* Mark the block as unused, decrement allocated size */
218 htx->data -= htx_get_blksz(blk);
219 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Faulet54483df2018-11-26 15:05:52 +0100220 if (htx->sl_off == blk->addr)
221 htx->sl_off = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200222 }
223
224 /* This is the last block in use */
225 if (htx->used == 1/* || !htx->data */) {
226 htx->front = htx->tail = 0;
227 htx->wrap = htx->used = 0;
228 htx->data = 0;
229 return NULL;
230 }
231
232 /* There is at least 2 blocks, so tail is always >= 0 */
233 pos = htx_get_blk_pos(htx, blk);
234 head = htx_get_head(htx);
235 blk = NULL;
236 next = pos + 1; /* By default retrun the next block */
237 if (htx->tail + 1 == htx->wrap) {
238 /* The HTTP message doesn't wrap */
239 if (pos == head) {
240 /* remove the head, so just return the new head */
241 htx->used--;
242 next = htx_get_head(htx);
243 }
244 else if (pos == htx->tail) {
245 /* remove the tail. this was the last inserted block so
246 * return NULL. */
247 htx->wrap--;
248 htx->tail--;
249 htx->used--;
250 goto end;
251 }
252 }
253 else {
254 /* The HTTP message wraps */
255 if (pos == htx->tail) {
256 /* remove the tail. try to unwrap the message (pos == 0)
257 * and return NULL. */
258 htx->tail = ((pos == 0) ? htx->wrap-1 : htx->tail-1);
259 htx->used--;
260 goto end;
261 }
262 else if (pos == head) {
263 /* remove the head, try to unwrap the message (pos+1 ==
264 * wrap) and return the new head */
265 htx->used--;
266 if (pos + 1 == htx->wrap)
267 htx->wrap = htx->tail + 1;
268 next = htx_get_head(htx);
269 }
270 }
271
272 blk = htx_get_blk(htx, next);
Christopher Faulet54483df2018-11-26 15:05:52 +0100273 if (htx->sl_off == -1) {
274 /* Try to update the start-line offset, if possible */
275 type = htx_get_blk_type(blk);
276 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
277 htx->sl_off = blk->addr;
278 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200279 end:
280 if (pos == htx->front)
281 htx->front = htx_find_front(htx);
282 return blk;
283}
284
285/* Tries to append data to the last inserted block, if the type matches and if
286 * there is enough non-wrapping space. Only DATA and TRAILERS content can be
287 * appended. If the append fails, a new block is inserted. If an error occurred,
288 * NULL is returned. Otherwise, on success, the updated block (or the new one)
289 * is returned.
290*/
291static struct htx_blk *htx_append_blk_value(struct htx *htx, enum htx_blk_type type,
292 const struct ist data)
293{
294 struct htx_blk *blk;
295 struct ist v;
296
297 if (!htx->used)
298 goto add_new_block;
299
300 /* Not enough space to store data */
301 if (data.len > htx_free_data_space(htx))
302 return NULL;
303
304 /* Append only DATA et TRAILERS data */
305 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
306 goto add_new_block;
307
308 /* get the tail block */
309 blk = htx_get_blk(htx, htx->tail);
310
311 /* Don't try to append data if the last inserted block is not of the
312 * same type */
313 if (type != htx_get_blk_type(blk))
314 goto add_new_block;
315
316 /*
317 * Same type and enough space: append data
318 */
319 if (htx->tail + 1 == htx->wrap) {
320 struct htx_blk *frtblk = htx_get_blk(htx, htx->front);
321 int32_t tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
322 if (tailroom >= (int32_t)data.len)
323 goto append_data;
324 htx_defrag(htx, NULL);
325 blk = htx_get_blk(htx, htx->tail);
326 }
327 else {
328 struct htx_blk *headblk = htx_get_blk(htx, htx_get_head(htx));
329 int32_t headroom = headblk->addr - (blk->addr + htx_get_blksz(blk));
330 if (headroom >= (int32_t)data.len)
331 goto append_data;
332 htx_defrag(htx, NULL);
333 blk = htx_get_blk(htx, htx->tail);
334 }
335
336 append_data:
337 /* get the value of the tail block */
338 /* FIXME: check v.len + data.len < 256MB */
339 v = htx_get_blk_value(htx, blk);
340
341 /* Append data and update the block itself */
342 memcpy(v.ptr + v.len, data.ptr, data.len);
343 htx_set_blk_value_len(blk, v.len + data.len);
344
345 /* Update HTTP message */
346 htx->data += data.len;
347
348 return blk;
349
350 add_new_block:
351 /* FIXME: check tlr.len (< 256MB) */
352 blk = htx_add_blk(htx, type, data.len);
353 if (!blk)
354 return NULL;
355
356 blk->info += data.len;
357 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
358 return blk;
359}
360
361/* Replaces a value part of a block by a new one. The new part can be smaller or
362 * larger than the old one. This function works for any kind of block with
363 * attached data. It returns the new block on success, otherwise it returns
364 * NULL.
365 */
366struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
367 const struct ist old, const struct ist new)
368{
369 struct htx_blk *frtblk;
370 struct buffer *tmp;
371 struct ist n, v;
372 uint32_t info, room;
373
374 n = htx_get_blk_name(htx, blk);
375 v = htx_get_blk_value(htx, blk);
376
377 /* easy case, new data are smaller, so replace it in-place */
378 if (new.len <= old.len) {
379 memcpy(old.ptr, new.ptr, new.len);
380 if (old.len != v.len)
381 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
382 htx_set_blk_value_len(blk, v.len - old.len + new.len);
383 htx->data -= (old.len - new.len);
384 return blk;
385 }
386
387 /* we need to allocate more space to store the new header value */
388 if ((new.len - old.len) > htx_free_space(htx))
389 return NULL; /* not enough space */
390
391 /*
392 * Copy the new header in a temp buffer
393 */
394 tmp = get_trash_chunk();
395
396 /* 1. copy the header name */
397 chunk_memcat(tmp, n.ptr, n.len);
398
399 /* 2. copy value before old part, if any */
400 if (old.ptr != v.ptr)
401 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
402
403 /* 3. copy new value */
404 chunk_memcat(tmp, new.ptr, new.len);
405
406 /* 4. copy value after old part if any */
407 if (old.len != v.len)
408 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
409
410 /*
411 * temporarely remove space reserved for the header
412 */
413 info = blk->info;
414 blk->info &= 0xf0000000;
415 htx->data -= (n.len + v.len);
416
417 /*
418 * Try to find right addr to copy all the data
419 */
420 if (htx->tail + 1 == htx->wrap) {
421 frtblk = htx_get_blk(htx, htx->front);
422 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
423 if (room >= htx->data) {
424 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
425 goto replace_value;
426 }
427 }
428
429 /* HTX message need to be defragmented first */
430 blk = htx_defrag(htx, blk);
431 frtblk = htx_get_blk(htx, htx->front);
432 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
433
434 replace_value:
435 blk->info = info;
436 htx_set_blk_value_len(blk, v.len - old.len + new.len);
437 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
438 htx->data += tmp->data;
439 htx->front = htx_get_blk_pos(htx, blk);
440
441 return blk;
442}
443
444/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
445 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were
446 * moved. It returns the number of bytes of data moved and the last HTX block
447 * inserted in <dst>.
448 */
449struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
450 enum htx_blk_type mark)
451{
452 struct htx_blk *blk, *dstblk;
453 enum htx_blk_type type;
454 uint32_t info, max, sz, ret;
455
456 ret = 0;
457 blk = htx_get_blk(src, htx_get_head(src));
458 dstblk = NULL;
459 while (blk && ret <= count) {
460 type = htx_get_blk_type(blk);
461
462 /* Ingore unused block */
463 if (type == HTX_BLK_UNUSED)
464 goto next;
465
466 sz = htx_get_blksz(blk);
467 if (!sz) {
468 dstblk = htx_reserve_nxblk(dst, 0);
469 if (!dstblk)
470 break;
471 dstblk->info = blk->info;
472 goto next;
473 }
474
475 info = blk->info;
476 max = htx_free_data_space(dst);
477 if (max > count)
478 max = count;
479 if (sz > max) {
480 sz = max;
481 info = (type << 28) + sz;
482 /* Headers and pseudo headers must be fully copied */
483 if (type < HTX_BLK_DATA || !sz)
484 break;
485 }
486
487 dstblk = htx_reserve_nxblk(dst, sz);
488 if (!dstblk)
489 break;
490 dstblk->info = info;
491 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
492
493 ret += sz;
494 if (blk->info != info) {
495 /* Partial move: don't remove <blk> from <src> but
496 * resize its content */
497 blk->addr += sz;
498 htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz);
499 src->data -= sz;
500 break;
501 }
502
Christopher Faulet54483df2018-11-26 15:05:52 +0100503 if (dst->sl_off == -1 && src->sl_off == blk->addr)
504 dst->sl_off = dstblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200505 next:
506 blk = htx_remove_blk(src, blk);
507 if (type == mark)
508 break;
509
510 }
511
512 return (struct htx_ret){.ret = ret, .blk = dstblk};
513}
514
515static struct htx_blk *htx_new_blk_value(struct htx *htx, struct htx_blk *blk,
516 uint32_t newsz)
517{
518 struct htx_blk *frtblk;
519 uint32_t sz, room;
520 int32_t delta;
521
522 sz = htx_get_blksz(blk);
523 delta = newsz - sz;
524
525 /* easy case, new value is smaller, so replace it in-place */
526 if (delta <= 0) {
527 /* Reset value size. It is the caller responsibility to set the new one */
528 blk->info &= 0xf0000000;
529 htx->data += delta;
530 return blk;
531 }
532
533 /* we need to allocate more space to store the new value */
534 if (delta > htx_free_space(htx))
535 return NULL; /* not enough space */
536
537 /*
538 * temporarely remove space reserved for the old value
539 */
540 /* Reset value size. It is the caller responsibility to set the new one */
541 blk->info &= 0xf0000000;
542 htx->data -= sz;
543
544 /*
545 * Try to find right addr to copy all the data
546 */
547 if (htx->tail + 1 == htx->wrap) {
548 frtblk = htx_get_blk(htx, htx->front);
549 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
550 if (room >= newsz)
551 goto replace_value;
552 }
553
554 /* HTX message need to be defragmented first */
555 blk = htx_defrag(htx, blk);
556 frtblk = htx_get_blk(htx, htx->front);
557
558 replace_value:
559 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
560 htx->data += newsz;
561 htx->front = htx_get_blk_pos(htx, blk);
562
563 return blk;
564}
565
566/* Replaces an header by a new one. The new header can be smaller or larger than
567 * the old one. It returns the new block on success, otherwise it returns NULL.
568 */
569struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
570 const struct ist name, const struct ist value)
571{
572 enum htx_blk_type type;
573
574 type = htx_get_blk_type(blk);
575 if (type != HTX_BLK_HDR)
576 return NULL;
577
578 blk = htx_new_blk_value(htx, blk, (name.len + value.len));
579 if (!blk)
580 return NULL;
581
582 blk->info = (type << 28) + (value.len << 8) + name.len;
583 memcpy(htx_get_blk_ptr(htx, blk), name.ptr, name.len);
584 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
585
586 return blk;
587}
588
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100589/* Replaces the parts of the start-line. It returns the new start-line on
590 * success, otherwise it returns NULL. It is the caller responsibility to update
591 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200592 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100593struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
594 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200595{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100596 struct htx_sl *sl;
597 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200598 enum htx_blk_type type;
599 uint32_t size;
600
601 type = htx_get_blk_type(blk);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100602 if (type != HTX_BLK_REQ_SL || HTX_BLK_RES_SL)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200603 return NULL;
604
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100605 /* Save start-line info and flags */
606 sl = htx_get_blk_ptr(htx, blk);
607 tmp.info = sl->info;
608 tmp.flags = sl->flags;
Christopher Faulet54483df2018-11-26 15:05:52 +0100609 if (htx->sl_off == blk->addr)
610 htx->sl_off = -1;
611
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100612
613 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614 blk = htx_new_blk_value(htx, blk, size);
615 if (!blk)
616 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617 blk->info = (type << 28) + size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100619 /* Restore start-line info and flags*/
620 sl = htx_get_blk_ptr(htx, blk);
621 sl->info = tmp.info;
622 sl->flags = tmp.flags;
623 if (htx->sl_off == -1)
624 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100626 HTX_SL_P1_LEN(sl) = p1.len;
627 HTX_SL_P2_LEN(sl) = p2.len;
628 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100629
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100630 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
631 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
632 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200633
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100634 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200635}
636
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100637/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
638 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100640struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
641 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200642{
643 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100644 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200645 uint32_t size;
646
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100647 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
648 return NULL;
649
650 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200651
652 /* FIXME: check size (< 256MB) */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100653 blk = htx_add_blk(htx, type, size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200654 if (!blk)
655 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200656 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200657
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100658 sl = htx_get_blk_ptr(htx, blk);
659 if (htx->sl_off == -1)
660 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200661
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100662 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200663
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100664 HTX_SL_P1_LEN(sl) = p1.len;
665 HTX_SL_P2_LEN(sl) = p2.len;
666 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200667
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100668 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
669 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
670 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
671
672 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200673}
674
675/* Adds an HTX block of type HDR in <htx>. It returns the new block on
676 * success. Otherwise, it returns NULL.
677 */
678struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
679 const struct ist value)
680{
681 struct htx_blk *blk;
682
683 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
684 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
685 if (!blk)
686 return NULL;
687
688 blk->info += (value.len << 8) + name.len;
689 memcpy(htx_get_blk_ptr(htx, blk), name.ptr, name.len);
690 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
691 return blk;
692}
693
694struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
695{
696 int i;
697
698 for (i = 0; hdrs[i].n.len; i++) {
699 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
700 return NULL;
701 }
702 return htx_add_endof(htx, HTX_BLK_EOH);
703}
704/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
705 * success. Otherwise, it returns NULL.
706 */
707struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
708 const struct ist value)
709{
710 struct htx_blk *blk;
711
712 /* FIXME: check value.len ( < 1MB) */
713 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
714 if (!blk)
715 return NULL;
716
717 blk->info += (value.len << 8) + phdr;
718 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
719 return blk;
720}
721
722/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
723 * on success. Otherwise, it returns NULL.
724 */
725struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
726{
727 struct htx_blk *blk;
728
729 blk = htx_add_blk(htx, type, 1);
730 if (!blk)
731 return NULL;
732
733 blk->info += 1;
734 return blk;
735}
736
737
738/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
739 * possible. It returns the new block on success. Otherwise, it returns NULL.
740 */
741struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
742{
743 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
744}
745
746/* Adds an HTX block of type TLR in <htx>. It first tries to append trailers
747 * data if possible. It returns the new block on success. Otherwise, it returns
748 * NULL.
749 */
750struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
751{
752 return htx_append_blk_value(htx, HTX_BLK_TLR, tlr);
753}
754
755/* Adds an HTX block of type OOB in <htx>. It returns the new block on
756 * success. Otherwise, it returns NULL.
757 */
758struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob)
759{
760 struct htx_blk *blk;
761
762 /* FIXME: check oob.len (< 256MB) */
763 blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len);
764 if (!blk)
765 return NULL;
766
767 blk->info += oob.len;
768 memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len);
769 return blk;
770}
771
Christopher Faulet24ed8352018-11-22 11:20:43 +0100772struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
773 const struct ist data)
774{
775 struct htx_blk *blk;
776 int32_t prev;
777
778 /* FIXME: check data.len (< 256MB) */
779 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
780 if (!blk)
781 return NULL;
782
783 blk->info += data.len;
784 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
785
786 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
787 struct htx_blk *pblk = htx_get_blk(htx, prev);
788
789 /* Swap .addr and .info fields */
790 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
791 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
792
793 if (blk->addr == pblk->addr)
794 blk->addr += htx_get_blksz(pblk);
795 htx->front = prev;
796
797 if (pblk == ref)
798 break;
799 blk = pblk;
800 }
801 return blk;
802}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803
804/* Appends the string representation of the request line block <blk> to the
805 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
806 * returns 0.
807 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100808int htx_reqline_to_str(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809{
Christopher Faulet570d1612018-11-26 11:13:57 +0100810 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811 return 0;
812
Christopher Faulet570d1612018-11-26 11:13:57 +0100813 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200814 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100815 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200816 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100817 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200818 chunk_memcat(chk, "\r\n", 2);
819
820 return 1;
821}
822
823/* Appends the string representation of the status line block <blk> to the chunk
824 * <chk>. It returns 1 if data are successfully appended, otherwise it
825 * returns 0.
826 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100827int htx_stline_to_str(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200828{
Christopher Faulet570d1612018-11-26 11:13:57 +0100829 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200830 return 0;
831
Christopher Faulet570d1612018-11-26 11:13:57 +0100832 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100834 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100836 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837 chunk_memcat(chk, "\r\n", 2);
838
839 return 1;
840}
841
842/* Appends the string representation of the header block <blk> to the chunk
843 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
844 * 0.
845 */
846int htx_hdr_to_str(const struct ist n, const struct ist v, struct buffer *chk)
847{
848 if (n.len + v.len + 4 > b_room(chk))
849 return 0;
850
851 chunk_memcat(chk, n.ptr, n.len);
852 chunk_memcat(chk, ": ", 2);
853 chunk_memcat(chk, v.ptr, v.len);
854 chunk_memcat(chk, "\r\n", 2);
855
856 return 1;
857}
858
859/* Appends the string representation of the data block <blk> to the chunk
860 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
861 * returns 1 if data are successfully appended, otherwise it returns 0.
862 */
863int htx_data_to_str(const struct ist data, struct buffer *chk, int chunked)
864{
865 if (chunked) {
866 uint32_t chksz;
867 char tmp[10];
868 char *beg, *end;
869
870 chksz = data.len;
871
872 beg = end = tmp+10;
873 *--beg = '\n';
874 *--beg = '\r';
875 do {
876 *--beg = hextab[chksz & 0xF];
877 } while (chksz >>= 4);
878
879 if (data.len + (end - beg) + 2 > b_room(chk))
880 return 0;
881 chunk_memcat(chk, beg, end - beg);
882 chunk_memcat(chk, data.ptr, data.len);
883 chunk_memcat(chk, "\r\n", 2);
884 }
885 else {
886 if (!chunk_memcat(chk, data.ptr, data.len))
887 return 0;
888 }
889
890 return 1;
891}
892
893/* Appends the string representation of the trailer block <blk> to the chunk
894 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
895 * 0.
896 */
897int htx_trailer_to_str(const struct ist tlr, struct buffer *chk)
898{
899 /* FIXME: be sure the CRLF is here or remove it when inserted */
900 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
901 return 0;
902 return 1;
903}