blob: 8b615627a1445cf1b7fe71015895e541149b12d8 [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 Tarreauf268ee82020-06-04 17:05:57 +020018#include <haproxy/global.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020019#include <haproxy/http_fetch.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020020#include <haproxy/log.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020021#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020022#include <haproxy/sample.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020023
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020025#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020029
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020030#include <haproxy/arg.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020031
Christopher Fauletf7346382019-07-17 22:02:08 +020032struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020033struct http_reply http_err_replies[HTTP_ERR_SIZE];
34
Christopher Faulet58857752020-01-15 15:19:50 +010035struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010036struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020037struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010038
Christopher Faulet76edc0f2020-01-13 15:52:01 +010039/* The declaration of an errorfiles/errorfile directives. Used during config
40 * parsing only. */
41struct conf_errors {
42 char type; /* directive type (0: errorfiles, 1: errorfile) */
43 union {
44 struct {
45 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020046 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010047 } errorfile; /* describe an "errorfile" directive */
48 struct {
49 char *name; /* the http-errors section name */
50 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
51 } errorfiles; /* describe an "errorfiles" directive */
52 } info;
53
54 char *file; /* file where the directive appears */
55 int line; /* line where the directive appears */
56
57 struct list list; /* next conf_errors */
58};
59
Christopher Faulet297fbb42019-05-13 14:41:27 +020060/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020061 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020062 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020063 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020064struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020065{
Christopher Faulet297fbb42019-05-13 14:41:27 +020066 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010067
Christopher Faulet29f17582019-05-23 11:03:26 +020068 BUG_ON(htx->first == -1);
69 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020070 if (!blk)
71 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020072 BUG_ON(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 +020073 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020074}
75
Christopher Faulet727a3f12020-02-07 16:39:41 +010076/* Returns the headers size in the HTX message */
77size_t http_get_hdrs_size(struct htx *htx)
78{
79 struct htx_blk *blk;
80 size_t sz = 0;
81
82 blk = htx_get_first_blk(htx);
83 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
84 return sz;
85
86 for (; blk; blk = htx_get_next_blk(htx, blk)) {
87 sz += htx_get_blksz(blk);
88 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
89 break;
90 }
91 return sz;
92}
93
Christopher Faulet8dd33e12020-05-05 07:42:42 +020094/* Finds the first or next occurrence of header matching <pattern> in the HTX
95 * message <htx> using the context <ctx>. This structure holds everything
96 * necessary to use the header and find next occurrence. If its <blk> member is
97 * NULL, the header is searched from the beginning. Otherwise, the next
98 * occurrence is returned. The function returns 1 when it finds a value, and 0
99 * when there is no more. It is designed to work with headers defined as
100 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
101 * full-line headers in whose comma is not a delimiter but is part of the
102 * syntax. A special case, if ctx->value is NULL when searching for a new values
103 * of a header, the current header is rescanned. This allows rescanning after a
104 * header deletion.
105 *
106 * The matching method is chosen by checking the flags :
107 *
108 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
109 * the regex are evaluated.
110 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
111 * to the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
113 * starting by the string are evaluated.
114 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
115 * ending by the string are evaluated.
116 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
117 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200118 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200119
120#define HTTP_FIND_FL_MATCH_STR 0x0001
121#define HTTP_FIND_FL_MATCH_PFX 0x0002
122#define HTTP_FIND_FL_MATCH_SFX 0x0003
123#define HTTP_FIND_FL_MATCH_SUB 0x0004
124#define HTTP_FIND_FL_MATCH_REG 0x0005
125/* 0x0006..0x000f: for other matching methods */
126#define HTTP_FIND_FL_MATCH_TYPE 0x000F
127#define HTTP_FIND_FL_FULL 0x0010
128
129static 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 +0200130{
131 struct htx_blk *blk = ctx->blk;
132 struct ist n, v;
133 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200134
135 if (blk) {
136 char *p;
137
Tim Duesterhused526372020-03-05 17:56:33 +0100138 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200139 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200140 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200141 goto next_blk;
142 v = htx_get_blk_value(htx, blk);
143 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
144 v.len -= (p - v.ptr);
145 v.ptr = p;
146 if (!v.len)
147 goto next_blk;
148 /* Skip comma */
149 if (*(v.ptr) == ',') {
150 v.ptr++;
151 v.len--;
152 }
153
154 goto return_hdr;
155 }
156
Christopher Faulet192c6a22019-06-11 16:32:24 +0200157 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200158 return 0;
159
Christopher Fauleta3f15502019-05-13 15:27:23 +0200160 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200162 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100163 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
164 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200165 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200166 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200167
168 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
169 const struct my_regex *re = pattern;
170
171 n = htx_get_blk_name(htx, blk);
172 if (!regex_exec2(re, n.ptr, n.len))
173 goto next_blk;
174 }
175 else {
176 const struct ist name = *(const struct ist *)(pattern);
177
Christopher Faulet47596d32018-10-22 09:17:28 +0200178 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200179 if (!istlen(name))
180 goto match;
181
Christopher Faulet47596d32018-10-22 09:17:28 +0200182 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200183 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
184 case HTTP_FIND_FL_MATCH_STR:
185 if (!isteqi(n, name))
186 goto next_blk;
187 break;
188 case HTTP_FIND_FL_MATCH_PFX:
189 if (istlen(n) < istlen(name))
190 goto next_blk;
191
192 n = ist2(istptr(n), istlen(name));
193 if (!isteqi(n, name))
194 goto next_blk;
195 break;
196 case HTTP_FIND_FL_MATCH_SFX:
197 if (istlen(n) < istlen(name))
198 goto next_blk;
199
200 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
201 if (!isteqi(n, name))
202 goto next_blk;
203 break;
204 case HTTP_FIND_FL_MATCH_SUB:
205 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
206 goto next_blk;
207 break;
208 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200209 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200210 break;
211 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200213 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200214 v = htx_get_blk_value(htx, blk);
215
216 return_hdr:
217 ctx->lws_before = 0;
218 ctx->lws_after = 0;
219 while (v.len && HTTP_IS_LWS(*v.ptr)) {
220 v.ptr++;
221 v.len--;
222 ctx->lws_before++;
223 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200224 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200225 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
226 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
227 v.len--;
228 ctx->lws_after++;
229 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200230 ctx->blk = blk;
231 ctx->value = v;
232 return 1;
233
234 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200235 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200236 }
237
238 ctx->blk = NULL;
239 ctx->value = ist("");
240 ctx->lws_before = ctx->lws_after = 0;
241 return 0;
242}
243
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200244
245/* Header names must match <name> */
246int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
247{
248 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
249}
250
251/* Header names must match <name>. Same than http_find_header */
252int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
253{
254 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
255}
256
257
258/* Header names must start with <prefix> */
259int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
260{
261 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
262}
263
264/* Header names must end with <suffix> */
265int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
266{
267 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
268}
269/* Header names must contain <sub> */
270int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
271{
272 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
273}
274
275/* Header names must match <re> regex*/
276int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
277{
278 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
279}
280
281
Christopher Faulet47596d32018-10-22 09:17:28 +0200282/* Adds a header block int the HTX message <htx>, just before the EOH block. It
283 * returns 1 on success, otherwise it returns 0.
284 */
285int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
286{
287 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200288 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200289 enum htx_blk_type type = htx_get_tail_type(htx);
290 int32_t prev;
291
292 blk = htx_add_header(htx, n, v);
293 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200294 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200295
296 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200297 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200298
299 /* <blk> is the head, swap it iteratively with its predecessor to place
300 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200301 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200302 struct htx_blk *pblk = htx_get_blk(htx, prev);
303 enum htx_blk_type type = htx_get_blk_type(pblk);
304
305 /* Swap .addr and .info fields */
306 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
307 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
308
309 if (blk->addr == pblk->addr)
310 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200311
312 /* Stop when end-of-header is reached */
313 if (type == HTX_BLK_EOH)
314 break;
315
316 blk = pblk;
317 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200318
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200319 end:
320 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100321 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200322 if (!http_update_authority(htx, sl, v))
323 goto fail;
324 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200325 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200326
327 fail:
328 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200329}
330
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100331/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200332 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200333 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100334int 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 +0200335{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200336 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200337
Christopher Faulet29f17582019-05-23 11:03:26 +0200338 blk = htx_get_first_blk(htx);
339 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200340 return 0;
341 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200342}
343
Christopher Faulete010c802018-10-24 10:36:45 +0200344/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
345 * on success, otherwise 0.
346 */
347int http_replace_req_meth(struct htx *htx, const struct ist meth)
348{
349 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200350 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100351 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200352
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100353 if (!sl)
354 return 0;
355
Christopher Faulete010c802018-10-24 10:36:45 +0200356 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100357 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
358 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200359
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100360 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
361 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200362
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100363 /* create the new start line */
364 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
365 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200366}
367
368/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
369 * success, otherwise 0.
370 */
371int http_replace_req_uri(struct htx *htx, const struct ist uri)
372{
373 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200374 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100375 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200376
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100377 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200378 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100379
Christopher Faulete010c802018-10-24 10:36:45 +0200380 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100381 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
382 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200383
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100384 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
385 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200386
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100387 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200388 if (!http_replace_stline(htx, meth, uri, vsn))
389 goto fail;
390
391 sl = http_get_stline(htx);
392 if (!http_update_host(htx, sl, uri))
393 goto fail;
394
395 return 1;
396 fail:
397 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200398}
399
400/* Replace the request path in the HTX message <htx> by <path>. The host part
401 * and the query string are preserved. It returns 1 on success, otherwise 0.
402 */
403int http_replace_req_path(struct htx *htx, const struct ist path)
404{
405 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200406 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100407 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200408 size_t plen = 0;
409
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100410 if (!sl)
411 return 0;
412
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100413 uri = htx_sl_req_uri(sl);
414 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100415 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100416 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200417 while (plen < p.len && *(p.ptr + plen) != '?')
418 plen++;
419
420 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100421 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
422 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200423
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100424 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
425 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
426
427 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200428 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
429 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100430 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200431
432 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100433 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200434}
435
436/* Replace the request query-string in the HTX message <htx> by <query>. The
437 * host part and the path are preserved. It returns 1 on success, otherwise
438 * 0.
439 */
440int http_replace_req_query(struct htx *htx, const struct ist query)
441{
442 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200443 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100444 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200445 int offset = 1;
446
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100447 if (!sl)
448 return 0;
449
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100450 uri = htx_sl_req_uri(sl);
451 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200452 while (q.len > 0 && *(q.ptr) != '?') {
453 q.ptr++;
454 q.len--;
455 }
456
457 /* skip the question mark or indicate that we must insert it
458 * (but only if the format string is not empty then).
459 */
460 if (q.len) {
461 q.ptr++;
462 q.len--;
463 }
464 else if (query.len > 1)
465 offset = 0;
466
467 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100468 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
469 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200470
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100471 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
472 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200473
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100474 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
475 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
476 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200477
478 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200480}
481
482/* Replace the response status in the HTX message <htx> by <status>. It returns
483 * 1 on success, otherwise 0.
484*/
485int http_replace_res_status(struct htx *htx, const struct ist status)
486{
487 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200488 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100489 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200490
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100491 if (!sl)
492 return 0;
493
Christopher Faulete010c802018-10-24 10:36:45 +0200494 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100495 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
496 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200497
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100498 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
499 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200500
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100501 /* create the new start line */
502 sl->info.res.status = strl2ui(status.ptr, status.len);
503 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200504}
505
506/* Replace the response reason in the HTX message <htx> by <reason>. It returns
507 * 1 on success, otherwise 0.
508*/
509int http_replace_res_reason(struct htx *htx, const struct ist reason)
510{
511 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200512 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100513 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200514
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100515 if (!sl)
516 return 0;
517
Christopher Faulete010c802018-10-24 10:36:45 +0200518 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100519 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
520 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200521
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
523 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200524
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100525 /* create the new start line */
526 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200527}
528
Christopher Faulet47596d32018-10-22 09:17:28 +0200529/* Replaces a part of a header value referenced in the context <ctx> by
530 * <data>. It returns 1 on success, otherwise it returns 0. The context is
531 * updated if necessary.
532 */
533int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
534{
535 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200536 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200537 char *start;
538 struct ist v;
539 uint32_t len, off;
540
541 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200542 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200543
544 v = htx_get_blk_value(htx, blk);
545 start = ctx->value.ptr - ctx->lws_before;
546 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
547 off = start - v.ptr;
548
549 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
550 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200551 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200552
553 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200554
555 sl = http_get_stline(htx);
556 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
557 struct ist n = htx_get_blk_name(htx, blk);
558
559 if (isteq(n, ist("host"))) {
560 if (!http_update_authority(htx, sl, v))
561 goto fail;
562 ctx->blk = NULL;
563 http_find_header(htx, ist("host"), ctx, 1);
564 blk = ctx->blk;
565 v = htx_get_blk_value(htx, blk);
566 }
567 }
568
Christopher Faulet47596d32018-10-22 09:17:28 +0200569 ctx->blk = blk;
570 ctx->value.ptr = v.ptr + off;
571 ctx->value.len = data.len;
572 ctx->lws_before = ctx->lws_after = 0;
573
574 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200575 fail:
576 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200577}
578
579/* Fully replaces a header referenced in the context <ctx> by the name <name>
580 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
581 * context is updated if necessary.
582 */
583int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
584 const struct ist name, const struct ist value)
585{
586 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200587 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200588
589 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200590 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200591
592 blk = htx_replace_header(htx, blk, name, value);
593 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200594 goto fail;
595
596 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100597 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200598 if (!http_update_authority(htx, sl, value))
599 goto fail;
600 ctx->blk = NULL;
601 http_find_header(htx, ist("host"), ctx, 1);
602 blk = ctx->blk;
603 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200604
605 ctx->blk = blk;
606 ctx->value = ist(NULL);
607 ctx->lws_before = ctx->lws_after = 0;
608
609 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200610 fail:
611 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200612}
613
614/* Remove one value of a header. This only works on a <ctx> returned by
615 * http_find_header function. The value is removed, as well as surrounding commas
616 * if any. If the removed value was alone, the whole header is removed. The
617 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
618 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
619 * form that can be handled by http_find_header() to find next occurrence.
620 */
621int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
622{
623 struct htx_blk *blk = ctx->blk;
624 char *start;
625 struct ist v;
626 uint32_t len;
627
628 if (!blk)
629 return 0;
630
631 start = ctx->value.ptr - ctx->lws_before;
632 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
633
634 v = htx_get_blk_value(htx, blk);
635 if (len == v.len) {
636 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200637 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200638 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100639 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200640 ctx->lws_before = ctx->lws_after = 0;
641 }
642 else {
643 ctx->blk = htx_get_blk(htx, htx->tail);
644 ctx->value = htx_get_blk_value(htx, ctx->blk);
645 ctx->lws_before = ctx->lws_after = 0;
646 }
647 return 1;
648 }
649
650 /* This was not the only value of this header. We have to remove the
651 * part pointed by ctx->value. If it is the last entry of the list, we
652 * remove the last separator.
653 */
654 if (start == v.ptr) {
655 /* It's the first header part but not the only one. So remove
656 * the comma after it. */
657 len++;
658 }
659 else {
660 /* There is at least one header part before the removed one. So
661 * remove the comma between them. */
662 start--;
663 len++;
664 }
665 /* Update the block content and its len */
666 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200667 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200668
669 /* Finally update the ctx */
670 ctx->value.ptr = start;
671 ctx->value.len = 0;
672 ctx->lws_before = ctx->lws_after = 0;
673
674 return 1;
675}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200676
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200677/* Updates the authority part of the uri with the value <host>. It happens when
678 * the header host is modified. It returns 0 on failure and 1 on success. It is
679 * the caller responsibility to provide the start-line and to be sure the uri
680 * contains an authority. Thus, if no authority is found in the uri, an error is
681 * returned.
682 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200683int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200684{
685 struct buffer *temp = get_trash_chunk();
686 struct ist meth, vsn, uri, authority;
687
688 uri = htx_sl_req_uri(sl);
689 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100690 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200691 return 0;
692
Christopher Faulet34b18e42020-02-18 11:02:21 +0100693 /* Don't update the uri if there is no change */
694 if (isteq(host, authority))
695 return 1;
696
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200697 /* Start by copying old method and version */
698 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
699 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
700
701 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
702 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
703
704 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
705 chunk_memcat(temp, host.ptr, host.len);
706 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
707 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
708
709 return http_replace_stline(htx, meth, uri, vsn);
710
711}
712
713/* Update the header host by extracting the authority of the uri <uri>. flags of
714 * the start-line are also updated accordingly. For orgin-form and asterisk-form
715 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
716 * removed from the flags of the start-line. Otherwise, this flag is set and the
717 * authority is used to set the value of the header host. This function returns
718 * 0 on failure and 1 on success.
719*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200720int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200721{
722 struct ist authority;
723 struct http_hdr_ctx ctx;
724
725 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
726 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
727 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
728 }
729 else {
730 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
731 if (sl->info.req.meth != HTTP_METH_CONNECT) {
732 // absolute-form (RFC7320 #5.3.2)
733 sl->flags |= HTX_SL_F_HAS_SCHM;
734 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
735 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
736
737 authority = http_get_authority(uri, 1);
738 if (!authority.len)
739 goto fail;
740 }
741 else {
742 // authority-form (RFC7320 #5.3.3)
743 authority = uri;
744 }
745
746 /* Replace header host value */
747 ctx.blk = NULL;
748 while (http_find_header(htx, ist("host"), &ctx, 1)) {
749 if (!http_replace_header_value(htx, &ctx, authority))
750 goto fail;
751 }
752
753 }
754 return 1;
755 fail:
756 return 0;
757}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200758
759/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
760 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
761 * performed over the whole headers. Otherwise it must contain a valid header
762 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
763 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
764 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
765 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
766 * -1. The value fetch stops at commas, so this function is suited for use with
767 * list headers.
768 * The return value is 0 if nothing was found, or non-zero otherwise.
769 */
770unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
771 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
772{
773 struct http_hdr_ctx local_ctx;
774 struct ist val_hist[MAX_HDR_HISTORY];
775 unsigned int hist_idx;
776 int found;
777
778 if (!ctx) {
779 local_ctx.blk = NULL;
780 ctx = &local_ctx;
781 }
782
783 if (occ >= 0) {
784 /* search from the beginning */
785 while (http_find_header(htx, hdr, ctx, 0)) {
786 occ--;
787 if (occ <= 0) {
788 *vptr = ctx->value.ptr;
789 *vlen = ctx->value.len;
790 return 1;
791 }
792 }
793 return 0;
794 }
795
796 /* negative occurrence, we scan all the list then walk back */
797 if (-occ > MAX_HDR_HISTORY)
798 return 0;
799
800 found = hist_idx = 0;
801 while (http_find_header(htx, hdr, ctx, 0)) {
802 val_hist[hist_idx] = ctx->value;
803 if (++hist_idx >= MAX_HDR_HISTORY)
804 hist_idx = 0;
805 found++;
806 }
807 if (-occ > found)
808 return 0;
809
810 /* OK now we have the last occurrence in [hist_idx-1], and we need to
811 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
812 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
813 * to remain in the 0..9 range.
814 */
815 hist_idx += occ + MAX_HDR_HISTORY;
816 if (hist_idx >= MAX_HDR_HISTORY)
817 hist_idx -= MAX_HDR_HISTORY;
818 *vptr = val_hist[hist_idx].ptr;
819 *vlen = val_hist[hist_idx].len;
820 return 1;
821}
822
823/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
824 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
825 * performed over the whole headers. Otherwise it must contain a valid header
826 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
827 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
828 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
829 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
830 * -1. This function differs from http_get_hdr() in that it only returns full
831 * line header values and does not stop at commas.
832 * The return value is 0 if nothing was found, or non-zero otherwise.
833 */
834unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
835 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
836{
837 struct http_hdr_ctx local_ctx;
838 struct ist val_hist[MAX_HDR_HISTORY];
839 unsigned int hist_idx;
840 int found;
841
842 if (!ctx) {
843 local_ctx.blk = NULL;
844 ctx = &local_ctx;
845 }
846
847 if (occ >= 0) {
848 /* search from the beginning */
849 while (http_find_header(htx, hdr, ctx, 1)) {
850 occ--;
851 if (occ <= 0) {
852 *vptr = ctx->value.ptr;
853 *vlen = ctx->value.len;
854 return 1;
855 }
856 }
857 return 0;
858 }
859
860 /* negative occurrence, we scan all the list then walk back */
861 if (-occ > MAX_HDR_HISTORY)
862 return 0;
863
864 found = hist_idx = 0;
865 while (http_find_header(htx, hdr, ctx, 1)) {
866 val_hist[hist_idx] = ctx->value;
867 if (++hist_idx >= MAX_HDR_HISTORY)
868 hist_idx = 0;
869 found++;
870 }
871 if (-occ > found)
872 return 0;
873
874 /* OK now we have the last occurrence in [hist_idx-1], and we need to
875 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
876 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
877 * to remain in the 0..9 range.
878 */
879 hist_idx += occ + MAX_HDR_HISTORY;
880 if (hist_idx >= MAX_HDR_HISTORY)
881 hist_idx -= MAX_HDR_HISTORY;
882 *vptr = val_hist[hist_idx].ptr;
883 *vlen = val_hist[hist_idx].len;
884 return 1;
885}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100886
Christopher Faulet90cc4812019-07-22 16:49:30 +0200887int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100888{
889 struct htx *htx;
890 struct htx_sl *sl;
891 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200892 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100893 union h1_sl h1sl;
894 unsigned int flags = HTX_SL_F_IS_RESP;
895 int ret = 0;
896
Christopher Faulet90cc4812019-07-22 16:49:30 +0200897 b_reset(buf);
898 if (!raw.len) {
899 buf->size = 0;
900 buf->area = malloc(raw.len);
901 return 1;
902 }
903
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100904 buf->size = global.tune.bufsize;
905 buf->area = (char *)malloc(buf->size);
906 if (!buf->area)
907 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100908
909 h1m_init_res(&h1m);
910 h1m.flags |= H1_MF_NO_PHDR;
911 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
912 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
913 if (ret <= 0)
914 goto error;
915
916 if (unlikely(h1sl.st.v.len != 8))
917 goto error;
918 if ((*(h1sl.st.v.ptr + 5) > '1') ||
919 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
920 h1m.flags |= H1_MF_VER_11;
921
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200922 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
923 goto error;
924
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100925 if (h1m.flags & H1_MF_VER_11)
926 flags |= HTX_SL_F_VER_11;
927 if (h1m.flags & H1_MF_XFER_ENC)
928 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200929 if (h1m.flags & H1_MF_CLEN) {
930 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
931 if (h1m.body_len == 0)
932 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100933 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200934 if (h1m.flags & H1_MF_CHNK)
935 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100936
937 htx = htx_from_buf(buf);
938 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
939 if (!sl || !htx_add_all_headers(htx, hdrs))
940 goto error;
941 sl->info.res.status = h1sl.st.status;
942
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200943 while (raw.len > ret) {
944 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
945 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100946 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200947 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100948 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200949
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100950 if (!htx_add_endof(htx, HTX_BLK_EOM))
951 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200952
Christopher Faulet90cc4812019-07-22 16:49:30 +0200953 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100954
955error:
956 if (buf->size)
957 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200958 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100959}
960
Christopher Faulet18630642020-05-12 18:57:28 +0200961void release_http_reply(struct http_reply *http_reply)
962{
963 struct logformat_node *lf, *lfb;
964 struct http_reply_hdr *hdr, *hdrb;
965
966 if (!http_reply)
967 return;
968
969 free(http_reply->ctype);
970 http_reply->ctype = NULL;
971 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
972 LIST_DEL(&hdr->list);
973 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
974 LIST_DEL(&lf->list);
975 release_sample_expr(lf->expr);
976 free(lf->arg);
977 free(lf);
978 }
979 istfree(&hdr->name);
980 free(hdr);
981 }
982
983 if (http_reply->type == HTTP_REPLY_ERRFILES) {
984 free(http_reply->body.http_errors);
985 http_reply->body.http_errors = NULL;
986 }
987 else if (http_reply->type == HTTP_REPLY_RAW)
988 chunk_destroy(&http_reply->body.obj);
989 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
990 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
991 LIST_DEL(&lf->list);
992 release_sample_expr(lf->expr);
993 free(lf->arg);
994 free(lf);
995 }
996 }
Christopher Faulet63d48242020-05-21 09:59:22 +0200997 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +0200998}
999
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001000static int http_htx_init(void)
1001{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001002 struct buffer chk;
1003 struct ist raw;
1004 int rc;
1005 int err_code = 0;
1006
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001007 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1008 if (!http_err_msgs[rc]) {
1009 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1010 err_code |= ERR_ALERT | ERR_FATAL;
1011 continue;
1012 }
1013
1014 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1015 if (!http_str_to_htx(&chk, raw)) {
1016 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1017 http_err_codes[rc]);
1018 err_code |= ERR_ALERT | ERR_FATAL;
1019 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001020 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001021 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1022 http_err_replies[rc].status = http_err_codes[rc];
1023 http_err_replies[rc].ctype = NULL;
1024 LIST_INIT(&http_err_replies[rc].hdrs);
1025 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001026 }
1027end:
1028 return err_code;
1029}
1030
Christopher Faulet58857752020-01-15 15:19:50 +01001031static void http_htx_deinit(void)
1032{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001033 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001034 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001035 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001036 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001037 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001038
1039 node = ebpt_first(&http_error_messages);
1040 while (node) {
1041 next = ebpt_next(node);
1042 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001043 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1044 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001045 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001046 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001047 node = next;
1048 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001049
1050 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1051 free(http_errs->conf.file);
1052 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001053 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1054 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001055 LIST_DEL(&http_errs->list);
1056 free(http_errs);
1057 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001058
1059 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
1060 LIST_DEL(&http_rep->list);
1061 release_http_reply(http_rep);
1062 }
Christopher Faulet58857752020-01-15 15:19:50 +01001063}
1064
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001065REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001066REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001067
Christopher Faulet58857752020-01-15 15:19:50 +01001068/* Reads content of the error file <file> and convert it into an HTX message. On
1069 * success, the HTX message is returned. On error, NULL is returned and an error
1070 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001071 */
Christopher Faulet58857752020-01-15 15:19:50 +01001072struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001073{
Christopher Faulet58857752020-01-15 15:19:50 +01001074 struct buffer *buf = NULL;
1075 struct buffer chk;
1076 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001077 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001078 struct stat stat;
1079 char *err = NULL;
1080 int errnum, errlen;
1081 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001082
1083 /* already loaded */
1084 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1085 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001086 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1087 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001088 goto out;
1089 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001090
Christopher Faulet58857752020-01-15 15:19:50 +01001091 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001092 fd = open(file, O_RDONLY);
1093 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1094 memprintf(errmsg, "error opening file '%s'.", file);
1095 goto out;
1096 }
1097
1098 if (stat.st_size <= global.tune.bufsize)
1099 errlen = stat.st_size;
1100 else {
1101 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1102 file, global.tune.bufsize);
1103 errlen = global.tune.bufsize;
1104 }
1105
1106 err = malloc(errlen);
1107 if (!err) {
1108 memprintf(errmsg, "out of memory.");
1109 goto out;
1110 }
1111
1112 errnum = read(fd, err, errlen);
1113 if (errnum != errlen) {
1114 memprintf(errmsg, "error reading file '%s'.", file);
1115 goto out;
1116 }
1117
Christopher Faulet58857752020-01-15 15:19:50 +01001118 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001119 http_errmsg = calloc(1, sizeof(*http_errmsg));
1120 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001121 memprintf(errmsg, "out of memory.");
1122 goto out;
1123 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001124 http_errmsg->node.key = strdup(file);
1125 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001126 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001127 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001128 goto out;
1129 }
1130
1131 /* Convert the error file into an HTX message */
1132 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001133 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001134 free(http_errmsg->node.key);
1135 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001136 goto out;
1137 }
1138
Christopher Faulet58857752020-01-15 15:19:50 +01001139 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001140 http_errmsg->msg = chk;
1141 ebis_insert(&http_error_messages, &http_errmsg->node);
1142 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001143
Christopher Faulet5031ef52020-01-15 11:22:07 +01001144 out:
1145 if (fd >= 0)
1146 close(fd);
1147 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001148 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001149}
1150
Ilya Shipitsind4259502020-04-08 01:07:56 +05001151/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001152 * message is returned. On error, NULL is returned and an error message is
1153 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001154 */
Christopher Faulet58857752020-01-15 15:19:50 +01001155struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001156{
Christopher Faulet58857752020-01-15 15:19:50 +01001157 struct buffer *buf = NULL;
1158 struct buffer chk;
1159 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001160 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001161
1162 /* already loaded */
1163 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1164 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001165 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1166 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001167 goto out;
1168 }
1169 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001170 http_errmsg = calloc(1, sizeof(*http_errmsg));
1171 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001172 memprintf(errmsg, "out of memory.");
1173 goto out;
1174 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001175 http_errmsg->node.key = strdup(key);
1176 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001177 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001178 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001179 goto out;
1180 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001181
1182 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001183 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001184 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001185 free(http_errmsg->node.key);
1186 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001187 goto out;
1188 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001189
Christopher Faulet58857752020-01-15 15:19:50 +01001190 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001191 http_errmsg->msg = chk;
1192 ebis_insert(&http_error_messages, &http_errmsg->node);
1193 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001194 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001195 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001196}
1197
Christopher Faulet5031ef52020-01-15 11:22:07 +01001198/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001199 * <status>. It returns NULL if there is any error, otherwise it return the
1200 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001201 */
Christopher Faulet58857752020-01-15 15:19:50 +01001202struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001203{
Christopher Faulet58857752020-01-15 15:19:50 +01001204 struct buffer *buf = NULL;
1205 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001206
1207 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1208 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001209 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001210 break;
1211 }
1212 }
1213
1214 if (rc >= HTTP_ERR_SIZE)
1215 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001216 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001217}
1218
1219/* This function creates HTX error message corresponding to a redirect message
1220 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001221 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1222 * returns NULL if there is any error, otherwise it return the corresponding HTX
1223 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001224 */
Christopher Faulet58857752020-01-15 15:19:50 +01001225struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001226{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001227 static const char *HTTP_302 =
1228 "HTTP/1.1 302 Found\r\n"
1229 "Cache-Control: no-cache\r\n"
1230 "Content-length: 0\r\n"
1231 "Location: "; /* not terminated since it will be concatenated with the URL */
1232 static const char *HTTP_303 =
1233 "HTTP/1.1 303 See Other\r\n"
1234 "Cache-Control: no-cache\r\n"
1235 "Content-length: 0\r\n"
1236 "Location: "; /* not terminated since it will be concatenated with the URL */
1237
Christopher Faulet58857752020-01-15 15:19:50 +01001238 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001239 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001240 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001241 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001242
1243 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1244 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001245 /* Create the error key */
1246 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1247 memprintf(errmsg, "out of memory.");
1248 goto out;
1249 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001250 /* Create the error message */
1251 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1252 errlen = strlen(msg) + strlen(url) + 5;
1253 err = malloc(errlen);
1254 if (!err) {
1255 memprintf(errmsg, "out of memory.");
1256 goto out;
1257 }
1258 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1259
1260 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001261 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001262 break;
1263 }
1264 }
1265
1266 if (rc >= HTTP_ERR_SIZE)
1267 memprintf(errmsg, "status code '%d' not handled.", status);
1268out:
Christopher Faulet58857752020-01-15 15:19:50 +01001269 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001270 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001271 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001272}
1273
Christopher Faulet7eea2412020-05-13 15:02:59 +02001274/* Check an "http reply" and, for replies referencing an http-errors section,
1275 * try to find the right section and the right error message in this section. If
1276 * found, the reply is updated. If the http-errors section exists but the error
1277 * message is not found, no error message is set to fallback on the default
1278 * ones. Otherwise (unknown section) an error is returned.
1279 *
1280 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1281 * filled.
1282 */
1283int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1284{
1285 struct http_errors *http_errs;
1286 int ret = 1;
1287
1288 if (reply->type != HTTP_REPLY_ERRFILES)
1289 goto end;
1290
1291 list_for_each_entry(http_errs, &http_errors_list, list) {
1292 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001293 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001294 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001295 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1296 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001297 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1298 "not declared in http-errors section '%s'.\n",
1299 px->id, reply->status, http_errs->id);
1300 break;
1301 }
1302 }
1303
1304 if (&http_errs->list == &http_errors_list) {
1305 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1306 reply->body.http_errors);
1307 ret = 0;
1308 }
1309
1310 end:
1311 return ret;
1312}
1313
Christopher Faulet47e791e2020-05-13 14:36:55 +02001314/* Parse an "http reply". It returns the reply on success or NULL on error. This
1315 * function creates one of the following http replies :
1316 *
1317 * - HTTP_REPLY_EMPTY : dummy response, no payload
1318 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1319 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1320 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1321 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1322 *
1323 * The content-type must be defined for non-empty payload. It is ignored for
1324 * error messages (implicit or explicit). When an http-errors section is
1325 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1326 * during the configuration validity check or dynamically. It is the caller
1327 * responsibility to choose. If no status code is configured, <default_status>
1328 * is set.
1329 */
1330struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1331 int default_status, char **errmsg)
1332{
1333 struct logformat_node *lf, *lfb;
1334 struct http_reply *reply = NULL;
1335 struct http_reply_hdr *hdr, *hdrb;
1336 struct stat stat;
1337 const char *act_arg = NULL;
1338 char *obj = NULL;
1339 int cur_arg, cap, objlen = 0, fd = -1;
1340
1341
1342 reply = calloc(1, sizeof(*reply));
1343 if (!reply) {
1344 memprintf(errmsg, "out of memory");
1345 goto error;
1346 }
1347 LIST_INIT(&reply->hdrs);
1348 reply->type = HTTP_REPLY_EMPTY;
1349 reply->status = default_status;
1350
Christopher Faulet3b967c12020-05-15 15:47:44 +02001351 if (px->conf.args.ctx == ARGC_HERR)
1352 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1353 else
1354 cap = ((px->conf.args.ctx == ARGC_HRQ)
1355 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1356 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001357
1358 cur_arg = *orig_arg;
1359 while (*args[cur_arg]) {
1360 if (strcmp(args[cur_arg], "status") == 0) {
1361 cur_arg++;
1362 if (!*args[cur_arg]) {
1363 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1364 goto error;
1365 }
1366 reply->status = atol(args[cur_arg]);
1367 if (reply->status < 200 || reply->status > 599) {
1368 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1369 goto error;
1370 }
1371 cur_arg++;
1372 }
1373 else if (strcmp(args[cur_arg], "content-type") == 0) {
1374 cur_arg++;
1375 if (!*args[cur_arg]) {
1376 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1377 goto error;
1378 }
1379 free(reply->ctype);
1380 reply->ctype = strdup(args[cur_arg]);
1381 cur_arg++;
1382 }
1383 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1384 if (reply->type != HTTP_REPLY_EMPTY) {
1385 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1386 goto error;
1387 }
1388 act_arg = args[cur_arg];
1389 cur_arg++;
1390 if (!*args[cur_arg]) {
1391 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1392 goto error;
1393 }
1394 reply->body.http_errors = strdup(args[cur_arg]);
1395 if (!reply->body.http_errors) {
1396 memprintf(errmsg, "out of memory");
1397 goto error;
1398 }
1399 reply->type = HTTP_REPLY_ERRFILES;
1400 cur_arg++;
1401 }
1402 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1403 if (reply->type != HTTP_REPLY_EMPTY) {
1404 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1405 goto error;
1406 }
1407 act_arg = args[cur_arg];
1408 reply->type = HTTP_REPLY_ERRMSG;
1409 cur_arg++;
1410 }
1411 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1412 if (reply->type != HTTP_REPLY_EMPTY) {
1413 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1414 goto error;
1415 }
1416 act_arg = args[cur_arg];
1417 cur_arg++;
1418 if (!*args[cur_arg]) {
1419 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1420 goto error;
1421 }
1422 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1423 if (!reply->body.errmsg) {
1424 goto error;
1425 }
1426 reply->type = HTTP_REPLY_ERRMSG;
1427 cur_arg++;
1428 }
1429 else if (strcmp(args[cur_arg], "file") == 0) {
1430 if (reply->type != HTTP_REPLY_EMPTY) {
1431 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1432 goto error;
1433 }
1434 act_arg = args[cur_arg];
1435 cur_arg++;
1436 if (!*args[cur_arg]) {
1437 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1438 goto error;
1439 }
1440 fd = open(args[cur_arg], O_RDONLY);
1441 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1442 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1443 goto error;
1444 }
1445 if (stat.st_size > global.tune.bufsize) {
1446 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1447 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1448 goto error;
1449 }
1450 objlen = stat.st_size;
1451 obj = malloc(objlen);
1452 if (!obj || read(fd, obj, objlen) != objlen) {
1453 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1454 goto error;
1455 }
1456 close(fd);
1457 fd = -1;
1458 reply->type = HTTP_REPLY_RAW;
1459 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1460 obj = NULL;
1461 cur_arg++;
1462 }
1463 else if (strcmp(args[cur_arg], "string") == 0) {
1464 if (reply->type != HTTP_REPLY_EMPTY) {
1465 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1466 goto error;
1467 }
1468 act_arg = args[cur_arg];
1469 cur_arg++;
1470 if (!*args[cur_arg]) {
1471 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1472 goto error;
1473 }
1474 obj = strdup(args[cur_arg]);
1475 objlen = strlen(args[cur_arg]);
1476 if (!obj) {
1477 memprintf(errmsg, "out of memory");
1478 goto error;
1479 }
1480 reply->type = HTTP_REPLY_RAW;
1481 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1482 obj = NULL;
1483 cur_arg++;
1484 }
1485 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1486 if (reply->type != HTTP_REPLY_EMPTY) {
1487 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1488 goto error;
1489 }
1490 act_arg = args[cur_arg];
1491 cur_arg++;
1492 if (!*args[cur_arg]) {
1493 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1494 goto error;
1495 }
1496 fd = open(args[cur_arg], O_RDONLY);
1497 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1498 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1499 goto error;
1500 }
1501 if (stat.st_size > global.tune.bufsize) {
1502 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1503 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1504 goto error;
1505 }
1506 objlen = stat.st_size;
1507 obj = malloc(objlen + 1);
1508 if (!obj || read(fd, obj, objlen) != objlen) {
1509 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1510 goto error;
1511 }
1512 close(fd);
1513 fd = -1;
1514 obj[objlen] = '\0';
1515 reply->type = HTTP_REPLY_LOGFMT;
1516 cur_arg++;
1517 }
1518 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1519 if (reply->type != HTTP_REPLY_EMPTY) {
1520 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1521 goto error;
1522 }
1523 act_arg = args[cur_arg];
1524 cur_arg++;
1525 if (!*args[cur_arg]) {
1526 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1527 goto error;
1528 }
1529 obj = strdup(args[cur_arg]);
1530 objlen = strlen(args[cur_arg]);
1531 reply->type = HTTP_REPLY_LOGFMT;
1532 cur_arg++;
1533 }
1534 else if (strcmp(args[cur_arg], "hdr") == 0) {
1535 cur_arg++;
1536 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1537 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1538 goto error;
1539 }
1540 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1541 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1542 strcasecmp(args[cur_arg], "content-type") == 0) {
1543 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1544 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1545 cur_arg += 2;
1546 continue;
1547 }
1548 hdr = calloc(1, sizeof(*hdr));
1549 if (!hdr) {
1550 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1551 goto error;
1552 }
Christopher Fauletd6e31232020-05-21 10:10:41 +02001553 LIST_ADDQ(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001554 LIST_INIT(&hdr->value);
1555 hdr->name = ist(strdup(args[cur_arg]));
1556 if (!isttest(hdr->name)) {
1557 memprintf(errmsg, "out of memory");
1558 goto error;
1559 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001560 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1561 goto error;
1562
1563 free(px->conf.lfs_file);
1564 px->conf.lfs_file = strdup(px->conf.args.file);
1565 px->conf.lfs_line = px->conf.args.line;
1566 cur_arg += 2;
1567 }
1568 else
1569 break;
1570 }
1571
1572 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1573 if (reply->ctype) {
1574 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1575 " neither errorfile nor payload defined.\n",
1576 px->conf.args.file, px->conf.args.line, reply->ctype);
1577 free(reply->ctype);
1578 reply->ctype = NULL;
1579 }
1580 }
1581 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1582
1583 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1584 /* default errorfile or errorfiles: check the status */
1585 int rc;
1586
1587 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1588 if (http_err_codes[rc] == reply->status)
1589 break;
1590 }
1591
1592 if (rc >= HTTP_ERR_SIZE) {
1593 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1594 reply->status, act_arg);
1595 goto error;
1596 }
1597 }
1598
1599 if (reply->ctype) {
1600 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1601 "with an erorrfile.\n",
1602 px->conf.args.file, px->conf.args.line, reply->ctype);
1603 free(reply->ctype);
1604 reply->ctype = NULL;
1605 }
1606 if (!LIST_ISEMPTY(&reply->hdrs)) {
1607 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1608 "with an erorrfile.\n",
1609 px->conf.args.file, px->conf.args.line);
1610 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1611 LIST_DEL(&hdr->list);
1612 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1613 LIST_DEL(&lf->list);
1614 release_sample_expr(lf->expr);
1615 free(lf->arg);
1616 free(lf);
1617 }
1618 istfree(&hdr->name);
1619 free(hdr);
1620 }
1621 }
1622 }
1623 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1624 if (!reply->ctype && objlen) {
1625 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1626 goto error;
1627 }
1628 if (reply->ctype && !b_data(&reply->body.obj)) {
1629 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1630 "with an emtpy payload.\n",
1631 px->conf.args.file, px->conf.args.line, reply->ctype);
1632 free(reply->ctype);
1633 reply->ctype = NULL;
1634 }
1635 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1636 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1637 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1638 px->conf.args.file, px->conf.args.line);
1639 }
1640 }
1641 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1642 LIST_INIT(&reply->body.fmt);
1643 if (!reply->ctype) {
1644 memprintf(errmsg, "a content type must be defined with a log-format payload");
1645 goto error;
1646 }
1647 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1648 goto error;
1649
1650 free(px->conf.lfs_file);
1651 px->conf.lfs_file = strdup(px->conf.args.file);
1652 px->conf.lfs_line = px->conf.args.line;
1653 }
1654
1655 free(obj);
1656 *orig_arg = cur_arg;
1657 return reply;
1658
1659 error:
1660 free(obj);
1661 if (fd >= 0)
1662 close(fd);
1663 release_http_reply(reply);
1664 return NULL;
1665}
1666
Christopher Faulet07f41f72020-01-16 16:16:06 +01001667/* Parses the "errorloc[302|303]" proxy keyword */
1668static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1669 struct proxy *defpx, const char *file, int line,
1670 char **errmsg)
1671{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001672 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001673 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001674 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001675 int errloc, status;
1676 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001677
1678 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1679 ret = 1;
1680 goto out;
1681 }
1682
1683 if (*(args[1]) == 0 || *(args[2]) == 0) {
1684 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1685 ret = -1;
1686 goto out;
1687 }
1688
1689 status = atol(args[1]);
1690 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1691 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1692 if (!msg) {
1693 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1694 ret = -1;
1695 goto out;
1696 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001697
1698 reply = calloc(1, sizeof(*reply));
1699 if (!reply) {
1700 memprintf(errmsg, "%s : out of memory.", args[0]);
1701 ret = -1;
1702 goto out;
1703 }
1704 reply->type = HTTP_REPLY_ERRMSG;
1705 reply->status = status;
1706 reply->ctype = NULL;
1707 LIST_INIT(&reply->hdrs);
1708 reply->body.errmsg = msg;
1709 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001710
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001711 conf_err = calloc(1, sizeof(*conf_err));
1712 if (!conf_err) {
1713 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001714 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001715 ret = -1;
1716 goto out;
1717 }
1718 conf_err->type = 1;
1719 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001720 conf_err->info.errorfile.reply = reply;
1721
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001722 conf_err->file = strdup(file);
1723 conf_err->line = line;
1724 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001725
1726 out:
1727 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001728
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001729}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001730
1731/* Parses the "errorfile" proxy keyword */
1732static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1733 struct proxy *defpx, const char *file, int line,
1734 char **errmsg)
1735{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001736 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001737 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001738 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001739 int status;
1740 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001741
1742 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1743 ret = 1;
1744 goto out;
1745 }
1746
1747 if (*(args[1]) == 0 || *(args[2]) == 0) {
1748 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1749 ret = -1;
1750 goto out;
1751 }
1752
1753 status = atol(args[1]);
1754 msg = http_parse_errorfile(status, args[2], errmsg);
1755 if (!msg) {
1756 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1757 ret = -1;
1758 goto out;
1759 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001760
Christopher Faulet5809e102020-05-14 17:31:52 +02001761 reply = calloc(1, sizeof(*reply));
1762 if (!reply) {
1763 memprintf(errmsg, "%s : out of memory.", args[0]);
1764 ret = -1;
1765 goto out;
1766 }
1767 reply->type = HTTP_REPLY_ERRMSG;
1768 reply->status = status;
1769 reply->ctype = NULL;
1770 LIST_INIT(&reply->hdrs);
1771 reply->body.errmsg = msg;
1772 LIST_ADDQ(&http_replies_list, &reply->list);
1773
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001774 conf_err = calloc(1, sizeof(*conf_err));
1775 if (!conf_err) {
1776 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001777 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001778 ret = -1;
1779 goto out;
1780 }
1781 conf_err->type = 1;
1782 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001783 conf_err->info.errorfile.reply = reply;
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);
1787
1788 out:
1789 return ret;
1790
1791}
1792
1793/* Parses the "errorfiles" proxy keyword */
1794static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1795 struct proxy *defpx, const char *file, int line,
1796 char **err)
1797{
1798 struct conf_errors *conf_err = NULL;
1799 char *name = NULL;
1800 int rc, ret = 0;
1801
1802 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1803 ret = 1;
1804 goto out;
1805 }
1806
1807 if (!*(args[1])) {
1808 memprintf(err, "%s : expects <name> as argument.", args[0]);
1809 ret = -1;
1810 goto out;
1811 }
1812
1813 name = strdup(args[1]);
1814 conf_err = calloc(1, sizeof(*conf_err));
1815 if (!name || !conf_err) {
1816 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001817 goto error;
1818 }
1819 conf_err->type = 0;
1820
1821 conf_err->info.errorfiles.name = name;
1822 if (!*(args[2])) {
1823 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1824 conf_err->info.errorfiles.status[rc] = 1;
1825 }
1826 else {
1827 int cur_arg, status;
1828 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1829 status = atol(args[cur_arg]);
1830
1831 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1832 if (http_err_codes[rc] == status) {
1833 conf_err->info.errorfiles.status[rc] = 2;
1834 break;
1835 }
1836 }
1837 if (rc >= HTTP_ERR_SIZE) {
1838 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001839 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001840 }
1841 }
1842 }
1843 conf_err->file = strdup(file);
1844 conf_err->line = line;
1845 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1846 out:
1847 return ret;
1848
1849 error:
1850 free(name);
1851 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001852 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001853 goto out;
1854}
1855
Christopher Faulet3b967c12020-05-15 15:47:44 +02001856/* Parses the "http-error" proxy keyword */
1857static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
1858 struct proxy *defpx, const char *file, int line,
1859 char **errmsg)
1860{
1861 struct conf_errors *conf_err;
1862 struct http_reply *reply = NULL;
1863 int rc, cur_arg, ret = 0;
1864
1865 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1866 ret = 1;
1867 goto out;
1868 }
1869
1870 cur_arg = 1;
1871 curpx->conf.args.ctx = ARGC_HERR;
1872 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1873 if (!reply) {
1874 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1875 goto error;
1876 }
1877 else if (!reply->status) {
1878 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1879 goto error;
1880 }
1881
1882 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1883 if (http_err_codes[rc] == reply->status)
1884 break;
1885 }
1886
1887 if (rc >= HTTP_ERR_SIZE) {
1888 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1889 goto error;
1890 }
1891 if (*args[cur_arg]) {
1892 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1893 goto error;
1894 }
1895
1896 conf_err = calloc(1, sizeof(*conf_err));
1897 if (!conf_err) {
1898 memprintf(errmsg, "%s : out of memory.", args[0]);
1899 goto error;
1900 }
1901 if (reply->type == HTTP_REPLY_ERRFILES) {
1902 int rc = http_get_status_idx(reply->status);
1903
1904 conf_err->type = 2;
1905 conf_err->info.errorfiles.name = reply->body.http_errors;
1906 conf_err->info.errorfiles.status[rc] = 2;
1907 reply->body.http_errors = NULL;
1908 release_http_reply(reply);
1909 }
1910 else {
1911 conf_err->type = 1;
1912 conf_err->info.errorfile.status = reply->status;
1913 conf_err->info.errorfile.reply = reply;
1914 LIST_ADDQ(&http_replies_list, &reply->list);
1915 }
1916 conf_err->file = strdup(file);
1917 conf_err->line = line;
1918 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1919
1920 out:
1921 return ret;
1922
1923 error:
1924 release_http_reply(reply);
1925 ret = -1;
1926 goto out;
1927
1928}
1929
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001930/* Check "errorfiles" proxy keyword */
1931static int proxy_check_errors(struct proxy *px)
1932{
1933 struct conf_errors *conf_err, *conf_err_back;
1934 struct http_errors *http_errs;
1935 int rc, err = 0;
1936
1937 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1938 if (conf_err->type == 1) {
1939 /* errorfile */
1940 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02001941 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001942
1943 /* For proxy, to rely on default replies, just don't reference a reply */
1944 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
1945 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001946 }
1947 else {
1948 /* errorfiles */
1949 list_for_each_entry(http_errs, &http_errors_list, list) {
1950 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1951 break;
1952 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001953
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001954 /* unknown http-errors section */
1955 if (&http_errs->list == &http_errors_list) {
1956 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1957 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1958 err |= ERR_ALERT | ERR_FATAL;
1959 free(conf_err->info.errorfiles.name);
1960 goto next;
1961 }
1962
1963 free(conf_err->info.errorfiles.name);
1964 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1965 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02001966 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02001967 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001968 else if (conf_err->info.errorfiles.status[rc] == 2)
1969 ha_warning("config: proxy '%s' : status '%d' not declared in"
1970 " http-errors section '%s' (at %s:%d).\n",
1971 px->id, http_err_codes[rc], http_errs->id,
1972 conf_err->file, conf_err->line);
1973 }
1974 }
1975 }
1976 next:
1977 LIST_DEL(&conf_err->list);
1978 free(conf_err->file);
1979 free(conf_err);
1980 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001981
1982 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001983 return err;
1984}
1985
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001986static int post_check_errors()
1987{
1988 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001989 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001990 struct htx *htx;
1991 int err_code = 0;
1992
1993 node = ebpt_first(&http_error_messages);
1994 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001995 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1996 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001997 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001998 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001999 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2000 ha_warning("config: errorfile '%s' runs over the buffer space"
2001 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002002 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002003 (char *)node->key);
2004 err_code |= ERR_WARN;
2005 }
2006 next:
2007 node = ebpt_next(node);
2008 }
2009
2010 return err_code;
2011}
2012
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002013int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
2014{
2015 struct conf_errors *conf_err, *new_conf_err = NULL;
2016 int ret = 0;
2017
2018 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2019 new_conf_err = calloc(1, sizeof(*new_conf_err));
2020 if (!new_conf_err) {
2021 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2022 goto out;
2023 }
2024 new_conf_err->type = conf_err->type;
2025 if (conf_err->type == 1) {
2026 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002027 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002028 }
2029 else {
2030 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2031 if (!new_conf_err->info.errorfiles.name) {
2032 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2033 goto out;
2034 }
2035 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2036 sizeof(conf_err->info.errorfiles.status));
2037 }
2038 new_conf_err->file = strdup(conf_err->file);
2039 new_conf_err->line = conf_err->line;
2040 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
2041 new_conf_err = NULL;
2042 }
2043 ret = 1;
2044
2045 out:
2046 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002047 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002048}
2049
2050void proxy_release_conf_errors(struct proxy *px)
2051{
2052 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002053
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002054 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2055 if (conf_err->type == 0)
2056 free(conf_err->info.errorfiles.name);
2057 LIST_DEL(&conf_err->list);
2058 free(conf_err->file);
2059 free(conf_err);
2060 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002061}
2062
2063/*
2064 * Parse an <http-errors> section.
2065 * Returns the error code, 0 if OK, or any combination of :
2066 * - ERR_ABORT: must abort ASAP
2067 * - ERR_FATAL: we can continue parsing but not start the service
2068 * - ERR_WARN: a warning has been emitted
2069 * - ERR_ALERT: an alert has been emitted
2070 * Only the two first ones can stop processing, the two others are just
2071 * indicators.
2072 */
2073static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2074{
2075 static struct http_errors *curr_errs = NULL;
2076 int err_code = 0;
2077 const char *err;
2078 char *errmsg = NULL;
2079
2080 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2081 if (!*args[1]) {
2082 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2083 err_code |= ERR_ALERT | ERR_ABORT;
2084 goto out;
2085 }
2086
2087 err = invalid_char(args[1]);
2088 if (err) {
2089 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2090 file, linenum, *err, args[0], args[1]);
2091 err_code |= ERR_ALERT | ERR_FATAL;
2092 }
2093
2094 list_for_each_entry(curr_errs, &http_errors_list, list) {
2095 /* Error if two errors section owns the same name */
2096 if (strcmp(curr_errs->id, args[1]) == 0) {
2097 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2098 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2099 err_code |= ERR_ALERT | ERR_FATAL;
2100 }
2101 }
2102
2103 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2104 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2105 err_code |= ERR_ALERT | ERR_ABORT;
2106 goto out;
2107 }
2108
2109 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2110 curr_errs->id = strdup(args[1]);
2111 curr_errs->conf.file = strdup(file);
2112 curr_errs->conf.line = linenum;
2113 }
2114 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002115 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002116 struct buffer *msg;
2117 int status, rc;
2118
2119 if (*(args[1]) == 0 || *(args[2]) == 0) {
2120 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2121 file, linenum, args[0]);
2122 err_code |= ERR_ALERT | ERR_FATAL;
2123 goto out;
2124 }
2125
2126 status = atol(args[1]);
2127 msg = http_parse_errorfile(status, args[2], &errmsg);
2128 if (!msg) {
2129 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2130 err_code |= ERR_ALERT | ERR_FATAL;
2131 goto out;
2132 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002133
2134 reply = calloc(1, sizeof(*reply));
2135 if (!reply) {
2136 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2137 err_code |= ERR_ALERT | ERR_FATAL;
2138 goto out;
2139 }
2140 reply->type = HTTP_REPLY_ERRMSG;
2141 reply->status = status;
2142 reply->ctype = NULL;
2143 LIST_INIT(&reply->hdrs);
2144 reply->body.errmsg = msg;
2145
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002146 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002147 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002148 }
2149 else if (*args[0] != 0) {
2150 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
2153 }
2154
2155out:
2156 free(errmsg);
2157 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002158}
2159
2160static struct cfg_kw_list cfg_kws = {ILH, {
2161 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2162 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2163 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2164 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002165 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002166 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002167 { 0, NULL, NULL },
2168}};
2169
2170INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002171REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002172REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002173
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002174REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2175
Christopher Faulet29f72842019-12-11 15:52:32 +01002176/************************************************************************/
2177/* HTX sample fetches */
2178/************************************************************************/
2179
2180/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2181static int
2182smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2183{
2184 if (!smp->strm)
2185 return 0;
2186
2187 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2188 smp->data.type = SMP_T_BOOL;
2189 return 1;
2190}
2191
2192/* Returns the number of blocks in an HTX message. The channel is chosen
2193 * depending on the sample direction. */
2194static int
2195smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2196{
2197 struct channel *chn;
2198 struct htx *htx;
2199
2200 if (!smp->strm)
2201 return 0;
2202
2203 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002204 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002205 if (!htx)
2206 return 0;
2207
2208 smp->data.u.sint = htx_nbblks(htx);
2209 smp->data.type = SMP_T_SINT;
2210 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2211 return 1;
2212}
2213
2214/* Returns the size of an HTX message. The channel is chosen depending on the
2215 * sample direction. */
2216static int
2217smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2218{
2219 struct channel *chn;
2220 struct htx *htx;
2221
2222 if (!smp->strm)
2223 return 0;
2224
2225 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002226 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002227 if (!htx)
2228 return 0;
2229
2230 smp->data.u.sint = htx->size;
2231 smp->data.type = SMP_T_SINT;
2232 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2233 return 1;
2234}
2235
2236/* Returns the data size of an HTX message. The channel is chosen depending on the
2237 * sample direction. */
2238static int
2239smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2240{
2241 struct channel *chn;
2242 struct htx *htx;
2243
2244 if (!smp->strm)
2245 return 0;
2246
2247 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002248 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002249 if (!htx)
2250 return 0;
2251
2252 smp->data.u.sint = htx->data;
2253 smp->data.type = SMP_T_SINT;
2254 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2255 return 1;
2256}
2257
2258/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2259 * depending on the sample direction. */
2260static int
2261smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2262{
2263 struct channel *chn;
2264 struct htx *htx;
2265
2266 if (!smp->strm)
2267 return 0;
2268
2269 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002270 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002271 if (!htx)
2272 return 0;
2273
2274 smp->data.u.sint = htx_used_space(htx);
2275 smp->data.type = SMP_T_SINT;
2276 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2277 return 1;
2278}
2279
2280/* Returns the free space (size-used) of an HTX message. The channel is chosen
2281 * depending on the sample direction. */
2282static int
2283smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2284{
2285 struct channel *chn;
2286 struct htx *htx;
2287
2288 if (!smp->strm)
2289 return 0;
2290
2291 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002292 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002293 if (!htx)
2294 return 0;
2295
2296 smp->data.u.sint = htx_free_space(htx);
2297 smp->data.type = SMP_T_SINT;
2298 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2299 return 1;
2300}
2301
2302/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2303 * channel is chosen depending on the sample direction. */
2304static int
2305smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2306{
2307 struct channel *chn;
2308 struct htx *htx;
2309
2310 if (!smp->strm)
2311 return 0;
2312
2313 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002314 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002315 if (!htx)
2316 return 0;
2317
2318 smp->data.u.sint = htx_free_data_space(htx);
2319 smp->data.type = SMP_T_SINT;
2320 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2321 return 1;
2322}
2323
2324/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2325 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2326 * the sample direction. */
2327static int
2328smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2329{
2330 struct channel *chn;
2331 struct htx *htx;
2332
2333 if (!smp->strm)
2334 return 0;
2335
2336 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002337 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002338 if (!htx)
2339 return 0;
2340
2341 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2342 smp->data.type = SMP_T_BOOL;
2343 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2344 return 1;
2345}
2346
2347/* Returns the type of a specific HTX block, if found in the message. Otherwise
2348 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2349 * "head", "tail" or "first". The channel is chosen depending on the sample
2350 * direction. */
2351static int
2352smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2353{
2354 struct channel *chn;
2355 struct htx *htx;
2356 enum htx_blk_type type;
2357 int32_t pos;
2358
2359 if (!smp->strm || !arg_p)
2360 return 0;
2361
2362 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002363 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002364 if (!htx)
2365 return 0;
2366
2367 pos = arg_p[0].data.sint;
2368 if (pos == -1)
2369 type = htx_get_head_type(htx);
2370 else if (pos == -2)
2371 type = htx_get_tail_type(htx);
2372 else if (pos == -3)
2373 type = htx_get_first_type(htx);
2374 else
2375 type = ((pos >= htx->head && pos <= htx->tail)
2376 ? htx_get_blk_type(htx_get_blk(htx, pos))
2377 : HTX_BLK_UNUSED);
2378
2379 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2380 smp->data.type = SMP_T_STR;
2381 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2382 return 1;
2383}
2384
2385/* Returns the size of a specific HTX block, if found in the message. Otherwise
2386 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2387 * "first". The channel is chosen depending on the sample direction. */
2388static int
2389smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2390{
2391 struct channel *chn;
2392 struct htx *htx;
2393 struct htx_blk *blk;
2394 int32_t pos;
2395
2396 if (!smp->strm || !arg_p)
2397 return 0;
2398
2399 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002400 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002401 if (!htx)
2402 return 0;
2403
2404 pos = arg_p[0].data.sint;
2405 if (pos == -1)
2406 blk = htx_get_head_blk(htx);
2407 else if (pos == -2)
2408 blk = htx_get_tail_blk(htx);
2409 else if (pos == -3)
2410 blk = htx_get_first_blk(htx);
2411 else
2412 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2413
2414 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2415 smp->data.type = SMP_T_SINT;
2416 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2417 return 1;
2418}
2419
2420/* Returns the start-line if the selected HTX block exists and is a
2421 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2422 * supported or "head", "tail" or "first". The channel is chosen depending on
2423 * the sample direction. */
2424static int
2425smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2426{
2427 struct buffer *temp;
2428 struct channel *chn;
2429 struct htx *htx;
2430 struct htx_blk *blk;
2431 struct htx_sl *sl;
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 blk = htx_get_head_blk(htx);
2445 else if (pos == -2)
2446 blk = htx_get_tail_blk(htx);
2447 else if (pos == -3)
2448 blk = htx_get_first_blk(htx);
2449 else
2450 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2451
2452 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2453 smp->data.u.str.size = 0;
2454 smp->data.u.str.area = "";
2455 smp->data.u.str.data = 0;
2456 }
2457 else {
2458 sl = htx_get_blk_ptr(htx, blk);
2459
2460 temp = get_trash_chunk();
2461 chunk_istcat(temp, htx_sl_p1(sl));
2462 temp->area[temp->data++] = ' ';
2463 chunk_istcat(temp, htx_sl_p2(sl));
2464 temp->area[temp->data++] = ' ';
2465 chunk_istcat(temp, htx_sl_p3(sl));
2466
2467 smp->data.u.str = *temp;
2468 }
2469
2470 smp->data.type = SMP_T_STR;
2471 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2472 return 1;
2473}
2474
2475/* Returns the header name if the selected HTX block exists and is a header or a
2476 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2477 * supported or "head", "tail" or "first". The channel is chosen depending on
2478 * the sample direction. */
2479static int
2480smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2481{
2482 struct channel *chn;
2483 struct htx *htx;
2484 struct htx_blk *blk;
2485 int32_t pos;
2486
2487 if (!smp->strm || !arg_p)
2488 return 0;
2489
2490 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002491 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002492 if (!htx)
2493 return 0;
2494
2495 pos = arg_p[0].data.sint;
2496 if (pos == -1)
2497 blk = htx_get_head_blk(htx);
2498 else if (pos == -2)
2499 blk = htx_get_tail_blk(htx);
2500 else if (pos == -3)
2501 blk = htx_get_first_blk(htx);
2502 else
2503 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2504
2505 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2506 smp->data.u.str.size = 0;
2507 smp->data.u.str.area = "";
2508 smp->data.u.str.data = 0;
2509 }
2510 else {
2511 struct ist name = htx_get_blk_name(htx, blk);
2512
2513 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2514 }
2515 smp->data.type = SMP_T_STR;
2516 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2517 return 1;
2518}
2519
2520/* Returns the header value if the selected HTX block exists and is a header or
2521 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2522 * supported or "head", "tail" or "first". The channel is chosen depending on
2523 * the sample direction. */
2524static int
2525smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2526{
2527 struct channel *chn;
2528 struct htx *htx;
2529 struct htx_blk *blk;
2530 int32_t pos;
2531
2532 if (!smp->strm || !arg_p)
2533 return 0;
2534
2535 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002536 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002537 if (!htx)
2538 return 0;
2539
2540 pos = arg_p[0].data.sint;
2541 if (pos == -1)
2542 blk = htx_get_head_blk(htx);
2543 else if (pos == -2)
2544 blk = htx_get_tail_blk(htx);
2545 else if (pos == -3)
2546 blk = htx_get_first_blk(htx);
2547 else
2548 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2549
2550 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2551 smp->data.u.str.size = 0;
2552 smp->data.u.str.area = "";
2553 smp->data.u.str.data = 0;
2554 }
2555 else {
2556 struct ist val = htx_get_blk_value(htx, blk);
2557
2558 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2559 }
2560 smp->data.type = SMP_T_STR;
2561 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2562 return 1;
2563}
2564
2565/* Returns the value if the selected HTX block exists and is a data
2566 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2567 * or "head", "tail" or "first". The channel is chosen depending on the sample
2568 * direction. */
2569static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002570smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002571{
2572 struct channel *chn;
2573 struct htx *htx;
2574 struct htx_blk *blk;
2575 int32_t pos;
2576
2577 if (!smp->strm || !arg_p)
2578 return 0;
2579
2580 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002581 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002582 if (!htx)
2583 return 0;
2584
2585 pos = arg_p[0].data.sint;
2586 if (pos == -1)
2587 blk = htx_get_head_blk(htx);
2588 else if (pos == -2)
2589 blk = htx_get_tail_blk(htx);
2590 else if (pos == -3)
2591 blk = htx_get_first_blk(htx);
2592 else
2593 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2594
2595 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2596 smp->data.u.str.size = 0;
2597 smp->data.u.str.area = "";
2598 smp->data.u.str.data = 0;
2599 }
2600 else {
2601 struct ist val = htx_get_blk_value(htx, blk);
2602
2603 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2604 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002605 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002606 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2607 return 1;
2608}
2609
2610/* This function is used to validate the arguments passed to any "htx_blk" fetch
2611 * keywords. An argument is expected by these keywords. It must be a positive
2612 * integer or on of the following strings: "head", "tail" or "first". It returns
2613 * 0 on error, and a non-zero value if OK.
2614 */
2615int val_blk_arg(struct arg *arg, char **err_msg)
2616{
2617 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2618 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2619 return 0;
2620 }
2621 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2622 free(arg[0].data.str.area);
2623 arg[0].type = ARGT_SINT;
2624 arg[0].data.sint = -1;
2625 }
2626 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2627 free(arg[0].data.str.area);
2628 arg[0].type = ARGT_SINT;
2629 arg[0].data.sint = -2;
2630 }
2631 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2632 free(arg[0].data.str.area);
2633 arg[0].type = ARGT_SINT;
2634 arg[0].data.sint = -3;
2635 }
2636 else {
2637 int pos;
2638
2639 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002640 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002641 memprintf(err_msg, "invalid block position");
2642 return 0;
2643 }
2644 }
2645
2646 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2647 if (pos < 0) {
2648 memprintf(err_msg, "block position must not be negative");
2649 return 0;
2650 }
2651 free(arg[0].data.str.area);
2652 arg[0].type = ARGT_SINT;
2653 arg[0].data.sint = pos;
2654 }
2655
2656 return 1;
2657}
2658
2659
2660/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002661 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002662 */
2663static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002664 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002665
Christopher Faulet01f44452020-01-08 14:23:40 +01002666 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2667 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2668 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2669 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2670 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2671 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2672 { "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 +01002673
Christopher Faulet01f44452020-01-08 14:23:40 +01002674 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2675 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2676 { "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},
2677 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2678 { "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 +01002679 { "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 +01002680
2681 { /* END */ },
2682}};
2683
2684INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);