blob: 93acbaaae54df94794533c3e7fc0f75e25553fac [file] [log] [blame]
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001/*
2 * internal HTTP message
3 *
4 * Copyright 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/chunk.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010014#include <common/htx.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020015
Christopher Faulet192c6a22019-06-11 16:32:24 +020016struct htx htx_empty = { .size = 0, .data = 0, .head = -1, .tail = -1, .first = -1 };
Christopher Fauleta3d2a162018-10-22 08:59:39 +020017
Christopher Faulet3b219722019-06-19 13:48:09 +020018/* Defragments an HTX message. It removes unused blocks and unwraps the payloads
19 * part. A temporary buffer is used to do so. This function never fails. if
20 * <blk> is not NULL, we replace it by the new block address, after the
21 * defragmentation. The new <blk> is returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +020022 */
23/* TODO: merge data blocks into one */
24struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk)
25{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010026 struct buffer *chunk = get_trash_chunk();
27 struct htx *tmp = htxbuf(chunk);
28 struct htx_blk *newblk, *oldblk;
Christopher Faulet200f8952019-01-02 11:23:44 +010029 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010030 uint32_t addr, blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +020031 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020032
Christopher Faulet192c6a22019-06-11 16:32:24 +020033 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010034 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020035
Christopher Faulet200f8952019-01-02 11:23:44 +010036 blkpos = -1;
37
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010038 new = 0;
39 addr = 0;
40 tmp->size = htx->size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020041
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010042 /* start from the head */
43 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
44 oldblk = htx_get_blk(htx, old);
Christopher Faulet28f29c72019-04-30 17:55:45 +020045 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010046 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020047
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010048 newblk = htx_get_blk(tmp, new);
49 newblk->addr = addr;
50 newblk->info = oldblk->info;
51 blksz = htx_get_blksz(oldblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020052
Christopher Faulet9c66b982019-04-30 18:08:26 +020053 /* update the start-line position */
Christopher Faulet29f17582019-05-23 11:03:26 +020054 if (htx->first == old)
55 first = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010056
Christopher Faulet3b219722019-06-19 13:48:09 +020057 /* if <blk> is defined, save its new position */
Christopher Faulet200f8952019-01-02 11:23:44 +010058 if (blk != NULL && blk == oldblk)
59 blkpos = new;
60
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010061 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
62 new++;
63 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020065 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020068 htx->head = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +020069 htx->tail = new - 1;
70 htx->head_addr = htx->end_addr = 0;
71 htx->tail_addr = addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010072 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020073
Christopher Faulet200f8952019-01-02 11:23:44 +010074 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020075}
76
Christopher Faulet3b219722019-06-19 13:48:09 +020077/* Degragments HTX blocks of an HTX message. Payloads part is keep untouched
78 * here. This function will move back all blocks starting at the position 0,
79 * removing unused blocks. It must never be called with an empty message.
80 */
Christopher Fauletd7884d32019-06-11 10:40:43 +020081static void htx_defrag_blks(struct htx *htx)
82{
83 int32_t pos, new;
84
85 new = 0;
86 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
87 struct htx_blk *posblk, *newblk;
88
89 if (pos == new) {
90 new++;
91 continue;
92 }
93
94 posblk = htx_get_blk(htx, pos);
95 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
96 continue;
97
98 if (htx->first == pos)
99 htx->first = new;
100 newblk = htx_get_blk(htx, new++);
101 newblk->info = posblk->info;
102 newblk->addr = posblk->addr;
103 }
104 BUG_ON(!new);
105 htx->head = 0;
106 htx->tail = new - 1;
107}
108
Christopher Faulet3b219722019-06-19 13:48:09 +0200109/* Reserves a new block in the HTX message <htx> with a content of <blksz>
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200110 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
Christopher Faulet3b219722019-06-19 13:48:09 +0200111 * block is returned and the HTX message is updated. Space for this new block is
112 * reserved in the HTX message. But it is the caller responsibility to set right
113 * info in the block to reflect the stored data.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200114 */
115static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
116{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200117 struct htx_blk *blk;
Christopher Faulet192c6a22019-06-11 16:32:24 +0200118 uint32_t tail, headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200119
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100120 if (blksz > htx_free_data_space(htx))
121 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200122
Christopher Faulet192c6a22019-06-11 16:32:24 +0200123 if (htx->head == -1) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100124 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200125 htx->head = htx->tail = htx->first = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100126 blk = htx_get_blk(htx, htx->tail);
127 blk->addr = 0;
128 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200129 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100130 return blk;
131 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200132
Christopher Fauletd7884d32019-06-11 10:40:43 +0200133 /* Find the block's position. First, we try to get the next position in
134 * the message, increasing the tail by one. If this position is not
135 * available with some holes, we try to defrag the blocks without
136 * touching their paylood. If it is impossible, we fully defrag the
137 * message.
138 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200139 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200140 if (htx_pos_to_addr(htx, tail) >= htx->tail_addr)
Christopher Faulet192c6a22019-06-11 16:32:24 +0200141 ;
142 else if (htx->head > 0) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200143 htx_defrag_blks(htx);
144 tail = htx->tail + 1;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200145 BUG_ON(htx_pos_to_addr(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100146 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200147 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100148 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200149
Christopher Fauletd7884d32019-06-11 10:40:43 +0200150 /* Now, we have found the block's position. Try do find where to put its
151 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 *
Christopher Fauletd7884d32019-06-11 10:40:43 +0200153 * * The free space in front of the blocks table. This one is used iff
154 * the other one was not used yet.
155 *
156 * * The free space at the beginning of the message. Once this one is
157 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100158 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200159 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200160 tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200161 BUG_ON((int32_t)headroom < 0);
162 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200163
Christopher Fauletd7884d32019-06-11 10:40:43 +0200164 if (blksz <= tailroom) {
165 blk = htx_get_blk(htx, tail);
166 blk->addr = htx->tail_addr;
167 htx->tail_addr += blksz;
168 }
169 else if (blksz <= headroom) {
170 blk = htx_get_blk(htx, tail);
171 blk->addr = htx->head_addr;
172 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100173 }
174 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200175 defrag:
Christopher Faulet3b219722019-06-19 13:48:09 +0200176 /* need to defragment the message before inserting upfront */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200177 htx_defrag(htx, NULL);
178 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200179 blk = htx_get_blk(htx, tail);
180 blk->addr = htx->tail_addr;
181 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100182 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200183
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100184 htx->tail = tail;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200186 /* Set first position if not already set */
187 if (htx->first == -1)
188 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200189
190 BUG_ON((int32_t)htx->tail_addr < 0);
191 BUG_ON((int32_t)htx->head_addr < 0);
192 BUG_ON(htx->end_addr > htx->tail_addr);
193 BUG_ON(htx->head_addr > htx->end_addr);
194
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100195 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200196}
197
Christopher Fauletd7884d32019-06-11 10:40:43 +0200198/* Prepares the block to an expansion of its payload. The payload will be
199 * expanded by <delta> bytes and we need find where this expansion will be
200 * performed. It can be a compression if <delta> is negative. This function only
201 * updates all addresses. The caller have the responsibility to performe the
Christopher Faulet3b219722019-06-19 13:48:09 +0200202 * expansion and update the block and the HTX message accordingly. No error must
203 * occurr. It returns following values:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200204 *
205 * 0: The expansion cannot be performed, there is not enough space.
206 *
Christopher Faulet3b219722019-06-19 13:48:09 +0200207 * 1: the expansion must be performed in place, there is enougth space after
Christopher Fauletd7884d32019-06-11 10:40:43 +0200208 * the block's payload to handle it. This is especially true if it is a
209 * compression and not an expension.
210 *
211 * 2: the block's payload must be moved at the new block address before doing
212 * the expansion.
213 *
214 * 3: the HTX message message must be defragmented
215 */
216static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
217{
218 uint32_t sz, tailroom, headroom;
219 int ret = 3;
220
Christopher Faulet192c6a22019-06-11 16:32:24 +0200221 BUG_ON(htx->head == -1);
222
Christopher Fauletd7884d32019-06-11 10:40:43 +0200223 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200224 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200225 BUG_ON((int32_t)headroom < 0);
226 BUG_ON((int32_t)tailroom < 0);
227
228 sz = htx_get_blksz(blk);
229 if (delta <= 0) {
230 /* It is a compression, it can be performed in place */
231 if (blk->addr+sz == htx->tail_addr)
232 htx->tail_addr += delta;
233 else if (blk->addr+sz == htx->head_addr)
234 htx->head_addr += delta;
235 ret = 1;
236 }
237 else if (delta > htx_free_space(htx)) {
238 /* There is not enought space to handle the expansion */
239 ret = 0;
240 }
241 else if (blk->addr+sz == htx->tail_addr) {
242 /* The block's payload is just before the tail room */
243 if (delta < tailroom) {
244 /* Expand the block's payload */
245 htx->tail_addr += delta;
246 ret = 1;
247 }
248 else if ((sz + delta) < headroom) {
249 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200250 blk->addr = htx->head_addr;
251 htx->tail_addr -= sz;
252 htx->head_addr += sz + delta;
Christopher Faulet8c654862019-06-12 11:08:11 +0200253 if (blk->addr == htx->end_addr) {
254 if (htx->end_addr == htx->tail_addr) {
255 htx->tail_addr = htx->head_addr;
256 htx->head_addr = htx->end_addr = 0;
257 }
258 else
259 htx->end_addr += sz;
260 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200261 ret = 2;
262 }
263 }
264 else if (blk->addr+sz == htx->head_addr) {
265 /* The block's payload is just before the head room */
266 if (delta < headroom) {
267 /* Expand the block's payload */
268 htx->head_addr += delta;
269 ret = 1;
270 }
271 }
272 else {
273 /* The block's payload is not at the rooms edge */
274 if (!htx->head_addr && sz+delta < tailroom) {
275 /* Move the block's payload into the tailroom */
276 if (blk->addr == htx->end_addr)
277 htx->end_addr += sz;
278 blk->addr = htx->tail_addr;
279 htx->tail_addr += sz + delta;
280 ret = 2;
281 }
282 else if (sz+delta < headroom) {
283 /* Move the block's payload into the headroom */
284 if (blk->addr == htx->end_addr)
285 htx->end_addr += sz;
286 blk->addr = htx->head_addr;
287 htx->head_addr += sz + delta;
288 ret = 2;
289 }
290 }
291 /* Otherwise defrag the HTX message */
292
293 BUG_ON((int32_t)htx->tail_addr < 0);
294 BUG_ON((int32_t)htx->head_addr < 0);
295 BUG_ON(htx->end_addr > htx->tail_addr);
296 BUG_ON(htx->head_addr > htx->end_addr);
297 return ret;
298}
299
Christopher Faulet3b219722019-06-19 13:48:09 +0200300/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
301 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200302 */
303struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
304{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100305 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200306
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100307 blk = htx_reserve_nxblk(htx, blksz);
308 if (!blk)
309 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200310 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200311
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100312 blk->info = (type << 28);
313 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200314}
315
Christopher Faulet3b219722019-06-19 13:48:09 +0200316/* Removes the block <blk> from the HTX message <htx>. The function returns the
317 * block following <blk> or NULL if <blk> is the last block or the last inserted
318 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200319 */
320struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
321{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200322 enum htx_blk_type type;
323 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200324
Christopher Faulet192c6a22019-06-11 16:32:24 +0200325 BUG_ON(htx->head == -1);
326
Christopher Fauletd7884d32019-06-11 10:40:43 +0200327 /* This is the last block in use */
Christopher Faulet192c6a22019-06-11 16:32:24 +0200328 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200329 htx_reset(htx);
330 return NULL;
331 }
332
333 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200334 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200335 sz = htx_get_blksz(blk);
336 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100337 if (type != HTX_BLK_UNUSED) {
338 /* Mark the block as unused, decrement allocated size */
339 htx->data -= htx_get_blksz(blk);
340 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100341 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200342
Christopher Faulet3b219722019-06-19 13:48:09 +0200343 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200344 if (pos == htx->head) {
345 /* move the head forward */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200346 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100347 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200348 else if (pos == htx->tail) {
349 /* remove the tail. this was the last inserted block so
350 * return NULL. */
351 htx->tail--;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200352 blk = NULL;
353 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100354 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200355 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200356
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200357 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200358 if (pos == htx->first)
359 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200360
Christopher Faulet192c6a22019-06-11 16:32:24 +0200361 if (htx->head == htx->tail) {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200362 /* If there is just one block in the HTX message, free space can
363 * be ajusted. This operation could save some defrags. */
364 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
365
366 htx->head_addr = 0;
367 htx->end_addr = lastblk->addr;
368 htx->tail_addr = lastblk->addr+htx->data;
369 }
370 else {
371 if (addr+sz == htx->tail_addr)
372 htx->tail_addr = addr;
373 else if (addr+sz == htx->head_addr)
374 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200375 if (addr == htx->end_addr) {
376 if (htx->tail_addr == htx->end_addr) {
377 htx->tail_addr = htx->head_addr;
378 htx->head_addr = htx->end_addr = 0;
379 }
380 else
381 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200382 }
383 }
384
385 BUG_ON((int32_t)htx->tail_addr < 0);
386 BUG_ON((int32_t)htx->head_addr < 0);
387 BUG_ON(htx->end_addr > htx->tail_addr);
388 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100389 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200390}
391
Christopher Faulet3b219722019-06-19 13:48:09 +0200392/* Removes all blocks after the one containing the offset <offset>. This last
393 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100394 */
395void htx_truncate(struct htx *htx, uint32_t offset)
396{
397 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100398
Christopher Fauletced39002019-05-23 11:12:43 +0200399 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100400 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200401 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100402
Christopher Fauletced39002019-05-23 11:12:43 +0200403 if (offset >= sz) {
404 offset -= sz;
405 continue;
406 }
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200407 if (type == HTX_BLK_DATA)
408 htx_change_blk_value_len(htx, blk, offset);
Christopher Fauletced39002019-05-23 11:12:43 +0200409 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100410 }
411 while (blk)
412 blk = htx_remove_blk(htx, blk);
413}
414
Christopher Faulet3b219722019-06-19 13:48:09 +0200415/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
416 * block, it will be cut if necessary. Others blocks will be removed at once if
417 * <count> is large enough. The function returns an htx_ret with the first block
418 * remaing in the messsage and the amount of data drained. If everything is
419 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100420 */
421struct htx_ret htx_drain(struct htx *htx, uint32_t count)
422{
423 struct htx_blk *blk;
424 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
425
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200426 if (count == htx->data) {
427 htx_reset(htx);
428 htxret.ret = count;
429 return htxret;
430 }
431
Christopher Faulet549822f2019-02-25 10:23:19 +0100432 blk = htx_get_head_blk(htx);
433 while (count && blk) {
434 uint32_t sz = htx_get_blksz(blk);
435 enum htx_blk_type type = htx_get_blk_type(blk);
436
437 /* Ingore unused block */
438 if (type == HTX_BLK_UNUSED)
439 goto next;
440
441 if (sz > count) {
442 if (type == HTX_BLK_DATA) {
443 htx_cut_data_blk(htx, blk, count);
444 htxret.ret += count;
445 }
446 break;
447 }
448 count -= sz;
449 htxret.ret += sz;
450 next:
451 blk = htx_remove_blk(htx, blk);
452 }
453 htxret.blk = blk;
454
455 return htxret;
456}
457
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200458/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200459 * there is enough space to take it all. If the space wraps, the buffer is
460 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200461 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200462 * returned. Due to its nature this function can be expensive and should be
463 * avoided whenever possible.
464 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200465struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200466{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200467 struct htx_blk *blk, *tailblk;
468 void *ptr;
469 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200470
Christopher Faulet192c6a22019-06-11 16:32:24 +0200471 if (htx->head == -1)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100472 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200473
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100474 /* Not enough space to store data */
475 if (data.len > htx_free_data_space(htx))
476 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200477
Christopher Fauletd7884d32019-06-11 10:40:43 +0200478 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200479 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200480 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200481 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200482 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200483
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100484 /* Don't try to append data if the last inserted block is not of the
485 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200486 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100487 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200488
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 /*
490 * Same type and enough space: append data
491 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200492 headroom = (htx->end_addr - htx->head_addr);
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200493 tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200494 BUG_ON((int32_t)headroom < 0);
495 BUG_ON((int32_t)tailroom < 0);
496
497 len = data.len;
498 if (tailblk->addr+sz == htx->tail_addr) {
499 if (data.len <= tailroom)
500 goto append_data;
501 else if (!htx->head_addr) {
502 len = tailroom;
503 goto append_data;
504 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100505 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200506 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
507 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200508
Christopher Fauletd7884d32019-06-11 10:40:43 +0200509 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200510
511 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100512 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200514 ptr = htx_get_blk_ptr(htx, tailblk);
515 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200516 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200517
Christopher Fauletd7884d32019-06-11 10:40:43 +0200518 if (data.len == len) {
519 blk = tailblk;
520 goto end;
521 }
522 data.ptr += len;
523 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524
525 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200526 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200527 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100528 if (!blk)
529 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200530
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100531 blk->info += data.len;
532 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200533
534 end:
535 BUG_ON((int32_t)htx->tail_addr < 0);
536 BUG_ON((int32_t)htx->head_addr < 0);
537 BUG_ON(htx->end_addr > htx->tail_addr);
538 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100539 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200540}
541
542/* Replaces a value part of a block by a new one. The new part can be smaller or
543 * larger than the old one. This function works for any kind of block with
544 * attached data. It returns the new block on success, otherwise it returns
545 * NULL.
546 */
547struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100548 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200549{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100550 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100551 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200552 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200553
Christopher Fauletd7884d32019-06-11 10:40:43 +0200554 n = htx_get_blk_name(htx, blk);
555 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100556 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200557 ret = htx_prepare_blk_expansion(htx, blk, delta);
558 if (!ret)
559 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100560
Christopher Fauletd7884d32019-06-11 10:40:43 +0200561 /* Before updating the payload, set the new block size and update HTX
562 * message */
563 htx_set_blk_value_len(blk, v.len + delta);
564 htx->data += delta;
565
Christopher Faulet3b219722019-06-19 13:48:09 +0200566 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200567 if (delta <= 0) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200568 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200569 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100570 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200571 }
572 else {
Christopher Faulet3b219722019-06-19 13:48:09 +0200573 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200574 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
575 memcpy(old.ptr, new.ptr, new.len);
576 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100577 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200578 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200579 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200580
Christopher Fauletd7884d32019-06-11 10:40:43 +0200581 /* Copy the name, if any */
582 memcpy(ptr, n.ptr, n.len);
583 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200584
Christopher Fauletd7884d32019-06-11 10:40:43 +0200585 /* Copy value before old part, if any */
586 memcpy(ptr, v.ptr, old.ptr - v.ptr);
587 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200588
Christopher Fauletd7884d32019-06-11 10:40:43 +0200589 /* Copy new value */
590 memcpy(ptr, new.ptr, new.len);
591 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200592
Christopher Fauletd7884d32019-06-11 10:40:43 +0200593 /* Copy value after old part, if any */
594 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
595 }
Christopher Faulet3b219722019-06-19 13:48:09 +0200596 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200598
Christopher Fauletd7884d32019-06-11 10:40:43 +0200599 /* Copy the header name, if any */
600 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200601
Christopher Fauletd7884d32019-06-11 10:40:43 +0200602 /* Copy value before old part, if any */
603 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 /* Copy new value */
606 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200607
Christopher Fauletd7884d32019-06-11 10:40:43 +0200608 /* Copy value after old part if any */
609 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200610
Christopher Fauletd7884d32019-06-11 10:40:43 +0200611 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 /* Finaly, copy data. */
614 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
615 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100616 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617}
618
619/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200620 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200621 * (including payload and meta-data). It returns the number of bytes moved and
622 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200623 */
624struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
625 enum htx_blk_type mark)
626{
627 struct htx_blk *blk, *dstblk;
628 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100629 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200630 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631
Christopher Faulet156852b2019-05-16 11:29:13 +0200632 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200633 blk = htx_get_blk(src, htx_get_head(src));
634 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200635
636 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637 type = htx_get_blk_type(blk);
638
639 /* Ingore unused block */
640 if (type == HTX_BLK_UNUSED)
641 goto next;
642
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200643 /* Be sure to have enough space to xfer all headers/trailers in
644 * one time. If not while <dst> is empty, we report a parsing
645 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200646 */
647 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
648 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
649
650 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
651 if (htx_is_empty(dst))
652 src->flags |= HTX_FL_PARSING_ERROR;
653 break;
654 }
655 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200656 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
657 !inside_trailers && mark >= HTX_BLK_EOT) {
658 inside_trailers = 1;
659 if (htx_used_space(src) > count) {
660 if (htx_is_empty(dst))
661 src->flags |= HTX_FL_PARSING_ERROR;
662 break;
663 }
664 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200665
Christopher Faulet156852b2019-05-16 11:29:13 +0200666 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200667 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200668 max = htx_get_max_blksz(dst, count);
669 if (!max)
670 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200671 if (sz > max) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200672 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200673 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200674 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200675 sz = max;
676 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200677 }
678
679 dstblk = htx_reserve_nxblk(dst, sz);
680 if (!dstblk)
681 break;
682 dstblk->info = info;
683 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
684
Christopher Faulet156852b2019-05-16 11:29:13 +0200685 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200686 if (blk->info != info) {
Christopher Faulet3b219722019-06-19 13:48:09 +0200687 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200688 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200689 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200690 break;
691 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692 next:
693 blk = htx_remove_blk(src, blk);
694 if (type == mark)
695 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200696 }
697
Christopher Faulet156852b2019-05-16 11:29:13 +0200698 end:
699 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700 return (struct htx_ret){.ret = ret, .blk = dstblk};
701}
702
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703/* Replaces an header by a new one. The new header can be smaller or larger than
704 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100705 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200706 */
707struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100708 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100710 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200711 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100712 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200713 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200714
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100715 type = htx_get_blk_type(blk);
716 if (type != HTX_BLK_HDR)
717 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200718
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100719 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200720 ret = htx_prepare_blk_expansion(htx, blk, delta);
721 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100722 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200723
Christopher Fauletd7884d32019-06-11 10:40:43 +0200724 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200725 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100726 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100727
Christopher Faulet3b219722019-06-19 13:48:09 +0200728 /* Replace in place or at a new address is the same. We replace all the
729 * header (name+value). Only take care to defrag the message if
730 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200731 if (ret == 3)
732 blk = htx_defrag(htx, blk);
733
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100734 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200735 ptr = htx_get_blk_ptr(htx, blk);
736 ist2bin_lc(ptr, name);
737 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100738 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200739}
740
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100741/* Replaces the parts of the start-line. It returns the new start-line on
742 * success, otherwise it returns NULL. It is the caller responsibility to update
743 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200744 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100745struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
746 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200748 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100749 struct htx_sl *sl;
750 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200751 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100752 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200753 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200754
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100755 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100756 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100757 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200758
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100759 /* Save start-line info and flags */
760 sl = htx_get_blk_ptr(htx, blk);
761 tmp.info = sl->info;
762 tmp.flags = sl->flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200763 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100764
Christopher Fauletd7884d32019-06-11 10:40:43 +0200765 sz = htx_get_blksz(blk);
766 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
767 ret = htx_prepare_blk_expansion(htx, blk, delta);
768 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100769 return NULL; /* not enough space */
770
Christopher Fauletd7884d32019-06-11 10:40:43 +0200771 /* Set the new block size and update HTX message */
772 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100773 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200774
Christopher Faulet3b219722019-06-19 13:48:09 +0200775 /* Replace in place or at a new address is the same. We replace all the
776 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200777 if (ret == 3)
778 blk = htx_defrag(htx, blk);
779
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100780 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100781 sl = htx_get_blk_ptr(htx, blk);
782 sl->info = tmp.info;
783 sl->flags = tmp.flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200784 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200785
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100786 HTX_SL_P1_LEN(sl) = p1.len;
787 HTX_SL_P2_LEN(sl) = p2.len;
788 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100789
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100790 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
791 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
792 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200793
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100794 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200795}
796
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100797/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
798 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200799 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100800struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
801 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200802{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100803 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100804 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805 uint32_t size;
806
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100807 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
808 return NULL;
809
810 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100812 /* FIXME: check size (< 256MB) */
813 blk = htx_add_blk(htx, type, size);
814 if (!blk)
815 return NULL;
816 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100818 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200819 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100820 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100822 HTX_SL_P1_LEN(sl) = p1.len;
823 HTX_SL_P2_LEN(sl) = p2.len;
824 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200825
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
827 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
828 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
829
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100830 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831}
832
833/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100834 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835 */
836struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100837 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100839 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200840
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100841 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
842 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
843 if (!blk)
844 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200845
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100846 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100847 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100848 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
849 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200850}
851
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200852/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet3b219722019-06-19 13:48:09 +0200853 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200854 */
855struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
856 const struct ist value)
857{
858 struct htx_blk *blk;
859
860 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
861 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
862 if (!blk)
863 return NULL;
864
865 blk->info += (value.len << 8) + name.len;
866 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
867 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
868 return blk;
869}
870
Christopher Faulet3b219722019-06-19 13:48:09 +0200871/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
872 * the EOH. On sucess, it returns the last block inserted (the EOH), otherwise
873 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200874struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
875{
876 int i;
877
878 for (i = 0; hdrs[i].n.len; i++) {
879 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
880 return NULL;
881 }
882 return htx_add_endof(htx, HTX_BLK_EOH);
883}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200884
Christopher Faulet3b219722019-06-19 13:48:09 +0200885/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
886 * the EOT. On sucess, it returns the last block inserted (the EOT), otherwise
887 * NULL is returned. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200888struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
889{
890 int i;
891
892 for (i = 0; hdrs[i].n.len; i++) {
893 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
894 return NULL;
895 }
896 return htx_add_endof(htx, HTX_BLK_EOT);
897}
898
Christopher Faulet3b219722019-06-19 13:48:09 +0200899/* Adds an HTX block of type EOH, EOT, or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200900 * on success. Otherwise, it returns NULL.
901 */
902struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
903{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100904 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200905
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100906 blk = htx_add_blk(htx, type, 1);
907 if (!blk)
908 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200909
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100910 blk->info += 1;
911 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200912}
913
914
915/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200916 * possible. It returns the number of bytes consumed from <data>, which may be
917 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200918 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200919size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200920{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200921 struct htx_blk *blk, *tailblk;
922 void *ptr;
923 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200924 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200925
Christopher Faulet192c6a22019-06-11 16:32:24 +0200926 if (htx->head == -1)
Willy Tarreau0350b902019-05-28 10:58:50 +0200927 goto add_new_block;
928
929 /* Not enough space to store data */
930 if (len > htx_free_data_space(htx))
931 len = htx_free_data_space(htx);
932
933 if (!len)
934 return 0;
935
936 /* get the tail and head block */
937 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200938 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200939 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200940 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200941
942 /* Don't try to append data if the last inserted block is not of the
943 * same type */
944 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
945 goto add_new_block;
946
947 /*
948 * Same type and enough space: append data
949 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200950 if (!htx->head_addr) {
951 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200952 goto add_new_block;
Christopher Faulet2bf43f02019-06-12 11:28:11 +0200953 room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200954 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200955 else {
956 if (tailblk->addr+sz != htx->head_addr)
957 goto add_new_block;
958 room = (htx->end_addr - htx->head_addr);
959 }
960 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200961 if (room < len)
962 len = room;
963
964 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200965 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200966 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200967 ptr = htx_get_blk_ptr(htx, tailblk);
968 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200969 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200970
971 BUG_ON((int32_t)htx->tail_addr < 0);
972 BUG_ON((int32_t)htx->head_addr < 0);
973 BUG_ON(htx->end_addr > htx->tail_addr);
974 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200975 return len;
976
977 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200978 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +0200979 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
980 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200981 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +0200982
983 blk->info += len;
984 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
985 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200986}
987
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200988
989/* Adds an HTX block of type DATA in <htx> just after all other DATA
990 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
991 * DATA block if possible. But, if the function succeeds, it will be the last
992 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
993 * on success, the updated block (or the new one) is returned.
994 */
995struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +0100996{
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200997 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100998
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200999 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001000 if (!blk)
1001 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001002
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001003 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001004 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1005 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001006
Christopher Faulet24ed8352018-11-22 11:20:43 +01001007 /* Swap .addr and .info fields */
1008 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1009 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1010
1011 if (blk->addr == pblk->addr)
1012 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001013 blk = pblk;
1014 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001015
Christopher Faulet24ed8352018-11-22 11:20:43 +01001016 return blk;
1017}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001018
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001019/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1020 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1021 * blocks are updated to remain valid after the move. */
1022void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1023{
1024 struct htx_blk *cblk, *pblk;
1025
1026 cblk = *blk;
1027 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1028 /* Swap .addr and .info fields */
1029 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1030 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1031
1032 if (cblk->addr == pblk->addr)
1033 cblk->addr += htx_get_blksz(pblk);
1034 if (pblk == *ref)
1035 break;
1036 cblk = pblk;
1037 }
1038 *blk = cblk;
1039 *ref = pblk;
1040}
1041
Christopher Faulet3b219722019-06-19 13:48:09 +02001042/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
1043 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001044 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001045int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001046{
Christopher Faulet570d1612018-11-26 11:13:57 +01001047 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001048 return 0;
1049
Christopher Faulet570d1612018-11-26 11:13:57 +01001050 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001051 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001052 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001053 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +01001054 if (sl->flags & HTX_SL_F_VER_11)
1055 chunk_memcat(chk, "HTTP/1.1", 8);
1056 else
1057 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1058
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001059 chunk_memcat(chk, "\r\n", 2);
1060
1061 return 1;
1062}
1063
Christopher Faulet3b219722019-06-19 13:48:09 +02001064/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
1065 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001066 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001067int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001068{
Christopher Faulet570d1612018-11-26 11:13:57 +01001069 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001070 return 0;
1071
Christopher Faulet1e7af462018-12-03 14:05:01 +01001072 if (sl->flags & HTX_SL_F_VER_11)
1073 chunk_memcat(chk, "HTTP/1.1", 8);
1074 else
1075 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001076 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001077 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001078 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001079 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001080 chunk_memcat(chk, "\r\n", 2);
1081
1082 return 1;
1083}
1084
Christopher Faulet3b219722019-06-19 13:48:09 +02001085/* Appends the H1 representation of the header <n> witht the value <v> to the
1086 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1087 * returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001088 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001089int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001090{
1091 if (n.len + v.len + 4 > b_room(chk))
1092 return 0;
1093
1094 chunk_memcat(chk, n.ptr, n.len);
1095 chunk_memcat(chk, ": ", 2);
1096 chunk_memcat(chk, v.ptr, v.len);
1097 chunk_memcat(chk, "\r\n", 2);
1098
1099 return 1;
1100}
1101
Christopher Faulet3b219722019-06-19 13:48:09 +02001102/* Appends the H1 representation of the data <data> to the chunk <chk>. If
1103 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
1104 * data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001105 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001106int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001107{
1108 if (chunked) {
1109 uint32_t chksz;
1110 char tmp[10];
1111 char *beg, *end;
1112
1113 chksz = data.len;
1114
1115 beg = end = tmp+10;
1116 *--beg = '\n';
1117 *--beg = '\r';
1118 do {
1119 *--beg = hextab[chksz & 0xF];
1120 } while (chksz >>= 4);
1121
1122 if (data.len + (end - beg) + 2 > b_room(chk))
1123 return 0;
1124 chunk_memcat(chk, beg, end - beg);
1125 chunk_memcat(chk, data.ptr, data.len);
1126 chunk_memcat(chk, "\r\n", 2);
1127 }
1128 else {
1129 if (!chunk_memcat(chk, data.ptr, data.len))
1130 return 0;
1131 }
1132
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001133 return 1;
1134}