Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 14 | #include <common/htx.h> |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 15 | |
| 16 | struct 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 */ |
| 24 | struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk) |
| 25 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 26 | struct buffer *chunk = get_trash_chunk(); |
| 27 | struct htx *tmp = htxbuf(chunk); |
| 28 | struct htx_blk *newblk, *oldblk; |
Christopher Faulet | 200f895 | 2019-01-02 11:23:44 +0100 | [diff] [blame] | 29 | uint32_t new, old, blkpos; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 30 | uint32_t addr, blksz; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 31 | int32_t first = -1; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 32 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 33 | if (!htx->used) |
| 34 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 35 | |
Christopher Faulet | 200f895 | 2019-01-02 11:23:44 +0100 | [diff] [blame] | 36 | blkpos = -1; |
| 37 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 38 | new = 0; |
| 39 | addr = 0; |
| 40 | tmp->size = htx->size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 41 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 42 | /* 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 Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 45 | if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 46 | continue; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 47 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 48 | newblk = htx_get_blk(tmp, new); |
| 49 | newblk->addr = addr; |
| 50 | newblk->info = oldblk->info; |
| 51 | blksz = htx_get_blksz(oldblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 52 | |
Christopher Faulet | 9c66b98 | 2019-04-30 18:08:26 +0200 | [diff] [blame] | 53 | /* update the start-line position */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 54 | if (htx->first == old) |
| 55 | first = new; |
Christopher Faulet | 174bfb1 | 2018-12-06 14:31:12 +0100 | [diff] [blame] | 56 | |
Christopher Faulet | 200f895 | 2019-01-02 11:23:44 +0100 | [diff] [blame] | 57 | /* if <blk> is defined, set its new position */ |
| 58 | if (blk != NULL && blk == oldblk) |
| 59 | blkpos = new; |
| 60 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 61 | memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz); |
| 62 | new++; |
| 63 | addr += blksz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 64 | |
Christopher Faulet | b8fd4c0 | 2019-05-20 09:32:25 +0200 | [diff] [blame] | 65 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 66 | |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 67 | htx->used = new; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 68 | htx->first = first; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 69 | htx->head = 0; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 70 | htx->tail = new - 1; |
| 71 | htx->head_addr = htx->end_addr = 0; |
| 72 | htx->tail_addr = addr; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 73 | memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 74 | |
Christopher Faulet | 200f895 | 2019-01-02 11:23:44 +0100 | [diff] [blame] | 75 | return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 78 | static 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 Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 106 | /* 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 | */ |
| 112 | static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz) |
| 113 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 114 | struct htx_blk *blk; |
| 115 | uint32_t used, tail; |
| 116 | uint32_t headroom, tailroom; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 117 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 118 | if (blksz > htx_free_data_space(htx)) |
| 119 | return NULL; /* full */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 120 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 121 | if (!htx->used) { |
| 122 | /* Empty message */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 123 | htx->head = htx->tail = htx->first = 0; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 124 | htx->used = 1; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 125 | blk = htx_get_blk(htx, htx->tail); |
| 126 | blk->addr = 0; |
| 127 | htx->data = blksz; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 128 | htx->tail_addr = blksz; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 129 | return blk; |
| 130 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 131 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 132 | /* 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 Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 138 | tail = htx->tail + 1; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 139 | 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 Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 146 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 147 | else |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 148 | goto defrag; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 149 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 150 | /* 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 Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 152 | * |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 153 | * * 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 Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 158 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 159 | 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 Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 165 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 166 | 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 Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 175 | } |
| 176 | else { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 177 | 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 Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 185 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 186 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 187 | htx->tail = tail; |
| 188 | htx->used = used; |
| 189 | htx->data += blksz; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 190 | /* Set first position if not already set */ |
| 191 | if (htx->first == -1) |
| 192 | htx->first = tail; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 193 | |
| 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 Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 199 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 202 | /* 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 | */ |
| 220 | static 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 */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 252 | blk->addr = htx->head_addr; |
| 253 | htx->tail_addr -= sz; |
| 254 | htx->head_addr += sz + delta; |
Christopher Faulet | 8c65486 | 2019-06-12 11:08:11 +0200 | [diff] [blame] | 255 | if (blk->addr == htx->end_addr) { |
| 256 | if (htx->end_addr == htx->tail_addr) { |
| 257 | htx->tail_addr = htx->head_addr; |
| 258 | htx->head_addr = htx->end_addr = 0; |
| 259 | } |
| 260 | else |
| 261 | htx->end_addr += sz; |
| 262 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 263 | ret = 2; |
| 264 | } |
| 265 | } |
| 266 | else if (blk->addr+sz == htx->head_addr) { |
| 267 | /* The block's payload is just before the head room */ |
| 268 | if (delta < headroom) { |
| 269 | /* Expand the block's payload */ |
| 270 | htx->head_addr += delta; |
| 271 | ret = 1; |
| 272 | } |
| 273 | } |
| 274 | else { |
| 275 | /* The block's payload is not at the rooms edge */ |
| 276 | if (!htx->head_addr && sz+delta < tailroom) { |
| 277 | /* Move the block's payload into the tailroom */ |
| 278 | if (blk->addr == htx->end_addr) |
| 279 | htx->end_addr += sz; |
| 280 | blk->addr = htx->tail_addr; |
| 281 | htx->tail_addr += sz + delta; |
| 282 | ret = 2; |
| 283 | } |
| 284 | else if (sz+delta < headroom) { |
| 285 | /* Move the block's payload into the headroom */ |
| 286 | if (blk->addr == htx->end_addr) |
| 287 | htx->end_addr += sz; |
| 288 | blk->addr = htx->head_addr; |
| 289 | htx->head_addr += sz + delta; |
| 290 | ret = 2; |
| 291 | } |
| 292 | } |
| 293 | /* Otherwise defrag the HTX message */ |
| 294 | |
| 295 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 296 | BUG_ON((int32_t)htx->head_addr < 0); |
| 297 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 298 | BUG_ON(htx->head_addr > htx->end_addr); |
| 299 | return ret; |
| 300 | } |
| 301 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 302 | /* Adds a new block of type <type> in the HTTP message <htx>. Its content size |
| 303 | * is passed but it is the caller responsibility to do the copy. |
| 304 | */ |
| 305 | struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 306 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 307 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 308 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 309 | blk = htx_reserve_nxblk(htx, blksz); |
| 310 | if (!blk) |
| 311 | return NULL; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 312 | BUG_ON(blk->addr > htx->size); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 313 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 314 | blk->info = (type << 28); |
| 315 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* Removes the block <blk> from the HTTP message <htx>. The function returns the |
| 319 | * block following <blk> or NULL if <blk> is the last block or the last |
| 320 | * inserted one. |
| 321 | */ |
| 322 | struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk) |
| 323 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 324 | enum htx_blk_type type; |
| 325 | uint32_t pos, addr, sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 326 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 327 | /* This is the last block in use */ |
| 328 | if (htx->used == 1) { |
| 329 | htx_reset(htx); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | type = htx_get_blk_type(blk); |
Christopher Faulet | 9c66b98 | 2019-04-30 18:08:26 +0200 | [diff] [blame] | 334 | pos = htx_get_blk_pos(htx, blk); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 335 | sz = htx_get_blksz(blk); |
| 336 | addr = blk->addr; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 337 | if (type != HTX_BLK_UNUSED) { |
| 338 | /* Mark the block as unused, decrement allocated size */ |
| 339 | htx->data -= htx_get_blksz(blk); |
| 340 | blk->info = ((uint32_t)HTX_BLK_UNUSED << 28); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 341 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 342 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 343 | /* There is at least 2 blocks, so tail is always >= 0 */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 344 | if (pos == htx->head) { |
| 345 | /* move the head forward */ |
| 346 | htx->used--; |
| 347 | htx->head++; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 348 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 349 | else if (pos == htx->tail) { |
| 350 | /* remove the tail. this was the last inserted block so |
| 351 | * return NULL. */ |
| 352 | htx->tail--; |
| 353 | htx->used--; |
| 354 | blk = NULL; |
| 355 | goto end; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 356 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 357 | blk = htx_get_blk(htx, pos+1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 358 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 359 | end: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 360 | if (pos == htx->first) |
| 361 | htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 362 | |
| 363 | if (htx->used == 1) { |
| 364 | /* If there is just one block in the HTX message, free space can |
| 365 | * be ajusted. This operation could save some defrags. */ |
| 366 | struct htx_blk *lastblk = htx_get_blk(htx, htx->tail); |
| 367 | |
| 368 | htx->head_addr = 0; |
| 369 | htx->end_addr = lastblk->addr; |
| 370 | htx->tail_addr = lastblk->addr+htx->data; |
| 371 | } |
| 372 | else { |
| 373 | if (addr+sz == htx->tail_addr) |
| 374 | htx->tail_addr = addr; |
| 375 | else if (addr+sz == htx->head_addr) |
| 376 | htx->head_addr = addr; |
Christopher Faulet | 8c65486 | 2019-06-12 11:08:11 +0200 | [diff] [blame] | 377 | if (addr == htx->end_addr) { |
| 378 | if (htx->tail_addr == htx->end_addr) { |
| 379 | htx->tail_addr = htx->head_addr; |
| 380 | htx->head_addr = htx->end_addr = 0; |
| 381 | } |
| 382 | else |
| 383 | htx->end_addr += sz; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 388 | BUG_ON((int32_t)htx->head_addr < 0); |
| 389 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 390 | BUG_ON(htx->head_addr > htx->end_addr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 391 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 394 | /* Truncate all blocks after the one containing the offset <offset>. This last |
| 395 | * one is truncated too. |
| 396 | */ |
| 397 | void htx_truncate(struct htx *htx, uint32_t offset) |
| 398 | { |
| 399 | struct htx_blk *blk; |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 400 | |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 401 | for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 402 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 403 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 404 | |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 405 | if (offset >= sz) { |
| 406 | offset -= sz; |
| 407 | continue; |
| 408 | } |
Christopher Faulet | 41dc843 | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 409 | if (type == HTX_BLK_DATA) |
| 410 | htx_change_blk_value_len(htx, blk, offset); |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 411 | offset = 0; |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 412 | } |
| 413 | while (blk) |
| 414 | blk = htx_remove_blk(htx, blk); |
| 415 | } |
| 416 | |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 417 | /* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if |
| 418 | * necessary. Others blocks will be removed at once if <count> is large |
| 419 | * enough. The function returns an htx_ret with the first block remaing in the |
| 420 | * messsage and the amount of data drained. If everything is removed, |
| 421 | * htx_ret.blk is set to NULL. |
| 422 | */ |
| 423 | struct htx_ret htx_drain(struct htx *htx, uint32_t count) |
| 424 | { |
| 425 | struct htx_blk *blk; |
| 426 | struct htx_ret htxret = { .blk = NULL, .ret = 0 }; |
| 427 | |
Christopher Faulet | 0f6d6a9 | 2019-05-23 11:11:52 +0200 | [diff] [blame] | 428 | if (count == htx->data) { |
| 429 | htx_reset(htx); |
| 430 | htxret.ret = count; |
| 431 | return htxret; |
| 432 | } |
| 433 | |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 434 | blk = htx_get_head_blk(htx); |
| 435 | while (count && blk) { |
| 436 | uint32_t sz = htx_get_blksz(blk); |
| 437 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 438 | |
| 439 | /* Ingore unused block */ |
| 440 | if (type == HTX_BLK_UNUSED) |
| 441 | goto next; |
| 442 | |
| 443 | if (sz > count) { |
| 444 | if (type == HTX_BLK_DATA) { |
| 445 | htx_cut_data_blk(htx, blk, count); |
| 446 | htxret.ret += count; |
| 447 | } |
| 448 | break; |
| 449 | } |
| 450 | count -= sz; |
| 451 | htxret.ret += sz; |
| 452 | next: |
| 453 | blk = htx_remove_blk(htx, blk); |
| 454 | } |
| 455 | htxret.blk = blk; |
| 456 | |
| 457 | return htxret; |
| 458 | } |
| 459 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 460 | /* Tries to append data to the last inserted block, if the type matches and if |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 461 | * there is enough space to take it all. If the space wraps, the buffer is |
| 462 | * defragmented and a new block is inserted. If an error occurred, NULL is |
Christopher Faulet | 6177509 | 2019-05-07 21:42:27 +0200 | [diff] [blame] | 463 | * returned. Otherwise, on success, the updated block (or the new one) is |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 464 | * returned. Due to its nature this function can be expensive and should be |
| 465 | * avoided whenever possible. |
| 466 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 467 | struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 468 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 469 | struct htx_blk *blk, *tailblk; |
| 470 | void *ptr; |
| 471 | uint32_t len, sz, tailroom, headroom; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 472 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 473 | if (!htx->used) |
| 474 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 475 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 476 | /* Not enough space to store data */ |
| 477 | if (data.len > htx_free_data_space(htx)) |
| 478 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 479 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 480 | /* get the tail block and its size */ |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 481 | tailblk = htx_get_tail_blk(htx); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 482 | if (tailblk == NULL) |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 483 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 484 | sz = htx_get_blksz(tailblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 485 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 486 | /* Don't try to append data if the last inserted block is not of the |
| 487 | * same type */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 488 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 489 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 490 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 491 | /* |
| 492 | * Same type and enough space: append data |
| 493 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 494 | headroom = (htx->end_addr - htx->head_addr); |
| 495 | tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr; |
| 496 | BUG_ON((int32_t)headroom < 0); |
| 497 | BUG_ON((int32_t)tailroom < 0); |
| 498 | |
| 499 | len = data.len; |
| 500 | if (tailblk->addr+sz == htx->tail_addr) { |
| 501 | if (data.len <= tailroom) |
| 502 | goto append_data; |
| 503 | else if (!htx->head_addr) { |
| 504 | len = tailroom; |
| 505 | goto append_data; |
| 506 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 507 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 508 | else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom) |
| 509 | goto append_data; |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 510 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 511 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 512 | |
| 513 | append_data: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 514 | /* FIXME: check v.len + data.len < 256MB */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 515 | /* Append data and update the block itself */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 516 | ptr = htx_get_blk_ptr(htx, tailblk); |
| 517 | memcpy(ptr+sz, data.ptr, len); |
Christopher Faulet | 41dc843 | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 518 | htx_change_blk_value_len(htx, tailblk, sz+len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 519 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 520 | if (data.len == len) { |
| 521 | blk = tailblk; |
| 522 | goto end; |
| 523 | } |
| 524 | data.ptr += len; |
| 525 | data.len -= len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 526 | |
| 527 | add_new_block: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 528 | /* FIXME: check data.len (< 256MB) */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 529 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 530 | if (!blk) |
| 531 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 533 | blk->info += data.len; |
| 534 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 535 | |
| 536 | end: |
| 537 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 538 | BUG_ON((int32_t)htx->head_addr < 0); |
| 539 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 540 | BUG_ON(htx->head_addr > htx->end_addr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 541 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Replaces a value part of a block by a new one. The new part can be smaller or |
| 545 | * larger than the old one. This function works for any kind of block with |
| 546 | * attached data. It returns the new block on success, otherwise it returns |
| 547 | * NULL. |
| 548 | */ |
| 549 | struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 550 | const struct ist old, const struct ist new) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 551 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 552 | struct ist n, v; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 553 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 554 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 555 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 556 | n = htx_get_blk_name(htx, blk); |
| 557 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 558 | delta = new.len - old.len; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 559 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 560 | if (!ret) |
| 561 | return NULL; /* not enough space */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 562 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 563 | /* Before updating the payload, set the new block size and update HTX |
| 564 | * message */ |
| 565 | htx_set_blk_value_len(blk, v.len + delta); |
| 566 | htx->data += delta; |
| 567 | |
| 568 | if (ret == 1) { |
| 569 | if (delta <= 0) { |
| 570 | /* compression: copy new data first */ |
| 571 | memcpy(old.ptr, new.ptr, new.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 572 | memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 573 | } |
| 574 | else { |
| 575 | /* expansion: move the end first */ |
| 576 | memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 577 | memcpy(old.ptr, new.ptr, new.len); |
| 578 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 579 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 580 | else if (ret == 2) { |
| 581 | void *ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 582 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 583 | /* Copy the name, if any */ |
| 584 | memcpy(ptr, n.ptr, n.len); |
| 585 | ptr += n.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 586 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 587 | /* Copy value before old part, if any */ |
| 588 | memcpy(ptr, v.ptr, old.ptr - v.ptr); |
| 589 | ptr += old.ptr - v.ptr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 590 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 591 | /* Copy new value */ |
| 592 | memcpy(ptr, new.ptr, new.len); |
| 593 | ptr += new.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 594 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 595 | /* Copy value after old part, if any */ |
| 596 | memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 597 | } |
| 598 | else { |
| 599 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 600 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 601 | /* Copy the header name, if any */ |
| 602 | chunk_memcat(tmp, n.ptr, n.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 603 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 604 | /* Copy value before old part, if any */ |
| 605 | chunk_memcat(tmp, v.ptr, old.ptr - v.ptr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 606 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 607 | /* Copy new value */ |
| 608 | chunk_memcat(tmp, new.ptr, new.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 609 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 610 | /* Copy value after old part if any */ |
| 611 | chunk_memcat(tmp, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 612 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 613 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 614 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 615 | /* Finaly, copy data. */ |
| 616 | memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data); |
| 617 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 618 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 622 | * type <mark> (typically EOH or EOM) or when <count> bytes were moved |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 623 | * (including payload and meta-data). It returns the number of bytes moved and |
| 624 | * the last HTX block inserted in <dst>. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 625 | */ |
| 626 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 627 | enum htx_blk_type mark) |
| 628 | { |
| 629 | struct htx_blk *blk, *dstblk; |
| 630 | enum htx_blk_type type; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 631 | uint32_t info, max, sz, ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 632 | int inside_trailers = 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 633 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 634 | ret = htx_used_space(dst); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 635 | blk = htx_get_blk(src, htx_get_head(src)); |
| 636 | dstblk = NULL; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 637 | |
| 638 | while (blk && count) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 639 | type = htx_get_blk_type(blk); |
| 640 | |
| 641 | /* Ingore unused block */ |
| 642 | if (type == HTX_BLK_UNUSED) |
| 643 | goto next; |
| 644 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 645 | /* Be sure to have enough space to xfer all headers/trailers in |
| 646 | * one time. If not while <dst> is empty, we report a parsing |
| 647 | * error on <src>. |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 648 | */ |
| 649 | if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) { |
| 650 | struct htx_sl *sl = htx_get_blk_ptr(src, blk); |
| 651 | |
| 652 | if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) { |
| 653 | if (htx_is_empty(dst)) |
| 654 | src->flags |= HTX_FL_PARSING_ERROR; |
| 655 | break; |
| 656 | } |
| 657 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 658 | else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && |
| 659 | !inside_trailers && mark >= HTX_BLK_EOT) { |
| 660 | inside_trailers = 1; |
| 661 | if (htx_used_space(src) > count) { |
| 662 | if (htx_is_empty(dst)) |
| 663 | src->flags |= HTX_FL_PARSING_ERROR; |
| 664 | break; |
| 665 | } |
| 666 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 667 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 668 | sz = htx_get_blksz(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 669 | info = blk->info; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 670 | max = htx_get_max_blksz(dst, count); |
| 671 | if (!max) |
| 672 | break; |
Willy Tarreau | 90caa07 | 2019-04-09 16:21:54 +0200 | [diff] [blame] | 673 | if (sz > max) { |
Christopher Faulet | 39744f7 | 2019-05-24 14:54:00 +0200 | [diff] [blame] | 674 | /* Headers must be fully copied */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 675 | if (type != HTX_BLK_DATA) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 676 | break; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 677 | sz = max; |
| 678 | info = (type << 28) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | dstblk = htx_reserve_nxblk(dst, sz); |
| 682 | if (!dstblk) |
| 683 | break; |
| 684 | dstblk->info = info; |
| 685 | memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz); |
| 686 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 687 | count -= sizeof(dstblk) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 688 | if (blk->info != info) { |
| 689 | /* Partial move: don't remove <blk> from <src> but |
| 690 | * resize its content */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 691 | htx_cut_data_blk(src, blk, sz); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 692 | break; |
| 693 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 694 | next: |
| 695 | blk = htx_remove_blk(src, blk); |
| 696 | if (type == mark) |
| 697 | break; |
| 698 | |
| 699 | } |
| 700 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 701 | end: |
| 702 | ret = htx_used_space(dst) - ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 703 | return (struct htx_ret){.ret = ret, .blk = dstblk}; |
| 704 | } |
| 705 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 706 | /* Replaces an header by a new one. The new header can be smaller or larger than |
| 707 | * the old one. It returns the new block on success, otherwise it returns NULL. |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 708 | * The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 709 | */ |
| 710 | struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 711 | const struct ist name, const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 712 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 713 | enum htx_blk_type type; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 714 | void *ptr; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 715 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 716 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 717 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 718 | type = htx_get_blk_type(blk); |
| 719 | if (type != HTX_BLK_HDR) |
| 720 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 721 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 722 | delta = name.len + value.len - htx_get_blksz(blk); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 723 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 724 | if (!ret) |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 725 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 726 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 727 | /* Set the new block size and update HTX message */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 728 | blk->info = (type << 28) + (value.len << 8) + name.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 729 | htx->data += delta; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 730 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 731 | if (ret == 3) |
| 732 | blk = htx_defrag(htx, blk); |
| 733 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 734 | /* Finaly, copy data. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 735 | ptr = htx_get_blk_ptr(htx, blk); |
| 736 | ist2bin_lc(ptr, name); |
| 737 | memcpy(ptr + name.len, value.ptr, value.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 738 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 741 | /* Replaces the parts of the start-line. It returns the new start-line on |
| 742 | * success, otherwise it returns NULL. It is the caller responsibility to update |
| 743 | * sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 744 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 745 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 746 | const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 747 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 748 | enum htx_blk_type type; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 749 | struct htx_sl *sl; |
| 750 | struct htx_sl tmp; /* used to save sl->info and sl->flags */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 751 | uint32_t sz; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 752 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 753 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 754 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 755 | type = htx_get_blk_type(blk); |
Willy Tarreau | c706cd7 | 2018-12-07 17:12:22 +0100 | [diff] [blame] | 756 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 757 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 758 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 759 | /* Save start-line info and flags */ |
| 760 | sl = htx_get_blk_ptr(htx, blk); |
| 761 | tmp.info = sl->info; |
| 762 | tmp.flags = sl->flags; |
Christopher Faulet | 703bed5 | 2019-06-25 21:31:26 +0200 | [diff] [blame] | 763 | tmp.hdrs_bytes = sl->hdrs_bytes; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 764 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 765 | sz = htx_get_blksz(blk); |
| 766 | delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz; |
| 767 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 768 | if (!ret) |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 769 | return NULL; /* not enough space */ |
| 770 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 771 | /* Set the new block size and update HTX message */ |
| 772 | htx_set_blk_value_len(blk, sz+delta); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 773 | htx->data += delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 774 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 775 | if (ret == 3) |
| 776 | blk = htx_defrag(htx, blk); |
| 777 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 778 | /* Restore start-line info and flags and copy parts of the start-line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 779 | sl = htx_get_blk_ptr(htx, blk); |
| 780 | sl->info = tmp.info; |
| 781 | sl->flags = tmp.flags; |
Christopher Faulet | 703bed5 | 2019-06-25 21:31:26 +0200 | [diff] [blame] | 782 | sl->hdrs_bytes = tmp.hdrs_bytes; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 783 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 784 | HTX_SL_P1_LEN(sl) = p1.len; |
| 785 | HTX_SL_P2_LEN(sl) = p2.len; |
| 786 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 787 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 788 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 789 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 790 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 791 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 792 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 795 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 796 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 797 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 798 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 799 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 800 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 801 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 802 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 803 | uint32_t size; |
| 804 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 805 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 806 | return NULL; |
| 807 | |
| 808 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 809 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 810 | /* FIXME: check size (< 256MB) */ |
| 811 | blk = htx_add_blk(htx, type, size); |
| 812 | if (!blk) |
| 813 | return NULL; |
| 814 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 815 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 816 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 817 | sl->hdrs_bytes = -1; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 818 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 819 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 820 | HTX_SL_P1_LEN(sl) = p1.len; |
| 821 | HTX_SL_P2_LEN(sl) = p2.len; |
| 822 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 823 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 824 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 825 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 826 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 827 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 828 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /* Adds an HTX block of type HDR in <htx>. It returns the new block on |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 832 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 833 | */ |
| 834 | struct htx_blk *htx_add_header(struct htx *htx, const struct ist name, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 835 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 836 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 837 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 838 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 839 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 840 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 841 | if (!blk) |
| 842 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 843 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 844 | blk->info += (value.len << 8) + name.len; |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 845 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 846 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 847 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 850 | /* Adds an HTX block of type TLR in <htx>. It returns the new block on |
| 851 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
| 852 | */ |
| 853 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, |
| 854 | const struct ist value) |
| 855 | { |
| 856 | struct htx_blk *blk; |
| 857 | |
| 858 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 859 | blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len); |
| 860 | if (!blk) |
| 861 | return NULL; |
| 862 | |
| 863 | blk->info += (value.len << 8) + name.len; |
| 864 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
| 865 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 866 | return blk; |
| 867 | } |
| 868 | |
Willy Tarreau | 52610e9 | 2019-01-03 18:26:17 +0100 | [diff] [blame] | 869 | /* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the |
| 870 | * new block on success. Otherwise, it returns NULL. The caller is responsible |
| 871 | * for filling the block itself. |
| 872 | */ |
| 873 | struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 874 | { |
| 875 | struct htx_blk *blk; |
| 876 | |
| 877 | blk = htx_add_blk(htx, type, blksz); |
| 878 | if (!blk) |
| 879 | return NULL; |
| 880 | |
| 881 | blk->info += blksz; |
| 882 | return blk; |
| 883 | } |
| 884 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 885 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 886 | { |
| 887 | int i; |
| 888 | |
| 889 | for (i = 0; hdrs[i].n.len; i++) { |
| 890 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 891 | return NULL; |
| 892 | } |
| 893 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 894 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 895 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 896 | struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs) |
| 897 | { |
| 898 | int i; |
| 899 | |
| 900 | for (i = 0; hdrs[i].n.len; i++) { |
| 901 | if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v)) |
| 902 | return NULL; |
| 903 | } |
| 904 | return htx_add_endof(htx, HTX_BLK_EOT); |
| 905 | } |
| 906 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 907 | /* Adds an HTX block of type EOH or EOM in <htx>. It returns the new block |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 908 | * on success. Otherwise, it returns NULL. |
| 909 | */ |
| 910 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 911 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 912 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 913 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 914 | blk = htx_add_blk(htx, type, 1); |
| 915 | if (!blk) |
| 916 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 917 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 918 | blk->info += 1; |
| 919 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
| 923 | /* Adds an HTX block of type DATA in <htx>. It first tries to append data if |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 924 | * possible. It returns the number of bytes consumed from <data>, which may be |
| 925 | * zero if nothing could be copied. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 926 | */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 927 | size_t htx_add_data(struct htx *htx, const struct ist data) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 928 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 929 | struct htx_blk *blk, *tailblk; |
| 930 | void *ptr; |
| 931 | uint32_t sz, room; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 932 | int32_t len = data.len; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 933 | |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 934 | if (!htx->used) |
| 935 | goto add_new_block; |
| 936 | |
| 937 | /* Not enough space to store data */ |
| 938 | if (len > htx_free_data_space(htx)) |
| 939 | len = htx_free_data_space(htx); |
| 940 | |
| 941 | if (!len) |
| 942 | return 0; |
| 943 | |
| 944 | /* get the tail and head block */ |
| 945 | tailblk = htx_get_tail_blk(htx); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 946 | if (tailblk == NULL) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 947 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 948 | sz = htx_get_blksz(tailblk); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 949 | |
| 950 | /* Don't try to append data if the last inserted block is not of the |
| 951 | * same type */ |
| 952 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
| 953 | goto add_new_block; |
| 954 | |
| 955 | /* |
| 956 | * Same type and enough space: append data |
| 957 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 958 | if (!htx->head_addr) { |
| 959 | if (tailblk->addr+sz != htx->tail_addr) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 960 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 961 | room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 962 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 963 | else { |
| 964 | if (tailblk->addr+sz != htx->head_addr) |
| 965 | goto add_new_block; |
| 966 | room = (htx->end_addr - htx->head_addr); |
| 967 | } |
| 968 | BUG_ON((int32_t)room < 0); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 969 | if (room < len) |
| 970 | len = room; |
| 971 | |
| 972 | append_data: |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 973 | /* FIXME: check v.len + data.len < 256MB */ |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 974 | /* Append data and update the block itself */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 975 | ptr = htx_get_blk_ptr(htx, tailblk); |
| 976 | memcpy(ptr + sz, data.ptr, len); |
Christopher Faulet | 41dc843 | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 977 | htx_change_blk_value_len(htx, tailblk, sz+len); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 978 | |
| 979 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 980 | BUG_ON((int32_t)htx->head_addr < 0); |
| 981 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 982 | BUG_ON(htx->head_addr > htx->end_addr); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 983 | return len; |
| 984 | |
| 985 | add_new_block: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 986 | /* FIXME: check data.len (< 256MB) */ |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 987 | blk = htx_add_blk(htx, HTX_BLK_DATA, len); |
| 988 | if (!blk) |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 989 | return 0; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 990 | |
| 991 | blk->info += len; |
| 992 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len); |
| 993 | return len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 994 | } |
| 995 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 996 | |
| 997 | /* Adds an HTX block of type DATA in <htx> just after all other DATA |
| 998 | * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a |
| 999 | * DATA block if possible. But, if the function succeeds, it will be the last |
| 1000 | * DATA block in all cases. If an error occurred, NULL is returned. Otherwise, |
| 1001 | * on success, the updated block (or the new one) is returned. |
| 1002 | */ |
| 1003 | struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data) |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1004 | { |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1005 | struct htx_blk *blk, *pblk; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1006 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1007 | blk = htx_add_data_atonce(htx, data); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1008 | if (!blk) |
| 1009 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1010 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1011 | for (pblk = htx_get_prev_blk(htx, blk); pblk; pblk = htx_get_prev_blk(htx, pblk)) { |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1012 | if (htx_get_blk_type(pblk) <= HTX_BLK_DATA) |
| 1013 | break; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1014 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1015 | /* Swap .addr and .info fields */ |
| 1016 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 1017 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 1018 | |
| 1019 | if (blk->addr == pblk->addr) |
| 1020 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1021 | blk = pblk; |
| 1022 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 1023 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1024 | return blk; |
| 1025 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1026 | |
Christopher Faulet | 86fcf6d | 2019-06-11 10:41:19 +0200 | [diff] [blame] | 1027 | /* Moves the block <blk> just before the block <ref>. Both blocks must be in the |
| 1028 | * HTX message <htx> and <blk> must be placed after <ref>. pointer to these |
| 1029 | * blocks are updated to remain valid after the move. */ |
| 1030 | void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref) |
| 1031 | { |
| 1032 | struct htx_blk *cblk, *pblk; |
| 1033 | |
| 1034 | cblk = *blk; |
| 1035 | for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) { |
| 1036 | /* Swap .addr and .info fields */ |
| 1037 | cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr; |
| 1038 | cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info; |
| 1039 | |
| 1040 | if (cblk->addr == pblk->addr) |
| 1041 | cblk->addr += htx_get_blksz(pblk); |
| 1042 | if (pblk == *ref) |
| 1043 | break; |
| 1044 | cblk = pblk; |
| 1045 | } |
| 1046 | *blk = cblk; |
| 1047 | *ref = pblk; |
| 1048 | } |
| 1049 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1050 | /* Appends the H1 representation of the request line block <blk> to the |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1051 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 1052 | * returns 0. |
| 1053 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1054 | int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1055 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1056 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1057 | return 0; |
| 1058 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1059 | chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1060 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1061 | chunk_memcat(chk, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1062 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 1063 | if (sl->flags & HTX_SL_F_VER_11) |
| 1064 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 1065 | else |
| 1066 | chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 1067 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1068 | chunk_memcat(chk, "\r\n", 2); |
| 1069 | |
| 1070 | return 1; |
| 1071 | } |
| 1072 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1073 | /* Appends the H1 representation of the status line block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1074 | * <chk>. It returns 1 if data are successfully appended, otherwise it |
| 1075 | * returns 0. |
| 1076 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1077 | int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1078 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1079 | if (HTX_SL_LEN(sl) + 4 > b_size(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1080 | return 0; |
| 1081 | |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 1082 | if (sl->flags & HTX_SL_F_VER_11) |
| 1083 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 1084 | else |
| 1085 | chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1086 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1087 | chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1088 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1089 | chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1090 | chunk_memcat(chk, "\r\n", 2); |
| 1091 | |
| 1092 | return 1; |
| 1093 | } |
| 1094 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1095 | /* Appends the H1 representation of the header block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1096 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 1097 | * 0. |
| 1098 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1099 | int htx_hdr_to_h1(const struct ist n, const struct ist v, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1100 | { |
| 1101 | if (n.len + v.len + 4 > b_room(chk)) |
| 1102 | return 0; |
| 1103 | |
| 1104 | chunk_memcat(chk, n.ptr, n.len); |
| 1105 | chunk_memcat(chk, ": ", 2); |
| 1106 | chunk_memcat(chk, v.ptr, v.len); |
| 1107 | chunk_memcat(chk, "\r\n", 2); |
| 1108 | |
| 1109 | return 1; |
| 1110 | } |
| 1111 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1112 | /* Appends the H1 representation of the data block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1113 | * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It |
| 1114 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 1115 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1116 | int htx_data_to_h1(const struct ist data, struct buffer *chk, int chunked) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1117 | { |
| 1118 | if (chunked) { |
| 1119 | uint32_t chksz; |
| 1120 | char tmp[10]; |
| 1121 | char *beg, *end; |
| 1122 | |
| 1123 | chksz = data.len; |
| 1124 | |
| 1125 | beg = end = tmp+10; |
| 1126 | *--beg = '\n'; |
| 1127 | *--beg = '\r'; |
| 1128 | do { |
| 1129 | *--beg = hextab[chksz & 0xF]; |
| 1130 | } while (chksz >>= 4); |
| 1131 | |
| 1132 | if (data.len + (end - beg) + 2 > b_room(chk)) |
| 1133 | return 0; |
| 1134 | chunk_memcat(chk, beg, end - beg); |
| 1135 | chunk_memcat(chk, data.ptr, data.len); |
| 1136 | chunk_memcat(chk, "\r\n", 2); |
| 1137 | } |
| 1138 | else { |
| 1139 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1143 | return 1; |
| 1144 | } |