blob: 4414ab378141346a30eb0f7be6513486096c6437 [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 Faulet1b13eca2020-05-14 09:54:26 +020032struct http_reply http_err_replies[HTTP_ERR_SIZE];
33
Christopher Faulet58857752020-01-15 15:19:50 +010034struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010035struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010036
Christopher Faulet76edc0f2020-01-13 15:52:01 +010037/* The declaration of an errorfiles/errorfile directives. Used during config
38 * parsing only. */
39struct conf_errors {
40 char type; /* directive type (0: errorfiles, 1: errorfile) */
41 union {
42 struct {
43 int status; /* the status code associated to this error */
44 struct buffer *msg; /* the HTX error message */
45 } errorfile; /* describe an "errorfile" directive */
46 struct {
47 char *name; /* the http-errors section name */
48 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
49 } errorfiles; /* describe an "errorfiles" directive */
50 } info;
51
52 char *file; /* file where the directive appears */
53 int line; /* line where the directive appears */
54
55 struct list list; /* next conf_errors */
56};
57
Christopher Faulet297fbb42019-05-13 14:41:27 +020058/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020059 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020060 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020061 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020062struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020063{
Christopher Faulet297fbb42019-05-13 14:41:27 +020064 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010065
Christopher Faulet29f17582019-05-23 11:03:26 +020066 BUG_ON(htx->first == -1);
67 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020068 if (!blk)
69 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020070 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 +020071 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020072}
73
Christopher Faulet727a3f12020-02-07 16:39:41 +010074/* Returns the headers size in the HTX message */
75size_t http_get_hdrs_size(struct htx *htx)
76{
77 struct htx_blk *blk;
78 size_t sz = 0;
79
80 blk = htx_get_first_blk(htx);
81 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
82 return sz;
83
84 for (; blk; blk = htx_get_next_blk(htx, blk)) {
85 sz += htx_get_blksz(blk);
86 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
87 break;
88 }
89 return sz;
90}
91
Christopher Faulet8dd33e12020-05-05 07:42:42 +020092/* Finds the first or next occurrence of header matching <pattern> in the HTX
93 * message <htx> using the context <ctx>. This structure holds everything
94 * necessary to use the header and find next occurrence. If its <blk> member is
95 * NULL, the header is searched from the beginning. Otherwise, the next
96 * occurrence is returned. The function returns 1 when it finds a value, and 0
97 * when there is no more. It is designed to work with headers defined as
98 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
99 * full-line headers in whose comma is not a delimiter but is part of the
100 * syntax. A special case, if ctx->value is NULL when searching for a new values
101 * of a header, the current header is rescanned. This allows rescanning after a
102 * header deletion.
103 *
104 * The matching method is chosen by checking the flags :
105 *
106 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
107 * the regex are evaluated.
108 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
109 * to the string are evaluated.
110 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
111 * starting by the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
113 * ending by the string are evaluated.
114 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
115 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200116 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200117
118#define HTTP_FIND_FL_MATCH_STR 0x0001
119#define HTTP_FIND_FL_MATCH_PFX 0x0002
120#define HTTP_FIND_FL_MATCH_SFX 0x0003
121#define HTTP_FIND_FL_MATCH_SUB 0x0004
122#define HTTP_FIND_FL_MATCH_REG 0x0005
123/* 0x0006..0x000f: for other matching methods */
124#define HTTP_FIND_FL_MATCH_TYPE 0x000F
125#define HTTP_FIND_FL_FULL 0x0010
126
127static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200128{
129 struct htx_blk *blk = ctx->blk;
130 struct ist n, v;
131 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200132
133 if (blk) {
134 char *p;
135
Tim Duesterhused526372020-03-05 17:56:33 +0100136 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200137 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200138 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200139 goto next_blk;
140 v = htx_get_blk_value(htx, blk);
141 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
142 v.len -= (p - v.ptr);
143 v.ptr = p;
144 if (!v.len)
145 goto next_blk;
146 /* Skip comma */
147 if (*(v.ptr) == ',') {
148 v.ptr++;
149 v.len--;
150 }
151
152 goto return_hdr;
153 }
154
Christopher Faulet192c6a22019-06-11 16:32:24 +0200155 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200156 return 0;
157
Christopher Fauleta3f15502019-05-13 15:27:23 +0200158 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200159 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100161 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
162 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200163 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200164 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200165
166 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
167 const struct my_regex *re = pattern;
168
169 n = htx_get_blk_name(htx, blk);
170 if (!regex_exec2(re, n.ptr, n.len))
171 goto next_blk;
172 }
173 else {
174 const struct ist name = *(const struct ist *)(pattern);
175
Christopher Faulet47596d32018-10-22 09:17:28 +0200176 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200177 if (!istlen(name))
178 goto match;
179
Christopher Faulet47596d32018-10-22 09:17:28 +0200180 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200181 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
182 case HTTP_FIND_FL_MATCH_STR:
183 if (!isteqi(n, name))
184 goto next_blk;
185 break;
186 case HTTP_FIND_FL_MATCH_PFX:
187 if (istlen(n) < istlen(name))
188 goto next_blk;
189
190 n = ist2(istptr(n), istlen(name));
191 if (!isteqi(n, name))
192 goto next_blk;
193 break;
194 case HTTP_FIND_FL_MATCH_SFX:
195 if (istlen(n) < istlen(name))
196 goto next_blk;
197
198 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
199 if (!isteqi(n, name))
200 goto next_blk;
201 break;
202 case HTTP_FIND_FL_MATCH_SUB:
203 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
204 goto next_blk;
205 break;
206 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200207 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200208 break;
209 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200210 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200211 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 v = htx_get_blk_value(htx, blk);
213
214 return_hdr:
215 ctx->lws_before = 0;
216 ctx->lws_after = 0;
217 while (v.len && HTTP_IS_LWS(*v.ptr)) {
218 v.ptr++;
219 v.len--;
220 ctx->lws_before++;
221 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200222 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200223 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
224 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
225 v.len--;
226 ctx->lws_after++;
227 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200228 ctx->blk = blk;
229 ctx->value = v;
230 return 1;
231
232 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200233 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200234 }
235
236 ctx->blk = NULL;
237 ctx->value = ist("");
238 ctx->lws_before = ctx->lws_after = 0;
239 return 0;
240}
241
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200242
243/* Header names must match <name> */
244int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
245{
246 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
247}
248
249/* Header names must match <name>. Same than http_find_header */
250int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
251{
252 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
253}
254
255
256/* Header names must start with <prefix> */
257int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
258{
259 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
260}
261
262/* Header names must end with <suffix> */
263int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
264{
265 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
266}
267/* Header names must contain <sub> */
268int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
269{
270 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
271}
272
273/* Header names must match <re> regex*/
274int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
275{
276 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
277}
278
279
Christopher Faulet47596d32018-10-22 09:17:28 +0200280/* Adds a header block int the HTX message <htx>, just before the EOH block. It
281 * returns 1 on success, otherwise it returns 0.
282 */
283int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
284{
285 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200286 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200287 enum htx_blk_type type = htx_get_tail_type(htx);
288 int32_t prev;
289
290 blk = htx_add_header(htx, n, v);
291 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200292 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200293
294 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200295 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200296
297 /* <blk> is the head, swap it iteratively with its predecessor to place
298 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200299 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200300 struct htx_blk *pblk = htx_get_blk(htx, prev);
301 enum htx_blk_type type = htx_get_blk_type(pblk);
302
303 /* Swap .addr and .info fields */
304 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
305 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
306
307 if (blk->addr == pblk->addr)
308 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200309
310 /* Stop when end-of-header is reached */
311 if (type == HTX_BLK_EOH)
312 break;
313
314 blk = pblk;
315 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200316
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200317 end:
318 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100319 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200320 if (!http_update_authority(htx, sl, v))
321 goto fail;
322 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200323 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200324
325 fail:
326 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200327}
328
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200330 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200331 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200333{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200334 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200335
Christopher Faulet29f17582019-05-23 11:03:26 +0200336 blk = htx_get_first_blk(htx);
337 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200338 return 0;
339 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200340}
341
Christopher Faulete010c802018-10-24 10:36:45 +0200342/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
343 * on success, otherwise 0.
344 */
345int http_replace_req_meth(struct htx *htx, const struct ist meth)
346{
347 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200348 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100349 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200350
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100351 if (!sl)
352 return 0;
353
Christopher Faulete010c802018-10-24 10:36:45 +0200354 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100355 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
356 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200357
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100358 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
359 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200360
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100361 /* create the new start line */
362 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
363 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200364}
365
366/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
367 * success, otherwise 0.
368 */
369int http_replace_req_uri(struct htx *htx, const struct ist uri)
370{
371 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200372 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100373 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200374
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100375 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200376 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100377
Christopher Faulete010c802018-10-24 10:36:45 +0200378 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100379 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
380 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200381
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100382 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
383 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200384
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100385 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200386 if (!http_replace_stline(htx, meth, uri, vsn))
387 goto fail;
388
389 sl = http_get_stline(htx);
390 if (!http_update_host(htx, sl, uri))
391 goto fail;
392
393 return 1;
394 fail:
395 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200396}
397
398/* Replace the request path in the HTX message <htx> by <path>. The host part
399 * and the query string are preserved. It returns 1 on success, otherwise 0.
400 */
401int http_replace_req_path(struct htx *htx, const struct ist path)
402{
403 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200404 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100405 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200406 size_t plen = 0;
407
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100408 if (!sl)
409 return 0;
410
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100411 uri = htx_sl_req_uri(sl);
412 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100413 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100414 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200415 while (plen < p.len && *(p.ptr + plen) != '?')
416 plen++;
417
418 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100419 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
420 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200421
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100422 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
423 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
424
425 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200426 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
427 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100428 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200429
430 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100431 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200432}
433
434/* Replace the request query-string in the HTX message <htx> by <query>. The
435 * host part and the path are preserved. It returns 1 on success, otherwise
436 * 0.
437 */
438int http_replace_req_query(struct htx *htx, const struct ist query)
439{
440 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200441 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100442 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200443 int offset = 1;
444
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100445 if (!sl)
446 return 0;
447
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100448 uri = htx_sl_req_uri(sl);
449 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200450 while (q.len > 0 && *(q.ptr) != '?') {
451 q.ptr++;
452 q.len--;
453 }
454
455 /* skip the question mark or indicate that we must insert it
456 * (but only if the format string is not empty then).
457 */
458 if (q.len) {
459 q.ptr++;
460 q.len--;
461 }
462 else if (query.len > 1)
463 offset = 0;
464
465 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100466 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
467 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200468
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100469 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
470 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200471
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100472 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
473 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
474 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200475
476 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200478}
479
480/* Replace the response status in the HTX message <htx> by <status>. It returns
481 * 1 on success, otherwise 0.
482*/
483int http_replace_res_status(struct htx *htx, const struct ist status)
484{
485 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200486 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100487 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200488
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100489 if (!sl)
490 return 0;
491
Christopher Faulete010c802018-10-24 10:36:45 +0200492 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100493 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
494 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200495
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100496 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
497 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200498
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100499 /* create the new start line */
500 sl->info.res.status = strl2ui(status.ptr, status.len);
501 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200502}
503
504/* Replace the response reason in the HTX message <htx> by <reason>. It returns
505 * 1 on success, otherwise 0.
506*/
507int http_replace_res_reason(struct htx *htx, const struct ist reason)
508{
509 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200510 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100511 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200512
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100513 if (!sl)
514 return 0;
515
Christopher Faulete010c802018-10-24 10:36:45 +0200516 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100517 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
518 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200519
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100520 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
521 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200522
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100523 /* create the new start line */
524 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200525}
526
Christopher Faulet47596d32018-10-22 09:17:28 +0200527/* Replaces a part of a header value referenced in the context <ctx> by
528 * <data>. It returns 1 on success, otherwise it returns 0. The context is
529 * updated if necessary.
530 */
531int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
532{
533 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200534 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200535 char *start;
536 struct ist v;
537 uint32_t len, off;
538
539 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200540 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200541
542 v = htx_get_blk_value(htx, blk);
543 start = ctx->value.ptr - ctx->lws_before;
544 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
545 off = start - v.ptr;
546
547 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
548 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200549 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200550
551 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200552
553 sl = http_get_stline(htx);
554 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
555 struct ist n = htx_get_blk_name(htx, blk);
556
557 if (isteq(n, ist("host"))) {
558 if (!http_update_authority(htx, sl, v))
559 goto fail;
560 ctx->blk = NULL;
561 http_find_header(htx, ist("host"), ctx, 1);
562 blk = ctx->blk;
563 v = htx_get_blk_value(htx, blk);
564 }
565 }
566
Christopher Faulet47596d32018-10-22 09:17:28 +0200567 ctx->blk = blk;
568 ctx->value.ptr = v.ptr + off;
569 ctx->value.len = data.len;
570 ctx->lws_before = ctx->lws_after = 0;
571
572 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200573 fail:
574 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200575}
576
577/* Fully replaces a header referenced in the context <ctx> by the name <name>
578 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
579 * context is updated if necessary.
580 */
581int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
582 const struct ist name, const struct ist value)
583{
584 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200585 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200586
587 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200588 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200589
590 blk = htx_replace_header(htx, blk, name, value);
591 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200592 goto fail;
593
594 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100595 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200596 if (!http_update_authority(htx, sl, value))
597 goto fail;
598 ctx->blk = NULL;
599 http_find_header(htx, ist("host"), ctx, 1);
600 blk = ctx->blk;
601 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200602
603 ctx->blk = blk;
604 ctx->value = ist(NULL);
605 ctx->lws_before = ctx->lws_after = 0;
606
607 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200608 fail:
609 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200610}
611
612/* Remove one value of a header. This only works on a <ctx> returned by
613 * http_find_header function. The value is removed, as well as surrounding commas
614 * if any. If the removed value was alone, the whole header is removed. The
615 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
616 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
617 * form that can be handled by http_find_header() to find next occurrence.
618 */
619int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
620{
621 struct htx_blk *blk = ctx->blk;
622 char *start;
623 struct ist v;
624 uint32_t len;
625
626 if (!blk)
627 return 0;
628
629 start = ctx->value.ptr - ctx->lws_before;
630 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
631
632 v = htx_get_blk_value(htx, blk);
633 if (len == v.len) {
634 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200635 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200636 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100637 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200638 ctx->lws_before = ctx->lws_after = 0;
639 }
640 else {
641 ctx->blk = htx_get_blk(htx, htx->tail);
642 ctx->value = htx_get_blk_value(htx, ctx->blk);
643 ctx->lws_before = ctx->lws_after = 0;
644 }
645 return 1;
646 }
647
648 /* This was not the only value of this header. We have to remove the
649 * part pointed by ctx->value. If it is the last entry of the list, we
650 * remove the last separator.
651 */
652 if (start == v.ptr) {
653 /* It's the first header part but not the only one. So remove
654 * the comma after it. */
655 len++;
656 }
657 else {
658 /* There is at least one header part before the removed one. So
659 * remove the comma between them. */
660 start--;
661 len++;
662 }
663 /* Update the block content and its len */
664 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200665 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200666
667 /* Finally update the ctx */
668 ctx->value.ptr = start;
669 ctx->value.len = 0;
670 ctx->lws_before = ctx->lws_after = 0;
671
672 return 1;
673}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200674
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200675/* Updates the authority part of the uri with the value <host>. It happens when
676 * the header host is modified. It returns 0 on failure and 1 on success. It is
677 * the caller responsibility to provide the start-line and to be sure the uri
678 * contains an authority. Thus, if no authority is found in the uri, an error is
679 * returned.
680 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200681int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200682{
683 struct buffer *temp = get_trash_chunk();
684 struct ist meth, vsn, uri, authority;
685
686 uri = htx_sl_req_uri(sl);
687 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100688 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200689 return 0;
690
Christopher Faulet34b18e42020-02-18 11:02:21 +0100691 /* Don't update the uri if there is no change */
692 if (isteq(host, authority))
693 return 1;
694
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200695 /* Start by copying old method and version */
696 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
697 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
698
699 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
700 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
701
702 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
703 chunk_memcat(temp, host.ptr, host.len);
704 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
705 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
706
707 return http_replace_stline(htx, meth, uri, vsn);
708
709}
710
711/* Update the header host by extracting the authority of the uri <uri>. flags of
712 * the start-line are also updated accordingly. For orgin-form and asterisk-form
713 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
714 * removed from the flags of the start-line. Otherwise, this flag is set and the
715 * authority is used to set the value of the header host. This function returns
716 * 0 on failure and 1 on success.
717*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200718int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200719{
720 struct ist authority;
721 struct http_hdr_ctx ctx;
722
723 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
724 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
725 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
726 }
727 else {
728 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
729 if (sl->info.req.meth != HTTP_METH_CONNECT) {
730 // absolute-form (RFC7320 #5.3.2)
731 sl->flags |= HTX_SL_F_HAS_SCHM;
732 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
733 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
734
735 authority = http_get_authority(uri, 1);
736 if (!authority.len)
737 goto fail;
738 }
739 else {
740 // authority-form (RFC7320 #5.3.3)
741 authority = uri;
742 }
743
744 /* Replace header host value */
745 ctx.blk = NULL;
746 while (http_find_header(htx, ist("host"), &ctx, 1)) {
747 if (!http_replace_header_value(htx, &ctx, authority))
748 goto fail;
749 }
750
751 }
752 return 1;
753 fail:
754 return 0;
755}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200756
757/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
758 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
759 * performed over the whole headers. Otherwise it must contain a valid header
760 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
761 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
762 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
763 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
764 * -1. The value fetch stops at commas, so this function is suited for use with
765 * list headers.
766 * The return value is 0 if nothing was found, or non-zero otherwise.
767 */
768unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
769 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
770{
771 struct http_hdr_ctx local_ctx;
772 struct ist val_hist[MAX_HDR_HISTORY];
773 unsigned int hist_idx;
774 int found;
775
776 if (!ctx) {
777 local_ctx.blk = NULL;
778 ctx = &local_ctx;
779 }
780
781 if (occ >= 0) {
782 /* search from the beginning */
783 while (http_find_header(htx, hdr, ctx, 0)) {
784 occ--;
785 if (occ <= 0) {
786 *vptr = ctx->value.ptr;
787 *vlen = ctx->value.len;
788 return 1;
789 }
790 }
791 return 0;
792 }
793
794 /* negative occurrence, we scan all the list then walk back */
795 if (-occ > MAX_HDR_HISTORY)
796 return 0;
797
798 found = hist_idx = 0;
799 while (http_find_header(htx, hdr, ctx, 0)) {
800 val_hist[hist_idx] = ctx->value;
801 if (++hist_idx >= MAX_HDR_HISTORY)
802 hist_idx = 0;
803 found++;
804 }
805 if (-occ > found)
806 return 0;
807
808 /* OK now we have the last occurrence in [hist_idx-1], and we need to
809 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
810 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
811 * to remain in the 0..9 range.
812 */
813 hist_idx += occ + MAX_HDR_HISTORY;
814 if (hist_idx >= MAX_HDR_HISTORY)
815 hist_idx -= MAX_HDR_HISTORY;
816 *vptr = val_hist[hist_idx].ptr;
817 *vlen = val_hist[hist_idx].len;
818 return 1;
819}
820
821/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
822 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
823 * performed over the whole headers. Otherwise it must contain a valid header
824 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
825 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
826 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
827 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
828 * -1. This function differs from http_get_hdr() in that it only returns full
829 * line header values and does not stop at commas.
830 * The return value is 0 if nothing was found, or non-zero otherwise.
831 */
832unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
833 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
834{
835 struct http_hdr_ctx local_ctx;
836 struct ist val_hist[MAX_HDR_HISTORY];
837 unsigned int hist_idx;
838 int found;
839
840 if (!ctx) {
841 local_ctx.blk = NULL;
842 ctx = &local_ctx;
843 }
844
845 if (occ >= 0) {
846 /* search from the beginning */
847 while (http_find_header(htx, hdr, ctx, 1)) {
848 occ--;
849 if (occ <= 0) {
850 *vptr = ctx->value.ptr;
851 *vlen = ctx->value.len;
852 return 1;
853 }
854 }
855 return 0;
856 }
857
858 /* negative occurrence, we scan all the list then walk back */
859 if (-occ > MAX_HDR_HISTORY)
860 return 0;
861
862 found = hist_idx = 0;
863 while (http_find_header(htx, hdr, ctx, 1)) {
864 val_hist[hist_idx] = ctx->value;
865 if (++hist_idx >= MAX_HDR_HISTORY)
866 hist_idx = 0;
867 found++;
868 }
869 if (-occ > found)
870 return 0;
871
872 /* OK now we have the last occurrence in [hist_idx-1], and we need to
873 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
874 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
875 * to remain in the 0..9 range.
876 */
877 hist_idx += occ + MAX_HDR_HISTORY;
878 if (hist_idx >= MAX_HDR_HISTORY)
879 hist_idx -= MAX_HDR_HISTORY;
880 *vptr = val_hist[hist_idx].ptr;
881 *vlen = val_hist[hist_idx].len;
882 return 1;
883}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100884
Christopher Faulet90cc4812019-07-22 16:49:30 +0200885int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100886{
887 struct htx *htx;
888 struct htx_sl *sl;
889 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200890 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100891 union h1_sl h1sl;
892 unsigned int flags = HTX_SL_F_IS_RESP;
893 int ret = 0;
894
Christopher Faulet90cc4812019-07-22 16:49:30 +0200895 b_reset(buf);
896 if (!raw.len) {
897 buf->size = 0;
898 buf->area = malloc(raw.len);
899 return 1;
900 }
901
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100902 buf->size = global.tune.bufsize;
903 buf->area = (char *)malloc(buf->size);
904 if (!buf->area)
905 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100906
907 h1m_init_res(&h1m);
908 h1m.flags |= H1_MF_NO_PHDR;
909 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
910 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
911 if (ret <= 0)
912 goto error;
913
914 if (unlikely(h1sl.st.v.len != 8))
915 goto error;
916 if ((*(h1sl.st.v.ptr + 5) > '1') ||
917 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
918 h1m.flags |= H1_MF_VER_11;
919
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200920 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
921 goto error;
922
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100923 if (h1m.flags & H1_MF_VER_11)
924 flags |= HTX_SL_F_VER_11;
925 if (h1m.flags & H1_MF_XFER_ENC)
926 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200927 if (h1m.flags & H1_MF_CLEN) {
928 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
929 if (h1m.body_len == 0)
930 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100931 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200932 if (h1m.flags & H1_MF_CHNK)
933 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100934
935 htx = htx_from_buf(buf);
936 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
937 if (!sl || !htx_add_all_headers(htx, hdrs))
938 goto error;
939 sl->info.res.status = h1sl.st.status;
940
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200941 while (raw.len > ret) {
942 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
943 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100944 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200945 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100946 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200947
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100948 if (!htx_add_endof(htx, HTX_BLK_EOM))
949 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200950
Christopher Faulet90cc4812019-07-22 16:49:30 +0200951 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100952
953error:
954 if (buf->size)
955 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200956 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100957}
958
Christopher Faulet18630642020-05-12 18:57:28 +0200959void release_http_reply(struct http_reply *http_reply)
960{
961 struct logformat_node *lf, *lfb;
962 struct http_reply_hdr *hdr, *hdrb;
963
964 if (!http_reply)
965 return;
966
967 free(http_reply->ctype);
968 http_reply->ctype = NULL;
969 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
970 LIST_DEL(&hdr->list);
971 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
972 LIST_DEL(&lf->list);
973 release_sample_expr(lf->expr);
974 free(lf->arg);
975 free(lf);
976 }
977 istfree(&hdr->name);
978 free(hdr);
979 }
980
981 if (http_reply->type == HTTP_REPLY_ERRFILES) {
982 free(http_reply->body.http_errors);
983 http_reply->body.http_errors = NULL;
984 }
985 else if (http_reply->type == HTTP_REPLY_RAW)
986 chunk_destroy(&http_reply->body.obj);
987 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
988 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
989 LIST_DEL(&lf->list);
990 release_sample_expr(lf->expr);
991 free(lf->arg);
992 free(lf);
993 }
994 }
995}
996
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100997static int http_htx_init(void)
998{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100999 struct buffer chk;
1000 struct ist raw;
1001 int rc;
1002 int err_code = 0;
1003
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001004 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1005 if (!http_err_msgs[rc]) {
1006 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1007 err_code |= ERR_ALERT | ERR_FATAL;
1008 continue;
1009 }
1010
1011 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1012 if (!http_str_to_htx(&chk, raw)) {
1013 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1014 http_err_codes[rc]);
1015 err_code |= ERR_ALERT | ERR_FATAL;
1016 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001017 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001018 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1019 http_err_replies[rc].status = http_err_codes[rc];
1020 http_err_replies[rc].ctype = NULL;
1021 LIST_INIT(&http_err_replies[rc].hdrs);
1022 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001023 }
1024end:
1025 return err_code;
1026}
1027
Christopher Faulet58857752020-01-15 15:19:50 +01001028static void http_htx_deinit(void)
1029{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001030 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +01001031 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001032 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001033
1034 node = ebpt_first(&http_error_messages);
1035 while (node) {
1036 next = ebpt_next(node);
1037 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001038 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1039 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001040 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001041 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001042 node = next;
1043 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001044
1045 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1046 free(http_errs->conf.file);
1047 free(http_errs->id);
1048 LIST_DEL(&http_errs->list);
1049 free(http_errs);
1050 }
Christopher Faulet58857752020-01-15 15:19:50 +01001051}
1052
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001053REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001054REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001055
Christopher Faulet58857752020-01-15 15:19:50 +01001056/* Reads content of the error file <file> and convert it into an HTX message. On
1057 * success, the HTX message is returned. On error, NULL is returned and an error
1058 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001059 */
Christopher Faulet58857752020-01-15 15:19:50 +01001060struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001061{
Christopher Faulet58857752020-01-15 15:19:50 +01001062 struct buffer *buf = NULL;
1063 struct buffer chk;
1064 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001065 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001066 struct stat stat;
1067 char *err = NULL;
1068 int errnum, errlen;
1069 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001070
1071 /* already loaded */
1072 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1073 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001074 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1075 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001076 goto out;
1077 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001078
Christopher Faulet58857752020-01-15 15:19:50 +01001079 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001080 fd = open(file, O_RDONLY);
1081 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1082 memprintf(errmsg, "error opening file '%s'.", file);
1083 goto out;
1084 }
1085
1086 if (stat.st_size <= global.tune.bufsize)
1087 errlen = stat.st_size;
1088 else {
1089 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1090 file, global.tune.bufsize);
1091 errlen = global.tune.bufsize;
1092 }
1093
1094 err = malloc(errlen);
1095 if (!err) {
1096 memprintf(errmsg, "out of memory.");
1097 goto out;
1098 }
1099
1100 errnum = read(fd, err, errlen);
1101 if (errnum != errlen) {
1102 memprintf(errmsg, "error reading file '%s'.", file);
1103 goto out;
1104 }
1105
Christopher Faulet58857752020-01-15 15:19:50 +01001106 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001107 http_errmsg = calloc(1, sizeof(*http_errmsg));
1108 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001109 memprintf(errmsg, "out of memory.");
1110 goto out;
1111 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001112 http_errmsg->node.key = strdup(file);
1113 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001114 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001115 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001116 goto out;
1117 }
1118
1119 /* Convert the error file into an HTX message */
1120 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001121 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001122 free(http_errmsg->node.key);
1123 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001124 goto out;
1125 }
1126
Christopher Faulet58857752020-01-15 15:19:50 +01001127 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001128 http_errmsg->msg = chk;
1129 ebis_insert(&http_error_messages, &http_errmsg->node);
1130 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001131
Christopher Faulet5031ef52020-01-15 11:22:07 +01001132 out:
1133 if (fd >= 0)
1134 close(fd);
1135 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001136 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001137}
1138
Ilya Shipitsind4259502020-04-08 01:07:56 +05001139/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001140 * message is returned. On error, NULL is returned and an error message is
1141 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001142 */
Christopher Faulet58857752020-01-15 15:19:50 +01001143struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001144{
Christopher Faulet58857752020-01-15 15:19:50 +01001145 struct buffer *buf = NULL;
1146 struct buffer chk;
1147 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001148 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001149
1150 /* already loaded */
1151 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1152 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001153 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1154 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001155 goto out;
1156 }
1157 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001158 http_errmsg = calloc(1, sizeof(*http_errmsg));
1159 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001160 memprintf(errmsg, "out of memory.");
1161 goto out;
1162 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001163 http_errmsg->node.key = strdup(key);
1164 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001165 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001166 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001167 goto out;
1168 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001169
1170 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001171 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001172 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001173 free(http_errmsg->node.key);
1174 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001175 goto out;
1176 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001177
Christopher Faulet58857752020-01-15 15:19:50 +01001178 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001179 http_errmsg->msg = chk;
1180 ebis_insert(&http_error_messages, &http_errmsg->node);
1181 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001182 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001183 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001184}
1185
Christopher Faulet5031ef52020-01-15 11:22:07 +01001186/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001187 * <status>. It returns NULL if there is any error, otherwise it return the
1188 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001189 */
Christopher Faulet58857752020-01-15 15:19:50 +01001190struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001191{
Christopher Faulet58857752020-01-15 15:19:50 +01001192 struct buffer *buf = NULL;
1193 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001194
1195 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1196 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001197 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001198 break;
1199 }
1200 }
1201
1202 if (rc >= HTTP_ERR_SIZE)
1203 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001204 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001205}
1206
1207/* This function creates HTX error message corresponding to a redirect message
1208 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001209 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1210 * returns NULL if there is any error, otherwise it return the corresponding HTX
1211 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001212 */
Christopher Faulet58857752020-01-15 15:19:50 +01001213struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001214{
Christopher Faulet58857752020-01-15 15:19:50 +01001215 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001216 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001217 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001218 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001219
1220 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1221 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001222 /* Create the error key */
1223 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1224 memprintf(errmsg, "out of memory.");
1225 goto out;
1226 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001227 /* Create the error message */
1228 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1229 errlen = strlen(msg) + strlen(url) + 5;
1230 err = malloc(errlen);
1231 if (!err) {
1232 memprintf(errmsg, "out of memory.");
1233 goto out;
1234 }
1235 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1236
1237 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001238 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001239 break;
1240 }
1241 }
1242
1243 if (rc >= HTTP_ERR_SIZE)
1244 memprintf(errmsg, "status code '%d' not handled.", status);
1245out:
Christopher Faulet58857752020-01-15 15:19:50 +01001246 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001247 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001248 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001249}
1250
Christopher Faulet7eea2412020-05-13 15:02:59 +02001251/* Check an "http reply" and, for replies referencing an http-errors section,
1252 * try to find the right section and the right error message in this section. If
1253 * found, the reply is updated. If the http-errors section exists but the error
1254 * message is not found, no error message is set to fallback on the default
1255 * ones. Otherwise (unknown section) an error is returned.
1256 *
1257 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1258 * filled.
1259 */
1260int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1261{
1262 struct http_errors *http_errs;
1263 int ret = 1;
1264
1265 if (reply->type != HTTP_REPLY_ERRFILES)
1266 goto end;
1267
1268 list_for_each_entry(http_errs, &http_errors_list, list) {
1269 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
1270 reply->type = HTTP_REPLY_ERRMSG;
1271 free(reply->body.http_errors);
1272 reply->body.errmsg = http_errs->errmsg[http_get_status_idx(reply->status)];
1273 if (!reply->body.errmsg)
1274 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1275 "not declared in http-errors section '%s'.\n",
1276 px->id, reply->status, http_errs->id);
1277 break;
1278 }
1279 }
1280
1281 if (&http_errs->list == &http_errors_list) {
1282 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1283 reply->body.http_errors);
1284 ret = 0;
1285 }
1286
1287 end:
1288 return ret;
1289}
1290
Christopher Faulet47e791e2020-05-13 14:36:55 +02001291/* Parse an "http reply". It returns the reply on success or NULL on error. This
1292 * function creates one of the following http replies :
1293 *
1294 * - HTTP_REPLY_EMPTY : dummy response, no payload
1295 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1296 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1297 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1298 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1299 *
1300 * The content-type must be defined for non-empty payload. It is ignored for
1301 * error messages (implicit or explicit). When an http-errors section is
1302 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1303 * during the configuration validity check or dynamically. It is the caller
1304 * responsibility to choose. If no status code is configured, <default_status>
1305 * is set.
1306 */
1307struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1308 int default_status, char **errmsg)
1309{
1310 struct logformat_node *lf, *lfb;
1311 struct http_reply *reply = NULL;
1312 struct http_reply_hdr *hdr, *hdrb;
1313 struct stat stat;
1314 const char *act_arg = NULL;
1315 char *obj = NULL;
1316 int cur_arg, cap, objlen = 0, fd = -1;
1317
1318
1319 reply = calloc(1, sizeof(*reply));
1320 if (!reply) {
1321 memprintf(errmsg, "out of memory");
1322 goto error;
1323 }
1324 LIST_INIT(&reply->hdrs);
1325 reply->type = HTTP_REPLY_EMPTY;
1326 reply->status = default_status;
1327
1328 cap = ((px->conf.args.ctx == ARGC_HRQ)
1329 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1330 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
1331
1332 cur_arg = *orig_arg;
1333 while (*args[cur_arg]) {
1334 if (strcmp(args[cur_arg], "status") == 0) {
1335 cur_arg++;
1336 if (!*args[cur_arg]) {
1337 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1338 goto error;
1339 }
1340 reply->status = atol(args[cur_arg]);
1341 if (reply->status < 200 || reply->status > 599) {
1342 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1343 goto error;
1344 }
1345 cur_arg++;
1346 }
1347 else if (strcmp(args[cur_arg], "content-type") == 0) {
1348 cur_arg++;
1349 if (!*args[cur_arg]) {
1350 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1351 goto error;
1352 }
1353 free(reply->ctype);
1354 reply->ctype = strdup(args[cur_arg]);
1355 cur_arg++;
1356 }
1357 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1358 if (reply->type != HTTP_REPLY_EMPTY) {
1359 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1360 goto error;
1361 }
1362 act_arg = args[cur_arg];
1363 cur_arg++;
1364 if (!*args[cur_arg]) {
1365 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1366 goto error;
1367 }
1368 reply->body.http_errors = strdup(args[cur_arg]);
1369 if (!reply->body.http_errors) {
1370 memprintf(errmsg, "out of memory");
1371 goto error;
1372 }
1373 reply->type = HTTP_REPLY_ERRFILES;
1374 cur_arg++;
1375 }
1376 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1377 if (reply->type != HTTP_REPLY_EMPTY) {
1378 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1379 goto error;
1380 }
1381 act_arg = args[cur_arg];
1382 reply->type = HTTP_REPLY_ERRMSG;
1383 cur_arg++;
1384 }
1385 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1386 if (reply->type != HTTP_REPLY_EMPTY) {
1387 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1388 goto error;
1389 }
1390 act_arg = args[cur_arg];
1391 cur_arg++;
1392 if (!*args[cur_arg]) {
1393 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1394 goto error;
1395 }
1396 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1397 if (!reply->body.errmsg) {
1398 goto error;
1399 }
1400 reply->type = HTTP_REPLY_ERRMSG;
1401 cur_arg++;
1402 }
1403 else if (strcmp(args[cur_arg], "file") == 0) {
1404 if (reply->type != HTTP_REPLY_EMPTY) {
1405 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1406 goto error;
1407 }
1408 act_arg = args[cur_arg];
1409 cur_arg++;
1410 if (!*args[cur_arg]) {
1411 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1412 goto error;
1413 }
1414 fd = open(args[cur_arg], O_RDONLY);
1415 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1416 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1417 goto error;
1418 }
1419 if (stat.st_size > global.tune.bufsize) {
1420 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1421 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1422 goto error;
1423 }
1424 objlen = stat.st_size;
1425 obj = malloc(objlen);
1426 if (!obj || read(fd, obj, objlen) != objlen) {
1427 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1428 goto error;
1429 }
1430 close(fd);
1431 fd = -1;
1432 reply->type = HTTP_REPLY_RAW;
1433 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1434 obj = NULL;
1435 cur_arg++;
1436 }
1437 else if (strcmp(args[cur_arg], "string") == 0) {
1438 if (reply->type != HTTP_REPLY_EMPTY) {
1439 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1440 goto error;
1441 }
1442 act_arg = args[cur_arg];
1443 cur_arg++;
1444 if (!*args[cur_arg]) {
1445 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1446 goto error;
1447 }
1448 obj = strdup(args[cur_arg]);
1449 objlen = strlen(args[cur_arg]);
1450 if (!obj) {
1451 memprintf(errmsg, "out of memory");
1452 goto error;
1453 }
1454 reply->type = HTTP_REPLY_RAW;
1455 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1456 obj = NULL;
1457 cur_arg++;
1458 }
1459 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1460 if (reply->type != HTTP_REPLY_EMPTY) {
1461 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1462 goto error;
1463 }
1464 act_arg = args[cur_arg];
1465 cur_arg++;
1466 if (!*args[cur_arg]) {
1467 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1468 goto error;
1469 }
1470 fd = open(args[cur_arg], O_RDONLY);
1471 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1472 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1473 goto error;
1474 }
1475 if (stat.st_size > global.tune.bufsize) {
1476 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1477 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1478 goto error;
1479 }
1480 objlen = stat.st_size;
1481 obj = malloc(objlen + 1);
1482 if (!obj || read(fd, obj, objlen) != objlen) {
1483 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1484 goto error;
1485 }
1486 close(fd);
1487 fd = -1;
1488 obj[objlen] = '\0';
1489 reply->type = HTTP_REPLY_LOGFMT;
1490 cur_arg++;
1491 }
1492 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1493 if (reply->type != HTTP_REPLY_EMPTY) {
1494 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1495 goto error;
1496 }
1497 act_arg = args[cur_arg];
1498 cur_arg++;
1499 if (!*args[cur_arg]) {
1500 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1501 goto error;
1502 }
1503 obj = strdup(args[cur_arg]);
1504 objlen = strlen(args[cur_arg]);
1505 reply->type = HTTP_REPLY_LOGFMT;
1506 cur_arg++;
1507 }
1508 else if (strcmp(args[cur_arg], "hdr") == 0) {
1509 cur_arg++;
1510 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1511 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1512 goto error;
1513 }
1514 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1515 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1516 strcasecmp(args[cur_arg], "content-type") == 0) {
1517 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1518 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1519 cur_arg += 2;
1520 continue;
1521 }
1522 hdr = calloc(1, sizeof(*hdr));
1523 if (!hdr) {
1524 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1525 goto error;
1526 }
1527 LIST_INIT(&hdr->value);
1528 hdr->name = ist(strdup(args[cur_arg]));
1529 if (!isttest(hdr->name)) {
1530 memprintf(errmsg, "out of memory");
1531 goto error;
1532 }
1533 LIST_ADDQ(&reply->hdrs, &hdr->list);
1534 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1535 goto error;
1536
1537 free(px->conf.lfs_file);
1538 px->conf.lfs_file = strdup(px->conf.args.file);
1539 px->conf.lfs_line = px->conf.args.line;
1540 cur_arg += 2;
1541 }
1542 else
1543 break;
1544 }
1545
1546 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1547 if (reply->ctype) {
1548 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1549 " neither errorfile nor payload defined.\n",
1550 px->conf.args.file, px->conf.args.line, reply->ctype);
1551 free(reply->ctype);
1552 reply->ctype = NULL;
1553 }
1554 }
1555 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1556
1557 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1558 /* default errorfile or errorfiles: check the status */
1559 int rc;
1560
1561 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1562 if (http_err_codes[rc] == reply->status)
1563 break;
1564 }
1565
1566 if (rc >= HTTP_ERR_SIZE) {
1567 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1568 reply->status, act_arg);
1569 goto error;
1570 }
1571 }
1572
1573 if (reply->ctype) {
1574 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1575 "with an erorrfile.\n",
1576 px->conf.args.file, px->conf.args.line, reply->ctype);
1577 free(reply->ctype);
1578 reply->ctype = NULL;
1579 }
1580 if (!LIST_ISEMPTY(&reply->hdrs)) {
1581 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1582 "with an erorrfile.\n",
1583 px->conf.args.file, px->conf.args.line);
1584 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1585 LIST_DEL(&hdr->list);
1586 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1587 LIST_DEL(&lf->list);
1588 release_sample_expr(lf->expr);
1589 free(lf->arg);
1590 free(lf);
1591 }
1592 istfree(&hdr->name);
1593 free(hdr);
1594 }
1595 }
1596 }
1597 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1598 if (!reply->ctype && objlen) {
1599 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1600 goto error;
1601 }
1602 if (reply->ctype && !b_data(&reply->body.obj)) {
1603 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1604 "with an emtpy payload.\n",
1605 px->conf.args.file, px->conf.args.line, reply->ctype);
1606 free(reply->ctype);
1607 reply->ctype = NULL;
1608 }
1609 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1610 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1611 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1612 px->conf.args.file, px->conf.args.line);
1613 }
1614 }
1615 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1616 LIST_INIT(&reply->body.fmt);
1617 if (!reply->ctype) {
1618 memprintf(errmsg, "a content type must be defined with a log-format payload");
1619 goto error;
1620 }
1621 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1622 goto error;
1623
1624 free(px->conf.lfs_file);
1625 px->conf.lfs_file = strdup(px->conf.args.file);
1626 px->conf.lfs_line = px->conf.args.line;
1627 }
1628
1629 free(obj);
1630 *orig_arg = cur_arg;
1631 return reply;
1632
1633 error:
1634 free(obj);
1635 if (fd >= 0)
1636 close(fd);
1637 release_http_reply(reply);
1638 return NULL;
1639}
1640
Christopher Faulet07f41f72020-01-16 16:16:06 +01001641/* Parses the "errorloc[302|303]" proxy keyword */
1642static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1643 struct proxy *defpx, const char *file, int line,
1644 char **errmsg)
1645{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001646 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001647 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001648 int errloc, status;
1649 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001650
1651 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1652 ret = 1;
1653 goto out;
1654 }
1655
1656 if (*(args[1]) == 0 || *(args[2]) == 0) {
1657 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1658 ret = -1;
1659 goto out;
1660 }
1661
1662 status = atol(args[1]);
1663 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1664 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1665 if (!msg) {
1666 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1667 ret = -1;
1668 goto out;
1669 }
1670
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001671 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);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001683
1684 out:
1685 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001686
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001687}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001688
1689/* Parses the "errorfile" proxy keyword */
1690static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1691 struct proxy *defpx, const char *file, int line,
1692 char **errmsg)
1693{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001694 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001695 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001696 int status;
1697 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001698
1699 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1700 ret = 1;
1701 goto out;
1702 }
1703
1704 if (*(args[1]) == 0 || *(args[2]) == 0) {
1705 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1706 ret = -1;
1707 goto out;
1708 }
1709
1710 status = atol(args[1]);
1711 msg = http_parse_errorfile(status, args[2], errmsg);
1712 if (!msg) {
1713 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1714 ret = -1;
1715 goto out;
1716 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001717
1718 conf_err = calloc(1, sizeof(*conf_err));
1719 if (!conf_err) {
1720 memprintf(errmsg, "%s : out of memory.", args[0]);
1721 ret = -1;
1722 goto out;
1723 }
1724 conf_err->type = 1;
1725 conf_err->info.errorfile.status = status;
1726 conf_err->info.errorfile.msg = msg;
1727 conf_err->file = strdup(file);
1728 conf_err->line = line;
1729 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1730
1731 out:
1732 return ret;
1733
1734}
1735
1736/* Parses the "errorfiles" proxy keyword */
1737static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1738 struct proxy *defpx, const char *file, int line,
1739 char **err)
1740{
1741 struct conf_errors *conf_err = NULL;
1742 char *name = NULL;
1743 int rc, ret = 0;
1744
1745 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1746 ret = 1;
1747 goto out;
1748 }
1749
1750 if (!*(args[1])) {
1751 memprintf(err, "%s : expects <name> as argument.", args[0]);
1752 ret = -1;
1753 goto out;
1754 }
1755
1756 name = strdup(args[1]);
1757 conf_err = calloc(1, sizeof(*conf_err));
1758 if (!name || !conf_err) {
1759 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001760 goto error;
1761 }
1762 conf_err->type = 0;
1763
1764 conf_err->info.errorfiles.name = name;
1765 if (!*(args[2])) {
1766 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1767 conf_err->info.errorfiles.status[rc] = 1;
1768 }
1769 else {
1770 int cur_arg, status;
1771 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1772 status = atol(args[cur_arg]);
1773
1774 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1775 if (http_err_codes[rc] == status) {
1776 conf_err->info.errorfiles.status[rc] = 2;
1777 break;
1778 }
1779 }
1780 if (rc >= HTTP_ERR_SIZE) {
1781 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001782 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001783 }
1784 }
1785 }
1786 conf_err->file = strdup(file);
1787 conf_err->line = line;
1788 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1789 out:
1790 return ret;
1791
1792 error:
1793 free(name);
1794 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001795 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001796 goto out;
1797}
1798
1799/* Check "errorfiles" proxy keyword */
1800static int proxy_check_errors(struct proxy *px)
1801{
1802 struct conf_errors *conf_err, *conf_err_back;
1803 struct http_errors *http_errs;
1804 int rc, err = 0;
1805
1806 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1807 if (conf_err->type == 1) {
1808 /* errorfile */
1809 rc = http_get_status_idx(conf_err->info.errorfile.status);
1810 px->errmsg[rc] = conf_err->info.errorfile.msg;
1811 }
1812 else {
1813 /* errorfiles */
1814 list_for_each_entry(http_errs, &http_errors_list, list) {
1815 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1816 break;
1817 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001818
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001819 /* unknown http-errors section */
1820 if (&http_errs->list == &http_errors_list) {
1821 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1822 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1823 err |= ERR_ALERT | ERR_FATAL;
1824 free(conf_err->info.errorfiles.name);
1825 goto next;
1826 }
1827
1828 free(conf_err->info.errorfiles.name);
1829 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1830 if (conf_err->info.errorfiles.status[rc] > 0) {
1831 if (http_errs->errmsg[rc])
1832 px->errmsg[rc] = http_errs->errmsg[rc];
1833 else if (conf_err->info.errorfiles.status[rc] == 2)
1834 ha_warning("config: proxy '%s' : status '%d' not declared in"
1835 " http-errors section '%s' (at %s:%d).\n",
1836 px->id, http_err_codes[rc], http_errs->id,
1837 conf_err->file, conf_err->line);
1838 }
1839 }
1840 }
1841 next:
1842 LIST_DEL(&conf_err->list);
1843 free(conf_err->file);
1844 free(conf_err);
1845 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001846
1847 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001848 return err;
1849}
1850
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001851static int post_check_errors()
1852{
1853 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001854 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001855 struct htx *htx;
1856 int err_code = 0;
1857
1858 node = ebpt_first(&http_error_messages);
1859 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001860 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1861 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001862 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001863 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001864 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1865 ha_warning("config: errorfile '%s' runs over the buffer space"
1866 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001867 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001868 (char *)node->key);
1869 err_code |= ERR_WARN;
1870 }
1871 next:
1872 node = ebpt_next(node);
1873 }
1874
1875 return err_code;
1876}
1877
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001878int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1879{
1880 struct conf_errors *conf_err, *new_conf_err = NULL;
1881 int ret = 0;
1882
1883 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1884 new_conf_err = calloc(1, sizeof(*new_conf_err));
1885 if (!new_conf_err) {
1886 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1887 goto out;
1888 }
1889 new_conf_err->type = conf_err->type;
1890 if (conf_err->type == 1) {
1891 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1892 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1893 }
1894 else {
1895 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1896 if (!new_conf_err->info.errorfiles.name) {
1897 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1898 goto out;
1899 }
1900 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1901 sizeof(conf_err->info.errorfiles.status));
1902 }
1903 new_conf_err->file = strdup(conf_err->file);
1904 new_conf_err->line = conf_err->line;
1905 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1906 new_conf_err = NULL;
1907 }
1908 ret = 1;
1909
1910 out:
1911 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001912 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001913}
1914
1915void proxy_release_conf_errors(struct proxy *px)
1916{
1917 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001918
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001919 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1920 if (conf_err->type == 0)
1921 free(conf_err->info.errorfiles.name);
1922 LIST_DEL(&conf_err->list);
1923 free(conf_err->file);
1924 free(conf_err);
1925 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001926}
1927
1928/*
1929 * Parse an <http-errors> section.
1930 * Returns the error code, 0 if OK, or any combination of :
1931 * - ERR_ABORT: must abort ASAP
1932 * - ERR_FATAL: we can continue parsing but not start the service
1933 * - ERR_WARN: a warning has been emitted
1934 * - ERR_ALERT: an alert has been emitted
1935 * Only the two first ones can stop processing, the two others are just
1936 * indicators.
1937 */
1938static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1939{
1940 static struct http_errors *curr_errs = NULL;
1941 int err_code = 0;
1942 const char *err;
1943 char *errmsg = NULL;
1944
1945 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1946 if (!*args[1]) {
1947 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1948 err_code |= ERR_ALERT | ERR_ABORT;
1949 goto out;
1950 }
1951
1952 err = invalid_char(args[1]);
1953 if (err) {
1954 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1955 file, linenum, *err, args[0], args[1]);
1956 err_code |= ERR_ALERT | ERR_FATAL;
1957 }
1958
1959 list_for_each_entry(curr_errs, &http_errors_list, list) {
1960 /* Error if two errors section owns the same name */
1961 if (strcmp(curr_errs->id, args[1]) == 0) {
1962 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1963 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1964 err_code |= ERR_ALERT | ERR_FATAL;
1965 }
1966 }
1967
1968 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1969 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1970 err_code |= ERR_ALERT | ERR_ABORT;
1971 goto out;
1972 }
1973
1974 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1975 curr_errs->id = strdup(args[1]);
1976 curr_errs->conf.file = strdup(file);
1977 curr_errs->conf.line = linenum;
1978 }
1979 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1980 struct buffer *msg;
1981 int status, rc;
1982
1983 if (*(args[1]) == 0 || *(args[2]) == 0) {
1984 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1985 file, linenum, args[0]);
1986 err_code |= ERR_ALERT | ERR_FATAL;
1987 goto out;
1988 }
1989
1990 status = atol(args[1]);
1991 msg = http_parse_errorfile(status, args[2], &errmsg);
1992 if (!msg) {
1993 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1994 err_code |= ERR_ALERT | ERR_FATAL;
1995 goto out;
1996 }
1997 rc = http_get_status_idx(status);
1998 curr_errs->errmsg[rc] = msg;
1999 }
2000 else if (*args[0] != 0) {
2001 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2002 err_code |= ERR_ALERT | ERR_FATAL;
2003 goto out;
2004 }
2005
2006out:
2007 free(errmsg);
2008 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002009}
2010
2011static struct cfg_kw_list cfg_kws = {ILH, {
2012 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2013 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2014 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2015 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002016 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002017 { 0, NULL, NULL },
2018}};
2019
2020INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002021REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002022REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002023
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002024REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2025
Christopher Faulet29f72842019-12-11 15:52:32 +01002026/************************************************************************/
2027/* HTX sample fetches */
2028/************************************************************************/
2029
2030/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2031static int
2032smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2033{
2034 if (!smp->strm)
2035 return 0;
2036
2037 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2038 smp->data.type = SMP_T_BOOL;
2039 return 1;
2040}
2041
2042/* Returns the number of blocks in an HTX message. The channel is chosen
2043 * depending on the sample direction. */
2044static int
2045smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2046{
2047 struct channel *chn;
2048 struct htx *htx;
2049
2050 if (!smp->strm)
2051 return 0;
2052
2053 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002054 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002055 if (!htx)
2056 return 0;
2057
2058 smp->data.u.sint = htx_nbblks(htx);
2059 smp->data.type = SMP_T_SINT;
2060 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2061 return 1;
2062}
2063
2064/* Returns the size of an HTX message. The channel is chosen depending on the
2065 * sample direction. */
2066static int
2067smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2068{
2069 struct channel *chn;
2070 struct htx *htx;
2071
2072 if (!smp->strm)
2073 return 0;
2074
2075 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002076 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002077 if (!htx)
2078 return 0;
2079
2080 smp->data.u.sint = htx->size;
2081 smp->data.type = SMP_T_SINT;
2082 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2083 return 1;
2084}
2085
2086/* Returns the data size of an HTX message. The channel is chosen depending on the
2087 * sample direction. */
2088static int
2089smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2090{
2091 struct channel *chn;
2092 struct htx *htx;
2093
2094 if (!smp->strm)
2095 return 0;
2096
2097 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002098 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002099 if (!htx)
2100 return 0;
2101
2102 smp->data.u.sint = htx->data;
2103 smp->data.type = SMP_T_SINT;
2104 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2105 return 1;
2106}
2107
2108/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2109 * depending on the sample direction. */
2110static int
2111smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2112{
2113 struct channel *chn;
2114 struct htx *htx;
2115
2116 if (!smp->strm)
2117 return 0;
2118
2119 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002120 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002121 if (!htx)
2122 return 0;
2123
2124 smp->data.u.sint = htx_used_space(htx);
2125 smp->data.type = SMP_T_SINT;
2126 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2127 return 1;
2128}
2129
2130/* Returns the free space (size-used) of an HTX message. The channel is chosen
2131 * depending on the sample direction. */
2132static int
2133smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2134{
2135 struct channel *chn;
2136 struct htx *htx;
2137
2138 if (!smp->strm)
2139 return 0;
2140
2141 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002142 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002143 if (!htx)
2144 return 0;
2145
2146 smp->data.u.sint = htx_free_space(htx);
2147 smp->data.type = SMP_T_SINT;
2148 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2149 return 1;
2150}
2151
2152/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2153 * channel is chosen depending on the sample direction. */
2154static int
2155smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2156{
2157 struct channel *chn;
2158 struct htx *htx;
2159
2160 if (!smp->strm)
2161 return 0;
2162
2163 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002164 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002165 if (!htx)
2166 return 0;
2167
2168 smp->data.u.sint = htx_free_data_space(htx);
2169 smp->data.type = SMP_T_SINT;
2170 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2171 return 1;
2172}
2173
2174/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2175 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2176 * the sample direction. */
2177static int
2178smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2179{
2180 struct channel *chn;
2181 struct htx *htx;
2182
2183 if (!smp->strm)
2184 return 0;
2185
2186 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002187 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002188 if (!htx)
2189 return 0;
2190
2191 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2192 smp->data.type = SMP_T_BOOL;
2193 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2194 return 1;
2195}
2196
2197/* Returns the type of a specific HTX block, if found in the message. Otherwise
2198 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2199 * "head", "tail" or "first". The channel is chosen depending on the sample
2200 * direction. */
2201static int
2202smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2203{
2204 struct channel *chn;
2205 struct htx *htx;
2206 enum htx_blk_type type;
2207 int32_t pos;
2208
2209 if (!smp->strm || !arg_p)
2210 return 0;
2211
2212 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002213 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002214 if (!htx)
2215 return 0;
2216
2217 pos = arg_p[0].data.sint;
2218 if (pos == -1)
2219 type = htx_get_head_type(htx);
2220 else if (pos == -2)
2221 type = htx_get_tail_type(htx);
2222 else if (pos == -3)
2223 type = htx_get_first_type(htx);
2224 else
2225 type = ((pos >= htx->head && pos <= htx->tail)
2226 ? htx_get_blk_type(htx_get_blk(htx, pos))
2227 : HTX_BLK_UNUSED);
2228
2229 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2230 smp->data.type = SMP_T_STR;
2231 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2232 return 1;
2233}
2234
2235/* Returns the size of a specific HTX block, if found in the message. Otherwise
2236 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2237 * "first". The channel is chosen depending on the sample direction. */
2238static int
2239smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2240{
2241 struct channel *chn;
2242 struct htx *htx;
2243 struct htx_blk *blk;
2244 int32_t pos;
2245
2246 if (!smp->strm || !arg_p)
2247 return 0;
2248
2249 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002250 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002251 if (!htx)
2252 return 0;
2253
2254 pos = arg_p[0].data.sint;
2255 if (pos == -1)
2256 blk = htx_get_head_blk(htx);
2257 else if (pos == -2)
2258 blk = htx_get_tail_blk(htx);
2259 else if (pos == -3)
2260 blk = htx_get_first_blk(htx);
2261 else
2262 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2263
2264 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2265 smp->data.type = SMP_T_SINT;
2266 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2267 return 1;
2268}
2269
2270/* Returns the start-line if the selected HTX block exists and is a
2271 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2272 * supported or "head", "tail" or "first". The channel is chosen depending on
2273 * the sample direction. */
2274static int
2275smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2276{
2277 struct buffer *temp;
2278 struct channel *chn;
2279 struct htx *htx;
2280 struct htx_blk *blk;
2281 struct htx_sl *sl;
2282 int32_t pos;
2283
2284 if (!smp->strm || !arg_p)
2285 return 0;
2286
2287 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002288 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002289 if (!htx)
2290 return 0;
2291
2292 pos = arg_p[0].data.sint;
2293 if (pos == -1)
2294 blk = htx_get_head_blk(htx);
2295 else if (pos == -2)
2296 blk = htx_get_tail_blk(htx);
2297 else if (pos == -3)
2298 blk = htx_get_first_blk(htx);
2299 else
2300 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2301
2302 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2303 smp->data.u.str.size = 0;
2304 smp->data.u.str.area = "";
2305 smp->data.u.str.data = 0;
2306 }
2307 else {
2308 sl = htx_get_blk_ptr(htx, blk);
2309
2310 temp = get_trash_chunk();
2311 chunk_istcat(temp, htx_sl_p1(sl));
2312 temp->area[temp->data++] = ' ';
2313 chunk_istcat(temp, htx_sl_p2(sl));
2314 temp->area[temp->data++] = ' ';
2315 chunk_istcat(temp, htx_sl_p3(sl));
2316
2317 smp->data.u.str = *temp;
2318 }
2319
2320 smp->data.type = SMP_T_STR;
2321 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2322 return 1;
2323}
2324
2325/* Returns the header name if the selected HTX block exists and is a header or a
2326 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2327 * supported or "head", "tail" or "first". The channel is chosen depending on
2328 * the sample direction. */
2329static int
2330smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2331{
2332 struct channel *chn;
2333 struct htx *htx;
2334 struct htx_blk *blk;
2335 int32_t pos;
2336
2337 if (!smp->strm || !arg_p)
2338 return 0;
2339
2340 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002341 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002342 if (!htx)
2343 return 0;
2344
2345 pos = arg_p[0].data.sint;
2346 if (pos == -1)
2347 blk = htx_get_head_blk(htx);
2348 else if (pos == -2)
2349 blk = htx_get_tail_blk(htx);
2350 else if (pos == -3)
2351 blk = htx_get_first_blk(htx);
2352 else
2353 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2354
2355 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2356 smp->data.u.str.size = 0;
2357 smp->data.u.str.area = "";
2358 smp->data.u.str.data = 0;
2359 }
2360 else {
2361 struct ist name = htx_get_blk_name(htx, blk);
2362
2363 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2364 }
2365 smp->data.type = SMP_T_STR;
2366 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2367 return 1;
2368}
2369
2370/* Returns the header value if the selected HTX block exists and is a header or
2371 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2372 * supported or "head", "tail" or "first". The channel is chosen depending on
2373 * the sample direction. */
2374static int
2375smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2376{
2377 struct channel *chn;
2378 struct htx *htx;
2379 struct htx_blk *blk;
2380 int32_t pos;
2381
2382 if (!smp->strm || !arg_p)
2383 return 0;
2384
2385 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002386 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002387 if (!htx)
2388 return 0;
2389
2390 pos = arg_p[0].data.sint;
2391 if (pos == -1)
2392 blk = htx_get_head_blk(htx);
2393 else if (pos == -2)
2394 blk = htx_get_tail_blk(htx);
2395 else if (pos == -3)
2396 blk = htx_get_first_blk(htx);
2397 else
2398 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2399
2400 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2401 smp->data.u.str.size = 0;
2402 smp->data.u.str.area = "";
2403 smp->data.u.str.data = 0;
2404 }
2405 else {
2406 struct ist val = htx_get_blk_value(htx, blk);
2407
2408 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2409 }
2410 smp->data.type = SMP_T_STR;
2411 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2412 return 1;
2413}
2414
2415/* Returns the value if the selected HTX block exists and is a data
2416 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2417 * or "head", "tail" or "first". The channel is chosen depending on the sample
2418 * direction. */
2419static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002420smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002421{
2422 struct channel *chn;
2423 struct htx *htx;
2424 struct htx_blk *blk;
2425 int32_t pos;
2426
2427 if (!smp->strm || !arg_p)
2428 return 0;
2429
2430 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002431 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002432 if (!htx)
2433 return 0;
2434
2435 pos = arg_p[0].data.sint;
2436 if (pos == -1)
2437 blk = htx_get_head_blk(htx);
2438 else if (pos == -2)
2439 blk = htx_get_tail_blk(htx);
2440 else if (pos == -3)
2441 blk = htx_get_first_blk(htx);
2442 else
2443 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2444
2445 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2446 smp->data.u.str.size = 0;
2447 smp->data.u.str.area = "";
2448 smp->data.u.str.data = 0;
2449 }
2450 else {
2451 struct ist val = htx_get_blk_value(htx, blk);
2452
2453 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2454 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002455 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002456 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2457 return 1;
2458}
2459
2460/* This function is used to validate the arguments passed to any "htx_blk" fetch
2461 * keywords. An argument is expected by these keywords. It must be a positive
2462 * integer or on of the following strings: "head", "tail" or "first". It returns
2463 * 0 on error, and a non-zero value if OK.
2464 */
2465int val_blk_arg(struct arg *arg, char **err_msg)
2466{
2467 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2468 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2469 return 0;
2470 }
2471 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2472 free(arg[0].data.str.area);
2473 arg[0].type = ARGT_SINT;
2474 arg[0].data.sint = -1;
2475 }
2476 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2477 free(arg[0].data.str.area);
2478 arg[0].type = ARGT_SINT;
2479 arg[0].data.sint = -2;
2480 }
2481 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2482 free(arg[0].data.str.area);
2483 arg[0].type = ARGT_SINT;
2484 arg[0].data.sint = -3;
2485 }
2486 else {
2487 int pos;
2488
2489 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002490 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002491 memprintf(err_msg, "invalid block position");
2492 return 0;
2493 }
2494 }
2495
2496 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2497 if (pos < 0) {
2498 memprintf(err_msg, "block position must not be negative");
2499 return 0;
2500 }
2501 free(arg[0].data.str.area);
2502 arg[0].type = ARGT_SINT;
2503 arg[0].data.sint = pos;
2504 }
2505
2506 return 1;
2507}
2508
2509
2510/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002511 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002512 */
2513static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002514 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002515
Christopher Faulet01f44452020-01-08 14:23:40 +01002516 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2517 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2518 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2519 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2520 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2521 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2522 { "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 +01002523
Christopher Faulet01f44452020-01-08 14:23:40 +01002524 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2525 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2526 { "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},
2527 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2528 { "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 +01002529 { "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 +01002530
2531 { /* END */ },
2532}};
2533
2534INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);