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 | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 31 | int32_t first = -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 */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 54 | if (htx->first == old) |
| 55 | first = 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 | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 68 | htx->first = first; |
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 | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 97 | htx->front = htx->head = htx->tail = htx->first = 0; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 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; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 191 | /* Set first position if not already set */ |
| 192 | if (htx->first == -1) |
| 193 | htx->first = tail; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 194 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* Adds a new block of type <type> in the HTTP message <htx>. Its content size |
| 198 | * is passed but it is the caller responsibility to do the copy. |
| 199 | */ |
| 200 | struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 201 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 202 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 203 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 204 | blk = htx_reserve_nxblk(htx, blksz); |
| 205 | if (!blk) |
| 206 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 207 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 208 | blk->info = (type << 28); |
| 209 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* Removes the block <blk> from the HTTP message <htx>. The function returns the |
| 213 | * block following <blk> or NULL if <blk> is the last block or the last |
| 214 | * inserted one. |
| 215 | */ |
| 216 | struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk) |
| 217 | { |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 218 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 219 | uint32_t next, pos, wrap; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 220 | |
Christopher Faulet | 9c66b98 | 2019-04-30 18:08:26 +0200 | [diff] [blame] | 221 | pos = htx_get_blk_pos(htx, blk); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 222 | if (type != HTX_BLK_UNUSED) { |
| 223 | /* Mark the block as unused, decrement allocated size */ |
| 224 | htx->data -= htx_get_blksz(blk); |
| 225 | blk->info = ((uint32_t)HTX_BLK_UNUSED << 28); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 226 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 227 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 228 | /* This is the last block in use */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 229 | if (htx->used == 1) { |
| 230 | htx_reset(htx); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 233 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 234 | /* There is at least 2 blocks, so tail is always >= 0 */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 235 | blk = NULL; |
| 236 | next = pos + 1; /* By default retrun the next block */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 237 | wrap = htx_get_wrap(htx); |
| 238 | if (htx->tail + 1 == wrap) { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 239 | /* The HTTP message doesn't wrap */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 240 | if (pos == htx->head) { |
| 241 | /* move the head forward */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 242 | htx->used--; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 243 | htx->head++; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 244 | } |
| 245 | else if (pos == htx->tail) { |
| 246 | /* remove the tail. this was the last inserted block so |
| 247 | * return NULL. */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 248 | htx->tail--; |
| 249 | htx->used--; |
| 250 | goto end; |
| 251 | } |
| 252 | } |
| 253 | else { |
| 254 | /* The HTTP message wraps */ |
| 255 | if (pos == htx->tail) { |
| 256 | /* remove the tail. try to unwrap the message (pos == 0) |
| 257 | * and return NULL. */ |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 258 | htx->tail = ((pos == 0) ? wrap-1 : htx->tail-1); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 259 | htx->used--; |
| 260 | goto end; |
| 261 | } |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 262 | else if (pos == htx->head) { |
| 263 | /* move the head forward and try to unwrap the message |
| 264 | * (head+1 == wrap) */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 265 | htx->used--; |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 266 | htx->head++; |
| 267 | if (htx->head == wrap) |
| 268 | htx->head = next = 0; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 271 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 272 | blk = htx_get_blk(htx, next); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 273 | end: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 274 | if (pos == htx->first) |
| 275 | htx->first = (blk ? htx_get_blk_pos(htx, blk) : -1); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 276 | if (pos == htx->front) |
| 277 | htx->front = htx_find_front(htx); |
| 278 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 281 | /* Truncate all blocks after the one containing the offset <offset>. This last |
| 282 | * one is truncated too. |
| 283 | */ |
| 284 | void htx_truncate(struct htx *htx, uint32_t offset) |
| 285 | { |
| 286 | struct htx_blk *blk; |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 287 | |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 288 | for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 289 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 290 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 291 | |
Christopher Faulet | ced3900 | 2019-05-23 11:12:43 +0200 | [diff] [blame] | 292 | if (offset >= sz) { |
| 293 | offset -= sz; |
| 294 | continue; |
| 295 | } |
| 296 | if (type == HTX_BLK_DATA || type == HTX_BLK_TLR) { |
| 297 | htx_set_blk_value_len(blk, offset); |
| 298 | htx->data -= (sz - offset); |
| 299 | } |
| 300 | offset = 0; |
Christopher Faulet | 00cf697 | 2019-01-07 14:53:27 +0100 | [diff] [blame] | 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 | |
Christopher Faulet | 0f6d6a9 | 2019-05-23 11:11:52 +0200 | [diff] [blame] | 317 | if (count == htx->data) { |
| 318 | htx_reset(htx); |
| 319 | htxret.ret = count; |
| 320 | return htxret; |
| 321 | } |
| 322 | |
Christopher Faulet | 549822f | 2019-02-25 10:23:19 +0100 | [diff] [blame] | 323 | blk = htx_get_head_blk(htx); |
| 324 | while (count && blk) { |
| 325 | uint32_t sz = htx_get_blksz(blk); |
| 326 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 327 | |
| 328 | /* Ingore unused block */ |
| 329 | if (type == HTX_BLK_UNUSED) |
| 330 | goto next; |
| 331 | |
| 332 | if (sz > count) { |
| 333 | if (type == HTX_BLK_DATA) { |
| 334 | htx_cut_data_blk(htx, blk, count); |
| 335 | htxret.ret += count; |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | count -= sz; |
| 340 | htxret.ret += sz; |
| 341 | next: |
| 342 | blk = htx_remove_blk(htx, blk); |
| 343 | } |
| 344 | htxret.blk = blk; |
| 345 | |
| 346 | return htxret; |
| 347 | } |
| 348 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 349 | /* Tries to append data to the last inserted block, if the type matches and if |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 350 | * there is enough space to take it all. If the space wraps, the buffer is |
| 351 | * defragmented and a new block is inserted. If an error occurred, NULL is |
Christopher Faulet | 6177509 | 2019-05-07 21:42:27 +0200 | [diff] [blame] | 352 | * returned. Otherwise, on success, the updated block (or the new one) is |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 353 | * returned. Due to its nature this function can be expensive and should be |
| 354 | * avoided whenever possible. |
| 355 | */ |
| 356 | struct htx_blk *htx_add_data_atonce(struct htx *htx, const struct ist data) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 357 | { |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 358 | struct htx_blk *blk, *tailblk, *headblk, *frtblk; |
| 359 | struct ist v; |
| 360 | int32_t room; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 361 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 362 | if (!htx->used) |
| 363 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 364 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 365 | /* Not enough space to store data */ |
| 366 | if (data.len > htx_free_data_space(htx)) |
| 367 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 368 | |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 369 | /* get the tail and head block */ |
| 370 | tailblk = htx_get_tail_blk(htx); |
| 371 | headblk = htx_get_head_blk(htx); |
| 372 | if (tailblk == NULL || headblk == NULL) |
| 373 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 374 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 375 | /* Don't try to append data if the last inserted block is not of the |
| 376 | * same type */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 377 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 378 | goto add_new_block; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 379 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 380 | /* |
| 381 | * Same type and enough space: append data |
| 382 | */ |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 383 | frtblk = htx_get_blk(htx, htx->front); |
| 384 | if (tailblk->addr >= headblk->addr) { |
| 385 | if (htx->tail != htx->front) |
| 386 | goto add_new_block; |
| 387 | 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] | 388 | } |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 389 | else |
| 390 | room = headblk->addr - (tailblk->addr + htx_get_blksz(tailblk)); |
| 391 | |
| 392 | if (room < (int32_t)data.len) |
| 393 | tailblk = htx_defrag(htx, tailblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 394 | |
| 395 | append_data: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 396 | /* get the value of the tail block */ |
| 397 | /* FIXME: check v.len + data.len < 256MB */ |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 398 | v = htx_get_blk_value(htx, tailblk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 399 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 400 | /* Append data and update the block itself */ |
| 401 | memcpy(v.ptr + v.len, data.ptr, data.len); |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 402 | htx_set_blk_value_len(tailblk, v.len + data.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 403 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 404 | /* Update HTTP message */ |
| 405 | htx->data += data.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 406 | |
Christopher Faulet | f1449b7 | 2019-04-10 14:54:46 +0200 | [diff] [blame] | 407 | return tailblk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 408 | |
| 409 | add_new_block: |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 410 | /* FIXME: check tlr.len (< 256MB) */ |
Willy Tarreau | d4908fa | 2019-05-28 10:23:46 +0200 | [diff] [blame] | 411 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 412 | if (!blk) |
| 413 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 414 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 415 | blk->info += data.len; |
| 416 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
| 417 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Replaces a value part of a block by a new one. The new part can be smaller or |
| 421 | * larger than the old one. This function works for any kind of block with |
| 422 | * attached data. It returns the new block on success, otherwise it returns |
| 423 | * NULL. |
| 424 | */ |
| 425 | 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] | 426 | const struct ist old, const struct ist new) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 427 | { |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 428 | struct buffer *tmp; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 429 | struct ist n, v; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 430 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 431 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 432 | n = htx_get_blk_name(htx, blk); |
| 433 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 434 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 435 | delta = new.len - old.len; |
| 436 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 437 | /* easy case, new data are smaller, so replace it in-place */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 438 | if (delta <= 0) { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 439 | memcpy(old.ptr, new.ptr, new.len); |
| 440 | if (old.len != v.len) |
| 441 | 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] | 442 | htx_set_blk_value_len(blk, v.len + delta); |
| 443 | htx->data += delta; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 444 | return blk; |
| 445 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 446 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 447 | /* we need to allocate more space to store the new header value */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 448 | if (delta > htx_free_space(htx)) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 449 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 450 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 451 | /* |
| 452 | * Copy the new header in a temp buffer |
| 453 | */ |
| 454 | tmp = get_trash_chunk(); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 455 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 456 | /* 1. copy the header name */ |
| 457 | chunk_memcat(tmp, n.ptr, n.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 458 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 459 | /* 2. copy value before old part, if any */ |
| 460 | if (old.ptr != v.ptr) |
| 461 | chunk_memcat(tmp, v.ptr, old.ptr - v.ptr); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 462 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 463 | /* 3. copy new value */ |
| 464 | chunk_memcat(tmp, new.ptr, new.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 466 | /* 4. copy value after old part if any */ |
| 467 | if (old.len != v.len) |
| 468 | 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] | 469 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 470 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 471 | /* Expand the block size. But data are not copied yet. Then defragment |
| 472 | * the HTX message. |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 473 | */ |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 474 | htx_set_blk_value_len(blk, v.len + delta); |
| 475 | htx->data += delta; |
| 476 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 477 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 478 | /* Finaly, copy data. */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 479 | memcpy(htx_get_blk_ptr(htx, blk), tmp->area, tmp->data); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 480 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 481 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* 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] | 485 | * type <mark> (typically EOH, EOD or EOM) or when <count> bytes were moved |
| 486 | * (including payload and meta-data). It returns the number of bytes moved and |
| 487 | * the last HTX block inserted in <dst>. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 488 | */ |
| 489 | struct htx_ret htx_xfer_blks(struct htx *dst, struct htx *src, uint32_t count, |
| 490 | enum htx_blk_type mark) |
| 491 | { |
| 492 | struct htx_blk *blk, *dstblk; |
| 493 | enum htx_blk_type type; |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 494 | uint32_t info, max, sz, ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 495 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 496 | ret = htx_used_space(dst); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 497 | blk = htx_get_blk(src, htx_get_head(src)); |
| 498 | dstblk = NULL; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 499 | |
| 500 | while (blk && count) { |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 501 | type = htx_get_blk_type(blk); |
| 502 | |
| 503 | /* Ingore unused block */ |
| 504 | if (type == HTX_BLK_UNUSED) |
| 505 | goto next; |
| 506 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 507 | /* Be sure to have enough space to xfer all headers in one |
| 508 | * time. If not while <dst> is empty, we report a parsing error |
| 509 | * on <src>. |
| 510 | */ |
| 511 | if (mark >= HTX_BLK_EOH && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)) { |
| 512 | struct htx_sl *sl = htx_get_blk_ptr(src, blk); |
| 513 | |
| 514 | if (sl->hdrs_bytes != -1 && sl->hdrs_bytes > count) { |
| 515 | if (htx_is_empty(dst)) |
| 516 | src->flags |= HTX_FL_PARSING_ERROR; |
| 517 | break; |
| 518 | } |
| 519 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 520 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 521 | sz = htx_get_blksz(blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 522 | info = blk->info; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 523 | max = htx_get_max_blksz(dst, count); |
| 524 | if (!max) |
| 525 | break; |
Willy Tarreau | 90caa07 | 2019-04-09 16:21:54 +0200 | [diff] [blame] | 526 | if (sz > max) { |
Christopher Faulet | 39744f7 | 2019-05-24 14:54:00 +0200 | [diff] [blame] | 527 | /* Headers must be fully copied */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 528 | if (type != HTX_BLK_DATA) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 529 | break; |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 530 | sz = max; |
| 531 | info = (type << 28) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | dstblk = htx_reserve_nxblk(dst, sz); |
| 535 | if (!dstblk) |
| 536 | break; |
| 537 | dstblk->info = info; |
| 538 | memcpy(htx_get_blk_ptr(dst, dstblk), htx_get_blk_ptr(src, blk), sz); |
| 539 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 540 | count -= sizeof(dstblk) + sz; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 541 | if (blk->info != info) { |
| 542 | /* Partial move: don't remove <blk> from <src> but |
| 543 | * resize its content */ |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 544 | htx_cut_data_blk(src, blk, sz); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 545 | break; |
| 546 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 547 | next: |
| 548 | blk = htx_remove_blk(src, blk); |
| 549 | if (type == mark) |
| 550 | break; |
| 551 | |
| 552 | } |
| 553 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 554 | end: |
| 555 | ret = htx_used_space(dst) - ret; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 556 | return (struct htx_ret){.ret = ret, .blk = dstblk}; |
| 557 | } |
| 558 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 559 | /* Replaces an header by a new one. The new header can be smaller or larger than |
| 560 | * 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] | 561 | * The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 562 | */ |
| 563 | 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] | 564 | const struct ist name, const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 565 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 566 | enum htx_blk_type type; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 567 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 568 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 569 | type = htx_get_blk_type(blk); |
| 570 | if (type != HTX_BLK_HDR) |
| 571 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 572 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 573 | delta = name.len + value.len - htx_get_blksz(blk); |
| 574 | |
| 575 | /* easy case, new value is smaller, so replace it in-place */ |
| 576 | if (delta <= 0) { |
| 577 | blk->info = (type << 28) + (value.len << 8) + name.len; |
| 578 | htx->data += delta; |
| 579 | goto copy; |
| 580 | } |
| 581 | |
| 582 | /* we need to allocate more space to store the new value */ |
| 583 | if (delta > htx_free_space(htx)) |
| 584 | return NULL; /* not enough space */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 585 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 586 | /* Expand the block size. But data are not copied yet. Then defragment |
| 587 | * the HTX message. |
| 588 | */ |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 589 | blk->info = (type << 28) + (value.len << 8) + name.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 590 | htx->data += delta; |
| 591 | blk = htx_defrag(htx, blk); |
| 592 | |
| 593 | copy: |
| 594 | /* Finaly, copy data. */ |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 595 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 596 | 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] | 597 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 600 | /* Replaces the parts of the start-line. It returns the new start-line on |
| 601 | * success, otherwise it returns NULL. It is the caller responsibility to update |
| 602 | * sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 603 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 604 | struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const struct ist p1, |
| 605 | const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 606 | { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 607 | struct htx_sl *sl; |
| 608 | struct htx_sl tmp; /* used to save sl->info and sl->flags */ |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 609 | enum htx_blk_type type; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 610 | uint32_t size; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 611 | int32_t delta; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 612 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 613 | type = htx_get_blk_type(blk); |
Willy Tarreau | c706cd7 | 2018-12-07 17:12:22 +0100 | [diff] [blame] | 614 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 615 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 616 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 617 | /* Save start-line info and flags */ |
| 618 | sl = htx_get_blk_ptr(htx, blk); |
| 619 | tmp.info = sl->info; |
| 620 | tmp.flags = sl->flags; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 621 | |
| 622 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 623 | delta = size - htx_get_blksz(blk); |
| 624 | |
| 625 | /* easy case, new data are smaller, so replace it in-place */ |
| 626 | if (delta <= 0) { |
| 627 | htx_set_blk_value_len(blk, size); |
| 628 | htx->data += delta; |
| 629 | goto copy; |
| 630 | } |
| 631 | |
| 632 | /* we need to allocate more space to store the new value */ |
| 633 | if (delta > htx_free_space(htx)) |
| 634 | return NULL; /* not enough space */ |
| 635 | |
| 636 | /* Expand the block size. But data are not copied yet. Then defragment |
| 637 | * the HTX message. |
| 638 | */ |
| 639 | htx_set_blk_value_len(blk, size); |
| 640 | htx->data += delta; |
| 641 | blk = htx_defrag(htx, blk); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 642 | |
Christopher Faulet | e97f3ba | 2018-12-10 15:39:40 +0100 | [diff] [blame] | 643 | copy: |
| 644 | /* 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] | 645 | sl = htx_get_blk_ptr(htx, blk); |
| 646 | sl->info = tmp.info; |
| 647 | sl->flags = tmp.flags; |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 648 | if (sl->hdrs_bytes != -1) |
| 649 | sl->hdrs_bytes += delta; |
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 | HTX_SL_P1_LEN(sl) = p1.len; |
| 652 | HTX_SL_P2_LEN(sl) = p2.len; |
| 653 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | 54483df | 2018-11-26 15:05:52 +0100 | [diff] [blame] | 654 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 655 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 656 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 657 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 658 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 659 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 662 | /* Add a new start-line. It returns it on success, otherwise it returns NULL. It |
| 663 | * is the caller responsibility to set sl->info, if necessary. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 664 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 665 | struct htx_sl *htx_add_stline(struct htx *htx, enum htx_blk_type type, unsigned int flags, |
| 666 | const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 667 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 668 | struct htx_blk *blk; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 669 | struct htx_sl *sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 670 | uint32_t size; |
| 671 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 672 | if (type != HTX_BLK_REQ_SL && type != HTX_BLK_RES_SL) |
| 673 | return NULL; |
| 674 | |
| 675 | size = sizeof(*sl) + p1.len + p2.len + p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 676 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 677 | /* FIXME: check size (< 256MB) */ |
| 678 | blk = htx_add_blk(htx, type, size); |
| 679 | if (!blk) |
| 680 | return NULL; |
| 681 | blk->info += size; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 682 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 683 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 05c083c | 2019-05-15 14:56:47 +0200 | [diff] [blame] | 684 | sl->hdrs_bytes = -1; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 685 | sl->flags = flags; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 686 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 687 | HTX_SL_P1_LEN(sl) = p1.len; |
| 688 | HTX_SL_P2_LEN(sl) = p2.len; |
| 689 | HTX_SL_P3_LEN(sl) = p3.len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 690 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 691 | memcpy(HTX_SL_P1_PTR(sl), p1.ptr, p1.len); |
| 692 | memcpy(HTX_SL_P2_PTR(sl), p2.ptr, p2.len); |
| 693 | memcpy(HTX_SL_P3_PTR(sl), p3.ptr, p3.len); |
| 694 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 695 | return sl; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* 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] | 699 | * success. Otherwise, it returns NULL. The header name is always lower cased. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 700 | */ |
| 701 | 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] | 702 | const struct ist value) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 703 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 704 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 705 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 706 | /* FIXME: check name.len (< 256B) and value.len (< 1MB) */ |
| 707 | blk = htx_add_blk(htx, HTX_BLK_HDR, name.len + value.len); |
| 708 | if (!blk) |
| 709 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 710 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 711 | blk->info += (value.len << 8) + name.len; |
Willy Tarreau | ed00e34 | 2018-12-07 08:47:45 +0100 | [diff] [blame] | 712 | ist2bin_lc(htx_get_blk_ptr(htx, blk), name); |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 713 | memcpy(htx_get_blk_ptr(htx, blk) + name.len, value.ptr, value.len); |
| 714 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Willy Tarreau | 52610e9 | 2019-01-03 18:26:17 +0100 | [diff] [blame] | 717 | /* Adds an HTX block of type <type> in <htx>, of size <blksz>. It returns the |
| 718 | * new block on success. Otherwise, it returns NULL. The caller is responsible |
| 719 | * for filling the block itself. |
| 720 | */ |
| 721 | struct htx_blk *htx_add_blk_type_size(struct htx *htx, enum htx_blk_type type, uint32_t blksz) |
| 722 | { |
| 723 | struct htx_blk *blk; |
| 724 | |
| 725 | blk = htx_add_blk(htx, type, blksz); |
| 726 | if (!blk) |
| 727 | return NULL; |
| 728 | |
| 729 | blk->info += blksz; |
| 730 | return blk; |
| 731 | } |
| 732 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 733 | struct htx_blk *htx_add_all_headers(struct htx *htx, const struct http_hdr *hdrs) |
| 734 | { |
| 735 | int i; |
| 736 | |
| 737 | for (i = 0; hdrs[i].n.len; i++) { |
| 738 | if (!htx_add_header(htx, hdrs[i].n, hdrs[i].v)) |
| 739 | return NULL; |
| 740 | } |
| 741 | return htx_add_endof(htx, HTX_BLK_EOH); |
| 742 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 743 | |
| 744 | /* Adds an HTX block of type EOH,EOD or EOM in <htx>. It returns the new block |
| 745 | * on success. Otherwise, it returns NULL. |
| 746 | */ |
| 747 | struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) |
| 748 | { |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 749 | struct htx_blk *blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 750 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 751 | blk = htx_add_blk(htx, type, 1); |
| 752 | if (!blk) |
| 753 | return NULL; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 754 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 755 | blk->info += 1; |
| 756 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | |
| 760 | /* Adds an HTX block of type DATA in <htx>. It first tries to append data if |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 761 | * possible. It returns the number of bytes consumed from <data>, which may be |
| 762 | * zero if nothing could be copied. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 763 | */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 764 | size_t htx_add_data(struct htx *htx, const struct ist data) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 765 | { |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 766 | struct htx_blk *blk, *tailblk, *headblk, *frtblk; |
| 767 | struct ist v; |
| 768 | int32_t room; |
| 769 | int32_t len = data.len; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 770 | |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 771 | if (!htx->used) |
| 772 | goto add_new_block; |
| 773 | |
| 774 | /* Not enough space to store data */ |
| 775 | if (len > htx_free_data_space(htx)) |
| 776 | len = htx_free_data_space(htx); |
| 777 | |
| 778 | if (!len) |
| 779 | return 0; |
| 780 | |
| 781 | /* get the tail and head block */ |
| 782 | tailblk = htx_get_tail_blk(htx); |
| 783 | headblk = htx_get_head_blk(htx); |
| 784 | if (tailblk == NULL || headblk == NULL) |
| 785 | goto add_new_block; |
| 786 | |
| 787 | /* Don't try to append data if the last inserted block is not of the |
| 788 | * same type */ |
| 789 | if (htx_get_blk_type(tailblk) != HTX_BLK_DATA) |
| 790 | goto add_new_block; |
| 791 | |
| 792 | /* |
| 793 | * Same type and enough space: append data |
| 794 | */ |
| 795 | frtblk = htx_get_blk(htx, htx->front); |
| 796 | if (tailblk->addr >= headblk->addr) { |
| 797 | if (htx->tail != htx->front) |
| 798 | goto add_new_block; |
| 799 | room = sizeof(htx->blocks[0]) * htx_pos_to_idx(htx, htx->tail) - (frtblk->addr + htx_get_blksz(frtblk)); |
| 800 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 801 | else |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 802 | room = headblk->addr - (tailblk->addr + htx_get_blksz(tailblk)); |
| 803 | |
| 804 | if (room < len) |
| 805 | len = room; |
| 806 | |
| 807 | append_data: |
| 808 | /* get the value of the tail block */ |
| 809 | /* FIXME: check v.len + len < 256MB */ |
| 810 | v = htx_get_blk_value(htx, tailblk); |
| 811 | |
| 812 | /* Append data and update the block itself */ |
| 813 | memcpy(v.ptr + v.len, data.ptr, len); |
| 814 | htx_set_blk_value_len(tailblk, v.len + len); |
| 815 | |
| 816 | /* Update HTTP message */ |
| 817 | htx->data += len; |
| 818 | return len; |
| 819 | |
| 820 | add_new_block: |
| 821 | /* FIXME: check tlr.len (< 256MB) */ |
| 822 | blk = htx_add_blk(htx, HTX_BLK_DATA, len); |
| 823 | if (!blk) |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 824 | return 0; |
Willy Tarreau | 0350b90 | 2019-05-28 10:58:50 +0200 | [diff] [blame] | 825 | |
| 826 | blk->info += len; |
| 827 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, len); |
| 828 | return len; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Christopher Faulet | 6177509 | 2019-05-07 21:42:27 +0200 | [diff] [blame] | 831 | /* Adds an HTX block of type TLR in <htx>. It returns the new block on |
| 832 | * success. Otherwise, it returns NULL. |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 833 | */ |
| 834 | struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr) |
| 835 | { |
Christopher Faulet | 6177509 | 2019-05-07 21:42:27 +0200 | [diff] [blame] | 836 | struct htx_blk *blk; |
| 837 | |
| 838 | /* FIXME: check tlr.len (< 256MB) */ |
| 839 | blk = htx_add_blk(htx, HTX_BLK_TLR, tlr.len); |
| 840 | if (!blk) |
| 841 | return NULL; |
| 842 | |
| 843 | blk->info += tlr.len; |
| 844 | memcpy(htx_get_blk_ptr(htx, blk), tlr.ptr, tlr.len); |
| 845 | return blk; |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 848 | struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, |
| 849 | const struct ist data) |
| 850 | { |
| 851 | struct htx_blk *blk; |
| 852 | int32_t prev; |
| 853 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 854 | /* FIXME: check data.len (< 256MB) */ |
| 855 | blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); |
| 856 | if (!blk) |
| 857 | return NULL; |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 858 | |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 859 | blk->info += data.len; |
| 860 | memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 861 | |
| 862 | for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) { |
| 863 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 864 | |
| 865 | /* Swap .addr and .info fields */ |
| 866 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 867 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 868 | |
| 869 | if (blk->addr == pblk->addr) |
| 870 | blk->addr += htx_get_blksz(pblk); |
| 871 | htx->front = prev; |
| 872 | |
| 873 | if (pblk == ref) |
| 874 | break; |
| 875 | blk = pblk; |
| 876 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 877 | |
| 878 | if (htx_get_blk_pos(htx, blk) != htx->front) |
| 879 | htx_defrag(htx, NULL); |
| 880 | |
Christopher Faulet | 24ed835 | 2018-11-22 11:20:43 +0100 | [diff] [blame] | 881 | return blk; |
| 882 | } |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 883 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 884 | /* Appends the H1 representation of the request line block <blk> to the |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 885 | * chunk <chk>. It returns 1 if data are successfully appended, otherwise it |
| 886 | * returns 0. |
| 887 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 888 | 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] | 889 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 890 | if (HTX_SL_LEN(sl) + 4 > b_room(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 891 | return 0; |
| 892 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 893 | 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] | 894 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 895 | 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] | 896 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 897 | if (sl->flags & HTX_SL_F_VER_11) |
| 898 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 899 | else |
| 900 | chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 901 | |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 902 | chunk_memcat(chk, "\r\n", 2); |
| 903 | |
| 904 | return 1; |
| 905 | } |
| 906 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 907 | /* 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] | 908 | * <chk>. It returns 1 if data are successfully appended, otherwise it |
| 909 | * returns 0. |
| 910 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 911 | 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] | 912 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 913 | if (HTX_SL_LEN(sl) + 4 > b_size(chk)) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 914 | return 0; |
| 915 | |
Christopher Faulet | 1e7af46 | 2018-12-03 14:05:01 +0100 | [diff] [blame] | 916 | if (sl->flags & HTX_SL_F_VER_11) |
| 917 | chunk_memcat(chk, "HTTP/1.1", 8); |
| 918 | else |
| 919 | 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] | 920 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 921 | 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] | 922 | chunk_memcat(chk, " ", 1); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 923 | 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] | 924 | chunk_memcat(chk, "\r\n", 2); |
| 925 | |
| 926 | return 1; |
| 927 | } |
| 928 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 929 | /* Appends the H1 representation of the header block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 930 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 931 | * 0. |
| 932 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 933 | 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] | 934 | { |
| 935 | if (n.len + v.len + 4 > b_room(chk)) |
| 936 | return 0; |
| 937 | |
| 938 | chunk_memcat(chk, n.ptr, n.len); |
| 939 | chunk_memcat(chk, ": ", 2); |
| 940 | chunk_memcat(chk, v.ptr, v.len); |
| 941 | chunk_memcat(chk, "\r\n", 2); |
| 942 | |
| 943 | return 1; |
| 944 | } |
| 945 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 946 | /* Appends the H1 representation of the data block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 947 | * <chk>. If <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It |
| 948 | * returns 1 if data are successfully appended, otherwise it returns 0. |
| 949 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 950 | 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] | 951 | { |
| 952 | if (chunked) { |
| 953 | uint32_t chksz; |
| 954 | char tmp[10]; |
| 955 | char *beg, *end; |
| 956 | |
| 957 | chksz = data.len; |
| 958 | |
| 959 | beg = end = tmp+10; |
| 960 | *--beg = '\n'; |
| 961 | *--beg = '\r'; |
| 962 | do { |
| 963 | *--beg = hextab[chksz & 0xF]; |
| 964 | } while (chksz >>= 4); |
| 965 | |
| 966 | if (data.len + (end - beg) + 2 > b_room(chk)) |
| 967 | return 0; |
| 968 | chunk_memcat(chk, beg, end - beg); |
| 969 | chunk_memcat(chk, data.ptr, data.len); |
| 970 | chunk_memcat(chk, "\r\n", 2); |
| 971 | } |
| 972 | else { |
| 973 | if (!chunk_memcat(chk, data.ptr, data.len)) |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | return 1; |
| 978 | } |
| 979 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 980 | /* Appends the h1 representation of the trailer block <blk> to the chunk |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 981 | * <chk>. It returns 1 if data are successfully appended, otherwise it returns |
| 982 | * 0. |
| 983 | */ |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 984 | int htx_trailer_to_h1(const struct ist tlr, struct buffer *chk) |
Christopher Faulet | a3d2a16 | 2018-10-22 08:59:39 +0200 | [diff] [blame] | 985 | { |
| 986 | /* FIXME: be sure the CRLF is here or remove it when inserted */ |
| 987 | if (!chunk_memcat(chk, tlr.ptr, tlr.len)) |
| 988 | return 0; |
| 989 | return 1; |
| 990 | } |