blob: 2978f2eb409e1d21772d517df33a71417e5545b6 [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 Denoyelle4c0882b2021-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>
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +020024#include <haproxy/http-hdr.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/log.h>
29#include <haproxy/regex.h>
30#include <haproxy/sample.h>
Willy Tarreau4cbf62d2021-05-08 13:01:23 +020031#include <haproxy/tools.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020032
Christopher Faulet47596d32018-10-22 09:17:28 +020033
Christopher Fauletf7346382019-07-17 22:02:08 +020034struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020035struct http_reply http_err_replies[HTTP_ERR_SIZE];
36
Christopher Faulet58857752020-01-15 15:19:50 +010037struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010038struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020039struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010040
Christopher Faulet76edc0f2020-01-13 15:52:01 +010041/* The declaration of an errorfiles/errorfile directives. Used during config
42 * parsing only. */
43struct conf_errors {
44 char type; /* directive type (0: errorfiles, 1: errorfile) */
45 union {
46 struct {
47 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020048 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010049 } errorfile; /* describe an "errorfile" directive */
50 struct {
51 char *name; /* the http-errors section name */
52 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
53 } errorfiles; /* describe an "errorfiles" directive */
54 } info;
55
56 char *file; /* file where the directive appears */
57 int line; /* line where the directive appears */
58
59 struct list list; /* next conf_errors */
60};
61
Christopher Faulet297fbb42019-05-13 14:41:27 +020062/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020063 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020064 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020065 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020066struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020067{
Christopher Faulet297fbb42019-05-13 14:41:27 +020068 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010069
Christopher Faulet29f17582019-05-23 11:03:26 +020070 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020071 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 +020072 return NULL;
73 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020074}
75
Christopher Faulet727a3f12020-02-07 16:39:41 +010076/* Returns the headers size in the HTX message */
77size_t http_get_hdrs_size(struct htx *htx)
78{
79 struct htx_blk *blk;
80 size_t sz = 0;
81
82 blk = htx_get_first_blk(htx);
83 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
84 return sz;
85
86 for (; blk; blk = htx_get_next_blk(htx, blk)) {
87 sz += htx_get_blksz(blk);
88 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
89 break;
90 }
91 return sz;
92}
93
Christopher Faulet8dd33e12020-05-05 07:42:42 +020094/* Finds the first or next occurrence of header matching <pattern> in the HTX
95 * message <htx> using the context <ctx>. This structure holds everything
96 * necessary to use the header and find next occurrence. If its <blk> member is
97 * NULL, the header is searched from the beginning. Otherwise, the next
98 * occurrence is returned. The function returns 1 when it finds a value, and 0
99 * when there is no more. It is designed to work with headers defined as
100 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
101 * full-line headers in whose comma is not a delimiter but is part of the
102 * syntax. A special case, if ctx->value is NULL when searching for a new values
103 * of a header, the current header is rescanned. This allows rescanning after a
104 * header deletion.
105 *
106 * The matching method is chosen by checking the flags :
107 *
108 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
109 * the regex are evaluated.
110 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
111 * to the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
113 * starting by the string are evaluated.
114 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
115 * ending by the string are evaluated.
116 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
117 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200118 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200119
120#define HTTP_FIND_FL_MATCH_STR 0x0001
121#define HTTP_FIND_FL_MATCH_PFX 0x0002
122#define HTTP_FIND_FL_MATCH_SFX 0x0003
123#define HTTP_FIND_FL_MATCH_SUB 0x0004
124#define HTTP_FIND_FL_MATCH_REG 0x0005
125/* 0x0006..0x000f: for other matching methods */
126#define HTTP_FIND_FL_MATCH_TYPE 0x000F
127#define HTTP_FIND_FL_FULL 0x0010
128
129static 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 +0200130{
131 struct htx_blk *blk = ctx->blk;
132 struct ist n, v;
133 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200134
135 if (blk) {
136 char *p;
137
Tim Duesterhused526372020-03-05 17:56:33 +0100138 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200139 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200140 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200141 goto next_blk;
142 v = htx_get_blk_value(htx, blk);
Tim Duesterhus77508502022-03-15 13:11:06 +0100143 p = istend(ctx->value) + ctx->lws_after;
Christopher Faulet47596d32018-10-22 09:17:28 +0200144 v.len -= (p - v.ptr);
145 v.ptr = p;
146 if (!v.len)
147 goto next_blk;
148 /* Skip comma */
149 if (*(v.ptr) == ',') {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100150 v = istnext(v);
Christopher Faulet47596d32018-10-22 09:17:28 +0200151 }
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
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100199 n = ist2(istend(n) - istlen(name),
200 istlen(name));
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200201 if (!isteqi(n, name))
202 goto next_blk;
203 break;
204 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000205 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200206 goto next_blk;
207 break;
208 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200209 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200210 break;
211 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200213 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200214 v = htx_get_blk_value(htx, blk);
215
216 return_hdr:
217 ctx->lws_before = 0;
218 ctx->lws_after = 0;
219 while (v.len && HTTP_IS_LWS(*v.ptr)) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100220 v = istnext(v);
Christopher Faulet47596d32018-10-22 09:17:28 +0200221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100224 v.len = http_find_hdr_value_end(v.ptr, istend(v)) - v.ptr;
225
226 while (v.len && HTTP_IS_LWS(*(istend(v) - 1))) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200227 v.len--;
228 ctx->lws_after++;
229 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200230 ctx->blk = blk;
231 ctx->value = v;
232 return 1;
233
234 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200235 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200236 }
237
238 ctx->blk = NULL;
239 ctx->value = ist("");
240 ctx->lws_before = ctx->lws_after = 0;
241 return 0;
242}
243
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200244
245/* Header names must match <name> */
246int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
247{
248 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
249}
250
251/* Header names must match <name>. Same than http_find_header */
252int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
253{
254 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
255}
256
257
258/* Header names must start with <prefix> */
259int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
260{
261 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
262}
263
264/* Header names must end with <suffix> */
265int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
266{
267 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
268}
269/* Header names must contain <sub> */
270int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
271{
272 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
273}
274
275/* Header names must match <re> regex*/
276int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
277{
278 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
279}
280
281
Christopher Faulet47596d32018-10-22 09:17:28 +0200282/* Adds a header block int the HTX message <htx>, just before the EOH block. It
283 * returns 1 on success, otherwise it returns 0.
284 */
285int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
286{
287 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200288 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200289 enum htx_blk_type type = htx_get_tail_type(htx);
290 int32_t prev;
291
292 blk = htx_add_header(htx, n, v);
293 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200294 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200295
296 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200297 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200298
299 /* <blk> is the head, swap it iteratively with its predecessor to place
300 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200301 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200302 struct htx_blk *pblk = htx_get_blk(htx, prev);
303 enum htx_blk_type type = htx_get_blk_type(pblk);
304
305 /* Swap .addr and .info fields */
306 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
307 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
308
309 if (blk->addr == pblk->addr)
310 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200311
312 /* Stop when end-of-header is reached */
313 if (type == HTX_BLK_EOH)
314 break;
315
316 blk = pblk;
317 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200318
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200319 end:
320 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100321 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200322 if (!http_update_authority(htx, sl, v))
323 goto fail;
324 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200325 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200326
327 fail:
328 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200329}
330
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100331/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200332 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200333 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100334int 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 +0200335{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200336 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200337
Christopher Faulet29f17582019-05-23 11:03:26 +0200338 blk = htx_get_first_blk(htx);
339 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200340 return 0;
341 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200342}
343
Christopher Faulete010c802018-10-24 10:36:45 +0200344/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
345 * on success, otherwise 0.
346 */
347int http_replace_req_meth(struct htx *htx, const struct ist meth)
348{
349 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200350 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100351 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200352
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100353 if (!sl)
354 return 0;
355
Christopher Faulete010c802018-10-24 10:36:45 +0200356 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100357 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
358 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200359
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100360 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
361 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200362
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100363 /* create the new start line */
364 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
365 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200366}
367
368/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
369 * success, otherwise 0.
370 */
371int http_replace_req_uri(struct htx *htx, const struct ist uri)
372{
373 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200374 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100375 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200376
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100377 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200378 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100379
Christopher Faulete010c802018-10-24 10:36:45 +0200380 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100381 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
382 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200383
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100384 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
385 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200386
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100387 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200388 if (!http_replace_stline(htx, meth, uri, vsn))
389 goto fail;
390
391 sl = http_get_stline(htx);
Ilya Shipitsin6f86eaa2022-11-30 16:22:42 +0500392 ALREADY_CHECKED(sl); /* the stline exists because http_replace_stline() succeeded */
Christopher Faulet92c2de12022-11-22 18:02:00 +0100393
Christopher Faulet84cdbe42022-11-22 15:41:48 +0100394 sl->flags &= ~HTX_SL_F_NORMALIZED_URI;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200395 if (!http_update_host(htx, sl, uri))
396 goto fail;
397
398 return 1;
399 fail:
400 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200401}
402
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200403/* Replace the request path in the HTX message <htx> by <path>. The host part is
404 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
405 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
406 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200407 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200408int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200409{
410 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200411 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100412 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200413 size_t plen = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200414 struct http_uri_parser parser;
Christopher Faulete010c802018-10-24 10:36:45 +0200415
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100416 if (!sl)
417 return 0;
418
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100419 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +0200420 parser = http_uri_parser_init(uri);
421 p = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +0100422 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100423 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200424 if (with_qs)
425 plen = p.len;
426 else {
427 while (plen < p.len && *(p.ptr + plen) != '?')
428 plen++;
429 }
Christopher Faulete010c802018-10-24 10:36:45 +0200430
431 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100432 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
433 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200434
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100435 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
436 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
437
438 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100439 chunk_istcat(temp, path); /* uri: new path */
Christopher Faulete010c802018-10-24 10:36:45 +0200440 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100441 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200442
443 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100444 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200445}
446
447/* Replace the request query-string in the HTX message <htx> by <query>. The
448 * host part and the path are preserved. It returns 1 on success, otherwise
449 * 0.
450 */
451int http_replace_req_query(struct htx *htx, const struct ist query)
452{
453 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200454 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100455 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200456 int offset = 1;
457
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100458 if (!sl)
459 return 0;
460
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100461 uri = htx_sl_req_uri(sl);
462 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200463 while (q.len > 0 && *(q.ptr) != '?') {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100464 q = istnext(q);
Christopher Faulete010c802018-10-24 10:36:45 +0200465 }
466
467 /* skip the question mark or indicate that we must insert it
468 * (but only if the format string is not empty then).
469 */
470 if (q.len) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100471 q = istnext(q);
Christopher Faulete010c802018-10-24 10:36:45 +0200472 }
473 else if (query.len > 1)
474 offset = 0;
475
476 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
478 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200479
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100480 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
481 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200482
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100483 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
484 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
485 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200486
487 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100488 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200489}
490
491/* Replace the response status in the HTX message <htx> by <status>. It returns
492 * 1 on success, otherwise 0.
493*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200494int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200495{
496 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200497 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200498 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200499
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100500 if (!sl)
501 return 0;
502
Christopher Faulete010c802018-10-24 10:36:45 +0200503 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100504 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
505 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200506 r = reason;
507 if (!isttest(r)) {
508 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
509 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
510 }
Christopher Faulete010c802018-10-24 10:36:45 +0200511
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100512 /* create the new start line */
513 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200514 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200515}
516
517/* Replace the response reason in the HTX message <htx> by <reason>. It returns
518 * 1 on success, otherwise 0.
519*/
520int http_replace_res_reason(struct htx *htx, const struct ist reason)
521{
522 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200523 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100524 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200525
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100526 if (!sl)
527 return 0;
528
Christopher Faulete010c802018-10-24 10:36:45 +0200529 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100530 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
531 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200532
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100533 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
534 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200535
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100536 /* create the new start line */
537 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200538}
539
Christopher Faulet47596d32018-10-22 09:17:28 +0200540/* Replaces a part of a header value referenced in the context <ctx> by
541 * <data>. It returns 1 on success, otherwise it returns 0. The context is
542 * updated if necessary.
543 */
544int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
545{
546 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200547 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200548 char *start;
549 struct ist v;
550 uint32_t len, off;
551
552 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200553 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200554
555 v = htx_get_blk_value(htx, blk);
556 start = ctx->value.ptr - ctx->lws_before;
557 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
558 off = start - v.ptr;
559
560 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
561 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200562 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200563
564 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200565
566 sl = http_get_stline(htx);
567 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
568 struct ist n = htx_get_blk_name(htx, blk);
569
570 if (isteq(n, ist("host"))) {
571 if (!http_update_authority(htx, sl, v))
572 goto fail;
573 ctx->blk = NULL;
574 http_find_header(htx, ist("host"), ctx, 1);
575 blk = ctx->blk;
576 v = htx_get_blk_value(htx, blk);
577 }
578 }
579
Christopher Faulet47596d32018-10-22 09:17:28 +0200580 ctx->blk = blk;
Tim Duesterhus77508502022-03-15 13:11:06 +0100581 ctx->value = ist2(v.ptr + off, data.len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200582 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 */
Tim Duesterhus77508502022-03-15 13:11:06 +0100680 ctx->value = ist2(start, 0);
Christopher Faulet47596d32018-10-22 09:17:28 +0200681 ctx->lws_before = ctx->lws_after = 0;
682
683 return 1;
684}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200685
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200686/* Updates the authority part of the uri with the value <host>. It happens when
687 * the header host is modified. It returns 0 on failure and 1 on success. It is
688 * the caller responsibility to provide the start-line and to be sure the uri
689 * contains an authority. Thus, if no authority is found in the uri, an error is
690 * returned.
691 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200692int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200693{
694 struct buffer *temp = get_trash_chunk();
695 struct ist meth, vsn, uri, authority;
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200696 struct http_uri_parser parser;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200697
698 uri = htx_sl_req_uri(sl);
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200699 parser = http_uri_parser_init(uri);
700 authority = http_parse_authority(&parser, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100701 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200702 return 0;
703
Christopher Faulet34b18e42020-02-18 11:02:21 +0100704 /* Don't update the uri if there is no change */
705 if (isteq(host, authority))
706 return 1;
707
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200708 /* Start by copying old method and version */
709 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
710 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
711
712 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
713 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
714
715 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100716 chunk_istcat(temp, host);
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100717 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200718 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
719
720 return http_replace_stline(htx, meth, uri, vsn);
721
722}
723
724/* Update the header host by extracting the authority of the uri <uri>. flags of
725 * the start-line are also updated accordingly. For orgin-form and asterisk-form
726 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
727 * removed from the flags of the start-line. Otherwise, this flag is set and the
728 * authority is used to set the value of the header host. This function returns
729 * 0 on failure and 1 on success.
730*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200731int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200732{
733 struct ist authority;
734 struct http_hdr_ctx ctx;
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200735 struct http_uri_parser parser = http_uri_parser_init(uri);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200736
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200737 if (parser.format == URI_PARSER_FORMAT_EMPTY ||
738 parser.format == URI_PARSER_FORMAT_ASTERISK ||
739 parser.format == URI_PARSER_FORMAT_ABSPATH) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200740 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
741 }
742 else {
743 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
744 if (sl->info.req.meth != HTTP_METH_CONNECT) {
745 // absolute-form (RFC7320 #5.3.2)
746 sl->flags |= HTX_SL_F_HAS_SCHM;
747 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
748 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
749
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200750 authority = http_parse_authority(&parser, 1);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200751 if (!authority.len)
752 goto fail;
753 }
754 else {
755 // authority-form (RFC7320 #5.3.3)
756 authority = uri;
757 }
758
759 /* Replace header host value */
760 ctx.blk = NULL;
761 while (http_find_header(htx, ist("host"), &ctx, 1)) {
762 if (!http_replace_header_value(htx, &ctx, authority))
763 goto fail;
764 }
765
766 }
767 return 1;
768 fail:
769 return 0;
770}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200771
772/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
773 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
774 * performed over the whole headers. Otherwise it must contain a valid header
775 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
776 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
777 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
778 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
779 * -1. The value fetch stops at commas, so this function is suited for use with
780 * list headers.
781 * The return value is 0 if nothing was found, or non-zero otherwise.
782 */
783unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
784 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
785{
786 struct http_hdr_ctx local_ctx;
787 struct ist val_hist[MAX_HDR_HISTORY];
788 unsigned int hist_idx;
789 int found;
790
791 if (!ctx) {
792 local_ctx.blk = NULL;
793 ctx = &local_ctx;
794 }
795
796 if (occ >= 0) {
797 /* search from the beginning */
798 while (http_find_header(htx, hdr, ctx, 0)) {
799 occ--;
800 if (occ <= 0) {
801 *vptr = ctx->value.ptr;
802 *vlen = ctx->value.len;
803 return 1;
804 }
805 }
806 return 0;
807 }
808
809 /* negative occurrence, we scan all the list then walk back */
810 if (-occ > MAX_HDR_HISTORY)
811 return 0;
812
813 found = hist_idx = 0;
814 while (http_find_header(htx, hdr, ctx, 0)) {
815 val_hist[hist_idx] = ctx->value;
816 if (++hist_idx >= MAX_HDR_HISTORY)
817 hist_idx = 0;
818 found++;
819 }
820 if (-occ > found)
821 return 0;
822
823 /* OK now we have the last occurrence in [hist_idx-1], and we need to
824 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
825 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
826 * to remain in the 0..9 range.
827 */
828 hist_idx += occ + MAX_HDR_HISTORY;
829 if (hist_idx >= MAX_HDR_HISTORY)
830 hist_idx -= MAX_HDR_HISTORY;
831 *vptr = val_hist[hist_idx].ptr;
832 *vlen = val_hist[hist_idx].len;
833 return 1;
834}
835
836/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
837 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
838 * performed over the whole headers. Otherwise it must contain a valid header
839 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
840 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
841 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
842 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
843 * -1. This function differs from http_get_hdr() in that it only returns full
844 * line header values and does not stop at commas.
845 * The return value is 0 if nothing was found, or non-zero otherwise.
846 */
847unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
848 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
849{
850 struct http_hdr_ctx local_ctx;
851 struct ist val_hist[MAX_HDR_HISTORY];
852 unsigned int hist_idx;
853 int found;
854
855 if (!ctx) {
856 local_ctx.blk = NULL;
857 ctx = &local_ctx;
858 }
859
860 if (occ >= 0) {
861 /* search from the beginning */
862 while (http_find_header(htx, hdr, ctx, 1)) {
863 occ--;
864 if (occ <= 0) {
865 *vptr = ctx->value.ptr;
866 *vlen = ctx->value.len;
867 return 1;
868 }
869 }
870 return 0;
871 }
872
873 /* negative occurrence, we scan all the list then walk back */
874 if (-occ > MAX_HDR_HISTORY)
875 return 0;
876
877 found = hist_idx = 0;
878 while (http_find_header(htx, hdr, ctx, 1)) {
879 val_hist[hist_idx] = ctx->value;
880 if (++hist_idx >= MAX_HDR_HISTORY)
881 hist_idx = 0;
882 found++;
883 }
884 if (-occ > found)
885 return 0;
886
887 /* OK now we have the last occurrence in [hist_idx-1], and we need to
888 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
889 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
890 * to remain in the 0..9 range.
891 */
892 hist_idx += occ + MAX_HDR_HISTORY;
893 if (hist_idx >= MAX_HDR_HISTORY)
894 hist_idx -= MAX_HDR_HISTORY;
895 *vptr = val_hist[hist_idx].ptr;
896 *vlen = val_hist[hist_idx].len;
897 return 1;
898}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100899
Christopher Fauleta66adf42020-11-05 22:43:41 +0100900int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100901{
902 struct htx *htx;
903 struct htx_sl *sl;
904 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200905 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100906 union h1_sl h1sl;
907 unsigned int flags = HTX_SL_F_IS_RESP;
908 int ret = 0;
909
Christopher Faulet90cc4812019-07-22 16:49:30 +0200910 b_reset(buf);
911 if (!raw.len) {
912 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100913 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200914 return 1;
915 }
916
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100917 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200918 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100919 if (!buf->area)
920 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100921
922 h1m_init_res(&h1m);
923 h1m.flags |= H1_MF_NO_PHDR;
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100924 ret = h1_headers_to_hdr_list(raw.ptr, istend(raw),
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100925 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100926 if (ret <= 0) {
927 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100928 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100929 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100930
Christopher Fauleta66adf42020-11-05 22:43:41 +0100931 if (unlikely(h1sl.st.v.len != 8)) {
932 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100933 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100934 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100935 if ((*(h1sl.st.v.ptr + 5) > '1') ||
936 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
937 h1m.flags |= H1_MF_VER_11;
938
Christopher Fauleta66adf42020-11-05 22:43:41 +0100939 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
940 memprintf(errmsg, "invalid http status code for an error message (%u)",
941 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200942 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100943 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200944
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200945 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
946 /* Responses known to have no body. */
947 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
948 h1m.flags |= H1_MF_XFER_LEN;
949 h1m.curr_len = h1m.body_len = 0;
950 }
951 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
952 h1m.flags |= H1_MF_XFER_LEN;
953
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100954 if (h1m.flags & H1_MF_VER_11)
955 flags |= HTX_SL_F_VER_11;
956 if (h1m.flags & H1_MF_XFER_ENC)
957 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200958 if (h1m.flags & H1_MF_XFER_LEN) {
959 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100960 if (h1m.flags & H1_MF_CHNK) {
961 memprintf(errmsg, "chunk-encoded payload not supported");
962 goto error;
963 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200964 else if (h1m.flags & H1_MF_CLEN) {
965 flags |= HTX_SL_F_CLEN;
966 if (h1m.body_len == 0)
967 flags |= HTX_SL_F_BODYLESS;
968 }
969 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200970 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100971 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200972
Christopher Fauleta66adf42020-11-05 22:43:41 +0100973 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
974 memprintf(errmsg, "message payload not expected");
975 goto error;
976 }
977 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
978 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100979 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100980 goto error;
981 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100982
983 htx = htx_from_buf(buf);
984 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 +0100985 if (!sl || !htx_add_all_headers(htx, hdrs)) {
986 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100987 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100988 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100989 sl->info.res.status = h1sl.st.status;
990
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200991 while (raw.len > ret) {
992 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100993 if (!sent) {
994 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100995 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100996 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200997 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100998 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200999
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001000 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +02001001
Christopher Faulet90cc4812019-07-22 16:49:30 +02001002 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001003
1004error:
1005 if (buf->size)
1006 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001007 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001008}
1009
Christopher Faulet18630642020-05-12 18:57:28 +02001010void release_http_reply(struct http_reply *http_reply)
1011{
1012 struct logformat_node *lf, *lfb;
1013 struct http_reply_hdr *hdr, *hdrb;
1014
1015 if (!http_reply)
1016 return;
1017
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001018 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001019 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001020 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001021 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001022 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001023 release_sample_expr(lf->expr);
1024 free(lf->arg);
1025 free(lf);
1026 }
1027 istfree(&hdr->name);
1028 free(hdr);
1029 }
1030
1031 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001032 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001033 }
1034 else if (http_reply->type == HTTP_REPLY_RAW)
1035 chunk_destroy(&http_reply->body.obj);
1036 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1037 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001038 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001039 release_sample_expr(lf->expr);
1040 free(lf->arg);
1041 free(lf);
1042 }
1043 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001044 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001045}
1046
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001047static int http_htx_init(void)
1048{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001049 struct buffer chk;
1050 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001051 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001052 int rc;
1053 int err_code = 0;
1054
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001055 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1056 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001057 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001058 err_code |= ERR_ALERT | ERR_FATAL;
1059 continue;
1060 }
1061
Tim Duesterhus77508502022-03-15 13:11:06 +01001062 raw = ist(http_err_msgs[rc]);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001063 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1064 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1065 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001066 err_code |= ERR_ALERT | ERR_FATAL;
1067 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001068 else if (errmsg) {
1069 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1070 err_code |= ERR_WARN;
1071 }
1072
1073 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001074 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001075
Christopher Fauletf7346382019-07-17 22:02:08 +02001076 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001077 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1078 http_err_replies[rc].status = http_err_codes[rc];
1079 http_err_replies[rc].ctype = NULL;
1080 LIST_INIT(&http_err_replies[rc].hdrs);
1081 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001082 }
1083end:
1084 return err_code;
1085}
1086
Christopher Faulet58857752020-01-15 15:19:50 +01001087static void http_htx_deinit(void)
1088{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001089 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001090 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001091 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001092 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001093 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001094
1095 node = ebpt_first(&http_error_messages);
1096 while (node) {
1097 next = ebpt_next(node);
1098 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001099 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1100 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001101 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001102 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001103 node = next;
1104 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001105
1106 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1107 free(http_errs->conf.file);
1108 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001109 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1110 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001111 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001112 free(http_errs);
1113 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001114
1115 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001116 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001117 release_http_reply(http_rep);
1118 }
Tim Duesterhus2b7fa9d2022-04-26 23:35:07 +02001119
1120 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1121 chunk_destroy(&http_err_chunks[rc]);
Christopher Faulet58857752020-01-15 15:19:50 +01001122}
1123
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001124REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001125REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001126
Christopher Faulet58857752020-01-15 15:19:50 +01001127/* Reads content of the error file <file> and convert it into an HTX message. On
1128 * success, the HTX message is returned. On error, NULL is returned and an error
1129 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001130 */
Christopher Faulet58857752020-01-15 15:19:50 +01001131struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001132{
Christopher Faulet58857752020-01-15 15:19:50 +01001133 struct buffer *buf = NULL;
1134 struct buffer chk;
1135 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001136 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001137 struct stat stat;
1138 char *err = NULL;
1139 int errnum, errlen;
1140 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001141
1142 /* already loaded */
1143 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1144 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001145 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1146 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001147 goto out;
1148 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001149
Christopher Faulet58857752020-01-15 15:19:50 +01001150 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001151 fd = open(file, O_RDONLY);
1152 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1153 memprintf(errmsg, "error opening file '%s'.", file);
1154 goto out;
1155 }
1156
1157 if (stat.st_size <= global.tune.bufsize)
1158 errlen = stat.st_size;
1159 else {
1160 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1161 file, global.tune.bufsize);
1162 errlen = global.tune.bufsize;
1163 }
1164
1165 err = malloc(errlen);
1166 if (!err) {
1167 memprintf(errmsg, "out of memory.");
1168 goto out;
1169 }
1170
1171 errnum = read(fd, err, errlen);
1172 if (errnum != errlen) {
1173 memprintf(errmsg, "error reading file '%s'.", file);
1174 goto out;
1175 }
1176
Christopher Faulet58857752020-01-15 15:19:50 +01001177 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001178 http_errmsg = calloc(1, sizeof(*http_errmsg));
1179 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001180 memprintf(errmsg, "out of memory.");
1181 goto out;
1182 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001183 http_errmsg->node.key = strdup(file);
1184 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001185 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001186 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001187 goto out;
1188 }
1189
1190 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001191 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1192 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001193 free(http_errmsg->node.key);
1194 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001195 goto out;
1196 }
1197
Christopher Faulet58857752020-01-15 15:19:50 +01001198 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001199 http_errmsg->msg = chk;
1200 ebis_insert(&http_error_messages, &http_errmsg->node);
1201 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001202
Christopher Faulet5031ef52020-01-15 11:22:07 +01001203 out:
1204 if (fd >= 0)
1205 close(fd);
1206 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001207 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001208}
1209
Ilya Shipitsind4259502020-04-08 01:07:56 +05001210/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001211 * message is returned. On error, NULL is returned and an error message is
1212 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001213 */
Christopher Faulet58857752020-01-15 15:19:50 +01001214struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001215{
Christopher Faulet58857752020-01-15 15:19:50 +01001216 struct buffer *buf = NULL;
1217 struct buffer chk;
1218 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001219 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001220
1221 /* already loaded */
1222 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1223 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001224 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1225 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001226 goto out;
1227 }
1228 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001229 http_errmsg = calloc(1, sizeof(*http_errmsg));
1230 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001231 memprintf(errmsg, "out of memory.");
1232 goto out;
1233 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001234 http_errmsg->node.key = strdup(key);
1235 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001236 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001237 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001238 goto out;
1239 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001240
1241 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001242 if (!http_str_to_htx(&chk, msg, errmsg)) {
1243 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001244 free(http_errmsg->node.key);
1245 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001246 goto out;
1247 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001248
Christopher Faulet58857752020-01-15 15:19:50 +01001249 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001250 http_errmsg->msg = chk;
1251 ebis_insert(&http_error_messages, &http_errmsg->node);
1252 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001253 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001254 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001255}
1256
Christopher Faulet5031ef52020-01-15 11:22:07 +01001257/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001258 * <status>. It returns NULL if there is any error, otherwise it return the
1259 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001260 */
Christopher Faulet58857752020-01-15 15:19:50 +01001261struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001262{
Christopher Faulet58857752020-01-15 15:19:50 +01001263 struct buffer *buf = NULL;
1264 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001265
1266 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1267 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001268 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001269 break;
1270 }
1271 }
1272
1273 if (rc >= HTTP_ERR_SIZE)
1274 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001275 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001276}
1277
1278/* This function creates HTX error message corresponding to a redirect message
1279 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001280 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1281 * returns NULL if there is any error, otherwise it return the corresponding HTX
1282 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001283 */
Christopher Faulet58857752020-01-15 15:19:50 +01001284struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001285{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001286 static const char *HTTP_302 =
1287 "HTTP/1.1 302 Found\r\n"
1288 "Cache-Control: no-cache\r\n"
1289 "Content-length: 0\r\n"
1290 "Location: "; /* not terminated since it will be concatenated with the URL */
1291 static const char *HTTP_303 =
1292 "HTTP/1.1 303 See Other\r\n"
1293 "Cache-Control: no-cache\r\n"
1294 "Content-length: 0\r\n"
1295 "Location: "; /* not terminated since it will be concatenated with the URL */
1296
Christopher Faulet58857752020-01-15 15:19:50 +01001297 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001298 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001299 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001300 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001301
1302 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1303 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001304 /* Create the error key */
1305 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1306 memprintf(errmsg, "out of memory.");
1307 goto out;
1308 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001309 /* Create the error message */
1310 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1311 errlen = strlen(msg) + strlen(url) + 5;
1312 err = malloc(errlen);
1313 if (!err) {
1314 memprintf(errmsg, "out of memory.");
1315 goto out;
1316 }
1317 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1318
1319 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001320 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001321 break;
1322 }
1323 }
1324
1325 if (rc >= HTTP_ERR_SIZE)
1326 memprintf(errmsg, "status code '%d' not handled.", status);
1327out:
Christopher Faulet58857752020-01-15 15:19:50 +01001328 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001329 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001330 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001331}
1332
Christopher Faulet7eea2412020-05-13 15:02:59 +02001333/* Check an "http reply" and, for replies referencing an http-errors section,
1334 * try to find the right section and the right error message in this section. If
1335 * found, the reply is updated. If the http-errors section exists but the error
1336 * message is not found, no error message is set to fallback on the default
1337 * ones. Otherwise (unknown section) an error is returned.
1338 *
1339 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1340 * filled.
1341 */
1342int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1343{
1344 struct http_errors *http_errs;
1345 int ret = 1;
1346
1347 if (reply->type != HTTP_REPLY_ERRFILES)
1348 goto end;
1349
1350 list_for_each_entry(http_errs, &http_errors_list, list) {
1351 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001352 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001353 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001354 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1355 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001356 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1357 "not declared in http-errors section '%s'.\n",
1358 px->id, reply->status, http_errs->id);
1359 break;
1360 }
1361 }
1362
1363 if (&http_errs->list == &http_errors_list) {
1364 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1365 reply->body.http_errors);
1366 ret = 0;
1367 }
1368
1369 end:
1370 return ret;
1371}
1372
Christopher Faulet47e791e2020-05-13 14:36:55 +02001373/* Parse an "http reply". It returns the reply on success or NULL on error. This
1374 * function creates one of the following http replies :
1375 *
1376 * - HTTP_REPLY_EMPTY : dummy response, no payload
1377 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1378 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1379 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1380 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1381 *
1382 * The content-type must be defined for non-empty payload. It is ignored for
1383 * error messages (implicit or explicit). When an http-errors section is
1384 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1385 * during the configuration validity check or dynamically. It is the caller
1386 * responsibility to choose. If no status code is configured, <default_status>
1387 * is set.
1388 */
1389struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1390 int default_status, char **errmsg)
1391{
1392 struct logformat_node *lf, *lfb;
1393 struct http_reply *reply = NULL;
1394 struct http_reply_hdr *hdr, *hdrb;
1395 struct stat stat;
1396 const char *act_arg = NULL;
1397 char *obj = NULL;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001398 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001399
1400
1401 reply = calloc(1, sizeof(*reply));
1402 if (!reply) {
1403 memprintf(errmsg, "out of memory");
1404 goto error;
1405 }
1406 LIST_INIT(&reply->hdrs);
1407 reply->type = HTTP_REPLY_EMPTY;
1408 reply->status = default_status;
1409
Christopher Faulet3b967c12020-05-15 15:47:44 +02001410 if (px->conf.args.ctx == ARGC_HERR)
1411 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001412 else {
1413 if (px->cap & PR_CAP_FE)
1414 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1415 if (px->cap & PR_CAP_BE)
Willy Tarreaub39e47a2021-10-16 14:41:09 +02001416 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001417 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001418
1419 cur_arg = *orig_arg;
1420 while (*args[cur_arg]) {
1421 if (strcmp(args[cur_arg], "status") == 0) {
1422 cur_arg++;
1423 if (!*args[cur_arg]) {
1424 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1425 goto error;
1426 }
1427 reply->status = atol(args[cur_arg]);
1428 if (reply->status < 200 || reply->status > 599) {
1429 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1430 goto error;
1431 }
1432 cur_arg++;
1433 }
1434 else if (strcmp(args[cur_arg], "content-type") == 0) {
1435 cur_arg++;
1436 if (!*args[cur_arg]) {
1437 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1438 goto error;
1439 }
1440 free(reply->ctype);
1441 reply->ctype = strdup(args[cur_arg]);
1442 cur_arg++;
1443 }
1444 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1445 if (reply->type != HTTP_REPLY_EMPTY) {
1446 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1447 goto error;
1448 }
1449 act_arg = args[cur_arg];
1450 cur_arg++;
1451 if (!*args[cur_arg]) {
1452 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1453 goto error;
1454 }
1455 reply->body.http_errors = strdup(args[cur_arg]);
1456 if (!reply->body.http_errors) {
1457 memprintf(errmsg, "out of memory");
1458 goto error;
1459 }
1460 reply->type = HTTP_REPLY_ERRFILES;
1461 cur_arg++;
1462 }
1463 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1464 if (reply->type != HTTP_REPLY_EMPTY) {
1465 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1466 goto error;
1467 }
1468 act_arg = args[cur_arg];
1469 reply->type = HTTP_REPLY_ERRMSG;
1470 cur_arg++;
1471 }
1472 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1473 if (reply->type != HTTP_REPLY_EMPTY) {
1474 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1475 goto error;
1476 }
1477 act_arg = args[cur_arg];
1478 cur_arg++;
1479 if (!*args[cur_arg]) {
1480 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1481 goto error;
1482 }
1483 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1484 if (!reply->body.errmsg) {
1485 goto error;
1486 }
1487 reply->type = HTTP_REPLY_ERRMSG;
1488 cur_arg++;
1489 }
1490 else if (strcmp(args[cur_arg], "file") == 0) {
1491 if (reply->type != HTTP_REPLY_EMPTY) {
1492 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1493 goto error;
1494 }
1495 act_arg = args[cur_arg];
1496 cur_arg++;
1497 if (!*args[cur_arg]) {
1498 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1499 goto error;
1500 }
1501 fd = open(args[cur_arg], O_RDONLY);
1502 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1503 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1504 goto error;
1505 }
1506 if (stat.st_size > global.tune.bufsize) {
1507 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1508 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1509 goto error;
1510 }
1511 objlen = stat.st_size;
1512 obj = malloc(objlen);
1513 if (!obj || read(fd, obj, objlen) != objlen) {
1514 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1515 goto error;
1516 }
1517 close(fd);
1518 fd = -1;
1519 reply->type = HTTP_REPLY_RAW;
1520 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1521 obj = NULL;
1522 cur_arg++;
1523 }
1524 else if (strcmp(args[cur_arg], "string") == 0) {
1525 if (reply->type != HTTP_REPLY_EMPTY) {
1526 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1527 goto error;
1528 }
1529 act_arg = args[cur_arg];
1530 cur_arg++;
1531 if (!*args[cur_arg]) {
1532 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1533 goto error;
1534 }
1535 obj = strdup(args[cur_arg]);
1536 objlen = strlen(args[cur_arg]);
1537 if (!obj) {
1538 memprintf(errmsg, "out of memory");
1539 goto error;
1540 }
1541 reply->type = HTTP_REPLY_RAW;
1542 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1543 obj = NULL;
1544 cur_arg++;
1545 }
1546 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1547 if (reply->type != HTTP_REPLY_EMPTY) {
1548 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1549 goto error;
1550 }
1551 act_arg = args[cur_arg];
1552 cur_arg++;
1553 if (!*args[cur_arg]) {
1554 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1555 goto error;
1556 }
1557 fd = open(args[cur_arg], O_RDONLY);
1558 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1559 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1560 goto error;
1561 }
1562 if (stat.st_size > global.tune.bufsize) {
1563 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1564 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1565 goto error;
1566 }
1567 objlen = stat.st_size;
1568 obj = malloc(objlen + 1);
1569 if (!obj || read(fd, obj, objlen) != objlen) {
1570 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1571 goto error;
1572 }
1573 close(fd);
1574 fd = -1;
1575 obj[objlen] = '\0';
1576 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet5a3d9a72022-11-14 08:49:28 +01001577 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001578 cur_arg++;
1579 }
1580 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1581 if (reply->type != HTTP_REPLY_EMPTY) {
1582 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1583 goto error;
1584 }
1585 act_arg = args[cur_arg];
1586 cur_arg++;
1587 if (!*args[cur_arg]) {
1588 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1589 goto error;
1590 }
1591 obj = strdup(args[cur_arg]);
1592 objlen = strlen(args[cur_arg]);
1593 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet5a3d9a72022-11-14 08:49:28 +01001594 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001595 cur_arg++;
1596 }
1597 else if (strcmp(args[cur_arg], "hdr") == 0) {
1598 cur_arg++;
1599 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1600 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1601 goto error;
1602 }
1603 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1604 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1605 strcasecmp(args[cur_arg], "content-type") == 0) {
1606 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1607 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1608 cur_arg += 2;
1609 continue;
1610 }
1611 hdr = calloc(1, sizeof(*hdr));
1612 if (!hdr) {
1613 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1614 goto error;
1615 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001616 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001617 LIST_INIT(&hdr->value);
1618 hdr->name = ist(strdup(args[cur_arg]));
1619 if (!isttest(hdr->name)) {
1620 memprintf(errmsg, "out of memory");
1621 goto error;
1622 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001623 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1624 goto error;
1625
1626 free(px->conf.lfs_file);
1627 px->conf.lfs_file = strdup(px->conf.args.file);
1628 px->conf.lfs_line = px->conf.args.line;
1629 cur_arg += 2;
1630 }
1631 else
1632 break;
1633 }
1634
1635 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1636 if (reply->ctype) {
1637 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1638 " neither errorfile nor payload defined.\n",
1639 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001640 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001641 }
1642 }
1643 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1644
1645 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1646 /* default errorfile or errorfiles: check the status */
1647 int rc;
1648
1649 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1650 if (http_err_codes[rc] == reply->status)
1651 break;
1652 }
1653
1654 if (rc >= HTTP_ERR_SIZE) {
1655 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1656 reply->status, act_arg);
1657 goto error;
1658 }
1659 }
1660
1661 if (reply->ctype) {
1662 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1663 "with an erorrfile.\n",
1664 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001665 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001666 }
1667 if (!LIST_ISEMPTY(&reply->hdrs)) {
1668 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1669 "with an erorrfile.\n",
1670 px->conf.args.file, px->conf.args.line);
1671 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001672 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001673 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001674 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001675 release_sample_expr(lf->expr);
1676 free(lf->arg);
1677 free(lf);
1678 }
1679 istfree(&hdr->name);
1680 free(hdr);
1681 }
1682 }
1683 }
1684 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001685 if ((reply->status == 204 || reply->status == 304) && objlen) {
1686 memprintf(errmsg, "No body expected for %d responses", reply->status);
1687 goto error;
1688 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001689 if (!reply->ctype && objlen) {
1690 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1691 goto error;
1692 }
1693 if (reply->ctype && !b_data(&reply->body.obj)) {
1694 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001695 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001696 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001697 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001698 }
1699 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1700 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1701 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1702 px->conf.args.file, px->conf.args.line);
1703 }
1704 }
1705 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1706 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001707 if ((reply->status == 204 || reply->status == 304)) {
1708 memprintf(errmsg, "No body expected for %d responses", reply->status);
1709 goto error;
1710 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001711 if (!reply->ctype) {
1712 memprintf(errmsg, "a content type must be defined with a log-format payload");
1713 goto error;
1714 }
1715 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1716 goto error;
1717
1718 free(px->conf.lfs_file);
1719 px->conf.lfs_file = strdup(px->conf.args.file);
1720 px->conf.lfs_line = px->conf.args.line;
1721 }
1722
1723 free(obj);
1724 *orig_arg = cur_arg;
1725 return reply;
1726
1727 error:
1728 free(obj);
1729 if (fd >= 0)
1730 close(fd);
1731 release_http_reply(reply);
1732 return NULL;
1733}
1734
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001735/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1736 * Returns 0 if no error has been found else non-zero.
1737 *
1738 * The normalization is processed on the target-uri at the condition that it is
1739 * in absolute-form. In the case where the target-uri was normalized, every
1740 * host headers values found are also replaced by the normalized hostname. This
1741 * assumes that the target-uri and host headers were properly identify as
1742 * similar before calling this function.
1743 */
1744int http_scheme_based_normalize(struct htx *htx)
1745{
1746 struct http_hdr_ctx ctx;
1747 struct htx_sl *sl;
1748 struct ist uri, scheme, authority, host, port;
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +02001749 struct http_uri_parser parser;
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001750
1751 sl = http_get_stline(htx);
1752
1753 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1754 return 0;
1755
1756 uri = htx_sl_req_uri(sl);
1757
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +02001758 parser = http_uri_parser_init(uri);
1759 scheme = http_parse_scheme(&parser);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001760 /* if no scheme found, no normalization to proceed */
1761 if (!isttest(scheme))
1762 return 0;
1763
Christopher Fauletd1d983f2022-07-05 10:24:52 +02001764 /* Extract the port if present in authority */
1765 authority = http_parse_authority(&parser, 1);
1766 port = http_get_host_port(authority);
1767 if (!isttest(port)) {
1768 /* if no port found, no normalization to proceed */
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001769 return 0;
Christopher Fauletd1d983f2022-07-05 10:24:52 +02001770 }
1771 host = isttrim(authority, istlen(authority) - istlen(port) - 1);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001772
Christopher Faulete5dfe112022-11-21 19:20:20 +01001773 if (http_is_default_port(scheme, port)) {
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001774 /* reconstruct the uri with removal of the port */
1775 struct buffer *temp = get_trash_chunk();
Christopher Faulet0eab0502022-07-06 17:41:31 +02001776 struct ist meth, vsn;
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001777
1778 /* meth */
1779 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1780 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1781
1782 /* vsn */
1783 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1784 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1785
1786 /* reconstruct uri without port */
Christopher Faulet0eab0502022-07-06 17:41:31 +02001787 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001788 chunk_istcat(temp, host);
Christopher Faulet0eab0502022-07-06 17:41:31 +02001789 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
1790 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001791
1792 http_replace_stline(htx, meth, uri, vsn);
1793
1794 /* replace every host headers values by the normalized host */
1795 ctx.blk = NULL;
1796 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1797 if (!http_replace_header_value(htx, &ctx, host))
1798 goto fail;
1799 }
1800 }
1801
1802 return 0;
1803
1804 fail:
1805 return 1;
1806}
1807
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +02001808/* First step function to merge multiple cookie headers in a single entry.
1809 *
1810 * Use it for each cookie header at <idx> index over HTTP headers in <list>.
1811 * <first> and <last> are state variables used internally and must be
1812 * initialized to -1 before the first invocation.
1813 */
1814void http_cookie_register(struct http_hdr *list, int idx, int *first, int *last)
1815{
1816 /* Build a linked list of cookie headers. Use header length to point to
1817 * the next one. The last entry will contains -1.
1818 */
1819
Amaury Denoyelle4328b612022-12-15 09:27:34 +01001820 /* Caller is responsible to initialize *first and *last to -1 on first
1821 * invocation. Both will thus be set to a valid index after it.
1822 */
1823 BUG_ON(*first > 0 && *last < 0);
1824
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +02001825 /* Mark the current end of cookie linked list. */
1826 list[idx].n.len = -1;
1827 if (*first < 0) {
1828 /* Save first found cookie for http_cookie_merge call. */
1829 *first = idx;
1830 }
1831 else {
1832 /* Update linked list of cookies. */
1833 list[*last].n.len = idx;
1834 }
1835
1836 *last = idx;
1837}
1838
1839/* Second step to merge multiple cookie headers in a single entry.
1840 *
1841 * Use it when looping over HTTP headers is done and <htx> message is built.
1842 * This will concatenate each cookie headers present from <list> directly into
1843 * <htx> message. <first> is reused from previous http_cookie_register
1844 * invocation.
1845 *
1846 * Returns 0 on success else non-zero.
1847 */
1848int http_cookie_merge(struct htx *htx, struct http_hdr *list, int first)
1849{
1850 uint32_t fs; /* free space */
1851 uint32_t bs; /* block size */
1852 uint32_t vl; /* value len */
1853 uint32_t tl; /* total length */
1854 struct htx_blk *blk;
1855
1856 if (first < 0)
1857 return 0;
1858
1859 blk = htx_add_header(htx, ist("cookie"), list[first].v);
1860 if (!blk)
1861 return 1;
1862
1863 tl = list[first].v.len;
1864 fs = htx_free_data_space(htx);
1865 bs = htx_get_blksz(blk);
1866
1867 /* for each extra cookie, we'll extend the cookie's value and insert
1868 * ";" before the new value.
1869 */
1870 fs += tl; /* first one is already counted */
1871
1872 /* Loop over cookies linked list built from http_cookie_register. */
1873 while ((first = list[first].n.len) >= 0) {
1874 vl = list[first].v.len;
1875 tl += vl + 2;
1876 if (tl > fs)
1877 return 1;
1878
1879 htx_change_blk_value_len(htx, blk, tl);
1880 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
1881 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
1882 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2,
1883 list[first].v.ptr, vl);
1884 bs += vl + 2;
1885 }
1886
1887 return 0;
1888}
1889
Christopher Faulet07f41f72020-01-16 16:16:06 +01001890/* Parses the "errorloc[302|303]" proxy keyword */
1891static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001892 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001893 char **errmsg)
1894{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001895 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001896 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001897 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001898 int errloc, status;
1899 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001900
1901 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1902 ret = 1;
1903 goto out;
1904 }
1905
1906 if (*(args[1]) == 0 || *(args[2]) == 0) {
1907 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1908 ret = -1;
1909 goto out;
1910 }
1911
1912 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001913 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001914 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1915 if (!msg) {
1916 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1917 ret = -1;
1918 goto out;
1919 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001920
1921 reply = calloc(1, sizeof(*reply));
1922 if (!reply) {
1923 memprintf(errmsg, "%s : out of memory.", args[0]);
1924 ret = -1;
1925 goto out;
1926 }
1927 reply->type = HTTP_REPLY_ERRMSG;
1928 reply->status = status;
1929 reply->ctype = NULL;
1930 LIST_INIT(&reply->hdrs);
1931 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001932 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001933
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001934 conf_err = calloc(1, sizeof(*conf_err));
1935 if (!conf_err) {
1936 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001937 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001938 ret = -1;
1939 goto out;
1940 }
1941 conf_err->type = 1;
1942 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001943 conf_err->info.errorfile.reply = reply;
1944
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001945 conf_err->file = strdup(file);
1946 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001947 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001948
Christopher Fauleta66adf42020-11-05 22:43:41 +01001949 /* handle warning message */
1950 if (*errmsg)
1951 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001952 out:
1953 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001954
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001955}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001956
1957/* Parses the "errorfile" proxy keyword */
1958static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001959 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001960 char **errmsg)
1961{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001962 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001963 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001964 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001965 int status;
1966 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001967
1968 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1969 ret = 1;
1970 goto out;
1971 }
1972
1973 if (*(args[1]) == 0 || *(args[2]) == 0) {
1974 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1975 ret = -1;
1976 goto out;
1977 }
1978
1979 status = atol(args[1]);
1980 msg = http_parse_errorfile(status, args[2], errmsg);
1981 if (!msg) {
1982 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1983 ret = -1;
1984 goto out;
1985 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001986
Christopher Faulet5809e102020-05-14 17:31:52 +02001987 reply = calloc(1, sizeof(*reply));
1988 if (!reply) {
1989 memprintf(errmsg, "%s : out of memory.", args[0]);
1990 ret = -1;
1991 goto out;
1992 }
1993 reply->type = HTTP_REPLY_ERRMSG;
1994 reply->status = status;
1995 reply->ctype = NULL;
1996 LIST_INIT(&reply->hdrs);
1997 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001998 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001999
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002000 conf_err = calloc(1, sizeof(*conf_err));
2001 if (!conf_err) {
2002 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02002003 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002004 ret = -1;
2005 goto out;
2006 }
2007 conf_err->type = 1;
2008 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02002009 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002010 conf_err->file = strdup(file);
2011 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002012 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002013
Christopher Fauleta66adf42020-11-05 22:43:41 +01002014 /* handle warning message */
2015 if (*errmsg)
2016 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002017 out:
2018 return ret;
2019
2020}
2021
2022/* Parses the "errorfiles" proxy keyword */
2023static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002024 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002025 char **err)
2026{
2027 struct conf_errors *conf_err = NULL;
2028 char *name = NULL;
2029 int rc, ret = 0;
2030
2031 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2032 ret = 1;
2033 goto out;
2034 }
2035
2036 if (!*(args[1])) {
2037 memprintf(err, "%s : expects <name> as argument.", args[0]);
2038 ret = -1;
2039 goto out;
2040 }
2041
2042 name = strdup(args[1]);
2043 conf_err = calloc(1, sizeof(*conf_err));
2044 if (!name || !conf_err) {
2045 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002046 goto error;
2047 }
2048 conf_err->type = 0;
2049
2050 conf_err->info.errorfiles.name = name;
2051 if (!*(args[2])) {
2052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
2053 conf_err->info.errorfiles.status[rc] = 1;
2054 }
2055 else {
2056 int cur_arg, status;
2057 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
2058 status = atol(args[cur_arg]);
2059
2060 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2061 if (http_err_codes[rc] == status) {
2062 conf_err->info.errorfiles.status[rc] = 2;
2063 break;
2064 }
2065 }
2066 if (rc >= HTTP_ERR_SIZE) {
2067 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01002068 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002069 }
2070 }
2071 }
2072 conf_err->file = strdup(file);
2073 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002074 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002075 out:
2076 return ret;
2077
2078 error:
2079 free(name);
2080 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01002081 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002082 goto out;
2083}
2084
Christopher Faulet3b967c12020-05-15 15:47:44 +02002085/* Parses the "http-error" proxy keyword */
2086static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002087 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02002088 char **errmsg)
2089{
2090 struct conf_errors *conf_err;
2091 struct http_reply *reply = NULL;
2092 int rc, cur_arg, ret = 0;
2093
2094 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2095 ret = 1;
2096 goto out;
2097 }
2098
2099 cur_arg = 1;
2100 curpx->conf.args.ctx = ARGC_HERR;
2101 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2102 if (!reply) {
2103 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2104 goto error;
2105 }
2106 else if (!reply->status) {
2107 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2108 goto error;
2109 }
2110
2111 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2112 if (http_err_codes[rc] == reply->status)
2113 break;
2114 }
2115
2116 if (rc >= HTTP_ERR_SIZE) {
2117 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2118 goto error;
2119 }
2120 if (*args[cur_arg]) {
2121 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2122 goto error;
2123 }
2124
2125 conf_err = calloc(1, sizeof(*conf_err));
2126 if (!conf_err) {
2127 memprintf(errmsg, "%s : out of memory.", args[0]);
2128 goto error;
2129 }
2130 if (reply->type == HTTP_REPLY_ERRFILES) {
2131 int rc = http_get_status_idx(reply->status);
2132
2133 conf_err->type = 2;
2134 conf_err->info.errorfiles.name = reply->body.http_errors;
2135 conf_err->info.errorfiles.status[rc] = 2;
2136 reply->body.http_errors = NULL;
2137 release_http_reply(reply);
2138 }
2139 else {
2140 conf_err->type = 1;
2141 conf_err->info.errorfile.status = reply->status;
2142 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002143 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002144 }
2145 conf_err->file = strdup(file);
2146 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002147 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002148
Christopher Faulet3005d282020-11-13 10:58:01 +01002149 /* handle warning message */
2150 if (*errmsg)
2151 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002152 out:
2153 return ret;
2154
2155 error:
2156 release_http_reply(reply);
2157 ret = -1;
2158 goto out;
2159
2160}
2161
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002162/* Check "errorfiles" proxy keyword */
2163static int proxy_check_errors(struct proxy *px)
2164{
2165 struct conf_errors *conf_err, *conf_err_back;
2166 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002167 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002168
2169 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2170 if (conf_err->type == 1) {
2171 /* errorfile */
2172 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002173 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002174
2175 /* For proxy, to rely on default replies, just don't reference a reply */
2176 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2177 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002178 }
2179 else {
2180 /* errorfiles */
2181 list_for_each_entry(http_errs, &http_errors_list, list) {
2182 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2183 break;
2184 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002185
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002186 /* unknown http-errors section */
2187 if (&http_errs->list == &http_errors_list) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02002188 ha_alert("proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002189 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2190 err |= ERR_ALERT | ERR_FATAL;
2191 free(conf_err->info.errorfiles.name);
2192 goto next;
2193 }
2194
2195 free(conf_err->info.errorfiles.name);
2196 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2197 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002198 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002199 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002200 else if (conf_err->info.errorfiles.status[rc] == 2)
2201 ha_warning("config: proxy '%s' : status '%d' not declared in"
2202 " http-errors section '%s' (at %s:%d).\n",
2203 px->id, http_err_codes[rc], http_errs->id,
2204 conf_err->file, conf_err->line);
2205 }
2206 }
2207 }
2208 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002209 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002210 free(conf_err->file);
2211 free(conf_err);
2212 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002213
2214 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002215 return err;
2216}
2217
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002218static int post_check_errors()
2219{
2220 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002221 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002222 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002223 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002224
2225 node = ebpt_first(&http_error_messages);
2226 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002227 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2228 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002229 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002230 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002231 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2232 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002233 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002234 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002235 (char *)node->key);
2236 err_code |= ERR_WARN;
2237 }
2238 next:
2239 node = ebpt_next(node);
2240 }
2241
2242 return err_code;
2243}
2244
Willy Tarreau016255a2021-02-12 08:40:29 +01002245int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002246{
2247 struct conf_errors *conf_err, *new_conf_err = NULL;
2248 int ret = 0;
2249
2250 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2251 new_conf_err = calloc(1, sizeof(*new_conf_err));
2252 if (!new_conf_err) {
2253 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2254 goto out;
2255 }
2256 new_conf_err->type = conf_err->type;
2257 if (conf_err->type == 1) {
2258 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002259 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002260 }
2261 else {
2262 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2263 if (!new_conf_err->info.errorfiles.name) {
2264 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2265 goto out;
2266 }
2267 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2268 sizeof(conf_err->info.errorfiles.status));
2269 }
2270 new_conf_err->file = strdup(conf_err->file);
2271 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002272 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002273 new_conf_err = NULL;
2274 }
2275 ret = 1;
2276
2277 out:
2278 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002279 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002280}
2281
2282void proxy_release_conf_errors(struct proxy *px)
2283{
2284 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002285
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002286 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2287 if (conf_err->type == 0)
2288 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002289 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002290 free(conf_err->file);
2291 free(conf_err);
2292 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002293}
2294
2295/*
2296 * Parse an <http-errors> section.
2297 * Returns the error code, 0 if OK, or any combination of :
2298 * - ERR_ABORT: must abort ASAP
2299 * - ERR_FATAL: we can continue parsing but not start the service
2300 * - ERR_WARN: a warning has been emitted
2301 * - ERR_ALERT: an alert has been emitted
2302 * Only the two first ones can stop processing, the two others are just
2303 * indicators.
2304 */
2305static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2306{
2307 static struct http_errors *curr_errs = NULL;
2308 int err_code = 0;
2309 const char *err;
2310 char *errmsg = NULL;
2311
2312 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2313 if (!*args[1]) {
2314 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2315 err_code |= ERR_ALERT | ERR_ABORT;
2316 goto out;
2317 }
2318
2319 err = invalid_char(args[1]);
2320 if (err) {
2321 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2322 file, linenum, *err, args[0], args[1]);
2323 err_code |= ERR_ALERT | ERR_FATAL;
2324 }
2325
2326 list_for_each_entry(curr_errs, &http_errors_list, list) {
2327 /* Error if two errors section owns the same name */
2328 if (strcmp(curr_errs->id, args[1]) == 0) {
2329 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2330 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 }
2333 }
2334
2335 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2336 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2337 err_code |= ERR_ALERT | ERR_ABORT;
2338 goto out;
2339 }
2340
Willy Tarreau2b718102021-04-21 07:32:39 +02002341 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002342 curr_errs->id = strdup(args[1]);
2343 curr_errs->conf.file = strdup(file);
2344 curr_errs->conf.line = linenum;
2345 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002346 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002347 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002348 struct buffer *msg;
2349 int status, rc;
2350
2351 if (*(args[1]) == 0 || *(args[2]) == 0) {
2352 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2353 file, linenum, args[0]);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357
2358 status = atol(args[1]);
2359 msg = http_parse_errorfile(status, args[2], &errmsg);
2360 if (!msg) {
2361 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2362 err_code |= ERR_ALERT | ERR_FATAL;
2363 goto out;
2364 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002365 if (errmsg) {
2366 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2367 err_code |= ERR_WARN;
2368 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002369
2370 reply = calloc(1, sizeof(*reply));
2371 if (!reply) {
2372 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376 reply->type = HTTP_REPLY_ERRMSG;
2377 reply->status = status;
2378 reply->ctype = NULL;
2379 LIST_INIT(&reply->hdrs);
2380 reply->body.errmsg = msg;
2381
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002382 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002383 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002384 }
2385 else if (*args[0] != 0) {
2386 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2387 err_code |= ERR_ALERT | ERR_FATAL;
2388 goto out;
2389 }
2390
2391out:
2392 free(errmsg);
2393 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002394}
2395
2396static struct cfg_kw_list cfg_kws = {ILH, {
2397 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2398 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2399 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2400 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002401 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002402 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002403 { 0, NULL, NULL },
2404}};
2405
2406INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002407REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002408REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002409
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002410REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2411
Christopher Faulet29f72842019-12-11 15:52:32 +01002412/************************************************************************/
2413/* HTX sample fetches */
2414/************************************************************************/
2415
2416/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2417static int
2418smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2419{
2420 if (!smp->strm)
2421 return 0;
2422
2423 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2424 smp->data.type = SMP_T_BOOL;
2425 return 1;
2426}
2427
2428/* Returns the number of blocks in an HTX message. The channel is chosen
2429 * depending on the sample direction. */
2430static int
2431smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2432{
2433 struct channel *chn;
2434 struct htx *htx;
2435
2436 if (!smp->strm)
2437 return 0;
2438
2439 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002440 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002441 if (!htx)
2442 return 0;
2443
2444 smp->data.u.sint = htx_nbblks(htx);
2445 smp->data.type = SMP_T_SINT;
2446 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2447 return 1;
2448}
2449
2450/* Returns the size of an HTX message. The channel is chosen depending on the
2451 * sample direction. */
2452static int
2453smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2454{
2455 struct channel *chn;
2456 struct htx *htx;
2457
2458 if (!smp->strm)
2459 return 0;
2460
2461 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002462 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002463 if (!htx)
2464 return 0;
2465
2466 smp->data.u.sint = htx->size;
2467 smp->data.type = SMP_T_SINT;
2468 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2469 return 1;
2470}
2471
2472/* Returns the data size of an HTX message. The channel is chosen depending on the
2473 * sample direction. */
2474static int
2475smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2476{
2477 struct channel *chn;
2478 struct htx *htx;
2479
2480 if (!smp->strm)
2481 return 0;
2482
2483 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002484 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002485 if (!htx)
2486 return 0;
2487
2488 smp->data.u.sint = htx->data;
2489 smp->data.type = SMP_T_SINT;
2490 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2491 return 1;
2492}
2493
2494/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2495 * depending on the sample direction. */
2496static int
2497smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2498{
2499 struct channel *chn;
2500 struct htx *htx;
2501
2502 if (!smp->strm)
2503 return 0;
2504
2505 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002506 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002507 if (!htx)
2508 return 0;
2509
2510 smp->data.u.sint = htx_used_space(htx);
2511 smp->data.type = SMP_T_SINT;
2512 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2513 return 1;
2514}
2515
2516/* Returns the free space (size-used) of an HTX message. The channel is chosen
2517 * depending on the sample direction. */
2518static int
2519smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2520{
2521 struct channel *chn;
2522 struct htx *htx;
2523
2524 if (!smp->strm)
2525 return 0;
2526
2527 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002528 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002529 if (!htx)
2530 return 0;
2531
2532 smp->data.u.sint = htx_free_space(htx);
2533 smp->data.type = SMP_T_SINT;
2534 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2535 return 1;
2536}
2537
2538/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2539 * channel is chosen depending on the sample direction. */
2540static int
2541smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2542{
2543 struct channel *chn;
2544 struct htx *htx;
2545
2546 if (!smp->strm)
2547 return 0;
2548
2549 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002550 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002551 if (!htx)
2552 return 0;
2553
2554 smp->data.u.sint = htx_free_data_space(htx);
2555 smp->data.type = SMP_T_SINT;
2556 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2557 return 1;
2558}
2559
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002560/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2561 * channel is chosen depending on the sample direction.
2562 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002563static int
2564smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2565{
2566 struct channel *chn;
2567 struct htx *htx;
2568
2569 if (!smp->strm)
2570 return 0;
2571
2572 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002573 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002574 if (!htx)
2575 return 0;
2576
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002577 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002578 smp->data.type = SMP_T_BOOL;
2579 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2580 return 1;
2581}
2582
2583/* Returns the type of a specific HTX block, if found in the message. Otherwise
2584 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2585 * "head", "tail" or "first". The channel is chosen depending on the sample
2586 * direction. */
2587static int
2588smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2589{
2590 struct channel *chn;
2591 struct htx *htx;
2592 enum htx_blk_type type;
2593 int32_t pos;
2594
2595 if (!smp->strm || !arg_p)
2596 return 0;
2597
2598 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002599 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002600 if (!htx)
2601 return 0;
2602
2603 pos = arg_p[0].data.sint;
2604 if (pos == -1)
2605 type = htx_get_head_type(htx);
2606 else if (pos == -2)
2607 type = htx_get_tail_type(htx);
2608 else if (pos == -3)
2609 type = htx_get_first_type(htx);
2610 else
2611 type = ((pos >= htx->head && pos <= htx->tail)
2612 ? htx_get_blk_type(htx_get_blk(htx, pos))
2613 : HTX_BLK_UNUSED);
2614
2615 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2616 smp->data.type = SMP_T_STR;
2617 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2618 return 1;
2619}
2620
2621/* Returns the size of a specific HTX block, if found in the message. Otherwise
2622 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2623 * "first". The channel is chosen depending on the sample direction. */
2624static int
2625smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2626{
2627 struct channel *chn;
2628 struct htx *htx;
2629 struct htx_blk *blk;
2630 int32_t pos;
2631
2632 if (!smp->strm || !arg_p)
2633 return 0;
2634
2635 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002636 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002637 if (!htx)
2638 return 0;
2639
2640 pos = arg_p[0].data.sint;
2641 if (pos == -1)
2642 blk = htx_get_head_blk(htx);
2643 else if (pos == -2)
2644 blk = htx_get_tail_blk(htx);
2645 else if (pos == -3)
2646 blk = htx_get_first_blk(htx);
2647 else
2648 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2649
2650 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2651 smp->data.type = SMP_T_SINT;
2652 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2653 return 1;
2654}
2655
2656/* Returns the start-line if the selected HTX block exists and is a
2657 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2658 * supported or "head", "tail" or "first". The channel is chosen depending on
2659 * the sample direction. */
2660static int
2661smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2662{
2663 struct buffer *temp;
2664 struct channel *chn;
2665 struct htx *htx;
2666 struct htx_blk *blk;
2667 struct htx_sl *sl;
2668 int32_t pos;
2669
2670 if (!smp->strm || !arg_p)
2671 return 0;
2672
2673 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002674 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002675 if (!htx)
2676 return 0;
2677
2678 pos = arg_p[0].data.sint;
2679 if (pos == -1)
2680 blk = htx_get_head_blk(htx);
2681 else if (pos == -2)
2682 blk = htx_get_tail_blk(htx);
2683 else if (pos == -3)
2684 blk = htx_get_first_blk(htx);
2685 else
2686 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2687
2688 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2689 smp->data.u.str.size = 0;
2690 smp->data.u.str.area = "";
2691 smp->data.u.str.data = 0;
2692 }
2693 else {
2694 sl = htx_get_blk_ptr(htx, blk);
2695
2696 temp = get_trash_chunk();
2697 chunk_istcat(temp, htx_sl_p1(sl));
2698 temp->area[temp->data++] = ' ';
2699 chunk_istcat(temp, htx_sl_p2(sl));
2700 temp->area[temp->data++] = ' ';
2701 chunk_istcat(temp, htx_sl_p3(sl));
2702
2703 smp->data.u.str = *temp;
2704 }
2705
2706 smp->data.type = SMP_T_STR;
2707 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2708 return 1;
2709}
2710
2711/* Returns the header name if the selected HTX block exists and is a header or a
2712 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2713 * supported or "head", "tail" or "first". The channel is chosen depending on
2714 * the sample direction. */
2715static int
2716smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2717{
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_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
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 name = htx_get_blk_name(htx, blk);
2748
2749 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2750 }
2751 smp->data.type = SMP_T_STR;
2752 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2753 return 1;
2754}
2755
2756/* Returns the header value if the selected HTX block exists and is a header or
2757 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2758 * supported or "head", "tail" or "first". The channel is chosen depending on
2759 * the sample direction. */
2760static int
2761smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2762{
2763 struct channel *chn;
2764 struct htx *htx;
2765 struct htx_blk *blk;
2766 int32_t pos;
2767
2768 if (!smp->strm || !arg_p)
2769 return 0;
2770
2771 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002772 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002773 if (!htx)
2774 return 0;
2775
2776 pos = arg_p[0].data.sint;
2777 if (pos == -1)
2778 blk = htx_get_head_blk(htx);
2779 else if (pos == -2)
2780 blk = htx_get_tail_blk(htx);
2781 else if (pos == -3)
2782 blk = htx_get_first_blk(htx);
2783 else
2784 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2785
2786 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2787 smp->data.u.str.size = 0;
2788 smp->data.u.str.area = "";
2789 smp->data.u.str.data = 0;
2790 }
2791 else {
2792 struct ist val = htx_get_blk_value(htx, blk);
2793
2794 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2795 }
2796 smp->data.type = SMP_T_STR;
2797 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2798 return 1;
2799}
2800
2801/* Returns the value if the selected HTX block exists and is a data
2802 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2803 * or "head", "tail" or "first". The channel is chosen depending on the sample
2804 * direction. */
2805static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002806smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002807{
2808 struct channel *chn;
2809 struct htx *htx;
2810 struct htx_blk *blk;
2811 int32_t pos;
2812
2813 if (!smp->strm || !arg_p)
2814 return 0;
2815
2816 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002817 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002818 if (!htx)
2819 return 0;
2820
2821 pos = arg_p[0].data.sint;
2822 if (pos == -1)
2823 blk = htx_get_head_blk(htx);
2824 else if (pos == -2)
2825 blk = htx_get_tail_blk(htx);
2826 else if (pos == -3)
2827 blk = htx_get_first_blk(htx);
2828 else
2829 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2830
2831 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2832 smp->data.u.str.size = 0;
2833 smp->data.u.str.area = "";
2834 smp->data.u.str.data = 0;
2835 }
2836 else {
2837 struct ist val = htx_get_blk_value(htx, blk);
2838
2839 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2840 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002841 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002842 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2843 return 1;
2844}
2845
2846/* This function is used to validate the arguments passed to any "htx_blk" fetch
2847 * keywords. An argument is expected by these keywords. It must be a positive
2848 * integer or on of the following strings: "head", "tail" or "first". It returns
2849 * 0 on error, and a non-zero value if OK.
2850 */
2851int val_blk_arg(struct arg *arg, char **err_msg)
2852{
2853 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2854 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2855 return 0;
2856 }
2857 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002858 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002859 arg[0].type = ARGT_SINT;
2860 arg[0].data.sint = -1;
2861 }
2862 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002863 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002864 arg[0].type = ARGT_SINT;
2865 arg[0].data.sint = -2;
2866 }
2867 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002868 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002869 arg[0].type = ARGT_SINT;
2870 arg[0].data.sint = -3;
2871 }
2872 else {
2873 int pos;
2874
2875 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002876 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002877 memprintf(err_msg, "invalid block position");
2878 return 0;
2879 }
2880 }
2881
2882 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2883 if (pos < 0) {
2884 memprintf(err_msg, "block position must not be negative");
2885 return 0;
2886 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002887 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002888 arg[0].type = ARGT_SINT;
2889 arg[0].data.sint = pos;
2890 }
2891
2892 return 1;
2893}
2894
2895
2896/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002897 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002898 */
2899static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002900 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002901
Christopher Faulet01f44452020-01-08 14:23:40 +01002902 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2903 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2904 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2905 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2906 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2907 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2908 { "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 +01002909
Christopher Faulet01f44452020-01-08 14:23:40 +01002910 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2911 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2912 { "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},
2913 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2914 { "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 +01002915 { "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 +01002916
2917 { /* END */ },
2918}};
2919
2920INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);