blob: bdde868af374d86ad031d777f8d33b257242471a [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 Faulet9c66b982019-04-30 18:08:26 +020031 int32_t sl_pos = -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);
Christopher Faulet28f29c72019-04-30 17:55:45 +020045 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010046 continue;
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 Faulet9c66b982019-04-30 18:08:26 +020053 /* update the start-line position */
54 if (htx->sl_pos == old)
55 sl_pos = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010056
Christopher Faulet200f8952019-01-02 11:23:44 +010057 /* if <blk> is defined, set its new position */
58 if (blk != NULL && blk == oldblk)
59 blkpos = new;
60
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010061 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
62 new++;
63 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020065 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher Faulet28f29c72019-04-30 17:55:45 +020067 htx->used = new;
Christopher Faulet9c66b982019-04-30 18:08:26 +020068 htx->sl_pos = sl_pos;
Christopher Faulet28f29c72019-04-30 17:55:45 +020069 htx->head = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010070 htx->front = htx->tail = new - 1;
71 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020072
Christopher Faulet200f8952019-01-02 11:23:44 +010073 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020074}
75
76/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
77 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
78 * block is returned and the HTTP message is updated. Space for this new block
79 * is reserved in the HTTP message. But it is the caller responsibility to set
80 * right info in the block to reflect the stored data.
81 */
82static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
83{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010084 struct htx_blk *blk, *prevblk, *headblk, *frtblk;
85 uint32_t used;
86 uint32_t tail;
87 uint32_t prev;
88 uint32_t wrap;
89 uint32_t head;
90 int32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020091
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010092 if (blksz > htx_free_data_space(htx))
93 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +020094
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010095 if (!htx->used) {
96 /* Empty message */
Christopher Faulet28f29c72019-04-30 17:55:45 +020097 htx->front = htx->head = htx->tail = 0;
98 htx->used = 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010099 blk = htx_get_blk(htx, htx->tail);
100 blk->addr = 0;
101 htx->data = blksz;
102 return blk;
103 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200104
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100105 used = htx->used + 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100106 prev = htx->tail;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200107 head = htx->head;
108 tail = htx->tail + 1;
109 wrap = htx_get_wrap(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200110
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100111 if (tail == wrap) {
112 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200113
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100114 /* Blocks don't wrap for now. We either need to push the new one
115 * on top of others or to defragement the table. */
116 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap+1) >= frtblk->addr + htx_get_blksz(frtblk))
117 wrap++;
118 else if (tail >= used) /* There is hole at the beginning */
119 tail = 0;
120 else {
121 /* No more room, tail hits data. We have to realign the
122 * whole message. */
123 goto defrag;
124 }
125 }
126 else if (used >= wrap) {
127 /* We have hit the tail, we need to reorganize the blocks. */
128 goto defrag;
129 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200130
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100131 /* Now we have updated tail, used and wrap, we know that there is some
132 * available room at least from the protocol's perspective. This space
133 * is split in two areas :
134 *
135 * 1: the space between the beginning of the blocks table and the
136 * front data's address. This space will only be used if data don't
137 * wrap yet.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200138
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100139 * 2: If the previous tail was the front block, the space between the
140 * beginning of the message and the head data's address.
141 * Otherwise, the space between the tail data's address and the
142 * tail's one.
143 */
144 prevblk = htx_get_blk(htx, prev);
145 headblk = htx_get_blk(htx, head);
146 if (prevblk->addr >= headblk->addr) {
147 /* the area was contiguous */
148 frtblk = htx_get_blk(htx, htx->front);
149 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap) - (frtblk->addr + htx_get_blksz(frtblk));
150 headroom = headblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200151
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 if (tailroom >= (int32_t)blksz) {
153 /* install upfront and update ->front */
154 blk = htx_get_blk(htx, tail);
155 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
156 htx->front = tail;
157 }
158 else if (headroom >= (int32_t)blksz) {
159 blk = htx_get_blk(htx, tail);
160 blk->addr = 0;
161 }
162 else {
163 /* need to defragment the table before inserting upfront */
164 goto defrag;
165 }
166 }
167 else {
168 /* it's already wrapped so we can't store anything in the tailroom */
169 headroom = headblk->addr - (prevblk->addr + htx_get_blksz(prevblk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200170
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100171 if (headroom >= (int32_t)blksz) {
172 blk = htx_get_blk(htx, tail);
173 blk->addr = prevblk->addr + htx_get_blksz(prevblk);
174 }
175 else {
176 defrag:
177 /* need to defragment the table before inserting upfront */
178 htx_defrag(htx, NULL);
179 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100180 tail = htx->tail + 1;
181 used = htx->used + 1;
182 blk = htx_get_blk(htx, tail);
183 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
184 htx->front = tail;
185 }
186 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200187
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100188 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 Faulet28f29c72019-04-30 17:55:45 +0200216 uint32_t next, pos, wrap;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200217
Christopher Faulet9c66b982019-04-30 18:08:26 +0200218 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100219 if (type != HTX_BLK_UNUSED) {
220 /* Mark the block as unused, decrement allocated size */
221 htx->data -= htx_get_blksz(blk);
222 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200223 if (htx->sl_pos == pos)
224 htx->sl_pos = -1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100225 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200226
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100227 /* This is the last block in use */
228 if (htx->used == 1/* || !htx->data */) {
Christopher Faulet28f29c72019-04-30 17:55:45 +0200229 htx->front = htx->head = htx->tail = 0;
230 htx->used = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100231 htx->data = 0;
232 return NULL;
233 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200234
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100235 /* There is at least 2 blocks, so tail is always >= 0 */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100236 blk = NULL;
237 next = pos + 1; /* By default retrun the next block */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200238 wrap = htx_get_wrap(htx);
239 if (htx->tail + 1 == wrap) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100240 /* The HTTP message doesn't wrap */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200241 if (pos == htx->head) {
242 /* move the head forward */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100243 htx->used--;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200244 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100245 }
246 else if (pos == htx->tail) {
247 /* remove the tail. this was the last inserted block so
248 * return NULL. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100249 htx->tail--;
250 htx->used--;
251 goto end;
252 }
253 }
254 else {
255 /* The HTTP message wraps */
256 if (pos == htx->tail) {
257 /* remove the tail. try to unwrap the message (pos == 0)
258 * and return NULL. */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200259 htx->tail = ((pos == 0) ? wrap-1 : htx->tail-1);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100260 htx->used--;
261 goto end;
262 }
Christopher Faulet28f29c72019-04-30 17:55:45 +0200263 else if (pos == htx->head) {
264 /* move the head forward and try to unwrap the message
265 * (head+1 == wrap) */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100266 htx->used--;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200267 htx->head++;
268 if (htx->head == wrap)
269 htx->head = next = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100270 }
271 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200272
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100273 blk = htx_get_blk(htx, next);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200274 if (htx->sl_pos == -1) {
275 /* Try to update the start-line payload addr, if possible */
Christopher Faulet54483df2018-11-26 15:05:52 +0100276 type = htx_get_blk_type(blk);
277 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
Christopher Faulet9c66b982019-04-30 18:08:26 +0200278 htx->sl_pos = htx_get_blk_pos(htx, blk);
Christopher Faulet54483df2018-11-26 15:05:52 +0100279 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200280 end:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100281 if (pos == htx->front)
282 htx->front = htx_find_front(htx);
283 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200284}
285
Christopher Faulet00cf6972019-01-07 14:53:27 +0100286/* Truncate all blocks after the one containing the offset <offset>. This last
287 * one is truncated too.
288 */
289void htx_truncate(struct htx *htx, uint32_t offset)
290{
291 struct htx_blk *blk;
292 struct htx_ret htxret;
293
294 htxret = htx_find_blk(htx, offset);
295 blk = htxret.blk;
296 if (blk && htxret.ret) {
297 uint32_t sz = htx_get_blksz(blk);
298
299 htx_set_blk_value_len(blk, sz - htxret.ret);
300 blk = htx_get_next_blk(htx, blk);
301 }
302 while (blk)
303 blk = htx_remove_blk(htx, blk);
304}
305
Christopher Faulet549822f2019-02-25 10:23:19 +0100306/* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if
307 * necessary. Others blocks will be removed at once if <count> is large
308 * enough. The function returns an htx_ret with the first block remaing in the
309 * messsage and the amount of data drained. If everything is removed,
310 * htx_ret.blk is set to NULL.
311 */
312struct htx_ret htx_drain(struct htx *htx, uint32_t count)
313{
314 struct htx_blk *blk;
315 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
316
317 blk = htx_get_head_blk(htx);
318 while (count && blk) {
319 uint32_t sz = htx_get_blksz(blk);
320 enum htx_blk_type type = htx_get_blk_type(blk);
321
322 /* Ingore unused block */
323 if (type == HTX_BLK_UNUSED)
324 goto next;
325
326 if (sz > count) {
327 if (type == HTX_BLK_DATA) {
328 htx_cut_data_blk(htx, blk, count);
329 htxret.ret += count;
330 }
331 break;
332 }
333 count -= sz;
334 htxret.ret += sz;
335 next:
336 blk = htx_remove_blk(htx, blk);
337 }
338 htxret.blk = blk;
339
340 return htxret;
341}
342
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200343/* Tries to append data to the last inserted block, if the type matches and if
Christopher Faulet61775092019-05-07 21:42:27 +0200344 * there is enough non-wrapping space. Only DATA content can be appended. If the
345 * append fails, a new block is inserted. If an error occurred, NULL is
346 * returned. Otherwise, on success, the updated block (or the new one) is
347 * returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200348*/
349static struct htx_blk *htx_append_blk_value(struct htx *htx, enum htx_blk_type type,
350 const struct ist data)
351{
Christopher Fauletf1449b72019-04-10 14:54:46 +0200352 struct htx_blk *blk, *tailblk, *headblk, *frtblk;
353 struct ist v;
354 int32_t room;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200355
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100356 if (!htx->used)
357 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200358
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100359 /* Not enough space to store data */
360 if (data.len > htx_free_data_space(htx))
361 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200362
Christopher Faulet61775092019-05-07 21:42:27 +0200363 /* Append only DATA */
364 if (type != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100365 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200366
Christopher Fauletf1449b72019-04-10 14:54:46 +0200367 /* get the tail and head block */
368 tailblk = htx_get_tail_blk(htx);
369 headblk = htx_get_head_blk(htx);
370 if (tailblk == NULL || headblk == NULL)
371 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200372
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100373 /* Don't try to append data if the last inserted block is not of the
374 * same type */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200375 if (type != htx_get_blk_type(tailblk))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100376 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200377
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100378 /*
379 * Same type and enough space: append data
380 */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200381 frtblk = htx_get_blk(htx, htx->front);
382 if (tailblk->addr >= headblk->addr) {
383 if (htx->tail != htx->front)
384 goto add_new_block;
385 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 +0100386 }
Christopher Fauletf1449b72019-04-10 14:54:46 +0200387 else
388 room = headblk->addr - (tailblk->addr + htx_get_blksz(tailblk));
389
390 if (room < (int32_t)data.len)
391 tailblk = htx_defrag(htx, tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200392
393 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100394 /* get the value of the tail block */
395 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200396 v = htx_get_blk_value(htx, tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200397
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100398 /* Append data and update the block itself */
399 memcpy(v.ptr + v.len, data.ptr, data.len);
Christopher Fauletf1449b72019-04-10 14:54:46 +0200400 htx_set_blk_value_len(tailblk, v.len + data.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200401
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100402 /* Update HTTP message */
403 htx->data += data.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200404
Christopher Fauletf1449b72019-04-10 14:54:46 +0200405 return tailblk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200406
407 add_new_block:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100408 /* FIXME: check tlr.len (< 256MB) */
409 blk = htx_add_blk(htx, type, data.len);
410 if (!blk)
411 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200412
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100413 blk->info += data.len;
414 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
415 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200416}
417
418/* Replaces a value part of a block by a new one. The new part can be smaller or
419 * larger than the old one. This function works for any kind of block with
420 * attached data. It returns the new block on success, otherwise it returns
421 * NULL.
422 */
423struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100424 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200425{
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100426 struct buffer *tmp;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100427 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100428 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200429
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100430 n = htx_get_blk_name(htx, blk);
431 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200432
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100433 delta = new.len - old.len;
434
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100435 /* easy case, new data are smaller, so replace it in-place */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100436 if (delta <= 0) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100437 memcpy(old.ptr, new.ptr, new.len);
438 if (old.len != v.len)
439 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100440 htx_set_blk_value_len(blk, v.len + delta);
441 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100442 return blk;
443 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200444
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100445 /* we need to allocate more space to store the new header value */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100446 if (delta > htx_free_space(htx))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100447 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200448
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100449 /*
450 * Copy the new header in a temp buffer
451 */
452 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200453
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100454 /* 1. copy the header name */
455 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200456
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100457 /* 2. copy value before old part, if any */
458 if (old.ptr != v.ptr)
459 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200460
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100461 /* 3. copy new value */
462 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200463
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100464 /* 4. copy value after old part if any */
465 if (old.len != v.len)
466 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200467
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200468
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100469 /* Expand the block size. But data are not copied yet. Then defragment
470 * the HTX message.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100471 */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100472 htx_set_blk_value_len(blk, v.len + delta);
473 htx->data += delta;
474 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200475
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100476 /* Finaly, copy data. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100477 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200478
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100479 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200480}
481
482/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
483 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were
484 * moved. It returns the number of bytes of data moved and the last HTX block
485 * inserted in <dst>.
486 */
487struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
488 enum htx_blk_type mark)
489{
490 struct htx_blk *blk, *dstblk;
491 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100492 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200493
494 ret = 0;
495 blk = htx_get_blk(src, htx_get_head(src));
496 dstblk = NULL;
497 while (blk && ret <= count) {
498 type = htx_get_blk_type(blk);
499
500 /* Ingore unused block */
501 if (type == HTX_BLK_UNUSED)
502 goto next;
503
504 sz = htx_get_blksz(blk);
505 if (!sz) {
506 dstblk = htx_reserve_nxblk(dst, 0);
507 if (!dstblk)
508 break;
509 dstblk->info = blk->info;
510 goto next;
511 }
512
513 info = blk->info;
514 max = htx_free_data_space(dst);
Christopher Fauletcc506022019-05-07 10:52:25 +0200515 if (max > count - ret)
516 max = count - ret;
Willy Tarreau90caa072019-04-09 16:21:54 +0200517 if (sz > max) {
518 sz = max;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200519 info = (type << 28) + sz;
520 /* Headers and pseudo headers must be fully copied */
Christopher Fauletbc5770b2019-05-07 10:52:54 +0200521 if (type != HTX_BLK_DATA || !sz)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200522 break;
523 }
524
525 dstblk = htx_reserve_nxblk(dst, sz);
526 if (!dstblk)
527 break;
528 dstblk->info = info;
529 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
530
531 ret += sz;
532 if (blk->info != info) {
533 /* Partial move: don't remove <blk> from <src> but
534 * resize its content */
535 blk->addr += sz;
536 htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz);
537 src->data -= sz;
538 break;
539 }
540
Christopher Faulet9c66b982019-04-30 18:08:26 +0200541 if (dst->sl_pos == -1 && src->sl_pos == htx_get_blk_pos(src, blk))
542 dst->sl_pos = htx_get_blk_pos(dst, dstblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200543 next:
544 blk = htx_remove_blk(src, blk);
545 if (type == mark)
546 break;
547
548 }
549
550 return (struct htx_ret){.ret = ret, .blk = dstblk};
551}
552
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200553/* Replaces an header by a new one. The new header can be smaller or larger than
554 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100555 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556 */
557struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100558 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200559{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100560 enum htx_blk_type type;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100561 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200562
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100563 type = htx_get_blk_type(blk);
564 if (type != HTX_BLK_HDR)
565 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100567 delta = name.len + value.len - htx_get_blksz(blk);
568
569 /* easy case, new value is smaller, so replace it in-place */
570 if (delta <= 0) {
571 blk->info = (type << 28) + (value.len << 8) + name.len;
572 htx->data += delta;
573 goto copy;
574 }
575
576 /* we need to allocate more space to store the new value */
577 if (delta > htx_free_space(htx))
578 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200579
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100580 /* Expand the block size. But data are not copied yet. Then defragment
581 * the HTX message.
582 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200583 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100584 htx->data += delta;
585 blk = htx_defrag(htx, blk);
586
587 copy:
588 /* Finaly, copy data. */
Willy Tarreaued00e342018-12-07 08:47:45 +0100589 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100590 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100591 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200592}
593
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100594/* Replaces the parts of the start-line. It returns the new start-line on
595 * success, otherwise it returns NULL. It is the caller responsibility to update
596 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200597 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100598struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
599 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100601 struct htx_sl *sl;
602 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100603 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604 uint32_t size;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100605 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100607 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100608 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100609 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200610
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100611 /* Save start-line info and flags */
612 sl = htx_get_blk_ptr(htx, blk);
613 tmp.info = sl->info;
614 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100615
616 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100617 delta = size - htx_get_blksz(blk);
618
619 /* easy case, new data are smaller, so replace it in-place */
620 if (delta <= 0) {
621 htx_set_blk_value_len(blk, size);
622 htx->data += delta;
623 goto copy;
624 }
625
626 /* we need to allocate more space to store the new value */
627 if (delta > htx_free_space(htx))
628 return NULL; /* not enough space */
629
630 /* Expand the block size. But data are not copied yet. Then defragment
631 * the HTX message.
632 */
633 htx_set_blk_value_len(blk, size);
634 htx->data += delta;
635 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100637 copy:
638 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100639 sl = htx_get_blk_ptr(htx, blk);
640 sl->info = tmp.info;
641 sl->flags = tmp.flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200642
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100643 HTX_SL_P1_LEN(sl) = p1.len;
644 HTX_SL_P2_LEN(sl) = p2.len;
645 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100646
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100647 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
648 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
649 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200650
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100651 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200652}
653
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100654/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
655 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200656 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100657struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
658 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200659{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100660 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100661 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200662 uint32_t size;
663
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100664 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
665 return NULL;
666
667 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100669 /* FIXME: check size (< 256MB) */
670 blk = htx_add_blk(htx, type, size);
671 if (!blk)
672 return NULL;
673 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200674
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100675 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200676 if (htx->sl_pos == -1)
677 htx->sl_pos = htx_get_blk_pos(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200678
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100679 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200680
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100681 HTX_SL_P1_LEN(sl) = p1.len;
682 HTX_SL_P2_LEN(sl) = p2.len;
683 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200684
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100685 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
686 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
687 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
688
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100689 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200690}
691
692/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100693 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694 */
695struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100696 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200697{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100698 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200699
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100700 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
701 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
702 if (!blk)
703 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200704
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100705 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100706 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100707 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
708 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709}
710
Willy Tarreau52610e92019-01-03 18:26:17 +0100711/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
712 * new block on success. Otherwise, it returns NULL. The caller is responsible
713 * for filling the block itself.
714 */
715struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
716{
717 struct htx_blk *blk;
718
719 blk = htx_add_blk(htx, type, blksz);
720 if (!blk)
721 return NULL;
722
723 blk->info += blksz;
724 return blk;
725}
726
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200727struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
728{
729 int i;
730
731 for (i = 0; hdrs[i].n.len; i++) {
732 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
733 return NULL;
734 }
735 return htx_add_endof(htx, HTX_BLK_EOH);
736}
737/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
738 * success. Otherwise, it returns NULL.
739 */
740struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100741 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200742{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100743 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200744
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100745 /* FIXME: check value.len ( < 1MB) */
746 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
747 if (!blk)
748 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200749
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100750 blk->info += (value.len << 8) + phdr;
751 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
752 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753}
754
755/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
756 * on success. Otherwise, it returns NULL.
757 */
758struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
759{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100760 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200761
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100762 blk = htx_add_blk(htx, type, 1);
763 if (!blk)
764 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200765
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100766 blk->info += 1;
767 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200768}
769
770
771/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
772 * possible. It returns the new block on success. Otherwise, it returns NULL.
773 */
774struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
775{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100776 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200777}
778
Christopher Faulet61775092019-05-07 21:42:27 +0200779/* Adds an HTX block of type TLR in <htx>. It returns the new block on
780 * success. Otherwise, it returns NULL.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200781 */
782struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
783{
Christopher Faulet61775092019-05-07 21:42:27 +0200784 struct htx_blk *blk;
785
786 /* FIXME: check tlr.len (< 256MB) */
787 blk = htx_add_blk(htx, HTX_BLK_TLR, tlr.len);
788 if (!blk)
789 return NULL;
790
791 blk->info += tlr.len;
792 memcpy(htx_get_blk_ptr(htx, blk), tlr.ptr, tlr.len);
793 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200794}
795
Christopher Faulet24ed8352018-11-22 11:20:43 +0100796struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
797 const struct ist data)
798{
799 struct htx_blk *blk;
800 int32_t prev;
801
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100802 /* FIXME: check data.len (< 256MB) */
803 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
804 if (!blk)
805 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100806
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100807 blk->info += data.len;
808 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100809
810 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
811 struct htx_blk *pblk = htx_get_blk(htx, prev);
812
813 /* Swap .addr and .info fields */
814 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
815 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
816
817 if (blk->addr == pblk->addr)
818 blk->addr += htx_get_blksz(pblk);
819 htx->front = prev;
820
821 if (pblk == ref)
822 break;
823 blk = pblk;
824 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200825
826 if (htx_get_blk_pos(htx, blk) != htx->front)
827 htx_defrag(htx, NULL);
828
Christopher Faulet24ed8352018-11-22 11:20:43 +0100829 return blk;
830}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831
Christopher Fauletc59ff232018-12-03 13:58:44 +0100832/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
834 * returns 0.
835 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100836int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837{
Christopher Faulet570d1612018-11-26 11:13:57 +0100838 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200839 return 0;
840
Christopher Faulet570d1612018-11-26 11:13:57 +0100841 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200842 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100843 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200844 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100845 if (sl->flags & HTX_SL_F_VER_11)
846 chunk_memcat(chk, "HTTP/1.1", 8);
847 else
848 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
849
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 status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200856 * <chk>. It returns 1 if data are successfully appended, otherwise it
857 * returns 0.
858 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100859int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200860{
Christopher Faulet570d1612018-11-26 11:13:57 +0100861 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200862 return 0;
863
Christopher Faulet1e7af462018-12-03 14:05:01 +0100864 if (sl->flags & HTX_SL_F_VER_11)
865 chunk_memcat(chk, "HTTP/1.1", 8);
866 else
867 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200868 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100869 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200870 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100871 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200872 chunk_memcat(chk, "\r\n", 2);
873
874 return 1;
875}
876
Christopher Fauletc59ff232018-12-03 13:58:44 +0100877/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200878 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
879 * 0.
880 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100881int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200882{
883 if (n.len + v.len + 4 > b_room(chk))
884 return 0;
885
886 chunk_memcat(chk, n.ptr, n.len);
887 chunk_memcat(chk, ": ", 2);
888 chunk_memcat(chk, v.ptr, v.len);
889 chunk_memcat(chk, "\r\n", 2);
890
891 return 1;
892}
893
Christopher Fauletc59ff232018-12-03 13:58:44 +0100894/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200895 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
896 * returns 1 if data are successfully appended, otherwise it returns 0.
897 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100898int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200899{
900 if (chunked) {
901 uint32_t chksz;
902 char tmp[10];
903 char *beg, *end;
904
905 chksz = data.len;
906
907 beg = end = tmp+10;
908 *--beg = '\n';
909 *--beg = '\r';
910 do {
911 *--beg = hextab[chksz & 0xF];
912 } while (chksz >>= 4);
913
914 if (data.len + (end - beg) + 2 > b_room(chk))
915 return 0;
916 chunk_memcat(chk, beg, end - beg);
917 chunk_memcat(chk, data.ptr, data.len);
918 chunk_memcat(chk, "\r\n", 2);
919 }
920 else {
921 if (!chunk_memcat(chk, data.ptr, data.len))
922 return 0;
923 }
924
925 return 1;
926}
927
Christopher Fauletc59ff232018-12-03 13:58:44 +0100928/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200929 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
930 * 0.
931 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100932int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200933{
934 /* FIXME: be sure the CRLF is here or remove it when inserted */
935 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
936 return 0;
937 return 1;
938}