blob: 3a2252febf05fb37f077a0835310b29931a57c17 [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
Christopher Faulet156852b2019-05-16 11:29:13 +0200483 * type <mark> (typically EOH, EOD or EOM) or when <count> bytes were moved
484 * (including payload and meta-data). It returns the number of bytes moved and
485 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200486 */
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
Christopher Faulet156852b2019-05-16 11:29:13 +0200494 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200495 blk = htx_get_blk(src, htx_get_head(src));
496 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200497
498 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200499 type = htx_get_blk_type(blk);
500
501 /* Ingore unused block */
502 if (type == HTX_BLK_UNUSED)
503 goto next;
504
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200505
Christopher Faulet156852b2019-05-16 11:29:13 +0200506 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200507 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200508 max = htx_get_max_blksz(dst, count);
509 if (!max)
510 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200511 if (sz > max) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512 /* Headers and pseudo headers must be fully copied */
Christopher Faulet156852b2019-05-16 11:29:13 +0200513 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200514 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200515 sz = max;
516 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200517 }
518
519 dstblk = htx_reserve_nxblk(dst, sz);
520 if (!dstblk)
521 break;
522 dstblk->info = info;
523 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
524
Christopher Faulet156852b2019-05-16 11:29:13 +0200525 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526 if (blk->info != info) {
527 /* Partial move: don't remove <blk> from <src> but
528 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200529 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200530 break;
531 }
532
Christopher Faulet9c66b982019-04-30 18:08:26 +0200533 if (dst->sl_pos == -1 && src->sl_pos == htx_get_blk_pos(src, blk))
534 dst->sl_pos = htx_get_blk_pos(dst, dstblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200535 next:
536 blk = htx_remove_blk(src, blk);
537 if (type == mark)
538 break;
539
540 }
541
Christopher Faulet156852b2019-05-16 11:29:13 +0200542 end:
543 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200544 return (struct htx_ret){.ret = ret, .blk = dstblk};
545}
546
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200547/* Replaces an header by a new one. The new header can be smaller or larger than
548 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100549 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200550 */
551struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100552 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200553{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100554 enum htx_blk_type type;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100555 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100557 type = htx_get_blk_type(blk);
558 if (type != HTX_BLK_HDR)
559 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200560
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100561 delta = name.len + value.len - htx_get_blksz(blk);
562
563 /* easy case, new value is smaller, so replace it in-place */
564 if (delta <= 0) {
565 blk->info = (type << 28) + (value.len << 8) + name.len;
566 htx->data += delta;
567 goto copy;
568 }
569
570 /* we need to allocate more space to store the new value */
571 if (delta > htx_free_space(htx))
572 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200573
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100574 /* Expand the block size. But data are not copied yet. Then defragment
575 * the HTX message.
576 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200577 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100578 htx->data += delta;
579 blk = htx_defrag(htx, blk);
580
581 copy:
582 /* Finaly, copy data. */
Willy Tarreaued00e342018-12-07 08:47:45 +0100583 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100584 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100585 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586}
587
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100588/* Replaces the parts of the start-line. It returns the new start-line on
589 * success, otherwise it returns NULL. It is the caller responsibility to update
590 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200591 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100592struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
593 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200594{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100595 struct htx_sl *sl;
596 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100597 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200598 uint32_t size;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100599 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100602 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100603 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100605 /* Save start-line info and flags */
606 sl = htx_get_blk_ptr(htx, blk);
607 tmp.info = sl->info;
608 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100609
610 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100611 delta = size - htx_get_blksz(blk);
612
613 /* easy case, new data are smaller, so replace it in-place */
614 if (delta <= 0) {
615 htx_set_blk_value_len(blk, size);
616 htx->data += delta;
617 goto copy;
618 }
619
620 /* we need to allocate more space to store the new value */
621 if (delta > htx_free_space(htx))
622 return NULL; /* not enough space */
623
624 /* Expand the block size. But data are not copied yet. Then defragment
625 * the HTX message.
626 */
627 htx_set_blk_value_len(blk, size);
628 htx->data += delta;
629 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200630
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100631 copy:
632 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100633 sl = htx_get_blk_ptr(htx, blk);
634 sl->info = tmp.info;
635 sl->flags = tmp.flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100637 HTX_SL_P1_LEN(sl) = p1.len;
638 HTX_SL_P2_LEN(sl) = p2.len;
639 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100640
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100641 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
642 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
643 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200644
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100645 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200646}
647
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100648/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
649 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200650 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100651struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
652 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200653{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100654 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100655 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200656 uint32_t size;
657
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100658 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
659 return NULL;
660
661 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200662
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100663 /* FIXME: check size (< 256MB) */
664 blk = htx_add_blk(htx, type, size);
665 if (!blk)
666 return NULL;
667 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100669 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200670 if (htx->sl_pos == -1)
671 htx->sl_pos = htx_get_blk_pos(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200672
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100673 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200674
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100675 HTX_SL_P1_LEN(sl) = p1.len;
676 HTX_SL_P2_LEN(sl) = p2.len;
677 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200678
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100679 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
680 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
681 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
682
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100683 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200684}
685
686/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100687 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200688 */
689struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100690 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200691{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100692 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200693
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100694 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
695 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
696 if (!blk)
697 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100699 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100700 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100701 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
702 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703}
704
Willy Tarreau52610e92019-01-03 18:26:17 +0100705/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
706 * new block on success. Otherwise, it returns NULL. The caller is responsible
707 * for filling the block itself.
708 */
709struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
710{
711 struct htx_blk *blk;
712
713 blk = htx_add_blk(htx, type, blksz);
714 if (!blk)
715 return NULL;
716
717 blk->info += blksz;
718 return blk;
719}
720
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200721struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
722{
723 int i;
724
725 for (i = 0; hdrs[i].n.len; i++) {
726 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
727 return NULL;
728 }
729 return htx_add_endof(htx, HTX_BLK_EOH);
730}
731/* Adds an HTX block of type PHDR in <htx>. It returns the new block on
732 * success. Otherwise, it returns NULL.
733 */
734struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100735 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100737 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200738
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100739 /* FIXME: check value.len ( < 1MB) */
740 blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len);
741 if (!blk)
742 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200743
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100744 blk->info += (value.len << 8) + phdr;
745 memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len);
746 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747}
748
749/* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block
750 * on success. Otherwise, it returns NULL.
751 */
752struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
753{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100754 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200755
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100756 blk = htx_add_blk(htx, type, 1);
757 if (!blk)
758 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200759
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100760 blk->info += 1;
761 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762}
763
764
765/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
766 * possible. It returns the new block on success. Otherwise, it returns NULL.
767 */
768struct htx_blk *htx_add_data(struct htx *htx, const struct ist data)
769{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100770 return htx_append_blk_value(htx, HTX_BLK_DATA, data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200771}
772
Christopher Faulet61775092019-05-07 21:42:27 +0200773/* Adds an HTX block of type TLR in <htx>. It returns the new block on
774 * success. Otherwise, it returns NULL.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200775 */
776struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr)
777{
Christopher Faulet61775092019-05-07 21:42:27 +0200778 struct htx_blk *blk;
779
780 /* FIXME: check tlr.len (< 256MB) */
781 blk = htx_add_blk(htx, HTX_BLK_TLR, tlr.len);
782 if (!blk)
783 return NULL;
784
785 blk->info += tlr.len;
786 memcpy(htx_get_blk_ptr(htx, blk), tlr.ptr, tlr.len);
787 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200788}
789
Christopher Faulet24ed8352018-11-22 11:20:43 +0100790struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
791 const struct ist data)
792{
793 struct htx_blk *blk;
794 int32_t prev;
795
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100796 /* FIXME: check data.len (< 256MB) */
797 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
798 if (!blk)
799 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100800
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100801 blk->info += data.len;
802 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100803
804 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
805 struct htx_blk *pblk = htx_get_blk(htx, prev);
806
807 /* Swap .addr and .info fields */
808 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
809 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
810
811 if (blk->addr == pblk->addr)
812 blk->addr += htx_get_blksz(pblk);
813 htx->front = prev;
814
815 if (pblk == ref)
816 break;
817 blk = pblk;
818 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200819
820 if (htx_get_blk_pos(htx, blk) != htx->front)
821 htx_defrag(htx, NULL);
822
Christopher Faulet24ed8352018-11-22 11:20:43 +0100823 return blk;
824}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200825
Christopher Fauletc59ff232018-12-03 13:58:44 +0100826/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200827 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
828 * returns 0.
829 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100830int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831{
Christopher Faulet570d1612018-11-26 11:13:57 +0100832 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833 return 0;
834
Christopher Faulet570d1612018-11-26 11:13:57 +0100835 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200836 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100837 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100839 if (sl->flags & HTX_SL_F_VER_11)
840 chunk_memcat(chk, "HTTP/1.1", 8);
841 else
842 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
843
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200844 chunk_memcat(chk, "\r\n", 2);
845
846 return 1;
847}
848
Christopher Fauletc59ff232018-12-03 13:58:44 +0100849/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200850 * <chk>. It returns 1 if data are successfully appended, otherwise it
851 * returns 0.
852 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100853int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200854{
Christopher Faulet570d1612018-11-26 11:13:57 +0100855 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200856 return 0;
857
Christopher Faulet1e7af462018-12-03 14:05:01 +0100858 if (sl->flags & HTX_SL_F_VER_11)
859 chunk_memcat(chk, "HTTP/1.1", 8);
860 else
861 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200862 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100863 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200864 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100865 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200866 chunk_memcat(chk, "\r\n", 2);
867
868 return 1;
869}
870
Christopher Fauletc59ff232018-12-03 13:58:44 +0100871/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200872 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
873 * 0.
874 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100875int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200876{
877 if (n.len + v.len + 4 > b_room(chk))
878 return 0;
879
880 chunk_memcat(chk, n.ptr, n.len);
881 chunk_memcat(chk, ": ", 2);
882 chunk_memcat(chk, v.ptr, v.len);
883 chunk_memcat(chk, "\r\n", 2);
884
885 return 1;
886}
887
Christopher Fauletc59ff232018-12-03 13:58:44 +0100888/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200889 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
890 * returns 1 if data are successfully appended, otherwise it returns 0.
891 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100892int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200893{
894 if (chunked) {
895 uint32_t chksz;
896 char tmp[10];
897 char *beg, *end;
898
899 chksz = data.len;
900
901 beg = end = tmp+10;
902 *--beg = '\n';
903 *--beg = '\r';
904 do {
905 *--beg = hextab[chksz & 0xF];
906 } while (chksz >>= 4);
907
908 if (data.len + (end - beg) + 2 > b_room(chk))
909 return 0;
910 chunk_memcat(chk, beg, end - beg);
911 chunk_memcat(chk, data.ptr, data.len);
912 chunk_memcat(chk, "\r\n", 2);
913 }
914 else {
915 if (!chunk_memcat(chk, data.ptr, data.len))
916 return 0;
917 }
918
919 return 1;
920}
921
Christopher Fauletc59ff232018-12-03 13:58:44 +0100922/* Appends the h1 representation of the trailer block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200923 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
924 * 0.
925 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100926int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200927{
928 /* FIXME: be sure the CRLF is here or remove it when inserted */
929 if (!chunk_memcat(chk, tlr.ptr, tlr.len))
930 return 0;
931 return 1;
932}