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 | |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 13 | #include <haproxy/chunk.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 14 | #include <haproxy/htx.h> |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 15 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 16 | struct htx htx_empty = { .size = 0, .data = 0, .head = -1, .tail = -1, .first = -1 }; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 17 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 18 | /* Defragments an HTX message. It removes unused blocks and unwraps the payloads |
| 19 | * part. A temporary buffer is used to do so. This function never fails. if |
| 20 | * <blk> is not NULL, we replace it by the new block address, after the |
| 21 | * defragmentation. The new <blk> is returned. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 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 | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 33 | if (htx->head == -1) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 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 | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 57 | /* if <blk> is defined, save its new position */ |
Christopher Faulet | 200f895 | 2019-01-02 11:23:44 +0100 | [diff] [blame] | 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 | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 67 | htx->first = first; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 68 | htx->head = 0; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 69 | htx->tail = new - 1; |
| 70 | htx->head_addr = htx->end_addr = 0; |
| 71 | htx->tail_addr = addr; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 72 | memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 73 | |
Christopher Faulet | 200f895 | 2019-01-02 11:23:44 +0100 | [diff] [blame] | 74 | return ((blkpos == -1) ? NULL : htx_get_blk(htx, blkpos)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 77 | /* Degragments HTX blocks of an HTX message. Payloads part is keep untouched |
| 78 | * here. This function will move back all blocks starting at the position 0, |
| 79 | * removing unused blocks. It must never be called with an empty message. |
| 80 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 81 | static void htx_defrag_blks(struct htx *htx) |
| 82 | { |
| 83 | int32_t pos, new; |
| 84 | |
| 85 | new = 0; |
| 86 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 87 | struct htx_blk *posblk, *newblk; |
| 88 | |
| 89 | if (pos == new) { |
| 90 | new++; |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | posblk = htx_get_blk(htx, pos); |
| 95 | if (htx_get_blk_type(posblk) == HTX_BLK_UNUSED) |
| 96 | continue; |
| 97 | |
| 98 | if (htx->first == pos) |
| 99 | htx->first = new; |
| 100 | newblk = htx_get_blk(htx, new++); |
| 101 | newblk->info = posblk->info; |
| 102 | newblk->addr = posblk->addr; |
| 103 | } |
| 104 | BUG_ON(!new); |
| 105 | htx->head = 0; |
| 106 | htx->tail = new - 1; |
| 107 | } |
| 108 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 109 | /* Reserves a new block in the HTX message <htx> with a content of <blksz> |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 110 | * bytes. If there is not enough space, NULL is returned. Otherwise the reserved |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 111 | * block is returned and the HTX message is updated. Space for this new block is |
| 112 | * reserved in the HTX message. But it is the caller responsibility to set right |
| 113 | * info in the block to reflect the stored data. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 114 | */ |
| 115 | static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz) |
| 116 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 117 | struct htx_blk *blk; |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 118 | uint32_t tail, headroom, tailroom; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 119 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 120 | if (blksz > htx_free_data_space(htx)) |
| 121 | return NULL; /* full */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 122 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 123 | if (htx->head == -1) { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 124 | /* Empty message */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 125 | htx->head = htx->tail = htx->first = 0; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 126 | blk = htx_get_blk(htx, htx->tail); |
| 127 | blk->addr = 0; |
| 128 | htx->data = blksz; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 129 | htx->tail_addr = blksz; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 130 | return blk; |
| 131 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 132 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 133 | /* Find the block's position. First, we try to get the next position in |
| 134 | * the message, increasing the tail by one. If this position is not |
| 135 | * available with some holes, we try to defrag the blocks without |
| 136 | * touching their paylood. If it is impossible, we fully defrag the |
| 137 | * message. |
| 138 | */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 139 | tail = htx->tail + 1; |
Christopher Faulet | 2bf43f0 | 2019-06-12 11:28:11 +0200 | [diff] [blame] | 140 | if (htx_pos_to_addr(htx, tail) >= htx->tail_addr) |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 141 | ; |
| 142 | else if (htx->head > 0) { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 143 | htx_defrag_blks(htx); |
| 144 | tail = htx->tail + 1; |
Christopher Faulet | 2bf43f0 | 2019-06-12 11:28:11 +0200 | [diff] [blame] | 145 | BUG_ON(htx_pos_to_addr(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 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 150 | /* Now, we have found the block's position. Try to find where to put its |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 151 | * payload. The free space is split in two areas: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 152 | * |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 153 | * * The free space in front of the blocks table. This one is used if and |
| 154 | * only if the other one was not used yet. |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 155 | * |
| 156 | * * The free space at the beginning of the message. Once this one is |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 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); |
Christopher Faulet | 2bf43f0 | 2019-06-12 11:28:11 +0200 | [diff] [blame] | 160 | tailroom = (!htx->head_addr ? htx_pos_to_addr(htx, tail) - htx->tail_addr : 0); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 161 | BUG_ON((int32_t)headroom < 0); |
| 162 | BUG_ON((int32_t)tailroom < 0); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 163 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 164 | if (blksz <= tailroom) { |
| 165 | blk = htx_get_blk(htx, tail); |
| 166 | blk->addr = htx->tail_addr; |
| 167 | htx->tail_addr += blksz; |
| 168 | } |
| 169 | else if (blksz <= headroom) { |
| 170 | blk = htx_get_blk(htx, tail); |
| 171 | blk->addr = htx->head_addr; |
| 172 | htx->head_addr += blksz; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 173 | } |
| 174 | else { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 175 | defrag: |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 176 | /* need to defragment the message before inserting upfront */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 177 | htx_defrag(htx, NULL); |
| 178 | tail = htx->tail + 1; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 179 | blk = htx_get_blk(htx, tail); |
| 180 | blk->addr = htx->tail_addr; |
| 181 | htx->tail_addr += blksz; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 182 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 183 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 184 | htx->tail = tail; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 185 | htx->data += blksz; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 186 | /* Set first position if not already set */ |
| 187 | if (htx->first == -1) |
| 188 | htx->first = tail; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 189 | |
| 190 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 191 | BUG_ON((int32_t)htx->head_addr < 0); |
| 192 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 193 | BUG_ON(htx->head_addr > htx->end_addr); |
| 194 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 195 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 198 | /* Prepares the block to an expansion of its payload. The payload will be |
| 199 | * expanded by <delta> bytes and we need find where this expansion will be |
| 200 | * performed. It can be a compression if <delta> is negative. This function only |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 201 | * updates all addresses. The caller have the responsibility to perform the |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 202 | * expansion and update the block and the HTX message accordingly. No error must |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 203 | * occur. It returns following values: |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 204 | * |
| 205 | * 0: The expansion cannot be performed, there is not enough space. |
| 206 | * |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 207 | * 1: the expansion must be performed in place, there is enough space after |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 208 | * the block's payload to handle it. This is especially true if it is a |
| 209 | * compression and not an expension. |
| 210 | * |
| 211 | * 2: the block's payload must be moved at the new block address before doing |
| 212 | * the expansion. |
| 213 | * |
| 214 | * 3: the HTX message message must be defragmented |
| 215 | */ |
| 216 | static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32_t delta) |
| 217 | { |
| 218 | uint32_t sz, tailroom, headroom; |
| 219 | int ret = 3; |
| 220 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 221 | BUG_ON(htx->head == -1); |
| 222 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 223 | headroom = (htx->end_addr - htx->head_addr); |
Christopher Faulet | 2bf43f0 | 2019-06-12 11:28:11 +0200 | [diff] [blame] | 224 | tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 225 | BUG_ON((int32_t)headroom < 0); |
| 226 | BUG_ON((int32_t)tailroom < 0); |
| 227 | |
| 228 | sz = htx_get_blksz(blk); |
| 229 | if (delta <= 0) { |
| 230 | /* It is a compression, it can be performed in place */ |
| 231 | if (blk->addr+sz == htx->tail_addr) |
| 232 | htx->tail_addr += delta; |
| 233 | else if (blk->addr+sz == htx->head_addr) |
| 234 | htx->head_addr += delta; |
| 235 | ret = 1; |
| 236 | } |
| 237 | else if (delta > htx_free_space(htx)) { |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 238 | /* There is not enough space to handle the expansion */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 239 | ret = 0; |
| 240 | } |
| 241 | else if (blk->addr+sz == htx->tail_addr) { |
| 242 | /* The block's payload is just before the tail room */ |
| 243 | if (delta < tailroom) { |
| 244 | /* Expand the block's payload */ |
| 245 | htx->tail_addr += delta; |
| 246 | ret = 1; |
| 247 | } |
| 248 | else if ((sz + delta) < headroom) { |
Christopher Faulet | 61ed779 | 2019-07-29 10:50:28 +0200 | [diff] [blame] | 249 | uint32_t oldaddr = blk->addr; |
| 250 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 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 | 61ed779 | 2019-07-29 10:50:28 +0200 | [diff] [blame] | 255 | if (oldaddr == htx->end_addr) { |
Christopher Faulet | 8c65486 | 2019-06-12 11:08:11 +0200 | [diff] [blame] | 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 | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 302 | /* Adds a new block of type <type> in the HTX message <htx>. Its content size is |
| 303 | * passed but it is the caller responsibility to do the copy. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 318 | /* Removes the block <blk> from the HTX message <htx>. The function returns the |
| 319 | * block following <blk> or NULL if <blk> is the last block or the last inserted |
| 320 | * one. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 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 | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 327 | BUG_ON(htx->head == -1); |
| 328 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 329 | /* This is the last block in use */ |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 330 | if (htx->head == htx->tail) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 331 | uint32_t flags = htx->flags; /* Preserve flags */ |
| 332 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 333 | htx_reset(htx); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 334 | htx->flags |= flags; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | type = htx_get_blk_type(blk); |
Christopher Faulet | 9c66b98 | 2019-04-30 18:08:26 +0200 | [diff] [blame] | 339 | pos = htx_get_blk_pos(htx, blk); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 340 | sz = htx_get_blksz(blk); |
| 341 | addr = blk->addr; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 342 | if (type != HTX_BLK_UNUSED) { |
| 343 | /* Mark the block as unused, decrement allocated size */ |
| 344 | htx->data -= htx_get_blksz(blk); |
| 345 | blk->info = ((uint32_t)HTX_BLK_UNUSED << 28); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 346 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 347 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 348 | /* There is at least 2 blocks, so tail is always > 0 */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 349 | if (pos == htx->head) { |
| 350 | /* move the head forward */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 351 | htx->head++; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 352 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 353 | else if (pos == htx->tail) { |
| 354 | /* remove the tail. this was the last inserted block so |
| 355 | * return NULL. */ |
| 356 | htx->tail--; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 357 | blk = NULL; |
| 358 | goto end; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 359 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 360 | blk = htx_get_blk(htx, pos+1); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 361 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 362 | end: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 363 | if (pos == htx->first) |
| 364 | htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 365 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 366 | if (htx->head == htx->tail) { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 367 | /* If there is just one block in the HTX message, free space can |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 368 | * be adjusted. This operation could save some defrags. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 369 | struct htx_blk *lastblk = htx_get_blk(htx, htx->tail); |
| 370 | |
| 371 | htx->head_addr = 0; |
| 372 | htx->end_addr = lastblk->addr; |
| 373 | htx->tail_addr = lastblk->addr+htx->data; |
| 374 | } |
| 375 | else { |
| 376 | if (addr+sz == htx->tail_addr) |
| 377 | htx->tail_addr = addr; |
| 378 | else if (addr+sz == htx->head_addr) |
| 379 | htx->head_addr = addr; |
Christopher Faulet | 8c65486 | 2019-06-12 11:08:11 +0200 | [diff] [blame] | 380 | if (addr == htx->end_addr) { |
| 381 | if (htx->tail_addr == htx->end_addr) { |
| 382 | htx->tail_addr = htx->head_addr; |
| 383 | htx->head_addr = htx->end_addr = 0; |
| 384 | } |
| 385 | else |
| 386 | htx->end_addr += sz; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 391 | BUG_ON((int32_t)htx->head_addr < 0); |
| 392 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 393 | BUG_ON(htx->head_addr > htx->end_addr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 394 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Christopher Faulet | 1cdceb9 | 2020-02-24 11:41:59 +0100 | [diff] [blame] | 397 | /* Looks for the HTX block containing the offset <offset>, starting at the HTX |
| 398 | * message's head. The function returns an htx_ret with the found HTX block and |
| 399 | * the position inside this block where the offset is. If the offset <offset> is |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 400 | * outside of the HTX message, htx_ret.blk is set to NULL. |
Christopher Faulet | 1cdceb9 | 2020-02-24 11:41:59 +0100 | [diff] [blame] | 401 | */ |
| 402 | struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset) |
| 403 | { |
| 404 | struct htx_blk *blk; |
| 405 | struct htx_ret htxret = { .blk = NULL, .ret = 0 }; |
| 406 | |
| 407 | if (offset >= htx->data) |
| 408 | return htxret; |
| 409 | |
| 410 | for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) { |
| 411 | uint32_t sz = htx_get_blksz(blk); |
| 412 | |
| 413 | if (offset < sz) |
| 414 | break; |
| 415 | offset -= sz; |
| 416 | } |
| 417 | htxret.blk = blk; |
| 418 | htxret.ret = offset; |
| 419 | return htxret; |
| 420 | } |
| 421 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 422 | /* Removes all blocks after the one containing the offset <offset>. This last |
| 423 | * one may be truncated if it is a DATA block. |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 424 | */ |
| 425 | void htx_truncate(struct htx *htx, uint32_t offset) |
| 426 | { |
| 427 | struct htx_blk *blk; |
Christopher Faulet | bb76aa4 | 2020-02-24 15:09:24 +0100 | [diff] [blame] | 428 | struct htx_ret htxret = htx_find_offset(htx, offset); |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 429 | |
Christopher Faulet | bb76aa4 | 2020-02-24 15:09:24 +0100 | [diff] [blame] | 430 | blk = htxret.blk; |
| 431 | if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) { |
| 432 | htx_change_blk_value_len(htx, blk, htxret.ret); |
| 433 | blk = htx_get_next_blk(htx, blk); |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 434 | } |
| 435 | while (blk) |
| 436 | blk = htx_remove_blk(htx, blk); |
| 437 | } |
| 438 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 439 | /* Drains <count> bytes from the HTX message <htx>. If the last block is a DATA |
| 440 | * block, it will be cut if necessary. Others blocks will be removed at once if |
| 441 | * <count> is large enough. The function returns an htx_ret with the first block |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 442 | * remaining in the message and the amount of data drained. If everything is |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 443 | * removed, htx_ret.blk is set to NULL. |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 444 | */ |
| 445 | struct htx_ret htx_drain(struct htx *htx, uint32_t count) |
| 446 | { |
| 447 | struct htx_blk *blk; |
| 448 | struct htx_ret htxret = { .blk = NULL, .ret = 0 }; |
| 449 | |
Christopher Faulet | 0f6d6a9 | 2019-05-23 11:11:52 +0200 | [diff] [blame] | 450 | if (count == htx->data) { |
| 451 | htx_reset(htx); |
| 452 | htxret.ret = count; |
| 453 | return htxret; |
| 454 | } |
| 455 | |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 456 | blk = htx_get_head_blk(htx); |
| 457 | while (count && blk) { |
| 458 | uint32_t sz = htx_get_blksz(blk); |
| 459 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 460 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 461 | /* Ignore unused block */ |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 462 | if (type == HTX_BLK_UNUSED) |
| 463 | goto next; |
| 464 | |
| 465 | if (sz > count) { |
| 466 | if (type == HTX_BLK_DATA) { |
| 467 | htx_cut_data_blk(htx, blk, count); |
| 468 | htxret.ret += count; |
| 469 | } |
| 470 | break; |
| 471 | } |
| 472 | count -= sz; |
| 473 | htxret.ret += sz; |
| 474 | next: |
| 475 | blk = htx_remove_blk(htx, blk); |
| 476 | } |
| 477 | htxret.blk = blk; |
| 478 | |
| 479 | return htxret; |
| 480 | } |
| 481 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 482 | /* 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] | 483 | * there is enough space to take it all. If the space wraps, the buffer is |
| 484 | * 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] | 485 | * returned. Otherwise, on success, the updated block (or the new one) is |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 486 | * returned. Due to its nature this function can be expensive and should be |
| 487 | * avoided whenever possible. |
| 488 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 489 | 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] | 490 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 491 | struct htx_blk *blk, *tailblk; |
| 492 | void *ptr; |
| 493 | uint32_t len, sz, tailroom, headroom; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 494 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 495 | if (htx->head == -1) |
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 | /* Not enough space to store data */ |
| 499 | if (data.len > htx_free_data_space(htx)) |
| 500 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 501 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 502 | /* get the tail block and its size */ |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 503 | tailblk = htx_get_tail_blk(htx); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 504 | if (tailblk == NULL) |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 505 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 506 | sz = htx_get_blksz(tailblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 507 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 508 | /* Don't try to append data if the last inserted block is not of the |
| 509 | * same type */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 510 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 511 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 512 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 513 | /* |
| 514 | * Same type and enough space: append data |
| 515 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 516 | headroom = (htx->end_addr - htx->head_addr); |
Christopher Faulet | 2bf43f0 | 2019-06-12 11:28:11 +0200 | [diff] [blame] | 517 | tailroom = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 518 | BUG_ON((int32_t)headroom < 0); |
| 519 | BUG_ON((int32_t)tailroom < 0); |
| 520 | |
| 521 | len = data.len; |
| 522 | if (tailblk->addr+sz == htx->tail_addr) { |
| 523 | if (data.len <= tailroom) |
| 524 | goto append_data; |
| 525 | else if (!htx->head_addr) { |
| 526 | len = tailroom; |
| 527 | goto append_data; |
| 528 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 529 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 530 | else if (tailblk->addr+sz == htx->head_addr && data.len <= headroom) |
| 531 | goto append_data; |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 533 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 534 | |
| 535 | append_data: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 536 | /* FIXME: check v.len + data.len < 256MB */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 537 | /* Append data and update the block itself */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 538 | ptr = htx_get_blk_ptr(htx, tailblk); |
| 539 | memcpy(ptr+sz, data.ptr, len); |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 540 | htx_change_blk_value_len(htx, tailblk, sz+len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 541 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 542 | if (data.len == len) { |
| 543 | blk = tailblk; |
| 544 | goto end; |
| 545 | } |
Tim Duesterhus | 154374c | 2021-03-02 18:57:27 +0100 | [diff] [blame] | 546 | data = istadv(data, len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 547 | |
| 548 | add_new_block: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 549 | /* FIXME: check data.len (< 256MB) */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 550 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 551 | if (!blk) |
| 552 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 553 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 554 | blk->info += data.len; |
| 555 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 556 | |
| 557 | end: |
| 558 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 559 | BUG_ON((int32_t)htx->head_addr < 0); |
| 560 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 561 | BUG_ON(htx->head_addr > htx->end_addr); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 562 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* Replaces a value part of a block by a new one. The new part can be smaller or |
| 566 | * larger than the old one. This function works for any kind of block with |
| 567 | * attached data. It returns the new block on success, otherwise it returns |
| 568 | * NULL. |
| 569 | */ |
| 570 | 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] | 571 | const struct ist old, const struct ist new) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 572 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 573 | struct ist n, v; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 574 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 575 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 576 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 577 | n = htx_get_blk_name(htx, blk); |
| 578 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 579 | delta = new.len - old.len; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 580 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 581 | if (!ret) |
| 582 | return NULL; /* not enough space */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 583 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 584 | /* Before updating the payload, set the new block size and update HTX |
| 585 | * message */ |
| 586 | htx_set_blk_value_len(blk, v.len + delta); |
| 587 | htx->data += delta; |
| 588 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 589 | if (ret == 1) { /* Replace in place */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 590 | if (delta <= 0) { |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 591 | /* compression: copy new data first then move the end */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 592 | memcpy(old.ptr, new.ptr, new.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 593 | 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] | 594 | } |
| 595 | else { |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 596 | /* expansion: move the end first then copy new data */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 597 | memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 598 | memcpy(old.ptr, new.ptr, new.len); |
| 599 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 600 | } |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 601 | else if (ret == 2) { /* New address but no defrag */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 602 | void *ptr = htx_get_blk_ptr(htx, blk); |
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 the name, if any */ |
| 605 | memcpy(ptr, n.ptr, n.len); |
| 606 | ptr += n.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 607 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 608 | /* Copy value before old part, if any */ |
| 609 | memcpy(ptr, v.ptr, old.ptr - v.ptr); |
| 610 | ptr += old.ptr - v.ptr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 611 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 612 | /* Copy new value */ |
| 613 | memcpy(ptr, new.ptr, new.len); |
| 614 | ptr += new.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 615 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 616 | /* Copy value after old part, if any */ |
| 617 | memcpy(ptr, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 618 | } |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 619 | else { /* Do a degrag first */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 620 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 621 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 622 | /* Copy the header name, if any */ |
| 623 | chunk_memcat(tmp, n.ptr, n.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 624 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 625 | /* Copy value before old part, if any */ |
| 626 | chunk_memcat(tmp, v.ptr, old.ptr - v.ptr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 627 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 628 | /* Copy new value */ |
| 629 | chunk_memcat(tmp, new.ptr, new.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 630 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 631 | /* Copy value after old part if any */ |
| 632 | 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] | 633 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 634 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 635 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 636 | /* Finally, copy data. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 637 | memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data); |
| 638 | } |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 639 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 643 | * type <mark> (typically EOH or EOT) or when <count> bytes were moved |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 644 | * (including payload and meta-data). It returns the number of bytes moved and |
| 645 | * the last HTX block inserted in <dst>. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 646 | */ |
| 647 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 648 | enum htx_blk_type mark) |
| 649 | { |
| 650 | struct htx_blk *blk, *dstblk; |
| 651 | enum htx_blk_type type; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 652 | uint32_t info, max, sz, ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 653 | int inside_trailers = 0; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 654 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 655 | ret = htx_used_space(dst); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 656 | blk = htx_get_blk(src, htx_get_head(src)); |
| 657 | dstblk = NULL; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 658 | |
| 659 | while (blk && count) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 660 | type = htx_get_blk_type(blk); |
| 661 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 662 | /* Ignore unused block */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 663 | if (type == HTX_BLK_UNUSED) |
| 664 | goto next; |
| 665 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 666 | /* Be sure to have enough space to xfer all headers/trailers in |
| 667 | * one time. If not while <dst> is empty, we report a parsing |
| 668 | * error on <src>. |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 669 | */ |
| 670 | if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) { |
| 671 | struct htx_sl *sl = htx_get_blk_ptr(src, blk); |
| 672 | |
| 673 | if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) { |
| 674 | if (htx_is_empty(dst)) |
| 675 | src->flags |= HTX_FL_PARSING_ERROR; |
| 676 | break; |
| 677 | } |
| 678 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 679 | else if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && |
| 680 | !inside_trailers && mark >= HTX_BLK_EOT) { |
| 681 | inside_trailers = 1; |
| 682 | if (htx_used_space(src) > count) { |
| 683 | if (htx_is_empty(dst)) |
| 684 | src->flags |= HTX_FL_PARSING_ERROR; |
| 685 | break; |
| 686 | } |
| 687 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 688 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 689 | sz = htx_get_blksz(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 690 | info = blk->info; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 691 | max = htx_get_max_blksz(dst, count); |
| 692 | if (!max) |
| 693 | break; |
Willy Tarreau | 90caa07 | 2019-04-09 16:21:54 +0200 | [diff] [blame] | 694 | if (sz > max) { |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 695 | /* Only DATA blocks can be partially xferred */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 696 | if (type != HTX_BLK_DATA) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 697 | break; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 698 | sz = max; |
| 699 | info = (type << 28) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | dstblk = htx_reserve_nxblk(dst, sz); |
| 703 | if (!dstblk) |
| 704 | break; |
| 705 | dstblk->info = info; |
| 706 | memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz); |
| 707 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 708 | count -= sizeof(dstblk) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 709 | if (blk->info != info) { |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 710 | /* Partial xfer: don't remove <blk> from <src> but |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 711 | * resize its content */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 712 | htx_cut_data_blk(src, blk, sz); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 713 | break; |
| 714 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 715 | next: |
| 716 | blk = htx_remove_blk(src, blk); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 717 | if (type != HTX_BLK_UNUSED && type == mark) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 718 | break; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 721 | end: |
| 722 | ret = htx_used_space(dst) - ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 723 | return (struct htx_ret){.ret = ret, .blk = dstblk}; |
| 724 | } |
| 725 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 726 | /* Replaces an header by a new one. The new header can be smaller or larger than |
| 727 | * 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] | 728 | * The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 729 | */ |
| 730 | 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] | 731 | const struct ist name, const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 732 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 733 | enum htx_blk_type type; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 734 | void *ptr; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 735 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 736 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 737 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 738 | type = htx_get_blk_type(blk); |
| 739 | if (type != HTX_BLK_HDR) |
| 740 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 741 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 742 | delta = name.len + value.len - htx_get_blksz(blk); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 743 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 744 | if (!ret) |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 745 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 746 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 747 | /* Set the new block size and update HTX message */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 748 | blk->info = (type << 28) + (value.len << 8) + name.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 749 | htx->data += delta; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 750 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 751 | /* Replace in place or at a new address is the same. We replace all the |
| 752 | * header (name+value). Only take care to defrag the message if |
| 753 | * necessary. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 754 | if (ret == 3) |
| 755 | blk = htx_defrag(htx, blk); |
| 756 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 757 | /* Finally, copy data. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 758 | ptr = htx_get_blk_ptr(htx, blk); |
| 759 | ist2bin_lc(ptr, name); |
| 760 | memcpy(ptr + name.len, value.ptr, value.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 761 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 764 | /* Replaces the parts of the start-line. It returns the new start-line on |
| 765 | * success, otherwise it returns NULL. It is the caller responsibility to update |
| 766 | * sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 767 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 768 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 769 | const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 770 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 771 | enum htx_blk_type type; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 772 | struct htx_sl *sl; |
| 773 | struct htx_sl tmp; /* used to save sl->info and sl->flags */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 774 | uint32_t sz; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 775 | int32_t delta; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 776 | int ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 777 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 778 | type = htx_get_blk_type(blk); |
Willy Tarreau | c706cd7 | 2018-12-07 17:12:22 +0100 | [diff] [blame] | 779 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 780 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 781 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 782 | /* Save start-line info and flags */ |
| 783 | sl = htx_get_blk_ptr(htx, blk); |
| 784 | tmp.info = sl->info; |
| 785 | tmp.flags = sl->flags; |
Christopher Faulet | 2bce046 | 2019-06-25 21:31:26 +0200 | [diff] [blame] | 786 | tmp.hdrs_bytes = sl->hdrs_bytes; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 787 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 788 | sz = htx_get_blksz(blk); |
| 789 | delta = sizeof(*sl) + p1.len + p2.len + p3.len - sz; |
| 790 | ret = htx_prepare_blk_expansion(htx, blk, delta); |
| 791 | if (!ret) |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 792 | return NULL; /* not enough space */ |
| 793 | |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 794 | /* Set the new block size and update HTX message */ |
| 795 | htx_set_blk_value_len(blk, sz+delta); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 796 | htx->data += delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 797 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 798 | /* Replace in place or at a new address is the same. We replace all the |
| 799 | * start-line. Only take care to defrag the message if necessary. */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 800 | if (ret == 3) |
| 801 | blk = htx_defrag(htx, blk); |
| 802 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 803 | /* 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] | 804 | sl = htx_get_blk_ptr(htx, blk); |
| 805 | sl->info = tmp.info; |
| 806 | sl->flags = tmp.flags; |
Christopher Faulet | 2bce046 | 2019-06-25 21:31:26 +0200 | [diff] [blame] | 807 | sl->hdrs_bytes = tmp.hdrs_bytes; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 808 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 809 | HTX_SL_P1_LEN(sl) = p1.len; |
| 810 | HTX_SL_P2_LEN(sl) = p2.len; |
| 811 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 812 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 813 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 814 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 815 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 816 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 817 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 820 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 821 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 822 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 823 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 824 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 825 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 826 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 827 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 828 | uint32_t size; |
| 829 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 830 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 831 | return NULL; |
| 832 | |
| 833 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 834 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 835 | /* FIXME: check size (< 256MB) */ |
| 836 | blk = htx_add_blk(htx, type, size); |
| 837 | if (!blk) |
| 838 | return NULL; |
| 839 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 840 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 841 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 842 | sl->hdrs_bytes = -1; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 843 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 844 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 845 | HTX_SL_P1_LEN(sl) = p1.len; |
| 846 | HTX_SL_P2_LEN(sl) = p2.len; |
| 847 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 848 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 849 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 850 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 851 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 852 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 853 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* 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] | 857 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 858 | */ |
| 859 | 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] | 860 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 861 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 862 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 863 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 864 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 865 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 866 | if (!blk) |
| 867 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 868 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 869 | blk->info += (value.len << 8) + name.len; |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 870 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 871 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 872 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 875 | /* Adds an HTX block of type TLR in <htx>. It returns the new block on |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 876 | * success. Otherwise, it returns NULL. The trailer name is always lower cased. |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 877 | */ |
| 878 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist name, |
| 879 | const struct ist value) |
| 880 | { |
| 881 | struct htx_blk *blk; |
| 882 | |
| 883 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 884 | blk = htx_add_blk(htx, HTX_BLK_TLR, name.len + value.len); |
| 885 | if (!blk) |
| 886 | return NULL; |
| 887 | |
| 888 | blk->info += (value.len << 8) + name.len; |
| 889 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
| 890 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 891 | return blk; |
| 892 | } |
| 893 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 894 | /* Add all headers from the list <hdrs> into the HTX message <htx>, followed by |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 895 | * the EOH. On success, it returns the last block inserted (the EOH), otherwise |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 896 | * NULL is returned. */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 897 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 898 | { |
| 899 | int i; |
| 900 | |
| 901 | for (i = 0; hdrs[i].n.len; i++) { |
| 902 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 903 | return NULL; |
| 904 | } |
| 905 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 906 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 907 | |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 908 | /* Add all trailers from the list <hdrs> into the HTX message <htx>, followed by |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 909 | * the EOT. On success, it returns the last block inserted (the EOT), otherwise |
Christopher Faulet | 3b21972 | 2019-06-19 13:48:09 +0200 | [diff] [blame] | 910 | * NULL is returned. */ |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 911 | struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct http_hdr *hdrs) |
| 912 | { |
| 913 | int i; |
| 914 | |
| 915 | for (i = 0; hdrs[i].n.len; i++) { |
| 916 | if (!htx_add_trailer(htx, hdrs[i].n, hdrs[i].v)) |
| 917 | return NULL; |
| 918 | } |
| 919 | return htx_add_endof(htx, HTX_BLK_EOT); |
| 920 | } |
| 921 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 922 | /* Adds an HTX block of type EOH or EOT in <htx>. It returns the new block on |
| 923 | * success. Otherwise, it returns NULL. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 924 | */ |
| 925 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 926 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 927 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 928 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 929 | blk = htx_add_blk(htx, type, 1); |
| 930 | if (!blk) |
| 931 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 932 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 933 | blk->info += 1; |
| 934 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Christopher Faulet | e071f0e | 2021-02-03 12:11:31 +0100 | [diff] [blame] | 937 | /* Reserves the maximum possible size for an HTX data block, by extending an |
| 938 | * existing one or by creating a now one. It returns a compound result with the |
| 939 | * HTX block and the position where new data must be inserted (0 for a new |
| 940 | * block). If an error occurs or if there is no space left, NULL is returned |
| 941 | * instead of a pointer on an HTX block. |
| 942 | */ |
| 943 | struct htx_ret htx_reserve_max_data(struct htx *htx) |
| 944 | { |
| 945 | struct htx_blk *blk, *tailblk; |
| 946 | uint32_t sz, room; |
| 947 | int32_t len = htx_free_data_space(htx); |
| 948 | |
| 949 | if (htx->head == -1) |
| 950 | goto rsv_new_block; |
| 951 | |
| 952 | if (!len) |
| 953 | return (struct htx_ret){.ret = 0, .blk = NULL}; |
| 954 | |
| 955 | /* get the tail and head block */ |
| 956 | tailblk = htx_get_tail_blk(htx); |
| 957 | if (tailblk == NULL) |
| 958 | goto rsv_new_block; |
| 959 | sz = htx_get_blksz(tailblk); |
| 960 | |
| 961 | /* Don't try to append data if the last inserted block is not of the |
| 962 | * same type */ |
| 963 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
| 964 | goto rsv_new_block; |
| 965 | |
| 966 | /* |
| 967 | * Same type and enough space: append data |
| 968 | */ |
| 969 | if (!htx->head_addr) { |
| 970 | if (tailblk->addr+sz != htx->tail_addr) |
| 971 | goto rsv_new_block; |
| 972 | room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr); |
| 973 | } |
| 974 | else { |
| 975 | if (tailblk->addr+sz != htx->head_addr) |
| 976 | goto rsv_new_block; |
| 977 | room = (htx->end_addr - htx->head_addr); |
| 978 | } |
| 979 | BUG_ON((int32_t)room < 0); |
| 980 | if (room < len) |
| 981 | len = room; |
| 982 | |
| 983 | append_data: |
| 984 | htx_change_blk_value_len(htx, tailblk, sz+len); |
| 985 | |
| 986 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 987 | BUG_ON((int32_t)htx->head_addr < 0); |
| 988 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 989 | BUG_ON(htx->head_addr > htx->end_addr); |
| 990 | return (struct htx_ret){.ret = sz, .blk = tailblk}; |
| 991 | |
| 992 | rsv_new_block: |
| 993 | /* FIXME: check data.len (< 256MB) */ |
| 994 | blk = htx_add_blk(htx, HTX_BLK_DATA, len); |
| 995 | if (!blk) |
| 996 | return (struct htx_ret){.ret = 0, .blk = NULL}; |
| 997 | blk->info += len; |
| 998 | return (struct htx_ret){.ret = 0, .blk = blk}; |
| 999 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1000 | |
| 1001 | /* 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] | 1002 | * possible. It returns the number of bytes consumed from <data>, which may be |
| 1003 | * zero if nothing could be copied. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1004 | */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1005 | size_t htx_add_data(struct htx *htx, const struct ist data) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1006 | { |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1007 | struct htx_blk *blk, *tailblk; |
| 1008 | void *ptr; |
| 1009 | uint32_t sz, room; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1010 | int32_t len = data.len; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1011 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 1012 | if (htx->head == -1) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1013 | goto add_new_block; |
| 1014 | |
| 1015 | /* Not enough space to store data */ |
| 1016 | if (len > htx_free_data_space(htx)) |
| 1017 | len = htx_free_data_space(htx); |
| 1018 | |
| 1019 | if (!len) |
| 1020 | return 0; |
| 1021 | |
| 1022 | /* get the tail and head block */ |
| 1023 | tailblk = htx_get_tail_blk(htx); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1024 | if (tailblk == NULL) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1025 | goto add_new_block; |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1026 | sz = htx_get_blksz(tailblk); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1027 | |
| 1028 | /* Don't try to append data if the last inserted block is not of the |
| 1029 | * same type */ |
| 1030 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
| 1031 | goto add_new_block; |
| 1032 | |
| 1033 | /* |
| 1034 | * Same type and enough space: append data |
| 1035 | */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1036 | if (!htx->head_addr) { |
| 1037 | if (tailblk->addr+sz != htx->tail_addr) |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1038 | goto add_new_block; |
Christopher Faulet | 2bf43f0 | 2019-06-12 11:28:11 +0200 | [diff] [blame] | 1039 | room = (htx_pos_to_addr(htx, htx->tail) - htx->tail_addr); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1040 | } |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1041 | else { |
| 1042 | if (tailblk->addr+sz != htx->head_addr) |
| 1043 | goto add_new_block; |
| 1044 | room = (htx->end_addr - htx->head_addr); |
| 1045 | } |
| 1046 | BUG_ON((int32_t)room < 0); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1047 | if (room < len) |
| 1048 | len = room; |
| 1049 | |
| 1050 | append_data: |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1051 | /* FIXME: check v.len + data.len < 256MB */ |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1052 | /* Append data and update the block itself */ |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1053 | ptr = htx_get_blk_ptr(htx, tailblk); |
| 1054 | memcpy(ptr + sz, data.ptr, len); |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 1055 | htx_change_blk_value_len(htx, tailblk, sz+len); |
Christopher Faulet | d7884d3 | 2019-06-11 10:40:43 +0200 | [diff] [blame] | 1056 | |
| 1057 | BUG_ON((int32_t)htx->tail_addr < 0); |
| 1058 | BUG_ON((int32_t)htx->head_addr < 0); |
| 1059 | BUG_ON(htx->end_addr > htx->tail_addr); |
| 1060 | BUG_ON(htx->head_addr > htx->end_addr); |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1061 | return len; |
| 1062 | |
| 1063 | add_new_block: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1064 | /* FIXME: check data.len (< 256MB) */ |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1065 | blk = htx_add_blk(htx, HTX_BLK_DATA, len); |
| 1066 | if (!blk) |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1067 | return 0; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 1068 | |
| 1069 | blk->info += len; |
| 1070 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len); |
| 1071 | return len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1074 | |
| 1075 | /* Adds an HTX block of type DATA in <htx> just after all other DATA |
| 1076 | * blocks. Because it relies on htx_add_data_atonce(), It may be happened to a |
| 1077 | * DATA block if possible. But, if the function succeeds, it will be the last |
| 1078 | * DATA block in all cases. If an error occurred, NULL is returned. Otherwise, |
| 1079 | * on success, the updated block (or the new one) is returned. |
| 1080 | */ |
| 1081 | 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] | 1082 | { |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1083 | struct htx_blk *blk, *pblk; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1084 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1085 | blk = htx_add_data_atonce(htx, data); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1086 | if (!blk) |
| 1087 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1088 | |
Christopher Faulet | 86bc8df | 2019-06-11 10:38:38 +0200 | [diff] [blame] | 1089 | 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] | 1090 | if (htx_get_blk_type(pblk) <= HTX_BLK_DATA) |
| 1091 | break; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1092 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1093 | /* Swap .addr and .info fields */ |
| 1094 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 1095 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 1096 | |
| 1097 | if (blk->addr == pblk->addr) |
| 1098 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1099 | blk = pblk; |
| 1100 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 1101 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 1102 | return blk; |
| 1103 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1104 | |
Christopher Faulet | 86fcf6d | 2019-06-11 10:41:19 +0200 | [diff] [blame] | 1105 | /* Moves the block <blk> just before the block <ref>. Both blocks must be in the |
| 1106 | * HTX message <htx> and <blk> must be placed after <ref>. pointer to these |
| 1107 | * blocks are updated to remain valid after the move. */ |
| 1108 | void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref) |
| 1109 | { |
| 1110 | struct htx_blk *cblk, *pblk; |
| 1111 | |
| 1112 | cblk = *blk; |
| 1113 | for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) { |
| 1114 | /* Swap .addr and .info fields */ |
| 1115 | cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr; |
| 1116 | cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info; |
| 1117 | |
| 1118 | if (cblk->addr == pblk->addr) |
| 1119 | cblk->addr += htx_get_blksz(pblk); |
| 1120 | if (pblk == *ref) |
| 1121 | break; |
| 1122 | cblk = pblk; |
| 1123 | } |
| 1124 | *blk = cblk; |
| 1125 | *ref = pblk; |
| 1126 | } |
Christopher Faulet | 0ea0c86 | 2020-01-23 11:47:53 +0100 | [diff] [blame] | 1127 | |
| 1128 | /* Append the HTX message <src> to the HTX message <dst>. It returns 1 on |
| 1129 | * success and 0 on error. All the message or nothing is copied. If an error |
| 1130 | * occurred, all blocks from <src> already appended to <dst> are truncated. |
| 1131 | */ |
| 1132 | int htx_append_msg(struct htx *dst, const struct htx *src) |
| 1133 | { |
| 1134 | struct htx_blk *blk, *newblk; |
| 1135 | enum htx_blk_type type; |
| 1136 | uint32_t blksz, offset = dst->data; |
| 1137 | |
| 1138 | for (blk = htx_get_head_blk(src); blk; blk = htx_get_next_blk(src, blk)) { |
| 1139 | type = htx_get_blk_type(blk); |
| 1140 | |
| 1141 | if (type == HTX_BLK_UNUSED) |
| 1142 | continue; |
| 1143 | |
| 1144 | blksz = htx_get_blksz(blk); |
| 1145 | newblk = htx_add_blk(dst, type, blksz); |
| 1146 | if (!newblk) |
| 1147 | goto error; |
| 1148 | newblk->info = blk->info; |
| 1149 | memcpy(htx_get_blk_ptr(dst, newblk), htx_get_blk_ptr(src, blk), blksz); |
| 1150 | } |
| 1151 | |
| 1152 | return 1; |
| 1153 | |
| 1154 | error: |
| 1155 | htx_truncate(dst, offset); |
| 1156 | return 0; |
| 1157 | } |