blob: 8be38d6f70cece88ea084fe0870bc9ee943a4725 [file] [log] [blame]
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001/*
2 * internal HTTP message
3 *
4 * Copyright 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/chunk.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010014#include <common/htx.h>
Christopher Fauleta3d2a162018-10-22 08:59:39 +020015
16struct htx htx_empty = { .size = 0, .data = 0, .used = 0 };
17
18/* Defragments an HTTP message, removing unused blocks and unwrapping blocks and
19 * their contents. A temporary message is used to do so. This function never
20 * fails. if <blk> is not NULL, we replace it by the new block address, after
21 * the defragmentation. The new <blk> is returned.
22 */
23/* TODO: merge data blocks into one */
24struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk)
25{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010026 struct buffer *chunk = get_trash_chunk();
27 struct htx *tmp = htxbuf(chunk);
28 struct htx_blk *newblk, *oldblk;
Christopher Faulet200f8952019-01-02 11:23:44 +010029 uint32_t new, old, blkpos;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010030 uint32_t addr, blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +020031 int32_t first = -1;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020032
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010033 if (!htx->used)
34 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020035
Christopher Faulet200f8952019-01-02 11:23:44 +010036 blkpos = -1;
37
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010038 new = 0;
39 addr = 0;
40 tmp->size = htx->size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020041
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010042 /* start from the head */
43 for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) {
44 oldblk = htx_get_blk(htx, old);
Christopher Faulet28f29c72019-04-30 17:55:45 +020045 if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010046 continue;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020047
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010048 newblk = htx_get_blk(tmp, new);
49 newblk->addr = addr;
50 newblk->info = oldblk->info;
51 blksz = htx_get_blksz(oldblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020052
Christopher Faulet9c66b982019-04-30 18:08:26 +020053 /* update the start-line position */
Christopher Faulet29f17582019-05-23 11:03:26 +020054 if (htx->first == old)
55 first = new;
Christopher Faulet174bfb12018-12-06 14:31:12 +010056
Christopher Faulet200f8952019-01-02 11:23:44 +010057 /* if <blk> is defined, set its new position */
58 if (blk != NULL && blk == oldblk)
59 blkpos = new;
60
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010061 memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz);
62 new++;
63 addr += blksz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020064
Christopher Fauletb8fd4c02019-05-20 09:32:25 +020065 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +020066
Christopher Faulet28f29c72019-04-30 17:55:45 +020067 htx->used = new;
Christopher Faulet29f17582019-05-23 11:03:26 +020068 htx->first = first;
Christopher Faulet28f29c72019-04-30 17:55:45 +020069 htx->head = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010070 htx->front = htx->tail = new - 1;
71 memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +020072
Christopher Faulet200f8952019-01-02 11:23:44 +010073 return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos));
Christopher Fauleta3d2a162018-10-22 08:59:39 +020074}
75
76/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
77 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
78 * block is returned and the HTTP message is updated. Space for this new block
79 * is reserved in the HTTP message. But it is the caller responsibility to set
80 * right info in the block to reflect the stored data.
81 */
82static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
83{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010084 struct htx_blk *blk, *prevblk, *headblk, *frtblk;
85 uint32_t used;
86 uint32_t tail;
87 uint32_t prev;
88 uint32_t wrap;
89 uint32_t head;
90 int32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +020091
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010092 if (blksz > htx_free_data_space(htx))
93 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +020094
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010095 if (!htx->used) {
96 /* Empty message */
Christopher Faulet29f17582019-05-23 11:03:26 +020097 htx->front = htx->head = htx->tail = htx->first = 0;
Christopher Faulet28f29c72019-04-30 17:55:45 +020098 htx->used = 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +010099 blk = htx_get_blk(htx, htx->tail);
100 blk->addr = 0;
101 htx->data = blksz;
102 return blk;
103 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200104
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100105 used = htx->used + 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100106 prev = htx->tail;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200107 head = htx->head;
108 tail = htx->tail + 1;
109 wrap = htx_get_wrap(htx);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200110
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100111 if (tail == wrap) {
112 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200113
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100114 /* Blocks don't wrap for now. We either need to push the new one
115 * on top of others or to defragement the table. */
116 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap+1) >= frtblk->addr + htx_get_blksz(frtblk))
117 wrap++;
118 else if (tail >= used) /* There is hole at the beginning */
119 tail = 0;
120 else {
121 /* No more room, tail hits data. We have to realign the
122 * whole message. */
123 goto defrag;
124 }
125 }
126 else if (used >= wrap) {
127 /* We have hit the tail, we need to reorganize the blocks. */
128 goto defrag;
129 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200130
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100131 /* Now we have updated tail, used and wrap, we know that there is some
132 * available room at least from the protocol's perspective. This space
133 * is split in two areas :
134 *
135 * 1: the space between the beginning of the blocks table and the
136 * front data's address. This space will only be used if data don't
137 * wrap yet.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200138
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100139 * 2: If the previous tail was the front block, the space between the
140 * beginning of the message and the head data's address.
141 * Otherwise, the space between the tail data's address and the
142 * tail's one.
143 */
144 prevblk = htx_get_blk(htx, prev);
145 headblk = htx_get_blk(htx, head);
146 if (prevblk->addr >= headblk->addr) {
147 /* the area was contiguous */
148 frtblk = htx_get_blk(htx, htx->front);
149 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap) - (frtblk->addr + htx_get_blksz(frtblk));
150 headroom = headblk->addr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200151
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 if (tailroom >= (int32_t)blksz) {
153 /* install upfront and update ->front */
154 blk = htx_get_blk(htx, tail);
155 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
156 htx->front = tail;
157 }
158 else if (headroom >= (int32_t)blksz) {
159 blk = htx_get_blk(htx, tail);
160 blk->addr = 0;
161 }
162 else {
163 /* need to defragment the table before inserting upfront */
164 goto defrag;
165 }
166 }
167 else {
168 /* it's already wrapped so we can't store anything in the tailroom */
169 headroom = headblk->addr - (prevblk->addr + htx_get_blksz(prevblk));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200170
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100171 if (headroom >= (int32_t)blksz) {
172 blk = htx_get_blk(htx, tail);
173 blk->addr = prevblk->addr + htx_get_blksz(prevblk);
174 }
175 else {
176 defrag:
177 /* need to defragment the table before inserting upfront */
178 htx_defrag(htx, NULL);
179 frtblk = htx_get_blk(htx, htx->front);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100180 tail = htx->tail + 1;
181 used = htx->used + 1;
182 blk = htx_get_blk(htx, tail);
183 blk->addr = frtblk->addr + htx_get_blksz(frtblk);
184 htx->front = tail;
185 }
186 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200187
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100188 htx->tail = tail;
189 htx->used = used;
190 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200191 /* Set first position if not already set */
192 if (htx->first == -1)
193 htx->first = tail;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100194 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200195}
196
197/* Adds a new block of type <type> in the HTTP message <htx>. Its content size
198 * is passed but it is the caller responsibility to do the copy.
199 */
200struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
201{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100202 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200203
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100204 blk = htx_reserve_nxblk(htx, blksz);
205 if (!blk)
206 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200207
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100208 blk->info = (type << 28);
209 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200210}
211
212/* Removes the block <blk> from the HTTP message <htx>. The function returns the
213 * block following <blk> or NULL if <blk> is the last block or the last
214 * inserted one.
215 */
216struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
217{
Christopher Faulet54483df2018-11-26 15:05:52 +0100218 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet28f29c72019-04-30 17:55:45 +0200219 uint32_t next, pos, wrap;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200220
Christopher Faulet9c66b982019-04-30 18:08:26 +0200221 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100222 if (type != HTX_BLK_UNUSED) {
223 /* Mark the block as unused, decrement allocated size */
224 htx->data -= htx_get_blksz(blk);
225 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100226 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200227
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100228 /* This is the last block in use */
Christopher Faulet29f17582019-05-23 11:03:26 +0200229 if (htx->used == 1) {
230 htx_reset(htx);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100231 return NULL;
232 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200233
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100234 /* There is at least 2 blocks, so tail is always >= 0 */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100235 blk = NULL;
236 next = pos + 1; /* By default retrun the next block */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200237 wrap = htx_get_wrap(htx);
238 if (htx->tail + 1 == wrap) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100239 /* The HTTP message doesn't wrap */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200240 if (pos == htx->head) {
241 /* move the head forward */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100242 htx->used--;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200243 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100244 }
245 else if (pos == htx->tail) {
246 /* remove the tail. this was the last inserted block so
247 * return NULL. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100248 htx->tail--;
249 htx->used--;
250 goto end;
251 }
252 }
253 else {
254 /* The HTTP message wraps */
255 if (pos == htx->tail) {
256 /* remove the tail. try to unwrap the message (pos == 0)
257 * and return NULL. */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200258 htx->tail = ((pos == 0) ? wrap-1 : htx->tail-1);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100259 htx->used--;
260 goto end;
261 }
Christopher Faulet28f29c72019-04-30 17:55:45 +0200262 else if (pos == htx->head) {
263 /* move the head forward and try to unwrap the message
264 * (head+1 == wrap) */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100265 htx->used--;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200266 htx->head++;
267 if (htx->head == wrap)
268 htx->head = next = 0;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100269 }
270 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200271
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100272 blk = htx_get_blk(htx, next);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200273 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200274 if (pos == htx->first)
275 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100276 if (pos == htx->front)
277 htx->front = htx_find_front(htx);
278 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200279}
280
Christopher Faulet00cf6972019-01-07 14:53:27 +0100281/* Truncate all blocks after the one containing the offset <offset>. This last
282 * one is truncated too.
283 */
284void htx_truncate(struct htx *htx, uint32_t offset)
285{
286 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100287
Christopher Fauletced39002019-05-23 11:12:43 +0200288 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100289 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200290 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100291
Christopher Fauletced39002019-05-23 11:12:43 +0200292 if (offset >= sz) {
293 offset -= sz;
294 continue;
295 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200296 if (type == HTX_BLK_DATA) {
Christopher Fauletced39002019-05-23 11:12:43 +0200297 htx_set_blk_value_len(blk, offset);
298 htx->data -= (sz - offset);
299 }
300 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100301 }
302 while (blk)
303 blk = htx_remove_blk(htx, blk);
304}
305
Christopher Faulet549822f2019-02-25 10:23:19 +0100306/* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if
307 * necessary. Others blocks will be removed at once if <count> is large
308 * enough. The function returns an htx_ret with the first block remaing in the
309 * messsage and the amount of data drained. If everything is removed,
310 * htx_ret.blk is set to NULL.
311 */
312struct htx_ret htx_drain(struct htx *htx, uint32_t count)
313{
314 struct htx_blk *blk;
315 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
316
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200317 if (count == htx->data) {
318 htx_reset(htx);
319 htxret.ret = count;
320 return htxret;
321 }
322
Christopher Faulet549822f2019-02-25 10:23:19 +0100323 blk = htx_get_head_blk(htx);
324 while (count && blk) {
325 uint32_t sz = htx_get_blksz(blk);
326 enum htx_blk_type type = htx_get_blk_type(blk);
327
328 /* Ingore unused block */
329 if (type == HTX_BLK_UNUSED)
330 goto next;
331
332 if (sz > count) {
333 if (type == HTX_BLK_DATA) {
334 htx_cut_data_blk(htx, blk, count);
335 htxret.ret += count;
336 }
337 break;
338 }
339 count -= sz;
340 htxret.ret += sz;
341 next:
342 blk = htx_remove_blk(htx, blk);
343 }
344 htxret.blk = blk;
345
346 return htxret;
347}
348
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200349/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200350 * there is enough space to take it all. If the space wraps, the buffer is
351 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200352 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200353 * returned. Due to its nature this function can be expensive and should be
354 * avoided whenever possible.
355 */
356struct htx_blk *htx_add_data_atonce(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200357{
Christopher Fauletf1449b72019-04-10 14:54:46 +0200358 struct htx_blk *blk, *tailblk, *headblk, *frtblk;
359 struct ist v;
360 int32_t room;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200361
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100362 if (!htx->used)
363 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200364
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100365 /* Not enough space to store data */
366 if (data.len > htx_free_data_space(htx))
367 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200368
Christopher Fauletf1449b72019-04-10 14:54:46 +0200369 /* get the tail and head block */
370 tailblk = htx_get_tail_blk(htx);
371 headblk = htx_get_head_blk(htx);
372 if (tailblk == NULL || headblk == NULL)
373 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200374
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100375 /* Don't try to append data if the last inserted block is not of the
376 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200377 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100378 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200379
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100380 /*
381 * Same type and enough space: append data
382 */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200383 frtblk = htx_get_blk(htx, htx->front);
384 if (tailblk->addr >= headblk->addr) {
385 if (htx->tail != htx->front)
386 goto add_new_block;
387 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100388 }
Christopher Fauletf1449b72019-04-10 14:54:46 +0200389 else
390 room = headblk->addr - (tailblk->addr + htx_get_blksz(tailblk));
391
392 if (room < (int32_t)data.len)
393 tailblk = htx_defrag(htx, tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200394
395 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100396 /* get the value of the tail block */
397 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200398 v = htx_get_blk_value(htx, tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200399
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100400 /* Append data and update the block itself */
401 memcpy(v.ptr + v.len, data.ptr, data.len);
Christopher Fauletf1449b72019-04-10 14:54:46 +0200402 htx_set_blk_value_len(tailblk, v.len + data.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200403
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100404 /* Update HTTP message */
405 htx->data += data.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200406
Christopher Fauletf1449b72019-04-10 14:54:46 +0200407 return tailblk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200408
409 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200410 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200411 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100412 if (!blk)
413 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200414
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100415 blk->info += data.len;
416 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
417 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200418}
419
420/* Replaces a value part of a block by a new one. The new part can be smaller or
421 * larger than the old one. This function works for any kind of block with
422 * attached data. It returns the new block on success, otherwise it returns
423 * NULL.
424 */
425struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100426 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200427{
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100428 struct buffer *tmp;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100429 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100430 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200431
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100432 n = htx_get_blk_name(htx, blk);
433 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200434
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100435 delta = new.len - old.len;
436
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100437 /* easy case, new data are smaller, so replace it in-place */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100438 if (delta <= 0) {
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100439 memcpy(old.ptr, new.ptr, new.len);
440 if (old.len != v.len)
441 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100442 htx_set_blk_value_len(blk, v.len + delta);
443 htx->data += delta;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100444 return blk;
445 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200446
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100447 /* we need to allocate more space to store the new header value */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100448 if (delta > htx_free_space(htx))
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100449 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200450
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100451 /*
452 * Copy the new header in a temp buffer
453 */
454 tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200455
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100456 /* 1. copy the header name */
457 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200458
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100459 /* 2. copy value before old part, if any */
460 if (old.ptr != v.ptr)
461 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200462
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100463 /* 3. copy new value */
464 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200465
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100466 /* 4. copy value after old part if any */
467 if (old.len != v.len)
468 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200469
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200470
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100471 /* Expand the block size. But data are not copied yet. Then defragment
472 * the HTX message.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100473 */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100474 htx_set_blk_value_len(blk, v.len + delta);
475 htx->data += delta;
476 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200477
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100478 /* Finaly, copy data. */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100479 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200480
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100481 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200482}
483
484/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200485 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200486 * (including payload and meta-data). It returns the number of bytes moved and
487 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200488 */
489struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
490 enum htx_blk_type mark)
491{
492 struct htx_blk *blk, *dstblk;
493 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100494 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200495 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200496
Christopher Faulet156852b2019-05-16 11:29:13 +0200497 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200498 blk = htx_get_blk(src, htx_get_head(src));
499 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200500
501 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200502 type = htx_get_blk_type(blk);
503
504 /* Ingore unused block */
505 if (type == HTX_BLK_UNUSED)
506 goto next;
507
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200508 /* Be sure to have enough space to xfer all headers/trailers in
509 * one time. If not while <dst> is empty, we report a parsing
510 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200511 */
512 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
513 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
514
515 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
516 if (htx_is_empty(dst))
517 src->flags |= HTX_FL_PARSING_ERROR;
518 break;
519 }
520 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200521 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
522 !inside_trailers && mark >= HTX_BLK_EOT) {
523 inside_trailers = 1;
524 if (htx_used_space(src) > count) {
525 if (htx_is_empty(dst))
526 src->flags |= HTX_FL_PARSING_ERROR;
527 break;
528 }
529 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200530
Christopher Faulet156852b2019-05-16 11:29:13 +0200531 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200532 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200533 max = htx_get_max_blksz(dst, count);
534 if (!max)
535 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200536 if (sz > max) {
Christopher Faulet39744f72019-05-24 14:54:00 +0200537 /* Headers must be fully copied */
Christopher Faulet156852b2019-05-16 11:29:13 +0200538 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200539 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200540 sz = max;
541 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200542 }
543
544 dstblk = htx_reserve_nxblk(dst, sz);
545 if (!dstblk)
546 break;
547 dstblk->info = info;
548 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
549
Christopher Faulet156852b2019-05-16 11:29:13 +0200550 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200551 if (blk->info != info) {
552 /* Partial move: don't remove <blk> from <src> but
553 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200554 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200555 break;
556 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557 next:
558 blk = htx_remove_blk(src, blk);
559 if (type == mark)
560 break;
561
562 }
563
Christopher Faulet156852b2019-05-16 11:29:13 +0200564 end:
565 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200566 return (struct htx_ret){.ret = ret, .blk = dstblk};
567}
568
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200569/* Replaces an header by a new one. The new header can be smaller or larger than
570 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100571 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200572 */
573struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100574 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200575{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100576 enum htx_blk_type type;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100577 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200578
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100579 type = htx_get_blk_type(blk);
580 if (type != HTX_BLK_HDR)
581 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200582
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100583 delta = name.len + value.len - htx_get_blksz(blk);
584
585 /* easy case, new value is smaller, so replace it in-place */
586 if (delta <= 0) {
587 blk->info = (type << 28) + (value.len << 8) + name.len;
588 htx->data += delta;
589 goto copy;
590 }
591
592 /* we need to allocate more space to store the new value */
593 if (delta > htx_free_space(htx))
594 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200595
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100596 /* Expand the block size. But data are not copied yet. Then defragment
597 * the HTX message.
598 */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200599 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100600 htx->data += delta;
601 blk = htx_defrag(htx, blk);
602
603 copy:
604 /* Finaly, copy data. */
Willy Tarreaued00e342018-12-07 08:47:45 +0100605 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100606 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100607 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200608}
609
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100610/* Replaces the parts of the start-line. It returns the new start-line on
611 * success, otherwise it returns NULL. It is the caller responsibility to update
612 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200613 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100614struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
615 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200616{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100617 struct htx_sl *sl;
618 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100619 enum htx_blk_type type;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620 uint32_t size;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100621 int32_t delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200622
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100623 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100624 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100625 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200626
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100627 /* Save start-line info and flags */
628 sl = htx_get_blk_ptr(htx, blk);
629 tmp.info = sl->info;
630 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100631
632 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100633 delta = size - htx_get_blksz(blk);
634
635 /* easy case, new data are smaller, so replace it in-place */
636 if (delta <= 0) {
637 htx_set_blk_value_len(blk, size);
638 htx->data += delta;
639 goto copy;
640 }
641
642 /* we need to allocate more space to store the new value */
643 if (delta > htx_free_space(htx))
644 return NULL; /* not enough space */
645
646 /* Expand the block size. But data are not copied yet. Then defragment
647 * the HTX message.
648 */
649 htx_set_blk_value_len(blk, size);
650 htx->data += delta;
651 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200652
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100653 copy:
654 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100655 sl = htx_get_blk_ptr(htx, blk);
656 sl->info = tmp.info;
657 sl->flags = tmp.flags;
Christopher Faulet05c083c2019-05-15 14:56:47 +0200658 if (sl->hdrs_bytes != -1)
659 sl->hdrs_bytes += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200660
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100661 HTX_SL_P1_LEN(sl) = p1.len;
662 HTX_SL_P2_LEN(sl) = p2.len;
663 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100664
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100665 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
666 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
667 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200668
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100669 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200670}
671
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100672/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
673 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200674 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100675struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
676 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200677{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100678 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100679 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200680 uint32_t size;
681
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100682 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
683 return NULL;
684
685 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200686
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100687 /* FIXME: check size (< 256MB) */
688 blk = htx_add_blk(htx, type, size);
689 if (!blk)
690 return NULL;
691 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200692
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100693 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200694 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100695 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200696
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100697 HTX_SL_P1_LEN(sl) = p1.len;
698 HTX_SL_P2_LEN(sl) = p2.len;
699 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100701 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
702 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
703 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
704
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100705 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200706}
707
708/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100709 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200710 */
711struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100712 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200713{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100714 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200715
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100716 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
717 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
718 if (!blk)
719 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200720
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100721 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100722 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100723 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
724 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200725}
726
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200727/* Adds an HTX block of type TLR in <htx>. It returns the new block on
728 * success. Otherwise, it returns NULL. The header name is always lower cased.
729 */
730struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
731 const struct ist value)
732{
733 struct htx_blk *blk;
734
735 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
736 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
737 if (!blk)
738 return NULL;
739
740 blk->info += (value.len << 8) + name.len;
741 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
742 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
743 return blk;
744}
745
Willy Tarreau52610e92019-01-03 18:26:17 +0100746/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
747 * new block on success. Otherwise, it returns NULL. The caller is responsible
748 * for filling the block itself.
749 */
750struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
751{
752 struct htx_blk *blk;
753
754 blk = htx_add_blk(htx, type, blksz);
755 if (!blk)
756 return NULL;
757
758 blk->info += blksz;
759 return blk;
760}
761
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200762struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
763{
764 int i;
765
766 for (i = 0; hdrs[i].n.len; i++) {
767 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
768 return NULL;
769 }
770 return htx_add_endof(htx, HTX_BLK_EOH);
771}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200772
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200773struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
774{
775 int i;
776
777 for (i = 0; hdrs[i].n.len; i++) {
778 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
779 return NULL;
780 }
781 return htx_add_endof(htx, HTX_BLK_EOT);
782}
783
Christopher Faulet54b5e212019-06-04 10:08:28 +0200784/* Adds an HTX block of type EOH or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200785 * on success. Otherwise, it returns NULL.
786 */
787struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
788{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100789 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200790
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100791 blk = htx_add_blk(htx, type, 1);
792 if (!blk)
793 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200794
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100795 blk->info += 1;
796 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200797}
798
799
800/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200801 * possible. It returns the number of bytes consumed from <data>, which may be
802 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200804size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200805{
Willy Tarreau0350b902019-05-28 10:58:50 +0200806 struct htx_blk *blk, *tailblk, *headblk, *frtblk;
807 struct ist v;
808 int32_t room;
809 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200810
Willy Tarreau0350b902019-05-28 10:58:50 +0200811 if (!htx->used)
812 goto add_new_block;
813
814 /* Not enough space to store data */
815 if (len > htx_free_data_space(htx))
816 len = htx_free_data_space(htx);
817
818 if (!len)
819 return 0;
820
821 /* get the tail and head block */
822 tailblk = htx_get_tail_blk(htx);
823 headblk = htx_get_head_blk(htx);
824 if (tailblk == NULL || headblk == NULL)
825 goto add_new_block;
826
827 /* Don't try to append data if the last inserted block is not of the
828 * same type */
829 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
830 goto add_new_block;
831
832 /*
833 * Same type and enough space: append data
834 */
835 frtblk = htx_get_blk(htx, htx->front);
836 if (tailblk->addr >= headblk->addr) {
837 if (htx->tail != htx->front)
838 goto add_new_block;
839 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk));
840 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200841 else
Willy Tarreau0350b902019-05-28 10:58:50 +0200842 room = headblk->addr - (tailblk->addr + htx_get_blksz(tailblk));
843
844 if (room < len)
845 len = room;
846
847 append_data:
848 /* get the value of the tail block */
849 /* FIXME: check v.len + len < 256MB */
850 v = htx_get_blk_value(htx, tailblk);
851
852 /* Append data and update the block itself */
853 memcpy(v.ptr + v.len, data.ptr, len);
854 htx_set_blk_value_len(tailblk, v.len + len);
855
856 /* Update HTTP message */
857 htx->data += len;
858 return len;
859
860 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200861 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +0200862 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
863 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200864 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +0200865
866 blk->info += len;
867 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
868 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200869}
870
Christopher Faulet24ed8352018-11-22 11:20:43 +0100871struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref,
872 const struct ist data)
873{
874 struct htx_blk *blk;
875 int32_t prev;
876
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100877 /* FIXME: check data.len (< 256MB) */
878 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
879 if (!blk)
880 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +0100881
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100882 blk->info += data.len;
883 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Faulet24ed8352018-11-22 11:20:43 +0100884
885 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
886 struct htx_blk *pblk = htx_get_blk(htx, prev);
887
888 /* Swap .addr and .info fields */
889 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
890 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
891
892 if (blk->addr == pblk->addr)
893 blk->addr += htx_get_blksz(pblk);
894 htx->front = prev;
895
896 if (pblk == ref)
897 break;
898 blk = pblk;
899 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200900
901 if (htx_get_blk_pos(htx, blk) != htx->front)
902 htx_defrag(htx, NULL);
903
Christopher Faulet24ed8352018-11-22 11:20:43 +0100904 return blk;
905}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200906
Christopher Fauletc59ff232018-12-03 13:58:44 +0100907/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200908 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
909 * returns 0.
910 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100911int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200912{
Christopher Faulet570d1612018-11-26 11:13:57 +0100913 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200914 return 0;
915
Christopher Faulet570d1612018-11-26 11:13:57 +0100916 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200917 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100918 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200919 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +0100920 if (sl->flags & HTX_SL_F_VER_11)
921 chunk_memcat(chk, "HTTP/1.1", 8);
922 else
923 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
924
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200925 chunk_memcat(chk, "\r\n", 2);
926
927 return 1;
928}
929
Christopher Fauletc59ff232018-12-03 13:58:44 +0100930/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200931 * <chk>. It returns 1 if data are successfully appended, otherwise it
932 * returns 0.
933 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100934int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200935{
Christopher Faulet570d1612018-11-26 11:13:57 +0100936 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200937 return 0;
938
Christopher Faulet1e7af462018-12-03 14:05:01 +0100939 if (sl->flags & HTX_SL_F_VER_11)
940 chunk_memcat(chk, "HTTP/1.1", 8);
941 else
942 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200943 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100944 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200945 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +0100946 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200947 chunk_memcat(chk, "\r\n", 2);
948
949 return 1;
950}
951
Christopher Fauletc59ff232018-12-03 13:58:44 +0100952/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200953 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
954 * 0.
955 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100956int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200957{
958 if (n.len + v.len + 4 > b_room(chk))
959 return 0;
960
961 chunk_memcat(chk, n.ptr, n.len);
962 chunk_memcat(chk, ": ", 2);
963 chunk_memcat(chk, v.ptr, v.len);
964 chunk_memcat(chk, "\r\n", 2);
965
966 return 1;
967}
968
Christopher Fauletc59ff232018-12-03 13:58:44 +0100969/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200970 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
971 * returns 1 if data are successfully appended, otherwise it returns 0.
972 */
Christopher Fauletc59ff232018-12-03 13:58:44 +0100973int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200974{
975 if (chunked) {
976 uint32_t chksz;
977 char tmp[10];
978 char *beg, *end;
979
980 chksz = data.len;
981
982 beg = end = tmp+10;
983 *--beg = '\n';
984 *--beg = '\r';
985 do {
986 *--beg = hextab[chksz & 0xF];
987 } while (chksz >>= 4);
988
989 if (data.len + (end - beg) + 2 > b_room(chk))
990 return 0;
991 chunk_memcat(chk, beg, end - beg);
992 chunk_memcat(chk, data.ptr, data.len);
993 chunk_memcat(chk, "\r\n", 2);
994 }
995 else {
996 if (!chunk_memcat(chk, data.ptr, data.len))
997 return 0;
998 }
999
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001000 return 1;
1001}