blob: b18ce5f6755aef34696fa4c35532411d2f87d05a [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 Faulet549822f2019-02-25 10:23:19 +0100310/* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if
311 * necessary. Others blocks will be removed at once if <count> is large
312 * enough. The function returns an htx_ret with the first block remaing in the
313 * messsage and the amount of data drained. If everything is removed,
314 * htx_ret.blk is set to NULL.
315 */
316struct htx_ret htx_drain(struct htx *htx, uint32_t count)
317{
318 struct htx_blk *blk;
319 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
320
321 blk = htx_get_head_blk(htx);
322 while (count && blk) {
323 uint32_t sz = htx_get_blksz(blk);
324 enum htx_blk_type type = htx_get_blk_type(blk);
325
326 /* Ingore unused block */
327 if (type == HTX_BLK_UNUSED)
328 goto next;
329
330 if (sz > count) {
331 if (type == HTX_BLK_DATA) {
332 htx_cut_data_blk(htx, blk, count);
333 htxret.ret += count;
334 }
335 break;
336 }
337 count -= sz;
338 htxret.ret += sz;
339 next:
340 blk = htx_remove_blk(htx, blk);
341 }
342 htxret.blk = blk;
343
344 return htxret;
345}
346
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200347/* Tries to append data to the last inserted block, if the type matches and if
348 * there is enough non-wrapping space. Only DATA and TRAILERS content can be
349 * appended. If the append fails, a new block is inserted. If an error occurred,
350 * NULL is returned. Otherwise, on success, the updated block (or the new one)
351 * is returned.
352*/
353static struct htx_blk *htx_append_blk_value(struct htx *htx, enum htx_blk_type type,
354 const struct ist data)
355{
Christopher Fauletf1449b72019-04-10 14:54:46 +0200356 struct htx_blk *blk, *tailblk, *headblk, *frtblk;
357 struct ist v;
358 int32_t room;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200359
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100360 if (!htx->used)
361 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200362
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100363 /* Not enough space to store data */
364 if (data.len > htx_free_data_space(htx))
365 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200366
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100367 /* Append only DATA et TRAILERS data */
368 if (type != HTX_BLK_DATA && type != HTX_BLK_TLR)
369 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200370
Christopher Fauletf1449b72019-04-10 14:54:46 +0200371 /* get the tail and head block */
372 tailblk = htx_get_tail_blk(htx);
373 headblk = htx_get_head_blk(htx);
374 if (tailblk == NULL || headblk == NULL)
375 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200376
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100377 /* Don't try to append data if the last inserted block is not of the
378 * same type */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200379 if (type != htx_get_blk_type(tailblk))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100380 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200381
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100382 /*
383 * Same type and enough space: append data
384 */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200385 frtblk = htx_get_blk(htx, htx->front);
386 if (tailblk->addr >= headblk->addr) {
387 if (htx->tail != htx->front)
388 goto add_new_block;
389 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100390 }
Christopher Fauletf1449b72019-04-10 14:54:46 +0200391 else
392 room = headblk->addr - (tailblk->addr + htx_get_blksz(tailblk));
393
394 if (room < (int32_t)data.len)
395 tailblk = htx_defrag(htx, tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200396
397 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100398 /* get the value of the tail block */
399 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200400 v = htx_get_blk_value(htx, tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200401
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100402 /* Append data and update the block itself */
403 memcpy(v.ptr + v.len, data.ptr, data.len);
Christopher Fauletf1449b72019-04-10 14:54:46 +0200404 htx_set_blk_value_len(tailblk, v.len + data.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200405
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100406 /* Update HTTP message */
407 htx->data += data.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200408
Christopher Fauletf1449b72019-04-10 14:54:46 +0200409 return tailblk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200410
411 add_new_block:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100412 /* FIXME: check tlr.len (< 256MB) */
413 blk = htx_add_blk(htx, type, data.len);
414 if (!blk)
415 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200416
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100417 blk->info += data.len;
418 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
419 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200420}
421
422/* Replaces a value part of a block by a new one. The new part can be smaller or
423 * larger than the old one. This function works for any kind of block with
424 * attached data. It returns the new block on success, otherwise it returns
425 * NULL.
426 */
427struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100428 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200429{
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100430 struct buffer *tmp;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100431 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100432 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200433
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100434 n = htx_get_blk_name(htx, blk);
435 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200436
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100437 delta = new.len - old.len;
438
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100439 /* easy case, new data are smaller, so replace it in-place */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100440 if (delta <= 0) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100441 memcpy(old.ptr, new.ptr, new.len);
442 if (old.len != v.len)
443 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100444 htx_set_blk_value_len(blk, v.len + delta);
445 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100446 return blk;
447 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200448
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100449 /* we need to allocate more space to store the new header value */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100450 if (delta > htx_free_space(htx))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100451 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200452
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100453 /*
454 * Copy the new header in a temp buffer
455 */
456 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200457
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100458 /* 1. copy the header name */
459 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200460
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100461 /* 2. copy value before old part, if any */
462 if (old.ptr != v.ptr)
463 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200464
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100465 /* 3. copy new value */
466 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200467
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100468 /* 4. copy value after old part if any */
469 if (old.len != v.len)
470 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200471
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200472
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100473 /* Expand the block size. But data are not copied yet. Then defragment
474 * the HTX message.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100475 */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100476 htx_set_blk_value_len(blk, v.len + delta);
477 htx->data += delta;
478 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200479
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100480 /* Finaly, copy data. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100481 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200482
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100483 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200484}
485
486/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
487 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were
488 * moved. It returns the number of bytes of data moved and the last HTX block
489 * inserted in <dst>.
490 */
491struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
492 enum htx_blk_type mark)
493{
494 struct htx_blk *blk, *dstblk;
495 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100496 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200497
498 ret = 0;
499 blk = htx_get_blk(src, htx_get_head(src));
500 dstblk = NULL;
501 while (blk && ret <= count) {
502 type = htx_get_blk_type(blk);
503
504 /* Ingore unused block */
505 if (type == HTX_BLK_UNUSED)
506 goto next;
507
508 sz = htx_get_blksz(blk);
509 if (!sz) {
510 dstblk = htx_reserve_nxblk(dst, 0);
511 if (!dstblk)
512 break;
513 dstblk->info = blk->info;
514 goto next;
515 }
516
517 info = blk->info;
518 max = htx_free_data_space(dst);
519 if (max > count)
520 max = count;
Willy Tarreau90caa072019-04-09 16:21:54 +0200521 if (sz > max) {
522 sz = max;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200523 info = (type << 28) + sz;
524 /* Headers and pseudo headers must be fully copied */
525 if (type < HTX_BLK_DATA || !sz)
526 break;
527 }
528
529 dstblk = htx_reserve_nxblk(dst, sz);
530 if (!dstblk)
531 break;
532 dstblk->info = info;
533 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
534
535 ret += sz;
536 if (blk->info != info) {
537 /* Partial move: don't remove <blk> from <src> but
538 * resize its content */
539 blk->addr += sz;
540 htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz);
541 src->data -= sz;
542 break;
543 }
544
Christopher Faulet54483df2018-11-26 15:05:52 +0100545 if (dst->sl_off == -1 && src->sl_off == blk->addr)
546 dst->sl_off = dstblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200547 next:
548 blk = htx_remove_blk(src, blk);
549 if (type == mark)
550 break;
551
552 }
553
554 return (struct htx_ret){.ret = ret, .blk = dstblk};
555}
556
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557/* Replaces an header by a new one. The new header can be smaller or larger than
558 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100559 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200560 */
561struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100562 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200563{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100564 enum htx_blk_type type;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100565 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100567 type = htx_get_blk_type(blk);
568 if (type != HTX_BLK_HDR)
569 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200570
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100571 delta = name.len + value.len - htx_get_blksz(blk);
572
573 /* easy case, new value is smaller, so replace it in-place */
574 if (delta <= 0) {
575 blk->info = (type << 28) + (value.len << 8) + name.len;
576 htx->data += delta;
577 goto copy;
578 }
579
580 /* we need to allocate more space to store the new value */
581 if (delta > htx_free_space(htx))
582 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200583
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100584 /* Expand the block size. But data are not copied yet. Then defragment
585 * the HTX message.
586 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200587 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100588 htx->data += delta;
589 blk = htx_defrag(htx, blk);
590
591 copy:
592 /* Finaly, copy data. */
Willy Tarreaued00e342018-12-07 08:47:45 +0100593 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100594 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100595 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200596}
597
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100598/* Replaces the parts of the start-line. It returns the new start-line on
599 * success, otherwise it returns NULL. It is the caller responsibility to update
600 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200601 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100602struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
603 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100605 struct htx_sl *sl;
606 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100607 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608 uint32_t size;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100609 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200610
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100611 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100612 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100613 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100615 /* Save start-line info and flags */
616 sl = htx_get_blk_ptr(htx, blk);
617 tmp.info = sl->info;
618 tmp.flags = sl->flags;
Christopher Faulet54483df2018-11-26 15:05:52 +0100619 if (htx->sl_off == blk->addr)
620 htx->sl_off = -1;
621
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100622
623 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100624 delta = size - htx_get_blksz(blk);
625
626 /* easy case, new data are smaller, so replace it in-place */
627 if (delta <= 0) {
628 htx_set_blk_value_len(blk, size);
629 htx->data += delta;
630 goto copy;
631 }
632
633 /* we need to allocate more space to store the new value */
634 if (delta > htx_free_space(htx))
635 return NULL; /* not enough space */
636
637 /* Expand the block size. But data are not copied yet. Then defragment
638 * the HTX message.
639 */
640 htx_set_blk_value_len(blk, size);
641 htx->data += delta;
642 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200643
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100644 copy:
645 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100646 sl = htx_get_blk_ptr(htx, blk);
647 sl->info = tmp.info;
648 sl->flags = tmp.flags;
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 HTX_SL_P1_LEN(sl) = p1.len;
653 HTX_SL_P2_LEN(sl) = p2.len;
654 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100655
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100656 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
657 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
658 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200659
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100660 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200661}
662
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100663/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
664 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100666struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
667 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100669 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100670 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671 uint32_t size;
672
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100673 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
674 return NULL;
675
676 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200677
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100678 /* FIXME: check size (< 256MB) */
679 blk = htx_add_blk(htx, type, size);
680 if (!blk)
681 return NULL;
682 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200683
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100684 sl = htx_get_blk_ptr(htx, blk);
685 if (htx->sl_off == -1)
686 htx->sl_off = blk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200687
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100688 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200689
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100690 HTX_SL_P1_LEN(sl) = p1.len;
691 HTX_SL_P2_LEN(sl) = p2.len;
692 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200693
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100694 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
695 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
696 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
697
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100698 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200699}
700
701/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100702 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703 */
704struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100705 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200706{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100707 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200708
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100709 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
710 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
711 if (!blk)
712 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200713
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100714 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100715 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100716 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
717 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200718}
719
Willy Tarreau52610e92019-01-03 18:26:17 +0100720/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
721 * new block on success. Otherwise, it returns NULL. The caller is responsible
722 * for filling the block itself.
723 */
724struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
725{
726 struct htx_blk *blk;
727
728 blk = htx_add_blk(htx, type, blksz);
729 if (!blk)
730 return NULL;
731
732 blk->info += blksz;
733 return blk;
734}
735
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
737{
738 int i;
739
740 for (i = 0; hdrs[i].n.len; i++) {
741 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
742 return NULL;
743 }
744 return htx_add_endof(htx, HTX_BLK_EOH);
745}
746/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
747 * success. Otherwise, it returns NULL.
748 */
749struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100750 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200751{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100752 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100754 /* FIXME: check value.len ( < 1MB) */
755 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
756 if (!blk)
757 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200758
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100759 blk->info += (value.len << 8) + phdr;
760 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
761 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762}
763
764/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
765 * on success. Otherwise, it returns NULL.
766 */
767struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
768{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100769 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200770
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100771 blk = htx_add_blk(htx, type, 1);
772 if (!blk)
773 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200774
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100775 blk->info += 1;
776 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200777}
778
779
780/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
781 * possible. It returns the new block on success. Otherwise, it returns NULL.
782 */
783struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
784{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100785 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200786}
787
788/* Adds an HTX block of type TLR in <htx>. It first tries to append trailers
789 * data if possible. It returns the new block on success. Otherwise, it returns
790 * NULL.
791 */
792struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
793{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100794 return htx_append_blk_value(htx, HTX_BLK_TLR, tlr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795}
796
797/* Adds an HTX block of type OOB in <htx>. It returns the new block on
798 * success. Otherwise, it returns NULL.
799 */
800struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob)
801{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100802 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100804 /* FIXME: check oob.len (< 256MB) */
805 blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len);
806 if (!blk)
807 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200808
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100809 blk->info += oob.len;
810 memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len);
811 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200812}
813
Christopher Faulet24ed8352018-11-22 11:20:43 +0100814struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
815 const struct ist data)
816{
817 struct htx_blk *blk;
818 int32_t prev;
819
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100820 /* FIXME: check data.len (< 256MB) */
821 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
822 if (!blk)
823 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100824
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100825 blk->info += data.len;
826 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100827
828 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
829 struct htx_blk *pblk = htx_get_blk(htx, prev);
830
831 /* Swap .addr and .info fields */
832 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
833 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
834
835 if (blk->addr == pblk->addr)
836 blk->addr += htx_get_blksz(pblk);
837 htx->front = prev;
838
839 if (pblk == ref)
840 break;
841 blk = pblk;
842 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200843
844 if (htx_get_blk_pos(htx, blk) != htx->front)
845 htx_defrag(htx, NULL);
846
Christopher Faulet24ed8352018-11-22 11:20:43 +0100847 return blk;
848}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200849
Christopher Fauletc59ff232018-12-03 13:58:44 +0100850/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200851 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
852 * returns 0.
853 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100854int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200855{
Christopher Faulet570d1612018-11-26 11:13:57 +0100856 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200857 return 0;
858
Christopher Faulet570d1612018-11-26 11:13:57 +0100859 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200860 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100861 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200862 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100863 if (sl->flags & HTX_SL_F_VER_11)
864 chunk_memcat(chk, "HTTP/1.1", 8);
865 else
866 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
867
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200868 chunk_memcat(chk, "\r\n", 2);
869
870 return 1;
871}
872
Christopher Fauletc59ff232018-12-03 13:58:44 +0100873/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200874 * <chk>. It returns 1 if data are successfully appended, otherwise it
875 * returns 0.
876 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100877int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200878{
Christopher Faulet570d1612018-11-26 11:13:57 +0100879 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200880 return 0;
881
Christopher Faulet1e7af462018-12-03 14:05:01 +0100882 if (sl->flags & HTX_SL_F_VER_11)
883 chunk_memcat(chk, "HTTP/1.1", 8);
884 else
885 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200886 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100887 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200888 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100889 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200890 chunk_memcat(chk, "\r\n", 2);
891
892 return 1;
893}
894
Christopher Fauletc59ff232018-12-03 13:58:44 +0100895/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200896 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
897 * 0.
898 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100899int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200900{
901 if (n.len + v.len + 4 > b_room(chk))
902 return 0;
903
904 chunk_memcat(chk, n.ptr, n.len);
905 chunk_memcat(chk, ": ", 2);
906 chunk_memcat(chk, v.ptr, v.len);
907 chunk_memcat(chk, "\r\n", 2);
908
909 return 1;
910}
911
Christopher Fauletc59ff232018-12-03 13:58:44 +0100912/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200913 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
914 * returns 1 if data are successfully appended, otherwise it returns 0.
915 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100916int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200917{
918 if (chunked) {
919 uint32_t chksz;
920 char tmp[10];
921 char *beg, *end;
922
923 chksz = data.len;
924
925 beg = end = tmp+10;
926 *--beg = '\n';
927 *--beg = '\r';
928 do {
929 *--beg = hextab[chksz & 0xF];
930 } while (chksz >>= 4);
931
932 if (data.len + (end - beg) + 2 > b_room(chk))
933 return 0;
934 chunk_memcat(chk, beg, end - beg);
935 chunk_memcat(chk, data.ptr, data.len);
936 chunk_memcat(chk, "\r\n", 2);
937 }
938 else {
939 if (!chunk_memcat(chk, data.ptr, data.len))
940 return 0;
941 }
942
943 return 1;
944}
945
Christopher Fauletc59ff232018-12-03 13:58:44 +0100946/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200947 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
948 * 0.
949 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100950int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200951{
952 /* FIXME: be sure the CRLF is here or remove it when inserted */
953 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
954 return 0;
955 return 1;
956}