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