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