blob: c29a66d78416f805e419d9bab708f883125f830f [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
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +020018/* Defragments an HTX message. It removes unused blocks and unwraps the payloads
19 * part. A temporary buffer is used to do so. This function never fails. if
20 * <blk> is not NULL, we replace it by the new block address, after the
21 * defragmentation. The new <blk> is returned.
Christopher Fauleta3d2a162018-10-22 08:59:39 +020022 */
23/* TODO: merge data blocks into one */
24struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk)
25{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010026 struct buffer *chunk = get_trash_chunk();
27 struct htx *tmp = htxbuf(chunk);
28 struct htx_blk *newblk, *oldblk;
Christopher Faulet200f8952019-01-02 11:23:44 +010029 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010030 uint32_t addr, blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +020031 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020032
Christopher 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 Faulet4eb8c3d2019-06-19 13:48:09 +020057 /* if <blk> is defined, save its new position */
Christopher Faulet200f8952019-01-02 11:23:44 +010058 if (blk != NULL && blk == oldblk)
59 blkpos = new;
60
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010061 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
62 new++;
63 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020065 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher 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 Faulet4eb8c3d2019-06-19 13:48:09 +020078/* Degragments HTX blocks of an HTX message. Payloads part is keep untouched
79 * here. This function will move back all blocks starting at the position 0,
80 * removing unused blocks. It must never be called with an empty message.
81 */
Christopher Fauletd7884d32019-06-11 10:40:43 +020082static void htx_defrag_blks(struct htx *htx)
83{
84 int32_t pos, new;
85
86 new = 0;
87 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
88 struct htx_blk *posblk, *newblk;
89
90 if (pos == new) {
91 new++;
92 continue;
93 }
94
95 posblk = htx_get_blk(htx, pos);
96 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
97 continue;
98
99 if (htx->first == pos)
100 htx->first = new;
101 newblk = htx_get_blk(htx, new++);
102 newblk->info = posblk->info;
103 newblk->addr = posblk->addr;
104 }
105 BUG_ON(!new);
106 htx->head = 0;
107 htx->tail = new - 1;
108}
109
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200110/* Reserves a new block in the HTX message <htx> with a content of <blksz>
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200111 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200112 * block is returned and the HTX message is updated. Space for this new block is
113 * reserved in the HTX message. But it is the caller responsibility to set right
114 * info in the block to reflect the stored data.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200115 */
116static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
117{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200118 struct htx_blk *blk;
119 uint32_t used, tail;
120 uint32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200121
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100122 if (blksz > htx_free_data_space(htx))
123 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200124
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100125 if (!htx->used) {
126 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200127 htx->head = htx->tail = htx->first = 0;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200128 htx->used = 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100129 blk = htx_get_blk(htx, htx->tail);
130 blk->addr = 0;
131 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200132 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100133 return blk;
134 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200135
Christopher Fauletd7884d32019-06-11 10:40:43 +0200136 /* Find the block's position. First, we try to get the next position in
137 * the message, increasing the tail by one. If this position is not
138 * available with some holes, we try to defrag the blocks without
139 * touching their paylood. If it is impossible, we fully defrag the
140 * message.
141 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200142 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200143 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) >= htx->tail_addr)
144 used = htx->used + 1;
145 else if (tail > htx->used) {
146 htx_defrag_blks(htx);
147 tail = htx->tail + 1;
148 used = htx->used + 1;
149 BUG_ON(sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100150 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200151 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200153
Christopher Fauletd7884d32019-06-11 10:40:43 +0200154 /* Now, we have found the block's position. Try do find where to put its
155 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100156 *
Christopher Fauletd7884d32019-06-11 10:40:43 +0200157 * * The free space in front of the blocks table. This one is used iff
158 * the other one was not used yet.
159 *
160 * * The free space at the beginning of the message. Once this one is
161 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100162 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200163 headroom = (htx->end_addr - htx->head_addr);
164 tailroom = (!htx->head_addr
165 ? sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) - htx->tail_addr
166 : 0);
167 BUG_ON((int32_t)headroom < 0);
168 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200169
Christopher Fauletd7884d32019-06-11 10:40:43 +0200170 if (blksz <= tailroom) {
171 blk = htx_get_blk(htx, tail);
172 blk->addr = htx->tail_addr;
173 htx->tail_addr += blksz;
174 }
175 else if (blksz <= headroom) {
176 blk = htx_get_blk(htx, tail);
177 blk->addr = htx->head_addr;
178 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100179 }
180 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200181 defrag:
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200182 /* need to defragment the message before inserting upfront */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200183 htx_defrag(htx, NULL);
184 tail = htx->tail + 1;
185 used = htx->used + 1;
186 blk = htx_get_blk(htx, tail);
187 blk->addr = htx->tail_addr;
188 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100189 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200190
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100191 htx->tail = tail;
192 htx->used = used;
193 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200194 /* Set first position if not already set */
195 if (htx->first == -1)
196 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200197
198 BUG_ON((int32_t)htx->tail_addr < 0);
199 BUG_ON((int32_t)htx->head_addr < 0);
200 BUG_ON(htx->end_addr > htx->tail_addr);
201 BUG_ON(htx->head_addr > htx->end_addr);
202
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100203 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200204}
205
Christopher Fauletd7884d32019-06-11 10:40:43 +0200206/* Prepares the block to an expansion of its payload. The payload will be
207 * expanded by <delta> bytes and we need find where this expansion will be
208 * performed. It can be a compression if <delta> is negative. This function only
209 * updates all addresses. The caller have the responsibility to performe the
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200210 * expansion and update the block and the HTX message accordingly. No error must
211 * occurr. It returns following values:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200212 *
213 * 0: The expansion cannot be performed, there is not enough space.
214 *
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200215 * 1: the expansion must be performed in place, there is enougth space after
Christopher Fauletd7884d32019-06-11 10:40:43 +0200216 * the block's payload to handle it. This is especially true if it is a
217 * compression and not an expension.
218 *
219 * 2: the block's payload must be moved at the new block address before doing
220 * the expansion.
221 *
222 * 3: the HTX message message must be defragmented
223 */
224static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
225{
226 uint32_t sz, tailroom, headroom;
227 int ret = 3;
228
229 headroom = (htx->end_addr - htx->head_addr);
230 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
231 BUG_ON((int32_t)headroom < 0);
232 BUG_ON((int32_t)tailroom < 0);
233
234 sz = htx_get_blksz(blk);
235 if (delta <= 0) {
236 /* It is a compression, it can be performed in place */
237 if (blk->addr+sz == htx->tail_addr)
238 htx->tail_addr += delta;
239 else if (blk->addr+sz == htx->head_addr)
240 htx->head_addr += delta;
241 ret = 1;
242 }
243 else if (delta > htx_free_space(htx)) {
244 /* There is not enought space to handle the expansion */
245 ret = 0;
246 }
247 else if (blk->addr+sz == htx->tail_addr) {
248 /* The block's payload is just before the tail room */
249 if (delta < tailroom) {
250 /* Expand the block's payload */
251 htx->tail_addr += delta;
252 ret = 1;
253 }
254 else if ((sz + delta) < headroom) {
255 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200256 blk->addr = htx->head_addr;
257 htx->tail_addr -= sz;
258 htx->head_addr += sz + delta;
Christopher Faulet8c654862019-06-12 11:08:11 +0200259 if (blk->addr == htx->end_addr) {
260 if (htx->end_addr == htx->tail_addr) {
261 htx->tail_addr = htx->head_addr;
262 htx->head_addr = htx->end_addr = 0;
263 }
264 else
265 htx->end_addr += sz;
266 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200267 ret = 2;
268 }
269 }
270 else if (blk->addr+sz == htx->head_addr) {
271 /* The block's payload is just before the head room */
272 if (delta < headroom) {
273 /* Expand the block's payload */
274 htx->head_addr += delta;
275 ret = 1;
276 }
277 }
278 else {
279 /* The block's payload is not at the rooms edge */
280 if (!htx->head_addr && sz+delta < tailroom) {
281 /* Move the block's payload into the tailroom */
282 if (blk->addr == htx->end_addr)
283 htx->end_addr += sz;
284 blk->addr = htx->tail_addr;
285 htx->tail_addr += sz + delta;
286 ret = 2;
287 }
288 else if (sz+delta < headroom) {
289 /* Move the block's payload into the headroom */
290 if (blk->addr == htx->end_addr)
291 htx->end_addr += sz;
292 blk->addr = htx->head_addr;
293 htx->head_addr += sz + delta;
294 ret = 2;
295 }
296 }
297 /* Otherwise defrag the HTX message */
298
299 BUG_ON((int32_t)htx->tail_addr < 0);
300 BUG_ON((int32_t)htx->head_addr < 0);
301 BUG_ON(htx->end_addr > htx->tail_addr);
302 BUG_ON(htx->head_addr > htx->end_addr);
303 return ret;
304}
305
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200306/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
307 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200308 */
309struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
310{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100311 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200312
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100313 blk = htx_reserve_nxblk(htx, blksz);
314 if (!blk)
315 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200316 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200317
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100318 blk->info = (type << 28);
319 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200320}
321
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200322/* Removes the block <blk> from the HTX message <htx>. The function returns the
323 * block following <blk> or NULL if <blk> is the last block or the last inserted
324 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200325 */
326struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
327{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200328 enum htx_blk_type type;
329 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200330
Christopher Fauletd7884d32019-06-11 10:40:43 +0200331 /* This is the last block in use */
332 if (htx->used == 1) {
333 htx_reset(htx);
334 return NULL;
335 }
336
337 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200338 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200339 sz = htx_get_blksz(blk);
340 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100341 if (type != HTX_BLK_UNUSED) {
342 /* Mark the block as unused, decrement allocated size */
343 htx->data -= htx_get_blksz(blk);
344 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100345 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200346
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200347 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200348 if (pos == htx->head) {
349 /* move the head forward */
350 htx->used--;
351 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100352 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200353 else if (pos == htx->tail) {
354 /* remove the tail. this was the last inserted block so
355 * return NULL. */
356 htx->tail--;
357 htx->used--;
358 blk = NULL;
359 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100360 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200361 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200362
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200363 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200364 if (pos == htx->first)
365 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200366
367 if (htx->used == 1) {
368 /* If there is just one block in the HTX message, free space can
369 * be ajusted. This operation could save some defrags. */
370 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
371
372 htx->head_addr = 0;
373 htx->end_addr = lastblk->addr;
374 htx->tail_addr = lastblk->addr+htx->data;
375 }
376 else {
377 if (addr+sz == htx->tail_addr)
378 htx->tail_addr = addr;
379 else if (addr+sz == htx->head_addr)
380 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200381 if (addr == htx->end_addr) {
382 if (htx->tail_addr == htx->end_addr) {
383 htx->tail_addr = htx->head_addr;
384 htx->head_addr = htx->end_addr = 0;
385 }
386 else
387 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200388 }
389 }
390
391 BUG_ON((int32_t)htx->tail_addr < 0);
392 BUG_ON((int32_t)htx->head_addr < 0);
393 BUG_ON(htx->end_addr > htx->tail_addr);
394 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100395 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200396}
397
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200398/* Removes all blocks after the one containing the offset <offset>. This last
399 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100400 */
401void htx_truncate(struct htx *htx, uint32_t offset)
402{
403 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100404
Christopher Fauletced39002019-05-23 11:12:43 +0200405 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100406 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200407 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100408
Christopher Fauletced39002019-05-23 11:12:43 +0200409 if (offset >= sz) {
410 offset -= sz;
411 continue;
412 }
Christopher Faulet41dc8432019-06-18 09:49:16 +0200413 if (type == HTX_BLK_DATA)
414 htx_change_blk_value_len(htx, blk, offset);
Christopher Fauletced39002019-05-23 11:12:43 +0200415 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100416 }
417 while (blk)
418 blk = htx_remove_blk(htx, blk);
419}
420
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200421/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
422 * block, it will be cut if necessary. Others blocks will be removed at once if
423 * <count> is large enough. The function returns an htx_ret with the first block
424 * remaing in the messsage and the amount of data drained. If everything is
425 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100426 */
427struct htx_ret htx_drain(struct htx *htx, uint32_t count)
428{
429 struct htx_blk *blk;
430 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
431
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200432 if (count == htx->data) {
433 htx_reset(htx);
434 htxret.ret = count;
435 return htxret;
436 }
437
Christopher Faulet549822f2019-02-25 10:23:19 +0100438 blk = htx_get_head_blk(htx);
439 while (count && blk) {
440 uint32_t sz = htx_get_blksz(blk);
441 enum htx_blk_type type = htx_get_blk_type(blk);
442
443 /* Ingore unused block */
444 if (type == HTX_BLK_UNUSED)
445 goto next;
446
447 if (sz > count) {
448 if (type == HTX_BLK_DATA) {
449 htx_cut_data_blk(htx, blk, count);
450 htxret.ret += count;
451 }
452 break;
453 }
454 count -= sz;
455 htxret.ret += sz;
456 next:
457 blk = htx_remove_blk(htx, blk);
458 }
459 htxret.blk = blk;
460
461 return htxret;
462}
463
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200464/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200465 * there is enough space to take it all. If the space wraps, the buffer is
466 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200467 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200468 * returned. Due to its nature this function can be expensive and should be
469 * avoided whenever possible.
470 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200471struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200472{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200473 struct htx_blk *blk, *tailblk;
474 void *ptr;
475 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200476
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100477 if (!htx->used)
478 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200479
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100480 /* Not enough space to store data */
481 if (data.len > htx_free_data_space(htx))
482 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200483
Christopher Fauletd7884d32019-06-11 10:40:43 +0200484 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200485 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200486 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200487 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200488 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200489
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100490 /* Don't try to append data if the last inserted block is not of the
491 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200492 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100493 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200494
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100495 /*
496 * Same type and enough space: append data
497 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200498 headroom = (htx->end_addr - htx->head_addr);
499 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
500 BUG_ON((int32_t)headroom < 0);
501 BUG_ON((int32_t)tailroom < 0);
502
503 len = data.len;
504 if (tailblk->addr+sz == htx->tail_addr) {
505 if (data.len <= tailroom)
506 goto append_data;
507 else if (!htx->head_addr) {
508 len = tailroom;
509 goto append_data;
510 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100511 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200512 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
513 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200514
Christopher Fauletd7884d32019-06-11 10:40:43 +0200515 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200516
517 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100518 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100519 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200520 ptr = htx_get_blk_ptr(htx, tailblk);
521 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +0200522 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200523
Christopher Fauletd7884d32019-06-11 10:40:43 +0200524 if (data.len == len) {
525 blk = tailblk;
526 goto end;
527 }
528 data.ptr += len;
529 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200530
531 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200532 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200533 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100534 if (!blk)
535 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200536
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100537 blk->info += data.len;
538 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200539
540 end:
541 BUG_ON((int32_t)htx->tail_addr < 0);
542 BUG_ON((int32_t)htx->head_addr < 0);
543 BUG_ON(htx->end_addr > htx->tail_addr);
544 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100545 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200546}
547
548/* Replaces a value part of a block by a new one. The new part can be smaller or
549 * larger than the old one. This function works for any kind of block with
550 * attached data. It returns the new block on success, otherwise it returns
551 * NULL.
552 */
553struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100554 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200555{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100556 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100557 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200558 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200559
Christopher Fauletd7884d32019-06-11 10:40:43 +0200560 n = htx_get_blk_name(htx, blk);
561 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100562 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200563 ret = htx_prepare_blk_expansion(htx, blk, delta);
564 if (!ret)
565 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100566
Christopher Fauletd7884d32019-06-11 10:40:43 +0200567 /* Before updating the payload, set the new block size and update HTX
568 * message */
569 htx_set_blk_value_len(blk, v.len + delta);
570 htx->data += delta;
571
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200572 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200573 if (delta <= 0) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200574 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200575 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100576 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200577 }
578 else {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200579 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200580 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
581 memcpy(old.ptr, new.ptr, new.len);
582 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100583 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200584 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200585 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 /* Copy the name, if any */
588 memcpy(ptr, n.ptr, n.len);
589 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200590
Christopher Fauletd7884d32019-06-11 10:40:43 +0200591 /* Copy value before old part, if any */
592 memcpy(ptr, v.ptr, old.ptr - v.ptr);
593 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200594
Christopher Fauletd7884d32019-06-11 10:40:43 +0200595 /* Copy new value */
596 memcpy(ptr, new.ptr, new.len);
597 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200598
Christopher Fauletd7884d32019-06-11 10:40:43 +0200599 /* Copy value after old part, if any */
600 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
601 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200602 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200603 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200604
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 /* Copy the header name, if any */
606 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200607
Christopher Fauletd7884d32019-06-11 10:40:43 +0200608 /* Copy value before old part, if any */
609 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200610
Christopher Fauletd7884d32019-06-11 10:40:43 +0200611 /* Copy new value */
612 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200613
Christopher Fauletd7884d32019-06-11 10:40:43 +0200614 /* Copy value after old part if any */
615 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200616
Christopher Fauletd7884d32019-06-11 10:40:43 +0200617 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletd7884d32019-06-11 10:40:43 +0200619 /* Finaly, copy data. */
620 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
621 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100622 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200623}
624
625/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200626 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200627 * (including payload and meta-data). It returns the number of bytes moved and
628 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200629 */
630struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
631 enum htx_blk_type mark)
632{
633 struct htx_blk *blk, *dstblk;
634 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100635 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200636 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637
Christopher Faulet156852b2019-05-16 11:29:13 +0200638 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639 blk = htx_get_blk(src, htx_get_head(src));
640 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200641
642 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200643 type = htx_get_blk_type(blk);
644
645 /* Ingore unused block */
646 if (type == HTX_BLK_UNUSED)
647 goto next;
648
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200649 /* Be sure to have enough space to xfer all headers/trailers in
650 * one time. If not while <dst> is empty, we report a parsing
651 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200652 */
653 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
654 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
655
656 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
657 if (htx_is_empty(dst))
658 src->flags |= HTX_FL_PARSING_ERROR;
659 break;
660 }
661 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200662 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
663 !inside_trailers && mark >= HTX_BLK_EOT) {
664 inside_trailers = 1;
665 if (htx_used_space(src) > count) {
666 if (htx_is_empty(dst))
667 src->flags |= HTX_FL_PARSING_ERROR;
668 break;
669 }
670 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200671
Christopher Faulet156852b2019-05-16 11:29:13 +0200672 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200673 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200674 max = htx_get_max_blksz(dst, count);
675 if (!max)
676 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200677 if (sz > max) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200678 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200679 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200680 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200681 sz = max;
682 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200683 }
684
685 dstblk = htx_reserve_nxblk(dst, sz);
686 if (!dstblk)
687 break;
688 dstblk->info = info;
689 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
690
Christopher Faulet156852b2019-05-16 11:29:13 +0200691 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692 if (blk->info != info) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200693 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200695 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200696 break;
697 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 next:
699 blk = htx_remove_blk(src, blk);
700 if (type == mark)
701 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200702 }
703
Christopher Faulet156852b2019-05-16 11:29:13 +0200704 end:
705 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200706 return (struct htx_ret){.ret = ret, .blk = dstblk};
707}
708
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709/* Replaces an header by a new one. The new header can be smaller or larger than
710 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100711 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200712 */
713struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100714 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200715{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100716 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200717 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100718 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200719 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100721 type = htx_get_blk_type(blk);
722 if (type != HTX_BLK_HDR)
723 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200724
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100725 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200726 ret = htx_prepare_blk_expansion(htx, blk, delta);
727 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100728 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200729
Christopher Fauletd7884d32019-06-11 10:40:43 +0200730 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200731 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100732 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100733
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200734 /* Replace in place or at a new address is the same. We replace all the
735 * header (name+value). Only take care to defrag the message if
736 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200737 if (ret == 3)
738 blk = htx_defrag(htx, blk);
739
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100740 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200741 ptr = htx_get_blk_ptr(htx, blk);
742 ist2bin_lc(ptr, name);
743 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100744 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200745}
746
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100747/* Replaces the parts of the start-line. It returns the new start-line on
748 * success, otherwise it returns NULL. It is the caller responsibility to update
749 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200750 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100751struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
752 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200754 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100755 struct htx_sl *sl;
756 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200757 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100758 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200759 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200760
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100761 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100762 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100763 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200764
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100765 /* Save start-line info and flags */
766 sl = htx_get_blk_ptr(htx, blk);
767 tmp.info = sl->info;
768 tmp.flags = sl->flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200769 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100770
Christopher Fauletd7884d32019-06-11 10:40:43 +0200771 sz = htx_get_blksz(blk);
772 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
773 ret = htx_prepare_blk_expansion(htx, blk, delta);
774 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100775 return NULL; /* not enough space */
776
Christopher Fauletd7884d32019-06-11 10:40:43 +0200777 /* Set the new block size and update HTX message */
778 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100779 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200780
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200781 /* Replace in place or at a new address is the same. We replace all the
782 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200783 if (ret == 3)
784 blk = htx_defrag(htx, blk);
785
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100786 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100787 sl = htx_get_blk_ptr(htx, blk);
788 sl->info = tmp.info;
789 sl->flags = tmp.flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200790 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200791
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100792 HTX_SL_P1_LEN(sl) = p1.len;
793 HTX_SL_P2_LEN(sl) = p2.len;
794 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100795
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100796 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
797 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
798 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200799
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100800 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200801}
802
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100803/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
804 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100806struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
807 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200808{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100809 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100810 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200811 uint32_t size;
812
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100813 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
814 return NULL;
815
816 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200817
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100818 /* FIXME: check size (< 256MB) */
819 blk = htx_add_blk(htx, type, size);
820 if (!blk)
821 return NULL;
822 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200823
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100824 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200825 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200827
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100828 HTX_SL_P1_LEN(sl) = p1.len;
829 HTX_SL_P2_LEN(sl) = p2.len;
830 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100832 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
833 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
834 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
835
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100836 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200837}
838
839/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100840 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200841 */
842struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100843 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200844{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100845 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200846
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100847 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
848 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
849 if (!blk)
850 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200851
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100852 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100853 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100854 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
855 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200856}
857
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200858/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200859 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200860 */
861struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
862 const struct ist value)
863{
864 struct htx_blk *blk;
865
866 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
867 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
868 if (!blk)
869 return NULL;
870
871 blk->info += (value.len << 8) + name.len;
872 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
873 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
874 return blk;
875}
876
Willy Tarreau52610e92019-01-03 18:26:17 +0100877/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
878 * new block on success. Otherwise, it returns NULL. The caller is responsible
879 * for filling the block itself.
880 */
881struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
882{
883 struct htx_blk *blk;
884
885 blk = htx_add_blk(htx, type, blksz);
886 if (!blk)
887 return NULL;
888
889 blk->info += blksz;
890 return blk;
891}
892
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200893/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
894 * the EOH. On sucess, it returns the last block inserted (the EOH), otherwise
895 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200896struct htx_blk *htx_add_all_headers(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_header(htx, hdrs[i].n, hdrs[i].v))
902 return NULL;
903 }
904 return htx_add_endof(htx, HTX_BLK_EOH);
905}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200906
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200907/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
908 * the EOT. On sucess, it returns the last block inserted (the EOT), otherwise
909 * NULL is returned. */
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 Faulet4eb8c3d2019-06-19 13:48:09 +0200921/* Adds an HTX block of type EOH, EOT, 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);
Christopher Faulet41dc8432019-06-18 09:49:16 +0200991 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200992
993 BUG_ON((int32_t)htx->tail_addr < 0);
994 BUG_ON((int32_t)htx->head_addr < 0);
995 BUG_ON(htx->end_addr > htx->tail_addr);
996 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200997 return len;
998
999 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001000 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001001 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1002 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001003 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001004
1005 blk->info += len;
1006 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1007 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001008}
1009
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001010
1011/* Adds an HTX block of type DATA in <htx> just after all other DATA
1012 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1013 * DATA block if possible. But, if the function succeeds, it will be the last
1014 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1015 * on success, the updated block (or the new one) is returned.
1016 */
1017struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001018{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001019 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001020
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001021 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001022 if (!blk)
1023 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001024
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001025 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001026 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1027 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001028
Christopher Faulet24ed8352018-11-22 11:20:43 +01001029 /* Swap .addr and .info fields */
1030 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1031 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1032
1033 if (blk->addr == pblk->addr)
1034 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001035 blk = pblk;
1036 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001037
Christopher Faulet24ed8352018-11-22 11:20:43 +01001038 return blk;
1039}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001040
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001041/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1042 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1043 * blocks are updated to remain valid after the move. */
1044void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1045{
1046 struct htx_blk *cblk, *pblk;
1047
1048 cblk = *blk;
1049 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1050 /* Swap .addr and .info fields */
1051 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1052 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1053
1054 if (cblk->addr == pblk->addr)
1055 cblk->addr += htx_get_blksz(pblk);
1056 if (pblk == *ref)
1057 break;
1058 cblk = pblk;
1059 }
1060 *blk = cblk;
1061 *ref = pblk;
1062}
1063
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001064/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
1065 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001066 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001067int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001068{
Christopher Faulet570d1612018-11-26 11:13:57 +01001069 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001070 return 0;
1071
Christopher Faulet570d1612018-11-26 11:13:57 +01001072 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001073 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001074 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001075 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +01001076 if (sl->flags & HTX_SL_F_VER_11)
1077 chunk_memcat(chk, "HTTP/1.1", 8);
1078 else
1079 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1080
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001081 chunk_memcat(chk, "\r\n", 2);
1082
1083 return 1;
1084}
1085
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001086/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
1087 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001088 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001089int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001090{
Christopher Faulet570d1612018-11-26 11:13:57 +01001091 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001092 return 0;
1093
Christopher Faulet1e7af462018-12-03 14:05:01 +01001094 if (sl->flags & HTX_SL_F_VER_11)
1095 chunk_memcat(chk, "HTTP/1.1", 8);
1096 else
1097 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001098 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001099 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001100 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001101 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001102 chunk_memcat(chk, "\r\n", 2);
1103
1104 return 1;
1105}
1106
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001107/* Appends the H1 representation of the header <n> witht the value <v> to the
1108 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1109 * returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001110 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001111int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001112{
1113 if (n.len + v.len + 4 > b_room(chk))
1114 return 0;
1115
1116 chunk_memcat(chk, n.ptr, n.len);
1117 chunk_memcat(chk, ": ", 2);
1118 chunk_memcat(chk, v.ptr, v.len);
1119 chunk_memcat(chk, "\r\n", 2);
1120
1121 return 1;
1122}
1123
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001124/* Appends the H1 representation of the data <data> to the chunk <chk>. If
1125 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
1126 * data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001127 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001128int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001129{
1130 if (chunked) {
1131 uint32_t chksz;
1132 char tmp[10];
1133 char *beg, *end;
1134
1135 chksz = data.len;
1136
1137 beg = end = tmp+10;
1138 *--beg = '\n';
1139 *--beg = '\r';
1140 do {
1141 *--beg = hextab[chksz & 0xF];
1142 } while (chksz >>= 4);
1143
1144 if (data.len + (end - beg) + 2 > b_room(chk))
1145 return 0;
1146 chunk_memcat(chk, beg, end - beg);
1147 chunk_memcat(chk, data.ptr, data.len);
1148 chunk_memcat(chk, "\r\n", 2);
1149 }
1150 else {
1151 if (!chunk_memcat(chk, data.ptr, data.len))
1152 return 0;
1153 }
1154
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001155 return 1;
1156}