blob: bfd136f460cf07df172acb882fd56569118d0f47 [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
16struct htx htx_empty = { .size = 0, .data = 0, .used = 0 };
17
18/* Defragments an HTTP message, removing unused blocks and unwrapping blocks and
19 * their contents. A temporary message is used to do so. This function never
20 * fails. if <blk> is not NULL, we replace it by the new block address, after
21 * the defragmentation. The new <blk> is returned.
22 */
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 Fauletaa75b3d2018-12-05 16:20:40 +010033 if (!htx->used)
34 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 Faulet200f8952019-01-02 11:23:44 +010057 /* if <blk> is defined, set its new position */
58 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 Faulet28f29c72019-04-30 17:55:45 +020067 htx->used = new;
Christopher Faulet29f17582019-05-23 11:03:26 +020068 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020069 htx->head = 0;
Christopher Fauletd7884d32019-06-11 10:40:43 +020070 htx->tail = new - 1;
71 htx->head_addr = htx->end_addr = 0;
72 htx->tail_addr = addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010073 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020074
Christopher Faulet200f8952019-01-02 11:23:44 +010075 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020076}
77
Christopher Fauletd7884d32019-06-11 10:40:43 +020078static void htx_defrag_blks(struct htx *htx)
79{
80 int32_t pos, new;
81
82 new = 0;
83 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
84 struct htx_blk *posblk, *newblk;
85
86 if (pos == new) {
87 new++;
88 continue;
89 }
90
91 posblk = htx_get_blk(htx, pos);
92 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
93 continue;
94
95 if (htx->first == pos)
96 htx->first = new;
97 newblk = htx_get_blk(htx, new++);
98 newblk->info = posblk->info;
99 newblk->addr = posblk->addr;
100 }
101 BUG_ON(!new);
102 htx->head = 0;
103 htx->tail = new - 1;
104}
105
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200106/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
107 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
108 * block is returned and the HTTP message is updated. Space for this new block
109 * is reserved in the HTTP message. But it is the caller responsibility to set
110 * right info in the block to reflect the stored data.
111 */
112static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
113{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200114 struct htx_blk *blk;
115 uint32_t used, tail;
116 uint32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200117
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100118 if (blksz > htx_free_data_space(htx))
119 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200120
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100121 if (!htx->used) {
122 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200123 htx->head = htx->tail = htx->first = 0;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200124 htx->used = 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100125 blk = htx_get_blk(htx, htx->tail);
126 blk->addr = 0;
127 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200128 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100129 return blk;
130 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200131
Christopher Fauletd7884d32019-06-11 10:40:43 +0200132 /* Find the block's position. First, we try to get the next position in
133 * the message, increasing the tail by one. If this position is not
134 * available with some holes, we try to defrag the blocks without
135 * touching their paylood. If it is impossible, we fully defrag the
136 * message.
137 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200138 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200139 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) >= htx->tail_addr)
140 used = htx->used + 1;
141 else if (tail > htx->used) {
142 htx_defrag_blks(htx);
143 tail = htx->tail + 1;
144 used = htx->used + 1;
145 BUG_ON(sizeof(htx->blocks[0]) * htx_pos_to_idx(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);
160 tailroom = (!htx->head_addr
161 ? sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) - htx->tail_addr
162 : 0);
163 BUG_ON((int32_t)headroom < 0);
164 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200165
Christopher Fauletd7884d32019-06-11 10:40:43 +0200166 if (blksz <= tailroom) {
167 blk = htx_get_blk(htx, tail);
168 blk->addr = htx->tail_addr;
169 htx->tail_addr += blksz;
170 }
171 else if (blksz <= headroom) {
172 blk = htx_get_blk(htx, tail);
173 blk->addr = htx->head_addr;
174 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100175 }
176 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200177 defrag:
178 /* need to defragment the table before inserting upfront */
179 htx_defrag(htx, NULL);
180 tail = htx->tail + 1;
181 used = htx->used + 1;
182 blk = htx_get_blk(htx, tail);
183 blk->addr = htx->tail_addr;
184 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200186
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100187 htx->tail = tail;
188 htx->used = used;
189 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200190 /* Set first position if not already set */
191 if (htx->first == -1)
192 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200193
194 BUG_ON((int32_t)htx->tail_addr < 0);
195 BUG_ON((int32_t)htx->head_addr < 0);
196 BUG_ON(htx->end_addr > htx->tail_addr);
197 BUG_ON(htx->head_addr > htx->end_addr);
198
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100199 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200200}
201
Christopher Fauletd7884d32019-06-11 10:40:43 +0200202/* Prepares the block to an expansion of its payload. The payload will be
203 * expanded by <delta> bytes and we need find where this expansion will be
204 * performed. It can be a compression if <delta> is negative. This function only
205 * updates all addresses. The caller have the responsibility to performe the
206 * expansion and update the block and the HTX message accordingly. No error msut
207 * occurre. It returns following values:
208 *
209 * 0: The expansion cannot be performed, there is not enough space.
210 *
211 * 1: the expansion must be performed in place, there is enought space after
212 * the block's payload to handle it. This is especially true if it is a
213 * compression and not an expension.
214 *
215 * 2: the block's payload must be moved at the new block address before doing
216 * the expansion.
217 *
218 * 3: the HTX message message must be defragmented
219 */
220static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
221{
222 uint32_t sz, tailroom, headroom;
223 int ret = 3;
224
225 headroom = (htx->end_addr - htx->head_addr);
226 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
227 BUG_ON((int32_t)headroom < 0);
228 BUG_ON((int32_t)tailroom < 0);
229
230 sz = htx_get_blksz(blk);
231 if (delta <= 0) {
232 /* It is a compression, it can be performed in place */
233 if (blk->addr+sz == htx->tail_addr)
234 htx->tail_addr += delta;
235 else if (blk->addr+sz == htx->head_addr)
236 htx->head_addr += delta;
237 ret = 1;
238 }
239 else if (delta > htx_free_space(htx)) {
240 /* There is not enought space to handle the expansion */
241 ret = 0;
242 }
243 else if (blk->addr+sz == htx->tail_addr) {
244 /* The block's payload is just before the tail room */
245 if (delta < tailroom) {
246 /* Expand the block's payload */
247 htx->tail_addr += delta;
248 ret = 1;
249 }
250 else if ((sz + delta) < headroom) {
251 /* 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 Faulet8c654862019-06-12 11:08:11 +0200255 if (blk->addr == htx->end_addr) {
256 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 Fauleta3d2a162018-10-22 08:59:39 +0200302/* Adds a new block of type <type> in the HTTP message <htx>. Its content size
303 * is passed but it is the caller responsibility to do the copy.
304 */
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
318/* Removes the block <blk> from the HTTP message <htx>. The function returns the
319 * block following <blk> or NULL if <blk> is the last block or the last
320 * inserted one.
321 */
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 Fauletd7884d32019-06-11 10:40:43 +0200327 /* This is the last block in use */
328 if (htx->used == 1) {
329 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 Fauletaa75b3d2018-12-05 16:20:40 +0100343 /* 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 */
346 htx->used--;
347 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100348 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200349 else if (pos == htx->tail) {
350 /* remove the tail. this was the last inserted block so
351 * return NULL. */
352 htx->tail--;
353 htx->used--;
354 blk = NULL;
355 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100356 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200357 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200358
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200359 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200360 if (pos == htx->first)
361 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200362
363 if (htx->used == 1) {
364 /* If there is just one block in the HTX message, free space can
365 * be ajusted. This operation could save some defrags. */
366 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
367
368 htx->head_addr = 0;
369 htx->end_addr = lastblk->addr;
370 htx->tail_addr = lastblk->addr+htx->data;
371 }
372 else {
373 if (addr+sz == htx->tail_addr)
374 htx->tail_addr = addr;
375 else if (addr+sz == htx->head_addr)
376 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200377 if (addr == htx->end_addr) {
378 if (htx->tail_addr == htx->end_addr) {
379 htx->tail_addr = htx->head_addr;
380 htx->head_addr = htx->end_addr = 0;
381 }
382 else
383 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200384 }
385 }
386
387 BUG_ON((int32_t)htx->tail_addr < 0);
388 BUG_ON((int32_t)htx->head_addr < 0);
389 BUG_ON(htx->end_addr > htx->tail_addr);
390 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100391 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200392}
393
Christopher Faulet00cf6972019-01-07 14:53:27 +0100394/* Truncate all blocks after the one containing the offset <offset>. This last
395 * one is truncated too.
396 */
397void htx_truncate(struct htx *htx, uint32_t offset)
398{
399 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100400
Christopher Fauletced39002019-05-23 11:12:43 +0200401 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100402 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200403 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100404
Christopher Fauletced39002019-05-23 11:12:43 +0200405 if (offset >= sz) {
406 offset -= sz;
407 continue;
408 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200409 if (type == HTX_BLK_DATA) {
Christopher Fauletced39002019-05-23 11:12:43 +0200410 htx_set_blk_value_len(blk, offset);
411 htx->data -= (sz - offset);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200412
413 if (blk->addr+sz == htx->tail_addr)
414 htx->tail_addr -= offset;
415 else if (blk->addr+sz == htx->head_addr)
416 htx->head_addr -= offset;
Christopher Fauletced39002019-05-23 11:12:43 +0200417 }
418 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100419 }
420 while (blk)
421 blk = htx_remove_blk(htx, blk);
422}
423
Christopher Faulet549822f2019-02-25 10:23:19 +0100424/* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if
425 * necessary. Others blocks will be removed at once if <count> is large
426 * enough. The function returns an htx_ret with the first block remaing in the
427 * messsage and the amount of data drained. If everything is removed,
428 * htx_ret.blk is set to NULL.
429 */
430struct htx_ret htx_drain(struct htx *htx, uint32_t count)
431{
432 struct htx_blk *blk;
433 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
434
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200435 if (count == htx->data) {
436 htx_reset(htx);
437 htxret.ret = count;
438 return htxret;
439 }
440
Christopher Faulet549822f2019-02-25 10:23:19 +0100441 blk = htx_get_head_blk(htx);
442 while (count && blk) {
443 uint32_t sz = htx_get_blksz(blk);
444 enum htx_blk_type type = htx_get_blk_type(blk);
445
446 /* Ingore unused block */
447 if (type == HTX_BLK_UNUSED)
448 goto next;
449
450 if (sz > count) {
451 if (type == HTX_BLK_DATA) {
452 htx_cut_data_blk(htx, blk, count);
453 htxret.ret += count;
454 }
455 break;
456 }
457 count -= sz;
458 htxret.ret += sz;
459 next:
460 blk = htx_remove_blk(htx, blk);
461 }
462 htxret.blk = blk;
463
464 return htxret;
465}
466
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200467/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200468 * there is enough space to take it all. If the space wraps, the buffer is
469 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200470 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200471 * returned. Due to its nature this function can be expensive and should be
472 * avoided whenever possible.
473 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200474struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200475{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200476 struct htx_blk *blk, *tailblk;
477 void *ptr;
478 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200479
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100480 if (!htx->used)
481 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200482
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100483 /* Not enough space to store data */
484 if (data.len > htx_free_data_space(htx))
485 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200486
Christopher Fauletd7884d32019-06-11 10:40:43 +0200487 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200488 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200489 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200490 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200491 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200492
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100493 /* Don't try to append data if the last inserted block is not of the
494 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200495 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100496 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200497
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100498 /*
499 * Same type and enough space: append data
500 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200501 headroom = (htx->end_addr - htx->head_addr);
502 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
503 BUG_ON((int32_t)headroom < 0);
504 BUG_ON((int32_t)tailroom < 0);
505
506 len = data.len;
507 if (tailblk->addr+sz == htx->tail_addr) {
508 if (data.len <= tailroom)
509 goto append_data;
510 else if (!htx->head_addr) {
511 len = tailroom;
512 goto append_data;
513 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200515 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
516 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200517
Christopher Fauletd7884d32019-06-11 10:40:43 +0200518 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200519
520 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100521 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100522 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200523 ptr = htx_get_blk_ptr(htx, tailblk);
524 memcpy(ptr+sz, data.ptr, len);
525 htx_set_blk_value_len(tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100527 /* Update HTTP message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200528 htx->data += len;
529 if (tailblk->addr+sz == htx->tail_addr)
530 htx->tail_addr += len;
531 else if (tailblk->addr+sz == htx->head_addr)
532 htx->head_addr += len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200533
Christopher Fauletd7884d32019-06-11 10:40:43 +0200534 if (data.len == len) {
535 blk = tailblk;
536 goto end;
537 }
538 data.ptr += len;
539 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200540
541 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200542 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200543 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100544 if (!blk)
545 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200546
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100547 blk->info += data.len;
548 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200549
550 end:
551 BUG_ON((int32_t)htx->tail_addr < 0);
552 BUG_ON((int32_t)htx->head_addr < 0);
553 BUG_ON(htx->end_addr > htx->tail_addr);
554 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100555 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200556}
557
558/* Replaces a value part of a block by a new one. The new part can be smaller or
559 * larger than the old one. This function works for any kind of block with
560 * attached data. It returns the new block on success, otherwise it returns
561 * NULL.
562 */
563struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100564 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200565{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100566 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100567 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200568 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200569
Christopher Fauletd7884d32019-06-11 10:40:43 +0200570 n = htx_get_blk_name(htx, blk);
571 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100572 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200573 ret = htx_prepare_blk_expansion(htx, blk, delta);
574 if (!ret)
575 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100576
Christopher Fauletd7884d32019-06-11 10:40:43 +0200577 /* Before updating the payload, set the new block size and update HTX
578 * message */
579 htx_set_blk_value_len(blk, v.len + delta);
580 htx->data += delta;
581
582 if (ret == 1) {
583 if (delta <= 0) {
584 /* compression: copy new data first */
585 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100586 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 }
588 else {
589 /* expansion: move the end first */
590 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
591 memcpy(old.ptr, new.ptr, new.len);
592 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100593 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200594 else if (ret == 2) {
595 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200596
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 /* Copy the name, if any */
598 memcpy(ptr, n.ptr, n.len);
599 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletd7884d32019-06-11 10:40:43 +0200601 /* Copy value before old part, if any */
602 memcpy(ptr, v.ptr, old.ptr - v.ptr);
603 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 memcpy(ptr, new.ptr, new.len);
607 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608
Christopher Fauletd7884d32019-06-11 10:40:43 +0200609 /* Copy value after old part, if any */
610 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
611 }
612 else {
613 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614
Christopher Fauletd7884d32019-06-11 10:40:43 +0200615 /* Copy the header name, if any */
616 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617
Christopher Fauletd7884d32019-06-11 10:40:43 +0200618 /* Copy value before old part, if any */
619 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620
Christopher Fauletd7884d32019-06-11 10:40:43 +0200621 /* Copy new value */
622 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200623
Christopher Fauletd7884d32019-06-11 10:40:43 +0200624 /* Copy value after old part if any */
625 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200626
Christopher Fauletd7884d32019-06-11 10:40:43 +0200627 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200628
Christopher Fauletd7884d32019-06-11 10:40:43 +0200629 /* Finaly, copy data. */
630 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
631 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100632 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200633}
634
635/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200636 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200637 * (including payload and meta-data). It returns the number of bytes moved and
638 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639 */
640struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
641 enum htx_blk_type mark)
642{
643 struct htx_blk *blk, *dstblk;
644 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100645 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200646 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200647
Christopher Faulet156852b2019-05-16 11:29:13 +0200648 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200649 blk = htx_get_blk(src, htx_get_head(src));
650 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200651
652 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200653 type = htx_get_blk_type(blk);
654
655 /* Ingore unused block */
656 if (type == HTX_BLK_UNUSED)
657 goto next;
658
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200659 /* Be sure to have enough space to xfer all headers/trailers in
660 * one time. If not while <dst> is empty, we report a parsing
661 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200662 */
663 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
664 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
665
666 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
667 if (htx_is_empty(dst))
668 src->flags |= HTX_FL_PARSING_ERROR;
669 break;
670 }
671 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200672 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
673 !inside_trailers && mark >= HTX_BLK_EOT) {
674 inside_trailers = 1;
675 if (htx_used_space(src) > count) {
676 if (htx_is_empty(dst))
677 src->flags |= HTX_FL_PARSING_ERROR;
678 break;
679 }
680 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200681
Christopher Faulet156852b2019-05-16 11:29:13 +0200682 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200683 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200684 max = htx_get_max_blksz(dst, count);
685 if (!max)
686 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200687 if (sz > max) {
Christopher Faulet39744f72019-05-24 14:54:00 +0200688 /* Headers must be fully copied */
Christopher Faulet156852b2019-05-16 11:29:13 +0200689 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200690 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200691 sz = max;
692 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200693 }
694
695 dstblk = htx_reserve_nxblk(dst, sz);
696 if (!dstblk)
697 break;
698 dstblk->info = info;
699 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
700
Christopher Faulet156852b2019-05-16 11:29:13 +0200701 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200702 if (blk->info != info) {
703 /* Partial move: don't remove <blk> from <src> but
704 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200705 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200706 break;
707 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200708 next:
709 blk = htx_remove_blk(src, blk);
710 if (type == mark)
711 break;
712
713 }
714
Christopher Faulet156852b2019-05-16 11:29:13 +0200715 end:
716 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200717 return (struct htx_ret){.ret = ret, .blk = dstblk};
718}
719
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720/* Replaces an header by a new one. The new header can be smaller or larger than
721 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100722 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200723 */
724struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100725 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200726{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100727 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200728 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100729 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200730 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200731
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100732 type = htx_get_blk_type(blk);
733 if (type != HTX_BLK_HDR)
734 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200735
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100736 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200737 ret = htx_prepare_blk_expansion(htx, blk, delta);
738 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100739 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200740
Christopher Fauletd7884d32019-06-11 10:40:43 +0200741 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200742 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100743 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100744
Christopher Fauletd7884d32019-06-11 10:40:43 +0200745 if (ret == 3)
746 blk = htx_defrag(htx, blk);
747
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100748 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200749 ptr = htx_get_blk_ptr(htx, blk);
750 ist2bin_lc(ptr, name);
751 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100752 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753}
754
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100755/* Replaces the parts of the start-line. It returns the new start-line on
756 * success, otherwise it returns NULL. It is the caller responsibility to update
757 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200758 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100759struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
760 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200761{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200762 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100763 struct htx_sl *sl;
764 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200765 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100766 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200767 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200768
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100769 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100770 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100771 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100773 /* Save start-line info and flags */
774 sl = htx_get_blk_ptr(htx, blk);
775 tmp.info = sl->info;
776 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100777
Christopher Fauletd7884d32019-06-11 10:40:43 +0200778 sz = htx_get_blksz(blk);
779 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
780 ret = htx_prepare_blk_expansion(htx, blk, delta);
781 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100782 return NULL; /* not enough space */
783
Christopher Fauletd7884d32019-06-11 10:40:43 +0200784 /* Set the new block size and update HTX message */
785 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100786 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787
Christopher Fauletd7884d32019-06-11 10:40:43 +0200788 if (ret == 3)
789 blk = htx_defrag(htx, blk);
790
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100791 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100792 sl = htx_get_blk_ptr(htx, blk);
793 sl->info = tmp.info;
794 sl->flags = tmp.flags;
Christopher Faulet05c083c2019-05-15 14:56:47 +0200795 if (sl->hdrs_bytes != -1)
796 sl->hdrs_bytes += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200797
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100798 HTX_SL_P1_LEN(sl) = p1.len;
799 HTX_SL_P2_LEN(sl) = p2.len;
800 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100801
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100802 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
803 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
804 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100806 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807}
808
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100809/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
810 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100812struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
813 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200814{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100815 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100816 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817 uint32_t size;
818
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100819 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
820 return NULL;
821
822 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200823
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100824 /* FIXME: check size (< 256MB) */
825 blk = htx_add_blk(htx, type, size);
826 if (!blk)
827 return NULL;
828 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100830 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200831 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100832 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100834 HTX_SL_P1_LEN(sl) = p1.len;
835 HTX_SL_P2_LEN(sl) = p2.len;
836 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100838 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
839 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
840 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
841
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100842 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200843}
844
845/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100846 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200847 */
848struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100849 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200850{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100851 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200852
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100853 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
854 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
855 if (!blk)
856 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200857
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100858 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100859 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100860 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
861 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200862}
863
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200864/* Adds an HTX block of type TLR in <htx>. It returns the new block on
865 * success. Otherwise, it returns NULL. The header name is always lower cased.
866 */
867struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
868 const struct ist value)
869{
870 struct htx_blk *blk;
871
872 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
873 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
874 if (!blk)
875 return NULL;
876
877 blk->info += (value.len << 8) + name.len;
878 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
879 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
880 return blk;
881}
882
Willy Tarreau52610e92019-01-03 18:26:17 +0100883/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
884 * new block on success. Otherwise, it returns NULL. The caller is responsible
885 * for filling the block itself.
886 */
887struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
888{
889 struct htx_blk *blk;
890
891 blk = htx_add_blk(htx, type, blksz);
892 if (!blk)
893 return NULL;
894
895 blk->info += blksz;
896 return blk;
897}
898
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200899struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
900{
901 int i;
902
903 for (i = 0; hdrs[i].n.len; i++) {
904 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
905 return NULL;
906 }
907 return htx_add_endof(htx, HTX_BLK_EOH);
908}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200909
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200910struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
911{
912 int i;
913
914 for (i = 0; hdrs[i].n.len; i++) {
915 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
916 return NULL;
917 }
918 return htx_add_endof(htx, HTX_BLK_EOT);
919}
920
Christopher Faulet54b5e212019-06-04 10:08:28 +0200921/* Adds an HTX block of type EOH or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200922 * on success. Otherwise, it returns NULL.
923 */
924struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
925{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100926 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200927
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100928 blk = htx_add_blk(htx, type, 1);
929 if (!blk)
930 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200931
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100932 blk->info += 1;
933 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200934}
935
936
937/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200938 * possible. It returns the number of bytes consumed from <data>, which may be
939 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200940 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200941size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200942{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200943 struct htx_blk *blk, *tailblk;
944 void *ptr;
945 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200946 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200947
Willy Tarreau0350b902019-05-28 10:58:50 +0200948 if (!htx->used)
949 goto add_new_block;
950
951 /* Not enough space to store data */
952 if (len > htx_free_data_space(htx))
953 len = htx_free_data_space(htx);
954
955 if (!len)
956 return 0;
957
958 /* get the tail and head block */
959 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200960 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200961 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200962 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200963
964 /* Don't try to append data if the last inserted block is not of the
965 * same type */
966 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
967 goto add_new_block;
968
969 /*
970 * Same type and enough space: append data
971 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200972 if (!htx->head_addr) {
973 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200974 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200975 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
Willy Tarreau0350b902019-05-28 10:58:50 +0200976 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200977 else {
978 if (tailblk->addr+sz != htx->head_addr)
979 goto add_new_block;
980 room = (htx->end_addr - htx->head_addr);
981 }
982 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200983 if (room < len)
984 len = room;
985
986 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200987 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200988 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200989 ptr = htx_get_blk_ptr(htx, tailblk);
990 memcpy(ptr + sz, data.ptr, len);
991 htx_set_blk_value_len(tailblk, sz + len);
Willy Tarreau0350b902019-05-28 10:58:50 +0200992
993 /* Update HTTP message */
994 htx->data += len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200995 if (tailblk->addr+sz == htx->tail_addr)
996 htx->tail_addr += len;
997 else if (tailblk->addr+sz == htx->head_addr)
998 htx->head_addr += len;
999
1000 BUG_ON((int32_t)htx->tail_addr < 0);
1001 BUG_ON((int32_t)htx->head_addr < 0);
1002 BUG_ON(htx->end_addr > htx->tail_addr);
1003 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +02001004 return len;
1005
1006 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001007 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001008 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1009 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001010 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001011
1012 blk->info += len;
1013 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1014 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001015}
1016
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001017
1018/* Adds an HTX block of type DATA in <htx> just after all other DATA
1019 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1020 * DATA block if possible. But, if the function succeeds, it will be the last
1021 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1022 * on success, the updated block (or the new one) is returned.
1023 */
1024struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001025{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001026 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001027
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001028 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001029 if (!blk)
1030 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001031
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001032 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001033 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1034 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001035
Christopher Faulet24ed8352018-11-22 11:20:43 +01001036 /* Swap .addr and .info fields */
1037 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1038 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1039
1040 if (blk->addr == pblk->addr)
1041 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001042 blk = pblk;
1043 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001044
Christopher Faulet24ed8352018-11-22 11:20:43 +01001045 return blk;
1046}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001047
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001048/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1049 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1050 * blocks are updated to remain valid after the move. */
1051void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1052{
1053 struct htx_blk *cblk, *pblk;
1054
1055 cblk = *blk;
1056 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1057 /* Swap .addr and .info fields */
1058 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1059 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1060
1061 if (cblk->addr == pblk->addr)
1062 cblk->addr += htx_get_blksz(pblk);
1063 if (pblk == *ref)
1064 break;
1065 cblk = pblk;
1066 }
1067 *blk = cblk;
1068 *ref = pblk;
1069}
1070
Christopher Fauletc59ff232018-12-03 13:58:44 +01001071/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001072 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1073 * returns 0.
1074 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001075int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001076{
Christopher Faulet570d1612018-11-26 11:13:57 +01001077 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001078 return 0;
1079
Christopher Faulet570d1612018-11-26 11:13:57 +01001080 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001081 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001082 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001083 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +01001084 if (sl->flags & HTX_SL_F_VER_11)
1085 chunk_memcat(chk, "HTTP/1.1", 8);
1086 else
1087 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1088
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001089 chunk_memcat(chk, "\r\n", 2);
1090
1091 return 1;
1092}
1093
Christopher Fauletc59ff232018-12-03 13:58:44 +01001094/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001095 * <chk>. It returns 1 if data are successfully appended, otherwise it
1096 * returns 0.
1097 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001098int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001099{
Christopher Faulet570d1612018-11-26 11:13:57 +01001100 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001101 return 0;
1102
Christopher Faulet1e7af462018-12-03 14:05:01 +01001103 if (sl->flags & HTX_SL_F_VER_11)
1104 chunk_memcat(chk, "HTTP/1.1", 8);
1105 else
1106 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001107 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001108 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001109 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001110 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001111 chunk_memcat(chk, "\r\n", 2);
1112
1113 return 1;
1114}
1115
Christopher Fauletc59ff232018-12-03 13:58:44 +01001116/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001117 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
1118 * 0.
1119 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001120int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001121{
1122 if (n.len + v.len + 4 > b_room(chk))
1123 return 0;
1124
1125 chunk_memcat(chk, n.ptr, n.len);
1126 chunk_memcat(chk, ": ", 2);
1127 chunk_memcat(chk, v.ptr, v.len);
1128 chunk_memcat(chk, "\r\n", 2);
1129
1130 return 1;
1131}
1132
Christopher Fauletc59ff232018-12-03 13:58:44 +01001133/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001134 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
1135 * returns 1 if data are successfully appended, otherwise it returns 0.
1136 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001137int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001138{
1139 if (chunked) {
1140 uint32_t chksz;
1141 char tmp[10];
1142 char *beg, *end;
1143
1144 chksz = data.len;
1145
1146 beg = end = tmp+10;
1147 *--beg = '\n';
1148 *--beg = '\r';
1149 do {
1150 *--beg = hextab[chksz & 0xF];
1151 } while (chksz >>= 4);
1152
1153 if (data.len + (end - beg) + 2 > b_room(chk))
1154 return 0;
1155 chunk_memcat(chk, beg, end - beg);
1156 chunk_memcat(chk, data.ptr, data.len);
1157 chunk_memcat(chk, "\r\n", 2);
1158 }
1159 else {
1160 if (!chunk_memcat(chk, data.ptr, data.len))
1161 return 0;
1162 }
1163
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001164 return 1;
1165}