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