blob: 9239a22b70b1378b61cd41a31517d72779a6d582 [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
Willy Tarreauc13ed532020-06-02 10:22:45 +020013#include <haproxy/chunk.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020014#include <haproxy/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
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500201 * updates all addresses. The caller have the responsibility to perform the
Christopher Faulet3b219722019-06-19 13:48:09 +0200202 * expansion and update the block and the HTX message accordingly. No error must
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500203 * occur. 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 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500207 * 1: the expansion must be performed in place, there is enough 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)) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500238 /* There is not enough space to handle the expansion */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200239 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
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500365 * be adjusted. This operation could save some defrags. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200366 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 Faulet1cdceb92020-02-24 11:41:59 +0100394/* Looks for the HTX block containing the offset <offset>, starting at the HTX
395 * message's head. The function returns an htx_ret with the found HTX block and
396 * the position inside this block where the offset is. If the offset <offset> is
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500397 * outside of the HTX message, htx_ret.blk is set to NULL.
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100398 */
399struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset)
400{
401 struct htx_blk *blk;
402 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
403
404 if (offset >= htx->data)
405 return htxret;
406
407 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
408 uint32_t sz = htx_get_blksz(blk);
409
410 if (offset < sz)
411 break;
412 offset -= sz;
413 }
414 htxret.blk = blk;
415 htxret.ret = offset;
416 return htxret;
417}
418
Christopher Faulet3b219722019-06-19 13:48:09 +0200419/* Removes all blocks after the one containing the offset <offset>. This last
420 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100421 */
422void htx_truncate(struct htx *htx, uint32_t offset)
423{
424 struct htx_blk *blk;
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100425 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100426
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100427 blk = htxret.blk;
428 if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) {
429 htx_change_blk_value_len(htx, blk, htxret.ret);
430 blk = htx_get_next_blk(htx, blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100431 }
432 while (blk)
433 blk = htx_remove_blk(htx, blk);
434}
435
Christopher Faulet3b219722019-06-19 13:48:09 +0200436/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
437 * block, it will be cut if necessary. Others blocks will be removed at once if
438 * <count> is large enough. The function returns an htx_ret with the first block
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500439 * remaining in the message and the amount of data drained. If everything is
Christopher Faulet3b219722019-06-19 13:48:09 +0200440 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100441 */
442struct htx_ret htx_drain(struct htx *htx, uint32_t count)
443{
444 struct htx_blk *blk;
445 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
446
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200447 if (count == htx->data) {
Christopher Faulet07b2b0a2021-04-22 09:43:47 +0200448 uint32_t flags = htx->flags;
449
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200450 htx_reset(htx);
Christopher Faulet07b2b0a2021-04-22 09:43:47 +0200451 htx->flags = flags; /* restore flags */
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200452 htxret.ret = count;
453 return htxret;
454 }
455
Christopher Faulet549822f2019-02-25 10:23:19 +0100456 blk = htx_get_head_blk(htx);
457 while (count && blk) {
458 uint32_t sz = htx_get_blksz(blk);
459 enum htx_blk_type type = htx_get_blk_type(blk);
460
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500461 /* Ignore unused block */
Christopher Faulet549822f2019-02-25 10:23:19 +0100462 if (type == HTX_BLK_UNUSED)
463 goto next;
464
465 if (sz > count) {
466 if (type == HTX_BLK_DATA) {
467 htx_cut_data_blk(htx, blk, count);
468 htxret.ret += count;
469 }
470 break;
471 }
472 count -= sz;
473 htxret.ret += sz;
474 next:
475 blk = htx_remove_blk(htx, blk);
476 }
477 htxret.blk = blk;
478
479 return htxret;
480}
481
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200482/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200483 * there is enough space to take it all. If the space wraps, the buffer is
484 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200485 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200486 * returned. Due to its nature this function can be expensive and should be
487 * avoided whenever possible.
488 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200489struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200490{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200491 struct htx_blk *blk, *tailblk;
492 void *ptr;
493 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200494
Christopher Faulet192c6a22019-06-11 16:32:24 +0200495 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100496 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200497
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100498 /* Not enough space to store data */
499 if (data.len > htx_free_data_space(htx))
500 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200501
Christopher Fauletd7884d32019-06-11 10:40:43 +0200502 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200503 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200504 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200505 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200506 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200507
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100508 /* Don't try to append data if the last inserted block is not of the
509 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200510 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100511 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 /*
514 * Same type and enough space: append data
515 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200516 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200517 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200518 BUG_ON((int32_t)headroom < 0);
519 BUG_ON((int32_t)tailroom < 0);
520
521 len = data.len;
522 if (tailblk->addr+sz == htx->tail_addr) {
523 if (data.len <= tailroom)
524 goto append_data;
525 else if (!htx->head_addr) {
526 len = tailroom;
527 goto append_data;
528 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100529 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200530 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
531 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200532
Christopher Fauletd7884d32019-06-11 10:40:43 +0200533 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200534
535 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100536 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100537 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200538 ptr = htx_get_blk_ptr(htx, tailblk);
539 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200540 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200541
Christopher Fauletd7884d32019-06-11 10:40:43 +0200542 if (data.len == len) {
543 blk = tailblk;
544 goto end;
545 }
546 data.ptr += len;
547 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200548
549 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200550 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200551 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100552 if (!blk)
553 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200554
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100555 blk->info += data.len;
556 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200557
558 end:
559 BUG_ON((int32_t)htx->tail_addr < 0);
560 BUG_ON((int32_t)htx->head_addr < 0);
561 BUG_ON(htx->end_addr > htx->tail_addr);
562 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100563 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200564}
565
566/* Replaces a value part of a block by a new one. The new part can be smaller or
567 * larger than the old one. This function works for any kind of block with
568 * attached data. It returns the new block on success, otherwise it returns
569 * NULL.
570 */
571struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100572 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200573{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100575 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200576 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200577
Christopher Fauletd7884d32019-06-11 10:40:43 +0200578 n = htx_get_blk_name(htx, blk);
579 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100580 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200581 ret = htx_prepare_blk_expansion(htx, blk, delta);
582 if (!ret)
583 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100584
Christopher Fauletd7884d32019-06-11 10:40:43 +0200585 /* Before updating the payload, set the new block size and update HTX
586 * message */
587 htx_set_blk_value_len(blk, v.len + delta);
588 htx->data += delta;
589
Christopher Faulet3b219722019-06-19 13:48:09 +0200590 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200591 if (delta <= 0) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200592 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200593 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100594 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200595 }
596 else {
Christopher Faulet3b219722019-06-19 13:48:09 +0200597 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200598 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
599 memcpy(old.ptr, new.ptr, new.len);
600 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200602 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200603 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 /* Copy the name, if any */
606 memcpy(ptr, n.ptr, n.len);
607 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608
Christopher Fauletd7884d32019-06-11 10:40:43 +0200609 /* Copy value before old part, if any */
610 memcpy(ptr, v.ptr, old.ptr - v.ptr);
611 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 /* Copy new value */
614 memcpy(ptr, new.ptr, new.len);
615 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200616
Christopher Fauletd7884d32019-06-11 10:40:43 +0200617 /* Copy value after old part, if any */
618 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
619 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200620 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200621 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200622
Christopher Fauletd7884d32019-06-11 10:40:43 +0200623 /* Copy the header name, if any */
624 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625
Christopher Fauletd7884d32019-06-11 10:40:43 +0200626 /* Copy value before old part, if any */
627 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200628
Christopher Fauletd7884d32019-06-11 10:40:43 +0200629 /* Copy new value */
630 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631
Christopher Fauletd7884d32019-06-11 10:40:43 +0200632 /* Copy value after old part if any */
633 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200634
Christopher Fauletd7884d32019-06-11 10:40:43 +0200635 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500637 /* Finally, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200638 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
639 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100640 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200641}
642
643/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200644 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200645 * (including payload and meta-data). It returns the number of bytes moved and
646 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647 */
648struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
649 enum htx_blk_type mark)
650{
651 struct htx_blk *blk, *dstblk;
652 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100653 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200654 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200655
Christopher Faulet156852b2019-05-16 11:29:13 +0200656 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200657 blk = htx_get_blk(src, htx_get_head(src));
658 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200659
660 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200661 type = htx_get_blk_type(blk);
662
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500663 /* Ignore unused block */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200664 if (type == HTX_BLK_UNUSED)
665 goto next;
666
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200667 /* Be sure to have enough space to xfer all headers/trailers in
668 * one time. If not while <dst> is empty, we report a parsing
669 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200670 */
671 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
672 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
673
674 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
675 if (htx_is_empty(dst))
676 src->flags |= HTX_FL_PARSING_ERROR;
677 break;
678 }
679 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200680 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
681 !inside_trailers && mark >= HTX_BLK_EOT) {
682 inside_trailers = 1;
683 if (htx_used_space(src) > count) {
684 if (htx_is_empty(dst))
685 src->flags |= HTX_FL_PARSING_ERROR;
686 break;
687 }
688 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200689
Christopher Faulet156852b2019-05-16 11:29:13 +0200690 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200691 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200692 max = htx_get_max_blksz(dst, count);
693 if (!max)
694 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200695 if (sz > max) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200696 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200697 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200699 sz = max;
700 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200701 }
702
703 dstblk = htx_reserve_nxblk(dst, sz);
704 if (!dstblk)
705 break;
706 dstblk->info = info;
707 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
708
Christopher Faulet156852b2019-05-16 11:29:13 +0200709 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200710 if (blk->info != info) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200711 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200712 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200713 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200714 break;
715 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200716 next:
717 blk = htx_remove_blk(src, blk);
718 if (type == mark)
719 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720 }
721
Christopher Faulet156852b2019-05-16 11:29:13 +0200722 end:
723 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200724 return (struct htx_ret){.ret = ret, .blk = dstblk};
725}
726
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200727/* Replaces an header by a new one. The new header can be smaller or larger than
728 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100729 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200730 */
731struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100732 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100734 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200735 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100736 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200737 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200738
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100739 type = htx_get_blk_type(blk);
740 if (type != HTX_BLK_HDR)
741 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200742
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100743 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200744 ret = htx_prepare_blk_expansion(htx, blk, delta);
745 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100746 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747
Christopher Fauletd7884d32019-06-11 10:40:43 +0200748 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200749 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100750 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100751
Christopher Faulet3b219722019-06-19 13:48:09 +0200752 /* Replace in place or at a new address is the same. We replace all the
753 * header (name+value). Only take care to defrag the message if
754 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200755 if (ret == 3)
756 blk = htx_defrag(htx, blk);
757
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500758 /* Finally, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200759 ptr = htx_get_blk_ptr(htx, blk);
760 ist2bin_lc(ptr, name);
761 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100762 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200763}
764
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100765/* Replaces the parts of the start-line. It returns the new start-line on
766 * success, otherwise it returns NULL. It is the caller responsibility to update
767 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200768 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100769struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
770 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200771{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200772 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100773 struct htx_sl *sl;
774 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200775 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100776 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200777 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200778
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100779 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100780 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100781 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200782
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100783 /* Save start-line info and flags */
784 sl = htx_get_blk_ptr(htx, blk);
785 tmp.info = sl->info;
786 tmp.flags = sl->flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200787 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100788
Christopher Fauletd7884d32019-06-11 10:40:43 +0200789 sz = htx_get_blksz(blk);
790 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
791 ret = htx_prepare_blk_expansion(htx, blk, delta);
792 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100793 return NULL; /* not enough space */
794
Christopher Fauletd7884d32019-06-11 10:40:43 +0200795 /* Set the new block size and update HTX message */
796 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100797 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200798
Christopher Faulet3b219722019-06-19 13:48:09 +0200799 /* Replace in place or at a new address is the same. We replace all the
800 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200801 if (ret == 3)
802 blk = htx_defrag(htx, blk);
803
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100804 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100805 sl = htx_get_blk_ptr(htx, blk);
806 sl->info = tmp.info;
807 sl->flags = tmp.flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200808 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100810 HTX_SL_P1_LEN(sl) = p1.len;
811 HTX_SL_P2_LEN(sl) = p2.len;
812 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100813
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100814 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
815 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
816 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100818 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200819}
820
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100821/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
822 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200823 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100824struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
825 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200826{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100827 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100828 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829 uint32_t size;
830
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100831 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
832 return NULL;
833
834 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100836 /* FIXME: check size (< 256MB) */
837 blk = htx_add_blk(htx, type, size);
838 if (!blk)
839 return NULL;
840 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200841
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100842 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200843 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100844 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200845
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100846 HTX_SL_P1_LEN(sl) = p1.len;
847 HTX_SL_P2_LEN(sl) = p2.len;
848 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200849
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100850 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
851 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
852 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
853
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100854 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200855}
856
857/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100858 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200859 */
860struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100861 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200862{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100863 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200864
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100865 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
866 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
867 if (!blk)
868 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200869
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100870 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100871 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100872 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
873 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200874}
875
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200876/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet3b219722019-06-19 13:48:09 +0200877 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200878 */
879struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
880 const struct ist value)
881{
882 struct htx_blk *blk;
883
884 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
885 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
886 if (!blk)
887 return NULL;
888
889 blk->info += (value.len << 8) + name.len;
890 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
891 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
892 return blk;
893}
894
Christopher Faulet3b219722019-06-19 13:48:09 +0200895/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500896 * the EOH. On success, it returns the last block inserted (the EOH), otherwise
Christopher Faulet3b219722019-06-19 13:48:09 +0200897 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200898struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
899{
900 int i;
901
902 for (i = 0; hdrs[i].n.len; i++) {
903 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
904 return NULL;
905 }
906 return htx_add_endof(htx, HTX_BLK_EOH);
907}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200908
Christopher Faulet3b219722019-06-19 13:48:09 +0200909/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500910 * the EOT. On success, it returns the last block inserted (the EOT), otherwise
Christopher Faulet3b219722019-06-19 13:48:09 +0200911 * NULL is returned. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200912struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
913{
914 int i;
915
916 for (i = 0; hdrs[i].n.len; i++) {
917 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
918 return NULL;
919 }
920 return htx_add_endof(htx, HTX_BLK_EOT);
921}
922
Christopher Faulet3b219722019-06-19 13:48:09 +0200923/* Adds an HTX block of type EOH, EOT, or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200924 * on success. Otherwise, it returns NULL.
925 */
926struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
927{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100928 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200929
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100930 blk = htx_add_blk(htx, type, 1);
931 if (!blk)
932 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200933
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100934 blk->info += 1;
935 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200936}
937
938
939/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200940 * possible. It returns the number of bytes consumed from <data>, which may be
941 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200942 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200943size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200944{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200945 struct htx_blk *blk, *tailblk;
946 void *ptr;
947 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200948 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200949
Christopher Faulet192c6a22019-06-11 16:32:24 +0200950 if (htx->head == -1)
Willy Tarreau0350b902019-05-28 10:58:50 +0200951 goto add_new_block;
952
953 /* Not enough space to store data */
954 if (len > htx_free_data_space(htx))
955 len = htx_free_data_space(htx);
956
957 if (!len)
958 return 0;
959
960 /* get the tail and head block */
961 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200962 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200963 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200964 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200965
966 /* Don't try to append data if the last inserted block is not of the
967 * same type */
968 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
969 goto add_new_block;
970
971 /*
972 * Same type and enough space: append data
973 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200974 if (!htx->head_addr) {
975 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200976 goto add_new_block;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200977 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200978 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200979 else {
980 if (tailblk->addr+sz != htx->head_addr)
981 goto add_new_block;
982 room = (htx->end_addr - htx->head_addr);
983 }
984 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200985 if (room < len)
986 len = room;
987
988 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200989 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200990 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200991 ptr = htx_get_blk_ptr(htx, tailblk);
992 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200993 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200994
995 BUG_ON((int32_t)htx->tail_addr < 0);
996 BUG_ON((int32_t)htx->head_addr < 0);
997 BUG_ON(htx->end_addr > htx->tail_addr);
998 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200999 return len;
1000
1001 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001002 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001003 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1004 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001005 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001006
1007 blk->info += len;
1008 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1009 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001010}
1011
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001012
1013/* Adds an HTX block of type DATA in <htx> just after all other DATA
1014 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1015 * DATA block if possible. But, if the function succeeds, it will be the last
1016 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1017 * on success, the updated block (or the new one) is returned.
1018 */
1019struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001020{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001021 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001022
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001023 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001024 if (!blk)
1025 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001026
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001027 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001028 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1029 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001030
Christopher Faulet24ed8352018-11-22 11:20:43 +01001031 /* Swap .addr and .info fields */
1032 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1033 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1034
1035 if (blk->addr == pblk->addr)
1036 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001037 blk = pblk;
1038 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001039
Christopher Faulet24ed8352018-11-22 11:20:43 +01001040 return blk;
1041}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001042
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001043/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1044 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1045 * blocks are updated to remain valid after the move. */
1046void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1047{
1048 struct htx_blk *cblk, *pblk;
1049
1050 cblk = *blk;
1051 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1052 /* Swap .addr and .info fields */
1053 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1054 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1055
1056 if (cblk->addr == pblk->addr)
1057 cblk->addr += htx_get_blksz(pblk);
1058 if (pblk == *ref)
1059 break;
1060 cblk = pblk;
1061 }
1062 *blk = cblk;
1063 *ref = pblk;
1064}
Christopher Faulet0ea0c862020-01-23 11:47:53 +01001065
1066/* Append the HTX message <src> to the HTX message <dst>. It returns 1 on
1067 * success and 0 on error. All the message or nothing is copied. If an error
1068 * occurred, all blocks from <src> already appended to <dst> are truncated.
1069 */
1070int htx_append_msg(struct htx *dst, const struct htx *src)
1071{
1072 struct htx_blk *blk, *newblk;
1073 enum htx_blk_type type;
1074 uint32_t blksz, offset = dst->data;
1075
1076 for (blk = htx_get_head_blk(src); blk; blk = htx_get_next_blk(src, blk)) {
1077 type = htx_get_blk_type(blk);
1078
1079 if (type == HTX_BLK_UNUSED)
1080 continue;
1081
1082 blksz = htx_get_blksz(blk);
1083 newblk = htx_add_blk(dst, type, blksz);
1084 if (!newblk)
1085 goto error;
1086 newblk->info = blk->info;
1087 memcpy(htx_get_blk_ptr(dst, newblk), htx_get_blk_ptr(src, blk), blksz);
1088 }
1089
1090 return 1;
1091
1092 error:
1093 htx_truncate(dst, offset);
1094 return 0;
1095}