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> |
| 14 | #include <proto/htx.h> |
| 15 | |
| 16 | struct htx htx_empty = { .size = 0, .data = 0, .used = 0 }; |
| 17 | |
| 18 | /* Defragments an HTTP message, removing unused blocks and unwrapping blocks and |
| 19 | * their contents. A temporary message is used to do so. This function never |
| 20 | * fails. if <blk> is not NULL, we replace it by the new block address, after |
| 21 | * the defragmentation. The new <blk> is returned. |
| 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; |
| 29 | uint32_t new, old; |
| 30 | uint32_t addr, blksz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 31 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 32 | if (!htx->used) |
| 33 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 34 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 35 | new = 0; |
| 36 | addr = 0; |
| 37 | tmp->size = htx->size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 38 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 39 | /* start from the head */ |
| 40 | for (old = htx_get_head(htx); old != -1; old = htx_get_next(htx, old)) { |
| 41 | oldblk = htx_get_blk(htx, old); |
| 42 | if (htx_get_blk_type(oldblk) == HTX_BLK_UNUSED) { |
| 43 | htx->used--; |
| 44 | continue; |
| 45 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 46 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 47 | newblk = htx_get_blk(tmp, new); |
| 48 | newblk->addr = addr; |
| 49 | newblk->info = oldblk->info; |
| 50 | blksz = htx_get_blksz(oldblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 51 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 52 | memcpy((void *)tmp->blocks + addr, htx_get_blk_ptr(htx, oldblk), blksz); |
| 53 | new++; |
| 54 | addr += blksz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 55 | |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 56 | /* update the start-line offset */ |
| 57 | if (htx->sl_off == oldblk->addr) |
| 58 | htx->sl_off = addr; |
| 59 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 60 | /* if <blk> is defined, set its new location */ |
| 61 | if (blk != NULL && blk == oldblk) |
| 62 | blk = newblk; |
| 63 | } while (new < htx->used); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 64 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 65 | htx->wrap = htx->used; |
| 66 | htx->front = htx->tail = new - 1; |
| 67 | memcpy((void *)htx->blocks, (void *)tmp->blocks, htx->size); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 69 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Reserves a new block in the HTTP message <htx> with a content of <blksz> |
| 73 | * bytes. If there is not enough space, NULL is returned. Otherwise the reserved |
| 74 | * block is returned and the HTTP message is updated. Space for this new block |
| 75 | * is reserved in the HTTP message. But it is the caller responsibility to set |
| 76 | * right info in the block to reflect the stored data. |
| 77 | */ |
| 78 | static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz) |
| 79 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 80 | struct htx_blk *blk, *prevblk, *headblk, *frtblk; |
| 81 | uint32_t used; |
| 82 | uint32_t tail; |
| 83 | uint32_t prev; |
| 84 | uint32_t wrap; |
| 85 | uint32_t head; |
| 86 | int32_t headroom, tailroom; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 87 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 88 | if (blksz > htx_free_data_space(htx)) |
| 89 | return NULL; /* full */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 90 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 91 | if (!htx->used) { |
| 92 | /* Empty message */ |
| 93 | htx->front = htx->tail = 0; |
| 94 | htx->wrap = htx->used = 1; |
| 95 | blk = htx_get_blk(htx, htx->tail); |
| 96 | blk->addr = 0; |
| 97 | htx->data = blksz; |
| 98 | return blk; |
| 99 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 100 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 101 | used = htx->used + 1; |
| 102 | tail = htx->tail + 1; |
| 103 | prev = htx->tail; |
| 104 | wrap = htx->wrap; |
| 105 | head = htx_get_head(htx); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 106 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 107 | if (tail == wrap) { |
| 108 | frtblk = htx_get_blk(htx, htx->front); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 109 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 110 | /* Blocks don't wrap for now. We either need to push the new one |
| 111 | * on top of others or to defragement the table. */ |
| 112 | if (sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap+1) >= frtblk->addr + htx_get_blksz(frtblk)) |
| 113 | wrap++; |
| 114 | else if (tail >= used) /* There is hole at the beginning */ |
| 115 | tail = 0; |
| 116 | else { |
| 117 | /* No more room, tail hits data. We have to realign the |
| 118 | * whole message. */ |
| 119 | goto defrag; |
| 120 | } |
| 121 | } |
| 122 | else if (used >= wrap) { |
| 123 | /* We have hit the tail, we need to reorganize the blocks. */ |
| 124 | goto defrag; |
| 125 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 126 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 127 | /* Now we have updated tail, used and wrap, we know that there is some |
| 128 | * available room at least from the protocol's perspective. This space |
| 129 | * is split in two areas : |
| 130 | * |
| 131 | * 1: the space between the beginning of the blocks table and the |
| 132 | * front data's address. This space will only be used if data don't |
| 133 | * wrap yet. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 134 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 135 | * 2: If the previous tail was the front block, the space between the |
| 136 | * beginning of the message and the head data's address. |
| 137 | * Otherwise, the space between the tail data's address and the |
| 138 | * tail's one. |
| 139 | */ |
| 140 | prevblk = htx_get_blk(htx, prev); |
| 141 | headblk = htx_get_blk(htx, head); |
| 142 | if (prevblk->addr >= headblk->addr) { |
| 143 | /* the area was contiguous */ |
| 144 | frtblk = htx_get_blk(htx, htx->front); |
| 145 | tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, wrap) - (frtblk->addr + htx_get_blksz(frtblk)); |
| 146 | headroom = headblk->addr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 147 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 148 | if (tailroom >= (int32_t)blksz) { |
| 149 | /* install upfront and update ->front */ |
| 150 | blk = htx_get_blk(htx, tail); |
| 151 | blk->addr = frtblk->addr + htx_get_blksz(frtblk); |
| 152 | htx->front = tail; |
| 153 | } |
| 154 | else if (headroom >= (int32_t)blksz) { |
| 155 | blk = htx_get_blk(htx, tail); |
| 156 | blk->addr = 0; |
| 157 | } |
| 158 | else { |
| 159 | /* need to defragment the table before inserting upfront */ |
| 160 | goto defrag; |
| 161 | } |
| 162 | } |
| 163 | else { |
| 164 | /* it's already wrapped so we can't store anything in the tailroom */ |
| 165 | headroom = headblk->addr - (prevblk->addr + htx_get_blksz(prevblk)); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 166 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 167 | if (headroom >= (int32_t)blksz) { |
| 168 | blk = htx_get_blk(htx, tail); |
| 169 | blk->addr = prevblk->addr + htx_get_blksz(prevblk); |
| 170 | } |
| 171 | else { |
| 172 | defrag: |
| 173 | /* need to defragment the table before inserting upfront */ |
| 174 | htx_defrag(htx, NULL); |
| 175 | frtblk = htx_get_blk(htx, htx->front); |
| 176 | wrap = htx->wrap + 1; |
| 177 | tail = htx->tail + 1; |
| 178 | used = htx->used + 1; |
| 179 | blk = htx_get_blk(htx, tail); |
| 180 | blk->addr = frtblk->addr + htx_get_blksz(frtblk); |
| 181 | htx->front = tail; |
| 182 | } |
| 183 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 184 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 185 | htx->wrap = wrap; |
| 186 | htx->tail = tail; |
| 187 | htx->used = used; |
| 188 | htx->data += blksz; |
| 189 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* Adds a new block of type <type> in the HTTP message <htx>. Its content size |
| 193 | * is passed but it is the caller responsibility to do the copy. |
| 194 | */ |
| 195 | struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 196 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 197 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 198 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 199 | blk = htx_reserve_nxblk(htx, blksz); |
| 200 | if (!blk) |
| 201 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 202 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 203 | blk->info = (type << 28); |
| 204 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Removes the block <blk> from the HTTP message <htx>. The function returns the |
| 208 | * block following <blk> or NULL if <blk> is the last block or the last |
| 209 | * inserted one. |
| 210 | */ |
| 211 | struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk) |
| 212 | { |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 213 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 214 | uint32_t next, head, pos; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 215 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 216 | if (type != HTX_BLK_UNUSED) { |
| 217 | /* Mark the block as unused, decrement allocated size */ |
| 218 | htx->data -= htx_get_blksz(blk); |
| 219 | blk->info = ((uint32_t)HTX_BLK_UNUSED << 28); |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 220 | if (htx->sl_off == blk->addr) |
| 221 | htx->sl_off = -1; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 222 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 223 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 224 | /* This is the last block in use */ |
| 225 | if (htx->used == 1/* || !htx->data */) { |
| 226 | htx->front = htx->tail = 0; |
| 227 | htx->wrap = htx->used = 0; |
| 228 | htx->data = 0; |
| 229 | return NULL; |
| 230 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 231 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 232 | /* There is at least 2 blocks, so tail is always >= 0 */ |
| 233 | pos = htx_get_blk_pos(htx, blk); |
| 234 | head = htx_get_head(htx); |
| 235 | blk = NULL; |
| 236 | next = pos + 1; /* By default retrun the next block */ |
| 237 | if (htx->tail + 1 == htx->wrap) { |
| 238 | /* The HTTP message doesn't wrap */ |
| 239 | if (pos == head) { |
| 240 | /* remove the head, so just return the new head */ |
| 241 | htx->used--; |
| 242 | next = htx_get_head(htx); |
| 243 | } |
| 244 | else if (pos == htx->tail) { |
| 245 | /* remove the tail. this was the last inserted block so |
| 246 | * return NULL. */ |
| 247 | htx->wrap--; |
| 248 | htx->tail--; |
| 249 | htx->used--; |
| 250 | goto end; |
| 251 | } |
| 252 | } |
| 253 | else { |
| 254 | /* The HTTP message wraps */ |
| 255 | if (pos == htx->tail) { |
| 256 | /* remove the tail. try to unwrap the message (pos == 0) |
| 257 | * and return NULL. */ |
| 258 | htx->tail = ((pos == 0) ? htx->wrap-1 : htx->tail-1); |
| 259 | htx->used--; |
| 260 | goto end; |
| 261 | } |
| 262 | else if (pos == head) { |
| 263 | /* remove the head, try to unwrap the message (pos+1 == |
| 264 | * wrap) and return the new head */ |
| 265 | htx->used--; |
| 266 | if (pos + 1 == htx->wrap) |
| 267 | htx->wrap = htx->tail + 1; |
| 268 | next = htx_get_head(htx); |
| 269 | } |
| 270 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 271 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 272 | blk = htx_get_blk(htx, next); |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 273 | if (htx->sl_off == -1) { |
| 274 | /* Try to update the start-line offset, if possible */ |
| 275 | type = htx_get_blk_type(blk); |
| 276 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) |
| 277 | htx->sl_off = blk->addr; |
| 278 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 279 | end: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 280 | if (pos == htx->front) |
| 281 | htx->front = htx_find_front(htx); |
| 282 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Tries to append data to the last inserted block, if the type matches and if |
| 286 | * there is enough non-wrapping space. Only DATA and TRAILERS content can be |
| 287 | * appended. If the append fails, a new block is inserted. If an error occurred, |
| 288 | * NULL is returned. Otherwise, on success, the updated block (or the new one) |
| 289 | * is returned. |
| 290 | */ |
| 291 | static struct htx_blk *htx_append_blk_value(struct htx *htx, enum htx_blk_type type, |
| 292 | const struct ist data) |
| 293 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 294 | struct htx_blk *blk; |
| 295 | struct ist v; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 296 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 297 | if (!htx->used) |
| 298 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 299 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 300 | /* Not enough space to store data */ |
| 301 | if (data.len > htx_free_data_space(htx)) |
| 302 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 303 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 304 | /* Append only DATA et TRAILERS data */ |
| 305 | if (type != HTX_BLK_DATA && type != HTX_BLK_TLR) |
| 306 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 307 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 308 | /* get the tail block */ |
| 309 | blk = htx_get_blk(htx, htx->tail); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 310 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 311 | /* Don't try to append data if the last inserted block is not of the |
| 312 | * same type */ |
| 313 | if (type != htx_get_blk_type(blk)) |
| 314 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 315 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 316 | /* |
| 317 | * Same type and enough space: append data |
| 318 | */ |
| 319 | if (htx->tail + 1 == htx->wrap) { |
| 320 | struct htx_blk *frtblk = htx_get_blk(htx, htx->front); |
| 321 | int32_t tailroom = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk)); |
| 322 | if (tailroom >= (int32_t)data.len) |
| 323 | goto append_data; |
| 324 | htx_defrag(htx, NULL); |
| 325 | blk = htx_get_blk(htx, htx->tail); |
| 326 | } |
| 327 | else { |
| 328 | struct htx_blk *headblk = htx_get_blk(htx, htx_get_head(htx)); |
| 329 | int32_t headroom = headblk->addr - (blk->addr + htx_get_blksz(blk)); |
| 330 | if (headroom >= (int32_t)data.len) |
| 331 | goto append_data; |
| 332 | htx_defrag(htx, NULL); |
| 333 | blk = htx_get_blk(htx, htx->tail); |
| 334 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 335 | |
| 336 | append_data: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 337 | /* get the value of the tail block */ |
| 338 | /* FIXME: check v.len + data.len < 256MB */ |
| 339 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 340 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 341 | /* Append data and update the block itself */ |
| 342 | memcpy(v.ptr + v.len, data.ptr, data.len); |
| 343 | htx_set_blk_value_len(blk, v.len + data.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 344 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 345 | /* Update HTTP message */ |
| 346 | htx->data += data.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 347 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 348 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 349 | |
| 350 | add_new_block: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 351 | /* FIXME: check tlr.len (< 256MB) */ |
| 352 | blk = htx_add_blk(htx, type, data.len); |
| 353 | if (!blk) |
| 354 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 355 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 356 | blk->info += data.len; |
| 357 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
| 358 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* Replaces a value part of a block by a new one. The new part can be smaller or |
| 362 | * larger than the old one. This function works for any kind of block with |
| 363 | * attached data. It returns the new block on success, otherwise it returns |
| 364 | * NULL. |
| 365 | */ |
| 366 | 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] | 367 | const struct ist old, const struct ist new) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 368 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 369 | struct htx_blk *frtblk; |
| 370 | struct buffer *tmp; |
| 371 | struct ist n, v; |
| 372 | uint32_t info, room; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 373 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 374 | n = htx_get_blk_name(htx, blk); |
| 375 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 376 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 377 | /* easy case, new data are smaller, so replace it in-place */ |
| 378 | if (new.len <= old.len) { |
| 379 | memcpy(old.ptr, new.ptr, new.len); |
| 380 | if (old.len != v.len) |
| 381 | memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
| 382 | htx_set_blk_value_len(blk, v.len - old.len + new.len); |
| 383 | htx->data -= (old.len - new.len); |
| 384 | return blk; |
| 385 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 386 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 387 | /* we need to allocate more space to store the new header value */ |
| 388 | if ((new.len - old.len) > htx_free_space(htx)) |
| 389 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 390 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 391 | /* |
| 392 | * Copy the new header in a temp buffer |
| 393 | */ |
| 394 | tmp = get_trash_chunk(); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 395 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 396 | /* 1. copy the header name */ |
| 397 | chunk_memcat(tmp, n.ptr, n.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 398 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 399 | /* 2. copy value before old part, if any */ |
| 400 | if (old.ptr != v.ptr) |
| 401 | chunk_memcat(tmp, v.ptr, old.ptr - v.ptr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 402 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 403 | /* 3. copy new value */ |
| 404 | chunk_memcat(tmp, new.ptr, new.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 405 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 406 | /* 4. copy value after old part if any */ |
| 407 | if (old.len != v.len) |
| 408 | 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] | 409 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 410 | /* |
| 411 | * temporarely remove space reserved for the header |
| 412 | */ |
| 413 | info = blk->info; |
| 414 | blk->info &= 0xf0000000; |
| 415 | htx->data -= (n.len + v.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 416 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 417 | /* |
| 418 | * Try to find right addr to copy all the data |
| 419 | */ |
| 420 | if (htx->tail + 1 == htx->wrap) { |
| 421 | frtblk = htx_get_blk(htx, htx->front); |
| 422 | room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk)); |
| 423 | if (room >= htx->data) { |
| 424 | blk->addr = frtblk->addr + htx_get_blksz(frtblk); |
| 425 | goto replace_value; |
| 426 | } |
| 427 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 428 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 429 | /* HTX message need to be defragmented first */ |
| 430 | blk = htx_defrag(htx, blk); |
| 431 | frtblk = htx_get_blk(htx, htx->front); |
| 432 | blk->addr = frtblk->addr + htx_get_blksz(frtblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 433 | |
| 434 | replace_value: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 435 | blk->info = info; |
| 436 | htx_set_blk_value_len(blk, v.len - old.len + new.len); |
| 437 | memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data); |
| 438 | htx->data += tmp->data; |
| 439 | htx->front = htx_get_blk_pos(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 440 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 441 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the |
| 445 | * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were |
| 446 | * moved. It returns the number of bytes of data moved and the last HTX block |
| 447 | * inserted in <dst>. |
| 448 | */ |
| 449 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 450 | enum htx_blk_type mark) |
| 451 | { |
| 452 | struct htx_blk *blk, *dstblk; |
| 453 | enum htx_blk_type type; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 454 | uint32_t info, max, sz, ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 455 | |
| 456 | ret = 0; |
| 457 | blk = htx_get_blk(src, htx_get_head(src)); |
| 458 | dstblk = NULL; |
| 459 | while (blk && ret <= count) { |
| 460 | type = htx_get_blk_type(blk); |
| 461 | |
| 462 | /* Ingore unused block */ |
| 463 | if (type == HTX_BLK_UNUSED) |
| 464 | goto next; |
| 465 | |
| 466 | sz = htx_get_blksz(blk); |
| 467 | if (!sz) { |
| 468 | dstblk = htx_reserve_nxblk(dst, 0); |
| 469 | if (!dstblk) |
| 470 | break; |
| 471 | dstblk->info = blk->info; |
| 472 | goto next; |
| 473 | } |
| 474 | |
| 475 | info = blk->info; |
| 476 | max = htx_free_data_space(dst); |
| 477 | if (max > count) |
| 478 | max = count; |
| 479 | if (sz > max) { |
| 480 | sz = max; |
| 481 | info = (type << 28) + sz; |
| 482 | /* Headers and pseudo headers must be fully copied */ |
| 483 | if (type < HTX_BLK_DATA || !sz) |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | dstblk = htx_reserve_nxblk(dst, sz); |
| 488 | if (!dstblk) |
| 489 | break; |
| 490 | dstblk->info = info; |
| 491 | memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz); |
| 492 | |
| 493 | ret += sz; |
| 494 | if (blk->info != info) { |
| 495 | /* Partial move: don't remove <blk> from <src> but |
| 496 | * resize its content */ |
| 497 | blk->addr += sz; |
| 498 | htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz); |
| 499 | src->data -= sz; |
| 500 | break; |
| 501 | } |
| 502 | |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 503 | if (dst->sl_off == -1 && src->sl_off == blk->addr) |
| 504 | dst->sl_off = dstblk->addr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 505 | next: |
| 506 | blk = htx_remove_blk(src, blk); |
| 507 | if (type == mark) |
| 508 | break; |
| 509 | |
| 510 | } |
| 511 | |
| 512 | return (struct htx_ret){.ret = ret, .blk = dstblk}; |
| 513 | } |
| 514 | |
| 515 | static struct htx_blk *htx_new_blk_value(struct htx *htx, struct htx_blk *blk, |
| 516 | uint32_t newsz) |
| 517 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 518 | struct htx_blk *frtblk; |
| 519 | uint32_t sz, room; |
| 520 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 521 | |
| 522 | sz = htx_get_blksz(blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 523 | delta = newsz - sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 524 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 525 | /* easy case, new value is smaller, so replace it in-place */ |
| 526 | if (delta <= 0) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 527 | /* Reset value size. It is the caller responsibility to set the new one */ |
| 528 | blk->info &= 0xf0000000; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 529 | htx->data += delta; |
| 530 | return blk; |
| 531 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 533 | /* we need to allocate more space to store the new value */ |
| 534 | if (delta > htx_free_space(htx)) |
| 535 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 536 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 537 | /* |
| 538 | * temporarely remove space reserved for the old value |
| 539 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 540 | /* Reset value size. It is the caller responsibility to set the new one */ |
| 541 | blk->info &= 0xf0000000; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 542 | htx->data -= sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 543 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 544 | /* |
| 545 | * Try to find right addr to copy all the data |
| 546 | */ |
| 547 | if (htx->tail + 1 == htx->wrap) { |
| 548 | frtblk = htx_get_blk(htx, htx->front); |
| 549 | room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk)); |
| 550 | if (room >= newsz) |
| 551 | goto replace_value; |
| 552 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 553 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 554 | /* HTX message need to be defragmented first */ |
| 555 | blk = htx_defrag(htx, blk); |
| 556 | frtblk = htx_get_blk(htx, htx->front); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 557 | |
| 558 | replace_value: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 559 | blk->addr = frtblk->addr + htx_get_blksz(frtblk); |
| 560 | htx->data += newsz; |
| 561 | htx->front = htx_get_blk_pos(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 562 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 563 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* Replaces an header by a new one. The new header can be smaller or larger than |
| 567 | * the old one. It returns the new block on success, otherwise it returns NULL. |
| 568 | */ |
| 569 | 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] | 570 | const struct ist name, const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 571 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 572 | enum htx_blk_type type; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 573 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 574 | type = htx_get_blk_type(blk); |
| 575 | if (type != HTX_BLK_HDR) |
| 576 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 577 | |
| 578 | blk = htx_new_blk_value(htx, blk, (name.len + value.len)); |
| 579 | if (!blk) |
| 580 | return NULL; |
| 581 | |
| 582 | blk->info = (type << 28) + (value.len << 8) + name.len; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 583 | memcpy(htx_get_blk_ptr(htx, blk), name.ptr, name.len); |
| 584 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 585 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 586 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 589 | /* Replaces the parts of the start-line. It returns the new start-line on |
| 590 | * success, otherwise it returns NULL. It is the caller responsibility to update |
| 591 | * sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 592 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 593 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 594 | const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 595 | { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 596 | struct htx_sl *sl; |
| 597 | struct htx_sl tmp; /* used to save sl->info and sl->flags */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 598 | enum htx_blk_type type; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 599 | uint32_t size; |
| 600 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 601 | type = htx_get_blk_type(blk); |
| 602 | if (type != HTX_BLK_REQ_SL || HTX_BLK_RES_SL) |
| 603 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 604 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 605 | /* Save start-line info and flags */ |
| 606 | sl = htx_get_blk_ptr(htx, blk); |
| 607 | tmp.info = sl->info; |
| 608 | tmp.flags = sl->flags; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 609 | if (htx->sl_off == blk->addr) |
| 610 | htx->sl_off = -1; |
| 611 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 612 | |
| 613 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 614 | blk = htx_new_blk_value(htx, blk, size); |
| 615 | if (!blk) |
| 616 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 617 | blk->info = (type << 28) + size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 618 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 619 | /* Restore start-line info and flags*/ |
| 620 | sl = htx_get_blk_ptr(htx, blk); |
| 621 | sl->info = tmp.info; |
| 622 | sl->flags = tmp.flags; |
| 623 | if (htx->sl_off == -1) |
| 624 | htx->sl_off = blk->addr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 625 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 626 | HTX_SL_P1_LEN(sl) = p1.len; |
| 627 | HTX_SL_P2_LEN(sl) = p2.len; |
| 628 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 629 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 630 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 631 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 632 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 633 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 634 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 637 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 638 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 639 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 640 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 641 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 642 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 643 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 644 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 645 | uint32_t size; |
| 646 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 647 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 648 | return NULL; |
| 649 | |
| 650 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 651 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 652 | /* FIXME: check size (< 256MB) */ |
| 653 | blk = htx_add_blk(htx, type, size); |
| 654 | if (!blk) |
| 655 | return NULL; |
| 656 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 657 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 658 | sl = htx_get_blk_ptr(htx, blk); |
| 659 | if (htx->sl_off == -1) |
| 660 | htx->sl_off = blk->addr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 661 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 662 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 663 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 664 | HTX_SL_P1_LEN(sl) = p1.len; |
| 665 | HTX_SL_P2_LEN(sl) = p2.len; |
| 666 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 667 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 668 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 669 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 670 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 671 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 672 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* Adds an HTX block of type HDR in <htx>. It returns the new block on |
| 676 | * success. Otherwise, it returns NULL. |
| 677 | */ |
| 678 | 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] | 679 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 680 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 681 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 682 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 683 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 684 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 685 | if (!blk) |
| 686 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 687 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 688 | blk->info += (value.len << 8) + name.len; |
| 689 | memcpy(htx_get_blk_ptr(htx, blk), name.ptr, name.len); |
| 690 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 691 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 695 | { |
| 696 | int i; |
| 697 | |
| 698 | for (i = 0; hdrs[i].n.len; i++) { |
| 699 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 700 | return NULL; |
| 701 | } |
| 702 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 703 | } |
| 704 | /* Adds an HTX block of type PHDR in <htx>. It returns the new block on |
| 705 | * success. Otherwise, it returns NULL. |
| 706 | */ |
| 707 | struct htx_blk *htx_add_pseudo_header(struct htx *htx, enum htx_phdr_type phdr, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 708 | 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 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 711 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 712 | /* FIXME: check value.len ( < 1MB) */ |
| 713 | blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len); |
| 714 | if (!blk) |
| 715 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 716 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 717 | blk->info += (value.len << 8) + phdr; |
| 718 | memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len); |
| 719 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block |
| 723 | * on success. Otherwise, it returns NULL. |
| 724 | */ |
| 725 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 726 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 727 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 728 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 729 | blk = htx_add_blk(htx, type, 1); |
| 730 | if (!blk) |
| 731 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 732 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 733 | blk->info += 1; |
| 734 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | |
| 738 | /* Adds an HTX block of type DATA in <htx>. It first tries to append data if |
| 739 | * possible. It returns the new block on success. Otherwise, it returns NULL. |
| 740 | */ |
| 741 | struct htx_blk *htx_add_data(struct htx *htx, const struct ist data) |
| 742 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 743 | return htx_append_blk_value(htx, HTX_BLK_DATA, data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Adds an HTX block of type TLR in <htx>. It first tries to append trailers |
| 747 | * data if possible. It returns the new block on success. Otherwise, it returns |
| 748 | * NULL. |
| 749 | */ |
| 750 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr) |
| 751 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 752 | return htx_append_blk_value(htx, HTX_BLK_TLR, tlr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* Adds an HTX block of type OOB in <htx>. It returns the new block on |
| 756 | * success. Otherwise, it returns NULL. |
| 757 | */ |
| 758 | struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob) |
| 759 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 760 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 761 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 762 | /* FIXME: check oob.len (< 256MB) */ |
| 763 | blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len); |
| 764 | if (!blk) |
| 765 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 766 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 767 | blk->info += oob.len; |
| 768 | memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len); |
| 769 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 770 | } |
| 771 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 772 | struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, |
| 773 | const struct ist data) |
| 774 | { |
| 775 | struct htx_blk *blk; |
| 776 | int32_t prev; |
| 777 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 778 | /* FIXME: check data.len (< 256MB) */ |
| 779 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
| 780 | if (!blk) |
| 781 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 782 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 783 | blk->info += data.len; |
| 784 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 785 | |
| 786 | for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) { |
| 787 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 788 | |
| 789 | /* Swap .addr and .info fields */ |
| 790 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 791 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 792 | |
| 793 | if (blk->addr == pblk->addr) |
| 794 | blk->addr += htx_get_blksz(pblk); |
| 795 | htx->front = prev; |
| 796 | |
| 797 | if (pblk == ref) |
| 798 | break; |
| 799 | blk = pblk; |
| 800 | } |
| 801 | return blk; |
| 802 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 803 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 804 | /* Appends the H1 representation of the request line block <blk> to the |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 805 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 806 | * returns 0. |
| 807 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 808 | 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] | 809 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 810 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 811 | return 0; |
| 812 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 813 | 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] | 814 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 815 | 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] | 816 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 817 | if (sl->flags & HTX_SL_F_VER_11) |
| 818 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 819 | else |
| 820 | chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 821 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 822 | chunk_memcat(chk, "\r\n", 2); |
| 823 | |
| 824 | return 1; |
| 825 | } |
| 826 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 827 | /* Appends the H1 representation of the status line block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 828 | * <chk>. It returns 1 if data are successfully appended, otherwise it |
| 829 | * returns 0. |
| 830 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 831 | 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] | 832 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 833 | if (HTX_SL_LEN(sl) + 4 > b_size(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 834 | return 0; |
| 835 | |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 836 | if (sl->flags & HTX_SL_F_VER_11) |
| 837 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 838 | else |
| 839 | 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] | 840 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 841 | 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] | 842 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 843 | 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] | 844 | chunk_memcat(chk, "\r\n", 2); |
| 845 | |
| 846 | return 1; |
| 847 | } |
| 848 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 849 | /* Appends the H1 representation of the header block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 850 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 851 | * 0. |
| 852 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 853 | 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] | 854 | { |
| 855 | if (n.len + v.len + 4 > b_room(chk)) |
| 856 | return 0; |
| 857 | |
| 858 | chunk_memcat(chk, n.ptr, n.len); |
| 859 | chunk_memcat(chk, ": ", 2); |
| 860 | chunk_memcat(chk, v.ptr, v.len); |
| 861 | chunk_memcat(chk, "\r\n", 2); |
| 862 | |
| 863 | return 1; |
| 864 | } |
| 865 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 866 | /* Appends the H1 representation of the data block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 867 | * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It |
| 868 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 869 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 870 | 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] | 871 | { |
| 872 | if (chunked) { |
| 873 | uint32_t chksz; |
| 874 | char tmp[10]; |
| 875 | char *beg, *end; |
| 876 | |
| 877 | chksz = data.len; |
| 878 | |
| 879 | beg = end = tmp+10; |
| 880 | *--beg = '\n'; |
| 881 | *--beg = '\r'; |
| 882 | do { |
| 883 | *--beg = hextab[chksz & 0xF]; |
| 884 | } while (chksz >>= 4); |
| 885 | |
| 886 | if (data.len + (end - beg) + 2 > b_room(chk)) |
| 887 | return 0; |
| 888 | chunk_memcat(chk, beg, end - beg); |
| 889 | chunk_memcat(chk, data.ptr, data.len); |
| 890 | chunk_memcat(chk, "\r\n", 2); |
| 891 | } |
| 892 | else { |
| 893 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | return 1; |
| 898 | } |
| 899 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 900 | /* Appends the h1 representation of the trailer block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 901 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 902 | * 0. |
| 903 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 904 | int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 905 | { |
| 906 | /* FIXME: be sure the CRLF is here or remove it when inserted */ |
| 907 | if (!chunk_memcat(chk, tlr.ptr, tlr.len)) |
| 908 | return 0; |
| 909 | return 1; |
| 910 | } |