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 | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 636 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 637 | HTX_SL_P1_LEN(sl) = p1.len; |
| 638 | HTX_SL_P2_LEN(sl) = p2.len; |
| 639 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 640 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 641 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 642 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 643 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 644 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 645 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 648 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 649 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 650 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 651 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 652 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 653 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 654 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 655 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 656 | uint32_t size; |
| 657 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 658 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 659 | return NULL; |
| 660 | |
| 661 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 662 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 663 | /* FIXME: check size (< 256MB) */ |
| 664 | blk = htx_add_blk(htx, type, size); |
| 665 | if (!blk) |
| 666 | return NULL; |
| 667 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 668 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 669 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c66b98 | 2019-04-30 18:08:26 +0200 | [diff] [blame] | 670 | if (htx->sl_pos == -1) |
| 671 | htx->sl_pos = htx_get_blk_pos(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 672 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 673 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 674 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 675 | HTX_SL_P1_LEN(sl) = p1.len; |
| 676 | HTX_SL_P2_LEN(sl) = p2.len; |
| 677 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 678 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 679 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 680 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 681 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 682 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 683 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* 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] | 687 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 688 | */ |
| 689 | 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] | 690 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 691 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 692 | struct htx_blk *blk; |
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 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 695 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 696 | if (!blk) |
| 697 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 698 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 699 | blk->info += (value.len << 8) + name.len; |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 700 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 701 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 702 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Willy Tarreau | 52610e9 | 2019-01-03 18:26:17 +0100 | [diff] [blame] | 705 | /* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the |
| 706 | * new block on success. Otherwise, it returns NULL. The caller is responsible |
| 707 | * for filling the block itself. |
| 708 | */ |
| 709 | struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 710 | { |
| 711 | struct htx_blk *blk; |
| 712 | |
| 713 | blk = htx_add_blk(htx, type, blksz); |
| 714 | if (!blk) |
| 715 | return NULL; |
| 716 | |
| 717 | blk->info += blksz; |
| 718 | return blk; |
| 719 | } |
| 720 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 721 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 722 | { |
| 723 | int i; |
| 724 | |
| 725 | for (i = 0; hdrs[i].n.len; i++) { |
| 726 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 727 | return NULL; |
| 728 | } |
| 729 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 730 | } |
| 731 | /* Adds an HTX block of type PHDR in <htx>. It returns the new block on |
| 732 | * success. Otherwise, it returns NULL. |
| 733 | */ |
| 734 | 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] | 735 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 736 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 737 | struct htx_blk *blk; |
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 | /* FIXME: check value.len ( < 1MB) */ |
| 740 | blk = htx_add_blk(htx, HTX_BLK_PHDR, value.len); |
| 741 | if (!blk) |
| 742 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 743 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 744 | blk->info += (value.len << 8) + phdr; |
| 745 | memcpy(htx_get_blk_ptr(htx, blk), value.ptr, value.len); |
| 746 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block |
| 750 | * on success. Otherwise, it returns NULL. |
| 751 | */ |
| 752 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 753 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 754 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 755 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 756 | blk = htx_add_blk(htx, type, 1); |
| 757 | if (!blk) |
| 758 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 759 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 760 | blk->info += 1; |
| 761 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | |
| 765 | /* Adds an HTX block of type DATA in <htx>. It first tries to append data if |
| 766 | * possible. It returns the new block on success. Otherwise, it returns NULL. |
| 767 | */ |
| 768 | struct htx_blk *htx_add_data(struct htx *htx, const struct ist data) |
| 769 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 770 | return htx_append_blk_value(htx, HTX_BLK_DATA, data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Christopher Faulet | 6177509 | 2019-05-07 21:42:27 +0200 | [diff] [blame] | 773 | /* Adds an HTX block of type TLR in <htx>. It returns the new block on |
| 774 | * success. Otherwise, it returns NULL. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 775 | */ |
| 776 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr) |
| 777 | { |
Christopher Faulet | 6177509 | 2019-05-07 21:42:27 +0200 | [diff] [blame] | 778 | struct htx_blk *blk; |
| 779 | |
| 780 | /* FIXME: check tlr.len (< 256MB) */ |
| 781 | blk = htx_add_blk(htx, HTX_BLK_TLR, tlr.len); |
| 782 | if (!blk) |
| 783 | return NULL; |
| 784 | |
| 785 | blk->info += tlr.len; |
| 786 | memcpy(htx_get_blk_ptr(htx, blk), tlr.ptr, tlr.len); |
| 787 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 788 | } |
| 789 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 790 | struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, |
| 791 | const struct ist data) |
| 792 | { |
| 793 | struct htx_blk *blk; |
| 794 | int32_t prev; |
| 795 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 796 | /* FIXME: check data.len (< 256MB) */ |
| 797 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
| 798 | if (!blk) |
| 799 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 800 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 801 | blk->info += data.len; |
| 802 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 803 | |
| 804 | for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) { |
| 805 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 806 | |
| 807 | /* Swap .addr and .info fields */ |
| 808 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 809 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 810 | |
| 811 | if (blk->addr == pblk->addr) |
| 812 | blk->addr += htx_get_blksz(pblk); |
| 813 | htx->front = prev; |
| 814 | |
| 815 | if (pblk == ref) |
| 816 | break; |
| 817 | blk = pblk; |
| 818 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 819 | |
| 820 | if (htx_get_blk_pos(htx, blk) != htx->front) |
| 821 | htx_defrag(htx, NULL); |
| 822 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 823 | return blk; |
| 824 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 825 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 826 | /* Appends the H1 representation of the request line block <blk> to the |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 827 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 828 | * returns 0. |
| 829 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 830 | 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] | 831 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 832 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 833 | return 0; |
| 834 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 835 | 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] | 836 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 837 | 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] | 838 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 839 | if (sl->flags & HTX_SL_F_VER_11) |
| 840 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 841 | else |
| 842 | chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 843 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 844 | chunk_memcat(chk, "\r\n", 2); |
| 845 | |
| 846 | return 1; |
| 847 | } |
| 848 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 849 | /* Appends the H1 representation of the status line block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 850 | * <chk>. It returns 1 if data are successfully appended, otherwise it |
| 851 | * returns 0. |
| 852 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 853 | 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] | 854 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 855 | if (HTX_SL_LEN(sl) + 4 > b_size(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 856 | return 0; |
| 857 | |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 858 | if (sl->flags & HTX_SL_F_VER_11) |
| 859 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 860 | else |
| 861 | 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] | 862 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 863 | 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] | 864 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 865 | 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] | 866 | chunk_memcat(chk, "\r\n", 2); |
| 867 | |
| 868 | return 1; |
| 869 | } |
| 870 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 871 | /* Appends the H1 representation of the header block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 872 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 873 | * 0. |
| 874 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 875 | 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] | 876 | { |
| 877 | if (n.len + v.len + 4 > b_room(chk)) |
| 878 | return 0; |
| 879 | |
| 880 | chunk_memcat(chk, n.ptr, n.len); |
| 881 | chunk_memcat(chk, ": ", 2); |
| 882 | chunk_memcat(chk, v.ptr, v.len); |
| 883 | chunk_memcat(chk, "\r\n", 2); |
| 884 | |
| 885 | return 1; |
| 886 | } |
| 887 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 888 | /* Appends the H1 representation of the data block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 889 | * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It |
| 890 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 891 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 892 | 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] | 893 | { |
| 894 | if (chunked) { |
| 895 | uint32_t chksz; |
| 896 | char tmp[10]; |
| 897 | char *beg, *end; |
| 898 | |
| 899 | chksz = data.len; |
| 900 | |
| 901 | beg = end = tmp+10; |
| 902 | *--beg = '\n'; |
| 903 | *--beg = '\r'; |
| 904 | do { |
| 905 | *--beg = hextab[chksz & 0xF]; |
| 906 | } while (chksz >>= 4); |
| 907 | |
| 908 | if (data.len + (end - beg) + 2 > b_room(chk)) |
| 909 | return 0; |
| 910 | chunk_memcat(chk, beg, end - beg); |
| 911 | chunk_memcat(chk, data.ptr, data.len); |
| 912 | chunk_memcat(chk, "\r\n", 2); |
| 913 | } |
| 914 | else { |
| 915 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | return 1; |
| 920 | } |
| 921 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 922 | /* Appends the h1 representation of the trailer block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 923 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 924 | * 0. |
| 925 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 926 | int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 927 | { |
| 928 | /* FIXME: be sure the CRLF is here or remove it when inserted */ |
| 929 | if (!chunk_memcat(chk, tlr.ptr, tlr.len)) |
| 930 | return 0; |
| 931 | return 1; |
| 932 | } |