blob: 67e4298880e39e6820ca0ea4e9150ad150006443 [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>
14#include <fcntl.h>
15#include <unistd.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020022#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020024#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020025#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/log.h>
27#include <haproxy/regex.h>
28#include <haproxy/sample.h>
Willy Tarreau4cbf62d2021-05-08 13:01:23 +020029#include <haproxy/tools.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Faulet47596d32018-10-22 09:17:28 +020031
Christopher Fauletf7346382019-07-17 22:02:08 +020032struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020033struct http_reply http_err_replies[HTTP_ERR_SIZE];
34
Christopher Faulet58857752020-01-15 15:19:50 +010035struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010036struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020037struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010038
Christopher Faulet76edc0f2020-01-13 15:52:01 +010039/* The declaration of an errorfiles/errorfile directives. Used during config
40 * parsing only. */
41struct conf_errors {
42 char type; /* directive type (0: errorfiles, 1: errorfile) */
43 union {
44 struct {
45 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020046 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010047 } errorfile; /* describe an "errorfile" directive */
48 struct {
49 char *name; /* the http-errors section name */
50 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
51 } errorfiles; /* describe an "errorfiles" directive */
52 } info;
53
54 char *file; /* file where the directive appears */
55 int line; /* line where the directive appears */
56
57 struct list list; /* next conf_errors */
58};
59
Christopher Faulet297fbb42019-05-13 14:41:27 +020060/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020061 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020062 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020063 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020064struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020065{
Christopher Faulet297fbb42019-05-13 14:41:27 +020066 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010067
Christopher Faulet29f17582019-05-23 11:03:26 +020068 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020069 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 +020070 return NULL;
71 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020072}
73
Christopher Faulet727a3f12020-02-07 16:39:41 +010074/* Returns the headers size in the HTX message */
75size_t http_get_hdrs_size(struct htx *htx)
76{
77 struct htx_blk *blk;
78 size_t sz = 0;
79
80 blk = htx_get_first_blk(htx);
81 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
82 return sz;
83
84 for (; blk; blk = htx_get_next_blk(htx, blk)) {
85 sz += htx_get_blksz(blk);
86 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
87 break;
88 }
89 return sz;
90}
91
Christopher Faulet8dd33e12020-05-05 07:42:42 +020092/* Finds the first or next occurrence of header matching <pattern> in the HTX
93 * message <htx> using the context <ctx>. This structure holds everything
94 * necessary to use the header and find next occurrence. If its <blk> member is
95 * NULL, the header is searched from the beginning. Otherwise, the next
96 * occurrence is returned. The function returns 1 when it finds a value, and 0
97 * when there is no more. It is designed to work with headers defined as
98 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
99 * full-line headers in whose comma is not a delimiter but is part of the
100 * syntax. A special case, if ctx->value is NULL when searching for a new values
101 * of a header, the current header is rescanned. This allows rescanning after a
102 * header deletion.
103 *
104 * The matching method is chosen by checking the flags :
105 *
106 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
107 * the regex are evaluated.
108 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
109 * to the string are evaluated.
110 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
111 * starting by the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
113 * ending by the string are evaluated.
114 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
115 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200116 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200117
118#define HTTP_FIND_FL_MATCH_STR 0x0001
119#define HTTP_FIND_FL_MATCH_PFX 0x0002
120#define HTTP_FIND_FL_MATCH_SFX 0x0003
121#define HTTP_FIND_FL_MATCH_SUB 0x0004
122#define HTTP_FIND_FL_MATCH_REG 0x0005
123/* 0x0006..0x000f: for other matching methods */
124#define HTTP_FIND_FL_MATCH_TYPE 0x000F
125#define HTTP_FIND_FL_FULL 0x0010
126
127static 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 +0200128{
129 struct htx_blk *blk = ctx->blk;
130 struct ist n, v;
131 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200132
133 if (blk) {
134 char *p;
135
Tim Duesterhused526372020-03-05 17:56:33 +0100136 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200137 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200138 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200139 goto next_blk;
140 v = htx_get_blk_value(htx, blk);
141 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
142 v.len -= (p - v.ptr);
143 v.ptr = p;
144 if (!v.len)
145 goto next_blk;
146 /* Skip comma */
147 if (*(v.ptr) == ',') {
148 v.ptr++;
149 v.len--;
150 }
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
198 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
199 if (!isteqi(n, name))
200 goto next_blk;
201 break;
202 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000203 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200204 goto next_blk;
205 break;
206 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200207 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200208 break;
209 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200210 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200211 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 v = htx_get_blk_value(htx, blk);
213
214 return_hdr:
215 ctx->lws_before = 0;
216 ctx->lws_after = 0;
217 while (v.len && HTTP_IS_LWS(*v.ptr)) {
218 v.ptr++;
219 v.len--;
220 ctx->lws_before++;
221 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200222 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200223 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
224 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
225 v.len--;
226 ctx->lws_after++;
227 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200228 ctx->blk = blk;
229 ctx->value = v;
230 return 1;
231
232 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200233 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200234 }
235
236 ctx->blk = NULL;
237 ctx->value = ist("");
238 ctx->lws_before = ctx->lws_after = 0;
239 return 0;
240}
241
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200242
243/* Header names must match <name> */
244int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
245{
246 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
247}
248
249/* Header names must match <name>. Same than http_find_header */
250int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
251{
252 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
253}
254
255
256/* Header names must start with <prefix> */
257int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
258{
259 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
260}
261
262/* Header names must end with <suffix> */
263int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
264{
265 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
266}
267/* Header names must contain <sub> */
268int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
269{
270 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
271}
272
273/* Header names must match <re> regex*/
274int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
275{
276 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
277}
278
279
Christopher Faulet47596d32018-10-22 09:17:28 +0200280/* Adds a header block int the HTX message <htx>, just before the EOH block. It
281 * returns 1 on success, otherwise it returns 0.
282 */
283int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
284{
285 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200286 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200287 enum htx_blk_type type = htx_get_tail_type(htx);
288 int32_t prev;
289
290 blk = htx_add_header(htx, n, v);
291 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200292 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200293
294 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200295 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200296
297 /* <blk> is the head, swap it iteratively with its predecessor to place
298 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200299 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200300 struct htx_blk *pblk = htx_get_blk(htx, prev);
301 enum htx_blk_type type = htx_get_blk_type(pblk);
302
303 /* Swap .addr and .info fields */
304 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
305 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
306
307 if (blk->addr == pblk->addr)
308 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200309
310 /* Stop when end-of-header is reached */
311 if (type == HTX_BLK_EOH)
312 break;
313
314 blk = pblk;
315 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200316
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200317 end:
318 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100319 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200320 if (!http_update_authority(htx, sl, v))
321 goto fail;
322 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200323 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200324
325 fail:
326 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200327}
328
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200330 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200331 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332int 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 +0200333{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200334 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200335
Christopher Faulet29f17582019-05-23 11:03:26 +0200336 blk = htx_get_first_blk(htx);
337 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200338 return 0;
339 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200340}
341
Christopher Faulete010c802018-10-24 10:36:45 +0200342/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
343 * on success, otherwise 0.
344 */
345int http_replace_req_meth(struct htx *htx, const struct ist meth)
346{
347 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200348 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100349 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200350
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100351 if (!sl)
352 return 0;
353
Christopher Faulete010c802018-10-24 10:36:45 +0200354 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100355 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
356 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200357
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100358 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
359 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200360
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100361 /* create the new start line */
362 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
363 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200364}
365
366/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
367 * success, otherwise 0.
368 */
369int http_replace_req_uri(struct htx *htx, const struct ist uri)
370{
371 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200372 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100373 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200374
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100375 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200376 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100377
Christopher Faulete010c802018-10-24 10:36:45 +0200378 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100379 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
380 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200381
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100382 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
383 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200384
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100385 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200386 if (!http_replace_stline(htx, meth, uri, vsn))
387 goto fail;
388
389 sl = http_get_stline(htx);
390 if (!http_update_host(htx, sl, uri))
391 goto fail;
392
393 return 1;
394 fail:
395 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200396}
397
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200398/* Replace the request path in the HTX message <htx> by <path>. The host part is
399 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
400 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
401 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200402 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200403int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200404{
405 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200406 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100407 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200408 size_t plen = 0;
409
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100410 if (!sl)
411 return 0;
412
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100413 uri = htx_sl_req_uri(sl);
414 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100415 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100416 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200417 if (with_qs)
418 plen = p.len;
419 else {
420 while (plen < p.len && *(p.ptr + plen) != '?')
421 plen++;
422 }
Christopher Faulete010c802018-10-24 10:36:45 +0200423
424 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100425 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
426 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200427
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100428 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
429 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
430
431 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200432 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
433 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100434 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200435
436 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100437 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200438}
439
440/* Replace the request query-string in the HTX message <htx> by <query>. The
441 * host part and the path are preserved. It returns 1 on success, otherwise
442 * 0.
443 */
444int http_replace_req_query(struct htx *htx, const struct ist query)
445{
446 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200447 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100448 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200449 int offset = 1;
450
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100451 if (!sl)
452 return 0;
453
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100454 uri = htx_sl_req_uri(sl);
455 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200456 while (q.len > 0 && *(q.ptr) != '?') {
457 q.ptr++;
458 q.len--;
459 }
460
461 /* skip the question mark or indicate that we must insert it
462 * (but only if the format string is not empty then).
463 */
464 if (q.len) {
465 q.ptr++;
466 q.len--;
467 }
468 else if (query.len > 1)
469 offset = 0;
470
471 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100472 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
473 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200474
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
476 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200477
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100478 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
479 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
480 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200481
482 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100483 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200484}
485
486/* Replace the response status in the HTX message <htx> by <status>. It returns
487 * 1 on success, otherwise 0.
488*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200489int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200490{
491 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200492 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200493 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200494
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100495 if (!sl)
496 return 0;
497
Christopher Faulete010c802018-10-24 10:36:45 +0200498 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100499 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
500 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200501 r = reason;
502 if (!isttest(r)) {
503 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
504 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
505 }
Christopher Faulete010c802018-10-24 10:36:45 +0200506
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100507 /* create the new start line */
508 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200509 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200510}
511
512/* Replace the response reason in the HTX message <htx> by <reason>. It returns
513 * 1 on success, otherwise 0.
514*/
515int http_replace_res_reason(struct htx *htx, const struct ist reason)
516{
517 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200518 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100519 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200520
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100521 if (!sl)
522 return 0;
523
Christopher Faulete010c802018-10-24 10:36:45 +0200524 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100525 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
526 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200527
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100528 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
529 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200530
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100531 /* create the new start line */
532 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200533}
534
Christopher Faulet47596d32018-10-22 09:17:28 +0200535/* Replaces a part of a header value referenced in the context <ctx> by
536 * <data>. It returns 1 on success, otherwise it returns 0. The context is
537 * updated if necessary.
538 */
539int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
540{
541 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200542 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200543 char *start;
544 struct ist v;
545 uint32_t len, off;
546
547 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200548 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200549
550 v = htx_get_blk_value(htx, blk);
551 start = ctx->value.ptr - ctx->lws_before;
552 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
553 off = start - v.ptr;
554
555 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
556 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200557 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200558
559 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200560
561 sl = http_get_stline(htx);
562 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
563 struct ist n = htx_get_blk_name(htx, blk);
564
565 if (isteq(n, ist("host"))) {
566 if (!http_update_authority(htx, sl, v))
567 goto fail;
568 ctx->blk = NULL;
569 http_find_header(htx, ist("host"), ctx, 1);
570 blk = ctx->blk;
571 v = htx_get_blk_value(htx, blk);
572 }
573 }
574
Christopher Faulet47596d32018-10-22 09:17:28 +0200575 ctx->blk = blk;
576 ctx->value.ptr = v.ptr + off;
577 ctx->value.len = data.len;
578 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 */
676 ctx->value.ptr = start;
677 ctx->value.len = 0;
678 ctx->lws_before = ctx->lws_after = 0;
679
680 return 1;
681}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200682
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200683/* Updates the authority part of the uri with the value <host>. It happens when
684 * the header host is modified. It returns 0 on failure and 1 on success. It is
685 * the caller responsibility to provide the start-line and to be sure the uri
686 * contains an authority. Thus, if no authority is found in the uri, an error is
687 * returned.
688 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200689int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200690{
691 struct buffer *temp = get_trash_chunk();
692 struct ist meth, vsn, uri, authority;
693
694 uri = htx_sl_req_uri(sl);
695 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100696 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200697 return 0;
698
Christopher Faulet34b18e42020-02-18 11:02:21 +0100699 /* Don't update the uri if there is no change */
700 if (isteq(host, authority))
701 return 1;
702
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200703 /* Start by copying old method and version */
704 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
705 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
706
707 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
708 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
709
710 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
711 chunk_memcat(temp, host.ptr, host.len);
712 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
713 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
714
715 return http_replace_stline(htx, meth, uri, vsn);
716
717}
718
719/* Update the header host by extracting the authority of the uri <uri>. flags of
720 * the start-line are also updated accordingly. For orgin-form and asterisk-form
721 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
722 * removed from the flags of the start-line. Otherwise, this flag is set and the
723 * authority is used to set the value of the header host. This function returns
724 * 0 on failure and 1 on success.
725*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200726int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200727{
728 struct ist authority;
729 struct http_hdr_ctx ctx;
730
731 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
732 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
733 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
734 }
735 else {
736 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
737 if (sl->info.req.meth != HTTP_METH_CONNECT) {
738 // absolute-form (RFC7320 #5.3.2)
739 sl->flags |= HTX_SL_F_HAS_SCHM;
740 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
741 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
742
743 authority = http_get_authority(uri, 1);
744 if (!authority.len)
745 goto fail;
746 }
747 else {
748 // authority-form (RFC7320 #5.3.3)
749 authority = uri;
750 }
751
752 /* Replace header host value */
753 ctx.blk = NULL;
754 while (http_find_header(htx, ist("host"), &ctx, 1)) {
755 if (!http_replace_header_value(htx, &ctx, authority))
756 goto fail;
757 }
758
759 }
760 return 1;
761 fail:
762 return 0;
763}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200764
765/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
766 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
767 * performed over the whole headers. Otherwise it must contain a valid header
768 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
769 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
770 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
771 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
772 * -1. The value fetch stops at commas, so this function is suited for use with
773 * list headers.
774 * The return value is 0 if nothing was found, or non-zero otherwise.
775 */
776unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
777 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
778{
779 struct http_hdr_ctx local_ctx;
780 struct ist val_hist[MAX_HDR_HISTORY];
781 unsigned int hist_idx;
782 int found;
783
784 if (!ctx) {
785 local_ctx.blk = NULL;
786 ctx = &local_ctx;
787 }
788
789 if (occ >= 0) {
790 /* search from the beginning */
791 while (http_find_header(htx, hdr, ctx, 0)) {
792 occ--;
793 if (occ <= 0) {
794 *vptr = ctx->value.ptr;
795 *vlen = ctx->value.len;
796 return 1;
797 }
798 }
799 return 0;
800 }
801
802 /* negative occurrence, we scan all the list then walk back */
803 if (-occ > MAX_HDR_HISTORY)
804 return 0;
805
806 found = hist_idx = 0;
807 while (http_find_header(htx, hdr, ctx, 0)) {
808 val_hist[hist_idx] = ctx->value;
809 if (++hist_idx >= MAX_HDR_HISTORY)
810 hist_idx = 0;
811 found++;
812 }
813 if (-occ > found)
814 return 0;
815
816 /* OK now we have the last occurrence in [hist_idx-1], and we need to
817 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
818 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
819 * to remain in the 0..9 range.
820 */
821 hist_idx += occ + MAX_HDR_HISTORY;
822 if (hist_idx >= MAX_HDR_HISTORY)
823 hist_idx -= MAX_HDR_HISTORY;
824 *vptr = val_hist[hist_idx].ptr;
825 *vlen = val_hist[hist_idx].len;
826 return 1;
827}
828
829/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
830 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
831 * performed over the whole headers. Otherwise it must contain a valid header
832 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
833 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
834 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
835 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
836 * -1. This function differs from http_get_hdr() in that it only returns full
837 * line header values and does not stop at commas.
838 * The return value is 0 if nothing was found, or non-zero otherwise.
839 */
840unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
841 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
842{
843 struct http_hdr_ctx local_ctx;
844 struct ist val_hist[MAX_HDR_HISTORY];
845 unsigned int hist_idx;
846 int found;
847
848 if (!ctx) {
849 local_ctx.blk = NULL;
850 ctx = &local_ctx;
851 }
852
853 if (occ >= 0) {
854 /* search from the beginning */
855 while (http_find_header(htx, hdr, ctx, 1)) {
856 occ--;
857 if (occ <= 0) {
858 *vptr = ctx->value.ptr;
859 *vlen = ctx->value.len;
860 return 1;
861 }
862 }
863 return 0;
864 }
865
866 /* negative occurrence, we scan all the list then walk back */
867 if (-occ > MAX_HDR_HISTORY)
868 return 0;
869
870 found = hist_idx = 0;
871 while (http_find_header(htx, hdr, ctx, 1)) {
872 val_hist[hist_idx] = ctx->value;
873 if (++hist_idx >= MAX_HDR_HISTORY)
874 hist_idx = 0;
875 found++;
876 }
877 if (-occ > found)
878 return 0;
879
880 /* OK now we have the last occurrence in [hist_idx-1], and we need to
881 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
882 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
883 * to remain in the 0..9 range.
884 */
885 hist_idx += occ + MAX_HDR_HISTORY;
886 if (hist_idx >= MAX_HDR_HISTORY)
887 hist_idx -= MAX_HDR_HISTORY;
888 *vptr = val_hist[hist_idx].ptr;
889 *vlen = val_hist[hist_idx].len;
890 return 1;
891}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100892
Christopher Fauleta66adf42020-11-05 22:43:41 +0100893int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100894{
895 struct htx *htx;
896 struct htx_sl *sl;
897 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200898 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100899 union h1_sl h1sl;
900 unsigned int flags = HTX_SL_F_IS_RESP;
901 int ret = 0;
902
Christopher Faulet90cc4812019-07-22 16:49:30 +0200903 b_reset(buf);
904 if (!raw.len) {
905 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100906 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200907 return 1;
908 }
909
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100910 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200911 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100912 if (!buf->area)
913 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100914
915 h1m_init_res(&h1m);
916 h1m.flags |= H1_MF_NO_PHDR;
917 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
918 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100919 if (ret <= 0) {
920 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100921 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100922 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100923
Christopher Fauleta66adf42020-11-05 22:43:41 +0100924 if (unlikely(h1sl.st.v.len != 8)) {
925 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100926 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100927 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100928 if ((*(h1sl.st.v.ptr + 5) > '1') ||
929 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
930 h1m.flags |= H1_MF_VER_11;
931
Christopher Fauleta66adf42020-11-05 22:43:41 +0100932 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
933 memprintf(errmsg, "invalid http status code for an error message (%u)",
934 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200935 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100936 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200937
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200938 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
939 /* Responses known to have no body. */
940 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
941 h1m.flags |= H1_MF_XFER_LEN;
942 h1m.curr_len = h1m.body_len = 0;
943 }
944 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
945 h1m.flags |= H1_MF_XFER_LEN;
946
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100947 if (h1m.flags & H1_MF_VER_11)
948 flags |= HTX_SL_F_VER_11;
949 if (h1m.flags & H1_MF_XFER_ENC)
950 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200951 if (h1m.flags & H1_MF_XFER_LEN) {
952 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100953 if (h1m.flags & H1_MF_CHNK) {
954 memprintf(errmsg, "chunk-encoded payload not supported");
955 goto error;
956 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200957 else if (h1m.flags & H1_MF_CLEN) {
958 flags |= HTX_SL_F_CLEN;
959 if (h1m.body_len == 0)
960 flags |= HTX_SL_F_BODYLESS;
961 }
962 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200963 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100964 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200965
Christopher Fauleta66adf42020-11-05 22:43:41 +0100966 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
967 memprintf(errmsg, "message payload not expected");
968 goto error;
969 }
970 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
971 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100972 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100973 goto error;
974 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100975
976 htx = htx_from_buf(buf);
977 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 +0100978 if (!sl || !htx_add_all_headers(htx, hdrs)) {
979 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100980 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100981 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100982 sl->info.res.status = h1sl.st.status;
983
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200984 while (raw.len > ret) {
985 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100986 if (!sent) {
987 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100988 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100989 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200990 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100991 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200992
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100993 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200994
Christopher Faulet90cc4812019-07-22 16:49:30 +0200995 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100996
997error:
998 if (buf->size)
999 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001000 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001001}
1002
Christopher Faulet18630642020-05-12 18:57:28 +02001003void release_http_reply(struct http_reply *http_reply)
1004{
1005 struct logformat_node *lf, *lfb;
1006 struct http_reply_hdr *hdr, *hdrb;
1007
1008 if (!http_reply)
1009 return;
1010
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001011 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001012 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001013 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001014 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001015 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001016 release_sample_expr(lf->expr);
1017 free(lf->arg);
1018 free(lf);
1019 }
1020 istfree(&hdr->name);
1021 free(hdr);
1022 }
1023
1024 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001025 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001026 }
1027 else if (http_reply->type == HTTP_REPLY_RAW)
1028 chunk_destroy(&http_reply->body.obj);
1029 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1030 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001031 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001032 release_sample_expr(lf->expr);
1033 free(lf->arg);
1034 free(lf);
1035 }
1036 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001037 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001038}
1039
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001040static int http_htx_init(void)
1041{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001042 struct buffer chk;
1043 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001044 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001045 int rc;
1046 int err_code = 0;
1047
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001048 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1049 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001050 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001051 err_code |= ERR_ALERT | ERR_FATAL;
1052 continue;
1053 }
1054
1055 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Fauleta66adf42020-11-05 22:43:41 +01001056 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1057 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1058 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001059 err_code |= ERR_ALERT | ERR_FATAL;
1060 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001061 else if (errmsg) {
1062 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1063 err_code |= ERR_WARN;
1064 }
1065
1066 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001067 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001068
Christopher Fauletf7346382019-07-17 22:02:08 +02001069 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001070 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1071 http_err_replies[rc].status = http_err_codes[rc];
1072 http_err_replies[rc].ctype = NULL;
1073 LIST_INIT(&http_err_replies[rc].hdrs);
1074 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001075 }
1076end:
1077 return err_code;
1078}
1079
Christopher Faulet58857752020-01-15 15:19:50 +01001080static void http_htx_deinit(void)
1081{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001082 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001083 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001084 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001085 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001086 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001087
1088 node = ebpt_first(&http_error_messages);
1089 while (node) {
1090 next = ebpt_next(node);
1091 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001092 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1093 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001094 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001095 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001096 node = next;
1097 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001098
1099 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1100 free(http_errs->conf.file);
1101 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001102 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1103 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001104 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001105 free(http_errs);
1106 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001107
1108 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001109 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001110 release_http_reply(http_rep);
1111 }
Christopher Faulet58857752020-01-15 15:19:50 +01001112}
1113
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001114REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001115REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001116
Christopher Faulet58857752020-01-15 15:19:50 +01001117/* Reads content of the error file <file> and convert it into an HTX message. On
1118 * success, the HTX message is returned. On error, NULL is returned and an error
1119 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001120 */
Christopher Faulet58857752020-01-15 15:19:50 +01001121struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001122{
Christopher Faulet58857752020-01-15 15:19:50 +01001123 struct buffer *buf = NULL;
1124 struct buffer chk;
1125 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001126 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001127 struct stat stat;
1128 char *err = NULL;
1129 int errnum, errlen;
1130 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001131
1132 /* already loaded */
1133 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1134 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001135 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1136 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001137 goto out;
1138 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001139
Christopher Faulet58857752020-01-15 15:19:50 +01001140 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001141 fd = open(file, O_RDONLY);
1142 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1143 memprintf(errmsg, "error opening file '%s'.", file);
1144 goto out;
1145 }
1146
1147 if (stat.st_size <= global.tune.bufsize)
1148 errlen = stat.st_size;
1149 else {
1150 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1151 file, global.tune.bufsize);
1152 errlen = global.tune.bufsize;
1153 }
1154
1155 err = malloc(errlen);
1156 if (!err) {
1157 memprintf(errmsg, "out of memory.");
1158 goto out;
1159 }
1160
1161 errnum = read(fd, err, errlen);
1162 if (errnum != errlen) {
1163 memprintf(errmsg, "error reading file '%s'.", file);
1164 goto out;
1165 }
1166
Christopher Faulet58857752020-01-15 15:19:50 +01001167 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001168 http_errmsg = calloc(1, sizeof(*http_errmsg));
1169 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001170 memprintf(errmsg, "out of memory.");
1171 goto out;
1172 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001173 http_errmsg->node.key = strdup(file);
1174 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001175 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001176 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001177 goto out;
1178 }
1179
1180 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001181 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1182 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001183 free(http_errmsg->node.key);
1184 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001185 goto out;
1186 }
1187
Christopher Faulet58857752020-01-15 15:19:50 +01001188 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001189 http_errmsg->msg = chk;
1190 ebis_insert(&http_error_messages, &http_errmsg->node);
1191 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001192
Christopher Faulet5031ef52020-01-15 11:22:07 +01001193 out:
1194 if (fd >= 0)
1195 close(fd);
1196 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001197 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001198}
1199
Ilya Shipitsind4259502020-04-08 01:07:56 +05001200/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001201 * message is returned. On error, NULL is returned and an error message is
1202 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001203 */
Christopher Faulet58857752020-01-15 15:19:50 +01001204struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001205{
Christopher Faulet58857752020-01-15 15:19:50 +01001206 struct buffer *buf = NULL;
1207 struct buffer chk;
1208 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001209 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001210
1211 /* already loaded */
1212 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1213 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001214 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1215 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001216 goto out;
1217 }
1218 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001219 http_errmsg = calloc(1, sizeof(*http_errmsg));
1220 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001221 memprintf(errmsg, "out of memory.");
1222 goto out;
1223 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001224 http_errmsg->node.key = strdup(key);
1225 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001226 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001227 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001228 goto out;
1229 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001230
1231 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001232 if (!http_str_to_htx(&chk, msg, errmsg)) {
1233 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001234 free(http_errmsg->node.key);
1235 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001236 goto out;
1237 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001238
Christopher Faulet58857752020-01-15 15:19:50 +01001239 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001240 http_errmsg->msg = chk;
1241 ebis_insert(&http_error_messages, &http_errmsg->node);
1242 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001243 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001244 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001245}
1246
Christopher Faulet5031ef52020-01-15 11:22:07 +01001247/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001248 * <status>. It returns NULL if there is any error, otherwise it return the
1249 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001250 */
Christopher Faulet58857752020-01-15 15:19:50 +01001251struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001252{
Christopher Faulet58857752020-01-15 15:19:50 +01001253 struct buffer *buf = NULL;
1254 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001255
1256 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1257 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001258 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001259 break;
1260 }
1261 }
1262
1263 if (rc >= HTTP_ERR_SIZE)
1264 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001265 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001266}
1267
1268/* This function creates HTX error message corresponding to a redirect message
1269 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001270 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1271 * returns NULL if there is any error, otherwise it return the corresponding HTX
1272 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001273 */
Christopher Faulet58857752020-01-15 15:19:50 +01001274struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001275{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001276 static const char *HTTP_302 =
1277 "HTTP/1.1 302 Found\r\n"
1278 "Cache-Control: no-cache\r\n"
1279 "Content-length: 0\r\n"
1280 "Location: "; /* not terminated since it will be concatenated with the URL */
1281 static const char *HTTP_303 =
1282 "HTTP/1.1 303 See Other\r\n"
1283 "Cache-Control: no-cache\r\n"
1284 "Content-length: 0\r\n"
1285 "Location: "; /* not terminated since it will be concatenated with the URL */
1286
Christopher Faulet58857752020-01-15 15:19:50 +01001287 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001288 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001289 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001290 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001291
1292 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1293 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001294 /* Create the error key */
1295 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1296 memprintf(errmsg, "out of memory.");
1297 goto out;
1298 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001299 /* Create the error message */
1300 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1301 errlen = strlen(msg) + strlen(url) + 5;
1302 err = malloc(errlen);
1303 if (!err) {
1304 memprintf(errmsg, "out of memory.");
1305 goto out;
1306 }
1307 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1308
1309 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001310 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001311 break;
1312 }
1313 }
1314
1315 if (rc >= HTTP_ERR_SIZE)
1316 memprintf(errmsg, "status code '%d' not handled.", status);
1317out:
Christopher Faulet58857752020-01-15 15:19:50 +01001318 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001319 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001320 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001321}
1322
Christopher Faulet7eea2412020-05-13 15:02:59 +02001323/* Check an "http reply" and, for replies referencing an http-errors section,
1324 * try to find the right section and the right error message in this section. If
1325 * found, the reply is updated. If the http-errors section exists but the error
1326 * message is not found, no error message is set to fallback on the default
1327 * ones. Otherwise (unknown section) an error is returned.
1328 *
1329 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1330 * filled.
1331 */
1332int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1333{
1334 struct http_errors *http_errs;
1335 int ret = 1;
1336
1337 if (reply->type != HTTP_REPLY_ERRFILES)
1338 goto end;
1339
1340 list_for_each_entry(http_errs, &http_errors_list, list) {
1341 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001342 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001343 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001344 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1345 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001346 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1347 "not declared in http-errors section '%s'.\n",
1348 px->id, reply->status, http_errs->id);
1349 break;
1350 }
1351 }
1352
1353 if (&http_errs->list == &http_errors_list) {
1354 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1355 reply->body.http_errors);
1356 ret = 0;
1357 }
1358
1359 end:
1360 return ret;
1361}
1362
Christopher Faulet47e791e2020-05-13 14:36:55 +02001363/* Parse an "http reply". It returns the reply on success or NULL on error. This
1364 * function creates one of the following http replies :
1365 *
1366 * - HTTP_REPLY_EMPTY : dummy response, no payload
1367 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1368 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1369 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1370 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1371 *
1372 * The content-type must be defined for non-empty payload. It is ignored for
1373 * error messages (implicit or explicit). When an http-errors section is
1374 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1375 * during the configuration validity check or dynamically. It is the caller
1376 * responsibility to choose. If no status code is configured, <default_status>
1377 * is set.
1378 */
1379struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1380 int default_status, char **errmsg)
1381{
1382 struct logformat_node *lf, *lfb;
1383 struct http_reply *reply = NULL;
1384 struct http_reply_hdr *hdr, *hdrb;
1385 struct stat stat;
1386 const char *act_arg = NULL;
1387 char *obj = NULL;
1388 int cur_arg, cap, objlen = 0, fd = -1;
1389
1390
1391 reply = calloc(1, sizeof(*reply));
1392 if (!reply) {
1393 memprintf(errmsg, "out of memory");
1394 goto error;
1395 }
1396 LIST_INIT(&reply->hdrs);
1397 reply->type = HTTP_REPLY_EMPTY;
1398 reply->status = default_status;
1399
Christopher Faulet3b967c12020-05-15 15:47:44 +02001400 if (px->conf.args.ctx == ARGC_HERR)
1401 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1402 else
1403 cap = ((px->conf.args.ctx == ARGC_HRQ)
1404 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1405 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001406
1407 cur_arg = *orig_arg;
1408 while (*args[cur_arg]) {
1409 if (strcmp(args[cur_arg], "status") == 0) {
1410 cur_arg++;
1411 if (!*args[cur_arg]) {
1412 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1413 goto error;
1414 }
1415 reply->status = atol(args[cur_arg]);
1416 if (reply->status < 200 || reply->status > 599) {
1417 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1418 goto error;
1419 }
1420 cur_arg++;
1421 }
1422 else if (strcmp(args[cur_arg], "content-type") == 0) {
1423 cur_arg++;
1424 if (!*args[cur_arg]) {
1425 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1426 goto error;
1427 }
1428 free(reply->ctype);
1429 reply->ctype = strdup(args[cur_arg]);
1430 cur_arg++;
1431 }
1432 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1433 if (reply->type != HTTP_REPLY_EMPTY) {
1434 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1435 goto error;
1436 }
1437 act_arg = args[cur_arg];
1438 cur_arg++;
1439 if (!*args[cur_arg]) {
1440 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1441 goto error;
1442 }
1443 reply->body.http_errors = strdup(args[cur_arg]);
1444 if (!reply->body.http_errors) {
1445 memprintf(errmsg, "out of memory");
1446 goto error;
1447 }
1448 reply->type = HTTP_REPLY_ERRFILES;
1449 cur_arg++;
1450 }
1451 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1452 if (reply->type != HTTP_REPLY_EMPTY) {
1453 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1454 goto error;
1455 }
1456 act_arg = args[cur_arg];
1457 reply->type = HTTP_REPLY_ERRMSG;
1458 cur_arg++;
1459 }
1460 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1461 if (reply->type != HTTP_REPLY_EMPTY) {
1462 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1463 goto error;
1464 }
1465 act_arg = args[cur_arg];
1466 cur_arg++;
1467 if (!*args[cur_arg]) {
1468 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1469 goto error;
1470 }
1471 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1472 if (!reply->body.errmsg) {
1473 goto error;
1474 }
1475 reply->type = HTTP_REPLY_ERRMSG;
1476 cur_arg++;
1477 }
1478 else if (strcmp(args[cur_arg], "file") == 0) {
1479 if (reply->type != HTTP_REPLY_EMPTY) {
1480 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1481 goto error;
1482 }
1483 act_arg = args[cur_arg];
1484 cur_arg++;
1485 if (!*args[cur_arg]) {
1486 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1487 goto error;
1488 }
1489 fd = open(args[cur_arg], O_RDONLY);
1490 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1491 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1492 goto error;
1493 }
1494 if (stat.st_size > global.tune.bufsize) {
1495 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1496 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1497 goto error;
1498 }
1499 objlen = stat.st_size;
1500 obj = malloc(objlen);
1501 if (!obj || read(fd, obj, objlen) != objlen) {
1502 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1503 goto error;
1504 }
1505 close(fd);
1506 fd = -1;
1507 reply->type = HTTP_REPLY_RAW;
1508 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1509 obj = NULL;
1510 cur_arg++;
1511 }
1512 else if (strcmp(args[cur_arg], "string") == 0) {
1513 if (reply->type != HTTP_REPLY_EMPTY) {
1514 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1515 goto error;
1516 }
1517 act_arg = args[cur_arg];
1518 cur_arg++;
1519 if (!*args[cur_arg]) {
1520 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1521 goto error;
1522 }
1523 obj = strdup(args[cur_arg]);
1524 objlen = strlen(args[cur_arg]);
1525 if (!obj) {
1526 memprintf(errmsg, "out of memory");
1527 goto error;
1528 }
1529 reply->type = HTTP_REPLY_RAW;
1530 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1531 obj = NULL;
1532 cur_arg++;
1533 }
1534 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1535 if (reply->type != HTTP_REPLY_EMPTY) {
1536 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1537 goto error;
1538 }
1539 act_arg = args[cur_arg];
1540 cur_arg++;
1541 if (!*args[cur_arg]) {
1542 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1543 goto error;
1544 }
1545 fd = open(args[cur_arg], O_RDONLY);
1546 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1547 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1548 goto error;
1549 }
1550 if (stat.st_size > global.tune.bufsize) {
1551 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1552 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1553 goto error;
1554 }
1555 objlen = stat.st_size;
1556 obj = malloc(objlen + 1);
1557 if (!obj || read(fd, obj, objlen) != objlen) {
1558 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1559 goto error;
1560 }
1561 close(fd);
1562 fd = -1;
1563 obj[objlen] = '\0';
1564 reply->type = HTTP_REPLY_LOGFMT;
1565 cur_arg++;
1566 }
1567 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1568 if (reply->type != HTTP_REPLY_EMPTY) {
1569 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1570 goto error;
1571 }
1572 act_arg = args[cur_arg];
1573 cur_arg++;
1574 if (!*args[cur_arg]) {
1575 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1576 goto error;
1577 }
1578 obj = strdup(args[cur_arg]);
1579 objlen = strlen(args[cur_arg]);
1580 reply->type = HTTP_REPLY_LOGFMT;
1581 cur_arg++;
1582 }
1583 else if (strcmp(args[cur_arg], "hdr") == 0) {
1584 cur_arg++;
1585 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1586 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1587 goto error;
1588 }
1589 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1590 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1591 strcasecmp(args[cur_arg], "content-type") == 0) {
1592 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1593 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1594 cur_arg += 2;
1595 continue;
1596 }
1597 hdr = calloc(1, sizeof(*hdr));
1598 if (!hdr) {
1599 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1600 goto error;
1601 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001602 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001603 LIST_INIT(&hdr->value);
1604 hdr->name = ist(strdup(args[cur_arg]));
1605 if (!isttest(hdr->name)) {
1606 memprintf(errmsg, "out of memory");
1607 goto error;
1608 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001609 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1610 goto error;
1611
1612 free(px->conf.lfs_file);
1613 px->conf.lfs_file = strdup(px->conf.args.file);
1614 px->conf.lfs_line = px->conf.args.line;
1615 cur_arg += 2;
1616 }
1617 else
1618 break;
1619 }
1620
1621 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1622 if (reply->ctype) {
1623 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1624 " neither errorfile nor payload defined.\n",
1625 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001626 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001627 }
1628 }
1629 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1630
1631 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1632 /* default errorfile or errorfiles: check the status */
1633 int rc;
1634
1635 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1636 if (http_err_codes[rc] == reply->status)
1637 break;
1638 }
1639
1640 if (rc >= HTTP_ERR_SIZE) {
1641 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1642 reply->status, act_arg);
1643 goto error;
1644 }
1645 }
1646
1647 if (reply->ctype) {
1648 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1649 "with an erorrfile.\n",
1650 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001651 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001652 }
1653 if (!LIST_ISEMPTY(&reply->hdrs)) {
1654 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1655 "with an erorrfile.\n",
1656 px->conf.args.file, px->conf.args.line);
1657 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001658 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001659 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001660 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001661 release_sample_expr(lf->expr);
1662 free(lf->arg);
1663 free(lf);
1664 }
1665 istfree(&hdr->name);
1666 free(hdr);
1667 }
1668 }
1669 }
1670 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001671 if ((reply->status == 204 || reply->status == 304) && objlen) {
1672 memprintf(errmsg, "No body expected for %d responses", reply->status);
1673 goto error;
1674 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001675 if (!reply->ctype && objlen) {
1676 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1677 goto error;
1678 }
1679 if (reply->ctype && !b_data(&reply->body.obj)) {
1680 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001681 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001682 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001683 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001684 }
1685 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1686 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1687 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1688 px->conf.args.file, px->conf.args.line);
1689 }
1690 }
1691 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1692 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001693 if ((reply->status == 204 || reply->status == 304)) {
1694 memprintf(errmsg, "No body expected for %d responses", reply->status);
1695 goto error;
1696 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001697 if (!reply->ctype) {
1698 memprintf(errmsg, "a content type must be defined with a log-format payload");
1699 goto error;
1700 }
1701 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1702 goto error;
1703
1704 free(px->conf.lfs_file);
1705 px->conf.lfs_file = strdup(px->conf.args.file);
1706 px->conf.lfs_line = px->conf.args.line;
1707 }
1708
1709 free(obj);
1710 *orig_arg = cur_arg;
1711 return reply;
1712
1713 error:
1714 free(obj);
1715 if (fd >= 0)
1716 close(fd);
1717 release_http_reply(reply);
1718 return NULL;
1719}
1720
Christopher Faulet07f41f72020-01-16 16:16:06 +01001721/* Parses the "errorloc[302|303]" proxy keyword */
1722static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001723 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001724 char **errmsg)
1725{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001726 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001727 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001728 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001729 int errloc, status;
1730 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001731
1732 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1733 ret = 1;
1734 goto out;
1735 }
1736
1737 if (*(args[1]) == 0 || *(args[2]) == 0) {
1738 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1739 ret = -1;
1740 goto out;
1741 }
1742
1743 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001744 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001745 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1746 if (!msg) {
1747 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1748 ret = -1;
1749 goto out;
1750 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001751
1752 reply = calloc(1, sizeof(*reply));
1753 if (!reply) {
1754 memprintf(errmsg, "%s : out of memory.", args[0]);
1755 ret = -1;
1756 goto out;
1757 }
1758 reply->type = HTTP_REPLY_ERRMSG;
1759 reply->status = status;
1760 reply->ctype = NULL;
1761 LIST_INIT(&reply->hdrs);
1762 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001763 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001764
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001765 conf_err = calloc(1, sizeof(*conf_err));
1766 if (!conf_err) {
1767 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001768 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001769 ret = -1;
1770 goto out;
1771 }
1772 conf_err->type = 1;
1773 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001774 conf_err->info.errorfile.reply = reply;
1775
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001776 conf_err->file = strdup(file);
1777 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001778 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001779
Christopher Fauleta66adf42020-11-05 22:43:41 +01001780 /* handle warning message */
1781 if (*errmsg)
1782 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001783 out:
1784 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001785
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001786}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001787
1788/* Parses the "errorfile" proxy keyword */
1789static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001790 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001791 char **errmsg)
1792{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001793 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001794 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001795 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001796 int status;
1797 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001798
1799 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1800 ret = 1;
1801 goto out;
1802 }
1803
1804 if (*(args[1]) == 0 || *(args[2]) == 0) {
1805 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1806 ret = -1;
1807 goto out;
1808 }
1809
1810 status = atol(args[1]);
1811 msg = http_parse_errorfile(status, args[2], errmsg);
1812 if (!msg) {
1813 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1814 ret = -1;
1815 goto out;
1816 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001817
Christopher Faulet5809e102020-05-14 17:31:52 +02001818 reply = calloc(1, sizeof(*reply));
1819 if (!reply) {
1820 memprintf(errmsg, "%s : out of memory.", args[0]);
1821 ret = -1;
1822 goto out;
1823 }
1824 reply->type = HTTP_REPLY_ERRMSG;
1825 reply->status = status;
1826 reply->ctype = NULL;
1827 LIST_INIT(&reply->hdrs);
1828 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001829 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001830
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001831 conf_err = calloc(1, sizeof(*conf_err));
1832 if (!conf_err) {
1833 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001834 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001835 ret = -1;
1836 goto out;
1837 }
1838 conf_err->type = 1;
1839 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001840 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001841 conf_err->file = strdup(file);
1842 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001843 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001844
Christopher Fauleta66adf42020-11-05 22:43:41 +01001845 /* handle warning message */
1846 if (*errmsg)
1847 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001848 out:
1849 return ret;
1850
1851}
1852
1853/* Parses the "errorfiles" proxy keyword */
1854static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001855 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001856 char **err)
1857{
1858 struct conf_errors *conf_err = NULL;
1859 char *name = NULL;
1860 int rc, ret = 0;
1861
1862 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1863 ret = 1;
1864 goto out;
1865 }
1866
1867 if (!*(args[1])) {
1868 memprintf(err, "%s : expects <name> as argument.", args[0]);
1869 ret = -1;
1870 goto out;
1871 }
1872
1873 name = strdup(args[1]);
1874 conf_err = calloc(1, sizeof(*conf_err));
1875 if (!name || !conf_err) {
1876 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001877 goto error;
1878 }
1879 conf_err->type = 0;
1880
1881 conf_err->info.errorfiles.name = name;
1882 if (!*(args[2])) {
1883 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1884 conf_err->info.errorfiles.status[rc] = 1;
1885 }
1886 else {
1887 int cur_arg, status;
1888 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1889 status = atol(args[cur_arg]);
1890
1891 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1892 if (http_err_codes[rc] == status) {
1893 conf_err->info.errorfiles.status[rc] = 2;
1894 break;
1895 }
1896 }
1897 if (rc >= HTTP_ERR_SIZE) {
1898 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001899 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001900 }
1901 }
1902 }
1903 conf_err->file = strdup(file);
1904 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001905 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001906 out:
1907 return ret;
1908
1909 error:
1910 free(name);
1911 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001912 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001913 goto out;
1914}
1915
Christopher Faulet3b967c12020-05-15 15:47:44 +02001916/* Parses the "http-error" proxy keyword */
1917static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001918 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02001919 char **errmsg)
1920{
1921 struct conf_errors *conf_err;
1922 struct http_reply *reply = NULL;
1923 int rc, cur_arg, ret = 0;
1924
1925 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1926 ret = 1;
1927 goto out;
1928 }
1929
1930 cur_arg = 1;
1931 curpx->conf.args.ctx = ARGC_HERR;
1932 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1933 if (!reply) {
1934 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1935 goto error;
1936 }
1937 else if (!reply->status) {
1938 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1939 goto error;
1940 }
1941
1942 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1943 if (http_err_codes[rc] == reply->status)
1944 break;
1945 }
1946
1947 if (rc >= HTTP_ERR_SIZE) {
1948 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1949 goto error;
1950 }
1951 if (*args[cur_arg]) {
1952 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1953 goto error;
1954 }
1955
1956 conf_err = calloc(1, sizeof(*conf_err));
1957 if (!conf_err) {
1958 memprintf(errmsg, "%s : out of memory.", args[0]);
1959 goto error;
1960 }
1961 if (reply->type == HTTP_REPLY_ERRFILES) {
1962 int rc = http_get_status_idx(reply->status);
1963
1964 conf_err->type = 2;
1965 conf_err->info.errorfiles.name = reply->body.http_errors;
1966 conf_err->info.errorfiles.status[rc] = 2;
1967 reply->body.http_errors = NULL;
1968 release_http_reply(reply);
1969 }
1970 else {
1971 conf_err->type = 1;
1972 conf_err->info.errorfile.status = reply->status;
1973 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02001974 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02001975 }
1976 conf_err->file = strdup(file);
1977 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001978 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02001979
Christopher Faulet3005d282020-11-13 10:58:01 +01001980 /* handle warning message */
1981 if (*errmsg)
1982 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001983 out:
1984 return ret;
1985
1986 error:
1987 release_http_reply(reply);
1988 ret = -1;
1989 goto out;
1990
1991}
1992
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001993/* Check "errorfiles" proxy keyword */
1994static int proxy_check_errors(struct proxy *px)
1995{
1996 struct conf_errors *conf_err, *conf_err_back;
1997 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001998 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001999
2000 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2001 if (conf_err->type == 1) {
2002 /* errorfile */
2003 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002004 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002005
2006 /* For proxy, to rely on default replies, just don't reference a reply */
2007 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2008 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002009 }
2010 else {
2011 /* errorfiles */
2012 list_for_each_entry(http_errs, &http_errors_list, list) {
2013 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2014 break;
2015 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002016
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002017 /* unknown http-errors section */
2018 if (&http_errs->list == &http_errors_list) {
2019 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2020 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2021 err |= ERR_ALERT | ERR_FATAL;
2022 free(conf_err->info.errorfiles.name);
2023 goto next;
2024 }
2025
2026 free(conf_err->info.errorfiles.name);
2027 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2028 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002029 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002030 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002031 else if (conf_err->info.errorfiles.status[rc] == 2)
2032 ha_warning("config: proxy '%s' : status '%d' not declared in"
2033 " http-errors section '%s' (at %s:%d).\n",
2034 px->id, http_err_codes[rc], http_errs->id,
2035 conf_err->file, conf_err->line);
2036 }
2037 }
2038 }
2039 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002040 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002041 free(conf_err->file);
2042 free(conf_err);
2043 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002044
2045 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002046 return err;
2047}
2048
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002049static int post_check_errors()
2050{
2051 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002052 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002053 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002054 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002055
2056 node = ebpt_first(&http_error_messages);
2057 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002058 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2059 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002060 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002061 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002062 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2063 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002064 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002065 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002066 (char *)node->key);
2067 err_code |= ERR_WARN;
2068 }
2069 next:
2070 node = ebpt_next(node);
2071 }
2072
2073 return err_code;
2074}
2075
Willy Tarreau016255a2021-02-12 08:40:29 +01002076int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002077{
2078 struct conf_errors *conf_err, *new_conf_err = NULL;
2079 int ret = 0;
2080
2081 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2082 new_conf_err = calloc(1, sizeof(*new_conf_err));
2083 if (!new_conf_err) {
2084 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2085 goto out;
2086 }
2087 new_conf_err->type = conf_err->type;
2088 if (conf_err->type == 1) {
2089 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002090 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002091 }
2092 else {
2093 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2094 if (!new_conf_err->info.errorfiles.name) {
2095 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2096 goto out;
2097 }
2098 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2099 sizeof(conf_err->info.errorfiles.status));
2100 }
2101 new_conf_err->file = strdup(conf_err->file);
2102 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002103 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002104 new_conf_err = NULL;
2105 }
2106 ret = 1;
2107
2108 out:
2109 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002110 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002111}
2112
2113void proxy_release_conf_errors(struct proxy *px)
2114{
2115 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002116
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002117 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2118 if (conf_err->type == 0)
2119 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002120 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002121 free(conf_err->file);
2122 free(conf_err);
2123 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002124}
2125
2126/*
2127 * Parse an <http-errors> section.
2128 * Returns the error code, 0 if OK, or any combination of :
2129 * - ERR_ABORT: must abort ASAP
2130 * - ERR_FATAL: we can continue parsing but not start the service
2131 * - ERR_WARN: a warning has been emitted
2132 * - ERR_ALERT: an alert has been emitted
2133 * Only the two first ones can stop processing, the two others are just
2134 * indicators.
2135 */
2136static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2137{
2138 static struct http_errors *curr_errs = NULL;
2139 int err_code = 0;
2140 const char *err;
2141 char *errmsg = NULL;
2142
2143 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2144 if (!*args[1]) {
2145 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2146 err_code |= ERR_ALERT | ERR_ABORT;
2147 goto out;
2148 }
2149
2150 err = invalid_char(args[1]);
2151 if (err) {
2152 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2153 file, linenum, *err, args[0], args[1]);
2154 err_code |= ERR_ALERT | ERR_FATAL;
2155 }
2156
2157 list_for_each_entry(curr_errs, &http_errors_list, list) {
2158 /* Error if two errors section owns the same name */
2159 if (strcmp(curr_errs->id, args[1]) == 0) {
2160 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2161 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2162 err_code |= ERR_ALERT | ERR_FATAL;
2163 }
2164 }
2165
2166 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2167 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2168 err_code |= ERR_ALERT | ERR_ABORT;
2169 goto out;
2170 }
2171
Willy Tarreau2b718102021-04-21 07:32:39 +02002172 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002173 curr_errs->id = strdup(args[1]);
2174 curr_errs->conf.file = strdup(file);
2175 curr_errs->conf.line = linenum;
2176 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002177 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002178 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002179 struct buffer *msg;
2180 int status, rc;
2181
2182 if (*(args[1]) == 0 || *(args[2]) == 0) {
2183 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2184 file, linenum, args[0]);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
2187 }
2188
2189 status = atol(args[1]);
2190 msg = http_parse_errorfile(status, args[2], &errmsg);
2191 if (!msg) {
2192 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2193 err_code |= ERR_ALERT | ERR_FATAL;
2194 goto out;
2195 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002196 if (errmsg) {
2197 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2198 err_code |= ERR_WARN;
2199 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002200
2201 reply = calloc(1, sizeof(*reply));
2202 if (!reply) {
2203 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207 reply->type = HTTP_REPLY_ERRMSG;
2208 reply->status = status;
2209 reply->ctype = NULL;
2210 LIST_INIT(&reply->hdrs);
2211 reply->body.errmsg = msg;
2212
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002213 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002214 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002215 }
2216 else if (*args[0] != 0) {
2217 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2218 err_code |= ERR_ALERT | ERR_FATAL;
2219 goto out;
2220 }
2221
2222out:
2223 free(errmsg);
2224 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002225}
2226
2227static struct cfg_kw_list cfg_kws = {ILH, {
2228 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2229 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2230 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2231 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002232 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002233 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002234 { 0, NULL, NULL },
2235}};
2236
2237INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002238REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002239REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002240
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002241REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2242
Christopher Faulet29f72842019-12-11 15:52:32 +01002243/************************************************************************/
2244/* HTX sample fetches */
2245/************************************************************************/
2246
2247/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2248static int
2249smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2250{
2251 if (!smp->strm)
2252 return 0;
2253
2254 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2255 smp->data.type = SMP_T_BOOL;
2256 return 1;
2257}
2258
2259/* Returns the number of blocks in an HTX message. The channel is chosen
2260 * depending on the sample direction. */
2261static int
2262smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2263{
2264 struct channel *chn;
2265 struct htx *htx;
2266
2267 if (!smp->strm)
2268 return 0;
2269
2270 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002271 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002272 if (!htx)
2273 return 0;
2274
2275 smp->data.u.sint = htx_nbblks(htx);
2276 smp->data.type = SMP_T_SINT;
2277 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2278 return 1;
2279}
2280
2281/* Returns the size of an HTX message. The channel is chosen depending on the
2282 * sample direction. */
2283static int
2284smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2285{
2286 struct channel *chn;
2287 struct htx *htx;
2288
2289 if (!smp->strm)
2290 return 0;
2291
2292 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002293 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002294 if (!htx)
2295 return 0;
2296
2297 smp->data.u.sint = htx->size;
2298 smp->data.type = SMP_T_SINT;
2299 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2300 return 1;
2301}
2302
2303/* Returns the data size of an HTX message. The channel is chosen depending on the
2304 * sample direction. */
2305static int
2306smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2307{
2308 struct channel *chn;
2309 struct htx *htx;
2310
2311 if (!smp->strm)
2312 return 0;
2313
2314 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002315 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002316 if (!htx)
2317 return 0;
2318
2319 smp->data.u.sint = htx->data;
2320 smp->data.type = SMP_T_SINT;
2321 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2322 return 1;
2323}
2324
2325/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2326 * depending on the sample direction. */
2327static int
2328smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2329{
2330 struct channel *chn;
2331 struct htx *htx;
2332
2333 if (!smp->strm)
2334 return 0;
2335
2336 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002337 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002338 if (!htx)
2339 return 0;
2340
2341 smp->data.u.sint = htx_used_space(htx);
2342 smp->data.type = SMP_T_SINT;
2343 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2344 return 1;
2345}
2346
2347/* Returns the free space (size-used) of an HTX message. The channel is chosen
2348 * depending on the sample direction. */
2349static int
2350smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2351{
2352 struct channel *chn;
2353 struct htx *htx;
2354
2355 if (!smp->strm)
2356 return 0;
2357
2358 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002359 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002360 if (!htx)
2361 return 0;
2362
2363 smp->data.u.sint = htx_free_space(htx);
2364 smp->data.type = SMP_T_SINT;
2365 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2366 return 1;
2367}
2368
2369/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2370 * channel is chosen depending on the sample direction. */
2371static int
2372smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2373{
2374 struct channel *chn;
2375 struct htx *htx;
2376
2377 if (!smp->strm)
2378 return 0;
2379
2380 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002381 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002382 if (!htx)
2383 return 0;
2384
2385 smp->data.u.sint = htx_free_data_space(htx);
2386 smp->data.type = SMP_T_SINT;
2387 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2388 return 1;
2389}
2390
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002391/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2392 * channel is chosen depending on the sample direction.
2393 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002394static int
2395smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2396{
2397 struct channel *chn;
2398 struct htx *htx;
2399
2400 if (!smp->strm)
2401 return 0;
2402
2403 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002404 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002405 if (!htx)
2406 return 0;
2407
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002408 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002409 smp->data.type = SMP_T_BOOL;
2410 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2411 return 1;
2412}
2413
2414/* Returns the type of a specific HTX block, if found in the message. Otherwise
2415 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2416 * "head", "tail" or "first". The channel is chosen depending on the sample
2417 * direction. */
2418static int
2419smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2420{
2421 struct channel *chn;
2422 struct htx *htx;
2423 enum htx_blk_type type;
2424 int32_t pos;
2425
2426 if (!smp->strm || !arg_p)
2427 return 0;
2428
2429 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002430 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002431 if (!htx)
2432 return 0;
2433
2434 pos = arg_p[0].data.sint;
2435 if (pos == -1)
2436 type = htx_get_head_type(htx);
2437 else if (pos == -2)
2438 type = htx_get_tail_type(htx);
2439 else if (pos == -3)
2440 type = htx_get_first_type(htx);
2441 else
2442 type = ((pos >= htx->head && pos <= htx->tail)
2443 ? htx_get_blk_type(htx_get_blk(htx, pos))
2444 : HTX_BLK_UNUSED);
2445
2446 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2447 smp->data.type = SMP_T_STR;
2448 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2449 return 1;
2450}
2451
2452/* Returns the size of a specific HTX block, if found in the message. Otherwise
2453 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2454 * "first". The channel is chosen depending on the sample direction. */
2455static int
2456smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2457{
2458 struct channel *chn;
2459 struct htx *htx;
2460 struct htx_blk *blk;
2461 int32_t pos;
2462
2463 if (!smp->strm || !arg_p)
2464 return 0;
2465
2466 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002467 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002468 if (!htx)
2469 return 0;
2470
2471 pos = arg_p[0].data.sint;
2472 if (pos == -1)
2473 blk = htx_get_head_blk(htx);
2474 else if (pos == -2)
2475 blk = htx_get_tail_blk(htx);
2476 else if (pos == -3)
2477 blk = htx_get_first_blk(htx);
2478 else
2479 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2480
2481 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2482 smp->data.type = SMP_T_SINT;
2483 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2484 return 1;
2485}
2486
2487/* Returns the start-line if the selected HTX block exists and is a
2488 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2489 * supported or "head", "tail" or "first". The channel is chosen depending on
2490 * the sample direction. */
2491static int
2492smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2493{
2494 struct buffer *temp;
2495 struct channel *chn;
2496 struct htx *htx;
2497 struct htx_blk *blk;
2498 struct htx_sl *sl;
2499 int32_t pos;
2500
2501 if (!smp->strm || !arg_p)
2502 return 0;
2503
2504 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002505 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002506 if (!htx)
2507 return 0;
2508
2509 pos = arg_p[0].data.sint;
2510 if (pos == -1)
2511 blk = htx_get_head_blk(htx);
2512 else if (pos == -2)
2513 blk = htx_get_tail_blk(htx);
2514 else if (pos == -3)
2515 blk = htx_get_first_blk(htx);
2516 else
2517 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2518
2519 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2520 smp->data.u.str.size = 0;
2521 smp->data.u.str.area = "";
2522 smp->data.u.str.data = 0;
2523 }
2524 else {
2525 sl = htx_get_blk_ptr(htx, blk);
2526
2527 temp = get_trash_chunk();
2528 chunk_istcat(temp, htx_sl_p1(sl));
2529 temp->area[temp->data++] = ' ';
2530 chunk_istcat(temp, htx_sl_p2(sl));
2531 temp->area[temp->data++] = ' ';
2532 chunk_istcat(temp, htx_sl_p3(sl));
2533
2534 smp->data.u.str = *temp;
2535 }
2536
2537 smp->data.type = SMP_T_STR;
2538 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2539 return 1;
2540}
2541
2542/* Returns the header name if the selected HTX block exists and is a header or a
2543 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2544 * supported or "head", "tail" or "first". The channel is chosen depending on
2545 * the sample direction. */
2546static int
2547smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2548{
2549 struct channel *chn;
2550 struct htx *htx;
2551 struct htx_blk *blk;
2552 int32_t pos;
2553
2554 if (!smp->strm || !arg_p)
2555 return 0;
2556
2557 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002558 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002559 if (!htx)
2560 return 0;
2561
2562 pos = arg_p[0].data.sint;
2563 if (pos == -1)
2564 blk = htx_get_head_blk(htx);
2565 else if (pos == -2)
2566 blk = htx_get_tail_blk(htx);
2567 else if (pos == -3)
2568 blk = htx_get_first_blk(htx);
2569 else
2570 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2571
2572 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2573 smp->data.u.str.size = 0;
2574 smp->data.u.str.area = "";
2575 smp->data.u.str.data = 0;
2576 }
2577 else {
2578 struct ist name = htx_get_blk_name(htx, blk);
2579
2580 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2581 }
2582 smp->data.type = SMP_T_STR;
2583 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2584 return 1;
2585}
2586
2587/* Returns the header value if the selected HTX block exists and is a header or
2588 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2589 * supported or "head", "tail" or "first". The channel is chosen depending on
2590 * the sample direction. */
2591static int
2592smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2593{
2594 struct channel *chn;
2595 struct htx *htx;
2596 struct htx_blk *blk;
2597 int32_t pos;
2598
2599 if (!smp->strm || !arg_p)
2600 return 0;
2601
2602 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002603 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002604 if (!htx)
2605 return 0;
2606
2607 pos = arg_p[0].data.sint;
2608 if (pos == -1)
2609 blk = htx_get_head_blk(htx);
2610 else if (pos == -2)
2611 blk = htx_get_tail_blk(htx);
2612 else if (pos == -3)
2613 blk = htx_get_first_blk(htx);
2614 else
2615 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2616
2617 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2618 smp->data.u.str.size = 0;
2619 smp->data.u.str.area = "";
2620 smp->data.u.str.data = 0;
2621 }
2622 else {
2623 struct ist val = htx_get_blk_value(htx, blk);
2624
2625 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2626 }
2627 smp->data.type = SMP_T_STR;
2628 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2629 return 1;
2630}
2631
2632/* Returns the value if the selected HTX block exists and is a data
2633 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2634 * or "head", "tail" or "first". The channel is chosen depending on the sample
2635 * direction. */
2636static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002637smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002638{
2639 struct channel *chn;
2640 struct htx *htx;
2641 struct htx_blk *blk;
2642 int32_t pos;
2643
2644 if (!smp->strm || !arg_p)
2645 return 0;
2646
2647 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002648 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002649 if (!htx)
2650 return 0;
2651
2652 pos = arg_p[0].data.sint;
2653 if (pos == -1)
2654 blk = htx_get_head_blk(htx);
2655 else if (pos == -2)
2656 blk = htx_get_tail_blk(htx);
2657 else if (pos == -3)
2658 blk = htx_get_first_blk(htx);
2659 else
2660 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2661
2662 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2663 smp->data.u.str.size = 0;
2664 smp->data.u.str.area = "";
2665 smp->data.u.str.data = 0;
2666 }
2667 else {
2668 struct ist val = htx_get_blk_value(htx, blk);
2669
2670 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2671 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002672 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002673 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2674 return 1;
2675}
2676
2677/* This function is used to validate the arguments passed to any "htx_blk" fetch
2678 * keywords. An argument is expected by these keywords. It must be a positive
2679 * integer or on of the following strings: "head", "tail" or "first". It returns
2680 * 0 on error, and a non-zero value if OK.
2681 */
2682int val_blk_arg(struct arg *arg, char **err_msg)
2683{
2684 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2685 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2686 return 0;
2687 }
2688 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002689 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002690 arg[0].type = ARGT_SINT;
2691 arg[0].data.sint = -1;
2692 }
2693 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002694 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002695 arg[0].type = ARGT_SINT;
2696 arg[0].data.sint = -2;
2697 }
2698 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002699 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002700 arg[0].type = ARGT_SINT;
2701 arg[0].data.sint = -3;
2702 }
2703 else {
2704 int pos;
2705
2706 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002707 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002708 memprintf(err_msg, "invalid block position");
2709 return 0;
2710 }
2711 }
2712
2713 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2714 if (pos < 0) {
2715 memprintf(err_msg, "block position must not be negative");
2716 return 0;
2717 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002718 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002719 arg[0].type = ARGT_SINT;
2720 arg[0].data.sint = pos;
2721 }
2722
2723 return 1;
2724}
2725
2726
2727/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002728 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002729 */
2730static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002731 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002732
Christopher Faulet01f44452020-01-08 14:23:40 +01002733 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2734 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2735 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2736 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2737 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2738 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2739 { "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 +01002740
Christopher Faulet01f44452020-01-08 14:23:40 +01002741 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2742 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2743 { "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},
2744 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2745 { "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 +01002746 { "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 +01002747
2748 { /* END */ },
2749}};
2750
2751INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);