blob: bb9474c8d957871de5faa8dd06ec0621977b105e [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +020014#include <ctype.h>
Christopher Faulet5031ef52020-01-15 11:22:07 +010015#include <fcntl.h>
16#include <unistd.h>
17
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020022#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/log.h>
28#include <haproxy/regex.h>
29#include <haproxy/sample.h>
Willy Tarreau4cbf62d2021-05-08 13:01:23 +020030#include <haproxy/tools.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020031
Christopher Faulet47596d32018-10-22 09:17:28 +020032
Christopher Fauletf7346382019-07-17 22:02:08 +020033struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020034struct http_reply http_err_replies[HTTP_ERR_SIZE];
35
Christopher Faulet58857752020-01-15 15:19:50 +010036struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010037struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020038struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010039
Christopher Faulet76edc0f2020-01-13 15:52:01 +010040/* The declaration of an errorfiles/errorfile directives. Used during config
41 * parsing only. */
42struct conf_errors {
43 char type; /* directive type (0: errorfiles, 1: errorfile) */
44 union {
45 struct {
46 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020047 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010048 } errorfile; /* describe an "errorfile" directive */
49 struct {
50 char *name; /* the http-errors section name */
51 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
52 } errorfiles; /* describe an "errorfiles" directive */
53 } info;
54
55 char *file; /* file where the directive appears */
56 int line; /* line where the directive appears */
57
58 struct list list; /* next conf_errors */
59};
60
Christopher Faulet297fbb42019-05-13 14:41:27 +020061/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020062 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020063 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020064 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020065struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020066{
Christopher Faulet297fbb42019-05-13 14:41:27 +020067 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010068
Christopher Faulet29f17582019-05-23 11:03:26 +020069 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020070 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL))
Christopher Faulet297fbb42019-05-13 14:41:27 +020071 return NULL;
72 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100162 if (type == HTX_BLK_EOH)
Christopher Faulet573fe732018-11-28 16:55:12 +0100163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000204 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
391 if (!http_update_host(htx, sl, uri))
392 goto fail;
393
394 return 1;
395 fail:
396 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200397}
398
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200399/* Replace the request path in the HTX message <htx> by <path>. The host part is
400 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
401 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
402 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200403 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200404int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200405{
406 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200407 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100408 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200409 size_t plen = 0;
410
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100411 if (!sl)
412 return 0;
413
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100414 uri = htx_sl_req_uri(sl);
415 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100416 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100417 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200418 if (with_qs)
419 plen = p.len;
420 else {
421 while (plen < p.len && *(p.ptr + plen) != '?')
422 plen++;
423 }
Christopher Faulete010c802018-10-24 10:36:45 +0200424
425 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100426 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
427 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200428
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100429 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
430 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
431
432 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200433 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
434 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100435 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200436
437 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100438 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200439}
440
441/* Replace the request query-string in the HTX message <htx> by <query>. The
442 * host part and the path are preserved. It returns 1 on success, otherwise
443 * 0.
444 */
445int http_replace_req_query(struct htx *htx, const struct ist query)
446{
447 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200448 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100449 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200450 int offset = 1;
451
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100452 if (!sl)
453 return 0;
454
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100455 uri = htx_sl_req_uri(sl);
456 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200457 while (q.len > 0 && *(q.ptr) != '?') {
458 q.ptr++;
459 q.len--;
460 }
461
462 /* skip the question mark or indicate that we must insert it
463 * (but only if the format string is not empty then).
464 */
465 if (q.len) {
466 q.ptr++;
467 q.len--;
468 }
469 else if (query.len > 1)
470 offset = 0;
471
472 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100473 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
474 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200475
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100476 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
477 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200478
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
480 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
481 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200482
483 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100484 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200485}
486
487/* Replace the response status in the HTX message <htx> by <status>. It returns
488 * 1 on success, otherwise 0.
489*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200490int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200491{
492 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200493 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200494 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200495
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100496 if (!sl)
497 return 0;
498
Christopher Faulete010c802018-10-24 10:36:45 +0200499 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
501 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200502 r = reason;
503 if (!isttest(r)) {
504 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
505 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
506 }
Christopher Faulete010c802018-10-24 10:36:45 +0200507
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100508 /* create the new start line */
509 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200510 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200511}
512
513/* Replace the response reason in the HTX message <htx> by <reason>. It returns
514 * 1 on success, otherwise 0.
515*/
516int http_replace_res_reason(struct htx *htx, const struct ist reason)
517{
518 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200519 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100520 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200521
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100522 if (!sl)
523 return 0;
524
Christopher Faulete010c802018-10-24 10:36:45 +0200525 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100526 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
527 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200528
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100529 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
530 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200531
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100532 /* create the new start line */
533 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200534}
535
Christopher Faulet47596d32018-10-22 09:17:28 +0200536/* Replaces a part of a header value referenced in the context <ctx> by
537 * <data>. It returns 1 on success, otherwise it returns 0. The context is
538 * updated if necessary.
539 */
540int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
541{
542 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200543 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200544 char *start;
545 struct ist v;
546 uint32_t len, off;
547
548 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200549 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200550
551 v = htx_get_blk_value(htx, blk);
552 start = ctx->value.ptr - ctx->lws_before;
553 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
554 off = start - v.ptr;
555
556 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
557 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200558 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200559
560 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200561
562 sl = http_get_stline(htx);
563 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
564 struct ist n = htx_get_blk_name(htx, blk);
565
566 if (isteq(n, ist("host"))) {
567 if (!http_update_authority(htx, sl, v))
568 goto fail;
569 ctx->blk = NULL;
570 http_find_header(htx, ist("host"), ctx, 1);
571 blk = ctx->blk;
572 v = htx_get_blk_value(htx, blk);
573 }
574 }
575
Christopher Faulet47596d32018-10-22 09:17:28 +0200576 ctx->blk = blk;
577 ctx->value.ptr = v.ptr + off;
578 ctx->value.len = data.len;
579 ctx->lws_before = ctx->lws_after = 0;
580
581 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200582 fail:
583 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200584}
585
586/* Fully replaces a header referenced in the context <ctx> by the name <name>
587 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
588 * context is updated if necessary.
589 */
590int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
591 const struct ist name, const struct ist value)
592{
593 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200594 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200595
596 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200597 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200598
599 blk = htx_replace_header(htx, blk, name, value);
600 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200601 goto fail;
602
603 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100604 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200605 if (!http_update_authority(htx, sl, value))
606 goto fail;
607 ctx->blk = NULL;
608 http_find_header(htx, ist("host"), ctx, 1);
609 blk = ctx->blk;
610 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200611
612 ctx->blk = blk;
613 ctx->value = ist(NULL);
614 ctx->lws_before = ctx->lws_after = 0;
615
616 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200617 fail:
618 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200619}
620
621/* Remove one value of a header. This only works on a <ctx> returned by
622 * http_find_header function. The value is removed, as well as surrounding commas
623 * if any. If the removed value was alone, the whole header is removed. The
624 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
625 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
626 * form that can be handled by http_find_header() to find next occurrence.
627 */
628int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
629{
630 struct htx_blk *blk = ctx->blk;
631 char *start;
632 struct ist v;
633 uint32_t len;
634
635 if (!blk)
636 return 0;
637
638 start = ctx->value.ptr - ctx->lws_before;
639 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
640
641 v = htx_get_blk_value(htx, blk);
642 if (len == v.len) {
643 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200644 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200645 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100646 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200647 ctx->lws_before = ctx->lws_after = 0;
648 }
649 else {
650 ctx->blk = htx_get_blk(htx, htx->tail);
651 ctx->value = htx_get_blk_value(htx, ctx->blk);
652 ctx->lws_before = ctx->lws_after = 0;
653 }
654 return 1;
655 }
656
657 /* This was not the only value of this header. We have to remove the
658 * part pointed by ctx->value. If it is the last entry of the list, we
659 * remove the last separator.
660 */
661 if (start == v.ptr) {
662 /* It's the first header part but not the only one. So remove
663 * the comma after it. */
664 len++;
665 }
666 else {
667 /* There is at least one header part before the removed one. So
668 * remove the comma between them. */
669 start--;
670 len++;
671 }
672 /* Update the block content and its len */
673 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200674 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200675
676 /* Finally update the ctx */
677 ctx->value.ptr = start;
678 ctx->value.len = 0;
679 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;
694
695 uri = htx_sl_req_uri(sl);
696 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100697 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200698 return 0;
699
Christopher Faulet34b18e42020-02-18 11:02:21 +0100700 /* Don't update the uri if there is no change */
701 if (isteq(host, authority))
702 return 1;
703
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200704 /* Start by copying old method and version */
705 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
706 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
707
708 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
709 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
710
711 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
712 chunk_memcat(temp, host.ptr, host.len);
713 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
714 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
715
716 return http_replace_stline(htx, meth, uri, vsn);
717
718}
719
720/* Update the header host by extracting the authority of the uri <uri>. flags of
721 * the start-line are also updated accordingly. For orgin-form and asterisk-form
722 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
723 * removed from the flags of the start-line. Otherwise, this flag is set and the
724 * authority is used to set the value of the header host. This function returns
725 * 0 on failure and 1 on success.
726*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200727int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200728{
729 struct ist authority;
730 struct http_hdr_ctx ctx;
731
732 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
733 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
734 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
735 }
736 else {
737 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
738 if (sl->info.req.meth != HTTP_METH_CONNECT) {
739 // absolute-form (RFC7320 #5.3.2)
740 sl->flags |= HTX_SL_F_HAS_SCHM;
741 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
742 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
743
744 authority = http_get_authority(uri, 1);
745 if (!authority.len)
746 goto fail;
747 }
748 else {
749 // authority-form (RFC7320 #5.3.3)
750 authority = uri;
751 }
752
753 /* Replace header host value */
754 ctx.blk = NULL;
755 while (http_find_header(htx, ist("host"), &ctx, 1)) {
756 if (!http_replace_header_value(htx, &ctx, authority))
757 goto fail;
758 }
759
760 }
761 return 1;
762 fail:
763 return 0;
764}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200765
766/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
767 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
768 * performed over the whole headers. Otherwise it must contain a valid header
769 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
770 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
771 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
772 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
773 * -1. The value fetch stops at commas, so this function is suited for use with
774 * list headers.
775 * The return value is 0 if nothing was found, or non-zero otherwise.
776 */
777unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
778 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
779{
780 struct http_hdr_ctx local_ctx;
781 struct ist val_hist[MAX_HDR_HISTORY];
782 unsigned int hist_idx;
783 int found;
784
785 if (!ctx) {
786 local_ctx.blk = NULL;
787 ctx = &local_ctx;
788 }
789
790 if (occ >= 0) {
791 /* search from the beginning */
792 while (http_find_header(htx, hdr, ctx, 0)) {
793 occ--;
794 if (occ <= 0) {
795 *vptr = ctx->value.ptr;
796 *vlen = ctx->value.len;
797 return 1;
798 }
799 }
800 return 0;
801 }
802
803 /* negative occurrence, we scan all the list then walk back */
804 if (-occ > MAX_HDR_HISTORY)
805 return 0;
806
807 found = hist_idx = 0;
808 while (http_find_header(htx, hdr, ctx, 0)) {
809 val_hist[hist_idx] = ctx->value;
810 if (++hist_idx >= MAX_HDR_HISTORY)
811 hist_idx = 0;
812 found++;
813 }
814 if (-occ > found)
815 return 0;
816
817 /* OK now we have the last occurrence in [hist_idx-1], and we need to
818 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
819 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
820 * to remain in the 0..9 range.
821 */
822 hist_idx += occ + MAX_HDR_HISTORY;
823 if (hist_idx >= MAX_HDR_HISTORY)
824 hist_idx -= MAX_HDR_HISTORY;
825 *vptr = val_hist[hist_idx].ptr;
826 *vlen = val_hist[hist_idx].len;
827 return 1;
828}
829
830/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
831 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
832 * performed over the whole headers. Otherwise it must contain a valid header
833 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
834 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
835 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
836 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
837 * -1. This function differs from http_get_hdr() in that it only returns full
838 * line header values and does not stop at commas.
839 * The return value is 0 if nothing was found, or non-zero otherwise.
840 */
841unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
842 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
843{
844 struct http_hdr_ctx local_ctx;
845 struct ist val_hist[MAX_HDR_HISTORY];
846 unsigned int hist_idx;
847 int found;
848
849 if (!ctx) {
850 local_ctx.blk = NULL;
851 ctx = &local_ctx;
852 }
853
854 if (occ >= 0) {
855 /* search from the beginning */
856 while (http_find_header(htx, hdr, ctx, 1)) {
857 occ--;
858 if (occ <= 0) {
859 *vptr = ctx->value.ptr;
860 *vlen = ctx->value.len;
861 return 1;
862 }
863 }
864 return 0;
865 }
866
867 /* negative occurrence, we scan all the list then walk back */
868 if (-occ > MAX_HDR_HISTORY)
869 return 0;
870
871 found = hist_idx = 0;
872 while (http_find_header(htx, hdr, ctx, 1)) {
873 val_hist[hist_idx] = ctx->value;
874 if (++hist_idx >= MAX_HDR_HISTORY)
875 hist_idx = 0;
876 found++;
877 }
878 if (-occ > found)
879 return 0;
880
881 /* OK now we have the last occurrence in [hist_idx-1], and we need to
882 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
883 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
884 * to remain in the 0..9 range.
885 */
886 hist_idx += occ + MAX_HDR_HISTORY;
887 if (hist_idx >= MAX_HDR_HISTORY)
888 hist_idx -= MAX_HDR_HISTORY;
889 *vptr = val_hist[hist_idx].ptr;
890 *vlen = val_hist[hist_idx].len;
891 return 1;
892}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100893
Christopher Fauleta66adf42020-11-05 22:43:41 +0100894int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100895{
896 struct htx *htx;
897 struct htx_sl *sl;
898 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200899 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100900 union h1_sl h1sl;
901 unsigned int flags = HTX_SL_F_IS_RESP;
902 int ret = 0;
903
Christopher Faulet90cc4812019-07-22 16:49:30 +0200904 b_reset(buf);
905 if (!raw.len) {
906 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100907 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200908 return 1;
909 }
910
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100911 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200912 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100913 if (!buf->area)
914 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100915
916 h1m_init_res(&h1m);
917 h1m.flags |= H1_MF_NO_PHDR;
918 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
919 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100920 if (ret <= 0) {
921 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100922 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100923 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100924
Christopher Fauleta66adf42020-11-05 22:43:41 +0100925 if (unlikely(h1sl.st.v.len != 8)) {
926 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100927 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100928 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100929 if ((*(h1sl.st.v.ptr + 5) > '1') ||
930 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
931 h1m.flags |= H1_MF_VER_11;
932
Christopher Fauleta66adf42020-11-05 22:43:41 +0100933 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
934 memprintf(errmsg, "invalid http status code for an error message (%u)",
935 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200936 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100937 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200938
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200939 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
940 /* Responses known to have no body. */
941 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
942 h1m.flags |= H1_MF_XFER_LEN;
943 h1m.curr_len = h1m.body_len = 0;
944 }
945 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
946 h1m.flags |= H1_MF_XFER_LEN;
947
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100948 if (h1m.flags & H1_MF_VER_11)
949 flags |= HTX_SL_F_VER_11;
950 if (h1m.flags & H1_MF_XFER_ENC)
951 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200952 if (h1m.flags & H1_MF_XFER_LEN) {
953 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100954 if (h1m.flags & H1_MF_CHNK) {
955 memprintf(errmsg, "chunk-encoded payload not supported");
956 goto error;
957 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200958 else if (h1m.flags & H1_MF_CLEN) {
959 flags |= HTX_SL_F_CLEN;
960 if (h1m.body_len == 0)
961 flags |= HTX_SL_F_BODYLESS;
962 }
963 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200964 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100965 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200966
Christopher Fauleta66adf42020-11-05 22:43:41 +0100967 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
968 memprintf(errmsg, "message payload not expected");
969 goto error;
970 }
971 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
972 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100973 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100974 goto error;
975 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100976
977 htx = htx_from_buf(buf);
978 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 +0100979 if (!sl || !htx_add_all_headers(htx, hdrs)) {
980 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100981 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100982 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100983 sl->info.res.status = h1sl.st.status;
984
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200985 while (raw.len > ret) {
986 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100987 if (!sent) {
988 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100989 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100990 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200991 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100992 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200993
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100994 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200995
Christopher Faulet90cc4812019-07-22 16:49:30 +0200996 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100997
998error:
999 if (buf->size)
1000 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001001 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001002}
1003
Christopher Faulet18630642020-05-12 18:57:28 +02001004void release_http_reply(struct http_reply *http_reply)
1005{
1006 struct logformat_node *lf, *lfb;
1007 struct http_reply_hdr *hdr, *hdrb;
1008
1009 if (!http_reply)
1010 return;
1011
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001012 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001013 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001014 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001015 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001016 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001017 release_sample_expr(lf->expr);
1018 free(lf->arg);
1019 free(lf);
1020 }
1021 istfree(&hdr->name);
1022 free(hdr);
1023 }
1024
1025 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001026 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001027 }
1028 else if (http_reply->type == HTTP_REPLY_RAW)
1029 chunk_destroy(&http_reply->body.obj);
1030 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1031 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001032 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001033 release_sample_expr(lf->expr);
1034 free(lf->arg);
1035 free(lf);
1036 }
1037 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001038 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001039}
1040
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001041static int http_htx_init(void)
1042{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001043 struct buffer chk;
1044 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001045 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001046 int rc;
1047 int err_code = 0;
1048
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001049 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1050 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001051 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001052 err_code |= ERR_ALERT | ERR_FATAL;
1053 continue;
1054 }
1055
1056 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Fauleta66adf42020-11-05 22:43:41 +01001057 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1058 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1059 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001060 err_code |= ERR_ALERT | ERR_FATAL;
1061 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001062 else if (errmsg) {
1063 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1064 err_code |= ERR_WARN;
1065 }
1066
1067 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001068 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001069
Christopher Fauletf7346382019-07-17 22:02:08 +02001070 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001071 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1072 http_err_replies[rc].status = http_err_codes[rc];
1073 http_err_replies[rc].ctype = NULL;
1074 LIST_INIT(&http_err_replies[rc].hdrs);
1075 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001076 }
1077end:
1078 return err_code;
1079}
1080
Christopher Faulet58857752020-01-15 15:19:50 +01001081static void http_htx_deinit(void)
1082{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001083 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001084 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001085 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001086 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001087 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001088
1089 node = ebpt_first(&http_error_messages);
1090 while (node) {
1091 next = ebpt_next(node);
1092 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001093 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1094 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001095 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001096 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001097 node = next;
1098 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001099
1100 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1101 free(http_errs->conf.file);
1102 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001103 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1104 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001105 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001106 free(http_errs);
1107 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001108
1109 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001110 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001111 release_http_reply(http_rep);
1112 }
Christopher Faulet58857752020-01-15 15:19:50 +01001113}
1114
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001115REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001116REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001117
Christopher Faulet58857752020-01-15 15:19:50 +01001118/* Reads content of the error file <file> and convert it into an HTX message. On
1119 * success, the HTX message is returned. On error, NULL is returned and an error
1120 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001121 */
Christopher Faulet58857752020-01-15 15:19:50 +01001122struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001123{
Christopher Faulet58857752020-01-15 15:19:50 +01001124 struct buffer *buf = NULL;
1125 struct buffer chk;
1126 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001127 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001128 struct stat stat;
1129 char *err = NULL;
1130 int errnum, errlen;
1131 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001132
1133 /* already loaded */
1134 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1135 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001136 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1137 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001138 goto out;
1139 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001140
Christopher Faulet58857752020-01-15 15:19:50 +01001141 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001142 fd = open(file, O_RDONLY);
1143 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1144 memprintf(errmsg, "error opening file '%s'.", file);
1145 goto out;
1146 }
1147
1148 if (stat.st_size <= global.tune.bufsize)
1149 errlen = stat.st_size;
1150 else {
1151 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1152 file, global.tune.bufsize);
1153 errlen = global.tune.bufsize;
1154 }
1155
1156 err = malloc(errlen);
1157 if (!err) {
1158 memprintf(errmsg, "out of memory.");
1159 goto out;
1160 }
1161
1162 errnum = read(fd, err, errlen);
1163 if (errnum != errlen) {
1164 memprintf(errmsg, "error reading file '%s'.", file);
1165 goto out;
1166 }
1167
Christopher Faulet58857752020-01-15 15:19:50 +01001168 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001169 http_errmsg = calloc(1, sizeof(*http_errmsg));
1170 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001171 memprintf(errmsg, "out of memory.");
1172 goto out;
1173 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001174 http_errmsg->node.key = strdup(file);
1175 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001176 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001177 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001178 goto out;
1179 }
1180
1181 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001182 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1183 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001184 free(http_errmsg->node.key);
1185 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001186 goto out;
1187 }
1188
Christopher Faulet58857752020-01-15 15:19:50 +01001189 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001190 http_errmsg->msg = chk;
1191 ebis_insert(&http_error_messages, &http_errmsg->node);
1192 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001193
Christopher Faulet5031ef52020-01-15 11:22:07 +01001194 out:
1195 if (fd >= 0)
1196 close(fd);
1197 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001198 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001199}
1200
Ilya Shipitsind4259502020-04-08 01:07:56 +05001201/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001202 * message is returned. On error, NULL is returned and an error message is
1203 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001204 */
Christopher Faulet58857752020-01-15 15:19:50 +01001205struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001206{
Christopher Faulet58857752020-01-15 15:19:50 +01001207 struct buffer *buf = NULL;
1208 struct buffer chk;
1209 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001210 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001211
1212 /* already loaded */
1213 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1214 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001215 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1216 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001217 goto out;
1218 }
1219 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001220 http_errmsg = calloc(1, sizeof(*http_errmsg));
1221 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001222 memprintf(errmsg, "out of memory.");
1223 goto out;
1224 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001225 http_errmsg->node.key = strdup(key);
1226 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001227 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001228 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001229 goto out;
1230 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001231
1232 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001233 if (!http_str_to_htx(&chk, msg, errmsg)) {
1234 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001235 free(http_errmsg->node.key);
1236 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001237 goto out;
1238 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001239
Christopher Faulet58857752020-01-15 15:19:50 +01001240 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001241 http_errmsg->msg = chk;
1242 ebis_insert(&http_error_messages, &http_errmsg->node);
1243 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001244 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001245 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001246}
1247
Christopher Faulet5031ef52020-01-15 11:22:07 +01001248/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001249 * <status>. It returns NULL if there is any error, otherwise it return the
1250 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001251 */
Christopher Faulet58857752020-01-15 15:19:50 +01001252struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001253{
Christopher Faulet58857752020-01-15 15:19:50 +01001254 struct buffer *buf = NULL;
1255 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001256
1257 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1258 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001259 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001260 break;
1261 }
1262 }
1263
1264 if (rc >= HTTP_ERR_SIZE)
1265 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001266 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001267}
1268
1269/* This function creates HTX error message corresponding to a redirect message
1270 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001271 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1272 * returns NULL if there is any error, otherwise it return the corresponding HTX
1273 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001274 */
Christopher Faulet58857752020-01-15 15:19:50 +01001275struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001276{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001277 static const char *HTTP_302 =
1278 "HTTP/1.1 302 Found\r\n"
1279 "Cache-Control: no-cache\r\n"
1280 "Content-length: 0\r\n"
1281 "Location: "; /* not terminated since it will be concatenated with the URL */
1282 static const char *HTTP_303 =
1283 "HTTP/1.1 303 See Other\r\n"
1284 "Cache-Control: no-cache\r\n"
1285 "Content-length: 0\r\n"
1286 "Location: "; /* not terminated since it will be concatenated with the URL */
1287
Christopher Faulet58857752020-01-15 15:19:50 +01001288 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001289 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001290 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001291 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001292
1293 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1294 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001295 /* Create the error key */
1296 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1297 memprintf(errmsg, "out of memory.");
1298 goto out;
1299 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001300 /* Create the error message */
1301 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1302 errlen = strlen(msg) + strlen(url) + 5;
1303 err = malloc(errlen);
1304 if (!err) {
1305 memprintf(errmsg, "out of memory.");
1306 goto out;
1307 }
1308 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1309
1310 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001311 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001312 break;
1313 }
1314 }
1315
1316 if (rc >= HTTP_ERR_SIZE)
1317 memprintf(errmsg, "status code '%d' not handled.", status);
1318out:
Christopher Faulet58857752020-01-15 15:19:50 +01001319 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001320 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001321 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001322}
1323
Christopher Faulet7eea2412020-05-13 15:02:59 +02001324/* Check an "http reply" and, for replies referencing an http-errors section,
1325 * try to find the right section and the right error message in this section. If
1326 * found, the reply is updated. If the http-errors section exists but the error
1327 * message is not found, no error message is set to fallback on the default
1328 * ones. Otherwise (unknown section) an error is returned.
1329 *
1330 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1331 * filled.
1332 */
1333int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1334{
1335 struct http_errors *http_errs;
1336 int ret = 1;
1337
1338 if (reply->type != HTTP_REPLY_ERRFILES)
1339 goto end;
1340
1341 list_for_each_entry(http_errs, &http_errors_list, list) {
1342 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001343 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001344 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001345 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1346 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001347 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1348 "not declared in http-errors section '%s'.\n",
1349 px->id, reply->status, http_errs->id);
1350 break;
1351 }
1352 }
1353
1354 if (&http_errs->list == &http_errors_list) {
1355 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1356 reply->body.http_errors);
1357 ret = 0;
1358 }
1359
1360 end:
1361 return ret;
1362}
1363
Christopher Faulet47e791e2020-05-13 14:36:55 +02001364/* Parse an "http reply". It returns the reply on success or NULL on error. This
1365 * function creates one of the following http replies :
1366 *
1367 * - HTTP_REPLY_EMPTY : dummy response, no payload
1368 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1369 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1370 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1371 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1372 *
1373 * The content-type must be defined for non-empty payload. It is ignored for
1374 * error messages (implicit or explicit). When an http-errors section is
1375 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1376 * during the configuration validity check or dynamically. It is the caller
1377 * responsibility to choose. If no status code is configured, <default_status>
1378 * is set.
1379 */
1380struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1381 int default_status, char **errmsg)
1382{
1383 struct logformat_node *lf, *lfb;
1384 struct http_reply *reply = NULL;
1385 struct http_reply_hdr *hdr, *hdrb;
1386 struct stat stat;
1387 const char *act_arg = NULL;
1388 char *obj = NULL;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001389 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001390
1391
1392 reply = calloc(1, sizeof(*reply));
1393 if (!reply) {
1394 memprintf(errmsg, "out of memory");
1395 goto error;
1396 }
1397 LIST_INIT(&reply->hdrs);
1398 reply->type = HTTP_REPLY_EMPTY;
1399 reply->status = default_status;
1400
Christopher Faulet3b967c12020-05-15 15:47:44 +02001401 if (px->conf.args.ctx == ARGC_HERR)
1402 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001403 else {
1404 if (px->cap & PR_CAP_FE)
1405 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1406 if (px->cap & PR_CAP_BE)
Willy Tarreauad745102021-10-16 14:41:09 +02001407 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001408 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001409
1410 cur_arg = *orig_arg;
1411 while (*args[cur_arg]) {
1412 if (strcmp(args[cur_arg], "status") == 0) {
1413 cur_arg++;
1414 if (!*args[cur_arg]) {
1415 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1416 goto error;
1417 }
1418 reply->status = atol(args[cur_arg]);
1419 if (reply->status < 200 || reply->status > 599) {
1420 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1421 goto error;
1422 }
1423 cur_arg++;
1424 }
1425 else if (strcmp(args[cur_arg], "content-type") == 0) {
1426 cur_arg++;
1427 if (!*args[cur_arg]) {
1428 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1429 goto error;
1430 }
1431 free(reply->ctype);
1432 reply->ctype = strdup(args[cur_arg]);
1433 cur_arg++;
1434 }
1435 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1436 if (reply->type != HTTP_REPLY_EMPTY) {
1437 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1438 goto error;
1439 }
1440 act_arg = args[cur_arg];
1441 cur_arg++;
1442 if (!*args[cur_arg]) {
1443 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1444 goto error;
1445 }
1446 reply->body.http_errors = strdup(args[cur_arg]);
1447 if (!reply->body.http_errors) {
1448 memprintf(errmsg, "out of memory");
1449 goto error;
1450 }
1451 reply->type = HTTP_REPLY_ERRFILES;
1452 cur_arg++;
1453 }
1454 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1455 if (reply->type != HTTP_REPLY_EMPTY) {
1456 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1457 goto error;
1458 }
1459 act_arg = args[cur_arg];
1460 reply->type = HTTP_REPLY_ERRMSG;
1461 cur_arg++;
1462 }
1463 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1464 if (reply->type != HTTP_REPLY_EMPTY) {
1465 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1466 goto error;
1467 }
1468 act_arg = args[cur_arg];
1469 cur_arg++;
1470 if (!*args[cur_arg]) {
1471 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1472 goto error;
1473 }
1474 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1475 if (!reply->body.errmsg) {
1476 goto error;
1477 }
1478 reply->type = HTTP_REPLY_ERRMSG;
1479 cur_arg++;
1480 }
1481 else if (strcmp(args[cur_arg], "file") == 0) {
1482 if (reply->type != HTTP_REPLY_EMPTY) {
1483 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1484 goto error;
1485 }
1486 act_arg = args[cur_arg];
1487 cur_arg++;
1488 if (!*args[cur_arg]) {
1489 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1490 goto error;
1491 }
1492 fd = open(args[cur_arg], O_RDONLY);
1493 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1494 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1495 goto error;
1496 }
1497 if (stat.st_size > global.tune.bufsize) {
1498 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1499 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1500 goto error;
1501 }
1502 objlen = stat.st_size;
1503 obj = malloc(objlen);
1504 if (!obj || read(fd, obj, objlen) != objlen) {
1505 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1506 goto error;
1507 }
1508 close(fd);
1509 fd = -1;
1510 reply->type = HTTP_REPLY_RAW;
1511 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1512 obj = NULL;
1513 cur_arg++;
1514 }
1515 else if (strcmp(args[cur_arg], "string") == 0) {
1516 if (reply->type != HTTP_REPLY_EMPTY) {
1517 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1518 goto error;
1519 }
1520 act_arg = args[cur_arg];
1521 cur_arg++;
1522 if (!*args[cur_arg]) {
1523 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1524 goto error;
1525 }
1526 obj = strdup(args[cur_arg]);
1527 objlen = strlen(args[cur_arg]);
1528 if (!obj) {
1529 memprintf(errmsg, "out of memory");
1530 goto error;
1531 }
1532 reply->type = HTTP_REPLY_RAW;
1533 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1534 obj = NULL;
1535 cur_arg++;
1536 }
1537 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1538 if (reply->type != HTTP_REPLY_EMPTY) {
1539 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1540 goto error;
1541 }
1542 act_arg = args[cur_arg];
1543 cur_arg++;
1544 if (!*args[cur_arg]) {
1545 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1546 goto error;
1547 }
1548 fd = open(args[cur_arg], O_RDONLY);
1549 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1550 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1551 goto error;
1552 }
1553 if (stat.st_size > global.tune.bufsize) {
1554 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1555 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1556 goto error;
1557 }
1558 objlen = stat.st_size;
1559 obj = malloc(objlen + 1);
1560 if (!obj || read(fd, obj, objlen) != objlen) {
1561 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1562 goto error;
1563 }
1564 close(fd);
1565 fd = -1;
1566 obj[objlen] = '\0';
1567 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001568 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001569 cur_arg++;
1570 }
1571 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1572 if (reply->type != HTTP_REPLY_EMPTY) {
1573 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1574 goto error;
1575 }
1576 act_arg = args[cur_arg];
1577 cur_arg++;
1578 if (!*args[cur_arg]) {
1579 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1580 goto error;
1581 }
1582 obj = strdup(args[cur_arg]);
1583 objlen = strlen(args[cur_arg]);
1584 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001585 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001586 cur_arg++;
1587 }
1588 else if (strcmp(args[cur_arg], "hdr") == 0) {
1589 cur_arg++;
1590 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1591 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1592 goto error;
1593 }
1594 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1595 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1596 strcasecmp(args[cur_arg], "content-type") == 0) {
1597 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1598 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1599 cur_arg += 2;
1600 continue;
1601 }
1602 hdr = calloc(1, sizeof(*hdr));
1603 if (!hdr) {
1604 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1605 goto error;
1606 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001607 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001608 LIST_INIT(&hdr->value);
1609 hdr->name = ist(strdup(args[cur_arg]));
1610 if (!isttest(hdr->name)) {
1611 memprintf(errmsg, "out of memory");
1612 goto error;
1613 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001614 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1615 goto error;
1616
1617 free(px->conf.lfs_file);
1618 px->conf.lfs_file = strdup(px->conf.args.file);
1619 px->conf.lfs_line = px->conf.args.line;
1620 cur_arg += 2;
1621 }
1622 else
1623 break;
1624 }
1625
1626 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1627 if (reply->ctype) {
1628 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1629 " neither errorfile nor payload defined.\n",
1630 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001631 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001632 }
1633 }
1634 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1635
1636 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1637 /* default errorfile or errorfiles: check the status */
1638 int rc;
1639
1640 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1641 if (http_err_codes[rc] == reply->status)
1642 break;
1643 }
1644
1645 if (rc >= HTTP_ERR_SIZE) {
1646 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1647 reply->status, act_arg);
1648 goto error;
1649 }
1650 }
1651
1652 if (reply->ctype) {
1653 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1654 "with an erorrfile.\n",
1655 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001656 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001657 }
1658 if (!LIST_ISEMPTY(&reply->hdrs)) {
1659 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1660 "with an erorrfile.\n",
1661 px->conf.args.file, px->conf.args.line);
1662 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001663 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001664 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001665 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001666 release_sample_expr(lf->expr);
1667 free(lf->arg);
1668 free(lf);
1669 }
1670 istfree(&hdr->name);
1671 free(hdr);
1672 }
1673 }
1674 }
1675 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001676 if ((reply->status == 204 || reply->status == 304) && objlen) {
1677 memprintf(errmsg, "No body expected for %d responses", reply->status);
1678 goto error;
1679 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001680 if (!reply->ctype && objlen) {
1681 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1682 goto error;
1683 }
1684 if (reply->ctype && !b_data(&reply->body.obj)) {
1685 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001686 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001687 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001688 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001689 }
1690 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1691 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1692 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1693 px->conf.args.file, px->conf.args.line);
1694 }
1695 }
1696 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1697 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001698 if ((reply->status == 204 || reply->status == 304)) {
1699 memprintf(errmsg, "No body expected for %d responses", reply->status);
1700 goto error;
1701 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001702 if (!reply->ctype) {
1703 memprintf(errmsg, "a content type must be defined with a log-format payload");
1704 goto error;
1705 }
1706 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1707 goto error;
1708
1709 free(px->conf.lfs_file);
1710 px->conf.lfs_file = strdup(px->conf.args.file);
1711 px->conf.lfs_line = px->conf.args.line;
1712 }
1713
1714 free(obj);
1715 *orig_arg = cur_arg;
1716 return reply;
1717
1718 error:
1719 free(obj);
1720 if (fd >= 0)
1721 close(fd);
1722 release_http_reply(reply);
1723 return NULL;
1724}
1725
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001726/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1727 * Returns 0 if no error has been found else non-zero.
1728 *
1729 * The normalization is processed on the target-uri at the condition that it is
1730 * in absolute-form. In the case where the target-uri was normalized, every
1731 * host headers values found are also replaced by the normalized hostname. This
1732 * assumes that the target-uri and host headers were properly identify as
1733 * similar before calling this function.
1734 */
1735int http_scheme_based_normalize(struct htx *htx)
1736{
1737 struct http_hdr_ctx ctx;
1738 struct htx_sl *sl;
1739 struct ist uri, scheme, authority, host, port;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001740
1741 sl = http_get_stline(htx);
1742
1743 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1744 return 0;
1745
1746 uri = htx_sl_req_uri(sl);
1747
1748 scheme = http_get_scheme(uri);
1749 /* if no scheme found, no normalization to proceed */
1750 if (!isttest(scheme))
1751 return 0;
1752
Christopher Fauletbe50b142022-07-05 10:24:52 +02001753 /* Extract the port if present in authority */
1754 authority = http_get_authority(uri, 1);
1755 port = http_get_host_port(authority);
1756 if (!isttest(port)) {
1757 /* if no port found, no normalization to proceed */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001758 return 0;
Christopher Fauletbe50b142022-07-05 10:24:52 +02001759 }
1760 host = isttrim(authority, istlen(authority) - istlen(port) - 1);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001761
Christopher Fauletbe50b142022-07-05 10:24:52 +02001762 if (istlen(port) && http_is_default_port(scheme, port)) {
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001763 /* reconstruct the uri with removal of the port */
1764 struct buffer *temp = get_trash_chunk();
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001765 struct ist meth, vsn;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001766
1767 /* meth */
1768 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1769 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1770
1771 /* vsn */
1772 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1773 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1774
1775 /* reconstruct uri without port */
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001776 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001777 chunk_istcat(temp, host);
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001778 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
1779 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001780
1781 http_replace_stline(htx, meth, uri, vsn);
1782
1783 /* replace every host headers values by the normalized host */
1784 ctx.blk = NULL;
1785 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1786 if (!http_replace_header_value(htx, &ctx, host))
1787 goto fail;
1788 }
1789 }
1790
1791 return 0;
1792
1793 fail:
1794 return 1;
1795}
1796
Christopher Faulet07f41f72020-01-16 16:16:06 +01001797/* Parses the "errorloc[302|303]" proxy keyword */
1798static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001799 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001800 char **errmsg)
1801{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001802 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001803 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001804 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001805 int errloc, status;
1806 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001807
1808 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1809 ret = 1;
1810 goto out;
1811 }
1812
1813 if (*(args[1]) == 0 || *(args[2]) == 0) {
1814 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1815 ret = -1;
1816 goto out;
1817 }
1818
1819 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001820 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001821 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1822 if (!msg) {
1823 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1824 ret = -1;
1825 goto out;
1826 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001827
1828 reply = calloc(1, sizeof(*reply));
1829 if (!reply) {
1830 memprintf(errmsg, "%s : out of memory.", args[0]);
1831 ret = -1;
1832 goto out;
1833 }
1834 reply->type = HTTP_REPLY_ERRMSG;
1835 reply->status = status;
1836 reply->ctype = NULL;
1837 LIST_INIT(&reply->hdrs);
1838 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001839 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001840
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001841 conf_err = calloc(1, sizeof(*conf_err));
1842 if (!conf_err) {
1843 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001844 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001845 ret = -1;
1846 goto out;
1847 }
1848 conf_err->type = 1;
1849 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001850 conf_err->info.errorfile.reply = reply;
1851
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001852 conf_err->file = strdup(file);
1853 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001854 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001855
Christopher Fauleta66adf42020-11-05 22:43:41 +01001856 /* handle warning message */
1857 if (*errmsg)
1858 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001859 out:
1860 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001861
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001862}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001863
1864/* Parses the "errorfile" proxy keyword */
1865static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001866 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001867 char **errmsg)
1868{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001869 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001870 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001871 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001872 int status;
1873 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001874
1875 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1876 ret = 1;
1877 goto out;
1878 }
1879
1880 if (*(args[1]) == 0 || *(args[2]) == 0) {
1881 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1882 ret = -1;
1883 goto out;
1884 }
1885
1886 status = atol(args[1]);
1887 msg = http_parse_errorfile(status, args[2], errmsg);
1888 if (!msg) {
1889 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1890 ret = -1;
1891 goto out;
1892 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001893
Christopher Faulet5809e102020-05-14 17:31:52 +02001894 reply = calloc(1, sizeof(*reply));
1895 if (!reply) {
1896 memprintf(errmsg, "%s : out of memory.", args[0]);
1897 ret = -1;
1898 goto out;
1899 }
1900 reply->type = HTTP_REPLY_ERRMSG;
1901 reply->status = status;
1902 reply->ctype = NULL;
1903 LIST_INIT(&reply->hdrs);
1904 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001905 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001906
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001907 conf_err = calloc(1, sizeof(*conf_err));
1908 if (!conf_err) {
1909 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001910 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001911 ret = -1;
1912 goto out;
1913 }
1914 conf_err->type = 1;
1915 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001916 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001917 conf_err->file = strdup(file);
1918 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001919 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001920
Christopher Fauleta66adf42020-11-05 22:43:41 +01001921 /* handle warning message */
1922 if (*errmsg)
1923 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001924 out:
1925 return ret;
1926
1927}
1928
1929/* Parses the "errorfiles" proxy keyword */
1930static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001931 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001932 char **err)
1933{
1934 struct conf_errors *conf_err = NULL;
1935 char *name = NULL;
1936 int rc, ret = 0;
1937
1938 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1939 ret = 1;
1940 goto out;
1941 }
1942
1943 if (!*(args[1])) {
1944 memprintf(err, "%s : expects <name> as argument.", args[0]);
1945 ret = -1;
1946 goto out;
1947 }
1948
1949 name = strdup(args[1]);
1950 conf_err = calloc(1, sizeof(*conf_err));
1951 if (!name || !conf_err) {
1952 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001953 goto error;
1954 }
1955 conf_err->type = 0;
1956
1957 conf_err->info.errorfiles.name = name;
1958 if (!*(args[2])) {
1959 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1960 conf_err->info.errorfiles.status[rc] = 1;
1961 }
1962 else {
1963 int cur_arg, status;
1964 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1965 status = atol(args[cur_arg]);
1966
1967 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1968 if (http_err_codes[rc] == status) {
1969 conf_err->info.errorfiles.status[rc] = 2;
1970 break;
1971 }
1972 }
1973 if (rc >= HTTP_ERR_SIZE) {
1974 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001975 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001976 }
1977 }
1978 }
1979 conf_err->file = strdup(file);
1980 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001981 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001982 out:
1983 return ret;
1984
1985 error:
1986 free(name);
1987 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001988 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001989 goto out;
1990}
1991
Christopher Faulet3b967c12020-05-15 15:47:44 +02001992/* Parses the "http-error" proxy keyword */
1993static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001994 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02001995 char **errmsg)
1996{
1997 struct conf_errors *conf_err;
1998 struct http_reply *reply = NULL;
1999 int rc, cur_arg, ret = 0;
2000
2001 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2002 ret = 1;
2003 goto out;
2004 }
2005
2006 cur_arg = 1;
2007 curpx->conf.args.ctx = ARGC_HERR;
2008 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2009 if (!reply) {
2010 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2011 goto error;
2012 }
2013 else if (!reply->status) {
2014 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2015 goto error;
2016 }
2017
2018 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2019 if (http_err_codes[rc] == reply->status)
2020 break;
2021 }
2022
2023 if (rc >= HTTP_ERR_SIZE) {
2024 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2025 goto error;
2026 }
2027 if (*args[cur_arg]) {
2028 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2029 goto error;
2030 }
2031
2032 conf_err = calloc(1, sizeof(*conf_err));
2033 if (!conf_err) {
2034 memprintf(errmsg, "%s : out of memory.", args[0]);
2035 goto error;
2036 }
2037 if (reply->type == HTTP_REPLY_ERRFILES) {
2038 int rc = http_get_status_idx(reply->status);
2039
2040 conf_err->type = 2;
2041 conf_err->info.errorfiles.name = reply->body.http_errors;
2042 conf_err->info.errorfiles.status[rc] = 2;
2043 reply->body.http_errors = NULL;
2044 release_http_reply(reply);
2045 }
2046 else {
2047 conf_err->type = 1;
2048 conf_err->info.errorfile.status = reply->status;
2049 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002050 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002051 }
2052 conf_err->file = strdup(file);
2053 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002054 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002055
Christopher Faulet3005d282020-11-13 10:58:01 +01002056 /* handle warning message */
2057 if (*errmsg)
2058 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002059 out:
2060 return ret;
2061
2062 error:
2063 release_http_reply(reply);
2064 ret = -1;
2065 goto out;
2066
2067}
2068
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002069/* Check "errorfiles" proxy keyword */
2070static int proxy_check_errors(struct proxy *px)
2071{
2072 struct conf_errors *conf_err, *conf_err_back;
2073 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002074 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002075
2076 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2077 if (conf_err->type == 1) {
2078 /* errorfile */
2079 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002080 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002081
2082 /* For proxy, to rely on default replies, just don't reference a reply */
2083 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2084 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002085 }
2086 else {
2087 /* errorfiles */
2088 list_for_each_entry(http_errs, &http_errors_list, list) {
2089 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2090 break;
2091 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002092
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002093 /* unknown http-errors section */
2094 if (&http_errs->list == &http_errors_list) {
2095 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2096 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2097 err |= ERR_ALERT | ERR_FATAL;
2098 free(conf_err->info.errorfiles.name);
2099 goto next;
2100 }
2101
2102 free(conf_err->info.errorfiles.name);
2103 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2104 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002105 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002106 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002107 else if (conf_err->info.errorfiles.status[rc] == 2)
2108 ha_warning("config: proxy '%s' : status '%d' not declared in"
2109 " http-errors section '%s' (at %s:%d).\n",
2110 px->id, http_err_codes[rc], http_errs->id,
2111 conf_err->file, conf_err->line);
2112 }
2113 }
2114 }
2115 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002116 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002117 free(conf_err->file);
2118 free(conf_err);
2119 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002120
2121 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002122 return err;
2123}
2124
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002125static int post_check_errors()
2126{
2127 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002128 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002129 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002130 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002131
2132 node = ebpt_first(&http_error_messages);
2133 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002134 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2135 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002136 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002137 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002138 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2139 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002140 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002141 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002142 (char *)node->key);
2143 err_code |= ERR_WARN;
2144 }
2145 next:
2146 node = ebpt_next(node);
2147 }
2148
2149 return err_code;
2150}
2151
Willy Tarreau016255a2021-02-12 08:40:29 +01002152int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002153{
2154 struct conf_errors *conf_err, *new_conf_err = NULL;
2155 int ret = 0;
2156
2157 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2158 new_conf_err = calloc(1, sizeof(*new_conf_err));
2159 if (!new_conf_err) {
2160 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2161 goto out;
2162 }
2163 new_conf_err->type = conf_err->type;
2164 if (conf_err->type == 1) {
2165 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002166 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002167 }
2168 else {
2169 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2170 if (!new_conf_err->info.errorfiles.name) {
2171 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2172 goto out;
2173 }
2174 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2175 sizeof(conf_err->info.errorfiles.status));
2176 }
2177 new_conf_err->file = strdup(conf_err->file);
2178 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002179 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002180 new_conf_err = NULL;
2181 }
2182 ret = 1;
2183
2184 out:
2185 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002186 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002187}
2188
2189void proxy_release_conf_errors(struct proxy *px)
2190{
2191 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002192
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002193 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2194 if (conf_err->type == 0)
2195 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002196 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002197 free(conf_err->file);
2198 free(conf_err);
2199 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002200}
2201
2202/*
2203 * Parse an <http-errors> section.
2204 * Returns the error code, 0 if OK, or any combination of :
2205 * - ERR_ABORT: must abort ASAP
2206 * - ERR_FATAL: we can continue parsing but not start the service
2207 * - ERR_WARN: a warning has been emitted
2208 * - ERR_ALERT: an alert has been emitted
2209 * Only the two first ones can stop processing, the two others are just
2210 * indicators.
2211 */
2212static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2213{
2214 static struct http_errors *curr_errs = NULL;
2215 int err_code = 0;
2216 const char *err;
2217 char *errmsg = NULL;
2218
2219 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2220 if (!*args[1]) {
2221 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2222 err_code |= ERR_ALERT | ERR_ABORT;
2223 goto out;
2224 }
2225
2226 err = invalid_char(args[1]);
2227 if (err) {
2228 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2229 file, linenum, *err, args[0], args[1]);
2230 err_code |= ERR_ALERT | ERR_FATAL;
2231 }
2232
2233 list_for_each_entry(curr_errs, &http_errors_list, list) {
2234 /* Error if two errors section owns the same name */
2235 if (strcmp(curr_errs->id, args[1]) == 0) {
2236 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2237 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2238 err_code |= ERR_ALERT | ERR_FATAL;
2239 }
2240 }
2241
2242 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2243 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2244 err_code |= ERR_ALERT | ERR_ABORT;
2245 goto out;
2246 }
2247
Willy Tarreau2b718102021-04-21 07:32:39 +02002248 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002249 curr_errs->id = strdup(args[1]);
2250 curr_errs->conf.file = strdup(file);
2251 curr_errs->conf.line = linenum;
2252 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002253 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002254 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002255 struct buffer *msg;
2256 int status, rc;
2257
2258 if (*(args[1]) == 0 || *(args[2]) == 0) {
2259 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2260 file, linenum, args[0]);
2261 err_code |= ERR_ALERT | ERR_FATAL;
2262 goto out;
2263 }
2264
2265 status = atol(args[1]);
2266 msg = http_parse_errorfile(status, args[2], &errmsg);
2267 if (!msg) {
2268 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002272 if (errmsg) {
2273 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2274 err_code |= ERR_WARN;
2275 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002276
2277 reply = calloc(1, sizeof(*reply));
2278 if (!reply) {
2279 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2280 err_code |= ERR_ALERT | ERR_FATAL;
2281 goto out;
2282 }
2283 reply->type = HTTP_REPLY_ERRMSG;
2284 reply->status = status;
2285 reply->ctype = NULL;
2286 LIST_INIT(&reply->hdrs);
2287 reply->body.errmsg = msg;
2288
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002289 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002290 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002291 }
2292 else if (*args[0] != 0) {
2293 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296 }
2297
2298out:
2299 free(errmsg);
2300 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002301}
2302
2303static struct cfg_kw_list cfg_kws = {ILH, {
2304 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2305 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2306 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2307 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002308 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002309 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002310 { 0, NULL, NULL },
2311}};
2312
2313INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002314REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002315REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002316
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002317REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2318
Christopher Faulet29f72842019-12-11 15:52:32 +01002319/************************************************************************/
2320/* HTX sample fetches */
2321/************************************************************************/
2322
2323/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2324static int
2325smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2326{
2327 if (!smp->strm)
2328 return 0;
2329
2330 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2331 smp->data.type = SMP_T_BOOL;
2332 return 1;
2333}
2334
2335/* Returns the number of blocks in an HTX message. The channel is chosen
2336 * depending on the sample direction. */
2337static int
2338smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2339{
2340 struct channel *chn;
2341 struct htx *htx;
2342
2343 if (!smp->strm)
2344 return 0;
2345
2346 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002347 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002348 if (!htx)
2349 return 0;
2350
2351 smp->data.u.sint = htx_nbblks(htx);
2352 smp->data.type = SMP_T_SINT;
2353 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2354 return 1;
2355}
2356
2357/* Returns the size of an HTX message. The channel is chosen depending on the
2358 * sample direction. */
2359static int
2360smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2361{
2362 struct channel *chn;
2363 struct htx *htx;
2364
2365 if (!smp->strm)
2366 return 0;
2367
2368 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002369 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002370 if (!htx)
2371 return 0;
2372
2373 smp->data.u.sint = htx->size;
2374 smp->data.type = SMP_T_SINT;
2375 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2376 return 1;
2377}
2378
2379/* Returns the data size of an HTX message. The channel is chosen depending on the
2380 * sample direction. */
2381static int
2382smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2383{
2384 struct channel *chn;
2385 struct htx *htx;
2386
2387 if (!smp->strm)
2388 return 0;
2389
2390 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002391 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002392 if (!htx)
2393 return 0;
2394
2395 smp->data.u.sint = htx->data;
2396 smp->data.type = SMP_T_SINT;
2397 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2398 return 1;
2399}
2400
2401/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2402 * depending on the sample direction. */
2403static int
2404smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2405{
2406 struct channel *chn;
2407 struct htx *htx;
2408
2409 if (!smp->strm)
2410 return 0;
2411
2412 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002413 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002414 if (!htx)
2415 return 0;
2416
2417 smp->data.u.sint = htx_used_space(htx);
2418 smp->data.type = SMP_T_SINT;
2419 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2420 return 1;
2421}
2422
2423/* Returns the free space (size-used) of an HTX message. The channel is chosen
2424 * depending on the sample direction. */
2425static int
2426smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2427{
2428 struct channel *chn;
2429 struct htx *htx;
2430
2431 if (!smp->strm)
2432 return 0;
2433
2434 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002435 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002436 if (!htx)
2437 return 0;
2438
2439 smp->data.u.sint = htx_free_space(htx);
2440 smp->data.type = SMP_T_SINT;
2441 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2442 return 1;
2443}
2444
2445/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2446 * channel is chosen depending on the sample direction. */
2447static int
2448smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2449{
2450 struct channel *chn;
2451 struct htx *htx;
2452
2453 if (!smp->strm)
2454 return 0;
2455
2456 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002457 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002458 if (!htx)
2459 return 0;
2460
2461 smp->data.u.sint = htx_free_data_space(htx);
2462 smp->data.type = SMP_T_SINT;
2463 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2464 return 1;
2465}
2466
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002467/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2468 * channel is chosen depending on the sample direction.
2469 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002470static int
2471smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2472{
2473 struct channel *chn;
2474 struct htx *htx;
2475
2476 if (!smp->strm)
2477 return 0;
2478
2479 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002480 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002481 if (!htx)
2482 return 0;
2483
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002484 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002485 smp->data.type = SMP_T_BOOL;
2486 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2487 return 1;
2488}
2489
2490/* Returns the type of a specific HTX block, if found in the message. Otherwise
2491 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2492 * "head", "tail" or "first". The channel is chosen depending on the sample
2493 * direction. */
2494static int
2495smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2496{
2497 struct channel *chn;
2498 struct htx *htx;
2499 enum htx_blk_type type;
2500 int32_t pos;
2501
2502 if (!smp->strm || !arg_p)
2503 return 0;
2504
2505 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002506 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002507 if (!htx)
2508 return 0;
2509
2510 pos = arg_p[0].data.sint;
2511 if (pos == -1)
2512 type = htx_get_head_type(htx);
2513 else if (pos == -2)
2514 type = htx_get_tail_type(htx);
2515 else if (pos == -3)
2516 type = htx_get_first_type(htx);
2517 else
2518 type = ((pos >= htx->head && pos <= htx->tail)
2519 ? htx_get_blk_type(htx_get_blk(htx, pos))
2520 : HTX_BLK_UNUSED);
2521
2522 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2523 smp->data.type = SMP_T_STR;
2524 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2525 return 1;
2526}
2527
2528/* Returns the size of a specific HTX block, if found in the message. Otherwise
2529 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2530 * "first". The channel is chosen depending on the sample direction. */
2531static int
2532smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2533{
2534 struct channel *chn;
2535 struct htx *htx;
2536 struct htx_blk *blk;
2537 int32_t pos;
2538
2539 if (!smp->strm || !arg_p)
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 pos = arg_p[0].data.sint;
2548 if (pos == -1)
2549 blk = htx_get_head_blk(htx);
2550 else if (pos == -2)
2551 blk = htx_get_tail_blk(htx);
2552 else if (pos == -3)
2553 blk = htx_get_first_blk(htx);
2554 else
2555 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2556
2557 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2558 smp->data.type = SMP_T_SINT;
2559 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2560 return 1;
2561}
2562
2563/* Returns the start-line if the selected HTX block exists and is a
2564 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2565 * supported or "head", "tail" or "first". The channel is chosen depending on
2566 * the sample direction. */
2567static int
2568smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2569{
2570 struct buffer *temp;
2571 struct channel *chn;
2572 struct htx *htx;
2573 struct htx_blk *blk;
2574 struct htx_sl *sl;
2575 int32_t pos;
2576
2577 if (!smp->strm || !arg_p)
2578 return 0;
2579
2580 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002581 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002582 if (!htx)
2583 return 0;
2584
2585 pos = arg_p[0].data.sint;
2586 if (pos == -1)
2587 blk = htx_get_head_blk(htx);
2588 else if (pos == -2)
2589 blk = htx_get_tail_blk(htx);
2590 else if (pos == -3)
2591 blk = htx_get_first_blk(htx);
2592 else
2593 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2594
2595 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2596 smp->data.u.str.size = 0;
2597 smp->data.u.str.area = "";
2598 smp->data.u.str.data = 0;
2599 }
2600 else {
2601 sl = htx_get_blk_ptr(htx, blk);
2602
2603 temp = get_trash_chunk();
2604 chunk_istcat(temp, htx_sl_p1(sl));
2605 temp->area[temp->data++] = ' ';
2606 chunk_istcat(temp, htx_sl_p2(sl));
2607 temp->area[temp->data++] = ' ';
2608 chunk_istcat(temp, htx_sl_p3(sl));
2609
2610 smp->data.u.str = *temp;
2611 }
2612
2613 smp->data.type = SMP_T_STR;
2614 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2615 return 1;
2616}
2617
2618/* Returns the header name if the selected HTX block exists and is a header or a
2619 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2620 * supported or "head", "tail" or "first". The channel is chosen depending on
2621 * the sample direction. */
2622static int
2623smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2624{
2625 struct channel *chn;
2626 struct htx *htx;
2627 struct htx_blk *blk;
2628 int32_t pos;
2629
2630 if (!smp->strm || !arg_p)
2631 return 0;
2632
2633 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002634 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002635 if (!htx)
2636 return 0;
2637
2638 pos = arg_p[0].data.sint;
2639 if (pos == -1)
2640 blk = htx_get_head_blk(htx);
2641 else if (pos == -2)
2642 blk = htx_get_tail_blk(htx);
2643 else if (pos == -3)
2644 blk = htx_get_first_blk(htx);
2645 else
2646 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2647
2648 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2649 smp->data.u.str.size = 0;
2650 smp->data.u.str.area = "";
2651 smp->data.u.str.data = 0;
2652 }
2653 else {
2654 struct ist name = htx_get_blk_name(htx, blk);
2655
2656 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2657 }
2658 smp->data.type = SMP_T_STR;
2659 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2660 return 1;
2661}
2662
2663/* Returns the header value if the selected HTX block exists and is a header or
2664 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2665 * supported or "head", "tail" or "first". The channel is chosen depending on
2666 * the sample direction. */
2667static int
2668smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2669{
2670 struct channel *chn;
2671 struct htx *htx;
2672 struct htx_blk *blk;
2673 int32_t pos;
2674
2675 if (!smp->strm || !arg_p)
2676 return 0;
2677
2678 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002679 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002680 if (!htx)
2681 return 0;
2682
2683 pos = arg_p[0].data.sint;
2684 if (pos == -1)
2685 blk = htx_get_head_blk(htx);
2686 else if (pos == -2)
2687 blk = htx_get_tail_blk(htx);
2688 else if (pos == -3)
2689 blk = htx_get_first_blk(htx);
2690 else
2691 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2692
2693 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2694 smp->data.u.str.size = 0;
2695 smp->data.u.str.area = "";
2696 smp->data.u.str.data = 0;
2697 }
2698 else {
2699 struct ist val = htx_get_blk_value(htx, blk);
2700
2701 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2702 }
2703 smp->data.type = SMP_T_STR;
2704 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2705 return 1;
2706}
2707
2708/* Returns the value if the selected HTX block exists and is a data
2709 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2710 * or "head", "tail" or "first". The channel is chosen depending on the sample
2711 * direction. */
2712static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002713smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002714{
2715 struct channel *chn;
2716 struct htx *htx;
2717 struct htx_blk *blk;
2718 int32_t pos;
2719
2720 if (!smp->strm || !arg_p)
2721 return 0;
2722
2723 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002724 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002725 if (!htx)
2726 return 0;
2727
2728 pos = arg_p[0].data.sint;
2729 if (pos == -1)
2730 blk = htx_get_head_blk(htx);
2731 else if (pos == -2)
2732 blk = htx_get_tail_blk(htx);
2733 else if (pos == -3)
2734 blk = htx_get_first_blk(htx);
2735 else
2736 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2737
2738 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2739 smp->data.u.str.size = 0;
2740 smp->data.u.str.area = "";
2741 smp->data.u.str.data = 0;
2742 }
2743 else {
2744 struct ist val = htx_get_blk_value(htx, blk);
2745
2746 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2747 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002748 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002749 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2750 return 1;
2751}
2752
2753/* This function is used to validate the arguments passed to any "htx_blk" fetch
2754 * keywords. An argument is expected by these keywords. It must be a positive
2755 * integer or on of the following strings: "head", "tail" or "first". It returns
2756 * 0 on error, and a non-zero value if OK.
2757 */
2758int val_blk_arg(struct arg *arg, char **err_msg)
2759{
2760 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2761 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2762 return 0;
2763 }
2764 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002765 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002766 arg[0].type = ARGT_SINT;
2767 arg[0].data.sint = -1;
2768 }
2769 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002770 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002771 arg[0].type = ARGT_SINT;
2772 arg[0].data.sint = -2;
2773 }
2774 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002775 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002776 arg[0].type = ARGT_SINT;
2777 arg[0].data.sint = -3;
2778 }
2779 else {
2780 int pos;
2781
2782 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002783 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002784 memprintf(err_msg, "invalid block position");
2785 return 0;
2786 }
2787 }
2788
2789 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2790 if (pos < 0) {
2791 memprintf(err_msg, "block position must not be negative");
2792 return 0;
2793 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002794 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002795 arg[0].type = ARGT_SINT;
2796 arg[0].data.sint = pos;
2797 }
2798
2799 return 1;
2800}
2801
2802
2803/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002804 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002805 */
2806static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002807 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002808
Christopher Faulet01f44452020-01-08 14:23:40 +01002809 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2810 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2811 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2812 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2813 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2814 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2815 { "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 +01002816
Christopher Faulet01f44452020-01-08 14:23:40 +01002817 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2818 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2819 { "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},
2820 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2821 { "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 +01002822 { "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 +01002823
2824 { /* END */ },
2825}};
2826
2827INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);