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