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