blob: 3dfbd010624feba44e9ca8e89dfef32afd5291dd [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);
Christopher Faulet84cdbe42022-11-22 15:41:48 +0100392 sl->flags &= ~HTX_SL_F_NORMALIZED_URI;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200393 if (!http_update_host(htx, sl, uri))
394 goto fail;
395
396 return 1;
397 fail:
398 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200399}
400
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200401/* Replace the request path in the HTX message <htx> by <path>. The host part is
402 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
403 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
404 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200405 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200406int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200407{
408 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200409 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100410 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200411 size_t plen = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200412 struct http_uri_parser parser;
Christopher Faulete010c802018-10-24 10:36:45 +0200413
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100414 if (!sl)
415 return 0;
416
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100417 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +0200418 parser = http_uri_parser_init(uri);
419 p = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +0100420 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100421 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200422 if (with_qs)
423 plen = p.len;
424 else {
425 while (plen < p.len && *(p.ptr + plen) != '?')
426 plen++;
427 }
Christopher Faulete010c802018-10-24 10:36:45 +0200428
429 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100430 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
431 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200432
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100433 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
434 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
435
436 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100437 chunk_istcat(temp, path); /* uri: new path */
Christopher Faulete010c802018-10-24 10:36:45 +0200438 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100439 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200440
441 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100442 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200443}
444
445/* Replace the request query-string in the HTX message <htx> by <query>. The
446 * host part and the path are preserved. It returns 1 on success, otherwise
447 * 0.
448 */
449int http_replace_req_query(struct htx *htx, const struct ist query)
450{
451 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200452 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100453 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200454 int offset = 1;
455
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100456 if (!sl)
457 return 0;
458
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100459 uri = htx_sl_req_uri(sl);
460 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200461 while (q.len > 0 && *(q.ptr) != '?') {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100462 q = istnext(q);
Christopher Faulete010c802018-10-24 10:36:45 +0200463 }
464
465 /* skip the question mark or indicate that we must insert it
466 * (but only if the format string is not empty then).
467 */
468 if (q.len) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100469 q = istnext(q);
Christopher Faulete010c802018-10-24 10:36:45 +0200470 }
471 else if (query.len > 1)
472 offset = 0;
473
474 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
476 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200477
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100478 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
479 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200480
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100481 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
482 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
483 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200484
485 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100486 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200487}
488
489/* Replace the response status in the HTX message <htx> by <status>. It returns
490 * 1 on success, otherwise 0.
491*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200492int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200493{
494 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200495 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200496 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200497
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100498 if (!sl)
499 return 0;
500
Christopher Faulete010c802018-10-24 10:36:45 +0200501 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100502 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
503 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200504 r = reason;
505 if (!isttest(r)) {
506 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
507 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
508 }
Christopher Faulete010c802018-10-24 10:36:45 +0200509
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100510 /* create the new start line */
511 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200512 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200513}
514
515/* Replace the response reason in the HTX message <htx> by <reason>. It returns
516 * 1 on success, otherwise 0.
517*/
518int http_replace_res_reason(struct htx *htx, const struct ist reason)
519{
520 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200521 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200523
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100524 if (!sl)
525 return 0;
526
Christopher Faulete010c802018-10-24 10:36:45 +0200527 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100528 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
529 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200530
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100531 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
532 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200533
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100534 /* create the new start line */
535 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200536}
537
Christopher Faulet47596d32018-10-22 09:17:28 +0200538/* Replaces a part of a header value referenced in the context <ctx> by
539 * <data>. It returns 1 on success, otherwise it returns 0. The context is
540 * updated if necessary.
541 */
542int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
543{
544 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200545 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200546 char *start;
547 struct ist v;
548 uint32_t len, off;
549
550 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200551 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200552
553 v = htx_get_blk_value(htx, blk);
554 start = ctx->value.ptr - ctx->lws_before;
555 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
556 off = start - v.ptr;
557
558 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
559 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200560 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200561
562 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200563
564 sl = http_get_stline(htx);
565 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
566 struct ist n = htx_get_blk_name(htx, blk);
567
568 if (isteq(n, ist("host"))) {
569 if (!http_update_authority(htx, sl, v))
570 goto fail;
571 ctx->blk = NULL;
572 http_find_header(htx, ist("host"), ctx, 1);
573 blk = ctx->blk;
574 v = htx_get_blk_value(htx, blk);
575 }
576 }
577
Christopher Faulet47596d32018-10-22 09:17:28 +0200578 ctx->blk = blk;
Tim Duesterhus77508502022-03-15 13:11:06 +0100579 ctx->value = ist2(v.ptr + off, data.len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200580 ctx->lws_before = ctx->lws_after = 0;
581
582 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200583 fail:
584 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200585}
586
587/* Fully replaces a header referenced in the context <ctx> by the name <name>
588 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
589 * context is updated if necessary.
590 */
591int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
592 const struct ist name, const struct ist value)
593{
594 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200595 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200596
597 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200598 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200599
600 blk = htx_replace_header(htx, blk, name, value);
601 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200602 goto fail;
603
604 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100605 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200606 if (!http_update_authority(htx, sl, value))
607 goto fail;
608 ctx->blk = NULL;
609 http_find_header(htx, ist("host"), ctx, 1);
610 blk = ctx->blk;
611 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200612
613 ctx->blk = blk;
614 ctx->value = ist(NULL);
615 ctx->lws_before = ctx->lws_after = 0;
616
617 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200618 fail:
619 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200620}
621
622/* Remove one value of a header. This only works on a <ctx> returned by
623 * http_find_header function. The value is removed, as well as surrounding commas
624 * if any. If the removed value was alone, the whole header is removed. The
625 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
626 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
627 * form that can be handled by http_find_header() to find next occurrence.
628 */
629int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
630{
631 struct htx_blk *blk = ctx->blk;
632 char *start;
633 struct ist v;
634 uint32_t len;
635
636 if (!blk)
637 return 0;
638
639 start = ctx->value.ptr - ctx->lws_before;
640 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
641
642 v = htx_get_blk_value(htx, blk);
643 if (len == v.len) {
644 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200645 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200646 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100647 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200648 ctx->lws_before = ctx->lws_after = 0;
649 }
650 else {
651 ctx->blk = htx_get_blk(htx, htx->tail);
652 ctx->value = htx_get_blk_value(htx, ctx->blk);
653 ctx->lws_before = ctx->lws_after = 0;
654 }
655 return 1;
656 }
657
658 /* This was not the only value of this header. We have to remove the
659 * part pointed by ctx->value. If it is the last entry of the list, we
660 * remove the last separator.
661 */
662 if (start == v.ptr) {
663 /* It's the first header part but not the only one. So remove
664 * the comma after it. */
665 len++;
666 }
667 else {
668 /* There is at least one header part before the removed one. So
669 * remove the comma between them. */
670 start--;
671 len++;
672 }
673 /* Update the block content and its len */
674 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200675 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200676
677 /* Finally update the ctx */
Tim Duesterhus77508502022-03-15 13:11:06 +0100678 ctx->value = ist2(start, 0);
Christopher Faulet47596d32018-10-22 09:17:28 +0200679 ctx->lws_before = ctx->lws_after = 0;
680
681 return 1;
682}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200683
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200684/* Updates the authority part of the uri with the value <host>. It happens when
685 * the header host is modified. It returns 0 on failure and 1 on success. It is
686 * the caller responsibility to provide the start-line and to be sure the uri
687 * contains an authority. Thus, if no authority is found in the uri, an error is
688 * returned.
689 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200690int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200691{
692 struct buffer *temp = get_trash_chunk();
693 struct ist meth, vsn, uri, authority;
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200694 struct http_uri_parser parser;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200695
696 uri = htx_sl_req_uri(sl);
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200697 parser = http_uri_parser_init(uri);
698 authority = http_parse_authority(&parser, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100699 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200700 return 0;
701
Christopher Faulet34b18e42020-02-18 11:02:21 +0100702 /* Don't update the uri if there is no change */
703 if (isteq(host, authority))
704 return 1;
705
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200706 /* Start by copying old method and version */
707 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
708 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
709
710 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
711 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
712
713 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100714 chunk_istcat(temp, host);
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100715 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200716 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
717
718 return http_replace_stline(htx, meth, uri, vsn);
719
720}
721
722/* Update the header host by extracting the authority of the uri <uri>. flags of
723 * the start-line are also updated accordingly. For orgin-form and asterisk-form
724 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
725 * removed from the flags of the start-line. Otherwise, this flag is set and the
726 * authority is used to set the value of the header host. This function returns
727 * 0 on failure and 1 on success.
728*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200729int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200730{
731 struct ist authority;
732 struct http_hdr_ctx ctx;
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200733 struct http_uri_parser parser = http_uri_parser_init(uri);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200734
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200735 if (parser.format == URI_PARSER_FORMAT_EMPTY ||
736 parser.format == URI_PARSER_FORMAT_ASTERISK ||
737 parser.format == URI_PARSER_FORMAT_ABSPATH) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200738 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
739 }
740 else {
741 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
742 if (sl->info.req.meth != HTTP_METH_CONNECT) {
743 // absolute-form (RFC7320 #5.3.2)
744 sl->flags |= HTX_SL_F_HAS_SCHM;
745 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
746 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
747
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200748 authority = http_parse_authority(&parser, 1);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200749 if (!authority.len)
750 goto fail;
751 }
752 else {
753 // authority-form (RFC7320 #5.3.3)
754 authority = uri;
755 }
756
757 /* Replace header host value */
758 ctx.blk = NULL;
759 while (http_find_header(htx, ist("host"), &ctx, 1)) {
760 if (!http_replace_header_value(htx, &ctx, authority))
761 goto fail;
762 }
763
764 }
765 return 1;
766 fail:
767 return 0;
768}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200769
770/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
771 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
772 * performed over the whole headers. Otherwise it must contain a valid header
773 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
774 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
775 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
776 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
777 * -1. The value fetch stops at commas, so this function is suited for use with
778 * list headers.
779 * The return value is 0 if nothing was found, or non-zero otherwise.
780 */
781unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
782 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
783{
784 struct http_hdr_ctx local_ctx;
785 struct ist val_hist[MAX_HDR_HISTORY];
786 unsigned int hist_idx;
787 int found;
788
789 if (!ctx) {
790 local_ctx.blk = NULL;
791 ctx = &local_ctx;
792 }
793
794 if (occ >= 0) {
795 /* search from the beginning */
796 while (http_find_header(htx, hdr, ctx, 0)) {
797 occ--;
798 if (occ <= 0) {
799 *vptr = ctx->value.ptr;
800 *vlen = ctx->value.len;
801 return 1;
802 }
803 }
804 return 0;
805 }
806
807 /* negative occurrence, we scan all the list then walk back */
808 if (-occ > MAX_HDR_HISTORY)
809 return 0;
810
811 found = hist_idx = 0;
812 while (http_find_header(htx, hdr, ctx, 0)) {
813 val_hist[hist_idx] = ctx->value;
814 if (++hist_idx >= MAX_HDR_HISTORY)
815 hist_idx = 0;
816 found++;
817 }
818 if (-occ > found)
819 return 0;
820
821 /* OK now we have the last occurrence in [hist_idx-1], and we need to
822 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
823 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
824 * to remain in the 0..9 range.
825 */
826 hist_idx += occ + MAX_HDR_HISTORY;
827 if (hist_idx >= MAX_HDR_HISTORY)
828 hist_idx -= MAX_HDR_HISTORY;
829 *vptr = val_hist[hist_idx].ptr;
830 *vlen = val_hist[hist_idx].len;
831 return 1;
832}
833
834/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
835 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
836 * performed over the whole headers. Otherwise it must contain a valid header
837 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
838 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
839 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
840 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
841 * -1. This function differs from http_get_hdr() in that it only returns full
842 * line header values and does not stop at commas.
843 * The return value is 0 if nothing was found, or non-zero otherwise.
844 */
845unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
846 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
847{
848 struct http_hdr_ctx local_ctx;
849 struct ist val_hist[MAX_HDR_HISTORY];
850 unsigned int hist_idx;
851 int found;
852
853 if (!ctx) {
854 local_ctx.blk = NULL;
855 ctx = &local_ctx;
856 }
857
858 if (occ >= 0) {
859 /* search from the beginning */
860 while (http_find_header(htx, hdr, ctx, 1)) {
861 occ--;
862 if (occ <= 0) {
863 *vptr = ctx->value.ptr;
864 *vlen = ctx->value.len;
865 return 1;
866 }
867 }
868 return 0;
869 }
870
871 /* negative occurrence, we scan all the list then walk back */
872 if (-occ > MAX_HDR_HISTORY)
873 return 0;
874
875 found = hist_idx = 0;
876 while (http_find_header(htx, hdr, ctx, 1)) {
877 val_hist[hist_idx] = ctx->value;
878 if (++hist_idx >= MAX_HDR_HISTORY)
879 hist_idx = 0;
880 found++;
881 }
882 if (-occ > found)
883 return 0;
884
885 /* OK now we have the last occurrence in [hist_idx-1], and we need to
886 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
887 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
888 * to remain in the 0..9 range.
889 */
890 hist_idx += occ + MAX_HDR_HISTORY;
891 if (hist_idx >= MAX_HDR_HISTORY)
892 hist_idx -= MAX_HDR_HISTORY;
893 *vptr = val_hist[hist_idx].ptr;
894 *vlen = val_hist[hist_idx].len;
895 return 1;
896}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100897
Christopher Fauleta66adf42020-11-05 22:43:41 +0100898int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100899{
900 struct htx *htx;
901 struct htx_sl *sl;
902 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200903 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100904 union h1_sl h1sl;
905 unsigned int flags = HTX_SL_F_IS_RESP;
906 int ret = 0;
907
Christopher Faulet90cc4812019-07-22 16:49:30 +0200908 b_reset(buf);
909 if (!raw.len) {
910 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100911 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200912 return 1;
913 }
914
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100915 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200916 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100917 if (!buf->area)
918 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100919
920 h1m_init_res(&h1m);
921 h1m.flags |= H1_MF_NO_PHDR;
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100922 ret = h1_headers_to_hdr_list(raw.ptr, istend(raw),
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100923 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100924 if (ret <= 0) {
925 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100926 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100927 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100928
Christopher Fauleta66adf42020-11-05 22:43:41 +0100929 if (unlikely(h1sl.st.v.len != 8)) {
930 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100931 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100932 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100933 if ((*(h1sl.st.v.ptr + 5) > '1') ||
934 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
935 h1m.flags |= H1_MF_VER_11;
936
Christopher Fauleta66adf42020-11-05 22:43:41 +0100937 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
938 memprintf(errmsg, "invalid http status code for an error message (%u)",
939 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200940 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100941 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200942
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200943 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
944 /* Responses known to have no body. */
945 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
946 h1m.flags |= H1_MF_XFER_LEN;
947 h1m.curr_len = h1m.body_len = 0;
948 }
949 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
950 h1m.flags |= H1_MF_XFER_LEN;
951
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100952 if (h1m.flags & H1_MF_VER_11)
953 flags |= HTX_SL_F_VER_11;
954 if (h1m.flags & H1_MF_XFER_ENC)
955 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200956 if (h1m.flags & H1_MF_XFER_LEN) {
957 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100958 if (h1m.flags & H1_MF_CHNK) {
959 memprintf(errmsg, "chunk-encoded payload not supported");
960 goto error;
961 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200962 else if (h1m.flags & H1_MF_CLEN) {
963 flags |= HTX_SL_F_CLEN;
964 if (h1m.body_len == 0)
965 flags |= HTX_SL_F_BODYLESS;
966 }
967 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200968 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100969 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200970
Christopher Fauleta66adf42020-11-05 22:43:41 +0100971 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
972 memprintf(errmsg, "message payload not expected");
973 goto error;
974 }
975 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
976 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100977 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100978 goto error;
979 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100980
981 htx = htx_from_buf(buf);
982 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 +0100983 if (!sl || !htx_add_all_headers(htx, hdrs)) {
984 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100985 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100986 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100987 sl->info.res.status = h1sl.st.status;
988
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200989 while (raw.len > ret) {
990 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100991 if (!sent) {
992 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100993 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100994 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200995 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100996 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200997
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100998 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200999
Christopher Faulet90cc4812019-07-22 16:49:30 +02001000 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001001
1002error:
1003 if (buf->size)
1004 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001005 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001006}
1007
Christopher Faulet18630642020-05-12 18:57:28 +02001008void release_http_reply(struct http_reply *http_reply)
1009{
1010 struct logformat_node *lf, *lfb;
1011 struct http_reply_hdr *hdr, *hdrb;
1012
1013 if (!http_reply)
1014 return;
1015
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001016 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001017 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001018 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001019 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001020 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001021 release_sample_expr(lf->expr);
1022 free(lf->arg);
1023 free(lf);
1024 }
1025 istfree(&hdr->name);
1026 free(hdr);
1027 }
1028
1029 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001030 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001031 }
1032 else if (http_reply->type == HTTP_REPLY_RAW)
1033 chunk_destroy(&http_reply->body.obj);
1034 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1035 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001036 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001037 release_sample_expr(lf->expr);
1038 free(lf->arg);
1039 free(lf);
1040 }
1041 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001042 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001043}
1044
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001045static int http_htx_init(void)
1046{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001047 struct buffer chk;
1048 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001049 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001050 int rc;
1051 int err_code = 0;
1052
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001053 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1054 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001055 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001056 err_code |= ERR_ALERT | ERR_FATAL;
1057 continue;
1058 }
1059
Tim Duesterhus77508502022-03-15 13:11:06 +01001060 raw = ist(http_err_msgs[rc]);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001061 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1062 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1063 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001064 err_code |= ERR_ALERT | ERR_FATAL;
1065 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001066 else if (errmsg) {
1067 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1068 err_code |= ERR_WARN;
1069 }
1070
1071 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001072 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001073
Christopher Fauletf7346382019-07-17 22:02:08 +02001074 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001075 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1076 http_err_replies[rc].status = http_err_codes[rc];
1077 http_err_replies[rc].ctype = NULL;
1078 LIST_INIT(&http_err_replies[rc].hdrs);
1079 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001080 }
1081end:
1082 return err_code;
1083}
1084
Christopher Faulet58857752020-01-15 15:19:50 +01001085static void http_htx_deinit(void)
1086{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001087 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001088 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001089 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001090 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001091 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001092
1093 node = ebpt_first(&http_error_messages);
1094 while (node) {
1095 next = ebpt_next(node);
1096 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001097 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1098 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001099 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001100 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001101 node = next;
1102 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001103
1104 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1105 free(http_errs->conf.file);
1106 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001107 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1108 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001109 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001110 free(http_errs);
1111 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001112
1113 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001114 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001115 release_http_reply(http_rep);
1116 }
Tim Duesterhus2b7fa9d2022-04-26 23:35:07 +02001117
1118 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1119 chunk_destroy(&http_err_chunks[rc]);
Christopher Faulet58857752020-01-15 15:19:50 +01001120}
1121
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001122REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001123REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001124
Christopher Faulet58857752020-01-15 15:19:50 +01001125/* Reads content of the error file <file> and convert it into an HTX message. On
1126 * success, the HTX message is returned. On error, NULL is returned and an error
1127 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001128 */
Christopher Faulet58857752020-01-15 15:19:50 +01001129struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001130{
Christopher Faulet58857752020-01-15 15:19:50 +01001131 struct buffer *buf = NULL;
1132 struct buffer chk;
1133 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001134 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001135 struct stat stat;
1136 char *err = NULL;
1137 int errnum, errlen;
1138 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001139
1140 /* already loaded */
1141 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1142 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001143 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1144 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001145 goto out;
1146 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001147
Christopher Faulet58857752020-01-15 15:19:50 +01001148 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001149 fd = open(file, O_RDONLY);
1150 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1151 memprintf(errmsg, "error opening file '%s'.", file);
1152 goto out;
1153 }
1154
1155 if (stat.st_size <= global.tune.bufsize)
1156 errlen = stat.st_size;
1157 else {
1158 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1159 file, global.tune.bufsize);
1160 errlen = global.tune.bufsize;
1161 }
1162
1163 err = malloc(errlen);
1164 if (!err) {
1165 memprintf(errmsg, "out of memory.");
1166 goto out;
1167 }
1168
1169 errnum = read(fd, err, errlen);
1170 if (errnum != errlen) {
1171 memprintf(errmsg, "error reading file '%s'.", file);
1172 goto out;
1173 }
1174
Christopher Faulet58857752020-01-15 15:19:50 +01001175 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001176 http_errmsg = calloc(1, sizeof(*http_errmsg));
1177 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001178 memprintf(errmsg, "out of memory.");
1179 goto out;
1180 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001181 http_errmsg->node.key = strdup(file);
1182 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001183 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001184 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001185 goto out;
1186 }
1187
1188 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001189 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1190 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001191 free(http_errmsg->node.key);
1192 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001193 goto out;
1194 }
1195
Christopher Faulet58857752020-01-15 15:19:50 +01001196 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001197 http_errmsg->msg = chk;
1198 ebis_insert(&http_error_messages, &http_errmsg->node);
1199 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001200
Christopher Faulet5031ef52020-01-15 11:22:07 +01001201 out:
1202 if (fd >= 0)
1203 close(fd);
1204 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001205 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001206}
1207
Ilya Shipitsind4259502020-04-08 01:07:56 +05001208/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001209 * message is returned. On error, NULL is returned and an error message is
1210 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001211 */
Christopher Faulet58857752020-01-15 15:19:50 +01001212struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001213{
Christopher Faulet58857752020-01-15 15:19:50 +01001214 struct buffer *buf = NULL;
1215 struct buffer chk;
1216 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001217 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001218
1219 /* already loaded */
1220 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1221 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001222 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1223 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001224 goto out;
1225 }
1226 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001227 http_errmsg = calloc(1, sizeof(*http_errmsg));
1228 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001229 memprintf(errmsg, "out of memory.");
1230 goto out;
1231 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001232 http_errmsg->node.key = strdup(key);
1233 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001234 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001235 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001236 goto out;
1237 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001238
1239 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001240 if (!http_str_to_htx(&chk, msg, errmsg)) {
1241 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001242 free(http_errmsg->node.key);
1243 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001244 goto out;
1245 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001246
Christopher Faulet58857752020-01-15 15:19:50 +01001247 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001248 http_errmsg->msg = chk;
1249 ebis_insert(&http_error_messages, &http_errmsg->node);
1250 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001251 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001252 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001253}
1254
Christopher Faulet5031ef52020-01-15 11:22:07 +01001255/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001256 * <status>. It returns NULL if there is any error, otherwise it return the
1257 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001258 */
Christopher Faulet58857752020-01-15 15:19:50 +01001259struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001260{
Christopher Faulet58857752020-01-15 15:19:50 +01001261 struct buffer *buf = NULL;
1262 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001263
1264 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1265 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001266 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001267 break;
1268 }
1269 }
1270
1271 if (rc >= HTTP_ERR_SIZE)
1272 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001273 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001274}
1275
1276/* This function creates HTX error message corresponding to a redirect message
1277 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001278 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1279 * returns NULL if there is any error, otherwise it return the corresponding HTX
1280 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001281 */
Christopher Faulet58857752020-01-15 15:19:50 +01001282struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001283{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001284 static const char *HTTP_302 =
1285 "HTTP/1.1 302 Found\r\n"
1286 "Cache-Control: no-cache\r\n"
1287 "Content-length: 0\r\n"
1288 "Location: "; /* not terminated since it will be concatenated with the URL */
1289 static const char *HTTP_303 =
1290 "HTTP/1.1 303 See Other\r\n"
1291 "Cache-Control: no-cache\r\n"
1292 "Content-length: 0\r\n"
1293 "Location: "; /* not terminated since it will be concatenated with the URL */
1294
Christopher Faulet58857752020-01-15 15:19:50 +01001295 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001296 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001297 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001298 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001299
1300 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1301 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001302 /* Create the error key */
1303 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1304 memprintf(errmsg, "out of memory.");
1305 goto out;
1306 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001307 /* Create the error message */
1308 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1309 errlen = strlen(msg) + strlen(url) + 5;
1310 err = malloc(errlen);
1311 if (!err) {
1312 memprintf(errmsg, "out of memory.");
1313 goto out;
1314 }
1315 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1316
1317 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001318 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001319 break;
1320 }
1321 }
1322
1323 if (rc >= HTTP_ERR_SIZE)
1324 memprintf(errmsg, "status code '%d' not handled.", status);
1325out:
Christopher Faulet58857752020-01-15 15:19:50 +01001326 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001327 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001328 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001329}
1330
Christopher Faulet7eea2412020-05-13 15:02:59 +02001331/* Check an "http reply" and, for replies referencing an http-errors section,
1332 * try to find the right section and the right error message in this section. If
1333 * found, the reply is updated. If the http-errors section exists but the error
1334 * message is not found, no error message is set to fallback on the default
1335 * ones. Otherwise (unknown section) an error is returned.
1336 *
1337 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1338 * filled.
1339 */
1340int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1341{
1342 struct http_errors *http_errs;
1343 int ret = 1;
1344
1345 if (reply->type != HTTP_REPLY_ERRFILES)
1346 goto end;
1347
1348 list_for_each_entry(http_errs, &http_errors_list, list) {
1349 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001350 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001351 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001352 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1353 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001354 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1355 "not declared in http-errors section '%s'.\n",
1356 px->id, reply->status, http_errs->id);
1357 break;
1358 }
1359 }
1360
1361 if (&http_errs->list == &http_errors_list) {
1362 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1363 reply->body.http_errors);
1364 ret = 0;
1365 }
1366
1367 end:
1368 return ret;
1369}
1370
Christopher Faulet47e791e2020-05-13 14:36:55 +02001371/* Parse an "http reply". It returns the reply on success or NULL on error. This
1372 * function creates one of the following http replies :
1373 *
1374 * - HTTP_REPLY_EMPTY : dummy response, no payload
1375 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1376 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1377 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1378 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1379 *
1380 * The content-type must be defined for non-empty payload. It is ignored for
1381 * error messages (implicit or explicit). When an http-errors section is
1382 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1383 * during the configuration validity check or dynamically. It is the caller
1384 * responsibility to choose. If no status code is configured, <default_status>
1385 * is set.
1386 */
1387struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1388 int default_status, char **errmsg)
1389{
1390 struct logformat_node *lf, *lfb;
1391 struct http_reply *reply = NULL;
1392 struct http_reply_hdr *hdr, *hdrb;
1393 struct stat stat;
1394 const char *act_arg = NULL;
1395 char *obj = NULL;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001396 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001397
1398
1399 reply = calloc(1, sizeof(*reply));
1400 if (!reply) {
1401 memprintf(errmsg, "out of memory");
1402 goto error;
1403 }
1404 LIST_INIT(&reply->hdrs);
1405 reply->type = HTTP_REPLY_EMPTY;
1406 reply->status = default_status;
1407
Christopher Faulet3b967c12020-05-15 15:47:44 +02001408 if (px->conf.args.ctx == ARGC_HERR)
1409 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001410 else {
1411 if (px->cap & PR_CAP_FE)
1412 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1413 if (px->cap & PR_CAP_BE)
Willy Tarreaub39e47a2021-10-16 14:41:09 +02001414 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001415 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001416
1417 cur_arg = *orig_arg;
1418 while (*args[cur_arg]) {
1419 if (strcmp(args[cur_arg], "status") == 0) {
1420 cur_arg++;
1421 if (!*args[cur_arg]) {
1422 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1423 goto error;
1424 }
1425 reply->status = atol(args[cur_arg]);
1426 if (reply->status < 200 || reply->status > 599) {
1427 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1428 goto error;
1429 }
1430 cur_arg++;
1431 }
1432 else if (strcmp(args[cur_arg], "content-type") == 0) {
1433 cur_arg++;
1434 if (!*args[cur_arg]) {
1435 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1436 goto error;
1437 }
1438 free(reply->ctype);
1439 reply->ctype = strdup(args[cur_arg]);
1440 cur_arg++;
1441 }
1442 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1443 if (reply->type != HTTP_REPLY_EMPTY) {
1444 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1445 goto error;
1446 }
1447 act_arg = args[cur_arg];
1448 cur_arg++;
1449 if (!*args[cur_arg]) {
1450 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1451 goto error;
1452 }
1453 reply->body.http_errors = strdup(args[cur_arg]);
1454 if (!reply->body.http_errors) {
1455 memprintf(errmsg, "out of memory");
1456 goto error;
1457 }
1458 reply->type = HTTP_REPLY_ERRFILES;
1459 cur_arg++;
1460 }
1461 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1462 if (reply->type != HTTP_REPLY_EMPTY) {
1463 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1464 goto error;
1465 }
1466 act_arg = args[cur_arg];
1467 reply->type = HTTP_REPLY_ERRMSG;
1468 cur_arg++;
1469 }
1470 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1471 if (reply->type != HTTP_REPLY_EMPTY) {
1472 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1473 goto error;
1474 }
1475 act_arg = args[cur_arg];
1476 cur_arg++;
1477 if (!*args[cur_arg]) {
1478 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1479 goto error;
1480 }
1481 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1482 if (!reply->body.errmsg) {
1483 goto error;
1484 }
1485 reply->type = HTTP_REPLY_ERRMSG;
1486 cur_arg++;
1487 }
1488 else if (strcmp(args[cur_arg], "file") == 0) {
1489 if (reply->type != HTTP_REPLY_EMPTY) {
1490 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1491 goto error;
1492 }
1493 act_arg = args[cur_arg];
1494 cur_arg++;
1495 if (!*args[cur_arg]) {
1496 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1497 goto error;
1498 }
1499 fd = open(args[cur_arg], O_RDONLY);
1500 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1501 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1502 goto error;
1503 }
1504 if (stat.st_size > global.tune.bufsize) {
1505 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1506 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1507 goto error;
1508 }
1509 objlen = stat.st_size;
1510 obj = malloc(objlen);
1511 if (!obj || read(fd, obj, objlen) != objlen) {
1512 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1513 goto error;
1514 }
1515 close(fd);
1516 fd = -1;
1517 reply->type = HTTP_REPLY_RAW;
1518 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1519 obj = NULL;
1520 cur_arg++;
1521 }
1522 else if (strcmp(args[cur_arg], "string") == 0) {
1523 if (reply->type != HTTP_REPLY_EMPTY) {
1524 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1525 goto error;
1526 }
1527 act_arg = args[cur_arg];
1528 cur_arg++;
1529 if (!*args[cur_arg]) {
1530 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1531 goto error;
1532 }
1533 obj = strdup(args[cur_arg]);
1534 objlen = strlen(args[cur_arg]);
1535 if (!obj) {
1536 memprintf(errmsg, "out of memory");
1537 goto error;
1538 }
1539 reply->type = HTTP_REPLY_RAW;
1540 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1541 obj = NULL;
1542 cur_arg++;
1543 }
1544 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1545 if (reply->type != HTTP_REPLY_EMPTY) {
1546 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1547 goto error;
1548 }
1549 act_arg = args[cur_arg];
1550 cur_arg++;
1551 if (!*args[cur_arg]) {
1552 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1553 goto error;
1554 }
1555 fd = open(args[cur_arg], O_RDONLY);
1556 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1557 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1558 goto error;
1559 }
1560 if (stat.st_size > global.tune.bufsize) {
1561 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1562 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1563 goto error;
1564 }
1565 objlen = stat.st_size;
1566 obj = malloc(objlen + 1);
1567 if (!obj || read(fd, obj, objlen) != objlen) {
1568 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1569 goto error;
1570 }
1571 close(fd);
1572 fd = -1;
1573 obj[objlen] = '\0';
1574 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet5a3d9a72022-11-14 08:49:28 +01001575 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001576 cur_arg++;
1577 }
1578 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1579 if (reply->type != HTTP_REPLY_EMPTY) {
1580 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1581 goto error;
1582 }
1583 act_arg = args[cur_arg];
1584 cur_arg++;
1585 if (!*args[cur_arg]) {
1586 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1587 goto error;
1588 }
1589 obj = strdup(args[cur_arg]);
1590 objlen = strlen(args[cur_arg]);
1591 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet5a3d9a72022-11-14 08:49:28 +01001592 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001593 cur_arg++;
1594 }
1595 else if (strcmp(args[cur_arg], "hdr") == 0) {
1596 cur_arg++;
1597 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1598 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1599 goto error;
1600 }
1601 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1602 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1603 strcasecmp(args[cur_arg], "content-type") == 0) {
1604 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1605 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1606 cur_arg += 2;
1607 continue;
1608 }
1609 hdr = calloc(1, sizeof(*hdr));
1610 if (!hdr) {
1611 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1612 goto error;
1613 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001614 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001615 LIST_INIT(&hdr->value);
1616 hdr->name = ist(strdup(args[cur_arg]));
1617 if (!isttest(hdr->name)) {
1618 memprintf(errmsg, "out of memory");
1619 goto error;
1620 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001621 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1622 goto error;
1623
1624 free(px->conf.lfs_file);
1625 px->conf.lfs_file = strdup(px->conf.args.file);
1626 px->conf.lfs_line = px->conf.args.line;
1627 cur_arg += 2;
1628 }
1629 else
1630 break;
1631 }
1632
1633 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1634 if (reply->ctype) {
1635 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1636 " neither errorfile nor payload defined.\n",
1637 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001638 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001639 }
1640 }
1641 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1642
1643 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1644 /* default errorfile or errorfiles: check the status */
1645 int rc;
1646
1647 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1648 if (http_err_codes[rc] == reply->status)
1649 break;
1650 }
1651
1652 if (rc >= HTTP_ERR_SIZE) {
1653 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1654 reply->status, act_arg);
1655 goto error;
1656 }
1657 }
1658
1659 if (reply->ctype) {
1660 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1661 "with an erorrfile.\n",
1662 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001663 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001664 }
1665 if (!LIST_ISEMPTY(&reply->hdrs)) {
1666 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1667 "with an erorrfile.\n",
1668 px->conf.args.file, px->conf.args.line);
1669 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001670 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001671 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001672 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001673 release_sample_expr(lf->expr);
1674 free(lf->arg);
1675 free(lf);
1676 }
1677 istfree(&hdr->name);
1678 free(hdr);
1679 }
1680 }
1681 }
1682 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001683 if ((reply->status == 204 || reply->status == 304) && objlen) {
1684 memprintf(errmsg, "No body expected for %d responses", reply->status);
1685 goto error;
1686 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001687 if (!reply->ctype && objlen) {
1688 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1689 goto error;
1690 }
1691 if (reply->ctype && !b_data(&reply->body.obj)) {
1692 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001693 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001694 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001695 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001696 }
1697 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1698 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1699 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1700 px->conf.args.file, px->conf.args.line);
1701 }
1702 }
1703 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1704 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001705 if ((reply->status == 204 || reply->status == 304)) {
1706 memprintf(errmsg, "No body expected for %d responses", reply->status);
1707 goto error;
1708 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001709 if (!reply->ctype) {
1710 memprintf(errmsg, "a content type must be defined with a log-format payload");
1711 goto error;
1712 }
1713 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1714 goto error;
1715
1716 free(px->conf.lfs_file);
1717 px->conf.lfs_file = strdup(px->conf.args.file);
1718 px->conf.lfs_line = px->conf.args.line;
1719 }
1720
1721 free(obj);
1722 *orig_arg = cur_arg;
1723 return reply;
1724
1725 error:
1726 free(obj);
1727 if (fd >= 0)
1728 close(fd);
1729 release_http_reply(reply);
1730 return NULL;
1731}
1732
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001733/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1734 * Returns 0 if no error has been found else non-zero.
1735 *
1736 * The normalization is processed on the target-uri at the condition that it is
1737 * in absolute-form. In the case where the target-uri was normalized, every
1738 * host headers values found are also replaced by the normalized hostname. This
1739 * assumes that the target-uri and host headers were properly identify as
1740 * similar before calling this function.
1741 */
1742int http_scheme_based_normalize(struct htx *htx)
1743{
1744 struct http_hdr_ctx ctx;
1745 struct htx_sl *sl;
1746 struct ist uri, scheme, authority, host, port;
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +02001747 struct http_uri_parser parser;
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001748
1749 sl = http_get_stline(htx);
1750
1751 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1752 return 0;
1753
1754 uri = htx_sl_req_uri(sl);
1755
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +02001756 parser = http_uri_parser_init(uri);
1757 scheme = http_parse_scheme(&parser);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001758 /* if no scheme found, no normalization to proceed */
1759 if (!isttest(scheme))
1760 return 0;
1761
Christopher Fauletd1d983f2022-07-05 10:24:52 +02001762 /* Extract the port if present in authority */
1763 authority = http_parse_authority(&parser, 1);
1764 port = http_get_host_port(authority);
1765 if (!isttest(port)) {
1766 /* if no port found, no normalization to proceed */
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001767 return 0;
Christopher Fauletd1d983f2022-07-05 10:24:52 +02001768 }
1769 host = isttrim(authority, istlen(authority) - istlen(port) - 1);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001770
Christopher Faulete5dfe112022-11-21 19:20:20 +01001771 if (http_is_default_port(scheme, port)) {
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001772 /* reconstruct the uri with removal of the port */
1773 struct buffer *temp = get_trash_chunk();
Christopher Faulet0eab0502022-07-06 17:41:31 +02001774 struct ist meth, vsn;
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001775
1776 /* meth */
1777 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1778 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1779
1780 /* vsn */
1781 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1782 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1783
1784 /* reconstruct uri without port */
Christopher Faulet0eab0502022-07-06 17:41:31 +02001785 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001786 chunk_istcat(temp, host);
Christopher Faulet0eab0502022-07-06 17:41:31 +02001787 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
1788 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001789
1790 http_replace_stline(htx, meth, uri, vsn);
1791
1792 /* replace every host headers values by the normalized host */
1793 ctx.blk = NULL;
1794 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1795 if (!http_replace_header_value(htx, &ctx, host))
1796 goto fail;
1797 }
1798 }
1799
1800 return 0;
1801
1802 fail:
1803 return 1;
1804}
1805
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +02001806/* First step function to merge multiple cookie headers in a single entry.
1807 *
1808 * Use it for each cookie header at <idx> index over HTTP headers in <list>.
1809 * <first> and <last> are state variables used internally and must be
1810 * initialized to -1 before the first invocation.
1811 */
1812void http_cookie_register(struct http_hdr *list, int idx, int *first, int *last)
1813{
1814 /* Build a linked list of cookie headers. Use header length to point to
1815 * the next one. The last entry will contains -1.
1816 */
1817
1818 /* Mark the current end of cookie linked list. */
1819 list[idx].n.len = -1;
1820 if (*first < 0) {
1821 /* Save first found cookie for http_cookie_merge call. */
1822 *first = idx;
1823 }
1824 else {
1825 /* Update linked list of cookies. */
1826 list[*last].n.len = idx;
1827 }
1828
1829 *last = idx;
1830}
1831
1832/* Second step to merge multiple cookie headers in a single entry.
1833 *
1834 * Use it when looping over HTTP headers is done and <htx> message is built.
1835 * This will concatenate each cookie headers present from <list> directly into
1836 * <htx> message. <first> is reused from previous http_cookie_register
1837 * invocation.
1838 *
1839 * Returns 0 on success else non-zero.
1840 */
1841int http_cookie_merge(struct htx *htx, struct http_hdr *list, int first)
1842{
1843 uint32_t fs; /* free space */
1844 uint32_t bs; /* block size */
1845 uint32_t vl; /* value len */
1846 uint32_t tl; /* total length */
1847 struct htx_blk *blk;
1848
1849 if (first < 0)
1850 return 0;
1851
1852 blk = htx_add_header(htx, ist("cookie"), list[first].v);
1853 if (!blk)
1854 return 1;
1855
1856 tl = list[first].v.len;
1857 fs = htx_free_data_space(htx);
1858 bs = htx_get_blksz(blk);
1859
1860 /* for each extra cookie, we'll extend the cookie's value and insert
1861 * ";" before the new value.
1862 */
1863 fs += tl; /* first one is already counted */
1864
1865 /* Loop over cookies linked list built from http_cookie_register. */
1866 while ((first = list[first].n.len) >= 0) {
1867 vl = list[first].v.len;
1868 tl += vl + 2;
1869 if (tl > fs)
1870 return 1;
1871
1872 htx_change_blk_value_len(htx, blk, tl);
1873 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
1874 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
1875 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2,
1876 list[first].v.ptr, vl);
1877 bs += vl + 2;
1878 }
1879
1880 return 0;
1881}
1882
Christopher Faulet07f41f72020-01-16 16:16:06 +01001883/* Parses the "errorloc[302|303]" proxy keyword */
1884static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001885 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001886 char **errmsg)
1887{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001888 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001889 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001890 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001891 int errloc, status;
1892 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001893
1894 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1895 ret = 1;
1896 goto out;
1897 }
1898
1899 if (*(args[1]) == 0 || *(args[2]) == 0) {
1900 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1901 ret = -1;
1902 goto out;
1903 }
1904
1905 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001906 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001907 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1908 if (!msg) {
1909 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1910 ret = -1;
1911 goto out;
1912 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001913
1914 reply = calloc(1, sizeof(*reply));
1915 if (!reply) {
1916 memprintf(errmsg, "%s : out of memory.", args[0]);
1917 ret = -1;
1918 goto out;
1919 }
1920 reply->type = HTTP_REPLY_ERRMSG;
1921 reply->status = status;
1922 reply->ctype = NULL;
1923 LIST_INIT(&reply->hdrs);
1924 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001925 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001926
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001927 conf_err = calloc(1, sizeof(*conf_err));
1928 if (!conf_err) {
1929 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001930 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001931 ret = -1;
1932 goto out;
1933 }
1934 conf_err->type = 1;
1935 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001936 conf_err->info.errorfile.reply = reply;
1937
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001938 conf_err->file = strdup(file);
1939 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001940 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001941
Christopher Fauleta66adf42020-11-05 22:43:41 +01001942 /* handle warning message */
1943 if (*errmsg)
1944 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001945 out:
1946 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001947
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001948}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001949
1950/* Parses the "errorfile" proxy keyword */
1951static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001952 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001953 char **errmsg)
1954{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001955 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001956 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001957 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001958 int status;
1959 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001960
1961 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1962 ret = 1;
1963 goto out;
1964 }
1965
1966 if (*(args[1]) == 0 || *(args[2]) == 0) {
1967 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1968 ret = -1;
1969 goto out;
1970 }
1971
1972 status = atol(args[1]);
1973 msg = http_parse_errorfile(status, args[2], errmsg);
1974 if (!msg) {
1975 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1976 ret = -1;
1977 goto out;
1978 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001979
Christopher Faulet5809e102020-05-14 17:31:52 +02001980 reply = calloc(1, sizeof(*reply));
1981 if (!reply) {
1982 memprintf(errmsg, "%s : out of memory.", args[0]);
1983 ret = -1;
1984 goto out;
1985 }
1986 reply->type = HTTP_REPLY_ERRMSG;
1987 reply->status = status;
1988 reply->ctype = NULL;
1989 LIST_INIT(&reply->hdrs);
1990 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001991 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001992
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001993 conf_err = calloc(1, sizeof(*conf_err));
1994 if (!conf_err) {
1995 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001996 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001997 ret = -1;
1998 goto out;
1999 }
2000 conf_err->type = 1;
2001 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02002002 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002003 conf_err->file = strdup(file);
2004 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002005 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002006
Christopher Fauleta66adf42020-11-05 22:43:41 +01002007 /* handle warning message */
2008 if (*errmsg)
2009 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002010 out:
2011 return ret;
2012
2013}
2014
2015/* Parses the "errorfiles" proxy keyword */
2016static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002017 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002018 char **err)
2019{
2020 struct conf_errors *conf_err = NULL;
2021 char *name = NULL;
2022 int rc, ret = 0;
2023
2024 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2025 ret = 1;
2026 goto out;
2027 }
2028
2029 if (!*(args[1])) {
2030 memprintf(err, "%s : expects <name> as argument.", args[0]);
2031 ret = -1;
2032 goto out;
2033 }
2034
2035 name = strdup(args[1]);
2036 conf_err = calloc(1, sizeof(*conf_err));
2037 if (!name || !conf_err) {
2038 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002039 goto error;
2040 }
2041 conf_err->type = 0;
2042
2043 conf_err->info.errorfiles.name = name;
2044 if (!*(args[2])) {
2045 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
2046 conf_err->info.errorfiles.status[rc] = 1;
2047 }
2048 else {
2049 int cur_arg, status;
2050 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
2051 status = atol(args[cur_arg]);
2052
2053 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2054 if (http_err_codes[rc] == status) {
2055 conf_err->info.errorfiles.status[rc] = 2;
2056 break;
2057 }
2058 }
2059 if (rc >= HTTP_ERR_SIZE) {
2060 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01002061 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002062 }
2063 }
2064 }
2065 conf_err->file = strdup(file);
2066 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002067 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002068 out:
2069 return ret;
2070
2071 error:
2072 free(name);
2073 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01002074 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002075 goto out;
2076}
2077
Christopher Faulet3b967c12020-05-15 15:47:44 +02002078/* Parses the "http-error" proxy keyword */
2079static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002080 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02002081 char **errmsg)
2082{
2083 struct conf_errors *conf_err;
2084 struct http_reply *reply = NULL;
2085 int rc, cur_arg, ret = 0;
2086
2087 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2088 ret = 1;
2089 goto out;
2090 }
2091
2092 cur_arg = 1;
2093 curpx->conf.args.ctx = ARGC_HERR;
2094 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2095 if (!reply) {
2096 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2097 goto error;
2098 }
2099 else if (!reply->status) {
2100 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2101 goto error;
2102 }
2103
2104 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2105 if (http_err_codes[rc] == reply->status)
2106 break;
2107 }
2108
2109 if (rc >= HTTP_ERR_SIZE) {
2110 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2111 goto error;
2112 }
2113 if (*args[cur_arg]) {
2114 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2115 goto error;
2116 }
2117
2118 conf_err = calloc(1, sizeof(*conf_err));
2119 if (!conf_err) {
2120 memprintf(errmsg, "%s : out of memory.", args[0]);
2121 goto error;
2122 }
2123 if (reply->type == HTTP_REPLY_ERRFILES) {
2124 int rc = http_get_status_idx(reply->status);
2125
2126 conf_err->type = 2;
2127 conf_err->info.errorfiles.name = reply->body.http_errors;
2128 conf_err->info.errorfiles.status[rc] = 2;
2129 reply->body.http_errors = NULL;
2130 release_http_reply(reply);
2131 }
2132 else {
2133 conf_err->type = 1;
2134 conf_err->info.errorfile.status = reply->status;
2135 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002136 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002137 }
2138 conf_err->file = strdup(file);
2139 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002140 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002141
Christopher Faulet3005d282020-11-13 10:58:01 +01002142 /* handle warning message */
2143 if (*errmsg)
2144 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002145 out:
2146 return ret;
2147
2148 error:
2149 release_http_reply(reply);
2150 ret = -1;
2151 goto out;
2152
2153}
2154
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002155/* Check "errorfiles" proxy keyword */
2156static int proxy_check_errors(struct proxy *px)
2157{
2158 struct conf_errors *conf_err, *conf_err_back;
2159 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002160 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002161
2162 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2163 if (conf_err->type == 1) {
2164 /* errorfile */
2165 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002166 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002167
2168 /* For proxy, to rely on default replies, just don't reference a reply */
2169 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2170 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002171 }
2172 else {
2173 /* errorfiles */
2174 list_for_each_entry(http_errs, &http_errors_list, list) {
2175 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2176 break;
2177 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002178
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002179 /* unknown http-errors section */
2180 if (&http_errs->list == &http_errors_list) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02002181 ha_alert("proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002182 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2183 err |= ERR_ALERT | ERR_FATAL;
2184 free(conf_err->info.errorfiles.name);
2185 goto next;
2186 }
2187
2188 free(conf_err->info.errorfiles.name);
2189 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2190 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002191 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002192 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002193 else if (conf_err->info.errorfiles.status[rc] == 2)
2194 ha_warning("config: proxy '%s' : status '%d' not declared in"
2195 " http-errors section '%s' (at %s:%d).\n",
2196 px->id, http_err_codes[rc], http_errs->id,
2197 conf_err->file, conf_err->line);
2198 }
2199 }
2200 }
2201 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002202 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002203 free(conf_err->file);
2204 free(conf_err);
2205 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002206
2207 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002208 return err;
2209}
2210
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002211static int post_check_errors()
2212{
2213 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002214 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002215 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002216 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002217
2218 node = ebpt_first(&http_error_messages);
2219 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002220 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2221 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002222 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002223 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002224 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2225 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002226 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002227 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002228 (char *)node->key);
2229 err_code |= ERR_WARN;
2230 }
2231 next:
2232 node = ebpt_next(node);
2233 }
2234
2235 return err_code;
2236}
2237
Willy Tarreau016255a2021-02-12 08:40:29 +01002238int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002239{
2240 struct conf_errors *conf_err, *new_conf_err = NULL;
2241 int ret = 0;
2242
2243 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2244 new_conf_err = calloc(1, sizeof(*new_conf_err));
2245 if (!new_conf_err) {
2246 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2247 goto out;
2248 }
2249 new_conf_err->type = conf_err->type;
2250 if (conf_err->type == 1) {
2251 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002252 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002253 }
2254 else {
2255 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2256 if (!new_conf_err->info.errorfiles.name) {
2257 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2258 goto out;
2259 }
2260 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2261 sizeof(conf_err->info.errorfiles.status));
2262 }
2263 new_conf_err->file = strdup(conf_err->file);
2264 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002265 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002266 new_conf_err = NULL;
2267 }
2268 ret = 1;
2269
2270 out:
2271 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002272 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002273}
2274
2275void proxy_release_conf_errors(struct proxy *px)
2276{
2277 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002278
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002279 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2280 if (conf_err->type == 0)
2281 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002282 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002283 free(conf_err->file);
2284 free(conf_err);
2285 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002286}
2287
2288/*
2289 * Parse an <http-errors> section.
2290 * Returns the error code, 0 if OK, or any combination of :
2291 * - ERR_ABORT: must abort ASAP
2292 * - ERR_FATAL: we can continue parsing but not start the service
2293 * - ERR_WARN: a warning has been emitted
2294 * - ERR_ALERT: an alert has been emitted
2295 * Only the two first ones can stop processing, the two others are just
2296 * indicators.
2297 */
2298static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2299{
2300 static struct http_errors *curr_errs = NULL;
2301 int err_code = 0;
2302 const char *err;
2303 char *errmsg = NULL;
2304
2305 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2306 if (!*args[1]) {
2307 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2308 err_code |= ERR_ALERT | ERR_ABORT;
2309 goto out;
2310 }
2311
2312 err = invalid_char(args[1]);
2313 if (err) {
2314 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2315 file, linenum, *err, args[0], args[1]);
2316 err_code |= ERR_ALERT | ERR_FATAL;
2317 }
2318
2319 list_for_each_entry(curr_errs, &http_errors_list, list) {
2320 /* Error if two errors section owns the same name */
2321 if (strcmp(curr_errs->id, args[1]) == 0) {
2322 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2323 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2324 err_code |= ERR_ALERT | ERR_FATAL;
2325 }
2326 }
2327
2328 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2329 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2330 err_code |= ERR_ALERT | ERR_ABORT;
2331 goto out;
2332 }
2333
Willy Tarreau2b718102021-04-21 07:32:39 +02002334 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002335 curr_errs->id = strdup(args[1]);
2336 curr_errs->conf.file = strdup(file);
2337 curr_errs->conf.line = linenum;
2338 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002339 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002340 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002341 struct buffer *msg;
2342 int status, rc;
2343
2344 if (*(args[1]) == 0 || *(args[2]) == 0) {
2345 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2346 file, linenum, args[0]);
2347 err_code |= ERR_ALERT | ERR_FATAL;
2348 goto out;
2349 }
2350
2351 status = atol(args[1]);
2352 msg = http_parse_errorfile(status, args[2], &errmsg);
2353 if (!msg) {
2354 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2355 err_code |= ERR_ALERT | ERR_FATAL;
2356 goto out;
2357 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002358 if (errmsg) {
2359 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2360 err_code |= ERR_WARN;
2361 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002362
2363 reply = calloc(1, sizeof(*reply));
2364 if (!reply) {
2365 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2366 err_code |= ERR_ALERT | ERR_FATAL;
2367 goto out;
2368 }
2369 reply->type = HTTP_REPLY_ERRMSG;
2370 reply->status = status;
2371 reply->ctype = NULL;
2372 LIST_INIT(&reply->hdrs);
2373 reply->body.errmsg = msg;
2374
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002375 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002376 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002377 }
2378 else if (*args[0] != 0) {
2379 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto out;
2382 }
2383
2384out:
2385 free(errmsg);
2386 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002387}
2388
2389static struct cfg_kw_list cfg_kws = {ILH, {
2390 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2391 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2392 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2393 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002394 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002395 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002396 { 0, NULL, NULL },
2397}};
2398
2399INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002400REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002401REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002402
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002403REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2404
Christopher Faulet29f72842019-12-11 15:52:32 +01002405/************************************************************************/
2406/* HTX sample fetches */
2407/************************************************************************/
2408
2409/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2410static int
2411smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2412{
2413 if (!smp->strm)
2414 return 0;
2415
2416 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2417 smp->data.type = SMP_T_BOOL;
2418 return 1;
2419}
2420
2421/* Returns the number of blocks in an HTX message. The channel is chosen
2422 * depending on the sample direction. */
2423static int
2424smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2425{
2426 struct channel *chn;
2427 struct htx *htx;
2428
2429 if (!smp->strm)
2430 return 0;
2431
2432 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002433 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002434 if (!htx)
2435 return 0;
2436
2437 smp->data.u.sint = htx_nbblks(htx);
2438 smp->data.type = SMP_T_SINT;
2439 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2440 return 1;
2441}
2442
2443/* Returns the size of an HTX message. The channel is chosen depending on the
2444 * sample direction. */
2445static int
2446smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2447{
2448 struct channel *chn;
2449 struct htx *htx;
2450
2451 if (!smp->strm)
2452 return 0;
2453
2454 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002455 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002456 if (!htx)
2457 return 0;
2458
2459 smp->data.u.sint = htx->size;
2460 smp->data.type = SMP_T_SINT;
2461 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2462 return 1;
2463}
2464
2465/* Returns the data size of an HTX message. The channel is chosen depending on the
2466 * sample direction. */
2467static int
2468smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2469{
2470 struct channel *chn;
2471 struct htx *htx;
2472
2473 if (!smp->strm)
2474 return 0;
2475
2476 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002477 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002478 if (!htx)
2479 return 0;
2480
2481 smp->data.u.sint = htx->data;
2482 smp->data.type = SMP_T_SINT;
2483 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2484 return 1;
2485}
2486
2487/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2488 * depending on the sample direction. */
2489static int
2490smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2491{
2492 struct channel *chn;
2493 struct htx *htx;
2494
2495 if (!smp->strm)
2496 return 0;
2497
2498 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002499 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002500 if (!htx)
2501 return 0;
2502
2503 smp->data.u.sint = htx_used_space(htx);
2504 smp->data.type = SMP_T_SINT;
2505 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2506 return 1;
2507}
2508
2509/* Returns the free space (size-used) of an HTX message. The channel is chosen
2510 * depending on the sample direction. */
2511static int
2512smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2513{
2514 struct channel *chn;
2515 struct htx *htx;
2516
2517 if (!smp->strm)
2518 return 0;
2519
2520 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002521 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002522 if (!htx)
2523 return 0;
2524
2525 smp->data.u.sint = htx_free_space(htx);
2526 smp->data.type = SMP_T_SINT;
2527 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2528 return 1;
2529}
2530
2531/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2532 * channel is chosen depending on the sample direction. */
2533static int
2534smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2535{
2536 struct channel *chn;
2537 struct htx *htx;
2538
2539 if (!smp->strm)
2540 return 0;
2541
2542 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002543 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002544 if (!htx)
2545 return 0;
2546
2547 smp->data.u.sint = htx_free_data_space(htx);
2548 smp->data.type = SMP_T_SINT;
2549 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2550 return 1;
2551}
2552
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002553/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2554 * channel is chosen depending on the sample direction.
2555 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002556static int
2557smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2558{
2559 struct channel *chn;
2560 struct htx *htx;
2561
2562 if (!smp->strm)
2563 return 0;
2564
2565 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002566 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002567 if (!htx)
2568 return 0;
2569
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002570 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002571 smp->data.type = SMP_T_BOOL;
2572 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2573 return 1;
2574}
2575
2576/* Returns the type of a specific HTX block, if found in the message. Otherwise
2577 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2578 * "head", "tail" or "first". The channel is chosen depending on the sample
2579 * direction. */
2580static int
2581smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2582{
2583 struct channel *chn;
2584 struct htx *htx;
2585 enum htx_blk_type type;
2586 int32_t pos;
2587
2588 if (!smp->strm || !arg_p)
2589 return 0;
2590
2591 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002592 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002593 if (!htx)
2594 return 0;
2595
2596 pos = arg_p[0].data.sint;
2597 if (pos == -1)
2598 type = htx_get_head_type(htx);
2599 else if (pos == -2)
2600 type = htx_get_tail_type(htx);
2601 else if (pos == -3)
2602 type = htx_get_first_type(htx);
2603 else
2604 type = ((pos >= htx->head && pos <= htx->tail)
2605 ? htx_get_blk_type(htx_get_blk(htx, pos))
2606 : HTX_BLK_UNUSED);
2607
2608 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2609 smp->data.type = SMP_T_STR;
2610 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2611 return 1;
2612}
2613
2614/* Returns the size of a specific HTX block, if found in the message. Otherwise
2615 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2616 * "first". The channel is chosen depending on the sample direction. */
2617static int
2618smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2619{
2620 struct channel *chn;
2621 struct htx *htx;
2622 struct htx_blk *blk;
2623 int32_t pos;
2624
2625 if (!smp->strm || !arg_p)
2626 return 0;
2627
2628 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002629 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002630 if (!htx)
2631 return 0;
2632
2633 pos = arg_p[0].data.sint;
2634 if (pos == -1)
2635 blk = htx_get_head_blk(htx);
2636 else if (pos == -2)
2637 blk = htx_get_tail_blk(htx);
2638 else if (pos == -3)
2639 blk = htx_get_first_blk(htx);
2640 else
2641 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2642
2643 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2644 smp->data.type = SMP_T_SINT;
2645 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2646 return 1;
2647}
2648
2649/* Returns the start-line if the selected HTX block exists and is a
2650 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2651 * supported or "head", "tail" or "first". The channel is chosen depending on
2652 * the sample direction. */
2653static int
2654smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2655{
2656 struct buffer *temp;
2657 struct channel *chn;
2658 struct htx *htx;
2659 struct htx_blk *blk;
2660 struct htx_sl *sl;
2661 int32_t pos;
2662
2663 if (!smp->strm || !arg_p)
2664 return 0;
2665
2666 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002667 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002668 if (!htx)
2669 return 0;
2670
2671 pos = arg_p[0].data.sint;
2672 if (pos == -1)
2673 blk = htx_get_head_blk(htx);
2674 else if (pos == -2)
2675 blk = htx_get_tail_blk(htx);
2676 else if (pos == -3)
2677 blk = htx_get_first_blk(htx);
2678 else
2679 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2680
2681 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2682 smp->data.u.str.size = 0;
2683 smp->data.u.str.area = "";
2684 smp->data.u.str.data = 0;
2685 }
2686 else {
2687 sl = htx_get_blk_ptr(htx, blk);
2688
2689 temp = get_trash_chunk();
2690 chunk_istcat(temp, htx_sl_p1(sl));
2691 temp->area[temp->data++] = ' ';
2692 chunk_istcat(temp, htx_sl_p2(sl));
2693 temp->area[temp->data++] = ' ';
2694 chunk_istcat(temp, htx_sl_p3(sl));
2695
2696 smp->data.u.str = *temp;
2697 }
2698
2699 smp->data.type = SMP_T_STR;
2700 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2701 return 1;
2702}
2703
2704/* Returns the header name if the selected HTX block exists and is a header or a
2705 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2706 * supported or "head", "tail" or "first". The channel is chosen depending on
2707 * the sample direction. */
2708static int
2709smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2710{
2711 struct channel *chn;
2712 struct htx *htx;
2713 struct htx_blk *blk;
2714 int32_t pos;
2715
2716 if (!smp->strm || !arg_p)
2717 return 0;
2718
2719 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002720 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002721 if (!htx)
2722 return 0;
2723
2724 pos = arg_p[0].data.sint;
2725 if (pos == -1)
2726 blk = htx_get_head_blk(htx);
2727 else if (pos == -2)
2728 blk = htx_get_tail_blk(htx);
2729 else if (pos == -3)
2730 blk = htx_get_first_blk(htx);
2731 else
2732 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2733
2734 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2735 smp->data.u.str.size = 0;
2736 smp->data.u.str.area = "";
2737 smp->data.u.str.data = 0;
2738 }
2739 else {
2740 struct ist name = htx_get_blk_name(htx, blk);
2741
2742 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2743 }
2744 smp->data.type = SMP_T_STR;
2745 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2746 return 1;
2747}
2748
2749/* Returns the header value if the selected HTX block exists and is a header or
2750 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2751 * supported or "head", "tail" or "first". The channel is chosen depending on
2752 * the sample direction. */
2753static int
2754smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2755{
2756 struct channel *chn;
2757 struct htx *htx;
2758 struct htx_blk *blk;
2759 int32_t pos;
2760
2761 if (!smp->strm || !arg_p)
2762 return 0;
2763
2764 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002765 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002766 if (!htx)
2767 return 0;
2768
2769 pos = arg_p[0].data.sint;
2770 if (pos == -1)
2771 blk = htx_get_head_blk(htx);
2772 else if (pos == -2)
2773 blk = htx_get_tail_blk(htx);
2774 else if (pos == -3)
2775 blk = htx_get_first_blk(htx);
2776 else
2777 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2778
2779 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2780 smp->data.u.str.size = 0;
2781 smp->data.u.str.area = "";
2782 smp->data.u.str.data = 0;
2783 }
2784 else {
2785 struct ist val = htx_get_blk_value(htx, blk);
2786
2787 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2788 }
2789 smp->data.type = SMP_T_STR;
2790 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2791 return 1;
2792}
2793
2794/* Returns the value if the selected HTX block exists and is a data
2795 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2796 * or "head", "tail" or "first". The channel is chosen depending on the sample
2797 * direction. */
2798static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002799smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002800{
2801 struct channel *chn;
2802 struct htx *htx;
2803 struct htx_blk *blk;
2804 int32_t pos;
2805
2806 if (!smp->strm || !arg_p)
2807 return 0;
2808
2809 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002810 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002811 if (!htx)
2812 return 0;
2813
2814 pos = arg_p[0].data.sint;
2815 if (pos == -1)
2816 blk = htx_get_head_blk(htx);
2817 else if (pos == -2)
2818 blk = htx_get_tail_blk(htx);
2819 else if (pos == -3)
2820 blk = htx_get_first_blk(htx);
2821 else
2822 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2823
2824 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2825 smp->data.u.str.size = 0;
2826 smp->data.u.str.area = "";
2827 smp->data.u.str.data = 0;
2828 }
2829 else {
2830 struct ist val = htx_get_blk_value(htx, blk);
2831
2832 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2833 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002834 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002835 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2836 return 1;
2837}
2838
2839/* This function is used to validate the arguments passed to any "htx_blk" fetch
2840 * keywords. An argument is expected by these keywords. It must be a positive
2841 * integer or on of the following strings: "head", "tail" or "first". It returns
2842 * 0 on error, and a non-zero value if OK.
2843 */
2844int val_blk_arg(struct arg *arg, char **err_msg)
2845{
2846 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2847 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2848 return 0;
2849 }
2850 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002851 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002852 arg[0].type = ARGT_SINT;
2853 arg[0].data.sint = -1;
2854 }
2855 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002856 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002857 arg[0].type = ARGT_SINT;
2858 arg[0].data.sint = -2;
2859 }
2860 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002861 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002862 arg[0].type = ARGT_SINT;
2863 arg[0].data.sint = -3;
2864 }
2865 else {
2866 int pos;
2867
2868 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002869 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002870 memprintf(err_msg, "invalid block position");
2871 return 0;
2872 }
2873 }
2874
2875 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2876 if (pos < 0) {
2877 memprintf(err_msg, "block position must not be negative");
2878 return 0;
2879 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002880 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002881 arg[0].type = ARGT_SINT;
2882 arg[0].data.sint = pos;
2883 }
2884
2885 return 1;
2886}
2887
2888
2889/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002890 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002891 */
2892static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002893 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002894
Christopher Faulet01f44452020-01-08 14:23:40 +01002895 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2896 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2897 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2898 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2899 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2900 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2901 { "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 +01002902
Christopher Faulet01f44452020-01-08 14:23:40 +01002903 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2904 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2905 { "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},
2906 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2907 { "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 +01002908 { "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 +01002909
2910 { /* END */ },
2911}};
2912
2913INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);