blob: c572697bed118d7a601b30b56eb20e7eb84a2e9b [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <unistd.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020022#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020024#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020025#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/log.h>
27#include <haproxy/regex.h>
28#include <haproxy/sample.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020029
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Fauletf7346382019-07-17 22:02:08 +020031struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020032struct http_reply http_err_replies[HTTP_ERR_SIZE];
33
Christopher Faulet58857752020-01-15 15:19:50 +010034struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010035struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020036struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010037
Christopher Faulet76edc0f2020-01-13 15:52:01 +010038/* The declaration of an errorfiles/errorfile directives. Used during config
39 * parsing only. */
40struct conf_errors {
41 char type; /* directive type (0: errorfiles, 1: errorfile) */
42 union {
43 struct {
44 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020045 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010046 } errorfile; /* describe an "errorfile" directive */
47 struct {
48 char *name; /* the http-errors section name */
49 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
50 } errorfiles; /* describe an "errorfiles" directive */
51 } info;
52
53 char *file; /* file where the directive appears */
54 int line; /* line where the directive appears */
55
56 struct list list; /* next conf_errors */
57};
58
Christopher Faulet297fbb42019-05-13 14:41:27 +020059/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020060 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020061 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020062 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020063struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020064{
Christopher Faulet297fbb42019-05-13 14:41:27 +020065 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 BUG_ON(htx->first == -1);
68 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020069 if (!blk)
70 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020071 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 +020072 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static 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 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100162 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
204 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int 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 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
391 if (!http_update_host(htx, sl, uri))
392 goto fail;
393
394 return 1;
395 fail:
396 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200397}
398
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200399/* Replace the request path in the HTX message <htx> by <path>. The host part is
400 * preserverd. if <with_qs> is set, the query string is evaluated as part of the
401 * path and replaced. Otherwise, it is preserved too. It returns 1 on success,
402 * otherwise 0.
Christopher Faulete010c802018-10-24 10:36:45 +0200403 */
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200404int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs)
Christopher Faulete010c802018-10-24 10:36:45 +0200405{
406 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200407 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100408 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200409 size_t plen = 0;
410
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100411 if (!sl)
412 return 0;
413
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100414 uri = htx_sl_req_uri(sl);
415 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100416 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100417 p = uri;
Christopher Fauletb8ce5052020-08-31 16:11:57 +0200418 if (with_qs)
419 plen = p.len;
420 else {
421 while (plen < p.len && *(p.ptr + plen) != '?')
422 plen++;
423 }
Christopher Faulete010c802018-10-24 10:36:45 +0200424
425 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100426 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
427 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200428
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100429 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
430 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
431
432 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200433 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
434 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100435 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200436
437 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100438 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200439}
440
441/* Replace the request query-string in the HTX message <htx> by <query>. The
442 * host part and the path are preserved. It returns 1 on success, otherwise
443 * 0.
444 */
445int http_replace_req_query(struct htx *htx, const struct ist query)
446{
447 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200448 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100449 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200450 int offset = 1;
451
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100452 if (!sl)
453 return 0;
454
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100455 uri = htx_sl_req_uri(sl);
456 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200457 while (q.len > 0 && *(q.ptr) != '?') {
458 q.ptr++;
459 q.len--;
460 }
461
462 /* skip the question mark or indicate that we must insert it
463 * (but only if the format string is not empty then).
464 */
465 if (q.len) {
466 q.ptr++;
467 q.len--;
468 }
469 else if (query.len > 1)
470 offset = 0;
471
472 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100473 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
474 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200475
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100476 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
477 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200478
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
480 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
481 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200482
483 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100484 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200485}
486
487/* Replace the response status in the HTX message <htx> by <status>. It returns
488 * 1 on success, otherwise 0.
489*/
490int http_replace_res_status(struct htx *htx, const struct ist status)
491{
492 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200493 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100494 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200495
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100496 if (!sl)
497 return 0;
498
Christopher Faulete010c802018-10-24 10:36:45 +0200499 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
501 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200502
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100503 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
504 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200505
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100506 /* create the new start line */
507 sl->info.res.status = strl2ui(status.ptr, status.len);
508 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200509}
510
511/* Replace the response reason in the HTX message <htx> by <reason>. It returns
512 * 1 on success, otherwise 0.
513*/
514int http_replace_res_reason(struct htx *htx, const struct ist reason)
515{
516 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200517 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200519
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100520 if (!sl)
521 return 0;
522
Christopher Faulete010c802018-10-24 10:36:45 +0200523 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100524 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
525 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200526
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100527 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
528 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200529
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100530 /* create the new start line */
531 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200532}
533
Christopher Faulet47596d32018-10-22 09:17:28 +0200534/* Replaces a part of a header value referenced in the context <ctx> by
535 * <data>. It returns 1 on success, otherwise it returns 0. The context is
536 * updated if necessary.
537 */
538int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
539{
540 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200541 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200542 char *start;
543 struct ist v;
544 uint32_t len, off;
545
546 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200547 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200548
549 v = htx_get_blk_value(htx, blk);
550 start = ctx->value.ptr - ctx->lws_before;
551 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
552 off = start - v.ptr;
553
554 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
555 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200556 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200557
558 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200559
560 sl = http_get_stline(htx);
561 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
562 struct ist n = htx_get_blk_name(htx, blk);
563
564 if (isteq(n, ist("host"))) {
565 if (!http_update_authority(htx, sl, v))
566 goto fail;
567 ctx->blk = NULL;
568 http_find_header(htx, ist("host"), ctx, 1);
569 blk = ctx->blk;
570 v = htx_get_blk_value(htx, blk);
571 }
572 }
573
Christopher Faulet47596d32018-10-22 09:17:28 +0200574 ctx->blk = blk;
575 ctx->value.ptr = v.ptr + off;
576 ctx->value.len = data.len;
577 ctx->lws_before = ctx->lws_after = 0;
578
579 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200580 fail:
581 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200582}
583
584/* Fully replaces a header referenced in the context <ctx> by the name <name>
585 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
586 * context is updated if necessary.
587 */
588int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
589 const struct ist name, const struct ist value)
590{
591 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200592 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200593
594 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200595 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200596
597 blk = htx_replace_header(htx, blk, name, value);
598 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200599 goto fail;
600
601 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100602 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200603 if (!http_update_authority(htx, sl, value))
604 goto fail;
605 ctx->blk = NULL;
606 http_find_header(htx, ist("host"), ctx, 1);
607 blk = ctx->blk;
608 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200609
610 ctx->blk = blk;
611 ctx->value = ist(NULL);
612 ctx->lws_before = ctx->lws_after = 0;
613
614 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200615 fail:
616 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200617}
618
619/* Remove one value of a header. This only works on a <ctx> returned by
620 * http_find_header function. The value is removed, as well as surrounding commas
621 * if any. If the removed value was alone, the whole header is removed. The
622 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
623 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
624 * form that can be handled by http_find_header() to find next occurrence.
625 */
626int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
627{
628 struct htx_blk *blk = ctx->blk;
629 char *start;
630 struct ist v;
631 uint32_t len;
632
633 if (!blk)
634 return 0;
635
636 start = ctx->value.ptr - ctx->lws_before;
637 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
638
639 v = htx_get_blk_value(htx, blk);
640 if (len == v.len) {
641 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200642 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200643 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100644 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200645 ctx->lws_before = ctx->lws_after = 0;
646 }
647 else {
648 ctx->blk = htx_get_blk(htx, htx->tail);
649 ctx->value = htx_get_blk_value(htx, ctx->blk);
650 ctx->lws_before = ctx->lws_after = 0;
651 }
652 return 1;
653 }
654
655 /* This was not the only value of this header. We have to remove the
656 * part pointed by ctx->value. If it is the last entry of the list, we
657 * remove the last separator.
658 */
659 if (start == v.ptr) {
660 /* It's the first header part but not the only one. So remove
661 * the comma after it. */
662 len++;
663 }
664 else {
665 /* There is at least one header part before the removed one. So
666 * remove the comma between them. */
667 start--;
668 len++;
669 }
670 /* Update the block content and its len */
671 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200672 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200673
674 /* Finally update the ctx */
675 ctx->value.ptr = start;
676 ctx->value.len = 0;
677 ctx->lws_before = ctx->lws_after = 0;
678
679 return 1;
680}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200681
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200682/* Updates the authority part of the uri with the value <host>. It happens when
683 * the header host is modified. It returns 0 on failure and 1 on success. It is
684 * the caller responsibility to provide the start-line and to be sure the uri
685 * contains an authority. Thus, if no authority is found in the uri, an error is
686 * returned.
687 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200688int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200689{
690 struct buffer *temp = get_trash_chunk();
691 struct ist meth, vsn, uri, authority;
692
693 uri = htx_sl_req_uri(sl);
694 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100695 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200696 return 0;
697
Christopher Faulet34b18e42020-02-18 11:02:21 +0100698 /* Don't update the uri if there is no change */
699 if (isteq(host, authority))
700 return 1;
701
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200702 /* Start by copying old method and version */
703 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
704 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
705
706 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
707 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
708
709 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
710 chunk_memcat(temp, host.ptr, host.len);
711 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
712 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
713
714 return http_replace_stline(htx, meth, uri, vsn);
715
716}
717
718/* Update the header host by extracting the authority of the uri <uri>. flags of
719 * the start-line are also updated accordingly. For orgin-form and asterisk-form
720 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
721 * removed from the flags of the start-line. Otherwise, this flag is set and the
722 * authority is used to set the value of the header host. This function returns
723 * 0 on failure and 1 on success.
724*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200725int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200726{
727 struct ist authority;
728 struct http_hdr_ctx ctx;
729
730 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
731 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
732 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
733 }
734 else {
735 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
736 if (sl->info.req.meth != HTTP_METH_CONNECT) {
737 // absolute-form (RFC7320 #5.3.2)
738 sl->flags |= HTX_SL_F_HAS_SCHM;
739 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
740 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
741
742 authority = http_get_authority(uri, 1);
743 if (!authority.len)
744 goto fail;
745 }
746 else {
747 // authority-form (RFC7320 #5.3.3)
748 authority = uri;
749 }
750
751 /* Replace header host value */
752 ctx.blk = NULL;
753 while (http_find_header(htx, ist("host"), &ctx, 1)) {
754 if (!http_replace_header_value(htx, &ctx, authority))
755 goto fail;
756 }
757
758 }
759 return 1;
760 fail:
761 return 0;
762}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200763
764/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
765 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
766 * performed over the whole headers. Otherwise it must contain a valid header
767 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
768 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
769 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
770 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
771 * -1. The value fetch stops at commas, so this function is suited for use with
772 * list headers.
773 * The return value is 0 if nothing was found, or non-zero otherwise.
774 */
775unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
776 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
777{
778 struct http_hdr_ctx local_ctx;
779 struct ist val_hist[MAX_HDR_HISTORY];
780 unsigned int hist_idx;
781 int found;
782
783 if (!ctx) {
784 local_ctx.blk = NULL;
785 ctx = &local_ctx;
786 }
787
788 if (occ >= 0) {
789 /* search from the beginning */
790 while (http_find_header(htx, hdr, ctx, 0)) {
791 occ--;
792 if (occ <= 0) {
793 *vptr = ctx->value.ptr;
794 *vlen = ctx->value.len;
795 return 1;
796 }
797 }
798 return 0;
799 }
800
801 /* negative occurrence, we scan all the list then walk back */
802 if (-occ > MAX_HDR_HISTORY)
803 return 0;
804
805 found = hist_idx = 0;
806 while (http_find_header(htx, hdr, ctx, 0)) {
807 val_hist[hist_idx] = ctx->value;
808 if (++hist_idx >= MAX_HDR_HISTORY)
809 hist_idx = 0;
810 found++;
811 }
812 if (-occ > found)
813 return 0;
814
815 /* OK now we have the last occurrence in [hist_idx-1], and we need to
816 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
817 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
818 * to remain in the 0..9 range.
819 */
820 hist_idx += occ + MAX_HDR_HISTORY;
821 if (hist_idx >= MAX_HDR_HISTORY)
822 hist_idx -= MAX_HDR_HISTORY;
823 *vptr = val_hist[hist_idx].ptr;
824 *vlen = val_hist[hist_idx].len;
825 return 1;
826}
827
828/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
829 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
830 * performed over the whole headers. Otherwise it must contain a valid header
831 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
832 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
833 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
834 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
835 * -1. This function differs from http_get_hdr() in that it only returns full
836 * line header values and does not stop at commas.
837 * The return value is 0 if nothing was found, or non-zero otherwise.
838 */
839unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
840 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
841{
842 struct http_hdr_ctx local_ctx;
843 struct ist val_hist[MAX_HDR_HISTORY];
844 unsigned int hist_idx;
845 int found;
846
847 if (!ctx) {
848 local_ctx.blk = NULL;
849 ctx = &local_ctx;
850 }
851
852 if (occ >= 0) {
853 /* search from the beginning */
854 while (http_find_header(htx, hdr, ctx, 1)) {
855 occ--;
856 if (occ <= 0) {
857 *vptr = ctx->value.ptr;
858 *vlen = ctx->value.len;
859 return 1;
860 }
861 }
862 return 0;
863 }
864
865 /* negative occurrence, we scan all the list then walk back */
866 if (-occ > MAX_HDR_HISTORY)
867 return 0;
868
869 found = hist_idx = 0;
870 while (http_find_header(htx, hdr, ctx, 1)) {
871 val_hist[hist_idx] = ctx->value;
872 if (++hist_idx >= MAX_HDR_HISTORY)
873 hist_idx = 0;
874 found++;
875 }
876 if (-occ > found)
877 return 0;
878
879 /* OK now we have the last occurrence in [hist_idx-1], and we need to
880 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
881 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
882 * to remain in the 0..9 range.
883 */
884 hist_idx += occ + MAX_HDR_HISTORY;
885 if (hist_idx >= MAX_HDR_HISTORY)
886 hist_idx -= MAX_HDR_HISTORY;
887 *vptr = val_hist[hist_idx].ptr;
888 *vlen = val_hist[hist_idx].len;
889 return 1;
890}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100891
Christopher Faulet90cc4812019-07-22 16:49:30 +0200892int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100893{
894 struct htx *htx;
895 struct htx_sl *sl;
896 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200897 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100898 union h1_sl h1sl;
899 unsigned int flags = HTX_SL_F_IS_RESP;
900 int ret = 0;
901
Christopher Faulet90cc4812019-07-22 16:49:30 +0200902 b_reset(buf);
903 if (!raw.len) {
904 buf->size = 0;
905 buf->area = malloc(raw.len);
906 return 1;
907 }
908
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100909 buf->size = global.tune.bufsize;
910 buf->area = (char *)malloc(buf->size);
911 if (!buf->area)
912 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100913
914 h1m_init_res(&h1m);
915 h1m.flags |= H1_MF_NO_PHDR;
916 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
917 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
918 if (ret <= 0)
919 goto error;
920
921 if (unlikely(h1sl.st.v.len != 8))
922 goto error;
923 if ((*(h1sl.st.v.ptr + 5) > '1') ||
924 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
925 h1m.flags |= H1_MF_VER_11;
926
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200927 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
928 goto error;
929
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100930 if (h1m.flags & H1_MF_VER_11)
931 flags |= HTX_SL_F_VER_11;
932 if (h1m.flags & H1_MF_XFER_ENC)
933 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200934 if (h1m.flags & H1_MF_CLEN) {
935 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
936 if (h1m.body_len == 0)
937 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100938 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200939 if (h1m.flags & H1_MF_CHNK)
940 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100941
942 htx = htx_from_buf(buf);
943 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
944 if (!sl || !htx_add_all_headers(htx, hdrs))
945 goto error;
946 sl->info.res.status = h1sl.st.status;
947
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200948 while (raw.len > ret) {
949 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
950 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100951 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200952 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100953 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200954
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100955 if (!htx_add_endof(htx, HTX_BLK_EOM))
956 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200957
Christopher Faulet90cc4812019-07-22 16:49:30 +0200958 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100959
960error:
961 if (buf->size)
962 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200963 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100964}
965
Christopher Faulet18630642020-05-12 18:57:28 +0200966void release_http_reply(struct http_reply *http_reply)
967{
968 struct logformat_node *lf, *lfb;
969 struct http_reply_hdr *hdr, *hdrb;
970
971 if (!http_reply)
972 return;
973
974 free(http_reply->ctype);
975 http_reply->ctype = NULL;
976 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
977 LIST_DEL(&hdr->list);
978 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
979 LIST_DEL(&lf->list);
980 release_sample_expr(lf->expr);
981 free(lf->arg);
982 free(lf);
983 }
984 istfree(&hdr->name);
985 free(hdr);
986 }
987
988 if (http_reply->type == HTTP_REPLY_ERRFILES) {
989 free(http_reply->body.http_errors);
990 http_reply->body.http_errors = NULL;
991 }
992 else if (http_reply->type == HTTP_REPLY_RAW)
993 chunk_destroy(&http_reply->body.obj);
994 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
995 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
996 LIST_DEL(&lf->list);
997 release_sample_expr(lf->expr);
998 free(lf->arg);
999 free(lf);
1000 }
1001 }
Christopher Faulet63d48242020-05-21 09:59:22 +02001002 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +02001003}
1004
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001005static int http_htx_init(void)
1006{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001007 struct buffer chk;
1008 struct ist raw;
1009 int rc;
1010 int err_code = 0;
1011
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001012 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1013 if (!http_err_msgs[rc]) {
1014 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1015 err_code |= ERR_ALERT | ERR_FATAL;
1016 continue;
1017 }
1018
1019 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1020 if (!http_str_to_htx(&chk, raw)) {
1021 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1022 http_err_codes[rc]);
1023 err_code |= ERR_ALERT | ERR_FATAL;
1024 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001025 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001026 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1027 http_err_replies[rc].status = http_err_codes[rc];
1028 http_err_replies[rc].ctype = NULL;
1029 LIST_INIT(&http_err_replies[rc].hdrs);
1030 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001031 }
1032end:
1033 return err_code;
1034}
1035
Christopher Faulet58857752020-01-15 15:19:50 +01001036static void http_htx_deinit(void)
1037{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001038 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001039 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001040 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001041 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001042 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001043
1044 node = ebpt_first(&http_error_messages);
1045 while (node) {
1046 next = ebpt_next(node);
1047 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001048 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1049 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001050 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001051 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001052 node = next;
1053 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001054
1055 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1056 free(http_errs->conf.file);
1057 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001058 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1059 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001060 LIST_DEL(&http_errs->list);
1061 free(http_errs);
1062 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001063
1064 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
1065 LIST_DEL(&http_rep->list);
1066 release_http_reply(http_rep);
1067 }
Christopher Faulet58857752020-01-15 15:19:50 +01001068}
1069
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001070REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001071REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001072
Christopher Faulet58857752020-01-15 15:19:50 +01001073/* Reads content of the error file <file> and convert it into an HTX message. On
1074 * success, the HTX message is returned. On error, NULL is returned and an error
1075 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001076 */
Christopher Faulet58857752020-01-15 15:19:50 +01001077struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001078{
Christopher Faulet58857752020-01-15 15:19:50 +01001079 struct buffer *buf = NULL;
1080 struct buffer chk;
1081 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001082 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001083 struct stat stat;
1084 char *err = NULL;
1085 int errnum, errlen;
1086 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001087
1088 /* already loaded */
1089 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1090 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001091 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1092 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001093 goto out;
1094 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001095
Christopher Faulet58857752020-01-15 15:19:50 +01001096 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001097 fd = open(file, O_RDONLY);
1098 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1099 memprintf(errmsg, "error opening file '%s'.", file);
1100 goto out;
1101 }
1102
1103 if (stat.st_size <= global.tune.bufsize)
1104 errlen = stat.st_size;
1105 else {
1106 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1107 file, global.tune.bufsize);
1108 errlen = global.tune.bufsize;
1109 }
1110
1111 err = malloc(errlen);
1112 if (!err) {
1113 memprintf(errmsg, "out of memory.");
1114 goto out;
1115 }
1116
1117 errnum = read(fd, err, errlen);
1118 if (errnum != errlen) {
1119 memprintf(errmsg, "error reading file '%s'.", file);
1120 goto out;
1121 }
1122
Christopher Faulet58857752020-01-15 15:19:50 +01001123 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001124 http_errmsg = calloc(1, sizeof(*http_errmsg));
1125 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001126 memprintf(errmsg, "out of memory.");
1127 goto out;
1128 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001129 http_errmsg->node.key = strdup(file);
1130 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001131 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001132 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001133 goto out;
1134 }
1135
1136 /* Convert the error file into an HTX message */
1137 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001138 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001139 free(http_errmsg->node.key);
1140 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001141 goto out;
1142 }
1143
Christopher Faulet58857752020-01-15 15:19:50 +01001144 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001145 http_errmsg->msg = chk;
1146 ebis_insert(&http_error_messages, &http_errmsg->node);
1147 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001148
Christopher Faulet5031ef52020-01-15 11:22:07 +01001149 out:
1150 if (fd >= 0)
1151 close(fd);
1152 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001153 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001154}
1155
Ilya Shipitsind4259502020-04-08 01:07:56 +05001156/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001157 * message is returned. On error, NULL is returned and an error message is
1158 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001159 */
Christopher Faulet58857752020-01-15 15:19:50 +01001160struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001161{
Christopher Faulet58857752020-01-15 15:19:50 +01001162 struct buffer *buf = NULL;
1163 struct buffer chk;
1164 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001165 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001166
1167 /* already loaded */
1168 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1169 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001170 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1171 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001172 goto out;
1173 }
1174 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001175 http_errmsg = calloc(1, sizeof(*http_errmsg));
1176 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001177 memprintf(errmsg, "out of memory.");
1178 goto out;
1179 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001180 http_errmsg->node.key = strdup(key);
1181 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001182 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001183 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001184 goto out;
1185 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001186
1187 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001188 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001189 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001190 free(http_errmsg->node.key);
1191 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001192 goto out;
1193 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001194
Christopher Faulet58857752020-01-15 15:19:50 +01001195 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001196 http_errmsg->msg = chk;
1197 ebis_insert(&http_error_messages, &http_errmsg->node);
1198 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001199 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001200 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001201}
1202
Christopher Faulet5031ef52020-01-15 11:22:07 +01001203/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001204 * <status>. It returns NULL if there is any error, otherwise it return the
1205 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001206 */
Christopher Faulet58857752020-01-15 15:19:50 +01001207struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001208{
Christopher Faulet58857752020-01-15 15:19:50 +01001209 struct buffer *buf = NULL;
1210 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001211
1212 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1213 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001214 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001215 break;
1216 }
1217 }
1218
1219 if (rc >= HTTP_ERR_SIZE)
1220 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001221 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001222}
1223
1224/* This function creates HTX error message corresponding to a redirect message
1225 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001226 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1227 * returns NULL if there is any error, otherwise it return the corresponding HTX
1228 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001229 */
Christopher Faulet58857752020-01-15 15:19:50 +01001230struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001231{
Christopher Faulet0bac4cd2020-05-27 10:11:59 +02001232 static const char *HTTP_302 =
1233 "HTTP/1.1 302 Found\r\n"
1234 "Cache-Control: no-cache\r\n"
1235 "Content-length: 0\r\n"
1236 "Location: "; /* not terminated since it will be concatenated with the URL */
1237 static const char *HTTP_303 =
1238 "HTTP/1.1 303 See Other\r\n"
1239 "Cache-Control: no-cache\r\n"
1240 "Content-length: 0\r\n"
1241 "Location: "; /* not terminated since it will be concatenated with the URL */
1242
Christopher Faulet58857752020-01-15 15:19:50 +01001243 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001244 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001245 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001246 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001247
1248 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1249 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001250 /* Create the error key */
1251 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1252 memprintf(errmsg, "out of memory.");
1253 goto out;
1254 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001255 /* Create the error message */
1256 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1257 errlen = strlen(msg) + strlen(url) + 5;
1258 err = malloc(errlen);
1259 if (!err) {
1260 memprintf(errmsg, "out of memory.");
1261 goto out;
1262 }
1263 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1264
1265 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001266 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001267 break;
1268 }
1269 }
1270
1271 if (rc >= HTTP_ERR_SIZE)
1272 memprintf(errmsg, "status code '%d' not handled.", status);
1273out:
Christopher Faulet58857752020-01-15 15:19:50 +01001274 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001275 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001276 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001277}
1278
Christopher Faulet7eea2412020-05-13 15:02:59 +02001279/* Check an "http reply" and, for replies referencing an http-errors section,
1280 * try to find the right section and the right error message in this section. If
1281 * found, the reply is updated. If the http-errors section exists but the error
1282 * message is not found, no error message is set to fallback on the default
1283 * ones. Otherwise (unknown section) an error is returned.
1284 *
1285 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1286 * filled.
1287 */
1288int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1289{
1290 struct http_errors *http_errs;
1291 int ret = 1;
1292
1293 if (reply->type != HTTP_REPLY_ERRFILES)
1294 goto end;
1295
1296 list_for_each_entry(http_errs, &http_errors_list, list) {
1297 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001298 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001299 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001300 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1301 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001302 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1303 "not declared in http-errors section '%s'.\n",
1304 px->id, reply->status, http_errs->id);
1305 break;
1306 }
1307 }
1308
1309 if (&http_errs->list == &http_errors_list) {
1310 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1311 reply->body.http_errors);
1312 ret = 0;
1313 }
1314
1315 end:
1316 return ret;
1317}
1318
Christopher Faulet47e791e2020-05-13 14:36:55 +02001319/* Parse an "http reply". It returns the reply on success or NULL on error. This
1320 * function creates one of the following http replies :
1321 *
1322 * - HTTP_REPLY_EMPTY : dummy response, no payload
1323 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1324 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1325 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1326 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1327 *
1328 * The content-type must be defined for non-empty payload. It is ignored for
1329 * error messages (implicit or explicit). When an http-errors section is
1330 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1331 * during the configuration validity check or dynamically. It is the caller
1332 * responsibility to choose. If no status code is configured, <default_status>
1333 * is set.
1334 */
1335struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1336 int default_status, char **errmsg)
1337{
1338 struct logformat_node *lf, *lfb;
1339 struct http_reply *reply = NULL;
1340 struct http_reply_hdr *hdr, *hdrb;
1341 struct stat stat;
1342 const char *act_arg = NULL;
1343 char *obj = NULL;
1344 int cur_arg, cap, objlen = 0, fd = -1;
1345
1346
1347 reply = calloc(1, sizeof(*reply));
1348 if (!reply) {
1349 memprintf(errmsg, "out of memory");
1350 goto error;
1351 }
1352 LIST_INIT(&reply->hdrs);
1353 reply->type = HTTP_REPLY_EMPTY;
1354 reply->status = default_status;
1355
Christopher Faulet3b967c12020-05-15 15:47:44 +02001356 if (px->conf.args.ctx == ARGC_HERR)
1357 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1358 else
1359 cap = ((px->conf.args.ctx == ARGC_HRQ)
1360 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1361 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001362
1363 cur_arg = *orig_arg;
1364 while (*args[cur_arg]) {
1365 if (strcmp(args[cur_arg], "status") == 0) {
1366 cur_arg++;
1367 if (!*args[cur_arg]) {
1368 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1369 goto error;
1370 }
1371 reply->status = atol(args[cur_arg]);
1372 if (reply->status < 200 || reply->status > 599) {
1373 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1374 goto error;
1375 }
1376 cur_arg++;
1377 }
1378 else if (strcmp(args[cur_arg], "content-type") == 0) {
1379 cur_arg++;
1380 if (!*args[cur_arg]) {
1381 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1382 goto error;
1383 }
1384 free(reply->ctype);
1385 reply->ctype = strdup(args[cur_arg]);
1386 cur_arg++;
1387 }
1388 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1389 if (reply->type != HTTP_REPLY_EMPTY) {
1390 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1391 goto error;
1392 }
1393 act_arg = args[cur_arg];
1394 cur_arg++;
1395 if (!*args[cur_arg]) {
1396 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1397 goto error;
1398 }
1399 reply->body.http_errors = strdup(args[cur_arg]);
1400 if (!reply->body.http_errors) {
1401 memprintf(errmsg, "out of memory");
1402 goto error;
1403 }
1404 reply->type = HTTP_REPLY_ERRFILES;
1405 cur_arg++;
1406 }
1407 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1408 if (reply->type != HTTP_REPLY_EMPTY) {
1409 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1410 goto error;
1411 }
1412 act_arg = args[cur_arg];
1413 reply->type = HTTP_REPLY_ERRMSG;
1414 cur_arg++;
1415 }
1416 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1417 if (reply->type != HTTP_REPLY_EMPTY) {
1418 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1419 goto error;
1420 }
1421 act_arg = args[cur_arg];
1422 cur_arg++;
1423 if (!*args[cur_arg]) {
1424 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1425 goto error;
1426 }
1427 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1428 if (!reply->body.errmsg) {
1429 goto error;
1430 }
1431 reply->type = HTTP_REPLY_ERRMSG;
1432 cur_arg++;
1433 }
1434 else if (strcmp(args[cur_arg], "file") == 0) {
1435 if (reply->type != HTTP_REPLY_EMPTY) {
1436 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1437 goto error;
1438 }
1439 act_arg = args[cur_arg];
1440 cur_arg++;
1441 if (!*args[cur_arg]) {
1442 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1443 goto error;
1444 }
1445 fd = open(args[cur_arg], O_RDONLY);
1446 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1447 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1448 goto error;
1449 }
1450 if (stat.st_size > global.tune.bufsize) {
1451 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1452 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1453 goto error;
1454 }
1455 objlen = stat.st_size;
1456 obj = malloc(objlen);
1457 if (!obj || read(fd, obj, objlen) != objlen) {
1458 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1459 goto error;
1460 }
1461 close(fd);
1462 fd = -1;
1463 reply->type = HTTP_REPLY_RAW;
1464 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1465 obj = NULL;
1466 cur_arg++;
1467 }
1468 else if (strcmp(args[cur_arg], "string") == 0) {
1469 if (reply->type != HTTP_REPLY_EMPTY) {
1470 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1471 goto error;
1472 }
1473 act_arg = args[cur_arg];
1474 cur_arg++;
1475 if (!*args[cur_arg]) {
1476 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1477 goto error;
1478 }
1479 obj = strdup(args[cur_arg]);
1480 objlen = strlen(args[cur_arg]);
1481 if (!obj) {
1482 memprintf(errmsg, "out of memory");
1483 goto error;
1484 }
1485 reply->type = HTTP_REPLY_RAW;
1486 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1487 obj = NULL;
1488 cur_arg++;
1489 }
1490 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1491 if (reply->type != HTTP_REPLY_EMPTY) {
1492 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1493 goto error;
1494 }
1495 act_arg = args[cur_arg];
1496 cur_arg++;
1497 if (!*args[cur_arg]) {
1498 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1499 goto error;
1500 }
1501 fd = open(args[cur_arg], O_RDONLY);
1502 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1503 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1504 goto error;
1505 }
1506 if (stat.st_size > global.tune.bufsize) {
1507 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1508 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1509 goto error;
1510 }
1511 objlen = stat.st_size;
1512 obj = malloc(objlen + 1);
1513 if (!obj || read(fd, obj, objlen) != objlen) {
1514 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1515 goto error;
1516 }
1517 close(fd);
1518 fd = -1;
1519 obj[objlen] = '\0';
1520 reply->type = HTTP_REPLY_LOGFMT;
1521 cur_arg++;
1522 }
1523 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1524 if (reply->type != HTTP_REPLY_EMPTY) {
1525 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1526 goto error;
1527 }
1528 act_arg = args[cur_arg];
1529 cur_arg++;
1530 if (!*args[cur_arg]) {
1531 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1532 goto error;
1533 }
1534 obj = strdup(args[cur_arg]);
1535 objlen = strlen(args[cur_arg]);
1536 reply->type = HTTP_REPLY_LOGFMT;
1537 cur_arg++;
1538 }
1539 else if (strcmp(args[cur_arg], "hdr") == 0) {
1540 cur_arg++;
1541 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1542 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1543 goto error;
1544 }
1545 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1546 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1547 strcasecmp(args[cur_arg], "content-type") == 0) {
1548 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1549 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1550 cur_arg += 2;
1551 continue;
1552 }
1553 hdr = calloc(1, sizeof(*hdr));
1554 if (!hdr) {
1555 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1556 goto error;
1557 }
Christopher Fauletd6e31232020-05-21 10:10:41 +02001558 LIST_ADDQ(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001559 LIST_INIT(&hdr->value);
1560 hdr->name = ist(strdup(args[cur_arg]));
1561 if (!isttest(hdr->name)) {
1562 memprintf(errmsg, "out of memory");
1563 goto error;
1564 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001565 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1566 goto error;
1567
1568 free(px->conf.lfs_file);
1569 px->conf.lfs_file = strdup(px->conf.args.file);
1570 px->conf.lfs_line = px->conf.args.line;
1571 cur_arg += 2;
1572 }
1573 else
1574 break;
1575 }
1576
1577 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1578 if (reply->ctype) {
1579 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1580 " neither errorfile nor payload defined.\n",
1581 px->conf.args.file, px->conf.args.line, reply->ctype);
1582 free(reply->ctype);
1583 reply->ctype = NULL;
1584 }
1585 }
1586 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1587
1588 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1589 /* default errorfile or errorfiles: check the status */
1590 int rc;
1591
1592 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1593 if (http_err_codes[rc] == reply->status)
1594 break;
1595 }
1596
1597 if (rc >= HTTP_ERR_SIZE) {
1598 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1599 reply->status, act_arg);
1600 goto error;
1601 }
1602 }
1603
1604 if (reply->ctype) {
1605 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1606 "with an erorrfile.\n",
1607 px->conf.args.file, px->conf.args.line, reply->ctype);
1608 free(reply->ctype);
1609 reply->ctype = NULL;
1610 }
1611 if (!LIST_ISEMPTY(&reply->hdrs)) {
1612 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1613 "with an erorrfile.\n",
1614 px->conf.args.file, px->conf.args.line);
1615 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1616 LIST_DEL(&hdr->list);
1617 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1618 LIST_DEL(&lf->list);
1619 release_sample_expr(lf->expr);
1620 free(lf->arg);
1621 free(lf);
1622 }
1623 istfree(&hdr->name);
1624 free(hdr);
1625 }
1626 }
1627 }
1628 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1629 if (!reply->ctype && objlen) {
1630 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1631 goto error;
1632 }
1633 if (reply->ctype && !b_data(&reply->body.obj)) {
1634 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001635 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001636 px->conf.args.file, px->conf.args.line, reply->ctype);
1637 free(reply->ctype);
1638 reply->ctype = NULL;
1639 }
1640 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1641 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1642 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1643 px->conf.args.file, px->conf.args.line);
1644 }
1645 }
1646 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1647 LIST_INIT(&reply->body.fmt);
1648 if (!reply->ctype) {
1649 memprintf(errmsg, "a content type must be defined with a log-format payload");
1650 goto error;
1651 }
1652 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1653 goto error;
1654
1655 free(px->conf.lfs_file);
1656 px->conf.lfs_file = strdup(px->conf.args.file);
1657 px->conf.lfs_line = px->conf.args.line;
1658 }
1659
1660 free(obj);
1661 *orig_arg = cur_arg;
1662 return reply;
1663
1664 error:
1665 free(obj);
1666 if (fd >= 0)
1667 close(fd);
1668 release_http_reply(reply);
1669 return NULL;
1670}
1671
Christopher Faulet07f41f72020-01-16 16:16:06 +01001672/* Parses the "errorloc[302|303]" proxy keyword */
1673static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1674 struct proxy *defpx, const char *file, int line,
1675 char **errmsg)
1676{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001677 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001678 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001679 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001680 int errloc, status;
1681 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001682
1683 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1684 ret = 1;
1685 goto out;
1686 }
1687
1688 if (*(args[1]) == 0 || *(args[2]) == 0) {
1689 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1690 ret = -1;
1691 goto out;
1692 }
1693
1694 status = atol(args[1]);
1695 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1696 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1697 if (!msg) {
1698 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1699 ret = -1;
1700 goto out;
1701 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001702
1703 reply = calloc(1, sizeof(*reply));
1704 if (!reply) {
1705 memprintf(errmsg, "%s : out of memory.", args[0]);
1706 ret = -1;
1707 goto out;
1708 }
1709 reply->type = HTTP_REPLY_ERRMSG;
1710 reply->status = status;
1711 reply->ctype = NULL;
1712 LIST_INIT(&reply->hdrs);
1713 reply->body.errmsg = msg;
1714 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001715
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001716 conf_err = calloc(1, sizeof(*conf_err));
1717 if (!conf_err) {
1718 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001719 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001720 ret = -1;
1721 goto out;
1722 }
1723 conf_err->type = 1;
1724 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001725 conf_err->info.errorfile.reply = reply;
1726
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001727 conf_err->file = strdup(file);
1728 conf_err->line = line;
1729 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001730
1731 out:
1732 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001733
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001734}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001735
1736/* Parses the "errorfile" proxy keyword */
1737static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1738 struct proxy *defpx, const char *file, int line,
1739 char **errmsg)
1740{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001741 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001742 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001743 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001744 int status;
1745 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001746
1747 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1748 ret = 1;
1749 goto out;
1750 }
1751
1752 if (*(args[1]) == 0 || *(args[2]) == 0) {
1753 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1754 ret = -1;
1755 goto out;
1756 }
1757
1758 status = atol(args[1]);
1759 msg = http_parse_errorfile(status, args[2], errmsg);
1760 if (!msg) {
1761 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1762 ret = -1;
1763 goto out;
1764 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001765
Christopher Faulet5809e102020-05-14 17:31:52 +02001766 reply = calloc(1, sizeof(*reply));
1767 if (!reply) {
1768 memprintf(errmsg, "%s : out of memory.", args[0]);
1769 ret = -1;
1770 goto out;
1771 }
1772 reply->type = HTTP_REPLY_ERRMSG;
1773 reply->status = status;
1774 reply->ctype = NULL;
1775 LIST_INIT(&reply->hdrs);
1776 reply->body.errmsg = msg;
1777 LIST_ADDQ(&http_replies_list, &reply->list);
1778
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001779 conf_err = calloc(1, sizeof(*conf_err));
1780 if (!conf_err) {
1781 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001782 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001783 ret = -1;
1784 goto out;
1785 }
1786 conf_err->type = 1;
1787 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001788 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001789 conf_err->file = strdup(file);
1790 conf_err->line = line;
1791 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1792
1793 out:
1794 return ret;
1795
1796}
1797
1798/* Parses the "errorfiles" proxy keyword */
1799static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1800 struct proxy *defpx, const char *file, int line,
1801 char **err)
1802{
1803 struct conf_errors *conf_err = NULL;
1804 char *name = NULL;
1805 int rc, ret = 0;
1806
1807 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1808 ret = 1;
1809 goto out;
1810 }
1811
1812 if (!*(args[1])) {
1813 memprintf(err, "%s : expects <name> as argument.", args[0]);
1814 ret = -1;
1815 goto out;
1816 }
1817
1818 name = strdup(args[1]);
1819 conf_err = calloc(1, sizeof(*conf_err));
1820 if (!name || !conf_err) {
1821 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001822 goto error;
1823 }
1824 conf_err->type = 0;
1825
1826 conf_err->info.errorfiles.name = name;
1827 if (!*(args[2])) {
1828 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1829 conf_err->info.errorfiles.status[rc] = 1;
1830 }
1831 else {
1832 int cur_arg, status;
1833 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1834 status = atol(args[cur_arg]);
1835
1836 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1837 if (http_err_codes[rc] == status) {
1838 conf_err->info.errorfiles.status[rc] = 2;
1839 break;
1840 }
1841 }
1842 if (rc >= HTTP_ERR_SIZE) {
1843 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001844 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001845 }
1846 }
1847 }
1848 conf_err->file = strdup(file);
1849 conf_err->line = line;
1850 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1851 out:
1852 return ret;
1853
1854 error:
1855 free(name);
1856 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001857 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001858 goto out;
1859}
1860
Christopher Faulet3b967c12020-05-15 15:47:44 +02001861/* Parses the "http-error" proxy keyword */
1862static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
1863 struct proxy *defpx, const char *file, int line,
1864 char **errmsg)
1865{
1866 struct conf_errors *conf_err;
1867 struct http_reply *reply = NULL;
1868 int rc, cur_arg, ret = 0;
1869
1870 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1871 ret = 1;
1872 goto out;
1873 }
1874
1875 cur_arg = 1;
1876 curpx->conf.args.ctx = ARGC_HERR;
1877 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1878 if (!reply) {
1879 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1880 goto error;
1881 }
1882 else if (!reply->status) {
1883 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1884 goto error;
1885 }
1886
1887 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1888 if (http_err_codes[rc] == reply->status)
1889 break;
1890 }
1891
1892 if (rc >= HTTP_ERR_SIZE) {
1893 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1894 goto error;
1895 }
1896 if (*args[cur_arg]) {
1897 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1898 goto error;
1899 }
1900
1901 conf_err = calloc(1, sizeof(*conf_err));
1902 if (!conf_err) {
1903 memprintf(errmsg, "%s : out of memory.", args[0]);
1904 goto error;
1905 }
1906 if (reply->type == HTTP_REPLY_ERRFILES) {
1907 int rc = http_get_status_idx(reply->status);
1908
1909 conf_err->type = 2;
1910 conf_err->info.errorfiles.name = reply->body.http_errors;
1911 conf_err->info.errorfiles.status[rc] = 2;
1912 reply->body.http_errors = NULL;
1913 release_http_reply(reply);
1914 }
1915 else {
1916 conf_err->type = 1;
1917 conf_err->info.errorfile.status = reply->status;
1918 conf_err->info.errorfile.reply = reply;
1919 LIST_ADDQ(&http_replies_list, &reply->list);
1920 }
1921 conf_err->file = strdup(file);
1922 conf_err->line = line;
1923 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1924
1925 out:
1926 return ret;
1927
1928 error:
1929 release_http_reply(reply);
1930 ret = -1;
1931 goto out;
1932
1933}
1934
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001935/* Check "errorfiles" proxy keyword */
1936static int proxy_check_errors(struct proxy *px)
1937{
1938 struct conf_errors *conf_err, *conf_err_back;
1939 struct http_errors *http_errs;
1940 int rc, err = 0;
1941
1942 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1943 if (conf_err->type == 1) {
1944 /* errorfile */
1945 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02001946 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001947
1948 /* For proxy, to rely on default replies, just don't reference a reply */
1949 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
1950 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001951 }
1952 else {
1953 /* errorfiles */
1954 list_for_each_entry(http_errs, &http_errors_list, list) {
1955 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1956 break;
1957 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001958
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001959 /* unknown http-errors section */
1960 if (&http_errs->list == &http_errors_list) {
1961 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1962 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1963 err |= ERR_ALERT | ERR_FATAL;
1964 free(conf_err->info.errorfiles.name);
1965 goto next;
1966 }
1967
1968 free(conf_err->info.errorfiles.name);
1969 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1970 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02001971 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02001972 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001973 else if (conf_err->info.errorfiles.status[rc] == 2)
1974 ha_warning("config: proxy '%s' : status '%d' not declared in"
1975 " http-errors section '%s' (at %s:%d).\n",
1976 px->id, http_err_codes[rc], http_errs->id,
1977 conf_err->file, conf_err->line);
1978 }
1979 }
1980 }
1981 next:
1982 LIST_DEL(&conf_err->list);
1983 free(conf_err->file);
1984 free(conf_err);
1985 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001986
1987 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001988 return err;
1989}
1990
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001991static int post_check_errors()
1992{
1993 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001994 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001995 struct htx *htx;
1996 int err_code = 0;
1997
1998 node = ebpt_first(&http_error_messages);
1999 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002000 http_errmsg = container_of(node, typeof(*http_errmsg), node);
2001 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002002 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02002003 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002004 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
2005 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002006 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002007 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002008 (char *)node->key);
2009 err_code |= ERR_WARN;
2010 }
2011 next:
2012 node = ebpt_next(node);
2013 }
2014
2015 return err_code;
2016}
2017
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002018int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
2019{
2020 struct conf_errors *conf_err, *new_conf_err = NULL;
2021 int ret = 0;
2022
2023 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2024 new_conf_err = calloc(1, sizeof(*new_conf_err));
2025 if (!new_conf_err) {
2026 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2027 goto out;
2028 }
2029 new_conf_err->type = conf_err->type;
2030 if (conf_err->type == 1) {
2031 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002032 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002033 }
2034 else {
2035 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2036 if (!new_conf_err->info.errorfiles.name) {
2037 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2038 goto out;
2039 }
2040 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2041 sizeof(conf_err->info.errorfiles.status));
2042 }
2043 new_conf_err->file = strdup(conf_err->file);
2044 new_conf_err->line = conf_err->line;
2045 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
2046 new_conf_err = NULL;
2047 }
2048 ret = 1;
2049
2050 out:
2051 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002052 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002053}
2054
2055void proxy_release_conf_errors(struct proxy *px)
2056{
2057 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002058
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002059 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2060 if (conf_err->type == 0)
2061 free(conf_err->info.errorfiles.name);
2062 LIST_DEL(&conf_err->list);
2063 free(conf_err->file);
2064 free(conf_err);
2065 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002066}
2067
2068/*
2069 * Parse an <http-errors> section.
2070 * Returns the error code, 0 if OK, or any combination of :
2071 * - ERR_ABORT: must abort ASAP
2072 * - ERR_FATAL: we can continue parsing but not start the service
2073 * - ERR_WARN: a warning has been emitted
2074 * - ERR_ALERT: an alert has been emitted
2075 * Only the two first ones can stop processing, the two others are just
2076 * indicators.
2077 */
2078static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2079{
2080 static struct http_errors *curr_errs = NULL;
2081 int err_code = 0;
2082 const char *err;
2083 char *errmsg = NULL;
2084
2085 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2086 if (!*args[1]) {
2087 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2088 err_code |= ERR_ALERT | ERR_ABORT;
2089 goto out;
2090 }
2091
2092 err = invalid_char(args[1]);
2093 if (err) {
2094 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2095 file, linenum, *err, args[0], args[1]);
2096 err_code |= ERR_ALERT | ERR_FATAL;
2097 }
2098
2099 list_for_each_entry(curr_errs, &http_errors_list, list) {
2100 /* Error if two errors section owns the same name */
2101 if (strcmp(curr_errs->id, args[1]) == 0) {
2102 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2103 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2104 err_code |= ERR_ALERT | ERR_FATAL;
2105 }
2106 }
2107
2108 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2109 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2110 err_code |= ERR_ALERT | ERR_ABORT;
2111 goto out;
2112 }
2113
2114 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2115 curr_errs->id = strdup(args[1]);
2116 curr_errs->conf.file = strdup(file);
2117 curr_errs->conf.line = linenum;
2118 }
2119 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002120 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002121 struct buffer *msg;
2122 int status, rc;
2123
2124 if (*(args[1]) == 0 || *(args[2]) == 0) {
2125 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2126 file, linenum, args[0]);
2127 err_code |= ERR_ALERT | ERR_FATAL;
2128 goto out;
2129 }
2130
2131 status = atol(args[1]);
2132 msg = http_parse_errorfile(status, args[2], &errmsg);
2133 if (!msg) {
2134 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2135 err_code |= ERR_ALERT | ERR_FATAL;
2136 goto out;
2137 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002138
2139 reply = calloc(1, sizeof(*reply));
2140 if (!reply) {
2141 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2142 err_code |= ERR_ALERT | ERR_FATAL;
2143 goto out;
2144 }
2145 reply->type = HTTP_REPLY_ERRMSG;
2146 reply->status = status;
2147 reply->ctype = NULL;
2148 LIST_INIT(&reply->hdrs);
2149 reply->body.errmsg = msg;
2150
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002151 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002152 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002153 }
2154 else if (*args[0] != 0) {
2155 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2156 err_code |= ERR_ALERT | ERR_FATAL;
2157 goto out;
2158 }
2159
2160out:
2161 free(errmsg);
2162 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002163}
2164
2165static struct cfg_kw_list cfg_kws = {ILH, {
2166 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2167 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2168 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2169 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002170 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002171 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002172 { 0, NULL, NULL },
2173}};
2174
2175INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002176REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002177REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002178
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002179REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2180
Christopher Faulet29f72842019-12-11 15:52:32 +01002181/************************************************************************/
2182/* HTX sample fetches */
2183/************************************************************************/
2184
2185/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2186static int
2187smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2188{
2189 if (!smp->strm)
2190 return 0;
2191
2192 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2193 smp->data.type = SMP_T_BOOL;
2194 return 1;
2195}
2196
2197/* Returns the number of blocks in an HTX message. The channel is chosen
2198 * depending on the sample direction. */
2199static int
2200smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2201{
2202 struct channel *chn;
2203 struct htx *htx;
2204
2205 if (!smp->strm)
2206 return 0;
2207
2208 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002209 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002210 if (!htx)
2211 return 0;
2212
2213 smp->data.u.sint = htx_nbblks(htx);
2214 smp->data.type = SMP_T_SINT;
2215 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2216 return 1;
2217}
2218
2219/* Returns the size of an HTX message. The channel is chosen depending on the
2220 * sample direction. */
2221static int
2222smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2223{
2224 struct channel *chn;
2225 struct htx *htx;
2226
2227 if (!smp->strm)
2228 return 0;
2229
2230 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002231 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002232 if (!htx)
2233 return 0;
2234
2235 smp->data.u.sint = htx->size;
2236 smp->data.type = SMP_T_SINT;
2237 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2238 return 1;
2239}
2240
2241/* Returns the data size of an HTX message. The channel is chosen depending on the
2242 * sample direction. */
2243static int
2244smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2245{
2246 struct channel *chn;
2247 struct htx *htx;
2248
2249 if (!smp->strm)
2250 return 0;
2251
2252 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002253 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002254 if (!htx)
2255 return 0;
2256
2257 smp->data.u.sint = htx->data;
2258 smp->data.type = SMP_T_SINT;
2259 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2260 return 1;
2261}
2262
2263/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2264 * depending on the sample direction. */
2265static int
2266smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2267{
2268 struct channel *chn;
2269 struct htx *htx;
2270
2271 if (!smp->strm)
2272 return 0;
2273
2274 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002275 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002276 if (!htx)
2277 return 0;
2278
2279 smp->data.u.sint = htx_used_space(htx);
2280 smp->data.type = SMP_T_SINT;
2281 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2282 return 1;
2283}
2284
2285/* Returns the free space (size-used) of an HTX message. The channel is chosen
2286 * depending on the sample direction. */
2287static int
2288smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2289{
2290 struct channel *chn;
2291 struct htx *htx;
2292
2293 if (!smp->strm)
2294 return 0;
2295
2296 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002297 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002298 if (!htx)
2299 return 0;
2300
2301 smp->data.u.sint = htx_free_space(htx);
2302 smp->data.type = SMP_T_SINT;
2303 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2304 return 1;
2305}
2306
2307/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2308 * channel is chosen depending on the sample direction. */
2309static int
2310smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2311{
2312 struct channel *chn;
2313 struct htx *htx;
2314
2315 if (!smp->strm)
2316 return 0;
2317
2318 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002319 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002320 if (!htx)
2321 return 0;
2322
2323 smp->data.u.sint = htx_free_data_space(htx);
2324 smp->data.type = SMP_T_SINT;
2325 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2326 return 1;
2327}
2328
2329/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2330 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2331 * the sample direction. */
2332static int
2333smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2334{
2335 struct channel *chn;
2336 struct htx *htx;
2337
2338 if (!smp->strm)
2339 return 0;
2340
2341 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002342 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002343 if (!htx)
2344 return 0;
2345
2346 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2347 smp->data.type = SMP_T_BOOL;
2348 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2349 return 1;
2350}
2351
2352/* Returns the type of a specific HTX block, if found in the message. Otherwise
2353 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2354 * "head", "tail" or "first". The channel is chosen depending on the sample
2355 * direction. */
2356static int
2357smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2358{
2359 struct channel *chn;
2360 struct htx *htx;
2361 enum htx_blk_type type;
2362 int32_t pos;
2363
2364 if (!smp->strm || !arg_p)
2365 return 0;
2366
2367 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002368 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002369 if (!htx)
2370 return 0;
2371
2372 pos = arg_p[0].data.sint;
2373 if (pos == -1)
2374 type = htx_get_head_type(htx);
2375 else if (pos == -2)
2376 type = htx_get_tail_type(htx);
2377 else if (pos == -3)
2378 type = htx_get_first_type(htx);
2379 else
2380 type = ((pos >= htx->head && pos <= htx->tail)
2381 ? htx_get_blk_type(htx_get_blk(htx, pos))
2382 : HTX_BLK_UNUSED);
2383
2384 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2385 smp->data.type = SMP_T_STR;
2386 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2387 return 1;
2388}
2389
2390/* Returns the size of a specific HTX block, if found in the message. Otherwise
2391 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2392 * "first". The channel is chosen depending on the sample direction. */
2393static int
2394smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2395{
2396 struct channel *chn;
2397 struct htx *htx;
2398 struct htx_blk *blk;
2399 int32_t pos;
2400
2401 if (!smp->strm || !arg_p)
2402 return 0;
2403
2404 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002405 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002406 if (!htx)
2407 return 0;
2408
2409 pos = arg_p[0].data.sint;
2410 if (pos == -1)
2411 blk = htx_get_head_blk(htx);
2412 else if (pos == -2)
2413 blk = htx_get_tail_blk(htx);
2414 else if (pos == -3)
2415 blk = htx_get_first_blk(htx);
2416 else
2417 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2418
2419 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2420 smp->data.type = SMP_T_SINT;
2421 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2422 return 1;
2423}
2424
2425/* Returns the start-line if the selected HTX block exists and is a
2426 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2427 * supported or "head", "tail" or "first". The channel is chosen depending on
2428 * the sample direction. */
2429static int
2430smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2431{
2432 struct buffer *temp;
2433 struct channel *chn;
2434 struct htx *htx;
2435 struct htx_blk *blk;
2436 struct htx_sl *sl;
2437 int32_t pos;
2438
2439 if (!smp->strm || !arg_p)
2440 return 0;
2441
2442 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002443 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002444 if (!htx)
2445 return 0;
2446
2447 pos = arg_p[0].data.sint;
2448 if (pos == -1)
2449 blk = htx_get_head_blk(htx);
2450 else if (pos == -2)
2451 blk = htx_get_tail_blk(htx);
2452 else if (pos == -3)
2453 blk = htx_get_first_blk(htx);
2454 else
2455 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2456
2457 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2458 smp->data.u.str.size = 0;
2459 smp->data.u.str.area = "";
2460 smp->data.u.str.data = 0;
2461 }
2462 else {
2463 sl = htx_get_blk_ptr(htx, blk);
2464
2465 temp = get_trash_chunk();
2466 chunk_istcat(temp, htx_sl_p1(sl));
2467 temp->area[temp->data++] = ' ';
2468 chunk_istcat(temp, htx_sl_p2(sl));
2469 temp->area[temp->data++] = ' ';
2470 chunk_istcat(temp, htx_sl_p3(sl));
2471
2472 smp->data.u.str = *temp;
2473 }
2474
2475 smp->data.type = SMP_T_STR;
2476 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2477 return 1;
2478}
2479
2480/* Returns the header name if the selected HTX block exists and is a header or a
2481 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2482 * supported or "head", "tail" or "first". The channel is chosen depending on
2483 * the sample direction. */
2484static int
2485smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2486{
2487 struct channel *chn;
2488 struct htx *htx;
2489 struct htx_blk *blk;
2490 int32_t pos;
2491
2492 if (!smp->strm || !arg_p)
2493 return 0;
2494
2495 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002496 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002497 if (!htx)
2498 return 0;
2499
2500 pos = arg_p[0].data.sint;
2501 if (pos == -1)
2502 blk = htx_get_head_blk(htx);
2503 else if (pos == -2)
2504 blk = htx_get_tail_blk(htx);
2505 else if (pos == -3)
2506 blk = htx_get_first_blk(htx);
2507 else
2508 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2509
2510 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2511 smp->data.u.str.size = 0;
2512 smp->data.u.str.area = "";
2513 smp->data.u.str.data = 0;
2514 }
2515 else {
2516 struct ist name = htx_get_blk_name(htx, blk);
2517
2518 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2519 }
2520 smp->data.type = SMP_T_STR;
2521 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2522 return 1;
2523}
2524
2525/* Returns the header value if the selected HTX block exists and is a header or
2526 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2527 * supported or "head", "tail" or "first". The channel is chosen depending on
2528 * the sample direction. */
2529static int
2530smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2531{
2532 struct channel *chn;
2533 struct htx *htx;
2534 struct htx_blk *blk;
2535 int32_t pos;
2536
2537 if (!smp->strm || !arg_p)
2538 return 0;
2539
2540 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002541 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002542 if (!htx)
2543 return 0;
2544
2545 pos = arg_p[0].data.sint;
2546 if (pos == -1)
2547 blk = htx_get_head_blk(htx);
2548 else if (pos == -2)
2549 blk = htx_get_tail_blk(htx);
2550 else if (pos == -3)
2551 blk = htx_get_first_blk(htx);
2552 else
2553 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2554
2555 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2556 smp->data.u.str.size = 0;
2557 smp->data.u.str.area = "";
2558 smp->data.u.str.data = 0;
2559 }
2560 else {
2561 struct ist val = htx_get_blk_value(htx, blk);
2562
2563 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2564 }
2565 smp->data.type = SMP_T_STR;
2566 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2567 return 1;
2568}
2569
2570/* Returns the value if the selected HTX block exists and is a data
2571 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2572 * or "head", "tail" or "first". The channel is chosen depending on the sample
2573 * direction. */
2574static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002575smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002576{
2577 struct channel *chn;
2578 struct htx *htx;
2579 struct htx_blk *blk;
2580 int32_t pos;
2581
2582 if (!smp->strm || !arg_p)
2583 return 0;
2584
2585 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002586 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002587 if (!htx)
2588 return 0;
2589
2590 pos = arg_p[0].data.sint;
2591 if (pos == -1)
2592 blk = htx_get_head_blk(htx);
2593 else if (pos == -2)
2594 blk = htx_get_tail_blk(htx);
2595 else if (pos == -3)
2596 blk = htx_get_first_blk(htx);
2597 else
2598 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2599
2600 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2601 smp->data.u.str.size = 0;
2602 smp->data.u.str.area = "";
2603 smp->data.u.str.data = 0;
2604 }
2605 else {
2606 struct ist val = htx_get_blk_value(htx, blk);
2607
2608 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2609 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002610 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002611 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2612 return 1;
2613}
2614
2615/* This function is used to validate the arguments passed to any "htx_blk" fetch
2616 * keywords. An argument is expected by these keywords. It must be a positive
2617 * integer or on of the following strings: "head", "tail" or "first". It returns
2618 * 0 on error, and a non-zero value if OK.
2619 */
2620int val_blk_arg(struct arg *arg, char **err_msg)
2621{
2622 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2623 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2624 return 0;
2625 }
2626 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002627 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002628 arg[0].type = ARGT_SINT;
2629 arg[0].data.sint = -1;
2630 }
2631 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002632 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002633 arg[0].type = ARGT_SINT;
2634 arg[0].data.sint = -2;
2635 }
2636 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002637 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002638 arg[0].type = ARGT_SINT;
2639 arg[0].data.sint = -3;
2640 }
2641 else {
2642 int pos;
2643
2644 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002645 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002646 memprintf(err_msg, "invalid block position");
2647 return 0;
2648 }
2649 }
2650
2651 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2652 if (pos < 0) {
2653 memprintf(err_msg, "block position must not be negative");
2654 return 0;
2655 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002656 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002657 arg[0].type = ARGT_SINT;
2658 arg[0].data.sint = pos;
2659 }
2660
2661 return 1;
2662}
2663
2664
2665/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002666 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002667 */
2668static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002669 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002670
Christopher Faulet01f44452020-01-08 14:23:40 +01002671 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2672 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2673 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2674 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2675 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2676 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2677 { "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 +01002678
Christopher Faulet01f44452020-01-08 14:23:40 +01002679 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2680 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2681 { "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},
2682 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2683 { "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 +01002684 { "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 +01002685
2686 { /* END */ },
2687}};
2688
2689INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);