blob: 502184aff47753de19555ade6c567e34039f8dd0 [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 Faulet3e2638e2019-06-18 09:49:16 +0200409 if (type == HTX_BLK_DATA)
410 htx_change_blk_value_len(htx, blk, offset);
Christopher Fauletced39002019-05-23 11:12:43 +0200411 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100412 }
413 while (blk)
414 blk = htx_remove_blk(htx, blk);
415}
416
Christopher Faulet549822f2019-02-25 10:23:19 +0100417/* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if
418 * necessary. Others blocks will be removed at once if <count> is large
419 * enough. The function returns an htx_ret with the first block remaing in the
420 * messsage and the amount of data drained. If everything is removed,
421 * htx_ret.blk is set to NULL.
422 */
423struct htx_ret htx_drain(struct htx *htx, uint32_t count)
424{
425 struct htx_blk *blk;
426 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
427
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200428 if (count == htx->data) {
429 htx_reset(htx);
430 htxret.ret = count;
431 return htxret;
432 }
433
Christopher Faulet549822f2019-02-25 10:23:19 +0100434 blk = htx_get_head_blk(htx);
435 while (count && blk) {
436 uint32_t sz = htx_get_blksz(blk);
437 enum htx_blk_type type = htx_get_blk_type(blk);
438
439 /* Ingore unused block */
440 if (type == HTX_BLK_UNUSED)
441 goto next;
442
443 if (sz > count) {
444 if (type == HTX_BLK_DATA) {
445 htx_cut_data_blk(htx, blk, count);
446 htxret.ret += count;
447 }
448 break;
449 }
450 count -= sz;
451 htxret.ret += sz;
452 next:
453 blk = htx_remove_blk(htx, blk);
454 }
455 htxret.blk = blk;
456
457 return htxret;
458}
459
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200460/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200461 * there is enough space to take it all. If the space wraps, the buffer is
462 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200463 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200464 * returned. Due to its nature this function can be expensive and should be
465 * avoided whenever possible.
466 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200467struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200468{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200469 struct htx_blk *blk, *tailblk;
470 void *ptr;
471 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200472
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100473 if (!htx->used)
474 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200475
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100476 /* Not enough space to store data */
477 if (data.len > htx_free_data_space(htx))
478 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200479
Christopher Fauletd7884d32019-06-11 10:40:43 +0200480 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200481 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200482 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200483 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200484 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200485
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100486 /* Don't try to append data if the last inserted block is not of the
487 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200488 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100489 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200490
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100491 /*
492 * Same type and enough space: append data
493 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200494 headroom = (htx->end_addr - htx->head_addr);
495 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
496 BUG_ON((int32_t)headroom < 0);
497 BUG_ON((int32_t)tailroom < 0);
498
499 len = data.len;
500 if (tailblk->addr+sz == htx->tail_addr) {
501 if (data.len <= tailroom)
502 goto append_data;
503 else if (!htx->head_addr) {
504 len = tailroom;
505 goto append_data;
506 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100507 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200508 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
509 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200510
Christopher Fauletd7884d32019-06-11 10:40:43 +0200511 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200512
513 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100515 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200516 ptr = htx_get_blk_ptr(htx, tailblk);
517 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200518 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200519
Christopher Fauletd7884d32019-06-11 10:40:43 +0200520 if (data.len == len) {
521 blk = tailblk;
522 goto end;
523 }
524 data.ptr += len;
525 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200526
527 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200528 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200529 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100530 if (!blk)
531 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200532
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100533 blk->info += data.len;
534 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200535
536 end:
537 BUG_ON((int32_t)htx->tail_addr < 0);
538 BUG_ON((int32_t)htx->head_addr < 0);
539 BUG_ON(htx->end_addr > htx->tail_addr);
540 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100541 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542}
543
544/* Replaces a value part of a block by a new one. The new part can be smaller or
545 * larger than the old one. This function works for any kind of block with
546 * attached data. It returns the new block on success, otherwise it returns
547 * NULL.
548 */
549struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100550 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200551{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100552 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100553 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200554 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200555
Christopher Fauletd7884d32019-06-11 10:40:43 +0200556 n = htx_get_blk_name(htx, blk);
557 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100558 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200559 ret = htx_prepare_blk_expansion(htx, blk, delta);
560 if (!ret)
561 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100562
Christopher Fauletd7884d32019-06-11 10:40:43 +0200563 /* Before updating the payload, set the new block size and update HTX
564 * message */
565 htx_set_blk_value_len(blk, v.len + delta);
566 htx->data += delta;
567
568 if (ret == 1) {
569 if (delta <= 0) {
570 /* compression: copy new data first */
571 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100572 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200573 }
574 else {
575 /* expansion: move the end first */
576 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
577 memcpy(old.ptr, new.ptr, new.len);
578 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100579 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200580 else if (ret == 2) {
581 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200582
Christopher Fauletd7884d32019-06-11 10:40:43 +0200583 /* Copy the name, if any */
584 memcpy(ptr, n.ptr, n.len);
585 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 /* Copy value before old part, if any */
588 memcpy(ptr, v.ptr, old.ptr - v.ptr);
589 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200590
Christopher Fauletd7884d32019-06-11 10:40:43 +0200591 /* Copy new value */
592 memcpy(ptr, new.ptr, new.len);
593 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200594
Christopher Fauletd7884d32019-06-11 10:40:43 +0200595 /* Copy value after old part, if any */
596 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
597 }
598 else {
599 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletd7884d32019-06-11 10:40:43 +0200601 /* Copy the header name, if any */
602 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200603
Christopher Fauletd7884d32019-06-11 10:40:43 +0200604 /* Copy value before old part, if any */
605 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 /* Copy new value */
608 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200609
Christopher Fauletd7884d32019-06-11 10:40:43 +0200610 /* Copy value after old part if any */
611 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200614
Christopher Fauletd7884d32019-06-11 10:40:43 +0200615 /* Finaly, copy data. */
616 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
617 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100618 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200619}
620
621/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200622 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200623 * (including payload and meta-data). It returns the number of bytes moved and
624 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625 */
626struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
627 enum htx_blk_type mark)
628{
629 struct htx_blk *blk, *dstblk;
630 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100631 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200632 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200633
Christopher Faulet156852b2019-05-16 11:29:13 +0200634 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200635 blk = htx_get_blk(src, htx_get_head(src));
636 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200637
638 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639 type = htx_get_blk_type(blk);
640
641 /* Ingore unused block */
642 if (type == HTX_BLK_UNUSED)
643 goto next;
644
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200645 /* Be sure to have enough space to xfer all headers/trailers in
646 * one time. If not while <dst> is empty, we report a parsing
647 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200648 */
649 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
650 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
651
652 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
653 if (htx_is_empty(dst))
654 src->flags |= HTX_FL_PARSING_ERROR;
655 break;
656 }
657 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200658 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
659 !inside_trailers && mark >= HTX_BLK_EOT) {
660 inside_trailers = 1;
661 if (htx_used_space(src) > count) {
662 if (htx_is_empty(dst))
663 src->flags |= HTX_FL_PARSING_ERROR;
664 break;
665 }
666 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200667
Christopher Faulet156852b2019-05-16 11:29:13 +0200668 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200669 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200670 max = htx_get_max_blksz(dst, count);
671 if (!max)
672 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200673 if (sz > max) {
Christopher Faulet39744f72019-05-24 14:54:00 +0200674 /* Headers must be fully copied */
Christopher Faulet156852b2019-05-16 11:29:13 +0200675 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200676 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200677 sz = max;
678 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200679 }
680
681 dstblk = htx_reserve_nxblk(dst, sz);
682 if (!dstblk)
683 break;
684 dstblk->info = info;
685 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
686
Christopher Faulet156852b2019-05-16 11:29:13 +0200687 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200688 if (blk->info != info) {
689 /* Partial move: don't remove <blk> from <src> but
690 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200691 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692 break;
693 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694 next:
695 blk = htx_remove_blk(src, blk);
696 if (type == mark)
697 break;
698
699 }
700
Christopher Faulet156852b2019-05-16 11:29:13 +0200701 end:
702 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703 return (struct htx_ret){.ret = ret, .blk = dstblk};
704}
705
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200706/* Replaces an header by a new one. The new header can be smaller or larger than
707 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100708 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709 */
710struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100711 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200712{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100713 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200714 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100715 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200716 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200717
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100718 type = htx_get_blk_type(blk);
719 if (type != HTX_BLK_HDR)
720 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200721
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100722 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200723 ret = htx_prepare_blk_expansion(htx, blk, delta);
724 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100725 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200726
Christopher Fauletd7884d32019-06-11 10:40:43 +0200727 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200728 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100729 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100730
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 Fauletd7884d32019-06-11 10:40:43 +0200775 if (ret == 3)
776 blk = htx_defrag(htx, blk);
777
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100778 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100779 sl = htx_get_blk_ptr(htx, blk);
780 sl->info = tmp.info;
781 sl->flags = tmp.flags;
Christopher Faulet2bce0462019-06-25 21:31:26 +0200782 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200783
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100784 HTX_SL_P1_LEN(sl) = p1.len;
785 HTX_SL_P2_LEN(sl) = p2.len;
786 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100787
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100788 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
789 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
790 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200791
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100792 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200793}
794
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100795/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
796 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200797 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100798struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
799 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200800{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100801 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100802 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803 uint32_t size;
804
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100805 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
806 return NULL;
807
808 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100810 /* FIXME: check size (< 256MB) */
811 blk = htx_add_blk(htx, type, size);
812 if (!blk)
813 return NULL;
814 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200815
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100816 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200817 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100818 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200819
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100820 HTX_SL_P1_LEN(sl) = p1.len;
821 HTX_SL_P2_LEN(sl) = p2.len;
822 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200823
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100824 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
825 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
826 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
827
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100828 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829}
830
831/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100832 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833 */
834struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100835 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200836{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100837 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100839 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
840 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
841 if (!blk)
842 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200843
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100844 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100845 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100846 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
847 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200848}
849
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200850/* Adds an HTX block of type TLR in <htx>. It returns the new block on
851 * success. Otherwise, it returns NULL. The header name is always lower cased.
852 */
853struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
854 const struct ist value)
855{
856 struct htx_blk *blk;
857
858 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
859 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
860 if (!blk)
861 return NULL;
862
863 blk->info += (value.len << 8) + name.len;
864 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
865 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
866 return blk;
867}
868
Willy Tarreau52610e92019-01-03 18:26:17 +0100869/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
870 * new block on success. Otherwise, it returns NULL. The caller is responsible
871 * for filling the block itself.
872 */
873struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
874{
875 struct htx_blk *blk;
876
877 blk = htx_add_blk(htx, type, blksz);
878 if (!blk)
879 return NULL;
880
881 blk->info += blksz;
882 return blk;
883}
884
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200885struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
886{
887 int i;
888
889 for (i = 0; hdrs[i].n.len; i++) {
890 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
891 return NULL;
892 }
893 return htx_add_endof(htx, HTX_BLK_EOH);
894}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200895
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200896struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
897{
898 int i;
899
900 for (i = 0; hdrs[i].n.len; i++) {
901 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
902 return NULL;
903 }
904 return htx_add_endof(htx, HTX_BLK_EOT);
905}
906
Christopher Faulet54b5e212019-06-04 10:08:28 +0200907/* Adds an HTX block of type EOH or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200908 * on success. Otherwise, it returns NULL.
909 */
910struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
911{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100912 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200913
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100914 blk = htx_add_blk(htx, type, 1);
915 if (!blk)
916 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200917
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100918 blk->info += 1;
919 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200920}
921
922
923/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200924 * possible. It returns the number of bytes consumed from <data>, which may be
925 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200926 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200927size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200928{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200929 struct htx_blk *blk, *tailblk;
930 void *ptr;
931 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200932 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200933
Willy Tarreau0350b902019-05-28 10:58:50 +0200934 if (!htx->used)
935 goto add_new_block;
936
937 /* Not enough space to store data */
938 if (len > htx_free_data_space(htx))
939 len = htx_free_data_space(htx);
940
941 if (!len)
942 return 0;
943
944 /* get the tail and head block */
945 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200946 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200947 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200948 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200949
950 /* Don't try to append data if the last inserted block is not of the
951 * same type */
952 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
953 goto add_new_block;
954
955 /*
956 * Same type and enough space: append data
957 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200958 if (!htx->head_addr) {
959 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200960 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200961 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
Willy Tarreau0350b902019-05-28 10:58:50 +0200962 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200963 else {
964 if (tailblk->addr+sz != htx->head_addr)
965 goto add_new_block;
966 room = (htx->end_addr - htx->head_addr);
967 }
968 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200969 if (room < len)
970 len = room;
971
972 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200973 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200974 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200975 ptr = htx_get_blk_ptr(htx, tailblk);
976 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200977 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200978
979 BUG_ON((int32_t)htx->tail_addr < 0);
980 BUG_ON((int32_t)htx->head_addr < 0);
981 BUG_ON(htx->end_addr > htx->tail_addr);
982 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200983 return len;
984
985 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200986 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +0200987 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
988 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200989 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +0200990
991 blk->info += len;
992 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
993 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200994}
995
Christopher Faulet86bc8df2019-06-11 10:38:38 +0200996
997/* Adds an HTX block of type DATA in <htx> just after all other DATA
998 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
999 * DATA block if possible. But, if the function succeeds, it will be the last
1000 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1001 * on success, the updated block (or the new one) is returned.
1002 */
1003struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001004{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001005 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001006
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001007 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001008 if (!blk)
1009 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001010
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001011 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001012 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1013 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001014
Christopher Faulet24ed8352018-11-22 11:20:43 +01001015 /* Swap .addr and .info fields */
1016 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1017 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1018
1019 if (blk->addr == pblk->addr)
1020 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001021 blk = pblk;
1022 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001023
Christopher Faulet24ed8352018-11-22 11:20:43 +01001024 return blk;
1025}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001026
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001027/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1028 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1029 * blocks are updated to remain valid after the move. */
1030void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1031{
1032 struct htx_blk *cblk, *pblk;
1033
1034 cblk = *blk;
1035 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1036 /* Swap .addr and .info fields */
1037 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1038 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1039
1040 if (cblk->addr == pblk->addr)
1041 cblk->addr += htx_get_blksz(pblk);
1042 if (pblk == *ref)
1043 break;
1044 cblk = pblk;
1045 }
1046 *blk = cblk;
1047 *ref = pblk;
1048}
1049
Christopher Fauletc59ff232018-12-03 13:58:44 +01001050/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001051 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1052 * returns 0.
1053 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001054int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001055{
Christopher Faulet570d1612018-11-26 11:13:57 +01001056 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001057 return 0;
1058
Christopher Faulet570d1612018-11-26 11:13:57 +01001059 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001060 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001061 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001062 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +01001063 if (sl->flags & HTX_SL_F_VER_11)
1064 chunk_memcat(chk, "HTTP/1.1", 8);
1065 else
1066 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1067
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001068 chunk_memcat(chk, "\r\n", 2);
1069
1070 return 1;
1071}
1072
Christopher Fauletc59ff232018-12-03 13:58:44 +01001073/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001074 * <chk>. It returns 1 if data are successfully appended, otherwise it
1075 * returns 0.
1076 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001077int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001078{
Christopher Faulet570d1612018-11-26 11:13:57 +01001079 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001080 return 0;
1081
Christopher Faulet1e7af462018-12-03 14:05:01 +01001082 if (sl->flags & HTX_SL_F_VER_11)
1083 chunk_memcat(chk, "HTTP/1.1", 8);
1084 else
1085 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001086 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001087 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001088 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001089 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001090 chunk_memcat(chk, "\r\n", 2);
1091
1092 return 1;
1093}
1094
Christopher Fauletc59ff232018-12-03 13:58:44 +01001095/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001096 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
1097 * 0.
1098 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001099int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001100{
1101 if (n.len + v.len + 4 > b_room(chk))
1102 return 0;
1103
1104 chunk_memcat(chk, n.ptr, n.len);
1105 chunk_memcat(chk, ": ", 2);
1106 chunk_memcat(chk, v.ptr, v.len);
1107 chunk_memcat(chk, "\r\n", 2);
1108
1109 return 1;
1110}
1111
Christopher Fauletc59ff232018-12-03 13:58:44 +01001112/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001113 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
1114 * returns 1 if data are successfully appended, otherwise it returns 0.
1115 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001116int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001117{
1118 if (chunked) {
1119 uint32_t chksz;
1120 char tmp[10];
1121 char *beg, *end;
1122
1123 chksz = data.len;
1124
1125 beg = end = tmp+10;
1126 *--beg = '\n';
1127 *--beg = '\r';
1128 do {
1129 *--beg = hextab[chksz & 0xF];
1130 } while (chksz >>= 4);
1131
1132 if (data.len + (end - beg) + 2 > b_room(chk))
1133 return 0;
1134 chunk_memcat(chk, beg, end - beg);
1135 chunk_memcat(chk, data.ptr, data.len);
1136 chunk_memcat(chk, "\r\n", 2);
1137 }
1138 else {
1139 if (!chunk_memcat(chk, data.ptr, data.len))
1140 return 0;
1141 }
1142
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001143 return 1;
1144}