blob: cd21050c538326a593bfc0cc428e8d2705a5caa3 [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) {
Christopher Faulet3cd7a1e2019-07-29 10:50:28 +0200255 uint32_t oldaddr = blk->addr;
256
Christopher Fauletd7884d32019-06-11 10:40:43 +0200257 /* Move the block's payload into the headroom */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200258 blk->addr = htx->head_addr;
259 htx->tail_addr -= sz;
260 htx->head_addr += sz + delta;
Christopher Faulet3cd7a1e2019-07-29 10:50:28 +0200261 if (oldaddr == htx->end_addr) {
Christopher Faulet8c654862019-06-12 11:08:11 +0200262 if (htx->end_addr == htx->tail_addr) {
263 htx->tail_addr = htx->head_addr;
264 htx->head_addr = htx->end_addr = 0;
265 }
266 else
267 htx->end_addr += sz;
268 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200269 ret = 2;
270 }
271 }
272 else if (blk->addr+sz == htx->head_addr) {
273 /* The block's payload is just before the head room */
274 if (delta < headroom) {
275 /* Expand the block's payload */
276 htx->head_addr += delta;
277 ret = 1;
278 }
279 }
280 else {
281 /* The block's payload is not at the rooms edge */
282 if (!htx->head_addr && sz+delta < tailroom) {
283 /* Move the block's payload into the tailroom */
284 if (blk->addr == htx->end_addr)
285 htx->end_addr += sz;
286 blk->addr = htx->tail_addr;
287 htx->tail_addr += sz + delta;
288 ret = 2;
289 }
290 else if (sz+delta < headroom) {
291 /* Move the block's payload into the headroom */
292 if (blk->addr == htx->end_addr)
293 htx->end_addr += sz;
294 blk->addr = htx->head_addr;
295 htx->head_addr += sz + delta;
296 ret = 2;
297 }
298 }
299 /* Otherwise defrag the HTX message */
300
301 BUG_ON((int32_t)htx->tail_addr < 0);
302 BUG_ON((int32_t)htx->head_addr < 0);
303 BUG_ON(htx->end_addr > htx->tail_addr);
304 BUG_ON(htx->head_addr > htx->end_addr);
305 return ret;
306}
307
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200308/* Adds a new block of type <type> in the HTX message <htx>. Its content size is
309 * passed but it is the caller responsibility to do the copy.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200310 */
311struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
312{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100313 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200314
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100315 blk = htx_reserve_nxblk(htx, blksz);
316 if (!blk)
317 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200318 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200319
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100320 blk->info = (type << 28);
321 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200322}
323
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200324/* Removes the block <blk> from the HTX message <htx>. The function returns the
325 * block following <blk> or NULL if <blk> is the last block or the last inserted
326 * one.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200327 */
328struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
329{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200330 enum htx_blk_type type;
331 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200332
Christopher Fauletd7884d32019-06-11 10:40:43 +0200333 /* This is the last block in use */
334 if (htx->used == 1) {
335 htx_reset(htx);
336 return NULL;
337 }
338
339 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200340 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200341 sz = htx_get_blksz(blk);
342 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100343 if (type != HTX_BLK_UNUSED) {
344 /* Mark the block as unused, decrement allocated size */
345 htx->data -= htx_get_blksz(blk);
346 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100347 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200348
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200349 /* There is at least 2 blocks, so tail is always > 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200350 if (pos == htx->head) {
351 /* move the head forward */
352 htx->used--;
353 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100354 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200355 else if (pos == htx->tail) {
356 /* remove the tail. this was the last inserted block so
357 * return NULL. */
358 htx->tail--;
359 htx->used--;
360 blk = NULL;
361 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100362 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200363 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200364
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200365 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200366 if (pos == htx->first)
367 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200368
369 if (htx->used == 1) {
370 /* If there is just one block in the HTX message, free space can
371 * be ajusted. This operation could save some defrags. */
372 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
373
374 htx->head_addr = 0;
375 htx->end_addr = lastblk->addr;
376 htx->tail_addr = lastblk->addr+htx->data;
377 }
378 else {
379 if (addr+sz == htx->tail_addr)
380 htx->tail_addr = addr;
381 else if (addr+sz == htx->head_addr)
382 htx->head_addr = addr;
Christopher Faulet8c654862019-06-12 11:08:11 +0200383 if (addr == htx->end_addr) {
384 if (htx->tail_addr == htx->end_addr) {
385 htx->tail_addr = htx->head_addr;
386 htx->head_addr = htx->end_addr = 0;
387 }
388 else
389 htx->end_addr += sz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200390 }
391 }
392
393 BUG_ON((int32_t)htx->tail_addr < 0);
394 BUG_ON((int32_t)htx->head_addr < 0);
395 BUG_ON(htx->end_addr > htx->tail_addr);
396 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100397 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200398}
399
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200400/* Removes all blocks after the one containing the offset <offset>. This last
401 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100402 */
403void htx_truncate(struct htx *htx, uint32_t offset)
404{
405 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100406
Christopher Fauletced39002019-05-23 11:12:43 +0200407 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100408 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200409 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100410
Christopher Fauletced39002019-05-23 11:12:43 +0200411 if (offset >= sz) {
412 offset -= sz;
413 continue;
414 }
Christopher Faulet41dc8432019-06-18 09:49:16 +0200415 if (type == HTX_BLK_DATA)
416 htx_change_blk_value_len(htx, blk, offset);
Christopher Fauletced39002019-05-23 11:12:43 +0200417 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100418 }
419 while (blk)
420 blk = htx_remove_blk(htx, blk);
421}
422
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200423/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
424 * block, it will be cut if necessary. Others blocks will be removed at once if
425 * <count> is large enough. The function returns an htx_ret with the first block
426 * remaing in the messsage and the amount of data drained. If everything is
427 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100428 */
429struct htx_ret htx_drain(struct htx *htx, uint32_t count)
430{
431 struct htx_blk *blk;
432 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
433
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200434 if (count == htx->data) {
435 htx_reset(htx);
436 htxret.ret = count;
437 return htxret;
438 }
439
Christopher Faulet549822f2019-02-25 10:23:19 +0100440 blk = htx_get_head_blk(htx);
441 while (count && blk) {
442 uint32_t sz = htx_get_blksz(blk);
443 enum htx_blk_type type = htx_get_blk_type(blk);
444
445 /* Ingore unused block */
446 if (type == HTX_BLK_UNUSED)
447 goto next;
448
449 if (sz > count) {
450 if (type == HTX_BLK_DATA) {
451 htx_cut_data_blk(htx, blk, count);
452 htxret.ret += count;
453 }
454 break;
455 }
456 count -= sz;
457 htxret.ret += sz;
458 next:
459 blk = htx_remove_blk(htx, blk);
460 }
461 htxret.blk = blk;
462
463 return htxret;
464}
465
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200466/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200467 * there is enough space to take it all. If the space wraps, the buffer is
468 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200469 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200470 * returned. Due to its nature this function can be expensive and should be
471 * avoided whenever possible.
472 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200473struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200474{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200475 struct htx_blk *blk, *tailblk;
476 void *ptr;
477 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200478
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100479 if (!htx->used)
480 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200481
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100482 /* Not enough space to store data */
483 if (data.len > htx_free_data_space(htx))
484 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200485
Christopher Fauletd7884d32019-06-11 10:40:43 +0200486 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200487 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200488 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200489 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200490 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200491
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100492 /* Don't try to append data if the last inserted block is not of the
493 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200494 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100495 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200496
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100497 /*
498 * Same type and enough space: append data
499 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200500 headroom = (htx->end_addr - htx->head_addr);
501 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
502 BUG_ON((int32_t)headroom < 0);
503 BUG_ON((int32_t)tailroom < 0);
504
505 len = data.len;
506 if (tailblk->addr+sz == htx->tail_addr) {
507 if (data.len <= tailroom)
508 goto append_data;
509 else if (!htx->head_addr) {
510 len = tailroom;
511 goto append_data;
512 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200514 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
515 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200516
Christopher Fauletd7884d32019-06-11 10:40:43 +0200517 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200518
519 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100520 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100521 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200522 ptr = htx_get_blk_ptr(htx, tailblk);
523 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +0200524 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200525
Christopher Fauletd7884d32019-06-11 10:40:43 +0200526 if (data.len == len) {
527 blk = tailblk;
528 goto end;
529 }
530 data.ptr += len;
531 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200532
533 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200534 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200535 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100536 if (!blk)
537 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200538
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100539 blk->info += data.len;
540 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200541
542 end:
543 BUG_ON((int32_t)htx->tail_addr < 0);
544 BUG_ON((int32_t)htx->head_addr < 0);
545 BUG_ON(htx->end_addr > htx->tail_addr);
546 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100547 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200548}
549
550/* Replaces a value part of a block by a new one. The new part can be smaller or
551 * larger than the old one. This function works for any kind of block with
552 * attached data. It returns the new block on success, otherwise it returns
553 * NULL.
554 */
555struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100556 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100558 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100559 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200560 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200561
Christopher Fauletd7884d32019-06-11 10:40:43 +0200562 n = htx_get_blk_name(htx, blk);
563 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100564 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200565 ret = htx_prepare_blk_expansion(htx, blk, delta);
566 if (!ret)
567 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100568
Christopher Fauletd7884d32019-06-11 10:40:43 +0200569 /* Before updating the payload, set the new block size and update HTX
570 * message */
571 htx_set_blk_value_len(blk, v.len + delta);
572 htx->data += delta;
573
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200574 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200575 if (delta <= 0) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200576 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200577 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200579 }
580 else {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200581 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200582 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
583 memcpy(old.ptr, new.ptr, new.len);
584 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100585 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200586 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200588
Christopher Fauletd7884d32019-06-11 10:40:43 +0200589 /* Copy the name, if any */
590 memcpy(ptr, n.ptr, n.len);
591 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200592
Christopher Fauletd7884d32019-06-11 10:40:43 +0200593 /* Copy value before old part, if any */
594 memcpy(ptr, v.ptr, old.ptr - v.ptr);
595 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200596
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 /* Copy new value */
598 memcpy(ptr, new.ptr, new.len);
599 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletd7884d32019-06-11 10:40:43 +0200601 /* Copy value after old part, if any */
602 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
603 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200604 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 /* Copy the header name, if any */
608 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200609
Christopher Fauletd7884d32019-06-11 10:40:43 +0200610 /* Copy value before old part, if any */
611 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 /* Copy new value */
614 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200615
Christopher Fauletd7884d32019-06-11 10:40:43 +0200616 /* Copy value after old part if any */
617 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletd7884d32019-06-11 10:40:43 +0200619 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620
Christopher Fauletd7884d32019-06-11 10:40:43 +0200621 /* Finaly, copy data. */
622 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
623 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100624 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625}
626
627/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200628 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200629 * (including payload and meta-data). It returns the number of bytes moved and
630 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631 */
632struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
633 enum htx_blk_type mark)
634{
635 struct htx_blk *blk, *dstblk;
636 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100637 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200638 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639
Christopher Faulet156852b2019-05-16 11:29:13 +0200640 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200641 blk = htx_get_blk(src, htx_get_head(src));
642 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200643
644 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200645 type = htx_get_blk_type(blk);
646
647 /* Ingore unused block */
648 if (type == HTX_BLK_UNUSED)
649 goto next;
650
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200651 /* Be sure to have enough space to xfer all headers/trailers in
652 * one time. If not while <dst> is empty, we report a parsing
653 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200654 */
655 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
656 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
657
658 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
659 if (htx_is_empty(dst))
660 src->flags |= HTX_FL_PARSING_ERROR;
661 break;
662 }
663 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200664 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
665 !inside_trailers && mark >= HTX_BLK_EOT) {
666 inside_trailers = 1;
667 if (htx_used_space(src) > count) {
668 if (htx_is_empty(dst))
669 src->flags |= HTX_FL_PARSING_ERROR;
670 break;
671 }
672 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200673
Christopher Faulet156852b2019-05-16 11:29:13 +0200674 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200675 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200676 max = htx_get_max_blksz(dst, count);
677 if (!max)
678 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200679 if (sz > max) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200680 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200681 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200682 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200683 sz = max;
684 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200685 }
686
687 dstblk = htx_reserve_nxblk(dst, sz);
688 if (!dstblk)
689 break;
690 dstblk->info = info;
691 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
692
Christopher Faulet156852b2019-05-16 11:29:13 +0200693 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694 if (blk->info != info) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200695 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200696 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200697 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 break;
699 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700 next:
701 blk = htx_remove_blk(src, blk);
702 if (type == mark)
703 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200704 }
705
Christopher Faulet156852b2019-05-16 11:29:13 +0200706 end:
707 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200708 return (struct htx_ret){.ret = ret, .blk = dstblk};
709}
710
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200711/* Replaces an header by a new one. The new header can be smaller or larger than
712 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100713 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200714 */
715struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100716 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200717{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100718 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200719 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100720 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200721 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200722
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100723 type = htx_get_blk_type(blk);
724 if (type != HTX_BLK_HDR)
725 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200726
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100727 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200728 ret = htx_prepare_blk_expansion(htx, blk, delta);
729 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100730 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200731
Christopher Fauletd7884d32019-06-11 10:40:43 +0200732 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100734 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100735
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200736 /* Replace in place or at a new address is the same. We replace all the
737 * header (name+value). Only take care to defrag the message if
738 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200739 if (ret == 3)
740 blk = htx_defrag(htx, blk);
741
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100742 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200743 ptr = htx_get_blk_ptr(htx, blk);
744 ist2bin_lc(ptr, name);
745 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100746 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747}
748
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100749/* Replaces the parts of the start-line. It returns the new start-line on
750 * success, otherwise it returns NULL. It is the caller responsibility to update
751 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200752 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100753struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
754 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200755{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200756 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100757 struct htx_sl *sl;
758 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200759 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100760 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200761 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100763 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100764 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100765 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200766
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100767 /* Save start-line info and flags */
768 sl = htx_get_blk_ptr(htx, blk);
769 tmp.info = sl->info;
770 tmp.flags = sl->flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200771 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100772
Christopher Fauletd7884d32019-06-11 10:40:43 +0200773 sz = htx_get_blksz(blk);
774 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
775 ret = htx_prepare_blk_expansion(htx, blk, delta);
776 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100777 return NULL; /* not enough space */
778
Christopher Fauletd7884d32019-06-11 10:40:43 +0200779 /* Set the new block size and update HTX message */
780 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100781 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200782
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200783 /* Replace in place or at a new address is the same. We replace all the
784 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200785 if (ret == 3)
786 blk = htx_defrag(htx, blk);
787
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100788 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100789 sl = htx_get_blk_ptr(htx, blk);
790 sl->info = tmp.info;
791 sl->flags = tmp.flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200792 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200793
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100794 HTX_SL_P1_LEN(sl) = p1.len;
795 HTX_SL_P2_LEN(sl) = p2.len;
796 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100797
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100798 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
799 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
800 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200801
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100802 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803}
804
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100805/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
806 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100808struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
809 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200810{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100811 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100812 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200813 uint32_t size;
814
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100815 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
816 return NULL;
817
818 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200819
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100820 /* FIXME: check size (< 256MB) */
821 blk = htx_add_blk(htx, type, size);
822 if (!blk)
823 return NULL;
824 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200825
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200827 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100828 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100830 HTX_SL_P1_LEN(sl) = p1.len;
831 HTX_SL_P2_LEN(sl) = p2.len;
832 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200833
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100834 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
835 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
836 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
837
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100838 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200839}
840
841/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100842 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200843 */
844struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100845 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200846{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100847 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200848
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100849 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
850 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
851 if (!blk)
852 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200853
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100854 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100855 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100856 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
857 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200858}
859
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200860/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200861 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200862 */
863struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
864 const struct ist value)
865{
866 struct htx_blk *blk;
867
868 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
869 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
870 if (!blk)
871 return NULL;
872
873 blk->info += (value.len << 8) + name.len;
874 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
875 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
876 return blk;
877}
878
Willy Tarreau52610e92019-01-03 18:26:17 +0100879/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
880 * new block on success. Otherwise, it returns NULL. The caller is responsible
881 * for filling the block itself.
882 */
883struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
884{
885 struct htx_blk *blk;
886
887 blk = htx_add_blk(htx, type, blksz);
888 if (!blk)
889 return NULL;
890
891 blk->info += blksz;
892 return blk;
893}
894
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200895/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
896 * the EOH. On sucess, it returns the last block inserted (the EOH), otherwise
897 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200898struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
899{
900 int i;
901
902 for (i = 0; hdrs[i].n.len; i++) {
903 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
904 return NULL;
905 }
906 return htx_add_endof(htx, HTX_BLK_EOH);
907}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200908
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200909/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
910 * the EOT. On sucess, it returns the last block inserted (the EOT), otherwise
911 * NULL is returned. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200912struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
913{
914 int i;
915
916 for (i = 0; hdrs[i].n.len; i++) {
917 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
918 return NULL;
919 }
920 return htx_add_endof(htx, HTX_BLK_EOT);
921}
922
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200923/* Adds an HTX block of type EOH, EOT, or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200924 * on success. Otherwise, it returns NULL.
925 */
926struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
927{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100928 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200929
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100930 blk = htx_add_blk(htx, type, 1);
931 if (!blk)
932 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200933
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100934 blk->info += 1;
935 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200936}
937
938
939/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200940 * possible. It returns the number of bytes consumed from <data>, which may be
941 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200942 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200943size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200944{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200945 struct htx_blk *blk, *tailblk;
946 void *ptr;
947 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200948 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200949
Willy Tarreau0350b902019-05-28 10:58:50 +0200950 if (!htx->used)
951 goto add_new_block;
952
953 /* Not enough space to store data */
954 if (len > htx_free_data_space(htx))
955 len = htx_free_data_space(htx);
956
957 if (!len)
958 return 0;
959
960 /* get the tail and head block */
961 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200962 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200963 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200964 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200965
966 /* Don't try to append data if the last inserted block is not of the
967 * same type */
968 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
969 goto add_new_block;
970
971 /*
972 * Same type and enough space: append data
973 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200974 if (!htx->head_addr) {
975 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200976 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200977 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
Willy Tarreau0350b902019-05-28 10:58:50 +0200978 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200979 else {
980 if (tailblk->addr+sz != htx->head_addr)
981 goto add_new_block;
982 room = (htx->end_addr - htx->head_addr);
983 }
984 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200985 if (room < len)
986 len = room;
987
988 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200989 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200990 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200991 ptr = htx_get_blk_ptr(htx, tailblk);
992 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +0200993 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200994
995 BUG_ON((int32_t)htx->tail_addr < 0);
996 BUG_ON((int32_t)htx->head_addr < 0);
997 BUG_ON(htx->end_addr > htx->tail_addr);
998 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200999 return len;
1000
1001 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001002 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001003 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1004 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001005 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001006
1007 blk->info += len;
1008 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1009 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001010}
1011
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001012
1013/* Adds an HTX block of type DATA in <htx> just after all other DATA
1014 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1015 * DATA block if possible. But, if the function succeeds, it will be the last
1016 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1017 * on success, the updated block (or the new one) is returned.
1018 */
1019struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001020{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001021 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001022
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001023 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001024 if (!blk)
1025 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001026
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001027 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001028 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1029 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001030
Christopher Faulet24ed8352018-11-22 11:20:43 +01001031 /* Swap .addr and .info fields */
1032 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1033 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1034
1035 if (blk->addr == pblk->addr)
1036 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001037 blk = pblk;
1038 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001039
Christopher Faulet24ed8352018-11-22 11:20:43 +01001040 return blk;
1041}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001042
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001043/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1044 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1045 * blocks are updated to remain valid after the move. */
1046void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1047{
1048 struct htx_blk *cblk, *pblk;
1049
1050 cblk = *blk;
1051 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1052 /* Swap .addr and .info fields */
1053 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1054 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1055
1056 if (cblk->addr == pblk->addr)
1057 cblk->addr += htx_get_blksz(pblk);
1058 if (pblk == *ref)
1059 break;
1060 cblk = pblk;
1061 }
1062 *blk = cblk;
1063 *ref = pblk;
1064}
1065
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001066/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
1067 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001068 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001069int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001070{
Christopher Faulet570d1612018-11-26 11:13:57 +01001071 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001072 return 0;
1073
Christopher Faulet570d1612018-11-26 11:13:57 +01001074 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001075 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001076 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001077 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +01001078 if (sl->flags & HTX_SL_F_VER_11)
1079 chunk_memcat(chk, "HTTP/1.1", 8);
1080 else
1081 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1082
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001083 chunk_memcat(chk, "\r\n", 2);
1084
1085 return 1;
1086}
1087
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001088/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
1089 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001090 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001091int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001092{
Christopher Faulet570d1612018-11-26 11:13:57 +01001093 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001094 return 0;
1095
Christopher Faulet1e7af462018-12-03 14:05:01 +01001096 if (sl->flags & HTX_SL_F_VER_11)
1097 chunk_memcat(chk, "HTTP/1.1", 8);
1098 else
1099 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(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_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001102 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001103 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001104 chunk_memcat(chk, "\r\n", 2);
1105
1106 return 1;
1107}
1108
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001109/* Appends the H1 representation of the header <n> witht the value <v> to the
1110 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1111 * returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001112 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001113int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001114{
1115 if (n.len + v.len + 4 > b_room(chk))
1116 return 0;
1117
1118 chunk_memcat(chk, n.ptr, n.len);
1119 chunk_memcat(chk, ": ", 2);
1120 chunk_memcat(chk, v.ptr, v.len);
1121 chunk_memcat(chk, "\r\n", 2);
1122
1123 return 1;
1124}
1125
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001126/* Appends the H1 representation of the data <data> to the chunk <chk>. If
1127 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
1128 * data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001129 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001130int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001131{
1132 if (chunked) {
1133 uint32_t chksz;
1134 char tmp[10];
1135 char *beg, *end;
1136
1137 chksz = data.len;
1138
1139 beg = end = tmp+10;
1140 *--beg = '\n';
1141 *--beg = '\r';
1142 do {
1143 *--beg = hextab[chksz & 0xF];
1144 } while (chksz >>= 4);
1145
1146 if (data.len + (end - beg) + 2 > b_room(chk))
1147 return 0;
1148 chunk_memcat(chk, beg, end - beg);
1149 chunk_memcat(chk, data.ptr, data.len);
1150 chunk_memcat(chk, "\r\n", 2);
1151 }
1152 else {
1153 if (!chunk_memcat(chk, data.ptr, data.len))
1154 return 0;
1155 }
1156
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001157 return 1;
1158}