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 | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 409 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 410 | htx_set_blk_value_len(blk, offset); |
| 411 | htx->data -= (sz - offset); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 412 | |
| 413 | if (blk->addr+sz == htx->tail_addr) |
| 414 | htx->tail_addr -= offset; |
| 415 | else if (blk->addr+sz == htx->head_addr) |
| 416 | htx->head_addr -= offset; |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 417 | } |
| 418 | offset = 0; |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 419 | } |
| 420 | while (blk) |
| 421 | blk = htx_remove_blk(htx, blk); |
| 422 | } |
| 423 | |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 424 | /* Drain <count> bytes from the HTX message <htx>. DATA blocks will be cut if |
| 425 | * necessary. Others blocks will be removed at once if <count> is large |
| 426 | * enough. The function returns an htx_ret with the first block remaing in the |
| 427 | * messsage and the amount of data drained. If everything is removed, |
| 428 | * htx_ret.blk is set to NULL. |
| 429 | */ |
| 430 | struct htx_ret htx_drain(struct htx *htx, uint32_t count) |
| 431 | { |
| 432 | struct htx_blk *blk; |
| 433 | struct htx_ret htxret = { .blk = NULL, .ret = 0 }; |
| 434 | |
Christopher Faulet | 0f6d6a9 | 2019-05-23 11:11:52 +0200 | [diff] [blame] | 435 | if (count == htx->data) { |
| 436 | htx_reset(htx); |
| 437 | htxret.ret = count; |
| 438 | return htxret; |
| 439 | } |
| 440 | |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 441 | blk = htx_get_head_blk(htx); |
| 442 | while (count && blk) { |
| 443 | uint32_t sz = htx_get_blksz(blk); |
| 444 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 445 | |
| 446 | /* Ingore unused block */ |
| 447 | if (type == HTX_BLK_UNUSED) |
| 448 | goto next; |
| 449 | |
| 450 | if (sz > count) { |
| 451 | if (type == HTX_BLK_DATA) { |
| 452 | htx_cut_data_blk(htx, blk, count); |
| 453 | htxret.ret += count; |
| 454 | } |
| 455 | break; |
| 456 | } |
| 457 | count -= sz; |
| 458 | htxret.ret += sz; |
| 459 | next: |
| 460 | blk = htx_remove_blk(htx, blk); |
| 461 | } |
| 462 | htxret.blk = blk; |
| 463 | |
| 464 | return htxret; |
| 465 | } |
| 466 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 467 | /* 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] | 468 | * there is enough space to take it all. If the space wraps, the buffer is |
| 469 | * 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] | 470 | * returned. Otherwise, on success, the updated block (or the new one) is |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 471 | * returned. Due to its nature this function can be expensive and should be |
| 472 | * avoided whenever possible. |
| 473 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 474 | 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] | 475 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 476 | struct htx_blk *blk, *tailblk; |
| 477 | void *ptr; |
| 478 | uint32_t len, sz, tailroom, headroom; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 479 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 480 | if (!htx->used) |
| 481 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 482 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 483 | /* Not enough space to store data */ |
| 484 | if (data.len > htx_free_data_space(htx)) |
| 485 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 486 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 487 | /* get the tail block and its size */ |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 488 | tailblk = htx_get_tail_blk(htx); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 489 | if (tailblk == NULL) |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 490 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 491 | sz = htx_get_blksz(tailblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 492 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 493 | /* Don't try to append data if the last inserted block is not of the |
| 494 | * same type */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 495 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 496 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 497 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 498 | /* |
| 499 | * Same type and enough space: append data |
| 500 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 501 | headroom = (htx->end_addr - htx->head_addr); |
| 502 | tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - htx->tail_addr; |
| 503 | BUG_ON((int32_t)headroom < 0); |
| 504 | BUG_ON((int32_t)tailroom < 0); |
| 505 | |
| 506 | len = data.len; |
| 507 | if (tailblk->addr+sz == htx->tail_addr) { |
| 508 | if (data.len <= tailroom) |
| 509 | goto append_data; |
| 510 | else if (!htx->head_addr) { |
| 511 | len = tailroom; |
| 512 | goto append_data; |
| 513 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 514 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 515 | else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom) |
| 516 | goto append_data; |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 517 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 518 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 519 | |
| 520 | append_data: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 521 | /* FIXME: check v.len + data.len < 256MB */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 522 | /* Append data and update the block itself */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 523 | ptr = htx_get_blk_ptr(htx, tailblk); |
| 524 | memcpy(ptr+sz, data.ptr, len); |
| 525 | htx_set_blk_value_len(tailblk, sz+len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 526 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 527 | /* Update HTTP message */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 528 | htx->data += len; |
| 529 | if (tailblk->addr+sz == htx->tail_addr) |
| 530 | htx->tail_addr += len; |
| 531 | else if (tailblk->addr+sz == htx->head_addr) |
| 532 | htx->head_addr += len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 533 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 534 | if (data.len == len) { |
| 535 | blk = tailblk; |
| 536 | goto end; |
| 537 | } |
| 538 | data.ptr += len; |
| 539 | data.len -= len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 540 | |
| 541 | add_new_block: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 542 | /* FIXME: check data.len (< 256MB) */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 543 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 544 | if (!blk) |
| 545 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 546 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 547 | blk->info += data.len; |
| 548 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 549 | |
| 550 | end: |
| 551 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 552 | BUG_ON((int32_t)htx->head_addr < 0); |
| 553 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 554 | BUG_ON(htx->head_addr > htx->end_addr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 555 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Replaces a value part of a block by a new one. The new part can be smaller or |
| 559 | * larger than the old one. This function works for any kind of block with |
| 560 | * attached data. It returns the new block on success, otherwise it returns |
| 561 | * NULL. |
| 562 | */ |
| 563 | 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] | 564 | const struct ist old, const struct ist new) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 565 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 566 | struct ist n, v; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 567 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 568 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 569 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 570 | n = htx_get_blk_name(htx, blk); |
| 571 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 572 | delta = new.len - old.len; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 573 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 574 | if (!ret) |
| 575 | return NULL; /* not enough space */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 576 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 577 | /* Before updating the payload, set the new block size and update HTX |
| 578 | * message */ |
| 579 | htx_set_blk_value_len(blk, v.len + delta); |
| 580 | htx->data += delta; |
| 581 | |
| 582 | if (ret == 1) { |
| 583 | if (delta <= 0) { |
| 584 | /* compression: copy new data first */ |
| 585 | memcpy(old.ptr, new.ptr, new.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 586 | 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] | 587 | } |
| 588 | else { |
| 589 | /* expansion: move the end first */ |
| 590 | memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 591 | memcpy(old.ptr, new.ptr, new.len); |
| 592 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 593 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 594 | else if (ret == 2) { |
| 595 | void *ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 596 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 597 | /* Copy the name, if any */ |
| 598 | memcpy(ptr, n.ptr, n.len); |
| 599 | ptr += n.len; |
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 value before old part, if any */ |
| 602 | memcpy(ptr, v.ptr, old.ptr - v.ptr); |
| 603 | ptr += old.ptr - v.ptr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 604 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 605 | /* Copy new value */ |
| 606 | memcpy(ptr, new.ptr, new.len); |
| 607 | ptr += new.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 608 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 609 | /* Copy value after old part, if any */ |
| 610 | memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 611 | } |
| 612 | else { |
| 613 | struct buffer *tmp = get_trash_chunk(); |
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 | /* Copy the header name, if any */ |
| 616 | chunk_memcat(tmp, n.ptr, n.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 617 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 618 | /* Copy value before old part, if any */ |
| 619 | chunk_memcat(tmp, v.ptr, old.ptr - v.ptr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 620 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 621 | /* Copy new value */ |
| 622 | chunk_memcat(tmp, new.ptr, new.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 623 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 624 | /* Copy value after old part if any */ |
| 625 | 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] | 626 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 627 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 628 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 629 | /* Finaly, copy data. */ |
| 630 | memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data); |
| 631 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 632 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* 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] | 636 | * type <mark> (typically EOH or EOM) or when <count> bytes were moved |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 637 | * (including payload and meta-data). It returns the number of bytes moved and |
| 638 | * the last HTX block inserted in <dst>. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 639 | */ |
| 640 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 641 | enum htx_blk_type mark) |
| 642 | { |
| 643 | struct htx_blk *blk, *dstblk; |
| 644 | enum htx_blk_type type; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 645 | uint32_t info, max, sz, ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 646 | int inside_trailers = 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 647 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 648 | ret = htx_used_space(dst); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 649 | blk = htx_get_blk(src, htx_get_head(src)); |
| 650 | dstblk = NULL; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 651 | |
| 652 | while (blk && count) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 653 | type = htx_get_blk_type(blk); |
| 654 | |
| 655 | /* Ingore unused block */ |
| 656 | if (type == HTX_BLK_UNUSED) |
| 657 | goto next; |
| 658 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 659 | /* Be sure to have enough space to xfer all headers/trailers in |
| 660 | * one time. If not while <dst> is empty, we report a parsing |
| 661 | * error on <src>. |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 662 | */ |
| 663 | if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) { |
| 664 | struct htx_sl *sl = htx_get_blk_ptr(src, blk); |
| 665 | |
| 666 | if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) { |
| 667 | if (htx_is_empty(dst)) |
| 668 | src->flags |= HTX_FL_PARSING_ERROR; |
| 669 | break; |
| 670 | } |
| 671 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 672 | else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && |
| 673 | !inside_trailers && mark >= HTX_BLK_EOT) { |
| 674 | inside_trailers = 1; |
| 675 | if (htx_used_space(src) > count) { |
| 676 | if (htx_is_empty(dst)) |
| 677 | src->flags |= HTX_FL_PARSING_ERROR; |
| 678 | break; |
| 679 | } |
| 680 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 681 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 682 | sz = htx_get_blksz(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 683 | info = blk->info; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 684 | max = htx_get_max_blksz(dst, count); |
| 685 | if (!max) |
| 686 | break; |
Willy Tarreau | 90caa07 | 2019-04-09 16:21:54 +0200 | [diff] [blame] | 687 | if (sz > max) { |
Christopher Faulet | 39744f7 | 2019-05-24 14:54:00 +0200 | [diff] [blame] | 688 | /* Headers must be fully copied */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 689 | if (type != HTX_BLK_DATA) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 690 | break; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 691 | sz = max; |
| 692 | info = (type << 28) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | dstblk = htx_reserve_nxblk(dst, sz); |
| 696 | if (!dstblk) |
| 697 | break; |
| 698 | dstblk->info = info; |
| 699 | memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz); |
| 700 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 701 | count -= sizeof(dstblk) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 702 | if (blk->info != info) { |
| 703 | /* Partial move: don't remove <blk> from <src> but |
| 704 | * resize its content */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 705 | htx_cut_data_blk(src, blk, sz); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 706 | break; |
| 707 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 708 | next: |
| 709 | blk = htx_remove_blk(src, blk); |
| 710 | if (type == mark) |
| 711 | break; |
| 712 | |
| 713 | } |
| 714 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 715 | end: |
| 716 | ret = htx_used_space(dst) - ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 717 | return (struct htx_ret){.ret = ret, .blk = dstblk}; |
| 718 | } |
| 719 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 720 | /* Replaces an header by a new one. The new header can be smaller or larger than |
| 721 | * 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] | 722 | * The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 723 | */ |
| 724 | 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] | 725 | const struct ist name, const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 726 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 727 | enum htx_blk_type type; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 728 | void *ptr; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 729 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 730 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 731 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 732 | type = htx_get_blk_type(blk); |
| 733 | if (type != HTX_BLK_HDR) |
| 734 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 735 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 736 | delta = name.len + value.len - htx_get_blksz(blk); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 737 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 738 | if (!ret) |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 739 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 740 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 741 | /* Set the new block size and update HTX message */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 742 | blk->info = (type << 28) + (value.len << 8) + name.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 743 | htx->data += delta; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 744 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 745 | if (ret == 3) |
| 746 | blk = htx_defrag(htx, blk); |
| 747 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 748 | /* Finaly, copy data. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 749 | ptr = htx_get_blk_ptr(htx, blk); |
| 750 | ist2bin_lc(ptr, name); |
| 751 | memcpy(ptr + name.len, value.ptr, value.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 752 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 755 | /* Replaces the parts of the start-line. It returns the new start-line on |
| 756 | * success, otherwise it returns NULL. It is the caller responsibility to update |
| 757 | * sl->info, if necessary. |
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 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 760 | const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 761 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 762 | enum htx_blk_type type; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 763 | struct htx_sl *sl; |
| 764 | struct htx_sl tmp; /* used to save sl->info and sl->flags */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 765 | uint32_t sz; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 766 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 767 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 768 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 769 | type = htx_get_blk_type(blk); |
Willy Tarreau | c706cd7 | 2018-12-07 17:12:22 +0100 | [diff] [blame] | 770 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 771 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 772 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 773 | /* Save start-line info and flags */ |
| 774 | sl = htx_get_blk_ptr(htx, blk); |
| 775 | tmp.info = sl->info; |
| 776 | tmp.flags = sl->flags; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 777 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 778 | sz = htx_get_blksz(blk); |
| 779 | delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz; |
| 780 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 781 | if (!ret) |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 782 | return NULL; /* not enough space */ |
| 783 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 784 | /* Set the new block size and update HTX message */ |
| 785 | htx_set_blk_value_len(blk, sz+delta); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 786 | htx->data += delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 787 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 788 | if (ret == 3) |
| 789 | blk = htx_defrag(htx, blk); |
| 790 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 791 | /* 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] | 792 | sl = htx_get_blk_ptr(htx, blk); |
| 793 | sl->info = tmp.info; |
| 794 | sl->flags = tmp.flags; |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 795 | if (sl->hdrs_bytes != -1) |
| 796 | sl->hdrs_bytes += delta; |
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 | HTX_SL_P1_LEN(sl) = p1.len; |
| 799 | HTX_SL_P2_LEN(sl) = p2.len; |
| 800 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 801 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 802 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 803 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 804 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 805 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 806 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 809 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 810 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 811 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 812 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 813 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 814 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 815 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 816 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 817 | uint32_t size; |
| 818 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 819 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 820 | return NULL; |
| 821 | |
| 822 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 823 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 824 | /* FIXME: check size (< 256MB) */ |
| 825 | blk = htx_add_blk(htx, type, size); |
| 826 | if (!blk) |
| 827 | return NULL; |
| 828 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 829 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 830 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 831 | sl->hdrs_bytes = -1; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 832 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 833 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 834 | HTX_SL_P1_LEN(sl) = p1.len; |
| 835 | HTX_SL_P2_LEN(sl) = p2.len; |
| 836 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 837 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 838 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 839 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 840 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 841 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 842 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* 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] | 846 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 847 | */ |
| 848 | 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] | 849 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 850 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 851 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 852 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 853 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 854 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 855 | if (!blk) |
| 856 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 857 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 858 | blk->info += (value.len << 8) + name.len; |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 859 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 860 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 861 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 864 | /* Adds an HTX block of type TLR in <htx>. It returns the new block on |
| 865 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
| 866 | */ |
| 867 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, |
| 868 | const struct ist value) |
| 869 | { |
| 870 | struct htx_blk *blk; |
| 871 | |
| 872 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 873 | blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len); |
| 874 | if (!blk) |
| 875 | return NULL; |
| 876 | |
| 877 | blk->info += (value.len << 8) + name.len; |
| 878 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
| 879 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 880 | return blk; |
| 881 | } |
| 882 | |
Willy Tarreau | 52610e9 | 2019-01-03 18:26:17 +0100 | [diff] [blame] | 883 | /* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the |
| 884 | * new block on success. Otherwise, it returns NULL. The caller is responsible |
| 885 | * for filling the block itself. |
| 886 | */ |
| 887 | struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 888 | { |
| 889 | struct htx_blk *blk; |
| 890 | |
| 891 | blk = htx_add_blk(htx, type, blksz); |
| 892 | if (!blk) |
| 893 | return NULL; |
| 894 | |
| 895 | blk->info += blksz; |
| 896 | return blk; |
| 897 | } |
| 898 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 899 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 900 | { |
| 901 | int i; |
| 902 | |
| 903 | for (i = 0; hdrs[i].n.len; i++) { |
| 904 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 905 | return NULL; |
| 906 | } |
| 907 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 908 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 909 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 910 | struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs) |
| 911 | { |
| 912 | int i; |
| 913 | |
| 914 | for (i = 0; hdrs[i].n.len; i++) { |
| 915 | if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v)) |
| 916 | return NULL; |
| 917 | } |
| 918 | return htx_add_endof(htx, HTX_BLK_EOT); |
| 919 | } |
| 920 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 921 | /* 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] | 922 | * on success. Otherwise, it returns NULL. |
| 923 | */ |
| 924 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 925 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 926 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 927 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 928 | blk = htx_add_blk(htx, type, 1); |
| 929 | if (!blk) |
| 930 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 931 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 932 | blk->info += 1; |
| 933 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | |
| 937 | /* 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] | 938 | * possible. It returns the number of bytes consumed from <data>, which may be |
| 939 | * zero if nothing could be copied. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 940 | */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 941 | size_t htx_add_data(struct htx *htx, const struct ist data) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 942 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 943 | struct htx_blk *blk, *tailblk; |
| 944 | void *ptr; |
| 945 | uint32_t sz, room; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 946 | int32_t len = data.len; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 947 | |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 948 | if (!htx->used) |
| 949 | goto add_new_block; |
| 950 | |
| 951 | /* Not enough space to store data */ |
| 952 | if (len > htx_free_data_space(htx)) |
| 953 | len = htx_free_data_space(htx); |
| 954 | |
| 955 | if (!len) |
| 956 | return 0; |
| 957 | |
| 958 | /* get the tail and head block */ |
| 959 | tailblk = htx_get_tail_blk(htx); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 960 | if (tailblk == NULL) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 961 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 962 | sz = htx_get_blksz(tailblk); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 963 | |
| 964 | /* Don't try to append data if the last inserted block is not of the |
| 965 | * same type */ |
| 966 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
| 967 | goto add_new_block; |
| 968 | |
| 969 | /* |
| 970 | * Same type and enough space: append data |
| 971 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 972 | if (!htx->head_addr) { |
| 973 | if (tailblk->addr+sz != htx->tail_addr) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 974 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 975 | 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] | 976 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 977 | else { |
| 978 | if (tailblk->addr+sz != htx->head_addr) |
| 979 | goto add_new_block; |
| 980 | room = (htx->end_addr - htx->head_addr); |
| 981 | } |
| 982 | BUG_ON((int32_t)room < 0); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 983 | if (room < len) |
| 984 | len = room; |
| 985 | |
| 986 | append_data: |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 987 | /* FIXME: check v.len + data.len < 256MB */ |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 988 | /* Append data and update the block itself */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 989 | ptr = htx_get_blk_ptr(htx, tailblk); |
| 990 | memcpy(ptr + sz, data.ptr, len); |
| 991 | htx_set_blk_value_len(tailblk, sz + len); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 992 | |
| 993 | /* Update HTTP message */ |
| 994 | htx->data += len; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 995 | if (tailblk->addr+sz == htx->tail_addr) |
| 996 | htx->tail_addr += len; |
| 997 | else if (tailblk->addr+sz == htx->head_addr) |
| 998 | htx->head_addr += len; |
| 999 | |
| 1000 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 1001 | BUG_ON((int32_t)htx->head_addr < 0); |
| 1002 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 1003 | BUG_ON(htx->head_addr > htx->end_addr); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1004 | return len; |
| 1005 | |
| 1006 | add_new_block: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1007 | /* FIXME: check data.len (< 256MB) */ |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1008 | blk = htx_add_blk(htx, HTX_BLK_DATA, len); |
| 1009 | if (!blk) |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1010 | return 0; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1011 | |
| 1012 | blk->info += len; |
| 1013 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len); |
| 1014 | return len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1017 | |
| 1018 | /* Adds an HTX block of type DATA in <htx> just after all other DATA |
| 1019 | * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a |
| 1020 | * DATA block if possible. But, if the function succeeds, it will be the last |
| 1021 | * DATA block in all cases. If an error occurred, NULL is returned. Otherwise, |
| 1022 | * on success, the updated block (or the new one) is returned. |
| 1023 | */ |
| 1024 | 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] | 1025 | { |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1026 | struct htx_blk *blk, *pblk; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1027 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1028 | blk = htx_add_data_atonce(htx, data); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1029 | if (!blk) |
| 1030 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1031 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1032 | 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] | 1033 | if (htx_get_blk_type(pblk) <= HTX_BLK_DATA) |
| 1034 | break; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1035 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1036 | /* Swap .addr and .info fields */ |
| 1037 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 1038 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 1039 | |
| 1040 | if (blk->addr == pblk->addr) |
| 1041 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1042 | blk = pblk; |
| 1043 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 1044 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1045 | return blk; |
| 1046 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1047 | |
Christopher Faulet | 86fcf6d | 2019-06-11 10:41:19 +0200 | [diff] [blame] | 1048 | /* Moves the block <blk> just before the block <ref>. Both blocks must be in the |
| 1049 | * HTX message <htx> and <blk> must be placed after <ref>. pointer to these |
| 1050 | * blocks are updated to remain valid after the move. */ |
| 1051 | void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref) |
| 1052 | { |
| 1053 | struct htx_blk *cblk, *pblk; |
| 1054 | |
| 1055 | cblk = *blk; |
| 1056 | for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) { |
| 1057 | /* Swap .addr and .info fields */ |
| 1058 | cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr; |
| 1059 | cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info; |
| 1060 | |
| 1061 | if (cblk->addr == pblk->addr) |
| 1062 | cblk->addr += htx_get_blksz(pblk); |
| 1063 | if (pblk == *ref) |
| 1064 | break; |
| 1065 | cblk = pblk; |
| 1066 | } |
| 1067 | *blk = cblk; |
| 1068 | *ref = pblk; |
| 1069 | } |
| 1070 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1071 | /* Appends the H1 representation of the request line block <blk> to the |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1072 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 1073 | * returns 0. |
| 1074 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1075 | 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] | 1076 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1077 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1078 | return 0; |
| 1079 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1080 | 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] | 1081 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1082 | 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] | 1083 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 1084 | if (sl->flags & HTX_SL_F_VER_11) |
| 1085 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 1086 | else |
| 1087 | chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 1088 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1089 | chunk_memcat(chk, "\r\n", 2); |
| 1090 | |
| 1091 | return 1; |
| 1092 | } |
| 1093 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1094 | /* 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] | 1095 | * <chk>. It returns 1 if data are successfully appended, otherwise it |
| 1096 | * returns 0. |
| 1097 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1098 | 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] | 1099 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1100 | if (HTX_SL_LEN(sl) + 4 > b_size(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1101 | return 0; |
| 1102 | |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 1103 | if (sl->flags & HTX_SL_F_VER_11) |
| 1104 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 1105 | else |
| 1106 | 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] | 1107 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1108 | 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] | 1109 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1110 | 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] | 1111 | chunk_memcat(chk, "\r\n", 2); |
| 1112 | |
| 1113 | return 1; |
| 1114 | } |
| 1115 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1116 | /* Appends the H1 representation of the header block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1117 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 1118 | * 0. |
| 1119 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1120 | 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] | 1121 | { |
| 1122 | if (n.len + v.len + 4 > b_room(chk)) |
| 1123 | return 0; |
| 1124 | |
| 1125 | chunk_memcat(chk, n.ptr, n.len); |
| 1126 | chunk_memcat(chk, ": ", 2); |
| 1127 | chunk_memcat(chk, v.ptr, v.len); |
| 1128 | chunk_memcat(chk, "\r\n", 2); |
| 1129 | |
| 1130 | return 1; |
| 1131 | } |
| 1132 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1133 | /* Appends the H1 representation of the data block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1134 | * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It |
| 1135 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 1136 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1137 | 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] | 1138 | { |
| 1139 | if (chunked) { |
| 1140 | uint32_t chksz; |
| 1141 | char tmp[10]; |
| 1142 | char *beg, *end; |
| 1143 | |
| 1144 | chksz = data.len; |
| 1145 | |
| 1146 | beg = end = tmp+10; |
| 1147 | *--beg = '\n'; |
| 1148 | *--beg = '\r'; |
| 1149 | do { |
| 1150 | *--beg = hextab[chksz & 0xF]; |
| 1151 | } while (chksz >>= 4); |
| 1152 | |
| 1153 | if (data.len + (end - beg) + 2 > b_room(chk)) |
| 1154 | return 0; |
| 1155 | chunk_memcat(chk, beg, end - beg); |
| 1156 | chunk_memcat(chk, data.ptr, data.len); |
| 1157 | chunk_memcat(chk, "\r\n", 2); |
| 1158 | } |
| 1159 | else { |
| 1160 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1164 | return 1; |
| 1165 | } |