blob: 8cd7c1efd275a396a6df3fb5d321baba072b0bae [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 Fauletff035cc2020-02-24 11:41:59 +0100400/* Looks for the HTX block containing the offset <offset>, starting at the HTX
401 * message's head. The function returns an htx_ret with the found HTX block and
402 * the position inside this block where the offset is. If the offset <offset> is
403 * ouside of the HTX message, htx_ret.blk is set to NULL.
404 */
405struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset)
406{
407 struct htx_blk *blk;
408 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
409
410 if (offset >= htx->data)
411 return htxret;
412
413 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
414 uint32_t sz = htx_get_blksz(blk);
415
416 if (offset < sz)
417 break;
418 offset -= sz;
419 }
420 htxret.blk = blk;
421 htxret.ret = offset;
422 return htxret;
423}
424
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200425/* Removes all blocks after the one containing the offset <offset>. This last
426 * one may be truncated if it is a DATA block.
Christopher Faulet00cf6972019-01-07 14:53:27 +0100427 */
428void htx_truncate(struct htx *htx, uint32_t offset)
429{
430 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100431
Christopher Fauletced39002019-05-23 11:12:43 +0200432 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100433 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200434 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100435
Christopher Fauletced39002019-05-23 11:12:43 +0200436 if (offset >= sz) {
437 offset -= sz;
438 continue;
439 }
Christopher Faulet41dc8432019-06-18 09:49:16 +0200440 if (type == HTX_BLK_DATA)
441 htx_change_blk_value_len(htx, blk, offset);
Christopher Fauletced39002019-05-23 11:12:43 +0200442 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100443 }
444 while (blk)
445 blk = htx_remove_blk(htx, blk);
446}
447
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200448/* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA
449 * block, it will be cut if necessary. Others blocks will be removed at once if
450 * <count> is large enough. The function returns an htx_ret with the first block
451 * remaing in the messsage and the amount of data drained. If everything is
452 * removed, htx_ret.blk is set to NULL.
Christopher Faulet549822f2019-02-25 10:23:19 +0100453 */
454struct htx_ret htx_drain(struct htx *htx, uint32_t count)
455{
456 struct htx_blk *blk;
457 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
458
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200459 if (count == htx->data) {
Christopher Faulete9a67cc2021-04-22 09:43:47 +0200460 uint32_t flags = htx->flags;
461
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200462 htx_reset(htx);
Christopher Faulete9a67cc2021-04-22 09:43:47 +0200463 htx->flags = flags; /* restore flags */
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200464 htxret.ret = count;
465 return htxret;
466 }
467
Christopher Faulet549822f2019-02-25 10:23:19 +0100468 blk = htx_get_head_blk(htx);
469 while (count && blk) {
470 uint32_t sz = htx_get_blksz(blk);
471 enum htx_blk_type type = htx_get_blk_type(blk);
472
473 /* Ingore unused block */
474 if (type == HTX_BLK_UNUSED)
475 goto next;
476
477 if (sz > count) {
478 if (type == HTX_BLK_DATA) {
479 htx_cut_data_blk(htx, blk, count);
480 htxret.ret += count;
481 }
482 break;
483 }
484 count -= sz;
485 htxret.ret += sz;
486 next:
487 blk = htx_remove_blk(htx, blk);
488 }
489 htxret.blk = blk;
490
491 return htxret;
492}
493
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200494/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200495 * there is enough space to take it all. If the space wraps, the buffer is
496 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200497 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200498 * returned. Due to its nature this function can be expensive and should be
499 * avoided whenever possible.
500 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200501struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200502{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200503 struct htx_blk *blk, *tailblk;
504 void *ptr;
505 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200506
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100507 if (!htx->used)
508 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200509
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100510 /* Not enough space to store data */
511 if (data.len > htx_free_data_space(htx))
512 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200513
Christopher Fauletd7884d32019-06-11 10:40:43 +0200514 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200515 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200516 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200517 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200518 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200519
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100520 /* Don't try to append data if the last inserted block is not of the
521 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200522 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100523 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200524
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100525 /*
526 * Same type and enough space: append data
527 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200528 headroom = (htx->end_addr - htx->head_addr);
529 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
530 BUG_ON((int32_t)headroom < 0);
531 BUG_ON((int32_t)tailroom < 0);
532
533 len = data.len;
534 if (tailblk->addr+sz == htx->tail_addr) {
535 if (data.len <= tailroom)
536 goto append_data;
537 else if (!htx->head_addr) {
538 len = tailroom;
539 goto append_data;
540 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100541 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200542 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
543 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200544
Christopher Fauletd7884d32019-06-11 10:40:43 +0200545 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200546
547 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100548 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100549 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200550 ptr = htx_get_blk_ptr(htx, tailblk);
551 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +0200552 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200553
Christopher Fauletd7884d32019-06-11 10:40:43 +0200554 if (data.len == len) {
555 blk = tailblk;
556 goto end;
557 }
558 data.ptr += len;
559 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200560
561 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200562 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200563 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100564 if (!blk)
565 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100567 blk->info += data.len;
568 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200569
570 end:
571 BUG_ON((int32_t)htx->tail_addr < 0);
572 BUG_ON((int32_t)htx->head_addr < 0);
573 BUG_ON(htx->end_addr > htx->tail_addr);
574 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100575 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200576}
577
578/* Replaces a value part of a block by a new one. The new part can be smaller or
579 * larger than the old one. This function works for any kind of block with
580 * attached data. It returns the new block on success, otherwise it returns
581 * NULL.
582 */
583struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100584 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200585{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100586 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100587 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200588 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200589
Christopher Fauletd7884d32019-06-11 10:40:43 +0200590 n = htx_get_blk_name(htx, blk);
591 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100592 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200593 ret = htx_prepare_blk_expansion(htx, blk, delta);
594 if (!ret)
595 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100596
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 /* Before updating the payload, set the new block size and update HTX
598 * message */
599 htx_set_blk_value_len(blk, v.len + delta);
600 htx->data += delta;
601
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200602 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200603 if (delta <= 0) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200604 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200605 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100606 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 }
608 else {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200609 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200610 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
611 memcpy(old.ptr, new.ptr, new.len);
612 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100613 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200614 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200615 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200616
Christopher Fauletd7884d32019-06-11 10:40:43 +0200617 /* Copy the name, if any */
618 memcpy(ptr, n.ptr, n.len);
619 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620
Christopher Fauletd7884d32019-06-11 10:40:43 +0200621 /* Copy value before old part, if any */
622 memcpy(ptr, v.ptr, old.ptr - v.ptr);
623 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200624
Christopher Fauletd7884d32019-06-11 10:40:43 +0200625 /* Copy new value */
626 memcpy(ptr, new.ptr, new.len);
627 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200628
Christopher Fauletd7884d32019-06-11 10:40:43 +0200629 /* Copy value after old part, if any */
630 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
631 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200632 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200633 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200634
Christopher Fauletd7884d32019-06-11 10:40:43 +0200635 /* Copy the header name, if any */
636 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637
Christopher Fauletd7884d32019-06-11 10:40:43 +0200638 /* Copy value before old part, if any */
639 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200640
Christopher Fauletd7884d32019-06-11 10:40:43 +0200641 /* Copy new value */
642 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200643
Christopher Fauletd7884d32019-06-11 10:40:43 +0200644 /* Copy value after old part if any */
645 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200646
Christopher Fauletd7884d32019-06-11 10:40:43 +0200647 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200648
Christopher Fauletd7884d32019-06-11 10:40:43 +0200649 /* Finaly, copy data. */
650 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
651 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100652 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200653}
654
655/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200656 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200657 * (including payload and meta-data). It returns the number of bytes moved and
658 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200659 */
660struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
661 enum htx_blk_type mark)
662{
663 struct htx_blk *blk, *dstblk;
664 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100665 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200666 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200667
Christopher Faulet156852b2019-05-16 11:29:13 +0200668 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200669 blk = htx_get_blk(src, htx_get_head(src));
670 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200671
672 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200673 type = htx_get_blk_type(blk);
674
675 /* Ingore unused block */
676 if (type == HTX_BLK_UNUSED)
677 goto next;
678
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200679 /* Be sure to have enough space to xfer all headers/trailers in
680 * one time. If not while <dst> is empty, we report a parsing
681 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200682 */
683 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
684 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
685
686 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
687 if (htx_is_empty(dst))
688 src->flags |= HTX_FL_PARSING_ERROR;
689 break;
690 }
691 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200692 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
693 !inside_trailers && mark >= HTX_BLK_EOT) {
694 inside_trailers = 1;
695 if (htx_used_space(src) > count) {
696 if (htx_is_empty(dst))
697 src->flags |= HTX_FL_PARSING_ERROR;
698 break;
699 }
700 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200701
Christopher Faulet156852b2019-05-16 11:29:13 +0200702 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200703 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200704 max = htx_get_max_blksz(dst, count);
705 if (!max)
706 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200707 if (sz > max) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200708 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200709 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200710 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200711 sz = max;
712 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200713 }
714
715 dstblk = htx_reserve_nxblk(dst, sz);
716 if (!dstblk)
717 break;
718 dstblk->info = info;
719 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
720
Christopher Faulet156852b2019-05-16 11:29:13 +0200721 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200722 if (blk->info != info) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200723 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200724 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200725 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200726 break;
727 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200728 next:
729 blk = htx_remove_blk(src, blk);
730 if (type == mark)
731 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200732 }
733
Christopher Faulet156852b2019-05-16 11:29:13 +0200734 end:
735 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736 return (struct htx_ret){.ret = ret, .blk = dstblk};
737}
738
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200739/* Replaces an header by a new one. The new header can be smaller or larger than
740 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100741 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200742 */
743struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100744 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200745{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100746 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200747 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100748 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200749 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200750
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100751 type = htx_get_blk_type(blk);
752 if (type != HTX_BLK_HDR)
753 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200754
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100755 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200756 ret = htx_prepare_blk_expansion(htx, blk, delta);
757 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100758 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200759
Christopher Fauletd7884d32019-06-11 10:40:43 +0200760 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200761 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100762 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100763
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200764 /* Replace in place or at a new address is the same. We replace all the
765 * header (name+value). Only take care to defrag the message if
766 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200767 if (ret == 3)
768 blk = htx_defrag(htx, blk);
769
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100770 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200771 ptr = htx_get_blk_ptr(htx, blk);
772 ist2bin_lc(ptr, name);
773 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100774 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200775}
776
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100777/* Replaces the parts of the start-line. It returns the new start-line on
778 * success, otherwise it returns NULL. It is the caller responsibility to update
779 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200780 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100781struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
782 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200783{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200784 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100785 struct htx_sl *sl;
786 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200787 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100788 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200789 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200790
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100791 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100792 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100793 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200794
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100795 /* Save start-line info and flags */
796 sl = htx_get_blk_ptr(htx, blk);
797 tmp.info = sl->info;
798 tmp.flags = sl->flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200799 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100800
Christopher Fauletd7884d32019-06-11 10:40:43 +0200801 sz = htx_get_blksz(blk);
802 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
803 ret = htx_prepare_blk_expansion(htx, blk, delta);
804 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100805 return NULL; /* not enough space */
806
Christopher Fauletd7884d32019-06-11 10:40:43 +0200807 /* Set the new block size and update HTX message */
808 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100809 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200810
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200811 /* Replace in place or at a new address is the same. We replace all the
812 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200813 if (ret == 3)
814 blk = htx_defrag(htx, blk);
815
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100816 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100817 sl = htx_get_blk_ptr(htx, blk);
818 sl->info = tmp.info;
819 sl->flags = tmp.flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200820 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100822 HTX_SL_P1_LEN(sl) = p1.len;
823 HTX_SL_P2_LEN(sl) = p2.len;
824 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100825
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
827 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
828 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100830 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200831}
832
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100833/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
834 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100836struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
837 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100839 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100840 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200841 uint32_t size;
842
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100843 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
844 return NULL;
845
846 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200847
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100848 /* FIXME: check size (< 256MB) */
849 blk = htx_add_blk(htx, type, size);
850 if (!blk)
851 return NULL;
852 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200853
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100854 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200855 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100856 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200857
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100858 HTX_SL_P1_LEN(sl) = p1.len;
859 HTX_SL_P2_LEN(sl) = p2.len;
860 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200861
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100862 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
863 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
864 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
865
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100866 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200867}
868
869/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100870 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200871 */
872struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100873 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200874{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100875 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200876
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100877 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
878 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
879 if (!blk)
880 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200881
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100882 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100883 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100884 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
885 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200886}
887
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200888/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200889 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200890 */
891struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
892 const struct ist value)
893{
894 struct htx_blk *blk;
895
896 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
897 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
898 if (!blk)
899 return NULL;
900
901 blk->info += (value.len << 8) + name.len;
902 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
903 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
904 return blk;
905}
906
Willy Tarreau52610e92019-01-03 18:26:17 +0100907/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
908 * new block on success. Otherwise, it returns NULL. The caller is responsible
909 * for filling the block itself.
910 */
911struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
912{
913 struct htx_blk *blk;
914
915 blk = htx_add_blk(htx, type, blksz);
916 if (!blk)
917 return NULL;
918
919 blk->info += blksz;
920 return blk;
921}
922
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200923/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
924 * the EOH. On sucess, it returns the last block inserted (the EOH), otherwise
925 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200926struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
927{
928 int i;
929
930 for (i = 0; hdrs[i].n.len; i++) {
931 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
932 return NULL;
933 }
934 return htx_add_endof(htx, HTX_BLK_EOH);
935}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200936
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200937/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
938 * the EOT. On sucess, it returns the last block inserted (the EOT), otherwise
939 * NULL is returned. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200940struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
941{
942 int i;
943
944 for (i = 0; hdrs[i].n.len; i++) {
945 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
946 return NULL;
947 }
948 return htx_add_endof(htx, HTX_BLK_EOT);
949}
950
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200951/* Adds an HTX block of type EOH, EOT, or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200952 * on success. Otherwise, it returns NULL.
953 */
954struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
955{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100956 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200957
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100958 blk = htx_add_blk(htx, type, 1);
959 if (!blk)
960 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200961
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100962 blk->info += 1;
963 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200964}
965
966
967/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200968 * possible. It returns the number of bytes consumed from <data>, which may be
969 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200970 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200971size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200972{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200973 struct htx_blk *blk, *tailblk;
974 void *ptr;
975 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200976 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200977
Willy Tarreau0350b902019-05-28 10:58:50 +0200978 if (!htx->used)
979 goto add_new_block;
980
981 /* Not enough space to store data */
982 if (len > htx_free_data_space(htx))
983 len = htx_free_data_space(htx);
984
985 if (!len)
986 return 0;
987
988 /* get the tail and head block */
989 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200990 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200991 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200992 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200993
994 /* Don't try to append data if the last inserted block is not of the
995 * same type */
996 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
997 goto add_new_block;
998
999 /*
1000 * Same type and enough space: append data
1001 */
Christopher Fauletd7884d32019-06-11 10:40:43 +02001002 if (!htx->head_addr) {
1003 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +02001004 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +02001005 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
Willy Tarreau0350b902019-05-28 10:58:50 +02001006 }
Christopher Fauletd7884d32019-06-11 10:40:43 +02001007 else {
1008 if (tailblk->addr+sz != htx->head_addr)
1009 goto add_new_block;
1010 room = (htx->end_addr - htx->head_addr);
1011 }
1012 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +02001013 if (room < len)
1014 len = room;
1015
1016 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +02001017 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +02001018 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +02001019 ptr = htx_get_blk_ptr(htx, tailblk);
1020 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +02001021 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +02001022
1023 BUG_ON((int32_t)htx->tail_addr < 0);
1024 BUG_ON((int32_t)htx->head_addr < 0);
1025 BUG_ON(htx->end_addr > htx->tail_addr);
1026 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +02001027 return len;
1028
1029 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001030 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001031 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1032 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001033 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001034
1035 blk->info += len;
1036 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1037 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001038}
1039
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001040
1041/* Adds an HTX block of type DATA in <htx> just after all other DATA
1042 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1043 * DATA block if possible. But, if the function succeeds, it will be the last
1044 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1045 * on success, the updated block (or the new one) is returned.
1046 */
1047struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001048{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001049 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001050
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001051 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001052 if (!blk)
1053 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001054
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001055 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001056 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1057 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001058
Christopher Faulet24ed8352018-11-22 11:20:43 +01001059 /* Swap .addr and .info fields */
1060 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1061 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1062
1063 if (blk->addr == pblk->addr)
1064 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001065 blk = pblk;
1066 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001067
Christopher Faulet24ed8352018-11-22 11:20:43 +01001068 return blk;
1069}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001070
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001071/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1072 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1073 * blocks are updated to remain valid after the move. */
1074void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1075{
1076 struct htx_blk *cblk, *pblk;
1077
1078 cblk = *blk;
1079 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1080 /* Swap .addr and .info fields */
1081 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1082 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1083
1084 if (cblk->addr == pblk->addr)
1085 cblk->addr += htx_get_blksz(pblk);
1086 if (pblk == *ref)
1087 break;
1088 cblk = pblk;
1089 }
1090 *blk = cblk;
1091 *ref = pblk;
1092}
1093
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001094/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
1095 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001096 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001097int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001098{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001099 size_t sz = chk->data;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001100
Christopher Faulet3cc76472019-10-14 14:36:51 +02001101 if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) ||
1102 !chunk_memcat(chk, " ", 1) ||
1103 !chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)) ||
1104 !chunk_memcat(chk, " ", 1))
1105 goto full;
Christopher Faulet1e7af462018-12-03 14:05:01 +01001106
Christopher Faulet3cc76472019-10-14 14:36:51 +02001107 if (sl->flags & HTX_SL_F_VER_11) {
1108 if (!chunk_memcat(chk, "HTTP/1.1", 8))
1109 goto full;
1110 }
1111 else {
1112 if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)))
1113 goto full;
1114 }
1115
1116 if (!chunk_memcat(chk, "\r\n", 2))
1117 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001118
1119 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001120
1121 full:
1122 chk->data = sz;
1123 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001124}
1125
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001126/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
1127 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001128 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001129int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001130{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001131 size_t sz = chk->data;
1132
1133 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001134 return 0;
1135
Christopher Faulet3cc76472019-10-14 14:36:51 +02001136 if (sl->flags & HTX_SL_F_VER_11) {
1137 if (!chunk_memcat(chk, "HTTP/1.1", 8))
1138 goto full;
1139 }
1140 else {
1141 if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)))
1142 goto full;
1143 }
1144 if (!chunk_memcat(chk, " ", 1) ||
1145 !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) ||
1146 !chunk_memcat(chk, " ", 1) ||
1147 !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) ||
1148 !chunk_memcat(chk, "\r\n", 2))
1149 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001150
1151 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001152
1153 full:
1154 chk->data = sz;
1155 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001156}
1157
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001158/* Appends the H1 representation of the header <n> witht the value <v> to the
1159 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1160 * returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001161 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001162int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001163{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001164 size_t sz = chk->data;
1165
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001166 if (n.len + v.len + 4 > b_room(chk))
1167 return 0;
1168
Christopher Faulet3cc76472019-10-14 14:36:51 +02001169 if (!chunk_memcat(chk, n.ptr, n.len) ||
1170 !chunk_memcat(chk, ": ", 2) ||
1171 !chunk_memcat(chk, v.ptr, v.len) ||
1172 !chunk_memcat(chk, "\r\n", 2))
1173 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001174
1175 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001176
1177 full:
1178 chk->data = sz;
1179 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001180}
1181
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001182/* Appends the H1 representation of the data <data> to the chunk <chk>. If
1183 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
1184 * data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001185 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001186int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001187{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001188 size_t sz = chk->data;
1189
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001190 if (chunked) {
1191 uint32_t chksz;
1192 char tmp[10];
1193 char *beg, *end;
1194
1195 chksz = data.len;
1196
1197 beg = end = tmp+10;
1198 *--beg = '\n';
1199 *--beg = '\r';
1200 do {
1201 *--beg = hextab[chksz & 0xF];
1202 } while (chksz >>= 4);
1203
Christopher Faulet3cc76472019-10-14 14:36:51 +02001204 if (!chunk_memcat(chk, beg, end - beg) ||
1205 !chunk_memcat(chk, data.ptr, data.len) ||
1206 !chunk_memcat(chk, "\r\n", 2))
1207 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001208 }
1209 else {
1210 if (!chunk_memcat(chk, data.ptr, data.len))
1211 return 0;
1212 }
1213
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001214 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001215
1216 full:
1217 chk->data = sz;
1218 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001219}