blob: 63023a88f0f6853d12c234b48e47c4da8fd20262 [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +020014#include <ctype.h>
Christopher Faulet5031ef52020-01-15 11:22:07 +010015#include <fcntl.h>
16#include <unistd.h>
17
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020022#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/log.h>
28#include <haproxy/regex.h>
29#include <haproxy/sample.h>
Willy Tarreau4cbf62d2021-05-08 13:01:23 +020030#include <haproxy/tools.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020031
Christopher Faulet47596d32018-10-22 09:17:28 +020032
Christopher Fauletf7346382019-07-17 22:02:08 +020033struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020034struct http_reply http_err_replies[HTTP_ERR_SIZE];
35
Christopher Faulet58857752020-01-15 15:19:50 +010036struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010037struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020038struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010039
Christopher Faulet76edc0f2020-01-13 15:52:01 +010040/* The declaration of an errorfiles/errorfile directives. Used during config
41 * parsing only. */
42struct conf_errors {
43 char type; /* directive type (0: errorfiles, 1: errorfile) */
44 union {
45 struct {
46 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020047 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010048 } errorfile; /* describe an "errorfile" directive */
49 struct {
50 char *name; /* the http-errors section name */
51 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
52 } errorfiles; /* describe an "errorfiles" directive */
53 } info;
54
55 char *file; /* file where the directive appears */
56 int line; /* line where the directive appears */
57
58 struct list list; /* next conf_errors */
59};
60
Christopher Faulet297fbb42019-05-13 14:41:27 +020061/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020062 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020063 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020064 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020065struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020066{
Christopher Faulet297fbb42019-05-13 14:41:27 +020067 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010068
Christopher Faulet29f17582019-05-23 11:03:26 +020069 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020070 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL))
Christopher Faulet297fbb42019-05-13 14:41:27 +020071 return NULL;
72 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100162 if (type == HTX_BLK_EOH)
Christopher Faulet573fe732018-11-28 16:55:12 +0100163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000204 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
Christopher Faulet26590532022-11-22 18:02:00 +0100391 ALREADY_CHECKED(sl); /* the stline exists because http_replace_stline() succeded */
392
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200393 if (!http_update_host(htx, sl, uri))
394 goto fail;
395
396 return 1;
397 fail:
398 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200399}
400
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200401/* Replace the request path in the HTX message <htx> by <path>. The host part is
402 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
403 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
404 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200405 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200406int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200407{
408 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200409 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100410 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200411 size_t plen = 0;
412
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100413 if (!sl)
414 return 0;
415
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100416 uri = htx_sl_req_uri(sl);
417 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100418 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100419 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200420 if (with_qs)
421 plen = p.len;
422 else {
423 while (plen < p.len && *(p.ptr + plen) != '?')
424 plen++;
425 }
Christopher Faulete010c802018-10-24 10:36:45 +0200426
427 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100428 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
429 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200430
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100431 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
432 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
433
434 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200435 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
436 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100437 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200438
439 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100440 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200441}
442
443/* Replace the request query-string in the HTX message <htx> by <query>. The
444 * host part and the path are preserved. It returns 1 on success, otherwise
445 * 0.
446 */
447int http_replace_req_query(struct htx *htx, const struct ist query)
448{
449 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200450 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100451 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200452 int offset = 1;
453
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100454 if (!sl)
455 return 0;
456
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100457 uri = htx_sl_req_uri(sl);
458 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200459 while (q.len > 0 && *(q.ptr) != '?') {
460 q.ptr++;
461 q.len--;
462 }
463
464 /* skip the question mark or indicate that we must insert it
465 * (but only if the format string is not empty then).
466 */
467 if (q.len) {
468 q.ptr++;
469 q.len--;
470 }
471 else if (query.len > 1)
472 offset = 0;
473
474 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
476 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200477
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100478 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
479 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200480
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100481 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
482 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
483 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200484
485 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100486 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200487}
488
489/* Replace the response status in the HTX message <htx> by <status>. It returns
490 * 1 on success, otherwise 0.
491*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200492int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200493{
494 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200495 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200496 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200497
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100498 if (!sl)
499 return 0;
500
Christopher Faulete010c802018-10-24 10:36:45 +0200501 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100502 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
503 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200504 r = reason;
505 if (!isttest(r)) {
506 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
507 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
508 }
Christopher Faulete010c802018-10-24 10:36:45 +0200509
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100510 /* create the new start line */
511 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200512 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200513}
514
515/* Replace the response reason in the HTX message <htx> by <reason>. It returns
516 * 1 on success, otherwise 0.
517*/
518int http_replace_res_reason(struct htx *htx, const struct ist reason)
519{
520 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200521 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200523
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100524 if (!sl)
525 return 0;
526
Christopher Faulete010c802018-10-24 10:36:45 +0200527 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100528 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
529 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200530
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100531 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
532 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200533
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100534 /* create the new start line */
535 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200536}
537
Christopher Faulet47596d32018-10-22 09:17:28 +0200538/* Replaces a part of a header value referenced in the context <ctx> by
539 * <data>. It returns 1 on success, otherwise it returns 0. The context is
540 * updated if necessary.
541 */
542int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
543{
544 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200545 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200546 char *start;
547 struct ist v;
548 uint32_t len, off;
549
550 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200551 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200552
553 v = htx_get_blk_value(htx, blk);
554 start = ctx->value.ptr - ctx->lws_before;
555 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
556 off = start - v.ptr;
557
558 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
559 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200560 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200561
562 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200563
564 sl = http_get_stline(htx);
565 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
566 struct ist n = htx_get_blk_name(htx, blk);
567
568 if (isteq(n, ist("host"))) {
569 if (!http_update_authority(htx, sl, v))
570 goto fail;
571 ctx->blk = NULL;
572 http_find_header(htx, ist("host"), ctx, 1);
573 blk = ctx->blk;
574 v = htx_get_blk_value(htx, blk);
575 }
576 }
577
Christopher Faulet47596d32018-10-22 09:17:28 +0200578 ctx->blk = blk;
579 ctx->value.ptr = v.ptr + off;
580 ctx->value.len = data.len;
581 ctx->lws_before = ctx->lws_after = 0;
582
583 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200584 fail:
585 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200586}
587
588/* Fully replaces a header referenced in the context <ctx> by the name <name>
589 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
590 * context is updated if necessary.
591 */
592int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
593 const struct ist name, const struct ist value)
594{
595 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200596 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200597
598 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200599 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200600
601 blk = htx_replace_header(htx, blk, name, value);
602 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200603 goto fail;
604
605 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100606 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200607 if (!http_update_authority(htx, sl, value))
608 goto fail;
609 ctx->blk = NULL;
610 http_find_header(htx, ist("host"), ctx, 1);
611 blk = ctx->blk;
612 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200613
614 ctx->blk = blk;
615 ctx->value = ist(NULL);
616 ctx->lws_before = ctx->lws_after = 0;
617
618 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200619 fail:
620 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200621}
622
623/* Remove one value of a header. This only works on a <ctx> returned by
624 * http_find_header function. The value is removed, as well as surrounding commas
625 * if any. If the removed value was alone, the whole header is removed. The
626 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
627 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
628 * form that can be handled by http_find_header() to find next occurrence.
629 */
630int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
631{
632 struct htx_blk *blk = ctx->blk;
633 char *start;
634 struct ist v;
635 uint32_t len;
636
637 if (!blk)
638 return 0;
639
640 start = ctx->value.ptr - ctx->lws_before;
641 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
642
643 v = htx_get_blk_value(htx, blk);
644 if (len == v.len) {
645 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200646 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200647 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100648 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200649 ctx->lws_before = ctx->lws_after = 0;
650 }
651 else {
652 ctx->blk = htx_get_blk(htx, htx->tail);
653 ctx->value = htx_get_blk_value(htx, ctx->blk);
654 ctx->lws_before = ctx->lws_after = 0;
655 }
656 return 1;
657 }
658
659 /* This was not the only value of this header. We have to remove the
660 * part pointed by ctx->value. If it is the last entry of the list, we
661 * remove the last separator.
662 */
663 if (start == v.ptr) {
664 /* It's the first header part but not the only one. So remove
665 * the comma after it. */
666 len++;
667 }
668 else {
669 /* There is at least one header part before the removed one. So
670 * remove the comma between them. */
671 start--;
672 len++;
673 }
674 /* Update the block content and its len */
675 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200676 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200677
678 /* Finally update the ctx */
679 ctx->value.ptr = start;
680 ctx->value.len = 0;
681 ctx->lws_before = ctx->lws_after = 0;
682
683 return 1;
684}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200685
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200686/* Updates the authority part of the uri with the value <host>. It happens when
687 * the header host is modified. It returns 0 on failure and 1 on success. It is
688 * the caller responsibility to provide the start-line and to be sure the uri
689 * contains an authority. Thus, if no authority is found in the uri, an error is
690 * returned.
691 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200692int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200693{
694 struct buffer *temp = get_trash_chunk();
695 struct ist meth, vsn, uri, authority;
696
697 uri = htx_sl_req_uri(sl);
698 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100699 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200700 return 0;
701
Christopher Faulet34b18e42020-02-18 11:02:21 +0100702 /* Don't update the uri if there is no change */
703 if (isteq(host, authority))
704 return 1;
705
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200706 /* Start by copying old method and version */
707 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
708 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
709
710 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
711 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
712
713 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
714 chunk_memcat(temp, host.ptr, host.len);
715 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
716 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
717
718 return http_replace_stline(htx, meth, uri, vsn);
719
720}
721
722/* Update the header host by extracting the authority of the uri <uri>. flags of
723 * the start-line are also updated accordingly. For orgin-form and asterisk-form
724 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
725 * removed from the flags of the start-line. Otherwise, this flag is set and the
726 * authority is used to set the value of the header host. This function returns
727 * 0 on failure and 1 on success.
728*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200729int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200730{
731 struct ist authority;
732 struct http_hdr_ctx ctx;
733
734 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
735 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
736 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
737 }
738 else {
739 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
740 if (sl->info.req.meth != HTTP_METH_CONNECT) {
741 // absolute-form (RFC7320 #5.3.2)
742 sl->flags |= HTX_SL_F_HAS_SCHM;
743 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
744 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
745
746 authority = http_get_authority(uri, 1);
747 if (!authority.len)
748 goto fail;
749 }
750 else {
751 // authority-form (RFC7320 #5.3.3)
752 authority = uri;
753 }
754
755 /* Replace header host value */
756 ctx.blk = NULL;
757 while (http_find_header(htx, ist("host"), &ctx, 1)) {
758 if (!http_replace_header_value(htx, &ctx, authority))
759 goto fail;
760 }
761
762 }
763 return 1;
764 fail:
765 return 0;
766}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200767
768/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
769 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
770 * performed over the whole headers. Otherwise it must contain a valid header
771 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
772 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
773 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
774 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
775 * -1. The value fetch stops at commas, so this function is suited for use with
776 * list headers.
777 * The return value is 0 if nothing was found, or non-zero otherwise.
778 */
779unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
780 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
781{
782 struct http_hdr_ctx local_ctx;
783 struct ist val_hist[MAX_HDR_HISTORY];
784 unsigned int hist_idx;
785 int found;
786
787 if (!ctx) {
788 local_ctx.blk = NULL;
789 ctx = &local_ctx;
790 }
791
792 if (occ >= 0) {
793 /* search from the beginning */
794 while (http_find_header(htx, hdr, ctx, 0)) {
795 occ--;
796 if (occ <= 0) {
797 *vptr = ctx->value.ptr;
798 *vlen = ctx->value.len;
799 return 1;
800 }
801 }
802 return 0;
803 }
804
805 /* negative occurrence, we scan all the list then walk back */
806 if (-occ > MAX_HDR_HISTORY)
807 return 0;
808
809 found = hist_idx = 0;
810 while (http_find_header(htx, hdr, ctx, 0)) {
811 val_hist[hist_idx] = ctx->value;
812 if (++hist_idx >= MAX_HDR_HISTORY)
813 hist_idx = 0;
814 found++;
815 }
816 if (-occ > found)
817 return 0;
818
819 /* OK now we have the last occurrence in [hist_idx-1], and we need to
820 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
821 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
822 * to remain in the 0..9 range.
823 */
824 hist_idx += occ + MAX_HDR_HISTORY;
825 if (hist_idx >= MAX_HDR_HISTORY)
826 hist_idx -= MAX_HDR_HISTORY;
827 *vptr = val_hist[hist_idx].ptr;
828 *vlen = val_hist[hist_idx].len;
829 return 1;
830}
831
832/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
833 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
834 * performed over the whole headers. Otherwise it must contain a valid header
835 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
836 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
837 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
838 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
839 * -1. This function differs from http_get_hdr() in that it only returns full
840 * line header values and does not stop at commas.
841 * The return value is 0 if nothing was found, or non-zero otherwise.
842 */
843unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
844 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
845{
846 struct http_hdr_ctx local_ctx;
847 struct ist val_hist[MAX_HDR_HISTORY];
848 unsigned int hist_idx;
849 int found;
850
851 if (!ctx) {
852 local_ctx.blk = NULL;
853 ctx = &local_ctx;
854 }
855
856 if (occ >= 0) {
857 /* search from the beginning */
858 while (http_find_header(htx, hdr, ctx, 1)) {
859 occ--;
860 if (occ <= 0) {
861 *vptr = ctx->value.ptr;
862 *vlen = ctx->value.len;
863 return 1;
864 }
865 }
866 return 0;
867 }
868
869 /* negative occurrence, we scan all the list then walk back */
870 if (-occ > MAX_HDR_HISTORY)
871 return 0;
872
873 found = hist_idx = 0;
874 while (http_find_header(htx, hdr, ctx, 1)) {
875 val_hist[hist_idx] = ctx->value;
876 if (++hist_idx >= MAX_HDR_HISTORY)
877 hist_idx = 0;
878 found++;
879 }
880 if (-occ > found)
881 return 0;
882
883 /* OK now we have the last occurrence in [hist_idx-1], and we need to
884 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
885 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
886 * to remain in the 0..9 range.
887 */
888 hist_idx += occ + MAX_HDR_HISTORY;
889 if (hist_idx >= MAX_HDR_HISTORY)
890 hist_idx -= MAX_HDR_HISTORY;
891 *vptr = val_hist[hist_idx].ptr;
892 *vlen = val_hist[hist_idx].len;
893 return 1;
894}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100895
Christopher Fauleta66adf42020-11-05 22:43:41 +0100896int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100897{
898 struct htx *htx;
899 struct htx_sl *sl;
900 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200901 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100902 union h1_sl h1sl;
903 unsigned int flags = HTX_SL_F_IS_RESP;
904 int ret = 0;
905
Christopher Faulet90cc4812019-07-22 16:49:30 +0200906 b_reset(buf);
907 if (!raw.len) {
908 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100909 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200910 return 1;
911 }
912
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100913 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200914 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100915 if (!buf->area)
916 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100917
918 h1m_init_res(&h1m);
919 h1m.flags |= H1_MF_NO_PHDR;
920 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
921 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100922 if (ret <= 0) {
923 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100924 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100925 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100926
Christopher Fauleta66adf42020-11-05 22:43:41 +0100927 if (unlikely(h1sl.st.v.len != 8)) {
928 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100929 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100930 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100931 if ((*(h1sl.st.v.ptr + 5) > '1') ||
932 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
933 h1m.flags |= H1_MF_VER_11;
934
Christopher Fauleta66adf42020-11-05 22:43:41 +0100935 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
936 memprintf(errmsg, "invalid http status code for an error message (%u)",
937 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200938 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100939 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200940
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200941 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
942 /* Responses known to have no body. */
943 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
944 h1m.flags |= H1_MF_XFER_LEN;
945 h1m.curr_len = h1m.body_len = 0;
946 }
947 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
948 h1m.flags |= H1_MF_XFER_LEN;
949
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100950 if (h1m.flags & H1_MF_VER_11)
951 flags |= HTX_SL_F_VER_11;
952 if (h1m.flags & H1_MF_XFER_ENC)
953 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200954 if (h1m.flags & H1_MF_XFER_LEN) {
955 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100956 if (h1m.flags & H1_MF_CHNK) {
957 memprintf(errmsg, "chunk-encoded payload not supported");
958 goto error;
959 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200960 else if (h1m.flags & H1_MF_CLEN) {
961 flags |= HTX_SL_F_CLEN;
962 if (h1m.body_len == 0)
963 flags |= HTX_SL_F_BODYLESS;
964 }
965 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200966 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100967 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200968
Christopher Fauleta66adf42020-11-05 22:43:41 +0100969 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
970 memprintf(errmsg, "message payload not expected");
971 goto error;
972 }
973 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
974 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100975 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100976 goto error;
977 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100978
979 htx = htx_from_buf(buf);
980 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 +0100981 if (!sl || !htx_add_all_headers(htx, hdrs)) {
982 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100983 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100984 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100985 sl->info.res.status = h1sl.st.status;
986
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200987 while (raw.len > ret) {
988 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100989 if (!sent) {
990 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100991 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100992 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200993 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100994 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200995
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100996 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200997
Christopher Faulet90cc4812019-07-22 16:49:30 +0200998 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100999
1000error:
1001 if (buf->size)
1002 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001003 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001004}
1005
Christopher Faulet18630642020-05-12 18:57:28 +02001006void release_http_reply(struct http_reply *http_reply)
1007{
1008 struct logformat_node *lf, *lfb;
1009 struct http_reply_hdr *hdr, *hdrb;
1010
1011 if (!http_reply)
1012 return;
1013
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001014 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001015 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001016 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001017 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001018 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001019 release_sample_expr(lf->expr);
1020 free(lf->arg);
1021 free(lf);
1022 }
1023 istfree(&hdr->name);
1024 free(hdr);
1025 }
1026
1027 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001028 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001029 }
1030 else if (http_reply->type == HTTP_REPLY_RAW)
1031 chunk_destroy(&http_reply->body.obj);
1032 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1033 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001034 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001035 release_sample_expr(lf->expr);
1036 free(lf->arg);
1037 free(lf);
1038 }
1039 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001040 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001041}
1042
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001043static int http_htx_init(void)
1044{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001045 struct buffer chk;
1046 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001047 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001048 int rc;
1049 int err_code = 0;
1050
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001051 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1052 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001053 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001054 err_code |= ERR_ALERT | ERR_FATAL;
1055 continue;
1056 }
1057
1058 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Fauleta66adf42020-11-05 22:43:41 +01001059 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1060 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1061 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001062 err_code |= ERR_ALERT | ERR_FATAL;
1063 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001064 else if (errmsg) {
1065 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1066 err_code |= ERR_WARN;
1067 }
1068
1069 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001070 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001071
Christopher Fauletf7346382019-07-17 22:02:08 +02001072 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001073 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1074 http_err_replies[rc].status = http_err_codes[rc];
1075 http_err_replies[rc].ctype = NULL;
1076 LIST_INIT(&http_err_replies[rc].hdrs);
1077 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001078 }
1079end:
1080 return err_code;
1081}
1082
Christopher Faulet58857752020-01-15 15:19:50 +01001083static void http_htx_deinit(void)
1084{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001085 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001086 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001087 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001088 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001089 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001090
1091 node = ebpt_first(&http_error_messages);
1092 while (node) {
1093 next = ebpt_next(node);
1094 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001095 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1096 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001097 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001098 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001099 node = next;
1100 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001101
1102 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1103 free(http_errs->conf.file);
1104 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001105 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1106 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001107 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001108 free(http_errs);
1109 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001110
1111 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001112 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001113 release_http_reply(http_rep);
1114 }
Christopher Faulet58857752020-01-15 15:19:50 +01001115}
1116
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001117REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001118REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001119
Christopher Faulet58857752020-01-15 15:19:50 +01001120/* Reads content of the error file <file> and convert it into an HTX message. On
1121 * success, the HTX message is returned. On error, NULL is returned and an error
1122 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001123 */
Christopher Faulet58857752020-01-15 15:19:50 +01001124struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001125{
Christopher Faulet58857752020-01-15 15:19:50 +01001126 struct buffer *buf = NULL;
1127 struct buffer chk;
1128 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001129 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001130 struct stat stat;
1131 char *err = NULL;
1132 int errnum, errlen;
1133 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001134
1135 /* already loaded */
1136 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1137 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001138 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1139 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001140 goto out;
1141 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001142
Christopher Faulet58857752020-01-15 15:19:50 +01001143 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001144 fd = open(file, O_RDONLY);
1145 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1146 memprintf(errmsg, "error opening file '%s'.", file);
1147 goto out;
1148 }
1149
1150 if (stat.st_size <= global.tune.bufsize)
1151 errlen = stat.st_size;
1152 else {
1153 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1154 file, global.tune.bufsize);
1155 errlen = global.tune.bufsize;
1156 }
1157
1158 err = malloc(errlen);
1159 if (!err) {
1160 memprintf(errmsg, "out of memory.");
1161 goto out;
1162 }
1163
1164 errnum = read(fd, err, errlen);
1165 if (errnum != errlen) {
1166 memprintf(errmsg, "error reading file '%s'.", file);
1167 goto out;
1168 }
1169
Christopher Faulet58857752020-01-15 15:19:50 +01001170 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001171 http_errmsg = calloc(1, sizeof(*http_errmsg));
1172 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001173 memprintf(errmsg, "out of memory.");
1174 goto out;
1175 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001176 http_errmsg->node.key = strdup(file);
1177 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001178 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001179 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001180 goto out;
1181 }
1182
1183 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001184 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1185 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001186 free(http_errmsg->node.key);
1187 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001188 goto out;
1189 }
1190
Christopher Faulet58857752020-01-15 15:19:50 +01001191 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001192 http_errmsg->msg = chk;
1193 ebis_insert(&http_error_messages, &http_errmsg->node);
1194 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001195
Christopher Faulet5031ef52020-01-15 11:22:07 +01001196 out:
1197 if (fd >= 0)
1198 close(fd);
1199 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001200 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001201}
1202
Ilya Shipitsind4259502020-04-08 01:07:56 +05001203/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001204 * message is returned. On error, NULL is returned and an error message is
1205 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001206 */
Christopher Faulet58857752020-01-15 15:19:50 +01001207struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001208{
Christopher Faulet58857752020-01-15 15:19:50 +01001209 struct buffer *buf = NULL;
1210 struct buffer chk;
1211 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001212 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001213
1214 /* already loaded */
1215 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1216 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001217 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1218 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001219 goto out;
1220 }
1221 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001222 http_errmsg = calloc(1, sizeof(*http_errmsg));
1223 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001224 memprintf(errmsg, "out of memory.");
1225 goto out;
1226 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001227 http_errmsg->node.key = strdup(key);
1228 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001229 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001230 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001231 goto out;
1232 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001233
1234 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001235 if (!http_str_to_htx(&chk, msg, errmsg)) {
1236 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001237 free(http_errmsg->node.key);
1238 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001239 goto out;
1240 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001241
Christopher Faulet58857752020-01-15 15:19:50 +01001242 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001243 http_errmsg->msg = chk;
1244 ebis_insert(&http_error_messages, &http_errmsg->node);
1245 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001246 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001247 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001248}
1249
Christopher Faulet5031ef52020-01-15 11:22:07 +01001250/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001251 * <status>. It returns NULL if there is any error, otherwise it return the
1252 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001253 */
Christopher Faulet58857752020-01-15 15:19:50 +01001254struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001255{
Christopher Faulet58857752020-01-15 15:19:50 +01001256 struct buffer *buf = NULL;
1257 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001258
1259 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1260 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001261 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001262 break;
1263 }
1264 }
1265
1266 if (rc >= HTTP_ERR_SIZE)
1267 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001268 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001269}
1270
1271/* This function creates HTX error message corresponding to a redirect message
1272 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001273 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1274 * returns NULL if there is any error, otherwise it return the corresponding HTX
1275 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001276 */
Christopher Faulet58857752020-01-15 15:19:50 +01001277struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001278{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001279 static const char *HTTP_302 =
1280 "HTTP/1.1 302 Found\r\n"
1281 "Cache-Control: no-cache\r\n"
1282 "Content-length: 0\r\n"
1283 "Location: "; /* not terminated since it will be concatenated with the URL */
1284 static const char *HTTP_303 =
1285 "HTTP/1.1 303 See Other\r\n"
1286 "Cache-Control: no-cache\r\n"
1287 "Content-length: 0\r\n"
1288 "Location: "; /* not terminated since it will be concatenated with the URL */
1289
Christopher Faulet58857752020-01-15 15:19:50 +01001290 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001291 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001292 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001293 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001294
1295 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1296 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001297 /* Create the error key */
1298 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1299 memprintf(errmsg, "out of memory.");
1300 goto out;
1301 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001302 /* Create the error message */
1303 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1304 errlen = strlen(msg) + strlen(url) + 5;
1305 err = malloc(errlen);
1306 if (!err) {
1307 memprintf(errmsg, "out of memory.");
1308 goto out;
1309 }
1310 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1311
1312 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001313 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001314 break;
1315 }
1316 }
1317
1318 if (rc >= HTTP_ERR_SIZE)
1319 memprintf(errmsg, "status code '%d' not handled.", status);
1320out:
Christopher Faulet58857752020-01-15 15:19:50 +01001321 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001322 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001323 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001324}
1325
Christopher Faulet7eea2412020-05-13 15:02:59 +02001326/* Check an "http reply" and, for replies referencing an http-errors section,
1327 * try to find the right section and the right error message in this section. If
1328 * found, the reply is updated. If the http-errors section exists but the error
1329 * message is not found, no error message is set to fallback on the default
1330 * ones. Otherwise (unknown section) an error is returned.
1331 *
1332 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1333 * filled.
1334 */
1335int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1336{
1337 struct http_errors *http_errs;
1338 int ret = 1;
1339
1340 if (reply->type != HTTP_REPLY_ERRFILES)
1341 goto end;
1342
1343 list_for_each_entry(http_errs, &http_errors_list, list) {
1344 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001345 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001346 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001347 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1348 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001349 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1350 "not declared in http-errors section '%s'.\n",
1351 px->id, reply->status, http_errs->id);
1352 break;
1353 }
1354 }
1355
1356 if (&http_errs->list == &http_errors_list) {
1357 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1358 reply->body.http_errors);
1359 ret = 0;
1360 }
1361
1362 end:
1363 return ret;
1364}
1365
Christopher Faulet47e791e2020-05-13 14:36:55 +02001366/* Parse an "http reply". It returns the reply on success or NULL on error. This
1367 * function creates one of the following http replies :
1368 *
1369 * - HTTP_REPLY_EMPTY : dummy response, no payload
1370 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1371 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1372 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1373 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1374 *
1375 * The content-type must be defined for non-empty payload. It is ignored for
1376 * error messages (implicit or explicit). When an http-errors section is
1377 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1378 * during the configuration validity check or dynamically. It is the caller
1379 * responsibility to choose. If no status code is configured, <default_status>
1380 * is set.
1381 */
1382struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1383 int default_status, char **errmsg)
1384{
1385 struct logformat_node *lf, *lfb;
1386 struct http_reply *reply = NULL;
1387 struct http_reply_hdr *hdr, *hdrb;
1388 struct stat stat;
1389 const char *act_arg = NULL;
1390 char *obj = NULL;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001391 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001392
1393
1394 reply = calloc(1, sizeof(*reply));
1395 if (!reply) {
1396 memprintf(errmsg, "out of memory");
1397 goto error;
1398 }
1399 LIST_INIT(&reply->hdrs);
1400 reply->type = HTTP_REPLY_EMPTY;
1401 reply->status = default_status;
1402
Christopher Faulet3b967c12020-05-15 15:47:44 +02001403 if (px->conf.args.ctx == ARGC_HERR)
1404 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001405 else {
1406 if (px->cap & PR_CAP_FE)
1407 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1408 if (px->cap & PR_CAP_BE)
Willy Tarreauad745102021-10-16 14:41:09 +02001409 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet5f802b32021-10-13 17:22:17 +02001410 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001411
1412 cur_arg = *orig_arg;
1413 while (*args[cur_arg]) {
1414 if (strcmp(args[cur_arg], "status") == 0) {
1415 cur_arg++;
1416 if (!*args[cur_arg]) {
1417 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1418 goto error;
1419 }
1420 reply->status = atol(args[cur_arg]);
1421 if (reply->status < 200 || reply->status > 599) {
1422 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1423 goto error;
1424 }
1425 cur_arg++;
1426 }
1427 else if (strcmp(args[cur_arg], "content-type") == 0) {
1428 cur_arg++;
1429 if (!*args[cur_arg]) {
1430 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1431 goto error;
1432 }
1433 free(reply->ctype);
1434 reply->ctype = strdup(args[cur_arg]);
1435 cur_arg++;
1436 }
1437 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1438 if (reply->type != HTTP_REPLY_EMPTY) {
1439 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1440 goto error;
1441 }
1442 act_arg = args[cur_arg];
1443 cur_arg++;
1444 if (!*args[cur_arg]) {
1445 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1446 goto error;
1447 }
1448 reply->body.http_errors = strdup(args[cur_arg]);
1449 if (!reply->body.http_errors) {
1450 memprintf(errmsg, "out of memory");
1451 goto error;
1452 }
1453 reply->type = HTTP_REPLY_ERRFILES;
1454 cur_arg++;
1455 }
1456 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1457 if (reply->type != HTTP_REPLY_EMPTY) {
1458 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1459 goto error;
1460 }
1461 act_arg = args[cur_arg];
1462 reply->type = HTTP_REPLY_ERRMSG;
1463 cur_arg++;
1464 }
1465 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1466 if (reply->type != HTTP_REPLY_EMPTY) {
1467 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1468 goto error;
1469 }
1470 act_arg = args[cur_arg];
1471 cur_arg++;
1472 if (!*args[cur_arg]) {
1473 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1474 goto error;
1475 }
1476 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1477 if (!reply->body.errmsg) {
1478 goto error;
1479 }
1480 reply->type = HTTP_REPLY_ERRMSG;
1481 cur_arg++;
1482 }
1483 else if (strcmp(args[cur_arg], "file") == 0) {
1484 if (reply->type != HTTP_REPLY_EMPTY) {
1485 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1486 goto error;
1487 }
1488 act_arg = args[cur_arg];
1489 cur_arg++;
1490 if (!*args[cur_arg]) {
1491 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1492 goto error;
1493 }
1494 fd = open(args[cur_arg], O_RDONLY);
1495 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1496 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1497 goto error;
1498 }
1499 if (stat.st_size > global.tune.bufsize) {
1500 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1501 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1502 goto error;
1503 }
1504 objlen = stat.st_size;
1505 obj = malloc(objlen);
1506 if (!obj || read(fd, obj, objlen) != objlen) {
1507 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1508 goto error;
1509 }
1510 close(fd);
1511 fd = -1;
1512 reply->type = HTTP_REPLY_RAW;
1513 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1514 obj = NULL;
1515 cur_arg++;
1516 }
1517 else if (strcmp(args[cur_arg], "string") == 0) {
1518 if (reply->type != HTTP_REPLY_EMPTY) {
1519 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1520 goto error;
1521 }
1522 act_arg = args[cur_arg];
1523 cur_arg++;
1524 if (!*args[cur_arg]) {
1525 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1526 goto error;
1527 }
1528 obj = strdup(args[cur_arg]);
1529 objlen = strlen(args[cur_arg]);
1530 if (!obj) {
1531 memprintf(errmsg, "out of memory");
1532 goto error;
1533 }
1534 reply->type = HTTP_REPLY_RAW;
1535 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1536 obj = NULL;
1537 cur_arg++;
1538 }
1539 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1540 if (reply->type != HTTP_REPLY_EMPTY) {
1541 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1542 goto error;
1543 }
1544 act_arg = args[cur_arg];
1545 cur_arg++;
1546 if (!*args[cur_arg]) {
1547 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1548 goto error;
1549 }
1550 fd = open(args[cur_arg], O_RDONLY);
1551 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1552 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1553 goto error;
1554 }
1555 if (stat.st_size > global.tune.bufsize) {
1556 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1557 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1558 goto error;
1559 }
1560 objlen = stat.st_size;
1561 obj = malloc(objlen + 1);
1562 if (!obj || read(fd, obj, objlen) != objlen) {
1563 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1564 goto error;
1565 }
1566 close(fd);
1567 fd = -1;
1568 obj[objlen] = '\0';
1569 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001570 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001571 cur_arg++;
1572 }
1573 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1574 if (reply->type != HTTP_REPLY_EMPTY) {
1575 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1576 goto error;
1577 }
1578 act_arg = args[cur_arg];
1579 cur_arg++;
1580 if (!*args[cur_arg]) {
1581 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1582 goto error;
1583 }
1584 obj = strdup(args[cur_arg]);
1585 objlen = strlen(args[cur_arg]);
1586 reply->type = HTTP_REPLY_LOGFMT;
Christopher Faulet204017c2022-11-14 08:49:28 +01001587 LIST_INIT(&reply->body.fmt);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001588 cur_arg++;
1589 }
1590 else if (strcmp(args[cur_arg], "hdr") == 0) {
1591 cur_arg++;
1592 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1593 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1594 goto error;
1595 }
1596 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1597 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1598 strcasecmp(args[cur_arg], "content-type") == 0) {
1599 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1600 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1601 cur_arg += 2;
1602 continue;
1603 }
1604 hdr = calloc(1, sizeof(*hdr));
1605 if (!hdr) {
1606 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1607 goto error;
1608 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001609 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001610 LIST_INIT(&hdr->value);
1611 hdr->name = ist(strdup(args[cur_arg]));
1612 if (!isttest(hdr->name)) {
1613 memprintf(errmsg, "out of memory");
1614 goto error;
1615 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001616 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1617 goto error;
1618
1619 free(px->conf.lfs_file);
1620 px->conf.lfs_file = strdup(px->conf.args.file);
1621 px->conf.lfs_line = px->conf.args.line;
1622 cur_arg += 2;
1623 }
1624 else
1625 break;
1626 }
1627
1628 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1629 if (reply->ctype) {
1630 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1631 " neither errorfile nor payload defined.\n",
1632 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001633 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001634 }
1635 }
1636 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1637
1638 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1639 /* default errorfile or errorfiles: check the status */
1640 int rc;
1641
1642 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1643 if (http_err_codes[rc] == reply->status)
1644 break;
1645 }
1646
1647 if (rc >= HTTP_ERR_SIZE) {
1648 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1649 reply->status, act_arg);
1650 goto error;
1651 }
1652 }
1653
1654 if (reply->ctype) {
1655 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1656 "with an erorrfile.\n",
1657 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001658 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001659 }
1660 if (!LIST_ISEMPTY(&reply->hdrs)) {
1661 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1662 "with an erorrfile.\n",
1663 px->conf.args.file, px->conf.args.line);
1664 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001665 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001666 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001667 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001668 release_sample_expr(lf->expr);
1669 free(lf->arg);
1670 free(lf);
1671 }
1672 istfree(&hdr->name);
1673 free(hdr);
1674 }
1675 }
1676 }
1677 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001678 if ((reply->status == 204 || reply->status == 304) && objlen) {
1679 memprintf(errmsg, "No body expected for %d responses", reply->status);
1680 goto error;
1681 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001682 if (!reply->ctype && objlen) {
1683 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1684 goto error;
1685 }
1686 if (reply->ctype && !b_data(&reply->body.obj)) {
1687 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001688 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001689 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001690 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001691 }
1692 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1693 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1694 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1695 px->conf.args.file, px->conf.args.line);
1696 }
1697 }
1698 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1699 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001700 if ((reply->status == 204 || reply->status == 304)) {
1701 memprintf(errmsg, "No body expected for %d responses", reply->status);
1702 goto error;
1703 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001704 if (!reply->ctype) {
1705 memprintf(errmsg, "a content type must be defined with a log-format payload");
1706 goto error;
1707 }
1708 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1709 goto error;
1710
1711 free(px->conf.lfs_file);
1712 px->conf.lfs_file = strdup(px->conf.args.file);
1713 px->conf.lfs_line = px->conf.args.line;
1714 }
1715
1716 free(obj);
1717 *orig_arg = cur_arg;
1718 return reply;
1719
1720 error:
1721 free(obj);
1722 if (fd >= 0)
1723 close(fd);
1724 release_http_reply(reply);
1725 return NULL;
1726}
1727
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001728/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1729 * Returns 0 if no error has been found else non-zero.
1730 *
1731 * The normalization is processed on the target-uri at the condition that it is
1732 * in absolute-form. In the case where the target-uri was normalized, every
1733 * host headers values found are also replaced by the normalized hostname. This
1734 * assumes that the target-uri and host headers were properly identify as
1735 * similar before calling this function.
1736 */
1737int http_scheme_based_normalize(struct htx *htx)
1738{
1739 struct http_hdr_ctx ctx;
1740 struct htx_sl *sl;
1741 struct ist uri, scheme, authority, host, port;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001742
1743 sl = http_get_stline(htx);
1744
1745 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1746 return 0;
1747
1748 uri = htx_sl_req_uri(sl);
1749
1750 scheme = http_get_scheme(uri);
1751 /* if no scheme found, no normalization to proceed */
1752 if (!isttest(scheme))
1753 return 0;
1754
Christopher Fauletbe50b142022-07-05 10:24:52 +02001755 /* Extract the port if present in authority */
1756 authority = http_get_authority(uri, 1);
1757 port = http_get_host_port(authority);
1758 if (!isttest(port)) {
1759 /* if no port found, no normalization to proceed */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001760 return 0;
Christopher Fauletbe50b142022-07-05 10:24:52 +02001761 }
1762 host = isttrim(authority, istlen(authority) - istlen(port) - 1);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001763
Christopher Fauletbe50b142022-07-05 10:24:52 +02001764 if (istlen(port) && http_is_default_port(scheme, port)) {
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001765 /* reconstruct the uri with removal of the port */
1766 struct buffer *temp = get_trash_chunk();
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001767 struct ist meth, vsn;
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001768
1769 /* meth */
1770 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1771 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1772
1773 /* vsn */
1774 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1775 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1776
1777 /* reconstruct uri without port */
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001778 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001779 chunk_istcat(temp, host);
Christopher Faulet3a499ee2022-07-06 17:41:31 +02001780 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
1781 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
Amaury Denoyelleca87a9a2021-07-07 10:49:26 +02001782
1783 http_replace_stline(htx, meth, uri, vsn);
1784
1785 /* replace every host headers values by the normalized host */
1786 ctx.blk = NULL;
1787 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1788 if (!http_replace_header_value(htx, &ctx, host))
1789 goto fail;
1790 }
1791 }
1792
1793 return 0;
1794
1795 fail:
1796 return 1;
1797}
1798
Christopher Faulet07f41f72020-01-16 16:16:06 +01001799/* Parses the "errorloc[302|303]" proxy keyword */
1800static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001801 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001802 char **errmsg)
1803{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001804 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001805 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001806 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001807 int errloc, status;
1808 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001809
1810 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1811 ret = 1;
1812 goto out;
1813 }
1814
1815 if (*(args[1]) == 0 || *(args[2]) == 0) {
1816 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1817 ret = -1;
1818 goto out;
1819 }
1820
1821 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001822 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001823 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1824 if (!msg) {
1825 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1826 ret = -1;
1827 goto out;
1828 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001829
1830 reply = calloc(1, sizeof(*reply));
1831 if (!reply) {
1832 memprintf(errmsg, "%s : out of memory.", args[0]);
1833 ret = -1;
1834 goto out;
1835 }
1836 reply->type = HTTP_REPLY_ERRMSG;
1837 reply->status = status;
1838 reply->ctype = NULL;
1839 LIST_INIT(&reply->hdrs);
1840 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001841 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001842
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001843 conf_err = calloc(1, sizeof(*conf_err));
1844 if (!conf_err) {
1845 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001846 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001847 ret = -1;
1848 goto out;
1849 }
1850 conf_err->type = 1;
1851 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001852 conf_err->info.errorfile.reply = reply;
1853
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001854 conf_err->file = strdup(file);
1855 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001856 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001857
Christopher Fauleta66adf42020-11-05 22:43:41 +01001858 /* handle warning message */
1859 if (*errmsg)
1860 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001861 out:
1862 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001863
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001864}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001865
1866/* Parses the "errorfile" proxy keyword */
1867static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001868 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001869 char **errmsg)
1870{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001871 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001872 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001873 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001874 int status;
1875 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001876
1877 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1878 ret = 1;
1879 goto out;
1880 }
1881
1882 if (*(args[1]) == 0 || *(args[2]) == 0) {
1883 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1884 ret = -1;
1885 goto out;
1886 }
1887
1888 status = atol(args[1]);
1889 msg = http_parse_errorfile(status, args[2], errmsg);
1890 if (!msg) {
1891 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1892 ret = -1;
1893 goto out;
1894 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001895
Christopher Faulet5809e102020-05-14 17:31:52 +02001896 reply = calloc(1, sizeof(*reply));
1897 if (!reply) {
1898 memprintf(errmsg, "%s : out of memory.", args[0]);
1899 ret = -1;
1900 goto out;
1901 }
1902 reply->type = HTTP_REPLY_ERRMSG;
1903 reply->status = status;
1904 reply->ctype = NULL;
1905 LIST_INIT(&reply->hdrs);
1906 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001907 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001908
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001909 conf_err = calloc(1, sizeof(*conf_err));
1910 if (!conf_err) {
1911 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001912 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001913 ret = -1;
1914 goto out;
1915 }
1916 conf_err->type = 1;
1917 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001918 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001919 conf_err->file = strdup(file);
1920 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001921 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001922
Christopher Fauleta66adf42020-11-05 22:43:41 +01001923 /* handle warning message */
1924 if (*errmsg)
1925 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001926 out:
1927 return ret;
1928
1929}
1930
1931/* Parses the "errorfiles" proxy keyword */
1932static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001933 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001934 char **err)
1935{
1936 struct conf_errors *conf_err = NULL;
1937 char *name = NULL;
1938 int rc, ret = 0;
1939
1940 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1941 ret = 1;
1942 goto out;
1943 }
1944
1945 if (!*(args[1])) {
1946 memprintf(err, "%s : expects <name> as argument.", args[0]);
1947 ret = -1;
1948 goto out;
1949 }
1950
1951 name = strdup(args[1]);
1952 conf_err = calloc(1, sizeof(*conf_err));
1953 if (!name || !conf_err) {
1954 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001955 goto error;
1956 }
1957 conf_err->type = 0;
1958
1959 conf_err->info.errorfiles.name = name;
1960 if (!*(args[2])) {
1961 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1962 conf_err->info.errorfiles.status[rc] = 1;
1963 }
1964 else {
1965 int cur_arg, status;
1966 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1967 status = atol(args[cur_arg]);
1968
1969 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1970 if (http_err_codes[rc] == status) {
1971 conf_err->info.errorfiles.status[rc] = 2;
1972 break;
1973 }
1974 }
1975 if (rc >= HTTP_ERR_SIZE) {
1976 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001977 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001978 }
1979 }
1980 }
1981 conf_err->file = strdup(file);
1982 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001983 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001984 out:
1985 return ret;
1986
1987 error:
1988 free(name);
1989 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001990 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001991 goto out;
1992}
1993
Christopher Faulet3b967c12020-05-15 15:47:44 +02001994/* Parses the "http-error" proxy keyword */
1995static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001996 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02001997 char **errmsg)
1998{
1999 struct conf_errors *conf_err;
2000 struct http_reply *reply = NULL;
2001 int rc, cur_arg, ret = 0;
2002
2003 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2004 ret = 1;
2005 goto out;
2006 }
2007
2008 cur_arg = 1;
2009 curpx->conf.args.ctx = ARGC_HERR;
2010 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2011 if (!reply) {
2012 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2013 goto error;
2014 }
2015 else if (!reply->status) {
2016 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2017 goto error;
2018 }
2019
2020 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2021 if (http_err_codes[rc] == reply->status)
2022 break;
2023 }
2024
2025 if (rc >= HTTP_ERR_SIZE) {
2026 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2027 goto error;
2028 }
2029 if (*args[cur_arg]) {
2030 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2031 goto error;
2032 }
2033
2034 conf_err = calloc(1, sizeof(*conf_err));
2035 if (!conf_err) {
2036 memprintf(errmsg, "%s : out of memory.", args[0]);
2037 goto error;
2038 }
2039 if (reply->type == HTTP_REPLY_ERRFILES) {
2040 int rc = http_get_status_idx(reply->status);
2041
2042 conf_err->type = 2;
2043 conf_err->info.errorfiles.name = reply->body.http_errors;
2044 conf_err->info.errorfiles.status[rc] = 2;
2045 reply->body.http_errors = NULL;
2046 release_http_reply(reply);
2047 }
2048 else {
2049 conf_err->type = 1;
2050 conf_err->info.errorfile.status = reply->status;
2051 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002052 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002053 }
2054 conf_err->file = strdup(file);
2055 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002056 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002057
Christopher Faulet3005d282020-11-13 10:58:01 +01002058 /* handle warning message */
2059 if (*errmsg)
2060 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002061 out:
2062 return ret;
2063
2064 error:
2065 release_http_reply(reply);
2066 ret = -1;
2067 goto out;
2068
2069}
2070
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002071/* Check "errorfiles" proxy keyword */
2072static int proxy_check_errors(struct proxy *px)
2073{
2074 struct conf_errors *conf_err, *conf_err_back;
2075 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002076 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002077
2078 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2079 if (conf_err->type == 1) {
2080 /* errorfile */
2081 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002082 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002083
2084 /* For proxy, to rely on default replies, just don't reference a reply */
2085 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2086 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002087 }
2088 else {
2089 /* errorfiles */
2090 list_for_each_entry(http_errs, &http_errors_list, list) {
2091 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2092 break;
2093 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002094
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002095 /* unknown http-errors section */
2096 if (&http_errs->list == &http_errors_list) {
2097 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2098 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2099 err |= ERR_ALERT | ERR_FATAL;
2100 free(conf_err->info.errorfiles.name);
2101 goto next;
2102 }
2103
2104 free(conf_err->info.errorfiles.name);
2105 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2106 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002107 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002108 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002109 else if (conf_err->info.errorfiles.status[rc] == 2)
2110 ha_warning("config: proxy '%s' : status '%d' not declared in"
2111 " http-errors section '%s' (at %s:%d).\n",
2112 px->id, http_err_codes[rc], http_errs->id,
2113 conf_err->file, conf_err->line);
2114 }
2115 }
2116 }
2117 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002118 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002119 free(conf_err->file);
2120 free(conf_err);
2121 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002122
2123 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002124 return err;
2125}
2126
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002127static int post_check_errors()
2128{
2129 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002130 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002131 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002132 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002133
2134 node = ebpt_first(&http_error_messages);
2135 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002136 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2137 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002138 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002139 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002140 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2141 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002142 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002143 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002144 (char *)node->key);
2145 err_code |= ERR_WARN;
2146 }
2147 next:
2148 node = ebpt_next(node);
2149 }
2150
2151 return err_code;
2152}
2153
Willy Tarreau016255a2021-02-12 08:40:29 +01002154int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002155{
2156 struct conf_errors *conf_err, *new_conf_err = NULL;
2157 int ret = 0;
2158
2159 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2160 new_conf_err = calloc(1, sizeof(*new_conf_err));
2161 if (!new_conf_err) {
2162 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2163 goto out;
2164 }
2165 new_conf_err->type = conf_err->type;
2166 if (conf_err->type == 1) {
2167 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002168 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002169 }
2170 else {
2171 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2172 if (!new_conf_err->info.errorfiles.name) {
2173 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2174 goto out;
2175 }
2176 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2177 sizeof(conf_err->info.errorfiles.status));
2178 }
2179 new_conf_err->file = strdup(conf_err->file);
2180 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002181 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002182 new_conf_err = NULL;
2183 }
2184 ret = 1;
2185
2186 out:
2187 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002188 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002189}
2190
2191void proxy_release_conf_errors(struct proxy *px)
2192{
2193 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002194
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002195 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2196 if (conf_err->type == 0)
2197 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002198 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002199 free(conf_err->file);
2200 free(conf_err);
2201 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002202}
2203
2204/*
2205 * Parse an <http-errors> section.
2206 * Returns the error code, 0 if OK, or any combination of :
2207 * - ERR_ABORT: must abort ASAP
2208 * - ERR_FATAL: we can continue parsing but not start the service
2209 * - ERR_WARN: a warning has been emitted
2210 * - ERR_ALERT: an alert has been emitted
2211 * Only the two first ones can stop processing, the two others are just
2212 * indicators.
2213 */
2214static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2215{
2216 static struct http_errors *curr_errs = NULL;
2217 int err_code = 0;
2218 const char *err;
2219 char *errmsg = NULL;
2220
2221 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2222 if (!*args[1]) {
2223 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2224 err_code |= ERR_ALERT | ERR_ABORT;
2225 goto out;
2226 }
2227
2228 err = invalid_char(args[1]);
2229 if (err) {
2230 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2231 file, linenum, *err, args[0], args[1]);
2232 err_code |= ERR_ALERT | ERR_FATAL;
2233 }
2234
2235 list_for_each_entry(curr_errs, &http_errors_list, list) {
2236 /* Error if two errors section owns the same name */
2237 if (strcmp(curr_errs->id, args[1]) == 0) {
2238 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2239 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2240 err_code |= ERR_ALERT | ERR_FATAL;
2241 }
2242 }
2243
2244 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2245 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2246 err_code |= ERR_ALERT | ERR_ABORT;
2247 goto out;
2248 }
2249
Willy Tarreau2b718102021-04-21 07:32:39 +02002250 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002251 curr_errs->id = strdup(args[1]);
2252 curr_errs->conf.file = strdup(file);
2253 curr_errs->conf.line = linenum;
2254 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002255 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002256 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002257 struct buffer *msg;
2258 int status, rc;
2259
2260 if (*(args[1]) == 0 || *(args[2]) == 0) {
2261 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2262 file, linenum, args[0]);
2263 err_code |= ERR_ALERT | ERR_FATAL;
2264 goto out;
2265 }
2266
2267 status = atol(args[1]);
2268 msg = http_parse_errorfile(status, args[2], &errmsg);
2269 if (!msg) {
2270 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
2273 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002274 if (errmsg) {
2275 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2276 err_code |= ERR_WARN;
2277 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002278
2279 reply = calloc(1, sizeof(*reply));
2280 if (!reply) {
2281 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2282 err_code |= ERR_ALERT | ERR_FATAL;
2283 goto out;
2284 }
2285 reply->type = HTTP_REPLY_ERRMSG;
2286 reply->status = status;
2287 reply->ctype = NULL;
2288 LIST_INIT(&reply->hdrs);
2289 reply->body.errmsg = msg;
2290
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002291 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002292 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002293 }
2294 else if (*args[0] != 0) {
2295 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2296 err_code |= ERR_ALERT | ERR_FATAL;
2297 goto out;
2298 }
2299
2300out:
2301 free(errmsg);
2302 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002303}
2304
2305static struct cfg_kw_list cfg_kws = {ILH, {
2306 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2307 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2308 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2309 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002310 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002311 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002312 { 0, NULL, NULL },
2313}};
2314
2315INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002316REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002317REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002318
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002319REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2320
Christopher Faulet29f72842019-12-11 15:52:32 +01002321/************************************************************************/
2322/* HTX sample fetches */
2323/************************************************************************/
2324
2325/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2326static int
2327smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2328{
2329 if (!smp->strm)
2330 return 0;
2331
2332 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2333 smp->data.type = SMP_T_BOOL;
2334 return 1;
2335}
2336
2337/* Returns the number of blocks in an HTX message. The channel is chosen
2338 * depending on the sample direction. */
2339static int
2340smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2341{
2342 struct channel *chn;
2343 struct htx *htx;
2344
2345 if (!smp->strm)
2346 return 0;
2347
2348 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002349 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002350 if (!htx)
2351 return 0;
2352
2353 smp->data.u.sint = htx_nbblks(htx);
2354 smp->data.type = SMP_T_SINT;
2355 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2356 return 1;
2357}
2358
2359/* Returns the size of an HTX message. The channel is chosen depending on the
2360 * sample direction. */
2361static int
2362smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2363{
2364 struct channel *chn;
2365 struct htx *htx;
2366
2367 if (!smp->strm)
2368 return 0;
2369
2370 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002371 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002372 if (!htx)
2373 return 0;
2374
2375 smp->data.u.sint = htx->size;
2376 smp->data.type = SMP_T_SINT;
2377 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2378 return 1;
2379}
2380
2381/* Returns the data size of an HTX message. The channel is chosen depending on the
2382 * sample direction. */
2383static int
2384smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2385{
2386 struct channel *chn;
2387 struct htx *htx;
2388
2389 if (!smp->strm)
2390 return 0;
2391
2392 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002393 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002394 if (!htx)
2395 return 0;
2396
2397 smp->data.u.sint = htx->data;
2398 smp->data.type = SMP_T_SINT;
2399 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2400 return 1;
2401}
2402
2403/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2404 * depending on the sample direction. */
2405static int
2406smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2407{
2408 struct channel *chn;
2409 struct htx *htx;
2410
2411 if (!smp->strm)
2412 return 0;
2413
2414 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002415 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002416 if (!htx)
2417 return 0;
2418
2419 smp->data.u.sint = htx_used_space(htx);
2420 smp->data.type = SMP_T_SINT;
2421 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2422 return 1;
2423}
2424
2425/* Returns the free space (size-used) of an HTX message. The channel is chosen
2426 * depending on the sample direction. */
2427static int
2428smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2429{
2430 struct channel *chn;
2431 struct htx *htx;
2432
2433 if (!smp->strm)
2434 return 0;
2435
2436 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002437 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002438 if (!htx)
2439 return 0;
2440
2441 smp->data.u.sint = htx_free_space(htx);
2442 smp->data.type = SMP_T_SINT;
2443 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2444 return 1;
2445}
2446
2447/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2448 * channel is chosen depending on the sample direction. */
2449static int
2450smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2451{
2452 struct channel *chn;
2453 struct htx *htx;
2454
2455 if (!smp->strm)
2456 return 0;
2457
2458 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002459 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002460 if (!htx)
2461 return 0;
2462
2463 smp->data.u.sint = htx_free_data_space(htx);
2464 smp->data.type = SMP_T_SINT;
2465 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2466 return 1;
2467}
2468
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002469/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2470 * channel is chosen depending on the sample direction.
2471 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002472static int
2473smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2474{
2475 struct channel *chn;
2476 struct htx *htx;
2477
2478 if (!smp->strm)
2479 return 0;
2480
2481 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002482 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002483 if (!htx)
2484 return 0;
2485
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002486 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002487 smp->data.type = SMP_T_BOOL;
2488 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2489 return 1;
2490}
2491
2492/* Returns the type of a specific HTX block, if found in the message. Otherwise
2493 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2494 * "head", "tail" or "first". The channel is chosen depending on the sample
2495 * direction. */
2496static int
2497smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2498{
2499 struct channel *chn;
2500 struct htx *htx;
2501 enum htx_blk_type type;
2502 int32_t pos;
2503
2504 if (!smp->strm || !arg_p)
2505 return 0;
2506
2507 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002508 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002509 if (!htx)
2510 return 0;
2511
2512 pos = arg_p[0].data.sint;
2513 if (pos == -1)
2514 type = htx_get_head_type(htx);
2515 else if (pos == -2)
2516 type = htx_get_tail_type(htx);
2517 else if (pos == -3)
2518 type = htx_get_first_type(htx);
2519 else
2520 type = ((pos >= htx->head && pos <= htx->tail)
2521 ? htx_get_blk_type(htx_get_blk(htx, pos))
2522 : HTX_BLK_UNUSED);
2523
2524 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2525 smp->data.type = SMP_T_STR;
2526 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2527 return 1;
2528}
2529
2530/* Returns the size of a specific HTX block, if found in the message. Otherwise
2531 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2532 * "first". The channel is chosen depending on the sample direction. */
2533static int
2534smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2535{
2536 struct channel *chn;
2537 struct htx *htx;
2538 struct htx_blk *blk;
2539 int32_t pos;
2540
2541 if (!smp->strm || !arg_p)
2542 return 0;
2543
2544 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002545 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002546 if (!htx)
2547 return 0;
2548
2549 pos = arg_p[0].data.sint;
2550 if (pos == -1)
2551 blk = htx_get_head_blk(htx);
2552 else if (pos == -2)
2553 blk = htx_get_tail_blk(htx);
2554 else if (pos == -3)
2555 blk = htx_get_first_blk(htx);
2556 else
2557 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2558
2559 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2560 smp->data.type = SMP_T_SINT;
2561 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2562 return 1;
2563}
2564
2565/* Returns the start-line if the selected HTX block exists and is a
2566 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2567 * supported or "head", "tail" or "first". The channel is chosen depending on
2568 * the sample direction. */
2569static int
2570smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2571{
2572 struct buffer *temp;
2573 struct channel *chn;
2574 struct htx *htx;
2575 struct htx_blk *blk;
2576 struct htx_sl *sl;
2577 int32_t pos;
2578
2579 if (!smp->strm || !arg_p)
2580 return 0;
2581
2582 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002583 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002584 if (!htx)
2585 return 0;
2586
2587 pos = arg_p[0].data.sint;
2588 if (pos == -1)
2589 blk = htx_get_head_blk(htx);
2590 else if (pos == -2)
2591 blk = htx_get_tail_blk(htx);
2592 else if (pos == -3)
2593 blk = htx_get_first_blk(htx);
2594 else
2595 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2596
2597 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2598 smp->data.u.str.size = 0;
2599 smp->data.u.str.area = "";
2600 smp->data.u.str.data = 0;
2601 }
2602 else {
2603 sl = htx_get_blk_ptr(htx, blk);
2604
2605 temp = get_trash_chunk();
2606 chunk_istcat(temp, htx_sl_p1(sl));
2607 temp->area[temp->data++] = ' ';
2608 chunk_istcat(temp, htx_sl_p2(sl));
2609 temp->area[temp->data++] = ' ';
2610 chunk_istcat(temp, htx_sl_p3(sl));
2611
2612 smp->data.u.str = *temp;
2613 }
2614
2615 smp->data.type = SMP_T_STR;
2616 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2617 return 1;
2618}
2619
2620/* Returns the header name if the selected HTX block exists and is a header or a
2621 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2622 * supported or "head", "tail" or "first". The channel is chosen depending on
2623 * the sample direction. */
2624static int
2625smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2626{
2627 struct channel *chn;
2628 struct htx *htx;
2629 struct htx_blk *blk;
2630 int32_t pos;
2631
2632 if (!smp->strm || !arg_p)
2633 return 0;
2634
2635 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002636 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002637 if (!htx)
2638 return 0;
2639
2640 pos = arg_p[0].data.sint;
2641 if (pos == -1)
2642 blk = htx_get_head_blk(htx);
2643 else if (pos == -2)
2644 blk = htx_get_tail_blk(htx);
2645 else if (pos == -3)
2646 blk = htx_get_first_blk(htx);
2647 else
2648 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2649
2650 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2651 smp->data.u.str.size = 0;
2652 smp->data.u.str.area = "";
2653 smp->data.u.str.data = 0;
2654 }
2655 else {
2656 struct ist name = htx_get_blk_name(htx, blk);
2657
2658 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2659 }
2660 smp->data.type = SMP_T_STR;
2661 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2662 return 1;
2663}
2664
2665/* Returns the header value if the selected HTX block exists and is a header or
2666 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2667 * supported or "head", "tail" or "first". The channel is chosen depending on
2668 * the sample direction. */
2669static int
2670smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2671{
2672 struct channel *chn;
2673 struct htx *htx;
2674 struct htx_blk *blk;
2675 int32_t pos;
2676
2677 if (!smp->strm || !arg_p)
2678 return 0;
2679
2680 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002681 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002682 if (!htx)
2683 return 0;
2684
2685 pos = arg_p[0].data.sint;
2686 if (pos == -1)
2687 blk = htx_get_head_blk(htx);
2688 else if (pos == -2)
2689 blk = htx_get_tail_blk(htx);
2690 else if (pos == -3)
2691 blk = htx_get_first_blk(htx);
2692 else
2693 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2694
2695 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2696 smp->data.u.str.size = 0;
2697 smp->data.u.str.area = "";
2698 smp->data.u.str.data = 0;
2699 }
2700 else {
2701 struct ist val = htx_get_blk_value(htx, blk);
2702
2703 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2704 }
2705 smp->data.type = SMP_T_STR;
2706 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2707 return 1;
2708}
2709
2710/* Returns the value if the selected HTX block exists and is a data
2711 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2712 * or "head", "tail" or "first". The channel is chosen depending on the sample
2713 * direction. */
2714static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002715smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002716{
2717 struct channel *chn;
2718 struct htx *htx;
2719 struct htx_blk *blk;
2720 int32_t pos;
2721
2722 if (!smp->strm || !arg_p)
2723 return 0;
2724
2725 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002726 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002727 if (!htx)
2728 return 0;
2729
2730 pos = arg_p[0].data.sint;
2731 if (pos == -1)
2732 blk = htx_get_head_blk(htx);
2733 else if (pos == -2)
2734 blk = htx_get_tail_blk(htx);
2735 else if (pos == -3)
2736 blk = htx_get_first_blk(htx);
2737 else
2738 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2739
2740 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2741 smp->data.u.str.size = 0;
2742 smp->data.u.str.area = "";
2743 smp->data.u.str.data = 0;
2744 }
2745 else {
2746 struct ist val = htx_get_blk_value(htx, blk);
2747
2748 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2749 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002750 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002751 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2752 return 1;
2753}
2754
2755/* This function is used to validate the arguments passed to any "htx_blk" fetch
2756 * keywords. An argument is expected by these keywords. It must be a positive
2757 * integer or on of the following strings: "head", "tail" or "first". It returns
2758 * 0 on error, and a non-zero value if OK.
2759 */
2760int val_blk_arg(struct arg *arg, char **err_msg)
2761{
2762 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2763 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2764 return 0;
2765 }
2766 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002767 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002768 arg[0].type = ARGT_SINT;
2769 arg[0].data.sint = -1;
2770 }
2771 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002772 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002773 arg[0].type = ARGT_SINT;
2774 arg[0].data.sint = -2;
2775 }
2776 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002777 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002778 arg[0].type = ARGT_SINT;
2779 arg[0].data.sint = -3;
2780 }
2781 else {
2782 int pos;
2783
2784 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002785 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002786 memprintf(err_msg, "invalid block position");
2787 return 0;
2788 }
2789 }
2790
2791 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2792 if (pos < 0) {
2793 memprintf(err_msg, "block position must not be negative");
2794 return 0;
2795 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002796 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002797 arg[0].type = ARGT_SINT;
2798 arg[0].data.sint = pos;
2799 }
2800
2801 return 1;
2802}
2803
2804
2805/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002806 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002807 */
2808static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002809 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002810
Christopher Faulet01f44452020-01-08 14:23:40 +01002811 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2812 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2813 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2814 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2815 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2816 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2817 { "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 +01002818
Christopher Faulet01f44452020-01-08 14:23:40 +01002819 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2820 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2821 { "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},
2822 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2823 { "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 +01002824 { "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 +01002825
2826 { /* END */ },
2827}};
2828
2829INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);