blob: 8334e1911c14cef00702d52b7f4a8c8de43e52f2 [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 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020063struct htx_sl *http_get_stline(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 Faulet04a94a72021-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 Faulet573fe732018-11-28 16:55:12 +0100160 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
161 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 Zdebf77465c2020-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 Faulet3a107102020-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;
905 buf->area = malloc(raw.len);
906 return 1;
907 }
908
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100909 buf->size = global.tune.bufsize;
910 buf->area = (char *)malloc(buf->size);
911 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 Faulet3a107102020-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 Faulet3a107102020-11-05 22:43:41 +0100921 }
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100922
Christopher Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Tarreau37b96302020-11-06 14:24:02 +0100971 (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len);
Christopher Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-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 Faulet3a107102020-11-05 22:43:41 +0100992 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
993 memprintf(errmsg, "unable to add EOM into the HTX message");
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100994 goto error;
Christopher Faulet3a107102020-11-05 22:43:41 +0100995 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200996
Christopher Faulet90cc4812019-07-22 16:49:30 +0200997 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100998
999error:
1000 if (buf->size)
1001 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +02001002 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001003}
1004
Christopher Faulet18630642020-05-12 18:57:28 +02001005void release_http_reply(struct http_reply *http_reply)
1006{
1007 struct logformat_node *lf, *lfb;
1008 struct http_reply_hdr *hdr, *hdrb;
1009
1010 if (!http_reply)
1011 return;
1012
1013 free(http_reply->ctype);
1014 http_reply->ctype = NULL;
1015 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
1016 LIST_DEL(&hdr->list);
1017 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1018 LIST_DEL(&lf->list);
1019 release_sample_expr(lf->expr);
1020 free(lf->arg);
1021 free(lf);
1022 }
1023 istfree(&hdr->name);
1024 free(hdr);
1025 }
1026
1027 if (http_reply->type == HTTP_REPLY_ERRFILES) {
1028 free(http_reply->body.http_errors);
1029 http_reply->body.http_errors = NULL;
1030 }
1031 else if (http_reply->type == HTTP_REPLY_RAW)
1032 chunk_destroy(&http_reply->body.obj);
1033 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
1034 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
1035 LIST_DEL(&lf->list);
1036 release_sample_expr(lf->expr);
1037 free(lf->arg);
1038 free(lf);
1039 }
1040 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001041 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001042}
1043
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001044static int http_htx_init(void)
1045{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001046 struct buffer chk;
1047 struct ist raw;
Christopher Faulet3a107102020-11-05 22:43:41 +01001048 char *errmsg = NULL;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001049 int rc;
1050 int err_code = 0;
1051
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1053 if (!http_err_msgs[rc]) {
Christopher Faulet3a107102020-11-05 22:43:41 +01001054 ha_alert("Internal error: no default message defined for HTTP return code %d", rc);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001055 err_code |= ERR_ALERT | ERR_FATAL;
1056 continue;
1057 }
1058
1059 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
Christopher Faulet3a107102020-11-05 22:43:41 +01001060 if (!http_str_to_htx(&chk, raw, &errmsg)) {
1061 ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n",
1062 http_err_codes[rc], errmsg);
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001063 err_code |= ERR_ALERT | ERR_FATAL;
1064 }
Christopher Faulet3a107102020-11-05 22:43:41 +01001065 else if (errmsg) {
1066 ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg);
1067 err_code |= ERR_WARN;
1068 }
1069
1070 /* Reset errmsg */
1071 free(errmsg);
1072 errmsg = NULL;
1073
Christopher Fauletf7346382019-07-17 22:02:08 +02001074 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001075 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1076 http_err_replies[rc].status = http_err_codes[rc];
1077 http_err_replies[rc].ctype = NULL;
1078 LIST_INIT(&http_err_replies[rc].hdrs);
1079 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001080 }
1081end:
1082 return err_code;
1083}
1084
Christopher Faulet58857752020-01-15 15:19:50 +01001085static void http_htx_deinit(void)
1086{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001087 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001088 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001089 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001090 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001091 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001092
1093 node = ebpt_first(&http_error_messages);
1094 while (node) {
1095 next = ebpt_next(node);
1096 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001097 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1098 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001099 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001100 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001101 node = next;
1102 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001103
1104 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1105 free(http_errs->conf.file);
1106 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001107 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1108 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001109 LIST_DEL(&http_errs->list);
1110 free(http_errs);
1111 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001112
1113 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
1114 LIST_DEL(&http_rep->list);
1115 release_http_reply(http_rep);
1116 }
Christopher Faulet58857752020-01-15 15:19:50 +01001117}
1118
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001119REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001120REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001121
Christopher Faulet58857752020-01-15 15:19:50 +01001122/* Reads content of the error file <file> and convert it into an HTX message. On
1123 * success, the HTX message is returned. On error, NULL is returned and an error
1124 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001125 */
Christopher Faulet58857752020-01-15 15:19:50 +01001126struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001127{
Christopher Faulet58857752020-01-15 15:19:50 +01001128 struct buffer *buf = NULL;
1129 struct buffer chk;
1130 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001131 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001132 struct stat stat;
1133 char *err = NULL;
1134 int errnum, errlen;
1135 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001136
1137 /* already loaded */
1138 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1139 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001140 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1141 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001142 goto out;
1143 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001144
Christopher Faulet58857752020-01-15 15:19:50 +01001145 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001146 fd = open(file, O_RDONLY);
1147 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1148 memprintf(errmsg, "error opening file '%s'.", file);
1149 goto out;
1150 }
1151
1152 if (stat.st_size <= global.tune.bufsize)
1153 errlen = stat.st_size;
1154 else {
1155 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1156 file, global.tune.bufsize);
1157 errlen = global.tune.bufsize;
1158 }
1159
1160 err = malloc(errlen);
1161 if (!err) {
1162 memprintf(errmsg, "out of memory.");
1163 goto out;
1164 }
1165
1166 errnum = read(fd, err, errlen);
1167 if (errnum != errlen) {
1168 memprintf(errmsg, "error reading file '%s'.", file);
1169 goto out;
1170 }
1171
Christopher Faulet58857752020-01-15 15:19:50 +01001172 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001173 http_errmsg = calloc(1, sizeof(*http_errmsg));
1174 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001175 memprintf(errmsg, "out of memory.");
1176 goto out;
1177 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001178 http_errmsg->node.key = strdup(file);
1179 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001180 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001181 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001182 goto out;
1183 }
1184
1185 /* Convert the error file into an HTX message */
Christopher Faulet3a107102020-11-05 22:43:41 +01001186 if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) {
1187 memprintf(errmsg, "'%s': %s", file, *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001188 free(http_errmsg->node.key);
1189 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001190 goto out;
1191 }
1192
Christopher Faulet58857752020-01-15 15:19:50 +01001193 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001194 http_errmsg->msg = chk;
1195 ebis_insert(&http_error_messages, &http_errmsg->node);
1196 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001197
Christopher Faulet5031ef52020-01-15 11:22:07 +01001198 out:
1199 if (fd >= 0)
1200 close(fd);
1201 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001202 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001203}
1204
Ilya Shipitsind4259502020-04-08 01:07:56 +05001205/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001206 * message is returned. On error, NULL is returned and an error message is
1207 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001208 */
Christopher Faulet58857752020-01-15 15:19:50 +01001209struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001210{
Christopher Faulet58857752020-01-15 15:19:50 +01001211 struct buffer *buf = NULL;
1212 struct buffer chk;
1213 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001214 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001215
1216 /* already loaded */
1217 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1218 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001219 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1220 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001221 goto out;
1222 }
1223 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001224 http_errmsg = calloc(1, sizeof(*http_errmsg));
1225 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001226 memprintf(errmsg, "out of memory.");
1227 goto out;
1228 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001229 http_errmsg->node.key = strdup(key);
1230 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001231 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001232 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001233 goto out;
1234 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001235
1236 /* Convert the error file into an HTX message */
Christopher Faulet3a107102020-11-05 22:43:41 +01001237 if (!http_str_to_htx(&chk, msg, errmsg)) {
1238 memprintf(errmsg, "invalid error message: %s", *errmsg);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001239 free(http_errmsg->node.key);
1240 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001241 goto out;
1242 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001243
Christopher Faulet58857752020-01-15 15:19:50 +01001244 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001245 http_errmsg->msg = chk;
1246 ebis_insert(&http_error_messages, &http_errmsg->node);
1247 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001248 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001249 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001250}
1251
Christopher Faulet5031ef52020-01-15 11:22:07 +01001252/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001253 * <status>. It returns NULL if there is any error, otherwise it return the
1254 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001255 */
Christopher Faulet58857752020-01-15 15:19:50 +01001256struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001257{
Christopher Faulet58857752020-01-15 15:19:50 +01001258 struct buffer *buf = NULL;
1259 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001260
1261 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1262 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001263 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001264 break;
1265 }
1266 }
1267
1268 if (rc >= HTTP_ERR_SIZE)
1269 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001270 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001271}
1272
1273/* This function creates HTX error message corresponding to a redirect message
1274 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001275 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1276 * returns NULL if there is any error, otherwise it return the corresponding HTX
1277 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001278 */
Christopher Faulet58857752020-01-15 15:19:50 +01001279struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001280{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001281 static const char *HTTP_302 =
1282 "HTTP/1.1 302 Found\r\n"
1283 "Cache-Control: no-cache\r\n"
1284 "Content-length: 0\r\n"
1285 "Location: "; /* not terminated since it will be concatenated with the URL */
1286 static const char *HTTP_303 =
1287 "HTTP/1.1 303 See Other\r\n"
1288 "Cache-Control: no-cache\r\n"
1289 "Content-length: 0\r\n"
1290 "Location: "; /* not terminated since it will be concatenated with the URL */
1291
Christopher Faulet58857752020-01-15 15:19:50 +01001292 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001293 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001294 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001295 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001296
1297 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1298 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001299 /* Create the error key */
1300 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1301 memprintf(errmsg, "out of memory.");
1302 goto out;
1303 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001304 /* Create the error message */
1305 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1306 errlen = strlen(msg) + strlen(url) + 5;
1307 err = malloc(errlen);
1308 if (!err) {
1309 memprintf(errmsg, "out of memory.");
1310 goto out;
1311 }
1312 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1313
1314 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001315 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001316 break;
1317 }
1318 }
1319
1320 if (rc >= HTTP_ERR_SIZE)
1321 memprintf(errmsg, "status code '%d' not handled.", status);
1322out:
Christopher Faulet58857752020-01-15 15:19:50 +01001323 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001324 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001325 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001326}
1327
Christopher Faulet7eea2412020-05-13 15:02:59 +02001328/* Check an "http reply" and, for replies referencing an http-errors section,
1329 * try to find the right section and the right error message in this section. If
1330 * found, the reply is updated. If the http-errors section exists but the error
1331 * message is not found, no error message is set to fallback on the default
1332 * ones. Otherwise (unknown section) an error is returned.
1333 *
1334 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1335 * filled.
1336 */
1337int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1338{
1339 struct http_errors *http_errs;
1340 int ret = 1;
1341
1342 if (reply->type != HTTP_REPLY_ERRFILES)
1343 goto end;
1344
1345 list_for_each_entry(http_errs, &http_errors_list, list) {
1346 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001347 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001348 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001349 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1350 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001351 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1352 "not declared in http-errors section '%s'.\n",
1353 px->id, reply->status, http_errs->id);
1354 break;
1355 }
1356 }
1357
1358 if (&http_errs->list == &http_errors_list) {
1359 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1360 reply->body.http_errors);
1361 ret = 0;
1362 }
1363
1364 end:
1365 return ret;
1366}
1367
Christopher Faulet47e791e2020-05-13 14:36:55 +02001368/* Parse an "http reply". It returns the reply on success or NULL on error. This
1369 * function creates one of the following http replies :
1370 *
1371 * - HTTP_REPLY_EMPTY : dummy response, no payload
1372 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1373 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1374 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1375 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1376 *
1377 * The content-type must be defined for non-empty payload. It is ignored for
1378 * error messages (implicit or explicit). When an http-errors section is
1379 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1380 * during the configuration validity check or dynamically. It is the caller
1381 * responsibility to choose. If no status code is configured, <default_status>
1382 * is set.
1383 */
1384struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1385 int default_status, char **errmsg)
1386{
1387 struct logformat_node *lf, *lfb;
1388 struct http_reply *reply = NULL;
1389 struct http_reply_hdr *hdr, *hdrb;
1390 struct stat stat;
1391 const char *act_arg = NULL;
1392 char *obj = NULL;
1393 int cur_arg, cap, objlen = 0, fd = -1;
1394
1395
1396 reply = calloc(1, sizeof(*reply));
1397 if (!reply) {
1398 memprintf(errmsg, "out of memory");
1399 goto error;
1400 }
1401 LIST_INIT(&reply->hdrs);
1402 reply->type = HTTP_REPLY_EMPTY;
1403 reply->status = default_status;
1404
Christopher Faulet3b967c12020-05-15 15:47:44 +02001405 if (px->conf.args.ctx == ARGC_HERR)
1406 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1407 else
1408 cap = ((px->conf.args.ctx == ARGC_HRQ)
1409 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1410 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001411
1412 cur_arg = *orig_arg;
1413 while (*args[cur_arg]) {
1414 if (strcmp(args[cur_arg], "status") == 0) {
1415 cur_arg++;
1416 if (!*args[cur_arg]) {
1417 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1418 goto error;
1419 }
1420 reply->status = atol(args[cur_arg]);
1421 if (reply->status < 200 || reply->status > 599) {
1422 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1423 goto error;
1424 }
1425 cur_arg++;
1426 }
1427 else if (strcmp(args[cur_arg], "content-type") == 0) {
1428 cur_arg++;
1429 if (!*args[cur_arg]) {
1430 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1431 goto error;
1432 }
1433 free(reply->ctype);
1434 reply->ctype = strdup(args[cur_arg]);
1435 cur_arg++;
1436 }
1437 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1438 if (reply->type != HTTP_REPLY_EMPTY) {
1439 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1440 goto error;
1441 }
1442 act_arg = args[cur_arg];
1443 cur_arg++;
1444 if (!*args[cur_arg]) {
1445 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1446 goto error;
1447 }
1448 reply->body.http_errors = strdup(args[cur_arg]);
1449 if (!reply->body.http_errors) {
1450 memprintf(errmsg, "out of memory");
1451 goto error;
1452 }
1453 reply->type = HTTP_REPLY_ERRFILES;
1454 cur_arg++;
1455 }
1456 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1457 if (reply->type != HTTP_REPLY_EMPTY) {
1458 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1459 goto error;
1460 }
1461 act_arg = args[cur_arg];
1462 reply->type = HTTP_REPLY_ERRMSG;
1463 cur_arg++;
1464 }
1465 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1466 if (reply->type != HTTP_REPLY_EMPTY) {
1467 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1468 goto error;
1469 }
1470 act_arg = args[cur_arg];
1471 cur_arg++;
1472 if (!*args[cur_arg]) {
1473 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1474 goto error;
1475 }
1476 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1477 if (!reply->body.errmsg) {
1478 goto error;
1479 }
1480 reply->type = HTTP_REPLY_ERRMSG;
1481 cur_arg++;
1482 }
1483 else if (strcmp(args[cur_arg], "file") == 0) {
1484 if (reply->type != HTTP_REPLY_EMPTY) {
1485 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1486 goto error;
1487 }
1488 act_arg = args[cur_arg];
1489 cur_arg++;
1490 if (!*args[cur_arg]) {
1491 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1492 goto error;
1493 }
1494 fd = open(args[cur_arg], O_RDONLY);
1495 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1496 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1497 goto error;
1498 }
1499 if (stat.st_size > global.tune.bufsize) {
1500 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1501 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1502 goto error;
1503 }
1504 objlen = stat.st_size;
1505 obj = malloc(objlen);
1506 if (!obj || read(fd, obj, objlen) != objlen) {
1507 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1508 goto error;
1509 }
1510 close(fd);
1511 fd = -1;
1512 reply->type = HTTP_REPLY_RAW;
1513 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1514 obj = NULL;
1515 cur_arg++;
1516 }
1517 else if (strcmp(args[cur_arg], "string") == 0) {
1518 if (reply->type != HTTP_REPLY_EMPTY) {
1519 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1520 goto error;
1521 }
1522 act_arg = args[cur_arg];
1523 cur_arg++;
1524 if (!*args[cur_arg]) {
1525 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1526 goto error;
1527 }
1528 obj = strdup(args[cur_arg]);
1529 objlen = strlen(args[cur_arg]);
1530 if (!obj) {
1531 memprintf(errmsg, "out of memory");
1532 goto error;
1533 }
1534 reply->type = HTTP_REPLY_RAW;
1535 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1536 obj = NULL;
1537 cur_arg++;
1538 }
1539 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1540 if (reply->type != HTTP_REPLY_EMPTY) {
1541 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1542 goto error;
1543 }
1544 act_arg = args[cur_arg];
1545 cur_arg++;
1546 if (!*args[cur_arg]) {
1547 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1548 goto error;
1549 }
1550 fd = open(args[cur_arg], O_RDONLY);
1551 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1552 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1553 goto error;
1554 }
1555 if (stat.st_size > global.tune.bufsize) {
1556 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1557 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1558 goto error;
1559 }
1560 objlen = stat.st_size;
1561 obj = malloc(objlen + 1);
1562 if (!obj || read(fd, obj, objlen) != objlen) {
1563 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1564 goto error;
1565 }
1566 close(fd);
1567 fd = -1;
1568 obj[objlen] = '\0';
1569 reply->type = HTTP_REPLY_LOGFMT;
1570 cur_arg++;
1571 }
1572 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1573 if (reply->type != HTTP_REPLY_EMPTY) {
1574 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1575 goto error;
1576 }
1577 act_arg = args[cur_arg];
1578 cur_arg++;
1579 if (!*args[cur_arg]) {
1580 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1581 goto error;
1582 }
1583 obj = strdup(args[cur_arg]);
1584 objlen = strlen(args[cur_arg]);
1585 reply->type = HTTP_REPLY_LOGFMT;
1586 cur_arg++;
1587 }
1588 else if (strcmp(args[cur_arg], "hdr") == 0) {
1589 cur_arg++;
1590 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1591 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1592 goto error;
1593 }
1594 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1595 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1596 strcasecmp(args[cur_arg], "content-type") == 0) {
1597 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1598 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1599 cur_arg += 2;
1600 continue;
1601 }
1602 hdr = calloc(1, sizeof(*hdr));
1603 if (!hdr) {
1604 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1605 goto error;
1606 }
Christopher Fauletd6e31232020-05-21 10:10:41 +02001607 LIST_ADDQ(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001608 LIST_INIT(&hdr->value);
1609 hdr->name = ist(strdup(args[cur_arg]));
1610 if (!isttest(hdr->name)) {
1611 memprintf(errmsg, "out of memory");
1612 goto error;
1613 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001614 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1615 goto error;
1616
1617 free(px->conf.lfs_file);
1618 px->conf.lfs_file = strdup(px->conf.args.file);
1619 px->conf.lfs_line = px->conf.args.line;
1620 cur_arg += 2;
1621 }
1622 else
1623 break;
1624 }
1625
1626 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1627 if (reply->ctype) {
1628 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1629 " neither errorfile nor payload defined.\n",
1630 px->conf.args.file, px->conf.args.line, reply->ctype);
1631 free(reply->ctype);
1632 reply->ctype = NULL;
1633 }
1634 }
1635 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1636
1637 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1638 /* default errorfile or errorfiles: check the status */
1639 int rc;
1640
1641 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1642 if (http_err_codes[rc] == reply->status)
1643 break;
1644 }
1645
1646 if (rc >= HTTP_ERR_SIZE) {
1647 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1648 reply->status, act_arg);
1649 goto error;
1650 }
1651 }
1652
1653 if (reply->ctype) {
1654 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1655 "with an erorrfile.\n",
1656 px->conf.args.file, px->conf.args.line, reply->ctype);
1657 free(reply->ctype);
1658 reply->ctype = NULL;
1659 }
1660 if (!LIST_ISEMPTY(&reply->hdrs)) {
1661 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1662 "with an erorrfile.\n",
1663 px->conf.args.file, px->conf.args.line);
1664 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1665 LIST_DEL(&hdr->list);
1666 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1667 LIST_DEL(&lf->list);
1668 release_sample_expr(lf->expr);
1669 free(lf->arg);
1670 free(lf);
1671 }
1672 istfree(&hdr->name);
1673 free(hdr);
1674 }
1675 }
1676 }
1677 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001678 if ((reply->status == 204 || reply->status == 304) && objlen) {
1679 memprintf(errmsg, "No body expected for %d responses", reply->status);
1680 goto error;
1681 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001682 if (!reply->ctype && objlen) {
1683 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1684 goto error;
1685 }
1686 if (reply->ctype && !b_data(&reply->body.obj)) {
1687 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001688 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001689 px->conf.args.file, px->conf.args.line, reply->ctype);
1690 free(reply->ctype);
1691 reply->ctype = NULL;
1692 }
1693 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1694 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1695 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1696 px->conf.args.file, px->conf.args.line);
1697 }
1698 }
1699 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1700 LIST_INIT(&reply->body.fmt);
Christopher Fauletb8d148a2020-10-09 08:50:26 +02001701 if ((reply->status == 204 || reply->status == 304)) {
1702 memprintf(errmsg, "No body expected for %d responses", reply->status);
1703 goto error;
1704 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001705 if (!reply->ctype) {
1706 memprintf(errmsg, "a content type must be defined with a log-format payload");
1707 goto error;
1708 }
1709 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1710 goto error;
1711
1712 free(px->conf.lfs_file);
1713 px->conf.lfs_file = strdup(px->conf.args.file);
1714 px->conf.lfs_line = px->conf.args.line;
1715 }
1716
1717 free(obj);
1718 *orig_arg = cur_arg;
1719 return reply;
1720
1721 error:
1722 free(obj);
1723 if (fd >= 0)
1724 close(fd);
1725 release_http_reply(reply);
1726 return NULL;
1727}
1728
Christopher Faulet07f41f72020-01-16 16:16:06 +01001729/* Parses the "errorloc[302|303]" proxy keyword */
1730static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1731 struct proxy *defpx, const char *file, int line,
1732 char **errmsg)
1733{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001734 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001735 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001736 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001737 int errloc, status;
1738 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001739
1740 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1741 ret = 1;
1742 goto out;
1743 }
1744
1745 if (*(args[1]) == 0 || *(args[2]) == 0) {
1746 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1747 ret = -1;
1748 goto out;
1749 }
1750
1751 status = atol(args[1]);
1752 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1753 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1754 if (!msg) {
1755 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1756 ret = -1;
1757 goto out;
1758 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001759
1760 reply = calloc(1, sizeof(*reply));
1761 if (!reply) {
1762 memprintf(errmsg, "%s : out of memory.", args[0]);
1763 ret = -1;
1764 goto out;
1765 }
1766 reply->type = HTTP_REPLY_ERRMSG;
1767 reply->status = status;
1768 reply->ctype = NULL;
1769 LIST_INIT(&reply->hdrs);
1770 reply->body.errmsg = msg;
1771 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001772
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001773 conf_err = calloc(1, sizeof(*conf_err));
1774 if (!conf_err) {
1775 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001776 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001777 ret = -1;
1778 goto out;
1779 }
1780 conf_err->type = 1;
1781 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001782 conf_err->info.errorfile.reply = reply;
1783
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001784 conf_err->file = strdup(file);
1785 conf_err->line = line;
1786 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001787
Christopher Faulet3a107102020-11-05 22:43:41 +01001788 /* handle warning message */
1789 if (*errmsg)
1790 ret = 1;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001791 out:
1792 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001793
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001794}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001795
1796/* Parses the "errorfile" proxy keyword */
1797static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1798 struct proxy *defpx, const char *file, int line,
1799 char **errmsg)
1800{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001801 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001802 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001803 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001804 int status;
1805 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001806
1807 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1808 ret = 1;
1809 goto out;
1810 }
1811
1812 if (*(args[1]) == 0 || *(args[2]) == 0) {
1813 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1814 ret = -1;
1815 goto out;
1816 }
1817
1818 status = atol(args[1]);
1819 msg = http_parse_errorfile(status, args[2], errmsg);
1820 if (!msg) {
1821 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1822 ret = -1;
1823 goto out;
1824 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001825
Christopher Faulet5809e102020-05-14 17:31:52 +02001826 reply = calloc(1, sizeof(*reply));
1827 if (!reply) {
1828 memprintf(errmsg, "%s : out of memory.", args[0]);
1829 ret = -1;
1830 goto out;
1831 }
1832 reply->type = HTTP_REPLY_ERRMSG;
1833 reply->status = status;
1834 reply->ctype = NULL;
1835 LIST_INIT(&reply->hdrs);
1836 reply->body.errmsg = msg;
1837 LIST_ADDQ(&http_replies_list, &reply->list);
1838
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001839 conf_err = calloc(1, sizeof(*conf_err));
1840 if (!conf_err) {
1841 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001842 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001843 ret = -1;
1844 goto out;
1845 }
1846 conf_err->type = 1;
1847 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001848 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001849 conf_err->file = strdup(file);
1850 conf_err->line = line;
1851 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1852
Christopher Faulet3a107102020-11-05 22:43:41 +01001853 /* handle warning message */
1854 if (*errmsg)
1855 ret = 1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001856 out:
1857 return ret;
1858
1859}
1860
1861/* Parses the "errorfiles" proxy keyword */
1862static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1863 struct proxy *defpx, const char *file, int line,
1864 char **err)
1865{
1866 struct conf_errors *conf_err = NULL;
1867 char *name = NULL;
1868 int rc, ret = 0;
1869
1870 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1871 ret = 1;
1872 goto out;
1873 }
1874
1875 if (!*(args[1])) {
1876 memprintf(err, "%s : expects <name> as argument.", args[0]);
1877 ret = -1;
1878 goto out;
1879 }
1880
1881 name = strdup(args[1]);
1882 conf_err = calloc(1, sizeof(*conf_err));
1883 if (!name || !conf_err) {
1884 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001885 goto error;
1886 }
1887 conf_err->type = 0;
1888
1889 conf_err->info.errorfiles.name = name;
1890 if (!*(args[2])) {
1891 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1892 conf_err->info.errorfiles.status[rc] = 1;
1893 }
1894 else {
1895 int cur_arg, status;
1896 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1897 status = atol(args[cur_arg]);
1898
1899 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1900 if (http_err_codes[rc] == status) {
1901 conf_err->info.errorfiles.status[rc] = 2;
1902 break;
1903 }
1904 }
1905 if (rc >= HTTP_ERR_SIZE) {
1906 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001907 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001908 }
1909 }
1910 }
1911 conf_err->file = strdup(file);
1912 conf_err->line = line;
1913 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1914 out:
1915 return ret;
1916
1917 error:
1918 free(name);
1919 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001920 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001921 goto out;
1922}
1923
Christopher Faulet3b967c12020-05-15 15:47:44 +02001924/* Parses the "http-error" proxy keyword */
1925static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
1926 struct proxy *defpx, const char *file, int line,
1927 char **errmsg)
1928{
1929 struct conf_errors *conf_err;
1930 struct http_reply *reply = NULL;
1931 int rc, cur_arg, ret = 0;
1932
1933 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1934 ret = 1;
1935 goto out;
1936 }
1937
1938 cur_arg = 1;
1939 curpx->conf.args.ctx = ARGC_HERR;
1940 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1941 if (!reply) {
1942 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1943 goto error;
1944 }
1945 else if (!reply->status) {
1946 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1947 goto error;
1948 }
1949
1950 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1951 if (http_err_codes[rc] == reply->status)
1952 break;
1953 }
1954
1955 if (rc >= HTTP_ERR_SIZE) {
1956 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1957 goto error;
1958 }
1959 if (*args[cur_arg]) {
1960 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1961 goto error;
1962 }
1963
1964 conf_err = calloc(1, sizeof(*conf_err));
1965 if (!conf_err) {
1966 memprintf(errmsg, "%s : out of memory.", args[0]);
1967 goto error;
1968 }
1969 if (reply->type == HTTP_REPLY_ERRFILES) {
1970 int rc = http_get_status_idx(reply->status);
1971
1972 conf_err->type = 2;
1973 conf_err->info.errorfiles.name = reply->body.http_errors;
1974 conf_err->info.errorfiles.status[rc] = 2;
1975 reply->body.http_errors = NULL;
1976 release_http_reply(reply);
1977 }
1978 else {
1979 conf_err->type = 1;
1980 conf_err->info.errorfile.status = reply->status;
1981 conf_err->info.errorfile.reply = reply;
1982 LIST_ADDQ(&http_replies_list, &reply->list);
1983 }
1984 conf_err->file = strdup(file);
1985 conf_err->line = line;
1986 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1987
Christopher Faulete90e1612020-11-13 10:58:01 +01001988 /* handle warning message */
1989 if (*errmsg)
1990 ret = 1;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001991 out:
1992 return ret;
1993
1994 error:
1995 release_http_reply(reply);
1996 ret = -1;
1997 goto out;
1998
1999}
2000
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002001/* Check "errorfiles" proxy keyword */
2002static int proxy_check_errors(struct proxy *px)
2003{
2004 struct conf_errors *conf_err, *conf_err_back;
2005 struct http_errors *http_errs;
2006 int rc, err = 0;
2007
2008 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2009 if (conf_err->type == 1) {
2010 /* errorfile */
2011 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02002012 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02002013
2014 /* For proxy, to rely on default replies, just don't reference a reply */
2015 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
2016 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002017 }
2018 else {
2019 /* errorfiles */
2020 list_for_each_entry(http_errs, &http_errors_list, list) {
2021 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
2022 break;
2023 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002024
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002025 /* unknown http-errors section */
2026 if (&http_errs->list == &http_errors_list) {
2027 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
2028 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
2029 err |= ERR_ALERT | ERR_FATAL;
2030 free(conf_err->info.errorfiles.name);
2031 goto next;
2032 }
2033
2034 free(conf_err->info.errorfiles.name);
2035 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2036 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02002037 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02002038 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002039 else if (conf_err->info.errorfiles.status[rc] == 2)
2040 ha_warning("config: proxy '%s' : status '%d' not declared in"
2041 " http-errors section '%s' (at %s:%d).\n",
2042 px->id, http_err_codes[rc], http_errs->id,
2043 conf_err->file, conf_err->line);
2044 }
2045 }
2046 }
2047 next:
2048 LIST_DEL(&conf_err->list);
2049 free(conf_err->file);
2050 free(conf_err);
2051 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01002052
2053 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002054 return err;
2055}
2056
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002057static int post_check_errors()
2058{
2059 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002060 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002061 struct htx *htx;
2062 int err_code = 0;
2063
2064 node = ebpt_first(&http_error_messages);
2065 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002066 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2067 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002068 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002069 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002070 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2071 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002072 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002073 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002074 (char *)node->key);
2075 err_code |= ERR_WARN;
2076 }
2077 next:
2078 node = ebpt_next(node);
2079 }
2080
2081 return err_code;
2082}
2083
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002084int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
2085{
2086 struct conf_errors *conf_err, *new_conf_err = NULL;
2087 int ret = 0;
2088
2089 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2090 new_conf_err = calloc(1, sizeof(*new_conf_err));
2091 if (!new_conf_err) {
2092 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2093 goto out;
2094 }
2095 new_conf_err->type = conf_err->type;
2096 if (conf_err->type == 1) {
2097 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002098 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002099 }
2100 else {
2101 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2102 if (!new_conf_err->info.errorfiles.name) {
2103 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2104 goto out;
2105 }
2106 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2107 sizeof(conf_err->info.errorfiles.status));
2108 }
2109 new_conf_err->file = strdup(conf_err->file);
2110 new_conf_err->line = conf_err->line;
2111 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
2112 new_conf_err = NULL;
2113 }
2114 ret = 1;
2115
2116 out:
2117 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002118 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002119}
2120
2121void proxy_release_conf_errors(struct proxy *px)
2122{
2123 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002124
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002125 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2126 if (conf_err->type == 0)
2127 free(conf_err->info.errorfiles.name);
2128 LIST_DEL(&conf_err->list);
2129 free(conf_err->file);
2130 free(conf_err);
2131 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002132}
2133
2134/*
2135 * Parse an <http-errors> section.
2136 * Returns the error code, 0 if OK, or any combination of :
2137 * - ERR_ABORT: must abort ASAP
2138 * - ERR_FATAL: we can continue parsing but not start the service
2139 * - ERR_WARN: a warning has been emitted
2140 * - ERR_ALERT: an alert has been emitted
2141 * Only the two first ones can stop processing, the two others are just
2142 * indicators.
2143 */
2144static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2145{
2146 static struct http_errors *curr_errs = NULL;
2147 int err_code = 0;
2148 const char *err;
2149 char *errmsg = NULL;
2150
2151 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2152 if (!*args[1]) {
2153 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2154 err_code |= ERR_ALERT | ERR_ABORT;
2155 goto out;
2156 }
2157
2158 err = invalid_char(args[1]);
2159 if (err) {
2160 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2161 file, linenum, *err, args[0], args[1]);
2162 err_code |= ERR_ALERT | ERR_FATAL;
2163 }
2164
2165 list_for_each_entry(curr_errs, &http_errors_list, list) {
2166 /* Error if two errors section owns the same name */
2167 if (strcmp(curr_errs->id, args[1]) == 0) {
2168 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2169 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2170 err_code |= ERR_ALERT | ERR_FATAL;
2171 }
2172 }
2173
2174 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2175 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2176 err_code |= ERR_ALERT | ERR_ABORT;
2177 goto out;
2178 }
2179
2180 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2181 curr_errs->id = strdup(args[1]);
2182 curr_errs->conf.file = strdup(file);
2183 curr_errs->conf.line = linenum;
2184 }
2185 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002186 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002187 struct buffer *msg;
2188 int status, rc;
2189
2190 if (*(args[1]) == 0 || *(args[2]) == 0) {
2191 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2192 file, linenum, args[0]);
2193 err_code |= ERR_ALERT | ERR_FATAL;
2194 goto out;
2195 }
2196
2197 status = atol(args[1]);
2198 msg = http_parse_errorfile(status, args[2], &errmsg);
2199 if (!msg) {
2200 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2201 err_code |= ERR_ALERT | ERR_FATAL;
2202 goto out;
2203 }
Christopher Faulete90e1612020-11-13 10:58:01 +01002204 if (errmsg) {
2205 ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg);
2206 err_code |= ERR_WARN;
2207 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002208
2209 reply = calloc(1, sizeof(*reply));
2210 if (!reply) {
2211 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2212 err_code |= ERR_ALERT | ERR_FATAL;
2213 goto out;
2214 }
2215 reply->type = HTTP_REPLY_ERRMSG;
2216 reply->status = status;
2217 reply->ctype = NULL;
2218 LIST_INIT(&reply->hdrs);
2219 reply->body.errmsg = msg;
2220
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002221 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002222 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002223 }
2224 else if (*args[0] != 0) {
2225 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2226 err_code |= ERR_ALERT | ERR_FATAL;
2227 goto out;
2228 }
2229
2230out:
2231 free(errmsg);
2232 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002233}
2234
2235static struct cfg_kw_list cfg_kws = {ILH, {
2236 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2237 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2238 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2239 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002240 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002241 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002242 { 0, NULL, NULL },
2243}};
2244
2245INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002246REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002247REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002248
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002249REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2250
Christopher Faulet29f72842019-12-11 15:52:32 +01002251/************************************************************************/
2252/* HTX sample fetches */
2253/************************************************************************/
2254
2255/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2256static int
2257smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2258{
2259 if (!smp->strm)
2260 return 0;
2261
2262 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2263 smp->data.type = SMP_T_BOOL;
2264 return 1;
2265}
2266
2267/* Returns the number of blocks in an HTX message. The channel is chosen
2268 * depending on the sample direction. */
2269static int
2270smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2271{
2272 struct channel *chn;
2273 struct htx *htx;
2274
2275 if (!smp->strm)
2276 return 0;
2277
2278 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002279 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002280 if (!htx)
2281 return 0;
2282
2283 smp->data.u.sint = htx_nbblks(htx);
2284 smp->data.type = SMP_T_SINT;
2285 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2286 return 1;
2287}
2288
2289/* Returns the size of an HTX message. The channel is chosen depending on the
2290 * sample direction. */
2291static int
2292smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2293{
2294 struct channel *chn;
2295 struct htx *htx;
2296
2297 if (!smp->strm)
2298 return 0;
2299
2300 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002301 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002302 if (!htx)
2303 return 0;
2304
2305 smp->data.u.sint = htx->size;
2306 smp->data.type = SMP_T_SINT;
2307 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2308 return 1;
2309}
2310
2311/* Returns the data size of an HTX message. The channel is chosen depending on the
2312 * sample direction. */
2313static int
2314smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2315{
2316 struct channel *chn;
2317 struct htx *htx;
2318
2319 if (!smp->strm)
2320 return 0;
2321
2322 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002323 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002324 if (!htx)
2325 return 0;
2326
2327 smp->data.u.sint = htx->data;
2328 smp->data.type = SMP_T_SINT;
2329 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2330 return 1;
2331}
2332
2333/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2334 * depending on the sample direction. */
2335static int
2336smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2337{
2338 struct channel *chn;
2339 struct htx *htx;
2340
2341 if (!smp->strm)
2342 return 0;
2343
2344 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002345 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002346 if (!htx)
2347 return 0;
2348
2349 smp->data.u.sint = htx_used_space(htx);
2350 smp->data.type = SMP_T_SINT;
2351 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2352 return 1;
2353}
2354
2355/* Returns the free space (size-used) of an HTX message. The channel is chosen
2356 * depending on the sample direction. */
2357static int
2358smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2359{
2360 struct channel *chn;
2361 struct htx *htx;
2362
2363 if (!smp->strm)
2364 return 0;
2365
2366 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002367 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002368 if (!htx)
2369 return 0;
2370
2371 smp->data.u.sint = htx_free_space(htx);
2372 smp->data.type = SMP_T_SINT;
2373 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2374 return 1;
2375}
2376
2377/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2378 * channel is chosen depending on the sample direction. */
2379static int
2380smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2381{
2382 struct channel *chn;
2383 struct htx *htx;
2384
2385 if (!smp->strm)
2386 return 0;
2387
2388 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002389 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002390 if (!htx)
2391 return 0;
2392
2393 smp->data.u.sint = htx_free_data_space(htx);
2394 smp->data.type = SMP_T_SINT;
2395 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2396 return 1;
2397}
2398
2399/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2400 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2401 * the sample direction. */
2402static int
2403smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2404{
2405 struct channel *chn;
2406 struct htx *htx;
2407
2408 if (!smp->strm)
2409 return 0;
2410
2411 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002412 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002413 if (!htx)
2414 return 0;
2415
2416 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2417 smp->data.type = SMP_T_BOOL;
2418 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2419 return 1;
2420}
2421
2422/* Returns the type of a specific HTX block, if found in the message. Otherwise
2423 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2424 * "head", "tail" or "first". The channel is chosen depending on the sample
2425 * direction. */
2426static int
2427smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2428{
2429 struct channel *chn;
2430 struct htx *htx;
2431 enum htx_blk_type type;
2432 int32_t pos;
2433
2434 if (!smp->strm || !arg_p)
2435 return 0;
2436
2437 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002438 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002439 if (!htx)
2440 return 0;
2441
2442 pos = arg_p[0].data.sint;
2443 if (pos == -1)
2444 type = htx_get_head_type(htx);
2445 else if (pos == -2)
2446 type = htx_get_tail_type(htx);
2447 else if (pos == -3)
2448 type = htx_get_first_type(htx);
2449 else
2450 type = ((pos >= htx->head && pos <= htx->tail)
2451 ? htx_get_blk_type(htx_get_blk(htx, pos))
2452 : HTX_BLK_UNUSED);
2453
2454 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2455 smp->data.type = SMP_T_STR;
2456 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2457 return 1;
2458}
2459
2460/* Returns the size of a specific HTX block, if found in the message. Otherwise
2461 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2462 * "first". The channel is chosen depending on the sample direction. */
2463static int
2464smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2465{
2466 struct channel *chn;
2467 struct htx *htx;
2468 struct htx_blk *blk;
2469 int32_t pos;
2470
2471 if (!smp->strm || !arg_p)
2472 return 0;
2473
2474 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002475 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002476 if (!htx)
2477 return 0;
2478
2479 pos = arg_p[0].data.sint;
2480 if (pos == -1)
2481 blk = htx_get_head_blk(htx);
2482 else if (pos == -2)
2483 blk = htx_get_tail_blk(htx);
2484 else if (pos == -3)
2485 blk = htx_get_first_blk(htx);
2486 else
2487 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2488
2489 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2490 smp->data.type = SMP_T_SINT;
2491 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2492 return 1;
2493}
2494
2495/* Returns the start-line if the selected HTX block exists and is a
2496 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2497 * supported or "head", "tail" or "first". The channel is chosen depending on
2498 * the sample direction. */
2499static int
2500smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2501{
2502 struct buffer *temp;
2503 struct channel *chn;
2504 struct htx *htx;
2505 struct htx_blk *blk;
2506 struct htx_sl *sl;
2507 int32_t pos;
2508
2509 if (!smp->strm || !arg_p)
2510 return 0;
2511
2512 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002513 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002514 if (!htx)
2515 return 0;
2516
2517 pos = arg_p[0].data.sint;
2518 if (pos == -1)
2519 blk = htx_get_head_blk(htx);
2520 else if (pos == -2)
2521 blk = htx_get_tail_blk(htx);
2522 else if (pos == -3)
2523 blk = htx_get_first_blk(htx);
2524 else
2525 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2526
2527 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2528 smp->data.u.str.size = 0;
2529 smp->data.u.str.area = "";
2530 smp->data.u.str.data = 0;
2531 }
2532 else {
2533 sl = htx_get_blk_ptr(htx, blk);
2534
2535 temp = get_trash_chunk();
2536 chunk_istcat(temp, htx_sl_p1(sl));
2537 temp->area[temp->data++] = ' ';
2538 chunk_istcat(temp, htx_sl_p2(sl));
2539 temp->area[temp->data++] = ' ';
2540 chunk_istcat(temp, htx_sl_p3(sl));
2541
2542 smp->data.u.str = *temp;
2543 }
2544
2545 smp->data.type = SMP_T_STR;
2546 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2547 return 1;
2548}
2549
2550/* Returns the header name if the selected HTX block exists and is a header or a
2551 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2552 * supported or "head", "tail" or "first". The channel is chosen depending on
2553 * the sample direction. */
2554static int
2555smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2556{
2557 struct channel *chn;
2558 struct htx *htx;
2559 struct htx_blk *blk;
2560 int32_t pos;
2561
2562 if (!smp->strm || !arg_p)
2563 return 0;
2564
2565 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002566 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002567 if (!htx)
2568 return 0;
2569
2570 pos = arg_p[0].data.sint;
2571 if (pos == -1)
2572 blk = htx_get_head_blk(htx);
2573 else if (pos == -2)
2574 blk = htx_get_tail_blk(htx);
2575 else if (pos == -3)
2576 blk = htx_get_first_blk(htx);
2577 else
2578 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2579
2580 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2581 smp->data.u.str.size = 0;
2582 smp->data.u.str.area = "";
2583 smp->data.u.str.data = 0;
2584 }
2585 else {
2586 struct ist name = htx_get_blk_name(htx, blk);
2587
2588 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2589 }
2590 smp->data.type = SMP_T_STR;
2591 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2592 return 1;
2593}
2594
2595/* Returns the header value if the selected HTX block exists and is a header or
2596 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2597 * supported or "head", "tail" or "first". The channel is chosen depending on
2598 * the sample direction. */
2599static int
2600smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2601{
2602 struct channel *chn;
2603 struct htx *htx;
2604 struct htx_blk *blk;
2605 int32_t pos;
2606
2607 if (!smp->strm || !arg_p)
2608 return 0;
2609
2610 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002611 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002612 if (!htx)
2613 return 0;
2614
2615 pos = arg_p[0].data.sint;
2616 if (pos == -1)
2617 blk = htx_get_head_blk(htx);
2618 else if (pos == -2)
2619 blk = htx_get_tail_blk(htx);
2620 else if (pos == -3)
2621 blk = htx_get_first_blk(htx);
2622 else
2623 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2624
2625 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2626 smp->data.u.str.size = 0;
2627 smp->data.u.str.area = "";
2628 smp->data.u.str.data = 0;
2629 }
2630 else {
2631 struct ist val = htx_get_blk_value(htx, blk);
2632
2633 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2634 }
2635 smp->data.type = SMP_T_STR;
2636 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2637 return 1;
2638}
2639
2640/* Returns the value if the selected HTX block exists and is a data
2641 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2642 * or "head", "tail" or "first". The channel is chosen depending on the sample
2643 * direction. */
2644static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002645smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002646{
2647 struct channel *chn;
2648 struct htx *htx;
2649 struct htx_blk *blk;
2650 int32_t pos;
2651
2652 if (!smp->strm || !arg_p)
2653 return 0;
2654
2655 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002656 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002657 if (!htx)
2658 return 0;
2659
2660 pos = arg_p[0].data.sint;
2661 if (pos == -1)
2662 blk = htx_get_head_blk(htx);
2663 else if (pos == -2)
2664 blk = htx_get_tail_blk(htx);
2665 else if (pos == -3)
2666 blk = htx_get_first_blk(htx);
2667 else
2668 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2669
2670 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2671 smp->data.u.str.size = 0;
2672 smp->data.u.str.area = "";
2673 smp->data.u.str.data = 0;
2674 }
2675 else {
2676 struct ist val = htx_get_blk_value(htx, blk);
2677
2678 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2679 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002680 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002681 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2682 return 1;
2683}
2684
2685/* This function is used to validate the arguments passed to any "htx_blk" fetch
2686 * keywords. An argument is expected by these keywords. It must be a positive
2687 * integer or on of the following strings: "head", "tail" or "first". It returns
2688 * 0 on error, and a non-zero value if OK.
2689 */
2690int val_blk_arg(struct arg *arg, char **err_msg)
2691{
2692 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2693 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2694 return 0;
2695 }
2696 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002697 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002698 arg[0].type = ARGT_SINT;
2699 arg[0].data.sint = -1;
2700 }
2701 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002702 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002703 arg[0].type = ARGT_SINT;
2704 arg[0].data.sint = -2;
2705 }
2706 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002707 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002708 arg[0].type = ARGT_SINT;
2709 arg[0].data.sint = -3;
2710 }
2711 else {
2712 int pos;
2713
2714 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002715 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002716 memprintf(err_msg, "invalid block position");
2717 return 0;
2718 }
2719 }
2720
2721 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2722 if (pos < 0) {
2723 memprintf(err_msg, "block position must not be negative");
2724 return 0;
2725 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002726 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002727 arg[0].type = ARGT_SINT;
2728 arg[0].data.sint = pos;
2729 }
2730
2731 return 1;
2732}
2733
2734
2735/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002736 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002737 */
2738static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002739 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002740
Christopher Faulet01f44452020-01-08 14:23:40 +01002741 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2742 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2743 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2744 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2745 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2746 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2747 { "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 +01002748
Christopher Faulet01f44452020-01-08 14:23:40 +01002749 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2750 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2751 { "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},
2752 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2753 { "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 +01002754 { "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 +01002755
2756 { /* END */ },
2757}};
2758
2759INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);