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