blob: bda293b43a9fdfe708f10133bf6b41b48f9b0114 [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>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010014#include <common/htx.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020015
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 Faulet174bfb12018-12-06 14:31:12 +010031 int32_t sl_off = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020032
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010033 if (!htx->used)
34 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020035
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010036 new = 0;
37 addr = 0;
38 tmp->size = htx->size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020039
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010040 /* start from the head */
41 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
42 oldblk = htx_get_blk(htx, old);
43 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED) {
44 htx->used--;
45 continue;
46 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020047
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010048 newblk = htx_get_blk(tmp, new);
49 newblk->addr = addr;
50 newblk->info = oldblk->info;
51 blksz = htx_get_blksz(oldblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020052
Christopher Faulet174bfb12018-12-06 14:31:12 +010053 /* update the start-line offset */
54 if (htx->sl_off == oldblk->addr)
55 sl_off = addr;
56
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010057 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
58 new++;
59 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020060
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010061 /* if <blk> is defined, set its new location */
62 if (blk != NULL && blk == oldblk)
63 blk = newblk;
64 } while (new < htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020065
Christopher Faulet174bfb12018-12-06 14:31:12 +010066 htx->sl_off = sl_off;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010067 htx->wrap = htx->used;
68 htx->front = htx->tail = new - 1;
69 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020070
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010071 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020072}
73
74/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
75 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
76 * block is returned and the HTTP message is updated. Space for this new block
77 * is reserved in the HTTP message. But it is the caller responsibility to set
78 * right info in the block to reflect the stored data.
79 */
80static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
81{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010082 struct htx_blk *blk, *prevblk, *headblk, *frtblk;
83 uint32_t used;
84 uint32_t tail;
85 uint32_t prev;
86 uint32_t wrap;
87 uint32_t head;
88 int32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020089
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010090 if (blksz > htx_free_data_space(htx))
91 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +020092
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010093 if (!htx->used) {
94 /* Empty message */
95 htx->front = htx->tail = 0;
96 htx->wrap = htx->used = 1;
97 blk = htx_get_blk(htx, htx->tail);
98 blk->addr = 0;
99 htx->data = blksz;
100 return blk;
101 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200102
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100103 used = htx->used + 1;
104 tail = htx->tail + 1;
105 prev = htx->tail;
106 wrap = htx->wrap;
107 head = htx_get_head(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200108
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100109 if (tail == wrap) {
110 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200111
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100112 /* Blocks don't wrap for now. We either need to push the new one
113 * on top of others or to defragement the table. */
114 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap+1) >= frtblk->addr + htx_get_blksz(frtblk))
115 wrap++;
116 else if (tail >= used) /* There is hole at the beginning */
117 tail = 0;
118 else {
119 /* No more room, tail hits data. We have to realign the
120 * whole message. */
121 goto defrag;
122 }
123 }
124 else if (used >= wrap) {
125 /* We have hit the tail, we need to reorganize the blocks. */
126 goto defrag;
127 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200128
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100129 /* Now we have updated tail, used and wrap, we know that there is some
130 * available room at least from the protocol's perspective. This space
131 * is split in two areas :
132 *
133 * 1: the space between the beginning of the blocks table and the
134 * front data's address. This space will only be used if data don't
135 * wrap yet.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200136
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100137 * 2: If the previous tail was the front block, the space between the
138 * beginning of the message and the head data's address.
139 * Otherwise, the space between the tail data's address and the
140 * tail's one.
141 */
142 prevblk = htx_get_blk(htx, prev);
143 headblk = htx_get_blk(htx, head);
144 if (prevblk->addr >= headblk->addr) {
145 /* the area was contiguous */
146 frtblk = htx_get_blk(htx, htx->front);
147 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap) - (frtblk->addr + htx_get_blksz(frtblk));
148 headroom = headblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200149
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100150 if (tailroom >= (int32_t)blksz) {
151 /* install upfront and update ->front */
152 blk = htx_get_blk(htx, tail);
153 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
154 htx->front = tail;
155 }
156 else if (headroom >= (int32_t)blksz) {
157 blk = htx_get_blk(htx, tail);
158 blk->addr = 0;
159 }
160 else {
161 /* need to defragment the table before inserting upfront */
162 goto defrag;
163 }
164 }
165 else {
166 /* it's already wrapped so we can't store anything in the tailroom */
167 headroom = headblk->addr - (prevblk->addr + htx_get_blksz(prevblk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200168
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100169 if (headroom >= (int32_t)blksz) {
170 blk = htx_get_blk(htx, tail);
171 blk->addr = prevblk->addr + htx_get_blksz(prevblk);
172 }
173 else {
174 defrag:
175 /* need to defragment the table before inserting upfront */
176 htx_defrag(htx, NULL);
177 frtblk = htx_get_blk(htx, htx->front);
178 wrap = htx->wrap + 1;
179 tail = htx->tail + 1;
180 used = htx->used + 1;
181 blk = htx_get_blk(htx, tail);
182 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
183 htx->front = tail;
184 }
185 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200186
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100187 htx->wrap = wrap;
188 htx->tail = tail;
189 htx->used = used;
190 htx->data += blksz;
191 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200192}
193
194/* Adds a new block of type <type> in the HTTP message <htx>. Its content size
195 * is passed but it is the caller responsibility to do the copy.
196 */
197struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
198{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100199 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200200
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100201 blk = htx_reserve_nxblk(htx, blksz);
202 if (!blk)
203 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200204
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100205 blk->info = (type << 28);
206 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200207}
208
209/* Removes the block <blk> from the HTTP message <htx>. The function returns the
210 * block following <blk> or NULL if <blk> is the last block or the last
211 * inserted one.
212 */
213struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
214{
Christopher Faulet54483df2018-11-26 15:05:52 +0100215 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100216 uint32_t next, head, pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200217
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100218 if (type != HTX_BLK_UNUSED) {
219 /* Mark the block as unused, decrement allocated size */
220 htx->data -= htx_get_blksz(blk);
221 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Faulet54483df2018-11-26 15:05:52 +0100222 if (htx->sl_off == blk->addr)
223 htx->sl_off = -1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100224 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200225
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100226 /* This is the last block in use */
227 if (htx->used == 1/* || !htx->data */) {
228 htx->front = htx->tail = 0;
229 htx->wrap = htx->used = 0;
230 htx->data = 0;
231 return NULL;
232 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200233
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100234 /* There is at least 2 blocks, so tail is always >= 0 */
235 pos = htx_get_blk_pos(htx, blk);
236 head = htx_get_head(htx);
237 blk = NULL;
238 next = pos + 1; /* By default retrun the next block */
239 if (htx->tail + 1 == htx->wrap) {
240 /* The HTTP message doesn't wrap */
241 if (pos == head) {
242 /* remove the head, so just return the new head */
243 htx->used--;
244 next = htx_get_head(htx);
245 }
246 else if (pos == htx->tail) {
247 /* remove the tail. this was the last inserted block so
248 * return NULL. */
249 htx->wrap--;
250 htx->tail--;
251 htx->used--;
252 goto end;
253 }
254 }
255 else {
256 /* The HTTP message wraps */
257 if (pos == htx->tail) {
258 /* remove the tail. try to unwrap the message (pos == 0)
259 * and return NULL. */
260 htx->tail = ((pos == 0) ? htx->wrap-1 : htx->tail-1);
261 htx->used--;
262 goto end;
263 }
264 else if (pos == head) {
265 /* remove the head, try to unwrap the message (pos+1 ==
266 * wrap) and return the new head */
267 htx->used--;
268 if (pos + 1 == htx->wrap)
269 htx->wrap = htx->tail + 1;
270 next = htx_get_head(htx);
271 }
272 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200273
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100274 blk = htx_get_blk(htx, next);
Christopher Faulet54483df2018-11-26 15:05:52 +0100275 if (htx->sl_off == -1) {
276 /* Try to update the start-line offset, if possible */
277 type = htx_get_blk_type(blk);
278 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
279 htx->sl_off = blk->addr;
280 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200281 end:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100282 if (pos == htx->front)
283 htx->front = htx_find_front(htx);
284 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200285}
286
287/* Tries to append data to the last inserted block, if the type matches and if
288 * there is enough non-wrapping space. Only DATA and TRAILERS content can be
289 * appended. If the append fails, a new block is inserted. If an error occurred,
290 * NULL is returned. Otherwise, on success, the updated block (or the new one)
291 * is returned.
292*/
293static struct htx_blk *htx_append_blk_value(struct htx *htx, enum htx_blk_type type,
294 const struct ist data)
295{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100296 struct htx_blk *blk;
297 struct ist v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200298
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100299 if (!htx->used)
300 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200301
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100302 /* Not enough space to store data */
303 if (data.len > htx_free_data_space(htx))
304 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200305
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100306 /* Append only DATA et TRAILERS data */
307 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
308 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200309
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100310 /* get the tail block */
311 blk = htx_get_blk(htx, htx->tail);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200312
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100313 /* Don't try to append data if the last inserted block is not of the
314 * same type */
315 if (type != htx_get_blk_type(blk))
316 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200317
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100318 /*
319 * Same type and enough space: append data
320 */
321 if (htx->tail + 1 == htx->wrap) {
322 struct htx_blk *frtblk = htx_get_blk(htx, htx->front);
323 int32_t tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
324 if (tailroom >= (int32_t)data.len)
325 goto append_data;
326 htx_defrag(htx, NULL);
327 blk = htx_get_blk(htx, htx->tail);
328 }
329 else {
330 struct htx_blk *headblk = htx_get_blk(htx, htx_get_head(htx));
331 int32_t headroom = headblk->addr - (blk->addr + htx_get_blksz(blk));
332 if (headroom >= (int32_t)data.len)
333 goto append_data;
334 htx_defrag(htx, NULL);
335 blk = htx_get_blk(htx, htx->tail);
336 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200337
338 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100339 /* get the value of the tail block */
340 /* FIXME: check v.len + data.len < 256MB */
341 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200342
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100343 /* Append data and update the block itself */
344 memcpy(v.ptr + v.len, data.ptr, data.len);
345 htx_set_blk_value_len(blk, v.len + data.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200346
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100347 /* Update HTTP message */
348 htx->data += data.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200349
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100350 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200351
352 add_new_block:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100353 /* FIXME: check tlr.len (< 256MB) */
354 blk = htx_add_blk(htx, type, data.len);
355 if (!blk)
356 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200357
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100358 blk->info += data.len;
359 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
360 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200361}
362
363/* Replaces a value part of a block by a new one. The new part can be smaller or
364 * larger than the old one. This function works for any kind of block with
365 * attached data. It returns the new block on success, otherwise it returns
366 * NULL.
367 */
368struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100369 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200370{
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100371 struct buffer *tmp;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100372 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100373 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200374
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100375 n = htx_get_blk_name(htx, blk);
376 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200377
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100378 delta = new.len - old.len;
379
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100380 /* easy case, new data are smaller, so replace it in-place */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100381 if (delta <= 0) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100382 memcpy(old.ptr, new.ptr, new.len);
383 if (old.len != v.len)
384 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100385 htx_set_blk_value_len(blk, v.len + delta);
386 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100387 return blk;
388 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200389
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100390 /* we need to allocate more space to store the new header value */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100391 if (delta > htx_free_space(htx))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100392 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200393
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100394 /*
395 * Copy the new header in a temp buffer
396 */
397 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200398
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100399 /* 1. copy the header name */
400 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200401
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100402 /* 2. copy value before old part, if any */
403 if (old.ptr != v.ptr)
404 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200405
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100406 /* 3. copy new value */
407 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200408
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100409 /* 4. copy value after old part if any */
410 if (old.len != v.len)
411 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200412
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200413
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100414 /* Expand the block size. But data are not copied yet. Then defragment
415 * the HTX message.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100416 */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100417 htx_set_blk_value_len(blk, v.len + delta);
418 htx->data += delta;
419 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200420
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100421 /* Finaly, copy data. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100422 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200423
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100424 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200425}
426
427/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
428 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were
429 * moved. It returns the number of bytes of data moved and the last HTX block
430 * inserted in <dst>.
431 */
432struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
433 enum htx_blk_type mark)
434{
435 struct htx_blk *blk, *dstblk;
436 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100437 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200438
439 ret = 0;
440 blk = htx_get_blk(src, htx_get_head(src));
441 dstblk = NULL;
442 while (blk && ret <= count) {
443 type = htx_get_blk_type(blk);
444
445 /* Ingore unused block */
446 if (type == HTX_BLK_UNUSED)
447 goto next;
448
449 sz = htx_get_blksz(blk);
450 if (!sz) {
451 dstblk = htx_reserve_nxblk(dst, 0);
452 if (!dstblk)
453 break;
454 dstblk->info = blk->info;
455 goto next;
456 }
457
458 info = blk->info;
459 max = htx_free_data_space(dst);
460 if (max > count)
461 max = count;
462 if (sz > max) {
463 sz = max;
464 info = (type << 28) + sz;
465 /* Headers and pseudo headers must be fully copied */
466 if (type < HTX_BLK_DATA || !sz)
467 break;
468 }
469
470 dstblk = htx_reserve_nxblk(dst, sz);
471 if (!dstblk)
472 break;
473 dstblk->info = info;
474 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
475
476 ret += sz;
477 if (blk->info != info) {
478 /* Partial move: don't remove <blk> from <src> but
479 * resize its content */
480 blk->addr += sz;
481 htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz);
482 src->data -= sz;
483 break;
484 }
485
Christopher Faulet54483df2018-11-26 15:05:52 +0100486 if (dst->sl_off == -1 && src->sl_off == blk->addr)
487 dst->sl_off = dstblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200488 next:
489 blk = htx_remove_blk(src, blk);
490 if (type == mark)
491 break;
492
493 }
494
495 return (struct htx_ret){.ret = ret, .blk = dstblk};
496}
497
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200498/* Replaces an header by a new one. The new header can be smaller or larger than
499 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100500 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200501 */
502struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100503 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200504{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100505 enum htx_blk_type type;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100506 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200507
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100508 type = htx_get_blk_type(blk);
509 if (type != HTX_BLK_HDR)
510 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200511
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100512 delta = name.len + value.len - htx_get_blksz(blk);
513
514 /* easy case, new value is smaller, so replace it in-place */
515 if (delta <= 0) {
516 blk->info = (type << 28) + (value.len << 8) + name.len;
517 htx->data += delta;
518 goto copy;
519 }
520
521 /* we need to allocate more space to store the new value */
522 if (delta > htx_free_space(htx))
523 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100525 /* Expand the block size. But data are not copied yet. Then defragment
526 * the HTX message.
527 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200528 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100529 htx->data += delta;
530 blk = htx_defrag(htx, blk);
531
532 copy:
533 /* Finaly, copy data. */
Willy Tarreaued00e342018-12-07 08:47:45 +0100534 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100535 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100536 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200537}
538
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100539/* Replaces the parts of the start-line. It returns the new start-line on
540 * success, otherwise it returns NULL. It is the caller responsibility to update
541 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100543struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
544 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200545{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100546 struct htx_sl *sl;
547 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100548 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200549 uint32_t size;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100550 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200551
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100552 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100553 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100554 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200555
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100556 /* Save start-line info and flags */
557 sl = htx_get_blk_ptr(htx, blk);
558 tmp.info = sl->info;
559 tmp.flags = sl->flags;
Christopher Faulet54483df2018-11-26 15:05:52 +0100560 if (htx->sl_off == blk->addr)
561 htx->sl_off = -1;
562
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100563
564 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100565 delta = size - htx_get_blksz(blk);
566
567 /* easy case, new data are smaller, so replace it in-place */
568 if (delta <= 0) {
569 htx_set_blk_value_len(blk, size);
570 htx->data += delta;
571 goto copy;
572 }
573
574 /* we need to allocate more space to store the new value */
575 if (delta > htx_free_space(htx))
576 return NULL; /* not enough space */
577
578 /* Expand the block size. But data are not copied yet. Then defragment
579 * the HTX message.
580 */
581 htx_set_blk_value_len(blk, size);
582 htx->data += delta;
583 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200584
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100585 copy:
586 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100587 sl = htx_get_blk_ptr(htx, blk);
588 sl->info = tmp.info;
589 sl->flags = tmp.flags;
590 if (htx->sl_off == -1)
591 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200592
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100593 HTX_SL_P1_LEN(sl) = p1.len;
594 HTX_SL_P2_LEN(sl) = p2.len;
595 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100596
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100597 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
598 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
599 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100601 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200602}
603
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100604/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
605 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100607struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
608 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200609{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100610 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100611 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612 uint32_t size;
613
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100614 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
615 return NULL;
616
617 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100619 /* FIXME: check size (< 256MB) */
620 blk = htx_add_blk(htx, type, size);
621 if (!blk)
622 return NULL;
623 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200624
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100625 sl = htx_get_blk_ptr(htx, blk);
626 if (htx->sl_off == -1)
627 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200628
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100629 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200630
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100631 HTX_SL_P1_LEN(sl) = p1.len;
632 HTX_SL_P2_LEN(sl) = p2.len;
633 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200634
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100635 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
636 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
637 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
638
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100639 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200640}
641
642/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100643 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200644 */
645struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100646 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100648 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200649
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100650 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
651 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
652 if (!blk)
653 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200654
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100655 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100656 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100657 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
658 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200659}
660
661struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
662{
663 int i;
664
665 for (i = 0; hdrs[i].n.len; i++) {
666 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
667 return NULL;
668 }
669 return htx_add_endof(htx, HTX_BLK_EOH);
670}
671/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
672 * success. Otherwise, it returns NULL.
673 */
674struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100675 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200676{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100677 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200678
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100679 /* FIXME: check value.len ( < 1MB) */
680 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
681 if (!blk)
682 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200683
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100684 blk->info += (value.len << 8) + phdr;
685 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
686 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200687}
688
689/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
690 * on success. Otherwise, it returns NULL.
691 */
692struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
693{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100694 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200695
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100696 blk = htx_add_blk(htx, type, 1);
697 if (!blk)
698 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200699
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100700 blk->info += 1;
701 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200702}
703
704
705/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
706 * possible. It returns the new block on success. Otherwise, it returns NULL.
707 */
708struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
709{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100710 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200711}
712
713/* Adds an HTX block of type TLR in <htx>. It first tries to append trailers
714 * data if possible. It returns the new block on success. Otherwise, it returns
715 * NULL.
716 */
717struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
718{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100719 return htx_append_blk_value(htx, HTX_BLK_TLR, tlr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720}
721
722/* Adds an HTX block of type OOB in <htx>. It returns the new block on
723 * success. Otherwise, it returns NULL.
724 */
725struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob)
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 /* FIXME: check oob.len (< 256MB) */
730 blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len);
731 if (!blk)
732 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100734 blk->info += oob.len;
735 memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len);
736 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200737}
738
Christopher Faulet24ed8352018-11-22 11:20:43 +0100739struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
740 const struct ist data)
741{
742 struct htx_blk *blk;
743 int32_t prev;
744
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100745 /* FIXME: check data.len (< 256MB) */
746 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
747 if (!blk)
748 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100749
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100750 blk->info += data.len;
751 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100752
753 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
754 struct htx_blk *pblk = htx_get_blk(htx, prev);
755
756 /* Swap .addr and .info fields */
757 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
758 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
759
760 if (blk->addr == pblk->addr)
761 blk->addr += htx_get_blksz(pblk);
762 htx->front = prev;
763
764 if (pblk == ref)
765 break;
766 blk = pblk;
767 }
768 return blk;
769}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200770
Christopher Fauletc59ff232018-12-03 13:58:44 +0100771/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
773 * returns 0.
774 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100775int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200776{
Christopher Faulet570d1612018-11-26 11:13:57 +0100777 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200778 return 0;
779
Christopher Faulet570d1612018-11-26 11:13:57 +0100780 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200781 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100782 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200783 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100784 if (sl->flags & HTX_SL_F_VER_11)
785 chunk_memcat(chk, "HTTP/1.1", 8);
786 else
787 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
788
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200789 chunk_memcat(chk, "\r\n", 2);
790
791 return 1;
792}
793
Christopher Fauletc59ff232018-12-03 13:58:44 +0100794/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795 * <chk>. It returns 1 if data are successfully appended, otherwise it
796 * returns 0.
797 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100798int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200799{
Christopher Faulet570d1612018-11-26 11:13:57 +0100800 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200801 return 0;
802
Christopher Faulet1e7af462018-12-03 14:05:01 +0100803 if (sl->flags & HTX_SL_F_VER_11)
804 chunk_memcat(chk, "HTTP/1.1", 8);
805 else
806 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100808 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100810 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811 chunk_memcat(chk, "\r\n", 2);
812
813 return 1;
814}
815
Christopher Fauletc59ff232018-12-03 13:58:44 +0100816/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
818 * 0.
819 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100820int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821{
822 if (n.len + v.len + 4 > b_room(chk))
823 return 0;
824
825 chunk_memcat(chk, n.ptr, n.len);
826 chunk_memcat(chk, ": ", 2);
827 chunk_memcat(chk, v.ptr, v.len);
828 chunk_memcat(chk, "\r\n", 2);
829
830 return 1;
831}
832
Christopher Fauletc59ff232018-12-03 13:58:44 +0100833/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200834 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
835 * returns 1 if data are successfully appended, otherwise it returns 0.
836 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100837int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838{
839 if (chunked) {
840 uint32_t chksz;
841 char tmp[10];
842 char *beg, *end;
843
844 chksz = data.len;
845
846 beg = end = tmp+10;
847 *--beg = '\n';
848 *--beg = '\r';
849 do {
850 *--beg = hextab[chksz & 0xF];
851 } while (chksz >>= 4);
852
853 if (data.len + (end - beg) + 2 > b_room(chk))
854 return 0;
855 chunk_memcat(chk, beg, end - beg);
856 chunk_memcat(chk, data.ptr, data.len);
857 chunk_memcat(chk, "\r\n", 2);
858 }
859 else {
860 if (!chunk_memcat(chk, data.ptr, data.len))
861 return 0;
862 }
863
864 return 1;
865}
866
Christopher Fauletc59ff232018-12-03 13:58:44 +0100867/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200868 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
869 * 0.
870 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100871int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200872{
873 /* FIXME: be sure the CRLF is here or remove it when inserted */
874 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
875 return 0;
876 return 1;
877}