blob: 1f0ebebc919d3d27c3ec99ecb32e7fabc8ff499f [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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010026 struct buffer *chunk = get_trash_chunk();
27 struct htx *tmp = htxbuf(chunk);
28 struct htx_blk *newblk, *oldblk;
29 uint32_t new, old;
30 uint32_t addr, blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020031
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010032 if (!htx->used)
33 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020034
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010035 new = 0;
36 addr = 0;
37 tmp->size = htx->size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020038
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010039 /* 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020046
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010047 newblk = htx_get_blk(tmp, new);
48 newblk->addr = addr;
49 newblk->info = oldblk->info;
50 blksz = htx_get_blksz(oldblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020051
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010052 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
53 new++;
54 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020055
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 Fauletaa75b3d2018-12-05 16:20:40 +010060 /* if <blk> is defined, set its new location */
61 if (blk != NULL && blk == oldblk)
62 blk = newblk;
63 } while (new < htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010065 htx->wrap = htx->used;
66 htx->front = htx->tail = new - 1;
67 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020068
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010069 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020070}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010080 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;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020087
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010088 if (blksz > htx_free_data_space(htx))
89 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +020090
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010091 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200100
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100101 used = htx->used + 1;
102 tail = htx->tail + 1;
103 prev = htx->tail;
104 wrap = htx->wrap;
105 head = htx_get_head(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200106
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100107 if (tail == wrap) {
108 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200109
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100110 /* 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200126
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100127 /* 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.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200134
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100135 * 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;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200147
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100148 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));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200166
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100167 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200184
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 htx->wrap = wrap;
186 htx->tail = tail;
187 htx->used = used;
188 htx->data += blksz;
189 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200190}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100197 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200198
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100199 blk = htx_reserve_nxblk(htx, blksz);
200 if (!blk)
201 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200202
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100203 blk->info = (type << 28);
204 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200205}
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 Fauletaa75b3d2018-12-05 16:20:40 +0100214 uint32_t next, head, pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200215
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100216 if (type != HTX_BLK_UNUSED) {
217 /* 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 Fauletaa75b3d2018-12-05 16:20:40 +0100222 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200223
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100224 /* 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200231
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100232 /* 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200271
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100272 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:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100280 if (pos == htx->front)
281 htx->front = htx_find_front(htx);
282 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200283}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100294 struct htx_blk *blk;
295 struct ist v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200296
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100297 if (!htx->used)
298 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200299
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100300 /* Not enough space to store data */
301 if (data.len > htx_free_data_space(htx))
302 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200303
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100304 /* Append only DATA et TRAILERS data */
305 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
306 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200307
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100308 /* get the tail block */
309 blk = htx_get_blk(htx, htx->tail);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200310
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100311 /* 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;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200315
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100316 /*
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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200335
336 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100337 /* get the value of the tail block */
338 /* FIXME: check v.len + data.len < 256MB */
339 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200340
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100341 /* 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);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200344
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100345 /* Update HTTP message */
346 htx->data += data.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200347
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100348 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200349
350 add_new_block:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100351 /* FIXME: check tlr.len (< 256MB) */
352 blk = htx_add_blk(htx, type, data.len);
353 if (!blk)
354 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200355
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100356 blk->info += data.len;
357 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
358 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200359}
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,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100367 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200368{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100369 struct htx_blk *frtblk;
370 struct buffer *tmp;
371 struct ist n, v;
372 uint32_t info, room;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200373
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100374 n = htx_get_blk_name(htx, blk);
375 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200376
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100377 /* 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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200386
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100387 /* 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 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200390
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100391 /*
392 * Copy the new header in a temp buffer
393 */
394 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200395
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100396 /* 1. copy the header name */
397 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200398
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100399 /* 2. copy value before old part, if any */
400 if (old.ptr != v.ptr)
401 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200402
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100403 /* 3. copy new value */
404 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200405
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100406 /* 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));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200409
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100410 /*
411 * temporarely remove space reserved for the header
412 */
413 info = blk->info;
414 blk->info &= 0xf0000000;
415 htx->data -= (n.len + v.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200416
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100417 /*
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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200428
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100429 /* 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);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200433
434 replace_value:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100435 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);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200440
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100441 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200442}
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;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100454 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200455
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100518 struct htx_blk *frtblk;
519 uint32_t sz, room;
520 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200521
522 sz = htx_get_blksz(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100523 delta = newsz - sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100525 /* easy case, new value is smaller, so replace it in-place */
526 if (delta <= 0) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200527 /* Reset value size. It is the caller responsibility to set the new one */
528 blk->info &= 0xf0000000;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100529 htx->data += delta;
530 return blk;
531 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200532
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100533 /* we need to allocate more space to store the new value */
534 if (delta > htx_free_space(htx))
535 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200536
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100537 /*
538 * temporarely remove space reserved for the old value
539 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200540 /* Reset value size. It is the caller responsibility to set the new one */
541 blk->info &= 0xf0000000;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100542 htx->data -= sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200543
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100544 /*
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 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200553
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100554 /* HTX message need to be defragmented first */
555 blk = htx_defrag(htx, blk);
556 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557
558 replace_value:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100559 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
560 htx->data += newsz;
561 htx->front = htx_get_blk_pos(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200562
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100563 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200564}
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,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100570 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200571{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100572 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200573
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 type = htx_get_blk_type(blk);
575 if (type != HTX_BLK_HDR)
576 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200577
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;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100583 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);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200585
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100586 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200587}
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 Fauletaa75b3d2018-12-05 16:20:40 +0100598 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200599 uint32_t size;
600
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 type = htx_get_blk_type(blk);
602 if (type != HTX_BLK_REQ_SL || HTX_BLK_RES_SL)
603 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100643 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
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100652 /* FIXME: check size (< 256MB) */
653 blk = htx_add_blk(htx, type, size);
654 if (!blk)
655 return NULL;
656 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
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100672 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,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100679 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200680{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100681 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200682
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100683 /* 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;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200687
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100688 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;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692}
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,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100708 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100710 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200711
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100712 /* FIXME: check value.len ( < 1MB) */
713 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
714 if (!blk)
715 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200716
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100717 blk->info += (value.len << 8) + phdr;
718 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
719 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100727 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200728
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100729 blk = htx_add_blk(htx, type, 1);
730 if (!blk)
731 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200732
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100733 blk->info += 1;
734 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200735}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100743 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200744}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100752 return htx_append_blk_value(htx, HTX_BLK_TLR, tlr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753}
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{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100760 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200761
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100762 /* FIXME: check oob.len (< 256MB) */
763 blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len);
764 if (!blk)
765 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200766
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100767 blk->info += oob.len;
768 memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len);
769 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200770}
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
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100778 /* FIXME: check data.len (< 256MB) */
779 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
780 if (!blk)
781 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100782
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100783 blk->info += data.len;
784 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100785
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
Christopher Fauletc59ff232018-12-03 13:58:44 +0100804/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
806 * returns 0.
807 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100808int htx_reqline_to_h1(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 Faulet1e7af462018-12-03 14:05:01 +0100817 if (sl->flags & HTX_SL_F_VER_11)
818 chunk_memcat(chk, "HTTP/1.1", 8);
819 else
820 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
821
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200822 chunk_memcat(chk, "\r\n", 2);
823
824 return 1;
825}
826
Christopher Fauletc59ff232018-12-03 13:58:44 +0100827/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200828 * <chk>. It returns 1 if data are successfully appended, otherwise it
829 * returns 0.
830 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100831int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200832{
Christopher Faulet570d1612018-11-26 11:13:57 +0100833 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200834 return 0;
835
Christopher Faulet1e7af462018-12-03 14:05:01 +0100836 if (sl->flags & HTX_SL_F_VER_11)
837 chunk_memcat(chk, "HTTP/1.1", 8);
838 else
839 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200840 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100841 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200842 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100843 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200844 chunk_memcat(chk, "\r\n", 2);
845
846 return 1;
847}
848
Christopher Fauletc59ff232018-12-03 13:58:44 +0100849/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200850 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
851 * 0.
852 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100853int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200854{
855 if (n.len + v.len + 4 > b_room(chk))
856 return 0;
857
858 chunk_memcat(chk, n.ptr, n.len);
859 chunk_memcat(chk, ": ", 2);
860 chunk_memcat(chk, v.ptr, v.len);
861 chunk_memcat(chk, "\r\n", 2);
862
863 return 1;
864}
865
Christopher Fauletc59ff232018-12-03 13:58:44 +0100866/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200867 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
868 * returns 1 if data are successfully appended, otherwise it returns 0.
869 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100870int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200871{
872 if (chunked) {
873 uint32_t chksz;
874 char tmp[10];
875 char *beg, *end;
876
877 chksz = data.len;
878
879 beg = end = tmp+10;
880 *--beg = '\n';
881 *--beg = '\r';
882 do {
883 *--beg = hextab[chksz & 0xF];
884 } while (chksz >>= 4);
885
886 if (data.len + (end - beg) + 2 > b_room(chk))
887 return 0;
888 chunk_memcat(chk, beg, end - beg);
889 chunk_memcat(chk, data.ptr, data.len);
890 chunk_memcat(chk, "\r\n", 2);
891 }
892 else {
893 if (!chunk_memcat(chk, data.ptr, data.len))
894 return 0;
895 }
896
897 return 1;
898}
899
Christopher Fauletc59ff232018-12-03 13:58:44 +0100900/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200901 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
902 * 0.
903 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100904int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200905{
906 /* FIXME: be sure the CRLF is here or remove it when inserted */
907 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
908 return 0;
909 return 1;
910}