blob: 3137fbebbf37c3c855af5e401ad070e102da753c [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <unistd.h>
16
17#include <types/global.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020018
19#include <common/config.h>
Christopher Faulet29f17582019-05-23 11:03:26 +020020#include <common/debug.h>
Christopher Fauleta7b677c2018-11-29 16:48:49 +010021#include <common/cfgparse.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010022#include <common/h1.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020023#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010024#include <common/htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020025
Christopher Faulet29f72842019-12-11 15:52:32 +010026#include <proto/arg.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020027#include <proto/http_htx.h>
Christopher Faulet29f72842019-12-11 15:52:32 +010028#include <proto/http_fetch.h>
29#include <proto/sample.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Fauletf7346382019-07-17 22:02:08 +020031struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020032struct http_reply http_err_replies[HTTP_ERR_SIZE];
33
Christopher Faulet58857752020-01-15 15:19:50 +010034struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010035struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher 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
399/* Replace the request path in the HTX message <htx> by <path>. The host part
400 * and the query string are preserved. It returns 1 on success, otherwise 0.
401 */
402int http_replace_req_path(struct htx *htx, const struct ist path)
403{
404 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200405 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100406 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200407 size_t plen = 0;
408
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100409 if (!sl)
410 return 0;
411
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100412 uri = htx_sl_req_uri(sl);
413 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100414 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100415 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200416 while (plen < p.len && *(p.ptr + plen) != '?')
417 plen++;
418
419 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100420 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
421 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200422
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100423 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
424 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
425
426 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200427 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
428 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100429 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200430
431 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100432 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200433}
434
435/* Replace the request query-string in the HTX message <htx> by <query>. The
436 * host part and the path are preserved. It returns 1 on success, otherwise
437 * 0.
438 */
439int http_replace_req_query(struct htx *htx, const struct ist query)
440{
441 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200442 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100443 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200444 int offset = 1;
445
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100446 if (!sl)
447 return 0;
448
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100449 uri = htx_sl_req_uri(sl);
450 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200451 while (q.len > 0 && *(q.ptr) != '?') {
452 q.ptr++;
453 q.len--;
454 }
455
456 /* skip the question mark or indicate that we must insert it
457 * (but only if the format string is not empty then).
458 */
459 if (q.len) {
460 q.ptr++;
461 q.len--;
462 }
463 else if (query.len > 1)
464 offset = 0;
465
466 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100467 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
468 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200469
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100470 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
471 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200472
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100473 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
474 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
475 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200476
477 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100478 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200479}
480
481/* Replace the response status in the HTX message <htx> by <status>. It returns
482 * 1 on success, otherwise 0.
483*/
484int http_replace_res_status(struct htx *htx, const struct ist status)
485{
486 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200487 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100488 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200489
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100490 if (!sl)
491 return 0;
492
Christopher Faulete010c802018-10-24 10:36:45 +0200493 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100494 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
495 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200496
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100497 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
498 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200499
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 /* create the new start line */
501 sl->info.res.status = strl2ui(status.ptr, status.len);
502 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200503}
504
505/* Replace the response reason in the HTX message <htx> by <reason>. It returns
506 * 1 on success, otherwise 0.
507*/
508int http_replace_res_reason(struct htx *htx, const struct ist reason)
509{
510 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200511 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100512 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200513
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100514 if (!sl)
515 return 0;
516
Christopher Faulete010c802018-10-24 10:36:45 +0200517 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
519 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200520
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100521 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
522 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200523
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100524 /* create the new start line */
525 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200526}
527
Christopher Faulet47596d32018-10-22 09:17:28 +0200528/* Replaces a part of a header value referenced in the context <ctx> by
529 * <data>. It returns 1 on success, otherwise it returns 0. The context is
530 * updated if necessary.
531 */
532int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
533{
534 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200535 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200536 char *start;
537 struct ist v;
538 uint32_t len, off;
539
540 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200541 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200542
543 v = htx_get_blk_value(htx, blk);
544 start = ctx->value.ptr - ctx->lws_before;
545 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
546 off = start - v.ptr;
547
548 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
549 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200550 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200551
552 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200553
554 sl = http_get_stline(htx);
555 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
556 struct ist n = htx_get_blk_name(htx, blk);
557
558 if (isteq(n, ist("host"))) {
559 if (!http_update_authority(htx, sl, v))
560 goto fail;
561 ctx->blk = NULL;
562 http_find_header(htx, ist("host"), ctx, 1);
563 blk = ctx->blk;
564 v = htx_get_blk_value(htx, blk);
565 }
566 }
567
Christopher Faulet47596d32018-10-22 09:17:28 +0200568 ctx->blk = blk;
569 ctx->value.ptr = v.ptr + off;
570 ctx->value.len = data.len;
571 ctx->lws_before = ctx->lws_after = 0;
572
573 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200574 fail:
575 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200576}
577
578/* Fully replaces a header referenced in the context <ctx> by the name <name>
579 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
580 * context is updated if necessary.
581 */
582int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
583 const struct ist name, const struct ist value)
584{
585 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200586 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200587
588 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200589 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200590
591 blk = htx_replace_header(htx, blk, name, value);
592 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200593 goto fail;
594
595 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100596 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200597 if (!http_update_authority(htx, sl, value))
598 goto fail;
599 ctx->blk = NULL;
600 http_find_header(htx, ist("host"), ctx, 1);
601 blk = ctx->blk;
602 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200603
604 ctx->blk = blk;
605 ctx->value = ist(NULL);
606 ctx->lws_before = ctx->lws_after = 0;
607
608 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200609 fail:
610 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200611}
612
613/* Remove one value of a header. This only works on a <ctx> returned by
614 * http_find_header function. The value is removed, as well as surrounding commas
615 * if any. If the removed value was alone, the whole header is removed. The
616 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
617 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
618 * form that can be handled by http_find_header() to find next occurrence.
619 */
620int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
621{
622 struct htx_blk *blk = ctx->blk;
623 char *start;
624 struct ist v;
625 uint32_t len;
626
627 if (!blk)
628 return 0;
629
630 start = ctx->value.ptr - ctx->lws_before;
631 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
632
633 v = htx_get_blk_value(htx, blk);
634 if (len == v.len) {
635 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200636 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200637 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100638 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200639 ctx->lws_before = ctx->lws_after = 0;
640 }
641 else {
642 ctx->blk = htx_get_blk(htx, htx->tail);
643 ctx->value = htx_get_blk_value(htx, ctx->blk);
644 ctx->lws_before = ctx->lws_after = 0;
645 }
646 return 1;
647 }
648
649 /* This was not the only value of this header. We have to remove the
650 * part pointed by ctx->value. If it is the last entry of the list, we
651 * remove the last separator.
652 */
653 if (start == v.ptr) {
654 /* It's the first header part but not the only one. So remove
655 * the comma after it. */
656 len++;
657 }
658 else {
659 /* There is at least one header part before the removed one. So
660 * remove the comma between them. */
661 start--;
662 len++;
663 }
664 /* Update the block content and its len */
665 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200666 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200667
668 /* Finally update the ctx */
669 ctx->value.ptr = start;
670 ctx->value.len = 0;
671 ctx->lws_before = ctx->lws_after = 0;
672
673 return 1;
674}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200675
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200676/* Updates the authority part of the uri with the value <host>. It happens when
677 * the header host is modified. It returns 0 on failure and 1 on success. It is
678 * the caller responsibility to provide the start-line and to be sure the uri
679 * contains an authority. Thus, if no authority is found in the uri, an error is
680 * returned.
681 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200682int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200683{
684 struct buffer *temp = get_trash_chunk();
685 struct ist meth, vsn, uri, authority;
686
687 uri = htx_sl_req_uri(sl);
688 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100689 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200690 return 0;
691
Christopher Faulet34b18e42020-02-18 11:02:21 +0100692 /* Don't update the uri if there is no change */
693 if (isteq(host, authority))
694 return 1;
695
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200696 /* Start by copying old method and version */
697 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
698 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
699
700 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
701 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
702
703 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
704 chunk_memcat(temp, host.ptr, host.len);
705 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
706 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
707
708 return http_replace_stline(htx, meth, uri, vsn);
709
710}
711
712/* Update the header host by extracting the authority of the uri <uri>. flags of
713 * the start-line are also updated accordingly. For orgin-form and asterisk-form
714 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
715 * removed from the flags of the start-line. Otherwise, this flag is set and the
716 * authority is used to set the value of the header host. This function returns
717 * 0 on failure and 1 on success.
718*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200719int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200720{
721 struct ist authority;
722 struct http_hdr_ctx ctx;
723
724 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
725 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
726 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
727 }
728 else {
729 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
730 if (sl->info.req.meth != HTTP_METH_CONNECT) {
731 // absolute-form (RFC7320 #5.3.2)
732 sl->flags |= HTX_SL_F_HAS_SCHM;
733 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
734 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
735
736 authority = http_get_authority(uri, 1);
737 if (!authority.len)
738 goto fail;
739 }
740 else {
741 // authority-form (RFC7320 #5.3.3)
742 authority = uri;
743 }
744
745 /* Replace header host value */
746 ctx.blk = NULL;
747 while (http_find_header(htx, ist("host"), &ctx, 1)) {
748 if (!http_replace_header_value(htx, &ctx, authority))
749 goto fail;
750 }
751
752 }
753 return 1;
754 fail:
755 return 0;
756}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200757
758/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
759 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
760 * performed over the whole headers. Otherwise it must contain a valid header
761 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
762 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
763 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
764 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
765 * -1. The value fetch stops at commas, so this function is suited for use with
766 * list headers.
767 * The return value is 0 if nothing was found, or non-zero otherwise.
768 */
769unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
770 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
771{
772 struct http_hdr_ctx local_ctx;
773 struct ist val_hist[MAX_HDR_HISTORY];
774 unsigned int hist_idx;
775 int found;
776
777 if (!ctx) {
778 local_ctx.blk = NULL;
779 ctx = &local_ctx;
780 }
781
782 if (occ >= 0) {
783 /* search from the beginning */
784 while (http_find_header(htx, hdr, ctx, 0)) {
785 occ--;
786 if (occ <= 0) {
787 *vptr = ctx->value.ptr;
788 *vlen = ctx->value.len;
789 return 1;
790 }
791 }
792 return 0;
793 }
794
795 /* negative occurrence, we scan all the list then walk back */
796 if (-occ > MAX_HDR_HISTORY)
797 return 0;
798
799 found = hist_idx = 0;
800 while (http_find_header(htx, hdr, ctx, 0)) {
801 val_hist[hist_idx] = ctx->value;
802 if (++hist_idx >= MAX_HDR_HISTORY)
803 hist_idx = 0;
804 found++;
805 }
806 if (-occ > found)
807 return 0;
808
809 /* OK now we have the last occurrence in [hist_idx-1], and we need to
810 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
811 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
812 * to remain in the 0..9 range.
813 */
814 hist_idx += occ + MAX_HDR_HISTORY;
815 if (hist_idx >= MAX_HDR_HISTORY)
816 hist_idx -= MAX_HDR_HISTORY;
817 *vptr = val_hist[hist_idx].ptr;
818 *vlen = val_hist[hist_idx].len;
819 return 1;
820}
821
822/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
823 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
824 * performed over the whole headers. Otherwise it must contain a valid header
825 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
826 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
827 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
828 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
829 * -1. This function differs from http_get_hdr() in that it only returns full
830 * line header values and does not stop at commas.
831 * The return value is 0 if nothing was found, or non-zero otherwise.
832 */
833unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
834 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
835{
836 struct http_hdr_ctx local_ctx;
837 struct ist val_hist[MAX_HDR_HISTORY];
838 unsigned int hist_idx;
839 int found;
840
841 if (!ctx) {
842 local_ctx.blk = NULL;
843 ctx = &local_ctx;
844 }
845
846 if (occ >= 0) {
847 /* search from the beginning */
848 while (http_find_header(htx, hdr, ctx, 1)) {
849 occ--;
850 if (occ <= 0) {
851 *vptr = ctx->value.ptr;
852 *vlen = ctx->value.len;
853 return 1;
854 }
855 }
856 return 0;
857 }
858
859 /* negative occurrence, we scan all the list then walk back */
860 if (-occ > MAX_HDR_HISTORY)
861 return 0;
862
863 found = hist_idx = 0;
864 while (http_find_header(htx, hdr, ctx, 1)) {
865 val_hist[hist_idx] = ctx->value;
866 if (++hist_idx >= MAX_HDR_HISTORY)
867 hist_idx = 0;
868 found++;
869 }
870 if (-occ > found)
871 return 0;
872
873 /* OK now we have the last occurrence in [hist_idx-1], and we need to
874 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
875 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
876 * to remain in the 0..9 range.
877 */
878 hist_idx += occ + MAX_HDR_HISTORY;
879 if (hist_idx >= MAX_HDR_HISTORY)
880 hist_idx -= MAX_HDR_HISTORY;
881 *vptr = val_hist[hist_idx].ptr;
882 *vlen = val_hist[hist_idx].len;
883 return 1;
884}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100885
Christopher Faulet90cc4812019-07-22 16:49:30 +0200886int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100887{
888 struct htx *htx;
889 struct htx_sl *sl;
890 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200891 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100892 union h1_sl h1sl;
893 unsigned int flags = HTX_SL_F_IS_RESP;
894 int ret = 0;
895
Christopher Faulet90cc4812019-07-22 16:49:30 +0200896 b_reset(buf);
897 if (!raw.len) {
898 buf->size = 0;
899 buf->area = malloc(raw.len);
900 return 1;
901 }
902
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100903 buf->size = global.tune.bufsize;
904 buf->area = (char *)malloc(buf->size);
905 if (!buf->area)
906 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100907
908 h1m_init_res(&h1m);
909 h1m.flags |= H1_MF_NO_PHDR;
910 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
911 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
912 if (ret <= 0)
913 goto error;
914
915 if (unlikely(h1sl.st.v.len != 8))
916 goto error;
917 if ((*(h1sl.st.v.ptr + 5) > '1') ||
918 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
919 h1m.flags |= H1_MF_VER_11;
920
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200921 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
922 goto error;
923
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100924 if (h1m.flags & H1_MF_VER_11)
925 flags |= HTX_SL_F_VER_11;
926 if (h1m.flags & H1_MF_XFER_ENC)
927 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200928 if (h1m.flags & H1_MF_CLEN) {
929 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
930 if (h1m.body_len == 0)
931 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100932 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200933 if (h1m.flags & H1_MF_CHNK)
934 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100935
936 htx = htx_from_buf(buf);
937 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
938 if (!sl || !htx_add_all_headers(htx, hdrs))
939 goto error;
940 sl->info.res.status = h1sl.st.status;
941
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200942 while (raw.len > ret) {
943 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
944 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100945 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200946 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100947 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200948
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100949 if (!htx_add_endof(htx, HTX_BLK_EOM))
950 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200951
Christopher Faulet90cc4812019-07-22 16:49:30 +0200952 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100953
954error:
955 if (buf->size)
956 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200957 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100958}
959
Christopher Faulet18630642020-05-12 18:57:28 +0200960void release_http_reply(struct http_reply *http_reply)
961{
962 struct logformat_node *lf, *lfb;
963 struct http_reply_hdr *hdr, *hdrb;
964
965 if (!http_reply)
966 return;
967
968 free(http_reply->ctype);
969 http_reply->ctype = NULL;
970 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
971 LIST_DEL(&hdr->list);
972 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
973 LIST_DEL(&lf->list);
974 release_sample_expr(lf->expr);
975 free(lf->arg);
976 free(lf);
977 }
978 istfree(&hdr->name);
979 free(hdr);
980 }
981
982 if (http_reply->type == HTTP_REPLY_ERRFILES) {
983 free(http_reply->body.http_errors);
984 http_reply->body.http_errors = NULL;
985 }
986 else if (http_reply->type == HTTP_REPLY_RAW)
987 chunk_destroy(&http_reply->body.obj);
988 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
989 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
990 LIST_DEL(&lf->list);
991 release_sample_expr(lf->expr);
992 free(lf->arg);
993 free(lf);
994 }
995 }
Christopher Faulet63d48242020-05-21 09:59:22 +0200996 free(http_reply);
Christopher Faulet18630642020-05-12 18:57:28 +0200997}
998
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100999static int http_htx_init(void)
1000{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001001 struct buffer chk;
1002 struct ist raw;
1003 int rc;
1004 int err_code = 0;
1005
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001006 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1007 if (!http_err_msgs[rc]) {
1008 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1009 err_code |= ERR_ALERT | ERR_FATAL;
1010 continue;
1011 }
1012
1013 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1014 if (!http_str_to_htx(&chk, raw)) {
1015 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1016 http_err_codes[rc]);
1017 err_code |= ERR_ALERT | ERR_FATAL;
1018 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001019 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001020 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1021 http_err_replies[rc].status = http_err_codes[rc];
1022 http_err_replies[rc].ctype = NULL;
1023 LIST_INIT(&http_err_replies[rc].hdrs);
1024 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001025 }
1026end:
1027 return err_code;
1028}
1029
Christopher Faulet58857752020-01-15 15:19:50 +01001030static void http_htx_deinit(void)
1031{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001032 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001033 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001034 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001035 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001036 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001037
1038 node = ebpt_first(&http_error_messages);
1039 while (node) {
1040 next = ebpt_next(node);
1041 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001042 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1043 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001044 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001045 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001046 node = next;
1047 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001048
1049 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1050 free(http_errs->conf.file);
1051 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1053 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001054 LIST_DEL(&http_errs->list);
1055 free(http_errs);
1056 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001057
1058 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
1059 LIST_DEL(&http_rep->list);
1060 release_http_reply(http_rep);
1061 }
Christopher Faulet58857752020-01-15 15:19:50 +01001062}
1063
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001064REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001065REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001066
Christopher Faulet58857752020-01-15 15:19:50 +01001067/* Reads content of the error file <file> and convert it into an HTX message. On
1068 * success, the HTX message is returned. On error, NULL is returned and an error
1069 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001070 */
Christopher Faulet58857752020-01-15 15:19:50 +01001071struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001072{
Christopher Faulet58857752020-01-15 15:19:50 +01001073 struct buffer *buf = NULL;
1074 struct buffer chk;
1075 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001076 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001077 struct stat stat;
1078 char *err = NULL;
1079 int errnum, errlen;
1080 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001081
1082 /* already loaded */
1083 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1084 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001085 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1086 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001087 goto out;
1088 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001089
Christopher Faulet58857752020-01-15 15:19:50 +01001090 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001091 fd = open(file, O_RDONLY);
1092 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1093 memprintf(errmsg, "error opening file '%s'.", file);
1094 goto out;
1095 }
1096
1097 if (stat.st_size <= global.tune.bufsize)
1098 errlen = stat.st_size;
1099 else {
1100 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1101 file, global.tune.bufsize);
1102 errlen = global.tune.bufsize;
1103 }
1104
1105 err = malloc(errlen);
1106 if (!err) {
1107 memprintf(errmsg, "out of memory.");
1108 goto out;
1109 }
1110
1111 errnum = read(fd, err, errlen);
1112 if (errnum != errlen) {
1113 memprintf(errmsg, "error reading file '%s'.", file);
1114 goto out;
1115 }
1116
Christopher Faulet58857752020-01-15 15:19:50 +01001117 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001118 http_errmsg = calloc(1, sizeof(*http_errmsg));
1119 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001120 memprintf(errmsg, "out of memory.");
1121 goto out;
1122 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001123 http_errmsg->node.key = strdup(file);
1124 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001125 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001126 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001127 goto out;
1128 }
1129
1130 /* Convert the error file into an HTX message */
1131 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001132 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001133 free(http_errmsg->node.key);
1134 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001135 goto out;
1136 }
1137
Christopher Faulet58857752020-01-15 15:19:50 +01001138 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001139 http_errmsg->msg = chk;
1140 ebis_insert(&http_error_messages, &http_errmsg->node);
1141 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001142
Christopher Faulet5031ef52020-01-15 11:22:07 +01001143 out:
1144 if (fd >= 0)
1145 close(fd);
1146 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001147 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001148}
1149
Ilya Shipitsind4259502020-04-08 01:07:56 +05001150/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001151 * message is returned. On error, NULL is returned and an error message is
1152 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001153 */
Christopher Faulet58857752020-01-15 15:19:50 +01001154struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001155{
Christopher Faulet58857752020-01-15 15:19:50 +01001156 struct buffer *buf = NULL;
1157 struct buffer chk;
1158 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001159 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001160
1161 /* already loaded */
1162 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1163 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001164 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1165 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001166 goto out;
1167 }
1168 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001169 http_errmsg = calloc(1, sizeof(*http_errmsg));
1170 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001171 memprintf(errmsg, "out of memory.");
1172 goto out;
1173 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001174 http_errmsg->node.key = strdup(key);
1175 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001176 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001177 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001178 goto out;
1179 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001180
1181 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001182 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001183 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001184 free(http_errmsg->node.key);
1185 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001186 goto out;
1187 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001188
Christopher Faulet58857752020-01-15 15:19:50 +01001189 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001190 http_errmsg->msg = chk;
1191 ebis_insert(&http_error_messages, &http_errmsg->node);
1192 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001193 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001194 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001195}
1196
Christopher Faulet5031ef52020-01-15 11:22:07 +01001197/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001198 * <status>. It returns NULL if there is any error, otherwise it return the
1199 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001200 */
Christopher Faulet58857752020-01-15 15:19:50 +01001201struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001202{
Christopher Faulet58857752020-01-15 15:19:50 +01001203 struct buffer *buf = NULL;
1204 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001205
1206 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1207 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001208 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001209 break;
1210 }
1211 }
1212
1213 if (rc >= HTTP_ERR_SIZE)
1214 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001215 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001216}
1217
1218/* This function creates HTX error message corresponding to a redirect message
1219 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001220 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1221 * returns NULL if there is any error, otherwise it return the corresponding HTX
1222 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001223 */
Christopher Faulet58857752020-01-15 15:19:50 +01001224struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001225{
Christopher Faulet58857752020-01-15 15:19:50 +01001226 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001227 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001228 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001229 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001230
1231 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1232 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001233 /* Create the error key */
1234 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1235 memprintf(errmsg, "out of memory.");
1236 goto out;
1237 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001238 /* Create the error message */
1239 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1240 errlen = strlen(msg) + strlen(url) + 5;
1241 err = malloc(errlen);
1242 if (!err) {
1243 memprintf(errmsg, "out of memory.");
1244 goto out;
1245 }
1246 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1247
1248 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001249 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001250 break;
1251 }
1252 }
1253
1254 if (rc >= HTTP_ERR_SIZE)
1255 memprintf(errmsg, "status code '%d' not handled.", status);
1256out:
Christopher Faulet58857752020-01-15 15:19:50 +01001257 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001258 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001259 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001260}
1261
Christopher Faulet7eea2412020-05-13 15:02:59 +02001262/* Check an "http reply" and, for replies referencing an http-errors section,
1263 * try to find the right section and the right error message in this section. If
1264 * found, the reply is updated. If the http-errors section exists but the error
1265 * message is not found, no error message is set to fallback on the default
1266 * ones. Otherwise (unknown section) an error is returned.
1267 *
1268 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1269 * filled.
1270 */
1271int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1272{
1273 struct http_errors *http_errs;
1274 int ret = 1;
1275
1276 if (reply->type != HTTP_REPLY_ERRFILES)
1277 goto end;
1278
1279 list_for_each_entry(http_errs, &http_errors_list, list) {
1280 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001281 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001282 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001283 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1284 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001285 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1286 "not declared in http-errors section '%s'.\n",
1287 px->id, reply->status, http_errs->id);
1288 break;
1289 }
1290 }
1291
1292 if (&http_errs->list == &http_errors_list) {
1293 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1294 reply->body.http_errors);
1295 ret = 0;
1296 }
1297
1298 end:
1299 return ret;
1300}
1301
Christopher Faulet47e791e2020-05-13 14:36:55 +02001302/* Parse an "http reply". It returns the reply on success or NULL on error. This
1303 * function creates one of the following http replies :
1304 *
1305 * - HTTP_REPLY_EMPTY : dummy response, no payload
1306 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1307 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1308 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1309 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1310 *
1311 * The content-type must be defined for non-empty payload. It is ignored for
1312 * error messages (implicit or explicit). When an http-errors section is
1313 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1314 * during the configuration validity check or dynamically. It is the caller
1315 * responsibility to choose. If no status code is configured, <default_status>
1316 * is set.
1317 */
1318struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1319 int default_status, char **errmsg)
1320{
1321 struct logformat_node *lf, *lfb;
1322 struct http_reply *reply = NULL;
1323 struct http_reply_hdr *hdr, *hdrb;
1324 struct stat stat;
1325 const char *act_arg = NULL;
1326 char *obj = NULL;
1327 int cur_arg, cap, objlen = 0, fd = -1;
1328
1329
1330 reply = calloc(1, sizeof(*reply));
1331 if (!reply) {
1332 memprintf(errmsg, "out of memory");
1333 goto error;
1334 }
1335 LIST_INIT(&reply->hdrs);
1336 reply->type = HTTP_REPLY_EMPTY;
1337 reply->status = default_status;
1338
Christopher Faulet3b967c12020-05-15 15:47:44 +02001339 if (px->conf.args.ctx == ARGC_HERR)
1340 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1341 else
1342 cap = ((px->conf.args.ctx == ARGC_HRQ)
1343 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1344 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001345
1346 cur_arg = *orig_arg;
1347 while (*args[cur_arg]) {
1348 if (strcmp(args[cur_arg], "status") == 0) {
1349 cur_arg++;
1350 if (!*args[cur_arg]) {
1351 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1352 goto error;
1353 }
1354 reply->status = atol(args[cur_arg]);
1355 if (reply->status < 200 || reply->status > 599) {
1356 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1357 goto error;
1358 }
1359 cur_arg++;
1360 }
1361 else if (strcmp(args[cur_arg], "content-type") == 0) {
1362 cur_arg++;
1363 if (!*args[cur_arg]) {
1364 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1365 goto error;
1366 }
1367 free(reply->ctype);
1368 reply->ctype = strdup(args[cur_arg]);
1369 cur_arg++;
1370 }
1371 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1372 if (reply->type != HTTP_REPLY_EMPTY) {
1373 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1374 goto error;
1375 }
1376 act_arg = args[cur_arg];
1377 cur_arg++;
1378 if (!*args[cur_arg]) {
1379 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1380 goto error;
1381 }
1382 reply->body.http_errors = strdup(args[cur_arg]);
1383 if (!reply->body.http_errors) {
1384 memprintf(errmsg, "out of memory");
1385 goto error;
1386 }
1387 reply->type = HTTP_REPLY_ERRFILES;
1388 cur_arg++;
1389 }
1390 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1391 if (reply->type != HTTP_REPLY_EMPTY) {
1392 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1393 goto error;
1394 }
1395 act_arg = args[cur_arg];
1396 reply->type = HTTP_REPLY_ERRMSG;
1397 cur_arg++;
1398 }
1399 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1400 if (reply->type != HTTP_REPLY_EMPTY) {
1401 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1402 goto error;
1403 }
1404 act_arg = args[cur_arg];
1405 cur_arg++;
1406 if (!*args[cur_arg]) {
1407 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1408 goto error;
1409 }
1410 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1411 if (!reply->body.errmsg) {
1412 goto error;
1413 }
1414 reply->type = HTTP_REPLY_ERRMSG;
1415 cur_arg++;
1416 }
1417 else if (strcmp(args[cur_arg], "file") == 0) {
1418 if (reply->type != HTTP_REPLY_EMPTY) {
1419 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1420 goto error;
1421 }
1422 act_arg = args[cur_arg];
1423 cur_arg++;
1424 if (!*args[cur_arg]) {
1425 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1426 goto error;
1427 }
1428 fd = open(args[cur_arg], O_RDONLY);
1429 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1430 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1431 goto error;
1432 }
1433 if (stat.st_size > global.tune.bufsize) {
1434 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1435 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1436 goto error;
1437 }
1438 objlen = stat.st_size;
1439 obj = malloc(objlen);
1440 if (!obj || read(fd, obj, objlen) != objlen) {
1441 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1442 goto error;
1443 }
1444 close(fd);
1445 fd = -1;
1446 reply->type = HTTP_REPLY_RAW;
1447 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1448 obj = NULL;
1449 cur_arg++;
1450 }
1451 else if (strcmp(args[cur_arg], "string") == 0) {
1452 if (reply->type != HTTP_REPLY_EMPTY) {
1453 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1454 goto error;
1455 }
1456 act_arg = args[cur_arg];
1457 cur_arg++;
1458 if (!*args[cur_arg]) {
1459 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1460 goto error;
1461 }
1462 obj = strdup(args[cur_arg]);
1463 objlen = strlen(args[cur_arg]);
1464 if (!obj) {
1465 memprintf(errmsg, "out of memory");
1466 goto error;
1467 }
1468 reply->type = HTTP_REPLY_RAW;
1469 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1470 obj = NULL;
1471 cur_arg++;
1472 }
1473 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1474 if (reply->type != HTTP_REPLY_EMPTY) {
1475 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1476 goto error;
1477 }
1478 act_arg = args[cur_arg];
1479 cur_arg++;
1480 if (!*args[cur_arg]) {
1481 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1482 goto error;
1483 }
1484 fd = open(args[cur_arg], O_RDONLY);
1485 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1486 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1487 goto error;
1488 }
1489 if (stat.st_size > global.tune.bufsize) {
1490 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1491 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1492 goto error;
1493 }
1494 objlen = stat.st_size;
1495 obj = malloc(objlen + 1);
1496 if (!obj || read(fd, obj, objlen) != objlen) {
1497 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1498 goto error;
1499 }
1500 close(fd);
1501 fd = -1;
1502 obj[objlen] = '\0';
1503 reply->type = HTTP_REPLY_LOGFMT;
1504 cur_arg++;
1505 }
1506 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1507 if (reply->type != HTTP_REPLY_EMPTY) {
1508 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1509 goto error;
1510 }
1511 act_arg = args[cur_arg];
1512 cur_arg++;
1513 if (!*args[cur_arg]) {
1514 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1515 goto error;
1516 }
1517 obj = strdup(args[cur_arg]);
1518 objlen = strlen(args[cur_arg]);
1519 reply->type = HTTP_REPLY_LOGFMT;
1520 cur_arg++;
1521 }
1522 else if (strcmp(args[cur_arg], "hdr") == 0) {
1523 cur_arg++;
1524 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1525 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1526 goto error;
1527 }
1528 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1529 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1530 strcasecmp(args[cur_arg], "content-type") == 0) {
1531 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1532 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1533 cur_arg += 2;
1534 continue;
1535 }
1536 hdr = calloc(1, sizeof(*hdr));
1537 if (!hdr) {
1538 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1539 goto error;
1540 }
Christopher Fauletd6e31232020-05-21 10:10:41 +02001541 LIST_ADDQ(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001542 LIST_INIT(&hdr->value);
1543 hdr->name = ist(strdup(args[cur_arg]));
1544 if (!isttest(hdr->name)) {
1545 memprintf(errmsg, "out of memory");
1546 goto error;
1547 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001548 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1549 goto error;
1550
1551 free(px->conf.lfs_file);
1552 px->conf.lfs_file = strdup(px->conf.args.file);
1553 px->conf.lfs_line = px->conf.args.line;
1554 cur_arg += 2;
1555 }
1556 else
1557 break;
1558 }
1559
1560 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1561 if (reply->ctype) {
1562 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1563 " neither errorfile nor payload defined.\n",
1564 px->conf.args.file, px->conf.args.line, reply->ctype);
1565 free(reply->ctype);
1566 reply->ctype = NULL;
1567 }
1568 }
1569 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1570
1571 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1572 /* default errorfile or errorfiles: check the status */
1573 int rc;
1574
1575 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1576 if (http_err_codes[rc] == reply->status)
1577 break;
1578 }
1579
1580 if (rc >= HTTP_ERR_SIZE) {
1581 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1582 reply->status, act_arg);
1583 goto error;
1584 }
1585 }
1586
1587 if (reply->ctype) {
1588 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1589 "with an erorrfile.\n",
1590 px->conf.args.file, px->conf.args.line, reply->ctype);
1591 free(reply->ctype);
1592 reply->ctype = NULL;
1593 }
1594 if (!LIST_ISEMPTY(&reply->hdrs)) {
1595 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1596 "with an erorrfile.\n",
1597 px->conf.args.file, px->conf.args.line);
1598 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1599 LIST_DEL(&hdr->list);
1600 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1601 LIST_DEL(&lf->list);
1602 release_sample_expr(lf->expr);
1603 free(lf->arg);
1604 free(lf);
1605 }
1606 istfree(&hdr->name);
1607 free(hdr);
1608 }
1609 }
1610 }
1611 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1612 if (!reply->ctype && objlen) {
1613 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1614 goto error;
1615 }
1616 if (reply->ctype && !b_data(&reply->body.obj)) {
1617 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1618 "with an emtpy payload.\n",
1619 px->conf.args.file, px->conf.args.line, reply->ctype);
1620 free(reply->ctype);
1621 reply->ctype = NULL;
1622 }
1623 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1624 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1625 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1626 px->conf.args.file, px->conf.args.line);
1627 }
1628 }
1629 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1630 LIST_INIT(&reply->body.fmt);
1631 if (!reply->ctype) {
1632 memprintf(errmsg, "a content type must be defined with a log-format payload");
1633 goto error;
1634 }
1635 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1636 goto error;
1637
1638 free(px->conf.lfs_file);
1639 px->conf.lfs_file = strdup(px->conf.args.file);
1640 px->conf.lfs_line = px->conf.args.line;
1641 }
1642
1643 free(obj);
1644 *orig_arg = cur_arg;
1645 return reply;
1646
1647 error:
1648 free(obj);
1649 if (fd >= 0)
1650 close(fd);
1651 release_http_reply(reply);
1652 return NULL;
1653}
1654
Christopher Faulet07f41f72020-01-16 16:16:06 +01001655/* Parses the "errorloc[302|303]" proxy keyword */
1656static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1657 struct proxy *defpx, const char *file, int line,
1658 char **errmsg)
1659{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001660 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001661 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001662 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001663 int errloc, status;
1664 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001665
1666 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1667 ret = 1;
1668 goto out;
1669 }
1670
1671 if (*(args[1]) == 0 || *(args[2]) == 0) {
1672 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1673 ret = -1;
1674 goto out;
1675 }
1676
1677 status = atol(args[1]);
1678 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1679 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1680 if (!msg) {
1681 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1682 ret = -1;
1683 goto out;
1684 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001685
1686 reply = calloc(1, sizeof(*reply));
1687 if (!reply) {
1688 memprintf(errmsg, "%s : out of memory.", args[0]);
1689 ret = -1;
1690 goto out;
1691 }
1692 reply->type = HTTP_REPLY_ERRMSG;
1693 reply->status = status;
1694 reply->ctype = NULL;
1695 LIST_INIT(&reply->hdrs);
1696 reply->body.errmsg = msg;
1697 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001698
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001699 conf_err = calloc(1, sizeof(*conf_err));
1700 if (!conf_err) {
1701 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001702 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001703 ret = -1;
1704 goto out;
1705 }
1706 conf_err->type = 1;
1707 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001708 conf_err->info.errorfile.reply = reply;
1709
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001710 conf_err->file = strdup(file);
1711 conf_err->line = line;
1712 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001713
1714 out:
1715 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001716
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001717}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001718
1719/* Parses the "errorfile" proxy keyword */
1720static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1721 struct proxy *defpx, const char *file, int line,
1722 char **errmsg)
1723{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001724 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001725 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001726 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001727 int status;
1728 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001729
1730 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1731 ret = 1;
1732 goto out;
1733 }
1734
1735 if (*(args[1]) == 0 || *(args[2]) == 0) {
1736 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1737 ret = -1;
1738 goto out;
1739 }
1740
1741 status = atol(args[1]);
1742 msg = http_parse_errorfile(status, args[2], errmsg);
1743 if (!msg) {
1744 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1745 ret = -1;
1746 goto out;
1747 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001748
Christopher Faulet5809e102020-05-14 17:31:52 +02001749 reply = calloc(1, sizeof(*reply));
1750 if (!reply) {
1751 memprintf(errmsg, "%s : out of memory.", args[0]);
1752 ret = -1;
1753 goto out;
1754 }
1755 reply->type = HTTP_REPLY_ERRMSG;
1756 reply->status = status;
1757 reply->ctype = NULL;
1758 LIST_INIT(&reply->hdrs);
1759 reply->body.errmsg = msg;
1760 LIST_ADDQ(&http_replies_list, &reply->list);
1761
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001762 conf_err = calloc(1, sizeof(*conf_err));
1763 if (!conf_err) {
1764 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001765 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001766 ret = -1;
1767 goto out;
1768 }
1769 conf_err->type = 1;
1770 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001771 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001772 conf_err->file = strdup(file);
1773 conf_err->line = line;
1774 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1775
1776 out:
1777 return ret;
1778
1779}
1780
1781/* Parses the "errorfiles" proxy keyword */
1782static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1783 struct proxy *defpx, const char *file, int line,
1784 char **err)
1785{
1786 struct conf_errors *conf_err = NULL;
1787 char *name = NULL;
1788 int rc, ret = 0;
1789
1790 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1791 ret = 1;
1792 goto out;
1793 }
1794
1795 if (!*(args[1])) {
1796 memprintf(err, "%s : expects <name> as argument.", args[0]);
1797 ret = -1;
1798 goto out;
1799 }
1800
1801 name = strdup(args[1]);
1802 conf_err = calloc(1, sizeof(*conf_err));
1803 if (!name || !conf_err) {
1804 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001805 goto error;
1806 }
1807 conf_err->type = 0;
1808
1809 conf_err->info.errorfiles.name = name;
1810 if (!*(args[2])) {
1811 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1812 conf_err->info.errorfiles.status[rc] = 1;
1813 }
1814 else {
1815 int cur_arg, status;
1816 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1817 status = atol(args[cur_arg]);
1818
1819 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1820 if (http_err_codes[rc] == status) {
1821 conf_err->info.errorfiles.status[rc] = 2;
1822 break;
1823 }
1824 }
1825 if (rc >= HTTP_ERR_SIZE) {
1826 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001827 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001828 }
1829 }
1830 }
1831 conf_err->file = strdup(file);
1832 conf_err->line = line;
1833 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1834 out:
1835 return ret;
1836
1837 error:
1838 free(name);
1839 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001840 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001841 goto out;
1842}
1843
Christopher Faulet3b967c12020-05-15 15:47:44 +02001844/* Parses the "http-error" proxy keyword */
1845static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
1846 struct proxy *defpx, const char *file, int line,
1847 char **errmsg)
1848{
1849 struct conf_errors *conf_err;
1850 struct http_reply *reply = NULL;
1851 int rc, cur_arg, ret = 0;
1852
1853 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1854 ret = 1;
1855 goto out;
1856 }
1857
1858 cur_arg = 1;
1859 curpx->conf.args.ctx = ARGC_HERR;
1860 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1861 if (!reply) {
1862 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1863 goto error;
1864 }
1865 else if (!reply->status) {
1866 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1867 goto error;
1868 }
1869
1870 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1871 if (http_err_codes[rc] == reply->status)
1872 break;
1873 }
1874
1875 if (rc >= HTTP_ERR_SIZE) {
1876 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1877 goto error;
1878 }
1879 if (*args[cur_arg]) {
1880 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1881 goto error;
1882 }
1883
1884 conf_err = calloc(1, sizeof(*conf_err));
1885 if (!conf_err) {
1886 memprintf(errmsg, "%s : out of memory.", args[0]);
1887 goto error;
1888 }
1889 if (reply->type == HTTP_REPLY_ERRFILES) {
1890 int rc = http_get_status_idx(reply->status);
1891
1892 conf_err->type = 2;
1893 conf_err->info.errorfiles.name = reply->body.http_errors;
1894 conf_err->info.errorfiles.status[rc] = 2;
1895 reply->body.http_errors = NULL;
1896 release_http_reply(reply);
1897 }
1898 else {
1899 conf_err->type = 1;
1900 conf_err->info.errorfile.status = reply->status;
1901 conf_err->info.errorfile.reply = reply;
1902 LIST_ADDQ(&http_replies_list, &reply->list);
1903 }
1904 conf_err->file = strdup(file);
1905 conf_err->line = line;
1906 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1907
1908 out:
1909 return ret;
1910
1911 error:
1912 release_http_reply(reply);
1913 ret = -1;
1914 goto out;
1915
1916}
1917
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001918/* Check "errorfiles" proxy keyword */
1919static int proxy_check_errors(struct proxy *px)
1920{
1921 struct conf_errors *conf_err, *conf_err_back;
1922 struct http_errors *http_errs;
1923 int rc, err = 0;
1924
1925 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1926 if (conf_err->type == 1) {
1927 /* errorfile */
1928 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02001929 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001930
1931 /* For proxy, to rely on default replies, just don't reference a reply */
1932 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
1933 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001934 }
1935 else {
1936 /* errorfiles */
1937 list_for_each_entry(http_errs, &http_errors_list, list) {
1938 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1939 break;
1940 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001941
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001942 /* unknown http-errors section */
1943 if (&http_errs->list == &http_errors_list) {
1944 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1945 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1946 err |= ERR_ALERT | ERR_FATAL;
1947 free(conf_err->info.errorfiles.name);
1948 goto next;
1949 }
1950
1951 free(conf_err->info.errorfiles.name);
1952 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1953 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02001954 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02001955 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001956 else if (conf_err->info.errorfiles.status[rc] == 2)
1957 ha_warning("config: proxy '%s' : status '%d' not declared in"
1958 " http-errors section '%s' (at %s:%d).\n",
1959 px->id, http_err_codes[rc], http_errs->id,
1960 conf_err->file, conf_err->line);
1961 }
1962 }
1963 }
1964 next:
1965 LIST_DEL(&conf_err->list);
1966 free(conf_err->file);
1967 free(conf_err);
1968 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001969
1970 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001971 return err;
1972}
1973
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001974static int post_check_errors()
1975{
1976 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001977 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001978 struct htx *htx;
1979 int err_code = 0;
1980
1981 node = ebpt_first(&http_error_messages);
1982 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001983 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1984 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001985 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001986 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001987 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1988 ha_warning("config: errorfile '%s' runs over the buffer space"
1989 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001990 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001991 (char *)node->key);
1992 err_code |= ERR_WARN;
1993 }
1994 next:
1995 node = ebpt_next(node);
1996 }
1997
1998 return err_code;
1999}
2000
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002001int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
2002{
2003 struct conf_errors *conf_err, *new_conf_err = NULL;
2004 int ret = 0;
2005
2006 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2007 new_conf_err = calloc(1, sizeof(*new_conf_err));
2008 if (!new_conf_err) {
2009 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2010 goto out;
2011 }
2012 new_conf_err->type = conf_err->type;
2013 if (conf_err->type == 1) {
2014 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002015 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002016 }
2017 else {
2018 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2019 if (!new_conf_err->info.errorfiles.name) {
2020 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2021 goto out;
2022 }
2023 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2024 sizeof(conf_err->info.errorfiles.status));
2025 }
2026 new_conf_err->file = strdup(conf_err->file);
2027 new_conf_err->line = conf_err->line;
2028 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
2029 new_conf_err = NULL;
2030 }
2031 ret = 1;
2032
2033 out:
2034 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002035 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002036}
2037
2038void proxy_release_conf_errors(struct proxy *px)
2039{
2040 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002041
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002042 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2043 if (conf_err->type == 0)
2044 free(conf_err->info.errorfiles.name);
2045 LIST_DEL(&conf_err->list);
2046 free(conf_err->file);
2047 free(conf_err);
2048 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002049}
2050
2051/*
2052 * Parse an <http-errors> section.
2053 * Returns the error code, 0 if OK, or any combination of :
2054 * - ERR_ABORT: must abort ASAP
2055 * - ERR_FATAL: we can continue parsing but not start the service
2056 * - ERR_WARN: a warning has been emitted
2057 * - ERR_ALERT: an alert has been emitted
2058 * Only the two first ones can stop processing, the two others are just
2059 * indicators.
2060 */
2061static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2062{
2063 static struct http_errors *curr_errs = NULL;
2064 int err_code = 0;
2065 const char *err;
2066 char *errmsg = NULL;
2067
2068 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2069 if (!*args[1]) {
2070 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2071 err_code |= ERR_ALERT | ERR_ABORT;
2072 goto out;
2073 }
2074
2075 err = invalid_char(args[1]);
2076 if (err) {
2077 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2078 file, linenum, *err, args[0], args[1]);
2079 err_code |= ERR_ALERT | ERR_FATAL;
2080 }
2081
2082 list_for_each_entry(curr_errs, &http_errors_list, list) {
2083 /* Error if two errors section owns the same name */
2084 if (strcmp(curr_errs->id, args[1]) == 0) {
2085 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2086 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2087 err_code |= ERR_ALERT | ERR_FATAL;
2088 }
2089 }
2090
2091 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2092 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2093 err_code |= ERR_ALERT | ERR_ABORT;
2094 goto out;
2095 }
2096
2097 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2098 curr_errs->id = strdup(args[1]);
2099 curr_errs->conf.file = strdup(file);
2100 curr_errs->conf.line = linenum;
2101 }
2102 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002103 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002104 struct buffer *msg;
2105 int status, rc;
2106
2107 if (*(args[1]) == 0 || *(args[2]) == 0) {
2108 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2109 file, linenum, args[0]);
2110 err_code |= ERR_ALERT | ERR_FATAL;
2111 goto out;
2112 }
2113
2114 status = atol(args[1]);
2115 msg = http_parse_errorfile(status, args[2], &errmsg);
2116 if (!msg) {
2117 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2118 err_code |= ERR_ALERT | ERR_FATAL;
2119 goto out;
2120 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002121
2122 reply = calloc(1, sizeof(*reply));
2123 if (!reply) {
2124 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2125 err_code |= ERR_ALERT | ERR_FATAL;
2126 goto out;
2127 }
2128 reply->type = HTTP_REPLY_ERRMSG;
2129 reply->status = status;
2130 reply->ctype = NULL;
2131 LIST_INIT(&reply->hdrs);
2132 reply->body.errmsg = msg;
2133
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002134 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002135 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002136 }
2137 else if (*args[0] != 0) {
2138 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2139 err_code |= ERR_ALERT | ERR_FATAL;
2140 goto out;
2141 }
2142
2143out:
2144 free(errmsg);
2145 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002146}
2147
2148static struct cfg_kw_list cfg_kws = {ILH, {
2149 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2150 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2151 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2152 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002153 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002154 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002155 { 0, NULL, NULL },
2156}};
2157
2158INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002159REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002160REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002161
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002162REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2163
Christopher Faulet29f72842019-12-11 15:52:32 +01002164/************************************************************************/
2165/* HTX sample fetches */
2166/************************************************************************/
2167
2168/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2169static int
2170smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2171{
2172 if (!smp->strm)
2173 return 0;
2174
2175 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2176 smp->data.type = SMP_T_BOOL;
2177 return 1;
2178}
2179
2180/* Returns the number of blocks in an HTX message. The channel is chosen
2181 * depending on the sample direction. */
2182static int
2183smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2184{
2185 struct channel *chn;
2186 struct htx *htx;
2187
2188 if (!smp->strm)
2189 return 0;
2190
2191 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002192 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002193 if (!htx)
2194 return 0;
2195
2196 smp->data.u.sint = htx_nbblks(htx);
2197 smp->data.type = SMP_T_SINT;
2198 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2199 return 1;
2200}
2201
2202/* Returns the size of an HTX message. The channel is chosen depending on the
2203 * sample direction. */
2204static int
2205smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2206{
2207 struct channel *chn;
2208 struct htx *htx;
2209
2210 if (!smp->strm)
2211 return 0;
2212
2213 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002214 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002215 if (!htx)
2216 return 0;
2217
2218 smp->data.u.sint = htx->size;
2219 smp->data.type = SMP_T_SINT;
2220 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2221 return 1;
2222}
2223
2224/* Returns the data size of an HTX message. The channel is chosen depending on the
2225 * sample direction. */
2226static int
2227smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2228{
2229 struct channel *chn;
2230 struct htx *htx;
2231
2232 if (!smp->strm)
2233 return 0;
2234
2235 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002236 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002237 if (!htx)
2238 return 0;
2239
2240 smp->data.u.sint = htx->data;
2241 smp->data.type = SMP_T_SINT;
2242 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2243 return 1;
2244}
2245
2246/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2247 * depending on the sample direction. */
2248static int
2249smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2250{
2251 struct channel *chn;
2252 struct htx *htx;
2253
2254 if (!smp->strm)
2255 return 0;
2256
2257 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002258 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002259 if (!htx)
2260 return 0;
2261
2262 smp->data.u.sint = htx_used_space(htx);
2263 smp->data.type = SMP_T_SINT;
2264 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2265 return 1;
2266}
2267
2268/* Returns the free space (size-used) of an HTX message. The channel is chosen
2269 * depending on the sample direction. */
2270static int
2271smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2272{
2273 struct channel *chn;
2274 struct htx *htx;
2275
2276 if (!smp->strm)
2277 return 0;
2278
2279 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002280 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002281 if (!htx)
2282 return 0;
2283
2284 smp->data.u.sint = htx_free_space(htx);
2285 smp->data.type = SMP_T_SINT;
2286 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2287 return 1;
2288}
2289
2290/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2291 * channel is chosen depending on the sample direction. */
2292static int
2293smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2294{
2295 struct channel *chn;
2296 struct htx *htx;
2297
2298 if (!smp->strm)
2299 return 0;
2300
2301 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002302 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002303 if (!htx)
2304 return 0;
2305
2306 smp->data.u.sint = htx_free_data_space(htx);
2307 smp->data.type = SMP_T_SINT;
2308 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2309 return 1;
2310}
2311
2312/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2313 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2314 * the sample direction. */
2315static int
2316smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2317{
2318 struct channel *chn;
2319 struct htx *htx;
2320
2321 if (!smp->strm)
2322 return 0;
2323
2324 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002325 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002326 if (!htx)
2327 return 0;
2328
2329 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2330 smp->data.type = SMP_T_BOOL;
2331 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2332 return 1;
2333}
2334
2335/* Returns the type of a specific HTX block, if found in the message. Otherwise
2336 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2337 * "head", "tail" or "first". The channel is chosen depending on the sample
2338 * direction. */
2339static int
2340smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2341{
2342 struct channel *chn;
2343 struct htx *htx;
2344 enum htx_blk_type type;
2345 int32_t pos;
2346
2347 if (!smp->strm || !arg_p)
2348 return 0;
2349
2350 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002351 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002352 if (!htx)
2353 return 0;
2354
2355 pos = arg_p[0].data.sint;
2356 if (pos == -1)
2357 type = htx_get_head_type(htx);
2358 else if (pos == -2)
2359 type = htx_get_tail_type(htx);
2360 else if (pos == -3)
2361 type = htx_get_first_type(htx);
2362 else
2363 type = ((pos >= htx->head && pos <= htx->tail)
2364 ? htx_get_blk_type(htx_get_blk(htx, pos))
2365 : HTX_BLK_UNUSED);
2366
2367 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2368 smp->data.type = SMP_T_STR;
2369 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2370 return 1;
2371}
2372
2373/* Returns the size of a specific HTX block, if found in the message. Otherwise
2374 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2375 * "first". The channel is chosen depending on the sample direction. */
2376static int
2377smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2378{
2379 struct channel *chn;
2380 struct htx *htx;
2381 struct htx_blk *blk;
2382 int32_t pos;
2383
2384 if (!smp->strm || !arg_p)
2385 return 0;
2386
2387 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002388 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002389 if (!htx)
2390 return 0;
2391
2392 pos = arg_p[0].data.sint;
2393 if (pos == -1)
2394 blk = htx_get_head_blk(htx);
2395 else if (pos == -2)
2396 blk = htx_get_tail_blk(htx);
2397 else if (pos == -3)
2398 blk = htx_get_first_blk(htx);
2399 else
2400 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2401
2402 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2403 smp->data.type = SMP_T_SINT;
2404 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2405 return 1;
2406}
2407
2408/* Returns the start-line if the selected HTX block exists and is a
2409 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2410 * supported or "head", "tail" or "first". The channel is chosen depending on
2411 * the sample direction. */
2412static int
2413smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2414{
2415 struct buffer *temp;
2416 struct channel *chn;
2417 struct htx *htx;
2418 struct htx_blk *blk;
2419 struct htx_sl *sl;
2420 int32_t pos;
2421
2422 if (!smp->strm || !arg_p)
2423 return 0;
2424
2425 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002426 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002427 if (!htx)
2428 return 0;
2429
2430 pos = arg_p[0].data.sint;
2431 if (pos == -1)
2432 blk = htx_get_head_blk(htx);
2433 else if (pos == -2)
2434 blk = htx_get_tail_blk(htx);
2435 else if (pos == -3)
2436 blk = htx_get_first_blk(htx);
2437 else
2438 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2439
2440 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2441 smp->data.u.str.size = 0;
2442 smp->data.u.str.area = "";
2443 smp->data.u.str.data = 0;
2444 }
2445 else {
2446 sl = htx_get_blk_ptr(htx, blk);
2447
2448 temp = get_trash_chunk();
2449 chunk_istcat(temp, htx_sl_p1(sl));
2450 temp->area[temp->data++] = ' ';
2451 chunk_istcat(temp, htx_sl_p2(sl));
2452 temp->area[temp->data++] = ' ';
2453 chunk_istcat(temp, htx_sl_p3(sl));
2454
2455 smp->data.u.str = *temp;
2456 }
2457
2458 smp->data.type = SMP_T_STR;
2459 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2460 return 1;
2461}
2462
2463/* Returns the header name if the selected HTX block exists and is a header or a
2464 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2465 * supported or "head", "tail" or "first". The channel is chosen depending on
2466 * the sample direction. */
2467static int
2468smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2469{
2470 struct channel *chn;
2471 struct htx *htx;
2472 struct htx_blk *blk;
2473 int32_t pos;
2474
2475 if (!smp->strm || !arg_p)
2476 return 0;
2477
2478 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002479 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002480 if (!htx)
2481 return 0;
2482
2483 pos = arg_p[0].data.sint;
2484 if (pos == -1)
2485 blk = htx_get_head_blk(htx);
2486 else if (pos == -2)
2487 blk = htx_get_tail_blk(htx);
2488 else if (pos == -3)
2489 blk = htx_get_first_blk(htx);
2490 else
2491 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2492
2493 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2494 smp->data.u.str.size = 0;
2495 smp->data.u.str.area = "";
2496 smp->data.u.str.data = 0;
2497 }
2498 else {
2499 struct ist name = htx_get_blk_name(htx, blk);
2500
2501 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2502 }
2503 smp->data.type = SMP_T_STR;
2504 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2505 return 1;
2506}
2507
2508/* Returns the header value if the selected HTX block exists and is a header or
2509 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2510 * supported or "head", "tail" or "first". The channel is chosen depending on
2511 * the sample direction. */
2512static int
2513smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2514{
2515 struct channel *chn;
2516 struct htx *htx;
2517 struct htx_blk *blk;
2518 int32_t pos;
2519
2520 if (!smp->strm || !arg_p)
2521 return 0;
2522
2523 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002524 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002525 if (!htx)
2526 return 0;
2527
2528 pos = arg_p[0].data.sint;
2529 if (pos == -1)
2530 blk = htx_get_head_blk(htx);
2531 else if (pos == -2)
2532 blk = htx_get_tail_blk(htx);
2533 else if (pos == -3)
2534 blk = htx_get_first_blk(htx);
2535 else
2536 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2537
2538 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2539 smp->data.u.str.size = 0;
2540 smp->data.u.str.area = "";
2541 smp->data.u.str.data = 0;
2542 }
2543 else {
2544 struct ist val = htx_get_blk_value(htx, blk);
2545
2546 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2547 }
2548 smp->data.type = SMP_T_STR;
2549 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2550 return 1;
2551}
2552
2553/* Returns the value if the selected HTX block exists and is a data
2554 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2555 * or "head", "tail" or "first". The channel is chosen depending on the sample
2556 * direction. */
2557static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002558smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002559{
2560 struct channel *chn;
2561 struct htx *htx;
2562 struct htx_blk *blk;
2563 int32_t pos;
2564
2565 if (!smp->strm || !arg_p)
2566 return 0;
2567
2568 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002569 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002570 if (!htx)
2571 return 0;
2572
2573 pos = arg_p[0].data.sint;
2574 if (pos == -1)
2575 blk = htx_get_head_blk(htx);
2576 else if (pos == -2)
2577 blk = htx_get_tail_blk(htx);
2578 else if (pos == -3)
2579 blk = htx_get_first_blk(htx);
2580 else
2581 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2582
2583 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2584 smp->data.u.str.size = 0;
2585 smp->data.u.str.area = "";
2586 smp->data.u.str.data = 0;
2587 }
2588 else {
2589 struct ist val = htx_get_blk_value(htx, blk);
2590
2591 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2592 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002593 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002594 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2595 return 1;
2596}
2597
2598/* This function is used to validate the arguments passed to any "htx_blk" fetch
2599 * keywords. An argument is expected by these keywords. It must be a positive
2600 * integer or on of the following strings: "head", "tail" or "first". It returns
2601 * 0 on error, and a non-zero value if OK.
2602 */
2603int val_blk_arg(struct arg *arg, char **err_msg)
2604{
2605 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2606 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2607 return 0;
2608 }
2609 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2610 free(arg[0].data.str.area);
2611 arg[0].type = ARGT_SINT;
2612 arg[0].data.sint = -1;
2613 }
2614 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2615 free(arg[0].data.str.area);
2616 arg[0].type = ARGT_SINT;
2617 arg[0].data.sint = -2;
2618 }
2619 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2620 free(arg[0].data.str.area);
2621 arg[0].type = ARGT_SINT;
2622 arg[0].data.sint = -3;
2623 }
2624 else {
2625 int pos;
2626
2627 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002628 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002629 memprintf(err_msg, "invalid block position");
2630 return 0;
2631 }
2632 }
2633
2634 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2635 if (pos < 0) {
2636 memprintf(err_msg, "block position must not be negative");
2637 return 0;
2638 }
2639 free(arg[0].data.str.area);
2640 arg[0].type = ARGT_SINT;
2641 arg[0].data.sint = pos;
2642 }
2643
2644 return 1;
2645}
2646
2647
2648/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002649 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002650 */
2651static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002652 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002653
Christopher Faulet01f44452020-01-08 14:23:40 +01002654 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2655 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2656 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2657 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2658 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2659 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2660 { "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 +01002661
Christopher Faulet01f44452020-01-08 14:23:40 +01002662 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2663 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2664 { "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},
2665 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2666 { "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 +01002667 { "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 +01002668
2669 { /* END */ },
2670}};
2671
2672INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);