blob: 3015320ec46093e18ff36da2015cb35ab7f91e4b [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
17#include <types/global.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020018
19#include <common/config.h>
Christopher Faulet29f17582019-05-23 11:03:26 +020020#include <common/debug.h>
Christopher Fauleta7b677c2018-11-29 16:48:49 +010021#include <common/cfgparse.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010022#include <common/h1.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020023#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010024#include <common/htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020025
Christopher Faulet29f72842019-12-11 15:52:32 +010026#include <proto/arg.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020027#include <proto/http_htx.h>
Christopher Faulet29f72842019-12-11 15:52:32 +010028#include <proto/http_fetch.h>
29#include <proto/sample.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Fauletf7346382019-07-17 22:02:08 +020031struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet58857752020-01-15 15:19:50 +010032struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010033struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010034
Christopher Faulet76edc0f2020-01-13 15:52:01 +010035/* The declaration of an errorfiles/errorfile directives. Used during config
36 * parsing only. */
37struct conf_errors {
38 char type; /* directive type (0: errorfiles, 1: errorfile) */
39 union {
40 struct {
41 int status; /* the status code associated to this error */
42 struct buffer *msg; /* the HTX error message */
43 } errorfile; /* describe an "errorfile" directive */
44 struct {
45 char *name; /* the http-errors section name */
46 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
47 } errorfiles; /* describe an "errorfiles" directive */
48 } info;
49
50 char *file; /* file where the directive appears */
51 int line; /* line where the directive appears */
52
53 struct list list; /* next conf_errors */
54};
55
Christopher Faulet297fbb42019-05-13 14:41:27 +020056/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020057 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020058 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020059 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020060struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020061{
Christopher Faulet297fbb42019-05-13 14:41:27 +020062 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010063
Christopher Faulet29f17582019-05-23 11:03:26 +020064 BUG_ON(htx->first == -1);
65 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020066 if (!blk)
67 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020068 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 +020069 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020070}
71
Christopher Faulet727a3f12020-02-07 16:39:41 +010072/* Returns the headers size in the HTX message */
73size_t http_get_hdrs_size(struct htx *htx)
74{
75 struct htx_blk *blk;
76 size_t sz = 0;
77
78 blk = htx_get_first_blk(htx);
79 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
80 return sz;
81
82 for (; blk; blk = htx_get_next_blk(htx, blk)) {
83 sz += htx_get_blksz(blk);
84 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
85 break;
86 }
87 return sz;
88}
89
Christopher Faulet8dd33e12020-05-05 07:42:42 +020090/* Finds the first or next occurrence of header matching <pattern> in the HTX
91 * message <htx> using the context <ctx>. This structure holds everything
92 * necessary to use the header and find next occurrence. If its <blk> member is
93 * NULL, the header is searched from the beginning. Otherwise, the next
94 * occurrence is returned. The function returns 1 when it finds a value, and 0
95 * when there is no more. It is designed to work with headers defined as
96 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
97 * full-line headers in whose comma is not a delimiter but is part of the
98 * syntax. A special case, if ctx->value is NULL when searching for a new values
99 * of a header, the current header is rescanned. This allows rescanning after a
100 * header deletion.
101 *
102 * The matching method is chosen by checking the flags :
103 *
104 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
105 * the regex are evaluated.
106 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
107 * to the string are evaluated.
108 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
109 * starting by the string are evaluated.
110 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
111 * ending by the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
113 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200114 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200115
116#define HTTP_FIND_FL_MATCH_STR 0x0001
117#define HTTP_FIND_FL_MATCH_PFX 0x0002
118#define HTTP_FIND_FL_MATCH_SFX 0x0003
119#define HTTP_FIND_FL_MATCH_SUB 0x0004
120#define HTTP_FIND_FL_MATCH_REG 0x0005
121/* 0x0006..0x000f: for other matching methods */
122#define HTTP_FIND_FL_MATCH_TYPE 0x000F
123#define HTTP_FIND_FL_FULL 0x0010
124
125static 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 +0200126{
127 struct htx_blk *blk = ctx->blk;
128 struct ist n, v;
129 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200130
131 if (blk) {
132 char *p;
133
Tim Duesterhused526372020-03-05 17:56:33 +0100134 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200135 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200136 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200137 goto next_blk;
138 v = htx_get_blk_value(htx, blk);
139 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
140 v.len -= (p - v.ptr);
141 v.ptr = p;
142 if (!v.len)
143 goto next_blk;
144 /* Skip comma */
145 if (*(v.ptr) == ',') {
146 v.ptr++;
147 v.len--;
148 }
149
150 goto return_hdr;
151 }
152
Christopher Faulet192c6a22019-06-11 16:32:24 +0200153 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200154 return 0;
155
Christopher Fauleta3f15502019-05-13 15:27:23 +0200156 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200158 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100159 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
160 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200162 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200163
164 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
165 const struct my_regex *re = pattern;
166
167 n = htx_get_blk_name(htx, blk);
168 if (!regex_exec2(re, n.ptr, n.len))
169 goto next_blk;
170 }
171 else {
172 const struct ist name = *(const struct ist *)(pattern);
173
Christopher Faulet47596d32018-10-22 09:17:28 +0200174 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200175 if (!istlen(name))
176 goto match;
177
Christopher Faulet47596d32018-10-22 09:17:28 +0200178 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200179 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
180 case HTTP_FIND_FL_MATCH_STR:
181 if (!isteqi(n, name))
182 goto next_blk;
183 break;
184 case HTTP_FIND_FL_MATCH_PFX:
185 if (istlen(n) < istlen(name))
186 goto next_blk;
187
188 n = ist2(istptr(n), istlen(name));
189 if (!isteqi(n, name))
190 goto next_blk;
191 break;
192 case HTTP_FIND_FL_MATCH_SFX:
193 if (istlen(n) < istlen(name))
194 goto next_blk;
195
196 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
197 if (!isteqi(n, name))
198 goto next_blk;
199 break;
200 case HTTP_FIND_FL_MATCH_SUB:
201 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
202 goto next_blk;
203 break;
204 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200205 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200206 break;
207 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200210 v = htx_get_blk_value(htx, blk);
211
212 return_hdr:
213 ctx->lws_before = 0;
214 ctx->lws_after = 0;
215 while (v.len && HTTP_IS_LWS(*v.ptr)) {
216 v.ptr++;
217 v.len--;
218 ctx->lws_before++;
219 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200220 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200221 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
222 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
223 v.len--;
224 ctx->lws_after++;
225 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200226 ctx->blk = blk;
227 ctx->value = v;
228 return 1;
229
230 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200231 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200232 }
233
234 ctx->blk = NULL;
235 ctx->value = ist("");
236 ctx->lws_before = ctx->lws_after = 0;
237 return 0;
238}
239
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200240
241/* Header names must match <name> */
242int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
243{
244 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
245}
246
247/* Header names must match <name>. Same than http_find_header */
248int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
249{
250 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
251}
252
253
254/* Header names must start with <prefix> */
255int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
256{
257 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
258}
259
260/* Header names must end with <suffix> */
261int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
262{
263 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
264}
265/* Header names must contain <sub> */
266int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
267{
268 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
269}
270
271/* Header names must match <re> regex*/
272int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
273{
274 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
275}
276
277
Christopher Faulet47596d32018-10-22 09:17:28 +0200278/* Adds a header block int the HTX message <htx>, just before the EOH block. It
279 * returns 1 on success, otherwise it returns 0.
280 */
281int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
282{
283 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200284 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200285 enum htx_blk_type type = htx_get_tail_type(htx);
286 int32_t prev;
287
288 blk = htx_add_header(htx, n, v);
289 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200290 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200291
292 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 /* <blk> is the head, swap it iteratively with its predecessor to place
296 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200297 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200298 struct htx_blk *pblk = htx_get_blk(htx, prev);
299 enum htx_blk_type type = htx_get_blk_type(pblk);
300
301 /* Swap .addr and .info fields */
302 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
303 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
304
305 if (blk->addr == pblk->addr)
306 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200307
308 /* Stop when end-of-header is reached */
309 if (type == HTX_BLK_EOH)
310 break;
311
312 blk = pblk;
313 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200314
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200315 end:
316 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100317 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 if (!http_update_authority(htx, sl, v))
319 goto fail;
320 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200321 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200322
323 fail:
324 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200325}
326
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100327/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200328 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200329 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330int 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 +0200331{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200332 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200333
Christopher Faulet29f17582019-05-23 11:03:26 +0200334 blk = htx_get_first_blk(htx);
335 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200336 return 0;
337 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200338}
339
Christopher Faulete010c802018-10-24 10:36:45 +0200340/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
341 * on success, otherwise 0.
342 */
343int http_replace_req_meth(struct htx *htx, const struct ist meth)
344{
345 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200346 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100347 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200348
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100349 if (!sl)
350 return 0;
351
Christopher Faulete010c802018-10-24 10:36:45 +0200352 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100353 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
354 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200355
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
357 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 /* create the new start line */
360 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
361 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200362}
363
364/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
365 * success, otherwise 0.
366 */
367int http_replace_req_uri(struct htx *htx, const struct ist uri)
368{
369 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200370 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100371 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200372
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100373 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200374 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100375
Christopher Faulete010c802018-10-24 10:36:45 +0200376 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100377 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
378 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200379
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
381 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200384 if (!http_replace_stline(htx, meth, uri, vsn))
385 goto fail;
386
387 sl = http_get_stline(htx);
388 if (!http_update_host(htx, sl, uri))
389 goto fail;
390
391 return 1;
392 fail:
393 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200394}
395
396/* Replace the request path in the HTX message <htx> by <path>. The host part
397 * and the query string are preserved. It returns 1 on success, otherwise 0.
398 */
399int http_replace_req_path(struct htx *htx, const struct ist path)
400{
401 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200402 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100403 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200404 size_t plen = 0;
405
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100406 if (!sl)
407 return 0;
408
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100409 uri = htx_sl_req_uri(sl);
410 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100411 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100412 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200413 while (plen < p.len && *(p.ptr + plen) != '?')
414 plen++;
415
416 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100417 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
418 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200419
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100420 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
421 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
422
423 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200424 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
425 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100426 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200427
428 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100429 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200430}
431
432/* Replace the request query-string in the HTX message <htx> by <query>. The
433 * host part and the path are preserved. It returns 1 on success, otherwise
434 * 0.
435 */
436int http_replace_req_query(struct htx *htx, const struct ist query)
437{
438 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200439 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100440 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200441 int offset = 1;
442
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100443 if (!sl)
444 return 0;
445
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100446 uri = htx_sl_req_uri(sl);
447 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200448 while (q.len > 0 && *(q.ptr) != '?') {
449 q.ptr++;
450 q.len--;
451 }
452
453 /* skip the question mark or indicate that we must insert it
454 * (but only if the format string is not empty then).
455 */
456 if (q.len) {
457 q.ptr++;
458 q.len--;
459 }
460 else if (query.len > 1)
461 offset = 0;
462
463 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100464 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
465 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200466
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100467 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
468 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200469
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100470 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
471 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
472 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200473
474 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200476}
477
478/* Replace the response status in the HTX message <htx> by <status>. It returns
479 * 1 on success, otherwise 0.
480*/
481int http_replace_res_status(struct htx *htx, const struct ist status)
482{
483 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200484 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100485 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200486
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100487 if (!sl)
488 return 0;
489
Christopher Faulete010c802018-10-24 10:36:45 +0200490 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100491 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
492 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200493
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100494 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
495 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200496
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100497 /* create the new start line */
498 sl->info.res.status = strl2ui(status.ptr, status.len);
499 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200500}
501
502/* Replace the response reason in the HTX message <htx> by <reason>. It returns
503 * 1 on success, otherwise 0.
504*/
505int http_replace_res_reason(struct htx *htx, const struct ist reason)
506{
507 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200508 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100509 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200510
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100511 if (!sl)
512 return 0;
513
Christopher Faulete010c802018-10-24 10:36:45 +0200514 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100515 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
516 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200517
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
519 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200520
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100521 /* create the new start line */
522 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200523}
524
Christopher Faulet47596d32018-10-22 09:17:28 +0200525/* Replaces a part of a header value referenced in the context <ctx> by
526 * <data>. It returns 1 on success, otherwise it returns 0. The context is
527 * updated if necessary.
528 */
529int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
530{
531 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200532 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200533 char *start;
534 struct ist v;
535 uint32_t len, off;
536
537 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200538 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200539
540 v = htx_get_blk_value(htx, blk);
541 start = ctx->value.ptr - ctx->lws_before;
542 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
543 off = start - v.ptr;
544
545 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
546 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200547 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200548
549 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200550
551 sl = http_get_stline(htx);
552 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
553 struct ist n = htx_get_blk_name(htx, blk);
554
555 if (isteq(n, ist("host"))) {
556 if (!http_update_authority(htx, sl, v))
557 goto fail;
558 ctx->blk = NULL;
559 http_find_header(htx, ist("host"), ctx, 1);
560 blk = ctx->blk;
561 v = htx_get_blk_value(htx, blk);
562 }
563 }
564
Christopher Faulet47596d32018-10-22 09:17:28 +0200565 ctx->blk = blk;
566 ctx->value.ptr = v.ptr + off;
567 ctx->value.len = data.len;
568 ctx->lws_before = ctx->lws_after = 0;
569
570 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200571 fail:
572 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200573}
574
575/* Fully replaces a header referenced in the context <ctx> by the name <name>
576 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
577 * context is updated if necessary.
578 */
579int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
580 const struct ist name, const struct ist value)
581{
582 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200583 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200584
585 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200586 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200587
588 blk = htx_replace_header(htx, blk, name, value);
589 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200590 goto fail;
591
592 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100593 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200594 if (!http_update_authority(htx, sl, value))
595 goto fail;
596 ctx->blk = NULL;
597 http_find_header(htx, ist("host"), ctx, 1);
598 blk = ctx->blk;
599 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200600
601 ctx->blk = blk;
602 ctx->value = ist(NULL);
603 ctx->lws_before = ctx->lws_after = 0;
604
605 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200606 fail:
607 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200608}
609
610/* Remove one value of a header. This only works on a <ctx> returned by
611 * http_find_header function. The value is removed, as well as surrounding commas
612 * if any. If the removed value was alone, the whole header is removed. The
613 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
614 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
615 * form that can be handled by http_find_header() to find next occurrence.
616 */
617int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
618{
619 struct htx_blk *blk = ctx->blk;
620 char *start;
621 struct ist v;
622 uint32_t len;
623
624 if (!blk)
625 return 0;
626
627 start = ctx->value.ptr - ctx->lws_before;
628 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
629
630 v = htx_get_blk_value(htx, blk);
631 if (len == v.len) {
632 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200633 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200634 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100635 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200636 ctx->lws_before = ctx->lws_after = 0;
637 }
638 else {
639 ctx->blk = htx_get_blk(htx, htx->tail);
640 ctx->value = htx_get_blk_value(htx, ctx->blk);
641 ctx->lws_before = ctx->lws_after = 0;
642 }
643 return 1;
644 }
645
646 /* This was not the only value of this header. We have to remove the
647 * part pointed by ctx->value. If it is the last entry of the list, we
648 * remove the last separator.
649 */
650 if (start == v.ptr) {
651 /* It's the first header part but not the only one. So remove
652 * the comma after it. */
653 len++;
654 }
655 else {
656 /* There is at least one header part before the removed one. So
657 * remove the comma between them. */
658 start--;
659 len++;
660 }
661 /* Update the block content and its len */
662 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200663 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200664
665 /* Finally update the ctx */
666 ctx->value.ptr = start;
667 ctx->value.len = 0;
668 ctx->lws_before = ctx->lws_after = 0;
669
670 return 1;
671}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200672
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200673/* Updates the authority part of the uri with the value <host>. It happens when
674 * the header host is modified. It returns 0 on failure and 1 on success. It is
675 * the caller responsibility to provide the start-line and to be sure the uri
676 * contains an authority. Thus, if no authority is found in the uri, an error is
677 * returned.
678 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200679int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200680{
681 struct buffer *temp = get_trash_chunk();
682 struct ist meth, vsn, uri, authority;
683
684 uri = htx_sl_req_uri(sl);
685 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100686 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200687 return 0;
688
Christopher Faulet34b18e42020-02-18 11:02:21 +0100689 /* Don't update the uri if there is no change */
690 if (isteq(host, authority))
691 return 1;
692
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200693 /* Start by copying old method and version */
694 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
695 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
696
697 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
698 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
699
700 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
701 chunk_memcat(temp, host.ptr, host.len);
702 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
703 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
704
705 return http_replace_stline(htx, meth, uri, vsn);
706
707}
708
709/* Update the header host by extracting the authority of the uri <uri>. flags of
710 * the start-line are also updated accordingly. For orgin-form and asterisk-form
711 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
712 * removed from the flags of the start-line. Otherwise, this flag is set and the
713 * authority is used to set the value of the header host. This function returns
714 * 0 on failure and 1 on success.
715*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200716int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200717{
718 struct ist authority;
719 struct http_hdr_ctx ctx;
720
721 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
722 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
723 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
724 }
725 else {
726 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
727 if (sl->info.req.meth != HTTP_METH_CONNECT) {
728 // absolute-form (RFC7320 #5.3.2)
729 sl->flags |= HTX_SL_F_HAS_SCHM;
730 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
731 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
732
733 authority = http_get_authority(uri, 1);
734 if (!authority.len)
735 goto fail;
736 }
737 else {
738 // authority-form (RFC7320 #5.3.3)
739 authority = uri;
740 }
741
742 /* Replace header host value */
743 ctx.blk = NULL;
744 while (http_find_header(htx, ist("host"), &ctx, 1)) {
745 if (!http_replace_header_value(htx, &ctx, authority))
746 goto fail;
747 }
748
749 }
750 return 1;
751 fail:
752 return 0;
753}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200754
755/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
756 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
757 * performed over the whole headers. Otherwise it must contain a valid header
758 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
759 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
760 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
761 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
762 * -1. The value fetch stops at commas, so this function is suited for use with
763 * list headers.
764 * The return value is 0 if nothing was found, or non-zero otherwise.
765 */
766unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
767 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
768{
769 struct http_hdr_ctx local_ctx;
770 struct ist val_hist[MAX_HDR_HISTORY];
771 unsigned int hist_idx;
772 int found;
773
774 if (!ctx) {
775 local_ctx.blk = NULL;
776 ctx = &local_ctx;
777 }
778
779 if (occ >= 0) {
780 /* search from the beginning */
781 while (http_find_header(htx, hdr, ctx, 0)) {
782 occ--;
783 if (occ <= 0) {
784 *vptr = ctx->value.ptr;
785 *vlen = ctx->value.len;
786 return 1;
787 }
788 }
789 return 0;
790 }
791
792 /* negative occurrence, we scan all the list then walk back */
793 if (-occ > MAX_HDR_HISTORY)
794 return 0;
795
796 found = hist_idx = 0;
797 while (http_find_header(htx, hdr, ctx, 0)) {
798 val_hist[hist_idx] = ctx->value;
799 if (++hist_idx >= MAX_HDR_HISTORY)
800 hist_idx = 0;
801 found++;
802 }
803 if (-occ > found)
804 return 0;
805
806 /* OK now we have the last occurrence in [hist_idx-1], and we need to
807 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
808 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
809 * to remain in the 0..9 range.
810 */
811 hist_idx += occ + MAX_HDR_HISTORY;
812 if (hist_idx >= MAX_HDR_HISTORY)
813 hist_idx -= MAX_HDR_HISTORY;
814 *vptr = val_hist[hist_idx].ptr;
815 *vlen = val_hist[hist_idx].len;
816 return 1;
817}
818
819/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
820 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
821 * performed over the whole headers. Otherwise it must contain a valid header
822 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
823 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
824 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
825 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
826 * -1. This function differs from http_get_hdr() in that it only returns full
827 * line header values and does not stop at commas.
828 * The return value is 0 if nothing was found, or non-zero otherwise.
829 */
830unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
831 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
832{
833 struct http_hdr_ctx local_ctx;
834 struct ist val_hist[MAX_HDR_HISTORY];
835 unsigned int hist_idx;
836 int found;
837
838 if (!ctx) {
839 local_ctx.blk = NULL;
840 ctx = &local_ctx;
841 }
842
843 if (occ >= 0) {
844 /* search from the beginning */
845 while (http_find_header(htx, hdr, ctx, 1)) {
846 occ--;
847 if (occ <= 0) {
848 *vptr = ctx->value.ptr;
849 *vlen = ctx->value.len;
850 return 1;
851 }
852 }
853 return 0;
854 }
855
856 /* negative occurrence, we scan all the list then walk back */
857 if (-occ > MAX_HDR_HISTORY)
858 return 0;
859
860 found = hist_idx = 0;
861 while (http_find_header(htx, hdr, ctx, 1)) {
862 val_hist[hist_idx] = ctx->value;
863 if (++hist_idx >= MAX_HDR_HISTORY)
864 hist_idx = 0;
865 found++;
866 }
867 if (-occ > found)
868 return 0;
869
870 /* OK now we have the last occurrence in [hist_idx-1], and we need to
871 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
872 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
873 * to remain in the 0..9 range.
874 */
875 hist_idx += occ + MAX_HDR_HISTORY;
876 if (hist_idx >= MAX_HDR_HISTORY)
877 hist_idx -= MAX_HDR_HISTORY;
878 *vptr = val_hist[hist_idx].ptr;
879 *vlen = val_hist[hist_idx].len;
880 return 1;
881}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100882
Christopher Faulet90cc4812019-07-22 16:49:30 +0200883int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100884{
885 struct htx *htx;
886 struct htx_sl *sl;
887 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200888 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100889 union h1_sl h1sl;
890 unsigned int flags = HTX_SL_F_IS_RESP;
891 int ret = 0;
892
Christopher Faulet90cc4812019-07-22 16:49:30 +0200893 b_reset(buf);
894 if (!raw.len) {
895 buf->size = 0;
896 buf->area = malloc(raw.len);
897 return 1;
898 }
899
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100900 buf->size = global.tune.bufsize;
901 buf->area = (char *)malloc(buf->size);
902 if (!buf->area)
903 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100904
905 h1m_init_res(&h1m);
906 h1m.flags |= H1_MF_NO_PHDR;
907 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
908 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
909 if (ret <= 0)
910 goto error;
911
912 if (unlikely(h1sl.st.v.len != 8))
913 goto error;
914 if ((*(h1sl.st.v.ptr + 5) > '1') ||
915 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
916 h1m.flags |= H1_MF_VER_11;
917
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200918 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
919 goto error;
920
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100921 if (h1m.flags & H1_MF_VER_11)
922 flags |= HTX_SL_F_VER_11;
923 if (h1m.flags & H1_MF_XFER_ENC)
924 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200925 if (h1m.flags & H1_MF_CLEN) {
926 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
927 if (h1m.body_len == 0)
928 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100929 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200930 if (h1m.flags & H1_MF_CHNK)
931 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100932
933 htx = htx_from_buf(buf);
934 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
935 if (!sl || !htx_add_all_headers(htx, hdrs))
936 goto error;
937 sl->info.res.status = h1sl.st.status;
938
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200939 while (raw.len > ret) {
940 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
941 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100942 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200943 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100944 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200945
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100946 if (!htx_add_endof(htx, HTX_BLK_EOM))
947 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200948
Christopher Faulet90cc4812019-07-22 16:49:30 +0200949 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100950
951error:
952 if (buf->size)
953 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200954 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100955}
956
Christopher Faulet18630642020-05-12 18:57:28 +0200957void release_http_reply(struct http_reply *http_reply)
958{
959 struct logformat_node *lf, *lfb;
960 struct http_reply_hdr *hdr, *hdrb;
961
962 if (!http_reply)
963 return;
964
965 free(http_reply->ctype);
966 http_reply->ctype = NULL;
967 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
968 LIST_DEL(&hdr->list);
969 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
970 LIST_DEL(&lf->list);
971 release_sample_expr(lf->expr);
972 free(lf->arg);
973 free(lf);
974 }
975 istfree(&hdr->name);
976 free(hdr);
977 }
978
979 if (http_reply->type == HTTP_REPLY_ERRFILES) {
980 free(http_reply->body.http_errors);
981 http_reply->body.http_errors = NULL;
982 }
983 else if (http_reply->type == HTTP_REPLY_RAW)
984 chunk_destroy(&http_reply->body.obj);
985 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
986 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
987 LIST_DEL(&lf->list);
988 release_sample_expr(lf->expr);
989 free(lf->arg);
990 free(lf);
991 }
992 }
993}
994
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100995static int http_htx_init(void)
996{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100997 struct buffer chk;
998 struct ist raw;
999 int rc;
1000 int err_code = 0;
1001
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001002 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1003 if (!http_err_msgs[rc]) {
1004 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1005 err_code |= ERR_ALERT | ERR_FATAL;
1006 continue;
1007 }
1008
1009 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1010 if (!http_str_to_htx(&chk, raw)) {
1011 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1012 http_err_codes[rc]);
1013 err_code |= ERR_ALERT | ERR_FATAL;
1014 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001015 http_err_chunks[rc] = chk;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001016 }
1017end:
1018 return err_code;
1019}
1020
Christopher Faulet58857752020-01-15 15:19:50 +01001021static void http_htx_deinit(void)
1022{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001023 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +01001024 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001025 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001026
1027 node = ebpt_first(&http_error_messages);
1028 while (node) {
1029 next = ebpt_next(node);
1030 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001031 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1032 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001033 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001034 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001035 node = next;
1036 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001037
1038 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1039 free(http_errs->conf.file);
1040 free(http_errs->id);
1041 LIST_DEL(&http_errs->list);
1042 free(http_errs);
1043 }
Christopher Faulet58857752020-01-15 15:19:50 +01001044}
1045
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001046REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001047REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001048
Christopher Faulet58857752020-01-15 15:19:50 +01001049/* Reads content of the error file <file> and convert it into an HTX message. On
1050 * success, the HTX message is returned. On error, NULL is returned and an error
1051 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001052 */
Christopher Faulet58857752020-01-15 15:19:50 +01001053struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001054{
Christopher Faulet58857752020-01-15 15:19:50 +01001055 struct buffer *buf = NULL;
1056 struct buffer chk;
1057 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001058 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001059 struct stat stat;
1060 char *err = NULL;
1061 int errnum, errlen;
1062 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001063
1064 /* already loaded */
1065 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1066 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001067 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1068 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001069 goto out;
1070 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001071
Christopher Faulet58857752020-01-15 15:19:50 +01001072 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001073 fd = open(file, O_RDONLY);
1074 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1075 memprintf(errmsg, "error opening file '%s'.", file);
1076 goto out;
1077 }
1078
1079 if (stat.st_size <= global.tune.bufsize)
1080 errlen = stat.st_size;
1081 else {
1082 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1083 file, global.tune.bufsize);
1084 errlen = global.tune.bufsize;
1085 }
1086
1087 err = malloc(errlen);
1088 if (!err) {
1089 memprintf(errmsg, "out of memory.");
1090 goto out;
1091 }
1092
1093 errnum = read(fd, err, errlen);
1094 if (errnum != errlen) {
1095 memprintf(errmsg, "error reading file '%s'.", file);
1096 goto out;
1097 }
1098
Christopher Faulet58857752020-01-15 15:19:50 +01001099 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001100 http_errmsg = calloc(1, sizeof(*http_errmsg));
1101 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001102 memprintf(errmsg, "out of memory.");
1103 goto out;
1104 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001105 http_errmsg->node.key = strdup(file);
1106 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001107 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001108 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001109 goto out;
1110 }
1111
1112 /* Convert the error file into an HTX message */
1113 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001114 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001115 free(http_errmsg->node.key);
1116 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001117 goto out;
1118 }
1119
Christopher Faulet58857752020-01-15 15:19:50 +01001120 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001121 http_errmsg->msg = chk;
1122 ebis_insert(&http_error_messages, &http_errmsg->node);
1123 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001124
Christopher Faulet5031ef52020-01-15 11:22:07 +01001125 out:
1126 if (fd >= 0)
1127 close(fd);
1128 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001129 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001130}
1131
Ilya Shipitsind4259502020-04-08 01:07:56 +05001132/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001133 * message is returned. On error, NULL is returned and an error message is
1134 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001135 */
Christopher Faulet58857752020-01-15 15:19:50 +01001136struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001137{
Christopher Faulet58857752020-01-15 15:19:50 +01001138 struct buffer *buf = NULL;
1139 struct buffer chk;
1140 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001141 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001142
1143 /* already loaded */
1144 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1145 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001146 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1147 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001148 goto out;
1149 }
1150 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001151 http_errmsg = calloc(1, sizeof(*http_errmsg));
1152 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001153 memprintf(errmsg, "out of memory.");
1154 goto out;
1155 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001156 http_errmsg->node.key = strdup(key);
1157 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001158 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001159 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001160 goto out;
1161 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001162
1163 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001164 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001165 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001166 free(http_errmsg->node.key);
1167 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001168 goto out;
1169 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001170
Christopher Faulet58857752020-01-15 15:19:50 +01001171 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001172 http_errmsg->msg = chk;
1173 ebis_insert(&http_error_messages, &http_errmsg->node);
1174 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001175 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001176 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001177}
1178
Christopher Faulet5031ef52020-01-15 11:22:07 +01001179/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001180 * <status>. It returns NULL if there is any error, otherwise it return the
1181 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001182 */
Christopher Faulet58857752020-01-15 15:19:50 +01001183struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001184{
Christopher Faulet58857752020-01-15 15:19:50 +01001185 struct buffer *buf = NULL;
1186 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001187
1188 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1189 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001190 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001191 break;
1192 }
1193 }
1194
1195 if (rc >= HTTP_ERR_SIZE)
1196 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001197 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001198}
1199
1200/* This function creates HTX error message corresponding to a redirect message
1201 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001202 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1203 * returns NULL if there is any error, otherwise it return the corresponding HTX
1204 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001205 */
Christopher Faulet58857752020-01-15 15:19:50 +01001206struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001207{
Christopher Faulet58857752020-01-15 15:19:50 +01001208 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001209 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001210 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001211 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001212
1213 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1214 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001215 /* Create the error key */
1216 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1217 memprintf(errmsg, "out of memory.");
1218 goto out;
1219 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001220 /* Create the error message */
1221 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1222 errlen = strlen(msg) + strlen(url) + 5;
1223 err = malloc(errlen);
1224 if (!err) {
1225 memprintf(errmsg, "out of memory.");
1226 goto out;
1227 }
1228 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1229
1230 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001231 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001232 break;
1233 }
1234 }
1235
1236 if (rc >= HTTP_ERR_SIZE)
1237 memprintf(errmsg, "status code '%d' not handled.", status);
1238out:
Christopher Faulet58857752020-01-15 15:19:50 +01001239 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001240 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001241 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001242}
1243
Christopher Faulet47e791e2020-05-13 14:36:55 +02001244/* Parse an "http reply". It returns the reply on success or NULL on error. This
1245 * function creates one of the following http replies :
1246 *
1247 * - HTTP_REPLY_EMPTY : dummy response, no payload
1248 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1249 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1250 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1251 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1252 *
1253 * The content-type must be defined for non-empty payload. It is ignored for
1254 * error messages (implicit or explicit). When an http-errors section is
1255 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1256 * during the configuration validity check or dynamically. It is the caller
1257 * responsibility to choose. If no status code is configured, <default_status>
1258 * is set.
1259 */
1260struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1261 int default_status, char **errmsg)
1262{
1263 struct logformat_node *lf, *lfb;
1264 struct http_reply *reply = NULL;
1265 struct http_reply_hdr *hdr, *hdrb;
1266 struct stat stat;
1267 const char *act_arg = NULL;
1268 char *obj = NULL;
1269 int cur_arg, cap, objlen = 0, fd = -1;
1270
1271
1272 reply = calloc(1, sizeof(*reply));
1273 if (!reply) {
1274 memprintf(errmsg, "out of memory");
1275 goto error;
1276 }
1277 LIST_INIT(&reply->hdrs);
1278 reply->type = HTTP_REPLY_EMPTY;
1279 reply->status = default_status;
1280
1281 cap = ((px->conf.args.ctx == ARGC_HRQ)
1282 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1283 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
1284
1285 cur_arg = *orig_arg;
1286 while (*args[cur_arg]) {
1287 if (strcmp(args[cur_arg], "status") == 0) {
1288 cur_arg++;
1289 if (!*args[cur_arg]) {
1290 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1291 goto error;
1292 }
1293 reply->status = atol(args[cur_arg]);
1294 if (reply->status < 200 || reply->status > 599) {
1295 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1296 goto error;
1297 }
1298 cur_arg++;
1299 }
1300 else if (strcmp(args[cur_arg], "content-type") == 0) {
1301 cur_arg++;
1302 if (!*args[cur_arg]) {
1303 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1304 goto error;
1305 }
1306 free(reply->ctype);
1307 reply->ctype = strdup(args[cur_arg]);
1308 cur_arg++;
1309 }
1310 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1311 if (reply->type != HTTP_REPLY_EMPTY) {
1312 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1313 goto error;
1314 }
1315 act_arg = args[cur_arg];
1316 cur_arg++;
1317 if (!*args[cur_arg]) {
1318 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1319 goto error;
1320 }
1321 reply->body.http_errors = strdup(args[cur_arg]);
1322 if (!reply->body.http_errors) {
1323 memprintf(errmsg, "out of memory");
1324 goto error;
1325 }
1326 reply->type = HTTP_REPLY_ERRFILES;
1327 cur_arg++;
1328 }
1329 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1330 if (reply->type != HTTP_REPLY_EMPTY) {
1331 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1332 goto error;
1333 }
1334 act_arg = args[cur_arg];
1335 reply->type = HTTP_REPLY_ERRMSG;
1336 cur_arg++;
1337 }
1338 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1339 if (reply->type != HTTP_REPLY_EMPTY) {
1340 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1341 goto error;
1342 }
1343 act_arg = args[cur_arg];
1344 cur_arg++;
1345 if (!*args[cur_arg]) {
1346 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1347 goto error;
1348 }
1349 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1350 if (!reply->body.errmsg) {
1351 goto error;
1352 }
1353 reply->type = HTTP_REPLY_ERRMSG;
1354 cur_arg++;
1355 }
1356 else if (strcmp(args[cur_arg], "file") == 0) {
1357 if (reply->type != HTTP_REPLY_EMPTY) {
1358 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1359 goto error;
1360 }
1361 act_arg = args[cur_arg];
1362 cur_arg++;
1363 if (!*args[cur_arg]) {
1364 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1365 goto error;
1366 }
1367 fd = open(args[cur_arg], O_RDONLY);
1368 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1369 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1370 goto error;
1371 }
1372 if (stat.st_size > global.tune.bufsize) {
1373 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1374 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1375 goto error;
1376 }
1377 objlen = stat.st_size;
1378 obj = malloc(objlen);
1379 if (!obj || read(fd, obj, objlen) != objlen) {
1380 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1381 goto error;
1382 }
1383 close(fd);
1384 fd = -1;
1385 reply->type = HTTP_REPLY_RAW;
1386 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1387 obj = NULL;
1388 cur_arg++;
1389 }
1390 else if (strcmp(args[cur_arg], "string") == 0) {
1391 if (reply->type != HTTP_REPLY_EMPTY) {
1392 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1393 goto error;
1394 }
1395 act_arg = args[cur_arg];
1396 cur_arg++;
1397 if (!*args[cur_arg]) {
1398 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1399 goto error;
1400 }
1401 obj = strdup(args[cur_arg]);
1402 objlen = strlen(args[cur_arg]);
1403 if (!obj) {
1404 memprintf(errmsg, "out of memory");
1405 goto error;
1406 }
1407 reply->type = HTTP_REPLY_RAW;
1408 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1409 obj = NULL;
1410 cur_arg++;
1411 }
1412 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1413 if (reply->type != HTTP_REPLY_EMPTY) {
1414 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1415 goto error;
1416 }
1417 act_arg = args[cur_arg];
1418 cur_arg++;
1419 if (!*args[cur_arg]) {
1420 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1421 goto error;
1422 }
1423 fd = open(args[cur_arg], O_RDONLY);
1424 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1425 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1426 goto error;
1427 }
1428 if (stat.st_size > global.tune.bufsize) {
1429 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1430 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1431 goto error;
1432 }
1433 objlen = stat.st_size;
1434 obj = malloc(objlen + 1);
1435 if (!obj || read(fd, obj, objlen) != objlen) {
1436 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1437 goto error;
1438 }
1439 close(fd);
1440 fd = -1;
1441 obj[objlen] = '\0';
1442 reply->type = HTTP_REPLY_LOGFMT;
1443 cur_arg++;
1444 }
1445 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1446 if (reply->type != HTTP_REPLY_EMPTY) {
1447 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1448 goto error;
1449 }
1450 act_arg = args[cur_arg];
1451 cur_arg++;
1452 if (!*args[cur_arg]) {
1453 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1454 goto error;
1455 }
1456 obj = strdup(args[cur_arg]);
1457 objlen = strlen(args[cur_arg]);
1458 reply->type = HTTP_REPLY_LOGFMT;
1459 cur_arg++;
1460 }
1461 else if (strcmp(args[cur_arg], "hdr") == 0) {
1462 cur_arg++;
1463 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1464 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1465 goto error;
1466 }
1467 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1468 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1469 strcasecmp(args[cur_arg], "content-type") == 0) {
1470 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1471 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1472 cur_arg += 2;
1473 continue;
1474 }
1475 hdr = calloc(1, sizeof(*hdr));
1476 if (!hdr) {
1477 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1478 goto error;
1479 }
1480 LIST_INIT(&hdr->value);
1481 hdr->name = ist(strdup(args[cur_arg]));
1482 if (!isttest(hdr->name)) {
1483 memprintf(errmsg, "out of memory");
1484 goto error;
1485 }
1486 LIST_ADDQ(&reply->hdrs, &hdr->list);
1487 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1488 goto error;
1489
1490 free(px->conf.lfs_file);
1491 px->conf.lfs_file = strdup(px->conf.args.file);
1492 px->conf.lfs_line = px->conf.args.line;
1493 cur_arg += 2;
1494 }
1495 else
1496 break;
1497 }
1498
1499 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1500 if (reply->ctype) {
1501 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1502 " neither errorfile nor payload defined.\n",
1503 px->conf.args.file, px->conf.args.line, reply->ctype);
1504 free(reply->ctype);
1505 reply->ctype = NULL;
1506 }
1507 }
1508 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1509
1510 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1511 /* default errorfile or errorfiles: check the status */
1512 int rc;
1513
1514 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1515 if (http_err_codes[rc] == reply->status)
1516 break;
1517 }
1518
1519 if (rc >= HTTP_ERR_SIZE) {
1520 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1521 reply->status, act_arg);
1522 goto error;
1523 }
1524 }
1525
1526 if (reply->ctype) {
1527 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1528 "with an erorrfile.\n",
1529 px->conf.args.file, px->conf.args.line, reply->ctype);
1530 free(reply->ctype);
1531 reply->ctype = NULL;
1532 }
1533 if (!LIST_ISEMPTY(&reply->hdrs)) {
1534 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1535 "with an erorrfile.\n",
1536 px->conf.args.file, px->conf.args.line);
1537 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1538 LIST_DEL(&hdr->list);
1539 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1540 LIST_DEL(&lf->list);
1541 release_sample_expr(lf->expr);
1542 free(lf->arg);
1543 free(lf);
1544 }
1545 istfree(&hdr->name);
1546 free(hdr);
1547 }
1548 }
1549 }
1550 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1551 if (!reply->ctype && objlen) {
1552 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1553 goto error;
1554 }
1555 if (reply->ctype && !b_data(&reply->body.obj)) {
1556 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1557 "with an emtpy payload.\n",
1558 px->conf.args.file, px->conf.args.line, reply->ctype);
1559 free(reply->ctype);
1560 reply->ctype = NULL;
1561 }
1562 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1563 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1564 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1565 px->conf.args.file, px->conf.args.line);
1566 }
1567 }
1568 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1569 LIST_INIT(&reply->body.fmt);
1570 if (!reply->ctype) {
1571 memprintf(errmsg, "a content type must be defined with a log-format payload");
1572 goto error;
1573 }
1574 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1575 goto error;
1576
1577 free(px->conf.lfs_file);
1578 px->conf.lfs_file = strdup(px->conf.args.file);
1579 px->conf.lfs_line = px->conf.args.line;
1580 }
1581
1582 free(obj);
1583 *orig_arg = cur_arg;
1584 return reply;
1585
1586 error:
1587 free(obj);
1588 if (fd >= 0)
1589 close(fd);
1590 release_http_reply(reply);
1591 return NULL;
1592}
1593
Christopher Faulet07f41f72020-01-16 16:16:06 +01001594/* Parses the "errorloc[302|303]" proxy keyword */
1595static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1596 struct proxy *defpx, const char *file, int line,
1597 char **errmsg)
1598{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001599 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001600 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001601 int errloc, status;
1602 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001603
1604 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1605 ret = 1;
1606 goto out;
1607 }
1608
1609 if (*(args[1]) == 0 || *(args[2]) == 0) {
1610 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1611 ret = -1;
1612 goto out;
1613 }
1614
1615 status = atol(args[1]);
1616 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1617 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1618 if (!msg) {
1619 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1620 ret = -1;
1621 goto out;
1622 }
1623
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001624 conf_err = calloc(1, sizeof(*conf_err));
1625 if (!conf_err) {
1626 memprintf(errmsg, "%s : out of memory.", args[0]);
1627 ret = -1;
1628 goto out;
1629 }
1630 conf_err->type = 1;
1631 conf_err->info.errorfile.status = status;
1632 conf_err->info.errorfile.msg = msg;
1633 conf_err->file = strdup(file);
1634 conf_err->line = line;
1635 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001636
1637 out:
1638 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001639
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001640}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001641
1642/* Parses the "errorfile" proxy keyword */
1643static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1644 struct proxy *defpx, const char *file, int line,
1645 char **errmsg)
1646{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001647 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001648 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001649 int status;
1650 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001651
1652 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1653 ret = 1;
1654 goto out;
1655 }
1656
1657 if (*(args[1]) == 0 || *(args[2]) == 0) {
1658 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1659 ret = -1;
1660 goto out;
1661 }
1662
1663 status = atol(args[1]);
1664 msg = http_parse_errorfile(status, args[2], errmsg);
1665 if (!msg) {
1666 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1667 ret = -1;
1668 goto out;
1669 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001670
1671 conf_err = calloc(1, sizeof(*conf_err));
1672 if (!conf_err) {
1673 memprintf(errmsg, "%s : out of memory.", args[0]);
1674 ret = -1;
1675 goto out;
1676 }
1677 conf_err->type = 1;
1678 conf_err->info.errorfile.status = status;
1679 conf_err->info.errorfile.msg = msg;
1680 conf_err->file = strdup(file);
1681 conf_err->line = line;
1682 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1683
1684 out:
1685 return ret;
1686
1687}
1688
1689/* Parses the "errorfiles" proxy keyword */
1690static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1691 struct proxy *defpx, const char *file, int line,
1692 char **err)
1693{
1694 struct conf_errors *conf_err = NULL;
1695 char *name = NULL;
1696 int rc, ret = 0;
1697
1698 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1699 ret = 1;
1700 goto out;
1701 }
1702
1703 if (!*(args[1])) {
1704 memprintf(err, "%s : expects <name> as argument.", args[0]);
1705 ret = -1;
1706 goto out;
1707 }
1708
1709 name = strdup(args[1]);
1710 conf_err = calloc(1, sizeof(*conf_err));
1711 if (!name || !conf_err) {
1712 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001713 goto error;
1714 }
1715 conf_err->type = 0;
1716
1717 conf_err->info.errorfiles.name = name;
1718 if (!*(args[2])) {
1719 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1720 conf_err->info.errorfiles.status[rc] = 1;
1721 }
1722 else {
1723 int cur_arg, status;
1724 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1725 status = atol(args[cur_arg]);
1726
1727 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1728 if (http_err_codes[rc] == status) {
1729 conf_err->info.errorfiles.status[rc] = 2;
1730 break;
1731 }
1732 }
1733 if (rc >= HTTP_ERR_SIZE) {
1734 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001735 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001736 }
1737 }
1738 }
1739 conf_err->file = strdup(file);
1740 conf_err->line = line;
1741 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1742 out:
1743 return ret;
1744
1745 error:
1746 free(name);
1747 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001748 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001749 goto out;
1750}
1751
1752/* Check "errorfiles" proxy keyword */
1753static int proxy_check_errors(struct proxy *px)
1754{
1755 struct conf_errors *conf_err, *conf_err_back;
1756 struct http_errors *http_errs;
1757 int rc, err = 0;
1758
1759 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1760 if (conf_err->type == 1) {
1761 /* errorfile */
1762 rc = http_get_status_idx(conf_err->info.errorfile.status);
1763 px->errmsg[rc] = conf_err->info.errorfile.msg;
1764 }
1765 else {
1766 /* errorfiles */
1767 list_for_each_entry(http_errs, &http_errors_list, list) {
1768 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1769 break;
1770 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001771
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001772 /* unknown http-errors section */
1773 if (&http_errs->list == &http_errors_list) {
1774 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1775 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1776 err |= ERR_ALERT | ERR_FATAL;
1777 free(conf_err->info.errorfiles.name);
1778 goto next;
1779 }
1780
1781 free(conf_err->info.errorfiles.name);
1782 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1783 if (conf_err->info.errorfiles.status[rc] > 0) {
1784 if (http_errs->errmsg[rc])
1785 px->errmsg[rc] = http_errs->errmsg[rc];
1786 else if (conf_err->info.errorfiles.status[rc] == 2)
1787 ha_warning("config: proxy '%s' : status '%d' not declared in"
1788 " http-errors section '%s' (at %s:%d).\n",
1789 px->id, http_err_codes[rc], http_errs->id,
1790 conf_err->file, conf_err->line);
1791 }
1792 }
1793 }
1794 next:
1795 LIST_DEL(&conf_err->list);
1796 free(conf_err->file);
1797 free(conf_err);
1798 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001799
1800 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001801 return err;
1802}
1803
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001804static int post_check_errors()
1805{
1806 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001807 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001808 struct htx *htx;
1809 int err_code = 0;
1810
1811 node = ebpt_first(&http_error_messages);
1812 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001813 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1814 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001815 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001816 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001817 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1818 ha_warning("config: errorfile '%s' runs over the buffer space"
1819 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001820 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001821 (char *)node->key);
1822 err_code |= ERR_WARN;
1823 }
1824 next:
1825 node = ebpt_next(node);
1826 }
1827
1828 return err_code;
1829}
1830
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001831int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1832{
1833 struct conf_errors *conf_err, *new_conf_err = NULL;
1834 int ret = 0;
1835
1836 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1837 new_conf_err = calloc(1, sizeof(*new_conf_err));
1838 if (!new_conf_err) {
1839 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1840 goto out;
1841 }
1842 new_conf_err->type = conf_err->type;
1843 if (conf_err->type == 1) {
1844 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1845 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1846 }
1847 else {
1848 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1849 if (!new_conf_err->info.errorfiles.name) {
1850 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1851 goto out;
1852 }
1853 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1854 sizeof(conf_err->info.errorfiles.status));
1855 }
1856 new_conf_err->file = strdup(conf_err->file);
1857 new_conf_err->line = conf_err->line;
1858 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1859 new_conf_err = NULL;
1860 }
1861 ret = 1;
1862
1863 out:
1864 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001865 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001866}
1867
1868void proxy_release_conf_errors(struct proxy *px)
1869{
1870 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001871
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001872 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1873 if (conf_err->type == 0)
1874 free(conf_err->info.errorfiles.name);
1875 LIST_DEL(&conf_err->list);
1876 free(conf_err->file);
1877 free(conf_err);
1878 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001879}
1880
1881/*
1882 * Parse an <http-errors> section.
1883 * Returns the error code, 0 if OK, or any combination of :
1884 * - ERR_ABORT: must abort ASAP
1885 * - ERR_FATAL: we can continue parsing but not start the service
1886 * - ERR_WARN: a warning has been emitted
1887 * - ERR_ALERT: an alert has been emitted
1888 * Only the two first ones can stop processing, the two others are just
1889 * indicators.
1890 */
1891static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1892{
1893 static struct http_errors *curr_errs = NULL;
1894 int err_code = 0;
1895 const char *err;
1896 char *errmsg = NULL;
1897
1898 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1899 if (!*args[1]) {
1900 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1901 err_code |= ERR_ALERT | ERR_ABORT;
1902 goto out;
1903 }
1904
1905 err = invalid_char(args[1]);
1906 if (err) {
1907 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1908 file, linenum, *err, args[0], args[1]);
1909 err_code |= ERR_ALERT | ERR_FATAL;
1910 }
1911
1912 list_for_each_entry(curr_errs, &http_errors_list, list) {
1913 /* Error if two errors section owns the same name */
1914 if (strcmp(curr_errs->id, args[1]) == 0) {
1915 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1916 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1917 err_code |= ERR_ALERT | ERR_FATAL;
1918 }
1919 }
1920
1921 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1922 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1923 err_code |= ERR_ALERT | ERR_ABORT;
1924 goto out;
1925 }
1926
1927 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1928 curr_errs->id = strdup(args[1]);
1929 curr_errs->conf.file = strdup(file);
1930 curr_errs->conf.line = linenum;
1931 }
1932 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1933 struct buffer *msg;
1934 int status, rc;
1935
1936 if (*(args[1]) == 0 || *(args[2]) == 0) {
1937 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1938 file, linenum, args[0]);
1939 err_code |= ERR_ALERT | ERR_FATAL;
1940 goto out;
1941 }
1942
1943 status = atol(args[1]);
1944 msg = http_parse_errorfile(status, args[2], &errmsg);
1945 if (!msg) {
1946 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1947 err_code |= ERR_ALERT | ERR_FATAL;
1948 goto out;
1949 }
1950 rc = http_get_status_idx(status);
1951 curr_errs->errmsg[rc] = msg;
1952 }
1953 else if (*args[0] != 0) {
1954 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1955 err_code |= ERR_ALERT | ERR_FATAL;
1956 goto out;
1957 }
1958
1959out:
1960 free(errmsg);
1961 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001962}
1963
1964static struct cfg_kw_list cfg_kws = {ILH, {
1965 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1966 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1967 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1968 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001969 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01001970 { 0, NULL, NULL },
1971}};
1972
1973INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001974REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001975REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001976
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001977REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1978
Christopher Faulet29f72842019-12-11 15:52:32 +01001979/************************************************************************/
1980/* HTX sample fetches */
1981/************************************************************************/
1982
1983/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1984static int
1985smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1986{
1987 if (!smp->strm)
1988 return 0;
1989
1990 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1991 smp->data.type = SMP_T_BOOL;
1992 return 1;
1993}
1994
1995/* Returns the number of blocks in an HTX message. The channel is chosen
1996 * depending on the sample direction. */
1997static int
1998smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1999{
2000 struct channel *chn;
2001 struct htx *htx;
2002
2003 if (!smp->strm)
2004 return 0;
2005
2006 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002007 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002008 if (!htx)
2009 return 0;
2010
2011 smp->data.u.sint = htx_nbblks(htx);
2012 smp->data.type = SMP_T_SINT;
2013 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2014 return 1;
2015}
2016
2017/* Returns the size of an HTX message. The channel is chosen depending on the
2018 * sample direction. */
2019static int
2020smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2021{
2022 struct channel *chn;
2023 struct htx *htx;
2024
2025 if (!smp->strm)
2026 return 0;
2027
2028 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002029 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002030 if (!htx)
2031 return 0;
2032
2033 smp->data.u.sint = htx->size;
2034 smp->data.type = SMP_T_SINT;
2035 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2036 return 1;
2037}
2038
2039/* Returns the data size of an HTX message. The channel is chosen depending on the
2040 * sample direction. */
2041static int
2042smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2043{
2044 struct channel *chn;
2045 struct htx *htx;
2046
2047 if (!smp->strm)
2048 return 0;
2049
2050 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002051 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002052 if (!htx)
2053 return 0;
2054
2055 smp->data.u.sint = htx->data;
2056 smp->data.type = SMP_T_SINT;
2057 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2058 return 1;
2059}
2060
2061/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2062 * depending on the sample direction. */
2063static int
2064smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2065{
2066 struct channel *chn;
2067 struct htx *htx;
2068
2069 if (!smp->strm)
2070 return 0;
2071
2072 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002073 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002074 if (!htx)
2075 return 0;
2076
2077 smp->data.u.sint = htx_used_space(htx);
2078 smp->data.type = SMP_T_SINT;
2079 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2080 return 1;
2081}
2082
2083/* Returns the free space (size-used) of an HTX message. The channel is chosen
2084 * depending on the sample direction. */
2085static int
2086smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2087{
2088 struct channel *chn;
2089 struct htx *htx;
2090
2091 if (!smp->strm)
2092 return 0;
2093
2094 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002095 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002096 if (!htx)
2097 return 0;
2098
2099 smp->data.u.sint = htx_free_space(htx);
2100 smp->data.type = SMP_T_SINT;
2101 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2102 return 1;
2103}
2104
2105/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2106 * channel is chosen depending on the sample direction. */
2107static int
2108smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2109{
2110 struct channel *chn;
2111 struct htx *htx;
2112
2113 if (!smp->strm)
2114 return 0;
2115
2116 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002117 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002118 if (!htx)
2119 return 0;
2120
2121 smp->data.u.sint = htx_free_data_space(htx);
2122 smp->data.type = SMP_T_SINT;
2123 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2124 return 1;
2125}
2126
2127/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2128 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2129 * the sample direction. */
2130static int
2131smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2132{
2133 struct channel *chn;
2134 struct htx *htx;
2135
2136 if (!smp->strm)
2137 return 0;
2138
2139 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002140 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002141 if (!htx)
2142 return 0;
2143
2144 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2145 smp->data.type = SMP_T_BOOL;
2146 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2147 return 1;
2148}
2149
2150/* Returns the type of a specific HTX block, if found in the message. Otherwise
2151 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2152 * "head", "tail" or "first". The channel is chosen depending on the sample
2153 * direction. */
2154static int
2155smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2156{
2157 struct channel *chn;
2158 struct htx *htx;
2159 enum htx_blk_type type;
2160 int32_t pos;
2161
2162 if (!smp->strm || !arg_p)
2163 return 0;
2164
2165 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002166 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002167 if (!htx)
2168 return 0;
2169
2170 pos = arg_p[0].data.sint;
2171 if (pos == -1)
2172 type = htx_get_head_type(htx);
2173 else if (pos == -2)
2174 type = htx_get_tail_type(htx);
2175 else if (pos == -3)
2176 type = htx_get_first_type(htx);
2177 else
2178 type = ((pos >= htx->head && pos <= htx->tail)
2179 ? htx_get_blk_type(htx_get_blk(htx, pos))
2180 : HTX_BLK_UNUSED);
2181
2182 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2183 smp->data.type = SMP_T_STR;
2184 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2185 return 1;
2186}
2187
2188/* Returns the size of a specific HTX block, if found in the message. Otherwise
2189 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2190 * "first". The channel is chosen depending on the sample direction. */
2191static int
2192smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2193{
2194 struct channel *chn;
2195 struct htx *htx;
2196 struct htx_blk *blk;
2197 int32_t pos;
2198
2199 if (!smp->strm || !arg_p)
2200 return 0;
2201
2202 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002203 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002204 if (!htx)
2205 return 0;
2206
2207 pos = arg_p[0].data.sint;
2208 if (pos == -1)
2209 blk = htx_get_head_blk(htx);
2210 else if (pos == -2)
2211 blk = htx_get_tail_blk(htx);
2212 else if (pos == -3)
2213 blk = htx_get_first_blk(htx);
2214 else
2215 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2216
2217 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2218 smp->data.type = SMP_T_SINT;
2219 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2220 return 1;
2221}
2222
2223/* Returns the start-line if the selected HTX block exists and is a
2224 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2225 * supported or "head", "tail" or "first". The channel is chosen depending on
2226 * the sample direction. */
2227static int
2228smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2229{
2230 struct buffer *temp;
2231 struct channel *chn;
2232 struct htx *htx;
2233 struct htx_blk *blk;
2234 struct htx_sl *sl;
2235 int32_t pos;
2236
2237 if (!smp->strm || !arg_p)
2238 return 0;
2239
2240 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002241 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002242 if (!htx)
2243 return 0;
2244
2245 pos = arg_p[0].data.sint;
2246 if (pos == -1)
2247 blk = htx_get_head_blk(htx);
2248 else if (pos == -2)
2249 blk = htx_get_tail_blk(htx);
2250 else if (pos == -3)
2251 blk = htx_get_first_blk(htx);
2252 else
2253 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2254
2255 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2256 smp->data.u.str.size = 0;
2257 smp->data.u.str.area = "";
2258 smp->data.u.str.data = 0;
2259 }
2260 else {
2261 sl = htx_get_blk_ptr(htx, blk);
2262
2263 temp = get_trash_chunk();
2264 chunk_istcat(temp, htx_sl_p1(sl));
2265 temp->area[temp->data++] = ' ';
2266 chunk_istcat(temp, htx_sl_p2(sl));
2267 temp->area[temp->data++] = ' ';
2268 chunk_istcat(temp, htx_sl_p3(sl));
2269
2270 smp->data.u.str = *temp;
2271 }
2272
2273 smp->data.type = SMP_T_STR;
2274 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2275 return 1;
2276}
2277
2278/* Returns the header name if the selected HTX block exists and is a header or a
2279 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2280 * supported or "head", "tail" or "first". The channel is chosen depending on
2281 * the sample direction. */
2282static int
2283smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2284{
2285 struct channel *chn;
2286 struct htx *htx;
2287 struct htx_blk *blk;
2288 int32_t pos;
2289
2290 if (!smp->strm || !arg_p)
2291 return 0;
2292
2293 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002294 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002295 if (!htx)
2296 return 0;
2297
2298 pos = arg_p[0].data.sint;
2299 if (pos == -1)
2300 blk = htx_get_head_blk(htx);
2301 else if (pos == -2)
2302 blk = htx_get_tail_blk(htx);
2303 else if (pos == -3)
2304 blk = htx_get_first_blk(htx);
2305 else
2306 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2307
2308 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2309 smp->data.u.str.size = 0;
2310 smp->data.u.str.area = "";
2311 smp->data.u.str.data = 0;
2312 }
2313 else {
2314 struct ist name = htx_get_blk_name(htx, blk);
2315
2316 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2317 }
2318 smp->data.type = SMP_T_STR;
2319 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2320 return 1;
2321}
2322
2323/* Returns the header value if the selected HTX block exists and is a header or
2324 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2325 * supported or "head", "tail" or "first". The channel is chosen depending on
2326 * the sample direction. */
2327static int
2328smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2329{
2330 struct channel *chn;
2331 struct htx *htx;
2332 struct htx_blk *blk;
2333 int32_t pos;
2334
2335 if (!smp->strm || !arg_p)
2336 return 0;
2337
2338 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002339 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002340 if (!htx)
2341 return 0;
2342
2343 pos = arg_p[0].data.sint;
2344 if (pos == -1)
2345 blk = htx_get_head_blk(htx);
2346 else if (pos == -2)
2347 blk = htx_get_tail_blk(htx);
2348 else if (pos == -3)
2349 blk = htx_get_first_blk(htx);
2350 else
2351 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2352
2353 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2354 smp->data.u.str.size = 0;
2355 smp->data.u.str.area = "";
2356 smp->data.u.str.data = 0;
2357 }
2358 else {
2359 struct ist val = htx_get_blk_value(htx, blk);
2360
2361 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2362 }
2363 smp->data.type = SMP_T_STR;
2364 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2365 return 1;
2366}
2367
2368/* Returns the value if the selected HTX block exists and is a data
2369 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2370 * or "head", "tail" or "first". The channel is chosen depending on the sample
2371 * direction. */
2372static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002373smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002374{
2375 struct channel *chn;
2376 struct htx *htx;
2377 struct htx_blk *blk;
2378 int32_t pos;
2379
2380 if (!smp->strm || !arg_p)
2381 return 0;
2382
2383 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002384 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002385 if (!htx)
2386 return 0;
2387
2388 pos = arg_p[0].data.sint;
2389 if (pos == -1)
2390 blk = htx_get_head_blk(htx);
2391 else if (pos == -2)
2392 blk = htx_get_tail_blk(htx);
2393 else if (pos == -3)
2394 blk = htx_get_first_blk(htx);
2395 else
2396 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2397
2398 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2399 smp->data.u.str.size = 0;
2400 smp->data.u.str.area = "";
2401 smp->data.u.str.data = 0;
2402 }
2403 else {
2404 struct ist val = htx_get_blk_value(htx, blk);
2405
2406 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2407 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002408 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002409 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2410 return 1;
2411}
2412
2413/* This function is used to validate the arguments passed to any "htx_blk" fetch
2414 * keywords. An argument is expected by these keywords. It must be a positive
2415 * integer or on of the following strings: "head", "tail" or "first". It returns
2416 * 0 on error, and a non-zero value if OK.
2417 */
2418int val_blk_arg(struct arg *arg, char **err_msg)
2419{
2420 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2421 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2422 return 0;
2423 }
2424 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2425 free(arg[0].data.str.area);
2426 arg[0].type = ARGT_SINT;
2427 arg[0].data.sint = -1;
2428 }
2429 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2430 free(arg[0].data.str.area);
2431 arg[0].type = ARGT_SINT;
2432 arg[0].data.sint = -2;
2433 }
2434 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2435 free(arg[0].data.str.area);
2436 arg[0].type = ARGT_SINT;
2437 arg[0].data.sint = -3;
2438 }
2439 else {
2440 int pos;
2441
2442 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002443 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002444 memprintf(err_msg, "invalid block position");
2445 return 0;
2446 }
2447 }
2448
2449 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2450 if (pos < 0) {
2451 memprintf(err_msg, "block position must not be negative");
2452 return 0;
2453 }
2454 free(arg[0].data.str.area);
2455 arg[0].type = ARGT_SINT;
2456 arg[0].data.sint = pos;
2457 }
2458
2459 return 1;
2460}
2461
2462
2463/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002464 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002465 */
2466static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002467 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002468
Christopher Faulet01f44452020-01-08 14:23:40 +01002469 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2470 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2471 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2472 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2473 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2474 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2475 { "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 +01002476
Christopher Faulet01f44452020-01-08 14:23:40 +01002477 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2478 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2479 { "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},
2480 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2481 { "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 +01002482 { "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 +01002483
2484 { /* END */ },
2485}};
2486
2487INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);