blob: 60525bb015c946a3aafa406bfa1ab5a36b40b41e [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
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 Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +020014#include <ctype.h>
Christopher Faulet5031ef52020-01-15 11:22:07 +010015#include <fcntl.h>
16#include <unistd.h>
17
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020022#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/log.h>
28#include <haproxy/regex.h>
29#include <haproxy/sample.h>
Willy Tarreau4cbf62d2021-05-08 13:01:23 +020030#include <haproxy/tools.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020031
Christopher Faulet47596d32018-10-22 09:17:28 +020032
Christopher Fauletf7346382019-07-17 22:02:08 +020033struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020034struct http_reply http_err_replies[HTTP_ERR_SIZE];
35
Christopher Faulet58857752020-01-15 15:19:50 +010036struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010037struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020038struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010039
Christopher Faulet76edc0f2020-01-13 15:52:01 +010040/* The declaration of an errorfiles/errorfile directives. Used during config
41 * parsing only. */
42struct conf_errors {
43 char type; /* directive type (0: errorfiles, 1: errorfile) */
44 union {
45 struct {
46 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020047 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010048 } errorfile; /* describe an "errorfile" directive */
49 struct {
50 char *name; /* the http-errors section name */
51 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
52 } errorfiles; /* describe an "errorfiles" directive */
53 } info;
54
55 char *file; /* file where the directive appears */
56 int line; /* line where the directive appears */
57
58 struct list list; /* next conf_errors */
59};
60
Christopher Faulet297fbb42019-05-13 14:41:27 +020061/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020062 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020063 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020064 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020065struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020066{
Christopher Faulet297fbb42019-05-13 14:41:27 +020067 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010068
Christopher Faulet29f17582019-05-23 11:03:26 +020069 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020070 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL))
Christopher Faulet297fbb42019-05-13 14:41:27 +020071 return NULL;
72 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100162 if (type == HTX_BLK_EOH)
Christopher Faulet573fe732018-11-28 16:55:12 +0100163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000204 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
Christopher Faulet26590532022-11-22 18:02:00 +0100391 ALREADY_CHECKED(sl); /* the stline exists because http_replace_stline() succeded */
Christopher Faulete6794272022-11-22 15:41:48 +0100392 sl->flags &= ~HTX_SL_F_NORMALIZED_URI;
Christopher Faulet26590532022-11-22 18:02:00 +0100393
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200394 if (!http_update_host(htx, sl, uri))
395 goto fail;
396
397 return 1;
398 fail:
399 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200400}
401
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200402/* Replace the request path in the HTX message <htx> by <path>. The host part is
403 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
404 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
405 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200406 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200407int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200408{
409 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200410 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100411 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200412 size_t plen = 0;
413
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100414 if (!sl)
415 return 0;
416
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100417 uri = htx_sl_req_uri(sl);
418 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100419 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100420 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200421 if (with_qs)
422 plen = p.len;
423 else {
424 while (plen < p.len && *(p.ptr + plen) != '?')
425 plen++;
426 }
Christopher Faulete010c802018-10-24 10:36:45 +0200427
428 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100429 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
430 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200431
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100432 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
433 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
434
435 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200436 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
437 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100438 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200439
440 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100441 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200442}
443
444/* Replace the request query-string in the HTX message <htx> by <query>. The
445 * host part and the path are preserved. It returns 1 on success, otherwise
446 * 0.
447 */
448int http_replace_req_query(struct htx *htx, const struct ist query)
449{
450 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200451 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100452 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200453 int offset = 1;
454
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100455 if (!sl)
456 return 0;
457
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100458 uri = htx_sl_req_uri(sl);
459 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200460 while (q.len > 0 && *(q.ptr) != '?') {
461 q.ptr++;
462 q.len--;
463 }
464
465 /* skip the question mark or indicate that we must insert it
466 * (but only if the format string is not empty then).
467 */
468 if (q.len) {
469 q.ptr++;
470 q.len--;
471 }
472 else if (query.len > 1)
473 offset = 0;
474
475 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100476 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
477 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200478
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
480 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200481
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100482 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
483 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
484 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200485
486 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100487 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200488}
489
490/* Replace the response status in the HTX message <htx> by <status>. It returns
491 * 1 on success, otherwise 0.
492*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200493int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200494{
495 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200496 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200497 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200498
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100499 if (!sl)
500 return 0;
501
Christopher Faulete010c802018-10-24 10:36:45 +0200502 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100503 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
504 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200505 r = reason;
506 if (!isttest(r)) {
507 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
508 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
509 }
Christopher Faulete010c802018-10-24 10:36:45 +0200510
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100511 /* create the new start line */
512 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200513 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200514}
515
516/* Replace the response reason in the HTX message <htx> by <reason>. It returns
517 * 1 on success, otherwise 0.
518*/
519int http_replace_res_reason(struct htx *htx, const struct ist reason)
520{
521 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200522 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100523 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200524
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100525 if (!sl)
526 return 0;
527
Christopher Faulete010c802018-10-24 10:36:45 +0200528 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100529 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
530 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200531
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100532 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
533 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200534
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100535 /* create the new start line */
536 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200537}
538
Christopher Faulet47596d32018-10-22 09:17:28 +0200539/* Replaces a part of a header value referenced in the context <ctx> by
540 * <data>. It returns 1 on success, otherwise it returns 0. The context is
541 * updated if necessary.
542 */
543int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
544{
545 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200546 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200547 char *start;
548 struct ist v;
549 uint32_t len, off;
550
551 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200552 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200553
554 v = htx_get_blk_value(htx, blk);
555 start = ctx->value.ptr - ctx->lws_before;
556 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
557 off = start - v.ptr;
558
559 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
560 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200561 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200562
563 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200564
565 sl = http_get_stline(htx);
566 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
567 struct ist n = htx_get_blk_name(htx, blk);
568
569 if (isteq(n, ist("host"))) {
570 if (!http_update_authority(htx, sl, v))
571 goto fail;
572 ctx->blk = NULL;
573 http_find_header(htx, ist("host"), ctx, 1);
574 blk = ctx->blk;
575 v = htx_get_blk_value(htx, blk);
576 }
577 }
578
Christopher Faulet47596d32018-10-22 09:17:28 +0200579 ctx->blk = blk;
580 ctx->value.ptr = v.ptr + off;
581 ctx->value.len = data.len;
582 ctx->lws_before = ctx->lws_after = 0;
583
584 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200585 fail:
586 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200587}
588
589/* Fully replaces a header referenced in the context <ctx> by the name <name>
590 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
591 * context is updated if necessary.
592 */
593int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
594 const struct ist name, const struct ist value)
595{
596 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200597 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200598
599 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200600 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200601
602 blk = htx_replace_header(htx, blk, name, value);
603 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200604 goto fail;
605
606 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100607 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200608 if (!http_update_authority(htx, sl, value))
609 goto fail;
610 ctx->blk = NULL;
611 http_find_header(htx, ist("host"), ctx, 1);
612 blk = ctx->blk;
613 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200614
615 ctx->blk = blk;
616 ctx->value = ist(NULL);
617 ctx->lws_before = ctx->lws_after = 0;
618
619 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200620 fail:
621 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200622}
623
624/* Remove one value of a header. This only works on a <ctx> returned by
625 * http_find_header function. The value is removed, as well as surrounding commas
626 * if any. If the removed value was alone, the whole header is removed. The
627 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
628 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
629 * form that can be handled by http_find_header() to find next occurrence.
630 */
631int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
632{
633 struct htx_blk *blk = ctx->blk;
634 char *start;
635 struct ist v;
636 uint32_t len;
637
638 if (!blk)
639 return 0;
640
641 start = ctx->value.ptr - ctx->lws_before;
642 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
643
644 v = htx_get_blk_value(htx, blk);
645 if (len == v.len) {
646 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200647 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200648 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100649 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200650 ctx->lws_before = ctx->lws_after = 0;
651 }
652 else {
653 ctx->blk = htx_get_blk(htx, htx->tail);
654 ctx->value = htx_get_blk_value(htx, ctx->blk);
655 ctx->lws_before = ctx->lws_after = 0;
656 }
657 return 1;
658 }
659
660 /* This was not the only value of this header. We have to remove the
661 * part pointed by ctx->value. If it is the last entry of the list, we
662 * remove the last separator.
663 */
664 if (start == v.ptr) {
665 /* It's the first header part but not the only one. So remove
666 * the comma after it. */
667 len++;
668 }
669 else {
670 /* There is at least one header part before the removed one. So
671 * remove the comma between them. */
672 start--;
673 len++;
674 }
675 /* Update the block content and its len */
676 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200677 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200678
679 /* Finally update the ctx */
680 ctx->value.ptr = start;
681 ctx->value.len = 0;
682 ctx->lws_before = ctx->lws_after = 0;
683
684 return 1;
685}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200686
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200687/* Updates the authority part of the uri with the value <host>. It happens when
688 * the header host is modified. It returns 0 on failure and 1 on success. It is
689 * the caller responsibility to provide the start-line and to be sure the uri
690 * contains an authority. Thus, if no authority is found in the uri, an error is
691 * returned.
692 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200693int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200694{
695 struct buffer *temp = get_trash_chunk();
696 struct ist meth, vsn, uri, authority;
697
698 uri = htx_sl_req_uri(sl);
699 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100700 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200701 return 0;
702
Christopher Faulet34b18e42020-02-18 11:02:21 +0100703 /* Don't update the uri if there is no change */
704 if (isteq(host, authority))
705 return 1;
706
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200707 /* Start by copying old method and version */
708 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
709 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
710
711 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
712 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
713
714 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
715 chunk_memcat(temp, host.ptr, host.len);
716 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
717 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
718
719 return http_replace_stline(htx, meth, uri, vsn);
720
721}
722
723/* Update the header host by extracting the authority of the uri <uri>. flags of
724 * the start-line are also updated accordingly. For orgin-form and asterisk-form
725 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
726 * removed from the flags of the start-line. Otherwise, this flag is set and the
727 * authority is used to set the value of the header host. This function returns
728 * 0 on failure and 1 on success.
729*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200730int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200731{
732 struct ist authority;
733 struct http_hdr_ctx ctx;
734
735 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
736 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
737 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
738 }
739 else {
740 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
741 if (sl->info.req.meth != HTTP_METH_CONNECT) {
742 // absolute-form (RFC7320 #5.3.2)
743 sl->flags |= HTX_SL_F_HAS_SCHM;
744 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
745 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
746
747 authority = http_get_authority(uri, 1);
748 if (!authority.len)
749 goto fail;
750 }
751 else {
752 // authority-form (RFC7320 #5.3.3)
753 authority = uri;
754 }
755
756 /* Replace header host value */
757 ctx.blk = NULL;
758 while (http_find_header(htx, ist("host"), &ctx, 1)) {
759 if (!http_replace_header_value(htx, &ctx, authority))
760 goto fail;
761 }
762
763 }
764 return 1;
765 fail:
766 return 0;
767}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200768
769/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
770 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
771 * performed over the whole headers. Otherwise it must contain a valid header
772 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
773 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
774 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
775 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
776 * -1. The value fetch stops at commas, so this function is suited for use with
777 * list headers.
778 * The return value is 0 if nothing was found, or non-zero otherwise.
779 */
780unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
781 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
782{
783 struct http_hdr_ctx local_ctx;
784 struct ist val_hist[MAX_HDR_HISTORY];
785 unsigned int hist_idx;
786 int found;
787
788 if (!ctx) {
789 local_ctx.blk = NULL;
790 ctx = &local_ctx;
791 }
792
793 if (occ >= 0) {
794 /* search from the beginning */
795 while (http_find_header(htx, hdr, ctx, 0)) {
796 occ--;
797 if (occ <= 0) {
798 *vptr = ctx->value.ptr;
799 *vlen = ctx->value.len;
800 return 1;
801 }
802 }
803 return 0;
804 }
805
806 /* negative occurrence, we scan all the list then walk back */
807 if (-occ > MAX_HDR_HISTORY)
808 return 0;
809
810 found = hist_idx = 0;
811 while (http_find_header(htx, hdr, ctx, 0)) {
812 val_hist[hist_idx] = ctx->value;
813 if (++hist_idx >= MAX_HDR_HISTORY)
814 hist_idx = 0;
815 found++;
816 }
817 if (-occ > found)
818 return 0;
819
820 /* OK now we have the last occurrence in [hist_idx-1], and we need to
821 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
822 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
823 * to remain in the 0..9 range.
824 */
825 hist_idx += occ + MAX_HDR_HISTORY;
826 if (hist_idx >= MAX_HDR_HISTORY)
827 hist_idx -= MAX_HDR_HISTORY;
828 *vptr = val_hist[hist_idx].ptr;
829 *vlen = val_hist[hist_idx].len;
830 return 1;
831}
832
833/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
834 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
835 * performed over the whole headers. Otherwise it must contain a valid header
836 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
837 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
838 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
839 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
840 * -1. This function differs from http_get_hdr() in that it only returns full
841 * line header values and does not stop at commas.
842 * The return value is 0 if nothing was found, or non-zero otherwise.
843 */
844unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
845 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
846{
847 struct http_hdr_ctx local_ctx;
848 struct ist val_hist[MAX_HDR_HISTORY];
849 unsigned int hist_idx;
850 int found;
851
852 if (!ctx) {
853 local_ctx.blk = NULL;
854 ctx = &local_ctx;
855 }
856
857 if (occ >= 0) {
858 /* search from the beginning */
859 while (http_find_header(htx, hdr, ctx, 1)) {
860 occ--;
861 if (occ <= 0) {
862 *vptr = ctx->value.ptr;
863 *vlen = ctx->value.len;
864 return 1;
865 }
866 }
867 return 0;
868 }
869
870 /* negative occurrence, we scan all the list then walk back */
871 if (-occ > MAX_HDR_HISTORY)
872 return 0;
873
874 found = hist_idx = 0;
875 while (http_find_header(htx, hdr, ctx, 1)) {
876 val_hist[hist_idx] = ctx->value;
877 if (++hist_idx >= MAX_HDR_HISTORY)
878 hist_idx = 0;
879 found++;
880 }
881 if (-occ > found)
882 return 0;
883
884 /* OK now we have the last occurrence in [hist_idx-1], and we need to
885 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
886 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
887 * to remain in the 0..9 range.
888 */
889 hist_idx += occ + MAX_HDR_HISTORY;
890 if (hist_idx >= MAX_HDR_HISTORY)
891 hist_idx -= MAX_HDR_HISTORY;
892 *vptr = val_hist[hist_idx].ptr;
893 *vlen = val_hist[hist_idx].len;
894 return 1;
895}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100896
Christopher Fauleta66adf42020-11-05 22:43:41 +0100897int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100898{
899 struct htx *htx;
900 struct htx_sl *sl;
901 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200902 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100903 union h1_sl h1sl;
904 unsigned int flags = HTX_SL_F_IS_RESP;
905 int ret = 0;
906
Christopher Faulet90cc4812019-07-22 16:49:30 +0200907 b_reset(buf);
908 if (!raw.len) {
909 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100910 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200911 return 1;
912 }
913
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100914 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200915 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100916 if (!buf->area)
917 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100918
919 h1m_init_res(&h1m);
920 h1m.flags |= H1_MF_NO_PHDR;
921 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
922 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100923 if (ret <= 0) {
Manu Nicolas9d0ad652023-01-09 01:31:06 +0000924 memprintf(errmsg, "unable to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100925 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100926 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100927
Christopher Fauleta66adf42020-11-05 22:43:41 +0100928 if (unlikely(h1sl.st.v.len != 8)) {
929 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100930 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100931 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100932 if ((*(h1sl.st.v.ptr + 5) > '1') ||
933 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
934 h1m.flags |= H1_MF_VER_11;
935
Christopher Fauleta66adf42020-11-05 22:43:41 +0100936 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
937 memprintf(errmsg, "invalid http status code for an error message (%u)",
938 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200939 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100940 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200941
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200942 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
943 /* Responses known to have no body. */
944 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
945 h1m.flags |= H1_MF_XFER_LEN;
946 h1m.curr_len = h1m.body_len = 0;
947 }
948 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
949 h1m.flags |= H1_MF_XFER_LEN;
950
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100951 if (h1m.flags & H1_MF_VER_11)
952 flags |= HTX_SL_F_VER_11;
953 if (h1m.flags & H1_MF_XFER_ENC)
954 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200955 if (h1m.flags & H1_MF_XFER_LEN) {
956 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100957 if (h1m.flags & H1_MF_CHNK) {
958 memprintf(errmsg, "chunk-encoded payload not supported");
959 goto error;
960 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200961 else if (h1m.flags & H1_MF_CLEN) {
962 flags |= HTX_SL_F_CLEN;
963 if (h1m.body_len == 0)
964 flags |= HTX_SL_F_BODYLESS;
965 }
966 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200967 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100968 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200969
Christopher Fauleta66adf42020-11-05 22:43:41 +0100970 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
971 memprintf(errmsg, "message payload not expected");
972 goto error;
973 }
974 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
975 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100976 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100977 goto error;
978 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100979
980 htx = htx_from_buf(buf);
981 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100982 if (!sl || !htx_add_all_headers(htx, hdrs)) {
983 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100984 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100985 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100986 sl->info.res.status = h1sl.st.status;
987
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200988 while (raw.len > ret) {
989 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100990 if (!sent) {
991 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100992 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100993 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200994 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100995 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200996
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100997 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200998
Christopher Faulet90cc4812019-07-22 16:49:30 +0200999 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001000
1001error:
1002 if (buf->size)
1003 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001004 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001005}
1006
Christopher Faulet18630642020-05-12 18:57:28 +02001007void release_http_reply(struct http_reply *http_reply)
1008{
1009 struct logformat_node *lf, *lfb;
1010 struct http_reply_hdr *hdr, *hdrb;
1011
1012 if (!http_reply)
1013 return;
1014
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001015 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001016 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001017 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001018 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001019 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001020 release_sample_expr(lf->expr);
1021 free(lf->arg);
1022 free(lf);
1023 }
1024 istfree(&hdr->name);
1025 free(hdr);
1026 }
1027
1028 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001029 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001030 }
1031 else if (http_reply->type == HTTP_REPLY_RAW)
1032 chunk_destroy(&http_reply->body.obj);
1033 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1034 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001035 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001036 release_sample_expr(lf->expr);
1037 free(lf->arg);
1038 free(lf);
1039 }
1040 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001041 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001042}
1043
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001044static int http_htx_init(void)
1045{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001046 struct buffer chk;
1047 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001048 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001049 int rc;
1050 int err_code = 0;
1051
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1053 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001054 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001055 err_code |= ERR_ALERT | ERR_FATAL;
1056 continue;
1057 }
1058
1059 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Fauleta66adf42020-11-05 22:43:41 +01001060 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1061 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1062 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001063 err_code |= ERR_ALERT | ERR_FATAL;
1064 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001065 else if (errmsg) {
1066 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1067 err_code |= ERR_WARN;
1068 }
1069
1070 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001071 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001072
Christopher Fauletf7346382019-07-17 22:02:08 +02001073 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001074 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1075 http_err_replies[rc].status = http_err_codes[rc];
1076 http_err_replies[rc].ctype = NULL;
1077 LIST_INIT(&http_err_replies[rc].hdrs);
1078 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001079 }
1080end:
1081 return err_code;
1082}
1083
Christopher Faulet58857752020-01-15 15:19:50 +01001084static void http_htx_deinit(void)
1085{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001086 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001087 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001088 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001089 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001090 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001091
1092 node = ebpt_first(&http_error_messages);
1093 while (node) {
1094 next = ebpt_next(node);
1095 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001096 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1097 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001098 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001099 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001100 node = next;
1101 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001102
1103 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1104 free(http_errs->conf.file);
1105 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001106 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1107 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001108 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001109 free(http_errs);
1110 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001111
1112 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001113 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001114 release_http_reply(http_rep);
1115 }
Christopher Faulet58857752020-01-15 15:19:50 +01001116}
1117
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001118REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001119REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001120
Christopher Faulet58857752020-01-15 15:19:50 +01001121/* Reads content of the error file <file> and convert it into an HTX message. On
1122 * success, the HTX message is returned. On error, NULL is returned and an error
1123 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001124 */
Christopher Faulet58857752020-01-15 15:19:50 +01001125struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001126{
Christopher Faulet58857752020-01-15 15:19:50 +01001127 struct buffer *buf = NULL;
1128 struct buffer chk;
1129 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001130 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001131 struct stat stat;
1132 char *err = NULL;
1133 int errnum, errlen;
1134 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001135
1136 /* already loaded */
1137 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1138 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001139 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1140 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001141 goto out;
1142 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001143
Christopher Faulet58857752020-01-15 15:19:50 +01001144 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001145 fd = open(file, O_RDONLY);
1146 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1147 memprintf(errmsg, "error opening file '%s'.", file);
1148 goto out;
1149 }
1150
1151 if (stat.st_size <= global.tune.bufsize)
1152 errlen = stat.st_size;
1153 else {
1154 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1155 file, global.tune.bufsize);
1156 errlen = global.tune.bufsize;
1157 }
1158
1159 err = malloc(errlen);
1160 if (!err) {
1161 memprintf(errmsg, "out of memory.");
1162 goto out;
1163 }
1164
1165 errnum = read(fd, err, errlen);
1166 if (errnum != errlen) {
1167 memprintf(errmsg, "error reading file '%s'.", file);
1168 goto out;
1169 }
1170
Christopher Faulet58857752020-01-15 15:19:50 +01001171 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001172 http_errmsg = calloc(1, sizeof(*http_errmsg));
1173 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001174 memprintf(errmsg, "out of memory.");
1175 goto out;
1176 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001177 http_errmsg->node.key = strdup(file);
1178 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001179 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001180 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001181 goto out;
1182 }
1183
1184 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001185 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1186 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001187 free(http_errmsg->node.key);
1188 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001189 goto out;
1190 }
1191
Christopher Faulet58857752020-01-15 15:19:50 +01001192 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001193 http_errmsg->msg = chk;
1194 ebis_insert(&http_error_messages, &http_errmsg->node);
1195 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001196
Christopher Faulet5031ef52020-01-15 11:22:07 +01001197 out:
1198 if (fd >= 0)
1199 close(fd);
1200 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001201 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001202}
1203
Ilya Shipitsind4259502020-04-08 01:07:56 +05001204/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001205 * message is returned. On error, NULL is returned and an error message is
1206 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001207 */
Christopher Faulet58857752020-01-15 15:19:50 +01001208struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001209{
Christopher Faulet58857752020-01-15 15:19:50 +01001210 struct buffer *buf = NULL;
1211 struct buffer chk;
1212 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001213 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001214
1215 /* already loaded */
1216 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1217 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001218 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1219 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001220 goto out;
1221 }
1222 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001223 http_errmsg = calloc(1, sizeof(*http_errmsg));
1224 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001225 memprintf(errmsg, "out of memory.");
1226 goto out;
1227 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001228 http_errmsg->node.key = strdup(key);
1229 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001230 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001231 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001232 goto out;
1233 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001234
1235 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001236 if (!http_str_to_htx(&chk, msg, errmsg)) {
1237 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001238 free(http_errmsg->node.key);
1239 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001240 goto out;
1241 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001242
Christopher Faulet58857752020-01-15 15:19:50 +01001243 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001244 http_errmsg->msg = chk;
1245 ebis_insert(&http_error_messages, &http_errmsg->node);
1246 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001247 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001248 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001249}
1250
Christopher Faulet5031ef52020-01-15 11:22:07 +01001251/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001252 * <status>. It returns NULL if there is any error, otherwise it return the
1253 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001254 */
Christopher Faulet58857752020-01-15 15:19:50 +01001255struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001256{
Christopher Faulet58857752020-01-15 15:19:50 +01001257 struct buffer *buf = NULL;
1258 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001259
1260 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1261 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001262 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001263 break;
1264 }
1265 }
1266
1267 if (rc >= HTTP_ERR_SIZE)
1268 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001269 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001270}
1271
1272/* This function creates HTX error message corresponding to a redirect message
1273 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001274 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1275 * returns NULL if there is any error, otherwise it return the corresponding HTX
1276 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001277 */
Christopher Faulet58857752020-01-15 15:19:50 +01001278struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001279{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001280 static const char *HTTP_302 =
1281 "HTTP/1.1 302 Found\r\n"
1282 "Cache-Control: no-cache\r\n"
1283 "Content-length: 0\r\n"
1284 "Location: "; /* not terminated since it will be concatenated with the URL */
1285 static const char *HTTP_303 =
1286 "HTTP/1.1 303 See Other\r\n"
1287 "Cache-Control: no-cache\r\n"
1288 "Content-length: 0\r\n"
1289 "Location: "; /* not terminated since it will be concatenated with the URL */
1290
Christopher Faulet58857752020-01-15 15:19:50 +01001291 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001292 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001293 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001294 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001295
1296 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1297 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001298 /* Create the error key */
1299 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1300 memprintf(errmsg, "out of memory.");
1301 goto out;
1302 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001303 /* Create the error message */
1304 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1305 errlen = strlen(msg) + strlen(url) + 5;
1306 err = malloc(errlen);
1307 if (!err) {
1308 memprintf(errmsg, "out of memory.");
1309 goto out;
1310 }
1311 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1312
1313 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001314 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001315 break;
1316 }
1317 }
1318
1319 if (rc >= HTTP_ERR_SIZE)
1320 memprintf(errmsg, "status code '%d' not handled.", status);
1321out:
Christopher Faulet58857752020-01-15 15:19:50 +01001322 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001323 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001324 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001325}
1326
Christopher Faulet7eea2412020-05-13 15:02:59 +02001327/* Check an "http reply" and, for replies referencing an http-errors section,
1328 * try to find the right section and the right error message in this section. If
1329 * found, the reply is updated. If the http-errors section exists but the error
1330 * message is not found, no error message is set to fallback on the default
1331 * ones. Otherwise (unknown section) an error is returned.
1332 *
1333 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1334 * filled.
1335 */
1336int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1337{
1338 struct http_errors *http_errs;
1339 int ret = 1;
1340
1341 if (reply->type != HTTP_REPLY_ERRFILES)
1342 goto end;
1343
1344 list_for_each_entry(http_errs, &http_errors_list, list) {
1345 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001346 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001347 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001348 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1349 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001350 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1351 "not declared in http-errors section '%s'.\n",
1352 px->id, reply->status, http_errs->id);
1353 break;
1354 }
1355 }
1356
1357 if (&http_errs->list == &http_errors_list) {
1358 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1359 reply->body.http_errors);
1360 ret = 0;
1361 }
1362
1363 end:
1364 return ret;
1365}
1366
Christopher Faulet47e791e2020-05-13 14:36:55 +02001367/* Parse an "http reply". It returns the reply on success or NULL on error. This
1368 * function creates one of the following http replies :
1369 *
1370 * - HTTP_REPLY_EMPTY : dummy response, no payload
1371 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1372 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1373 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1374 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1375 *
1376 * The content-type must be defined for non-empty payload. It is ignored for
1377 * error messages (implicit or explicit). When an http-errors section is
1378 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1379 * during the configuration validity check or dynamically. It is the caller
1380 * responsibility to choose. If no status code is configured, <default_status>
1381 * is set.
1382 */
1383struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1384 int default_status, char **errmsg)
1385{
1386 struct logformat_node *lf, *lfb;
1387 struct http_reply *reply = NULL;
1388 struct http_reply_hdr *hdr, *hdrb;
1389 struct stat stat;
1390 const char *act_arg = NULL;
1391 char *obj = NULL;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001392 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001393
1394
1395 reply = calloc(1, sizeof(*reply));
1396 if (!reply) {
1397 memprintf(errmsg, "out of memory");
1398 goto error;
1399 }
1400 LIST_INIT(&reply->hdrs);
1401 reply->type = HTTP_REPLY_EMPTY;
1402 reply->status = default_status;
1403
Christopher Faulet3b967c12020-05-15 15:47:44 +02001404 if (px->conf.args.ctx == ARGC_HERR)
1405 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001406 else {
1407 if (px->cap & PR_CAP_FE)
1408 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1409 if (px->cap & PR_CAP_BE)
Willy Tarreauad745102021-10-16 14:41:09 +02001410 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001411 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001412
1413 cur_arg = *orig_arg;
1414 while (*args[cur_arg]) {
1415 if (strcmp(args[cur_arg], "status") == 0) {
1416 cur_arg++;
1417 if (!*args[cur_arg]) {
1418 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1419 goto error;
1420 }
1421 reply->status = atol(args[cur_arg]);
1422 if (reply->status < 200 || reply->status > 599) {
1423 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1424 goto error;
1425 }
1426 cur_arg++;
1427 }
1428 else if (strcmp(args[cur_arg], "content-type") == 0) {
1429 cur_arg++;
1430 if (!*args[cur_arg]) {
1431 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1432 goto error;
1433 }
1434 free(reply->ctype);
1435 reply->ctype = strdup(args[cur_arg]);
1436 cur_arg++;
1437 }
1438 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1439 if (reply->type != HTTP_REPLY_EMPTY) {
1440 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1441 goto error;
1442 }
1443 act_arg = args[cur_arg];
1444 cur_arg++;
1445 if (!*args[cur_arg]) {
1446 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1447 goto error;
1448 }
1449 reply->body.http_errors = strdup(args[cur_arg]);
1450 if (!reply->body.http_errors) {
1451 memprintf(errmsg, "out of memory");
1452 goto error;
1453 }
1454 reply->type = HTTP_REPLY_ERRFILES;
1455 cur_arg++;
1456 }
1457 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1458 if (reply->type != HTTP_REPLY_EMPTY) {
1459 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1460 goto error;
1461 }
1462 act_arg = args[cur_arg];
1463 reply->type = HTTP_REPLY_ERRMSG;
1464 cur_arg++;
1465 }
1466 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1467 if (reply->type != HTTP_REPLY_EMPTY) {
1468 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1469 goto error;
1470 }
1471 act_arg = args[cur_arg];
1472 cur_arg++;
1473 if (!*args[cur_arg]) {
1474 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1475 goto error;
1476 }
1477 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1478 if (!reply->body.errmsg) {
1479 goto error;
1480 }
1481 reply->type = HTTP_REPLY_ERRMSG;
1482 cur_arg++;
1483 }
1484 else if (strcmp(args[cur_arg], "file") == 0) {
1485 if (reply->type != HTTP_REPLY_EMPTY) {
1486 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1487 goto error;
1488 }
1489 act_arg = args[cur_arg];
1490 cur_arg++;
1491 if (!*args[cur_arg]) {
1492 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1493 goto error;
1494 }
1495 fd = open(args[cur_arg], O_RDONLY);
1496 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1497 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1498 goto error;
1499 }
1500 if (stat.st_size > global.tune.bufsize) {
1501 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1502 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1503 goto error;
1504 }
1505 objlen = stat.st_size;
1506 obj = malloc(objlen);
1507 if (!obj || read(fd, obj, objlen) != objlen) {
1508 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1509 goto error;
1510 }
1511 close(fd);
1512 fd = -1;
1513 reply->type = HTTP_REPLY_RAW;
1514 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1515 obj = NULL;
1516 cur_arg++;
1517 }
1518 else if (strcmp(args[cur_arg], "string") == 0) {
1519 if (reply->type != HTTP_REPLY_EMPTY) {
1520 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1521 goto error;
1522 }
1523 act_arg = args[cur_arg];
1524 cur_arg++;
1525 if (!*args[cur_arg]) {
1526 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1527 goto error;
1528 }
1529 obj = strdup(args[cur_arg]);
1530 objlen = strlen(args[cur_arg]);
1531 if (!obj) {
1532 memprintf(errmsg, "out of memory");
1533 goto error;
1534 }
1535 reply->type = HTTP_REPLY_RAW;
1536 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1537 obj = NULL;
1538 cur_arg++;
1539 }
1540 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1541 if (reply->type != HTTP_REPLY_EMPTY) {
1542 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1543 goto error;
1544 }
1545 act_arg = args[cur_arg];
1546 cur_arg++;
1547 if (!*args[cur_arg]) {
1548 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1549 goto error;
1550 }
1551 fd = open(args[cur_arg], O_RDONLY);
1552 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1553 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1554 goto error;
1555 }
1556 if (stat.st_size > global.tune.bufsize) {
1557 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1558 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1559 goto error;
1560 }
1561 objlen = stat.st_size;
1562 obj = malloc(objlen + 1);
1563 if (!obj || read(fd, obj, objlen) != objlen) {
1564 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1565 goto error;
1566 }
1567 close(fd);
1568 fd = -1;
1569 obj[objlen] = '\0';
1570 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001571 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001572 cur_arg++;
1573 }
1574 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1575 if (reply->type != HTTP_REPLY_EMPTY) {
1576 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1577 goto error;
1578 }
1579 act_arg = args[cur_arg];
1580 cur_arg++;
1581 if (!*args[cur_arg]) {
1582 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1583 goto error;
1584 }
1585 obj = strdup(args[cur_arg]);
1586 objlen = strlen(args[cur_arg]);
1587 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001588 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001589 cur_arg++;
1590 }
1591 else if (strcmp(args[cur_arg], "hdr") == 0) {
1592 cur_arg++;
1593 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1594 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1595 goto error;
1596 }
1597 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1598 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1599 strcasecmp(args[cur_arg], "content-type") == 0) {
1600 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1601 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1602 cur_arg += 2;
1603 continue;
1604 }
1605 hdr = calloc(1, sizeof(*hdr));
1606 if (!hdr) {
1607 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1608 goto error;
1609 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001610 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001611 LIST_INIT(&hdr->value);
1612 hdr->name = ist(strdup(args[cur_arg]));
1613 if (!isttest(hdr->name)) {
1614 memprintf(errmsg, "out of memory");
1615 goto error;
1616 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001617 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1618 goto error;
1619
1620 free(px->conf.lfs_file);
1621 px->conf.lfs_file = strdup(px->conf.args.file);
1622 px->conf.lfs_line = px->conf.args.line;
1623 cur_arg += 2;
1624 }
1625 else
1626 break;
1627 }
1628
1629 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1630 if (reply->ctype) {
1631 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1632 " neither errorfile nor payload defined.\n",
1633 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001634 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001635 }
1636 }
1637 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1638
1639 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1640 /* default errorfile or errorfiles: check the status */
1641 int rc;
1642
1643 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1644 if (http_err_codes[rc] == reply->status)
1645 break;
1646 }
1647
1648 if (rc >= HTTP_ERR_SIZE) {
1649 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1650 reply->status, act_arg);
1651 goto error;
1652 }
1653 }
1654
1655 if (reply->ctype) {
1656 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1657 "with an erorrfile.\n",
1658 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001659 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001660 }
1661 if (!LIST_ISEMPTY(&reply->hdrs)) {
1662 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1663 "with an erorrfile.\n",
1664 px->conf.args.file, px->conf.args.line);
1665 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001666 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001667 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001668 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001669 release_sample_expr(lf->expr);
1670 free(lf->arg);
1671 free(lf);
1672 }
1673 istfree(&hdr->name);
1674 free(hdr);
1675 }
1676 }
1677 }
1678 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001679 if ((reply->status == 204 || reply->status == 304) && objlen) {
1680 memprintf(errmsg, "No body expected for %d responses", reply->status);
1681 goto error;
1682 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001683 if (!reply->ctype && objlen) {
1684 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1685 goto error;
1686 }
1687 if (reply->ctype && !b_data(&reply->body.obj)) {
1688 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001689 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001690 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001691 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001692 }
1693 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1694 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1695 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1696 px->conf.args.file, px->conf.args.line);
1697 }
1698 }
1699 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1700 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001701 if ((reply->status == 204 || reply->status == 304)) {
1702 memprintf(errmsg, "No body expected for %d responses", reply->status);
1703 goto error;
1704 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001705 if (!reply->ctype) {
1706 memprintf(errmsg, "a content type must be defined with a log-format payload");
1707 goto error;
1708 }
1709 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1710 goto error;
1711
1712 free(px->conf.lfs_file);
1713 px->conf.lfs_file = strdup(px->conf.args.file);
1714 px->conf.lfs_line = px->conf.args.line;
1715 }
1716
1717 free(obj);
1718 *orig_arg = cur_arg;
1719 return reply;
1720
1721 error:
1722 free(obj);
1723 if (fd >= 0)
1724 close(fd);
1725 release_http_reply(reply);
1726 return NULL;
1727}
1728
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001729/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1730 * Returns 0 if no error has been found else non-zero.
1731 *
1732 * The normalization is processed on the target-uri at the condition that it is
1733 * in absolute-form. In the case where the target-uri was normalized, every
1734 * host headers values found are also replaced by the normalized hostname. This
1735 * assumes that the target-uri and host headers were properly identify as
1736 * similar before calling this function.
1737 */
1738int http_scheme_based_normalize(struct htx *htx)
1739{
1740 struct http_hdr_ctx ctx;
1741 struct htx_sl *sl;
1742 struct ist uri, scheme, authority, host, port;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001743
1744 sl = http_get_stline(htx);
1745
1746 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1747 return 0;
1748
1749 uri = htx_sl_req_uri(sl);
1750
1751 scheme = http_get_scheme(uri);
1752 /* if no scheme found, no normalization to proceed */
1753 if (!isttest(scheme))
1754 return 0;
1755
Christopher Fauletbe50b142022-07-05 10:24:52 +02001756 /* Extract the port if present in authority */
1757 authority = http_get_authority(uri, 1);
1758 port = http_get_host_port(authority);
1759 if (!isttest(port)) {
1760 /* if no port found, no normalization to proceed */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001761 return 0;
Christopher Fauletbe50b142022-07-05 10:24:52 +02001762 }
1763 host = isttrim(authority, istlen(authority) - istlen(port) - 1);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001764
Christopher Fauletbe50b142022-07-05 10:24:52 +02001765 if (istlen(port) && http_is_default_port(scheme, port)) {
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001766 /* reconstruct the uri with removal of the port */
1767 struct buffer *temp = get_trash_chunk();
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001768 struct ist meth, vsn;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001769
1770 /* meth */
1771 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1772 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1773
1774 /* vsn */
1775 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1776 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1777
1778 /* reconstruct uri without port */
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001779 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001780 chunk_istcat(temp, host);
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001781 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
1782 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001783
1784 http_replace_stline(htx, meth, uri, vsn);
1785
1786 /* replace every host headers values by the normalized host */
1787 ctx.blk = NULL;
1788 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1789 if (!http_replace_header_value(htx, &ctx, host))
1790 goto fail;
1791 }
1792 }
1793
1794 return 0;
1795
1796 fail:
1797 return 1;
1798}
1799
Christopher Faulet07f41f72020-01-16 16:16:06 +01001800/* Parses the "errorloc[302|303]" proxy keyword */
1801static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001802 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001803 char **errmsg)
1804{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001805 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001806 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001807 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001808 int errloc, status;
1809 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001810
1811 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1812 ret = 1;
1813 goto out;
1814 }
1815
1816 if (*(args[1]) == 0 || *(args[2]) == 0) {
1817 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1818 ret = -1;
1819 goto out;
1820 }
1821
1822 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001823 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001824 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1825 if (!msg) {
1826 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1827 ret = -1;
1828 goto out;
1829 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001830
1831 reply = calloc(1, sizeof(*reply));
1832 if (!reply) {
1833 memprintf(errmsg, "%s : out of memory.", args[0]);
1834 ret = -1;
1835 goto out;
1836 }
1837 reply->type = HTTP_REPLY_ERRMSG;
1838 reply->status = status;
1839 reply->ctype = NULL;
1840 LIST_INIT(&reply->hdrs);
1841 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001842 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001843
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001844 conf_err = calloc(1, sizeof(*conf_err));
1845 if (!conf_err) {
1846 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001847 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001848 ret = -1;
1849 goto out;
1850 }
1851 conf_err->type = 1;
1852 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001853 conf_err->info.errorfile.reply = reply;
1854
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001855 conf_err->file = strdup(file);
1856 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001857 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001858
Christopher Fauleta66adf42020-11-05 22:43:41 +01001859 /* handle warning message */
1860 if (*errmsg)
1861 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001862 out:
1863 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001864
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001865}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001866
1867/* Parses the "errorfile" proxy keyword */
1868static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001869 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001870 char **errmsg)
1871{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001872 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001873 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001874 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001875 int status;
1876 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001877
1878 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1879 ret = 1;
1880 goto out;
1881 }
1882
1883 if (*(args[1]) == 0 || *(args[2]) == 0) {
1884 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1885 ret = -1;
1886 goto out;
1887 }
1888
1889 status = atol(args[1]);
1890 msg = http_parse_errorfile(status, args[2], errmsg);
1891 if (!msg) {
1892 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1893 ret = -1;
1894 goto out;
1895 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001896
Christopher Faulet5809e102020-05-14 17:31:52 +02001897 reply = calloc(1, sizeof(*reply));
1898 if (!reply) {
1899 memprintf(errmsg, "%s : out of memory.", args[0]);
1900 ret = -1;
1901 goto out;
1902 }
1903 reply->type = HTTP_REPLY_ERRMSG;
1904 reply->status = status;
1905 reply->ctype = NULL;
1906 LIST_INIT(&reply->hdrs);
1907 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001908 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001909
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001910 conf_err = calloc(1, sizeof(*conf_err));
1911 if (!conf_err) {
1912 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001913 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001914 ret = -1;
1915 goto out;
1916 }
1917 conf_err->type = 1;
1918 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001919 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001920 conf_err->file = strdup(file);
1921 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001922 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001923
Christopher Fauleta66adf42020-11-05 22:43:41 +01001924 /* handle warning message */
1925 if (*errmsg)
1926 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001927 out:
1928 return ret;
1929
1930}
1931
1932/* Parses the "errorfiles" proxy keyword */
1933static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001934 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001935 char **err)
1936{
1937 struct conf_errors *conf_err = NULL;
1938 char *name = NULL;
1939 int rc, ret = 0;
1940
1941 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1942 ret = 1;
1943 goto out;
1944 }
1945
1946 if (!*(args[1])) {
1947 memprintf(err, "%s : expects <name> as argument.", args[0]);
1948 ret = -1;
1949 goto out;
1950 }
1951
1952 name = strdup(args[1]);
1953 conf_err = calloc(1, sizeof(*conf_err));
1954 if (!name || !conf_err) {
1955 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001956 goto error;
1957 }
1958 conf_err->type = 0;
1959
1960 conf_err->info.errorfiles.name = name;
1961 if (!*(args[2])) {
1962 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1963 conf_err->info.errorfiles.status[rc] = 1;
1964 }
1965 else {
1966 int cur_arg, status;
1967 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1968 status = atol(args[cur_arg]);
1969
1970 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1971 if (http_err_codes[rc] == status) {
1972 conf_err->info.errorfiles.status[rc] = 2;
1973 break;
1974 }
1975 }
1976 if (rc >= HTTP_ERR_SIZE) {
1977 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001978 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001979 }
1980 }
1981 }
1982 conf_err->file = strdup(file);
1983 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001984 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001985 out:
1986 return ret;
1987
1988 error:
1989 free(name);
1990 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001991 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001992 goto out;
1993}
1994
Christopher Faulet3b967c12020-05-15 15:47:44 +02001995/* Parses the "http-error" proxy keyword */
1996static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001997 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02001998 char **errmsg)
1999{
2000 struct conf_errors *conf_err;
2001 struct http_reply *reply = NULL;
2002 int rc, cur_arg, ret = 0;
2003
2004 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2005 ret = 1;
2006 goto out;
2007 }
2008
2009 cur_arg = 1;
2010 curpx->conf.args.ctx = ARGC_HERR;
2011 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2012 if (!reply) {
2013 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2014 goto error;
2015 }
2016 else if (!reply->status) {
2017 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2018 goto error;
2019 }
2020
2021 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2022 if (http_err_codes[rc] == reply->status)
2023 break;
2024 }
2025
2026 if (rc >= HTTP_ERR_SIZE) {
2027 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2028 goto error;
2029 }
2030 if (*args[cur_arg]) {
2031 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2032 goto error;
2033 }
2034
2035 conf_err = calloc(1, sizeof(*conf_err));
2036 if (!conf_err) {
2037 memprintf(errmsg, "%s : out of memory.", args[0]);
2038 goto error;
2039 }
2040 if (reply->type == HTTP_REPLY_ERRFILES) {
2041 int rc = http_get_status_idx(reply->status);
2042
2043 conf_err->type = 2;
2044 conf_err->info.errorfiles.name = reply->body.http_errors;
2045 conf_err->info.errorfiles.status[rc] = 2;
2046 reply->body.http_errors = NULL;
2047 release_http_reply(reply);
2048 }
2049 else {
2050 conf_err->type = 1;
2051 conf_err->info.errorfile.status = reply->status;
2052 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002053 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002054 }
2055 conf_err->file = strdup(file);
2056 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002057 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002058
Christopher Faulet3005d282020-11-13 10:58:01 +01002059 /* handle warning message */
2060 if (*errmsg)
2061 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002062 out:
2063 return ret;
2064
2065 error:
2066 release_http_reply(reply);
2067 ret = -1;
2068 goto out;
2069
2070}
2071
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002072/* Check "errorfiles" proxy keyword */
2073static int proxy_check_errors(struct proxy *px)
2074{
2075 struct conf_errors *conf_err, *conf_err_back;
2076 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002077 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002078
2079 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2080 if (conf_err->type == 1) {
2081 /* errorfile */
2082 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002083 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002084
2085 /* For proxy, to rely on default replies, just don't reference a reply */
2086 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2087 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002088 }
2089 else {
2090 /* errorfiles */
2091 list_for_each_entry(http_errs, &http_errors_list, list) {
2092 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2093 break;
2094 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002095
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002096 /* unknown http-errors section */
2097 if (&http_errs->list == &http_errors_list) {
2098 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2099 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2100 err |= ERR_ALERT | ERR_FATAL;
2101 free(conf_err->info.errorfiles.name);
2102 goto next;
2103 }
2104
2105 free(conf_err->info.errorfiles.name);
2106 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2107 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002108 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002109 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002110 else if (conf_err->info.errorfiles.status[rc] == 2)
2111 ha_warning("config: proxy '%s' : status '%d' not declared in"
2112 " http-errors section '%s' (at %s:%d).\n",
2113 px->id, http_err_codes[rc], http_errs->id,
2114 conf_err->file, conf_err->line);
2115 }
2116 }
2117 }
2118 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002119 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002120 free(conf_err->file);
2121 free(conf_err);
2122 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002123
2124 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002125 return err;
2126}
2127
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002128static int post_check_errors()
2129{
2130 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002131 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002132 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002133 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002134
2135 node = ebpt_first(&http_error_messages);
2136 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002137 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2138 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002139 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002140 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002141 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2142 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002143 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002144 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002145 (char *)node->key);
2146 err_code |= ERR_WARN;
2147 }
2148 next:
2149 node = ebpt_next(node);
2150 }
2151
2152 return err_code;
2153}
2154
Willy Tarreau016255a2021-02-12 08:40:29 +01002155int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002156{
2157 struct conf_errors *conf_err, *new_conf_err = NULL;
2158 int ret = 0;
2159
2160 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2161 new_conf_err = calloc(1, sizeof(*new_conf_err));
2162 if (!new_conf_err) {
2163 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2164 goto out;
2165 }
2166 new_conf_err->type = conf_err->type;
2167 if (conf_err->type == 1) {
2168 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002169 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002170 }
2171 else {
2172 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2173 if (!new_conf_err->info.errorfiles.name) {
2174 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2175 goto out;
2176 }
2177 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2178 sizeof(conf_err->info.errorfiles.status));
2179 }
2180 new_conf_err->file = strdup(conf_err->file);
2181 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002182 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002183 new_conf_err = NULL;
2184 }
2185 ret = 1;
2186
2187 out:
2188 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002189 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002190}
2191
2192void proxy_release_conf_errors(struct proxy *px)
2193{
2194 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002195
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002196 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2197 if (conf_err->type == 0)
2198 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002199 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002200 free(conf_err->file);
2201 free(conf_err);
2202 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002203}
2204
2205/*
2206 * Parse an <http-errors> section.
2207 * Returns the error code, 0 if OK, or any combination of :
2208 * - ERR_ABORT: must abort ASAP
2209 * - ERR_FATAL: we can continue parsing but not start the service
2210 * - ERR_WARN: a warning has been emitted
2211 * - ERR_ALERT: an alert has been emitted
2212 * Only the two first ones can stop processing, the two others are just
2213 * indicators.
2214 */
2215static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2216{
2217 static struct http_errors *curr_errs = NULL;
2218 int err_code = 0;
2219 const char *err;
2220 char *errmsg = NULL;
2221
2222 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2223 if (!*args[1]) {
2224 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2225 err_code |= ERR_ALERT | ERR_ABORT;
2226 goto out;
2227 }
2228
2229 err = invalid_char(args[1]);
2230 if (err) {
2231 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2232 file, linenum, *err, args[0], args[1]);
2233 err_code |= ERR_ALERT | ERR_FATAL;
2234 }
2235
2236 list_for_each_entry(curr_errs, &http_errors_list, list) {
2237 /* Error if two errors section owns the same name */
2238 if (strcmp(curr_errs->id, args[1]) == 0) {
2239 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2240 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2241 err_code |= ERR_ALERT | ERR_FATAL;
2242 }
2243 }
2244
2245 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2246 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2247 err_code |= ERR_ALERT | ERR_ABORT;
2248 goto out;
2249 }
2250
Willy Tarreau2b718102021-04-21 07:32:39 +02002251 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002252 curr_errs->id = strdup(args[1]);
2253 curr_errs->conf.file = strdup(file);
2254 curr_errs->conf.line = linenum;
2255 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002256 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002257 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002258 struct buffer *msg;
2259 int status, rc;
2260
2261 if (*(args[1]) == 0 || *(args[2]) == 0) {
2262 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2263 file, linenum, args[0]);
2264 err_code |= ERR_ALERT | ERR_FATAL;
2265 goto out;
2266 }
2267
2268 status = atol(args[1]);
2269 msg = http_parse_errorfile(status, args[2], &errmsg);
2270 if (!msg) {
2271 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
2274 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002275 if (errmsg) {
2276 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2277 err_code |= ERR_WARN;
2278 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002279
2280 reply = calloc(1, sizeof(*reply));
2281 if (!reply) {
2282 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2283 err_code |= ERR_ALERT | ERR_FATAL;
2284 goto out;
2285 }
2286 reply->type = HTTP_REPLY_ERRMSG;
2287 reply->status = status;
2288 reply->ctype = NULL;
2289 LIST_INIT(&reply->hdrs);
2290 reply->body.errmsg = msg;
2291
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002292 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002293 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002294 }
2295 else if (*args[0] != 0) {
2296 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2297 err_code |= ERR_ALERT | ERR_FATAL;
2298 goto out;
2299 }
2300
2301out:
2302 free(errmsg);
2303 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002304}
2305
2306static struct cfg_kw_list cfg_kws = {ILH, {
2307 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2308 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2309 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2310 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002311 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002312 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002313 { 0, NULL, NULL },
2314}};
2315
2316INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002317REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002318REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002319
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002320REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2321
Christopher Faulet29f72842019-12-11 15:52:32 +01002322/************************************************************************/
2323/* HTX sample fetches */
2324/************************************************************************/
2325
2326/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2327static int
2328smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2329{
2330 if (!smp->strm)
2331 return 0;
2332
2333 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2334 smp->data.type = SMP_T_BOOL;
2335 return 1;
2336}
2337
2338/* Returns the number of blocks in an HTX message. The channel is chosen
2339 * depending on the sample direction. */
2340static int
2341smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2342{
2343 struct channel *chn;
2344 struct htx *htx;
2345
2346 if (!smp->strm)
2347 return 0;
2348
2349 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002350 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002351 if (!htx)
2352 return 0;
2353
2354 smp->data.u.sint = htx_nbblks(htx);
2355 smp->data.type = SMP_T_SINT;
2356 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2357 return 1;
2358}
2359
2360/* Returns the size of an HTX message. The channel is chosen depending on the
2361 * sample direction. */
2362static int
2363smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2364{
2365 struct channel *chn;
2366 struct htx *htx;
2367
2368 if (!smp->strm)
2369 return 0;
2370
2371 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002372 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002373 if (!htx)
2374 return 0;
2375
2376 smp->data.u.sint = htx->size;
2377 smp->data.type = SMP_T_SINT;
2378 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2379 return 1;
2380}
2381
2382/* Returns the data size of an HTX message. The channel is chosen depending on the
2383 * sample direction. */
2384static int
2385smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2386{
2387 struct channel *chn;
2388 struct htx *htx;
2389
2390 if (!smp->strm)
2391 return 0;
2392
2393 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002394 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002395 if (!htx)
2396 return 0;
2397
2398 smp->data.u.sint = htx->data;
2399 smp->data.type = SMP_T_SINT;
2400 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2401 return 1;
2402}
2403
2404/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2405 * depending on the sample direction. */
2406static int
2407smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2408{
2409 struct channel *chn;
2410 struct htx *htx;
2411
2412 if (!smp->strm)
2413 return 0;
2414
2415 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002416 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002417 if (!htx)
2418 return 0;
2419
2420 smp->data.u.sint = htx_used_space(htx);
2421 smp->data.type = SMP_T_SINT;
2422 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2423 return 1;
2424}
2425
2426/* Returns the free space (size-used) of an HTX message. The channel is chosen
2427 * depending on the sample direction. */
2428static int
2429smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2430{
2431 struct channel *chn;
2432 struct htx *htx;
2433
2434 if (!smp->strm)
2435 return 0;
2436
2437 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002438 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002439 if (!htx)
2440 return 0;
2441
2442 smp->data.u.sint = htx_free_space(htx);
2443 smp->data.type = SMP_T_SINT;
2444 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2445 return 1;
2446}
2447
2448/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2449 * channel is chosen depending on the sample direction. */
2450static int
2451smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2452{
2453 struct channel *chn;
2454 struct htx *htx;
2455
2456 if (!smp->strm)
2457 return 0;
2458
2459 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002460 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002461 if (!htx)
2462 return 0;
2463
2464 smp->data.u.sint = htx_free_data_space(htx);
2465 smp->data.type = SMP_T_SINT;
2466 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2467 return 1;
2468}
2469
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002470/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2471 * channel is chosen depending on the sample direction.
2472 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002473static int
2474smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2475{
2476 struct channel *chn;
2477 struct htx *htx;
2478
2479 if (!smp->strm)
2480 return 0;
2481
2482 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002483 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002484 if (!htx)
2485 return 0;
2486
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002487 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002488 smp->data.type = SMP_T_BOOL;
2489 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2490 return 1;
2491}
2492
2493/* Returns the type of a specific HTX block, if found in the message. Otherwise
2494 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2495 * "head", "tail" or "first". The channel is chosen depending on the sample
2496 * direction. */
2497static int
2498smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2499{
2500 struct channel *chn;
2501 struct htx *htx;
2502 enum htx_blk_type type;
2503 int32_t pos;
2504
2505 if (!smp->strm || !arg_p)
2506 return 0;
2507
2508 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002509 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002510 if (!htx)
2511 return 0;
2512
2513 pos = arg_p[0].data.sint;
2514 if (pos == -1)
2515 type = htx_get_head_type(htx);
2516 else if (pos == -2)
2517 type = htx_get_tail_type(htx);
2518 else if (pos == -3)
2519 type = htx_get_first_type(htx);
2520 else
2521 type = ((pos >= htx->head && pos <= htx->tail)
2522 ? htx_get_blk_type(htx_get_blk(htx, pos))
2523 : HTX_BLK_UNUSED);
2524
2525 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2526 smp->data.type = SMP_T_STR;
2527 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2528 return 1;
2529}
2530
2531/* Returns the size of a specific HTX block, if found in the message. Otherwise
2532 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2533 * "first". The channel is chosen depending on the sample direction. */
2534static int
2535smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2536{
2537 struct channel *chn;
2538 struct htx *htx;
2539 struct htx_blk *blk;
2540 int32_t pos;
2541
2542 if (!smp->strm || !arg_p)
2543 return 0;
2544
2545 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002546 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002547 if (!htx)
2548 return 0;
2549
2550 pos = arg_p[0].data.sint;
2551 if (pos == -1)
2552 blk = htx_get_head_blk(htx);
2553 else if (pos == -2)
2554 blk = htx_get_tail_blk(htx);
2555 else if (pos == -3)
2556 blk = htx_get_first_blk(htx);
2557 else
2558 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2559
2560 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2561 smp->data.type = SMP_T_SINT;
2562 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2563 return 1;
2564}
2565
2566/* Returns the start-line if the selected HTX block exists and is a
2567 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2568 * supported or "head", "tail" or "first". The channel is chosen depending on
2569 * the sample direction. */
2570static int
2571smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2572{
2573 struct buffer *temp;
2574 struct channel *chn;
2575 struct htx *htx;
2576 struct htx_blk *blk;
2577 struct htx_sl *sl;
2578 int32_t pos;
2579
2580 if (!smp->strm || !arg_p)
2581 return 0;
2582
2583 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002584 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002585 if (!htx)
2586 return 0;
2587
2588 pos = arg_p[0].data.sint;
2589 if (pos == -1)
2590 blk = htx_get_head_blk(htx);
2591 else if (pos == -2)
2592 blk = htx_get_tail_blk(htx);
2593 else if (pos == -3)
2594 blk = htx_get_first_blk(htx);
2595 else
2596 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2597
2598 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2599 smp->data.u.str.size = 0;
2600 smp->data.u.str.area = "";
2601 smp->data.u.str.data = 0;
2602 }
2603 else {
2604 sl = htx_get_blk_ptr(htx, blk);
2605
2606 temp = get_trash_chunk();
2607 chunk_istcat(temp, htx_sl_p1(sl));
2608 temp->area[temp->data++] = ' ';
2609 chunk_istcat(temp, htx_sl_p2(sl));
2610 temp->area[temp->data++] = ' ';
2611 chunk_istcat(temp, htx_sl_p3(sl));
2612
2613 smp->data.u.str = *temp;
2614 }
2615
2616 smp->data.type = SMP_T_STR;
2617 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2618 return 1;
2619}
2620
2621/* Returns the header name if the selected HTX block exists and is a header or a
2622 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2623 * supported or "head", "tail" or "first". The channel is chosen depending on
2624 * the sample direction. */
2625static int
2626smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2627{
2628 struct channel *chn;
2629 struct htx *htx;
2630 struct htx_blk *blk;
2631 int32_t pos;
2632
2633 if (!smp->strm || !arg_p)
2634 return 0;
2635
2636 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002637 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002638 if (!htx)
2639 return 0;
2640
2641 pos = arg_p[0].data.sint;
2642 if (pos == -1)
2643 blk = htx_get_head_blk(htx);
2644 else if (pos == -2)
2645 blk = htx_get_tail_blk(htx);
2646 else if (pos == -3)
2647 blk = htx_get_first_blk(htx);
2648 else
2649 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2650
2651 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2652 smp->data.u.str.size = 0;
2653 smp->data.u.str.area = "";
2654 smp->data.u.str.data = 0;
2655 }
2656 else {
2657 struct ist name = htx_get_blk_name(htx, blk);
2658
2659 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2660 }
2661 smp->data.type = SMP_T_STR;
2662 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2663 return 1;
2664}
2665
2666/* Returns the header value if the selected HTX block exists and is a header or
2667 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2668 * supported or "head", "tail" or "first". The channel is chosen depending on
2669 * the sample direction. */
2670static int
2671smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2672{
2673 struct channel *chn;
2674 struct htx *htx;
2675 struct htx_blk *blk;
2676 int32_t pos;
2677
2678 if (!smp->strm || !arg_p)
2679 return 0;
2680
2681 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002682 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002683 if (!htx)
2684 return 0;
2685
2686 pos = arg_p[0].data.sint;
2687 if (pos == -1)
2688 blk = htx_get_head_blk(htx);
2689 else if (pos == -2)
2690 blk = htx_get_tail_blk(htx);
2691 else if (pos == -3)
2692 blk = htx_get_first_blk(htx);
2693 else
2694 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2695
2696 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2697 smp->data.u.str.size = 0;
2698 smp->data.u.str.area = "";
2699 smp->data.u.str.data = 0;
2700 }
2701 else {
2702 struct ist val = htx_get_blk_value(htx, blk);
2703
2704 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2705 }
2706 smp->data.type = SMP_T_STR;
2707 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2708 return 1;
2709}
2710
2711/* Returns the value if the selected HTX block exists and is a data
2712 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2713 * or "head", "tail" or "first". The channel is chosen depending on the sample
2714 * direction. */
2715static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002716smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002717{
2718 struct channel *chn;
2719 struct htx *htx;
2720 struct htx_blk *blk;
2721 int32_t pos;
2722
2723 if (!smp->strm || !arg_p)
2724 return 0;
2725
2726 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002727 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002728 if (!htx)
2729 return 0;
2730
2731 pos = arg_p[0].data.sint;
2732 if (pos == -1)
2733 blk = htx_get_head_blk(htx);
2734 else if (pos == -2)
2735 blk = htx_get_tail_blk(htx);
2736 else if (pos == -3)
2737 blk = htx_get_first_blk(htx);
2738 else
2739 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2740
2741 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2742 smp->data.u.str.size = 0;
2743 smp->data.u.str.area = "";
2744 smp->data.u.str.data = 0;
2745 }
2746 else {
2747 struct ist val = htx_get_blk_value(htx, blk);
2748
2749 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2750 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002751 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002752 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2753 return 1;
2754}
2755
2756/* This function is used to validate the arguments passed to any "htx_blk" fetch
2757 * keywords. An argument is expected by these keywords. It must be a positive
2758 * integer or on of the following strings: "head", "tail" or "first". It returns
2759 * 0 on error, and a non-zero value if OK.
2760 */
2761int val_blk_arg(struct arg *arg, char **err_msg)
2762{
2763 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2764 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2765 return 0;
2766 }
2767 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002768 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002769 arg[0].type = ARGT_SINT;
2770 arg[0].data.sint = -1;
2771 }
2772 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002773 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002774 arg[0].type = ARGT_SINT;
2775 arg[0].data.sint = -2;
2776 }
2777 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002778 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002779 arg[0].type = ARGT_SINT;
2780 arg[0].data.sint = -3;
2781 }
2782 else {
2783 int pos;
2784
2785 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002786 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002787 memprintf(err_msg, "invalid block position");
2788 return 0;
2789 }
2790 }
2791
2792 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2793 if (pos < 0) {
2794 memprintf(err_msg, "block position must not be negative");
2795 return 0;
2796 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002797 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002798 arg[0].type = ARGT_SINT;
2799 arg[0].data.sint = pos;
2800 }
2801
2802 return 1;
2803}
2804
2805
2806/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002807 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002808 */
2809static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002810 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002811
Christopher Faulet01f44452020-01-08 14:23:40 +01002812 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2813 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2814 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2815 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2816 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2817 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2818 { "internal.htx.has_eom", smp_fetch_htx_has_eom, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHV|SMP_USE_HRSHV},
Christopher Faulet29f72842019-12-11 15:52:32 +01002819
Christopher Faulet01f44452020-01-08 14:23:40 +01002820 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2821 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2822 { "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},
2823 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2824 { "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 Fauletc5db14c2020-01-08 14:51:03 +01002825 { "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 Faulet29f72842019-12-11 15:52:32 +01002826
2827 { /* END */ },
2828}};
2829
2830INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);