Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions to manipulate HTTP messages using the internal representation. |
| 3 | * |
| 4 | * Copyright (C) 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 | */ |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include <types/global.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 18 | |
| 19 | #include <common/config.h> |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 20 | #include <common/debug.h> |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 21 | #include <common/cfgparse.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 22 | #include <common/h1.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 23 | #include <common/http.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 24 | #include <common/htx.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 25 | |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 26 | #include <proto/arg.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 27 | #include <proto/http_htx.h> |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 28 | #include <proto/http_fetch.h> |
| 29 | #include <proto/sample.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 30 | |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 31 | struct buffer http_err_chunks[HTTP_ERR_SIZE]; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 32 | struct eb_root http_error_messages = EB_ROOT; |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 33 | struct list http_errors_list = LIST_HEAD_INIT(http_errors_list); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 34 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 35 | /* The declaration of an errorfiles/errorfile directives. Used during config |
| 36 | * parsing only. */ |
| 37 | struct conf_errors { |
| 38 | char type; /* directive type (0: errorfiles, 1: errorfile) */ |
| 39 | union { |
| 40 | struct { |
| 41 | int status; /* the status code associated to this error */ |
| 42 | struct buffer *msg; /* the HTX error message */ |
| 43 | } errorfile; /* describe an "errorfile" directive */ |
| 44 | struct { |
| 45 | char *name; /* the http-errors section name */ |
| 46 | char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */ |
| 47 | } errorfiles; /* describe an "errorfiles" directive */ |
| 48 | } info; |
| 49 | |
| 50 | char *file; /* file where the directive appears */ |
| 51 | int line; /* line where the directive appears */ |
| 52 | |
| 53 | struct list list; /* next conf_errors */ |
| 54 | }; |
| 55 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 56 | static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host); |
| 57 | static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri); |
| 58 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 59 | /* Returns the next unporocessed start line in the HTX message. It returns NULL |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 60 | * if the start-line is undefined (first == -1). Otherwise, it returns the |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 61 | * pointer on the htx_sl structure. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 62 | */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 63 | struct htx_sl *http_get_stline(struct htx *htx) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 64 | { |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 65 | struct htx_blk *blk; |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 66 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 67 | BUG_ON(htx->first == -1); |
| 68 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 69 | if (!blk) |
| 70 | return NULL; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 71 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 72 | return htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* Finds the first or next occurrence of header <name> in the HTX message <htx> |
| 76 | * using the context <ctx>. This structure holds everything necessary to use the |
| 77 | * header and find next occurrence. If its <blk> member is NULL, the header is |
| 78 | * searched from the beginning. Otherwise, the next occurrence is returned. The |
| 79 | * function returns 1 when it finds a value, and 0 when there is no more. It is |
| 80 | * designed to work with headers defined as comma-separated lists. If <full> is |
| 81 | * set, it works on full-line headers in whose comma is not a delimiter but is |
| 82 | * part of the syntax. A special case, if ctx->value is NULL when searching for |
| 83 | * a new values of a header, the current header is rescanned. This allows |
| 84 | * rescanning after a header deletion. |
| 85 | */ |
| 86 | int http_find_header(const struct htx *htx, const struct ist name, |
| 87 | struct http_hdr_ctx *ctx, int full) |
| 88 | { |
| 89 | struct htx_blk *blk = ctx->blk; |
| 90 | struct ist n, v; |
| 91 | enum htx_blk_type type; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 92 | |
| 93 | if (blk) { |
| 94 | char *p; |
| 95 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 96 | if (!ctx->value.ptr) |
| 97 | goto rescan_hdr; |
| 98 | if (full) |
| 99 | goto next_blk; |
| 100 | v = htx_get_blk_value(htx, blk); |
| 101 | p = ctx->value.ptr + ctx->value.len + ctx->lws_after; |
| 102 | v.len -= (p - v.ptr); |
| 103 | v.ptr = p; |
| 104 | if (!v.len) |
| 105 | goto next_blk; |
| 106 | /* Skip comma */ |
| 107 | if (*(v.ptr) == ',') { |
| 108 | v.ptr++; |
| 109 | v.len--; |
| 110 | } |
| 111 | |
| 112 | goto return_hdr; |
| 113 | } |
| 114 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 115 | if (htx_is_empty(htx)) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 118 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 119 | rescan_hdr: |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 120 | type = htx_get_blk_type(blk); |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 121 | if (type == HTX_BLK_EOH || type == HTX_BLK_EOM) |
| 122 | break; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 123 | if (type != HTX_BLK_HDR) |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 124 | continue; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 125 | if (name.len) { |
| 126 | /* If no name was passed, we want any header. So skip the comparison */ |
| 127 | n = htx_get_blk_name(htx, blk); |
| 128 | if (!isteqi(n, name)) |
| 129 | goto next_blk; |
| 130 | } |
| 131 | v = htx_get_blk_value(htx, blk); |
| 132 | |
| 133 | return_hdr: |
| 134 | ctx->lws_before = 0; |
| 135 | ctx->lws_after = 0; |
| 136 | while (v.len && HTTP_IS_LWS(*v.ptr)) { |
| 137 | v.ptr++; |
| 138 | v.len--; |
| 139 | ctx->lws_before++; |
| 140 | } |
| 141 | if (!full) |
| 142 | v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr; |
| 143 | while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) { |
| 144 | v.len--; |
| 145 | ctx->lws_after++; |
| 146 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 147 | ctx->blk = blk; |
| 148 | ctx->value = v; |
| 149 | return 1; |
| 150 | |
| 151 | next_blk: |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 152 | ; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | ctx->blk = NULL; |
| 156 | ctx->value = ist(""); |
| 157 | ctx->lws_before = ctx->lws_after = 0; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* Adds a header block int the HTX message <htx>, just before the EOH block. It |
| 162 | * returns 1 on success, otherwise it returns 0. |
| 163 | */ |
| 164 | int http_add_header(struct htx *htx, const struct ist n, const struct ist v) |
| 165 | { |
| 166 | struct htx_blk *blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 167 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 168 | enum htx_blk_type type = htx_get_tail_type(htx); |
| 169 | int32_t prev; |
| 170 | |
| 171 | blk = htx_add_header(htx, n, v); |
| 172 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 173 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 174 | |
| 175 | if (unlikely(type < HTX_BLK_EOH)) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 176 | goto end; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 177 | |
| 178 | /* <blk> is the head, swap it iteratively with its predecessor to place |
| 179 | * it just before the end-of-header block. So blocks remains ordered. */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 180 | for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 181 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 182 | enum htx_blk_type type = htx_get_blk_type(pblk); |
| 183 | |
| 184 | /* Swap .addr and .info fields */ |
| 185 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 186 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 187 | |
| 188 | if (blk->addr == pblk->addr) |
| 189 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 190 | |
| 191 | /* Stop when end-of-header is reached */ |
| 192 | if (type == HTX_BLK_EOH) |
| 193 | break; |
| 194 | |
| 195 | blk = pblk; |
| 196 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 197 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 198 | end: |
| 199 | sl = http_get_stline(htx); |
| 200 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(n, ist("host"))) { |
| 201 | if (!http_update_authority(htx, sl, v)) |
| 202 | goto fail; |
| 203 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 204 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 205 | |
| 206 | fail: |
| 207 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 210 | /* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 211 | * success, otherwise it returns 0. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 212 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 213 | int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 214 | { |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 215 | struct htx_blk *blk; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 216 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 217 | blk = htx_get_first_blk(htx); |
| 218 | if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3)) |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | return 1; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 223 | /* Replace the request method in the HTX message <htx> by <meth>. It returns 1 |
| 224 | * on success, otherwise 0. |
| 225 | */ |
| 226 | int http_replace_req_meth(struct htx *htx, const struct ist meth) |
| 227 | { |
| 228 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 229 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 230 | struct ist uri, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 232 | if (!sl) |
| 233 | return 0; |
| 234 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 235 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 236 | chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */ |
| 237 | uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 238 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 239 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 240 | vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 241 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 242 | /* create the new start line */ |
| 243 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 244 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on |
| 248 | * success, otherwise 0. |
| 249 | */ |
| 250 | int http_replace_req_uri(struct htx *htx, const struct ist uri) |
| 251 | { |
| 252 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 253 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 254 | struct ist meth, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 255 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 256 | if (!sl) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 257 | goto fail; |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 258 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 259 | /* Start by copying old method and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 260 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 261 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 262 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 263 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 264 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 265 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 266 | /* create the new start line */ |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 267 | if (!http_replace_stline(htx, meth, uri, vsn)) |
| 268 | goto fail; |
| 269 | |
| 270 | sl = http_get_stline(htx); |
| 271 | if (!http_update_host(htx, sl, uri)) |
| 272 | goto fail; |
| 273 | |
| 274 | return 1; |
| 275 | fail: |
| 276 | return 0; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Replace the request path in the HTX message <htx> by <path>. The host part |
| 280 | * and the query string are preserved. It returns 1 on success, otherwise 0. |
| 281 | */ |
| 282 | int http_replace_req_path(struct htx *htx, const struct ist path) |
| 283 | { |
| 284 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 285 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 286 | struct ist meth, uri, vsn, p; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 287 | size_t plen = 0; |
| 288 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 289 | if (!sl) |
| 290 | return 0; |
| 291 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 292 | uri = htx_sl_req_uri(sl); |
| 293 | p = http_get_path(uri); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 294 | if (!p.ptr) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 295 | p = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 296 | while (plen < p.len && *(p.ptr + plen) != '?') |
| 297 | plen++; |
| 298 | |
| 299 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 300 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 301 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 302 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 303 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 304 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 305 | |
| 306 | chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */ |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 307 | chunk_memcat(temp, path.ptr, path.len); /* uri: new path */ |
| 308 | chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 309 | uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 310 | |
| 311 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 312 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* Replace the request query-string in the HTX message <htx> by <query>. The |
| 316 | * host part and the path are preserved. It returns 1 on success, otherwise |
| 317 | * 0. |
| 318 | */ |
| 319 | int http_replace_req_query(struct htx *htx, const struct ist query) |
| 320 | { |
| 321 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 322 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 323 | struct ist meth, uri, vsn, q; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 324 | int offset = 1; |
| 325 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 326 | if (!sl) |
| 327 | return 0; |
| 328 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 329 | uri = htx_sl_req_uri(sl); |
| 330 | q = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 331 | while (q.len > 0 && *(q.ptr) != '?') { |
| 332 | q.ptr++; |
| 333 | q.len--; |
| 334 | } |
| 335 | |
| 336 | /* skip the question mark or indicate that we must insert it |
| 337 | * (but only if the format string is not empty then). |
| 338 | */ |
| 339 | if (q.len) { |
| 340 | q.ptr++; |
| 341 | q.len--; |
| 342 | } |
| 343 | else if (query.len > 1) |
| 344 | offset = 0; |
| 345 | |
| 346 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 347 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 348 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 349 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 350 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 351 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 352 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 353 | chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */ |
| 354 | chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */ |
| 355 | uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 356 | |
| 357 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 358 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* Replace the response status in the HTX message <htx> by <status>. It returns |
| 362 | * 1 on success, otherwise 0. |
| 363 | */ |
| 364 | int http_replace_res_status(struct htx *htx, const struct ist status) |
| 365 | { |
| 366 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 367 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 368 | struct ist vsn, reason; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 369 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 370 | if (!sl) |
| 371 | return 0; |
| 372 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 373 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 374 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 375 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 376 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 377 | chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */ |
| 378 | reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 379 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 380 | /* create the new start line */ |
| 381 | sl->info.res.status = strl2ui(status.ptr, status.len); |
| 382 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Replace the response reason in the HTX message <htx> by <reason>. It returns |
| 386 | * 1 on success, otherwise 0. |
| 387 | */ |
| 388 | int http_replace_res_reason(struct htx *htx, const struct ist reason) |
| 389 | { |
| 390 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 391 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 392 | struct ist vsn, status; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 393 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 394 | if (!sl) |
| 395 | return 0; |
| 396 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 397 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 398 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 399 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 400 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 401 | chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */ |
| 402 | status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 403 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 404 | /* create the new start line */ |
| 405 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 408 | /* Replaces a part of a header value referenced in the context <ctx> by |
| 409 | * <data>. It returns 1 on success, otherwise it returns 0. The context is |
| 410 | * updated if necessary. |
| 411 | */ |
| 412 | int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data) |
| 413 | { |
| 414 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 415 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 416 | char *start; |
| 417 | struct ist v; |
| 418 | uint32_t len, off; |
| 419 | |
| 420 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 421 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 422 | |
| 423 | v = htx_get_blk_value(htx, blk); |
| 424 | start = ctx->value.ptr - ctx->lws_before; |
| 425 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 426 | off = start - v.ptr; |
| 427 | |
| 428 | blk = htx_replace_blk_value(htx, blk, ist2(start, len), data); |
| 429 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 430 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 431 | |
| 432 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 433 | |
| 434 | sl = http_get_stline(htx); |
| 435 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
| 436 | struct ist n = htx_get_blk_name(htx, blk); |
| 437 | |
| 438 | if (isteq(n, ist("host"))) { |
| 439 | if (!http_update_authority(htx, sl, v)) |
| 440 | goto fail; |
| 441 | ctx->blk = NULL; |
| 442 | http_find_header(htx, ist("host"), ctx, 1); |
| 443 | blk = ctx->blk; |
| 444 | v = htx_get_blk_value(htx, blk); |
| 445 | } |
| 446 | } |
| 447 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 448 | ctx->blk = blk; |
| 449 | ctx->value.ptr = v.ptr + off; |
| 450 | ctx->value.len = data.len; |
| 451 | ctx->lws_before = ctx->lws_after = 0; |
| 452 | |
| 453 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 454 | fail: |
| 455 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Fully replaces a header referenced in the context <ctx> by the name <name> |
| 459 | * with the value <value>. It returns 1 on success, otherwise it returns 0. The |
| 460 | * context is updated if necessary. |
| 461 | */ |
| 462 | int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx, |
| 463 | const struct ist name, const struct ist value) |
| 464 | { |
| 465 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 466 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 467 | |
| 468 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 469 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 470 | |
| 471 | blk = htx_replace_header(htx, blk, name, value); |
| 472 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 473 | goto fail; |
| 474 | |
| 475 | sl = http_get_stline(htx); |
| 476 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(name, ist("host"))) { |
| 477 | if (!http_update_authority(htx, sl, value)) |
| 478 | goto fail; |
| 479 | ctx->blk = NULL; |
| 480 | http_find_header(htx, ist("host"), ctx, 1); |
| 481 | blk = ctx->blk; |
| 482 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 483 | |
| 484 | ctx->blk = blk; |
| 485 | ctx->value = ist(NULL); |
| 486 | ctx->lws_before = ctx->lws_after = 0; |
| 487 | |
| 488 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 489 | fail: |
| 490 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* Remove one value of a header. This only works on a <ctx> returned by |
| 494 | * http_find_header function. The value is removed, as well as surrounding commas |
| 495 | * if any. If the removed value was alone, the whole header is removed. The |
| 496 | * <ctx> is always updated accordingly, as well as the HTX message <htx>. It |
| 497 | * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a |
| 498 | * form that can be handled by http_find_header() to find next occurrence. |
| 499 | */ |
| 500 | int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx) |
| 501 | { |
| 502 | struct htx_blk *blk = ctx->blk; |
| 503 | char *start; |
| 504 | struct ist v; |
| 505 | uint32_t len; |
| 506 | |
| 507 | if (!blk) |
| 508 | return 0; |
| 509 | |
| 510 | start = ctx->value.ptr - ctx->lws_before; |
| 511 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 512 | |
| 513 | v = htx_get_blk_value(htx, blk); |
| 514 | if (len == v.len) { |
| 515 | blk = htx_remove_blk(htx, blk); |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 516 | if (blk || htx_is_empty(htx)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 517 | ctx->blk = blk; |
| 518 | ctx->value = ist2(NULL, 0); |
| 519 | ctx->lws_before = ctx->lws_after = 0; |
| 520 | } |
| 521 | else { |
| 522 | ctx->blk = htx_get_blk(htx, htx->tail); |
| 523 | ctx->value = htx_get_blk_value(htx, ctx->blk); |
| 524 | ctx->lws_before = ctx->lws_after = 0; |
| 525 | } |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | /* This was not the only value of this header. We have to remove the |
| 530 | * part pointed by ctx->value. If it is the last entry of the list, we |
| 531 | * remove the last separator. |
| 532 | */ |
| 533 | if (start == v.ptr) { |
| 534 | /* It's the first header part but not the only one. So remove |
| 535 | * the comma after it. */ |
| 536 | len++; |
| 537 | } |
| 538 | else { |
| 539 | /* There is at least one header part before the removed one. So |
| 540 | * remove the comma between them. */ |
| 541 | start--; |
| 542 | len++; |
| 543 | } |
| 544 | /* Update the block content and its len */ |
| 545 | memmove(start, start+len, v.len-len); |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 546 | htx_change_blk_value_len(htx, blk, v.len-len); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 547 | |
| 548 | /* Finally update the ctx */ |
| 549 | ctx->value.ptr = start; |
| 550 | ctx->value.len = 0; |
| 551 | ctx->lws_before = ctx->lws_after = 0; |
| 552 | |
| 553 | return 1; |
| 554 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 555 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 556 | /* Updates the authority part of the uri with the value <host>. It happens when |
| 557 | * the header host is modified. It returns 0 on failure and 1 on success. It is |
| 558 | * the caller responsibility to provide the start-line and to be sure the uri |
| 559 | * contains an authority. Thus, if no authority is found in the uri, an error is |
| 560 | * returned. |
| 561 | */ |
| 562 | static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host) |
| 563 | { |
| 564 | struct buffer *temp = get_trash_chunk(); |
| 565 | struct ist meth, vsn, uri, authority; |
| 566 | |
| 567 | uri = htx_sl_req_uri(sl); |
| 568 | authority = http_get_authority(uri, 1); |
| 569 | if (!authority.len || isteq(host, authority)) |
| 570 | return 0; |
| 571 | |
| 572 | /* Start by copying old method and version */ |
| 573 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 574 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
| 575 | |
| 576 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 577 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 578 | |
| 579 | chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr); |
| 580 | chunk_memcat(temp, host.ptr, host.len); |
| 581 | chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len)); |
| 582 | uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */ |
| 583 | |
| 584 | return http_replace_stline(htx, meth, uri, vsn); |
| 585 | |
| 586 | } |
| 587 | |
| 588 | /* Update the header host by extracting the authority of the uri <uri>. flags of |
| 589 | * the start-line are also updated accordingly. For orgin-form and asterisk-form |
| 590 | * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is |
| 591 | * removed from the flags of the start-line. Otherwise, this flag is set and the |
| 592 | * authority is used to set the value of the header host. This function returns |
| 593 | * 0 on failure and 1 on success. |
| 594 | */ |
| 595 | static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri) |
| 596 | { |
| 597 | struct ist authority; |
| 598 | struct http_hdr_ctx ctx; |
| 599 | |
| 600 | if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') { |
| 601 | // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4) |
| 602 | sl->flags &= ~HTX_SL_F_HAS_AUTHORITY; |
| 603 | } |
| 604 | else { |
| 605 | sl->flags |= HTX_SL_F_HAS_AUTHORITY; |
| 606 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 607 | // absolute-form (RFC7320 #5.3.2) |
| 608 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 609 | if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h') |
| 610 | sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
| 611 | |
| 612 | authority = http_get_authority(uri, 1); |
| 613 | if (!authority.len) |
| 614 | goto fail; |
| 615 | } |
| 616 | else { |
| 617 | // authority-form (RFC7320 #5.3.3) |
| 618 | authority = uri; |
| 619 | } |
| 620 | |
| 621 | /* Replace header host value */ |
| 622 | ctx.blk = NULL; |
| 623 | while (http_find_header(htx, ist("host"), &ctx, 1)) { |
| 624 | if (!http_replace_header_value(htx, &ctx, authority)) |
| 625 | goto fail; |
| 626 | } |
| 627 | |
| 628 | } |
| 629 | return 1; |
| 630 | fail: |
| 631 | return 0; |
| 632 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 633 | |
| 634 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 635 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 636 | * performed over the whole headers. Otherwise it must contain a valid header |
| 637 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 638 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 639 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 640 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 641 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 642 | * list headers. |
| 643 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 644 | */ |
| 645 | unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr, |
| 646 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 647 | { |
| 648 | struct http_hdr_ctx local_ctx; |
| 649 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 650 | unsigned int hist_idx; |
| 651 | int found; |
| 652 | |
| 653 | if (!ctx) { |
| 654 | local_ctx.blk = NULL; |
| 655 | ctx = &local_ctx; |
| 656 | } |
| 657 | |
| 658 | if (occ >= 0) { |
| 659 | /* search from the beginning */ |
| 660 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 661 | occ--; |
| 662 | if (occ <= 0) { |
| 663 | *vptr = ctx->value.ptr; |
| 664 | *vlen = ctx->value.len; |
| 665 | return 1; |
| 666 | } |
| 667 | } |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | /* negative occurrence, we scan all the list then walk back */ |
| 672 | if (-occ > MAX_HDR_HISTORY) |
| 673 | return 0; |
| 674 | |
| 675 | found = hist_idx = 0; |
| 676 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 677 | val_hist[hist_idx] = ctx->value; |
| 678 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 679 | hist_idx = 0; |
| 680 | found++; |
| 681 | } |
| 682 | if (-occ > found) |
| 683 | return 0; |
| 684 | |
| 685 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 686 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 687 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 688 | * to remain in the 0..9 range. |
| 689 | */ |
| 690 | hist_idx += occ + MAX_HDR_HISTORY; |
| 691 | if (hist_idx >= MAX_HDR_HISTORY) |
| 692 | hist_idx -= MAX_HDR_HISTORY; |
| 693 | *vptr = val_hist[hist_idx].ptr; |
| 694 | *vlen = val_hist[hist_idx].len; |
| 695 | return 1; |
| 696 | } |
| 697 | |
| 698 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 699 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 700 | * performed over the whole headers. Otherwise it must contain a valid header |
| 701 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 702 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 703 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 704 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 705 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 706 | * line header values and does not stop at commas. |
| 707 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 708 | */ |
| 709 | unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr, |
| 710 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 711 | { |
| 712 | struct http_hdr_ctx local_ctx; |
| 713 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 714 | unsigned int hist_idx; |
| 715 | int found; |
| 716 | |
| 717 | if (!ctx) { |
| 718 | local_ctx.blk = NULL; |
| 719 | ctx = &local_ctx; |
| 720 | } |
| 721 | |
| 722 | if (occ >= 0) { |
| 723 | /* search from the beginning */ |
| 724 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 725 | occ--; |
| 726 | if (occ <= 0) { |
| 727 | *vptr = ctx->value.ptr; |
| 728 | *vlen = ctx->value.len; |
| 729 | return 1; |
| 730 | } |
| 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /* negative occurrence, we scan all the list then walk back */ |
| 736 | if (-occ > MAX_HDR_HISTORY) |
| 737 | return 0; |
| 738 | |
| 739 | found = hist_idx = 0; |
| 740 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 741 | val_hist[hist_idx] = ctx->value; |
| 742 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 743 | hist_idx = 0; |
| 744 | found++; |
| 745 | } |
| 746 | if (-occ > found) |
| 747 | return 0; |
| 748 | |
| 749 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 750 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 751 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 752 | * to remain in the 0..9 range. |
| 753 | */ |
| 754 | hist_idx += occ + MAX_HDR_HISTORY; |
| 755 | if (hist_idx >= MAX_HDR_HISTORY) |
| 756 | hist_idx -= MAX_HDR_HISTORY; |
| 757 | *vptr = val_hist[hist_idx].ptr; |
| 758 | *vlen = val_hist[hist_idx].len; |
| 759 | return 1; |
| 760 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 761 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 762 | int http_str_to_htx(struct buffer *buf, struct ist raw) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 763 | { |
| 764 | struct htx *htx; |
| 765 | struct htx_sl *sl; |
| 766 | struct h1m h1m; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 767 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 768 | union h1_sl h1sl; |
| 769 | unsigned int flags = HTX_SL_F_IS_RESP; |
| 770 | int ret = 0; |
| 771 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 772 | b_reset(buf); |
| 773 | if (!raw.len) { |
| 774 | buf->size = 0; |
| 775 | buf->area = malloc(raw.len); |
| 776 | return 1; |
| 777 | } |
| 778 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 779 | buf->size = global.tune.bufsize; |
| 780 | buf->area = (char *)malloc(buf->size); |
| 781 | if (!buf->area) |
| 782 | goto error; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 783 | |
| 784 | h1m_init_res(&h1m); |
| 785 | h1m.flags |= H1_MF_NO_PHDR; |
| 786 | ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len, |
| 787 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
| 788 | if (ret <= 0) |
| 789 | goto error; |
| 790 | |
| 791 | if (unlikely(h1sl.st.v.len != 8)) |
| 792 | goto error; |
| 793 | if ((*(h1sl.st.v.ptr + 5) > '1') || |
| 794 | ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1'))) |
| 795 | h1m.flags |= H1_MF_VER_11; |
| 796 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 797 | if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) |
| 798 | goto error; |
| 799 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 800 | if (h1m.flags & H1_MF_VER_11) |
| 801 | flags |= HTX_SL_F_VER_11; |
| 802 | if (h1m.flags & H1_MF_XFER_ENC) |
| 803 | flags |= HTX_SL_F_XFER_ENC; |
Christopher Faulet | 0d4ce93 | 2019-10-16 09:09:04 +0200 | [diff] [blame] | 804 | if (h1m.flags & H1_MF_CLEN) { |
| 805 | flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 806 | if (h1m.body_len == 0) |
| 807 | flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 808 | } |
Christopher Faulet | 0d4ce93 | 2019-10-16 09:09:04 +0200 | [diff] [blame] | 809 | if (h1m.flags & H1_MF_CHNK) |
| 810 | goto error; /* Unsupported because there is no body parsing */ |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 811 | |
| 812 | htx = htx_from_buf(buf); |
| 813 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r); |
| 814 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
| 815 | goto error; |
| 816 | sl->info.res.status = h1sl.st.status; |
| 817 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 818 | while (raw.len > ret) { |
| 819 | int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret)); |
| 820 | if (!sent) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 821 | goto error; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 822 | ret += sent; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 823 | } |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 824 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 825 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 826 | goto error; |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 827 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 828 | return 1; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 829 | |
| 830 | error: |
| 831 | if (buf->size) |
| 832 | free(buf->area); |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 833 | return 0; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static int http_htx_init(void) |
| 837 | { |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 838 | struct buffer chk; |
| 839 | struct ist raw; |
| 840 | int rc; |
| 841 | int err_code = 0; |
| 842 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 843 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 844 | if (!http_err_msgs[rc]) { |
| 845 | ha_alert("Internal error: no message defined for HTTP return code %d", rc); |
| 846 | err_code |= ERR_ALERT | ERR_FATAL; |
| 847 | continue; |
| 848 | } |
| 849 | |
| 850 | raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])); |
| 851 | if (!http_str_to_htx(&chk, raw)) { |
| 852 | ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n", |
| 853 | http_err_codes[rc]); |
| 854 | err_code |= ERR_ALERT | ERR_FATAL; |
| 855 | } |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 856 | http_err_chunks[rc] = chk; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 857 | } |
| 858 | end: |
| 859 | return err_code; |
| 860 | } |
| 861 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 862 | static void http_htx_deinit(void) |
| 863 | { |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 864 | struct http_errors *http_errs, *http_errsb; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 865 | struct ebpt_node *node, *next; |
| 866 | struct http_error *http_err; |
| 867 | |
| 868 | node = ebpt_first(&http_error_messages); |
| 869 | while (node) { |
| 870 | next = ebpt_next(node); |
| 871 | ebpt_delete(node); |
| 872 | http_err = container_of(node, typeof(*http_err), node); |
| 873 | chunk_destroy(&http_err->msg); |
| 874 | free(node->key); |
| 875 | free(http_err); |
| 876 | node = next; |
| 877 | } |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 878 | |
| 879 | list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) { |
| 880 | free(http_errs->conf.file); |
| 881 | free(http_errs->id); |
| 882 | LIST_DEL(&http_errs->list); |
| 883 | free(http_errs); |
| 884 | } |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 885 | } |
| 886 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 887 | REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 888 | REGISTER_POST_DEINIT(http_htx_deinit); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 889 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 890 | /* Reads content of the error file <file> and convert it into an HTX message. On |
| 891 | * success, the HTX message is returned. On error, NULL is returned and an error |
| 892 | * message is written into the <errmsg> buffer. |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 893 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 894 | struct buffer *http_load_errorfile(const char *file, char **errmsg) |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 895 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 896 | struct buffer *buf = NULL; |
| 897 | struct buffer chk; |
| 898 | struct ebpt_node *node; |
| 899 | struct http_error *http_err; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 900 | struct stat stat; |
| 901 | char *err = NULL; |
| 902 | int errnum, errlen; |
| 903 | int fd = -1; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 904 | |
| 905 | /* already loaded */ |
| 906 | node = ebis_lookup_len(&http_error_messages, file, strlen(file)); |
| 907 | if (node) { |
| 908 | http_err = container_of(node, typeof(*http_err), node); |
| 909 | buf = &http_err->msg; |
| 910 | goto out; |
| 911 | } |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 912 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 913 | /* Read the error file content */ |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 914 | fd = open(file, O_RDONLY); |
| 915 | if ((fd < 0) || (fstat(fd, &stat) < 0)) { |
| 916 | memprintf(errmsg, "error opening file '%s'.", file); |
| 917 | goto out; |
| 918 | } |
| 919 | |
| 920 | if (stat.st_size <= global.tune.bufsize) |
| 921 | errlen = stat.st_size; |
| 922 | else { |
| 923 | ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n", |
| 924 | file, global.tune.bufsize); |
| 925 | errlen = global.tune.bufsize; |
| 926 | } |
| 927 | |
| 928 | err = malloc(errlen); |
| 929 | if (!err) { |
| 930 | memprintf(errmsg, "out of memory."); |
| 931 | goto out; |
| 932 | } |
| 933 | |
| 934 | errnum = read(fd, err, errlen); |
| 935 | if (errnum != errlen) { |
| 936 | memprintf(errmsg, "error reading file '%s'.", file); |
| 937 | goto out; |
| 938 | } |
| 939 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 940 | /* Create the node corresponding to the error file */ |
| 941 | http_err = calloc(1, sizeof(*http_err)); |
| 942 | if (!http_err) { |
| 943 | memprintf(errmsg, "out of memory."); |
| 944 | goto out; |
| 945 | } |
| 946 | http_err->node.key = strdup(file); |
| 947 | if (!http_err->node.key) { |
| 948 | memprintf(errmsg, "out of memory."); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 949 | free(http_err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 950 | goto out; |
| 951 | } |
| 952 | |
| 953 | /* Convert the error file into an HTX message */ |
| 954 | if (!http_str_to_htx(&chk, ist2(err, errlen))) { |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 955 | memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 956 | free(http_err->node.key); |
| 957 | free(http_err); |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 958 | goto out; |
| 959 | } |
| 960 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 961 | /* Insert the node in the tree and return the HTX message */ |
| 962 | http_err->msg = chk; |
| 963 | ebis_insert(&http_error_messages, &http_err->node); |
| 964 | buf = &http_err->msg; |
| 965 | |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 966 | out: |
| 967 | if (fd >= 0) |
| 968 | close(fd); |
| 969 | free(err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 970 | return buf; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 973 | /* Convert the raw http message <msg> into an HTX message. On sucess, the HTX |
| 974 | * message is returned. On error, NULL is returned and an error message is |
| 975 | * written into the <errmsg> buffer. |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 976 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 977 | struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg) |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 978 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 979 | struct buffer *buf = NULL; |
| 980 | struct buffer chk; |
| 981 | struct ebpt_node *node; |
| 982 | struct http_error *http_err; |
| 983 | |
| 984 | /* already loaded */ |
| 985 | node = ebis_lookup_len(&http_error_messages, key, strlen(key)); |
| 986 | if (node) { |
| 987 | http_err = container_of(node, typeof(*http_err), node); |
| 988 | buf = &http_err->msg; |
| 989 | goto out; |
| 990 | } |
| 991 | /* Create the node corresponding to the error file */ |
| 992 | http_err = calloc(1, sizeof(*http_err)); |
| 993 | if (!http_err) { |
| 994 | memprintf(errmsg, "out of memory."); |
| 995 | goto out; |
| 996 | } |
| 997 | http_err->node.key = strdup(key); |
| 998 | if (!http_err->node.key) { |
| 999 | memprintf(errmsg, "out of memory."); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 1000 | free(http_err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1001 | goto out; |
| 1002 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1003 | |
| 1004 | /* Convert the error file into an HTX message */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1005 | if (!http_str_to_htx(&chk, msg)) { |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1006 | memprintf(errmsg, "unable to convert message in HTX."); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1007 | free(http_err->node.key); |
| 1008 | free(http_err); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1009 | goto out; |
| 1010 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1011 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1012 | /* Insert the node in the tree and return the HTX message */ |
| 1013 | http_err->msg = chk; |
| 1014 | ebis_insert(&http_error_messages, &http_err->node); |
| 1015 | buf = &http_err->msg; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1016 | out: |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1017 | return buf; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1020 | /* This function parses the raw HTTP error file <file> for the status code |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1021 | * <status>. It returns NULL if there is any error, otherwise it return the |
| 1022 | * corresponding HTX message. |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1023 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1024 | struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg) |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1025 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1026 | struct buffer *buf = NULL; |
| 1027 | int rc; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1028 | |
| 1029 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1030 | if (http_err_codes[rc] == status) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1031 | buf = http_load_errorfile(file, errmsg); |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | if (rc >= HTTP_ERR_SIZE) |
| 1037 | memprintf(errmsg, "status code '%d' not handled.", status); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1038 | return buf; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /* This function creates HTX error message corresponding to a redirect message |
| 1042 | * for the status code <status>. <url> is used as location url for the |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1043 | * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It |
| 1044 | * returns NULL if there is any error, otherwise it return the corresponding HTX |
| 1045 | * message. |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1046 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1047 | struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg) |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1048 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1049 | struct buffer *buf = NULL; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1050 | const char *msg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1051 | char *key = NULL, *err = NULL; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1052 | int rc, errlen; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1053 | |
| 1054 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1055 | if (http_err_codes[rc] == status) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1056 | /* Create the error key */ |
| 1057 | if (!memprintf(&key, "errorloc%d %s", errloc, url)) { |
| 1058 | memprintf(errmsg, "out of memory."); |
| 1059 | goto out; |
| 1060 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1061 | /* Create the error message */ |
| 1062 | msg = (errloc == 302 ? HTTP_302 : HTTP_303); |
| 1063 | errlen = strlen(msg) + strlen(url) + 5; |
| 1064 | err = malloc(errlen); |
| 1065 | if (!err) { |
| 1066 | memprintf(errmsg, "out of memory."); |
| 1067 | goto out; |
| 1068 | } |
| 1069 | errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url); |
| 1070 | |
| 1071 | /* Load it */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1072 | buf = http_load_errormsg(key, ist2(err, errlen), errmsg); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1073 | break; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | if (rc >= HTTP_ERR_SIZE) |
| 1078 | memprintf(errmsg, "status code '%d' not handled.", status); |
| 1079 | out: |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1080 | free(key); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1081 | free(err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1082 | return buf; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1085 | /* Parses the "errorloc[302|303]" proxy keyword */ |
| 1086 | static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx, |
| 1087 | struct proxy *defpx, const char *file, int line, |
| 1088 | char **errmsg) |
| 1089 | { |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1090 | struct conf_errors *conf_err; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1091 | struct buffer *msg; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1092 | int errloc, status; |
| 1093 | int ret = 0; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1094 | |
| 1095 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1096 | ret = 1; |
| 1097 | goto out; |
| 1098 | } |
| 1099 | |
| 1100 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1101 | memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]); |
| 1102 | ret = -1; |
| 1103 | goto out; |
| 1104 | } |
| 1105 | |
| 1106 | status = atol(args[1]); |
| 1107 | errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302); |
| 1108 | msg = http_parse_errorloc(errloc, status, args[2], errmsg); |
| 1109 | if (!msg) { |
| 1110 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 1111 | ret = -1; |
| 1112 | goto out; |
| 1113 | } |
| 1114 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1115 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1116 | if (!conf_err) { |
| 1117 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 1118 | ret = -1; |
| 1119 | goto out; |
| 1120 | } |
| 1121 | conf_err->type = 1; |
| 1122 | conf_err->info.errorfile.status = status; |
| 1123 | conf_err->info.errorfile.msg = msg; |
| 1124 | conf_err->file = strdup(file); |
| 1125 | conf_err->line = line; |
| 1126 | LIST_ADDQ(&curpx->conf.errors, &conf_err->list); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1127 | |
| 1128 | out: |
| 1129 | return ret; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1130 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1131 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1132 | |
| 1133 | /* Parses the "errorfile" proxy keyword */ |
| 1134 | static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx, |
| 1135 | struct proxy *defpx, const char *file, int line, |
| 1136 | char **errmsg) |
| 1137 | { |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1138 | struct conf_errors *conf_err; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1139 | struct buffer *msg; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1140 | int status; |
| 1141 | int ret = 0; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1142 | |
| 1143 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1144 | ret = 1; |
| 1145 | goto out; |
| 1146 | } |
| 1147 | |
| 1148 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1149 | memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]); |
| 1150 | ret = -1; |
| 1151 | goto out; |
| 1152 | } |
| 1153 | |
| 1154 | status = atol(args[1]); |
| 1155 | msg = http_parse_errorfile(status, args[2], errmsg); |
| 1156 | if (!msg) { |
| 1157 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 1158 | ret = -1; |
| 1159 | goto out; |
| 1160 | } |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1161 | |
| 1162 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1163 | if (!conf_err) { |
| 1164 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 1165 | ret = -1; |
| 1166 | goto out; |
| 1167 | } |
| 1168 | conf_err->type = 1; |
| 1169 | conf_err->info.errorfile.status = status; |
| 1170 | conf_err->info.errorfile.msg = msg; |
| 1171 | conf_err->file = strdup(file); |
| 1172 | conf_err->line = line; |
| 1173 | LIST_ADDQ(&curpx->conf.errors, &conf_err->list); |
| 1174 | |
| 1175 | out: |
| 1176 | return ret; |
| 1177 | |
| 1178 | } |
| 1179 | |
| 1180 | /* Parses the "errorfiles" proxy keyword */ |
| 1181 | static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx, |
| 1182 | struct proxy *defpx, const char *file, int line, |
| 1183 | char **err) |
| 1184 | { |
| 1185 | struct conf_errors *conf_err = NULL; |
| 1186 | char *name = NULL; |
| 1187 | int rc, ret = 0; |
| 1188 | |
| 1189 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1190 | ret = 1; |
| 1191 | goto out; |
| 1192 | } |
| 1193 | |
| 1194 | if (!*(args[1])) { |
| 1195 | memprintf(err, "%s : expects <name> as argument.", args[0]); |
| 1196 | ret = -1; |
| 1197 | goto out; |
| 1198 | } |
| 1199 | |
| 1200 | name = strdup(args[1]); |
| 1201 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1202 | if (!name || !conf_err) { |
| 1203 | memprintf(err, "%s : out of memory.", args[0]); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1204 | goto error; |
| 1205 | } |
| 1206 | conf_err->type = 0; |
| 1207 | |
| 1208 | conf_err->info.errorfiles.name = name; |
| 1209 | if (!*(args[2])) { |
| 1210 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) |
| 1211 | conf_err->info.errorfiles.status[rc] = 1; |
| 1212 | } |
| 1213 | else { |
| 1214 | int cur_arg, status; |
| 1215 | for (cur_arg = 2; *(args[cur_arg]); cur_arg++) { |
| 1216 | status = atol(args[cur_arg]); |
| 1217 | |
| 1218 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1219 | if (http_err_codes[rc] == status) { |
| 1220 | conf_err->info.errorfiles.status[rc] = 2; |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | if (rc >= HTTP_ERR_SIZE) { |
| 1225 | memprintf(err, "%s : status code '%d' not handled.", args[0], status); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 1226 | goto error; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | conf_err->file = strdup(file); |
| 1231 | conf_err->line = line; |
| 1232 | LIST_ADDQ(&curpx->conf.errors, &conf_err->list); |
| 1233 | out: |
| 1234 | return ret; |
| 1235 | |
| 1236 | error: |
| 1237 | free(name); |
| 1238 | free(conf_err); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 1239 | ret = -1; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1240 | goto out; |
| 1241 | } |
| 1242 | |
| 1243 | /* Check "errorfiles" proxy keyword */ |
| 1244 | static int proxy_check_errors(struct proxy *px) |
| 1245 | { |
| 1246 | struct conf_errors *conf_err, *conf_err_back; |
| 1247 | struct http_errors *http_errs; |
| 1248 | int rc, err = 0; |
| 1249 | |
| 1250 | list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) { |
| 1251 | if (conf_err->type == 1) { |
| 1252 | /* errorfile */ |
| 1253 | rc = http_get_status_idx(conf_err->info.errorfile.status); |
| 1254 | px->errmsg[rc] = conf_err->info.errorfile.msg; |
| 1255 | } |
| 1256 | else { |
| 1257 | /* errorfiles */ |
| 1258 | list_for_each_entry(http_errs, &http_errors_list, list) { |
| 1259 | if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0) |
| 1260 | break; |
| 1261 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1262 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1263 | /* unknown http-errors section */ |
| 1264 | if (&http_errs->list == &http_errors_list) { |
| 1265 | ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n", |
| 1266 | px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line); |
| 1267 | err |= ERR_ALERT | ERR_FATAL; |
| 1268 | free(conf_err->info.errorfiles.name); |
| 1269 | goto next; |
| 1270 | } |
| 1271 | |
| 1272 | free(conf_err->info.errorfiles.name); |
| 1273 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1274 | if (conf_err->info.errorfiles.status[rc] > 0) { |
| 1275 | if (http_errs->errmsg[rc]) |
| 1276 | px->errmsg[rc] = http_errs->errmsg[rc]; |
| 1277 | else if (conf_err->info.errorfiles.status[rc] == 2) |
| 1278 | ha_warning("config: proxy '%s' : status '%d' not declared in" |
| 1279 | " http-errors section '%s' (at %s:%d).\n", |
| 1280 | px->id, http_err_codes[rc], http_errs->id, |
| 1281 | conf_err->file, conf_err->line); |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | next: |
| 1286 | LIST_DEL(&conf_err->list); |
| 1287 | free(conf_err->file); |
| 1288 | free(conf_err); |
| 1289 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1290 | |
| 1291 | out: |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1292 | return err; |
| 1293 | } |
| 1294 | |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 1295 | static int post_check_errors() |
| 1296 | { |
| 1297 | struct ebpt_node *node; |
| 1298 | struct http_error *http_err; |
| 1299 | struct htx *htx; |
| 1300 | int err_code = 0; |
| 1301 | |
| 1302 | node = ebpt_first(&http_error_messages); |
| 1303 | while (node) { |
| 1304 | http_err = container_of(node, typeof(*http_err), node); |
| 1305 | if (b_is_null(&http_err->msg)) |
| 1306 | goto next; |
| 1307 | htx = htxbuf(&http_err->msg); |
| 1308 | if (htx_free_data_space(htx) < global.tune.maxrewrite) { |
| 1309 | ha_warning("config: errorfile '%s' runs over the buffer space" |
| 1310 | " reserved to headers rewritting. It may lead to internal errors if " |
| 1311 | " http-final-response rules are evaluated on this message.\n", |
| 1312 | (char *)node->key); |
| 1313 | err_code |= ERR_WARN; |
| 1314 | } |
| 1315 | next: |
| 1316 | node = ebpt_next(node); |
| 1317 | } |
| 1318 | |
| 1319 | return err_code; |
| 1320 | } |
| 1321 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1322 | int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg) |
| 1323 | { |
| 1324 | struct conf_errors *conf_err, *new_conf_err = NULL; |
| 1325 | int ret = 0; |
| 1326 | |
| 1327 | list_for_each_entry(conf_err, &defpx->conf.errors, list) { |
| 1328 | new_conf_err = calloc(1, sizeof(*new_conf_err)); |
| 1329 | if (!new_conf_err) { |
| 1330 | memprintf(errmsg, "unable to duplicate default errors (out of memory)."); |
| 1331 | goto out; |
| 1332 | } |
| 1333 | new_conf_err->type = conf_err->type; |
| 1334 | if (conf_err->type == 1) { |
| 1335 | new_conf_err->info.errorfile.status = conf_err->info.errorfile.status; |
| 1336 | new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg; |
| 1337 | } |
| 1338 | else { |
| 1339 | new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name); |
| 1340 | if (!new_conf_err->info.errorfiles.name) { |
| 1341 | memprintf(errmsg, "unable to duplicate default errors (out of memory)."); |
| 1342 | goto out; |
| 1343 | } |
| 1344 | memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status, |
| 1345 | sizeof(conf_err->info.errorfiles.status)); |
| 1346 | } |
| 1347 | new_conf_err->file = strdup(conf_err->file); |
| 1348 | new_conf_err->line = conf_err->line; |
| 1349 | LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list); |
| 1350 | new_conf_err = NULL; |
| 1351 | } |
| 1352 | ret = 1; |
| 1353 | |
| 1354 | out: |
| 1355 | free(new_conf_err); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1356 | return ret; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | void proxy_release_conf_errors(struct proxy *px) |
| 1360 | { |
| 1361 | struct conf_errors *conf_err, *conf_err_back; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1362 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1363 | list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) { |
| 1364 | if (conf_err->type == 0) |
| 1365 | free(conf_err->info.errorfiles.name); |
| 1366 | LIST_DEL(&conf_err->list); |
| 1367 | free(conf_err->file); |
| 1368 | free(conf_err); |
| 1369 | } |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * Parse an <http-errors> section. |
| 1374 | * Returns the error code, 0 if OK, or any combination of : |
| 1375 | * - ERR_ABORT: must abort ASAP |
| 1376 | * - ERR_FATAL: we can continue parsing but not start the service |
| 1377 | * - ERR_WARN: a warning has been emitted |
| 1378 | * - ERR_ALERT: an alert has been emitted |
| 1379 | * Only the two first ones can stop processing, the two others are just |
| 1380 | * indicators. |
| 1381 | */ |
| 1382 | static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm) |
| 1383 | { |
| 1384 | static struct http_errors *curr_errs = NULL; |
| 1385 | int err_code = 0; |
| 1386 | const char *err; |
| 1387 | char *errmsg = NULL; |
| 1388 | |
| 1389 | if (strcmp(args[0], "http-errors") == 0) { /* new errors section */ |
| 1390 | if (!*args[1]) { |
| 1391 | ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum); |
| 1392 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1393 | goto out; |
| 1394 | } |
| 1395 | |
| 1396 | err = invalid_char(args[1]); |
| 1397 | if (err) { |
| 1398 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 1399 | file, linenum, *err, args[0], args[1]); |
| 1400 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1401 | } |
| 1402 | |
| 1403 | list_for_each_entry(curr_errs, &http_errors_list, list) { |
| 1404 | /* Error if two errors section owns the same name */ |
| 1405 | if (strcmp(curr_errs->id, args[1]) == 0) { |
| 1406 | ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n", |
| 1407 | file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line); |
| 1408 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) { |
| 1413 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 1414 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1415 | goto out; |
| 1416 | } |
| 1417 | |
| 1418 | LIST_ADDQ(&http_errors_list, &curr_errs->list); |
| 1419 | curr_errs->id = strdup(args[1]); |
| 1420 | curr_errs->conf.file = strdup(file); |
| 1421 | curr_errs->conf.line = linenum; |
| 1422 | } |
| 1423 | else if (!strcmp(args[0], "errorfile")) { /* error message from a file */ |
| 1424 | struct buffer *msg; |
| 1425 | int status, rc; |
| 1426 | |
| 1427 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1428 | ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n", |
| 1429 | file, linenum, args[0]); |
| 1430 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1431 | goto out; |
| 1432 | } |
| 1433 | |
| 1434 | status = atol(args[1]); |
| 1435 | msg = http_parse_errorfile(status, args[2], &errmsg); |
| 1436 | if (!msg) { |
| 1437 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 1438 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | rc = http_get_status_idx(status); |
| 1442 | curr_errs->errmsg[rc] = msg; |
| 1443 | } |
| 1444 | else if (*args[0] != 0) { |
| 1445 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection); |
| 1446 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1447 | goto out; |
| 1448 | } |
| 1449 | |
| 1450 | out: |
| 1451 | free(errmsg); |
| 1452 | return err_code; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1456 | { CFG_LISTEN, "errorloc", proxy_parse_errorloc }, |
| 1457 | { CFG_LISTEN, "errorloc302", proxy_parse_errorloc }, |
| 1458 | { CFG_LISTEN, "errorloc303", proxy_parse_errorloc }, |
| 1459 | { CFG_LISTEN, "errorfile", proxy_parse_errorfile }, |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1460 | { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles }, |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1461 | { 0, NULL, NULL }, |
| 1462 | }}; |
| 1463 | |
| 1464 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1465 | REGISTER_POST_PROXY_CHECK(proxy_check_errors); |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 1466 | REGISTER_POST_CHECK(post_check_errors); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1467 | |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1468 | REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL); |
| 1469 | |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1470 | /************************************************************************/ |
| 1471 | /* HTX sample fetches */ |
| 1472 | /************************************************************************/ |
| 1473 | |
| 1474 | /* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */ |
| 1475 | static int |
| 1476 | smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1477 | { |
| 1478 | if (!smp->strm) |
| 1479 | return 0; |
| 1480 | |
| 1481 | smp->data.u.sint = !!IS_HTX_STRM(smp->strm); |
| 1482 | smp->data.type = SMP_T_BOOL; |
| 1483 | return 1; |
| 1484 | } |
| 1485 | |
| 1486 | /* Returns the number of blocks in an HTX message. The channel is chosen |
| 1487 | * depending on the sample direction. */ |
| 1488 | static int |
| 1489 | smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1490 | { |
| 1491 | struct channel *chn; |
| 1492 | struct htx *htx; |
| 1493 | |
| 1494 | if (!smp->strm) |
| 1495 | return 0; |
| 1496 | |
| 1497 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1498 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1499 | if (!htx) |
| 1500 | return 0; |
| 1501 | |
| 1502 | smp->data.u.sint = htx_nbblks(htx); |
| 1503 | smp->data.type = SMP_T_SINT; |
| 1504 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1505 | return 1; |
| 1506 | } |
| 1507 | |
| 1508 | /* Returns the size of an HTX message. The channel is chosen depending on the |
| 1509 | * sample direction. */ |
| 1510 | static int |
| 1511 | smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1512 | { |
| 1513 | struct channel *chn; |
| 1514 | struct htx *htx; |
| 1515 | |
| 1516 | if (!smp->strm) |
| 1517 | return 0; |
| 1518 | |
| 1519 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1520 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1521 | if (!htx) |
| 1522 | return 0; |
| 1523 | |
| 1524 | smp->data.u.sint = htx->size; |
| 1525 | smp->data.type = SMP_T_SINT; |
| 1526 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1527 | return 1; |
| 1528 | } |
| 1529 | |
| 1530 | /* Returns the data size of an HTX message. The channel is chosen depending on the |
| 1531 | * sample direction. */ |
| 1532 | static int |
| 1533 | smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1534 | { |
| 1535 | struct channel *chn; |
| 1536 | struct htx *htx; |
| 1537 | |
| 1538 | if (!smp->strm) |
| 1539 | return 0; |
| 1540 | |
| 1541 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1542 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1543 | if (!htx) |
| 1544 | return 0; |
| 1545 | |
| 1546 | smp->data.u.sint = htx->data; |
| 1547 | smp->data.type = SMP_T_SINT; |
| 1548 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | /* Returns the used space (data+meta) of an HTX message. The channel is chosen |
| 1553 | * depending on the sample direction. */ |
| 1554 | static int |
| 1555 | smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1556 | { |
| 1557 | struct channel *chn; |
| 1558 | struct htx *htx; |
| 1559 | |
| 1560 | if (!smp->strm) |
| 1561 | return 0; |
| 1562 | |
| 1563 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1564 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1565 | if (!htx) |
| 1566 | return 0; |
| 1567 | |
| 1568 | smp->data.u.sint = htx_used_space(htx); |
| 1569 | smp->data.type = SMP_T_SINT; |
| 1570 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1571 | return 1; |
| 1572 | } |
| 1573 | |
| 1574 | /* Returns the free space (size-used) of an HTX message. The channel is chosen |
| 1575 | * depending on the sample direction. */ |
| 1576 | static int |
| 1577 | smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1578 | { |
| 1579 | struct channel *chn; |
| 1580 | struct htx *htx; |
| 1581 | |
| 1582 | if (!smp->strm) |
| 1583 | return 0; |
| 1584 | |
| 1585 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1586 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1587 | if (!htx) |
| 1588 | return 0; |
| 1589 | |
| 1590 | smp->data.u.sint = htx_free_space(htx); |
| 1591 | smp->data.type = SMP_T_SINT; |
| 1592 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | /* Returns the free space for data (free-sizeof(blk)) of an HTX message. The |
| 1597 | * channel is chosen depending on the sample direction. */ |
| 1598 | static int |
| 1599 | smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1600 | { |
| 1601 | struct channel *chn; |
| 1602 | struct htx *htx; |
| 1603 | |
| 1604 | if (!smp->strm) |
| 1605 | return 0; |
| 1606 | |
| 1607 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1608 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1609 | if (!htx) |
| 1610 | return 0; |
| 1611 | |
| 1612 | smp->data.u.sint = htx_free_data_space(htx); |
| 1613 | smp->data.type = SMP_T_SINT; |
| 1614 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1615 | return 1; |
| 1616 | } |
| 1617 | |
| 1618 | /* Returns 1 if the HTX message contains an EOM block. Otherwise it returns |
| 1619 | * 0. Concretely, it only checks the tail. The channel is chosen depending on |
| 1620 | * the sample direction. */ |
| 1621 | static int |
| 1622 | smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1623 | { |
| 1624 | struct channel *chn; |
| 1625 | struct htx *htx; |
| 1626 | |
| 1627 | if (!smp->strm) |
| 1628 | return 0; |
| 1629 | |
| 1630 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1631 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1632 | if (!htx) |
| 1633 | return 0; |
| 1634 | |
| 1635 | smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM); |
| 1636 | smp->data.type = SMP_T_BOOL; |
| 1637 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1638 | return 1; |
| 1639 | } |
| 1640 | |
| 1641 | /* Returns the type of a specific HTX block, if found in the message. Otherwise |
| 1642 | * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or |
| 1643 | * "head", "tail" or "first". The channel is chosen depending on the sample |
| 1644 | * direction. */ |
| 1645 | static int |
| 1646 | smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1647 | { |
| 1648 | struct channel *chn; |
| 1649 | struct htx *htx; |
| 1650 | enum htx_blk_type type; |
| 1651 | int32_t pos; |
| 1652 | |
| 1653 | if (!smp->strm || !arg_p) |
| 1654 | return 0; |
| 1655 | |
| 1656 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1657 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1658 | if (!htx) |
| 1659 | return 0; |
| 1660 | |
| 1661 | pos = arg_p[0].data.sint; |
| 1662 | if (pos == -1) |
| 1663 | type = htx_get_head_type(htx); |
| 1664 | else if (pos == -2) |
| 1665 | type = htx_get_tail_type(htx); |
| 1666 | else if (pos == -3) |
| 1667 | type = htx_get_first_type(htx); |
| 1668 | else |
| 1669 | type = ((pos >= htx->head && pos <= htx->tail) |
| 1670 | ? htx_get_blk_type(htx_get_blk(htx, pos)) |
| 1671 | : HTX_BLK_UNUSED); |
| 1672 | |
| 1673 | chunk_initstr(&smp->data.u.str, htx_blk_type_str(type)); |
| 1674 | smp->data.type = SMP_T_STR; |
| 1675 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | /* Returns the size of a specific HTX block, if found in the message. Otherwise |
| 1680 | * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or |
| 1681 | * "first". The channel is chosen depending on the sample direction. */ |
| 1682 | static int |
| 1683 | smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1684 | { |
| 1685 | struct channel *chn; |
| 1686 | struct htx *htx; |
| 1687 | struct htx_blk *blk; |
| 1688 | int32_t pos; |
| 1689 | |
| 1690 | if (!smp->strm || !arg_p) |
| 1691 | return 0; |
| 1692 | |
| 1693 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1694 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1695 | if (!htx) |
| 1696 | return 0; |
| 1697 | |
| 1698 | pos = arg_p[0].data.sint; |
| 1699 | if (pos == -1) |
| 1700 | blk = htx_get_head_blk(htx); |
| 1701 | else if (pos == -2) |
| 1702 | blk = htx_get_tail_blk(htx); |
| 1703 | else if (pos == -3) |
| 1704 | blk = htx_get_first_blk(htx); |
| 1705 | else |
| 1706 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1707 | |
| 1708 | smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0); |
| 1709 | smp->data.type = SMP_T_SINT; |
| 1710 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1711 | return 1; |
| 1712 | } |
| 1713 | |
| 1714 | /* Returns the start-line if the selected HTX block exists and is a |
| 1715 | * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 1716 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 1717 | * the sample direction. */ |
| 1718 | static int |
| 1719 | smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1720 | { |
| 1721 | struct buffer *temp; |
| 1722 | struct channel *chn; |
| 1723 | struct htx *htx; |
| 1724 | struct htx_blk *blk; |
| 1725 | struct htx_sl *sl; |
| 1726 | int32_t pos; |
| 1727 | |
| 1728 | if (!smp->strm || !arg_p) |
| 1729 | return 0; |
| 1730 | |
| 1731 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1732 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1733 | if (!htx) |
| 1734 | return 0; |
| 1735 | |
| 1736 | pos = arg_p[0].data.sint; |
| 1737 | if (pos == -1) |
| 1738 | blk = htx_get_head_blk(htx); |
| 1739 | else if (pos == -2) |
| 1740 | blk = htx_get_tail_blk(htx); |
| 1741 | else if (pos == -3) |
| 1742 | blk = htx_get_first_blk(htx); |
| 1743 | else |
| 1744 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1745 | |
| 1746 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) { |
| 1747 | smp->data.u.str.size = 0; |
| 1748 | smp->data.u.str.area = ""; |
| 1749 | smp->data.u.str.data = 0; |
| 1750 | } |
| 1751 | else { |
| 1752 | sl = htx_get_blk_ptr(htx, blk); |
| 1753 | |
| 1754 | temp = get_trash_chunk(); |
| 1755 | chunk_istcat(temp, htx_sl_p1(sl)); |
| 1756 | temp->area[temp->data++] = ' '; |
| 1757 | chunk_istcat(temp, htx_sl_p2(sl)); |
| 1758 | temp->area[temp->data++] = ' '; |
| 1759 | chunk_istcat(temp, htx_sl_p3(sl)); |
| 1760 | |
| 1761 | smp->data.u.str = *temp; |
| 1762 | } |
| 1763 | |
| 1764 | smp->data.type = SMP_T_STR; |
| 1765 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1766 | return 1; |
| 1767 | } |
| 1768 | |
| 1769 | /* Returns the header name if the selected HTX block exists and is a header or a |
| 1770 | * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 1771 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 1772 | * the sample direction. */ |
| 1773 | static int |
| 1774 | smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1775 | { |
| 1776 | struct channel *chn; |
| 1777 | struct htx *htx; |
| 1778 | struct htx_blk *blk; |
| 1779 | int32_t pos; |
| 1780 | |
| 1781 | if (!smp->strm || !arg_p) |
| 1782 | return 0; |
| 1783 | |
| 1784 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1785 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1786 | if (!htx) |
| 1787 | return 0; |
| 1788 | |
| 1789 | pos = arg_p[0].data.sint; |
| 1790 | if (pos == -1) |
| 1791 | blk = htx_get_head_blk(htx); |
| 1792 | else if (pos == -2) |
| 1793 | blk = htx_get_tail_blk(htx); |
| 1794 | else if (pos == -3) |
| 1795 | blk = htx_get_first_blk(htx); |
| 1796 | else |
| 1797 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1798 | |
| 1799 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) { |
| 1800 | smp->data.u.str.size = 0; |
| 1801 | smp->data.u.str.area = ""; |
| 1802 | smp->data.u.str.data = 0; |
| 1803 | } |
| 1804 | else { |
| 1805 | struct ist name = htx_get_blk_name(htx, blk); |
| 1806 | |
| 1807 | chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len); |
| 1808 | } |
| 1809 | smp->data.type = SMP_T_STR; |
| 1810 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1811 | return 1; |
| 1812 | } |
| 1813 | |
| 1814 | /* Returns the header value if the selected HTX block exists and is a header or |
| 1815 | * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 1816 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 1817 | * the sample direction. */ |
| 1818 | static int |
| 1819 | smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1820 | { |
| 1821 | struct channel *chn; |
| 1822 | struct htx *htx; |
| 1823 | struct htx_blk *blk; |
| 1824 | int32_t pos; |
| 1825 | |
| 1826 | if (!smp->strm || !arg_p) |
| 1827 | return 0; |
| 1828 | |
| 1829 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1830 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1831 | if (!htx) |
| 1832 | return 0; |
| 1833 | |
| 1834 | pos = arg_p[0].data.sint; |
| 1835 | if (pos == -1) |
| 1836 | blk = htx_get_head_blk(htx); |
| 1837 | else if (pos == -2) |
| 1838 | blk = htx_get_tail_blk(htx); |
| 1839 | else if (pos == -3) |
| 1840 | blk = htx_get_first_blk(htx); |
| 1841 | else |
| 1842 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1843 | |
| 1844 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) { |
| 1845 | smp->data.u.str.size = 0; |
| 1846 | smp->data.u.str.area = ""; |
| 1847 | smp->data.u.str.data = 0; |
| 1848 | } |
| 1849 | else { |
| 1850 | struct ist val = htx_get_blk_value(htx, blk); |
| 1851 | |
| 1852 | chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len); |
| 1853 | } |
| 1854 | smp->data.type = SMP_T_STR; |
| 1855 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1856 | return 1; |
| 1857 | } |
| 1858 | |
| 1859 | /* Returns the value if the selected HTX block exists and is a data |
| 1860 | * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported |
| 1861 | * or "head", "tail" or "first". The channel is chosen depending on the sample |
| 1862 | * direction. */ |
| 1863 | static int |
Christopher Faulet | c5db14c | 2020-01-08 14:51:03 +0100 | [diff] [blame] | 1864 | smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1865 | { |
| 1866 | struct channel *chn; |
| 1867 | struct htx *htx; |
| 1868 | struct htx_blk *blk; |
| 1869 | int32_t pos; |
| 1870 | |
| 1871 | if (!smp->strm || !arg_p) |
| 1872 | return 0; |
| 1873 | |
| 1874 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1875 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1876 | if (!htx) |
| 1877 | return 0; |
| 1878 | |
| 1879 | pos = arg_p[0].data.sint; |
| 1880 | if (pos == -1) |
| 1881 | blk = htx_get_head_blk(htx); |
| 1882 | else if (pos == -2) |
| 1883 | blk = htx_get_tail_blk(htx); |
| 1884 | else if (pos == -3) |
| 1885 | blk = htx_get_first_blk(htx); |
| 1886 | else |
| 1887 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1888 | |
| 1889 | if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) { |
| 1890 | smp->data.u.str.size = 0; |
| 1891 | smp->data.u.str.area = ""; |
| 1892 | smp->data.u.str.data = 0; |
| 1893 | } |
| 1894 | else { |
| 1895 | struct ist val = htx_get_blk_value(htx, blk); |
| 1896 | |
| 1897 | chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len); |
| 1898 | } |
Christopher Faulet | 8178e40 | 2020-01-08 14:38:58 +0100 | [diff] [blame] | 1899 | smp->data.type = SMP_T_BIN; |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1900 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1901 | return 1; |
| 1902 | } |
| 1903 | |
| 1904 | /* This function is used to validate the arguments passed to any "htx_blk" fetch |
| 1905 | * keywords. An argument is expected by these keywords. It must be a positive |
| 1906 | * integer or on of the following strings: "head", "tail" or "first". It returns |
| 1907 | * 0 on error, and a non-zero value if OK. |
| 1908 | */ |
| 1909 | int val_blk_arg(struct arg *arg, char **err_msg) |
| 1910 | { |
| 1911 | if (arg[0].type != ARGT_STR || !arg[0].data.str.data) { |
| 1912 | memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)"); |
| 1913 | return 0; |
| 1914 | } |
| 1915 | if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) { |
| 1916 | free(arg[0].data.str.area); |
| 1917 | arg[0].type = ARGT_SINT; |
| 1918 | arg[0].data.sint = -1; |
| 1919 | } |
| 1920 | else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) { |
| 1921 | free(arg[0].data.str.area); |
| 1922 | arg[0].type = ARGT_SINT; |
| 1923 | arg[0].data.sint = -2; |
| 1924 | } |
| 1925 | else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) { |
| 1926 | free(arg[0].data.str.area); |
| 1927 | arg[0].type = ARGT_SINT; |
| 1928 | arg[0].data.sint = -3; |
| 1929 | } |
| 1930 | else { |
| 1931 | int pos; |
| 1932 | |
| 1933 | for (pos = 0; pos < arg[0].data.str.data; pos++) { |
| 1934 | if (!isdigit(arg[0].data.str.area[pos])) { |
| 1935 | memprintf(err_msg, "invalid block position"); |
| 1936 | return 0; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data); |
| 1941 | if (pos < 0) { |
| 1942 | memprintf(err_msg, "block position must not be negative"); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | free(arg[0].data.str.area); |
| 1946 | arg[0].type = ARGT_SINT; |
| 1947 | arg[0].data.sint = pos; |
| 1948 | } |
| 1949 | |
| 1950 | return 1; |
| 1951 | } |
| 1952 | |
| 1953 | |
| 1954 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1955 | * Note: htx sample fetches should only used for developpement purpose. |
| 1956 | */ |
| 1957 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 1958 | { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ }, |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1959 | |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 1960 | { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1961 | { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1962 | { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1963 | { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1964 | { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1965 | { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1966 | { "internal.htx.has_eom", smp_fetch_htx_has_eom, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1967 | |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 1968 | { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1969 | { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1970 | { "internal.htx_blk.start_line", smp_fetch_htx_blk_stline, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1971 | { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1972 | { "internal.htx_blk.hdrval", smp_fetch_htx_blk_hdrval, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
Christopher Faulet | c5db14c | 2020-01-08 14:51:03 +0100 | [diff] [blame] | 1973 | { "internal.htx_blk.data", smp_fetch_htx_blk_data, ARG1(1,STR), val_blk_arg, SMP_T_BIN, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1974 | |
| 1975 | { /* END */ }, |
| 1976 | }}; |
| 1977 | |
| 1978 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |