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