blob: 3e7e4df38435267dcb96d00104eb5b3fd921d017 [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;
Christopher Faulet200f8952019-01-02 11:23:44 +010029 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010030 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 Faulet200f8952019-01-02 11:23:44 +010036 blkpos = -1;
37
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010038 new = 0;
39 addr = 0;
40 tmp->size = htx->size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020041
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010042 /* start from the head */
43 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
44 oldblk = htx_get_blk(htx, old);
45 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED) {
46 htx->used--;
47 continue;
48 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020049
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010050 newblk = htx_get_blk(tmp, new);
51 newblk->addr = addr;
52 newblk->info = oldblk->info;
53 blksz = htx_get_blksz(oldblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020054
Christopher Faulet174bfb12018-12-06 14:31:12 +010055 /* update the start-line offset */
56 if (htx->sl_off == oldblk->addr)
57 sl_off = addr;
58
Christopher Faulet200f8952019-01-02 11:23:44 +010059 /* if <blk> is defined, set its new position */
60 if (blk != NULL && blk == oldblk)
61 blkpos = new;
62
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010063 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
64 new++;
65 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010067 } while (new < htx->used);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020068
Christopher Faulet174bfb12018-12-06 14:31:12 +010069 htx->sl_off = sl_off;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010070 htx->wrap = htx->used;
71 htx->front = htx->tail = new - 1;
72 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020073
Christopher Faulet200f8952019-01-02 11:23:44 +010074 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020075}
76
77/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
78 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
79 * block is returned and the HTTP message is updated. Space for this new block
80 * is reserved in the HTTP message. But it is the caller responsibility to set
81 * right info in the block to reflect the stored data.
82 */
83static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
84{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010085 struct htx_blk *blk, *prevblk, *headblk, *frtblk;
86 uint32_t used;
87 uint32_t tail;
88 uint32_t prev;
89 uint32_t wrap;
90 uint32_t head;
91 int32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020092
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010093 if (blksz > htx_free_data_space(htx))
94 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +020095
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010096 if (!htx->used) {
97 /* Empty message */
98 htx->front = htx->tail = 0;
99 htx->wrap = htx->used = 1;
100 blk = htx_get_blk(htx, htx->tail);
101 blk->addr = 0;
102 htx->data = blksz;
103 return blk;
104 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200105
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100106 used = htx->used + 1;
107 tail = htx->tail + 1;
108 prev = htx->tail;
109 wrap = htx->wrap;
110 head = htx_get_head(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200111
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100112 if (tail == wrap) {
113 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200114
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100115 /* Blocks don't wrap for now. We either need to push the new one
116 * on top of others or to defragement the table. */
117 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap+1) >= frtblk->addr + htx_get_blksz(frtblk))
118 wrap++;
119 else if (tail >= used) /* There is hole at the beginning */
120 tail = 0;
121 else {
122 /* No more room, tail hits data. We have to realign the
123 * whole message. */
124 goto defrag;
125 }
126 }
127 else if (used >= wrap) {
128 /* We have hit the tail, we need to reorganize the blocks. */
129 goto defrag;
130 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200131
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100132 /* Now we have updated tail, used and wrap, we know that there is some
133 * available room at least from the protocol's perspective. This space
134 * is split in two areas :
135 *
136 * 1: the space between the beginning of the blocks table and the
137 * front data's address. This space will only be used if data don't
138 * wrap yet.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200139
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100140 * 2: If the previous tail was the front block, the space between the
141 * beginning of the message and the head data's address.
142 * Otherwise, the space between the tail data's address and the
143 * tail's one.
144 */
145 prevblk = htx_get_blk(htx, prev);
146 headblk = htx_get_blk(htx, head);
147 if (prevblk->addr >= headblk->addr) {
148 /* the area was contiguous */
149 frtblk = htx_get_blk(htx, htx->front);
150 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap) - (frtblk->addr + htx_get_blksz(frtblk));
151 headroom = headblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200152
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100153 if (tailroom >= (int32_t)blksz) {
154 /* install upfront and update ->front */
155 blk = htx_get_blk(htx, tail);
156 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
157 htx->front = tail;
158 }
159 else if (headroom >= (int32_t)blksz) {
160 blk = htx_get_blk(htx, tail);
161 blk->addr = 0;
162 }
163 else {
164 /* need to defragment the table before inserting upfront */
165 goto defrag;
166 }
167 }
168 else {
169 /* it's already wrapped so we can't store anything in the tailroom */
170 headroom = headblk->addr - (prevblk->addr + htx_get_blksz(prevblk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200171
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100172 if (headroom >= (int32_t)blksz) {
173 blk = htx_get_blk(htx, tail);
174 blk->addr = prevblk->addr + htx_get_blksz(prevblk);
175 }
176 else {
177 defrag:
178 /* need to defragment the table before inserting upfront */
179 htx_defrag(htx, NULL);
180 frtblk = htx_get_blk(htx, htx->front);
181 wrap = htx->wrap + 1;
182 tail = htx->tail + 1;
183 used = htx->used + 1;
184 blk = htx_get_blk(htx, tail);
185 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
186 htx->front = tail;
187 }
188 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200189
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100190 htx->wrap = wrap;
191 htx->tail = tail;
192 htx->used = used;
193 htx->data += blksz;
194 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200195}
196
197/* Adds a new block of type <type> in the HTTP message <htx>. Its content size
198 * is passed but it is the caller responsibility to do the copy.
199 */
200struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
201{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100202 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200203
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100204 blk = htx_reserve_nxblk(htx, blksz);
205 if (!blk)
206 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200207
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100208 blk->info = (type << 28);
209 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200210}
211
212/* Removes the block <blk> from the HTTP message <htx>. The function returns the
213 * block following <blk> or NULL if <blk> is the last block or the last
214 * inserted one.
215 */
216struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
217{
Christopher Faulet54483df2018-11-26 15:05:52 +0100218 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100219 uint32_t next, head, pos;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200220
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100221 if (type != HTX_BLK_UNUSED) {
222 /* Mark the block as unused, decrement allocated size */
223 htx->data -= htx_get_blksz(blk);
224 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Faulet54483df2018-11-26 15:05:52 +0100225 if (htx->sl_off == blk->addr)
226 htx->sl_off = -1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100227 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200228
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100229 /* This is the last block in use */
230 if (htx->used == 1/* || !htx->data */) {
231 htx->front = htx->tail = 0;
232 htx->wrap = htx->used = 0;
233 htx->data = 0;
234 return NULL;
235 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200236
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100237 /* There is at least 2 blocks, so tail is always >= 0 */
238 pos = htx_get_blk_pos(htx, blk);
239 head = htx_get_head(htx);
240 blk = NULL;
241 next = pos + 1; /* By default retrun the next block */
242 if (htx->tail + 1 == htx->wrap) {
243 /* The HTTP message doesn't wrap */
244 if (pos == head) {
245 /* remove the head, so just return the new head */
246 htx->used--;
247 next = htx_get_head(htx);
248 }
249 else if (pos == htx->tail) {
250 /* remove the tail. this was the last inserted block so
251 * return NULL. */
252 htx->wrap--;
253 htx->tail--;
254 htx->used--;
255 goto end;
256 }
257 }
258 else {
259 /* The HTTP message wraps */
260 if (pos == htx->tail) {
261 /* remove the tail. try to unwrap the message (pos == 0)
262 * and return NULL. */
263 htx->tail = ((pos == 0) ? htx->wrap-1 : htx->tail-1);
264 htx->used--;
265 goto end;
266 }
267 else if (pos == head) {
268 /* remove the head, try to unwrap the message (pos+1 ==
269 * wrap) and return the new head */
270 htx->used--;
271 if (pos + 1 == htx->wrap)
272 htx->wrap = htx->tail + 1;
273 next = htx_get_head(htx);
274 }
275 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200276
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100277 blk = htx_get_blk(htx, next);
Christopher Faulet54483df2018-11-26 15:05:52 +0100278 if (htx->sl_off == -1) {
279 /* Try to update the start-line offset, if possible */
280 type = htx_get_blk_type(blk);
281 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
282 htx->sl_off = blk->addr;
283 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200284 end:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100285 if (pos == htx->front)
286 htx->front = htx_find_front(htx);
287 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200288}
289
Christopher Faulet00cf6972019-01-07 14:53:27 +0100290/* Truncate all blocks after the one containing the offset <offset>. This last
291 * one is truncated too.
292 */
293void htx_truncate(struct htx *htx, uint32_t offset)
294{
295 struct htx_blk *blk;
296 struct htx_ret htxret;
297
298 htxret = htx_find_blk(htx, offset);
299 blk = htxret.blk;
300 if (blk && htxret.ret) {
301 uint32_t sz = htx_get_blksz(blk);
302
303 htx_set_blk_value_len(blk, sz - htxret.ret);
304 blk = htx_get_next_blk(htx, blk);
305 }
306 while (blk)
307 blk = htx_remove_blk(htx, blk);
308}
309
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200310/* Tries to append data to the last inserted block, if the type matches and if
311 * there is enough non-wrapping space. Only DATA and TRAILERS content can be
312 * appended. If the append fails, a new block is inserted. If an error occurred,
313 * NULL is returned. Otherwise, on success, the updated block (or the new one)
314 * is returned.
315*/
316static struct htx_blk *htx_append_blk_value(struct htx *htx, enum htx_blk_type type,
317 const struct ist data)
318{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100319 struct htx_blk *blk;
320 struct ist v;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200321
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100322 if (!htx->used)
323 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200324
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100325 /* Not enough space to store data */
326 if (data.len > htx_free_data_space(htx))
327 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200328
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100329 /* Append only DATA et TRAILERS data */
330 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
331 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200332
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100333 /* get the tail block */
334 blk = htx_get_blk(htx, htx->tail);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200335
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100336 /* Don't try to append data if the last inserted block is not of the
337 * same type */
338 if (type != htx_get_blk_type(blk))
339 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200340
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100341 /*
342 * Same type and enough space: append data
343 */
344 if (htx->tail + 1 == htx->wrap) {
345 struct htx_blk *frtblk = htx_get_blk(htx, htx->front);
346 int32_t tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
347 if (tailroom >= (int32_t)data.len)
348 goto append_data;
349 htx_defrag(htx, NULL);
350 blk = htx_get_blk(htx, htx->tail);
351 }
352 else {
353 struct htx_blk *headblk = htx_get_blk(htx, htx_get_head(htx));
354 int32_t headroom = headblk->addr - (blk->addr + htx_get_blksz(blk));
355 if (headroom >= (int32_t)data.len)
356 goto append_data;
357 htx_defrag(htx, NULL);
358 blk = htx_get_blk(htx, htx->tail);
359 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200360
361 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100362 /* get the value of the tail block */
363 /* FIXME: check v.len + data.len < 256MB */
364 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200365
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100366 /* Append data and update the block itself */
367 memcpy(v.ptr + v.len, data.ptr, data.len);
368 htx_set_blk_value_len(blk, v.len + data.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200369
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100370 /* Update HTTP message */
371 htx->data += data.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200372
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100373 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200374
375 add_new_block:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100376 /* FIXME: check tlr.len (< 256MB) */
377 blk = htx_add_blk(htx, type, data.len);
378 if (!blk)
379 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200380
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100381 blk->info += data.len;
382 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
383 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200384}
385
386/* Replaces a value part of a block by a new one. The new part can be smaller or
387 * larger than the old one. This function works for any kind of block with
388 * attached data. It returns the new block on success, otherwise it returns
389 * NULL.
390 */
391struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100392 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200393{
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100394 struct buffer *tmp;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100395 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100396 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200397
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100398 n = htx_get_blk_name(htx, blk);
399 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200400
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100401 delta = new.len - old.len;
402
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100403 /* easy case, new data are smaller, so replace it in-place */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100404 if (delta <= 0) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100405 memcpy(old.ptr, new.ptr, new.len);
406 if (old.len != v.len)
407 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100408 htx_set_blk_value_len(blk, v.len + delta);
409 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100410 return blk;
411 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200412
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100413 /* we need to allocate more space to store the new header value */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100414 if (delta > htx_free_space(htx))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100415 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200416
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100417 /*
418 * Copy the new header in a temp buffer
419 */
420 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200421
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100422 /* 1. copy the header name */
423 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200424
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100425 /* 2. copy value before old part, if any */
426 if (old.ptr != v.ptr)
427 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200428
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100429 /* 3. copy new value */
430 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200431
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100432 /* 4. copy value after old part if any */
433 if (old.len != v.len)
434 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200435
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200436
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100437 /* Expand the block size. But data are not copied yet. Then defragment
438 * the HTX message.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100439 */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100440 htx_set_blk_value_len(blk, v.len + delta);
441 htx->data += delta;
442 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200443
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100444 /* Finaly, copy data. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100445 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200446
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100447 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200448}
449
450/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
451 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were
452 * moved. It returns the number of bytes of data moved and the last HTX block
453 * inserted in <dst>.
454 */
455struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
456 enum htx_blk_type mark)
457{
458 struct htx_blk *blk, *dstblk;
459 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100460 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200461
462 ret = 0;
463 blk = htx_get_blk(src, htx_get_head(src));
464 dstblk = NULL;
465 while (blk && ret <= count) {
466 type = htx_get_blk_type(blk);
467
468 /* Ingore unused block */
469 if (type == HTX_BLK_UNUSED)
470 goto next;
471
472 sz = htx_get_blksz(blk);
473 if (!sz) {
474 dstblk = htx_reserve_nxblk(dst, 0);
475 if (!dstblk)
476 break;
477 dstblk->info = blk->info;
478 goto next;
479 }
480
481 info = blk->info;
482 max = htx_free_data_space(dst);
483 if (max > count)
484 max = count;
485 if (sz > max) {
486 sz = max;
487 info = (type << 28) + sz;
488 /* Headers and pseudo headers must be fully copied */
489 if (type < HTX_BLK_DATA || !sz)
490 break;
491 }
492
493 dstblk = htx_reserve_nxblk(dst, sz);
494 if (!dstblk)
495 break;
496 dstblk->info = info;
497 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
498
499 ret += sz;
500 if (blk->info != info) {
501 /* Partial move: don't remove <blk> from <src> but
502 * resize its content */
503 blk->addr += sz;
504 htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz);
505 src->data -= sz;
506 break;
507 }
508
Christopher Faulet54483df2018-11-26 15:05:52 +0100509 if (dst->sl_off == -1 && src->sl_off == blk->addr)
510 dst->sl_off = dstblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200511 next:
512 blk = htx_remove_blk(src, blk);
513 if (type == mark)
514 break;
515
516 }
517
518 return (struct htx_ret){.ret = ret, .blk = dstblk};
519}
520
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200521/* Replaces an header by a new one. The new header can be smaller or larger than
522 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100523 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524 */
525struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100526 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200527{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100528 enum htx_blk_type type;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100529 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200530
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100531 type = htx_get_blk_type(blk);
532 if (type != HTX_BLK_HDR)
533 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200534
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100535 delta = name.len + value.len - htx_get_blksz(blk);
536
537 /* easy case, new value is smaller, so replace it in-place */
538 if (delta <= 0) {
539 blk->info = (type << 28) + (value.len << 8) + name.len;
540 htx->data += delta;
541 goto copy;
542 }
543
544 /* we need to allocate more space to store the new value */
545 if (delta > htx_free_space(htx))
546 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200547
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100548 /* Expand the block size. But data are not copied yet. Then defragment
549 * the HTX message.
550 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200551 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100552 htx->data += delta;
553 blk = htx_defrag(htx, blk);
554
555 copy:
556 /* Finaly, copy data. */
Willy Tarreaued00e342018-12-07 08:47:45 +0100557 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100558 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100559 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200560}
561
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100562/* Replaces the parts of the start-line. It returns the new start-line on
563 * success, otherwise it returns NULL. It is the caller responsibility to update
564 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200565 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100566struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
567 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200568{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100569 struct htx_sl *sl;
570 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100571 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200572 uint32_t size;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100573 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200574
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100575 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100576 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100577 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200578
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100579 /* Save start-line info and flags */
580 sl = htx_get_blk_ptr(htx, blk);
581 tmp.info = sl->info;
582 tmp.flags = sl->flags;
Christopher Faulet54483df2018-11-26 15:05:52 +0100583 if (htx->sl_off == blk->addr)
584 htx->sl_off = -1;
585
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100586
587 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100588 delta = size - htx_get_blksz(blk);
589
590 /* easy case, new data are smaller, so replace it in-place */
591 if (delta <= 0) {
592 htx_set_blk_value_len(blk, size);
593 htx->data += delta;
594 goto copy;
595 }
596
597 /* we need to allocate more space to store the new value */
598 if (delta > htx_free_space(htx))
599 return NULL; /* not enough space */
600
601 /* Expand the block size. But data are not copied yet. Then defragment
602 * the HTX message.
603 */
604 htx_set_blk_value_len(blk, size);
605 htx->data += delta;
606 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200607
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100608 copy:
609 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100610 sl = htx_get_blk_ptr(htx, blk);
611 sl->info = tmp.info;
612 sl->flags = tmp.flags;
613 if (htx->sl_off == -1)
614 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200615
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100616 HTX_SL_P1_LEN(sl) = p1.len;
617 HTX_SL_P2_LEN(sl) = p2.len;
618 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100619
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100620 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
621 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
622 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200623
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100624 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625}
626
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100627/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
628 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200629 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100630struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
631 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200632{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100633 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100634 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200635 uint32_t size;
636
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100637 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
638 return NULL;
639
640 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200641
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100642 /* FIXME: check size (< 256MB) */
643 blk = htx_add_blk(htx, type, size);
644 if (!blk)
645 return NULL;
646 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100648 sl = htx_get_blk_ptr(htx, blk);
649 if (htx->sl_off == -1)
650 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200651
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100652 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200653
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100654 HTX_SL_P1_LEN(sl) = p1.len;
655 HTX_SL_P2_LEN(sl) = p2.len;
656 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200657
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100658 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
659 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
660 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
661
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100662 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200663}
664
665/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100666 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200667 */
668struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100669 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200670{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100671 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200672
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100673 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
674 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
675 if (!blk)
676 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200677
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100678 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100679 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100680 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
681 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200682}
683
Willy Tarreau52610e92019-01-03 18:26:17 +0100684/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
685 * new block on success. Otherwise, it returns NULL. The caller is responsible
686 * for filling the block itself.
687 */
688struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
689{
690 struct htx_blk *blk;
691
692 blk = htx_add_blk(htx, type, blksz);
693 if (!blk)
694 return NULL;
695
696 blk->info += blksz;
697 return blk;
698}
699
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
701{
702 int i;
703
704 for (i = 0; hdrs[i].n.len; i++) {
705 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
706 return NULL;
707 }
708 return htx_add_endof(htx, HTX_BLK_EOH);
709}
710/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
711 * success. Otherwise, it returns NULL.
712 */
713struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100714 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200715{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100716 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200717
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100718 /* FIXME: check value.len ( < 1MB) */
719 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
720 if (!blk)
721 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200722
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100723 blk->info += (value.len << 8) + phdr;
724 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
725 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200726}
727
728/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
729 * on success. Otherwise, it returns NULL.
730 */
731struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
732{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100733 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200734
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100735 blk = htx_add_blk(htx, type, 1);
736 if (!blk)
737 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200738
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100739 blk->info += 1;
740 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200741}
742
743
744/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
745 * possible. It returns the new block on success. Otherwise, it returns NULL.
746 */
747struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
748{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100749 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200750}
751
752/* Adds an HTX block of type TLR in <htx>. It first tries to append trailers
753 * data if possible. It returns the new block on success. Otherwise, it returns
754 * NULL.
755 */
756struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
757{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100758 return htx_append_blk_value(htx, HTX_BLK_TLR, tlr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200759}
760
761/* Adds an HTX block of type OOB in <htx>. It returns the new block on
762 * success. Otherwise, it returns NULL.
763 */
764struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob)
765{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100766 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200767
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100768 /* FIXME: check oob.len (< 256MB) */
769 blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len);
770 if (!blk)
771 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100773 blk->info += oob.len;
774 memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len);
775 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200776}
777
Christopher Faulet24ed8352018-11-22 11:20:43 +0100778struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
779 const struct ist data)
780{
781 struct htx_blk *blk;
782 int32_t prev;
783
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100784 /* FIXME: check data.len (< 256MB) */
785 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
786 if (!blk)
787 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100788
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100789 blk->info += data.len;
790 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100791
792 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
793 struct htx_blk *pblk = htx_get_blk(htx, prev);
794
795 /* Swap .addr and .info fields */
796 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
797 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
798
799 if (blk->addr == pblk->addr)
800 blk->addr += htx_get_blksz(pblk);
801 htx->front = prev;
802
803 if (pblk == ref)
804 break;
805 blk = pblk;
806 }
807 return blk;
808}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809
Christopher Fauletc59ff232018-12-03 13:58:44 +0100810/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
812 * returns 0.
813 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100814int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200815{
Christopher Faulet570d1612018-11-26 11:13:57 +0100816 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817 return 0;
818
Christopher Faulet570d1612018-11-26 11:13:57 +0100819 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200820 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100821 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200822 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100823 if (sl->flags & HTX_SL_F_VER_11)
824 chunk_memcat(chk, "HTTP/1.1", 8);
825 else
826 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
827
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200828 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 status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200834 * <chk>. It returns 1 if data are successfully appended, otherwise it
835 * returns 0.
836 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100837int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838{
Christopher Faulet570d1612018-11-26 11:13:57 +0100839 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200840 return 0;
841
Christopher Faulet1e7af462018-12-03 14:05:01 +0100842 if (sl->flags & HTX_SL_F_VER_11)
843 chunk_memcat(chk, "HTTP/1.1", 8);
844 else
845 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200846 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100847 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200848 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100849 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200850 chunk_memcat(chk, "\r\n", 2);
851
852 return 1;
853}
854
Christopher Fauletc59ff232018-12-03 13:58:44 +0100855/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200856 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
857 * 0.
858 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100859int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200860{
861 if (n.len + v.len + 4 > b_room(chk))
862 return 0;
863
864 chunk_memcat(chk, n.ptr, n.len);
865 chunk_memcat(chk, ": ", 2);
866 chunk_memcat(chk, v.ptr, v.len);
867 chunk_memcat(chk, "\r\n", 2);
868
869 return 1;
870}
871
Christopher Fauletc59ff232018-12-03 13:58:44 +0100872/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200873 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
874 * returns 1 if data are successfully appended, otherwise it returns 0.
875 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100876int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200877{
878 if (chunked) {
879 uint32_t chksz;
880 char tmp[10];
881 char *beg, *end;
882
883 chksz = data.len;
884
885 beg = end = tmp+10;
886 *--beg = '\n';
887 *--beg = '\r';
888 do {
889 *--beg = hextab[chksz & 0xF];
890 } while (chksz >>= 4);
891
892 if (data.len + (end - beg) + 2 > b_room(chk))
893 return 0;
894 chunk_memcat(chk, beg, end - beg);
895 chunk_memcat(chk, data.ptr, data.len);
896 chunk_memcat(chk, "\r\n", 2);
897 }
898 else {
899 if (!chunk_memcat(chk, data.ptr, data.len))
900 return 0;
901 }
902
903 return 1;
904}
905
Christopher Fauletc59ff232018-12-03 13:58:44 +0100906/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200907 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
908 * 0.
909 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100910int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200911{
912 /* FIXME: be sure the CRLF is here or remove it when inserted */
913 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
914 return 0;
915 return 1;
916}