blob: cd16bf3a1b161d4c75f366ddc5b480bd93c28187 [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 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 Fauletaa75b3d2018-12-05 16:20:40 +0100371 struct htx_blk *frtblk;
372 struct buffer *tmp;
373 struct ist n, v;
374 uint32_t info, room;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200375
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100376 n = htx_get_blk_name(htx, blk);
377 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200378
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100379 /* easy case, new data are smaller, so replace it in-place */
380 if (new.len <= old.len) {
381 memcpy(old.ptr, new.ptr, new.len);
382 if (old.len != v.len)
383 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
384 htx_set_blk_value_len(blk, v.len - old.len + new.len);
385 htx->data -= (old.len - new.len);
386 return blk;
387 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200388
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100389 /* we need to allocate more space to store the new header value */
390 if ((new.len - old.len) > htx_free_space(htx))
391 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200392
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100393 /*
394 * Copy the new header in a temp buffer
395 */
396 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200397
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100398 /* 1. copy the header name */
399 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200400
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100401 /* 2. copy value before old part, if any */
402 if (old.ptr != v.ptr)
403 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200404
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100405 /* 3. copy new value */
406 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200407
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100408 /* 4. copy value after old part if any */
409 if (old.len != v.len)
410 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200411
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100412 /*
413 * temporarely remove space reserved for the header
414 */
415 info = blk->info;
416 blk->info &= 0xf0000000;
417 htx->data -= (n.len + v.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200418
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100419 /*
420 * Try to find right addr to copy all the data
421 */
422 if (htx->tail + 1 == htx->wrap) {
423 frtblk = htx_get_blk(htx, htx->front);
424 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
425 if (room >= htx->data) {
426 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
427 goto replace_value;
428 }
429 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200430
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100431 /* HTX message need to be defragmented first */
432 blk = htx_defrag(htx, blk);
433 frtblk = htx_get_blk(htx, htx->front);
434 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200435
436 replace_value:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100437 blk->info = info;
438 htx_set_blk_value_len(blk, v.len - old.len + new.len);
439 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
440 htx->data += tmp->data;
441 htx->front = htx_get_blk_pos(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200442
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100443 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200444}
445
446/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
447 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were
448 * moved. It returns the number of bytes of data moved and the last HTX block
449 * inserted in <dst>.
450 */
451struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
452 enum htx_blk_type mark)
453{
454 struct htx_blk *blk, *dstblk;
455 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100456 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200457
458 ret = 0;
459 blk = htx_get_blk(src, htx_get_head(src));
460 dstblk = NULL;
461 while (blk && ret <= count) {
462 type = htx_get_blk_type(blk);
463
464 /* Ingore unused block */
465 if (type == HTX_BLK_UNUSED)
466 goto next;
467
468 sz = htx_get_blksz(blk);
469 if (!sz) {
470 dstblk = htx_reserve_nxblk(dst, 0);
471 if (!dstblk)
472 break;
473 dstblk->info = blk->info;
474 goto next;
475 }
476
477 info = blk->info;
478 max = htx_free_data_space(dst);
479 if (max > count)
480 max = count;
481 if (sz > max) {
482 sz = max;
483 info = (type << 28) + sz;
484 /* Headers and pseudo headers must be fully copied */
485 if (type < HTX_BLK_DATA || !sz)
486 break;
487 }
488
489 dstblk = htx_reserve_nxblk(dst, sz);
490 if (!dstblk)
491 break;
492 dstblk->info = info;
493 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
494
495 ret += sz;
496 if (blk->info != info) {
497 /* Partial move: don't remove <blk> from <src> but
498 * resize its content */
499 blk->addr += sz;
500 htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz);
501 src->data -= sz;
502 break;
503 }
504
Christopher Faulet54483df2018-11-26 15:05:52 +0100505 if (dst->sl_off == -1 && src->sl_off == blk->addr)
506 dst->sl_off = dstblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200507 next:
508 blk = htx_remove_blk(src, blk);
509 if (type == mark)
510 break;
511
512 }
513
514 return (struct htx_ret){.ret = ret, .blk = dstblk};
515}
516
517static struct htx_blk *htx_new_blk_value(struct htx *htx, struct htx_blk *blk,
518 uint32_t newsz)
519{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100520 struct htx_blk *frtblk;
521 uint32_t sz, room;
522 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200523
524 sz = htx_get_blksz(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100525 delta = newsz - sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100527 /* easy case, new value is smaller, so replace it in-place */
528 if (delta <= 0) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200529 /* Reset value size. It is the caller responsibility to set the new one */
530 blk->info &= 0xf0000000;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100531 htx->data += delta;
532 return blk;
533 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200534
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100535 /* we need to allocate more space to store the new value */
536 if (delta > htx_free_space(htx))
537 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200538
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100539 /*
540 * temporarely remove space reserved for the old value
541 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542 /* Reset value size. It is the caller responsibility to set the new one */
543 blk->info &= 0xf0000000;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100544 htx->data -= sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200545
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100546 /*
547 * Try to find right addr to copy all the data
548 */
549 if (htx->tail + 1 == htx->wrap) {
550 frtblk = htx_get_blk(htx, htx->front);
551 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
552 if (room >= newsz)
553 goto replace_value;
554 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200555
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100556 /* HTX message need to be defragmented first */
557 blk = htx_defrag(htx, blk);
558 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200559
560 replace_value:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100561 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
562 htx->data += newsz;
563 htx->front = htx_get_blk_pos(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200564
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100565 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566}
567
568/* Replaces an header by a new one. The new header can be smaller or larger than
569 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100570 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200571 */
572struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100573 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200574{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100575 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200576
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100577 type = htx_get_blk_type(blk);
578 if (type != HTX_BLK_HDR)
579 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200580
581 blk = htx_new_blk_value(htx, blk, (name.len + value.len));
582 if (!blk)
583 return NULL;
584
585 blk->info = (type << 28) + (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100586 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100587 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200588
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100589 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200590}
591
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100592/* Replaces the parts of the start-line. It returns the new start-line on
593 * success, otherwise it returns NULL. It is the caller responsibility to update
594 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200595 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100596struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
597 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200598{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100599 struct htx_sl *sl;
600 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200602 uint32_t size;
603
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100604 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100605 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100606 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200607
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100608 /* Save start-line info and flags */
609 sl = htx_get_blk_ptr(htx, blk);
610 tmp.info = sl->info;
611 tmp.flags = sl->flags;
Christopher Faulet54483df2018-11-26 15:05:52 +0100612 if (htx->sl_off == blk->addr)
613 htx->sl_off = -1;
614
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100615
616 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617 blk = htx_new_blk_value(htx, blk, size);
618 if (!blk)
619 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620 blk->info = (type << 28) + size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200621
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100622 /* Restore start-line info and flags*/
623 sl = htx_get_blk_ptr(htx, blk);
624 sl->info = tmp.info;
625 sl->flags = tmp.flags;
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 HTX_SL_P1_LEN(sl) = p1.len;
630 HTX_SL_P2_LEN(sl) = p2.len;
631 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100632
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100633 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
634 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
635 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100637 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200638}
639
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100640/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
641 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200642 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100643struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
644 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200645{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100646 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100647 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200648 uint32_t size;
649
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100650 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
651 return NULL;
652
653 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200654
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100655 /* FIXME: check size (< 256MB) */
656 blk = htx_add_blk(htx, type, size);
657 if (!blk)
658 return NULL;
659 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200660
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100661 sl = htx_get_blk_ptr(htx, blk);
662 if (htx->sl_off == -1)
663 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200664
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100665 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200666
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100667 HTX_SL_P1_LEN(sl) = p1.len;
668 HTX_SL_P2_LEN(sl) = p2.len;
669 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200670
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100671 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
672 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
673 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
674
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100675 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200676}
677
678/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100679 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200680 */
681struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100682 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200683{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100684 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200685
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100686 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
687 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
688 if (!blk)
689 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200690
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100691 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100692 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100693 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
694 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200695}
696
697struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
698{
699 int i;
700
701 for (i = 0; hdrs[i].n.len; i++) {
702 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
703 return NULL;
704 }
705 return htx_add_endof(htx, HTX_BLK_EOH);
706}
707/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
708 * success. Otherwise, it returns NULL.
709 */
710struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100711 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200712{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100713 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200714
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100715 /* FIXME: check value.len ( < 1MB) */
716 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
717 if (!blk)
718 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200719
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100720 blk->info += (value.len << 8) + phdr;
721 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
722 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200723}
724
725/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
726 * on success. Otherwise, it returns NULL.
727 */
728struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
729{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100730 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200731
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100732 blk = htx_add_blk(htx, type, 1);
733 if (!blk)
734 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200735
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100736 blk->info += 1;
737 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200738}
739
740
741/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
742 * possible. It returns the new block on success. Otherwise, it returns NULL.
743 */
744struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
745{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100746 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747}
748
749/* Adds an HTX block of type TLR in <htx>. It first tries to append trailers
750 * data if possible. It returns the new block on success. Otherwise, it returns
751 * NULL.
752 */
753struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
754{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100755 return htx_append_blk_value(htx, HTX_BLK_TLR, tlr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200756}
757
758/* Adds an HTX block of type OOB in <htx>. It returns the new block on
759 * success. Otherwise, it returns NULL.
760 */
761struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob)
762{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100763 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200764
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100765 /* FIXME: check oob.len (< 256MB) */
766 blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len);
767 if (!blk)
768 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200769
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100770 blk->info += oob.len;
771 memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len);
772 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200773}
774
Christopher Faulet24ed8352018-11-22 11:20:43 +0100775struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
776 const struct ist data)
777{
778 struct htx_blk *blk;
779 int32_t prev;
780
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100781 /* FIXME: check data.len (< 256MB) */
782 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
783 if (!blk)
784 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100785
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100786 blk->info += data.len;
787 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100788
789 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
790 struct htx_blk *pblk = htx_get_blk(htx, prev);
791
792 /* Swap .addr and .info fields */
793 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
794 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
795
796 if (blk->addr == pblk->addr)
797 blk->addr += htx_get_blksz(pblk);
798 htx->front = prev;
799
800 if (pblk == ref)
801 break;
802 blk = pblk;
803 }
804 return blk;
805}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200806
Christopher Fauletc59ff232018-12-03 13:58:44 +0100807/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200808 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
809 * returns 0.
810 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100811int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200812{
Christopher Faulet570d1612018-11-26 11:13:57 +0100813 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200814 return 0;
815
Christopher Faulet570d1612018-11-26 11:13:57 +0100816 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100818 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200819 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100820 if (sl->flags & HTX_SL_F_VER_11)
821 chunk_memcat(chk, "HTTP/1.1", 8);
822 else
823 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
824
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200825 chunk_memcat(chk, "\r\n", 2);
826
827 return 1;
828}
829
Christopher Fauletc59ff232018-12-03 13:58:44 +0100830/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831 * <chk>. It returns 1 if data are successfully appended, otherwise it
832 * returns 0.
833 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100834int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835{
Christopher Faulet570d1612018-11-26 11:13:57 +0100836 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837 return 0;
838
Christopher Faulet1e7af462018-12-03 14:05:01 +0100839 if (sl->flags & HTX_SL_F_VER_11)
840 chunk_memcat(chk, "HTTP/1.1", 8);
841 else
842 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200843 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100844 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200845 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100846 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200847 chunk_memcat(chk, "\r\n", 2);
848
849 return 1;
850}
851
Christopher Fauletc59ff232018-12-03 13:58:44 +0100852/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200853 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
854 * 0.
855 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100856int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200857{
858 if (n.len + v.len + 4 > b_room(chk))
859 return 0;
860
861 chunk_memcat(chk, n.ptr, n.len);
862 chunk_memcat(chk, ": ", 2);
863 chunk_memcat(chk, v.ptr, v.len);
864 chunk_memcat(chk, "\r\n", 2);
865
866 return 1;
867}
868
Christopher Fauletc59ff232018-12-03 13:58:44 +0100869/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200870 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
871 * returns 1 if data are successfully appended, otherwise it returns 0.
872 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100873int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200874{
875 if (chunked) {
876 uint32_t chksz;
877 char tmp[10];
878 char *beg, *end;
879
880 chksz = data.len;
881
882 beg = end = tmp+10;
883 *--beg = '\n';
884 *--beg = '\r';
885 do {
886 *--beg = hextab[chksz & 0xF];
887 } while (chksz >>= 4);
888
889 if (data.len + (end - beg) + 2 > b_room(chk))
890 return 0;
891 chunk_memcat(chk, beg, end - beg);
892 chunk_memcat(chk, data.ptr, data.len);
893 chunk_memcat(chk, "\r\n", 2);
894 }
895 else {
896 if (!chunk_memcat(chk, data.ptr, data.len))
897 return 0;
898 }
899
900 return 1;
901}
902
Christopher Fauletc59ff232018-12-03 13:58:44 +0100903/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200904 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
905 * 0.
906 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100907int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200908{
909 /* FIXME: be sure the CRLF is here or remove it when inserted */
910 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
911 return 0;
912 return 1;
913}