blob: f8178ba7c27073c3ff5e5df92c5439f768388809 [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +020014#include <ctype.h>
Christopher Faulet5031ef52020-01-15 11:22:07 +010015#include <fcntl.h>
16#include <unistd.h>
17
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020022#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/log.h>
28#include <haproxy/regex.h>
29#include <haproxy/sample.h>
Willy Tarreau4cbf62d2021-05-08 13:01:23 +020030#include <haproxy/tools.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020031
Christopher Faulet47596d32018-10-22 09:17:28 +020032
Christopher Fauletf7346382019-07-17 22:02:08 +020033struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020034struct http_reply http_err_replies[HTTP_ERR_SIZE];
35
Christopher Faulet58857752020-01-15 15:19:50 +010036struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010037struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020038struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010039
Christopher Faulet76edc0f2020-01-13 15:52:01 +010040/* The declaration of an errorfiles/errorfile directives. Used during config
41 * parsing only. */
42struct conf_errors {
43 char type; /* directive type (0: errorfiles, 1: errorfile) */
44 union {
45 struct {
46 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020047 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010048 } errorfile; /* describe an "errorfile" directive */
49 struct {
50 char *name; /* the http-errors section name */
51 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
52 } errorfiles; /* describe an "errorfiles" directive */
53 } info;
54
55 char *file; /* file where the directive appears */
56 int line; /* line where the directive appears */
57
58 struct list list; /* next conf_errors */
59};
60
Christopher Faulet297fbb42019-05-13 14:41:27 +020061/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020062 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020063 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020064 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020065struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020066{
Christopher Faulet297fbb42019-05-13 14:41:27 +020067 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010068
Christopher Faulet29f17582019-05-23 11:03:26 +020069 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020070 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL))
Christopher Faulet297fbb42019-05-13 14:41:27 +020071 return NULL;
72 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100162 if (type == HTX_BLK_EOH)
Christopher Faulet573fe732018-11-28 16:55:12 +0100163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000204 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
Christopher Faulet56280382022-11-22 15:41:48 +0100391 sl->flags &= ~HTX_SL_F_NORMALIZED_URI;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200392 if (!http_update_host(htx, sl, uri))
393 goto fail;
394
395 return 1;
396 fail:
397 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200398}
399
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200400/* Replace the request path in the HTX message <htx> by <path>. The host part is
401 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
402 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
403 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200404 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200405int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200406{
407 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200408 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100409 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200410 size_t plen = 0;
411
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100412 if (!sl)
413 return 0;
414
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100415 uri = htx_sl_req_uri(sl);
416 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100417 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100418 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200419 if (with_qs)
420 plen = p.len;
421 else {
422 while (plen < p.len && *(p.ptr + plen) != '?')
423 plen++;
424 }
Christopher Faulete010c802018-10-24 10:36:45 +0200425
426 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100427 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
428 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200429
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100430 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
431 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
432
433 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200434 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
435 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100436 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200437
438 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100439 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200440}
441
442/* Replace the request query-string in the HTX message <htx> by <query>. The
443 * host part and the path are preserved. It returns 1 on success, otherwise
444 * 0.
445 */
446int http_replace_req_query(struct htx *htx, const struct ist query)
447{
448 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200449 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100450 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200451 int offset = 1;
452
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100453 if (!sl)
454 return 0;
455
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100456 uri = htx_sl_req_uri(sl);
457 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200458 while (q.len > 0 && *(q.ptr) != '?') {
459 q.ptr++;
460 q.len--;
461 }
462
463 /* skip the question mark or indicate that we must insert it
464 * (but only if the format string is not empty then).
465 */
466 if (q.len) {
467 q.ptr++;
468 q.len--;
469 }
470 else if (query.len > 1)
471 offset = 0;
472
473 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100474 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
475 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200476
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
478 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200479
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100480 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
481 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
482 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200483
484 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100485 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200486}
487
488/* Replace the response status in the HTX message <htx> by <status>. It returns
489 * 1 on success, otherwise 0.
490*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200491int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200492{
493 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200494 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200495 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200496
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100497 if (!sl)
498 return 0;
499
Christopher Faulete010c802018-10-24 10:36:45 +0200500 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100501 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
502 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200503 r = reason;
504 if (!isttest(r)) {
505 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
506 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
507 }
Christopher Faulete010c802018-10-24 10:36:45 +0200508
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100509 /* create the new start line */
510 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200511 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200512}
513
514/* Replace the response reason in the HTX message <htx> by <reason>. It returns
515 * 1 on success, otherwise 0.
516*/
517int http_replace_res_reason(struct htx *htx, const struct ist reason)
518{
519 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200520 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100521 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200522
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100523 if (!sl)
524 return 0;
525
Christopher Faulete010c802018-10-24 10:36:45 +0200526 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100527 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
528 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200529
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100530 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
531 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200532
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100533 /* create the new start line */
534 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200535}
536
Christopher Faulet47596d32018-10-22 09:17:28 +0200537/* Replaces a part of a header value referenced in the context <ctx> by
538 * <data>. It returns 1 on success, otherwise it returns 0. The context is
539 * updated if necessary.
540 */
541int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
542{
543 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200544 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200545 char *start;
546 struct ist v;
547 uint32_t len, off;
548
549 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200550 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200551
552 v = htx_get_blk_value(htx, blk);
553 start = ctx->value.ptr - ctx->lws_before;
554 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
555 off = start - v.ptr;
556
557 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
558 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200559 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200560
561 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200562
563 sl = http_get_stline(htx);
564 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
565 struct ist n = htx_get_blk_name(htx, blk);
566
567 if (isteq(n, ist("host"))) {
568 if (!http_update_authority(htx, sl, v))
569 goto fail;
570 ctx->blk = NULL;
571 http_find_header(htx, ist("host"), ctx, 1);
572 blk = ctx->blk;
573 v = htx_get_blk_value(htx, blk);
574 }
575 }
576
Christopher Faulet47596d32018-10-22 09:17:28 +0200577 ctx->blk = blk;
578 ctx->value.ptr = v.ptr + off;
579 ctx->value.len = data.len;
580 ctx->lws_before = ctx->lws_after = 0;
581
582 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200583 fail:
584 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200585}
586
587/* Fully replaces a header referenced in the context <ctx> by the name <name>
588 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
589 * context is updated if necessary.
590 */
591int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
592 const struct ist name, const struct ist value)
593{
594 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200595 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200596
597 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200598 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200599
600 blk = htx_replace_header(htx, blk, name, value);
601 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200602 goto fail;
603
604 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100605 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200606 if (!http_update_authority(htx, sl, value))
607 goto fail;
608 ctx->blk = NULL;
609 http_find_header(htx, ist("host"), ctx, 1);
610 blk = ctx->blk;
611 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200612
613 ctx->blk = blk;
614 ctx->value = ist(NULL);
615 ctx->lws_before = ctx->lws_after = 0;
616
617 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200618 fail:
619 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200620}
621
622/* Remove one value of a header. This only works on a <ctx> returned by
623 * http_find_header function. The value is removed, as well as surrounding commas
624 * if any. If the removed value was alone, the whole header is removed. The
625 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
626 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
627 * form that can be handled by http_find_header() to find next occurrence.
628 */
629int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
630{
631 struct htx_blk *blk = ctx->blk;
632 char *start;
633 struct ist v;
634 uint32_t len;
635
636 if (!blk)
637 return 0;
638
639 start = ctx->value.ptr - ctx->lws_before;
640 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
641
642 v = htx_get_blk_value(htx, blk);
643 if (len == v.len) {
644 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200645 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200646 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100647 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200648 ctx->lws_before = ctx->lws_after = 0;
649 }
650 else {
651 ctx->blk = htx_get_blk(htx, htx->tail);
652 ctx->value = htx_get_blk_value(htx, ctx->blk);
653 ctx->lws_before = ctx->lws_after = 0;
654 }
655 return 1;
656 }
657
658 /* This was not the only value of this header. We have to remove the
659 * part pointed by ctx->value. If it is the last entry of the list, we
660 * remove the last separator.
661 */
662 if (start == v.ptr) {
663 /* It's the first header part but not the only one. So remove
664 * the comma after it. */
665 len++;
666 }
667 else {
668 /* There is at least one header part before the removed one. So
669 * remove the comma between them. */
670 start--;
671 len++;
672 }
673 /* Update the block content and its len */
674 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200675 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200676
677 /* Finally update the ctx */
678 ctx->value.ptr = start;
679 ctx->value.len = 0;
680 ctx->lws_before = ctx->lws_after = 0;
681
682 return 1;
683}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200684
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200685/* Updates the authority part of the uri with the value <host>. It happens when
686 * the header host is modified. It returns 0 on failure and 1 on success. It is
687 * the caller responsibility to provide the start-line and to be sure the uri
688 * contains an authority. Thus, if no authority is found in the uri, an error is
689 * returned.
690 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200691int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200692{
693 struct buffer *temp = get_trash_chunk();
694 struct ist meth, vsn, uri, authority;
695
696 uri = htx_sl_req_uri(sl);
697 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100698 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200699 return 0;
700
Christopher Faulet34b18e42020-02-18 11:02:21 +0100701 /* Don't update the uri if there is no change */
702 if (isteq(host, authority))
703 return 1;
704
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200705 /* Start by copying old method and version */
706 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
707 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
708
709 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
710 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
711
712 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
713 chunk_memcat(temp, host.ptr, host.len);
714 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
715 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
716
717 return http_replace_stline(htx, meth, uri, vsn);
718
719}
720
721/* Update the header host by extracting the authority of the uri <uri>. flags of
722 * the start-line are also updated accordingly. For orgin-form and asterisk-form
723 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
724 * removed from the flags of the start-line. Otherwise, this flag is set and the
725 * authority is used to set the value of the header host. This function returns
726 * 0 on failure and 1 on success.
727*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200728int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200729{
730 struct ist authority;
731 struct http_hdr_ctx ctx;
732
733 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
734 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
735 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
736 }
737 else {
738 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
739 if (sl->info.req.meth != HTTP_METH_CONNECT) {
740 // absolute-form (RFC7320 #5.3.2)
741 sl->flags |= HTX_SL_F_HAS_SCHM;
742 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
743 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
744
745 authority = http_get_authority(uri, 1);
746 if (!authority.len)
747 goto fail;
748 }
749 else {
750 // authority-form (RFC7320 #5.3.3)
751 authority = uri;
752 }
753
754 /* Replace header host value */
755 ctx.blk = NULL;
756 while (http_find_header(htx, ist("host"), &ctx, 1)) {
757 if (!http_replace_header_value(htx, &ctx, authority))
758 goto fail;
759 }
760
761 }
762 return 1;
763 fail:
764 return 0;
765}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200766
767/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
768 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
769 * performed over the whole headers. Otherwise it must contain a valid header
770 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
771 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
772 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
773 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
774 * -1. The value fetch stops at commas, so this function is suited for use with
775 * list headers.
776 * The return value is 0 if nothing was found, or non-zero otherwise.
777 */
778unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
779 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
780{
781 struct http_hdr_ctx local_ctx;
782 struct ist val_hist[MAX_HDR_HISTORY];
783 unsigned int hist_idx;
784 int found;
785
786 if (!ctx) {
787 local_ctx.blk = NULL;
788 ctx = &local_ctx;
789 }
790
791 if (occ >= 0) {
792 /* search from the beginning */
793 while (http_find_header(htx, hdr, ctx, 0)) {
794 occ--;
795 if (occ <= 0) {
796 *vptr = ctx->value.ptr;
797 *vlen = ctx->value.len;
798 return 1;
799 }
800 }
801 return 0;
802 }
803
804 /* negative occurrence, we scan all the list then walk back */
805 if (-occ > MAX_HDR_HISTORY)
806 return 0;
807
808 found = hist_idx = 0;
809 while (http_find_header(htx, hdr, ctx, 0)) {
810 val_hist[hist_idx] = ctx->value;
811 if (++hist_idx >= MAX_HDR_HISTORY)
812 hist_idx = 0;
813 found++;
814 }
815 if (-occ > found)
816 return 0;
817
818 /* OK now we have the last occurrence in [hist_idx-1], and we need to
819 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
820 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
821 * to remain in the 0..9 range.
822 */
823 hist_idx += occ + MAX_HDR_HISTORY;
824 if (hist_idx >= MAX_HDR_HISTORY)
825 hist_idx -= MAX_HDR_HISTORY;
826 *vptr = val_hist[hist_idx].ptr;
827 *vlen = val_hist[hist_idx].len;
828 return 1;
829}
830
831/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
832 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
833 * performed over the whole headers. Otherwise it must contain a valid header
834 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
835 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
836 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
837 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
838 * -1. This function differs from http_get_hdr() in that it only returns full
839 * line header values and does not stop at commas.
840 * The return value is 0 if nothing was found, or non-zero otherwise.
841 */
842unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
843 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
844{
845 struct http_hdr_ctx local_ctx;
846 struct ist val_hist[MAX_HDR_HISTORY];
847 unsigned int hist_idx;
848 int found;
849
850 if (!ctx) {
851 local_ctx.blk = NULL;
852 ctx = &local_ctx;
853 }
854
855 if (occ >= 0) {
856 /* search from the beginning */
857 while (http_find_header(htx, hdr, ctx, 1)) {
858 occ--;
859 if (occ <= 0) {
860 *vptr = ctx->value.ptr;
861 *vlen = ctx->value.len;
862 return 1;
863 }
864 }
865 return 0;
866 }
867
868 /* negative occurrence, we scan all the list then walk back */
869 if (-occ > MAX_HDR_HISTORY)
870 return 0;
871
872 found = hist_idx = 0;
873 while (http_find_header(htx, hdr, ctx, 1)) {
874 val_hist[hist_idx] = ctx->value;
875 if (++hist_idx >= MAX_HDR_HISTORY)
876 hist_idx = 0;
877 found++;
878 }
879 if (-occ > found)
880 return 0;
881
882 /* OK now we have the last occurrence in [hist_idx-1], and we need to
883 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
884 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
885 * to remain in the 0..9 range.
886 */
887 hist_idx += occ + MAX_HDR_HISTORY;
888 if (hist_idx >= MAX_HDR_HISTORY)
889 hist_idx -= MAX_HDR_HISTORY;
890 *vptr = val_hist[hist_idx].ptr;
891 *vlen = val_hist[hist_idx].len;
892 return 1;
893}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100894
Christopher Fauleta66adf42020-11-05 22:43:41 +0100895int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100896{
897 struct htx *htx;
898 struct htx_sl *sl;
899 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200900 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100901 union h1_sl h1sl;
902 unsigned int flags = HTX_SL_F_IS_RESP;
903 int ret = 0;
904
Christopher Faulet90cc4812019-07-22 16:49:30 +0200905 b_reset(buf);
906 if (!raw.len) {
907 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100908 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200909 return 1;
910 }
911
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100912 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200913 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100914 if (!buf->area)
915 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100916
917 h1m_init_res(&h1m);
918 h1m.flags |= H1_MF_NO_PHDR;
919 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
920 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100921 if (ret <= 0) {
922 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100923 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100924 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100925
Christopher Fauleta66adf42020-11-05 22:43:41 +0100926 if (unlikely(h1sl.st.v.len != 8)) {
927 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100928 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100929 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100930 if ((*(h1sl.st.v.ptr + 5) > '1') ||
931 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
932 h1m.flags |= H1_MF_VER_11;
933
Christopher Fauleta66adf42020-11-05 22:43:41 +0100934 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
935 memprintf(errmsg, "invalid http status code for an error message (%u)",
936 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200937 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100938 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200939
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200940 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
941 /* Responses known to have no body. */
942 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
943 h1m.flags |= H1_MF_XFER_LEN;
944 h1m.curr_len = h1m.body_len = 0;
945 }
946 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
947 h1m.flags |= H1_MF_XFER_LEN;
948
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100949 if (h1m.flags & H1_MF_VER_11)
950 flags |= HTX_SL_F_VER_11;
951 if (h1m.flags & H1_MF_XFER_ENC)
952 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200953 if (h1m.flags & H1_MF_XFER_LEN) {
954 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100955 if (h1m.flags & H1_MF_CHNK) {
956 memprintf(errmsg, "chunk-encoded payload not supported");
957 goto error;
958 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200959 else if (h1m.flags & H1_MF_CLEN) {
960 flags |= HTX_SL_F_CLEN;
961 if (h1m.body_len == 0)
962 flags |= HTX_SL_F_BODYLESS;
963 }
964 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200965 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100966 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200967
Christopher Fauleta66adf42020-11-05 22:43:41 +0100968 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
969 memprintf(errmsg, "message payload not expected");
970 goto error;
971 }
972 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
973 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100974 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100975 goto error;
976 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100977
978 htx = htx_from_buf(buf);
979 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 +0100980 if (!sl || !htx_add_all_headers(htx, hdrs)) {
981 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100982 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100983 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100984 sl->info.res.status = h1sl.st.status;
985
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200986 while (raw.len > ret) {
987 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100988 if (!sent) {
989 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100990 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100991 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200992 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100993 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200994
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100995 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200996
Christopher Faulet90cc4812019-07-22 16:49:30 +0200997 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100998
999error:
1000 if (buf->size)
1001 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001002 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001003}
1004
Christopher Faulet18630642020-05-12 18:57:28 +02001005void release_http_reply(struct http_reply *http_reply)
1006{
1007 struct logformat_node *lf, *lfb;
1008 struct http_reply_hdr *hdr, *hdrb;
1009
1010 if (!http_reply)
1011 return;
1012
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001013 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001014 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001015 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001016 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001017 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001018 release_sample_expr(lf->expr);
1019 free(lf->arg);
1020 free(lf);
1021 }
1022 istfree(&hdr->name);
1023 free(hdr);
1024 }
1025
1026 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001027 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001028 }
1029 else if (http_reply->type == HTTP_REPLY_RAW)
1030 chunk_destroy(&http_reply->body.obj);
1031 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1032 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001033 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001034 release_sample_expr(lf->expr);
1035 free(lf->arg);
1036 free(lf);
1037 }
1038 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001039 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001040}
1041
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001042static int http_htx_init(void)
1043{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001044 struct buffer chk;
1045 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001046 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001047 int rc;
1048 int err_code = 0;
1049
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001050 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1051 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001052 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001053 err_code |= ERR_ALERT | ERR_FATAL;
1054 continue;
1055 }
1056
1057 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Fauleta66adf42020-11-05 22:43:41 +01001058 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1059 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1060 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001061 err_code |= ERR_ALERT | ERR_FATAL;
1062 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001063 else if (errmsg) {
1064 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1065 err_code |= ERR_WARN;
1066 }
1067
1068 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001069 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001070
Christopher Fauletf7346382019-07-17 22:02:08 +02001071 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001072 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1073 http_err_replies[rc].status = http_err_codes[rc];
1074 http_err_replies[rc].ctype = NULL;
1075 LIST_INIT(&http_err_replies[rc].hdrs);
1076 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001077 }
1078end:
1079 return err_code;
1080}
1081
Christopher Faulet58857752020-01-15 15:19:50 +01001082static void http_htx_deinit(void)
1083{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001084 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001085 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001086 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001087 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001088 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001089
1090 node = ebpt_first(&http_error_messages);
1091 while (node) {
1092 next = ebpt_next(node);
1093 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001094 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1095 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001096 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001097 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001098 node = next;
1099 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001100
1101 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1102 free(http_errs->conf.file);
1103 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001104 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1105 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001106 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001107 free(http_errs);
1108 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001109
1110 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001111 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001112 release_http_reply(http_rep);
1113 }
Christopher Faulet58857752020-01-15 15:19:50 +01001114}
1115
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001116REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001117REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001118
Christopher Faulet58857752020-01-15 15:19:50 +01001119/* Reads content of the error file <file> and convert it into an HTX message. On
1120 * success, the HTX message is returned. On error, NULL is returned and an error
1121 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001122 */
Christopher Faulet58857752020-01-15 15:19:50 +01001123struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001124{
Christopher Faulet58857752020-01-15 15:19:50 +01001125 struct buffer *buf = NULL;
1126 struct buffer chk;
1127 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001128 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001129 struct stat stat;
1130 char *err = NULL;
1131 int errnum, errlen;
1132 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001133
1134 /* already loaded */
1135 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1136 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001137 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1138 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001139 goto out;
1140 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001141
Christopher Faulet58857752020-01-15 15:19:50 +01001142 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001143 fd = open(file, O_RDONLY);
1144 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1145 memprintf(errmsg, "error opening file '%s'.", file);
1146 goto out;
1147 }
1148
1149 if (stat.st_size <= global.tune.bufsize)
1150 errlen = stat.st_size;
1151 else {
1152 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1153 file, global.tune.bufsize);
1154 errlen = global.tune.bufsize;
1155 }
1156
1157 err = malloc(errlen);
1158 if (!err) {
1159 memprintf(errmsg, "out of memory.");
1160 goto out;
1161 }
1162
1163 errnum = read(fd, err, errlen);
1164 if (errnum != errlen) {
1165 memprintf(errmsg, "error reading file '%s'.", file);
1166 goto out;
1167 }
1168
Christopher Faulet58857752020-01-15 15:19:50 +01001169 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001170 http_errmsg = calloc(1, sizeof(*http_errmsg));
1171 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001172 memprintf(errmsg, "out of memory.");
1173 goto out;
1174 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001175 http_errmsg->node.key = strdup(file);
1176 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001177 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001178 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001179 goto out;
1180 }
1181
1182 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001183 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1184 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001185 free(http_errmsg->node.key);
1186 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001187 goto out;
1188 }
1189
Christopher Faulet58857752020-01-15 15:19:50 +01001190 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001191 http_errmsg->msg = chk;
1192 ebis_insert(&http_error_messages, &http_errmsg->node);
1193 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001194
Christopher Faulet5031ef52020-01-15 11:22:07 +01001195 out:
1196 if (fd >= 0)
1197 close(fd);
1198 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001199 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001200}
1201
Ilya Shipitsind4259502020-04-08 01:07:56 +05001202/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001203 * message is returned. On error, NULL is returned and an error message is
1204 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001205 */
Christopher Faulet58857752020-01-15 15:19:50 +01001206struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001207{
Christopher Faulet58857752020-01-15 15:19:50 +01001208 struct buffer *buf = NULL;
1209 struct buffer chk;
1210 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001211 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001212
1213 /* already loaded */
1214 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1215 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001216 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1217 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001218 goto out;
1219 }
1220 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001221 http_errmsg = calloc(1, sizeof(*http_errmsg));
1222 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001223 memprintf(errmsg, "out of memory.");
1224 goto out;
1225 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001226 http_errmsg->node.key = strdup(key);
1227 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001228 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001229 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001230 goto out;
1231 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001232
1233 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001234 if (!http_str_to_htx(&chk, msg, errmsg)) {
1235 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001236 free(http_errmsg->node.key);
1237 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001238 goto out;
1239 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001240
Christopher Faulet58857752020-01-15 15:19:50 +01001241 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001242 http_errmsg->msg = chk;
1243 ebis_insert(&http_error_messages, &http_errmsg->node);
1244 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001245 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001246 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001247}
1248
Christopher Faulet5031ef52020-01-15 11:22:07 +01001249/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001250 * <status>. It returns NULL if there is any error, otherwise it return the
1251 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001252 */
Christopher Faulet58857752020-01-15 15:19:50 +01001253struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001254{
Christopher Faulet58857752020-01-15 15:19:50 +01001255 struct buffer *buf = NULL;
1256 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001257
1258 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1259 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001260 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001261 break;
1262 }
1263 }
1264
1265 if (rc >= HTTP_ERR_SIZE)
1266 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001267 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001268}
1269
1270/* This function creates HTX error message corresponding to a redirect message
1271 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001272 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1273 * returns NULL if there is any error, otherwise it return the corresponding HTX
1274 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001275 */
Christopher Faulet58857752020-01-15 15:19:50 +01001276struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001277{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001278 static const char *HTTP_302 =
1279 "HTTP/1.1 302 Found\r\n"
1280 "Cache-Control: no-cache\r\n"
1281 "Content-length: 0\r\n"
1282 "Location: "; /* not terminated since it will be concatenated with the URL */
1283 static const char *HTTP_303 =
1284 "HTTP/1.1 303 See Other\r\n"
1285 "Cache-Control: no-cache\r\n"
1286 "Content-length: 0\r\n"
1287 "Location: "; /* not terminated since it will be concatenated with the URL */
1288
Christopher Faulet58857752020-01-15 15:19:50 +01001289 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001290 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001291 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001292 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001293
1294 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1295 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001296 /* Create the error key */
1297 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1298 memprintf(errmsg, "out of memory.");
1299 goto out;
1300 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001301 /* Create the error message */
1302 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1303 errlen = strlen(msg) + strlen(url) + 5;
1304 err = malloc(errlen);
1305 if (!err) {
1306 memprintf(errmsg, "out of memory.");
1307 goto out;
1308 }
1309 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1310
1311 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001312 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001313 break;
1314 }
1315 }
1316
1317 if (rc >= HTTP_ERR_SIZE)
1318 memprintf(errmsg, "status code '%d' not handled.", status);
1319out:
Christopher Faulet58857752020-01-15 15:19:50 +01001320 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001321 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001322 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001323}
1324
Christopher Faulet7eea2412020-05-13 15:02:59 +02001325/* Check an "http reply" and, for replies referencing an http-errors section,
1326 * try to find the right section and the right error message in this section. If
1327 * found, the reply is updated. If the http-errors section exists but the error
1328 * message is not found, no error message is set to fallback on the default
1329 * ones. Otherwise (unknown section) an error is returned.
1330 *
1331 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1332 * filled.
1333 */
1334int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1335{
1336 struct http_errors *http_errs;
1337 int ret = 1;
1338
1339 if (reply->type != HTTP_REPLY_ERRFILES)
1340 goto end;
1341
1342 list_for_each_entry(http_errs, &http_errors_list, list) {
1343 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001344 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001345 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001346 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1347 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001348 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1349 "not declared in http-errors section '%s'.\n",
1350 px->id, reply->status, http_errs->id);
1351 break;
1352 }
1353 }
1354
1355 if (&http_errs->list == &http_errors_list) {
1356 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1357 reply->body.http_errors);
1358 ret = 0;
1359 }
1360
1361 end:
1362 return ret;
1363}
1364
Christopher Faulet47e791e2020-05-13 14:36:55 +02001365/* Parse an "http reply". It returns the reply on success or NULL on error. This
1366 * function creates one of the following http replies :
1367 *
1368 * - HTTP_REPLY_EMPTY : dummy response, no payload
1369 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1370 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1371 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1372 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1373 *
1374 * The content-type must be defined for non-empty payload. It is ignored for
1375 * error messages (implicit or explicit). When an http-errors section is
1376 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1377 * during the configuration validity check or dynamically. It is the caller
1378 * responsibility to choose. If no status code is configured, <default_status>
1379 * is set.
1380 */
1381struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1382 int default_status, char **errmsg)
1383{
1384 struct logformat_node *lf, *lfb;
1385 struct http_reply *reply = NULL;
1386 struct http_reply_hdr *hdr, *hdrb;
1387 struct stat stat;
1388 const char *act_arg = NULL;
1389 char *obj = NULL;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001390 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001391
1392
1393 reply = calloc(1, sizeof(*reply));
1394 if (!reply) {
1395 memprintf(errmsg, "out of memory");
1396 goto error;
1397 }
1398 LIST_INIT(&reply->hdrs);
1399 reply->type = HTTP_REPLY_EMPTY;
1400 reply->status = default_status;
1401
Christopher Faulet3b967c12020-05-15 15:47:44 +02001402 if (px->conf.args.ctx == ARGC_HERR)
1403 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001404 else {
1405 if (px->cap & PR_CAP_FE)
1406 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1407 if (px->cap & PR_CAP_BE)
Willy Tarreauad745102021-10-16 14:41:09 +02001408 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001409 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001410
1411 cur_arg = *orig_arg;
1412 while (*args[cur_arg]) {
1413 if (strcmp(args[cur_arg], "status") == 0) {
1414 cur_arg++;
1415 if (!*args[cur_arg]) {
1416 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1417 goto error;
1418 }
1419 reply->status = atol(args[cur_arg]);
1420 if (reply->status < 200 || reply->status > 599) {
1421 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1422 goto error;
1423 }
1424 cur_arg++;
1425 }
1426 else if (strcmp(args[cur_arg], "content-type") == 0) {
1427 cur_arg++;
1428 if (!*args[cur_arg]) {
1429 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1430 goto error;
1431 }
1432 free(reply->ctype);
1433 reply->ctype = strdup(args[cur_arg]);
1434 cur_arg++;
1435 }
1436 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1437 if (reply->type != HTTP_REPLY_EMPTY) {
1438 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1439 goto error;
1440 }
1441 act_arg = args[cur_arg];
1442 cur_arg++;
1443 if (!*args[cur_arg]) {
1444 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1445 goto error;
1446 }
1447 reply->body.http_errors = strdup(args[cur_arg]);
1448 if (!reply->body.http_errors) {
1449 memprintf(errmsg, "out of memory");
1450 goto error;
1451 }
1452 reply->type = HTTP_REPLY_ERRFILES;
1453 cur_arg++;
1454 }
1455 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1456 if (reply->type != HTTP_REPLY_EMPTY) {
1457 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1458 goto error;
1459 }
1460 act_arg = args[cur_arg];
1461 reply->type = HTTP_REPLY_ERRMSG;
1462 cur_arg++;
1463 }
1464 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1465 if (reply->type != HTTP_REPLY_EMPTY) {
1466 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1467 goto error;
1468 }
1469 act_arg = args[cur_arg];
1470 cur_arg++;
1471 if (!*args[cur_arg]) {
1472 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1473 goto error;
1474 }
1475 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1476 if (!reply->body.errmsg) {
1477 goto error;
1478 }
1479 reply->type = HTTP_REPLY_ERRMSG;
1480 cur_arg++;
1481 }
1482 else if (strcmp(args[cur_arg], "file") == 0) {
1483 if (reply->type != HTTP_REPLY_EMPTY) {
1484 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1485 goto error;
1486 }
1487 act_arg = args[cur_arg];
1488 cur_arg++;
1489 if (!*args[cur_arg]) {
1490 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1491 goto error;
1492 }
1493 fd = open(args[cur_arg], O_RDONLY);
1494 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1495 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1496 goto error;
1497 }
1498 if (stat.st_size > global.tune.bufsize) {
1499 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1500 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1501 goto error;
1502 }
1503 objlen = stat.st_size;
1504 obj = malloc(objlen);
1505 if (!obj || read(fd, obj, objlen) != objlen) {
1506 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1507 goto error;
1508 }
1509 close(fd);
1510 fd = -1;
1511 reply->type = HTTP_REPLY_RAW;
1512 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1513 obj = NULL;
1514 cur_arg++;
1515 }
1516 else if (strcmp(args[cur_arg], "string") == 0) {
1517 if (reply->type != HTTP_REPLY_EMPTY) {
1518 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1519 goto error;
1520 }
1521 act_arg = args[cur_arg];
1522 cur_arg++;
1523 if (!*args[cur_arg]) {
1524 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1525 goto error;
1526 }
1527 obj = strdup(args[cur_arg]);
1528 objlen = strlen(args[cur_arg]);
1529 if (!obj) {
1530 memprintf(errmsg, "out of memory");
1531 goto error;
1532 }
1533 reply->type = HTTP_REPLY_RAW;
1534 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1535 obj = NULL;
1536 cur_arg++;
1537 }
1538 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1539 if (reply->type != HTTP_REPLY_EMPTY) {
1540 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1541 goto error;
1542 }
1543 act_arg = args[cur_arg];
1544 cur_arg++;
1545 if (!*args[cur_arg]) {
1546 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1547 goto error;
1548 }
1549 fd = open(args[cur_arg], O_RDONLY);
1550 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1551 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1552 goto error;
1553 }
1554 if (stat.st_size > global.tune.bufsize) {
1555 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1556 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1557 goto error;
1558 }
1559 objlen = stat.st_size;
1560 obj = malloc(objlen + 1);
1561 if (!obj || read(fd, obj, objlen) != objlen) {
1562 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1563 goto error;
1564 }
1565 close(fd);
1566 fd = -1;
1567 obj[objlen] = '\0';
1568 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001569 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001570 cur_arg++;
1571 }
1572 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1573 if (reply->type != HTTP_REPLY_EMPTY) {
1574 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1575 goto error;
1576 }
1577 act_arg = args[cur_arg];
1578 cur_arg++;
1579 if (!*args[cur_arg]) {
1580 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1581 goto error;
1582 }
1583 obj = strdup(args[cur_arg]);
1584 objlen = strlen(args[cur_arg]);
1585 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001586 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001587 cur_arg++;
1588 }
1589 else if (strcmp(args[cur_arg], "hdr") == 0) {
1590 cur_arg++;
1591 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1592 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1593 goto error;
1594 }
1595 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1596 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1597 strcasecmp(args[cur_arg], "content-type") == 0) {
1598 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1599 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1600 cur_arg += 2;
1601 continue;
1602 }
1603 hdr = calloc(1, sizeof(*hdr));
1604 if (!hdr) {
1605 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1606 goto error;
1607 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001608 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001609 LIST_INIT(&hdr->value);
1610 hdr->name = ist(strdup(args[cur_arg]));
1611 if (!isttest(hdr->name)) {
1612 memprintf(errmsg, "out of memory");
1613 goto error;
1614 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001615 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1616 goto error;
1617
1618 free(px->conf.lfs_file);
1619 px->conf.lfs_file = strdup(px->conf.args.file);
1620 px->conf.lfs_line = px->conf.args.line;
1621 cur_arg += 2;
1622 }
1623 else
1624 break;
1625 }
1626
1627 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1628 if (reply->ctype) {
1629 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1630 " neither errorfile nor payload defined.\n",
1631 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001632 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001633 }
1634 }
1635 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1636
1637 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1638 /* default errorfile or errorfiles: check the status */
1639 int rc;
1640
1641 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1642 if (http_err_codes[rc] == reply->status)
1643 break;
1644 }
1645
1646 if (rc >= HTTP_ERR_SIZE) {
1647 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1648 reply->status, act_arg);
1649 goto error;
1650 }
1651 }
1652
1653 if (reply->ctype) {
1654 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1655 "with an erorrfile.\n",
1656 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001657 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001658 }
1659 if (!LIST_ISEMPTY(&reply->hdrs)) {
1660 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1661 "with an erorrfile.\n",
1662 px->conf.args.file, px->conf.args.line);
1663 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001664 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001665 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001666 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001667 release_sample_expr(lf->expr);
1668 free(lf->arg);
1669 free(lf);
1670 }
1671 istfree(&hdr->name);
1672 free(hdr);
1673 }
1674 }
1675 }
1676 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001677 if ((reply->status == 204 || reply->status == 304) && objlen) {
1678 memprintf(errmsg, "No body expected for %d responses", reply->status);
1679 goto error;
1680 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001681 if (!reply->ctype && objlen) {
1682 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1683 goto error;
1684 }
1685 if (reply->ctype && !b_data(&reply->body.obj)) {
1686 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001687 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001688 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001689 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001690 }
1691 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1692 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1693 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1694 px->conf.args.file, px->conf.args.line);
1695 }
1696 }
1697 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1698 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001699 if ((reply->status == 204 || reply->status == 304)) {
1700 memprintf(errmsg, "No body expected for %d responses", reply->status);
1701 goto error;
1702 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001703 if (!reply->ctype) {
1704 memprintf(errmsg, "a content type must be defined with a log-format payload");
1705 goto error;
1706 }
1707 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1708 goto error;
1709
1710 free(px->conf.lfs_file);
1711 px->conf.lfs_file = strdup(px->conf.args.file);
1712 px->conf.lfs_line = px->conf.args.line;
1713 }
1714
1715 free(obj);
1716 *orig_arg = cur_arg;
1717 return reply;
1718
1719 error:
1720 free(obj);
1721 if (fd >= 0)
1722 close(fd);
1723 release_http_reply(reply);
1724 return NULL;
1725}
1726
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001727/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1728 * Returns 0 if no error has been found else non-zero.
1729 *
1730 * The normalization is processed on the target-uri at the condition that it is
1731 * in absolute-form. In the case where the target-uri was normalized, every
1732 * host headers values found are also replaced by the normalized hostname. This
1733 * assumes that the target-uri and host headers were properly identify as
1734 * similar before calling this function.
1735 */
1736int http_scheme_based_normalize(struct htx *htx)
1737{
1738 struct http_hdr_ctx ctx;
1739 struct htx_sl *sl;
1740 struct ist uri, scheme, authority, host, port;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001741
1742 sl = http_get_stline(htx);
1743
1744 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1745 return 0;
1746
1747 uri = htx_sl_req_uri(sl);
1748
1749 scheme = http_get_scheme(uri);
1750 /* if no scheme found, no normalization to proceed */
1751 if (!isttest(scheme))
1752 return 0;
1753
Christopher Fauletbe50b142022-07-05 10:24:52 +02001754 /* Extract the port if present in authority */
1755 authority = http_get_authority(uri, 1);
1756 port = http_get_host_port(authority);
1757 if (!isttest(port)) {
1758 /* if no port found, no normalization to proceed */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001759 return 0;
Christopher Fauletbe50b142022-07-05 10:24:52 +02001760 }
1761 host = isttrim(authority, istlen(authority) - istlen(port) - 1);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001762
Christopher Fauletbe50b142022-07-05 10:24:52 +02001763 if (istlen(port) && http_is_default_port(scheme, port)) {
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001764 /* reconstruct the uri with removal of the port */
1765 struct buffer *temp = get_trash_chunk();
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001766 struct ist meth, vsn;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001767
1768 /* meth */
1769 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1770 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1771
1772 /* vsn */
1773 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1774 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1775
1776 /* reconstruct uri without port */
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001777 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001778 chunk_istcat(temp, host);
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001779 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
1780 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001781
1782 http_replace_stline(htx, meth, uri, vsn);
1783
1784 /* replace every host headers values by the normalized host */
1785 ctx.blk = NULL;
1786 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1787 if (!http_replace_header_value(htx, &ctx, host))
1788 goto fail;
1789 }
1790 }
1791
1792 return 0;
1793
1794 fail:
1795 return 1;
1796}
1797
Christopher Faulet07f41f72020-01-16 16:16:06 +01001798/* Parses the "errorloc[302|303]" proxy keyword */
1799static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001800 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001801 char **errmsg)
1802{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001803 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001804 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001805 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001806 int errloc, status;
1807 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001808
1809 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1810 ret = 1;
1811 goto out;
1812 }
1813
1814 if (*(args[1]) == 0 || *(args[2]) == 0) {
1815 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1816 ret = -1;
1817 goto out;
1818 }
1819
1820 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001821 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001822 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1823 if (!msg) {
1824 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1825 ret = -1;
1826 goto out;
1827 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001828
1829 reply = calloc(1, sizeof(*reply));
1830 if (!reply) {
1831 memprintf(errmsg, "%s : out of memory.", args[0]);
1832 ret = -1;
1833 goto out;
1834 }
1835 reply->type = HTTP_REPLY_ERRMSG;
1836 reply->status = status;
1837 reply->ctype = NULL;
1838 LIST_INIT(&reply->hdrs);
1839 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001840 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001841
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001842 conf_err = calloc(1, sizeof(*conf_err));
1843 if (!conf_err) {
1844 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001845 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001846 ret = -1;
1847 goto out;
1848 }
1849 conf_err->type = 1;
1850 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001851 conf_err->info.errorfile.reply = reply;
1852
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001853 conf_err->file = strdup(file);
1854 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001855 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001856
Christopher Fauleta66adf42020-11-05 22:43:41 +01001857 /* handle warning message */
1858 if (*errmsg)
1859 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001860 out:
1861 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001862
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001863}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001864
1865/* Parses the "errorfile" proxy keyword */
1866static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001867 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001868 char **errmsg)
1869{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001870 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001871 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001872 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001873 int status;
1874 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001875
1876 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1877 ret = 1;
1878 goto out;
1879 }
1880
1881 if (*(args[1]) == 0 || *(args[2]) == 0) {
1882 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1883 ret = -1;
1884 goto out;
1885 }
1886
1887 status = atol(args[1]);
1888 msg = http_parse_errorfile(status, args[2], errmsg);
1889 if (!msg) {
1890 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1891 ret = -1;
1892 goto out;
1893 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001894
Christopher Faulet5809e102020-05-14 17:31:52 +02001895 reply = calloc(1, sizeof(*reply));
1896 if (!reply) {
1897 memprintf(errmsg, "%s : out of memory.", args[0]);
1898 ret = -1;
1899 goto out;
1900 }
1901 reply->type = HTTP_REPLY_ERRMSG;
1902 reply->status = status;
1903 reply->ctype = NULL;
1904 LIST_INIT(&reply->hdrs);
1905 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001906 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001907
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001908 conf_err = calloc(1, sizeof(*conf_err));
1909 if (!conf_err) {
1910 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001911 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001912 ret = -1;
1913 goto out;
1914 }
1915 conf_err->type = 1;
1916 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001917 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001918 conf_err->file = strdup(file);
1919 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001920 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001921
Christopher Fauleta66adf42020-11-05 22:43:41 +01001922 /* handle warning message */
1923 if (*errmsg)
1924 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001925 out:
1926 return ret;
1927
1928}
1929
1930/* Parses the "errorfiles" proxy keyword */
1931static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001932 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001933 char **err)
1934{
1935 struct conf_errors *conf_err = NULL;
1936 char *name = NULL;
1937 int rc, ret = 0;
1938
1939 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1940 ret = 1;
1941 goto out;
1942 }
1943
1944 if (!*(args[1])) {
1945 memprintf(err, "%s : expects <name> as argument.", args[0]);
1946 ret = -1;
1947 goto out;
1948 }
1949
1950 name = strdup(args[1]);
1951 conf_err = calloc(1, sizeof(*conf_err));
1952 if (!name || !conf_err) {
1953 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001954 goto error;
1955 }
1956 conf_err->type = 0;
1957
1958 conf_err->info.errorfiles.name = name;
1959 if (!*(args[2])) {
1960 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1961 conf_err->info.errorfiles.status[rc] = 1;
1962 }
1963 else {
1964 int cur_arg, status;
1965 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1966 status = atol(args[cur_arg]);
1967
1968 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1969 if (http_err_codes[rc] == status) {
1970 conf_err->info.errorfiles.status[rc] = 2;
1971 break;
1972 }
1973 }
1974 if (rc >= HTTP_ERR_SIZE) {
1975 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001976 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001977 }
1978 }
1979 }
1980 conf_err->file = strdup(file);
1981 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001982 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001983 out:
1984 return ret;
1985
1986 error:
1987 free(name);
1988 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001989 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001990 goto out;
1991}
1992
Christopher Faulet3b967c12020-05-15 15:47:44 +02001993/* Parses the "http-error" proxy keyword */
1994static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001995 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02001996 char **errmsg)
1997{
1998 struct conf_errors *conf_err;
1999 struct http_reply *reply = NULL;
2000 int rc, cur_arg, ret = 0;
2001
2002 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2003 ret = 1;
2004 goto out;
2005 }
2006
2007 cur_arg = 1;
2008 curpx->conf.args.ctx = ARGC_HERR;
2009 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2010 if (!reply) {
2011 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2012 goto error;
2013 }
2014 else if (!reply->status) {
2015 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2016 goto error;
2017 }
2018
2019 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2020 if (http_err_codes[rc] == reply->status)
2021 break;
2022 }
2023
2024 if (rc >= HTTP_ERR_SIZE) {
2025 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2026 goto error;
2027 }
2028 if (*args[cur_arg]) {
2029 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2030 goto error;
2031 }
2032
2033 conf_err = calloc(1, sizeof(*conf_err));
2034 if (!conf_err) {
2035 memprintf(errmsg, "%s : out of memory.", args[0]);
2036 goto error;
2037 }
2038 if (reply->type == HTTP_REPLY_ERRFILES) {
2039 int rc = http_get_status_idx(reply->status);
2040
2041 conf_err->type = 2;
2042 conf_err->info.errorfiles.name = reply->body.http_errors;
2043 conf_err->info.errorfiles.status[rc] = 2;
2044 reply->body.http_errors = NULL;
2045 release_http_reply(reply);
2046 }
2047 else {
2048 conf_err->type = 1;
2049 conf_err->info.errorfile.status = reply->status;
2050 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002051 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002052 }
2053 conf_err->file = strdup(file);
2054 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002055 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002056
Christopher Faulet3005d282020-11-13 10:58:01 +01002057 /* handle warning message */
2058 if (*errmsg)
2059 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002060 out:
2061 return ret;
2062
2063 error:
2064 release_http_reply(reply);
2065 ret = -1;
2066 goto out;
2067
2068}
2069
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002070/* Check "errorfiles" proxy keyword */
2071static int proxy_check_errors(struct proxy *px)
2072{
2073 struct conf_errors *conf_err, *conf_err_back;
2074 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002075 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002076
2077 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2078 if (conf_err->type == 1) {
2079 /* errorfile */
2080 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002081 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002082
2083 /* For proxy, to rely on default replies, just don't reference a reply */
2084 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2085 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002086 }
2087 else {
2088 /* errorfiles */
2089 list_for_each_entry(http_errs, &http_errors_list, list) {
2090 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2091 break;
2092 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002093
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002094 /* unknown http-errors section */
2095 if (&http_errs->list == &http_errors_list) {
2096 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2097 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2098 err |= ERR_ALERT | ERR_FATAL;
2099 free(conf_err->info.errorfiles.name);
2100 goto next;
2101 }
2102
2103 free(conf_err->info.errorfiles.name);
2104 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2105 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002106 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002107 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002108 else if (conf_err->info.errorfiles.status[rc] == 2)
2109 ha_warning("config: proxy '%s' : status '%d' not declared in"
2110 " http-errors section '%s' (at %s:%d).\n",
2111 px->id, http_err_codes[rc], http_errs->id,
2112 conf_err->file, conf_err->line);
2113 }
2114 }
2115 }
2116 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002117 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002118 free(conf_err->file);
2119 free(conf_err);
2120 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002121
2122 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002123 return err;
2124}
2125
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002126static int post_check_errors()
2127{
2128 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002129 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002130 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002131 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002132
2133 node = ebpt_first(&http_error_messages);
2134 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002135 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2136 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002137 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002138 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002139 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2140 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002141 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002142 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002143 (char *)node->key);
2144 err_code |= ERR_WARN;
2145 }
2146 next:
2147 node = ebpt_next(node);
2148 }
2149
2150 return err_code;
2151}
2152
Willy Tarreau016255a2021-02-12 08:40:29 +01002153int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002154{
2155 struct conf_errors *conf_err, *new_conf_err = NULL;
2156 int ret = 0;
2157
2158 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2159 new_conf_err = calloc(1, sizeof(*new_conf_err));
2160 if (!new_conf_err) {
2161 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2162 goto out;
2163 }
2164 new_conf_err->type = conf_err->type;
2165 if (conf_err->type == 1) {
2166 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002167 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002168 }
2169 else {
2170 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2171 if (!new_conf_err->info.errorfiles.name) {
2172 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2173 goto out;
2174 }
2175 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2176 sizeof(conf_err->info.errorfiles.status));
2177 }
2178 new_conf_err->file = strdup(conf_err->file);
2179 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002180 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002181 new_conf_err = NULL;
2182 }
2183 ret = 1;
2184
2185 out:
2186 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002187 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002188}
2189
2190void proxy_release_conf_errors(struct proxy *px)
2191{
2192 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002193
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002194 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2195 if (conf_err->type == 0)
2196 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002197 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002198 free(conf_err->file);
2199 free(conf_err);
2200 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002201}
2202
2203/*
2204 * Parse an <http-errors> section.
2205 * Returns the error code, 0 if OK, or any combination of :
2206 * - ERR_ABORT: must abort ASAP
2207 * - ERR_FATAL: we can continue parsing but not start the service
2208 * - ERR_WARN: a warning has been emitted
2209 * - ERR_ALERT: an alert has been emitted
2210 * Only the two first ones can stop processing, the two others are just
2211 * indicators.
2212 */
2213static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2214{
2215 static struct http_errors *curr_errs = NULL;
2216 int err_code = 0;
2217 const char *err;
2218 char *errmsg = NULL;
2219
2220 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2221 if (!*args[1]) {
2222 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2223 err_code |= ERR_ALERT | ERR_ABORT;
2224 goto out;
2225 }
2226
2227 err = invalid_char(args[1]);
2228 if (err) {
2229 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2230 file, linenum, *err, args[0], args[1]);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 }
2233
2234 list_for_each_entry(curr_errs, &http_errors_list, list) {
2235 /* Error if two errors section owns the same name */
2236 if (strcmp(curr_errs->id, args[1]) == 0) {
2237 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2238 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 }
2241 }
2242
2243 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2244 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2245 err_code |= ERR_ALERT | ERR_ABORT;
2246 goto out;
2247 }
2248
Willy Tarreau2b718102021-04-21 07:32:39 +02002249 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002250 curr_errs->id = strdup(args[1]);
2251 curr_errs->conf.file = strdup(file);
2252 curr_errs->conf.line = linenum;
2253 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002254 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002255 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002256 struct buffer *msg;
2257 int status, rc;
2258
2259 if (*(args[1]) == 0 || *(args[2]) == 0) {
2260 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2261 file, linenum, args[0]);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265
2266 status = atol(args[1]);
2267 msg = http_parse_errorfile(status, args[2], &errmsg);
2268 if (!msg) {
2269 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2270 err_code |= ERR_ALERT | ERR_FATAL;
2271 goto out;
2272 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002273 if (errmsg) {
2274 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2275 err_code |= ERR_WARN;
2276 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002277
2278 reply = calloc(1, sizeof(*reply));
2279 if (!reply) {
2280 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284 reply->type = HTTP_REPLY_ERRMSG;
2285 reply->status = status;
2286 reply->ctype = NULL;
2287 LIST_INIT(&reply->hdrs);
2288 reply->body.errmsg = msg;
2289
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002290 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002291 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002292 }
2293 else if (*args[0] != 0) {
2294 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2295 err_code |= ERR_ALERT | ERR_FATAL;
2296 goto out;
2297 }
2298
2299out:
2300 free(errmsg);
2301 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002302}
2303
2304static struct cfg_kw_list cfg_kws = {ILH, {
2305 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2306 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2307 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2308 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002309 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002310 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002311 { 0, NULL, NULL },
2312}};
2313
2314INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002315REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002316REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002317
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002318REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2319
Christopher Faulet29f72842019-12-11 15:52:32 +01002320/************************************************************************/
2321/* HTX sample fetches */
2322/************************************************************************/
2323
2324/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2325static int
2326smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2327{
2328 if (!smp->strm)
2329 return 0;
2330
2331 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2332 smp->data.type = SMP_T_BOOL;
2333 return 1;
2334}
2335
2336/* Returns the number of blocks in an HTX message. The channel is chosen
2337 * depending on the sample direction. */
2338static int
2339smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2340{
2341 struct channel *chn;
2342 struct htx *htx;
2343
2344 if (!smp->strm)
2345 return 0;
2346
2347 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002348 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002349 if (!htx)
2350 return 0;
2351
2352 smp->data.u.sint = htx_nbblks(htx);
2353 smp->data.type = SMP_T_SINT;
2354 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2355 return 1;
2356}
2357
2358/* Returns the size of an HTX message. The channel is chosen depending on the
2359 * sample direction. */
2360static int
2361smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2362{
2363 struct channel *chn;
2364 struct htx *htx;
2365
2366 if (!smp->strm)
2367 return 0;
2368
2369 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002370 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002371 if (!htx)
2372 return 0;
2373
2374 smp->data.u.sint = htx->size;
2375 smp->data.type = SMP_T_SINT;
2376 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2377 return 1;
2378}
2379
2380/* Returns the data size of an HTX message. The channel is chosen depending on the
2381 * sample direction. */
2382static int
2383smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2384{
2385 struct channel *chn;
2386 struct htx *htx;
2387
2388 if (!smp->strm)
2389 return 0;
2390
2391 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002392 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002393 if (!htx)
2394 return 0;
2395
2396 smp->data.u.sint = htx->data;
2397 smp->data.type = SMP_T_SINT;
2398 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2399 return 1;
2400}
2401
2402/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2403 * depending on the sample direction. */
2404static int
2405smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2406{
2407 struct channel *chn;
2408 struct htx *htx;
2409
2410 if (!smp->strm)
2411 return 0;
2412
2413 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002414 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002415 if (!htx)
2416 return 0;
2417
2418 smp->data.u.sint = htx_used_space(htx);
2419 smp->data.type = SMP_T_SINT;
2420 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2421 return 1;
2422}
2423
2424/* Returns the free space (size-used) of an HTX message. The channel is chosen
2425 * depending on the sample direction. */
2426static int
2427smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2428{
2429 struct channel *chn;
2430 struct htx *htx;
2431
2432 if (!smp->strm)
2433 return 0;
2434
2435 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002436 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002437 if (!htx)
2438 return 0;
2439
2440 smp->data.u.sint = htx_free_space(htx);
2441 smp->data.type = SMP_T_SINT;
2442 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2443 return 1;
2444}
2445
2446/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2447 * channel is chosen depending on the sample direction. */
2448static int
2449smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2450{
2451 struct channel *chn;
2452 struct htx *htx;
2453
2454 if (!smp->strm)
2455 return 0;
2456
2457 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002458 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002459 if (!htx)
2460 return 0;
2461
2462 smp->data.u.sint = htx_free_data_space(htx);
2463 smp->data.type = SMP_T_SINT;
2464 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2465 return 1;
2466}
2467
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002468/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2469 * channel is chosen depending on the sample direction.
2470 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002471static int
2472smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2473{
2474 struct channel *chn;
2475 struct htx *htx;
2476
2477 if (!smp->strm)
2478 return 0;
2479
2480 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002481 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002482 if (!htx)
2483 return 0;
2484
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002485 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002486 smp->data.type = SMP_T_BOOL;
2487 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2488 return 1;
2489}
2490
2491/* Returns the type of a specific HTX block, if found in the message. Otherwise
2492 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2493 * "head", "tail" or "first". The channel is chosen depending on the sample
2494 * direction. */
2495static int
2496smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2497{
2498 struct channel *chn;
2499 struct htx *htx;
2500 enum htx_blk_type type;
2501 int32_t pos;
2502
2503 if (!smp->strm || !arg_p)
2504 return 0;
2505
2506 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002507 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002508 if (!htx)
2509 return 0;
2510
2511 pos = arg_p[0].data.sint;
2512 if (pos == -1)
2513 type = htx_get_head_type(htx);
2514 else if (pos == -2)
2515 type = htx_get_tail_type(htx);
2516 else if (pos == -3)
2517 type = htx_get_first_type(htx);
2518 else
2519 type = ((pos >= htx->head && pos <= htx->tail)
2520 ? htx_get_blk_type(htx_get_blk(htx, pos))
2521 : HTX_BLK_UNUSED);
2522
2523 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2524 smp->data.type = SMP_T_STR;
2525 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2526 return 1;
2527}
2528
2529/* Returns the size of a specific HTX block, if found in the message. Otherwise
2530 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2531 * "first". The channel is chosen depending on the sample direction. */
2532static int
2533smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2534{
2535 struct channel *chn;
2536 struct htx *htx;
2537 struct htx_blk *blk;
2538 int32_t pos;
2539
2540 if (!smp->strm || !arg_p)
2541 return 0;
2542
2543 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002544 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002545 if (!htx)
2546 return 0;
2547
2548 pos = arg_p[0].data.sint;
2549 if (pos == -1)
2550 blk = htx_get_head_blk(htx);
2551 else if (pos == -2)
2552 blk = htx_get_tail_blk(htx);
2553 else if (pos == -3)
2554 blk = htx_get_first_blk(htx);
2555 else
2556 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2557
2558 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2559 smp->data.type = SMP_T_SINT;
2560 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2561 return 1;
2562}
2563
2564/* Returns the start-line if the selected HTX block exists and is a
2565 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2566 * supported or "head", "tail" or "first". The channel is chosen depending on
2567 * the sample direction. */
2568static int
2569smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2570{
2571 struct buffer *temp;
2572 struct channel *chn;
2573 struct htx *htx;
2574 struct htx_blk *blk;
2575 struct htx_sl *sl;
2576 int32_t pos;
2577
2578 if (!smp->strm || !arg_p)
2579 return 0;
2580
2581 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002582 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002583 if (!htx)
2584 return 0;
2585
2586 pos = arg_p[0].data.sint;
2587 if (pos == -1)
2588 blk = htx_get_head_blk(htx);
2589 else if (pos == -2)
2590 blk = htx_get_tail_blk(htx);
2591 else if (pos == -3)
2592 blk = htx_get_first_blk(htx);
2593 else
2594 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2595
2596 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2597 smp->data.u.str.size = 0;
2598 smp->data.u.str.area = "";
2599 smp->data.u.str.data = 0;
2600 }
2601 else {
2602 sl = htx_get_blk_ptr(htx, blk);
2603
2604 temp = get_trash_chunk();
2605 chunk_istcat(temp, htx_sl_p1(sl));
2606 temp->area[temp->data++] = ' ';
2607 chunk_istcat(temp, htx_sl_p2(sl));
2608 temp->area[temp->data++] = ' ';
2609 chunk_istcat(temp, htx_sl_p3(sl));
2610
2611 smp->data.u.str = *temp;
2612 }
2613
2614 smp->data.type = SMP_T_STR;
2615 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2616 return 1;
2617}
2618
2619/* Returns the header name if the selected HTX block exists and is a header or a
2620 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2621 * supported or "head", "tail" or "first". The channel is chosen depending on
2622 * the sample direction. */
2623static int
2624smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2625{
2626 struct channel *chn;
2627 struct htx *htx;
2628 struct htx_blk *blk;
2629 int32_t pos;
2630
2631 if (!smp->strm || !arg_p)
2632 return 0;
2633
2634 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002635 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002636 if (!htx)
2637 return 0;
2638
2639 pos = arg_p[0].data.sint;
2640 if (pos == -1)
2641 blk = htx_get_head_blk(htx);
2642 else if (pos == -2)
2643 blk = htx_get_tail_blk(htx);
2644 else if (pos == -3)
2645 blk = htx_get_first_blk(htx);
2646 else
2647 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2648
2649 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2650 smp->data.u.str.size = 0;
2651 smp->data.u.str.area = "";
2652 smp->data.u.str.data = 0;
2653 }
2654 else {
2655 struct ist name = htx_get_blk_name(htx, blk);
2656
2657 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2658 }
2659 smp->data.type = SMP_T_STR;
2660 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2661 return 1;
2662}
2663
2664/* Returns the header value if the selected HTX block exists and is a header or
2665 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2666 * supported or "head", "tail" or "first". The channel is chosen depending on
2667 * the sample direction. */
2668static int
2669smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2670{
2671 struct channel *chn;
2672 struct htx *htx;
2673 struct htx_blk *blk;
2674 int32_t pos;
2675
2676 if (!smp->strm || !arg_p)
2677 return 0;
2678
2679 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002680 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002681 if (!htx)
2682 return 0;
2683
2684 pos = arg_p[0].data.sint;
2685 if (pos == -1)
2686 blk = htx_get_head_blk(htx);
2687 else if (pos == -2)
2688 blk = htx_get_tail_blk(htx);
2689 else if (pos == -3)
2690 blk = htx_get_first_blk(htx);
2691 else
2692 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2693
2694 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2695 smp->data.u.str.size = 0;
2696 smp->data.u.str.area = "";
2697 smp->data.u.str.data = 0;
2698 }
2699 else {
2700 struct ist val = htx_get_blk_value(htx, blk);
2701
2702 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2703 }
2704 smp->data.type = SMP_T_STR;
2705 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2706 return 1;
2707}
2708
2709/* Returns the value if the selected HTX block exists and is a data
2710 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2711 * or "head", "tail" or "first". The channel is chosen depending on the sample
2712 * direction. */
2713static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002714smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002715{
2716 struct channel *chn;
2717 struct htx *htx;
2718 struct htx_blk *blk;
2719 int32_t pos;
2720
2721 if (!smp->strm || !arg_p)
2722 return 0;
2723
2724 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002725 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002726 if (!htx)
2727 return 0;
2728
2729 pos = arg_p[0].data.sint;
2730 if (pos == -1)
2731 blk = htx_get_head_blk(htx);
2732 else if (pos == -2)
2733 blk = htx_get_tail_blk(htx);
2734 else if (pos == -3)
2735 blk = htx_get_first_blk(htx);
2736 else
2737 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2738
2739 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2740 smp->data.u.str.size = 0;
2741 smp->data.u.str.area = "";
2742 smp->data.u.str.data = 0;
2743 }
2744 else {
2745 struct ist val = htx_get_blk_value(htx, blk);
2746
2747 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2748 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002749 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002750 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2751 return 1;
2752}
2753
2754/* This function is used to validate the arguments passed to any "htx_blk" fetch
2755 * keywords. An argument is expected by these keywords. It must be a positive
2756 * integer or on of the following strings: "head", "tail" or "first". It returns
2757 * 0 on error, and a non-zero value if OK.
2758 */
2759int val_blk_arg(struct arg *arg, char **err_msg)
2760{
2761 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2762 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2763 return 0;
2764 }
2765 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002766 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002767 arg[0].type = ARGT_SINT;
2768 arg[0].data.sint = -1;
2769 }
2770 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002771 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002772 arg[0].type = ARGT_SINT;
2773 arg[0].data.sint = -2;
2774 }
2775 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002776 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002777 arg[0].type = ARGT_SINT;
2778 arg[0].data.sint = -3;
2779 }
2780 else {
2781 int pos;
2782
2783 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002784 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002785 memprintf(err_msg, "invalid block position");
2786 return 0;
2787 }
2788 }
2789
2790 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2791 if (pos < 0) {
2792 memprintf(err_msg, "block position must not be negative");
2793 return 0;
2794 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002795 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002796 arg[0].type = ARGT_SINT;
2797 arg[0].data.sint = pos;
2798 }
2799
2800 return 1;
2801}
2802
2803
2804/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002805 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002806 */
2807static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002808 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002809
Christopher Faulet01f44452020-01-08 14:23:40 +01002810 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2811 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2812 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2813 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2814 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2815 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2816 { "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 +01002817
Christopher Faulet01f44452020-01-08 14:23:40 +01002818 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2819 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2820 { "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},
2821 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2822 { "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 +01002823 { "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 +01002824
2825 { /* END */ },
2826}};
2827
2828INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);