blob: e83271cb3c74aafa2508fb4c3c119f2fac01d6ef [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 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 Fauletd7884d32019-06-11 10:40:43 +020078static void htx_defrag_blks(struct htx *htx)
79{
80 int32_t pos, new;
81
82 new = 0;
83 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
84 struct htx_blk *posblk, *newblk;
85
86 if (pos == new) {
87 new++;
88 continue;
89 }
90
91 posblk = htx_get_blk(htx, pos);
92 if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED)
93 continue;
94
95 if (htx->first == pos)
96 htx->first = new;
97 newblk = htx_get_blk(htx, new++);
98 newblk->info = posblk->info;
99 newblk->addr = posblk->addr;
100 }
101 BUG_ON(!new);
102 htx->head = 0;
103 htx->tail = new - 1;
104}
105
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200106/* Reserves a new block in the HTTP message <htx> with a content of <blksz>
107 * bytes. If there is not enough space, NULL is returned. Otherwise the reserved
108 * block is returned and the HTTP message is updated. Space for this new block
109 * is reserved in the HTTP message. But it is the caller responsibility to set
110 * right info in the block to reflect the stored data.
111 */
112static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
113{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200114 struct htx_blk *blk;
115 uint32_t used, tail;
116 uint32_t headroom, tailroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200117
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100118 if (blksz > htx_free_data_space(htx))
119 return NULL; /* full */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200120
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100121 if (!htx->used) {
122 /* Empty message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200123 htx->head = htx->tail = htx->first = 0;
Christopher Faulet28f29c72019-04-30 17:55:45 +0200124 htx->used = 1;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100125 blk = htx_get_blk(htx, htx->tail);
126 blk->addr = 0;
127 htx->data = blksz;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200128 htx->tail_addr = blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100129 return blk;
130 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200131
Christopher Fauletd7884d32019-06-11 10:40:43 +0200132 /* Find the block's position. First, we try to get the next position in
133 * the message, increasing the tail by one. If this position is not
134 * available with some holes, we try to defrag the blocks without
135 * touching their paylood. If it is impossible, we fully defrag the
136 * message.
137 */
Christopher Faulet28f29c72019-04-30 17:55:45 +0200138 tail = htx->tail + 1;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200139 if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) >= htx->tail_addr)
140 used = htx->used + 1;
141 else if (tail > htx->used) {
142 htx_defrag_blks(htx);
143 tail = htx->tail + 1;
144 used = htx->used + 1;
145 BUG_ON(sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) < htx->tail_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100146 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200147 else
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100148 goto defrag;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200149
Christopher Fauletd7884d32019-06-11 10:40:43 +0200150 /* Now, we have found the block's position. Try do find where to put its
151 * payload. The free space is split in two areas:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100152 *
Christopher Fauletd7884d32019-06-11 10:40:43 +0200153 * * The free space in front of the blocks table. This one is used iff
154 * the other one was not used yet.
155 *
156 * * The free space at the beginning of the message. Once this one is
157 * used, the other one is never used again, until the next defrag.
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100158 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200159 headroom = (htx->end_addr - htx->head_addr);
160 tailroom = (!htx->head_addr
161 ? sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, tail) - htx->tail_addr
162 : 0);
163 BUG_ON((int32_t)headroom < 0);
164 BUG_ON((int32_t)tailroom < 0);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200165
Christopher Fauletd7884d32019-06-11 10:40:43 +0200166 if (blksz <= tailroom) {
167 blk = htx_get_blk(htx, tail);
168 blk->addr = htx->tail_addr;
169 htx->tail_addr += blksz;
170 }
171 else if (blksz <= headroom) {
172 blk = htx_get_blk(htx, tail);
173 blk->addr = htx->head_addr;
174 htx->head_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100175 }
176 else {
Christopher Fauletd7884d32019-06-11 10:40:43 +0200177 defrag:
178 /* need to defragment the table before inserting upfront */
179 htx_defrag(htx, NULL);
180 tail = htx->tail + 1;
181 used = htx->used + 1;
182 blk = htx_get_blk(htx, tail);
183 blk->addr = htx->tail_addr;
184 htx->tail_addr += blksz;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100185 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200186
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100187 htx->tail = tail;
188 htx->used = used;
189 htx->data += blksz;
Christopher Faulet29f17582019-05-23 11:03:26 +0200190 /* Set first position if not already set */
191 if (htx->first == -1)
192 htx->first = tail;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200193
194 BUG_ON((int32_t)htx->tail_addr < 0);
195 BUG_ON((int32_t)htx->head_addr < 0);
196 BUG_ON(htx->end_addr > htx->tail_addr);
197 BUG_ON(htx->head_addr > htx->end_addr);
198
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100199 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200200}
201
Christopher Fauletd7884d32019-06-11 10:40:43 +0200202/* Prepares the block to an expansion of its payload. The payload will be
203 * expanded by <delta> bytes and we need find where this expansion will be
204 * performed. It can be a compression if <delta> is negative. This function only
205 * updates all addresses. The caller have the responsibility to performe the
206 * expansion and update the block and the HTX message accordingly. No error msut
207 * occurre. It returns following values:
208 *
209 * 0: The expansion cannot be performed, there is not enough space.
210 *
211 * 1: the expansion must be performed in place, there is enought space after
212 * the block's payload to handle it. This is especially true if it is a
213 * compression and not an expension.
214 *
215 * 2: the block's payload must be moved at the new block address before doing
216 * the expansion.
217 *
218 * 3: the HTX message message must be defragmented
219 */
220static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta)
221{
222 uint32_t sz, tailroom, headroom;
223 int ret = 3;
224
225 headroom = (htx->end_addr - htx->head_addr);
226 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
227 BUG_ON((int32_t)headroom < 0);
228 BUG_ON((int32_t)tailroom < 0);
229
230 sz = htx_get_blksz(blk);
231 if (delta <= 0) {
232 /* It is a compression, it can be performed in place */
233 if (blk->addr+sz == htx->tail_addr)
234 htx->tail_addr += delta;
235 else if (blk->addr+sz == htx->head_addr)
236 htx->head_addr += delta;
237 ret = 1;
238 }
239 else if (delta > htx_free_space(htx)) {
240 /* There is not enought space to handle the expansion */
241 ret = 0;
242 }
243 else if (blk->addr+sz == htx->tail_addr) {
244 /* The block's payload is just before the tail room */
245 if (delta < tailroom) {
246 /* Expand the block's payload */
247 htx->tail_addr += delta;
248 ret = 1;
249 }
250 else if ((sz + delta) < headroom) {
251 /* Move the block's payload into the headroom */
252 if (blk->addr == htx->end_addr)
253 htx->end_addr += sz;
254 blk->addr = htx->head_addr;
255 htx->tail_addr -= sz;
256 htx->head_addr += sz + delta;
257 ret = 2;
258 }
259 }
260 else if (blk->addr+sz == htx->head_addr) {
261 /* The block's payload is just before the head room */
262 if (delta < headroom) {
263 /* Expand the block's payload */
264 htx->head_addr += delta;
265 ret = 1;
266 }
267 }
268 else {
269 /* The block's payload is not at the rooms edge */
270 if (!htx->head_addr && sz+delta < tailroom) {
271 /* Move the block's payload into the tailroom */
272 if (blk->addr == htx->end_addr)
273 htx->end_addr += sz;
274 blk->addr = htx->tail_addr;
275 htx->tail_addr += sz + delta;
276 ret = 2;
277 }
278 else if (sz+delta < headroom) {
279 /* Move the block's payload into the headroom */
280 if (blk->addr == htx->end_addr)
281 htx->end_addr += sz;
282 blk->addr = htx->head_addr;
283 htx->head_addr += sz + delta;
284 ret = 2;
285 }
286 }
287 /* Otherwise defrag the HTX message */
288
289 BUG_ON((int32_t)htx->tail_addr < 0);
290 BUG_ON((int32_t)htx->head_addr < 0);
291 BUG_ON(htx->end_addr > htx->tail_addr);
292 BUG_ON(htx->head_addr > htx->end_addr);
293 return ret;
294}
295
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200296/* Adds a new block of type <type> in the HTTP message <htx>. Its content size
297 * is passed but it is the caller responsibility to do the copy.
298 */
299struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
300{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100301 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200302
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100303 blk = htx_reserve_nxblk(htx, blksz);
304 if (!blk)
305 return NULL;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200306 BUG_ON(blk->addr > htx->size);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200307
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100308 blk->info = (type << 28);
309 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200310}
311
312/* Removes the block <blk> from the HTTP message <htx>. The function returns the
313 * block following <blk> or NULL if <blk> is the last block or the last
314 * inserted one.
315 */
316struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
317{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200318 enum htx_blk_type type;
319 uint32_t pos, addr, sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200320
Christopher Fauletd7884d32019-06-11 10:40:43 +0200321 /* This is the last block in use */
322 if (htx->used == 1) {
323 htx_reset(htx);
324 return NULL;
325 }
326
327 type = htx_get_blk_type(blk);
Christopher Faulet9c66b982019-04-30 18:08:26 +0200328 pos = htx_get_blk_pos(htx, blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200329 sz = htx_get_blksz(blk);
330 addr = blk->addr;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100331 if (type != HTX_BLK_UNUSED) {
332 /* Mark the block as unused, decrement allocated size */
333 htx->data -= htx_get_blksz(blk);
334 blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100335 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200336
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100337 /* There is at least 2 blocks, so tail is always >= 0 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200338 if (pos == htx->head) {
339 /* move the head forward */
340 htx->used--;
341 htx->head++;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100342 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200343 else if (pos == htx->tail) {
344 /* remove the tail. this was the last inserted block so
345 * return NULL. */
346 htx->tail--;
347 htx->used--;
348 blk = NULL;
349 goto end;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100350 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200351 blk = htx_get_blk(htx, pos+1);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200352
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200353 end:
Christopher Faulet29f17582019-05-23 11:03:26 +0200354 if (pos == htx->first)
355 htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200356
357 if (htx->used == 1) {
358 /* If there is just one block in the HTX message, free space can
359 * be ajusted. This operation could save some defrags. */
360 struct htx_blk *lastblk = htx_get_blk(htx, htx->tail);
361
362 htx->head_addr = 0;
363 htx->end_addr = lastblk->addr;
364 htx->tail_addr = lastblk->addr+htx->data;
365 }
366 else {
367 if (addr+sz == htx->tail_addr)
368 htx->tail_addr = addr;
369 else if (addr+sz == htx->head_addr)
370 htx->head_addr = addr;
371 if (addr == htx->end_addr)
372 htx->end_addr += sz;
373 if (htx->tail_addr == htx->end_addr) {
374 htx->tail_addr = htx->head_addr;
375 htx->head_addr = htx->end_addr = 0;
376 }
377 }
378
379 BUG_ON((int32_t)htx->tail_addr < 0);
380 BUG_ON((int32_t)htx->head_addr < 0);
381 BUG_ON(htx->end_addr > htx->tail_addr);
382 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100383 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200384}
385
Christopher Faulet00cf6972019-01-07 14:53:27 +0100386/* Truncate all blocks after the one containing the offset <offset>. This last
387 * one is truncated too.
388 */
389void htx_truncate(struct htx *htx, uint32_t offset)
390{
391 struct htx_blk *blk;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100392
Christopher Fauletced39002019-05-23 11:12:43 +0200393 for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet00cf6972019-01-07 14:53:27 +0100394 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletced39002019-05-23 11:12:43 +0200395 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet00cf6972019-01-07 14:53:27 +0100396
Christopher Fauletced39002019-05-23 11:12:43 +0200397 if (offset >= sz) {
398 offset -= sz;
399 continue;
400 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200401 if (type == HTX_BLK_DATA) {
Christopher Fauletced39002019-05-23 11:12:43 +0200402 htx_set_blk_value_len(blk, offset);
403 htx->data -= (sz - offset);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200404
405 if (blk->addr+sz == htx->tail_addr)
406 htx->tail_addr -= offset;
407 else if (blk->addr+sz == htx->head_addr)
408 htx->head_addr -= offset;
Christopher Fauletced39002019-05-23 11:12:43 +0200409 }
410 offset = 0;
Christopher Faulet00cf6972019-01-07 14:53:27 +0100411 }
412 while (blk)
413 blk = htx_remove_blk(htx, blk);
414}
415
Christopher Faulet549822f2019-02-25 10:23:19 +0100416/* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if
417 * necessary. Others blocks will be removed at once if <count> is large
418 * enough. The function returns an htx_ret with the first block remaing in the
419 * messsage and the amount of data drained. If everything is removed,
420 * htx_ret.blk is set to NULL.
421 */
422struct htx_ret htx_drain(struct htx *htx, uint32_t count)
423{
424 struct htx_blk *blk;
425 struct htx_ret htxret = { .blk = NULL, .ret = 0 };
426
Christopher Faulet0f6d6a92019-05-23 11:11:52 +0200427 if (count == htx->data) {
428 htx_reset(htx);
429 htxret.ret = count;
430 return htxret;
431 }
432
Christopher Faulet549822f2019-02-25 10:23:19 +0100433 blk = htx_get_head_blk(htx);
434 while (count && blk) {
435 uint32_t sz = htx_get_blksz(blk);
436 enum htx_blk_type type = htx_get_blk_type(blk);
437
438 /* Ingore unused block */
439 if (type == HTX_BLK_UNUSED)
440 goto next;
441
442 if (sz > count) {
443 if (type == HTX_BLK_DATA) {
444 htx_cut_data_blk(htx, blk, count);
445 htxret.ret += count;
446 }
447 break;
448 }
449 count -= sz;
450 htxret.ret += sz;
451 next:
452 blk = htx_remove_blk(htx, blk);
453 }
454 htxret.blk = blk;
455
456 return htxret;
457}
458
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200459/* Tries to append data to the last inserted block, if the type matches and if
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200460 * there is enough space to take it all. If the space wraps, the buffer is
461 * defragmented and a new block is inserted. If an error occurred, NULL is
Christopher Faulet61775092019-05-07 21:42:27 +0200462 * returned. Otherwise, on success, the updated block (or the new one) is
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200463 * returned. Due to its nature this function can be expensive and should be
464 * avoided whenever possible.
465 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200466struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200467{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200468 struct htx_blk *blk, *tailblk;
469 void *ptr;
470 uint32_t len, sz, tailroom, headroom;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200471
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100472 if (!htx->used)
473 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200474
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100475 /* Not enough space to store data */
476 if (data.len > htx_free_data_space(htx))
477 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200478
Christopher Fauletd7884d32019-06-11 10:40:43 +0200479 /* get the tail block and its size */
Christopher Fauletf1449b72019-04-10 14:54:46 +0200480 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200481 if (tailblk == NULL)
Christopher Fauletf1449b72019-04-10 14:54:46 +0200482 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200483 sz = htx_get_blksz(tailblk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200484
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100485 /* Don't try to append data if the last inserted block is not of the
486 * same type */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200487 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100488 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200489
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100490 /*
491 * Same type and enough space: append data
492 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200493 headroom = (htx->end_addr - htx->head_addr);
494 tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
495 BUG_ON((int32_t)headroom < 0);
496 BUG_ON((int32_t)tailroom < 0);
497
498 len = data.len;
499 if (tailblk->addr+sz == htx->tail_addr) {
500 if (data.len <= tailroom)
501 goto append_data;
502 else if (!htx->head_addr) {
503 len = tailroom;
504 goto append_data;
505 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100506 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200507 else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom)
508 goto append_data;
Christopher Fauletf1449b72019-04-10 14:54:46 +0200509
Christopher Fauletd7884d32019-06-11 10:40:43 +0200510 goto add_new_block;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200511
512 append_data:
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100513 /* FIXME: check v.len + data.len < 256MB */
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100514 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200515 ptr = htx_get_blk_ptr(htx, tailblk);
516 memcpy(ptr+sz, data.ptr, len);
517 htx_set_blk_value_len(tailblk, sz+len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200518
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100519 /* Update HTTP message */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200520 htx->data += len;
521 if (tailblk->addr+sz == htx->tail_addr)
522 htx->tail_addr += len;
523 else if (tailblk->addr+sz == htx->head_addr)
524 htx->head_addr += len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200525
Christopher Fauletd7884d32019-06-11 10:40:43 +0200526 if (data.len == len) {
527 blk = tailblk;
528 goto end;
529 }
530 data.ptr += len;
531 data.len -= len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200532
533 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200534 /* FIXME: check data.len (< 256MB) */
Willy Tarreaud4908fa2019-05-28 10:23:46 +0200535 blk = htx_add_blk(htx, HTX_BLK_DATA, data.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100536 if (!blk)
537 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200538
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100539 blk->info += data.len;
540 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200541
542 end:
543 BUG_ON((int32_t)htx->tail_addr < 0);
544 BUG_ON((int32_t)htx->head_addr < 0);
545 BUG_ON(htx->end_addr > htx->tail_addr);
546 BUG_ON(htx->head_addr > htx->end_addr);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100547 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200548}
549
550/* Replaces a value part of a block by a new one. The new part can be smaller or
551 * larger than the old one. This function works for any kind of block with
552 * attached data. It returns the new block on success, otherwise it returns
553 * NULL.
554 */
555struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100556 const struct ist old, const struct ist new)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200557{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100558 struct ist n, v;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100559 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200560 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200561
Christopher Fauletd7884d32019-06-11 10:40:43 +0200562 n = htx_get_blk_name(htx, blk);
563 v = htx_get_blk_value(htx, blk);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100564 delta = new.len - old.len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200565 ret = htx_prepare_blk_expansion(htx, blk, delta);
566 if (!ret)
567 return NULL; /* not enough space */
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100568
Christopher Fauletd7884d32019-06-11 10:40:43 +0200569 /* Before updating the payload, set the new block size and update HTX
570 * message */
571 htx_set_blk_value_len(blk, v.len + delta);
572 htx->data += delta;
573
574 if (ret == 1) {
575 if (delta <= 0) {
576 /* compression: copy new data first */
577 memcpy(old.ptr, new.ptr, new.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100578 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauletd7884d32019-06-11 10:40:43 +0200579 }
580 else {
581 /* expansion: move the end first */
582 memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
583 memcpy(old.ptr, new.ptr, new.len);
584 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100585 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200586 else if (ret == 2) {
587 void *ptr = htx_get_blk_ptr(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200588
Christopher Fauletd7884d32019-06-11 10:40:43 +0200589 /* Copy the name, if any */
590 memcpy(ptr, n.ptr, n.len);
591 ptr += n.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200592
Christopher Fauletd7884d32019-06-11 10:40:43 +0200593 /* Copy value before old part, if any */
594 memcpy(ptr, v.ptr, old.ptr - v.ptr);
595 ptr += old.ptr - v.ptr;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200596
Christopher Fauletd7884d32019-06-11 10:40:43 +0200597 /* Copy new value */
598 memcpy(ptr, new.ptr, new.len);
599 ptr += new.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200600
Christopher Fauletd7884d32019-06-11 10:40:43 +0200601 /* Copy value after old part, if any */
602 memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
603 }
604 else {
605 struct buffer *tmp = get_trash_chunk();
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200606
Christopher Fauletd7884d32019-06-11 10:40:43 +0200607 /* Copy the header name, if any */
608 chunk_memcat(tmp, n.ptr, n.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200609
Christopher Fauletd7884d32019-06-11 10:40:43 +0200610 /* Copy value before old part, if any */
611 chunk_memcat(tmp, v.ptr, old.ptr - v.ptr);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200612
Christopher Fauletd7884d32019-06-11 10:40:43 +0200613 /* Copy new value */
614 chunk_memcat(tmp, new.ptr, new.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200615
Christopher Fauletd7884d32019-06-11 10:40:43 +0200616 /* Copy value after old part if any */
617 chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len));
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200618
Christopher Fauletd7884d32019-06-11 10:40:43 +0200619 blk = htx_defrag(htx, blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200620
Christopher Fauletd7884d32019-06-11 10:40:43 +0200621 /* Finaly, copy data. */
622 memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data);
623 }
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100624 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200625}
626
627/* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the
Christopher Faulet54b5e212019-06-04 10:08:28 +0200628 * type <mark> (typically EOH or EOM) or when <count> bytes were moved
Christopher Faulet156852b2019-05-16 11:29:13 +0200629 * (including payload and meta-data). It returns the number of bytes moved and
630 * the last HTX block inserted in <dst>.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200631 */
632struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count,
633 enum htx_blk_type mark)
634{
635 struct htx_blk *blk, *dstblk;
636 enum htx_blk_type type;
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100637 uint32_t info, max, sz, ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200638 int inside_trailers = 0;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200639
Christopher Faulet156852b2019-05-16 11:29:13 +0200640 ret = htx_used_space(dst);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200641 blk = htx_get_blk(src, htx_get_head(src));
642 dstblk = NULL;
Christopher Faulet156852b2019-05-16 11:29:13 +0200643
644 while (blk && count) {
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200645 type = htx_get_blk_type(blk);
646
647 /* Ingore unused block */
648 if (type == HTX_BLK_UNUSED)
649 goto next;
650
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200651 /* Be sure to have enough space to xfer all headers/trailers in
652 * one time. If not while <dst> is empty, we report a parsing
653 * error on <src>.
Christopher Fauleta61e97b2019-05-16 11:30:31 +0200654 */
655 if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) {
656 struct htx_sl *sl = htx_get_blk_ptr(src, blk);
657
658 if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) {
659 if (htx_is_empty(dst))
660 src->flags |= HTX_FL_PARSING_ERROR;
661 break;
662 }
663 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200664 else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) &&
665 !inside_trailers && mark >= HTX_BLK_EOT) {
666 inside_trailers = 1;
667 if (htx_used_space(src) > count) {
668 if (htx_is_empty(dst))
669 src->flags |= HTX_FL_PARSING_ERROR;
670 break;
671 }
672 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200673
Christopher Faulet156852b2019-05-16 11:29:13 +0200674 sz = htx_get_blksz(blk);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200675 info = blk->info;
Christopher Faulet156852b2019-05-16 11:29:13 +0200676 max = htx_get_max_blksz(dst, count);
677 if (!max)
678 break;
Willy Tarreau90caa072019-04-09 16:21:54 +0200679 if (sz > max) {
Christopher Faulet39744f72019-05-24 14:54:00 +0200680 /* Headers must be fully copied */
Christopher Faulet156852b2019-05-16 11:29:13 +0200681 if (type != HTX_BLK_DATA)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200682 break;
Christopher Faulet156852b2019-05-16 11:29:13 +0200683 sz = max;
684 info = (type << 28) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200685 }
686
687 dstblk = htx_reserve_nxblk(dst, sz);
688 if (!dstblk)
689 break;
690 dstblk->info = info;
691 memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz);
692
Christopher Faulet156852b2019-05-16 11:29:13 +0200693 count -= sizeof(dstblk) + sz;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200694 if (blk->info != info) {
695 /* Partial move: don't remove <blk> from <src> but
696 * resize its content */
Christopher Faulet156852b2019-05-16 11:29:13 +0200697 htx_cut_data_blk(src, blk, sz);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200698 break;
699 }
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200700 next:
701 blk = htx_remove_blk(src, blk);
702 if (type == mark)
703 break;
704
705 }
706
Christopher Faulet156852b2019-05-16 11:29:13 +0200707 end:
708 ret = htx_used_space(dst) - ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200709 return (struct htx_ret){.ret = ret, .blk = dstblk};
710}
711
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200712/* Replaces an header by a new one. The new header can be smaller or larger than
713 * the old one. It returns the new block on success, otherwise it returns NULL.
Willy Tarreaued00e342018-12-07 08:47:45 +0100714 * The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200715 */
716struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100717 const struct ist name, const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200718{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100719 enum htx_blk_type type;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200720 void *ptr;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100721 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200722 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200723
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100724 type = htx_get_blk_type(blk);
725 if (type != HTX_BLK_HDR)
726 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200727
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100728 delta = name.len + value.len - htx_get_blksz(blk);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200729 ret = htx_prepare_blk_expansion(htx, blk, delta);
730 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100731 return NULL; /* not enough space */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200732
Christopher Fauletd7884d32019-06-11 10:40:43 +0200733 /* Set the new block size and update HTX message */
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200734 blk->info = (type << 28) + (value.len << 8) + name.len;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100735 htx->data += delta;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100736
Christopher Fauletd7884d32019-06-11 10:40:43 +0200737 if (ret == 3)
738 blk = htx_defrag(htx, blk);
739
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100740 /* Finaly, copy data. */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200741 ptr = htx_get_blk_ptr(htx, blk);
742 ist2bin_lc(ptr, name);
743 memcpy(ptr + name.len, value.ptr, value.len);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100744 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200745}
746
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100747/* Replaces the parts of the start-line. It returns the new start-line on
748 * success, otherwise it returns NULL. It is the caller responsibility to update
749 * sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200750 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100751struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1,
752 const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200753{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200754 enum htx_blk_type type;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100755 struct htx_sl *sl;
756 struct htx_sl tmp; /* used to save sl->info and sl->flags */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200757 uint32_t sz;
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100758 int32_t delta;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200759 int ret;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200760
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100761 type = htx_get_blk_type(blk);
Willy Tarreauc706cd72018-12-07 17:12:22 +0100762 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100763 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200764
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100765 /* Save start-line info and flags */
766 sl = htx_get_blk_ptr(htx, blk);
767 tmp.info = sl->info;
768 tmp.flags = sl->flags;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100769
Christopher Fauletd7884d32019-06-11 10:40:43 +0200770 sz = htx_get_blksz(blk);
771 delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz;
772 ret = htx_prepare_blk_expansion(htx, blk, delta);
773 if (!ret)
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100774 return NULL; /* not enough space */
775
Christopher Fauletd7884d32019-06-11 10:40:43 +0200776 /* Set the new block size and update HTX message */
777 htx_set_blk_value_len(blk, sz+delta);
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100778 htx->data += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200779
Christopher Fauletd7884d32019-06-11 10:40:43 +0200780 if (ret == 3)
781 blk = htx_defrag(htx, blk);
782
Christopher Faulete97f3ba2018-12-10 15:39:40 +0100783 /* Restore start-line info and flags and copy parts of the start-line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100784 sl = htx_get_blk_ptr(htx, blk);
785 sl->info = tmp.info;
786 sl->flags = tmp.flags;
Christopher Faulet05c083c2019-05-15 14:56:47 +0200787 if (sl->hdrs_bytes != -1)
788 sl->hdrs_bytes += delta;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200789
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100790 HTX_SL_P1_LEN(sl) = p1.len;
791 HTX_SL_P2_LEN(sl) = p2.len;
792 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Faulet54483df2018-11-26 15:05:52 +0100793
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100794 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
795 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
796 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200797
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100798 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200799}
800
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100801/* Add a new start-line. It returns it on success, otherwise it returns NULL. It
802 * is the caller responsibility to set sl->info, if necessary.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200803 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100804struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags,
805 const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200806{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100807 struct htx_blk *blk;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100808 struct htx_sl *sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200809 uint32_t size;
810
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100811 if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL)
812 return NULL;
813
814 size = sizeof(*sl) + p1.len + p2.len + p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200815
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100816 /* FIXME: check size (< 256MB) */
817 blk = htx_add_blk(htx, type, size);
818 if (!blk)
819 return NULL;
820 blk->info += size;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200821
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100822 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet05c083c2019-05-15 14:56:47 +0200823 sl->hdrs_bytes = -1;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100824 sl->flags = flags;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200825
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100826 HTX_SL_P1_LEN(sl) = p1.len;
827 HTX_SL_P2_LEN(sl) = p2.len;
828 HTX_SL_P3_LEN(sl) = p3.len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200829
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100830 memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len);
831 memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len);
832 memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len);
833
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100834 return sl;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200835}
836
837/* Adds an HTX block of type HDR in <htx>. It returns the new block on
Willy Tarreaued00e342018-12-07 08:47:45 +0100838 * success. Otherwise, it returns NULL. The header name is always lower cased.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200839 */
840struct htx_blk *htx_add_header(struct htx *htx, const struct ist name,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100841 const struct ist value)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200842{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100843 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200844
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100845 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
846 blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len);
847 if (!blk)
848 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200849
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100850 blk->info += (value.len << 8) + name.len;
Willy Tarreaued00e342018-12-07 08:47:45 +0100851 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100852 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
853 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200854}
855
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200856/* Adds an HTX block of type TLR in <htx>. It returns the new block on
857 * success. Otherwise, it returns NULL. The header name is always lower cased.
858 */
859struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name,
860 const struct ist value)
861{
862 struct htx_blk *blk;
863
864 /* FIXME: check name.len (< 256B) and value.len (< 1MB) */
865 blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len);
866 if (!blk)
867 return NULL;
868
869 blk->info += (value.len << 8) + name.len;
870 ist2bin_lc(htx_get_blk_ptr(htx, blk), name);
871 memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len);
872 return blk;
873}
874
Willy Tarreau52610e92019-01-03 18:26:17 +0100875/* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the
876 * new block on success. Otherwise, it returns NULL. The caller is responsible
877 * for filling the block itself.
878 */
879struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
880{
881 struct htx_blk *blk;
882
883 blk = htx_add_blk(htx, type, blksz);
884 if (!blk)
885 return NULL;
886
887 blk->info += blksz;
888 return blk;
889}
890
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200891struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs)
892{
893 int i;
894
895 for (i = 0; hdrs[i].n.len; i++) {
896 if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v))
897 return NULL;
898 }
899 return htx_add_endof(htx, HTX_BLK_EOH);
900}
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200901
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200902struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs)
903{
904 int i;
905
906 for (i = 0; hdrs[i].n.len; i++) {
907 if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v))
908 return NULL;
909 }
910 return htx_add_endof(htx, HTX_BLK_EOT);
911}
912
Christopher Faulet54b5e212019-06-04 10:08:28 +0200913/* Adds an HTX block of type EOH or EOM in <htx>. It returns the new block
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200914 * on success. Otherwise, it returns NULL.
915 */
916struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type)
917{
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100918 struct htx_blk *blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200919
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100920 blk = htx_add_blk(htx, type, 1);
921 if (!blk)
922 return NULL;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200923
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100924 blk->info += 1;
925 return blk;
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200926}
927
928
929/* Adds an HTX block of type DATA in <htx>. It first tries to append data if
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200930 * possible. It returns the number of bytes consumed from <data>, which may be
931 * zero if nothing could be copied.
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200932 */
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200933size_t htx_add_data(struct htx *htx, const struct ist data)
Christopher Fauleta3d2a162018-10-22 08:59:39 +0200934{
Christopher Fauletd7884d32019-06-11 10:40:43 +0200935 struct htx_blk *blk, *tailblk;
936 void *ptr;
937 uint32_t sz, room;
Willy Tarreau0350b902019-05-28 10:58:50 +0200938 int32_t len = data.len;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200939
Willy Tarreau0350b902019-05-28 10:58:50 +0200940 if (!htx->used)
941 goto add_new_block;
942
943 /* Not enough space to store data */
944 if (len > htx_free_data_space(htx))
945 len = htx_free_data_space(htx);
946
947 if (!len)
948 return 0;
949
950 /* get the tail and head block */
951 tailblk = htx_get_tail_blk(htx);
Christopher Fauletd7884d32019-06-11 10:40:43 +0200952 if (tailblk == NULL)
Willy Tarreau0350b902019-05-28 10:58:50 +0200953 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200954 sz = htx_get_blksz(tailblk);
Willy Tarreau0350b902019-05-28 10:58:50 +0200955
956 /* Don't try to append data if the last inserted block is not of the
957 * same type */
958 if (htx_get_blk_type(tailblk) != HTX_BLK_DATA)
959 goto add_new_block;
960
961 /*
962 * Same type and enough space: append data
963 */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200964 if (!htx->head_addr) {
965 if (tailblk->addr+sz != htx->tail_addr)
Willy Tarreau0350b902019-05-28 10:58:50 +0200966 goto add_new_block;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200967 room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr;
Willy Tarreau0350b902019-05-28 10:58:50 +0200968 }
Christopher Fauletd7884d32019-06-11 10:40:43 +0200969 else {
970 if (tailblk->addr+sz != htx->head_addr)
971 goto add_new_block;
972 room = (htx->end_addr - htx->head_addr);
973 }
974 BUG_ON((int32_t)room < 0);
Willy Tarreau0350b902019-05-28 10:58:50 +0200975 if (room < len)
976 len = room;
977
978 append_data:
Christopher Fauletd7884d32019-06-11 10:40:43 +0200979 /* FIXME: check v.len + data.len < 256MB */
Willy Tarreau0350b902019-05-28 10:58:50 +0200980 /* Append data and update the block itself */
Christopher Fauletd7884d32019-06-11 10:40:43 +0200981 ptr = htx_get_blk_ptr(htx, tailblk);
982 memcpy(ptr + sz, data.ptr, len);
983 htx_set_blk_value_len(tailblk, sz + len);
Willy Tarreau0350b902019-05-28 10:58:50 +0200984
985 /* Update HTTP message */
986 htx->data += len;
Christopher Fauletd7884d32019-06-11 10:40:43 +0200987 if (tailblk->addr+sz == htx->tail_addr)
988 htx->tail_addr += len;
989 else if (tailblk->addr+sz == htx->head_addr)
990 htx->head_addr += len;
991
992 BUG_ON((int32_t)htx->tail_addr < 0);
993 BUG_ON((int32_t)htx->head_addr < 0);
994 BUG_ON(htx->end_addr > htx->tail_addr);
995 BUG_ON(htx->head_addr > htx->end_addr);
Willy Tarreau0350b902019-05-28 10:58:50 +0200996 return len;
997
998 add_new_block:
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200999 /* FIXME: check data.len (< 256MB) */
Willy Tarreau0350b902019-05-28 10:58:50 +02001000 blk = htx_add_blk(htx, HTX_BLK_DATA, len);
1001 if (!blk)
Willy Tarreau0a7ef022019-05-28 10:30:11 +02001002 return 0;
Willy Tarreau0350b902019-05-28 10:58:50 +02001003
1004 blk->info += len;
1005 memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len);
1006 return len;
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001007}
1008
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001009
1010/* Adds an HTX block of type DATA in <htx> just after all other DATA
1011 * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a
1012 * DATA block if possible. But, if the function succeeds, it will be the last
1013 * DATA block in all cases. If an error occurred, NULL is returned. Otherwise,
1014 * on success, the updated block (or the new one) is returned.
1015 */
1016struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
Christopher Faulet24ed8352018-11-22 11:20:43 +01001017{
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001018 struct htx_blk *blk, *pblk;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001019
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001020 blk = htx_add_data_atonce(htx, data);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001021 if (!blk)
1022 return NULL;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001023
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001024 for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
Christopher Faulet86bc8df2019-06-11 10:38:38 +02001025 if (htx_get_blk_type(pblk) <= HTX_BLK_DATA)
1026 break;
Christopher Faulet24ed8352018-11-22 11:20:43 +01001027
Christopher Faulet24ed8352018-11-22 11:20:43 +01001028 /* Swap .addr and .info fields */
1029 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
1030 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
1031
1032 if (blk->addr == pblk->addr)
1033 blk->addr += htx_get_blksz(pblk);
Christopher Faulet24ed8352018-11-22 11:20:43 +01001034 blk = pblk;
1035 }
Christopher Faulet05aab642019-04-11 13:43:57 +02001036
Christopher Faulet24ed8352018-11-22 11:20:43 +01001037 return blk;
1038}
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001039
Christopher Faulet86fcf6d2019-06-11 10:41:19 +02001040/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
1041 * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
1042 * blocks are updated to remain valid after the move. */
1043void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
1044{
1045 struct htx_blk *cblk, *pblk;
1046
1047 cblk = *blk;
1048 for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
1049 /* Swap .addr and .info fields */
1050 cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
1051 cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
1052
1053 if (cblk->addr == pblk->addr)
1054 cblk->addr += htx_get_blksz(pblk);
1055 if (pblk == *ref)
1056 break;
1057 cblk = pblk;
1058 }
1059 *blk = cblk;
1060 *ref = pblk;
1061}
1062
Christopher Fauletc59ff232018-12-03 13:58:44 +01001063/* Appends the H1 representation of the request line block <blk> to the
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001064 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
1065 * returns 0.
1066 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001067int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001068{
Christopher Faulet570d1612018-11-26 11:13:57 +01001069 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001070 return 0;
1071
Christopher Faulet570d1612018-11-26 11:13:57 +01001072 chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001073 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001074 chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001075 chunk_memcat(chk, " ", 1);
Christopher Faulet1e7af462018-12-03 14:05:01 +01001076 if (sl->flags & HTX_SL_F_VER_11)
1077 chunk_memcat(chk, "HTTP/1.1", 8);
1078 else
1079 chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1080
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001081 chunk_memcat(chk, "\r\n", 2);
1082
1083 return 1;
1084}
1085
Christopher Fauletc59ff232018-12-03 13:58:44 +01001086/* Appends the H1 representation of the status line block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001087 * <chk>. It returns 1 if data are successfully appended, otherwise it
1088 * returns 0.
1089 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001090int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001091{
Christopher Faulet570d1612018-11-26 11:13:57 +01001092 if (HTX_SL_LEN(sl) + 4 > b_size(chk))
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001093 return 0;
1094
Christopher Faulet1e7af462018-12-03 14:05:01 +01001095 if (sl->flags & HTX_SL_F_VER_11)
1096 chunk_memcat(chk, "HTTP/1.1", 8);
1097 else
1098 chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001099 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001100 chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001101 chunk_memcat(chk, " ", 1);
Christopher Faulet570d1612018-11-26 11:13:57 +01001102 chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001103 chunk_memcat(chk, "\r\n", 2);
1104
1105 return 1;
1106}
1107
Christopher Fauletc59ff232018-12-03 13:58:44 +01001108/* Appends the H1 representation of the header block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001109 * <chk>. It returns 1 if data are successfully appended, otherwise it returns
1110 * 0.
1111 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001112int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001113{
1114 if (n.len + v.len + 4 > b_room(chk))
1115 return 0;
1116
1117 chunk_memcat(chk, n.ptr, n.len);
1118 chunk_memcat(chk, ": ", 2);
1119 chunk_memcat(chk, v.ptr, v.len);
1120 chunk_memcat(chk, "\r\n", 2);
1121
1122 return 1;
1123}
1124
Christopher Fauletc59ff232018-12-03 13:58:44 +01001125/* Appends the H1 representation of the data block <blk> to the chunk
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001126 * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It
1127 * returns 1 if data are successfully appended, otherwise it returns 0.
1128 */
Christopher Fauletc59ff232018-12-03 13:58:44 +01001129int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked)
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001130{
1131 if (chunked) {
1132 uint32_t chksz;
1133 char tmp[10];
1134 char *beg, *end;
1135
1136 chksz = data.len;
1137
1138 beg = end = tmp+10;
1139 *--beg = '\n';
1140 *--beg = '\r';
1141 do {
1142 *--beg = hextab[chksz & 0xF];
1143 } while (chksz >>= 4);
1144
1145 if (data.len + (end - beg) + 2 > b_room(chk))
1146 return 0;
1147 chunk_memcat(chk, beg, end - beg);
1148 chunk_memcat(chk, data.ptr, data.len);
1149 chunk_memcat(chk, "\r\n", 2);
1150 }
1151 else {
1152 if (!chunk_memcat(chk, data.ptr, data.len))
1153 return 0;
1154 }
1155
Christopher Fauleta3d2a162018-10-22 08:59:39 +02001156 return 1;
1157}