Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * internal HTTP message |
| 3 | * |
| 4 | * Copyright 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/chunk.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 14 | #include <common/htx.h> |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 15 | |
| 16 | struct htx htx_empty = { .size = 0, .data = 0, .used = 0 }; |
| 17 | |
| 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 | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 371 | struct buffer *tmp; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 372 | struct ist n, v; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 373 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 374 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 375 | n = htx_get_blk_name(htx, blk); |
| 376 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 377 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 378 | delta = new.len - old.len; |
| 379 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 380 | /* easy case, new data are smaller, so replace it in-place */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 381 | if (delta <= 0) { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 382 | memcpy(old.ptr, new.ptr, new.len); |
| 383 | if (old.len != v.len) |
| 384 | memmove(old.ptr + new.len, old.ptr + old.len, (v.ptr + v.len) - (old.ptr + old.len)); |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 385 | htx_set_blk_value_len(blk, v.len + delta); |
| 386 | htx->data += delta; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 387 | return blk; |
| 388 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 389 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 390 | /* we need to allocate more space to store the new header value */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 391 | if (delta > htx_free_space(htx)) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 392 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 393 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 394 | /* |
| 395 | * Copy the new header in a temp buffer |
| 396 | */ |
| 397 | tmp = get_trash_chunk(); |
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 | /* 1. copy the header name */ |
| 400 | chunk_memcat(tmp, n.ptr, n.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 401 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 402 | /* 2. copy value before old part, if any */ |
| 403 | if (old.ptr != v.ptr) |
| 404 | chunk_memcat(tmp, v.ptr, old.ptr - v.ptr); |
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 | /* 3. copy new value */ |
| 407 | chunk_memcat(tmp, new.ptr, new.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 408 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 409 | /* 4. copy value after old part if any */ |
| 410 | if (old.len != v.len) |
| 411 | 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] | 412 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 413 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 414 | /* Expand the block size. But data are not copied yet. Then defragment |
| 415 | * the HTX message. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 416 | */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 417 | htx_set_blk_value_len(blk, v.len + delta); |
| 418 | htx->data += delta; |
| 419 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 420 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 421 | /* Finaly, copy data. */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 422 | memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 423 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 424 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Transfer HTX blocks from <src> to <dst>, stopping on the first block of the |
| 428 | * type <mark> (typically EOH, EOD or EOM) or when <count> bytes of data were |
| 429 | * moved. It returns the number of bytes of data moved and the last HTX block |
| 430 | * inserted in <dst>. |
| 431 | */ |
| 432 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 433 | enum htx_blk_type mark) |
| 434 | { |
| 435 | struct htx_blk *blk, *dstblk; |
| 436 | enum htx_blk_type type; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 437 | uint32_t info, max, sz, ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 438 | |
| 439 | ret = 0; |
| 440 | blk = htx_get_blk(src, htx_get_head(src)); |
| 441 | dstblk = NULL; |
| 442 | while (blk && ret <= count) { |
| 443 | type = htx_get_blk_type(blk); |
| 444 | |
| 445 | /* Ingore unused block */ |
| 446 | if (type == HTX_BLK_UNUSED) |
| 447 | goto next; |
| 448 | |
| 449 | sz = htx_get_blksz(blk); |
| 450 | if (!sz) { |
| 451 | dstblk = htx_reserve_nxblk(dst, 0); |
| 452 | if (!dstblk) |
| 453 | break; |
| 454 | dstblk->info = blk->info; |
| 455 | goto next; |
| 456 | } |
| 457 | |
| 458 | info = blk->info; |
| 459 | max = htx_free_data_space(dst); |
| 460 | if (max > count) |
| 461 | max = count; |
| 462 | if (sz > max) { |
| 463 | sz = max; |
| 464 | info = (type << 28) + sz; |
| 465 | /* Headers and pseudo headers must be fully copied */ |
| 466 | if (type < HTX_BLK_DATA || !sz) |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | dstblk = htx_reserve_nxblk(dst, sz); |
| 471 | if (!dstblk) |
| 472 | break; |
| 473 | dstblk->info = info; |
| 474 | memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz); |
| 475 | |
| 476 | ret += sz; |
| 477 | if (blk->info != info) { |
| 478 | /* Partial move: don't remove <blk> from <src> but |
| 479 | * resize its content */ |
| 480 | blk->addr += sz; |
| 481 | htx_set_blk_value_len(blk, htx_get_blksz(blk) - sz); |
| 482 | src->data -= sz; |
| 483 | break; |
| 484 | } |
| 485 | |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 486 | if (dst->sl_off == -1 && src->sl_off == blk->addr) |
| 487 | dst->sl_off = dstblk->addr; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 488 | next: |
| 489 | blk = htx_remove_blk(src, blk); |
| 490 | if (type == mark) |
| 491 | break; |
| 492 | |
| 493 | } |
| 494 | |
| 495 | return (struct htx_ret){.ret = ret, .blk = dstblk}; |
| 496 | } |
| 497 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 498 | /* Replaces an header by a new one. The new header can be smaller or larger than |
| 499 | * 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] | 500 | * The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 501 | */ |
| 502 | 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] | 503 | const struct ist name, const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 504 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 505 | enum htx_blk_type type; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 506 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 507 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 508 | type = htx_get_blk_type(blk); |
| 509 | if (type != HTX_BLK_HDR) |
| 510 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 511 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 512 | delta = name.len + value.len - htx_get_blksz(blk); |
| 513 | |
| 514 | /* easy case, new value is smaller, so replace it in-place */ |
| 515 | if (delta <= 0) { |
| 516 | blk->info = (type << 28) + (value.len << 8) + name.len; |
| 517 | htx->data += delta; |
| 518 | goto copy; |
| 519 | } |
| 520 | |
| 521 | /* we need to allocate more space to store the new value */ |
| 522 | if (delta > htx_free_space(htx)) |
| 523 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 524 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 525 | /* Expand the block size. But data are not copied yet. Then defragment |
| 526 | * the HTX message. |
| 527 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 528 | blk->info = (type << 28) + (value.len << 8) + name.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 529 | htx->data += delta; |
| 530 | blk = htx_defrag(htx, blk); |
| 531 | |
| 532 | copy: |
| 533 | /* Finaly, copy data. */ |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 534 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 535 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 536 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 539 | /* Replaces the parts of the start-line. It returns the new start-line on |
| 540 | * success, otherwise it returns NULL. It is the caller responsibility to update |
| 541 | * sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 542 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 543 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 544 | const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 545 | { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 546 | struct htx_sl *sl; |
| 547 | struct htx_sl tmp; /* used to save sl->info and sl->flags */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 548 | enum htx_blk_type type; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 549 | uint32_t size; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 550 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 551 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 552 | type = htx_get_blk_type(blk); |
Willy Tarreau | c706cd7 | 2018-12-07 17:12:22 +0100 | [diff] [blame] | 553 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 554 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 555 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 556 | /* Save start-line info and flags */ |
| 557 | sl = htx_get_blk_ptr(htx, blk); |
| 558 | tmp.info = sl->info; |
| 559 | tmp.flags = sl->flags; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 560 | if (htx->sl_off == blk->addr) |
| 561 | htx->sl_off = -1; |
| 562 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 563 | |
| 564 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 565 | delta = size - htx_get_blksz(blk); |
| 566 | |
| 567 | /* easy case, new data are smaller, so replace it in-place */ |
| 568 | if (delta <= 0) { |
| 569 | htx_set_blk_value_len(blk, size); |
| 570 | htx->data += delta; |
| 571 | goto copy; |
| 572 | } |
| 573 | |
| 574 | /* we need to allocate more space to store the new value */ |
| 575 | if (delta > htx_free_space(htx)) |
| 576 | return NULL; /* not enough space */ |
| 577 | |
| 578 | /* Expand the block size. But data are not copied yet. Then defragment |
| 579 | * the HTX message. |
| 580 | */ |
| 581 | htx_set_blk_value_len(blk, size); |
| 582 | htx->data += delta; |
| 583 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 584 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 585 | copy: |
| 586 | /* Restore start-line info and flags and copy parts of the start-line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 587 | sl = htx_get_blk_ptr(htx, blk); |
| 588 | sl->info = tmp.info; |
| 589 | sl->flags = tmp.flags; |
| 590 | if (htx->sl_off == -1) |
| 591 | htx->sl_off = blk->addr; |
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 | HTX_SL_P1_LEN(sl) = p1.len; |
| 594 | HTX_SL_P2_LEN(sl) = p2.len; |
| 595 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 596 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 597 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 598 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 599 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 600 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 601 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 604 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 605 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 606 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 607 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 608 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 609 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 610 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 611 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 612 | uint32_t size; |
| 613 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 614 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 615 | return NULL; |
| 616 | |
| 617 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 618 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 619 | /* FIXME: check size (< 256MB) */ |
| 620 | blk = htx_add_blk(htx, type, size); |
| 621 | if (!blk) |
| 622 | return NULL; |
| 623 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 624 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 625 | sl = htx_get_blk_ptr(htx, blk); |
| 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 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 630 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 631 | HTX_SL_P1_LEN(sl) = p1.len; |
| 632 | HTX_SL_P2_LEN(sl) = p2.len; |
| 633 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 634 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 635 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 636 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 637 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 638 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 639 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* 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] | 643 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 644 | */ |
| 645 | 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] | 646 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 647 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 648 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 649 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 650 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 651 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 652 | if (!blk) |
| 653 | return NULL; |
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 | blk->info += (value.len << 8) + name.len; |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 656 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 657 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 658 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 662 | { |
| 663 | int i; |
| 664 | |
| 665 | for (i = 0; hdrs[i].n.len; i++) { |
| 666 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 667 | return NULL; |
| 668 | } |
| 669 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 670 | } |
| 671 | /* Adds an HTX block of type PHDR in <htx>. It returns the new block on |
| 672 | * success. Otherwise, it returns NULL. |
| 673 | */ |
| 674 | 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] | 675 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 676 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 677 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 678 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 679 | /* FIXME: check value.len ( < 1MB) */ |
| 680 | blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len); |
| 681 | if (!blk) |
| 682 | return NULL; |
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 | blk->info += (value.len << 8) + phdr; |
| 685 | memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len); |
| 686 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block |
| 690 | * on success. Otherwise, it returns NULL. |
| 691 | */ |
| 692 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 693 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 694 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 695 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 696 | blk = htx_add_blk(htx, type, 1); |
| 697 | if (!blk) |
| 698 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 699 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 700 | blk->info += 1; |
| 701 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
| 705 | /* Adds an HTX block of type DATA in <htx>. It first tries to append data if |
| 706 | * possible. It returns the new block on success. Otherwise, it returns NULL. |
| 707 | */ |
| 708 | struct htx_blk *htx_add_data(struct htx *htx, const struct ist data) |
| 709 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 710 | return htx_append_blk_value(htx, HTX_BLK_DATA, data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* Adds an HTX block of type TLR in <htx>. It first tries to append trailers |
| 714 | * data if possible. It returns the new block on success. Otherwise, it returns |
| 715 | * NULL. |
| 716 | */ |
| 717 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr) |
| 718 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 719 | return htx_append_blk_value(htx, HTX_BLK_TLR, tlr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* Adds an HTX block of type OOB in <htx>. It returns the new block on |
| 723 | * success. Otherwise, it returns NULL. |
| 724 | */ |
| 725 | struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob) |
| 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 | /* FIXME: check oob.len (< 256MB) */ |
| 730 | blk = htx_add_blk(htx, HTX_BLK_OOB, oob.len); |
| 731 | if (!blk) |
| 732 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 733 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 734 | blk->info += oob.len; |
| 735 | memcpy(htx_get_blk_ptr(htx, blk), oob.ptr, oob.len); |
| 736 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 739 | struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, |
| 740 | const struct ist data) |
| 741 | { |
| 742 | struct htx_blk *blk; |
| 743 | int32_t prev; |
| 744 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 745 | /* FIXME: check data.len (< 256MB) */ |
| 746 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
| 747 | if (!blk) |
| 748 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 749 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 750 | blk->info += data.len; |
| 751 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 752 | |
| 753 | for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) { |
| 754 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 755 | |
| 756 | /* Swap .addr and .info fields */ |
| 757 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 758 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 759 | |
| 760 | if (blk->addr == pblk->addr) |
| 761 | blk->addr += htx_get_blksz(pblk); |
| 762 | htx->front = prev; |
| 763 | |
| 764 | if (pblk == ref) |
| 765 | break; |
| 766 | blk = pblk; |
| 767 | } |
| 768 | return blk; |
| 769 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 770 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 771 | /* Appends the H1 representation of the request line block <blk> to the |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 772 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 773 | * returns 0. |
| 774 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 775 | 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] | 776 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 777 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 778 | return 0; |
| 779 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 780 | 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] | 781 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 782 | 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] | 783 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 784 | if (sl->flags & HTX_SL_F_VER_11) |
| 785 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 786 | else |
| 787 | chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 788 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 789 | chunk_memcat(chk, "\r\n", 2); |
| 790 | |
| 791 | return 1; |
| 792 | } |
| 793 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 794 | /* 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] | 795 | * <chk>. It returns 1 if data are successfully appended, otherwise it |
| 796 | * returns 0. |
| 797 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 798 | 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] | 799 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 800 | if (HTX_SL_LEN(sl) + 4 > b_size(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 801 | return 0; |
| 802 | |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 803 | if (sl->flags & HTX_SL_F_VER_11) |
| 804 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 805 | else |
| 806 | 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] | 807 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 808 | 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] | 809 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 810 | 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] | 811 | chunk_memcat(chk, "\r\n", 2); |
| 812 | |
| 813 | return 1; |
| 814 | } |
| 815 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 816 | /* Appends the H1 representation of the header block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 817 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 818 | * 0. |
| 819 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 820 | 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] | 821 | { |
| 822 | if (n.len + v.len + 4 > b_room(chk)) |
| 823 | return 0; |
| 824 | |
| 825 | chunk_memcat(chk, n.ptr, n.len); |
| 826 | chunk_memcat(chk, ": ", 2); |
| 827 | chunk_memcat(chk, v.ptr, v.len); |
| 828 | chunk_memcat(chk, "\r\n", 2); |
| 829 | |
| 830 | return 1; |
| 831 | } |
| 832 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 833 | /* Appends the H1 representation of the data block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 834 | * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It |
| 835 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 836 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 837 | 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] | 838 | { |
| 839 | if (chunked) { |
| 840 | uint32_t chksz; |
| 841 | char tmp[10]; |
| 842 | char *beg, *end; |
| 843 | |
| 844 | chksz = data.len; |
| 845 | |
| 846 | beg = end = tmp+10; |
| 847 | *--beg = '\n'; |
| 848 | *--beg = '\r'; |
| 849 | do { |
| 850 | *--beg = hextab[chksz & 0xF]; |
| 851 | } while (chksz >>= 4); |
| 852 | |
| 853 | if (data.len + (end - beg) + 2 > b_room(chk)) |
| 854 | return 0; |
| 855 | chunk_memcat(chk, beg, end - beg); |
| 856 | chunk_memcat(chk, data.ptr, data.len); |
| 857 | chunk_memcat(chk, "\r\n", 2); |
| 858 | } |
| 859 | else { |
| 860 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | return 1; |
| 865 | } |
| 866 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 867 | /* Appends the h1 representation of the trailer block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 868 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 869 | * 0. |
| 870 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 871 | int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 872 | { |
| 873 | /* FIXME: be sure the CRLF is here or remove it when inserted */ |
| 874 | if (!chunk_memcat(chk, tlr.ptr, tlr.len)) |
| 875 | return 0; |
| 876 | return 1; |
| 877 | } |