blob: 05ba03ba9d38b27d549a1754b98850e2ac071c0a [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
Christopher Faulet159873a2021-06-09 17:30:40 +020019 * part. A temporary buffer is used to do so. This function never fails. Most of
20 * time, we need keep a ref on a specific HTX block. Thus is <blk> is set, the
21 * pointer on its new position, after defrag, is returned. In addition, if the
22 * size of the block must be altered, <blkinfo> info must be provided (!=
23 * 0). But in this case, it remains the caller responsibility to update the
24 * block content.
Christopher Fauleta3d2a162018-10-22 08:59:39 +020025 */
26/* TODO: merge data blocks into one */
Christopher Faulet159873a2021-06-09 17:30:40 +020027struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk, uint32_t blkinfo)
Christopher Fauleta3d2a162018-10-22 08:59:39 +020028{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010029 struct buffer *chunk = get_trash_chunk();
30 struct htx *tmp = htxbuf(chunk);
31 struct htx_blk *newblk, *oldblk;
Christopher Faulet200f8952019-01-02 11:23:44 +010032 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010033 uint32_t addr, blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +020034 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020035
Christopher Faulet192c6a22019-06-11 16:32:24 +020036 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010037 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020038
Christopher Faulet200f8952019-01-02 11:23:44 +010039 blkpos = -1;
40
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010041 new = 0;
42 addr = 0;
43 tmp->size = htx->size;
Christopher Faulet159873a2021-06-09 17:30:40 +020044 tmp->data = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020045
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010046 /* start from the head */
47 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
48 oldblk = htx_get_blk(htx, old);
Christopher Faulet28f29c72019-04-30 17:55:45 +020049 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010050 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020051
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010052 blksz = htx_get_blksz(oldblk);
Christopher Faulet159873a2021-06-09 17:30:40 +020053 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020054
Christopher Faulet9c66b982019-04-30 18:08:26 +020055 /* update the start-line position */
Christopher Faulet29f17582019-05-23 11:03:26 +020056 if (htx->first == old)
57 first = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010058
Christopher Faulet159873a2021-06-09 17:30:40 +020059 newblk = htx_get_blk(tmp, new);
60 newblk->addr = addr;
61 newblk->info = oldblk->info;
62
Christopher Faulet3b219722019-06-19 13:48:09 +020063 /* if <blk> is defined, save its new position */
Christopher Faulet159873a2021-06-09 17:30:40 +020064 if (blk != NULL && blk == oldblk) {
65 if (blkinfo)
66 newblk->info = blkinfo;
Christopher Faulet200f8952019-01-02 11:23:44 +010067 blkpos = new;
Christopher Faulet159873a2021-06-09 17:30:40 +020068 }
Christopher Faulet200f8952019-01-02 11:23:44 +010069
Christopher Faulet159873a2021-06-09 17:30:40 +020070 blksz = htx_get_blksz(newblk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010071 addr += blksz;
Christopher Faulet159873a2021-06-09 17:30:40 +020072 tmp->data += blksz;
73 new++;
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020074 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020075
Christopher Faulet159873a2021-06-09 17:30:40 +020076 htx->data = tmp->data;
Christopher Faulet29f17582019-05-23 11:03:26 +020077 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020078 htx->head = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +020079 htx->tail = new - 1;
80 htx->head_addr = htx->end_addr = 0;
81 htx->tail_addr = addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010082 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020083
Christopher Faulet200f8952019-01-02 11:23:44 +010084 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020085}
86
Christopher Faulet3b219722019-06-19 13:48:09 +020087/* Degragments HTX blocks of an HTX message. Payloads part is keep untouched
88 * here. This function will move back all blocks starting at the position 0,
89 * removing unused blocks. It must never be called with an empty message.
90 */
Christopher Fauletd7884d32019-06-11 10:40:43 +020091static void htx_defrag_blks(struct htx *htx)
92{
93 int32_t pos, new;
94
95 new = 0;
96 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
97 struct htx_blk *posblk, *newblk;
98
99 if (pos == new) {
100 new++;
101 continue;
102 }
103
104 posblk = htx_get_blk(htx, pos);
105 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
106 continue;
107
108 if (htx->first == pos)
109 htx->first = new;
110 newblk = htx_get_blk(htx, new++);
111 newblk->info = posblk->info;
112 newblk->addr = posblk->addr;
113 }
114 BUG_ON(!new);
115 htx->head = 0;
116 htx->tail = new - 1;
117}
118
Christopher Faulet3b219722019-06-19 13:48:09 +0200119/* Reserves a new block in the HTX message <htx> with a content of <blksz>
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200120 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
Christopher Faulet3b219722019-06-19 13:48:09 +0200121 * block is returned and the HTX message is updated. Space for this new block is
122 * reserved in the HTX message. But it is the caller responsibility to set right
123 * info in the block to reflect the stored data.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200124 */
125static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
126{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200127 struct htx_blk *blk;
Christopher Faulet192c6a22019-06-11 16:32:24 +0200128 uint32_t tail, headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200129
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100130 if (blksz > htx_free_data_space(htx))
131 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200132
Christopher Faulet192c6a22019-06-11 16:32:24 +0200133 if (htx->head == -1) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100134 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200135 htx->head = htx->tail = htx->first = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100136 blk = htx_get_blk(htx, htx->tail);
137 blk->addr = 0;
138 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200139 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100140 return blk;
141 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200142
Christopher Fauletd7884d32019-06-11 10:40:43 +0200143 /* Find the block's position. First, we try to get the next position in
144 * the message, increasing the tail by one. If this position is not
145 * available with some holes, we try to defrag the blocks without
146 * touching their paylood. If it is impossible, we fully defrag the
147 * message.
148 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200149 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200150 if (htx_pos_to_addr(htx, tail) >= htx->tail_addr)
Christopher Faulet192c6a22019-06-11 16:32:24 +0200151 ;
152 else if (htx->head > 0) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200153 htx_defrag_blks(htx);
154 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200155 BUG_ON(htx_pos_to_addr(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100156 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200157 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100158 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200159
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700160 /* Now, we have found the block's position. Try to find where to put its
Christopher Fauletd7884d32019-06-11 10:40:43 +0200161 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100162 *
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700163 * * The free space in front of the blocks table. This one is used if and
164 * only if the other one was not used yet.
Christopher Fauletd7884d32019-06-11 10:40:43 +0200165 *
166 * * The free space at the beginning of the message. Once this one is
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700167 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100168 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200169 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200170 tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200171 BUG_ON((int32_t)headroom < 0);
172 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200173
Christopher Fauletd7884d32019-06-11 10:40:43 +0200174 if (blksz <= tailroom) {
175 blk = htx_get_blk(htx, tail);
176 blk->addr = htx->tail_addr;
177 htx->tail_addr += blksz;
178 }
179 else if (blksz <= headroom) {
180 blk = htx_get_blk(htx, tail);
181 blk->addr = htx->head_addr;
182 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100183 }
184 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200185 defrag:
Christopher Faulet3b219722019-06-19 13:48:09 +0200186 /* need to defragment the message before inserting upfront */
Christopher Faulet159873a2021-06-09 17:30:40 +0200187 htx_defrag(htx, NULL, 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200188 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200189 blk = htx_get_blk(htx, tail);
190 blk->addr = htx->tail_addr;
191 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100192 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200193
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100194 htx->tail = tail;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100195 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200196 /* Set first position if not already set */
197 if (htx->first == -1)
198 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200199
200 BUG_ON((int32_t)htx->tail_addr < 0);
201 BUG_ON((int32_t)htx->head_addr < 0);
202 BUG_ON(htx->end_addr > htx->tail_addr);
203 BUG_ON(htx->head_addr > htx->end_addr);
204
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100205 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200206}
207
Christopher Fauletd7884d32019-06-11 10:40:43 +0200208/* Prepares the block to an expansion of its payload. The payload will be
209 * expanded by <delta> bytes and we need find where this expansion will be
210 * performed. It can be a compression if <delta> is negative. This function only
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500211 * updates all addresses. The caller have the responsibility to perform the
Christopher Faulet3b219722019-06-19 13:48:09 +0200212 * expansion and update the block and the HTX message accordingly. No error must
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500213 * occur. It returns following values:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200214 *
215 * 0: The expansion cannot be performed, there is not enough space.
216 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500217 * 1: the expansion must be performed in place, there is enough space after
Christopher Fauletd7884d32019-06-11 10:40:43 +0200218 * the block's payload to handle it. This is especially true if it is a
219 * compression and not an expension.
220 *
221 * 2: the block's payload must be moved at the new block address before doing
222 * the expansion.
223 *
224 * 3: the HTX message message must be defragmented
225 */
226static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
227{
228 uint32_t sz, tailroom, headroom;
229 int ret = 3;
230
Christopher Faulet192c6a22019-06-11 16:32:24 +0200231 BUG_ON(htx->head == -1);
232
Christopher Fauletd7884d32019-06-11 10:40:43 +0200233 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200234 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200235 BUG_ON((int32_t)headroom < 0);
236 BUG_ON((int32_t)tailroom < 0);
237
238 sz = htx_get_blksz(blk);
239 if (delta <= 0) {
240 /* It is a compression, it can be performed in place */
241 if (blk->addr+sz == htx->tail_addr)
242 htx->tail_addr += delta;
243 else if (blk->addr+sz == htx->head_addr)
244 htx->head_addr += delta;
245 ret = 1;
246 }
247 else if (delta > htx_free_space(htx)) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500248 /* There is not enough space to handle the expansion */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200249 ret = 0;
250 }
251 else if (blk->addr+sz == htx->tail_addr) {
252 /* The block's payload is just before the tail room */
253 if (delta < tailroom) {
254 /* Expand the block's payload */
255 htx->tail_addr += delta;
256 ret = 1;
257 }
258 else if ((sz + delta) < headroom) {
Christopher Faulet61ed7792019-07-29 10:50:28 +0200259 uint32_t oldaddr = blk->addr;
260
Christopher Fauletd7884d32019-06-11 10:40:43 +0200261 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200262 blk->addr = htx->head_addr;
263 htx->tail_addr -= sz;
264 htx->head_addr += sz + delta;
Christopher Faulet61ed7792019-07-29 10:50:28 +0200265 if (oldaddr == htx->end_addr) {
Christopher Faulet8c654862019-06-12 11:08:11 +0200266 if (htx->end_addr == htx->tail_addr) {
267 htx->tail_addr = htx->head_addr;
268 htx->head_addr = htx->end_addr = 0;
269 }
270 else
271 htx->end_addr += sz;
272 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200273 ret = 2;
274 }
275 }
276 else if (blk->addr+sz == htx->head_addr) {
277 /* The block's payload is just before the head room */
278 if (delta < headroom) {
279 /* Expand the block's payload */
280 htx->head_addr += delta;
281 ret = 1;
282 }
283 }
284 else {
285 /* The block's payload is not at the rooms edge */
286 if (!htx->head_addr && sz+delta < tailroom) {
287 /* Move the block's payload into the tailroom */
288 if (blk->addr == htx->end_addr)
289 htx->end_addr += sz;
290 blk->addr = htx->tail_addr;
291 htx->tail_addr += sz + delta;
292 ret = 2;
293 }
294 else if (sz+delta < headroom) {
295 /* Move the block's payload into the headroom */
296 if (blk->addr == htx->end_addr)
297 htx->end_addr += sz;
298 blk->addr = htx->head_addr;
299 htx->head_addr += sz + delta;
300 ret = 2;
301 }
302 }
303 /* Otherwise defrag the HTX message */
304
305 BUG_ON((int32_t)htx->tail_addr < 0);
306 BUG_ON((int32_t)htx->head_addr < 0);
307 BUG_ON(htx->end_addr > htx->tail_addr);
308 BUG_ON(htx->head_addr > htx->end_addr);
309 return ret;
310}
311
Christopher Faulet3b219722019-06-19 13:48:09 +0200312/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
313 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200314 */
315struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
316{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100317 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200318
Willy Tarreau86cb2cd2021-08-26 16:07:22 +0200319 BUG_ON(blksz >= 256 << 20);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100320 blk = htx_reserve_nxblk(htx, blksz);
321 if (!blk)
322 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200323 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200324
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100325 blk->info = (type << 28);
326 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200327}
328
Christopher Faulet3b219722019-06-19 13:48:09 +0200329/* Removes the block <blk> from the HTX message <htx>. The function returns the
330 * block following <blk> or NULL if <blk> is the last block or the last inserted
331 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200332 */
333struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
334{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200335 enum htx_blk_type type;
336 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200337
Christopher Faulet192c6a22019-06-11 16:32:24 +0200338 BUG_ON(htx->head == -1);
339
Christopher Fauletd7884d32019-06-11 10:40:43 +0200340 /* This is the last block in use */
Christopher Faulet192c6a22019-06-11 16:32:24 +0200341 if (htx->head == htx->tail) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100342 uint32_t flags = htx->flags; /* Preserve flags */
343
Christopher Fauletd7884d32019-06-11 10:40:43 +0200344 htx_reset(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100345 htx->flags |= flags;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200346 return NULL;
347 }
348
349 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200350 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200351 sz = htx_get_blksz(blk);
352 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100353 if (type != HTX_BLK_UNUSED) {
354 /* Mark the block as unused, decrement allocated size */
355 htx->data -= htx_get_blksz(blk);
356 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100357 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200358
Christopher Faulet3b219722019-06-19 13:48:09 +0200359 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200360 if (pos == htx->head) {
361 /* move the head forward */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200362 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100363 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200364 else if (pos == htx->tail) {
365 /* remove the tail. this was the last inserted block so
366 * return NULL. */
367 htx->tail--;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200368 blk = NULL;
369 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100370 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200371 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200372
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200373 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200374 if (pos == htx->first)
375 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200376
Christopher Faulet192c6a22019-06-11 16:32:24 +0200377 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200378 /* If there is just one block in the HTX message, free space can
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500379 * be adjusted. This operation could save some defrags. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200380 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
381
382 htx->head_addr = 0;
383 htx->end_addr = lastblk->addr;
384 htx->tail_addr = lastblk->addr+htx->data;
385 }
386 else {
387 if (addr+sz == htx->tail_addr)
388 htx->tail_addr = addr;
389 else if (addr+sz == htx->head_addr)
390 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200391 if (addr == htx->end_addr) {
392 if (htx->tail_addr == htx->end_addr) {
393 htx->tail_addr = htx->head_addr;
394 htx->head_addr = htx->end_addr = 0;
395 }
396 else
397 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200398 }
399 }
400
401 BUG_ON((int32_t)htx->tail_addr < 0);
402 BUG_ON((int32_t)htx->head_addr < 0);
403 BUG_ON(htx->end_addr > htx->tail_addr);
404 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100405 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200406}
407
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100408/* Looks for the HTX block containing the offset <offset>, starting at the HTX
409 * message's head. The function returns an htx_ret with the found HTX block and
410 * the position inside this block where the offset is. If the offset <offset> is
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500411 * outside of the HTX message, htx_ret.blk is set to NULL.
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100412 */
413struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset)
414{
415 struct htx_blk *blk;
416 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
417
418 if (offset >= htx->data)
419 return htxret;
420
421 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
422 uint32_t sz = htx_get_blksz(blk);
423
424 if (offset < sz)
425 break;
426 offset -= sz;
427 }
428 htxret.blk = blk;
429 htxret.ret = offset;
430 return htxret;
431}
432
Christopher Faulet3b219722019-06-19 13:48:09 +0200433/* Removes all blocks after the one containing the offset <offset>. This last
434 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100435 */
436void htx_truncate(struct htx *htx, uint32_t offset)
437{
438 struct htx_blk *blk;
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100439 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100440
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100441 blk = htxret.blk;
442 if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) {
443 htx_change_blk_value_len(htx, blk, htxret.ret);
444 blk = htx_get_next_blk(htx, blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100445 }
446 while (blk)
447 blk = htx_remove_blk(htx, blk);
448}
449
Christopher Faulet3b219722019-06-19 13:48:09 +0200450/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
451 * block, it will be cut if necessary. Others blocks will be removed at once if
452 * <count> is large enough. The function returns an htx_ret with the first block
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500453 * remaining in the message and the amount of data drained. If everything is
Christopher Faulet3b219722019-06-19 13:48:09 +0200454 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100455 */
456struct htx_ret htx_drain(struct htx *htx, uint32_t count)
457{
458 struct htx_blk *blk;
459 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
460
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200461 if (count == htx->data) {
Christopher Faulet5e9b24f2021-04-22 09:43:47 +0200462 uint32_t flags = htx->flags;
463
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200464 htx_reset(htx);
Christopher Faulet5e9b24f2021-04-22 09:43:47 +0200465 htx->flags = flags; /* restore flags */
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200466 htxret.ret = count;
467 return htxret;
468 }
469
Christopher Faulet549822f2019-02-25 10:23:19 +0100470 blk = htx_get_head_blk(htx);
471 while (count && blk) {
472 uint32_t sz = htx_get_blksz(blk);
473 enum htx_blk_type type = htx_get_blk_type(blk);
474
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500475 /* Ignore unused block */
Christopher Faulet549822f2019-02-25 10:23:19 +0100476 if (type == HTX_BLK_UNUSED)
477 goto next;
478
479 if (sz > count) {
480 if (type == HTX_BLK_DATA) {
481 htx_cut_data_blk(htx, blk, count);
482 htxret.ret += count;
483 }
484 break;
485 }
486 count -= sz;
487 htxret.ret += sz;
488 next:
489 blk = htx_remove_blk(htx, blk);
490 }
491 htxret.blk = blk;
492
493 return htxret;
494}
495
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200496/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200497 * there is enough space to take it all. If the space wraps, the buffer is
498 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200499 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200500 * returned. Due to its nature this function can be expensive and should be
501 * avoided whenever possible.
502 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200503struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200504{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200505 struct htx_blk *blk, *tailblk;
506 void *ptr;
507 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200508
Christopher Faulet192c6a22019-06-11 16:32:24 +0200509 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100510 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200511
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100512 /* Not enough space to store data */
513 if (data.len > htx_free_data_space(htx))
514 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200515
Christopher Fauletd7884d32019-06-11 10:40:43 +0200516 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200517 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200518 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200519 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200520 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200521
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100522 /* Don't try to append data if the last inserted block is not of the
523 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200524 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100525 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100527 /*
528 * Same type and enough space: append data
529 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200530 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200531 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200532 BUG_ON((int32_t)headroom < 0);
533 BUG_ON((int32_t)tailroom < 0);
534
535 len = data.len;
536 if (tailblk->addr+sz == htx->tail_addr) {
537 if (data.len <= tailroom)
538 goto append_data;
539 else if (!htx->head_addr) {
540 len = tailroom;
541 goto append_data;
542 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100543 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200544 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
545 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200546
Christopher Fauletd7884d32019-06-11 10:40:43 +0200547 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200548
549 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100550 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200551 ptr = htx_get_blk_ptr(htx, tailblk);
552 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200553 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200554
Christopher Fauletd7884d32019-06-11 10:40:43 +0200555 if (data.len == len) {
556 blk = tailblk;
557 goto end;
558 }
Tim Duesterhus154374c2021-03-02 18:57:27 +0100559 data = istadv(data, len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200560
561 add_new_block:
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200562 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100563 if (!blk)
564 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200565
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100566 blk->info += data.len;
567 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200568
569 end:
570 BUG_ON((int32_t)htx->tail_addr < 0);
571 BUG_ON((int32_t)htx->head_addr < 0);
572 BUG_ON(htx->end_addr > htx->tail_addr);
573 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200575}
576
577/* Replaces a value part of a block by a new one. The new part can be smaller or
578 * larger than the old one. This function works for any kind of block with
579 * attached data. It returns the new block on success, otherwise it returns
580 * NULL.
581 */
582struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100583 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200584{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100585 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100586 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200588
Christopher Fauletd7884d32019-06-11 10:40:43 +0200589 n = htx_get_blk_name(htx, blk);
590 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100591 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200592 ret = htx_prepare_blk_expansion(htx, blk, delta);
593 if (!ret)
594 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100595
Christopher Faulet3b219722019-06-19 13:48:09 +0200596 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 if (delta <= 0) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200598 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200599 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100600 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200601 }
602 else {
Christopher Faulet3b219722019-06-19 13:48:09 +0200603 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200604 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
605 memcpy(old.ptr, new.ptr, new.len);
606 }
Christopher Faulet159873a2021-06-09 17:30:40 +0200607
608 /* set the new block size and update HTX message */
609 htx_set_blk_value_len(blk, v.len + delta);
610 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100611 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200612 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614
Christopher Fauletd7884d32019-06-11 10:40:43 +0200615 /* Copy the name, if any */
616 memcpy(ptr, n.ptr, n.len);
617 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletd7884d32019-06-11 10:40:43 +0200619 /* Copy value before old part, if any */
620 memcpy(ptr, v.ptr, old.ptr - v.ptr);
621 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200622
Christopher Fauletd7884d32019-06-11 10:40:43 +0200623 /* Copy new value */
624 memcpy(ptr, new.ptr, new.len);
625 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200626
Christopher Fauletd7884d32019-06-11 10:40:43 +0200627 /* Copy value after old part, if any */
628 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200629
Christopher Faulet159873a2021-06-09 17:30:40 +0200630 /* set the new block size and update HTX message */
631 htx_set_blk_value_len(blk, v.len + delta);
632 htx->data += delta;
633 }
634 else { /* Do a degrag first (it is always an expansion) */
635 struct htx_blk tmpblk;
636 int32_t offset;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637
Christopher Faulet159873a2021-06-09 17:30:40 +0200638 /* use tmpblk to set new block size before defrag and to compute
639 * the offset after defrag
640 */
641 tmpblk.addr = blk->addr;
642 tmpblk.info = blk->info;
643 htx_set_blk_value_len(&tmpblk, v.len + delta);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200644
Christopher Faulet159873a2021-06-09 17:30:40 +0200645 /* htx_defrag() will take care to update the block size and the htx message */
646 blk = htx_defrag(htx, blk, tmpblk.info);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647
Christopher Faulet159873a2021-06-09 17:30:40 +0200648 /* newblk is now the new HTX block. Compute the offset to copy/move payload */
649 offset = blk->addr - tmpblk.addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200650
Christopher Faulet159873a2021-06-09 17:30:40 +0200651 /* move the end first and copy new data
652 */
653 memmove(old.ptr + offset + new.len, old.ptr + offset + old.len, (v.ptr + v.len) - (old.ptr + old.len));
654 memcpy(old.ptr + offset, new.ptr, new.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200655 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100656 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200657}
658
659/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100660 * type <mark> (typically EOH or EOT) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200661 * (including payload and meta-data). It returns the number of bytes moved and
662 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200663 */
664struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
665 enum htx_blk_type mark)
666{
667 struct htx_blk *blk, *dstblk;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200668 struct htx_blk *srcref, *dstref;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200669 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100670 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671
Christopher Faulet156852b2019-05-16 11:29:13 +0200672 ret = htx_used_space(dst);
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200673 srcref = dstref = dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200674
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200675 /* blocks are not removed yet from <src> HTX message to be able to
676 * rollback the transfer if all the headers/trailers are not copied.
677 */
678 for (blk = htx_get_head_blk(src); blk && count; blk = htx_get_next_blk(src, blk)) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200679 type = htx_get_blk_type(blk);
680
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500681 /* Ignore unused block */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200682 if (type == HTX_BLK_UNUSED)
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200683 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200684
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200685
Christopher Faulet156852b2019-05-16 11:29:13 +0200686 max = htx_get_max_blksz(dst, count);
687 if (!max)
688 break;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200689
690 sz = htx_get_blksz(blk);
691 info = blk->info;
Willy Tarreau90caa072019-04-09 16:21:54 +0200692 if (sz > max) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200693 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200694 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200695 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200696 sz = max;
697 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 }
699
700 dstblk = htx_reserve_nxblk(dst, sz);
701 if (!dstblk)
702 break;
703 dstblk->info = info;
704 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
705
Christopher Faulet156852b2019-05-16 11:29:13 +0200706 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200707 if (blk->info != info) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200708 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200710 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200711 break;
712 }
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200713
714 if (type == mark) {
715 blk = htx_get_next_blk(src, blk);
716 srcref = dstref = NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200717 break;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200718 }
719
720 /* Save <blk> to <srcref> and <dstblk> to <dstref> when we start
721 * to xfer headers or trailers. When EOH/EOT block is reached,
722 * both are reset. It is mandatory to be able to rollback a
723 * partial transfer.
724 */
725 if (!srcref && !dstref &&
726 (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_TLR)) {
727 srcref = blk;
728 dstref = dstblk;
729 }
730 else if (type == HTX_BLK_EOH || type == HTX_BLK_EOT)
731 srcref = dstref = NULL;
732 }
733
734 if (unlikely(dstref)) {
735 /* Headers or trailers part was partially xferred, so rollback the copy
736 * by removing all block between <dstref> and <dstblk>, both included.
737 */
738 while (dstref && dstref != dstblk)
739 dstref = htx_remove_blk(dst, dstref);
740 htx_remove_blk(dst, dstblk);
741
742 /* <dst> HTX message is empty, it means the headers or trailers
743 * part is too big to be copied at once.
744 */
745 if (htx_is_empty(dst))
746 src->flags |= HTX_FL_PARSING_ERROR;
747 }
748
749 /* Now, remove xferred blocks from <src> htx message */
750 if (!blk && !srcref) {
751 /* End of src reached, all blocks were consumed, drain all data */
752 htx_drain(src, src->data);
753 }
754 else {
755 /* Remove all block from the head to <blk>, or <srcref> if defined, excluded */
756 srcref = (srcref ? srcref : blk);
757 for (blk = htx_get_head_blk(src); blk && blk != srcref; blk = htx_remove_blk(src, blk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200758 }
759
Christopher Faulet156852b2019-05-16 11:29:13 +0200760 end:
761 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762 return (struct htx_ret){.ret = ret, .blk = dstblk};
763}
764
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200765/* Replaces an header by a new one. The new header can be smaller or larger than
766 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100767 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200768 */
769struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100770 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200771{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100772 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200773 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100774 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200775 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200776
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100777 type = htx_get_blk_type(blk);
778 if (type != HTX_BLK_HDR)
779 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200780
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100781 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200782 ret = htx_prepare_blk_expansion(htx, blk, delta);
783 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100784 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200785
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100786
Christopher Faulet3b219722019-06-19 13:48:09 +0200787 /* Replace in place or at a new address is the same. We replace all the
788 * header (name+value). Only take care to defrag the message if
789 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200790 if (ret == 3)
Christopher Faulet159873a2021-06-09 17:30:40 +0200791 blk = htx_defrag(htx, blk, (type << 28) + (value.len << 8) + name.len);
792 else {
793 /* Set the new block size and update HTX message */
794 blk->info = (type << 28) + (value.len << 8) + name.len;
795 htx->data += delta;
796 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200797
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500798 /* Finally, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200799 ptr = htx_get_blk_ptr(htx, blk);
800 ist2bin_lc(ptr, name);
801 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100802 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803}
804
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100805/* Replaces the parts of the start-line. It returns the new start-line on
806 * success, otherwise it returns NULL. It is the caller responsibility to update
807 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200808 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100809struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
810 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200812 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100813 struct htx_sl *sl;
814 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200815 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100816 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200817 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200818
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100819 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100820 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100821 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200822
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100823 /* Save start-line info and flags */
824 sl = htx_get_blk_ptr(htx, blk);
825 tmp.info = sl->info;
826 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100827
Christopher Fauletd7884d32019-06-11 10:40:43 +0200828 sz = htx_get_blksz(blk);
829 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
830 ret = htx_prepare_blk_expansion(htx, blk, delta);
831 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100832 return NULL; /* not enough space */
833
Christopher Faulet3b219722019-06-19 13:48:09 +0200834 /* Replace in place or at a new address is the same. We replace all the
835 * start-line. Only take care to defrag the message if necessary. */
Christopher Faulet159873a2021-06-09 17:30:40 +0200836 if (ret == 3) {
837 blk = htx_defrag(htx, blk, (type << 28) + sz + delta);
838 }
839 else {
840 /* Set the new block size and update HTX message */
841 blk->info = (type << 28) + sz + delta;
842 htx->data += delta;
843 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200844
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100845 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100846 sl = htx_get_blk_ptr(htx, blk);
847 sl->info = tmp.info;
848 sl->flags = tmp.flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200849
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100850 HTX_SL_P1_LEN(sl) = p1.len;
851 HTX_SL_P2_LEN(sl) = p2.len;
852 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100853
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100854 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
855 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
856 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200857
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100858 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200859}
860
Christopher Faulete071f0e2021-02-03 12:11:31 +0100861/* Reserves the maximum possible size for an HTX data block, by extending an
862 * existing one or by creating a now one. It returns a compound result with the
863 * HTX block and the position where new data must be inserted (0 for a new
864 * block). If an error occurs or if there is no space left, NULL is returned
865 * instead of a pointer on an HTX block.
866 */
867struct htx_ret htx_reserve_max_data(struct htx *htx)
868{
869 struct htx_blk *blk, *tailblk;
870 uint32_t sz, room;
871 int32_t len = htx_free_data_space(htx);
872
873 if (htx->head == -1)
874 goto rsv_new_block;
875
876 if (!len)
877 return (struct htx_ret){.ret = 0, .blk = NULL};
878
879 /* get the tail and head block */
880 tailblk = htx_get_tail_blk(htx);
881 if (tailblk == NULL)
882 goto rsv_new_block;
883 sz = htx_get_blksz(tailblk);
884
885 /* Don't try to append data if the last inserted block is not of the
886 * same type */
887 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
888 goto rsv_new_block;
889
890 /*
891 * Same type and enough space: append data
892 */
893 if (!htx->head_addr) {
894 if (tailblk->addr+sz != htx->tail_addr)
895 goto rsv_new_block;
896 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
897 }
898 else {
899 if (tailblk->addr+sz != htx->head_addr)
900 goto rsv_new_block;
901 room = (htx->end_addr - htx->head_addr);
902 }
903 BUG_ON((int32_t)room < 0);
904 if (room < len)
905 len = room;
906
907 append_data:
908 htx_change_blk_value_len(htx, tailblk, sz+len);
909
910 BUG_ON((int32_t)htx->tail_addr < 0);
911 BUG_ON((int32_t)htx->head_addr < 0);
912 BUG_ON(htx->end_addr > htx->tail_addr);
913 BUG_ON(htx->head_addr > htx->end_addr);
914 return (struct htx_ret){.ret = sz, .blk = tailblk};
915
916 rsv_new_block:
Christopher Faulete071f0e2021-02-03 12:11:31 +0100917 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
918 if (!blk)
919 return (struct htx_ret){.ret = 0, .blk = NULL};
920 blk->info += len;
921 return (struct htx_ret){.ret = 0, .blk = blk};
922}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200923
924/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200925 * possible. It returns the number of bytes consumed from <data>, which may be
926 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200927 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200928size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200929{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200930 struct htx_blk *blk, *tailblk;
931 void *ptr;
932 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200933 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200934
Christopher Faulet192c6a22019-06-11 16:32:24 +0200935 if (htx->head == -1)
Willy Tarreau0350b902019-05-28 10:58:50 +0200936 goto add_new_block;
937
938 /* Not enough space to store data */
939 if (len > htx_free_data_space(htx))
940 len = htx_free_data_space(htx);
941
942 if (!len)
943 return 0;
944
945 /* get the tail and head block */
946 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200947 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200948 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200949 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200950
951 /* Don't try to append data if the last inserted block is not of the
952 * same type */
953 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
954 goto add_new_block;
955
956 /*
957 * Same type and enough space: append data
958 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200959 if (!htx->head_addr) {
960 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200961 goto add_new_block;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200962 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200963 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200964 else {
965 if (tailblk->addr+sz != htx->head_addr)
966 goto add_new_block;
967 room = (htx->end_addr - htx->head_addr);
968 }
969 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200970 if (room < len)
971 len = room;
972
973 append_data:
Willy Tarreau0350b902019-05-28 10:58:50 +0200974 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200975 ptr = htx_get_blk_ptr(htx, tailblk);
976 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200977 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200978
979 BUG_ON((int32_t)htx->tail_addr < 0);
980 BUG_ON((int32_t)htx->head_addr < 0);
981 BUG_ON(htx->end_addr > htx->tail_addr);
982 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200983 return len;
984
985 add_new_block:
Willy Tarreau0350b902019-05-28 10:58:50 +0200986 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
987 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200988 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +0200989
990 blk->info += len;
991 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
992 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200993}
994
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200995
996/* Adds an HTX block of type DATA in <htx> just after all other DATA
997 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
998 * DATA block if possible. But, if the function succeeds, it will be the last
999 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1000 * on success, the updated block (or the new one) is returned.
1001 */
1002struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001003{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001004 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001005
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001006 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001007 if (!blk)
1008 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001009
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001010 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001011 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1012 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001013
Christopher Faulet24ed8352018-11-22 11:20:43 +01001014 /* Swap .addr and .info fields */
1015 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1016 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1017
1018 if (blk->addr == pblk->addr)
1019 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001020 blk = pblk;
1021 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001022
Christopher Faulet24ed8352018-11-22 11:20:43 +01001023 return blk;
1024}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001025
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001026/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1027 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1028 * blocks are updated to remain valid after the move. */
1029void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1030{
1031 struct htx_blk *cblk, *pblk;
1032
1033 cblk = *blk;
1034 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1035 /* Swap .addr and .info fields */
1036 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1037 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1038
1039 if (cblk->addr == pblk->addr)
1040 cblk->addr += htx_get_blksz(pblk);
1041 if (pblk == *ref)
1042 break;
1043 cblk = pblk;
1044 }
1045 *blk = cblk;
1046 *ref = pblk;
1047}
Christopher Faulet0ea0c862020-01-23 11:47:53 +01001048
1049/* Append the HTX message <src> to the HTX message <dst>. It returns 1 on
1050 * success and 0 on error. All the message or nothing is copied. If an error
1051 * occurred, all blocks from <src> already appended to <dst> are truncated.
1052 */
1053int htx_append_msg(struct htx *dst, const struct htx *src)
1054{
1055 struct htx_blk *blk, *newblk;
1056 enum htx_blk_type type;
1057 uint32_t blksz, offset = dst->data;
1058
1059 for (blk = htx_get_head_blk(src); blk; blk = htx_get_next_blk(src, blk)) {
1060 type = htx_get_blk_type(blk);
1061
1062 if (type == HTX_BLK_UNUSED)
1063 continue;
1064
1065 blksz = htx_get_blksz(blk);
1066 newblk = htx_add_blk(dst, type, blksz);
1067 if (!newblk)
1068 goto error;
1069 newblk->info = blk->info;
1070 memcpy(htx_get_blk_ptr(dst, newblk), htx_get_blk_ptr(src, blk), blksz);
1071 }
1072
1073 return 1;
1074
1075 error:
1076 htx_truncate(dst, offset);
1077 return 0;
1078}