blob: 28b2c47b679aced97089d1d973460eef8ecaa996 [file] [log] [blame]
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001/*
2 * internal HTTP message
3 *
4 * Copyright 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauc13ed532020-06-02 10:22:45 +020013#include <haproxy/chunk.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020014#include <haproxy/htx.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020015
Christopher Faulet192c6a22019-06-11 16:32:24 +020016struct htx htx_empty = { .size = 0, .data = 0, .head = -1, .tail = -1, .first = -1 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +020017
Christopher Faulet3b219722019-06-19 13:48:09 +020018/* Defragments an HTX message. It removes unused blocks and unwraps the payloads
19 * part. A temporary buffer is used to do so. This function never fails. if
20 * <blk> is not NULL, we replace it by the new block address, after the
21 * defragmentation. The new <blk> is returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +020022 */
23/* TODO: merge data blocks into one */
24struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk)
25{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010026 struct buffer *chunk = get_trash_chunk();
27 struct htx *tmp = htxbuf(chunk);
28 struct htx_blk *newblk, *oldblk;
Christopher Faulet200f8952019-01-02 11:23:44 +010029 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010030 uint32_t addr, blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +020031 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020032
Christopher Faulet192c6a22019-06-11 16:32:24 +020033 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010034 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020035
Christopher Faulet200f8952019-01-02 11:23:44 +010036 blkpos = -1;
37
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010038 new = 0;
39 addr = 0;
40 tmp->size = htx->size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020041
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010042 /* start from the head */
43 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
44 oldblk = htx_get_blk(htx, old);
Christopher Faulet28f29c72019-04-30 17:55:45 +020045 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010046 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020047
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010048 newblk = htx_get_blk(tmp, new);
49 newblk->addr = addr;
50 newblk->info = oldblk->info;
51 blksz = htx_get_blksz(oldblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020052
Christopher Faulet9c66b982019-04-30 18:08:26 +020053 /* update the start-line position */
Christopher Faulet29f17582019-05-23 11:03:26 +020054 if (htx->first == old)
55 first = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010056
Christopher Faulet3b219722019-06-19 13:48:09 +020057 /* if <blk> is defined, save its new position */
Christopher Faulet200f8952019-01-02 11:23:44 +010058 if (blk != NULL && blk == oldblk)
59 blkpos = new;
60
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010061 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
62 new++;
63 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020065 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020068 htx->head = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +020069 htx->tail = new - 1;
70 htx->head_addr = htx->end_addr = 0;
71 htx->tail_addr = addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010072 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020073
Christopher Faulet200f8952019-01-02 11:23:44 +010074 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020075}
76
Christopher Faulet3b219722019-06-19 13:48:09 +020077/* Degragments HTX blocks of an HTX message. Payloads part is keep untouched
78 * here. This function will move back all blocks starting at the position 0,
79 * removing unused blocks. It must never be called with an empty message.
80 */
Christopher Fauletd7884d32019-06-11 10:40:43 +020081static void htx_defrag_blks(struct htx *htx)
82{
83 int32_t pos, new;
84
85 new = 0;
86 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
87 struct htx_blk *posblk, *newblk;
88
89 if (pos == new) {
90 new++;
91 continue;
92 }
93
94 posblk = htx_get_blk(htx, pos);
95 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
96 continue;
97
98 if (htx->first == pos)
99 htx->first = new;
100 newblk = htx_get_blk(htx, new++);
101 newblk->info = posblk->info;
102 newblk->addr = posblk->addr;
103 }
104 BUG_ON(!new);
105 htx->head = 0;
106 htx->tail = new - 1;
107}
108
Christopher Faulet3b219722019-06-19 13:48:09 +0200109/* Reserves a new block in the HTX message <htx> with a content of <blksz>
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200110 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
Christopher Faulet3b219722019-06-19 13:48:09 +0200111 * block is returned and the HTX message is updated. Space for this new block is
112 * reserved in the HTX message. But it is the caller responsibility to set right
113 * info in the block to reflect the stored data.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200114 */
115static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
116{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200117 struct htx_blk *blk;
Christopher Faulet192c6a22019-06-11 16:32:24 +0200118 uint32_t tail, headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200119
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100120 if (blksz > htx_free_data_space(htx))
121 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200122
Christopher Faulet192c6a22019-06-11 16:32:24 +0200123 if (htx->head == -1) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100124 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200125 htx->head = htx->tail = htx->first = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100126 blk = htx_get_blk(htx, htx->tail);
127 blk->addr = 0;
128 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200129 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100130 return blk;
131 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200132
Christopher Fauletd7884d32019-06-11 10:40:43 +0200133 /* Find the block's position. First, we try to get the next position in
134 * the message, increasing the tail by one. If this position is not
135 * available with some holes, we try to defrag the blocks without
136 * touching their paylood. If it is impossible, we fully defrag the
137 * message.
138 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200139 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200140 if (htx_pos_to_addr(htx, tail) >= htx->tail_addr)
Christopher Faulet192c6a22019-06-11 16:32:24 +0200141 ;
142 else if (htx->head > 0) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200143 htx_defrag_blks(htx);
144 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200145 BUG_ON(htx_pos_to_addr(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100146 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200147 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100148 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200149
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700150 /* Now, we have found the block's position. Try to find where to put its
Christopher Fauletd7884d32019-06-11 10:40:43 +0200151 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 *
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700153 * * The free space in front of the blocks table. This one is used if and
154 * only if the other one was not used yet.
Christopher Fauletd7884d32019-06-11 10:40:43 +0200155 *
156 * * The free space at the beginning of the message. Once this one is
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700157 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100158 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200159 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200160 tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200161 BUG_ON((int32_t)headroom < 0);
162 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200163
Christopher Fauletd7884d32019-06-11 10:40:43 +0200164 if (blksz <= tailroom) {
165 blk = htx_get_blk(htx, tail);
166 blk->addr = htx->tail_addr;
167 htx->tail_addr += blksz;
168 }
169 else if (blksz <= headroom) {
170 blk = htx_get_blk(htx, tail);
171 blk->addr = htx->head_addr;
172 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100173 }
174 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200175 defrag:
Christopher Faulet3b219722019-06-19 13:48:09 +0200176 /* need to defragment the message before inserting upfront */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200177 htx_defrag(htx, NULL);
178 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200179 blk = htx_get_blk(htx, tail);
180 blk->addr = htx->tail_addr;
181 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100182 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200183
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100184 htx->tail = tail;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200186 /* Set first position if not already set */
187 if (htx->first == -1)
188 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200189
190 BUG_ON((int32_t)htx->tail_addr < 0);
191 BUG_ON((int32_t)htx->head_addr < 0);
192 BUG_ON(htx->end_addr > htx->tail_addr);
193 BUG_ON(htx->head_addr > htx->end_addr);
194
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100195 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200196}
197
Christopher Fauletd7884d32019-06-11 10:40:43 +0200198/* Prepares the block to an expansion of its payload. The payload will be
199 * expanded by <delta> bytes and we need find where this expansion will be
200 * performed. It can be a compression if <delta> is negative. This function only
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500201 * updates all addresses. The caller have the responsibility to perform the
Christopher Faulet3b219722019-06-19 13:48:09 +0200202 * expansion and update the block and the HTX message accordingly. No error must
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500203 * occur. It returns following values:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200204 *
205 * 0: The expansion cannot be performed, there is not enough space.
206 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500207 * 1: the expansion must be performed in place, there is enough space after
Christopher Fauletd7884d32019-06-11 10:40:43 +0200208 * the block's payload to handle it. This is especially true if it is a
209 * compression and not an expension.
210 *
211 * 2: the block's payload must be moved at the new block address before doing
212 * the expansion.
213 *
214 * 3: the HTX message message must be defragmented
215 */
216static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
217{
218 uint32_t sz, tailroom, headroom;
219 int ret = 3;
220
Christopher Faulet192c6a22019-06-11 16:32:24 +0200221 BUG_ON(htx->head == -1);
222
Christopher Fauletd7884d32019-06-11 10:40:43 +0200223 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200224 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200225 BUG_ON((int32_t)headroom < 0);
226 BUG_ON((int32_t)tailroom < 0);
227
228 sz = htx_get_blksz(blk);
229 if (delta <= 0) {
230 /* It is a compression, it can be performed in place */
231 if (blk->addr+sz == htx->tail_addr)
232 htx->tail_addr += delta;
233 else if (blk->addr+sz == htx->head_addr)
234 htx->head_addr += delta;
235 ret = 1;
236 }
237 else if (delta > htx_free_space(htx)) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500238 /* There is not enough space to handle the expansion */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200239 ret = 0;
240 }
241 else if (blk->addr+sz == htx->tail_addr) {
242 /* The block's payload is just before the tail room */
243 if (delta < tailroom) {
244 /* Expand the block's payload */
245 htx->tail_addr += delta;
246 ret = 1;
247 }
248 else if ((sz + delta) < headroom) {
Christopher Faulet61ed7792019-07-29 10:50:28 +0200249 uint32_t oldaddr = blk->addr;
250
Christopher Fauletd7884d32019-06-11 10:40:43 +0200251 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200252 blk->addr = htx->head_addr;
253 htx->tail_addr -= sz;
254 htx->head_addr += sz + delta;
Christopher Faulet61ed7792019-07-29 10:50:28 +0200255 if (oldaddr == htx->end_addr) {
Christopher Faulet8c654862019-06-12 11:08:11 +0200256 if (htx->end_addr == htx->tail_addr) {
257 htx->tail_addr = htx->head_addr;
258 htx->head_addr = htx->end_addr = 0;
259 }
260 else
261 htx->end_addr += sz;
262 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200263 ret = 2;
264 }
265 }
266 else if (blk->addr+sz == htx->head_addr) {
267 /* The block's payload is just before the head room */
268 if (delta < headroom) {
269 /* Expand the block's payload */
270 htx->head_addr += delta;
271 ret = 1;
272 }
273 }
274 else {
275 /* The block's payload is not at the rooms edge */
276 if (!htx->head_addr && sz+delta < tailroom) {
277 /* Move the block's payload into the tailroom */
278 if (blk->addr == htx->end_addr)
279 htx->end_addr += sz;
280 blk->addr = htx->tail_addr;
281 htx->tail_addr += sz + delta;
282 ret = 2;
283 }
284 else if (sz+delta < headroom) {
285 /* Move the block's payload into the headroom */
286 if (blk->addr == htx->end_addr)
287 htx->end_addr += sz;
288 blk->addr = htx->head_addr;
289 htx->head_addr += sz + delta;
290 ret = 2;
291 }
292 }
293 /* Otherwise defrag the HTX message */
294
295 BUG_ON((int32_t)htx->tail_addr < 0);
296 BUG_ON((int32_t)htx->head_addr < 0);
297 BUG_ON(htx->end_addr > htx->tail_addr);
298 BUG_ON(htx->head_addr > htx->end_addr);
299 return ret;
300}
301
Christopher Faulet3b219722019-06-19 13:48:09 +0200302/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
303 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200304 */
305struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
306{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100307 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200308
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100309 blk = htx_reserve_nxblk(htx, blksz);
310 if (!blk)
311 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200312 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200313
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100314 blk->info = (type << 28);
315 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200316}
317
Christopher Faulet3b219722019-06-19 13:48:09 +0200318/* Removes the block <blk> from the HTX message <htx>. The function returns the
319 * block following <blk> or NULL if <blk> is the last block or the last inserted
320 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200321 */
322struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
323{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200324 enum htx_blk_type type;
325 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200326
Christopher Faulet192c6a22019-06-11 16:32:24 +0200327 BUG_ON(htx->head == -1);
328
Christopher Fauletd7884d32019-06-11 10:40:43 +0200329 /* This is the last block in use */
Christopher Faulet192c6a22019-06-11 16:32:24 +0200330 if (htx->head == htx->tail) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100331 uint32_t flags = htx->flags; /* Preserve flags */
332
Christopher Fauletd7884d32019-06-11 10:40:43 +0200333 htx_reset(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100334 htx->flags |= flags;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200335 return NULL;
336 }
337
338 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200339 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200340 sz = htx_get_blksz(blk);
341 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100342 if (type != HTX_BLK_UNUSED) {
343 /* Mark the block as unused, decrement allocated size */
344 htx->data -= htx_get_blksz(blk);
345 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100346 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200347
Christopher Faulet3b219722019-06-19 13:48:09 +0200348 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200349 if (pos == htx->head) {
350 /* move the head forward */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200351 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100352 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200353 else if (pos == htx->tail) {
354 /* remove the tail. this was the last inserted block so
355 * return NULL. */
356 htx->tail--;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200357 blk = NULL;
358 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100359 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200360 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200361
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200362 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200363 if (pos == htx->first)
364 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200365
Christopher Faulet192c6a22019-06-11 16:32:24 +0200366 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200367 /* If there is just one block in the HTX message, free space can
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500368 * be adjusted. This operation could save some defrags. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200369 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
370
371 htx->head_addr = 0;
372 htx->end_addr = lastblk->addr;
373 htx->tail_addr = lastblk->addr+htx->data;
374 }
375 else {
376 if (addr+sz == htx->tail_addr)
377 htx->tail_addr = addr;
378 else if (addr+sz == htx->head_addr)
379 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200380 if (addr == htx->end_addr) {
381 if (htx->tail_addr == htx->end_addr) {
382 htx->tail_addr = htx->head_addr;
383 htx->head_addr = htx->end_addr = 0;
384 }
385 else
386 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200387 }
388 }
389
390 BUG_ON((int32_t)htx->tail_addr < 0);
391 BUG_ON((int32_t)htx->head_addr < 0);
392 BUG_ON(htx->end_addr > htx->tail_addr);
393 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100394 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200395}
396
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100397/* Looks for the HTX block containing the offset <offset>, starting at the HTX
398 * message's head. The function returns an htx_ret with the found HTX block and
399 * the position inside this block where the offset is. If the offset <offset> is
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500400 * outside of the HTX message, htx_ret.blk is set to NULL.
Christopher Faulet1cdceb92020-02-24 11:41:59 +0100401 */
402struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset)
403{
404 struct htx_blk *blk;
405 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
406
407 if (offset >= htx->data)
408 return htxret;
409
410 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
411 uint32_t sz = htx_get_blksz(blk);
412
413 if (offset < sz)
414 break;
415 offset -= sz;
416 }
417 htxret.blk = blk;
418 htxret.ret = offset;
419 return htxret;
420}
421
Christopher Faulet3b219722019-06-19 13:48:09 +0200422/* Removes all blocks after the one containing the offset <offset>. This last
423 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100424 */
425void htx_truncate(struct htx *htx, uint32_t offset)
426{
427 struct htx_blk *blk;
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100428 struct htx_ret htxret = htx_find_offset(htx, offset);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100429
Christopher Fauletbb76aa42020-02-24 15:09:24 +0100430 blk = htxret.blk;
431 if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) {
432 htx_change_blk_value_len(htx, blk, htxret.ret);
433 blk = htx_get_next_blk(htx, blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100434 }
435 while (blk)
436 blk = htx_remove_blk(htx, blk);
437}
438
Christopher Faulet3b219722019-06-19 13:48:09 +0200439/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
440 * block, it will be cut if necessary. Others blocks will be removed at once if
441 * <count> is large enough. The function returns an htx_ret with the first block
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500442 * remaining in the message and the amount of data drained. If everything is
Christopher Faulet3b219722019-06-19 13:48:09 +0200443 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100444 */
445struct htx_ret htx_drain(struct htx *htx, uint32_t count)
446{
447 struct htx_blk *blk;
448 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
449
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200450 if (count == htx->data) {
Christopher Faulet5e9b24f2021-04-22 09:43:47 +0200451 uint32_t flags = htx->flags;
452
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200453 htx_reset(htx);
Christopher Faulet5e9b24f2021-04-22 09:43:47 +0200454 htx->flags = flags; /* restore flags */
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200455 htxret.ret = count;
456 return htxret;
457 }
458
Christopher Faulet549822f2019-02-25 10:23:19 +0100459 blk = htx_get_head_blk(htx);
460 while (count && blk) {
461 uint32_t sz = htx_get_blksz(blk);
462 enum htx_blk_type type = htx_get_blk_type(blk);
463
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500464 /* Ignore unused block */
Christopher Faulet549822f2019-02-25 10:23:19 +0100465 if (type == HTX_BLK_UNUSED)
466 goto next;
467
468 if (sz > count) {
469 if (type == HTX_BLK_DATA) {
470 htx_cut_data_blk(htx, blk, count);
471 htxret.ret += count;
472 }
473 break;
474 }
475 count -= sz;
476 htxret.ret += sz;
477 next:
478 blk = htx_remove_blk(htx, blk);
479 }
480 htxret.blk = blk;
481
482 return htxret;
483}
484
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200485/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200486 * there is enough space to take it all. If the space wraps, the buffer is
487 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200488 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200489 * returned. Due to its nature this function can be expensive and should be
490 * avoided whenever possible.
491 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200492struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200493{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200494 struct htx_blk *blk, *tailblk;
495 void *ptr;
496 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200497
Christopher Faulet192c6a22019-06-11 16:32:24 +0200498 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100499 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200500
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100501 /* Not enough space to store data */
502 if (data.len > htx_free_data_space(htx))
503 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200504
Christopher Fauletd7884d32019-06-11 10:40:43 +0200505 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200506 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200507 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200508 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200509 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200510
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100511 /* Don't try to append data if the last inserted block is not of the
512 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200513 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200515
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100516 /*
517 * Same type and enough space: append data
518 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200519 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200520 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200521 BUG_ON((int32_t)headroom < 0);
522 BUG_ON((int32_t)tailroom < 0);
523
524 len = data.len;
525 if (tailblk->addr+sz == htx->tail_addr) {
526 if (data.len <= tailroom)
527 goto append_data;
528 else if (!htx->head_addr) {
529 len = tailroom;
530 goto append_data;
531 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100532 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200533 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
534 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200535
Christopher Fauletd7884d32019-06-11 10:40:43 +0200536 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200537
538 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100539 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100540 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200541 ptr = htx_get_blk_ptr(htx, tailblk);
542 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200543 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200544
Christopher Fauletd7884d32019-06-11 10:40:43 +0200545 if (data.len == len) {
546 blk = tailblk;
547 goto end;
548 }
Tim Duesterhus154374c2021-03-02 18:57:27 +0100549 data = istadv(data, len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200550
551 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200552 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200553 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100554 if (!blk)
555 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100557 blk->info += data.len;
558 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200559
560 end:
561 BUG_ON((int32_t)htx->tail_addr < 0);
562 BUG_ON((int32_t)htx->head_addr < 0);
563 BUG_ON(htx->end_addr > htx->tail_addr);
564 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100565 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566}
567
568/* Replaces a value part of a block by a new one. The new part can be smaller or
569 * larger than the old one. This function works for any kind of block with
570 * attached data. It returns the new block on success, otherwise it returns
571 * NULL.
572 */
573struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200575{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100576 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100577 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200578 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200579
Christopher Fauletd7884d32019-06-11 10:40:43 +0200580 n = htx_get_blk_name(htx, blk);
581 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100582 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200583 ret = htx_prepare_blk_expansion(htx, blk, delta);
584 if (!ret)
585 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100586
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 /* Before updating the payload, set the new block size and update HTX
588 * message */
589 htx_set_blk_value_len(blk, v.len + delta);
590 htx->data += delta;
591
Christopher Faulet3b219722019-06-19 13:48:09 +0200592 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200593 if (delta <= 0) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200594 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200595 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100596 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 }
598 else {
Christopher Faulet3b219722019-06-19 13:48:09 +0200599 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200600 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
601 memcpy(old.ptr, new.ptr, new.len);
602 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100603 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200604 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 /* Copy the name, if any */
608 memcpy(ptr, n.ptr, n.len);
609 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200610
Christopher Fauletd7884d32019-06-11 10:40:43 +0200611 /* Copy value before old part, if any */
612 memcpy(ptr, v.ptr, old.ptr - v.ptr);
613 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614
Christopher Fauletd7884d32019-06-11 10:40:43 +0200615 /* Copy new value */
616 memcpy(ptr, new.ptr, new.len);
617 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletd7884d32019-06-11 10:40:43 +0200619 /* Copy value after old part, if any */
620 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
621 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200622 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200623 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200624
Christopher Fauletd7884d32019-06-11 10:40:43 +0200625 /* Copy the header name, if any */
626 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200627
Christopher Fauletd7884d32019-06-11 10:40:43 +0200628 /* Copy value before old part, if any */
629 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200630
Christopher Fauletd7884d32019-06-11 10:40:43 +0200631 /* Copy new value */
632 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200633
Christopher Fauletd7884d32019-06-11 10:40:43 +0200634 /* Copy value after old part if any */
635 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200636
Christopher Fauletd7884d32019-06-11 10:40:43 +0200637 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200638
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500639 /* Finally, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200640 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
641 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100642 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200643}
644
645/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100646 * type <mark> (typically EOH or EOT) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200647 * (including payload and meta-data). It returns the number of bytes moved and
648 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200649 */
650struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
651 enum htx_blk_type mark)
652{
653 struct htx_blk *blk, *dstblk;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200654 struct htx_blk *srcref, *dstref;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200655 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100656 uint32_t info, max, sz, ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200657
Christopher Faulet156852b2019-05-16 11:29:13 +0200658 ret = htx_used_space(dst);
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200659 srcref = dstref = dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200660
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200661 /* blocks are not removed yet from <src> HTX message to be able to
662 * rollback the transfer if all the headers/trailers are not copied.
663 */
664 for (blk = htx_get_head_blk(src); blk && count; blk = htx_get_next_blk(src, blk)) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665 type = htx_get_blk_type(blk);
666
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500667 /* Ignore unused block */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668 if (type == HTX_BLK_UNUSED)
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200669 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200670
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671
Christopher Faulet156852b2019-05-16 11:29:13 +0200672 max = htx_get_max_blksz(dst, count);
673 if (!max)
674 break;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200675
676 sz = htx_get_blksz(blk);
677 info = blk->info;
Willy Tarreau90caa072019-04-09 16:21:54 +0200678 if (sz > max) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200679 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200680 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200681 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200682 sz = max;
683 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200684 }
685
686 dstblk = htx_reserve_nxblk(dst, sz);
687 if (!dstblk)
688 break;
689 dstblk->info = info;
690 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
691
Christopher Faulet156852b2019-05-16 11:29:13 +0200692 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200693 if (blk->info != info) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200694 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200695 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200696 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200697 break;
698 }
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200699
700 if (type == mark) {
701 blk = htx_get_next_blk(src, blk);
702 srcref = dstref = NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703 break;
Christopher Fauletc92ec0b2021-04-22 09:45:18 +0200704 }
705
706 /* Save <blk> to <srcref> and <dstblk> to <dstref> when we start
707 * to xfer headers or trailers. When EOH/EOT block is reached,
708 * both are reset. It is mandatory to be able to rollback a
709 * partial transfer.
710 */
711 if (!srcref && !dstref &&
712 (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_TLR)) {
713 srcref = blk;
714 dstref = dstblk;
715 }
716 else if (type == HTX_BLK_EOH || type == HTX_BLK_EOT)
717 srcref = dstref = NULL;
718 }
719
720 if (unlikely(dstref)) {
721 /* Headers or trailers part was partially xferred, so rollback the copy
722 * by removing all block between <dstref> and <dstblk>, both included.
723 */
724 while (dstref && dstref != dstblk)
725 dstref = htx_remove_blk(dst, dstref);
726 htx_remove_blk(dst, dstblk);
727
728 /* <dst> HTX message is empty, it means the headers or trailers
729 * part is too big to be copied at once.
730 */
731 if (htx_is_empty(dst))
732 src->flags |= HTX_FL_PARSING_ERROR;
733 }
734
735 /* Now, remove xferred blocks from <src> htx message */
736 if (!blk && !srcref) {
737 /* End of src reached, all blocks were consumed, drain all data */
738 htx_drain(src, src->data);
739 }
740 else {
741 /* Remove all block from the head to <blk>, or <srcref> if defined, excluded */
742 srcref = (srcref ? srcref : blk);
743 for (blk = htx_get_head_blk(src); blk && blk != srcref; blk = htx_remove_blk(src, blk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200744 }
745
Christopher Faulet156852b2019-05-16 11:29:13 +0200746 end:
747 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200748 return (struct htx_ret){.ret = ret, .blk = dstblk};
749}
750
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200751/* Replaces an header by a new one. The new header can be smaller or larger than
752 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100753 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200754 */
755struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100756 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200757{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100758 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200759 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100760 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200761 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100763 type = htx_get_blk_type(blk);
764 if (type != HTX_BLK_HDR)
765 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200766
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100767 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200768 ret = htx_prepare_blk_expansion(htx, blk, delta);
769 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100770 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200771
Christopher Fauletd7884d32019-06-11 10:40:43 +0200772 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200773 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100774 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100775
Christopher Faulet3b219722019-06-19 13:48:09 +0200776 /* Replace in place or at a new address is the same. We replace all the
777 * header (name+value). Only take care to defrag the message if
778 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200779 if (ret == 3)
780 blk = htx_defrag(htx, blk);
781
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500782 /* Finally, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200783 ptr = htx_get_blk_ptr(htx, blk);
784 ist2bin_lc(ptr, name);
785 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100786 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787}
788
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100789/* Replaces the parts of the start-line. It returns the new start-line on
790 * success, otherwise it returns NULL. It is the caller responsibility to update
791 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200792 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100793struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
794 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200796 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100797 struct htx_sl *sl;
798 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200799 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100800 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200801 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200802
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100803 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100804 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100805 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200806
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100807 /* Save start-line info and flags */
808 sl = htx_get_blk_ptr(htx, blk);
809 tmp.info = sl->info;
810 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100811
Christopher Fauletd7884d32019-06-11 10:40:43 +0200812 sz = htx_get_blksz(blk);
813 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
814 ret = htx_prepare_blk_expansion(htx, blk, delta);
815 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100816 return NULL; /* not enough space */
817
Christopher Fauletd7884d32019-06-11 10:40:43 +0200818 /* Set the new block size and update HTX message */
819 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100820 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821
Christopher Faulet3b219722019-06-19 13:48:09 +0200822 /* Replace in place or at a new address is the same. We replace all the
823 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200824 if (ret == 3)
825 blk = htx_defrag(htx, blk);
826
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100827 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100828 sl = htx_get_blk_ptr(htx, blk);
829 sl->info = tmp.info;
830 sl->flags = tmp.flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100832 HTX_SL_P1_LEN(sl) = p1.len;
833 HTX_SL_P2_LEN(sl) = p2.len;
834 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100835
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100836 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
837 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
838 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200839
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100840 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200841}
842
Christopher Faulete071f0e2021-02-03 12:11:31 +0100843/* Reserves the maximum possible size for an HTX data block, by extending an
844 * existing one or by creating a now one. It returns a compound result with the
845 * HTX block and the position where new data must be inserted (0 for a new
846 * block). If an error occurs or if there is no space left, NULL is returned
847 * instead of a pointer on an HTX block.
848 */
849struct htx_ret htx_reserve_max_data(struct htx *htx)
850{
851 struct htx_blk *blk, *tailblk;
852 uint32_t sz, room;
853 int32_t len = htx_free_data_space(htx);
854
855 if (htx->head == -1)
856 goto rsv_new_block;
857
858 if (!len)
859 return (struct htx_ret){.ret = 0, .blk = NULL};
860
861 /* get the tail and head block */
862 tailblk = htx_get_tail_blk(htx);
863 if (tailblk == NULL)
864 goto rsv_new_block;
865 sz = htx_get_blksz(tailblk);
866
867 /* Don't try to append data if the last inserted block is not of the
868 * same type */
869 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
870 goto rsv_new_block;
871
872 /*
873 * Same type and enough space: append data
874 */
875 if (!htx->head_addr) {
876 if (tailblk->addr+sz != htx->tail_addr)
877 goto rsv_new_block;
878 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
879 }
880 else {
881 if (tailblk->addr+sz != htx->head_addr)
882 goto rsv_new_block;
883 room = (htx->end_addr - htx->head_addr);
884 }
885 BUG_ON((int32_t)room < 0);
886 if (room < len)
887 len = room;
888
889 append_data:
890 htx_change_blk_value_len(htx, tailblk, sz+len);
891
892 BUG_ON((int32_t)htx->tail_addr < 0);
893 BUG_ON((int32_t)htx->head_addr < 0);
894 BUG_ON(htx->end_addr > htx->tail_addr);
895 BUG_ON(htx->head_addr > htx->end_addr);
896 return (struct htx_ret){.ret = sz, .blk = tailblk};
897
898 rsv_new_block:
899 /* FIXME: check data.len (< 256MB) */
900 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
901 if (!blk)
902 return (struct htx_ret){.ret = 0, .blk = NULL};
903 blk->info += len;
904 return (struct htx_ret){.ret = 0, .blk = blk};
905}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200906
907/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200908 * possible. It returns the number of bytes consumed from <data>, which may be
909 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200910 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200911size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200912{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200913 struct htx_blk *blk, *tailblk;
914 void *ptr;
915 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200916 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200917
Christopher Faulet192c6a22019-06-11 16:32:24 +0200918 if (htx->head == -1)
Willy Tarreau0350b902019-05-28 10:58:50 +0200919 goto add_new_block;
920
921 /* Not enough space to store data */
922 if (len > htx_free_data_space(htx))
923 len = htx_free_data_space(htx);
924
925 if (!len)
926 return 0;
927
928 /* get the tail and head block */
929 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200930 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200931 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200932 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200933
934 /* Don't try to append data if the last inserted block is not of the
935 * same type */
936 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
937 goto add_new_block;
938
939 /*
940 * Same type and enough space: append data
941 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200942 if (!htx->head_addr) {
943 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200944 goto add_new_block;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200945 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200946 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200947 else {
948 if (tailblk->addr+sz != htx->head_addr)
949 goto add_new_block;
950 room = (htx->end_addr - htx->head_addr);
951 }
952 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200953 if (room < len)
954 len = room;
955
956 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200957 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200958 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200959 ptr = htx_get_blk_ptr(htx, tailblk);
960 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200961 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200962
963 BUG_ON((int32_t)htx->tail_addr < 0);
964 BUG_ON((int32_t)htx->head_addr < 0);
965 BUG_ON(htx->end_addr > htx->tail_addr);
966 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200967 return len;
968
969 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200970 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +0200971 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
972 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200973 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +0200974
975 blk->info += len;
976 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
977 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200978}
979
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200980
981/* Adds an HTX block of type DATA in <htx> just after all other DATA
982 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
983 * DATA block if possible. But, if the function succeeds, it will be the last
984 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
985 * on success, the updated block (or the new one) is returned.
986 */
987struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +0100988{
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200989 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100990
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200991 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100992 if (!blk)
993 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100994
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200995 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200996 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
997 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100998
Christopher Faulet24ed8352018-11-22 11:20:43 +0100999 /* Swap .addr and .info fields */
1000 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1001 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1002
1003 if (blk->addr == pblk->addr)
1004 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001005 blk = pblk;
1006 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001007
Christopher Faulet24ed8352018-11-22 11:20:43 +01001008 return blk;
1009}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001010
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001011/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1012 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1013 * blocks are updated to remain valid after the move. */
1014void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1015{
1016 struct htx_blk *cblk, *pblk;
1017
1018 cblk = *blk;
1019 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1020 /* Swap .addr and .info fields */
1021 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1022 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1023
1024 if (cblk->addr == pblk->addr)
1025 cblk->addr += htx_get_blksz(pblk);
1026 if (pblk == *ref)
1027 break;
1028 cblk = pblk;
1029 }
1030 *blk = cblk;
1031 *ref = pblk;
1032}
Christopher Faulet0ea0c862020-01-23 11:47:53 +01001033
1034/* Append the HTX message <src> to the HTX message <dst>. It returns 1 on
1035 * success and 0 on error. All the message or nothing is copied. If an error
1036 * occurred, all blocks from <src> already appended to <dst> are truncated.
1037 */
1038int htx_append_msg(struct htx *dst, const struct htx *src)
1039{
1040 struct htx_blk *blk, *newblk;
1041 enum htx_blk_type type;
1042 uint32_t blksz, offset = dst->data;
1043
1044 for (blk = htx_get_head_blk(src); blk; blk = htx_get_next_blk(src, blk)) {
1045 type = htx_get_blk_type(blk);
1046
1047 if (type == HTX_BLK_UNUSED)
1048 continue;
1049
1050 blksz = htx_get_blksz(blk);
1051 newblk = htx_add_blk(dst, type, blksz);
1052 if (!newblk)
1053 goto error;
1054 newblk->info = blk->info;
1055 memcpy(htx_get_blk_ptr(dst, newblk), htx_get_blk_ptr(src, blk), blksz);
1056 }
1057
1058 return 1;
1059
1060 error:
1061 htx_truncate(dst, offset);
1062 return 0;
1063}