blob: c3422d47acf81c609f91a42b520c538edaab7a7f [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>
Christopher Faulet47596d32018-10-22 09:17:28 +020029
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Fauletf7346382019-07-17 22:02:08 +020031struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020032struct http_reply http_err_replies[HTTP_ERR_SIZE];
33
Christopher Faulet58857752020-01-15 15:19:50 +010034struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010035struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020036struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010037
Christopher Faulet76edc0f2020-01-13 15:52:01 +010038/* The declaration of an errorfiles/errorfile directives. Used during config
39 * parsing only. */
40struct conf_errors {
41 char type; /* directive type (0: errorfiles, 1: errorfile) */
42 union {
43 struct {
44 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020045 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010046 } errorfile; /* describe an "errorfile" directive */
47 struct {
48 char *name; /* the http-errors section name */
49 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
50 } errorfiles; /* describe an "errorfiles" directive */
51 } info;
52
53 char *file; /* file where the directive appears */
54 int line; /* line where the directive appears */
55
56 struct list list; /* next conf_errors */
57};
58
Christopher Faulet297fbb42019-05-13 14:41:27 +020059/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020060 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020061 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020062 */
Tim Duesterhusb8ee8942021-04-03 20:39:20 +020063struct htx_sl *http_get_stline(const struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020064{
Christopher Faulet297fbb42019-05-13 14:41:27 +020065 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 blk = htx_get_first_blk(htx);
Christopher Fauleta7d6cf22021-04-15 10:25:35 +020068 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 +020069 return NULL;
70 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020071}
72
Christopher Faulet727a3f12020-02-07 16:39:41 +010073/* Returns the headers size in the HTX message */
74size_t http_get_hdrs_size(struct htx *htx)
75{
76 struct htx_blk *blk;
77 size_t sz = 0;
78
79 blk = htx_get_first_blk(htx);
80 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
81 return sz;
82
83 for (; blk; blk = htx_get_next_blk(htx, blk)) {
84 sz += htx_get_blksz(blk);
85 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
86 break;
87 }
88 return sz;
89}
90
Christopher Faulet8dd33e12020-05-05 07:42:42 +020091/* Finds the first or next occurrence of header matching <pattern> in the HTX
92 * message <htx> using the context <ctx>. This structure holds everything
93 * necessary to use the header and find next occurrence. If its <blk> member is
94 * NULL, the header is searched from the beginning. Otherwise, the next
95 * occurrence is returned. The function returns 1 when it finds a value, and 0
96 * when there is no more. It is designed to work with headers defined as
97 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
98 * full-line headers in whose comma is not a delimiter but is part of the
99 * syntax. A special case, if ctx->value is NULL when searching for a new values
100 * of a header, the current header is rescanned. This allows rescanning after a
101 * header deletion.
102 *
103 * The matching method is chosen by checking the flags :
104 *
105 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
106 * the regex are evaluated.
107 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
108 * to the string are evaluated.
109 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
110 * starting by the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
112 * ending by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
114 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200115 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200116
117#define HTTP_FIND_FL_MATCH_STR 0x0001
118#define HTTP_FIND_FL_MATCH_PFX 0x0002
119#define HTTP_FIND_FL_MATCH_SFX 0x0003
120#define HTTP_FIND_FL_MATCH_SUB 0x0004
121#define HTTP_FIND_FL_MATCH_REG 0x0005
122/* 0x0006..0x000f: for other matching methods */
123#define HTTP_FIND_FL_MATCH_TYPE 0x000F
124#define HTTP_FIND_FL_FULL 0x0010
125
126static 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 +0200127{
128 struct htx_blk *blk = ctx->blk;
129 struct ist n, v;
130 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200131
132 if (blk) {
133 char *p;
134
Tim Duesterhused526372020-03-05 17:56:33 +0100135 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200136 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200137 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto next_blk;
139 v = htx_get_blk_value(htx, blk);
140 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
141 v.len -= (p - v.ptr);
142 v.ptr = p;
143 if (!v.len)
144 goto next_blk;
145 /* Skip comma */
146 if (*(v.ptr) == ',') {
147 v.ptr++;
148 v.len--;
149 }
150
151 goto return_hdr;
152 }
153
Christopher Faulet192c6a22019-06-11 16:32:24 +0200154 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200155 return 0;
156
Christopher Fauleta3f15502019-05-13 15:27:23 +0200157 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200158 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200159 type = htx_get_blk_type(blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100160 if (type == HTX_BLK_EOH)
Christopher Faulet573fe732018-11-28 16:55:12 +0100161 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200162 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200163 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200164
165 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
166 const struct my_regex *re = pattern;
167
168 n = htx_get_blk_name(htx, blk);
169 if (!regex_exec2(re, n.ptr, n.len))
170 goto next_blk;
171 }
172 else {
173 const struct ist name = *(const struct ist *)(pattern);
174
Christopher Faulet47596d32018-10-22 09:17:28 +0200175 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200176 if (!istlen(name))
177 goto match;
178
Christopher Faulet47596d32018-10-22 09:17:28 +0200179 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200180 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
181 case HTTP_FIND_FL_MATCH_STR:
182 if (!isteqi(n, name))
183 goto next_blk;
184 break;
185 case HTTP_FIND_FL_MATCH_PFX:
186 if (istlen(n) < istlen(name))
187 goto next_blk;
188
189 n = ist2(istptr(n), istlen(name));
190 if (!isteqi(n, name))
191 goto next_blk;
192 break;
193 case HTTP_FIND_FL_MATCH_SFX:
194 if (istlen(n) < istlen(name))
195 goto next_blk;
196
197 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
198 if (!isteqi(n, name))
199 goto next_blk;
200 break;
201 case HTTP_FIND_FL_MATCH_SUB:
Maciej Zdeb302b9f82020-11-20 12:12:24 +0000202 if (!strnistr(n.ptr, n.len, name.ptr, name.len))
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200203 goto next_blk;
204 break;
205 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200206 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200207 break;
208 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200209 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200210 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 v = htx_get_blk_value(htx, blk);
212
213 return_hdr:
214 ctx->lws_before = 0;
215 ctx->lws_after = 0;
216 while (v.len && HTTP_IS_LWS(*v.ptr)) {
217 v.ptr++;
218 v.len--;
219 ctx->lws_before++;
220 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200221 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200222 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
223 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
224 v.len--;
225 ctx->lws_after++;
226 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200227 ctx->blk = blk;
228 ctx->value = v;
229 return 1;
230
231 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200232 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200233 }
234
235 ctx->blk = NULL;
236 ctx->value = ist("");
237 ctx->lws_before = ctx->lws_after = 0;
238 return 0;
239}
240
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200241
242/* Header names must match <name> */
243int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
244{
245 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
246}
247
248/* Header names must match <name>. Same than http_find_header */
249int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
250{
251 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
252}
253
254
255/* Header names must start with <prefix> */
256int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
257{
258 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
259}
260
261/* Header names must end with <suffix> */
262int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
263{
264 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
265}
266/* Header names must contain <sub> */
267int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
268{
269 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
270}
271
272/* Header names must match <re> regex*/
273int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
274{
275 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
276}
277
278
Christopher Faulet47596d32018-10-22 09:17:28 +0200279/* Adds a header block int the HTX message <htx>, just before the EOH block. It
280 * returns 1 on success, otherwise it returns 0.
281 */
282int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
283{
284 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200285 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200286 enum htx_blk_type type = htx_get_tail_type(htx);
287 int32_t prev;
288
289 blk = htx_add_header(htx, n, v);
290 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200291 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200292
293 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200294 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200295
296 /* <blk> is the head, swap it iteratively with its predecessor to place
297 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200298 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200299 struct htx_blk *pblk = htx_get_blk(htx, prev);
300 enum htx_blk_type type = htx_get_blk_type(pblk);
301
302 /* Swap .addr and .info fields */
303 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
304 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
305
306 if (blk->addr == pblk->addr)
307 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200308
309 /* Stop when end-of-header is reached */
310 if (type == HTX_BLK_EOH)
311 break;
312
313 blk = pblk;
314 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200315
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200316 end:
317 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100318 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200319 if (!http_update_authority(htx, sl, v))
320 goto fail;
321 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200322 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200323
324 fail:
325 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200326}
327
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100328/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200329 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200330 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100331int 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 +0200332{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200333 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200334
Christopher Faulet29f17582019-05-23 11:03:26 +0200335 blk = htx_get_first_blk(htx);
336 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200337 return 0;
338 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200339}
340
Christopher Faulete010c802018-10-24 10:36:45 +0200341/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
342 * on success, otherwise 0.
343 */
344int http_replace_req_meth(struct htx *htx, const struct ist meth)
345{
346 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200347 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100348 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200349
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100350 if (!sl)
351 return 0;
352
Christopher Faulete010c802018-10-24 10:36:45 +0200353 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100354 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
355 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200356
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100357 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
358 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200359
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100360 /* create the new start line */
361 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
362 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200363}
364
365/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
366 * success, otherwise 0.
367 */
368int http_replace_req_uri(struct htx *htx, const struct ist uri)
369{
370 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200371 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100372 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200373
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100374 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200375 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376
Christopher Faulete010c802018-10-24 10:36:45 +0200377 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100378 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
379 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200380
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100381 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
382 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200383
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100384 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200385 if (!http_replace_stline(htx, meth, uri, vsn))
386 goto fail;
387
388 sl = http_get_stline(htx);
389 if (!http_update_host(htx, sl, uri))
390 goto fail;
391
392 return 1;
393 fail:
394 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200395}
396
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200397/* Replace the request path in the HTX message <htx> by <path>. The host part is
398 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
399 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
400 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200401 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200402int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200403{
404 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200405 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100406 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200407 size_t plen = 0;
408
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100409 if (!sl)
410 return 0;
411
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100412 uri = htx_sl_req_uri(sl);
413 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100414 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100415 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200416 if (with_qs)
417 plen = p.len;
418 else {
419 while (plen < p.len && *(p.ptr + plen) != '?')
420 plen++;
421 }
Christopher Faulete010c802018-10-24 10:36:45 +0200422
423 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100424 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
425 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200426
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100427 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
428 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
429
430 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200431 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
432 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100433 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200434
435 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100436 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200437}
438
439/* Replace the request query-string in the HTX message <htx> by <query>. The
440 * host part and the path are preserved. It returns 1 on success, otherwise
441 * 0.
442 */
443int http_replace_req_query(struct htx *htx, const struct ist query)
444{
445 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200446 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100447 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200448 int offset = 1;
449
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100450 if (!sl)
451 return 0;
452
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100453 uri = htx_sl_req_uri(sl);
454 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200455 while (q.len > 0 && *(q.ptr) != '?') {
456 q.ptr++;
457 q.len--;
458 }
459
460 /* skip the question mark or indicate that we must insert it
461 * (but only if the format string is not empty then).
462 */
463 if (q.len) {
464 q.ptr++;
465 q.len--;
466 }
467 else if (query.len > 1)
468 offset = 0;
469
470 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100471 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
472 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200473
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100474 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
475 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200476
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
478 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
479 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200480
481 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100482 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200483}
484
485/* Replace the response status in the HTX message <htx> by <status>. It returns
486 * 1 on success, otherwise 0.
487*/
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200488int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
Christopher Faulete010c802018-10-24 10:36:45 +0200489{
490 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200491 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200492 struct ist vsn, r;
Christopher Faulete010c802018-10-24 10:36:45 +0200493
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100494 if (!sl)
495 return 0;
496
Christopher Faulete010c802018-10-24 10:36:45 +0200497 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100498 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
499 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200500 r = reason;
501 if (!isttest(r)) {
502 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
503 r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
504 }
Christopher Faulete010c802018-10-24 10:36:45 +0200505
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100506 /* create the new start line */
507 sl->info.res.status = strl2ui(status.ptr, status.len);
Christopher Fauletbde2c4c2020-08-31 16:43:34 +0200508 return http_replace_stline(htx, vsn, status, r);
Christopher Faulete010c802018-10-24 10:36:45 +0200509}
510
511/* Replace the response reason in the HTX message <htx> by <reason>. It returns
512 * 1 on success, otherwise 0.
513*/
514int http_replace_res_reason(struct htx *htx, const struct ist reason)
515{
516 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200517 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200519
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100520 if (!sl)
521 return 0;
522
Christopher Faulete010c802018-10-24 10:36:45 +0200523 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100524 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
525 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200526
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100527 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
528 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200529
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100530 /* create the new start line */
531 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200532}
533
Christopher Faulet47596d32018-10-22 09:17:28 +0200534/* Replaces a part of a header value referenced in the context <ctx> by
535 * <data>. It returns 1 on success, otherwise it returns 0. The context is
536 * updated if necessary.
537 */
538int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
539{
540 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200541 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200542 char *start;
543 struct ist v;
544 uint32_t len, off;
545
546 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200547 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200548
549 v = htx_get_blk_value(htx, blk);
550 start = ctx->value.ptr - ctx->lws_before;
551 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
552 off = start - v.ptr;
553
554 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
555 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200556 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200557
558 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200559
560 sl = http_get_stline(htx);
561 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
562 struct ist n = htx_get_blk_name(htx, blk);
563
564 if (isteq(n, ist("host"))) {
565 if (!http_update_authority(htx, sl, v))
566 goto fail;
567 ctx->blk = NULL;
568 http_find_header(htx, ist("host"), ctx, 1);
569 blk = ctx->blk;
570 v = htx_get_blk_value(htx, blk);
571 }
572 }
573
Christopher Faulet47596d32018-10-22 09:17:28 +0200574 ctx->blk = blk;
575 ctx->value.ptr = v.ptr + off;
576 ctx->value.len = data.len;
577 ctx->lws_before = ctx->lws_after = 0;
578
579 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200580 fail:
581 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200582}
583
584/* Fully replaces a header referenced in the context <ctx> by the name <name>
585 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
586 * context is updated if necessary.
587 */
588int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
589 const struct ist name, const struct ist value)
590{
591 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200592 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200593
594 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200595 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200596
597 blk = htx_replace_header(htx, blk, name, value);
598 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200599 goto fail;
600
601 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100602 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200603 if (!http_update_authority(htx, sl, value))
604 goto fail;
605 ctx->blk = NULL;
606 http_find_header(htx, ist("host"), ctx, 1);
607 blk = ctx->blk;
608 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200609
610 ctx->blk = blk;
611 ctx->value = ist(NULL);
612 ctx->lws_before = ctx->lws_after = 0;
613
614 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200615 fail:
616 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200617}
618
619/* Remove one value of a header. This only works on a <ctx> returned by
620 * http_find_header function. The value is removed, as well as surrounding commas
621 * if any. If the removed value was alone, the whole header is removed. The
622 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
623 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
624 * form that can be handled by http_find_header() to find next occurrence.
625 */
626int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
627{
628 struct htx_blk *blk = ctx->blk;
629 char *start;
630 struct ist v;
631 uint32_t len;
632
633 if (!blk)
634 return 0;
635
636 start = ctx->value.ptr - ctx->lws_before;
637 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
638
639 v = htx_get_blk_value(htx, blk);
640 if (len == v.len) {
641 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200642 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200643 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100644 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200645 ctx->lws_before = ctx->lws_after = 0;
646 }
647 else {
648 ctx->blk = htx_get_blk(htx, htx->tail);
649 ctx->value = htx_get_blk_value(htx, ctx->blk);
650 ctx->lws_before = ctx->lws_after = 0;
651 }
652 return 1;
653 }
654
655 /* This was not the only value of this header. We have to remove the
656 * part pointed by ctx->value. If it is the last entry of the list, we
657 * remove the last separator.
658 */
659 if (start == v.ptr) {
660 /* It's the first header part but not the only one. So remove
661 * the comma after it. */
662 len++;
663 }
664 else {
665 /* There is at least one header part before the removed one. So
666 * remove the comma between them. */
667 start--;
668 len++;
669 }
670 /* Update the block content and its len */
671 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200672 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200673
674 /* Finally update the ctx */
675 ctx->value.ptr = start;
676 ctx->value.len = 0;
677 ctx->lws_before = ctx->lws_after = 0;
678
679 return 1;
680}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200681
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200682/* Updates the authority part of the uri with the value <host>. It happens when
683 * the header host is modified. It returns 0 on failure and 1 on success. It is
684 * the caller responsibility to provide the start-line and to be sure the uri
685 * contains an authority. Thus, if no authority is found in the uri, an error is
686 * returned.
687 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200688int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200689{
690 struct buffer *temp = get_trash_chunk();
691 struct ist meth, vsn, uri, authority;
692
693 uri = htx_sl_req_uri(sl);
694 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100695 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200696 return 0;
697
Christopher Faulet34b18e42020-02-18 11:02:21 +0100698 /* Don't update the uri if there is no change */
699 if (isteq(host, authority))
700 return 1;
701
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200702 /* Start by copying old method and version */
703 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
704 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
705
706 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
707 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
708
709 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
710 chunk_memcat(temp, host.ptr, host.len);
711 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
712 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
713
714 return http_replace_stline(htx, meth, uri, vsn);
715
716}
717
718/* Update the header host by extracting the authority of the uri <uri>. flags of
719 * the start-line are also updated accordingly. For orgin-form and asterisk-form
720 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
721 * removed from the flags of the start-line. Otherwise, this flag is set and the
722 * authority is used to set the value of the header host. This function returns
723 * 0 on failure and 1 on success.
724*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200725int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200726{
727 struct ist authority;
728 struct http_hdr_ctx ctx;
729
730 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
731 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
732 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
733 }
734 else {
735 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
736 if (sl->info.req.meth != HTTP_METH_CONNECT) {
737 // absolute-form (RFC7320 #5.3.2)
738 sl->flags |= HTX_SL_F_HAS_SCHM;
739 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
740 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
741
742 authority = http_get_authority(uri, 1);
743 if (!authority.len)
744 goto fail;
745 }
746 else {
747 // authority-form (RFC7320 #5.3.3)
748 authority = uri;
749 }
750
751 /* Replace header host value */
752 ctx.blk = NULL;
753 while (http_find_header(htx, ist("host"), &ctx, 1)) {
754 if (!http_replace_header_value(htx, &ctx, authority))
755 goto fail;
756 }
757
758 }
759 return 1;
760 fail:
761 return 0;
762}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200763
764/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
765 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
766 * performed over the whole headers. Otherwise it must contain a valid header
767 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
768 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
769 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
770 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
771 * -1. The value fetch stops at commas, so this function is suited for use with
772 * list headers.
773 * The return value is 0 if nothing was found, or non-zero otherwise.
774 */
775unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
776 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
777{
778 struct http_hdr_ctx local_ctx;
779 struct ist val_hist[MAX_HDR_HISTORY];
780 unsigned int hist_idx;
781 int found;
782
783 if (!ctx) {
784 local_ctx.blk = NULL;
785 ctx = &local_ctx;
786 }
787
788 if (occ >= 0) {
789 /* search from the beginning */
790 while (http_find_header(htx, hdr, ctx, 0)) {
791 occ--;
792 if (occ <= 0) {
793 *vptr = ctx->value.ptr;
794 *vlen = ctx->value.len;
795 return 1;
796 }
797 }
798 return 0;
799 }
800
801 /* negative occurrence, we scan all the list then walk back */
802 if (-occ > MAX_HDR_HISTORY)
803 return 0;
804
805 found = hist_idx = 0;
806 while (http_find_header(htx, hdr, ctx, 0)) {
807 val_hist[hist_idx] = ctx->value;
808 if (++hist_idx >= MAX_HDR_HISTORY)
809 hist_idx = 0;
810 found++;
811 }
812 if (-occ > found)
813 return 0;
814
815 /* OK now we have the last occurrence in [hist_idx-1], and we need to
816 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
817 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
818 * to remain in the 0..9 range.
819 */
820 hist_idx += occ + MAX_HDR_HISTORY;
821 if (hist_idx >= MAX_HDR_HISTORY)
822 hist_idx -= MAX_HDR_HISTORY;
823 *vptr = val_hist[hist_idx].ptr;
824 *vlen = val_hist[hist_idx].len;
825 return 1;
826}
827
828/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
829 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
830 * performed over the whole headers. Otherwise it must contain a valid header
831 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
832 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
833 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
834 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
835 * -1. This function differs from http_get_hdr() in that it only returns full
836 * line header values and does not stop at commas.
837 * The return value is 0 if nothing was found, or non-zero otherwise.
838 */
839unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
840 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
841{
842 struct http_hdr_ctx local_ctx;
843 struct ist val_hist[MAX_HDR_HISTORY];
844 unsigned int hist_idx;
845 int found;
846
847 if (!ctx) {
848 local_ctx.blk = NULL;
849 ctx = &local_ctx;
850 }
851
852 if (occ >= 0) {
853 /* search from the beginning */
854 while (http_find_header(htx, hdr, ctx, 1)) {
855 occ--;
856 if (occ <= 0) {
857 *vptr = ctx->value.ptr;
858 *vlen = ctx->value.len;
859 return 1;
860 }
861 }
862 return 0;
863 }
864
865 /* negative occurrence, we scan all the list then walk back */
866 if (-occ > MAX_HDR_HISTORY)
867 return 0;
868
869 found = hist_idx = 0;
870 while (http_find_header(htx, hdr, ctx, 1)) {
871 val_hist[hist_idx] = ctx->value;
872 if (++hist_idx >= MAX_HDR_HISTORY)
873 hist_idx = 0;
874 found++;
875 }
876 if (-occ > found)
877 return 0;
878
879 /* OK now we have the last occurrence in [hist_idx-1], and we need to
880 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
881 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
882 * to remain in the 0..9 range.
883 */
884 hist_idx += occ + MAX_HDR_HISTORY;
885 if (hist_idx >= MAX_HDR_HISTORY)
886 hist_idx -= MAX_HDR_HISTORY;
887 *vptr = val_hist[hist_idx].ptr;
888 *vlen = val_hist[hist_idx].len;
889 return 1;
890}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100891
Christopher Fauleta66adf42020-11-05 22:43:41 +0100892int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100893{
894 struct htx *htx;
895 struct htx_sl *sl;
896 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200897 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100898 union h1_sl h1sl;
899 unsigned int flags = HTX_SL_F_IS_RESP;
900 int ret = 0;
901
Christopher Faulet90cc4812019-07-22 16:49:30 +0200902 b_reset(buf);
903 if (!raw.len) {
904 buf->size = 0;
Christopher Faulet1cdc0282021-02-05 10:29:29 +0100905 buf->area = NULL;
Christopher Faulet90cc4812019-07-22 16:49:30 +0200906 return 1;
907 }
908
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100909 buf->size = global.tune.bufsize;
Tim Duesterhus403fd722021-04-08 20:05:23 +0200910 buf->area = malloc(buf->size);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100911 if (!buf->area)
912 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100913
914 h1m_init_res(&h1m);
915 h1m.flags |= H1_MF_NO_PHDR;
916 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
917 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100918 if (ret <= 0) {
919 memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100920 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100921 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100922
Christopher Fauleta66adf42020-11-05 22:43:41 +0100923 if (unlikely(h1sl.st.v.len != 8)) {
924 memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr);
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100925 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100926 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100927 if ((*(h1sl.st.v.ptr + 5) > '1') ||
928 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
929 h1m.flags |= H1_MF_VER_11;
930
Christopher Fauleta66adf42020-11-05 22:43:41 +0100931 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) {
932 memprintf(errmsg, "invalid http status code for an error message (%u)",
933 h1sl.st.status);
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200934 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100935 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200936
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200937 if (h1sl.st.status == 204 || h1sl.st.status == 304) {
938 /* Responses known to have no body. */
939 h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
940 h1m.flags |= H1_MF_XFER_LEN;
941 h1m.curr_len = h1m.body_len = 0;
942 }
943 else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
944 h1m.flags |= H1_MF_XFER_LEN;
945
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100946 if (h1m.flags & H1_MF_VER_11)
947 flags |= HTX_SL_F_VER_11;
948 if (h1m.flags & H1_MF_XFER_ENC)
949 flags |= HTX_SL_F_XFER_ENC;
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200950 if (h1m.flags & H1_MF_XFER_LEN) {
951 flags |= HTX_SL_F_XFER_LEN;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100952 if (h1m.flags & H1_MF_CHNK) {
953 memprintf(errmsg, "chunk-encoded payload not supported");
954 goto error;
955 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200956 else if (h1m.flags & H1_MF_CLEN) {
957 flags |= HTX_SL_F_CLEN;
958 if (h1m.body_len == 0)
959 flags |= HTX_SL_F_BODYLESS;
960 }
961 else
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200962 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100963 }
Christopher Fauletb8d148a2020-10-09 08:50:26 +0200964
Christopher Fauleta66adf42020-11-05 22:43:41 +0100965 if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) {
966 memprintf(errmsg, "message payload not expected");
967 goto error;
968 }
969 if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) {
970 memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)",
Willy Tarreau431a12c2020-11-06 14:24:02 +0100971 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Fauleta66adf42020-11-05 22:43:41 +0100972 goto error;
973 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100974
975 htx = htx_from_buf(buf);
976 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 +0100977 if (!sl || !htx_add_all_headers(htx, hdrs)) {
978 memprintf(errmsg, "unable to add headers into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100979 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100980 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100981 sl->info.res.status = h1sl.st.status;
982
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200983 while (raw.len > ret) {
984 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
Christopher Fauleta66adf42020-11-05 22:43:41 +0100985 if (!sent) {
986 memprintf(errmsg, "unable to add payload into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100987 goto error;
Christopher Fauleta66adf42020-11-05 22:43:41 +0100988 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200989 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100990 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200991
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100992 htx->flags |= HTX_FL_EOM;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200993
Christopher Faulet90cc4812019-07-22 16:49:30 +0200994 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100995
996error:
997 if (buf->size)
998 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200999 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001000}
1001
Christopher Faulet18630642020-05-12 18:57:28 +02001002void release_http_reply(struct http_reply *http_reply)
1003{
1004 struct logformat_node *lf, *lfb;
1005 struct http_reply_hdr *hdr, *hdrb;
1006
1007 if (!http_reply)
1008 return;
1009
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001010 ha_free(&http_reply->ctype);
Christopher Faulet18630642020-05-12 18:57:28 +02001011 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001012 LIST_DELETE(&hdr->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001013 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001014 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001015 release_sample_expr(lf->expr);
1016 free(lf->arg);
1017 free(lf);
1018 }
1019 istfree(&hdr->name);
1020 free(hdr);
1021 }
1022
1023 if (http_reply->type == HTTP_REPLY_ERRFILES) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001024 ha_free(&http_reply->body.http_errors);
Christopher Faulet18630642020-05-12 18:57:28 +02001025 }
1026 else if (http_reply->type == HTTP_REPLY_RAW)
1027 chunk_destroy(&http_reply->body.obj);
1028 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1029 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001030 LIST_DELETE(&lf->list);
Christopher Faulet18630642020-05-12 18:57:28 +02001031 release_sample_expr(lf->expr);
1032 free(lf->arg);
1033 free(lf);
1034 }
1035 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001036 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001037}
1038
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001039static int http_htx_init(void)
1040{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001041 struct buffer chk;
1042 struct ist raw;
Christopher Fauleta66adf42020-11-05 22:43:41 +01001043 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001044 int rc;
1045 int err_code = 0;
1046
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001047 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1048 if (!http_err_msgs[rc]) {
Christopher Fauleta66adf42020-11-05 22:43:41 +01001049 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001050 err_code |= ERR_ALERT | ERR_FATAL;
1051 continue;
1052 }
1053
1054 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Fauleta66adf42020-11-05 22:43:41 +01001055 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1056 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1057 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001058 err_code |= ERR_ALERT | ERR_FATAL;
1059 }
Christopher Fauleta66adf42020-11-05 22:43:41 +01001060 else if (errmsg) {
1061 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1062 err_code |= ERR_WARN;
1063 }
1064
1065 /* Reset errmsg */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001066 ha_free(&errmsg);
Christopher Fauleta66adf42020-11-05 22:43:41 +01001067
Christopher Fauletf7346382019-07-17 22:02:08 +02001068 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001069 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1070 http_err_replies[rc].status = http_err_codes[rc];
1071 http_err_replies[rc].ctype = NULL;
1072 LIST_INIT(&http_err_replies[rc].hdrs);
1073 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001074 }
1075end:
1076 return err_code;
1077}
1078
Christopher Faulet58857752020-01-15 15:19:50 +01001079static void http_htx_deinit(void)
1080{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001081 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001082 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001083 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001084 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001085 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001086
1087 node = ebpt_first(&http_error_messages);
1088 while (node) {
1089 next = ebpt_next(node);
1090 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001091 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1092 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001093 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001094 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001095 node = next;
1096 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001097
1098 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1099 free(http_errs->conf.file);
1100 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001101 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1102 release_http_reply(http_errs->replies[rc]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001103 LIST_DELETE(&http_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001104 free(http_errs);
1105 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001106
1107 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001108 LIST_DELETE(&http_rep->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001109 release_http_reply(http_rep);
1110 }
Christopher Faulet58857752020-01-15 15:19:50 +01001111}
1112
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001113REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001114REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001115
Christopher Faulet58857752020-01-15 15:19:50 +01001116/* Reads content of the error file <file> and convert it into an HTX message. On
1117 * success, the HTX message is returned. On error, NULL is returned and an error
1118 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001119 */
Christopher Faulet58857752020-01-15 15:19:50 +01001120struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001121{
Christopher Faulet58857752020-01-15 15:19:50 +01001122 struct buffer *buf = NULL;
1123 struct buffer chk;
1124 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001125 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001126 struct stat stat;
1127 char *err = NULL;
1128 int errnum, errlen;
1129 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001130
1131 /* already loaded */
1132 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1133 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001134 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1135 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001136 goto out;
1137 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001138
Christopher Faulet58857752020-01-15 15:19:50 +01001139 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001140 fd = open(file, O_RDONLY);
1141 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1142 memprintf(errmsg, "error opening file '%s'.", file);
1143 goto out;
1144 }
1145
1146 if (stat.st_size <= global.tune.bufsize)
1147 errlen = stat.st_size;
1148 else {
1149 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1150 file, global.tune.bufsize);
1151 errlen = global.tune.bufsize;
1152 }
1153
1154 err = malloc(errlen);
1155 if (!err) {
1156 memprintf(errmsg, "out of memory.");
1157 goto out;
1158 }
1159
1160 errnum = read(fd, err, errlen);
1161 if (errnum != errlen) {
1162 memprintf(errmsg, "error reading file '%s'.", file);
1163 goto out;
1164 }
1165
Christopher Faulet58857752020-01-15 15:19:50 +01001166 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001167 http_errmsg = calloc(1, sizeof(*http_errmsg));
1168 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001169 memprintf(errmsg, "out of memory.");
1170 goto out;
1171 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001172 http_errmsg->node.key = strdup(file);
1173 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001174 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001175 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001176 goto out;
1177 }
1178
1179 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001180 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1181 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001182 free(http_errmsg->node.key);
1183 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001184 goto out;
1185 }
1186
Christopher Faulet58857752020-01-15 15:19:50 +01001187 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001188 http_errmsg->msg = chk;
1189 ebis_insert(&http_error_messages, &http_errmsg->node);
1190 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001191
Christopher Faulet5031ef52020-01-15 11:22:07 +01001192 out:
1193 if (fd >= 0)
1194 close(fd);
1195 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001196 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001197}
1198
Ilya Shipitsind4259502020-04-08 01:07:56 +05001199/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001200 * message is returned. On error, NULL is returned and an error message is
1201 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001202 */
Christopher Faulet58857752020-01-15 15:19:50 +01001203struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001204{
Christopher Faulet58857752020-01-15 15:19:50 +01001205 struct buffer *buf = NULL;
1206 struct buffer chk;
1207 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001208 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001209
1210 /* already loaded */
1211 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1212 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001213 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1214 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001215 goto out;
1216 }
1217 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001218 http_errmsg = calloc(1, sizeof(*http_errmsg));
1219 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001220 memprintf(errmsg, "out of memory.");
1221 goto out;
1222 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001223 http_errmsg->node.key = strdup(key);
1224 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001225 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001226 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001227 goto out;
1228 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001229
1230 /* Convert the error file into an HTX message */
Christopher Fauleta66adf42020-11-05 22:43:41 +01001231 if (!http_str_to_htx(&chk, msg, errmsg)) {
1232 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001233 free(http_errmsg->node.key);
1234 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001235 goto out;
1236 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001237
Christopher Faulet58857752020-01-15 15:19:50 +01001238 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001239 http_errmsg->msg = chk;
1240 ebis_insert(&http_error_messages, &http_errmsg->node);
1241 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001242 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001243 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001244}
1245
Christopher Faulet5031ef52020-01-15 11:22:07 +01001246/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001247 * <status>. It returns NULL if there is any error, otherwise it return the
1248 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001249 */
Christopher Faulet58857752020-01-15 15:19:50 +01001250struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001251{
Christopher Faulet58857752020-01-15 15:19:50 +01001252 struct buffer *buf = NULL;
1253 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001254
1255 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1256 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001257 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001258 break;
1259 }
1260 }
1261
1262 if (rc >= HTTP_ERR_SIZE)
1263 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001264 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001265}
1266
1267/* This function creates HTX error message corresponding to a redirect message
1268 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001269 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1270 * returns NULL if there is any error, otherwise it return the corresponding HTX
1271 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001272 */
Christopher Faulet58857752020-01-15 15:19:50 +01001273struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001274{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001275 static const char *HTTP_302 =
1276 "HTTP/1.1 302 Found\r\n"
1277 "Cache-Control: no-cache\r\n"
1278 "Content-length: 0\r\n"
1279 "Location: "; /* not terminated since it will be concatenated with the URL */
1280 static const char *HTTP_303 =
1281 "HTTP/1.1 303 See Other\r\n"
1282 "Cache-Control: no-cache\r\n"
1283 "Content-length: 0\r\n"
1284 "Location: "; /* not terminated since it will be concatenated with the URL */
1285
Christopher Faulet58857752020-01-15 15:19:50 +01001286 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001287 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001288 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001289 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001290
1291 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1292 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001293 /* Create the error key */
1294 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1295 memprintf(errmsg, "out of memory.");
1296 goto out;
1297 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001298 /* Create the error message */
1299 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1300 errlen = strlen(msg) + strlen(url) + 5;
1301 err = malloc(errlen);
1302 if (!err) {
1303 memprintf(errmsg, "out of memory.");
1304 goto out;
1305 }
1306 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1307
1308 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001309 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001310 break;
1311 }
1312 }
1313
1314 if (rc >= HTTP_ERR_SIZE)
1315 memprintf(errmsg, "status code '%d' not handled.", status);
1316out:
Christopher Faulet58857752020-01-15 15:19:50 +01001317 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001318 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001319 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001320}
1321
Christopher Faulet7eea2412020-05-13 15:02:59 +02001322/* Check an "http reply" and, for replies referencing an http-errors section,
1323 * try to find the right section and the right error message in this section. If
1324 * found, the reply is updated. If the http-errors section exists but the error
1325 * message is not found, no error message is set to fallback on the default
1326 * ones. Otherwise (unknown section) an error is returned.
1327 *
1328 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1329 * filled.
1330 */
1331int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1332{
1333 struct http_errors *http_errs;
1334 int ret = 1;
1335
1336 if (reply->type != HTTP_REPLY_ERRFILES)
1337 goto end;
1338
1339 list_for_each_entry(http_errs, &http_errors_list, list) {
1340 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001341 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001342 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001343 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1344 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001345 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1346 "not declared in http-errors section '%s'.\n",
1347 px->id, reply->status, http_errs->id);
1348 break;
1349 }
1350 }
1351
1352 if (&http_errs->list == &http_errors_list) {
1353 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1354 reply->body.http_errors);
1355 ret = 0;
1356 }
1357
1358 end:
1359 return ret;
1360}
1361
Christopher Faulet47e791e2020-05-13 14:36:55 +02001362/* Parse an "http reply". It returns the reply on success or NULL on error. This
1363 * function creates one of the following http replies :
1364 *
1365 * - HTTP_REPLY_EMPTY : dummy response, no payload
1366 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1367 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1368 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1369 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1370 *
1371 * The content-type must be defined for non-empty payload. It is ignored for
1372 * error messages (implicit or explicit). When an http-errors section is
1373 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1374 * during the configuration validity check or dynamically. It is the caller
1375 * responsibility to choose. If no status code is configured, <default_status>
1376 * is set.
1377 */
1378struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1379 int default_status, char **errmsg)
1380{
1381 struct logformat_node *lf, *lfb;
1382 struct http_reply *reply = NULL;
1383 struct http_reply_hdr *hdr, *hdrb;
1384 struct stat stat;
1385 const char *act_arg = NULL;
1386 char *obj = NULL;
1387 int cur_arg, cap, objlen = 0, fd = -1;
1388
1389
1390 reply = calloc(1, sizeof(*reply));
1391 if (!reply) {
1392 memprintf(errmsg, "out of memory");
1393 goto error;
1394 }
1395 LIST_INIT(&reply->hdrs);
1396 reply->type = HTTP_REPLY_EMPTY;
1397 reply->status = default_status;
1398
Christopher Faulet3b967c12020-05-15 15:47:44 +02001399 if (px->conf.args.ctx == ARGC_HERR)
1400 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1401 else
1402 cap = ((px->conf.args.ctx == ARGC_HRQ)
1403 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1404 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001405
1406 cur_arg = *orig_arg;
1407 while (*args[cur_arg]) {
1408 if (strcmp(args[cur_arg], "status") == 0) {
1409 cur_arg++;
1410 if (!*args[cur_arg]) {
1411 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1412 goto error;
1413 }
1414 reply->status = atol(args[cur_arg]);
1415 if (reply->status < 200 || reply->status > 599) {
1416 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1417 goto error;
1418 }
1419 cur_arg++;
1420 }
1421 else if (strcmp(args[cur_arg], "content-type") == 0) {
1422 cur_arg++;
1423 if (!*args[cur_arg]) {
1424 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1425 goto error;
1426 }
1427 free(reply->ctype);
1428 reply->ctype = strdup(args[cur_arg]);
1429 cur_arg++;
1430 }
1431 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1432 if (reply->type != HTTP_REPLY_EMPTY) {
1433 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1434 goto error;
1435 }
1436 act_arg = args[cur_arg];
1437 cur_arg++;
1438 if (!*args[cur_arg]) {
1439 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1440 goto error;
1441 }
1442 reply->body.http_errors = strdup(args[cur_arg]);
1443 if (!reply->body.http_errors) {
1444 memprintf(errmsg, "out of memory");
1445 goto error;
1446 }
1447 reply->type = HTTP_REPLY_ERRFILES;
1448 cur_arg++;
1449 }
1450 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1451 if (reply->type != HTTP_REPLY_EMPTY) {
1452 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1453 goto error;
1454 }
1455 act_arg = args[cur_arg];
1456 reply->type = HTTP_REPLY_ERRMSG;
1457 cur_arg++;
1458 }
1459 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1460 if (reply->type != HTTP_REPLY_EMPTY) {
1461 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1462 goto error;
1463 }
1464 act_arg = args[cur_arg];
1465 cur_arg++;
1466 if (!*args[cur_arg]) {
1467 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1468 goto error;
1469 }
1470 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1471 if (!reply->body.errmsg) {
1472 goto error;
1473 }
1474 reply->type = HTTP_REPLY_ERRMSG;
1475 cur_arg++;
1476 }
1477 else if (strcmp(args[cur_arg], "file") == 0) {
1478 if (reply->type != HTTP_REPLY_EMPTY) {
1479 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1480 goto error;
1481 }
1482 act_arg = args[cur_arg];
1483 cur_arg++;
1484 if (!*args[cur_arg]) {
1485 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1486 goto error;
1487 }
1488 fd = open(args[cur_arg], O_RDONLY);
1489 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1490 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1491 goto error;
1492 }
1493 if (stat.st_size > global.tune.bufsize) {
1494 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1495 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1496 goto error;
1497 }
1498 objlen = stat.st_size;
1499 obj = malloc(objlen);
1500 if (!obj || read(fd, obj, objlen) != objlen) {
1501 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1502 goto error;
1503 }
1504 close(fd);
1505 fd = -1;
1506 reply->type = HTTP_REPLY_RAW;
1507 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1508 obj = NULL;
1509 cur_arg++;
1510 }
1511 else if (strcmp(args[cur_arg], "string") == 0) {
1512 if (reply->type != HTTP_REPLY_EMPTY) {
1513 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1514 goto error;
1515 }
1516 act_arg = args[cur_arg];
1517 cur_arg++;
1518 if (!*args[cur_arg]) {
1519 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1520 goto error;
1521 }
1522 obj = strdup(args[cur_arg]);
1523 objlen = strlen(args[cur_arg]);
1524 if (!obj) {
1525 memprintf(errmsg, "out of memory");
1526 goto error;
1527 }
1528 reply->type = HTTP_REPLY_RAW;
1529 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1530 obj = NULL;
1531 cur_arg++;
1532 }
1533 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1534 if (reply->type != HTTP_REPLY_EMPTY) {
1535 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1536 goto error;
1537 }
1538 act_arg = args[cur_arg];
1539 cur_arg++;
1540 if (!*args[cur_arg]) {
1541 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1542 goto error;
1543 }
1544 fd = open(args[cur_arg], O_RDONLY);
1545 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1546 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1547 goto error;
1548 }
1549 if (stat.st_size > global.tune.bufsize) {
1550 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1551 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1552 goto error;
1553 }
1554 objlen = stat.st_size;
1555 obj = malloc(objlen + 1);
1556 if (!obj || read(fd, obj, objlen) != objlen) {
1557 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1558 goto error;
1559 }
1560 close(fd);
1561 fd = -1;
1562 obj[objlen] = '\0';
1563 reply->type = HTTP_REPLY_LOGFMT;
1564 cur_arg++;
1565 }
1566 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1567 if (reply->type != HTTP_REPLY_EMPTY) {
1568 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1569 goto error;
1570 }
1571 act_arg = args[cur_arg];
1572 cur_arg++;
1573 if (!*args[cur_arg]) {
1574 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1575 goto error;
1576 }
1577 obj = strdup(args[cur_arg]);
1578 objlen = strlen(args[cur_arg]);
1579 reply->type = HTTP_REPLY_LOGFMT;
1580 cur_arg++;
1581 }
1582 else if (strcmp(args[cur_arg], "hdr") == 0) {
1583 cur_arg++;
1584 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1585 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1586 goto error;
1587 }
1588 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1589 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1590 strcasecmp(args[cur_arg], "content-type") == 0) {
1591 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1592 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1593 cur_arg += 2;
1594 continue;
1595 }
1596 hdr = calloc(1, sizeof(*hdr));
1597 if (!hdr) {
1598 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1599 goto error;
1600 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001601 LIST_APPEND(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001602 LIST_INIT(&hdr->value);
1603 hdr->name = ist(strdup(args[cur_arg]));
1604 if (!isttest(hdr->name)) {
1605 memprintf(errmsg, "out of memory");
1606 goto error;
1607 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001608 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1609 goto error;
1610
1611 free(px->conf.lfs_file);
1612 px->conf.lfs_file = strdup(px->conf.args.file);
1613 px->conf.lfs_line = px->conf.args.line;
1614 cur_arg += 2;
1615 }
1616 else
1617 break;
1618 }
1619
1620 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1621 if (reply->ctype) {
1622 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1623 " neither errorfile nor payload defined.\n",
1624 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001625 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001626 }
1627 }
1628 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1629
1630 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1631 /* default errorfile or errorfiles: check the status */
1632 int rc;
1633
1634 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1635 if (http_err_codes[rc] == reply->status)
1636 break;
1637 }
1638
1639 if (rc >= HTTP_ERR_SIZE) {
1640 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1641 reply->status, act_arg);
1642 goto error;
1643 }
1644 }
1645
1646 if (reply->ctype) {
1647 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1648 "with an erorrfile.\n",
1649 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001650 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001651 }
1652 if (!LIST_ISEMPTY(&reply->hdrs)) {
1653 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1654 "with an erorrfile.\n",
1655 px->conf.args.file, px->conf.args.line);
1656 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001657 LIST_DELETE(&hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001658 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001659 LIST_DELETE(&lf->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001660 release_sample_expr(lf->expr);
1661 free(lf->arg);
1662 free(lf);
1663 }
1664 istfree(&hdr->name);
1665 free(hdr);
1666 }
1667 }
1668 }
1669 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001670 if ((reply->status == 204 || reply->status == 304) && objlen) {
1671 memprintf(errmsg, "No body expected for %d responses", reply->status);
1672 goto error;
1673 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001674 if (!reply->ctype && objlen) {
1675 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1676 goto error;
1677 }
1678 if (reply->ctype && !b_data(&reply->body.obj)) {
1679 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001680 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001681 px->conf.args.file, px->conf.args.line, reply->ctype);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001682 ha_free(&reply->ctype);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001683 }
1684 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1685 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1686 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1687 px->conf.args.file, px->conf.args.line);
1688 }
1689 }
1690 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1691 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001692 if ((reply->status == 204 || reply->status == 304)) {
1693 memprintf(errmsg, "No body expected for %d responses", reply->status);
1694 goto error;
1695 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001696 if (!reply->ctype) {
1697 memprintf(errmsg, "a content type must be defined with a log-format payload");
1698 goto error;
1699 }
1700 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1701 goto error;
1702
1703 free(px->conf.lfs_file);
1704 px->conf.lfs_file = strdup(px->conf.args.file);
1705 px->conf.lfs_line = px->conf.args.line;
1706 }
1707
1708 free(obj);
1709 *orig_arg = cur_arg;
1710 return reply;
1711
1712 error:
1713 free(obj);
1714 if (fd >= 0)
1715 close(fd);
1716 release_http_reply(reply);
1717 return NULL;
1718}
1719
Christopher Faulet07f41f72020-01-16 16:16:06 +01001720/* Parses the "errorloc[302|303]" proxy keyword */
1721static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001722 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001723 char **errmsg)
1724{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001725 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001726 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001727 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001728 int errloc, status;
1729 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001730
1731 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1732 ret = 1;
1733 goto out;
1734 }
1735
1736 if (*(args[1]) == 0 || *(args[2]) == 0) {
1737 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1738 ret = -1;
1739 goto out;
1740 }
1741
1742 status = atol(args[1]);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001743 errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001744 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1745 if (!msg) {
1746 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1747 ret = -1;
1748 goto out;
1749 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001750
1751 reply = calloc(1, sizeof(*reply));
1752 if (!reply) {
1753 memprintf(errmsg, "%s : out of memory.", args[0]);
1754 ret = -1;
1755 goto out;
1756 }
1757 reply->type = HTTP_REPLY_ERRMSG;
1758 reply->status = status;
1759 reply->ctype = NULL;
1760 LIST_INIT(&reply->hdrs);
1761 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001762 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001763
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001764 conf_err = calloc(1, sizeof(*conf_err));
1765 if (!conf_err) {
1766 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001767 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001768 ret = -1;
1769 goto out;
1770 }
1771 conf_err->type = 1;
1772 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001773 conf_err->info.errorfile.reply = reply;
1774
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001775 conf_err->file = strdup(file);
1776 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001777 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001778
Christopher Fauleta66adf42020-11-05 22:43:41 +01001779 /* handle warning message */
1780 if (*errmsg)
1781 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001782 out:
1783 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001784
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001785}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001786
1787/* Parses the "errorfile" proxy keyword */
1788static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001789 const struct proxy *defpx, const char *file, int line,
Christopher Faulet07f41f72020-01-16 16:16:06 +01001790 char **errmsg)
1791{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001792 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001793 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001794 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001795 int status;
1796 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001797
1798 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1799 ret = 1;
1800 goto out;
1801 }
1802
1803 if (*(args[1]) == 0 || *(args[2]) == 0) {
1804 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1805 ret = -1;
1806 goto out;
1807 }
1808
1809 status = atol(args[1]);
1810 msg = http_parse_errorfile(status, args[2], errmsg);
1811 if (!msg) {
1812 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1813 ret = -1;
1814 goto out;
1815 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001816
Christopher Faulet5809e102020-05-14 17:31:52 +02001817 reply = calloc(1, sizeof(*reply));
1818 if (!reply) {
1819 memprintf(errmsg, "%s : out of memory.", args[0]);
1820 ret = -1;
1821 goto out;
1822 }
1823 reply->type = HTTP_REPLY_ERRMSG;
1824 reply->status = status;
1825 reply->ctype = NULL;
1826 LIST_INIT(&reply->hdrs);
1827 reply->body.errmsg = msg;
Willy Tarreau2b718102021-04-21 07:32:39 +02001828 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet5809e102020-05-14 17:31:52 +02001829
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001830 conf_err = calloc(1, sizeof(*conf_err));
1831 if (!conf_err) {
1832 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001833 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001834 ret = -1;
1835 goto out;
1836 }
1837 conf_err->type = 1;
1838 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001839 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001840 conf_err->file = strdup(file);
1841 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001842 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001843
Christopher Fauleta66adf42020-11-05 22:43:41 +01001844 /* handle warning message */
1845 if (*errmsg)
1846 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001847 out:
1848 return ret;
1849
1850}
1851
1852/* Parses the "errorfiles" proxy keyword */
1853static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001854 const struct proxy *defpx, const char *file, int line,
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001855 char **err)
1856{
1857 struct conf_errors *conf_err = NULL;
1858 char *name = NULL;
1859 int rc, ret = 0;
1860
1861 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1862 ret = 1;
1863 goto out;
1864 }
1865
1866 if (!*(args[1])) {
1867 memprintf(err, "%s : expects <name> as argument.", args[0]);
1868 ret = -1;
1869 goto out;
1870 }
1871
1872 name = strdup(args[1]);
1873 conf_err = calloc(1, sizeof(*conf_err));
1874 if (!name || !conf_err) {
1875 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001876 goto error;
1877 }
1878 conf_err->type = 0;
1879
1880 conf_err->info.errorfiles.name = name;
1881 if (!*(args[2])) {
1882 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1883 conf_err->info.errorfiles.status[rc] = 1;
1884 }
1885 else {
1886 int cur_arg, status;
1887 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1888 status = atol(args[cur_arg]);
1889
1890 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1891 if (http_err_codes[rc] == status) {
1892 conf_err->info.errorfiles.status[rc] = 2;
1893 break;
1894 }
1895 }
1896 if (rc >= HTTP_ERR_SIZE) {
1897 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001898 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001899 }
1900 }
1901 }
1902 conf_err->file = strdup(file);
1903 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001904 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001905 out:
1906 return ret;
1907
1908 error:
1909 free(name);
1910 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001911 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001912 goto out;
1913}
1914
Christopher Faulet3b967c12020-05-15 15:47:44 +02001915/* Parses the "http-error" proxy keyword */
1916static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001917 const struct proxy *defpx, const char *file, int line,
Christopher Faulet3b967c12020-05-15 15:47:44 +02001918 char **errmsg)
1919{
1920 struct conf_errors *conf_err;
1921 struct http_reply *reply = NULL;
1922 int rc, cur_arg, ret = 0;
1923
1924 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1925 ret = 1;
1926 goto out;
1927 }
1928
1929 cur_arg = 1;
1930 curpx->conf.args.ctx = ARGC_HERR;
1931 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1932 if (!reply) {
1933 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1934 goto error;
1935 }
1936 else if (!reply->status) {
1937 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1938 goto error;
1939 }
1940
1941 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1942 if (http_err_codes[rc] == reply->status)
1943 break;
1944 }
1945
1946 if (rc >= HTTP_ERR_SIZE) {
1947 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1948 goto error;
1949 }
1950 if (*args[cur_arg]) {
1951 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1952 goto error;
1953 }
1954
1955 conf_err = calloc(1, sizeof(*conf_err));
1956 if (!conf_err) {
1957 memprintf(errmsg, "%s : out of memory.", args[0]);
1958 goto error;
1959 }
1960 if (reply->type == HTTP_REPLY_ERRFILES) {
1961 int rc = http_get_status_idx(reply->status);
1962
1963 conf_err->type = 2;
1964 conf_err->info.errorfiles.name = reply->body.http_errors;
1965 conf_err->info.errorfiles.status[rc] = 2;
1966 reply->body.http_errors = NULL;
1967 release_http_reply(reply);
1968 }
1969 else {
1970 conf_err->type = 1;
1971 conf_err->info.errorfile.status = reply->status;
1972 conf_err->info.errorfile.reply = reply;
Willy Tarreau2b718102021-04-21 07:32:39 +02001973 LIST_APPEND(&http_replies_list, &reply->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02001974 }
1975 conf_err->file = strdup(file);
1976 conf_err->line = line;
Willy Tarreau2b718102021-04-21 07:32:39 +02001977 LIST_APPEND(&curpx->conf.errors, &conf_err->list);
Christopher Faulet3b967c12020-05-15 15:47:44 +02001978
Christopher Faulet3005d282020-11-13 10:58:01 +01001979 /* handle warning message */
1980 if (*errmsg)
1981 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001982 out:
1983 return ret;
1984
1985 error:
1986 release_http_reply(reply);
1987 ret = -1;
1988 goto out;
1989
1990}
1991
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001992/* Check "errorfiles" proxy keyword */
1993static int proxy_check_errors(struct proxy *px)
1994{
1995 struct conf_errors *conf_err, *conf_err_back;
1996 struct http_errors *http_errs;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001997 int rc, err = ERR_NONE;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001998
1999 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2000 if (conf_err->type == 1) {
2001 /* errorfile */
2002 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002003 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002004
2005 /* For proxy, to rely on default replies, just don't reference a reply */
2006 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2007 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002008 }
2009 else {
2010 /* errorfiles */
2011 list_for_each_entry(http_errs, &http_errors_list, list) {
2012 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2013 break;
2014 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002015
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002016 /* unknown http-errors section */
2017 if (&http_errs->list == &http_errors_list) {
2018 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2019 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2020 err |= ERR_ALERT | ERR_FATAL;
2021 free(conf_err->info.errorfiles.name);
2022 goto next;
2023 }
2024
2025 free(conf_err->info.errorfiles.name);
2026 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2027 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002028 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002029 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002030 else if (conf_err->info.errorfiles.status[rc] == 2)
2031 ha_warning("config: proxy '%s' : status '%d' not declared in"
2032 " http-errors section '%s' (at %s:%d).\n",
2033 px->id, http_err_codes[rc], http_errs->id,
2034 conf_err->file, conf_err->line);
2035 }
2036 }
2037 }
2038 next:
Willy Tarreau2b718102021-04-21 07:32:39 +02002039 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002040 free(conf_err->file);
2041 free(conf_err);
2042 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002043
2044 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002045 return err;
2046}
2047
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002048static int post_check_errors()
2049{
2050 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002051 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002052 struct htx *htx;
Christopher Fauletfc633b62020-11-06 15:24:23 +01002053 int err_code = ERR_NONE;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002054
2055 node = ebpt_first(&http_error_messages);
2056 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002057 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2058 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002059 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002060 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002061 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2062 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002063 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002064 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002065 (char *)node->key);
2066 err_code |= ERR_WARN;
2067 }
2068 next:
2069 node = ebpt_next(node);
2070 }
2071
2072 return err_code;
2073}
2074
Willy Tarreau016255a2021-02-12 08:40:29 +01002075int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg)
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002076{
2077 struct conf_errors *conf_err, *new_conf_err = NULL;
2078 int ret = 0;
2079
2080 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2081 new_conf_err = calloc(1, sizeof(*new_conf_err));
2082 if (!new_conf_err) {
2083 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2084 goto out;
2085 }
2086 new_conf_err->type = conf_err->type;
2087 if (conf_err->type == 1) {
2088 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002089 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002090 }
2091 else {
2092 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2093 if (!new_conf_err->info.errorfiles.name) {
2094 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2095 goto out;
2096 }
2097 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2098 sizeof(conf_err->info.errorfiles.status));
2099 }
2100 new_conf_err->file = strdup(conf_err->file);
2101 new_conf_err->line = conf_err->line;
Willy Tarreau2b718102021-04-21 07:32:39 +02002102 LIST_APPEND(&curpx->conf.errors, &new_conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002103 new_conf_err = NULL;
2104 }
2105 ret = 1;
2106
2107 out:
2108 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002109 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002110}
2111
2112void proxy_release_conf_errors(struct proxy *px)
2113{
2114 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002115
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002116 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2117 if (conf_err->type == 0)
2118 free(conf_err->info.errorfiles.name);
Willy Tarreau2b718102021-04-21 07:32:39 +02002119 LIST_DELETE(&conf_err->list);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002120 free(conf_err->file);
2121 free(conf_err);
2122 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002123}
2124
2125/*
2126 * Parse an <http-errors> section.
2127 * Returns the error code, 0 if OK, or any combination of :
2128 * - ERR_ABORT: must abort ASAP
2129 * - ERR_FATAL: we can continue parsing but not start the service
2130 * - ERR_WARN: a warning has been emitted
2131 * - ERR_ALERT: an alert has been emitted
2132 * Only the two first ones can stop processing, the two others are just
2133 * indicators.
2134 */
2135static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2136{
2137 static struct http_errors *curr_errs = NULL;
2138 int err_code = 0;
2139 const char *err;
2140 char *errmsg = NULL;
2141
2142 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2143 if (!*args[1]) {
2144 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2145 err_code |= ERR_ALERT | ERR_ABORT;
2146 goto out;
2147 }
2148
2149 err = invalid_char(args[1]);
2150 if (err) {
2151 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2152 file, linenum, *err, args[0], args[1]);
2153 err_code |= ERR_ALERT | ERR_FATAL;
2154 }
2155
2156 list_for_each_entry(curr_errs, &http_errors_list, list) {
2157 /* Error if two errors section owns the same name */
2158 if (strcmp(curr_errs->id, args[1]) == 0) {
2159 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2160 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2161 err_code |= ERR_ALERT | ERR_FATAL;
2162 }
2163 }
2164
2165 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2166 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2167 err_code |= ERR_ALERT | ERR_ABORT;
2168 goto out;
2169 }
2170
Willy Tarreau2b718102021-04-21 07:32:39 +02002171 LIST_APPEND(&http_errors_list, &curr_errs->list);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002172 curr_errs->id = strdup(args[1]);
2173 curr_errs->conf.file = strdup(file);
2174 curr_errs->conf.line = linenum;
2175 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002176 else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002177 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002178 struct buffer *msg;
2179 int status, rc;
2180
2181 if (*(args[1]) == 0 || *(args[2]) == 0) {
2182 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2183 file, linenum, args[0]);
2184 err_code |= ERR_ALERT | ERR_FATAL;
2185 goto out;
2186 }
2187
2188 status = atol(args[1]);
2189 msg = http_parse_errorfile(status, args[2], &errmsg);
2190 if (!msg) {
2191 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2192 err_code |= ERR_ALERT | ERR_FATAL;
2193 goto out;
2194 }
Christopher Faulet3005d282020-11-13 10:58:01 +01002195 if (errmsg) {
2196 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2197 err_code |= ERR_WARN;
2198 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002199
2200 reply = calloc(1, sizeof(*reply));
2201 if (!reply) {
2202 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2203 err_code |= ERR_ALERT | ERR_FATAL;
2204 goto out;
2205 }
2206 reply->type = HTTP_REPLY_ERRMSG;
2207 reply->status = status;
2208 reply->ctype = NULL;
2209 LIST_INIT(&reply->hdrs);
2210 reply->body.errmsg = msg;
2211
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002212 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002213 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002214 }
2215 else if (*args[0] != 0) {
2216 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220
2221out:
2222 free(errmsg);
2223 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002224}
2225
2226static struct cfg_kw_list cfg_kws = {ILH, {
2227 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2228 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2229 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2230 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002231 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002232 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002233 { 0, NULL, NULL },
2234}};
2235
2236INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002237REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002238REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002239
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002240REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2241
Christopher Faulet29f72842019-12-11 15:52:32 +01002242/************************************************************************/
2243/* HTX sample fetches */
2244/************************************************************************/
2245
2246/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2247static int
2248smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2249{
2250 if (!smp->strm)
2251 return 0;
2252
2253 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2254 smp->data.type = SMP_T_BOOL;
2255 return 1;
2256}
2257
2258/* Returns the number of blocks in an HTX message. The channel is chosen
2259 * depending on the sample direction. */
2260static int
2261smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2262{
2263 struct channel *chn;
2264 struct htx *htx;
2265
2266 if (!smp->strm)
2267 return 0;
2268
2269 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002270 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002271 if (!htx)
2272 return 0;
2273
2274 smp->data.u.sint = htx_nbblks(htx);
2275 smp->data.type = SMP_T_SINT;
2276 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2277 return 1;
2278}
2279
2280/* Returns the size of an HTX message. The channel is chosen depending on the
2281 * sample direction. */
2282static int
2283smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2284{
2285 struct channel *chn;
2286 struct htx *htx;
2287
2288 if (!smp->strm)
2289 return 0;
2290
2291 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002292 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002293 if (!htx)
2294 return 0;
2295
2296 smp->data.u.sint = htx->size;
2297 smp->data.type = SMP_T_SINT;
2298 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2299 return 1;
2300}
2301
2302/* Returns the data size of an HTX message. The channel is chosen depending on the
2303 * sample direction. */
2304static int
2305smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2306{
2307 struct channel *chn;
2308 struct htx *htx;
2309
2310 if (!smp->strm)
2311 return 0;
2312
2313 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002314 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002315 if (!htx)
2316 return 0;
2317
2318 smp->data.u.sint = htx->data;
2319 smp->data.type = SMP_T_SINT;
2320 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2321 return 1;
2322}
2323
2324/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2325 * depending on the sample direction. */
2326static int
2327smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2328{
2329 struct channel *chn;
2330 struct htx *htx;
2331
2332 if (!smp->strm)
2333 return 0;
2334
2335 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002336 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002337 if (!htx)
2338 return 0;
2339
2340 smp->data.u.sint = htx_used_space(htx);
2341 smp->data.type = SMP_T_SINT;
2342 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2343 return 1;
2344}
2345
2346/* Returns the free space (size-used) of an HTX message. The channel is chosen
2347 * depending on the sample direction. */
2348static int
2349smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2350{
2351 struct channel *chn;
2352 struct htx *htx;
2353
2354 if (!smp->strm)
2355 return 0;
2356
2357 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002358 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002359 if (!htx)
2360 return 0;
2361
2362 smp->data.u.sint = htx_free_space(htx);
2363 smp->data.type = SMP_T_SINT;
2364 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2365 return 1;
2366}
2367
2368/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2369 * channel is chosen depending on the sample direction. */
2370static int
2371smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2372{
2373 struct channel *chn;
2374 struct htx *htx;
2375
2376 if (!smp->strm)
2377 return 0;
2378
2379 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002380 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002381 if (!htx)
2382 return 0;
2383
2384 smp->data.u.sint = htx_free_data_space(htx);
2385 smp->data.type = SMP_T_SINT;
2386 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2387 return 1;
2388}
2389
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002390/* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The
2391 * channel is chosen depending on the sample direction.
2392 */
Christopher Faulet29f72842019-12-11 15:52:32 +01002393static int
2394smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2395{
2396 struct channel *chn;
2397 struct htx *htx;
2398
2399 if (!smp->strm)
2400 return 0;
2401
2402 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002403 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002404 if (!htx)
2405 return 0;
2406
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002407 smp->data.u.sint = !!(htx->flags & HTX_FL_EOM);
Christopher Faulet29f72842019-12-11 15:52:32 +01002408 smp->data.type = SMP_T_BOOL;
2409 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2410 return 1;
2411}
2412
2413/* Returns the type of a specific HTX block, if found in the message. Otherwise
2414 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2415 * "head", "tail" or "first". The channel is chosen depending on the sample
2416 * direction. */
2417static int
2418smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2419{
2420 struct channel *chn;
2421 struct htx *htx;
2422 enum htx_blk_type type;
2423 int32_t pos;
2424
2425 if (!smp->strm || !arg_p)
2426 return 0;
2427
2428 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002429 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002430 if (!htx)
2431 return 0;
2432
2433 pos = arg_p[0].data.sint;
2434 if (pos == -1)
2435 type = htx_get_head_type(htx);
2436 else if (pos == -2)
2437 type = htx_get_tail_type(htx);
2438 else if (pos == -3)
2439 type = htx_get_first_type(htx);
2440 else
2441 type = ((pos >= htx->head && pos <= htx->tail)
2442 ? htx_get_blk_type(htx_get_blk(htx, pos))
2443 : HTX_BLK_UNUSED);
2444
2445 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2446 smp->data.type = SMP_T_STR;
2447 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2448 return 1;
2449}
2450
2451/* Returns the size of a specific HTX block, if found in the message. Otherwise
2452 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2453 * "first". The channel is chosen depending on the sample direction. */
2454static int
2455smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2456{
2457 struct channel *chn;
2458 struct htx *htx;
2459 struct htx_blk *blk;
2460 int32_t pos;
2461
2462 if (!smp->strm || !arg_p)
2463 return 0;
2464
2465 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002466 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002467 if (!htx)
2468 return 0;
2469
2470 pos = arg_p[0].data.sint;
2471 if (pos == -1)
2472 blk = htx_get_head_blk(htx);
2473 else if (pos == -2)
2474 blk = htx_get_tail_blk(htx);
2475 else if (pos == -3)
2476 blk = htx_get_first_blk(htx);
2477 else
2478 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2479
2480 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2481 smp->data.type = SMP_T_SINT;
2482 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2483 return 1;
2484}
2485
2486/* Returns the start-line if the selected HTX block exists and is a
2487 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2488 * supported or "head", "tail" or "first". The channel is chosen depending on
2489 * the sample direction. */
2490static int
2491smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2492{
2493 struct buffer *temp;
2494 struct channel *chn;
2495 struct htx *htx;
2496 struct htx_blk *blk;
2497 struct htx_sl *sl;
2498 int32_t pos;
2499
2500 if (!smp->strm || !arg_p)
2501 return 0;
2502
2503 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002504 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002505 if (!htx)
2506 return 0;
2507
2508 pos = arg_p[0].data.sint;
2509 if (pos == -1)
2510 blk = htx_get_head_blk(htx);
2511 else if (pos == -2)
2512 blk = htx_get_tail_blk(htx);
2513 else if (pos == -3)
2514 blk = htx_get_first_blk(htx);
2515 else
2516 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2517
2518 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2519 smp->data.u.str.size = 0;
2520 smp->data.u.str.area = "";
2521 smp->data.u.str.data = 0;
2522 }
2523 else {
2524 sl = htx_get_blk_ptr(htx, blk);
2525
2526 temp = get_trash_chunk();
2527 chunk_istcat(temp, htx_sl_p1(sl));
2528 temp->area[temp->data++] = ' ';
2529 chunk_istcat(temp, htx_sl_p2(sl));
2530 temp->area[temp->data++] = ' ';
2531 chunk_istcat(temp, htx_sl_p3(sl));
2532
2533 smp->data.u.str = *temp;
2534 }
2535
2536 smp->data.type = SMP_T_STR;
2537 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2538 return 1;
2539}
2540
2541/* Returns the header name if the selected HTX block exists and is a header or a
2542 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2543 * supported or "head", "tail" or "first". The channel is chosen depending on
2544 * the sample direction. */
2545static int
2546smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2547{
2548 struct channel *chn;
2549 struct htx *htx;
2550 struct htx_blk *blk;
2551 int32_t pos;
2552
2553 if (!smp->strm || !arg_p)
2554 return 0;
2555
2556 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002557 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002558 if (!htx)
2559 return 0;
2560
2561 pos = arg_p[0].data.sint;
2562 if (pos == -1)
2563 blk = htx_get_head_blk(htx);
2564 else if (pos == -2)
2565 blk = htx_get_tail_blk(htx);
2566 else if (pos == -3)
2567 blk = htx_get_first_blk(htx);
2568 else
2569 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2570
2571 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2572 smp->data.u.str.size = 0;
2573 smp->data.u.str.area = "";
2574 smp->data.u.str.data = 0;
2575 }
2576 else {
2577 struct ist name = htx_get_blk_name(htx, blk);
2578
2579 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2580 }
2581 smp->data.type = SMP_T_STR;
2582 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2583 return 1;
2584}
2585
2586/* Returns the header value if the selected HTX block exists and is a header or
2587 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2588 * supported or "head", "tail" or "first". The channel is chosen depending on
2589 * the sample direction. */
2590static int
2591smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2592{
2593 struct channel *chn;
2594 struct htx *htx;
2595 struct htx_blk *blk;
2596 int32_t pos;
2597
2598 if (!smp->strm || !arg_p)
2599 return 0;
2600
2601 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002602 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002603 if (!htx)
2604 return 0;
2605
2606 pos = arg_p[0].data.sint;
2607 if (pos == -1)
2608 blk = htx_get_head_blk(htx);
2609 else if (pos == -2)
2610 blk = htx_get_tail_blk(htx);
2611 else if (pos == -3)
2612 blk = htx_get_first_blk(htx);
2613 else
2614 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2615
2616 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2617 smp->data.u.str.size = 0;
2618 smp->data.u.str.area = "";
2619 smp->data.u.str.data = 0;
2620 }
2621 else {
2622 struct ist val = htx_get_blk_value(htx, blk);
2623
2624 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2625 }
2626 smp->data.type = SMP_T_STR;
2627 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2628 return 1;
2629}
2630
2631/* Returns the value if the selected HTX block exists and is a data
2632 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2633 * or "head", "tail" or "first". The channel is chosen depending on the sample
2634 * direction. */
2635static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002636smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002637{
2638 struct channel *chn;
2639 struct htx *htx;
2640 struct htx_blk *blk;
2641 int32_t pos;
2642
2643 if (!smp->strm || !arg_p)
2644 return 0;
2645
2646 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002647 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002648 if (!htx)
2649 return 0;
2650
2651 pos = arg_p[0].data.sint;
2652 if (pos == -1)
2653 blk = htx_get_head_blk(htx);
2654 else if (pos == -2)
2655 blk = htx_get_tail_blk(htx);
2656 else if (pos == -3)
2657 blk = htx_get_first_blk(htx);
2658 else
2659 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2660
2661 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2662 smp->data.u.str.size = 0;
2663 smp->data.u.str.area = "";
2664 smp->data.u.str.data = 0;
2665 }
2666 else {
2667 struct ist val = htx_get_blk_value(htx, blk);
2668
2669 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2670 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002671 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002672 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2673 return 1;
2674}
2675
2676/* This function is used to validate the arguments passed to any "htx_blk" fetch
2677 * keywords. An argument is expected by these keywords. It must be a positive
2678 * integer or on of the following strings: "head", "tail" or "first". It returns
2679 * 0 on error, and a non-zero value if OK.
2680 */
2681int val_blk_arg(struct arg *arg, char **err_msg)
2682{
2683 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2684 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2685 return 0;
2686 }
2687 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002688 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002689 arg[0].type = ARGT_SINT;
2690 arg[0].data.sint = -1;
2691 }
2692 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002693 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002694 arg[0].type = ARGT_SINT;
2695 arg[0].data.sint = -2;
2696 }
2697 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002698 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002699 arg[0].type = ARGT_SINT;
2700 arg[0].data.sint = -3;
2701 }
2702 else {
2703 int pos;
2704
2705 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002706 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002707 memprintf(err_msg, "invalid block position");
2708 return 0;
2709 }
2710 }
2711
2712 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2713 if (pos < 0) {
2714 memprintf(err_msg, "block position must not be negative");
2715 return 0;
2716 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002717 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002718 arg[0].type = ARGT_SINT;
2719 arg[0].data.sint = pos;
2720 }
2721
2722 return 1;
2723}
2724
2725
2726/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002727 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002728 */
2729static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet2e961942021-03-25 17:29:38 +01002730 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Christopher Faulet29f72842019-12-11 15:52:32 +01002731
Christopher Faulet01f44452020-01-08 14:23:40 +01002732 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2733 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2734 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2735 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2736 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2737 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2738 { "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 +01002739
Christopher Faulet01f44452020-01-08 14:23:40 +01002740 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2741 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2742 { "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},
2743 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2744 { "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 +01002745 { "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 +01002746
2747 { /* END */ },
2748}};
2749
2750INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);