blob: 41211bb8ff01690dc4b918a7da16d6d6aa3c9960 [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <unistd.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020022#include <haproxy/http.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020024#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020025#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/log.h>
27#include <haproxy/regex.h>
28#include <haproxy/sample.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020029
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Fauletf7346382019-07-17 22:02:08 +020031struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020032struct http_reply http_err_replies[HTTP_ERR_SIZE];
33
Christopher Faulet58857752020-01-15 15:19:50 +010034struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010035struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020036struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010037
Christopher Faulet76edc0f2020-01-13 15:52:01 +010038/* The declaration of an errorfiles/errorfile directives. Used during config
39 * parsing only. */
40struct conf_errors {
41 char type; /* directive type (0: errorfiles, 1: errorfile) */
42 union {
43 struct {
44 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020045 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010046 } errorfile; /* describe an "errorfile" directive */
47 struct {
48 char *name; /* the http-errors section name */
49 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
50 } errorfiles; /* describe an "errorfiles" directive */
51 } info;
52
53 char *file; /* file where the directive appears */
54 int line; /* line where the directive appears */
55
56 struct list list; /* next conf_errors */
57};
58
Christopher Faulet297fbb42019-05-13 14:41:27 +020059/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020060 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020061 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020062 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020063struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020064{
Christopher Faulet297fbb42019-05-13 14:41:27 +020065 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 BUG_ON(htx->first == -1);
68 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020069 if (!blk)
70 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020071 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +020072 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100162 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
204 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
391 if (!http_update_host(htx, sl, uri))
392 goto fail;
393
394 return 1;
395 fail:
396 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200397}
398
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 Faulet0bac4cd2020-05-27 10:11:59 +02001226 static const char *HTTP_302 =
1227 "HTTP/1.1 302 Found\r\n"
1228 "Cache-Control: no-cache\r\n"
1229 "Content-length: 0\r\n"
1230 "Location: "; /* not terminated since it will be concatenated with the URL */
1231 static const char *HTTP_303 =
1232 "HTTP/1.1 303 See Other\r\n"
1233 "Cache-Control: no-cache\r\n"
1234 "Content-length: 0\r\n"
1235 "Location: "; /* not terminated since it will be concatenated with the URL */
1236
Christopher Faulet58857752020-01-15 15:19:50 +01001237 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001238 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001239 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001240 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001241
1242 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1243 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001244 /* Create the error key */
1245 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1246 memprintf(errmsg, "out of memory.");
1247 goto out;
1248 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001249 /* Create the error message */
1250 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1251 errlen = strlen(msg) + strlen(url) + 5;
1252 err = malloc(errlen);
1253 if (!err) {
1254 memprintf(errmsg, "out of memory.");
1255 goto out;
1256 }
1257 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1258
1259 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001260 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001261 break;
1262 }
1263 }
1264
1265 if (rc >= HTTP_ERR_SIZE)
1266 memprintf(errmsg, "status code '%d' not handled.", status);
1267out:
Christopher Faulet58857752020-01-15 15:19:50 +01001268 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001269 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001270 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001271}
1272
Christopher Faulet7eea2412020-05-13 15:02:59 +02001273/* Check an "http reply" and, for replies referencing an http-errors section,
1274 * try to find the right section and the right error message in this section. If
1275 * found, the reply is updated. If the http-errors section exists but the error
1276 * message is not found, no error message is set to fallback on the default
1277 * ones. Otherwise (unknown section) an error is returned.
1278 *
1279 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1280 * filled.
1281 */
1282int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1283{
1284 struct http_errors *http_errs;
1285 int ret = 1;
1286
1287 if (reply->type != HTTP_REPLY_ERRFILES)
1288 goto end;
1289
1290 list_for_each_entry(http_errs, &http_errors_list, list) {
1291 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001292 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001293 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001294 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1295 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001296 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1297 "not declared in http-errors section '%s'.\n",
1298 px->id, reply->status, http_errs->id);
1299 break;
1300 }
1301 }
1302
1303 if (&http_errs->list == &http_errors_list) {
1304 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1305 reply->body.http_errors);
1306 ret = 0;
1307 }
1308
1309 end:
1310 return ret;
1311}
1312
Christopher Faulet47e791e2020-05-13 14:36:55 +02001313/* Parse an "http reply". It returns the reply on success or NULL on error. This
1314 * function creates one of the following http replies :
1315 *
1316 * - HTTP_REPLY_EMPTY : dummy response, no payload
1317 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1318 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1319 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1320 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1321 *
1322 * The content-type must be defined for non-empty payload. It is ignored for
1323 * error messages (implicit or explicit). When an http-errors section is
1324 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1325 * during the configuration validity check or dynamically. It is the caller
1326 * responsibility to choose. If no status code is configured, <default_status>
1327 * is set.
1328 */
1329struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1330 int default_status, char **errmsg)
1331{
1332 struct logformat_node *lf, *lfb;
1333 struct http_reply *reply = NULL;
1334 struct http_reply_hdr *hdr, *hdrb;
1335 struct stat stat;
1336 const char *act_arg = NULL;
1337 char *obj = NULL;
1338 int cur_arg, cap, objlen = 0, fd = -1;
1339
1340
1341 reply = calloc(1, sizeof(*reply));
1342 if (!reply) {
1343 memprintf(errmsg, "out of memory");
1344 goto error;
1345 }
1346 LIST_INIT(&reply->hdrs);
1347 reply->type = HTTP_REPLY_EMPTY;
1348 reply->status = default_status;
1349
Christopher Faulet3b967c12020-05-15 15:47:44 +02001350 if (px->conf.args.ctx == ARGC_HERR)
1351 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1352 else
1353 cap = ((px->conf.args.ctx == ARGC_HRQ)
1354 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1355 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001356
1357 cur_arg = *orig_arg;
1358 while (*args[cur_arg]) {
1359 if (strcmp(args[cur_arg], "status") == 0) {
1360 cur_arg++;
1361 if (!*args[cur_arg]) {
1362 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1363 goto error;
1364 }
1365 reply->status = atol(args[cur_arg]);
1366 if (reply->status < 200 || reply->status > 599) {
1367 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1368 goto error;
1369 }
1370 cur_arg++;
1371 }
1372 else if (strcmp(args[cur_arg], "content-type") == 0) {
1373 cur_arg++;
1374 if (!*args[cur_arg]) {
1375 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1376 goto error;
1377 }
1378 free(reply->ctype);
1379 reply->ctype = strdup(args[cur_arg]);
1380 cur_arg++;
1381 }
1382 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1383 if (reply->type != HTTP_REPLY_EMPTY) {
1384 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1385 goto error;
1386 }
1387 act_arg = args[cur_arg];
1388 cur_arg++;
1389 if (!*args[cur_arg]) {
1390 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1391 goto error;
1392 }
1393 reply->body.http_errors = strdup(args[cur_arg]);
1394 if (!reply->body.http_errors) {
1395 memprintf(errmsg, "out of memory");
1396 goto error;
1397 }
1398 reply->type = HTTP_REPLY_ERRFILES;
1399 cur_arg++;
1400 }
1401 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1402 if (reply->type != HTTP_REPLY_EMPTY) {
1403 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1404 goto error;
1405 }
1406 act_arg = args[cur_arg];
1407 reply->type = HTTP_REPLY_ERRMSG;
1408 cur_arg++;
1409 }
1410 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1411 if (reply->type != HTTP_REPLY_EMPTY) {
1412 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1413 goto error;
1414 }
1415 act_arg = args[cur_arg];
1416 cur_arg++;
1417 if (!*args[cur_arg]) {
1418 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1419 goto error;
1420 }
1421 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1422 if (!reply->body.errmsg) {
1423 goto error;
1424 }
1425 reply->type = HTTP_REPLY_ERRMSG;
1426 cur_arg++;
1427 }
1428 else if (strcmp(args[cur_arg], "file") == 0) {
1429 if (reply->type != HTTP_REPLY_EMPTY) {
1430 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1431 goto error;
1432 }
1433 act_arg = args[cur_arg];
1434 cur_arg++;
1435 if (!*args[cur_arg]) {
1436 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1437 goto error;
1438 }
1439 fd = open(args[cur_arg], O_RDONLY);
1440 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1441 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1442 goto error;
1443 }
1444 if (stat.st_size > global.tune.bufsize) {
1445 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1446 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1447 goto error;
1448 }
1449 objlen = stat.st_size;
1450 obj = malloc(objlen);
1451 if (!obj || read(fd, obj, objlen) != objlen) {
1452 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1453 goto error;
1454 }
1455 close(fd);
1456 fd = -1;
1457 reply->type = HTTP_REPLY_RAW;
1458 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1459 obj = NULL;
1460 cur_arg++;
1461 }
1462 else if (strcmp(args[cur_arg], "string") == 0) {
1463 if (reply->type != HTTP_REPLY_EMPTY) {
1464 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1465 goto error;
1466 }
1467 act_arg = args[cur_arg];
1468 cur_arg++;
1469 if (!*args[cur_arg]) {
1470 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1471 goto error;
1472 }
1473 obj = strdup(args[cur_arg]);
1474 objlen = strlen(args[cur_arg]);
1475 if (!obj) {
1476 memprintf(errmsg, "out of memory");
1477 goto error;
1478 }
1479 reply->type = HTTP_REPLY_RAW;
1480 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1481 obj = NULL;
1482 cur_arg++;
1483 }
1484 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1485 if (reply->type != HTTP_REPLY_EMPTY) {
1486 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1487 goto error;
1488 }
1489 act_arg = args[cur_arg];
1490 cur_arg++;
1491 if (!*args[cur_arg]) {
1492 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1493 goto error;
1494 }
1495 fd = open(args[cur_arg], O_RDONLY);
1496 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1497 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1498 goto error;
1499 }
1500 if (stat.st_size > global.tune.bufsize) {
1501 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1502 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1503 goto error;
1504 }
1505 objlen = stat.st_size;
1506 obj = malloc(objlen + 1);
1507 if (!obj || read(fd, obj, objlen) != objlen) {
1508 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1509 goto error;
1510 }
1511 close(fd);
1512 fd = -1;
1513 obj[objlen] = '\0';
1514 reply->type = HTTP_REPLY_LOGFMT;
1515 cur_arg++;
1516 }
1517 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1518 if (reply->type != HTTP_REPLY_EMPTY) {
1519 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1520 goto error;
1521 }
1522 act_arg = args[cur_arg];
1523 cur_arg++;
1524 if (!*args[cur_arg]) {
1525 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1526 goto error;
1527 }
1528 obj = strdup(args[cur_arg]);
1529 objlen = strlen(args[cur_arg]);
1530 reply->type = HTTP_REPLY_LOGFMT;
1531 cur_arg++;
1532 }
1533 else if (strcmp(args[cur_arg], "hdr") == 0) {
1534 cur_arg++;
1535 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1536 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1537 goto error;
1538 }
1539 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1540 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1541 strcasecmp(args[cur_arg], "content-type") == 0) {
1542 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1543 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1544 cur_arg += 2;
1545 continue;
1546 }
1547 hdr = calloc(1, sizeof(*hdr));
1548 if (!hdr) {
1549 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1550 goto error;
1551 }
Christopher Fauletd6e31232020-05-21 10:10:41 +02001552 LIST_ADDQ(&reply->hdrs, &hdr->list);
Christopher Faulet47e791e2020-05-13 14:36:55 +02001553 LIST_INIT(&hdr->value);
1554 hdr->name = ist(strdup(args[cur_arg]));
1555 if (!isttest(hdr->name)) {
1556 memprintf(errmsg, "out of memory");
1557 goto error;
1558 }
Christopher Faulet47e791e2020-05-13 14:36:55 +02001559 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1560 goto error;
1561
1562 free(px->conf.lfs_file);
1563 px->conf.lfs_file = strdup(px->conf.args.file);
1564 px->conf.lfs_line = px->conf.args.line;
1565 cur_arg += 2;
1566 }
1567 else
1568 break;
1569 }
1570
1571 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1572 if (reply->ctype) {
1573 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1574 " neither errorfile nor payload defined.\n",
1575 px->conf.args.file, px->conf.args.line, reply->ctype);
1576 free(reply->ctype);
1577 reply->ctype = NULL;
1578 }
1579 }
1580 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1581
1582 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1583 /* default errorfile or errorfiles: check the status */
1584 int rc;
1585
1586 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1587 if (http_err_codes[rc] == reply->status)
1588 break;
1589 }
1590
1591 if (rc >= HTTP_ERR_SIZE) {
1592 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1593 reply->status, act_arg);
1594 goto error;
1595 }
1596 }
1597
1598 if (reply->ctype) {
1599 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1600 "with an erorrfile.\n",
1601 px->conf.args.file, px->conf.args.line, reply->ctype);
1602 free(reply->ctype);
1603 reply->ctype = NULL;
1604 }
1605 if (!LIST_ISEMPTY(&reply->hdrs)) {
1606 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1607 "with an erorrfile.\n",
1608 px->conf.args.file, px->conf.args.line);
1609 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1610 LIST_DEL(&hdr->list);
1611 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1612 LIST_DEL(&lf->list);
1613 release_sample_expr(lf->expr);
1614 free(lf->arg);
1615 free(lf);
1616 }
1617 istfree(&hdr->name);
1618 free(hdr);
1619 }
1620 }
1621 }
1622 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1623 if (!reply->ctype && objlen) {
1624 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1625 goto error;
1626 }
1627 if (reply->ctype && !b_data(&reply->body.obj)) {
1628 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001629 "with an empty payload.\n",
Christopher Faulet47e791e2020-05-13 14:36:55 +02001630 px->conf.args.file, px->conf.args.line, reply->ctype);
1631 free(reply->ctype);
1632 reply->ctype = NULL;
1633 }
1634 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1635 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1636 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1637 px->conf.args.file, px->conf.args.line);
1638 }
1639 }
1640 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1641 LIST_INIT(&reply->body.fmt);
1642 if (!reply->ctype) {
1643 memprintf(errmsg, "a content type must be defined with a log-format payload");
1644 goto error;
1645 }
1646 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1647 goto error;
1648
1649 free(px->conf.lfs_file);
1650 px->conf.lfs_file = strdup(px->conf.args.file);
1651 px->conf.lfs_line = px->conf.args.line;
1652 }
1653
1654 free(obj);
1655 *orig_arg = cur_arg;
1656 return reply;
1657
1658 error:
1659 free(obj);
1660 if (fd >= 0)
1661 close(fd);
1662 release_http_reply(reply);
1663 return NULL;
1664}
1665
Christopher Faulet07f41f72020-01-16 16:16:06 +01001666/* Parses the "errorloc[302|303]" proxy keyword */
1667static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1668 struct proxy *defpx, const char *file, int line,
1669 char **errmsg)
1670{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001671 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001672 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001673 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001674 int errloc, status;
1675 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001676
1677 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1678 ret = 1;
1679 goto out;
1680 }
1681
1682 if (*(args[1]) == 0 || *(args[2]) == 0) {
1683 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1684 ret = -1;
1685 goto out;
1686 }
1687
1688 status = atol(args[1]);
1689 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1690 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1691 if (!msg) {
1692 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1693 ret = -1;
1694 goto out;
1695 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001696
1697 reply = calloc(1, sizeof(*reply));
1698 if (!reply) {
1699 memprintf(errmsg, "%s : out of memory.", args[0]);
1700 ret = -1;
1701 goto out;
1702 }
1703 reply->type = HTTP_REPLY_ERRMSG;
1704 reply->status = status;
1705 reply->ctype = NULL;
1706 LIST_INIT(&reply->hdrs);
1707 reply->body.errmsg = msg;
1708 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001709
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001710 conf_err = calloc(1, sizeof(*conf_err));
1711 if (!conf_err) {
1712 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001713 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001714 ret = -1;
1715 goto out;
1716 }
1717 conf_err->type = 1;
1718 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001719 conf_err->info.errorfile.reply = reply;
1720
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001721 conf_err->file = strdup(file);
1722 conf_err->line = line;
1723 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001724
1725 out:
1726 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001727
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001728}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001729
1730/* Parses the "errorfile" proxy keyword */
1731static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1732 struct proxy *defpx, const char *file, int line,
1733 char **errmsg)
1734{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001735 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001736 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001737 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001738 int status;
1739 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001740
1741 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1742 ret = 1;
1743 goto out;
1744 }
1745
1746 if (*(args[1]) == 0 || *(args[2]) == 0) {
1747 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1748 ret = -1;
1749 goto out;
1750 }
1751
1752 status = atol(args[1]);
1753 msg = http_parse_errorfile(status, args[2], errmsg);
1754 if (!msg) {
1755 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1756 ret = -1;
1757 goto out;
1758 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001759
Christopher Faulet5809e102020-05-14 17:31:52 +02001760 reply = calloc(1, sizeof(*reply));
1761 if (!reply) {
1762 memprintf(errmsg, "%s : out of memory.", args[0]);
1763 ret = -1;
1764 goto out;
1765 }
1766 reply->type = HTTP_REPLY_ERRMSG;
1767 reply->status = status;
1768 reply->ctype = NULL;
1769 LIST_INIT(&reply->hdrs);
1770 reply->body.errmsg = msg;
1771 LIST_ADDQ(&http_replies_list, &reply->list);
1772
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001773 conf_err = calloc(1, sizeof(*conf_err));
1774 if (!conf_err) {
1775 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001776 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001777 ret = -1;
1778 goto out;
1779 }
1780 conf_err->type = 1;
1781 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001782 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001783 conf_err->file = strdup(file);
1784 conf_err->line = line;
1785 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1786
1787 out:
1788 return ret;
1789
1790}
1791
1792/* Parses the "errorfiles" proxy keyword */
1793static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1794 struct proxy *defpx, const char *file, int line,
1795 char **err)
1796{
1797 struct conf_errors *conf_err = NULL;
1798 char *name = NULL;
1799 int rc, ret = 0;
1800
1801 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1802 ret = 1;
1803 goto out;
1804 }
1805
1806 if (!*(args[1])) {
1807 memprintf(err, "%s : expects <name> as argument.", args[0]);
1808 ret = -1;
1809 goto out;
1810 }
1811
1812 name = strdup(args[1]);
1813 conf_err = calloc(1, sizeof(*conf_err));
1814 if (!name || !conf_err) {
1815 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001816 goto error;
1817 }
1818 conf_err->type = 0;
1819
1820 conf_err->info.errorfiles.name = name;
1821 if (!*(args[2])) {
1822 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1823 conf_err->info.errorfiles.status[rc] = 1;
1824 }
1825 else {
1826 int cur_arg, status;
1827 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1828 status = atol(args[cur_arg]);
1829
1830 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1831 if (http_err_codes[rc] == status) {
1832 conf_err->info.errorfiles.status[rc] = 2;
1833 break;
1834 }
1835 }
1836 if (rc >= HTTP_ERR_SIZE) {
1837 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001838 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001839 }
1840 }
1841 }
1842 conf_err->file = strdup(file);
1843 conf_err->line = line;
1844 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1845 out:
1846 return ret;
1847
1848 error:
1849 free(name);
1850 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001851 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001852 goto out;
1853}
1854
Christopher Faulet3b967c12020-05-15 15:47:44 +02001855/* Parses the "http-error" proxy keyword */
1856static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
1857 struct proxy *defpx, const char *file, int line,
1858 char **errmsg)
1859{
1860 struct conf_errors *conf_err;
1861 struct http_reply *reply = NULL;
1862 int rc, cur_arg, ret = 0;
1863
1864 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1865 ret = 1;
1866 goto out;
1867 }
1868
1869 cur_arg = 1;
1870 curpx->conf.args.ctx = ARGC_HERR;
1871 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1872 if (!reply) {
1873 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1874 goto error;
1875 }
1876 else if (!reply->status) {
1877 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1878 goto error;
1879 }
1880
1881 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1882 if (http_err_codes[rc] == reply->status)
1883 break;
1884 }
1885
1886 if (rc >= HTTP_ERR_SIZE) {
1887 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1888 goto error;
1889 }
1890 if (*args[cur_arg]) {
1891 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1892 goto error;
1893 }
1894
1895 conf_err = calloc(1, sizeof(*conf_err));
1896 if (!conf_err) {
1897 memprintf(errmsg, "%s : out of memory.", args[0]);
1898 goto error;
1899 }
1900 if (reply->type == HTTP_REPLY_ERRFILES) {
1901 int rc = http_get_status_idx(reply->status);
1902
1903 conf_err->type = 2;
1904 conf_err->info.errorfiles.name = reply->body.http_errors;
1905 conf_err->info.errorfiles.status[rc] = 2;
1906 reply->body.http_errors = NULL;
1907 release_http_reply(reply);
1908 }
1909 else {
1910 conf_err->type = 1;
1911 conf_err->info.errorfile.status = reply->status;
1912 conf_err->info.errorfile.reply = reply;
1913 LIST_ADDQ(&http_replies_list, &reply->list);
1914 }
1915 conf_err->file = strdup(file);
1916 conf_err->line = line;
1917 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1918
1919 out:
1920 return ret;
1921
1922 error:
1923 release_http_reply(reply);
1924 ret = -1;
1925 goto out;
1926
1927}
1928
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001929/* Check "errorfiles" proxy keyword */
1930static int proxy_check_errors(struct proxy *px)
1931{
1932 struct conf_errors *conf_err, *conf_err_back;
1933 struct http_errors *http_errs;
1934 int rc, err = 0;
1935
1936 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1937 if (conf_err->type == 1) {
1938 /* errorfile */
1939 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02001940 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001941
1942 /* For proxy, to rely on default replies, just don't reference a reply */
1943 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
1944 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001945 }
1946 else {
1947 /* errorfiles */
1948 list_for_each_entry(http_errs, &http_errors_list, list) {
1949 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1950 break;
1951 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001952
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001953 /* unknown http-errors section */
1954 if (&http_errs->list == &http_errors_list) {
1955 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1956 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1957 err |= ERR_ALERT | ERR_FATAL;
1958 free(conf_err->info.errorfiles.name);
1959 goto next;
1960 }
1961
1962 free(conf_err->info.errorfiles.name);
1963 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1964 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02001965 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02001966 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001967 else if (conf_err->info.errorfiles.status[rc] == 2)
1968 ha_warning("config: proxy '%s' : status '%d' not declared in"
1969 " http-errors section '%s' (at %s:%d).\n",
1970 px->id, http_err_codes[rc], http_errs->id,
1971 conf_err->file, conf_err->line);
1972 }
1973 }
1974 }
1975 next:
1976 LIST_DEL(&conf_err->list);
1977 free(conf_err->file);
1978 free(conf_err);
1979 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001980
1981 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001982 return err;
1983}
1984
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001985static int post_check_errors()
1986{
1987 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001988 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001989 struct htx *htx;
1990 int err_code = 0;
1991
1992 node = ebpt_first(&http_error_messages);
1993 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001994 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1995 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001996 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001997 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001998 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1999 ha_warning("config: errorfile '%s' runs over the buffer space"
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002000 " reserved to headers rewriting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002001 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002002 (char *)node->key);
2003 err_code |= ERR_WARN;
2004 }
2005 next:
2006 node = ebpt_next(node);
2007 }
2008
2009 return err_code;
2010}
2011
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002012int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
2013{
2014 struct conf_errors *conf_err, *new_conf_err = NULL;
2015 int ret = 0;
2016
2017 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2018 new_conf_err = calloc(1, sizeof(*new_conf_err));
2019 if (!new_conf_err) {
2020 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2021 goto out;
2022 }
2023 new_conf_err->type = conf_err->type;
2024 if (conf_err->type == 1) {
2025 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002026 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002027 }
2028 else {
2029 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2030 if (!new_conf_err->info.errorfiles.name) {
2031 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2032 goto out;
2033 }
2034 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2035 sizeof(conf_err->info.errorfiles.status));
2036 }
2037 new_conf_err->file = strdup(conf_err->file);
2038 new_conf_err->line = conf_err->line;
2039 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
2040 new_conf_err = NULL;
2041 }
2042 ret = 1;
2043
2044 out:
2045 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002046 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002047}
2048
2049void proxy_release_conf_errors(struct proxy *px)
2050{
2051 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002052
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002053 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2054 if (conf_err->type == 0)
2055 free(conf_err->info.errorfiles.name);
2056 LIST_DEL(&conf_err->list);
2057 free(conf_err->file);
2058 free(conf_err);
2059 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002060}
2061
2062/*
2063 * Parse an <http-errors> section.
2064 * Returns the error code, 0 if OK, or any combination of :
2065 * - ERR_ABORT: must abort ASAP
2066 * - ERR_FATAL: we can continue parsing but not start the service
2067 * - ERR_WARN: a warning has been emitted
2068 * - ERR_ALERT: an alert has been emitted
2069 * Only the two first ones can stop processing, the two others are just
2070 * indicators.
2071 */
2072static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2073{
2074 static struct http_errors *curr_errs = NULL;
2075 int err_code = 0;
2076 const char *err;
2077 char *errmsg = NULL;
2078
2079 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2080 if (!*args[1]) {
2081 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2082 err_code |= ERR_ALERT | ERR_ABORT;
2083 goto out;
2084 }
2085
2086 err = invalid_char(args[1]);
2087 if (err) {
2088 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2089 file, linenum, *err, args[0], args[1]);
2090 err_code |= ERR_ALERT | ERR_FATAL;
2091 }
2092
2093 list_for_each_entry(curr_errs, &http_errors_list, list) {
2094 /* Error if two errors section owns the same name */
2095 if (strcmp(curr_errs->id, args[1]) == 0) {
2096 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2097 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2098 err_code |= ERR_ALERT | ERR_FATAL;
2099 }
2100 }
2101
2102 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2103 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2104 err_code |= ERR_ALERT | ERR_ABORT;
2105 goto out;
2106 }
2107
2108 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2109 curr_errs->id = strdup(args[1]);
2110 curr_errs->conf.file = strdup(file);
2111 curr_errs->conf.line = linenum;
2112 }
2113 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002114 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002115 struct buffer *msg;
2116 int status, rc;
2117
2118 if (*(args[1]) == 0 || *(args[2]) == 0) {
2119 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2120 file, linenum, args[0]);
2121 err_code |= ERR_ALERT | ERR_FATAL;
2122 goto out;
2123 }
2124
2125 status = atol(args[1]);
2126 msg = http_parse_errorfile(status, args[2], &errmsg);
2127 if (!msg) {
2128 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2129 err_code |= ERR_ALERT | ERR_FATAL;
2130 goto out;
2131 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002132
2133 reply = calloc(1, sizeof(*reply));
2134 if (!reply) {
2135 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2136 err_code |= ERR_ALERT | ERR_FATAL;
2137 goto out;
2138 }
2139 reply->type = HTTP_REPLY_ERRMSG;
2140 reply->status = status;
2141 reply->ctype = NULL;
2142 LIST_INIT(&reply->hdrs);
2143 reply->body.errmsg = msg;
2144
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002145 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002146 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002147 }
2148 else if (*args[0] != 0) {
2149 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
2152 }
2153
2154out:
2155 free(errmsg);
2156 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002157}
2158
2159static struct cfg_kw_list cfg_kws = {ILH, {
2160 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2161 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2162 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2163 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002164 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002165 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002166 { 0, NULL, NULL },
2167}};
2168
2169INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002170REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002171REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002172
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002173REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2174
Christopher Faulet29f72842019-12-11 15:52:32 +01002175/************************************************************************/
2176/* HTX sample fetches */
2177/************************************************************************/
2178
2179/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2180static int
2181smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2182{
2183 if (!smp->strm)
2184 return 0;
2185
2186 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2187 smp->data.type = SMP_T_BOOL;
2188 return 1;
2189}
2190
2191/* Returns the number of blocks in an HTX message. The channel is chosen
2192 * depending on the sample direction. */
2193static int
2194smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2195{
2196 struct channel *chn;
2197 struct htx *htx;
2198
2199 if (!smp->strm)
2200 return 0;
2201
2202 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002203 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002204 if (!htx)
2205 return 0;
2206
2207 smp->data.u.sint = htx_nbblks(htx);
2208 smp->data.type = SMP_T_SINT;
2209 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2210 return 1;
2211}
2212
2213/* Returns the size of an HTX message. The channel is chosen depending on the
2214 * sample direction. */
2215static int
2216smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2217{
2218 struct channel *chn;
2219 struct htx *htx;
2220
2221 if (!smp->strm)
2222 return 0;
2223
2224 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002225 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002226 if (!htx)
2227 return 0;
2228
2229 smp->data.u.sint = htx->size;
2230 smp->data.type = SMP_T_SINT;
2231 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2232 return 1;
2233}
2234
2235/* Returns the data size of an HTX message. The channel is chosen depending on the
2236 * sample direction. */
2237static int
2238smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2239{
2240 struct channel *chn;
2241 struct htx *htx;
2242
2243 if (!smp->strm)
2244 return 0;
2245
2246 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002247 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002248 if (!htx)
2249 return 0;
2250
2251 smp->data.u.sint = htx->data;
2252 smp->data.type = SMP_T_SINT;
2253 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2254 return 1;
2255}
2256
2257/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2258 * depending on the sample direction. */
2259static int
2260smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2261{
2262 struct channel *chn;
2263 struct htx *htx;
2264
2265 if (!smp->strm)
2266 return 0;
2267
2268 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002269 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002270 if (!htx)
2271 return 0;
2272
2273 smp->data.u.sint = htx_used_space(htx);
2274 smp->data.type = SMP_T_SINT;
2275 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2276 return 1;
2277}
2278
2279/* Returns the free space (size-used) of an HTX message. The channel is chosen
2280 * depending on the sample direction. */
2281static int
2282smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2283{
2284 struct channel *chn;
2285 struct htx *htx;
2286
2287 if (!smp->strm)
2288 return 0;
2289
2290 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002291 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002292 if (!htx)
2293 return 0;
2294
2295 smp->data.u.sint = htx_free_space(htx);
2296 smp->data.type = SMP_T_SINT;
2297 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2298 return 1;
2299}
2300
2301/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2302 * channel is chosen depending on the sample direction. */
2303static int
2304smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2305{
2306 struct channel *chn;
2307 struct htx *htx;
2308
2309 if (!smp->strm)
2310 return 0;
2311
2312 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002313 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002314 if (!htx)
2315 return 0;
2316
2317 smp->data.u.sint = htx_free_data_space(htx);
2318 smp->data.type = SMP_T_SINT;
2319 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2320 return 1;
2321}
2322
2323/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2324 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2325 * the sample direction. */
2326static int
2327smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2328{
2329 struct channel *chn;
2330 struct htx *htx;
2331
2332 if (!smp->strm)
2333 return 0;
2334
2335 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002336 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002337 if (!htx)
2338 return 0;
2339
2340 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2341 smp->data.type = SMP_T_BOOL;
2342 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2343 return 1;
2344}
2345
2346/* Returns the type of a specific HTX block, if found in the message. Otherwise
2347 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2348 * "head", "tail" or "first". The channel is chosen depending on the sample
2349 * direction. */
2350static int
2351smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2352{
2353 struct channel *chn;
2354 struct htx *htx;
2355 enum htx_blk_type type;
2356 int32_t pos;
2357
2358 if (!smp->strm || !arg_p)
2359 return 0;
2360
2361 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002362 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002363 if (!htx)
2364 return 0;
2365
2366 pos = arg_p[0].data.sint;
2367 if (pos == -1)
2368 type = htx_get_head_type(htx);
2369 else if (pos == -2)
2370 type = htx_get_tail_type(htx);
2371 else if (pos == -3)
2372 type = htx_get_first_type(htx);
2373 else
2374 type = ((pos >= htx->head && pos <= htx->tail)
2375 ? htx_get_blk_type(htx_get_blk(htx, pos))
2376 : HTX_BLK_UNUSED);
2377
2378 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2379 smp->data.type = SMP_T_STR;
2380 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2381 return 1;
2382}
2383
2384/* Returns the size of a specific HTX block, if found in the message. Otherwise
2385 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2386 * "first". The channel is chosen depending on the sample direction. */
2387static int
2388smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2389{
2390 struct channel *chn;
2391 struct htx *htx;
2392 struct htx_blk *blk;
2393 int32_t pos;
2394
2395 if (!smp->strm || !arg_p)
2396 return 0;
2397
2398 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002399 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002400 if (!htx)
2401 return 0;
2402
2403 pos = arg_p[0].data.sint;
2404 if (pos == -1)
2405 blk = htx_get_head_blk(htx);
2406 else if (pos == -2)
2407 blk = htx_get_tail_blk(htx);
2408 else if (pos == -3)
2409 blk = htx_get_first_blk(htx);
2410 else
2411 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2412
2413 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2414 smp->data.type = SMP_T_SINT;
2415 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2416 return 1;
2417}
2418
2419/* Returns the start-line if the selected HTX block exists and is a
2420 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2421 * supported or "head", "tail" or "first". The channel is chosen depending on
2422 * the sample direction. */
2423static int
2424smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2425{
2426 struct buffer *temp;
2427 struct channel *chn;
2428 struct htx *htx;
2429 struct htx_blk *blk;
2430 struct htx_sl *sl;
2431 int32_t pos;
2432
2433 if (!smp->strm || !arg_p)
2434 return 0;
2435
2436 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002437 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002438 if (!htx)
2439 return 0;
2440
2441 pos = arg_p[0].data.sint;
2442 if (pos == -1)
2443 blk = htx_get_head_blk(htx);
2444 else if (pos == -2)
2445 blk = htx_get_tail_blk(htx);
2446 else if (pos == -3)
2447 blk = htx_get_first_blk(htx);
2448 else
2449 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2450
2451 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2452 smp->data.u.str.size = 0;
2453 smp->data.u.str.area = "";
2454 smp->data.u.str.data = 0;
2455 }
2456 else {
2457 sl = htx_get_blk_ptr(htx, blk);
2458
2459 temp = get_trash_chunk();
2460 chunk_istcat(temp, htx_sl_p1(sl));
2461 temp->area[temp->data++] = ' ';
2462 chunk_istcat(temp, htx_sl_p2(sl));
2463 temp->area[temp->data++] = ' ';
2464 chunk_istcat(temp, htx_sl_p3(sl));
2465
2466 smp->data.u.str = *temp;
2467 }
2468
2469 smp->data.type = SMP_T_STR;
2470 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2471 return 1;
2472}
2473
2474/* Returns the header name if the selected HTX block exists and is a header or a
2475 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2476 * supported or "head", "tail" or "first". The channel is chosen depending on
2477 * the sample direction. */
2478static int
2479smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2480{
2481 struct channel *chn;
2482 struct htx *htx;
2483 struct htx_blk *blk;
2484 int32_t pos;
2485
2486 if (!smp->strm || !arg_p)
2487 return 0;
2488
2489 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002490 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002491 if (!htx)
2492 return 0;
2493
2494 pos = arg_p[0].data.sint;
2495 if (pos == -1)
2496 blk = htx_get_head_blk(htx);
2497 else if (pos == -2)
2498 blk = htx_get_tail_blk(htx);
2499 else if (pos == -3)
2500 blk = htx_get_first_blk(htx);
2501 else
2502 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2503
2504 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2505 smp->data.u.str.size = 0;
2506 smp->data.u.str.area = "";
2507 smp->data.u.str.data = 0;
2508 }
2509 else {
2510 struct ist name = htx_get_blk_name(htx, blk);
2511
2512 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2513 }
2514 smp->data.type = SMP_T_STR;
2515 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2516 return 1;
2517}
2518
2519/* Returns the header value if the selected HTX block exists and is a header or
2520 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2521 * supported or "head", "tail" or "first". The channel is chosen depending on
2522 * the sample direction. */
2523static int
2524smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2525{
2526 struct channel *chn;
2527 struct htx *htx;
2528 struct htx_blk *blk;
2529 int32_t pos;
2530
2531 if (!smp->strm || !arg_p)
2532 return 0;
2533
2534 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002535 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002536 if (!htx)
2537 return 0;
2538
2539 pos = arg_p[0].data.sint;
2540 if (pos == -1)
2541 blk = htx_get_head_blk(htx);
2542 else if (pos == -2)
2543 blk = htx_get_tail_blk(htx);
2544 else if (pos == -3)
2545 blk = htx_get_first_blk(htx);
2546 else
2547 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2548
2549 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2550 smp->data.u.str.size = 0;
2551 smp->data.u.str.area = "";
2552 smp->data.u.str.data = 0;
2553 }
2554 else {
2555 struct ist val = htx_get_blk_value(htx, blk);
2556
2557 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2558 }
2559 smp->data.type = SMP_T_STR;
2560 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2561 return 1;
2562}
2563
2564/* Returns the value if the selected HTX block exists and is a data
2565 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2566 * or "head", "tail" or "first". The channel is chosen depending on the sample
2567 * direction. */
2568static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002569smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002570{
2571 struct channel *chn;
2572 struct htx *htx;
2573 struct htx_blk *blk;
2574 int32_t pos;
2575
2576 if (!smp->strm || !arg_p)
2577 return 0;
2578
2579 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002580 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002581 if (!htx)
2582 return 0;
2583
2584 pos = arg_p[0].data.sint;
2585 if (pos == -1)
2586 blk = htx_get_head_blk(htx);
2587 else if (pos == -2)
2588 blk = htx_get_tail_blk(htx);
2589 else if (pos == -3)
2590 blk = htx_get_first_blk(htx);
2591 else
2592 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2593
2594 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2595 smp->data.u.str.size = 0;
2596 smp->data.u.str.area = "";
2597 smp->data.u.str.data = 0;
2598 }
2599 else {
2600 struct ist val = htx_get_blk_value(htx, blk);
2601
2602 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2603 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002604 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002605 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2606 return 1;
2607}
2608
2609/* This function is used to validate the arguments passed to any "htx_blk" fetch
2610 * keywords. An argument is expected by these keywords. It must be a positive
2611 * integer or on of the following strings: "head", "tail" or "first". It returns
2612 * 0 on error, and a non-zero value if OK.
2613 */
2614int val_blk_arg(struct arg *arg, char **err_msg)
2615{
2616 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2617 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2618 return 0;
2619 }
2620 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002621 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002622 arg[0].type = ARGT_SINT;
2623 arg[0].data.sint = -1;
2624 }
2625 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002626 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002627 arg[0].type = ARGT_SINT;
2628 arg[0].data.sint = -2;
2629 }
2630 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002631 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002632 arg[0].type = ARGT_SINT;
2633 arg[0].data.sint = -3;
2634 }
2635 else {
2636 int pos;
2637
2638 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002639 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002640 memprintf(err_msg, "invalid block position");
2641 return 0;
2642 }
2643 }
2644
2645 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2646 if (pos < 0) {
2647 memprintf(err_msg, "block position must not be negative");
2648 return 0;
2649 }
Christopher Faulet6ad7df42020-08-07 11:45:18 +02002650 chunk_destroy(&arg[0].data.str);
Christopher Faulet29f72842019-12-11 15:52:32 +01002651 arg[0].type = ARGT_SINT;
2652 arg[0].data.sint = pos;
2653 }
2654
2655 return 1;
2656}
2657
2658
2659/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002660 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002661 */
2662static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002663 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002664
Christopher Faulet01f44452020-01-08 14:23:40 +01002665 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2666 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2667 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2668 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2669 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2670 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2671 { "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 +01002672
Christopher Faulet01f44452020-01-08 14:23:40 +01002673 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2674 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2675 { "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},
2676 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2677 { "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 +01002678 { "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 +01002679
2680 { /* END */ },
2681}};
2682
2683INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);