blob: a494a8b9ff63105e5e6dfaa0a75ae93d7d4b67ed [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>
Willy Tarreau23aa79d2023-02-02 15:32:20 +010015#include <haproxy/net_helper.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020016
Christopher Faulet192c6a22019-06-11 16:32:24 +020017struct htx htx_empty = { .size = 0, .data = 0, .head = -1, .tail = -1, .first = -1 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +020018
Willy Tarreau23aa79d2023-02-02 15:32:20 +010019/* tests show that 63% of these calls are for 64-bit chunks, so better avoid calling
20 * memcpy() for that!
21 */
22static inline __attribute__((always_inline)) void htx_memcpy(void *dst, void *src, size_t len)
23{
24 if (likely(len == 8))
25 write_u64(dst, read_u64(src));
26 else
27 memcpy(dst, src, len);
28}
29
Christopher Faulet3b219722019-06-19 13:48:09 +020030/* Defragments an HTX message. It removes unused blocks and unwraps the payloads
Christopher Faulet1cf414b2021-06-09 17:30:40 +020031 * part. A temporary buffer is used to do so. This function never fails. Most of
32 * time, we need keep a ref on a specific HTX block. Thus is <blk> is set, the
33 * pointer on its new position, after defrag, is returned. In addition, if the
34 * size of the block must be altered, <blkinfo> info must be provided (!=
35 * 0). But in this case, it remains the caller responsibility to update the
36 * block content.
Christopher Fauleta3d2a162018-10-22 08:59:39 +020037 */
38/* TODO: merge data blocks into one */
Christopher Faulet1cf414b2021-06-09 17:30:40 +020039struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk, uint32_t blkinfo)
Christopher Fauleta3d2a162018-10-22 08:59:39 +020040{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010041 struct buffer *chunk = get_trash_chunk();
42 struct htx *tmp = htxbuf(chunk);
43 struct htx_blk *newblk, *oldblk;
Christopher Faulet200f8952019-01-02 11:23:44 +010044 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010045 uint32_t addr, blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +020046 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020047
Christopher Faulet192c6a22019-06-11 16:32:24 +020048 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010049 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020050
Christopher Faulet200f8952019-01-02 11:23:44 +010051 blkpos = -1;
52
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010053 new = 0;
54 addr = 0;
55 tmp->size = htx->size;
Christopher Faulet1cf414b2021-06-09 17:30:40 +020056 tmp->data = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020057
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010058 /* start from the head */
59 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
60 oldblk = htx_get_blk(htx, old);
Christopher Faulet28f29c72019-04-30 17:55:45 +020061 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010062 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020063
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010064 blksz = htx_get_blksz(oldblk);
Willy Tarreau23aa79d2023-02-02 15:32:20 +010065 htx_memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher Faulet9c66b982019-04-30 18:08:26 +020067 /* update the start-line position */
Christopher Faulet29f17582019-05-23 11:03:26 +020068 if (htx->first == old)
69 first = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010070
Christopher Faulet1cf414b2021-06-09 17:30:40 +020071 newblk = htx_get_blk(tmp, new);
72 newblk->addr = addr;
73 newblk->info = oldblk->info;
74
Christopher Faulet3b219722019-06-19 13:48:09 +020075 /* if <blk> is defined, save its new position */
Christopher Faulet1cf414b2021-06-09 17:30:40 +020076 if (blk != NULL && blk == oldblk) {
77 if (blkinfo)
78 newblk->info = blkinfo;
Christopher Faulet200f8952019-01-02 11:23:44 +010079 blkpos = new;
Christopher Faulet1cf414b2021-06-09 17:30:40 +020080 }
Christopher Faulet200f8952019-01-02 11:23:44 +010081
Christopher Faulet1cf414b2021-06-09 17:30:40 +020082 blksz = htx_get_blksz(newblk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010083 addr += blksz;
Christopher Faulet1cf414b2021-06-09 17:30:40 +020084 tmp->data += blksz;
85 new++;
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020086 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020087
Christopher Faulet1cf414b2021-06-09 17:30:40 +020088 htx->data = tmp->data;
Christopher Faulet29f17582019-05-23 11:03:26 +020089 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020090 htx->head = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +020091 htx->tail = new - 1;
92 htx->head_addr = htx->end_addr = 0;
93 htx->tail_addr = addr;
Christopher Faulet4697c922021-09-21 15:39:30 +020094 htx->flags &= ~HTX_FL_FRAGMENTED;
Willy Tarreau23aa79d2023-02-02 15:32:20 +010095 htx_memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020096
Christopher Faulet200f8952019-01-02 11:23:44 +010097 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020098}
99
Christopher Faulet3b219722019-06-19 13:48:09 +0200100/* Degragments HTX blocks of an HTX message. Payloads part is keep untouched
101 * here. This function will move back all blocks starting at the position 0,
102 * removing unused blocks. It must never be called with an empty message.
103 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200104static void htx_defrag_blks(struct htx *htx)
105{
106 int32_t pos, new;
107
108 new = 0;
109 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
110 struct htx_blk *posblk, *newblk;
111
112 if (pos == new) {
113 new++;
114 continue;
115 }
116
117 posblk = htx_get_blk(htx, pos);
118 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
119 continue;
120
121 if (htx->first == pos)
122 htx->first = new;
123 newblk = htx_get_blk(htx, new++);
124 newblk->info = posblk->info;
125 newblk->addr = posblk->addr;
126 }
127 BUG_ON(!new);
128 htx->head = 0;
129 htx->tail = new - 1;
130}
131
Christopher Faulet3b219722019-06-19 13:48:09 +0200132/* Reserves a new block in the HTX message <htx> with a content of <blksz>
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200133 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
Christopher Faulet3b219722019-06-19 13:48:09 +0200134 * block is returned and the HTX message is updated. Space for this new block is
135 * reserved in the HTX message. But it is the caller responsibility to set right
136 * info in the block to reflect the stored data.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200137 */
138static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
139{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200140 struct htx_blk *blk;
Christopher Faulet192c6a22019-06-11 16:32:24 +0200141 uint32_t tail, headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200142
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100143 if (blksz > htx_free_data_space(htx))
144 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200145
Christopher Faulet192c6a22019-06-11 16:32:24 +0200146 if (htx->head == -1) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100147 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200148 htx->head = htx->tail = htx->first = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100149 blk = htx_get_blk(htx, htx->tail);
150 blk->addr = 0;
151 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200152 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100153 return blk;
154 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200155
Christopher Fauletd7884d32019-06-11 10:40:43 +0200156 /* Find the block's position. First, we try to get the next position in
157 * the message, increasing the tail by one. If this position is not
158 * available with some holes, we try to defrag the blocks without
159 * touching their paylood. If it is impossible, we fully defrag the
160 * message.
161 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200162 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200163 if (htx_pos_to_addr(htx, tail) >= htx->tail_addr)
Christopher Faulet192c6a22019-06-11 16:32:24 +0200164 ;
165 else if (htx->head > 0) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200166 htx_defrag_blks(htx);
167 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200168 BUG_ON(htx_pos_to_addr(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100169 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200170 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100171 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200172
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700173 /* Now, we have found the block's position. Try to find where to put its
Christopher Fauletd7884d32019-06-11 10:40:43 +0200174 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100175 *
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700176 * * The free space in front of the blocks table. This one is used if and
177 * only if the other one was not used yet.
Christopher Fauletd7884d32019-06-11 10:40:43 +0200178 *
179 * * The free space at the beginning of the message. Once this one is
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700180 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100181 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200182 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200183 tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200184 BUG_ON((int32_t)headroom < 0);
185 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200186
Christopher Fauletd7884d32019-06-11 10:40:43 +0200187 if (blksz <= tailroom) {
188 blk = htx_get_blk(htx, tail);
189 blk->addr = htx->tail_addr;
190 htx->tail_addr += blksz;
191 }
192 else if (blksz <= headroom) {
193 blk = htx_get_blk(htx, tail);
194 blk->addr = htx->head_addr;
195 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100196 }
197 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200198 defrag:
Christopher Faulet3b219722019-06-19 13:48:09 +0200199 /* need to defragment the message before inserting upfront */
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200200 htx_defrag(htx, NULL, 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200201 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200202 blk = htx_get_blk(htx, tail);
203 blk->addr = htx->tail_addr;
204 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100205 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200206
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100207 htx->tail = tail;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100208 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200209 /* Set first position if not already set */
210 if (htx->first == -1)
211 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200212
213 BUG_ON((int32_t)htx->tail_addr < 0);
214 BUG_ON((int32_t)htx->head_addr < 0);
215 BUG_ON(htx->end_addr > htx->tail_addr);
216 BUG_ON(htx->head_addr > htx->end_addr);
217
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100218 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200219}
220
Christopher Fauletd7884d32019-06-11 10:40:43 +0200221/* Prepares the block to an expansion of its payload. The payload will be
222 * expanded by <delta> bytes and we need find where this expansion will be
223 * performed. It can be a compression if <delta> is negative. This function only
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500224 * updates all addresses. The caller have the responsibility to perform the
Christopher Faulet3b219722019-06-19 13:48:09 +0200225 * expansion and update the block and the HTX message accordingly. No error must
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500226 * occur. It returns following values:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200227 *
228 * 0: The expansion cannot be performed, there is not enough space.
229 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500230 * 1: the expansion must be performed in place, there is enough space after
Christopher Fauletd7884d32019-06-11 10:40:43 +0200231 * the block's payload to handle it. This is especially true if it is a
232 * compression and not an expension.
233 *
234 * 2: the block's payload must be moved at the new block address before doing
235 * the expansion.
236 *
237 * 3: the HTX message message must be defragmented
238 */
239static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
240{
241 uint32_t sz, tailroom, headroom;
242 int ret = 3;
243
Christopher Faulet192c6a22019-06-11 16:32:24 +0200244 BUG_ON(htx->head == -1);
245
Christopher Fauletd7884d32019-06-11 10:40:43 +0200246 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200247 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200248 BUG_ON((int32_t)headroom < 0);
249 BUG_ON((int32_t)tailroom < 0);
250
251 sz = htx_get_blksz(blk);
252 if (delta <= 0) {
253 /* It is a compression, it can be performed in place */
254 if (blk->addr+sz == htx->tail_addr)
255 htx->tail_addr += delta;
256 else if (blk->addr+sz == htx->head_addr)
257 htx->head_addr += delta;
258 ret = 1;
259 }
260 else if (delta > htx_free_space(htx)) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500261 /* There is not enough space to handle the expansion */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200262 ret = 0;
263 }
264 else if (blk->addr+sz == htx->tail_addr) {
265 /* The block's payload is just before the tail room */
266 if (delta < tailroom) {
267 /* Expand the block's payload */
268 htx->tail_addr += delta;
269 ret = 1;
270 }
271 else if ((sz + delta) < headroom) {
Christopher Faulet61ed7792019-07-29 10:50:28 +0200272 uint32_t oldaddr = blk->addr;
273
Christopher Fauletd7884d32019-06-11 10:40:43 +0200274 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200275 blk->addr = htx->head_addr;
276 htx->tail_addr -= sz;
277 htx->head_addr += sz + delta;
Christopher Faulet61ed7792019-07-29 10:50:28 +0200278 if (oldaddr == htx->end_addr) {
Christopher Faulet8c654862019-06-12 11:08:11 +0200279 if (htx->end_addr == htx->tail_addr) {
280 htx->tail_addr = htx->head_addr;
281 htx->head_addr = htx->end_addr = 0;
282 }
283 else
284 htx->end_addr += sz;
285 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200286 ret = 2;
287 }
288 }
289 else if (blk->addr+sz == htx->head_addr) {
290 /* The block's payload is just before the head room */
291 if (delta < headroom) {
292 /* Expand the block's payload */
293 htx->head_addr += delta;
294 ret = 1;
295 }
296 }
297 else {
298 /* The block's payload is not at the rooms edge */
299 if (!htx->head_addr && sz+delta < tailroom) {
300 /* Move the block's payload into the tailroom */
301 if (blk->addr == htx->end_addr)
302 htx->end_addr += sz;
303 blk->addr = htx->tail_addr;
304 htx->tail_addr += sz + delta;
305 ret = 2;
306 }
307 else if (sz+delta < headroom) {
308 /* Move the block's payload into the headroom */
309 if (blk->addr == htx->end_addr)
310 htx->end_addr += sz;
311 blk->addr = htx->head_addr;
312 htx->head_addr += sz + delta;
313 ret = 2;
314 }
315 }
316 /* Otherwise defrag the HTX message */
317
318 BUG_ON((int32_t)htx->tail_addr < 0);
319 BUG_ON((int32_t)htx->head_addr < 0);
320 BUG_ON(htx->end_addr > htx->tail_addr);
321 BUG_ON(htx->head_addr > htx->end_addr);
322 return ret;
323}
324
Christopher Faulet3b219722019-06-19 13:48:09 +0200325/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
326 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200327 */
328struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
329{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100330 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200331
Willy Tarreau3d5f19e2021-08-26 16:07:22 +0200332 BUG_ON(blksz >= 256 << 20);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100333 blk = htx_reserve_nxblk(htx, blksz);
334 if (!blk)
335 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200336 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200337
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100338 blk->info = (type << 28);
339 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200340}
341
Christopher Faulet3b219722019-06-19 13:48:09 +0200342/* Removes the block <blk> from the HTX message <htx>. The function returns the
343 * block following <blk> or NULL if <blk> is the last block or the last inserted
344 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200345 */
346struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
347{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200348 enum htx_blk_type type;
349 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200350
Christopher Faulet234a10a2022-02-28 15:29:56 +0100351 BUG_ON(!blk || htx->head == -1);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200352
Christopher Fauletd7884d32019-06-11 10:40:43 +0200353 /* This is the last block in use */
Christopher Faulet192c6a22019-06-11 16:32:24 +0200354 if (htx->head == htx->tail) {
Christopher Faulet4697c922021-09-21 15:39:30 +0200355 uint32_t flags = (htx->flags & ~HTX_FL_FRAGMENTED); /* Preserve flags except FRAGMENTED */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100356
Christopher Fauletd7884d32019-06-11 10:40:43 +0200357 htx_reset(htx);
Christopher Faulet4697c922021-09-21 15:39:30 +0200358 htx->flags = flags; /* restore flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200359 return NULL;
360 }
361
362 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200363 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200364 sz = htx_get_blksz(blk);
365 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100366 if (type != HTX_BLK_UNUSED) {
367 /* Mark the block as unused, decrement allocated size */
368 htx->data -= htx_get_blksz(blk);
369 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100370 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200371
Christopher Faulet3b219722019-06-19 13:48:09 +0200372 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200373 if (pos == htx->head) {
374 /* move the head forward */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200375 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100376 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200377 else if (pos == htx->tail) {
378 /* remove the tail. this was the last inserted block so
379 * return NULL. */
380 htx->tail--;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200381 blk = NULL;
382 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100383 }
Christopher Faulet4697c922021-09-21 15:39:30 +0200384 else
385 htx->flags |= HTX_FL_FRAGMENTED;
386
Christopher Fauletd7884d32019-06-11 10:40:43 +0200387 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200388
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200389 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200390 if (pos == htx->first)
391 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200392
Christopher Faulet192c6a22019-06-11 16:32:24 +0200393 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200394 /* If there is just one block in the HTX message, free space can
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500395 * be adjusted. This operation could save some defrags. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200396 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
397
398 htx->head_addr = 0;
399 htx->end_addr = lastblk->addr;
400 htx->tail_addr = lastblk->addr+htx->data;
401 }
402 else {
403 if (addr+sz == htx->tail_addr)
404 htx->tail_addr = addr;
405 else if (addr+sz == htx->head_addr)
406 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200407 if (addr == htx->end_addr) {
408 if (htx->tail_addr == htx->end_addr) {
409 htx->tail_addr = htx->head_addr;
410 htx->head_addr = htx->end_addr = 0;
411 }
412 else
413 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200414 }
415 }
416
417 BUG_ON((int32_t)htx->tail_addr < 0);
418 BUG_ON((int32_t)htx->head_addr < 0);
419 BUG_ON(htx->end_addr > htx->tail_addr);
420 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100421 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200422}
423
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100424/* Looks for the HTX block containing the offset <offset>, starting at the HTX
425 * message's head. The function returns an htx_ret with the found HTX block and
426 * the position inside this block where the offset is. If the offset <offset> is
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500427 * outside of the HTX message, htx_ret.blk is set to NULL.
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100428 */
429struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset)
430{
431 struct htx_blk *blk;
432 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
433
434 if (offset >= htx->data)
435 return htxret;
436
437 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
438 uint32_t sz = htx_get_blksz(blk);
439
440 if (offset < sz)
441 break;
442 offset -= sz;
443 }
444 htxret.blk = blk;
445 htxret.ret = offset;
446 return htxret;
447}
448
Christopher Faulet3b219722019-06-19 13:48:09 +0200449/* Removes all blocks after the one containing the offset <offset>. This last
450 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100451 */
452void htx_truncate(struct htx *htx, uint32_t offset)
453{
454 struct htx_blk *blk;
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100455 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100456
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100457 blk = htxret.blk;
458 if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) {
459 htx_change_blk_value_len(htx, blk, htxret.ret);
460 blk = htx_get_next_blk(htx, blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100461 }
462 while (blk)
463 blk = htx_remove_blk(htx, blk);
464}
465
Christopher Faulet3b219722019-06-19 13:48:09 +0200466/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
467 * block, it will be cut if necessary. Others blocks will be removed at once if
468 * <count> is large enough. The function returns an htx_ret with the first block
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500469 * remaining in the message and the amount of data drained. If everything is
Christopher Faulet3b219722019-06-19 13:48:09 +0200470 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100471 */
472struct htx_ret htx_drain(struct htx *htx, uint32_t count)
473{
474 struct htx_blk *blk;
475 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
476
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200477 if (count == htx->data) {
Christopher Faulet4697c922021-09-21 15:39:30 +0200478 uint32_t flags = (htx->flags & ~HTX_FL_FRAGMENTED); /* Preserve flags except FRAGMENTED */
Christopher Faulet5e9b24f2021-04-22 09:43:47 +0200479
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200480 htx_reset(htx);
Christopher Faulet5e9b24f2021-04-22 09:43:47 +0200481 htx->flags = flags; /* restore flags */
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200482 htxret.ret = count;
483 return htxret;
484 }
485
Christopher Faulet549822f2019-02-25 10:23:19 +0100486 blk = htx_get_head_blk(htx);
487 while (count && blk) {
488 uint32_t sz = htx_get_blksz(blk);
489 enum htx_blk_type type = htx_get_blk_type(blk);
490
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500491 /* Ignore unused block */
Christopher Faulet549822f2019-02-25 10:23:19 +0100492 if (type == HTX_BLK_UNUSED)
493 goto next;
494
495 if (sz > count) {
496 if (type == HTX_BLK_DATA) {
497 htx_cut_data_blk(htx, blk, count);
498 htxret.ret += count;
499 }
500 break;
501 }
502 count -= sz;
503 htxret.ret += sz;
504 next:
505 blk = htx_remove_blk(htx, blk);
506 }
507 htxret.blk = blk;
508
509 return htxret;
510}
511
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200513 * there is enough space to take it all. If the space wraps, the buffer is
514 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200515 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200516 * returned. Due to its nature this function can be expensive and should be
517 * avoided whenever possible.
518 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200519struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200520{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200521 struct htx_blk *blk, *tailblk;
522 void *ptr;
523 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524
Christopher Faulet192c6a22019-06-11 16:32:24 +0200525 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100526 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200527
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100528 /* Not enough space to store data */
529 if (data.len > htx_free_data_space(htx))
530 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200531
Christopher Fauletd7884d32019-06-11 10:40:43 +0200532 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200533 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200534 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200535 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200536 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200537
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100538 /* Don't try to append data if the last inserted block is not of the
539 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200540 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100541 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100543 /*
544 * Same type and enough space: append data
545 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200546 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200547 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200548 BUG_ON((int32_t)headroom < 0);
549 BUG_ON((int32_t)tailroom < 0);
550
551 len = data.len;
552 if (tailblk->addr+sz == htx->tail_addr) {
553 if (data.len <= tailroom)
554 goto append_data;
555 else if (!htx->head_addr) {
556 len = tailroom;
557 goto append_data;
558 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100559 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200560 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
561 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200562
Christopher Fauletd7884d32019-06-11 10:40:43 +0200563 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200564
565 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100566 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200567 ptr = htx_get_blk_ptr(htx, tailblk);
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100568 htx_memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200569 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200570
Christopher Fauletd7884d32019-06-11 10:40:43 +0200571 if (data.len == len) {
572 blk = tailblk;
573 goto end;
574 }
Tim Duesterhus154374c2021-03-02 18:57:27 +0100575 data = istadv(data, len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200576
577 add_new_block:
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200578 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100579 if (!blk)
580 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200581
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100582 blk->info += data.len;
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100583 htx_memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200584
585 end:
586 BUG_ON((int32_t)htx->tail_addr < 0);
587 BUG_ON((int32_t)htx->head_addr < 0);
588 BUG_ON(htx->end_addr > htx->tail_addr);
589 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100590 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200591}
592
593/* Replaces a value part of a block by a new one. The new part can be smaller or
594 * larger than the old one. This function works for any kind of block with
595 * attached data. It returns the new block on success, otherwise it returns
596 * NULL.
597 */
598struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100599 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100601 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100602 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200603 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 n = htx_get_blk_name(htx, blk);
606 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100607 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200608 ret = htx_prepare_blk_expansion(htx, blk, delta);
609 if (!ret)
610 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100611
Christopher Faulet3b219722019-06-19 13:48:09 +0200612 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 if (delta <= 0) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200614 /* compression: copy new data first then move the end */
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100615 htx_memcpy(old.ptr, new.ptr, new.len);
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100616 memmove(old.ptr + new.len, istend(old),
617 istend(v) - istend(old));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200618 }
619 else {
Christopher Faulet3b219722019-06-19 13:48:09 +0200620 /* expansion: move the end first then copy new data */
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100621 memmove(old.ptr + new.len, istend(old),
622 istend(v) - istend(old));
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100623 htx_memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200624 }
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200625
626 /* set the new block size and update HTX message */
627 htx_set_blk_value_len(blk, v.len + delta);
628 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100629 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200630 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200631 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200632
Christopher Fauletd7884d32019-06-11 10:40:43 +0200633 /* Copy the name, if any */
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100634 htx_memcpy(ptr, n.ptr, n.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200635 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Christopher Fauletd7884d32019-06-11 10:40:43 +0200637 /* Copy value before old part, if any */
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100638 htx_memcpy(ptr, v.ptr, old.ptr - v.ptr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200639 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200640
Christopher Fauletd7884d32019-06-11 10:40:43 +0200641 /* Copy new value */
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100642 htx_memcpy(ptr, new.ptr, new.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200643 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200644
Christopher Fauletd7884d32019-06-11 10:40:43 +0200645 /* Copy value after old part, if any */
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100646 htx_memcpy(ptr, istend(old), istend(v) - istend(old));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200648 /* set the new block size and update HTX message */
649 htx_set_blk_value_len(blk, v.len + delta);
650 htx->data += delta;
651 }
652 else { /* Do a degrag first (it is always an expansion) */
653 struct htx_blk tmpblk;
654 int32_t offset;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200655
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200656 /* use tmpblk to set new block size before defrag and to compute
657 * the offset after defrag
658 */
659 tmpblk.addr = blk->addr;
660 tmpblk.info = blk->info;
661 htx_set_blk_value_len(&tmpblk, v.len + delta);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200662
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200663 /* htx_defrag() will take care to update the block size and the htx message */
664 blk = htx_defrag(htx, blk, tmpblk.info);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200666 /* newblk is now the new HTX block. Compute the offset to copy/move payload */
667 offset = blk->addr - tmpblk.addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200669 /* move the end first and copy new data
670 */
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100671 memmove(old.ptr + offset + new.len, old.ptr + offset + old.len,
672 istend(v) - istend(old));
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100673 htx_memcpy(old.ptr + offset, new.ptr, new.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200674 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100675 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200676}
677
678/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100679 * type <mark> (typically EOH or EOT) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200680 * (including payload and meta-data). It returns the number of bytes moved and
681 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200682 */
683struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
684 enum htx_blk_type mark)
685{
686 struct htx_blk *blk, *dstblk;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200687 struct htx_blk *srcref, *dstref;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200688 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100689 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200690
Christopher Faulet156852b2019-05-16 11:29:13 +0200691 ret = htx_used_space(dst);
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200692 srcref = dstref = dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200693
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200694 /* blocks are not removed yet from <src> HTX message to be able to
695 * rollback the transfer if all the headers/trailers are not copied.
696 */
697 for (blk = htx_get_head_blk(src); blk && count; blk = htx_get_next_blk(src, blk)) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 type = htx_get_blk_type(blk);
699
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500700 /* Ignore unused block */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200701 if (type == HTX_BLK_UNUSED)
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200702 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200704
Christopher Faulet156852b2019-05-16 11:29:13 +0200705 max = htx_get_max_blksz(dst, count);
706 if (!max)
707 break;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200708
709 sz = htx_get_blksz(blk);
710 info = blk->info;
Willy Tarreau90caa072019-04-09 16:21:54 +0200711 if (sz > max) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200712 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200713 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200714 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200715 sz = max;
716 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200717 }
718
719 dstblk = htx_reserve_nxblk(dst, sz);
720 if (!dstblk)
721 break;
722 dstblk->info = info;
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100723 htx_memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200724
Christopher Faulet156852b2019-05-16 11:29:13 +0200725 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200726 if (blk->info != info) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200727 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200728 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200729 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200730 break;
731 }
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200732
733 if (type == mark) {
734 blk = htx_get_next_blk(src, blk);
735 srcref = dstref = NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736 break;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200737 }
738
739 /* Save <blk> to <srcref> and <dstblk> to <dstref> when we start
740 * to xfer headers or trailers. When EOH/EOT block is reached,
741 * both are reset. It is mandatory to be able to rollback a
742 * partial transfer.
743 */
744 if (!srcref && !dstref &&
745 (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_TLR)) {
746 srcref = blk;
747 dstref = dstblk;
748 }
749 else if (type == HTX_BLK_EOH || type == HTX_BLK_EOT)
750 srcref = dstref = NULL;
751 }
752
753 if (unlikely(dstref)) {
Christopher Faulet234a10a2022-02-28 15:29:56 +0100754 /* Headers or trailers part was partially xferred, so rollback
755 * the copy by removing all block between <dstref> and <dstblk>,
756 * both included. <dstblk> may be NULL.
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200757 */
758 while (dstref && dstref != dstblk)
759 dstref = htx_remove_blk(dst, dstref);
Christopher Faulet234a10a2022-02-28 15:29:56 +0100760 if (dstblk)
761 htx_remove_blk(dst, dstblk);
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200762
763 /* <dst> HTX message is empty, it means the headers or trailers
764 * part is too big to be copied at once.
765 */
766 if (htx_is_empty(dst))
767 src->flags |= HTX_FL_PARSING_ERROR;
768 }
769
770 /* Now, remove xferred blocks from <src> htx message */
771 if (!blk && !srcref) {
772 /* End of src reached, all blocks were consumed, drain all data */
773 htx_drain(src, src->data);
774 }
775 else {
776 /* Remove all block from the head to <blk>, or <srcref> if defined, excluded */
777 srcref = (srcref ? srcref : blk);
778 for (blk = htx_get_head_blk(src); blk && blk != srcref; blk = htx_remove_blk(src, blk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200779 }
780
Christopher Faulet156852b2019-05-16 11:29:13 +0200781 end:
782 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200783 return (struct htx_ret){.ret = ret, .blk = dstblk};
784}
785
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200786/* Replaces an header by a new one. The new header can be smaller or larger than
787 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100788 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200789 */
790struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100791 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200792{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100793 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200794 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100795 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200796 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200797
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100798 type = htx_get_blk_type(blk);
799 if (type != HTX_BLK_HDR)
800 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200801
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100802 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200803 ret = htx_prepare_blk_expansion(htx, blk, delta);
804 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100805 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200806
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100807
Christopher Faulet3b219722019-06-19 13:48:09 +0200808 /* Replace in place or at a new address is the same. We replace all the
809 * header (name+value). Only take care to defrag the message if
810 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200811 if (ret == 3)
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200812 blk = htx_defrag(htx, blk, (type << 28) + (value.len << 8) + name.len);
813 else {
814 /* Set the new block size and update HTX message */
815 blk->info = (type << 28) + (value.len << 8) + name.len;
816 htx->data += delta;
817 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200818
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500819 /* Finally, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200820 ptr = htx_get_blk_ptr(htx, blk);
821 ist2bin_lc(ptr, name);
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100822 htx_memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100823 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200824}
825
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826/* Replaces the parts of the start-line. It returns the new start-line on
827 * success, otherwise it returns NULL. It is the caller responsibility to update
828 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100830struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
831 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200832{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200833 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100834 struct htx_sl *sl;
835 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200836 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100837 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200838 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200839
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100840 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100841 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100842 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200843
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100844 /* Save start-line info and flags */
845 sl = htx_get_blk_ptr(htx, blk);
846 tmp.info = sl->info;
847 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100848
Christopher Fauletd7884d32019-06-11 10:40:43 +0200849 sz = htx_get_blksz(blk);
850 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
851 ret = htx_prepare_blk_expansion(htx, blk, delta);
852 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100853 return NULL; /* not enough space */
854
Christopher Faulet3b219722019-06-19 13:48:09 +0200855 /* Replace in place or at a new address is the same. We replace all the
856 * start-line. Only take care to defrag the message if necessary. */
Christopher Faulet1cf414b2021-06-09 17:30:40 +0200857 if (ret == 3) {
858 blk = htx_defrag(htx, blk, (type << 28) + sz + delta);
859 }
860 else {
861 /* Set the new block size and update HTX message */
862 blk->info = (type << 28) + sz + delta;
863 htx->data += delta;
864 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200865
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100866 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100867 sl = htx_get_blk_ptr(htx, blk);
868 sl->info = tmp.info;
869 sl->flags = tmp.flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200870
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100871 HTX_SL_P1_LEN(sl) = p1.len;
872 HTX_SL_P2_LEN(sl) = p2.len;
873 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100874
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100875 htx_memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
876 htx_memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
877 htx_memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200878
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100879 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200880}
881
Christopher Faulete071f0e2021-02-03 12:11:31 +0100882/* Reserves the maximum possible size for an HTX data block, by extending an
883 * existing one or by creating a now one. It returns a compound result with the
884 * HTX block and the position where new data must be inserted (0 for a new
885 * block). If an error occurs or if there is no space left, NULL is returned
886 * instead of a pointer on an HTX block.
887 */
888struct htx_ret htx_reserve_max_data(struct htx *htx)
889{
Christopher Faulet69f59f42023-11-07 07:50:22 +0100890 struct htx_blk *blk, *tailblk;
891 uint32_t sz, room;
892 int32_t len = htx_free_data_space(htx);
Christopher Faulete071f0e2021-02-03 12:11:31 +0100893
Christopher Faulet69f59f42023-11-07 07:50:22 +0100894 if (htx->head == -1)
895 goto rsv_new_block;
Christopher Faulete071f0e2021-02-03 12:11:31 +0100896
Christopher Faulet69f59f42023-11-07 07:50:22 +0100897 if (!len)
898 return (struct htx_ret){.ret = 0, .blk = NULL};
Christopher Faulete071f0e2021-02-03 12:11:31 +0100899
Christopher Faulet69f59f42023-11-07 07:50:22 +0100900 /* get the tail and head block */
901 tailblk = htx_get_tail_blk(htx);
902 if (tailblk == NULL)
903 goto rsv_new_block;
904 sz = htx_get_blksz(tailblk);
Christopher Faulete071f0e2021-02-03 12:11:31 +0100905
Christopher Faulet69f59f42023-11-07 07:50:22 +0100906 /* Don't try to append data if the last inserted block is not of the
907 * same type */
908 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
909 goto rsv_new_block;
Christopher Faulete071f0e2021-02-03 12:11:31 +0100910
Christopher Faulet69f59f42023-11-07 07:50:22 +0100911 /*
912 * Same type and enough space: append data
913 */
914 if (!htx->head_addr) {
915 if (tailblk->addr+sz != htx->tail_addr)
916 goto rsv_new_block;
917 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
918 }
919 else {
920 if (tailblk->addr+sz != htx->head_addr)
921 goto rsv_new_block;
922 room = (htx->end_addr - htx->head_addr);
923 }
924 BUG_ON((int32_t)room < 0);
925 if (room < len)
926 len = room;
Christopher Faulete071f0e2021-02-03 12:11:31 +0100927
Christopher Faulet69f59f42023-11-07 07:50:22 +0100928append_data:
929 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Faulete071f0e2021-02-03 12:11:31 +0100930
Christopher Faulet69f59f42023-11-07 07:50:22 +0100931 BUG_ON((int32_t)htx->tail_addr < 0);
932 BUG_ON((int32_t)htx->head_addr < 0);
933 BUG_ON(htx->end_addr > htx->tail_addr);
934 BUG_ON(htx->head_addr > htx->end_addr);
935 return (struct htx_ret){.ret = sz, .blk = tailblk};
Christopher Faulete071f0e2021-02-03 12:11:31 +0100936
Christopher Faulet69f59f42023-11-07 07:50:22 +0100937rsv_new_block:
938 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
939 if (!blk)
940 return (struct htx_ret){.ret = 0, .blk = NULL};
941 blk->info += len;
942 return (struct htx_ret){.ret = 0, .blk = blk};
Christopher Faulete071f0e2021-02-03 12:11:31 +0100943}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200944
945/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200946 * possible. It returns the number of bytes consumed from <data>, which may be
947 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200948 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200949size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200950{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200951 struct htx_blk *blk, *tailblk;
952 void *ptr;
953 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200954 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200955
Willy Tarreau0350b902019-05-28 10:58:50 +0200956 /* Not enough space to store data */
957 if (len > htx_free_data_space(htx))
958 len = htx_free_data_space(htx);
959
960 if (!len)
961 return 0;
962
Christopher Faulet28e7ba82022-01-12 14:03:42 +0100963 if (htx->head == -1)
964 goto add_new_block;
965
Willy Tarreau0350b902019-05-28 10:58:50 +0200966 /* get the tail and head block */
967 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200968 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200969 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200970 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200971
972 /* Don't try to append data if the last inserted block is not of the
973 * same type */
974 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
975 goto add_new_block;
976
977 /*
978 * Same type and enough space: append data
979 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200980 if (!htx->head_addr) {
981 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200982 goto add_new_block;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200983 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200984 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200985 else {
986 if (tailblk->addr+sz != htx->head_addr)
987 goto add_new_block;
988 room = (htx->end_addr - htx->head_addr);
989 }
990 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200991 if (room < len)
992 len = room;
993
994 append_data:
Willy Tarreau0350b902019-05-28 10:58:50 +0200995 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200996 ptr = htx_get_blk_ptr(htx, tailblk);
Willy Tarreau23aa79d2023-02-02 15:32:20 +0100997 htx_memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200998 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200999
1000 BUG_ON((int32_t)htx->tail_addr < 0);
1001 BUG_ON((int32_t)htx->head_addr < 0);
1002 BUG_ON(htx->end_addr > htx->tail_addr);
1003 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +02001004 return len;
1005
1006 add_new_block:
Willy Tarreau0350b902019-05-28 10:58:50 +02001007 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1008 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001009 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001010
1011 blk->info += len;
Willy Tarreau23aa79d2023-02-02 15:32:20 +01001012 htx_memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
Willy Tarreau0350b902019-05-28 10:58:50 +02001013 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001014}
1015
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001016
1017/* Adds an HTX block of type DATA in <htx> just after all other DATA
1018 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1019 * DATA block if possible. But, if the function succeeds, it will be the last
1020 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1021 * on success, the updated block (or the new one) is returned.
1022 */
1023struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001024{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001025 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001026
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001027 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001028 if (!blk)
1029 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001030
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001031 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001032 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1033 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001034
Christopher Faulet24ed8352018-11-22 11:20:43 +01001035 /* Swap .addr and .info fields */
1036 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1037 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1038
1039 if (blk->addr == pblk->addr)
1040 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001041 blk = pblk;
1042 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001043
Christopher Faulet24ed8352018-11-22 11:20:43 +01001044 return blk;
1045}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001046
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001047/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1048 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1049 * blocks are updated to remain valid after the move. */
1050void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1051{
1052 struct htx_blk *cblk, *pblk;
1053
1054 cblk = *blk;
1055 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1056 /* Swap .addr and .info fields */
1057 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1058 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1059
1060 if (cblk->addr == pblk->addr)
1061 cblk->addr += htx_get_blksz(pblk);
1062 if (pblk == *ref)
1063 break;
1064 cblk = pblk;
1065 }
1066 *blk = cblk;
1067 *ref = pblk;
1068}
Christopher Faulet0ea0c862020-01-23 11:47:53 +01001069
1070/* Append the HTX message <src> to the HTX message <dst>. It returns 1 on
1071 * success and 0 on error. All the message or nothing is copied. If an error
1072 * occurred, all blocks from <src> already appended to <dst> are truncated.
1073 */
1074int htx_append_msg(struct htx *dst, const struct htx *src)
1075{
1076 struct htx_blk *blk, *newblk;
1077 enum htx_blk_type type;
1078 uint32_t blksz, offset = dst->data;
1079
1080 for (blk = htx_get_head_blk(src); blk; blk = htx_get_next_blk(src, blk)) {
1081 type = htx_get_blk_type(blk);
1082
1083 if (type == HTX_BLK_UNUSED)
1084 continue;
1085
1086 blksz = htx_get_blksz(blk);
1087 newblk = htx_add_blk(dst, type, blksz);
1088 if (!newblk)
1089 goto error;
1090 newblk->info = blk->info;
Willy Tarreau23aa79d2023-02-02 15:32:20 +01001091 htx_memcpy(htx_get_blk_ptr(dst, newblk), htx_get_blk_ptr(src, blk), blksz);
Christopher Faulet0ea0c862020-01-23 11:47:53 +01001092 }
1093
1094 return 1;
1095
1096 error:
1097 htx_truncate(dst, offset);
1098 return 0;
1099}