blob: aabf1a598452e89c97127a6091c74509bb3a8168 [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Christopher Faulet5031ef52020-01-15 11:22:07 +010012#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <unistd.h>
16
17#include <types/global.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020018
19#include <common/config.h>
Christopher Faulet29f17582019-05-23 11:03:26 +020020#include <common/debug.h>
Christopher Fauleta7b677c2018-11-29 16:48:49 +010021#include <common/cfgparse.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010022#include <common/h1.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020023#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010024#include <common/htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020025
Christopher Faulet29f72842019-12-11 15:52:32 +010026#include <proto/arg.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020027#include <proto/http_htx.h>
Christopher Faulet29f72842019-12-11 15:52:32 +010028#include <proto/http_fetch.h>
29#include <proto/sample.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020030
Christopher Fauletf7346382019-07-17 22:02:08 +020031struct buffer http_err_chunks[HTTP_ERR_SIZE];
Christopher Faulet1b13eca2020-05-14 09:54:26 +020032struct http_reply http_err_replies[HTTP_ERR_SIZE];
33
Christopher Faulet58857752020-01-15 15:19:50 +010034struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010035struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Faulet5809e102020-05-14 17:31:52 +020036struct list http_replies_list = LIST_HEAD_INIT(http_replies_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010037
Christopher Faulet76edc0f2020-01-13 15:52:01 +010038/* The declaration of an errorfiles/errorfile directives. Used during config
39 * parsing only. */
40struct conf_errors {
41 char type; /* directive type (0: errorfiles, 1: errorfile) */
42 union {
43 struct {
44 int status; /* the status code associated to this error */
Christopher Faulet5809e102020-05-14 17:31:52 +020045 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010046 } errorfile; /* describe an "errorfile" directive */
47 struct {
48 char *name; /* the http-errors section name */
49 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
50 } errorfiles; /* describe an "errorfiles" directive */
51 } info;
52
53 char *file; /* file where the directive appears */
54 int line; /* line where the directive appears */
55
56 struct list list; /* next conf_errors */
57};
58
Christopher Faulet297fbb42019-05-13 14:41:27 +020059/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020060 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020061 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020062 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020063struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020064{
Christopher Faulet297fbb42019-05-13 14:41:27 +020065 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 BUG_ON(htx->first == -1);
68 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020069 if (!blk)
70 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020071 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +020072 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet8dd33e12020-05-05 07:42:42 +020093/* Finds the first or next occurrence of header matching <pattern> in the HTX
94 * message <htx> using the context <ctx>. This structure holds everything
95 * necessary to use the header and find next occurrence. If its <blk> member is
96 * NULL, the header is searched from the beginning. Otherwise, the next
97 * occurrence is returned. The function returns 1 when it finds a value, and 0
98 * when there is no more. It is designed to work with headers defined as
99 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
100 * full-line headers in whose comma is not a delimiter but is part of the
101 * syntax. A special case, if ctx->value is NULL when searching for a new values
102 * of a header, the current header is rescanned. This allows rescanning after a
103 * header deletion.
104 *
105 * The matching method is chosen by checking the flags :
106 *
107 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
108 * the regex are evaluated.
109 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
110 * to the string are evaluated.
111 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
112 * starting by the string are evaluated.
113 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
114 * ending by the string are evaluated.
115 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
116 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200117 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200118
119#define HTTP_FIND_FL_MATCH_STR 0x0001
120#define HTTP_FIND_FL_MATCH_PFX 0x0002
121#define HTTP_FIND_FL_MATCH_SFX 0x0003
122#define HTTP_FIND_FL_MATCH_SUB 0x0004
123#define HTTP_FIND_FL_MATCH_REG 0x0005
124/* 0x0006..0x000f: for other matching methods */
125#define HTTP_FIND_FL_MATCH_TYPE 0x000F
126#define HTTP_FIND_FL_FULL 0x0010
127
128static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags)
Christopher Faulet47596d32018-10-22 09:17:28 +0200129{
130 struct htx_blk *blk = ctx->blk;
131 struct ist n, v;
132 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200133
134 if (blk) {
135 char *p;
136
Tim Duesterhused526372020-03-05 17:56:33 +0100137 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200139 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 goto next_blk;
141 v = htx_get_blk_value(htx, blk);
142 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
143 v.len -= (p - v.ptr);
144 v.ptr = p;
145 if (!v.len)
146 goto next_blk;
147 /* Skip comma */
148 if (*(v.ptr) == ',') {
149 v.ptr++;
150 v.len--;
151 }
152
153 goto return_hdr;
154 }
155
Christopher Faulet192c6a22019-06-11 16:32:24 +0200156 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200157 return 0;
158
Christopher Fauleta3f15502019-05-13 15:27:23 +0200159 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100162 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
163 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200164 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200165 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200166
167 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
168 const struct my_regex *re = pattern;
169
170 n = htx_get_blk_name(htx, blk);
171 if (!regex_exec2(re, n.ptr, n.len))
172 goto next_blk;
173 }
174 else {
175 const struct ist name = *(const struct ist *)(pattern);
176
Christopher Faulet47596d32018-10-22 09:17:28 +0200177 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200178 if (!istlen(name))
179 goto match;
180
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200182 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
183 case HTTP_FIND_FL_MATCH_STR:
184 if (!isteqi(n, name))
185 goto next_blk;
186 break;
187 case HTTP_FIND_FL_MATCH_PFX:
188 if (istlen(n) < istlen(name))
189 goto next_blk;
190
191 n = ist2(istptr(n), istlen(name));
192 if (!isteqi(n, name))
193 goto next_blk;
194 break;
195 case HTTP_FIND_FL_MATCH_SFX:
196 if (istlen(n) < istlen(name))
197 goto next_blk;
198
199 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
200 if (!isteqi(n, name))
201 goto next_blk;
202 break;
203 case HTTP_FIND_FL_MATCH_SUB:
204 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
205 goto next_blk;
206 break;
207 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200208 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200209 break;
210 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200211 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200212 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200213 v = htx_get_blk_value(htx, blk);
214
215 return_hdr:
216 ctx->lws_before = 0;
217 ctx->lws_after = 0;
218 while (v.len && HTTP_IS_LWS(*v.ptr)) {
219 v.ptr++;
220 v.len--;
221 ctx->lws_before++;
222 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200223 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200224 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
225 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
226 v.len--;
227 ctx->lws_after++;
228 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200229 ctx->blk = blk;
230 ctx->value = v;
231 return 1;
232
233 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200234 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200235 }
236
237 ctx->blk = NULL;
238 ctx->value = ist("");
239 ctx->lws_before = ctx->lws_after = 0;
240 return 0;
241}
242
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200243
244/* Header names must match <name> */
245int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
246{
247 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
248}
249
250/* Header names must match <name>. Same than http_find_header */
251int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
252{
253 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
254}
255
256
257/* Header names must start with <prefix> */
258int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
259{
260 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
261}
262
263/* Header names must end with <suffix> */
264int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
265{
266 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
267}
268/* Header names must contain <sub> */
269int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
270{
271 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
272}
273
274/* Header names must match <re> regex*/
275int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
276{
277 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
278}
279
280
Christopher Faulet47596d32018-10-22 09:17:28 +0200281/* Adds a header block int the HTX message <htx>, just before the EOH block. It
282 * returns 1 on success, otherwise it returns 0.
283 */
284int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
285{
286 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200287 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200288 enum htx_blk_type type = htx_get_tail_type(htx);
289 int32_t prev;
290
291 blk = htx_add_header(htx, n, v);
292 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200293 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200294
295 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200296 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200297
298 /* <blk> is the head, swap it iteratively with its predecessor to place
299 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200300 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200301 struct htx_blk *pblk = htx_get_blk(htx, prev);
302 enum htx_blk_type type = htx_get_blk_type(pblk);
303
304 /* Swap .addr and .info fields */
305 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
306 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
307
308 if (blk->addr == pblk->addr)
309 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200310
311 /* Stop when end-of-header is reached */
312 if (type == HTX_BLK_EOH)
313 break;
314
315 blk = pblk;
316 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200317
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200318 end:
319 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100320 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200321 if (!http_update_authority(htx, sl, v))
322 goto fail;
323 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200324 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200325
326 fail:
327 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200328}
329
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200331 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200332 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100333int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200334{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200335 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200336
Christopher Faulet29f17582019-05-23 11:03:26 +0200337 blk = htx_get_first_blk(htx);
338 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200339 return 0;
340 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200341}
342
Christopher Faulete010c802018-10-24 10:36:45 +0200343/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
344 * on success, otherwise 0.
345 */
346int http_replace_req_meth(struct htx *htx, const struct ist meth)
347{
348 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200349 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200351
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100352 if (!sl)
353 return 0;
354
Christopher Faulete010c802018-10-24 10:36:45 +0200355 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
357 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
360 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200361
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 /* create the new start line */
363 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
364 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200365}
366
367/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
368 * success, otherwise 0.
369 */
370int http_replace_req_uri(struct htx *htx, const struct ist uri)
371{
372 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200373 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200375
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100376 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200377 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100378
Christopher Faulete010c802018-10-24 10:36:45 +0200379 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
381 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
384 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200387 if (!http_replace_stline(htx, meth, uri, vsn))
388 goto fail;
389
390 sl = http_get_stline(htx);
391 if (!http_update_host(htx, sl, uri))
392 goto fail;
393
394 return 1;
395 fail:
396 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200397}
398
399/* Replace the request path in the HTX message <htx> by <path>. The host part
400 * and the query string are preserved. It returns 1 on success, otherwise 0.
401 */
402int http_replace_req_path(struct htx *htx, const struct ist path)
403{
404 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200405 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100406 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200407 size_t plen = 0;
408
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100409 if (!sl)
410 return 0;
411
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100412 uri = htx_sl_req_uri(sl);
413 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100414 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100415 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200416 while (plen < p.len && *(p.ptr + plen) != '?')
417 plen++;
418
419 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100420 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
421 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200422
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100423 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
424 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
425
426 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200427 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
428 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100429 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200430
431 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100432 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200433}
434
435/* Replace the request query-string in the HTX message <htx> by <query>. The
436 * host part and the path are preserved. It returns 1 on success, otherwise
437 * 0.
438 */
439int http_replace_req_query(struct htx *htx, const struct ist query)
440{
441 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200442 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100443 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200444 int offset = 1;
445
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100446 if (!sl)
447 return 0;
448
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100449 uri = htx_sl_req_uri(sl);
450 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200451 while (q.len > 0 && *(q.ptr) != '?') {
452 q.ptr++;
453 q.len--;
454 }
455
456 /* skip the question mark or indicate that we must insert it
457 * (but only if the format string is not empty then).
458 */
459 if (q.len) {
460 q.ptr++;
461 q.len--;
462 }
463 else if (query.len > 1)
464 offset = 0;
465
466 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100467 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
468 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200469
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100470 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
471 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200472
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100473 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
474 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
475 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200476
477 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100478 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200479}
480
481/* Replace the response status in the HTX message <htx> by <status>. It returns
482 * 1 on success, otherwise 0.
483*/
484int http_replace_res_status(struct htx *htx, const struct ist status)
485{
486 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200487 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100488 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200489
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100490 if (!sl)
491 return 0;
492
Christopher Faulete010c802018-10-24 10:36:45 +0200493 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100494 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
495 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200496
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100497 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
498 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200499
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100500 /* create the new start line */
501 sl->info.res.status = strl2ui(status.ptr, status.len);
502 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200503}
504
505/* Replace the response reason in the HTX message <htx> by <reason>. It returns
506 * 1 on success, otherwise 0.
507*/
508int http_replace_res_reason(struct htx *htx, const struct ist reason)
509{
510 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200511 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100512 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200513
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100514 if (!sl)
515 return 0;
516
Christopher Faulete010c802018-10-24 10:36:45 +0200517 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
519 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200520
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100521 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
522 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200523
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100524 /* create the new start line */
525 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200526}
527
Christopher Faulet47596d32018-10-22 09:17:28 +0200528/* Replaces a part of a header value referenced in the context <ctx> by
529 * <data>. It returns 1 on success, otherwise it returns 0. The context is
530 * updated if necessary.
531 */
532int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
533{
534 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200535 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200536 char *start;
537 struct ist v;
538 uint32_t len, off;
539
540 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200541 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200542
543 v = htx_get_blk_value(htx, blk);
544 start = ctx->value.ptr - ctx->lws_before;
545 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
546 off = start - v.ptr;
547
548 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
549 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200550 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200551
552 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200553
554 sl = http_get_stline(htx);
555 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
556 struct ist n = htx_get_blk_name(htx, blk);
557
558 if (isteq(n, ist("host"))) {
559 if (!http_update_authority(htx, sl, v))
560 goto fail;
561 ctx->blk = NULL;
562 http_find_header(htx, ist("host"), ctx, 1);
563 blk = ctx->blk;
564 v = htx_get_blk_value(htx, blk);
565 }
566 }
567
Christopher Faulet47596d32018-10-22 09:17:28 +0200568 ctx->blk = blk;
569 ctx->value.ptr = v.ptr + off;
570 ctx->value.len = data.len;
571 ctx->lws_before = ctx->lws_after = 0;
572
573 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200574 fail:
575 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200576}
577
578/* Fully replaces a header referenced in the context <ctx> by the name <name>
579 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
580 * context is updated if necessary.
581 */
582int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
583 const struct ist name, const struct ist value)
584{
585 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200586 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200587
588 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200589 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200590
591 blk = htx_replace_header(htx, blk, name, value);
592 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200593 goto fail;
594
595 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100596 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200597 if (!http_update_authority(htx, sl, value))
598 goto fail;
599 ctx->blk = NULL;
600 http_find_header(htx, ist("host"), ctx, 1);
601 blk = ctx->blk;
602 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200603
604 ctx->blk = blk;
605 ctx->value = ist(NULL);
606 ctx->lws_before = ctx->lws_after = 0;
607
608 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200609 fail:
610 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200611}
612
613/* Remove one value of a header. This only works on a <ctx> returned by
614 * http_find_header function. The value is removed, as well as surrounding commas
615 * if any. If the removed value was alone, the whole header is removed. The
616 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
617 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
618 * form that can be handled by http_find_header() to find next occurrence.
619 */
620int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
621{
622 struct htx_blk *blk = ctx->blk;
623 char *start;
624 struct ist v;
625 uint32_t len;
626
627 if (!blk)
628 return 0;
629
630 start = ctx->value.ptr - ctx->lws_before;
631 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
632
633 v = htx_get_blk_value(htx, blk);
634 if (len == v.len) {
635 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200636 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200637 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100638 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200639 ctx->lws_before = ctx->lws_after = 0;
640 }
641 else {
642 ctx->blk = htx_get_blk(htx, htx->tail);
643 ctx->value = htx_get_blk_value(htx, ctx->blk);
644 ctx->lws_before = ctx->lws_after = 0;
645 }
646 return 1;
647 }
648
649 /* This was not the only value of this header. We have to remove the
650 * part pointed by ctx->value. If it is the last entry of the list, we
651 * remove the last separator.
652 */
653 if (start == v.ptr) {
654 /* It's the first header part but not the only one. So remove
655 * the comma after it. */
656 len++;
657 }
658 else {
659 /* There is at least one header part before the removed one. So
660 * remove the comma between them. */
661 start--;
662 len++;
663 }
664 /* Update the block content and its len */
665 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200666 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200667
668 /* Finally update the ctx */
669 ctx->value.ptr = start;
670 ctx->value.len = 0;
671 ctx->lws_before = ctx->lws_after = 0;
672
673 return 1;
674}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200675
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200676/* Updates the authority part of the uri with the value <host>. It happens when
677 * the header host is modified. It returns 0 on failure and 1 on success. It is
678 * the caller responsibility to provide the start-line and to be sure the uri
679 * contains an authority. Thus, if no authority is found in the uri, an error is
680 * returned.
681 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200682int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200683{
684 struct buffer *temp = get_trash_chunk();
685 struct ist meth, vsn, uri, authority;
686
687 uri = htx_sl_req_uri(sl);
688 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100689 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200690 return 0;
691
Christopher Faulet34b18e42020-02-18 11:02:21 +0100692 /* Don't update the uri if there is no change */
693 if (isteq(host, authority))
694 return 1;
695
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200696 /* Start by copying old method and version */
697 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
698 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
699
700 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
701 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
702
703 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
704 chunk_memcat(temp, host.ptr, host.len);
705 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
706 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
707
708 return http_replace_stline(htx, meth, uri, vsn);
709
710}
711
712/* Update the header host by extracting the authority of the uri <uri>. flags of
713 * the start-line are also updated accordingly. For orgin-form and asterisk-form
714 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
715 * removed from the flags of the start-line. Otherwise, this flag is set and the
716 * authority is used to set the value of the header host. This function returns
717 * 0 on failure and 1 on success.
718*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200719int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200720{
721 struct ist authority;
722 struct http_hdr_ctx ctx;
723
724 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
725 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
726 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
727 }
728 else {
729 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
730 if (sl->info.req.meth != HTTP_METH_CONNECT) {
731 // absolute-form (RFC7320 #5.3.2)
732 sl->flags |= HTX_SL_F_HAS_SCHM;
733 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
734 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
735
736 authority = http_get_authority(uri, 1);
737 if (!authority.len)
738 goto fail;
739 }
740 else {
741 // authority-form (RFC7320 #5.3.3)
742 authority = uri;
743 }
744
745 /* Replace header host value */
746 ctx.blk = NULL;
747 while (http_find_header(htx, ist("host"), &ctx, 1)) {
748 if (!http_replace_header_value(htx, &ctx, authority))
749 goto fail;
750 }
751
752 }
753 return 1;
754 fail:
755 return 0;
756}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200757
758/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
759 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
760 * performed over the whole headers. Otherwise it must contain a valid header
761 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
762 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
763 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
764 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
765 * -1. The value fetch stops at commas, so this function is suited for use with
766 * list headers.
767 * The return value is 0 if nothing was found, or non-zero otherwise.
768 */
769unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
770 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
771{
772 struct http_hdr_ctx local_ctx;
773 struct ist val_hist[MAX_HDR_HISTORY];
774 unsigned int hist_idx;
775 int found;
776
777 if (!ctx) {
778 local_ctx.blk = NULL;
779 ctx = &local_ctx;
780 }
781
782 if (occ >= 0) {
783 /* search from the beginning */
784 while (http_find_header(htx, hdr, ctx, 0)) {
785 occ--;
786 if (occ <= 0) {
787 *vptr = ctx->value.ptr;
788 *vlen = ctx->value.len;
789 return 1;
790 }
791 }
792 return 0;
793 }
794
795 /* negative occurrence, we scan all the list then walk back */
796 if (-occ > MAX_HDR_HISTORY)
797 return 0;
798
799 found = hist_idx = 0;
800 while (http_find_header(htx, hdr, ctx, 0)) {
801 val_hist[hist_idx] = ctx->value;
802 if (++hist_idx >= MAX_HDR_HISTORY)
803 hist_idx = 0;
804 found++;
805 }
806 if (-occ > found)
807 return 0;
808
809 /* OK now we have the last occurrence in [hist_idx-1], and we need to
810 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
811 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
812 * to remain in the 0..9 range.
813 */
814 hist_idx += occ + MAX_HDR_HISTORY;
815 if (hist_idx >= MAX_HDR_HISTORY)
816 hist_idx -= MAX_HDR_HISTORY;
817 *vptr = val_hist[hist_idx].ptr;
818 *vlen = val_hist[hist_idx].len;
819 return 1;
820}
821
822/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
823 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
824 * performed over the whole headers. Otherwise it must contain a valid header
825 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
826 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
827 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
828 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
829 * -1. This function differs from http_get_hdr() in that it only returns full
830 * line header values and does not stop at commas.
831 * The return value is 0 if nothing was found, or non-zero otherwise.
832 */
833unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
834 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
835{
836 struct http_hdr_ctx local_ctx;
837 struct ist val_hist[MAX_HDR_HISTORY];
838 unsigned int hist_idx;
839 int found;
840
841 if (!ctx) {
842 local_ctx.blk = NULL;
843 ctx = &local_ctx;
844 }
845
846 if (occ >= 0) {
847 /* search from the beginning */
848 while (http_find_header(htx, hdr, ctx, 1)) {
849 occ--;
850 if (occ <= 0) {
851 *vptr = ctx->value.ptr;
852 *vlen = ctx->value.len;
853 return 1;
854 }
855 }
856 return 0;
857 }
858
859 /* negative occurrence, we scan all the list then walk back */
860 if (-occ > MAX_HDR_HISTORY)
861 return 0;
862
863 found = hist_idx = 0;
864 while (http_find_header(htx, hdr, ctx, 1)) {
865 val_hist[hist_idx] = ctx->value;
866 if (++hist_idx >= MAX_HDR_HISTORY)
867 hist_idx = 0;
868 found++;
869 }
870 if (-occ > found)
871 return 0;
872
873 /* OK now we have the last occurrence in [hist_idx-1], and we need to
874 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
875 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
876 * to remain in the 0..9 range.
877 */
878 hist_idx += occ + MAX_HDR_HISTORY;
879 if (hist_idx >= MAX_HDR_HISTORY)
880 hist_idx -= MAX_HDR_HISTORY;
881 *vptr = val_hist[hist_idx].ptr;
882 *vlen = val_hist[hist_idx].len;
883 return 1;
884}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100885
Christopher Faulet90cc4812019-07-22 16:49:30 +0200886int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100887{
888 struct htx *htx;
889 struct htx_sl *sl;
890 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200891 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100892 union h1_sl h1sl;
893 unsigned int flags = HTX_SL_F_IS_RESP;
894 int ret = 0;
895
Christopher Faulet90cc4812019-07-22 16:49:30 +0200896 b_reset(buf);
897 if (!raw.len) {
898 buf->size = 0;
899 buf->area = malloc(raw.len);
900 return 1;
901 }
902
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100903 buf->size = global.tune.bufsize;
904 buf->area = (char *)malloc(buf->size);
905 if (!buf->area)
906 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100907
908 h1m_init_res(&h1m);
909 h1m.flags |= H1_MF_NO_PHDR;
910 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
911 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
912 if (ret <= 0)
913 goto error;
914
915 if (unlikely(h1sl.st.v.len != 8))
916 goto error;
917 if ((*(h1sl.st.v.ptr + 5) > '1') ||
918 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
919 h1m.flags |= H1_MF_VER_11;
920
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200921 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
922 goto error;
923
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100924 if (h1m.flags & H1_MF_VER_11)
925 flags |= HTX_SL_F_VER_11;
926 if (h1m.flags & H1_MF_XFER_ENC)
927 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200928 if (h1m.flags & H1_MF_CLEN) {
929 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
930 if (h1m.body_len == 0)
931 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100932 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200933 if (h1m.flags & H1_MF_CHNK)
934 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100935
936 htx = htx_from_buf(buf);
937 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
938 if (!sl || !htx_add_all_headers(htx, hdrs))
939 goto error;
940 sl->info.res.status = h1sl.st.status;
941
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200942 while (raw.len > ret) {
943 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
944 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100945 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200946 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100947 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200948
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100949 if (!htx_add_endof(htx, HTX_BLK_EOM))
950 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200951
Christopher Faulet90cc4812019-07-22 16:49:30 +0200952 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100953
954error:
955 if (buf->size)
956 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200957 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100958}
959
Christopher Faulet18630642020-05-12 18:57:28 +0200960void release_http_reply(struct http_reply *http_reply)
961{
962 struct logformat_node *lf, *lfb;
963 struct http_reply_hdr *hdr, *hdrb;
964
965 if (!http_reply)
966 return;
967
968 free(http_reply->ctype);
969 http_reply->ctype = NULL;
970 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
971 LIST_DEL(&hdr->list);
972 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
973 LIST_DEL(&lf->list);
974 release_sample_expr(lf->expr);
975 free(lf->arg);
976 free(lf);
977 }
978 istfree(&hdr->name);
979 free(hdr);
980 }
981
982 if (http_reply->type == HTTP_REPLY_ERRFILES) {
983 free(http_reply->body.http_errors);
984 http_reply->body.http_errors = NULL;
985 }
986 else if (http_reply->type == HTTP_REPLY_RAW)
987 chunk_destroy(&http_reply->body.obj);
988 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
989 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
990 LIST_DEL(&lf->list);
991 release_sample_expr(lf->expr);
992 free(lf->arg);
993 free(lf);
994 }
995 }
996}
997
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100998static int http_htx_init(void)
999{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001000 struct buffer chk;
1001 struct ist raw;
1002 int rc;
1003 int err_code = 0;
1004
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001005 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1006 if (!http_err_msgs[rc]) {
1007 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1008 err_code |= ERR_ALERT | ERR_FATAL;
1009 continue;
1010 }
1011
1012 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1013 if (!http_str_to_htx(&chk, raw)) {
1014 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1015 http_err_codes[rc]);
1016 err_code |= ERR_ALERT | ERR_FATAL;
1017 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001018 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001019 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1020 http_err_replies[rc].status = http_err_codes[rc];
1021 http_err_replies[rc].ctype = NULL;
1022 LIST_INIT(&http_err_replies[rc].hdrs);
1023 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001024 }
1025end:
1026 return err_code;
1027}
1028
Christopher Faulet58857752020-01-15 15:19:50 +01001029static void http_htx_deinit(void)
1030{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001031 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001032 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001033 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001034 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001035 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001036
1037 node = ebpt_first(&http_error_messages);
1038 while (node) {
1039 next = ebpt_next(node);
1040 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001041 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1042 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001043 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001044 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001045 node = next;
1046 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001047
1048 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1049 free(http_errs->conf.file);
1050 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001051 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1052 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001053 LIST_DEL(&http_errs->list);
1054 free(http_errs);
1055 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001056
1057 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
1058 LIST_DEL(&http_rep->list);
1059 release_http_reply(http_rep);
1060 }
Christopher Faulet58857752020-01-15 15:19:50 +01001061}
1062
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001063REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001064REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001065
Christopher Faulet58857752020-01-15 15:19:50 +01001066/* Reads content of the error file <file> and convert it into an HTX message. On
1067 * success, the HTX message is returned. On error, NULL is returned and an error
1068 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001069 */
Christopher Faulet58857752020-01-15 15:19:50 +01001070struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001071{
Christopher Faulet58857752020-01-15 15:19:50 +01001072 struct buffer *buf = NULL;
1073 struct buffer chk;
1074 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001075 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001076 struct stat stat;
1077 char *err = NULL;
1078 int errnum, errlen;
1079 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001080
1081 /* already loaded */
1082 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1083 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001084 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1085 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001086 goto out;
1087 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001088
Christopher Faulet58857752020-01-15 15:19:50 +01001089 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001090 fd = open(file, O_RDONLY);
1091 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1092 memprintf(errmsg, "error opening file '%s'.", file);
1093 goto out;
1094 }
1095
1096 if (stat.st_size <= global.tune.bufsize)
1097 errlen = stat.st_size;
1098 else {
1099 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1100 file, global.tune.bufsize);
1101 errlen = global.tune.bufsize;
1102 }
1103
1104 err = malloc(errlen);
1105 if (!err) {
1106 memprintf(errmsg, "out of memory.");
1107 goto out;
1108 }
1109
1110 errnum = read(fd, err, errlen);
1111 if (errnum != errlen) {
1112 memprintf(errmsg, "error reading file '%s'.", file);
1113 goto out;
1114 }
1115
Christopher Faulet58857752020-01-15 15:19:50 +01001116 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001117 http_errmsg = calloc(1, sizeof(*http_errmsg));
1118 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001119 memprintf(errmsg, "out of memory.");
1120 goto out;
1121 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001122 http_errmsg->node.key = strdup(file);
1123 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001124 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001125 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001126 goto out;
1127 }
1128
1129 /* Convert the error file into an HTX message */
1130 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001131 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001132 free(http_errmsg->node.key);
1133 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001134 goto out;
1135 }
1136
Christopher Faulet58857752020-01-15 15:19:50 +01001137 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001138 http_errmsg->msg = chk;
1139 ebis_insert(&http_error_messages, &http_errmsg->node);
1140 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001141
Christopher Faulet5031ef52020-01-15 11:22:07 +01001142 out:
1143 if (fd >= 0)
1144 close(fd);
1145 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001146 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001147}
1148
Ilya Shipitsind4259502020-04-08 01:07:56 +05001149/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001150 * message is returned. On error, NULL is returned and an error message is
1151 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001152 */
Christopher Faulet58857752020-01-15 15:19:50 +01001153struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001154{
Christopher Faulet58857752020-01-15 15:19:50 +01001155 struct buffer *buf = NULL;
1156 struct buffer chk;
1157 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001158 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001159
1160 /* already loaded */
1161 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1162 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001163 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1164 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001165 goto out;
1166 }
1167 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001168 http_errmsg = calloc(1, sizeof(*http_errmsg));
1169 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001170 memprintf(errmsg, "out of memory.");
1171 goto out;
1172 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001173 http_errmsg->node.key = strdup(key);
1174 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001175 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001176 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001177 goto out;
1178 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001179
1180 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001181 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001182 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001183 free(http_errmsg->node.key);
1184 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001185 goto out;
1186 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001187
Christopher Faulet58857752020-01-15 15:19:50 +01001188 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001189 http_errmsg->msg = chk;
1190 ebis_insert(&http_error_messages, &http_errmsg->node);
1191 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001192 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001193 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001194}
1195
Christopher Faulet5031ef52020-01-15 11:22:07 +01001196/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001197 * <status>. It returns NULL if there is any error, otherwise it return the
1198 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001199 */
Christopher Faulet58857752020-01-15 15:19:50 +01001200struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001201{
Christopher Faulet58857752020-01-15 15:19:50 +01001202 struct buffer *buf = NULL;
1203 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001204
1205 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1206 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001207 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001208 break;
1209 }
1210 }
1211
1212 if (rc >= HTTP_ERR_SIZE)
1213 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001214 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001215}
1216
1217/* This function creates HTX error message corresponding to a redirect message
1218 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001219 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1220 * returns NULL if there is any error, otherwise it return the corresponding HTX
1221 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001222 */
Christopher Faulet58857752020-01-15 15:19:50 +01001223struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001224{
Christopher Faulet58857752020-01-15 15:19:50 +01001225 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001226 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001227 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001228 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001229
1230 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1231 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001232 /* Create the error key */
1233 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1234 memprintf(errmsg, "out of memory.");
1235 goto out;
1236 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001237 /* Create the error message */
1238 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1239 errlen = strlen(msg) + strlen(url) + 5;
1240 err = malloc(errlen);
1241 if (!err) {
1242 memprintf(errmsg, "out of memory.");
1243 goto out;
1244 }
1245 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1246
1247 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001248 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001249 break;
1250 }
1251 }
1252
1253 if (rc >= HTTP_ERR_SIZE)
1254 memprintf(errmsg, "status code '%d' not handled.", status);
1255out:
Christopher Faulet58857752020-01-15 15:19:50 +01001256 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001257 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001258 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001259}
1260
Christopher Faulet7eea2412020-05-13 15:02:59 +02001261/* Check an "http reply" and, for replies referencing an http-errors section,
1262 * try to find the right section and the right error message in this section. If
1263 * found, the reply is updated. If the http-errors section exists but the error
1264 * message is not found, no error message is set to fallback on the default
1265 * ones. Otherwise (unknown section) an error is returned.
1266 *
1267 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1268 * filled.
1269 */
1270int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1271{
1272 struct http_errors *http_errs;
1273 int ret = 1;
1274
1275 if (reply->type != HTTP_REPLY_ERRFILES)
1276 goto end;
1277
1278 list_for_each_entry(http_errs, &http_errors_list, list) {
1279 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
Christopher Faulete29a97e2020-05-14 14:49:25 +02001280 reply->type = HTTP_REPLY_INDIRECT;
Christopher Faulet7eea2412020-05-13 15:02:59 +02001281 free(reply->body.http_errors);
Christopher Faulete29a97e2020-05-14 14:49:25 +02001282 reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
1283 if (!reply->body.reply)
Christopher Faulet7eea2412020-05-13 15:02:59 +02001284 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1285 "not declared in http-errors section '%s'.\n",
1286 px->id, reply->status, http_errs->id);
1287 break;
1288 }
1289 }
1290
1291 if (&http_errs->list == &http_errors_list) {
1292 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1293 reply->body.http_errors);
1294 ret = 0;
1295 }
1296
1297 end:
1298 return ret;
1299}
1300
Christopher Faulet47e791e2020-05-13 14:36:55 +02001301/* Parse an "http reply". It returns the reply on success or NULL on error. This
1302 * function creates one of the following http replies :
1303 *
1304 * - HTTP_REPLY_EMPTY : dummy response, no payload
1305 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1306 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1307 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1308 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1309 *
1310 * The content-type must be defined for non-empty payload. It is ignored for
1311 * error messages (implicit or explicit). When an http-errors section is
1312 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1313 * during the configuration validity check or dynamically. It is the caller
1314 * responsibility to choose. If no status code is configured, <default_status>
1315 * is set.
1316 */
1317struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1318 int default_status, char **errmsg)
1319{
1320 struct logformat_node *lf, *lfb;
1321 struct http_reply *reply = NULL;
1322 struct http_reply_hdr *hdr, *hdrb;
1323 struct stat stat;
1324 const char *act_arg = NULL;
1325 char *obj = NULL;
1326 int cur_arg, cap, objlen = 0, fd = -1;
1327
1328
1329 reply = calloc(1, sizeof(*reply));
1330 if (!reply) {
1331 memprintf(errmsg, "out of memory");
1332 goto error;
1333 }
1334 LIST_INIT(&reply->hdrs);
1335 reply->type = HTTP_REPLY_EMPTY;
1336 reply->status = default_status;
1337
Christopher Faulet3b967c12020-05-15 15:47:44 +02001338 if (px->conf.args.ctx == ARGC_HERR)
1339 cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE);
1340 else
1341 cap = ((px->conf.args.ctx == ARGC_HRQ)
1342 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1343 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
Christopher Faulet47e791e2020-05-13 14:36:55 +02001344
1345 cur_arg = *orig_arg;
1346 while (*args[cur_arg]) {
1347 if (strcmp(args[cur_arg], "status") == 0) {
1348 cur_arg++;
1349 if (!*args[cur_arg]) {
1350 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1351 goto error;
1352 }
1353 reply->status = atol(args[cur_arg]);
1354 if (reply->status < 200 || reply->status > 599) {
1355 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1356 goto error;
1357 }
1358 cur_arg++;
1359 }
1360 else if (strcmp(args[cur_arg], "content-type") == 0) {
1361 cur_arg++;
1362 if (!*args[cur_arg]) {
1363 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1364 goto error;
1365 }
1366 free(reply->ctype);
1367 reply->ctype = strdup(args[cur_arg]);
1368 cur_arg++;
1369 }
1370 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1371 if (reply->type != HTTP_REPLY_EMPTY) {
1372 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1373 goto error;
1374 }
1375 act_arg = args[cur_arg];
1376 cur_arg++;
1377 if (!*args[cur_arg]) {
1378 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1379 goto error;
1380 }
1381 reply->body.http_errors = strdup(args[cur_arg]);
1382 if (!reply->body.http_errors) {
1383 memprintf(errmsg, "out of memory");
1384 goto error;
1385 }
1386 reply->type = HTTP_REPLY_ERRFILES;
1387 cur_arg++;
1388 }
1389 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1390 if (reply->type != HTTP_REPLY_EMPTY) {
1391 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1392 goto error;
1393 }
1394 act_arg = args[cur_arg];
1395 reply->type = HTTP_REPLY_ERRMSG;
1396 cur_arg++;
1397 }
1398 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1399 if (reply->type != HTTP_REPLY_EMPTY) {
1400 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1401 goto error;
1402 }
1403 act_arg = args[cur_arg];
1404 cur_arg++;
1405 if (!*args[cur_arg]) {
1406 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1407 goto error;
1408 }
1409 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1410 if (!reply->body.errmsg) {
1411 goto error;
1412 }
1413 reply->type = HTTP_REPLY_ERRMSG;
1414 cur_arg++;
1415 }
1416 else if (strcmp(args[cur_arg], "file") == 0) {
1417 if (reply->type != HTTP_REPLY_EMPTY) {
1418 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1419 goto error;
1420 }
1421 act_arg = args[cur_arg];
1422 cur_arg++;
1423 if (!*args[cur_arg]) {
1424 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1425 goto error;
1426 }
1427 fd = open(args[cur_arg], O_RDONLY);
1428 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1429 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1430 goto error;
1431 }
1432 if (stat.st_size > global.tune.bufsize) {
1433 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1434 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1435 goto error;
1436 }
1437 objlen = stat.st_size;
1438 obj = malloc(objlen);
1439 if (!obj || read(fd, obj, objlen) != objlen) {
1440 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1441 goto error;
1442 }
1443 close(fd);
1444 fd = -1;
1445 reply->type = HTTP_REPLY_RAW;
1446 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1447 obj = NULL;
1448 cur_arg++;
1449 }
1450 else if (strcmp(args[cur_arg], "string") == 0) {
1451 if (reply->type != HTTP_REPLY_EMPTY) {
1452 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1453 goto error;
1454 }
1455 act_arg = args[cur_arg];
1456 cur_arg++;
1457 if (!*args[cur_arg]) {
1458 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1459 goto error;
1460 }
1461 obj = strdup(args[cur_arg]);
1462 objlen = strlen(args[cur_arg]);
1463 if (!obj) {
1464 memprintf(errmsg, "out of memory");
1465 goto error;
1466 }
1467 reply->type = HTTP_REPLY_RAW;
1468 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1469 obj = NULL;
1470 cur_arg++;
1471 }
1472 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1473 if (reply->type != HTTP_REPLY_EMPTY) {
1474 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1475 goto error;
1476 }
1477 act_arg = args[cur_arg];
1478 cur_arg++;
1479 if (!*args[cur_arg]) {
1480 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1481 goto error;
1482 }
1483 fd = open(args[cur_arg], O_RDONLY);
1484 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1485 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1486 goto error;
1487 }
1488 if (stat.st_size > global.tune.bufsize) {
1489 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1490 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1491 goto error;
1492 }
1493 objlen = stat.st_size;
1494 obj = malloc(objlen + 1);
1495 if (!obj || read(fd, obj, objlen) != objlen) {
1496 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1497 goto error;
1498 }
1499 close(fd);
1500 fd = -1;
1501 obj[objlen] = '\0';
1502 reply->type = HTTP_REPLY_LOGFMT;
1503 cur_arg++;
1504 }
1505 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1506 if (reply->type != HTTP_REPLY_EMPTY) {
1507 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1508 goto error;
1509 }
1510 act_arg = args[cur_arg];
1511 cur_arg++;
1512 if (!*args[cur_arg]) {
1513 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1514 goto error;
1515 }
1516 obj = strdup(args[cur_arg]);
1517 objlen = strlen(args[cur_arg]);
1518 reply->type = HTTP_REPLY_LOGFMT;
1519 cur_arg++;
1520 }
1521 else if (strcmp(args[cur_arg], "hdr") == 0) {
1522 cur_arg++;
1523 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1524 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1525 goto error;
1526 }
1527 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1528 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1529 strcasecmp(args[cur_arg], "content-type") == 0) {
1530 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1531 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1532 cur_arg += 2;
1533 continue;
1534 }
1535 hdr = calloc(1, sizeof(*hdr));
1536 if (!hdr) {
1537 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1538 goto error;
1539 }
1540 LIST_INIT(&hdr->value);
1541 hdr->name = ist(strdup(args[cur_arg]));
1542 if (!isttest(hdr->name)) {
1543 memprintf(errmsg, "out of memory");
1544 goto error;
1545 }
1546 LIST_ADDQ(&reply->hdrs, &hdr->list);
1547 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1548 goto error;
1549
1550 free(px->conf.lfs_file);
1551 px->conf.lfs_file = strdup(px->conf.args.file);
1552 px->conf.lfs_line = px->conf.args.line;
1553 cur_arg += 2;
1554 }
1555 else
1556 break;
1557 }
1558
1559 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1560 if (reply->ctype) {
1561 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1562 " neither errorfile nor payload defined.\n",
1563 px->conf.args.file, px->conf.args.line, reply->ctype);
1564 free(reply->ctype);
1565 reply->ctype = NULL;
1566 }
1567 }
1568 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1569
1570 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1571 /* default errorfile or errorfiles: check the status */
1572 int rc;
1573
1574 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1575 if (http_err_codes[rc] == reply->status)
1576 break;
1577 }
1578
1579 if (rc >= HTTP_ERR_SIZE) {
1580 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1581 reply->status, act_arg);
1582 goto error;
1583 }
1584 }
1585
1586 if (reply->ctype) {
1587 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1588 "with an erorrfile.\n",
1589 px->conf.args.file, px->conf.args.line, reply->ctype);
1590 free(reply->ctype);
1591 reply->ctype = NULL;
1592 }
1593 if (!LIST_ISEMPTY(&reply->hdrs)) {
1594 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1595 "with an erorrfile.\n",
1596 px->conf.args.file, px->conf.args.line);
1597 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1598 LIST_DEL(&hdr->list);
1599 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1600 LIST_DEL(&lf->list);
1601 release_sample_expr(lf->expr);
1602 free(lf->arg);
1603 free(lf);
1604 }
1605 istfree(&hdr->name);
1606 free(hdr);
1607 }
1608 }
1609 }
1610 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1611 if (!reply->ctype && objlen) {
1612 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1613 goto error;
1614 }
1615 if (reply->ctype && !b_data(&reply->body.obj)) {
1616 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1617 "with an emtpy payload.\n",
1618 px->conf.args.file, px->conf.args.line, reply->ctype);
1619 free(reply->ctype);
1620 reply->ctype = NULL;
1621 }
1622 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1623 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1624 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1625 px->conf.args.file, px->conf.args.line);
1626 }
1627 }
1628 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1629 LIST_INIT(&reply->body.fmt);
1630 if (!reply->ctype) {
1631 memprintf(errmsg, "a content type must be defined with a log-format payload");
1632 goto error;
1633 }
1634 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1635 goto error;
1636
1637 free(px->conf.lfs_file);
1638 px->conf.lfs_file = strdup(px->conf.args.file);
1639 px->conf.lfs_line = px->conf.args.line;
1640 }
1641
1642 free(obj);
1643 *orig_arg = cur_arg;
1644 return reply;
1645
1646 error:
1647 free(obj);
1648 if (fd >= 0)
1649 close(fd);
1650 release_http_reply(reply);
1651 return NULL;
1652}
1653
Christopher Faulet07f41f72020-01-16 16:16:06 +01001654/* Parses the "errorloc[302|303]" proxy keyword */
1655static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1656 struct proxy *defpx, const char *file, int line,
1657 char **errmsg)
1658{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001659 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001660 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001661 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001662 int errloc, status;
1663 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001664
1665 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1666 ret = 1;
1667 goto out;
1668 }
1669
1670 if (*(args[1]) == 0 || *(args[2]) == 0) {
1671 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1672 ret = -1;
1673 goto out;
1674 }
1675
1676 status = atol(args[1]);
1677 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1678 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1679 if (!msg) {
1680 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1681 ret = -1;
1682 goto out;
1683 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001684
1685 reply = calloc(1, sizeof(*reply));
1686 if (!reply) {
1687 memprintf(errmsg, "%s : out of memory.", args[0]);
1688 ret = -1;
1689 goto out;
1690 }
1691 reply->type = HTTP_REPLY_ERRMSG;
1692 reply->status = status;
1693 reply->ctype = NULL;
1694 LIST_INIT(&reply->hdrs);
1695 reply->body.errmsg = msg;
1696 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001697
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001698 conf_err = calloc(1, sizeof(*conf_err));
1699 if (!conf_err) {
1700 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001701 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001702 ret = -1;
1703 goto out;
1704 }
1705 conf_err->type = 1;
1706 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001707 conf_err->info.errorfile.reply = reply;
1708
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001709 conf_err->file = strdup(file);
1710 conf_err->line = line;
1711 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001712
1713 out:
1714 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001715
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001716}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001717
1718/* Parses the "errorfile" proxy keyword */
1719static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1720 struct proxy *defpx, const char *file, int line,
1721 char **errmsg)
1722{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001723 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001724 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001725 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001726 int status;
1727 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001728
1729 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1730 ret = 1;
1731 goto out;
1732 }
1733
1734 if (*(args[1]) == 0 || *(args[2]) == 0) {
1735 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1736 ret = -1;
1737 goto out;
1738 }
1739
1740 status = atol(args[1]);
1741 msg = http_parse_errorfile(status, args[2], errmsg);
1742 if (!msg) {
1743 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1744 ret = -1;
1745 goto out;
1746 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001747
Christopher Faulet5809e102020-05-14 17:31:52 +02001748 reply = calloc(1, sizeof(*reply));
1749 if (!reply) {
1750 memprintf(errmsg, "%s : out of memory.", args[0]);
1751 ret = -1;
1752 goto out;
1753 }
1754 reply->type = HTTP_REPLY_ERRMSG;
1755 reply->status = status;
1756 reply->ctype = NULL;
1757 LIST_INIT(&reply->hdrs);
1758 reply->body.errmsg = msg;
1759 LIST_ADDQ(&http_replies_list, &reply->list);
1760
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001761 conf_err = calloc(1, sizeof(*conf_err));
1762 if (!conf_err) {
1763 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001764 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001765 ret = -1;
1766 goto out;
1767 }
1768 conf_err->type = 1;
1769 conf_err->info.errorfile.status = status;
Christopher Faulet5809e102020-05-14 17:31:52 +02001770 conf_err->info.errorfile.reply = reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001771 conf_err->file = strdup(file);
1772 conf_err->line = line;
1773 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1774
1775 out:
1776 return ret;
1777
1778}
1779
1780/* Parses the "errorfiles" proxy keyword */
1781static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1782 struct proxy *defpx, const char *file, int line,
1783 char **err)
1784{
1785 struct conf_errors *conf_err = NULL;
1786 char *name = NULL;
1787 int rc, ret = 0;
1788
1789 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1790 ret = 1;
1791 goto out;
1792 }
1793
1794 if (!*(args[1])) {
1795 memprintf(err, "%s : expects <name> as argument.", args[0]);
1796 ret = -1;
1797 goto out;
1798 }
1799
1800 name = strdup(args[1]);
1801 conf_err = calloc(1, sizeof(*conf_err));
1802 if (!name || !conf_err) {
1803 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001804 goto error;
1805 }
1806 conf_err->type = 0;
1807
1808 conf_err->info.errorfiles.name = name;
1809 if (!*(args[2])) {
1810 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1811 conf_err->info.errorfiles.status[rc] = 1;
1812 }
1813 else {
1814 int cur_arg, status;
1815 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1816 status = atol(args[cur_arg]);
1817
1818 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1819 if (http_err_codes[rc] == status) {
1820 conf_err->info.errorfiles.status[rc] = 2;
1821 break;
1822 }
1823 }
1824 if (rc >= HTTP_ERR_SIZE) {
1825 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001826 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001827 }
1828 }
1829 }
1830 conf_err->file = strdup(file);
1831 conf_err->line = line;
1832 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1833 out:
1834 return ret;
1835
1836 error:
1837 free(name);
1838 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001839 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001840 goto out;
1841}
1842
Christopher Faulet3b967c12020-05-15 15:47:44 +02001843/* Parses the "http-error" proxy keyword */
1844static int proxy_parse_http_error(char **args, int section, struct proxy *curpx,
1845 struct proxy *defpx, const char *file, int line,
1846 char **errmsg)
1847{
1848 struct conf_errors *conf_err;
1849 struct http_reply *reply = NULL;
1850 int rc, cur_arg, ret = 0;
1851
1852 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1853 ret = 1;
1854 goto out;
1855 }
1856
1857 cur_arg = 1;
1858 curpx->conf.args.ctx = ARGC_HERR;
1859 reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg);
1860 if (!reply) {
1861 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1862 goto error;
1863 }
1864 else if (!reply->status) {
1865 memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]);
1866 goto error;
1867 }
1868
1869 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1870 if (http_err_codes[rc] == reply->status)
1871 break;
1872 }
1873
1874 if (rc >= HTTP_ERR_SIZE) {
1875 memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status);
1876 goto error;
1877 }
1878 if (*args[cur_arg]) {
1879 memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]);
1880 goto error;
1881 }
1882
1883 conf_err = calloc(1, sizeof(*conf_err));
1884 if (!conf_err) {
1885 memprintf(errmsg, "%s : out of memory.", args[0]);
1886 goto error;
1887 }
1888 if (reply->type == HTTP_REPLY_ERRFILES) {
1889 int rc = http_get_status_idx(reply->status);
1890
1891 conf_err->type = 2;
1892 conf_err->info.errorfiles.name = reply->body.http_errors;
1893 conf_err->info.errorfiles.status[rc] = 2;
1894 reply->body.http_errors = NULL;
1895 release_http_reply(reply);
1896 }
1897 else {
1898 conf_err->type = 1;
1899 conf_err->info.errorfile.status = reply->status;
1900 conf_err->info.errorfile.reply = reply;
1901 LIST_ADDQ(&http_replies_list, &reply->list);
1902 }
1903 conf_err->file = strdup(file);
1904 conf_err->line = line;
1905 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1906
1907 out:
1908 return ret;
1909
1910 error:
1911 release_http_reply(reply);
1912 ret = -1;
1913 goto out;
1914
1915}
1916
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001917/* Check "errorfiles" proxy keyword */
1918static int proxy_check_errors(struct proxy *px)
1919{
1920 struct conf_errors *conf_err, *conf_err_back;
1921 struct http_errors *http_errs;
1922 int rc, err = 0;
1923
1924 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1925 if (conf_err->type == 1) {
1926 /* errorfile */
1927 rc = http_get_status_idx(conf_err->info.errorfile.status);
Christopher Faulet40e85692020-05-14 17:34:31 +02001928 px->replies[rc] = conf_err->info.errorfile.reply;
Christopher Faulet3b967c12020-05-15 15:47:44 +02001929
1930 /* For proxy, to rely on default replies, just don't reference a reply */
1931 if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg)
1932 px->replies[rc] = NULL;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001933 }
1934 else {
1935 /* errorfiles */
1936 list_for_each_entry(http_errs, &http_errors_list, list) {
1937 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1938 break;
1939 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001940
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001941 /* unknown http-errors section */
1942 if (&http_errs->list == &http_errors_list) {
1943 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1944 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1945 err |= ERR_ALERT | ERR_FATAL;
1946 free(conf_err->info.errorfiles.name);
1947 goto next;
1948 }
1949
1950 free(conf_err->info.errorfiles.name);
1951 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1952 if (conf_err->info.errorfiles.status[rc] > 0) {
Christopher Fauletf1fedc32020-05-15 14:30:32 +02001953 if (http_errs->replies[rc])
Christopher Faulet40e85692020-05-14 17:34:31 +02001954 px->replies[rc] = http_errs->replies[rc];
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001955 else if (conf_err->info.errorfiles.status[rc] == 2)
1956 ha_warning("config: proxy '%s' : status '%d' not declared in"
1957 " http-errors section '%s' (at %s:%d).\n",
1958 px->id, http_err_codes[rc], http_errs->id,
1959 conf_err->file, conf_err->line);
1960 }
1961 }
1962 }
1963 next:
1964 LIST_DEL(&conf_err->list);
1965 free(conf_err->file);
1966 free(conf_err);
1967 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001968
1969 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001970 return err;
1971}
1972
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001973static int post_check_errors()
1974{
1975 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001976 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001977 struct htx *htx;
1978 int err_code = 0;
1979
1980 node = ebpt_first(&http_error_messages);
1981 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001982 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1983 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001984 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001985 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001986 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1987 ha_warning("config: errorfile '%s' runs over the buffer space"
1988 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001989 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001990 (char *)node->key);
1991 err_code |= ERR_WARN;
1992 }
1993 next:
1994 node = ebpt_next(node);
1995 }
1996
1997 return err_code;
1998}
1999
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002000int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
2001{
2002 struct conf_errors *conf_err, *new_conf_err = NULL;
2003 int ret = 0;
2004
2005 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
2006 new_conf_err = calloc(1, sizeof(*new_conf_err));
2007 if (!new_conf_err) {
2008 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2009 goto out;
2010 }
2011 new_conf_err->type = conf_err->type;
2012 if (conf_err->type == 1) {
2013 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
Christopher Faulet40e85692020-05-14 17:34:31 +02002014 new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002015 }
2016 else {
2017 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
2018 if (!new_conf_err->info.errorfiles.name) {
2019 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
2020 goto out;
2021 }
2022 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
2023 sizeof(conf_err->info.errorfiles.status));
2024 }
2025 new_conf_err->file = strdup(conf_err->file);
2026 new_conf_err->line = conf_err->line;
2027 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
2028 new_conf_err = NULL;
2029 }
2030 ret = 1;
2031
2032 out:
2033 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002034 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002035}
2036
2037void proxy_release_conf_errors(struct proxy *px)
2038{
2039 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002040
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002041 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
2042 if (conf_err->type == 0)
2043 free(conf_err->info.errorfiles.name);
2044 LIST_DEL(&conf_err->list);
2045 free(conf_err->file);
2046 free(conf_err);
2047 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002048}
2049
2050/*
2051 * Parse an <http-errors> section.
2052 * Returns the error code, 0 if OK, or any combination of :
2053 * - ERR_ABORT: must abort ASAP
2054 * - ERR_FATAL: we can continue parsing but not start the service
2055 * - ERR_WARN: a warning has been emitted
2056 * - ERR_ALERT: an alert has been emitted
2057 * Only the two first ones can stop processing, the two others are just
2058 * indicators.
2059 */
2060static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
2061{
2062 static struct http_errors *curr_errs = NULL;
2063 int err_code = 0;
2064 const char *err;
2065 char *errmsg = NULL;
2066
2067 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
2068 if (!*args[1]) {
2069 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
2070 err_code |= ERR_ALERT | ERR_ABORT;
2071 goto out;
2072 }
2073
2074 err = invalid_char(args[1]);
2075 if (err) {
2076 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2077 file, linenum, *err, args[0], args[1]);
2078 err_code |= ERR_ALERT | ERR_FATAL;
2079 }
2080
2081 list_for_each_entry(curr_errs, &http_errors_list, list) {
2082 /* Error if two errors section owns the same name */
2083 if (strcmp(curr_errs->id, args[1]) == 0) {
2084 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2085 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2086 err_code |= ERR_ALERT | ERR_FATAL;
2087 }
2088 }
2089
2090 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2091 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2092 err_code |= ERR_ALERT | ERR_ABORT;
2093 goto out;
2094 }
2095
2096 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2097 curr_errs->id = strdup(args[1]);
2098 curr_errs->conf.file = strdup(file);
2099 curr_errs->conf.line = linenum;
2100 }
2101 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002102 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002103 struct buffer *msg;
2104 int status, rc;
2105
2106 if (*(args[1]) == 0 || *(args[2]) == 0) {
2107 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2108 file, linenum, args[0]);
2109 err_code |= ERR_ALERT | ERR_FATAL;
2110 goto out;
2111 }
2112
2113 status = atol(args[1]);
2114 msg = http_parse_errorfile(status, args[2], &errmsg);
2115 if (!msg) {
2116 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2117 err_code |= ERR_ALERT | ERR_FATAL;
2118 goto out;
2119 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002120
2121 reply = calloc(1, sizeof(*reply));
2122 if (!reply) {
2123 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2124 err_code |= ERR_ALERT | ERR_FATAL;
2125 goto out;
2126 }
2127 reply->type = HTTP_REPLY_ERRMSG;
2128 reply->status = status;
2129 reply->ctype = NULL;
2130 LIST_INIT(&reply->hdrs);
2131 reply->body.errmsg = msg;
2132
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002133 rc = http_get_status_idx(status);
Christopher Fauletde30bb72020-05-14 10:03:55 +02002134 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002135 }
2136 else if (*args[0] != 0) {
2137 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2138 err_code |= ERR_ALERT | ERR_FATAL;
2139 goto out;
2140 }
2141
2142out:
2143 free(errmsg);
2144 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002145}
2146
2147static struct cfg_kw_list cfg_kws = {ILH, {
2148 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2149 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2150 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2151 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002152 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet3b967c12020-05-15 15:47:44 +02002153 { CFG_LISTEN, "http-error", proxy_parse_http_error },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002154 { 0, NULL, NULL },
2155}};
2156
2157INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002158REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002159REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002160
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002161REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2162
Christopher Faulet29f72842019-12-11 15:52:32 +01002163/************************************************************************/
2164/* HTX sample fetches */
2165/************************************************************************/
2166
2167/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2168static int
2169smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2170{
2171 if (!smp->strm)
2172 return 0;
2173
2174 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2175 smp->data.type = SMP_T_BOOL;
2176 return 1;
2177}
2178
2179/* Returns the number of blocks in an HTX message. The channel is chosen
2180 * depending on the sample direction. */
2181static int
2182smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2183{
2184 struct channel *chn;
2185 struct htx *htx;
2186
2187 if (!smp->strm)
2188 return 0;
2189
2190 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002191 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002192 if (!htx)
2193 return 0;
2194
2195 smp->data.u.sint = htx_nbblks(htx);
2196 smp->data.type = SMP_T_SINT;
2197 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2198 return 1;
2199}
2200
2201/* Returns the size of an HTX message. The channel is chosen depending on the
2202 * sample direction. */
2203static int
2204smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2205{
2206 struct channel *chn;
2207 struct htx *htx;
2208
2209 if (!smp->strm)
2210 return 0;
2211
2212 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002213 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002214 if (!htx)
2215 return 0;
2216
2217 smp->data.u.sint = htx->size;
2218 smp->data.type = SMP_T_SINT;
2219 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2220 return 1;
2221}
2222
2223/* Returns the data size of an HTX message. The channel is chosen depending on the
2224 * sample direction. */
2225static int
2226smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2227{
2228 struct channel *chn;
2229 struct htx *htx;
2230
2231 if (!smp->strm)
2232 return 0;
2233
2234 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002235 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002236 if (!htx)
2237 return 0;
2238
2239 smp->data.u.sint = htx->data;
2240 smp->data.type = SMP_T_SINT;
2241 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2242 return 1;
2243}
2244
2245/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2246 * depending on the sample direction. */
2247static int
2248smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2249{
2250 struct channel *chn;
2251 struct htx *htx;
2252
2253 if (!smp->strm)
2254 return 0;
2255
2256 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002257 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002258 if (!htx)
2259 return 0;
2260
2261 smp->data.u.sint = htx_used_space(htx);
2262 smp->data.type = SMP_T_SINT;
2263 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2264 return 1;
2265}
2266
2267/* Returns the free space (size-used) of an HTX message. The channel is chosen
2268 * depending on the sample direction. */
2269static int
2270smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2271{
2272 struct channel *chn;
2273 struct htx *htx;
2274
2275 if (!smp->strm)
2276 return 0;
2277
2278 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002279 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002280 if (!htx)
2281 return 0;
2282
2283 smp->data.u.sint = htx_free_space(htx);
2284 smp->data.type = SMP_T_SINT;
2285 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2286 return 1;
2287}
2288
2289/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2290 * channel is chosen depending on the sample direction. */
2291static int
2292smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2293{
2294 struct channel *chn;
2295 struct htx *htx;
2296
2297 if (!smp->strm)
2298 return 0;
2299
2300 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002301 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002302 if (!htx)
2303 return 0;
2304
2305 smp->data.u.sint = htx_free_data_space(htx);
2306 smp->data.type = SMP_T_SINT;
2307 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2308 return 1;
2309}
2310
2311/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2312 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2313 * the sample direction. */
2314static int
2315smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2316{
2317 struct channel *chn;
2318 struct htx *htx;
2319
2320 if (!smp->strm)
2321 return 0;
2322
2323 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002324 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002325 if (!htx)
2326 return 0;
2327
2328 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2329 smp->data.type = SMP_T_BOOL;
2330 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2331 return 1;
2332}
2333
2334/* Returns the type of a specific HTX block, if found in the message. Otherwise
2335 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2336 * "head", "tail" or "first". The channel is chosen depending on the sample
2337 * direction. */
2338static int
2339smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2340{
2341 struct channel *chn;
2342 struct htx *htx;
2343 enum htx_blk_type type;
2344 int32_t pos;
2345
2346 if (!smp->strm || !arg_p)
2347 return 0;
2348
2349 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002350 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002351 if (!htx)
2352 return 0;
2353
2354 pos = arg_p[0].data.sint;
2355 if (pos == -1)
2356 type = htx_get_head_type(htx);
2357 else if (pos == -2)
2358 type = htx_get_tail_type(htx);
2359 else if (pos == -3)
2360 type = htx_get_first_type(htx);
2361 else
2362 type = ((pos >= htx->head && pos <= htx->tail)
2363 ? htx_get_blk_type(htx_get_blk(htx, pos))
2364 : HTX_BLK_UNUSED);
2365
2366 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2367 smp->data.type = SMP_T_STR;
2368 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2369 return 1;
2370}
2371
2372/* Returns the size of a specific HTX block, if found in the message. Otherwise
2373 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2374 * "first". The channel is chosen depending on the sample direction. */
2375static int
2376smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2377{
2378 struct channel *chn;
2379 struct htx *htx;
2380 struct htx_blk *blk;
2381 int32_t pos;
2382
2383 if (!smp->strm || !arg_p)
2384 return 0;
2385
2386 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002387 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002388 if (!htx)
2389 return 0;
2390
2391 pos = arg_p[0].data.sint;
2392 if (pos == -1)
2393 blk = htx_get_head_blk(htx);
2394 else if (pos == -2)
2395 blk = htx_get_tail_blk(htx);
2396 else if (pos == -3)
2397 blk = htx_get_first_blk(htx);
2398 else
2399 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2400
2401 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2402 smp->data.type = SMP_T_SINT;
2403 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2404 return 1;
2405}
2406
2407/* Returns the start-line if the selected HTX block exists and is a
2408 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2409 * supported or "head", "tail" or "first". The channel is chosen depending on
2410 * the sample direction. */
2411static int
2412smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2413{
2414 struct buffer *temp;
2415 struct channel *chn;
2416 struct htx *htx;
2417 struct htx_blk *blk;
2418 struct htx_sl *sl;
2419 int32_t pos;
2420
2421 if (!smp->strm || !arg_p)
2422 return 0;
2423
2424 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002425 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002426 if (!htx)
2427 return 0;
2428
2429 pos = arg_p[0].data.sint;
2430 if (pos == -1)
2431 blk = htx_get_head_blk(htx);
2432 else if (pos == -2)
2433 blk = htx_get_tail_blk(htx);
2434 else if (pos == -3)
2435 blk = htx_get_first_blk(htx);
2436 else
2437 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2438
2439 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2440 smp->data.u.str.size = 0;
2441 smp->data.u.str.area = "";
2442 smp->data.u.str.data = 0;
2443 }
2444 else {
2445 sl = htx_get_blk_ptr(htx, blk);
2446
2447 temp = get_trash_chunk();
2448 chunk_istcat(temp, htx_sl_p1(sl));
2449 temp->area[temp->data++] = ' ';
2450 chunk_istcat(temp, htx_sl_p2(sl));
2451 temp->area[temp->data++] = ' ';
2452 chunk_istcat(temp, htx_sl_p3(sl));
2453
2454 smp->data.u.str = *temp;
2455 }
2456
2457 smp->data.type = SMP_T_STR;
2458 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2459 return 1;
2460}
2461
2462/* Returns the header name if the selected HTX block exists and is a header or a
2463 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2464 * supported or "head", "tail" or "first". The channel is chosen depending on
2465 * the sample direction. */
2466static int
2467smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2468{
2469 struct channel *chn;
2470 struct htx *htx;
2471 struct htx_blk *blk;
2472 int32_t pos;
2473
2474 if (!smp->strm || !arg_p)
2475 return 0;
2476
2477 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002478 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002479 if (!htx)
2480 return 0;
2481
2482 pos = arg_p[0].data.sint;
2483 if (pos == -1)
2484 blk = htx_get_head_blk(htx);
2485 else if (pos == -2)
2486 blk = htx_get_tail_blk(htx);
2487 else if (pos == -3)
2488 blk = htx_get_first_blk(htx);
2489 else
2490 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2491
2492 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2493 smp->data.u.str.size = 0;
2494 smp->data.u.str.area = "";
2495 smp->data.u.str.data = 0;
2496 }
2497 else {
2498 struct ist name = htx_get_blk_name(htx, blk);
2499
2500 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2501 }
2502 smp->data.type = SMP_T_STR;
2503 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2504 return 1;
2505}
2506
2507/* Returns the header value if the selected HTX block exists and is a header or
2508 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2509 * supported or "head", "tail" or "first". The channel is chosen depending on
2510 * the sample direction. */
2511static int
2512smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2513{
2514 struct channel *chn;
2515 struct htx *htx;
2516 struct htx_blk *blk;
2517 int32_t pos;
2518
2519 if (!smp->strm || !arg_p)
2520 return 0;
2521
2522 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002523 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002524 if (!htx)
2525 return 0;
2526
2527 pos = arg_p[0].data.sint;
2528 if (pos == -1)
2529 blk = htx_get_head_blk(htx);
2530 else if (pos == -2)
2531 blk = htx_get_tail_blk(htx);
2532 else if (pos == -3)
2533 blk = htx_get_first_blk(htx);
2534 else
2535 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2536
2537 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2538 smp->data.u.str.size = 0;
2539 smp->data.u.str.area = "";
2540 smp->data.u.str.data = 0;
2541 }
2542 else {
2543 struct ist val = htx_get_blk_value(htx, blk);
2544
2545 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2546 }
2547 smp->data.type = SMP_T_STR;
2548 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2549 return 1;
2550}
2551
2552/* Returns the value if the selected HTX block exists and is a data
2553 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2554 * or "head", "tail" or "first". The channel is chosen depending on the sample
2555 * direction. */
2556static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002557smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002558{
2559 struct channel *chn;
2560 struct htx *htx;
2561 struct htx_blk *blk;
2562 int32_t pos;
2563
2564 if (!smp->strm || !arg_p)
2565 return 0;
2566
2567 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002568 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002569 if (!htx)
2570 return 0;
2571
2572 pos = arg_p[0].data.sint;
2573 if (pos == -1)
2574 blk = htx_get_head_blk(htx);
2575 else if (pos == -2)
2576 blk = htx_get_tail_blk(htx);
2577 else if (pos == -3)
2578 blk = htx_get_first_blk(htx);
2579 else
2580 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2581
2582 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2583 smp->data.u.str.size = 0;
2584 smp->data.u.str.area = "";
2585 smp->data.u.str.data = 0;
2586 }
2587 else {
2588 struct ist val = htx_get_blk_value(htx, blk);
2589
2590 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2591 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002592 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002593 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2594 return 1;
2595}
2596
2597/* This function is used to validate the arguments passed to any "htx_blk" fetch
2598 * keywords. An argument is expected by these keywords. It must be a positive
2599 * integer or on of the following strings: "head", "tail" or "first". It returns
2600 * 0 on error, and a non-zero value if OK.
2601 */
2602int val_blk_arg(struct arg *arg, char **err_msg)
2603{
2604 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2605 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2606 return 0;
2607 }
2608 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2609 free(arg[0].data.str.area);
2610 arg[0].type = ARGT_SINT;
2611 arg[0].data.sint = -1;
2612 }
2613 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2614 free(arg[0].data.str.area);
2615 arg[0].type = ARGT_SINT;
2616 arg[0].data.sint = -2;
2617 }
2618 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2619 free(arg[0].data.str.area);
2620 arg[0].type = ARGT_SINT;
2621 arg[0].data.sint = -3;
2622 }
2623 else {
2624 int pos;
2625
2626 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002627 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002628 memprintf(err_msg, "invalid block position");
2629 return 0;
2630 }
2631 }
2632
2633 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2634 if (pos < 0) {
2635 memprintf(err_msg, "block position must not be negative");
2636 return 0;
2637 }
2638 free(arg[0].data.str.area);
2639 arg[0].type = ARGT_SINT;
2640 arg[0].data.sint = pos;
2641 }
2642
2643 return 1;
2644}
2645
2646
2647/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002648 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002649 */
2650static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002651 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002652
Christopher Faulet01f44452020-01-08 14:23:40 +01002653 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2654 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2655 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2656 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2657 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2658 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2659 { "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 +01002660
Christopher Faulet01f44452020-01-08 14:23:40 +01002661 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2662 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2663 { "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},
2664 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2665 { "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 +01002666 { "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 +01002667
2668 { /* END */ },
2669}};
2670
2671INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);