blob: cb389c2f74c9d89325b28a8bb54b3142f2c95d88 [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) {
460 htx_reset(htx);
461 htxret.ret = count;
462 return htxret;
463 }
464
Christopher Faulet549822f2019-02-25 10:23:19 +0100465 blk = htx_get_head_blk(htx);
466 while (count && blk) {
467 uint32_t sz = htx_get_blksz(blk);
468 enum htx_blk_type type = htx_get_blk_type(blk);
469
470 /* Ingore unused block */
471 if (type == HTX_BLK_UNUSED)
472 goto next;
473
474 if (sz > count) {
475 if (type == HTX_BLK_DATA) {
476 htx_cut_data_blk(htx, blk, count);
477 htxret.ret += count;
478 }
479 break;
480 }
481 count -= sz;
482 htxret.ret += sz;
483 next:
484 blk = htx_remove_blk(htx, blk);
485 }
486 htxret.blk = blk;
487
488 return htxret;
489}
490
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200491/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200492 * there is enough space to take it all. If the space wraps, the buffer is
493 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200494 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200495 * returned. Due to its nature this function can be expensive and should be
496 * avoided whenever possible.
497 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200498struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200499{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200500 struct htx_blk *blk, *tailblk;
501 void *ptr;
502 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200503
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100504 if (!htx->used)
505 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200506
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100507 /* Not enough space to store data */
508 if (data.len > htx_free_data_space(htx))
509 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200510
Christopher Fauletd7884d32019-06-11 10:40:43 +0200511 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200512 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200513 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200514 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200515 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200516
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100517 /* Don't try to append data if the last inserted block is not of the
518 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200519 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100520 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200521
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100522 /*
523 * Same type and enough space: append data
524 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200525 headroom = (htx->end_addr - htx->head_addr);
526 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
527 BUG_ON((int32_t)headroom < 0);
528 BUG_ON((int32_t)tailroom < 0);
529
530 len = data.len;
531 if (tailblk->addr+sz == htx->tail_addr) {
532 if (data.len <= tailroom)
533 goto append_data;
534 else if (!htx->head_addr) {
535 len = tailroom;
536 goto append_data;
537 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100538 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200539 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
540 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200541
Christopher Fauletd7884d32019-06-11 10:40:43 +0200542 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200543
544 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100545 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100546 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200547 ptr = htx_get_blk_ptr(htx, tailblk);
548 memcpy(ptr+sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +0200549 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200550
Christopher Fauletd7884d32019-06-11 10:40:43 +0200551 if (data.len == len) {
552 blk = tailblk;
553 goto end;
554 }
555 data.ptr += len;
556 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557
558 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200559 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200560 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100561 if (!blk)
562 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200563
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100564 blk->info += data.len;
565 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200566
567 end:
568 BUG_ON((int32_t)htx->tail_addr < 0);
569 BUG_ON((int32_t)htx->head_addr < 0);
570 BUG_ON(htx->end_addr > htx->tail_addr);
571 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100572 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200573}
574
575/* Replaces a value part of a block by a new one. The new part can be smaller or
576 * larger than the old one. This function works for any kind of block with
577 * attached data. It returns the new block on success, otherwise it returns
578 * NULL.
579 */
580struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100581 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200582{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100583 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100584 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200585 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200586
Christopher Fauletd7884d32019-06-11 10:40:43 +0200587 n = htx_get_blk_name(htx, blk);
588 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100589 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200590 ret = htx_prepare_blk_expansion(htx, blk, delta);
591 if (!ret)
592 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100593
Christopher Fauletd7884d32019-06-11 10:40:43 +0200594 /* Before updating the payload, set the new block size and update HTX
595 * message */
596 htx_set_blk_value_len(blk, v.len + delta);
597 htx->data += delta;
598
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200599 if (ret == 1) { /* Replace in place */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200600 if (delta <= 0) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200601 /* compression: copy new data first then move the end */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200602 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100603 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200604 }
605 else {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200606 /* expansion: move the end first then copy new data */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
608 memcpy(old.ptr, new.ptr, new.len);
609 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100610 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200611 else if (ret == 2) { /* New address but no defrag */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200612 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200613
Christopher Fauletd7884d32019-06-11 10:40:43 +0200614 /* Copy the name, if any */
615 memcpy(ptr, n.ptr, n.len);
616 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200617
Christopher Fauletd7884d32019-06-11 10:40:43 +0200618 /* Copy value before old part, if any */
619 memcpy(ptr, v.ptr, old.ptr - v.ptr);
620 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200621
Christopher Fauletd7884d32019-06-11 10:40:43 +0200622 /* Copy new value */
623 memcpy(ptr, new.ptr, new.len);
624 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625
Christopher Fauletd7884d32019-06-11 10:40:43 +0200626 /* Copy value after old part, if any */
627 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
628 }
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200629 else { /* Do a degrag first */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200630 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631
Christopher Fauletd7884d32019-06-11 10:40:43 +0200632 /* Copy the header name, if any */
633 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200634
Christopher Fauletd7884d32019-06-11 10:40:43 +0200635 /* Copy value before old part, if any */
636 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200637
Christopher Fauletd7884d32019-06-11 10:40:43 +0200638 /* Copy new value */
639 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200640
Christopher Fauletd7884d32019-06-11 10:40:43 +0200641 /* Copy value after old part if any */
642 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200643
Christopher Fauletd7884d32019-06-11 10:40:43 +0200644 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200645
Christopher Fauletd7884d32019-06-11 10:40:43 +0200646 /* Finaly, copy data. */
647 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
648 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100649 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200650}
651
652/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200653 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200654 * (including payload and meta-data). It returns the number of bytes moved and
655 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200656 */
657struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
658 enum htx_blk_type mark)
659{
660 struct htx_blk *blk, *dstblk;
661 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100662 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200663 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200664
Christopher Faulet156852b2019-05-16 11:29:13 +0200665 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200666 blk = htx_get_blk(src, htx_get_head(src));
667 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200668
669 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200670 type = htx_get_blk_type(blk);
671
672 /* Ingore unused block */
673 if (type == HTX_BLK_UNUSED)
674 goto next;
675
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200676 /* Be sure to have enough space to xfer all headers/trailers in
677 * one time. If not while <dst> is empty, we report a parsing
678 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200679 */
680 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
681 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
682
683 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
684 if (htx_is_empty(dst))
685 src->flags |= HTX_FL_PARSING_ERROR;
686 break;
687 }
688 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200689 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
690 !inside_trailers && mark >= HTX_BLK_EOT) {
691 inside_trailers = 1;
692 if (htx_used_space(src) > count) {
693 if (htx_is_empty(dst))
694 src->flags |= HTX_FL_PARSING_ERROR;
695 break;
696 }
697 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698
Christopher Faulet156852b2019-05-16 11:29:13 +0200699 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200701 max = htx_get_max_blksz(dst, count);
702 if (!max)
703 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200704 if (sz > max) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200705 /* Only DATA blocks can be partially xferred */
Christopher Faulet156852b2019-05-16 11:29:13 +0200706 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200707 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200708 sz = max;
709 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200710 }
711
712 dstblk = htx_reserve_nxblk(dst, sz);
713 if (!dstblk)
714 break;
715 dstblk->info = info;
716 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
717
Christopher Faulet156852b2019-05-16 11:29:13 +0200718 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200719 if (blk->info != info) {
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200720 /* Partial xfer: don't remove <blk> from <src> but
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200721 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200722 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200723 break;
724 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200725 next:
726 blk = htx_remove_blk(src, blk);
727 if (type == mark)
728 break;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200729 }
730
Christopher Faulet156852b2019-05-16 11:29:13 +0200731 end:
732 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200733 return (struct htx_ret){.ret = ret, .blk = dstblk};
734}
735
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200736/* Replaces an header by a new one. The new header can be smaller or larger than
737 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100738 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200739 */
740struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100741 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200742{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100743 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200744 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100745 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200746 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200747
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100748 type = htx_get_blk_type(blk);
749 if (type != HTX_BLK_HDR)
750 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200751
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100752 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200753 ret = htx_prepare_blk_expansion(htx, blk, delta);
754 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100755 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200756
Christopher Fauletd7884d32019-06-11 10:40:43 +0200757 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200758 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100759 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100760
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200761 /* Replace in place or at a new address is the same. We replace all the
762 * header (name+value). Only take care to defrag the message if
763 * necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200764 if (ret == 3)
765 blk = htx_defrag(htx, blk);
766
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100767 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200768 ptr = htx_get_blk_ptr(htx, blk);
769 ist2bin_lc(ptr, name);
770 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100771 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772}
773
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100774/* Replaces the parts of the start-line. It returns the new start-line on
775 * success, otherwise it returns NULL. It is the caller responsibility to update
776 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200777 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
779 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200780{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200781 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100782 struct htx_sl *sl;
783 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200784 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100785 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200786 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200787
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100788 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100789 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100790 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200791
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100792 /* Save start-line info and flags */
793 sl = htx_get_blk_ptr(htx, blk);
794 tmp.info = sl->info;
795 tmp.flags = sl->flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200796 tmp.hdrs_bytes = sl->hdrs_bytes;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100797
Christopher Fauletd7884d32019-06-11 10:40:43 +0200798 sz = htx_get_blksz(blk);
799 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
800 ret = htx_prepare_blk_expansion(htx, blk, delta);
801 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100802 return NULL; /* not enough space */
803
Christopher Fauletd7884d32019-06-11 10:40:43 +0200804 /* Set the new block size and update HTX message */
805 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100806 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200807
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200808 /* Replace in place or at a new address is the same. We replace all the
809 * start-line. Only take care to defrag the message if necessary. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200810 if (ret == 3)
811 blk = htx_defrag(htx, blk);
812
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100813 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100814 sl = htx_get_blk_ptr(htx, blk);
815 sl->info = tmp.info;
816 sl->flags = tmp.flags;
Christopher Faulet703bed52019-06-25 21:31:26 +0200817 sl->hdrs_bytes = tmp.hdrs_bytes;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200818
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100819 HTX_SL_P1_LEN(sl) = p1.len;
820 HTX_SL_P2_LEN(sl) = p2.len;
821 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100822
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100823 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
824 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
825 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200826
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100827 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200828}
829
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100830/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
831 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200832 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100833struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
834 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100836 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100837 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200838 uint32_t size;
839
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100840 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
841 return NULL;
842
843 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200844
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100845 /* FIXME: check size (< 256MB) */
846 blk = htx_add_blk(htx, type, size);
847 if (!blk)
848 return NULL;
849 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200850
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100851 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200852 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100853 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200854
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100855 HTX_SL_P1_LEN(sl) = p1.len;
856 HTX_SL_P2_LEN(sl) = p2.len;
857 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200858
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100859 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
860 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
861 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
862
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100863 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200864}
865
866/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100867 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200868 */
869struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100870 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200871{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100872 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200873
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100874 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
875 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
876 if (!blk)
877 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200878
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100879 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100880 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100881 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
882 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200883}
884
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200885/* Adds an HTX block of type TLR in <htx>. It returns the new block on
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200886 * success. Otherwise, it returns NULL. The trailer name is always lower cased.
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200887 */
888struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
889 const struct ist value)
890{
891 struct htx_blk *blk;
892
893 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
894 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
895 if (!blk)
896 return NULL;
897
898 blk->info += (value.len << 8) + name.len;
899 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
900 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
901 return blk;
902}
903
Willy Tarreau52610e92019-01-03 18:26:17 +0100904/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
905 * new block on success. Otherwise, it returns NULL. The caller is responsible
906 * for filling the block itself.
907 */
908struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
909{
910 struct htx_blk *blk;
911
912 blk = htx_add_blk(htx, type, blksz);
913 if (!blk)
914 return NULL;
915
916 blk->info += blksz;
917 return blk;
918}
919
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200920/* Add all headers from the list <hdrs> into the HTX message <htx>, followed by
921 * the EOH. On sucess, it returns the last block inserted (the EOH), otherwise
922 * NULL is returned. */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200923struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
924{
925 int i;
926
927 for (i = 0; hdrs[i].n.len; i++) {
928 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
929 return NULL;
930 }
931 return htx_add_endof(htx, HTX_BLK_EOH);
932}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200933
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200934/* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by
935 * the EOT. On sucess, it returns the last block inserted (the EOT), otherwise
936 * NULL is returned. */
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200937struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
938{
939 int i;
940
941 for (i = 0; hdrs[i].n.len; i++) {
942 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
943 return NULL;
944 }
945 return htx_add_endof(htx, HTX_BLK_EOT);
946}
947
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +0200948/* Adds an HTX block of type EOH, EOT, or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200949 * on success. Otherwise, it returns NULL.
950 */
951struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
952{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100953 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200954
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100955 blk = htx_add_blk(htx, type, 1);
956 if (!blk)
957 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200958
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100959 blk->info += 1;
960 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200961}
962
963
964/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200965 * possible. It returns the number of bytes consumed from <data>, which may be
966 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200967 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200968size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200969{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200970 struct htx_blk *blk, *tailblk;
971 void *ptr;
972 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200973 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200974
Willy Tarreau0350b902019-05-28 10:58:50 +0200975 if (!htx->used)
976 goto add_new_block;
977
978 /* Not enough space to store data */
979 if (len > htx_free_data_space(htx))
980 len = htx_free_data_space(htx);
981
982 if (!len)
983 return 0;
984
985 /* get the tail and head block */
986 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200987 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200988 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200989 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200990
991 /* Don't try to append data if the last inserted block is not of the
992 * same type */
993 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
994 goto add_new_block;
995
996 /*
997 * Same type and enough space: append data
998 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200999 if (!htx->head_addr) {
1000 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +02001001 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +02001002 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
Willy Tarreau0350b902019-05-28 10:58:50 +02001003 }
Christopher Fauletd7884d32019-06-11 10:40:43 +02001004 else {
1005 if (tailblk->addr+sz != htx->head_addr)
1006 goto add_new_block;
1007 room = (htx->end_addr - htx->head_addr);
1008 }
1009 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +02001010 if (room < len)
1011 len = room;
1012
1013 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +02001014 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +02001015 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +02001016 ptr = htx_get_blk_ptr(htx, tailblk);
1017 memcpy(ptr + sz, data.ptr, len);
Christopher Faulet41dc8432019-06-18 09:49:16 +02001018 htx_change_blk_value_len(htx, tailblk, sz+len);
Christopher Fauletd7884d32019-06-11 10:40:43 +02001019
1020 BUG_ON((int32_t)htx->tail_addr < 0);
1021 BUG_ON((int32_t)htx->head_addr < 0);
1022 BUG_ON(htx->end_addr > htx->tail_addr);
1023 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +02001024 return len;
1025
1026 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001027 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001028 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1029 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001030 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001031
1032 blk->info += len;
1033 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1034 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001035}
1036
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001037
1038/* Adds an HTX block of type DATA in <htx> just after all other DATA
1039 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1040 * DATA block if possible. But, if the function succeeds, it will be the last
1041 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1042 * on success, the updated block (or the new one) is returned.
1043 */
1044struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001045{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001046 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001047
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001048 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001049 if (!blk)
1050 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001051
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001052 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001053 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1054 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001055
Christopher Faulet24ed8352018-11-22 11:20:43 +01001056 /* Swap .addr and .info fields */
1057 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1058 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1059
1060 if (blk->addr == pblk->addr)
1061 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001062 blk = pblk;
1063 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001064
Christopher Faulet24ed8352018-11-22 11:20:43 +01001065 return blk;
1066}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001067
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001068/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1069 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1070 * blocks are updated to remain valid after the move. */
1071void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1072{
1073 struct htx_blk *cblk, *pblk;
1074
1075 cblk = *blk;
1076 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1077 /* Swap .addr and .info fields */
1078 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1079 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1080
1081 if (cblk->addr == pblk->addr)
1082 cblk->addr += htx_get_blksz(pblk);
1083 if (pblk == *ref)
1084 break;
1085 cblk = pblk;
1086 }
1087 *blk = cblk;
1088 *ref = pblk;
1089}
1090
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001091/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
1092 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001093 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001094int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001095{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001096 size_t sz = chk->data;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001097
Christopher Faulet3cc76472019-10-14 14:36:51 +02001098 if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) ||
1099 !chunk_memcat(chk, " ", 1) ||
1100 !chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)) ||
1101 !chunk_memcat(chk, " ", 1))
1102 goto full;
Christopher Faulet1e7af462018-12-03 14:05:01 +01001103
Christopher Faulet3cc76472019-10-14 14:36:51 +02001104 if (sl->flags & HTX_SL_F_VER_11) {
1105 if (!chunk_memcat(chk, "HTTP/1.1", 8))
1106 goto full;
1107 }
1108 else {
1109 if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)))
1110 goto full;
1111 }
1112
1113 if (!chunk_memcat(chk, "\r\n", 2))
1114 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001115
1116 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001117
1118 full:
1119 chk->data = sz;
1120 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001121}
1122
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001123/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
1124 * returns 1 if data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001125 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001126int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001127{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001128 size_t sz = chk->data;
1129
1130 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001131 return 0;
1132
Christopher Faulet3cc76472019-10-14 14:36:51 +02001133 if (sl->flags & HTX_SL_F_VER_11) {
1134 if (!chunk_memcat(chk, "HTTP/1.1", 8))
1135 goto full;
1136 }
1137 else {
1138 if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)))
1139 goto full;
1140 }
1141 if (!chunk_memcat(chk, " ", 1) ||
1142 !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) ||
1143 !chunk_memcat(chk, " ", 1) ||
1144 !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) ||
1145 !chunk_memcat(chk, "\r\n", 2))
1146 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001147
1148 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001149
1150 full:
1151 chk->data = sz;
1152 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001153}
1154
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001155/* Appends the H1 representation of the header <n> witht the value <v> to the
1156 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1157 * returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001158 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001159int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001160{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001161 size_t sz = chk->data;
1162
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001163 if (n.len + v.len + 4 > b_room(chk))
1164 return 0;
1165
Christopher Faulet3cc76472019-10-14 14:36:51 +02001166 if (!chunk_memcat(chk, n.ptr, n.len) ||
1167 !chunk_memcat(chk, ": ", 2) ||
1168 !chunk_memcat(chk, v.ptr, v.len) ||
1169 !chunk_memcat(chk, "\r\n", 2))
1170 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001171
1172 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001173
1174 full:
1175 chk->data = sz;
1176 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001177}
1178
Christopher Faulet4eb8c3d2019-06-19 13:48:09 +02001179/* Appends the H1 representation of the data <data> to the chunk <chk>. If
1180 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
1181 * data are successfully appended, otherwise it returns 0.
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001182 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001183int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001184{
Christopher Faulet3cc76472019-10-14 14:36:51 +02001185 size_t sz = chk->data;
1186
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001187 if (chunked) {
1188 uint32_t chksz;
1189 char tmp[10];
1190 char *beg, *end;
1191
1192 chksz = data.len;
1193
1194 beg = end = tmp+10;
1195 *--beg = '\n';
1196 *--beg = '\r';
1197 do {
1198 *--beg = hextab[chksz & 0xF];
1199 } while (chksz >>= 4);
1200
Christopher Faulet3cc76472019-10-14 14:36:51 +02001201 if (!chunk_memcat(chk, beg, end - beg) ||
1202 !chunk_memcat(chk, data.ptr, data.len) ||
1203 !chunk_memcat(chk, "\r\n", 2))
1204 goto full;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001205 }
1206 else {
1207 if (!chunk_memcat(chk, data.ptr, data.len))
1208 return 0;
1209 }
1210
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001211 return 1;
Christopher Faulet3cc76472019-10-14 14:36:51 +02001212
1213 full:
1214 chk->data = sz;
1215 return 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001216}