blob: ea4c25f1acb31818b670eb43af029362101fd4de [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +020014#include <ctype.h>
Christopher Faulet5031ef52020-01-15 11:22:07 +010015#include <fcntl.h>
16#include <unistd.h>
17
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020022#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
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);
Tim Duesterhus77508502022-03-15 13:11:06 +0100142 p = istend(ctx->value) + ctx->lws_after;
Christopher Faulet47596d32018-10-22 09:17:28 +0200143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100149 v = istnext(v);
Christopher Faulet47596d32018-10-22 09:17:28 +0200150 }
151
152 goto return_hdr;
153 }
154
Christopher Faulet192c6a22019-06-11 16:32:24 +0200155 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200156 return 0;
157
Christopher Fauleta3f15502019-05-13 15:27:23 +0200158 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200159 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100161 if (type == HTX_BLK_EOH)
Christopher Faulet573fe732018-11-28 16:55:12 +0100162 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200163 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200164 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200165
166 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
167 const struct my_regex *re = pattern;
168
169 n = htx_get_blk_name(htx, blk);
170 if (!regex_exec2(re, n.ptr, n.len))
171 goto next_blk;
172 }
173 else {
174 const struct ist name = *(const struct ist *)(pattern);
175
Christopher Faulet47596d32018-10-22 09:17:28 +0200176 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200177 if (!istlen(name))
178 goto match;
179
Christopher Faulet47596d32018-10-22 09:17:28 +0200180 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200181 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
182 case HTTP_FIND_FL_MATCH_STR:
183 if (!isteqi(n, name))
184 goto next_blk;
185 break;
186 case HTTP_FIND_FL_MATCH_PFX:
187 if (istlen(n) < istlen(name))
188 goto next_blk;
189
190 n = ist2(istptr(n), istlen(name));
191 if (!isteqi(n, name))
192 goto next_blk;
193 break;
194 case HTTP_FIND_FL_MATCH_SFX:
195 if (istlen(n) < istlen(name))
196 goto next_blk;
197
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100198 n = ist2(istend(n) - istlen(name),
199 istlen(name));
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200200 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)) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100219 v = istnext(v);
Christopher Faulet47596d32018-10-22 09:17:28 +0200220 ctx->lws_before++;
221 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200222 if (!(flags & HTTP_FIND_FL_FULL))
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100223 v.len = http_find_hdr_value_end(v.ptr, istend(v)) - v.ptr;
224
225 while (v.len && HTTP_IS_LWS(*(istend(v) - 1))) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
391 if (!http_update_host(htx, sl, uri))
392 goto fail;
393
394 return 1;
395 fail:
396 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200397}
398
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200399/* Replace the request path in the HTX message <htx> by <path>. The host part is
400 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
401 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
402 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200403 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200404int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200405{
406 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200407 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100408 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200409 size_t plen = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200410 struct http_uri_parser parser;
Christopher Faulete010c802018-10-24 10:36:45 +0200411
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100412 if (!sl)
413 return 0;
414
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100415 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +0200416 parser = http_uri_parser_init(uri);
417 p = http_parse_path(&parser);
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 */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100435 chunk_istcat(temp, path); /* uri: new path */
Christopher Faulete010c802018-10-24 10:36:45 +0200436 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) != '?') {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100460 q = istnext(q);
Christopher Faulete010c802018-10-24 10:36:45 +0200461 }
462
463 /* skip the question mark or indicate that we must insert it
464 * (but only if the format string is not empty then).
465 */
466 if (q.len) {
Tim Duesterhus284fbe12021-11-04 22:35:44 +0100467 q = istnext(q);
Christopher Faulete010c802018-10-24 10:36:45 +0200468 }
469 else if (query.len > 1)
470 offset = 0;
471
472 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100473 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
474 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200475
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100476 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
477 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200478
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
480 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
481 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200482
483 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100484 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200485}
486
487/* Replace the response status in the HTX message <htx> by <status>. It returns
488 * 1 on success, otherwise 0.
489*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200490int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200491{
492 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200493 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200494 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200495
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100496 if (!sl)
497 return 0;
498
Christopher Faulete010c802018-10-24 10:36:45 +0200499 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
501 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200502 r = reason;
503 if (!isttest(r)) {
504 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
505 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
506 }
Christopher Faulete010c802018-10-24 10:36:45 +0200507
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100508 /* create the new start line */
509 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200510 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200511}
512
513/* Replace the response reason in the HTX message <htx> by <reason>. It returns
514 * 1 on success, otherwise 0.
515*/
516int http_replace_res_reason(struct htx *htx, const struct ist reason)
517{
518 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200519 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100520 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200521
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100522 if (!sl)
523 return 0;
524
Christopher Faulete010c802018-10-24 10:36:45 +0200525 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100526 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
527 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200528
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100529 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
530 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200531
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100532 /* create the new start line */
533 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200534}
535
Christopher Faulet47596d32018-10-22 09:17:28 +0200536/* Replaces a part of a header value referenced in the context <ctx> by
537 * <data>. It returns 1 on success, otherwise it returns 0. The context is
538 * updated if necessary.
539 */
540int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
541{
542 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200543 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200544 char *start;
545 struct ist v;
546 uint32_t len, off;
547
548 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200549 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200550
551 v = htx_get_blk_value(htx, blk);
552 start = ctx->value.ptr - ctx->lws_before;
553 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
554 off = start - v.ptr;
555
556 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
557 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200558 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200559
560 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200561
562 sl = http_get_stline(htx);
563 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
564 struct ist n = htx_get_blk_name(htx, blk);
565
566 if (isteq(n, ist("host"))) {
567 if (!http_update_authority(htx, sl, v))
568 goto fail;
569 ctx->blk = NULL;
570 http_find_header(htx, ist("host"), ctx, 1);
571 blk = ctx->blk;
572 v = htx_get_blk_value(htx, blk);
573 }
574 }
575
Christopher Faulet47596d32018-10-22 09:17:28 +0200576 ctx->blk = blk;
Tim Duesterhus77508502022-03-15 13:11:06 +0100577 ctx->value = ist2(v.ptr + off, data.len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200578 ctx->lws_before = ctx->lws_after = 0;
579
580 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200581 fail:
582 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200583}
584
585/* Fully replaces a header referenced in the context <ctx> by the name <name>
586 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
587 * context is updated if necessary.
588 */
589int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
590 const struct ist name, const struct ist value)
591{
592 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200593 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200594
595 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200596 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200597
598 blk = htx_replace_header(htx, blk, name, value);
599 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200600 goto fail;
601
602 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100603 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200604 if (!http_update_authority(htx, sl, value))
605 goto fail;
606 ctx->blk = NULL;
607 http_find_header(htx, ist("host"), ctx, 1);
608 blk = ctx->blk;
609 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200610
611 ctx->blk = blk;
612 ctx->value = ist(NULL);
613 ctx->lws_before = ctx->lws_after = 0;
614
615 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200616 fail:
617 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200618}
619
620/* Remove one value of a header. This only works on a <ctx> returned by
621 * http_find_header function. The value is removed, as well as surrounding commas
622 * if any. If the removed value was alone, the whole header is removed. The
623 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
624 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
625 * form that can be handled by http_find_header() to find next occurrence.
626 */
627int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
628{
629 struct htx_blk *blk = ctx->blk;
630 char *start;
631 struct ist v;
632 uint32_t len;
633
634 if (!blk)
635 return 0;
636
637 start = ctx->value.ptr - ctx->lws_before;
638 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
639
640 v = htx_get_blk_value(htx, blk);
641 if (len == v.len) {
642 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200643 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200644 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100645 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200646 ctx->lws_before = ctx->lws_after = 0;
647 }
648 else {
649 ctx->blk = htx_get_blk(htx, htx->tail);
650 ctx->value = htx_get_blk_value(htx, ctx->blk);
651 ctx->lws_before = ctx->lws_after = 0;
652 }
653 return 1;
654 }
655
656 /* This was not the only value of this header. We have to remove the
657 * part pointed by ctx->value. If it is the last entry of the list, we
658 * remove the last separator.
659 */
660 if (start == v.ptr) {
661 /* It's the first header part but not the only one. So remove
662 * the comma after it. */
663 len++;
664 }
665 else {
666 /* There is at least one header part before the removed one. So
667 * remove the comma between them. */
668 start--;
669 len++;
670 }
671 /* Update the block content and its len */
672 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200673 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200674
675 /* Finally update the ctx */
Tim Duesterhus77508502022-03-15 13:11:06 +0100676 ctx->value = ist2(start, 0);
Christopher Faulet47596d32018-10-22 09:17:28 +0200677 ctx->lws_before = ctx->lws_after = 0;
678
679 return 1;
680}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200681
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200682/* Updates the authority part of the uri with the value <host>. It happens when
683 * the header host is modified. It returns 0 on failure and 1 on success. It is
684 * the caller responsibility to provide the start-line and to be sure the uri
685 * contains an authority. Thus, if no authority is found in the uri, an error is
686 * returned.
687 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200688int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200689{
690 struct buffer *temp = get_trash_chunk();
691 struct ist meth, vsn, uri, authority;
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200692 struct http_uri_parser parser;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200693
694 uri = htx_sl_req_uri(sl);
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200695 parser = http_uri_parser_init(uri);
696 authority = http_parse_authority(&parser, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100697 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200698 return 0;
699
Christopher Faulet34b18e42020-02-18 11:02:21 +0100700 /* Don't update the uri if there is no change */
701 if (isteq(host, authority))
702 return 1;
703
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200704 /* Start by copying old method and version */
705 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
706 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
707
708 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
709 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
710
711 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100712 chunk_istcat(temp, host);
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100713 chunk_memcat(temp, istend(authority), istend(uri) - istend(authority));
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200714 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
715
716 return http_replace_stline(htx, meth, uri, vsn);
717
718}
719
720/* Update the header host by extracting the authority of the uri <uri>. flags of
721 * the start-line are also updated accordingly. For orgin-form and asterisk-form
722 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
723 * removed from the flags of the start-line. Otherwise, this flag is set and the
724 * authority is used to set the value of the header host. This function returns
725 * 0 on failure and 1 on success.
726*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200727int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200728{
729 struct ist authority;
730 struct http_hdr_ctx ctx;
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200731 struct http_uri_parser parser = http_uri_parser_init(uri);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200732
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200733 if (parser.format == URI_PARSER_FORMAT_EMPTY ||
734 parser.format == URI_PARSER_FORMAT_ASTERISK ||
735 parser.format == URI_PARSER_FORMAT_ABSPATH) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200736 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
Amaury Denoyelle69294b22021-07-06 11:02:22 +0200746 authority = http_parse_authority(&parser, 1);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200747 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;
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +0100920 ret = h1_headers_to_hdr_list(raw.ptr, istend(raw),
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100921 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
Tim Duesterhus77508502022-03-15 13:11:06 +01001058 raw = ist(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 }
Tim Duesterhus2b7fa9d2022-04-26 23:35:07 +02001115
1116 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1117 chunk_destroy(&http_err_chunks[rc]);
Christopher Faulet58857752020-01-15 15:19:50 +01001118}
1119
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001120REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001121REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001122
Christopher Faulet58857752020-01-15 15:19:50 +01001123/* Reads content of the error file <file> and convert it into an HTX message. On
1124 * success, the HTX message is returned. On error, NULL is returned and an error
1125 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001126 */
Christopher Faulet58857752020-01-15 15:19:50 +01001127struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001128{
Christopher Faulet58857752020-01-15 15:19:50 +01001129 struct buffer *buf = NULL;
1130 struct buffer chk;
1131 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001132 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001133 struct stat stat;
1134 char *err = NULL;
1135 int errnum, errlen;
1136 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001137
1138 /* already loaded */
1139 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1140 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001141 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1142 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001143 goto out;
1144 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001145
Christopher Faulet58857752020-01-15 15:19:50 +01001146 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001147 fd = open(file, O_RDONLY);
1148 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1149 memprintf(errmsg, "error opening file '%s'.", file);
1150 goto out;
1151 }
1152
1153 if (stat.st_size <= global.tune.bufsize)
1154 errlen = stat.st_size;
1155 else {
1156 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1157 file, global.tune.bufsize);
1158 errlen = global.tune.bufsize;
1159 }
1160
1161 err = malloc(errlen);
1162 if (!err) {
1163 memprintf(errmsg, "out of memory.");
1164 goto out;
1165 }
1166
1167 errnum = read(fd, err, errlen);
1168 if (errnum != errlen) {
1169 memprintf(errmsg, "error reading file '%s'.", file);
1170 goto out;
1171 }
1172
Christopher Faulet58857752020-01-15 15:19:50 +01001173 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001174 http_errmsg = calloc(1, sizeof(*http_errmsg));
1175 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001176 memprintf(errmsg, "out of memory.");
1177 goto out;
1178 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001179 http_errmsg->node.key = strdup(file);
1180 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001181 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001182 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001183 goto out;
1184 }
1185
1186 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001187 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1188 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001189 free(http_errmsg->node.key);
1190 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001191 goto out;
1192 }
1193
Christopher Faulet58857752020-01-15 15:19:50 +01001194 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001195 http_errmsg->msg = chk;
1196 ebis_insert(&http_error_messages, &http_errmsg->node);
1197 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001198
Christopher Faulet5031ef52020-01-15 11:22:07 +01001199 out:
1200 if (fd >= 0)
1201 close(fd);
1202 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001203 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001204}
1205
Ilya Shipitsind4259502020-04-08 01:07:56 +05001206/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001207 * message is returned. On error, NULL is returned and an error message is
1208 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001209 */
Christopher Faulet58857752020-01-15 15:19:50 +01001210struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001211{
Christopher Faulet58857752020-01-15 15:19:50 +01001212 struct buffer *buf = NULL;
1213 struct buffer chk;
1214 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001215 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001216
1217 /* already loaded */
1218 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1219 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001220 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1221 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001222 goto out;
1223 }
1224 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001225 http_errmsg = calloc(1, sizeof(*http_errmsg));
1226 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001227 memprintf(errmsg, "out of memory.");
1228 goto out;
1229 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001230 http_errmsg->node.key = strdup(key);
1231 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001232 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001233 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001234 goto out;
1235 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001236
1237 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001238 if (!http_str_to_htx(&chk, msg, errmsg)) {
1239 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001240 free(http_errmsg->node.key);
1241 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001242 goto out;
1243 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001244
Christopher Faulet58857752020-01-15 15:19:50 +01001245 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001246 http_errmsg->msg = chk;
1247 ebis_insert(&http_error_messages, &http_errmsg->node);
1248 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001249 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001250 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001251}
1252
Christopher Faulet5031ef52020-01-15 11:22:07 +01001253/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001254 * <status>. It returns NULL if there is any error, otherwise it return the
1255 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001256 */
Christopher Faulet58857752020-01-15 15:19:50 +01001257struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001258{
Christopher Faulet58857752020-01-15 15:19:50 +01001259 struct buffer *buf = NULL;
1260 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001261
1262 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1263 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001264 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001265 break;
1266 }
1267 }
1268
1269 if (rc >= HTTP_ERR_SIZE)
1270 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001271 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001272}
1273
1274/* This function creates HTX error message corresponding to a redirect message
1275 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001276 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1277 * returns NULL if there is any error, otherwise it return the corresponding HTX
1278 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001279 */
Christopher Faulet58857752020-01-15 15:19:50 +01001280struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001281{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001282 static const char *HTTP_302 =
1283 "HTTP/1.1 302 Found\r\n"
1284 "Cache-Control: no-cache\r\n"
1285 "Content-length: 0\r\n"
1286 "Location: "; /* not terminated since it will be concatenated with the URL */
1287 static const char *HTTP_303 =
1288 "HTTP/1.1 303 See Other\r\n"
1289 "Cache-Control: no-cache\r\n"
1290 "Content-length: 0\r\n"
1291 "Location: "; /* not terminated since it will be concatenated with the URL */
1292
Christopher Faulet58857752020-01-15 15:19:50 +01001293 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001294 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001295 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001296 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001297
1298 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1299 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001300 /* Create the error key */
1301 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1302 memprintf(errmsg, "out of memory.");
1303 goto out;
1304 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001305 /* Create the error message */
1306 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1307 errlen = strlen(msg) + strlen(url) + 5;
1308 err = malloc(errlen);
1309 if (!err) {
1310 memprintf(errmsg, "out of memory.");
1311 goto out;
1312 }
1313 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1314
1315 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001316 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001317 break;
1318 }
1319 }
1320
1321 if (rc >= HTTP_ERR_SIZE)
1322 memprintf(errmsg, "status code '%d' not handled.", status);
1323out:
Christopher Faulet58857752020-01-15 15:19:50 +01001324 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001325 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001326 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001327}
1328
Christopher Faulet7eea2412020-05-13 15:02:59 +02001329/* Check an "http reply" and, for replies referencing an http-errors section,
1330 * try to find the right section and the right error message in this section. If
1331 * found, the reply is updated. If the http-errors section exists but the error
1332 * message is not found, no error message is set to fallback on the default
1333 * ones. Otherwise (unknown section) an error is returned.
1334 *
1335 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1336 * filled.
1337 */
1338int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1339{
1340 struct http_errors *http_errs;
1341 int ret = 1;
1342
1343 if (reply->type != HTTP_REPLY_ERRFILES)
1344 goto end;
1345
1346 list_for_each_entry(http_errs, &http_errors_list, list) {
1347 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001348 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001349 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001350 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1351 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001352 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1353 "not declared in http-errors section '%s'.\n",
1354 px->id, reply->status, http_errs->id);
1355 break;
1356 }
1357 }
1358
1359 if (&http_errs->list == &http_errors_list) {
1360 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1361 reply->body.http_errors);
1362 ret = 0;
1363 }
1364
1365 end:
1366 return ret;
1367}
1368
Christopher Faulet47e791e2020-05-13 14:36:55 +02001369/* Parse an "http reply". It returns the reply on success or NULL on error. This
1370 * function creates one of the following http replies :
1371 *
1372 * - HTTP_REPLY_EMPTY : dummy response, no payload
1373 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1374 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1375 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1376 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1377 *
1378 * The content-type must be defined for non-empty payload. It is ignored for
1379 * error messages (implicit or explicit). When an http-errors section is
1380 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1381 * during the configuration validity check or dynamically. It is the caller
1382 * responsibility to choose. If no status code is configured, <default_status>
1383 * is set.
1384 */
1385struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1386 int default_status, char **errmsg)
1387{
1388 struct logformat_node *lf, *lfb;
1389 struct http_reply *reply = NULL;
1390 struct http_reply_hdr *hdr, *hdrb;
1391 struct stat stat;
1392 const char *act_arg = NULL;
1393 char *obj = NULL;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001394 int cur_arg, cap = 0, objlen = 0, fd = -1;
Christopher Faulet47e791e2020-05-13 14:36:55 +02001395
1396
1397 reply = calloc(1, sizeof(*reply));
1398 if (!reply) {
1399 memprintf(errmsg, "out of memory");
1400 goto error;
1401 }
1402 LIST_INIT(&reply->hdrs);
1403 reply->type = HTTP_REPLY_EMPTY;
1404 reply->status = default_status;
1405
Christopher Faulet3b967c12020-05-15 15:47:44 +02001406 if (px->conf.args.ctx == ARGC_HERR)
1407 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001408 else {
1409 if (px->cap & PR_CAP_FE)
1410 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1411 if (px->cap & PR_CAP_BE)
Willy Tarreaub39e47a2021-10-16 14:41:09 +02001412 cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001413 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001414
1415 cur_arg = *orig_arg;
1416 while (*args[cur_arg]) {
1417 if (strcmp(args[cur_arg], "status") == 0) {
1418 cur_arg++;
1419 if (!*args[cur_arg]) {
1420 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1421 goto error;
1422 }
1423 reply->status = atol(args[cur_arg]);
1424 if (reply->status < 200 || reply->status > 599) {
1425 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1426 goto error;
1427 }
1428 cur_arg++;
1429 }
1430 else if (strcmp(args[cur_arg], "content-type") == 0) {
1431 cur_arg++;
1432 if (!*args[cur_arg]) {
1433 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1434 goto error;
1435 }
1436 free(reply->ctype);
1437 reply->ctype = strdup(args[cur_arg]);
1438 cur_arg++;
1439 }
1440 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1441 if (reply->type != HTTP_REPLY_EMPTY) {
1442 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1443 goto error;
1444 }
1445 act_arg = args[cur_arg];
1446 cur_arg++;
1447 if (!*args[cur_arg]) {
1448 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1449 goto error;
1450 }
1451 reply->body.http_errors = strdup(args[cur_arg]);
1452 if (!reply->body.http_errors) {
1453 memprintf(errmsg, "out of memory");
1454 goto error;
1455 }
1456 reply->type = HTTP_REPLY_ERRFILES;
1457 cur_arg++;
1458 }
1459 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1460 if (reply->type != HTTP_REPLY_EMPTY) {
1461 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1462 goto error;
1463 }
1464 act_arg = args[cur_arg];
1465 reply->type = HTTP_REPLY_ERRMSG;
1466 cur_arg++;
1467 }
1468 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1469 if (reply->type != HTTP_REPLY_EMPTY) {
1470 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1471 goto error;
1472 }
1473 act_arg = args[cur_arg];
1474 cur_arg++;
1475 if (!*args[cur_arg]) {
1476 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1477 goto error;
1478 }
1479 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1480 if (!reply->body.errmsg) {
1481 goto error;
1482 }
1483 reply->type = HTTP_REPLY_ERRMSG;
1484 cur_arg++;
1485 }
1486 else if (strcmp(args[cur_arg], "file") == 0) {
1487 if (reply->type != HTTP_REPLY_EMPTY) {
1488 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1489 goto error;
1490 }
1491 act_arg = args[cur_arg];
1492 cur_arg++;
1493 if (!*args[cur_arg]) {
1494 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1495 goto error;
1496 }
1497 fd = open(args[cur_arg], O_RDONLY);
1498 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1499 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1500 goto error;
1501 }
1502 if (stat.st_size > global.tune.bufsize) {
1503 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1504 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1505 goto error;
1506 }
1507 objlen = stat.st_size;
1508 obj = malloc(objlen);
1509 if (!obj || read(fd, obj, objlen) != objlen) {
1510 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1511 goto error;
1512 }
1513 close(fd);
1514 fd = -1;
1515 reply->type = HTTP_REPLY_RAW;
1516 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1517 obj = NULL;
1518 cur_arg++;
1519 }
1520 else if (strcmp(args[cur_arg], "string") == 0) {
1521 if (reply->type != HTTP_REPLY_EMPTY) {
1522 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1523 goto error;
1524 }
1525 act_arg = args[cur_arg];
1526 cur_arg++;
1527 if (!*args[cur_arg]) {
1528 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1529 goto error;
1530 }
1531 obj = strdup(args[cur_arg]);
1532 objlen = strlen(args[cur_arg]);
1533 if (!obj) {
1534 memprintf(errmsg, "out of memory");
1535 goto error;
1536 }
1537 reply->type = HTTP_REPLY_RAW;
1538 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1539 obj = NULL;
1540 cur_arg++;
1541 }
1542 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1543 if (reply->type != HTTP_REPLY_EMPTY) {
1544 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1545 goto error;
1546 }
1547 act_arg = args[cur_arg];
1548 cur_arg++;
1549 if (!*args[cur_arg]) {
1550 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1551 goto error;
1552 }
1553 fd = open(args[cur_arg], O_RDONLY);
1554 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1555 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1556 goto error;
1557 }
1558 if (stat.st_size > global.tune.bufsize) {
1559 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1560 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1561 goto error;
1562 }
1563 objlen = stat.st_size;
1564 obj = malloc(objlen + 1);
1565 if (!obj || read(fd, obj, objlen) != objlen) {
1566 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1567 goto error;
1568 }
1569 close(fd);
1570 fd = -1;
1571 obj[objlen] = '\0';
1572 reply->type = HTTP_REPLY_LOGFMT;
1573 cur_arg++;
1574 }
1575 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1576 if (reply->type != HTTP_REPLY_EMPTY) {
1577 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1578 goto error;
1579 }
1580 act_arg = args[cur_arg];
1581 cur_arg++;
1582 if (!*args[cur_arg]) {
1583 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1584 goto error;
1585 }
1586 obj = strdup(args[cur_arg]);
1587 objlen = strlen(args[cur_arg]);
1588 reply->type = HTTP_REPLY_LOGFMT;
1589 cur_arg++;
1590 }
1591 else if (strcmp(args[cur_arg], "hdr") == 0) {
1592 cur_arg++;
1593 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1594 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1595 goto error;
1596 }
1597 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1598 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1599 strcasecmp(args[cur_arg], "content-type") == 0) {
1600 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1601 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1602 cur_arg += 2;
1603 continue;
1604 }
1605 hdr = calloc(1, sizeof(*hdr));
1606 if (!hdr) {
1607 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1608 goto error;
1609 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001610 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001611 LIST_INIT(&hdr->value);
1612 hdr->name = ist(strdup(args[cur_arg]));
1613 if (!isttest(hdr->name)) {
1614 memprintf(errmsg, "out of memory");
1615 goto error;
1616 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001617 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1618 goto error;
1619
1620 free(px->conf.lfs_file);
1621 px->conf.lfs_file = strdup(px->conf.args.file);
1622 px->conf.lfs_line = px->conf.args.line;
1623 cur_arg += 2;
1624 }
1625 else
1626 break;
1627 }
1628
1629 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1630 if (reply->ctype) {
1631 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1632 " neither errorfile nor payload defined.\n",
1633 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001634 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001635 }
1636 }
1637 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1638
1639 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1640 /* default errorfile or errorfiles: check the status */
1641 int rc;
1642
1643 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1644 if (http_err_codes[rc] == reply->status)
1645 break;
1646 }
1647
1648 if (rc >= HTTP_ERR_SIZE) {
1649 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1650 reply->status, act_arg);
1651 goto error;
1652 }
1653 }
1654
1655 if (reply->ctype) {
1656 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1657 "with an erorrfile.\n",
1658 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001659 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001660 }
1661 if (!LIST_ISEMPTY(&reply->hdrs)) {
1662 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1663 "with an erorrfile.\n",
1664 px->conf.args.file, px->conf.args.line);
1665 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001666 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001667 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001668 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001669 release_sample_expr(lf->expr);
1670 free(lf->arg);
1671 free(lf);
1672 }
1673 istfree(&hdr->name);
1674 free(hdr);
1675 }
1676 }
1677 }
1678 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001679 if ((reply->status == 204 || reply->status == 304) && objlen) {
1680 memprintf(errmsg, "No body expected for %d responses", reply->status);
1681 goto error;
1682 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001683 if (!reply->ctype && objlen) {
1684 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1685 goto error;
1686 }
1687 if (reply->ctype && !b_data(&reply->body.obj)) {
1688 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001689 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001690 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001691 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001692 }
1693 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1694 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1695 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1696 px->conf.args.file, px->conf.args.line);
1697 }
1698 }
1699 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1700 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001701 if ((reply->status == 204 || reply->status == 304)) {
1702 memprintf(errmsg, "No body expected for %d responses", reply->status);
1703 goto error;
1704 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001705 if (!reply->ctype) {
1706 memprintf(errmsg, "a content type must be defined with a log-format payload");
1707 goto error;
1708 }
1709 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1710 goto error;
1711
1712 free(px->conf.lfs_file);
1713 px->conf.lfs_file = strdup(px->conf.args.file);
1714 px->conf.lfs_line = px->conf.args.line;
1715 }
1716
1717 free(obj);
1718 *orig_arg = cur_arg;
1719 return reply;
1720
1721 error:
1722 free(obj);
1723 if (fd >= 0)
1724 close(fd);
1725 release_http_reply(reply);
1726 return NULL;
1727}
1728
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001729static int uri_is_default_port(const struct ist scheme, const struct ist port)
1730{
1731 return (isteq(port, ist("443")) && isteqi(scheme, ist("https://"))) ||
1732 (isteq(port, ist("80")) && isteqi(scheme, ist("http://")));
1733}
1734
1735/* Apply schemed-based normalization as described on rfc3986 on section 6.3.2.
1736 * Returns 0 if no error has been found else non-zero.
1737 *
1738 * The normalization is processed on the target-uri at the condition that it is
1739 * in absolute-form. In the case where the target-uri was normalized, every
1740 * host headers values found are also replaced by the normalized hostname. This
1741 * assumes that the target-uri and host headers were properly identify as
1742 * similar before calling this function.
1743 */
1744int http_scheme_based_normalize(struct htx *htx)
1745{
1746 struct http_hdr_ctx ctx;
1747 struct htx_sl *sl;
1748 struct ist uri, scheme, authority, host, port;
1749 char *start, *end, *ptr;
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +02001750 struct http_uri_parser parser;
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001751
1752 sl = http_get_stline(htx);
1753
1754 if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY)))
1755 return 0;
1756
1757 uri = htx_sl_req_uri(sl);
1758
Amaury Denoyelle8ac8cbf2021-07-06 10:52:58 +02001759 parser = http_uri_parser_init(uri);
1760 scheme = http_parse_scheme(&parser);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001761 /* if no scheme found, no normalization to proceed */
1762 if (!isttest(scheme))
1763 return 0;
1764
1765 /* Extract the port if present in authority. To properly support ipv6
1766 * hostnames, do a reverse search on the last ':' separator as long as
1767 * digits are found.
1768 */
Amaury Denoyelle69294b22021-07-06 11:02:22 +02001769 authority = http_parse_authority(&parser, 0);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001770 start = istptr(authority);
1771 end = istend(authority);
Amaury Denoyelle164ae4a2021-07-07 17:17:39 +02001772 for (ptr = end; ptr > start && isdigit((unsigned char)*--ptr); )
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001773 ;
1774
1775 /* if no port found, no normalization to proceed */
1776 if (likely(*ptr != ':'))
1777 return 0;
1778
1779 /* split host/port on the ':' separator found */
1780 host = ist2(start, ptr - start);
1781 port = istnext(ist2(ptr, end - ptr));
1782
1783 if (istlen(port) && uri_is_default_port(scheme, port)) {
1784 /* reconstruct the uri with removal of the port */
1785 struct buffer *temp = get_trash_chunk();
1786 struct ist meth, vsn, path;
1787
1788 /* meth */
1789 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
1790 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
1791
1792 /* vsn */
1793 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
1794 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
1795
1796 /* reconstruct uri without port */
Amaury Denoyellec453f952021-07-06 11:40:12 +02001797 path = http_parse_path(&parser);
Amaury Denoyelle4c0882b2021-07-07 10:49:26 +02001798 chunk_istcat(temp, scheme);
1799 chunk_istcat(temp, host);
1800 chunk_istcat(temp, path);
1801 uri = ist2(temp->area + meth.len + vsn.len,
1802 scheme.len + host.len + path.len);
1803
1804 http_replace_stline(htx, meth, uri, vsn);
1805
1806 /* replace every host headers values by the normalized host */
1807 ctx.blk = NULL;
1808 while (http_find_header(htx, ist("host"), &ctx, 0)) {
1809 if (!http_replace_header_value(htx, &ctx, host))
1810 goto fail;
1811 }
1812 }
1813
1814 return 0;
1815
1816 fail:
1817 return 1;
1818}
1819
Christopher Faulet07f41f72020-01-16 16:16:06 +01001820/* Parses the "errorloc[302|303]" proxy keyword */
1821static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001822 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001823 char **errmsg)
1824{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001825 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001826 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001827 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001828 int errloc, status;
1829 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001830
1831 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1832 ret = 1;
1833 goto out;
1834 }
1835
1836 if (*(args[1]) == 0 || *(args[2]) == 0) {
1837 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1838 ret = -1;
1839 goto out;
1840 }
1841
1842 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001843 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001844 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1845 if (!msg) {
1846 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1847 ret = -1;
1848 goto out;
1849 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001850
1851 reply = calloc(1, sizeof(*reply));
1852 if (!reply) {
1853 memprintf(errmsg, "%s : out of memory.", args[0]);
1854 ret = -1;
1855 goto out;
1856 }
1857 reply->type = HTTP_REPLY_ERRMSG;
1858 reply->status = status;
1859 reply->ctype = NULL;
1860 LIST_INIT(&reply->hdrs);
1861 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001862 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001863
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001864 conf_err = calloc(1, sizeof(*conf_err));
1865 if (!conf_err) {
1866 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001867 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001868 ret = -1;
1869 goto out;
1870 }
1871 conf_err->type = 1;
1872 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001873 conf_err->info.errorfile.reply = reply;
1874
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001875 conf_err->file = strdup(file);
1876 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001877 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001878
Christopher Fauleta66adf42020-11-05 22:43:41 +01001879 /* handle warning message */
1880 if (*errmsg)
1881 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001882 out:
1883 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001884
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001885}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001886
1887/* Parses the "errorfile" proxy keyword */
1888static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001889 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001890 char **errmsg)
1891{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001892 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001893 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001894 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001895 int status;
1896 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001897
1898 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1899 ret = 1;
1900 goto out;
1901 }
1902
1903 if (*(args[1]) == 0 || *(args[2]) == 0) {
1904 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1905 ret = -1;
1906 goto out;
1907 }
1908
1909 status = atol(args[1]);
1910 msg = http_parse_errorfile(status, args[2], errmsg);
1911 if (!msg) {
1912 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1913 ret = -1;
1914 goto out;
1915 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001916
Christopher Faulet5809e102020-05-14 17:31:52 +02001917 reply = calloc(1, sizeof(*reply));
1918 if (!reply) {
1919 memprintf(errmsg, "%s : out of memory.", args[0]);
1920 ret = -1;
1921 goto out;
1922 }
1923 reply->type = HTTP_REPLY_ERRMSG;
1924 reply->status = status;
1925 reply->ctype = NULL;
1926 LIST_INIT(&reply->hdrs);
1927 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001928 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001929
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001930 conf_err = calloc(1, sizeof(*conf_err));
1931 if (!conf_err) {
1932 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001933 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001934 ret = -1;
1935 goto out;
1936 }
1937 conf_err->type = 1;
1938 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001939 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001940 conf_err->file = strdup(file);
1941 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001942 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001943
Christopher Fauleta66adf42020-11-05 22:43:41 +01001944 /* handle warning message */
1945 if (*errmsg)
1946 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001947 out:
1948 return ret;
1949
1950}
1951
1952/* Parses the "errorfiles" proxy keyword */
1953static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001954 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001955 char **err)
1956{
1957 struct conf_errors *conf_err = NULL;
1958 char *name = NULL;
1959 int rc, ret = 0;
1960
1961 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1962 ret = 1;
1963 goto out;
1964 }
1965
1966 if (!*(args[1])) {
1967 memprintf(err, "%s : expects <name> as argument.", args[0]);
1968 ret = -1;
1969 goto out;
1970 }
1971
1972 name = strdup(args[1]);
1973 conf_err = calloc(1, sizeof(*conf_err));
1974 if (!name || !conf_err) {
1975 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001976 goto error;
1977 }
1978 conf_err->type = 0;
1979
1980 conf_err->info.errorfiles.name = name;
1981 if (!*(args[2])) {
1982 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1983 conf_err->info.errorfiles.status[rc] = 1;
1984 }
1985 else {
1986 int cur_arg, status;
1987 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1988 status = atol(args[cur_arg]);
1989
1990 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1991 if (http_err_codes[rc] == status) {
1992 conf_err->info.errorfiles.status[rc] = 2;
1993 break;
1994 }
1995 }
1996 if (rc >= HTTP_ERR_SIZE) {
1997 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001998 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001999 }
2000 }
2001 }
2002 conf_err->file = strdup(file);
2003 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002004 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002005 out:
2006 return ret;
2007
2008 error:
2009 free(name);
2010 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01002011 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002012 goto out;
2013}
2014
Christopher Faulet3b967c12020-05-15 15:47:44 +02002015/* Parses the "http-error" proxy keyword */
2016static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002017 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02002018 char **errmsg)
2019{
2020 struct conf_errors *conf_err;
2021 struct http_reply *reply = NULL;
2022 int rc, cur_arg, ret = 0;
2023
2024 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
2025 ret = 1;
2026 goto out;
2027 }
2028
2029 cur_arg = 1;
2030 curpx->conf.args.ctx = ARGC_HERR;
2031 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
2032 if (!reply) {
2033 memprintf(errmsg, "%s : %s", args[0], *errmsg);
2034 goto error;
2035 }
2036 else if (!reply->status) {
2037 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
2038 goto error;
2039 }
2040
2041 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2042 if (http_err_codes[rc] == reply->status)
2043 break;
2044 }
2045
2046 if (rc >= HTTP_ERR_SIZE) {
2047 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
2048 goto error;
2049 }
2050 if (*args[cur_arg]) {
2051 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
2052 goto error;
2053 }
2054
2055 conf_err = calloc(1, sizeof(*conf_err));
2056 if (!conf_err) {
2057 memprintf(errmsg, "%s : out of memory.", args[0]);
2058 goto error;
2059 }
2060 if (reply->type == HTTP_REPLY_ERRFILES) {
2061 int rc = http_get_status_idx(reply->status);
2062
2063 conf_err->type = 2;
2064 conf_err->info.errorfiles.name = reply->body.http_errors;
2065 conf_err->info.errorfiles.status[rc] = 2;
2066 reply->body.http_errors = NULL;
2067 release_http_reply(reply);
2068 }
2069 else {
2070 conf_err->type = 1;
2071 conf_err->info.errorfile.status = reply->status;
2072 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02002073 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002074 }
2075 conf_err->file = strdup(file);
2076 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002077 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02002078
Christopher Faulet3005d282020-11-13 10:58:01 +01002079 /* handle warning message */
2080 if (*errmsg)
2081 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002082 out:
2083 return ret;
2084
2085 error:
2086 release_http_reply(reply);
2087 ret = -1;
2088 goto out;
2089
2090}
2091
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002092/* Check "errorfiles" proxy keyword */
2093static int proxy_check_errors(struct proxy *px)
2094{
2095 struct conf_errors *conf_err, *conf_err_back;
2096 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002097 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002098
2099 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2100 if (conf_err->type == 1) {
2101 /* errorfile */
2102 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002103 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002104
2105 /* For proxy, to rely on default replies, just don't reference a reply */
2106 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2107 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002108 }
2109 else {
2110 /* errorfiles */
2111 list_for_each_entry(http_errs, &http_errors_list, list) {
2112 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2113 break;
2114 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002115
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002116 /* unknown http-errors section */
2117 if (&http_errs->list == &http_errors_list) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02002118 ha_alert("proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002119 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2120 err |= ERR_ALERT | ERR_FATAL;
2121 free(conf_err->info.errorfiles.name);
2122 goto next;
2123 }
2124
2125 free(conf_err->info.errorfiles.name);
2126 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2127 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002128 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002129 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002130 else if (conf_err->info.errorfiles.status[rc] == 2)
2131 ha_warning("config: proxy '%s' : status '%d' not declared in"
2132 " http-errors section '%s' (at %s:%d).\n",
2133 px->id, http_err_codes[rc], http_errs->id,
2134 conf_err->file, conf_err->line);
2135 }
2136 }
2137 }
2138 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002139 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002140 free(conf_err->file);
2141 free(conf_err);
2142 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002143
2144 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002145 return err;
2146}
2147
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002148static int post_check_errors()
2149{
2150 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002151 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002152 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002153 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002154
2155 node = ebpt_first(&http_error_messages);
2156 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002157 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2158 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002159 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002160 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002161 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2162 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002163 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002164 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002165 (char *)node->key);
2166 err_code |= ERR_WARN;
2167 }
2168 next:
2169 node = ebpt_next(node);
2170 }
2171
2172 return err_code;
2173}
2174
Willy Tarreau016255a2021-02-12 08:40:29 +01002175int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002176{
2177 struct conf_errors *conf_err, *new_conf_err = NULL;
2178 int ret = 0;
2179
2180 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2181 new_conf_err = calloc(1, sizeof(*new_conf_err));
2182 if (!new_conf_err) {
2183 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2184 goto out;
2185 }
2186 new_conf_err->type = conf_err->type;
2187 if (conf_err->type == 1) {
2188 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002189 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002190 }
2191 else {
2192 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2193 if (!new_conf_err->info.errorfiles.name) {
2194 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2195 goto out;
2196 }
2197 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2198 sizeof(conf_err->info.errorfiles.status));
2199 }
2200 new_conf_err->file = strdup(conf_err->file);
2201 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002202 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002203 new_conf_err = NULL;
2204 }
2205 ret = 1;
2206
2207 out:
2208 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002209 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002210}
2211
2212void proxy_release_conf_errors(struct proxy *px)
2213{
2214 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002215
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002216 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2217 if (conf_err->type == 0)
2218 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002219 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002220 free(conf_err->file);
2221 free(conf_err);
2222 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002223}
2224
2225/*
2226 * Parse an <http-errors> section.
2227 * Returns the error code, 0 if OK, or any combination of :
2228 * - ERR_ABORT: must abort ASAP
2229 * - ERR_FATAL: we can continue parsing but not start the service
2230 * - ERR_WARN: a warning has been emitted
2231 * - ERR_ALERT: an alert has been emitted
2232 * Only the two first ones can stop processing, the two others are just
2233 * indicators.
2234 */
2235static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2236{
2237 static struct http_errors *curr_errs = NULL;
2238 int err_code = 0;
2239 const char *err;
2240 char *errmsg = NULL;
2241
2242 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2243 if (!*args[1]) {
2244 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2245 err_code |= ERR_ALERT | ERR_ABORT;
2246 goto out;
2247 }
2248
2249 err = invalid_char(args[1]);
2250 if (err) {
2251 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2252 file, linenum, *err, args[0], args[1]);
2253 err_code |= ERR_ALERT | ERR_FATAL;
2254 }
2255
2256 list_for_each_entry(curr_errs, &http_errors_list, list) {
2257 /* Error if two errors section owns the same name */
2258 if (strcmp(curr_errs->id, args[1]) == 0) {
2259 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2260 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2261 err_code |= ERR_ALERT | ERR_FATAL;
2262 }
2263 }
2264
2265 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2266 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2267 err_code |= ERR_ALERT | ERR_ABORT;
2268 goto out;
2269 }
2270
Willy Tarreau2b718102021-04-21 07:32:39 +02002271 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002272 curr_errs->id = strdup(args[1]);
2273 curr_errs->conf.file = strdup(file);
2274 curr_errs->conf.line = linenum;
2275 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002276 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002277 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002278 struct buffer *msg;
2279 int status, rc;
2280
2281 if (*(args[1]) == 0 || *(args[2]) == 0) {
2282 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2283 file, linenum, args[0]);
2284 err_code |= ERR_ALERT | ERR_FATAL;
2285 goto out;
2286 }
2287
2288 status = atol(args[1]);
2289 msg = http_parse_errorfile(status, args[2], &errmsg);
2290 if (!msg) {
2291 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2292 err_code |= ERR_ALERT | ERR_FATAL;
2293 goto out;
2294 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002295 if (errmsg) {
2296 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2297 err_code |= ERR_WARN;
2298 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002299
2300 reply = calloc(1, sizeof(*reply));
2301 if (!reply) {
2302 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2303 err_code |= ERR_ALERT | ERR_FATAL;
2304 goto out;
2305 }
2306 reply->type = HTTP_REPLY_ERRMSG;
2307 reply->status = status;
2308 reply->ctype = NULL;
2309 LIST_INIT(&reply->hdrs);
2310 reply->body.errmsg = msg;
2311
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002312 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002313 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002314 }
2315 else if (*args[0] != 0) {
2316 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2317 err_code |= ERR_ALERT | ERR_FATAL;
2318 goto out;
2319 }
2320
2321out:
2322 free(errmsg);
2323 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002324}
2325
2326static struct cfg_kw_list cfg_kws = {ILH, {
2327 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2328 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2329 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2330 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002331 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002332 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002333 { 0, NULL, NULL },
2334}};
2335
2336INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002337REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002338REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002339
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002340REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2341
Christopher Faulet29f72842019-12-11 15:52:32 +01002342/************************************************************************/
2343/* HTX sample fetches */
2344/************************************************************************/
2345
2346/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2347static int
2348smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2349{
2350 if (!smp->strm)
2351 return 0;
2352
2353 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2354 smp->data.type = SMP_T_BOOL;
2355 return 1;
2356}
2357
2358/* Returns the number of blocks in an HTX message. The channel is chosen
2359 * depending on the sample direction. */
2360static int
2361smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2362{
2363 struct channel *chn;
2364 struct htx *htx;
2365
2366 if (!smp->strm)
2367 return 0;
2368
2369 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002370 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002371 if (!htx)
2372 return 0;
2373
2374 smp->data.u.sint = htx_nbblks(htx);
2375 smp->data.type = SMP_T_SINT;
2376 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2377 return 1;
2378}
2379
2380/* Returns the size of an HTX message. The channel is chosen depending on the
2381 * sample direction. */
2382static int
2383smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2384{
2385 struct channel *chn;
2386 struct htx *htx;
2387
2388 if (!smp->strm)
2389 return 0;
2390
2391 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002392 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002393 if (!htx)
2394 return 0;
2395
2396 smp->data.u.sint = htx->size;
2397 smp->data.type = SMP_T_SINT;
2398 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2399 return 1;
2400}
2401
2402/* Returns the data size of an HTX message. The channel is chosen depending on the
2403 * sample direction. */
2404static int
2405smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2406{
2407 struct channel *chn;
2408 struct htx *htx;
2409
2410 if (!smp->strm)
2411 return 0;
2412
2413 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002414 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002415 if (!htx)
2416 return 0;
2417
2418 smp->data.u.sint = htx->data;
2419 smp->data.type = SMP_T_SINT;
2420 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2421 return 1;
2422}
2423
2424/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2425 * depending on the sample direction. */
2426static int
2427smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2428{
2429 struct channel *chn;
2430 struct htx *htx;
2431
2432 if (!smp->strm)
2433 return 0;
2434
2435 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002436 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002437 if (!htx)
2438 return 0;
2439
2440 smp->data.u.sint = htx_used_space(htx);
2441 smp->data.type = SMP_T_SINT;
2442 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2443 return 1;
2444}
2445
2446/* Returns the free space (size-used) of an HTX message. The channel is chosen
2447 * depending on the sample direction. */
2448static int
2449smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2450{
2451 struct channel *chn;
2452 struct htx *htx;
2453
2454 if (!smp->strm)
2455 return 0;
2456
2457 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002458 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002459 if (!htx)
2460 return 0;
2461
2462 smp->data.u.sint = htx_free_space(htx);
2463 smp->data.type = SMP_T_SINT;
2464 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2465 return 1;
2466}
2467
2468/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2469 * channel is chosen depending on the sample direction. */
2470static int
2471smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2472{
2473 struct channel *chn;
2474 struct htx *htx;
2475
2476 if (!smp->strm)
2477 return 0;
2478
2479 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002480 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002481 if (!htx)
2482 return 0;
2483
2484 smp->data.u.sint = htx_free_data_space(htx);
2485 smp->data.type = SMP_T_SINT;
2486 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2487 return 1;
2488}
2489
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002490/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2491 * channel is chosen depending on the sample direction.
2492 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002493static int
2494smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2495{
2496 struct channel *chn;
2497 struct htx *htx;
2498
2499 if (!smp->strm)
2500 return 0;
2501
2502 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002503 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002504 if (!htx)
2505 return 0;
2506
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002507 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002508 smp->data.type = SMP_T_BOOL;
2509 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2510 return 1;
2511}
2512
2513/* Returns the type of a specific HTX block, if found in the message. Otherwise
2514 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2515 * "head", "tail" or "first". The channel is chosen depending on the sample
2516 * direction. */
2517static int
2518smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2519{
2520 struct channel *chn;
2521 struct htx *htx;
2522 enum htx_blk_type type;
2523 int32_t pos;
2524
2525 if (!smp->strm || !arg_p)
2526 return 0;
2527
2528 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002529 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002530 if (!htx)
2531 return 0;
2532
2533 pos = arg_p[0].data.sint;
2534 if (pos == -1)
2535 type = htx_get_head_type(htx);
2536 else if (pos == -2)
2537 type = htx_get_tail_type(htx);
2538 else if (pos == -3)
2539 type = htx_get_first_type(htx);
2540 else
2541 type = ((pos >= htx->head && pos <= htx->tail)
2542 ? htx_get_blk_type(htx_get_blk(htx, pos))
2543 : HTX_BLK_UNUSED);
2544
2545 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2546 smp->data.type = SMP_T_STR;
2547 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2548 return 1;
2549}
2550
2551/* Returns the size of a specific HTX block, if found in the message. Otherwise
2552 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2553 * "first". The channel is chosen depending on the sample direction. */
2554static int
2555smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2556{
2557 struct channel *chn;
2558 struct htx *htx;
2559 struct htx_blk *blk;
2560 int32_t pos;
2561
2562 if (!smp->strm || !arg_p)
2563 return 0;
2564
2565 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002566 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002567 if (!htx)
2568 return 0;
2569
2570 pos = arg_p[0].data.sint;
2571 if (pos == -1)
2572 blk = htx_get_head_blk(htx);
2573 else if (pos == -2)
2574 blk = htx_get_tail_blk(htx);
2575 else if (pos == -3)
2576 blk = htx_get_first_blk(htx);
2577 else
2578 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2579
2580 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2581 smp->data.type = SMP_T_SINT;
2582 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2583 return 1;
2584}
2585
2586/* Returns the start-line if the selected HTX block exists and is a
2587 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2588 * supported or "head", "tail" or "first". The channel is chosen depending on
2589 * the sample direction. */
2590static int
2591smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2592{
2593 struct buffer *temp;
2594 struct channel *chn;
2595 struct htx *htx;
2596 struct htx_blk *blk;
2597 struct htx_sl *sl;
2598 int32_t pos;
2599
2600 if (!smp->strm || !arg_p)
2601 return 0;
2602
2603 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002604 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002605 if (!htx)
2606 return 0;
2607
2608 pos = arg_p[0].data.sint;
2609 if (pos == -1)
2610 blk = htx_get_head_blk(htx);
2611 else if (pos == -2)
2612 blk = htx_get_tail_blk(htx);
2613 else if (pos == -3)
2614 blk = htx_get_first_blk(htx);
2615 else
2616 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2617
2618 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2619 smp->data.u.str.size = 0;
2620 smp->data.u.str.area = "";
2621 smp->data.u.str.data = 0;
2622 }
2623 else {
2624 sl = htx_get_blk_ptr(htx, blk);
2625
2626 temp = get_trash_chunk();
2627 chunk_istcat(temp, htx_sl_p1(sl));
2628 temp->area[temp->data++] = ' ';
2629 chunk_istcat(temp, htx_sl_p2(sl));
2630 temp->area[temp->data++] = ' ';
2631 chunk_istcat(temp, htx_sl_p3(sl));
2632
2633 smp->data.u.str = *temp;
2634 }
2635
2636 smp->data.type = SMP_T_STR;
2637 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2638 return 1;
2639}
2640
2641/* Returns the header name if the selected HTX block exists and is a header or a
2642 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2643 * supported or "head", "tail" or "first". The channel is chosen depending on
2644 * the sample direction. */
2645static int
2646smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2647{
2648 struct channel *chn;
2649 struct htx *htx;
2650 struct htx_blk *blk;
2651 int32_t pos;
2652
2653 if (!smp->strm || !arg_p)
2654 return 0;
2655
2656 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002657 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002658 if (!htx)
2659 return 0;
2660
2661 pos = arg_p[0].data.sint;
2662 if (pos == -1)
2663 blk = htx_get_head_blk(htx);
2664 else if (pos == -2)
2665 blk = htx_get_tail_blk(htx);
2666 else if (pos == -3)
2667 blk = htx_get_first_blk(htx);
2668 else
2669 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2670
2671 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2672 smp->data.u.str.size = 0;
2673 smp->data.u.str.area = "";
2674 smp->data.u.str.data = 0;
2675 }
2676 else {
2677 struct ist name = htx_get_blk_name(htx, blk);
2678
2679 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2680 }
2681 smp->data.type = SMP_T_STR;
2682 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2683 return 1;
2684}
2685
2686/* Returns the header value if the selected HTX block exists and is a header or
2687 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2688 * supported or "head", "tail" or "first". The channel is chosen depending on
2689 * the sample direction. */
2690static int
2691smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2692{
2693 struct channel *chn;
2694 struct htx *htx;
2695 struct htx_blk *blk;
2696 int32_t pos;
2697
2698 if (!smp->strm || !arg_p)
2699 return 0;
2700
2701 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002702 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002703 if (!htx)
2704 return 0;
2705
2706 pos = arg_p[0].data.sint;
2707 if (pos == -1)
2708 blk = htx_get_head_blk(htx);
2709 else if (pos == -2)
2710 blk = htx_get_tail_blk(htx);
2711 else if (pos == -3)
2712 blk = htx_get_first_blk(htx);
2713 else
2714 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2715
2716 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2717 smp->data.u.str.size = 0;
2718 smp->data.u.str.area = "";
2719 smp->data.u.str.data = 0;
2720 }
2721 else {
2722 struct ist val = htx_get_blk_value(htx, blk);
2723
2724 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2725 }
2726 smp->data.type = SMP_T_STR;
2727 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2728 return 1;
2729}
2730
2731/* Returns the value if the selected HTX block exists and is a data
2732 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2733 * or "head", "tail" or "first". The channel is chosen depending on the sample
2734 * direction. */
2735static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002736smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002737{
2738 struct channel *chn;
2739 struct htx *htx;
2740 struct htx_blk *blk;
2741 int32_t pos;
2742
2743 if (!smp->strm || !arg_p)
2744 return 0;
2745
2746 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002747 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002748 if (!htx)
2749 return 0;
2750
2751 pos = arg_p[0].data.sint;
2752 if (pos == -1)
2753 blk = htx_get_head_blk(htx);
2754 else if (pos == -2)
2755 blk = htx_get_tail_blk(htx);
2756 else if (pos == -3)
2757 blk = htx_get_first_blk(htx);
2758 else
2759 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2760
2761 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2762 smp->data.u.str.size = 0;
2763 smp->data.u.str.area = "";
2764 smp->data.u.str.data = 0;
2765 }
2766 else {
2767 struct ist val = htx_get_blk_value(htx, blk);
2768
2769 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2770 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002771 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002772 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2773 return 1;
2774}
2775
2776/* This function is used to validate the arguments passed to any "htx_blk" fetch
2777 * keywords. An argument is expected by these keywords. It must be a positive
2778 * integer or on of the following strings: "head", "tail" or "first". It returns
2779 * 0 on error, and a non-zero value if OK.
2780 */
2781int val_blk_arg(struct arg *arg, char **err_msg)
2782{
2783 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2784 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2785 return 0;
2786 }
2787 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002788 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002789 arg[0].type = ARGT_SINT;
2790 arg[0].data.sint = -1;
2791 }
2792 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002793 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002794 arg[0].type = ARGT_SINT;
2795 arg[0].data.sint = -2;
2796 }
2797 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002798 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002799 arg[0].type = ARGT_SINT;
2800 arg[0].data.sint = -3;
2801 }
2802 else {
2803 int pos;
2804
2805 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002806 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002807 memprintf(err_msg, "invalid block position");
2808 return 0;
2809 }
2810 }
2811
2812 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2813 if (pos < 0) {
2814 memprintf(err_msg, "block position must not be negative");
2815 return 0;
2816 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002817 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002818 arg[0].type = ARGT_SINT;
2819 arg[0].data.sint = pos;
2820 }
2821
2822 return 1;
2823}
2824
2825
2826/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002827 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002828 */
2829static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002830 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002831
Christopher Faulet01f44452020-01-08 14:23:40 +01002832 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2833 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2834 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2835 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2836 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2837 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2838 { "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 +01002839
Christopher Faulet01f44452020-01-08 14:23:40 +01002840 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2841 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2842 { "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},
2843 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2844 { "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 +01002845 { "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 +01002846
2847 { /* END */ },
2848}};
2849
2850INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);