blob: 41eb47274ecb035528e3ad9340b894c6d1670d8c [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
Christopher Faulet192c6a22019-06-11 16:32:24 +020016struct htx htx_empty = { .size = 0, .data = 0, .head = -1, .tail = -1, .first = -1 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +020017
Christopher Faulet3b219722019-06-19 13:48:09 +020018/* Defragments an HTX message. It removes unused blocks and unwraps the payloads
19 * part. A temporary buffer is used to do so. This function never fails. if
20 * <blk> is not NULL, we replace it by the new block address, after the
21 * defragmentation. The new <blk> is returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +020022 */
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 Faulet29f17582019-05-23 11:03:26 +020031 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020032
Christopher Faulet192c6a22019-06-11 16:32:24 +020033 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010034 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 */
Christopher Faulet29f17582019-05-23 11:03:26 +020054 if (htx->first == old)
55 first = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010056
Christopher Faulet3b219722019-06-19 13:48:09 +020057 /* if <blk> is defined, save its new position */
Christopher Faulet200f8952019-01-02 11:23:44 +010058 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 Faulet29f17582019-05-23 11:03:26 +020067 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020068 htx->head = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +020069 htx->tail = new - 1;
70 htx->head_addr = htx->end_addr = 0;
71 htx->tail_addr = addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010072 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
Christopher Faulet3b219722019-06-19 13:48:09 +020077/* Degragments HTX blocks of an HTX message. Payloads part is keep untouched
78 * here. This function will move back all blocks starting at the position 0,
79 * removing unused blocks. It must never be called with an empty message.
80 */
Christopher Fauletd7884d32019-06-11 10:40:43 +020081static void htx_defrag_blks(struct htx *htx)
82{
83 int32_t pos, new;
84
85 new = 0;
86 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
87 struct htx_blk *posblk, *newblk;
88
89 if (pos == new) {
90 new++;
91 continue;
92 }
93
94 posblk = htx_get_blk(htx, pos);
95 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
96 continue;
97
98 if (htx->first == pos)
99 htx->first = new;
100 newblk = htx_get_blk(htx, new++);
101 newblk->info = posblk->info;
102 newblk->addr = posblk->addr;
103 }
104 BUG_ON(!new);
105 htx->head = 0;
106 htx->tail = new - 1;
107}
108
Christopher Faulet3b219722019-06-19 13:48:09 +0200109/* Reserves a new block in the HTX message <htx> with a content of <blksz>
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200110 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
Christopher Faulet3b219722019-06-19 13:48:09 +0200111 * block is returned and the HTX message is updated. Space for this new block is
112 * reserved in the HTX message. But it is the caller responsibility to set right
113 * info in the block to reflect the stored data.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200114 */
115static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
116{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200117 struct htx_blk *blk;
Christopher Faulet192c6a22019-06-11 16:32:24 +0200118 uint32_t tail, headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200119
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100120 if (blksz > htx_free_data_space(htx))
121 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200122
Christopher Faulet192c6a22019-06-11 16:32:24 +0200123 if (htx->head == -1) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100124 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200125 htx->head = htx->tail = htx->first = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100126 blk = htx_get_blk(htx, htx->tail);
127 blk->addr = 0;
128 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200129 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100130 return blk;
131 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200132
Christopher Fauletd7884d32019-06-11 10:40:43 +0200133 /* Find the block's position. First, we try to get the next position in
134 * the message, increasing the tail by one. If this position is not
135 * available with some holes, we try to defrag the blocks without
136 * touching their paylood. If it is impossible, we fully defrag the
137 * message.
138 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200139 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200140 if (htx_pos_to_addr(htx, tail) >= htx->tail_addr)
Christopher Faulet192c6a22019-06-11 16:32:24 +0200141 ;
142 else if (htx->head > 0) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200143 htx_defrag_blks(htx);
144 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200145 BUG_ON(htx_pos_to_addr(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100146 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200147 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100148 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200149
Christopher Fauletd7884d32019-06-11 10:40:43 +0200150 /* Now, we have found the block's position. Try do find where to put its
151 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 *
Christopher Fauletd7884d32019-06-11 10:40:43 +0200153 * * The free space in front of the blocks table. This one is used iff
154 * the other one was not used yet.
155 *
156 * * The free space at the beginning of the message. Once this one is
157 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100158 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200159 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200160 tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200161 BUG_ON((int32_t)headroom < 0);
162 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200163
Christopher Fauletd7884d32019-06-11 10:40:43 +0200164 if (blksz <= tailroom) {
165 blk = htx_get_blk(htx, tail);
166 blk->addr = htx->tail_addr;
167 htx->tail_addr += blksz;
168 }
169 else if (blksz <= headroom) {
170 blk = htx_get_blk(htx, tail);
171 blk->addr = htx->head_addr;
172 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100173 }
174 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200175 defrag:
Christopher Faulet3b219722019-06-19 13:48:09 +0200176 /* need to defragment the message before inserting upfront */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200177 htx_defrag(htx, NULL);
178 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200179 blk = htx_get_blk(htx, tail);
180 blk->addr = htx->tail_addr;
181 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100182 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200183
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100184 htx->tail = tail;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200186 /* Set first position if not already set */
187 if (htx->first == -1)
188 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200189
190 BUG_ON((int32_t)htx->tail_addr < 0);
191 BUG_ON((int32_t)htx->head_addr < 0);
192 BUG_ON(htx->end_addr > htx->tail_addr);
193 BUG_ON(htx->head_addr > htx->end_addr);
194
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100195 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200196}
197
Christopher Fauletd7884d32019-06-11 10:40:43 +0200198/* Prepares the block to an expansion of its payload. The payload will be
199 * expanded by <delta> bytes and we need find where this expansion will be
200 * performed. It can be a compression if <delta> is negative. This function only
201 * updates all addresses. The caller have the responsibility to performe the
Christopher Faulet3b219722019-06-19 13:48:09 +0200202 * expansion and update the block and the HTX message accordingly. No error must
203 * occurr. It returns following values:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200204 *
205 * 0: The expansion cannot be performed, there is not enough space.
206 *
Christopher Faulet3b219722019-06-19 13:48:09 +0200207 * 1: the expansion must be performed in place, there is enougth space after
Christopher Fauletd7884d32019-06-11 10:40:43 +0200208 * the block's payload to handle it. This is especially true if it is a
209 * compression and not an expension.
210 *
211 * 2: the block's payload must be moved at the new block address before doing
212 * the expansion.
213 *
214 * 3: the HTX message message must be defragmented
215 */
216static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
217{
218 uint32_t sz, tailroom, headroom;
219 int ret = 3;
220
Christopher Faulet192c6a22019-06-11 16:32:24 +0200221 BUG_ON(htx->head == -1);
222
Christopher Fauletd7884d32019-06-11 10:40:43 +0200223 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200224 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200225 BUG_ON((int32_t)headroom < 0);
226 BUG_ON((int32_t)tailroom < 0);
227
228 sz = htx_get_blksz(blk);
229 if (delta <= 0) {
230 /* It is a compression, it can be performed in place */
231 if (blk->addr+sz == htx->tail_addr)
232 htx->tail_addr += delta;
233 else if (blk->addr+sz == htx->head_addr)
234 htx->head_addr += delta;
235 ret = 1;
236 }
237 else if (delta > htx_free_space(htx)) {
238 /* There is not enought space to handle the expansion */
239 ret = 0;
240 }
241 else if (blk->addr+sz == htx->tail_addr) {
242 /* The block's payload is just before the tail room */
243 if (delta < tailroom) {
244 /* Expand the block's payload */
245 htx->tail_addr += delta;
246 ret = 1;
247 }
248 else if ((sz + delta) < headroom) {
Christopher Faulet61ed7792019-07-29 10:50:28 +0200249 uint32_t oldaddr = blk->addr;
250
Christopher Fauletd7884d32019-06-11 10:40:43 +0200251 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200252 blk->addr = htx->head_addr;
253 htx->tail_addr -= sz;
254 htx->head_addr += sz + delta;
Christopher Faulet61ed7792019-07-29 10:50:28 +0200255 if (oldaddr == htx->end_addr) {
Christopher Faulet8c654862019-06-12 11:08:11 +0200256 if (htx->end_addr == htx->tail_addr) {
257 htx->tail_addr = htx->head_addr;
258 htx->head_addr = htx->end_addr = 0;
259 }
260 else
261 htx->end_addr += sz;
262 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200263 ret = 2;
264 }
265 }
266 else if (blk->addr+sz == htx->head_addr) {
267 /* The block's payload is just before the head room */
268 if (delta < headroom) {
269 /* Expand the block's payload */
270 htx->head_addr += delta;
271 ret = 1;
272 }
273 }
274 else {
275 /* The block's payload is not at the rooms edge */
276 if (!htx->head_addr && sz+delta < tailroom) {
277 /* Move the block's payload into the tailroom */
278 if (blk->addr == htx->end_addr)
279 htx->end_addr += sz;
280 blk->addr = htx->tail_addr;
281 htx->tail_addr += sz + delta;
282 ret = 2;
283 }
284 else if (sz+delta < headroom) {
285 /* Move the block's payload into the headroom */
286 if (blk->addr == htx->end_addr)
287 htx->end_addr += sz;
288 blk->addr = htx->head_addr;
289 htx->head_addr += sz + delta;
290 ret = 2;
291 }
292 }
293 /* Otherwise defrag the HTX message */
294
295 BUG_ON((int32_t)htx->tail_addr < 0);
296 BUG_ON((int32_t)htx->head_addr < 0);
297 BUG_ON(htx->end_addr > htx->tail_addr);
298 BUG_ON(htx->head_addr > htx->end_addr);
299 return ret;
300}
301
Christopher Faulet3b219722019-06-19 13:48:09 +0200302/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
303 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200304 */
305struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
306{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100307 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200308
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100309 blk = htx_reserve_nxblk(htx, blksz);
310 if (!blk)
311 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200312 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200313
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100314 blk->info = (type << 28);
315 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200316}
317
Christopher Faulet3b219722019-06-19 13:48:09 +0200318/* Removes the block <blk> from the HTX message <htx>. The function returns the
319 * block following <blk> or NULL if <blk> is the last block or the last inserted
320 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200321 */
322struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
323{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200324 enum htx_blk_type type;
325 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200326
Christopher Faulet192c6a22019-06-11 16:32:24 +0200327 BUG_ON(htx->head == -1);
328
Christopher Fauletd7884d32019-06-11 10:40:43 +0200329 /* This is the last block in use */
Christopher Faulet192c6a22019-06-11 16:32:24 +0200330 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200331 htx_reset(htx);
332 return NULL;
333 }
334
335 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200336 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200337 sz = htx_get_blksz(blk);
338 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100339 if (type != HTX_BLK_UNUSED) {
340 /* Mark the block as unused, decrement allocated size */
341 htx->data -= htx_get_blksz(blk);
342 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100343 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200344
Christopher Faulet3b219722019-06-19 13:48:09 +0200345 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200346 if (pos == htx->head) {
347 /* move the head forward */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200348 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100349 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200350 else if (pos == htx->tail) {
351 /* remove the tail. this was the last inserted block so
352 * return NULL. */
353 htx->tail--;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200354 blk = NULL;
355 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100356 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200357 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200358
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200359 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200360 if (pos == htx->first)
361 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200362
Christopher Faulet192c6a22019-06-11 16:32:24 +0200363 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200364 /* If there is just one block in the HTX message, free space can
365 * be ajusted. This operation could save some defrags. */
366 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
367
368 htx->head_addr = 0;
369 htx->end_addr = lastblk->addr;
370 htx->tail_addr = lastblk->addr+htx->data;
371 }
372 else {
373 if (addr+sz == htx->tail_addr)
374 htx->tail_addr = addr;
375 else if (addr+sz == htx->head_addr)
376 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200377 if (addr == htx->end_addr) {
378 if (htx->tail_addr == htx->end_addr) {
379 htx->tail_addr = htx->head_addr;
380 htx->head_addr = htx->end_addr = 0;
381 }
382 else
383 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200384 }
385 }
386
387 BUG_ON((int32_t)htx->tail_addr < 0);
388 BUG_ON((int32_t)htx->head_addr < 0);
389 BUG_ON(htx->end_addr > htx->tail_addr);
390 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100391 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200392}
393
Christopher Faulet3b219722019-06-19 13:48:09 +0200394/* Removes all blocks after the one containing the offset <offset>. This last
395 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100396 */
397void htx_truncate(struct htx *htx, uint32_t offset)
398{
399 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100400
Christopher Fauletced39002019-05-23 11:12:43 +0200401 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100402 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200403 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100404
Christopher Fauletced39002019-05-23 11:12:43 +0200405 if (offset >= sz) {
406 offset -= sz;
407 continue;
408 }
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200409 if (type == HTX_BLK_DATA)
410 htx_change_blk_value_len(htx, blk, offset);
Christopher Fauletced39002019-05-23 11:12:43 +0200411 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100412 }
413 while (blk)
414 blk = htx_remove_blk(htx, blk);
415}
416
Christopher Faulet3b219722019-06-19 13:48:09 +0200417/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
418 * block, it will be cut if necessary. Others blocks will be removed at once if
419 * <count> is large enough. The function returns an htx_ret with the first block
420 * remaing in the messsage and the amount of data drained. If everything is
421 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100422 */
423struct htx_ret htx_drain(struct htx *htx, uint32_t count)
424{
425 struct htx_blk *blk;
426 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
427
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200428 if (count == htx->data) {
429 htx_reset(htx);
430 htxret.ret = count;
431 return htxret;
432 }
433
Christopher Faulet549822f2019-02-25 10:23:19 +0100434 blk = htx_get_head_blk(htx);
435 while (count && blk) {
436 uint32_t sz = htx_get_blksz(blk);
437 enum htx_blk_type type = htx_get_blk_type(blk);
438
439 /* Ingore unused block */
440 if (type == HTX_BLK_UNUSED)
441 goto next;
442
443 if (sz > count) {
444 if (type == HTX_BLK_DATA) {
445 htx_cut_data_blk(htx, blk, count);
446 htxret.ret += count;
447 }
448 break;
449 }
450 count -= sz;
451 htxret.ret += sz;
452 next:
453 blk = htx_remove_blk(htx, blk);
454 }
455 htxret.blk = blk;
456
457 return htxret;
458}
459
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200460/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200461 * there is enough space to take it all. If the space wraps, the buffer is
462 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200463 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200464 * returned. Due to its nature this function can be expensive and should be
465 * avoided whenever possible.
466 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200467struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200468{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200469 struct htx_blk *blk, *tailblk;
470 void *ptr;
471 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200472
Christopher Faulet192c6a22019-06-11 16:32:24 +0200473 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100474 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200475
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100476 /* Not enough space to store data */
477 if (data.len > htx_free_data_space(htx))
478 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200479
Christopher Fauletd7884d32019-06-11 10:40:43 +0200480 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200481 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200482 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200483 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200484 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200485
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100486 /* Don't try to append data if the last inserted block is not of the
487 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200488 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200490
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100491 /*
492 * Same type and enough space: append data
493 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200494 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200495 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200496 BUG_ON((int32_t)headroom < 0);
497 BUG_ON((int32_t)tailroom < 0);
498
499 len = data.len;
500 if (tailblk->addr+sz == htx->tail_addr) {
501 if (data.len <= tailroom)
502 goto append_data;
503 else if (!htx->head_addr) {
504 len = tailroom;
505 goto append_data;
506 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100507 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200508 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
509 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200510
Christopher Fauletd7884d32019-06-11 10:40:43 +0200511 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512
513 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100515 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200516 ptr = htx_get_blk_ptr(htx, tailblk);
517 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200518 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200519
Christopher Fauletd7884d32019-06-11 10:40:43 +0200520 if (data.len == len) {
521 blk = tailblk;
522 goto end;
523 }
524 data.ptr += len;
525 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526
527 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200528 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200529 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100530 if (!blk)
531 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200532
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100533 blk->info += data.len;
534 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200535
536 end:
537 BUG_ON((int32_t)htx->tail_addr < 0);
538 BUG_ON((int32_t)htx->head_addr < 0);
539 BUG_ON(htx->end_addr > htx->tail_addr);
540 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100541 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542}
543
544/* Replaces a value part of a block by a new one. The new part can be smaller or
545 * larger than the old one. This function works for any kind of block with
546 * attached data. It returns the new block on success, otherwise it returns
547 * NULL.
548 */
549struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100550 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200551{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100552 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100553 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200554 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200555
Christopher Fauletd7884d32019-06-11 10:40:43 +0200556 n = htx_get_blk_name(htx, blk);
557 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100558 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200559 ret = htx_prepare_blk_expansion(htx, blk, delta);
560 if (!ret)
561 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100562
Christopher Fauletd7884d32019-06-11 10:40:43 +0200563 /* Before updating the payload, set the new block size and update HTX
564 * message */
565 htx_set_blk_value_len(blk, v.len + delta);
566 htx->data += delta;
567
Christopher Faulet3b219722019-06-19 13:48:09 +0200568 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200569 if (delta <= 0) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200570 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200571 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100572 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200573 }
574 else {
Christopher Faulet3b219722019-06-19 13:48:09 +0200575 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200576 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
577 memcpy(old.ptr, new.ptr, new.len);
578 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100579 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200580 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200581 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200582
Christopher Fauletd7884d32019-06-11 10:40:43 +0200583 /* Copy the name, if any */
584 memcpy(ptr, n.ptr, n.len);
585 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 /* Copy value before old part, if any */
588 memcpy(ptr, v.ptr, old.ptr - v.ptr);
589 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200590
Christopher Fauletd7884d32019-06-11 10:40:43 +0200591 /* Copy new value */
592 memcpy(ptr, new.ptr, new.len);
593 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200594
Christopher Fauletd7884d32019-06-11 10:40:43 +0200595 /* Copy value after old part, if any */
596 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
597 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200598 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200599 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletd7884d32019-06-11 10:40:43 +0200601 /* Copy the header name, if any */
602 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200603
Christopher Fauletd7884d32019-06-11 10:40:43 +0200604 /* Copy value before old part, if any */
605 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 /* Copy new value */
608 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200609
Christopher Fauletd7884d32019-06-11 10:40:43 +0200610 /* Copy value after old part if any */
611 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614
Christopher Fauletd7884d32019-06-11 10:40:43 +0200615 /* Finaly, copy data. */
616 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
617 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100618 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200619}
620
621/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200622 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200623 * (including payload and meta-data). It returns the number of bytes moved and
624 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625 */
626struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
627 enum htx_blk_type mark)
628{
629 struct htx_blk *blk, *dstblk;
630 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100631 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200632 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200633
Christopher Faulet156852b2019-05-16 11:29:13 +0200634 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200635 blk = htx_get_blk(src, htx_get_head(src));
636 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200637
638 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639 type = htx_get_blk_type(blk);
640
641 /* Ingore unused block */
642 if (type == HTX_BLK_UNUSED)
643 goto next;
644
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200645 /* Be sure to have enough space to xfer all headers/trailers in
646 * one time. If not while <dst> is empty, we report a parsing
647 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200648 */
649 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
650 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
651
652 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
653 if (htx_is_empty(dst))
654 src->flags |= HTX_FL_PARSING_ERROR;
655 break;
656 }
657 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200658 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
659 !inside_trailers && mark >= HTX_BLK_EOT) {
660 inside_trailers = 1;
661 if (htx_used_space(src) > count) {
662 if (htx_is_empty(dst))
663 src->flags |= HTX_FL_PARSING_ERROR;
664 break;
665 }
666 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200667
Christopher Faulet156852b2019-05-16 11:29:13 +0200668 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200669 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200670 max = htx_get_max_blksz(dst, count);
671 if (!max)
672 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200673 if (sz > max) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200674 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200675 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200676 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200677 sz = max;
678 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200679 }
680
681 dstblk = htx_reserve_nxblk(dst, sz);
682 if (!dstblk)
683 break;
684 dstblk->info = info;
685 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
686
Christopher Faulet156852b2019-05-16 11:29:13 +0200687 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200688 if (blk->info != info) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200689 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200690 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200691 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692 break;
693 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694 next:
695 blk = htx_remove_blk(src, blk);
696 if (type == mark)
697 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 }
699
Christopher Faulet156852b2019-05-16 11:29:13 +0200700 end:
701 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200702 return (struct htx_ret){.ret = ret, .blk = dstblk};
703}
704
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200705/* Replaces an header by a new one. The new header can be smaller or larger than
706 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100707 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200708 */
709struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100710 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200711{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100712 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200713 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100714 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200715 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200716
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100717 type = htx_get_blk_type(blk);
718 if (type != HTX_BLK_HDR)
719 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100721 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200722 ret = htx_prepare_blk_expansion(htx, blk, delta);
723 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100724 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200725
Christopher Fauletd7884d32019-06-11 10:40:43 +0200726 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200727 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100728 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100729
Christopher Faulet3b219722019-06-19 13:48:09 +0200730 /* Replace in place or at a new address is the same. We replace all the
731 * header (name+value). Only take care to defrag the message if
732 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200733 if (ret == 3)
734 blk = htx_defrag(htx, blk);
735
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100736 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200737 ptr = htx_get_blk_ptr(htx, blk);
738 ist2bin_lc(ptr, name);
739 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100740 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200741}
742
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100743/* Replaces the parts of the start-line. It returns the new start-line on
744 * success, otherwise it returns NULL. It is the caller responsibility to update
745 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200746 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100747struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
748 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200749{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200750 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100751 struct htx_sl *sl;
752 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200753 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100754 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200755 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200756
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100757 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100758 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100759 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200760
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100761 /* Save start-line info and flags */
762 sl = htx_get_blk_ptr(htx, blk);
763 tmp.info = sl->info;
764 tmp.flags = sl->flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200765 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100766
Christopher Fauletd7884d32019-06-11 10:40:43 +0200767 sz = htx_get_blksz(blk);
768 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
769 ret = htx_prepare_blk_expansion(htx, blk, delta);
770 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100771 return NULL; /* not enough space */
772
Christopher Fauletd7884d32019-06-11 10:40:43 +0200773 /* Set the new block size and update HTX message */
774 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100775 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200776
Christopher Faulet3b219722019-06-19 13:48:09 +0200777 /* Replace in place or at a new address is the same. We replace all the
778 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200779 if (ret == 3)
780 blk = htx_defrag(htx, blk);
781
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100782 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100783 sl = htx_get_blk_ptr(htx, blk);
784 sl->info = tmp.info;
785 sl->flags = tmp.flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200786 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100788 HTX_SL_P1_LEN(sl) = p1.len;
789 HTX_SL_P2_LEN(sl) = p2.len;
790 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100791
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100792 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
793 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
794 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100796 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200797}
798
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100799/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
800 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200801 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100802struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
803 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200804{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100805 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100806 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807 uint32_t size;
808
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100809 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
810 return NULL;
811
812 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200813
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100814 /* FIXME: check size (< 256MB) */
815 blk = htx_add_blk(htx, type, size);
816 if (!blk)
817 return NULL;
818 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200819
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100820 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200821 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100822 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200823
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100824 HTX_SL_P1_LEN(sl) = p1.len;
825 HTX_SL_P2_LEN(sl) = p2.len;
826 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200827
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100828 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
829 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
830 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
831
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100832 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833}
834
835/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100836 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837 */
838struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100839 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200840{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100841 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200842
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100843 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
844 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
845 if (!blk)
846 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200847
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100848 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100849 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100850 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
851 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200852}
853
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200854/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet3b219722019-06-19 13:48:09 +0200855 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200856 */
857struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
858 const struct ist value)
859{
860 struct htx_blk *blk;
861
862 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
863 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
864 if (!blk)
865 return NULL;
866
867 blk->info += (value.len << 8) + name.len;
868 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
869 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
870 return blk;
871}
872
Christopher Faulet3b219722019-06-19 13:48:09 +0200873/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
874 * the EOH. On sucess, it returns the last block inserted (the EOH), otherwise
875 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200876struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
877{
878 int i;
879
880 for (i = 0; hdrs[i].n.len; i++) {
881 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
882 return NULL;
883 }
884 return htx_add_endof(htx, HTX_BLK_EOH);
885}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200886
Christopher Faulet3b219722019-06-19 13:48:09 +0200887/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
888 * the EOT. On sucess, it returns the last block inserted (the EOT), otherwise
889 * NULL is returned. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200890struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
891{
892 int i;
893
894 for (i = 0; hdrs[i].n.len; i++) {
895 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
896 return NULL;
897 }
898 return htx_add_endof(htx, HTX_BLK_EOT);
899}
900
Christopher Faulet3b219722019-06-19 13:48:09 +0200901/* Adds an HTX block of type EOH, EOT, or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200902 * on success. Otherwise, it returns NULL.
903 */
904struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
905{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100906 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200907
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100908 blk = htx_add_blk(htx, type, 1);
909 if (!blk)
910 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200911
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100912 blk->info += 1;
913 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200914}
915
916
917/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200918 * possible. It returns the number of bytes consumed from <data>, which may be
919 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200920 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200921size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200922{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200923 struct htx_blk *blk, *tailblk;
924 void *ptr;
925 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200926 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200927
Christopher Faulet192c6a22019-06-11 16:32:24 +0200928 if (htx->head == -1)
Willy Tarreau0350b902019-05-28 10:58:50 +0200929 goto add_new_block;
930
931 /* Not enough space to store data */
932 if (len > htx_free_data_space(htx))
933 len = htx_free_data_space(htx);
934
935 if (!len)
936 return 0;
937
938 /* get the tail and head block */
939 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200940 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200941 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200942 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200943
944 /* Don't try to append data if the last inserted block is not of the
945 * same type */
946 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
947 goto add_new_block;
948
949 /*
950 * Same type and enough space: append data
951 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200952 if (!htx->head_addr) {
953 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200954 goto add_new_block;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200955 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200956 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200957 else {
958 if (tailblk->addr+sz != htx->head_addr)
959 goto add_new_block;
960 room = (htx->end_addr - htx->head_addr);
961 }
962 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200963 if (room < len)
964 len = room;
965
966 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200967 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200968 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200969 ptr = htx_get_blk_ptr(htx, tailblk);
970 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200971 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200972
973 BUG_ON((int32_t)htx->tail_addr < 0);
974 BUG_ON((int32_t)htx->head_addr < 0);
975 BUG_ON(htx->end_addr > htx->tail_addr);
976 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200977 return len;
978
979 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200980 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +0200981 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
982 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200983 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +0200984
985 blk->info += len;
986 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
987 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200988}
989
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200990
991/* Adds an HTX block of type DATA in <htx> just after all other DATA
992 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
993 * DATA block if possible. But, if the function succeeds, it will be the last
994 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
995 * on success, the updated block (or the new one) is returned.
996 */
997struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +0100998{
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200999 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001000
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001001 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001002 if (!blk)
1003 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001004
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001005 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001006 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1007 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001008
Christopher Faulet24ed8352018-11-22 11:20:43 +01001009 /* Swap .addr and .info fields */
1010 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1011 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1012
1013 if (blk->addr == pblk->addr)
1014 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001015 blk = pblk;
1016 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001017
Christopher Faulet24ed8352018-11-22 11:20:43 +01001018 return blk;
1019}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001020
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001021/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1022 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1023 * blocks are updated to remain valid after the move. */
1024void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1025{
1026 struct htx_blk *cblk, *pblk;
1027
1028 cblk = *blk;
1029 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1030 /* Swap .addr and .info fields */
1031 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1032 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1033
1034 if (cblk->addr == pblk->addr)
1035 cblk->addr += htx_get_blksz(pblk);
1036 if (pblk == *ref)
1037 break;
1038 cblk = pblk;
1039 }
1040 *blk = cblk;
1041 *ref = pblk;
1042}
Christopher Faulet0ea0c862020-01-23 11:47:53 +01001043
1044/* Append the HTX message <src> to the HTX message <dst>. It returns 1 on
1045 * success and 0 on error. All the message or nothing is copied. If an error
1046 * occurred, all blocks from <src> already appended to <dst> are truncated.
1047 */
1048int htx_append_msg(struct htx *dst, const struct htx *src)
1049{
1050 struct htx_blk *blk, *newblk;
1051 enum htx_blk_type type;
1052 uint32_t blksz, offset = dst->data;
1053
1054 for (blk = htx_get_head_blk(src); blk; blk = htx_get_next_blk(src, blk)) {
1055 type = htx_get_blk_type(blk);
1056
1057 if (type == HTX_BLK_UNUSED)
1058 continue;
1059
1060 blksz = htx_get_blksz(blk);
1061 newblk = htx_add_blk(dst, type, blksz);
1062 if (!newblk)
1063 goto error;
1064 newblk->info = blk->info;
1065 memcpy(htx_get_blk_ptr(dst, newblk), htx_get_blk_ptr(src, blk), blksz);
1066 }
1067
1068 return 1;
1069
1070 error:
1071 htx_truncate(dst, offset);
1072 return 0;
1073}