blob: b830c736e61bc6d7ed7b99a5b2fc1ff1b328173b [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 */
45 struct buffer *msg; /* the HTX error message */
Christopher Faulet5809e102020-05-14 17:31:52 +020046 struct http_reply *reply; /* the http reply for the errorfile */
Christopher Faulet76edc0f2020-01-13 15:52:01 +010047 } errorfile; /* describe an "errorfile" directive */
48 struct {
49 char *name; /* the http-errors section name */
50 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
51 } errorfiles; /* describe an "errorfiles" directive */
52 } info;
53
54 char *file; /* file where the directive appears */
55 int line; /* line where the directive appears */
56
57 struct list list; /* next conf_errors */
58};
59
Christopher Faulet297fbb42019-05-13 14:41:27 +020060/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020061 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020062 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020063 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020064struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020065{
Christopher Faulet297fbb42019-05-13 14:41:27 +020066 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010067
Christopher Faulet29f17582019-05-23 11:03:26 +020068 BUG_ON(htx->first == -1);
69 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020070 if (!blk)
71 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020072 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 +020073 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020074}
75
Christopher Faulet727a3f12020-02-07 16:39:41 +010076/* Returns the headers size in the HTX message */
77size_t http_get_hdrs_size(struct htx *htx)
78{
79 struct htx_blk *blk;
80 size_t sz = 0;
81
82 blk = htx_get_first_blk(htx);
83 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
84 return sz;
85
86 for (; blk; blk = htx_get_next_blk(htx, blk)) {
87 sz += htx_get_blksz(blk);
88 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
89 break;
90 }
91 return sz;
92}
93
Christopher Faulet8dd33e12020-05-05 07:42:42 +020094/* Finds the first or next occurrence of header matching <pattern> in the HTX
95 * message <htx> using the context <ctx>. This structure holds everything
96 * necessary to use the header and find next occurrence. If its <blk> member is
97 * NULL, the header is searched from the beginning. Otherwise, the next
98 * occurrence is returned. The function returns 1 when it finds a value, and 0
99 * when there is no more. It is designed to work with headers defined as
100 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
101 * full-line headers in whose comma is not a delimiter but is part of the
102 * syntax. A special case, if ctx->value is NULL when searching for a new values
103 * of a header, the current header is rescanned. This allows rescanning after a
104 * header deletion.
105 *
106 * The matching method is chosen by checking the flags :
107 *
108 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
109 * the regex are evaluated.
110 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
111 * to the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
113 * starting by the string are evaluated.
114 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
115 * ending by the string are evaluated.
116 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
117 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200118 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200119
120#define HTTP_FIND_FL_MATCH_STR 0x0001
121#define HTTP_FIND_FL_MATCH_PFX 0x0002
122#define HTTP_FIND_FL_MATCH_SFX 0x0003
123#define HTTP_FIND_FL_MATCH_SUB 0x0004
124#define HTTP_FIND_FL_MATCH_REG 0x0005
125/* 0x0006..0x000f: for other matching methods */
126#define HTTP_FIND_FL_MATCH_TYPE 0x000F
127#define HTTP_FIND_FL_FULL 0x0010
128
129static 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 +0200130{
131 struct htx_blk *blk = ctx->blk;
132 struct ist n, v;
133 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200134
135 if (blk) {
136 char *p;
137
Tim Duesterhused526372020-03-05 17:56:33 +0100138 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200139 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200140 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200141 goto next_blk;
142 v = htx_get_blk_value(htx, blk);
143 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
144 v.len -= (p - v.ptr);
145 v.ptr = p;
146 if (!v.len)
147 goto next_blk;
148 /* Skip comma */
149 if (*(v.ptr) == ',') {
150 v.ptr++;
151 v.len--;
152 }
153
154 goto return_hdr;
155 }
156
Christopher Faulet192c6a22019-06-11 16:32:24 +0200157 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200158 return 0;
159
Christopher Fauleta3f15502019-05-13 15:27:23 +0200160 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200161 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200162 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100163 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
164 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200165 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200166 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200167
168 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
169 const struct my_regex *re = pattern;
170
171 n = htx_get_blk_name(htx, blk);
172 if (!regex_exec2(re, n.ptr, n.len))
173 goto next_blk;
174 }
175 else {
176 const struct ist name = *(const struct ist *)(pattern);
177
Christopher Faulet47596d32018-10-22 09:17:28 +0200178 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200179 if (!istlen(name))
180 goto match;
181
Christopher Faulet47596d32018-10-22 09:17:28 +0200182 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200183 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
184 case HTTP_FIND_FL_MATCH_STR:
185 if (!isteqi(n, name))
186 goto next_blk;
187 break;
188 case HTTP_FIND_FL_MATCH_PFX:
189 if (istlen(n) < istlen(name))
190 goto next_blk;
191
192 n = ist2(istptr(n), istlen(name));
193 if (!isteqi(n, name))
194 goto next_blk;
195 break;
196 case HTTP_FIND_FL_MATCH_SFX:
197 if (istlen(n) < istlen(name))
198 goto next_blk;
199
200 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
201 if (!isteqi(n, name))
202 goto next_blk;
203 break;
204 case HTTP_FIND_FL_MATCH_SUB:
205 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
206 goto next_blk;
207 break;
208 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200209 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200210 break;
211 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200213 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200214 v = htx_get_blk_value(htx, blk);
215
216 return_hdr:
217 ctx->lws_before = 0;
218 ctx->lws_after = 0;
219 while (v.len && HTTP_IS_LWS(*v.ptr)) {
220 v.ptr++;
221 v.len--;
222 ctx->lws_before++;
223 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200224 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200225 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
226 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
227 v.len--;
228 ctx->lws_after++;
229 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200230 ctx->blk = blk;
231 ctx->value = v;
232 return 1;
233
234 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200235 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200236 }
237
238 ctx->blk = NULL;
239 ctx->value = ist("");
240 ctx->lws_before = ctx->lws_after = 0;
241 return 0;
242}
243
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200244
245/* Header names must match <name> */
246int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
247{
248 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
249}
250
251/* Header names must match <name>. Same than http_find_header */
252int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
253{
254 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
255}
256
257
258/* Header names must start with <prefix> */
259int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
260{
261 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
262}
263
264/* Header names must end with <suffix> */
265int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
266{
267 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
268}
269/* Header names must contain <sub> */
270int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
271{
272 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
273}
274
275/* Header names must match <re> regex*/
276int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
277{
278 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
279}
280
281
Christopher Faulet47596d32018-10-22 09:17:28 +0200282/* Adds a header block int the HTX message <htx>, just before the EOH block. It
283 * returns 1 on success, otherwise it returns 0.
284 */
285int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
286{
287 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200288 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200289 enum htx_blk_type type = htx_get_tail_type(htx);
290 int32_t prev;
291
292 blk = htx_add_header(htx, n, v);
293 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200294 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200295
296 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200297 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200298
299 /* <blk> is the head, swap it iteratively with its predecessor to place
300 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200301 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200302 struct htx_blk *pblk = htx_get_blk(htx, prev);
303 enum htx_blk_type type = htx_get_blk_type(pblk);
304
305 /* Swap .addr and .info fields */
306 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
307 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
308
309 if (blk->addr == pblk->addr)
310 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200311
312 /* Stop when end-of-header is reached */
313 if (type == HTX_BLK_EOH)
314 break;
315
316 blk = pblk;
317 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200318
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200319 end:
320 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100321 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200322 if (!http_update_authority(htx, sl, v))
323 goto fail;
324 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200325 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200326
327 fail:
328 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200329}
330
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100331/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200332 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200333 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100334int 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 +0200335{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200336 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200337
Christopher Faulet29f17582019-05-23 11:03:26 +0200338 blk = htx_get_first_blk(htx);
339 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200340 return 0;
341 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200342}
343
Christopher Faulete010c802018-10-24 10:36:45 +0200344/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
345 * on success, otherwise 0.
346 */
347int http_replace_req_meth(struct htx *htx, const struct ist meth)
348{
349 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200350 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100351 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200352
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100353 if (!sl)
354 return 0;
355
Christopher Faulete010c802018-10-24 10:36:45 +0200356 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100357 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
358 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200359
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100360 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
361 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200362
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100363 /* create the new start line */
364 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
365 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200366}
367
368/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
369 * success, otherwise 0.
370 */
371int http_replace_req_uri(struct htx *htx, const struct ist uri)
372{
373 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200374 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100375 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200376
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100377 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200378 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100379
Christopher Faulete010c802018-10-24 10:36:45 +0200380 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100381 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
382 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200383
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100384 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
385 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200386
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100387 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200388 if (!http_replace_stline(htx, meth, uri, vsn))
389 goto fail;
390
391 sl = http_get_stline(htx);
392 if (!http_update_host(htx, sl, uri))
393 goto fail;
394
395 return 1;
396 fail:
397 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200398}
399
400/* Replace the request path in the HTX message <htx> by <path>. The host part
401 * and the query string are preserved. It returns 1 on success, otherwise 0.
402 */
403int http_replace_req_path(struct htx *htx, const struct ist path)
404{
405 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200406 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100407 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200408 size_t plen = 0;
409
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100410 if (!sl)
411 return 0;
412
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100413 uri = htx_sl_req_uri(sl);
414 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100415 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100416 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200417 while (plen < p.len && *(p.ptr + plen) != '?')
418 plen++;
419
420 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100421 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
422 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200423
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100424 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
425 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
426
427 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200428 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
429 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100430 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200431
432 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100433 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200434}
435
436/* Replace the request query-string in the HTX message <htx> by <query>. The
437 * host part and the path are preserved. It returns 1 on success, otherwise
438 * 0.
439 */
440int http_replace_req_query(struct htx *htx, const struct ist query)
441{
442 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200443 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100444 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200445 int offset = 1;
446
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100447 if (!sl)
448 return 0;
449
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100450 uri = htx_sl_req_uri(sl);
451 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200452 while (q.len > 0 && *(q.ptr) != '?') {
453 q.ptr++;
454 q.len--;
455 }
456
457 /* skip the question mark or indicate that we must insert it
458 * (but only if the format string is not empty then).
459 */
460 if (q.len) {
461 q.ptr++;
462 q.len--;
463 }
464 else if (query.len > 1)
465 offset = 0;
466
467 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100468 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
469 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200470
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100471 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
472 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200473
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100474 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
475 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
476 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200477
478 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200480}
481
482/* Replace the response status in the HTX message <htx> by <status>. It returns
483 * 1 on success, otherwise 0.
484*/
485int http_replace_res_status(struct htx *htx, const struct ist status)
486{
487 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200488 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100489 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200490
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100491 if (!sl)
492 return 0;
493
Christopher Faulete010c802018-10-24 10:36:45 +0200494 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100495 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
496 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200497
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100498 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
499 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200500
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100501 /* create the new start line */
502 sl->info.res.status = strl2ui(status.ptr, status.len);
503 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200504}
505
506/* Replace the response reason in the HTX message <htx> by <reason>. It returns
507 * 1 on success, otherwise 0.
508*/
509int http_replace_res_reason(struct htx *htx, const struct ist reason)
510{
511 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200512 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100513 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200514
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100515 if (!sl)
516 return 0;
517
Christopher Faulete010c802018-10-24 10:36:45 +0200518 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100519 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
520 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200521
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
523 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200524
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100525 /* create the new start line */
526 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200527}
528
Christopher Faulet47596d32018-10-22 09:17:28 +0200529/* Replaces a part of a header value referenced in the context <ctx> by
530 * <data>. It returns 1 on success, otherwise it returns 0. The context is
531 * updated if necessary.
532 */
533int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
534{
535 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200536 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200537 char *start;
538 struct ist v;
539 uint32_t len, off;
540
541 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200542 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200543
544 v = htx_get_blk_value(htx, blk);
545 start = ctx->value.ptr - ctx->lws_before;
546 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
547 off = start - v.ptr;
548
549 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
550 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200551 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200552
553 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200554
555 sl = http_get_stline(htx);
556 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
557 struct ist n = htx_get_blk_name(htx, blk);
558
559 if (isteq(n, ist("host"))) {
560 if (!http_update_authority(htx, sl, v))
561 goto fail;
562 ctx->blk = NULL;
563 http_find_header(htx, ist("host"), ctx, 1);
564 blk = ctx->blk;
565 v = htx_get_blk_value(htx, blk);
566 }
567 }
568
Christopher Faulet47596d32018-10-22 09:17:28 +0200569 ctx->blk = blk;
570 ctx->value.ptr = v.ptr + off;
571 ctx->value.len = data.len;
572 ctx->lws_before = ctx->lws_after = 0;
573
574 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200575 fail:
576 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200577}
578
579/* Fully replaces a header referenced in the context <ctx> by the name <name>
580 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
581 * context is updated if necessary.
582 */
583int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
584 const struct ist name, const struct ist value)
585{
586 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200587 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200588
589 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200590 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200591
592 blk = htx_replace_header(htx, blk, name, value);
593 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200594 goto fail;
595
596 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100597 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200598 if (!http_update_authority(htx, sl, value))
599 goto fail;
600 ctx->blk = NULL;
601 http_find_header(htx, ist("host"), ctx, 1);
602 blk = ctx->blk;
603 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200604
605 ctx->blk = blk;
606 ctx->value = ist(NULL);
607 ctx->lws_before = ctx->lws_after = 0;
608
609 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200610 fail:
611 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200612}
613
614/* Remove one value of a header. This only works on a <ctx> returned by
615 * http_find_header function. The value is removed, as well as surrounding commas
616 * if any. If the removed value was alone, the whole header is removed. The
617 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
618 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
619 * form that can be handled by http_find_header() to find next occurrence.
620 */
621int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
622{
623 struct htx_blk *blk = ctx->blk;
624 char *start;
625 struct ist v;
626 uint32_t len;
627
628 if (!blk)
629 return 0;
630
631 start = ctx->value.ptr - ctx->lws_before;
632 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
633
634 v = htx_get_blk_value(htx, blk);
635 if (len == v.len) {
636 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200637 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200638 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100639 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200640 ctx->lws_before = ctx->lws_after = 0;
641 }
642 else {
643 ctx->blk = htx_get_blk(htx, htx->tail);
644 ctx->value = htx_get_blk_value(htx, ctx->blk);
645 ctx->lws_before = ctx->lws_after = 0;
646 }
647 return 1;
648 }
649
650 /* This was not the only value of this header. We have to remove the
651 * part pointed by ctx->value. If it is the last entry of the list, we
652 * remove the last separator.
653 */
654 if (start == v.ptr) {
655 /* It's the first header part but not the only one. So remove
656 * the comma after it. */
657 len++;
658 }
659 else {
660 /* There is at least one header part before the removed one. So
661 * remove the comma between them. */
662 start--;
663 len++;
664 }
665 /* Update the block content and its len */
666 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200667 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200668
669 /* Finally update the ctx */
670 ctx->value.ptr = start;
671 ctx->value.len = 0;
672 ctx->lws_before = ctx->lws_after = 0;
673
674 return 1;
675}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200676
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200677/* Updates the authority part of the uri with the value <host>. It happens when
678 * the header host is modified. It returns 0 on failure and 1 on success. It is
679 * the caller responsibility to provide the start-line and to be sure the uri
680 * contains an authority. Thus, if no authority is found in the uri, an error is
681 * returned.
682 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200683int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200684{
685 struct buffer *temp = get_trash_chunk();
686 struct ist meth, vsn, uri, authority;
687
688 uri = htx_sl_req_uri(sl);
689 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100690 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200691 return 0;
692
Christopher Faulet34b18e42020-02-18 11:02:21 +0100693 /* Don't update the uri if there is no change */
694 if (isteq(host, authority))
695 return 1;
696
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200697 /* Start by copying old method and version */
698 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
699 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
700
701 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
702 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
703
704 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
705 chunk_memcat(temp, host.ptr, host.len);
706 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
707 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
708
709 return http_replace_stline(htx, meth, uri, vsn);
710
711}
712
713/* Update the header host by extracting the authority of the uri <uri>. flags of
714 * the start-line are also updated accordingly. For orgin-form and asterisk-form
715 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
716 * removed from the flags of the start-line. Otherwise, this flag is set and the
717 * authority is used to set the value of the header host. This function returns
718 * 0 on failure and 1 on success.
719*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200720int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200721{
722 struct ist authority;
723 struct http_hdr_ctx ctx;
724
725 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
726 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
727 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
728 }
729 else {
730 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
731 if (sl->info.req.meth != HTTP_METH_CONNECT) {
732 // absolute-form (RFC7320 #5.3.2)
733 sl->flags |= HTX_SL_F_HAS_SCHM;
734 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
735 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
736
737 authority = http_get_authority(uri, 1);
738 if (!authority.len)
739 goto fail;
740 }
741 else {
742 // authority-form (RFC7320 #5.3.3)
743 authority = uri;
744 }
745
746 /* Replace header host value */
747 ctx.blk = NULL;
748 while (http_find_header(htx, ist("host"), &ctx, 1)) {
749 if (!http_replace_header_value(htx, &ctx, authority))
750 goto fail;
751 }
752
753 }
754 return 1;
755 fail:
756 return 0;
757}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200758
759/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
760 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
761 * performed over the whole headers. Otherwise it must contain a valid header
762 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
763 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
764 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
765 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
766 * -1. The value fetch stops at commas, so this function is suited for use with
767 * list headers.
768 * The return value is 0 if nothing was found, or non-zero otherwise.
769 */
770unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
771 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
772{
773 struct http_hdr_ctx local_ctx;
774 struct ist val_hist[MAX_HDR_HISTORY];
775 unsigned int hist_idx;
776 int found;
777
778 if (!ctx) {
779 local_ctx.blk = NULL;
780 ctx = &local_ctx;
781 }
782
783 if (occ >= 0) {
784 /* search from the beginning */
785 while (http_find_header(htx, hdr, ctx, 0)) {
786 occ--;
787 if (occ <= 0) {
788 *vptr = ctx->value.ptr;
789 *vlen = ctx->value.len;
790 return 1;
791 }
792 }
793 return 0;
794 }
795
796 /* negative occurrence, we scan all the list then walk back */
797 if (-occ > MAX_HDR_HISTORY)
798 return 0;
799
800 found = hist_idx = 0;
801 while (http_find_header(htx, hdr, ctx, 0)) {
802 val_hist[hist_idx] = ctx->value;
803 if (++hist_idx >= MAX_HDR_HISTORY)
804 hist_idx = 0;
805 found++;
806 }
807 if (-occ > found)
808 return 0;
809
810 /* OK now we have the last occurrence in [hist_idx-1], and we need to
811 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
812 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
813 * to remain in the 0..9 range.
814 */
815 hist_idx += occ + MAX_HDR_HISTORY;
816 if (hist_idx >= MAX_HDR_HISTORY)
817 hist_idx -= MAX_HDR_HISTORY;
818 *vptr = val_hist[hist_idx].ptr;
819 *vlen = val_hist[hist_idx].len;
820 return 1;
821}
822
823/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
824 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
825 * performed over the whole headers. Otherwise it must contain a valid header
826 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
827 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
828 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
829 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
830 * -1. This function differs from http_get_hdr() in that it only returns full
831 * line header values and does not stop at commas.
832 * The return value is 0 if nothing was found, or non-zero otherwise.
833 */
834unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
835 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
836{
837 struct http_hdr_ctx local_ctx;
838 struct ist val_hist[MAX_HDR_HISTORY];
839 unsigned int hist_idx;
840 int found;
841
842 if (!ctx) {
843 local_ctx.blk = NULL;
844 ctx = &local_ctx;
845 }
846
847 if (occ >= 0) {
848 /* search from the beginning */
849 while (http_find_header(htx, hdr, ctx, 1)) {
850 occ--;
851 if (occ <= 0) {
852 *vptr = ctx->value.ptr;
853 *vlen = ctx->value.len;
854 return 1;
855 }
856 }
857 return 0;
858 }
859
860 /* negative occurrence, we scan all the list then walk back */
861 if (-occ > MAX_HDR_HISTORY)
862 return 0;
863
864 found = hist_idx = 0;
865 while (http_find_header(htx, hdr, ctx, 1)) {
866 val_hist[hist_idx] = ctx->value;
867 if (++hist_idx >= MAX_HDR_HISTORY)
868 hist_idx = 0;
869 found++;
870 }
871 if (-occ > found)
872 return 0;
873
874 /* OK now we have the last occurrence in [hist_idx-1], and we need to
875 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
876 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
877 * to remain in the 0..9 range.
878 */
879 hist_idx += occ + MAX_HDR_HISTORY;
880 if (hist_idx >= MAX_HDR_HISTORY)
881 hist_idx -= MAX_HDR_HISTORY;
882 *vptr = val_hist[hist_idx].ptr;
883 *vlen = val_hist[hist_idx].len;
884 return 1;
885}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100886
Christopher Faulet90cc4812019-07-22 16:49:30 +0200887int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100888{
889 struct htx *htx;
890 struct htx_sl *sl;
891 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200892 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100893 union h1_sl h1sl;
894 unsigned int flags = HTX_SL_F_IS_RESP;
895 int ret = 0;
896
Christopher Faulet90cc4812019-07-22 16:49:30 +0200897 b_reset(buf);
898 if (!raw.len) {
899 buf->size = 0;
900 buf->area = malloc(raw.len);
901 return 1;
902 }
903
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100904 buf->size = global.tune.bufsize;
905 buf->area = (char *)malloc(buf->size);
906 if (!buf->area)
907 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100908
909 h1m_init_res(&h1m);
910 h1m.flags |= H1_MF_NO_PHDR;
911 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
912 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
913 if (ret <= 0)
914 goto error;
915
916 if (unlikely(h1sl.st.v.len != 8))
917 goto error;
918 if ((*(h1sl.st.v.ptr + 5) > '1') ||
919 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
920 h1m.flags |= H1_MF_VER_11;
921
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200922 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
923 goto error;
924
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100925 if (h1m.flags & H1_MF_VER_11)
926 flags |= HTX_SL_F_VER_11;
927 if (h1m.flags & H1_MF_XFER_ENC)
928 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200929 if (h1m.flags & H1_MF_CLEN) {
930 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
931 if (h1m.body_len == 0)
932 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100933 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200934 if (h1m.flags & H1_MF_CHNK)
935 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100936
937 htx = htx_from_buf(buf);
938 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
939 if (!sl || !htx_add_all_headers(htx, hdrs))
940 goto error;
941 sl->info.res.status = h1sl.st.status;
942
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200943 while (raw.len > ret) {
944 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
945 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100946 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200947 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100948 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200949
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100950 if (!htx_add_endof(htx, HTX_BLK_EOM))
951 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200952
Christopher Faulet90cc4812019-07-22 16:49:30 +0200953 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100954
955error:
956 if (buf->size)
957 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200958 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100959}
960
Christopher Faulet18630642020-05-12 18:57:28 +0200961void release_http_reply(struct http_reply *http_reply)
962{
963 struct logformat_node *lf, *lfb;
964 struct http_reply_hdr *hdr, *hdrb;
965
966 if (!http_reply)
967 return;
968
969 free(http_reply->ctype);
970 http_reply->ctype = NULL;
971 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
972 LIST_DEL(&hdr->list);
973 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
974 LIST_DEL(&lf->list);
975 release_sample_expr(lf->expr);
976 free(lf->arg);
977 free(lf);
978 }
979 istfree(&hdr->name);
980 free(hdr);
981 }
982
983 if (http_reply->type == HTTP_REPLY_ERRFILES) {
984 free(http_reply->body.http_errors);
985 http_reply->body.http_errors = NULL;
986 }
987 else if (http_reply->type == HTTP_REPLY_RAW)
988 chunk_destroy(&http_reply->body.obj);
989 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
990 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
991 LIST_DEL(&lf->list);
992 release_sample_expr(lf->expr);
993 free(lf->arg);
994 free(lf);
995 }
996 }
997}
998
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100999static int http_htx_init(void)
1000{
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001001 struct buffer chk;
1002 struct ist raw;
1003 int rc;
1004 int err_code = 0;
1005
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001006 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1007 if (!http_err_msgs[rc]) {
1008 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1009 err_code |= ERR_ALERT | ERR_FATAL;
1010 continue;
1011 }
1012
1013 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1014 if (!http_str_to_htx(&chk, raw)) {
1015 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1016 http_err_codes[rc]);
1017 err_code |= ERR_ALERT | ERR_FATAL;
1018 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001019 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001020 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1021 http_err_replies[rc].status = http_err_codes[rc];
1022 http_err_replies[rc].ctype = NULL;
1023 LIST_INIT(&http_err_replies[rc].hdrs);
1024 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001025 }
1026end:
1027 return err_code;
1028}
1029
Christopher Faulet58857752020-01-15 15:19:50 +01001030static void http_htx_deinit(void)
1031{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001032 struct http_errors *http_errs, *http_errsb;
Christopher Faulet5809e102020-05-14 17:31:52 +02001033 struct http_reply *http_rep, *http_repb;
Christopher Faulet58857752020-01-15 15:19:50 +01001034 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001035 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001036 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001037
1038 node = ebpt_first(&http_error_messages);
1039 while (node) {
1040 next = ebpt_next(node);
1041 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001042 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1043 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001044 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001045 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001046 node = next;
1047 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001048
1049 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1050 free(http_errs->conf.file);
1051 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1053 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001054 LIST_DEL(&http_errs->list);
1055 free(http_errs);
1056 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001057
1058 list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) {
1059 LIST_DEL(&http_rep->list);
1060 release_http_reply(http_rep);
1061 }
Christopher Faulet58857752020-01-15 15:19:50 +01001062}
1063
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001064REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001065REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001066
Christopher Faulet58857752020-01-15 15:19:50 +01001067/* Reads content of the error file <file> and convert it into an HTX message. On
1068 * success, the HTX message is returned. On error, NULL is returned and an error
1069 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001070 */
Christopher Faulet58857752020-01-15 15:19:50 +01001071struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001072{
Christopher Faulet58857752020-01-15 15:19:50 +01001073 struct buffer *buf = NULL;
1074 struct buffer chk;
1075 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001076 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001077 struct stat stat;
1078 char *err = NULL;
1079 int errnum, errlen;
1080 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001081
1082 /* already loaded */
1083 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1084 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001085 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1086 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001087 goto out;
1088 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001089
Christopher Faulet58857752020-01-15 15:19:50 +01001090 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001091 fd = open(file, O_RDONLY);
1092 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1093 memprintf(errmsg, "error opening file '%s'.", file);
1094 goto out;
1095 }
1096
1097 if (stat.st_size <= global.tune.bufsize)
1098 errlen = stat.st_size;
1099 else {
1100 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1101 file, global.tune.bufsize);
1102 errlen = global.tune.bufsize;
1103 }
1104
1105 err = malloc(errlen);
1106 if (!err) {
1107 memprintf(errmsg, "out of memory.");
1108 goto out;
1109 }
1110
1111 errnum = read(fd, err, errlen);
1112 if (errnum != errlen) {
1113 memprintf(errmsg, "error reading file '%s'.", file);
1114 goto out;
1115 }
1116
Christopher Faulet58857752020-01-15 15:19:50 +01001117 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001118 http_errmsg = calloc(1, sizeof(*http_errmsg));
1119 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001120 memprintf(errmsg, "out of memory.");
1121 goto out;
1122 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001123 http_errmsg->node.key = strdup(file);
1124 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001125 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001126 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001127 goto out;
1128 }
1129
1130 /* Convert the error file into an HTX message */
1131 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001132 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001133 free(http_errmsg->node.key);
1134 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001135 goto out;
1136 }
1137
Christopher Faulet58857752020-01-15 15:19:50 +01001138 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001139 http_errmsg->msg = chk;
1140 ebis_insert(&http_error_messages, &http_errmsg->node);
1141 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001142
Christopher Faulet5031ef52020-01-15 11:22:07 +01001143 out:
1144 if (fd >= 0)
1145 close(fd);
1146 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001147 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001148}
1149
Ilya Shipitsind4259502020-04-08 01:07:56 +05001150/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001151 * message is returned. On error, NULL is returned and an error message is
1152 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001153 */
Christopher Faulet58857752020-01-15 15:19:50 +01001154struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001155{
Christopher Faulet58857752020-01-15 15:19:50 +01001156 struct buffer *buf = NULL;
1157 struct buffer chk;
1158 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001159 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001160
1161 /* already loaded */
1162 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1163 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001164 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1165 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001166 goto out;
1167 }
1168 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001169 http_errmsg = calloc(1, sizeof(*http_errmsg));
1170 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001171 memprintf(errmsg, "out of memory.");
1172 goto out;
1173 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001174 http_errmsg->node.key = strdup(key);
1175 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001176 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001177 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001178 goto out;
1179 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001180
1181 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001182 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001183 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001184 free(http_errmsg->node.key);
1185 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001186 goto out;
1187 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001188
Christopher Faulet58857752020-01-15 15:19:50 +01001189 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001190 http_errmsg->msg = chk;
1191 ebis_insert(&http_error_messages, &http_errmsg->node);
1192 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001193 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001194 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001195}
1196
Christopher Faulet5031ef52020-01-15 11:22:07 +01001197/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001198 * <status>. It returns NULL if there is any error, otherwise it return the
1199 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001200 */
Christopher Faulet58857752020-01-15 15:19:50 +01001201struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001202{
Christopher Faulet58857752020-01-15 15:19:50 +01001203 struct buffer *buf = NULL;
1204 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001205
1206 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1207 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001208 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001209 break;
1210 }
1211 }
1212
1213 if (rc >= HTTP_ERR_SIZE)
1214 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001215 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001216}
1217
1218/* This function creates HTX error message corresponding to a redirect message
1219 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001220 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1221 * returns NULL if there is any error, otherwise it return the corresponding HTX
1222 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001223 */
Christopher Faulet58857752020-01-15 15:19:50 +01001224struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001225{
Christopher Faulet58857752020-01-15 15:19:50 +01001226 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001227 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001228 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001229 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001230
1231 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1232 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001233 /* Create the error key */
1234 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1235 memprintf(errmsg, "out of memory.");
1236 goto out;
1237 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001238 /* Create the error message */
1239 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1240 errlen = strlen(msg) + strlen(url) + 5;
1241 err = malloc(errlen);
1242 if (!err) {
1243 memprintf(errmsg, "out of memory.");
1244 goto out;
1245 }
1246 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1247
1248 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001249 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001250 break;
1251 }
1252 }
1253
1254 if (rc >= HTTP_ERR_SIZE)
1255 memprintf(errmsg, "status code '%d' not handled.", status);
1256out:
Christopher Faulet58857752020-01-15 15:19:50 +01001257 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001258 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001259 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001260}
1261
Christopher Faulet7eea2412020-05-13 15:02:59 +02001262/* Check an "http reply" and, for replies referencing an http-errors section,
1263 * try to find the right section and the right error message in this section. If
1264 * found, the reply is updated. If the http-errors section exists but the error
1265 * message is not found, no error message is set to fallback on the default
1266 * ones. Otherwise (unknown section) an error is returned.
1267 *
1268 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1269 * filled.
1270 */
1271int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1272{
1273 struct http_errors *http_errs;
1274 int ret = 1;
1275
1276 if (reply->type != HTTP_REPLY_ERRFILES)
1277 goto end;
1278
1279 list_for_each_entry(http_errs, &http_errors_list, list) {
1280 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
1281 reply->type = HTTP_REPLY_ERRMSG;
1282 free(reply->body.http_errors);
1283 reply->body.errmsg = http_errs->errmsg[http_get_status_idx(reply->status)];
1284 if (!reply->body.errmsg)
1285 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1286 "not declared in http-errors section '%s'.\n",
1287 px->id, reply->status, http_errs->id);
1288 break;
1289 }
1290 }
1291
1292 if (&http_errs->list == &http_errors_list) {
1293 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1294 reply->body.http_errors);
1295 ret = 0;
1296 }
1297
1298 end:
1299 return ret;
1300}
1301
Christopher Faulet47e791e2020-05-13 14:36:55 +02001302/* Parse an "http reply". It returns the reply on success or NULL on error. This
1303 * function creates one of the following http replies :
1304 *
1305 * - HTTP_REPLY_EMPTY : dummy response, no payload
1306 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1307 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1308 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1309 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1310 *
1311 * The content-type must be defined for non-empty payload. It is ignored for
1312 * error messages (implicit or explicit). When an http-errors section is
1313 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1314 * during the configuration validity check or dynamically. It is the caller
1315 * responsibility to choose. If no status code is configured, <default_status>
1316 * is set.
1317 */
1318struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1319 int default_status, char **errmsg)
1320{
1321 struct logformat_node *lf, *lfb;
1322 struct http_reply *reply = NULL;
1323 struct http_reply_hdr *hdr, *hdrb;
1324 struct stat stat;
1325 const char *act_arg = NULL;
1326 char *obj = NULL;
1327 int cur_arg, cap, objlen = 0, fd = -1;
1328
1329
1330 reply = calloc(1, sizeof(*reply));
1331 if (!reply) {
1332 memprintf(errmsg, "out of memory");
1333 goto error;
1334 }
1335 LIST_INIT(&reply->hdrs);
1336 reply->type = HTTP_REPLY_EMPTY;
1337 reply->status = default_status;
1338
1339 cap = ((px->conf.args.ctx == ARGC_HRQ)
1340 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1341 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
1342
1343 cur_arg = *orig_arg;
1344 while (*args[cur_arg]) {
1345 if (strcmp(args[cur_arg], "status") == 0) {
1346 cur_arg++;
1347 if (!*args[cur_arg]) {
1348 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1349 goto error;
1350 }
1351 reply->status = atol(args[cur_arg]);
1352 if (reply->status < 200 || reply->status > 599) {
1353 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1354 goto error;
1355 }
1356 cur_arg++;
1357 }
1358 else if (strcmp(args[cur_arg], "content-type") == 0) {
1359 cur_arg++;
1360 if (!*args[cur_arg]) {
1361 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1362 goto error;
1363 }
1364 free(reply->ctype);
1365 reply->ctype = strdup(args[cur_arg]);
1366 cur_arg++;
1367 }
1368 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1369 if (reply->type != HTTP_REPLY_EMPTY) {
1370 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1371 goto error;
1372 }
1373 act_arg = args[cur_arg];
1374 cur_arg++;
1375 if (!*args[cur_arg]) {
1376 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1377 goto error;
1378 }
1379 reply->body.http_errors = strdup(args[cur_arg]);
1380 if (!reply->body.http_errors) {
1381 memprintf(errmsg, "out of memory");
1382 goto error;
1383 }
1384 reply->type = HTTP_REPLY_ERRFILES;
1385 cur_arg++;
1386 }
1387 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1388 if (reply->type != HTTP_REPLY_EMPTY) {
1389 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1390 goto error;
1391 }
1392 act_arg = args[cur_arg];
1393 reply->type = HTTP_REPLY_ERRMSG;
1394 cur_arg++;
1395 }
1396 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1397 if (reply->type != HTTP_REPLY_EMPTY) {
1398 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1399 goto error;
1400 }
1401 act_arg = args[cur_arg];
1402 cur_arg++;
1403 if (!*args[cur_arg]) {
1404 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1405 goto error;
1406 }
1407 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1408 if (!reply->body.errmsg) {
1409 goto error;
1410 }
1411 reply->type = HTTP_REPLY_ERRMSG;
1412 cur_arg++;
1413 }
1414 else if (strcmp(args[cur_arg], "file") == 0) {
1415 if (reply->type != HTTP_REPLY_EMPTY) {
1416 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1417 goto error;
1418 }
1419 act_arg = args[cur_arg];
1420 cur_arg++;
1421 if (!*args[cur_arg]) {
1422 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1423 goto error;
1424 }
1425 fd = open(args[cur_arg], O_RDONLY);
1426 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1427 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1428 goto error;
1429 }
1430 if (stat.st_size > global.tune.bufsize) {
1431 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1432 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1433 goto error;
1434 }
1435 objlen = stat.st_size;
1436 obj = malloc(objlen);
1437 if (!obj || read(fd, obj, objlen) != objlen) {
1438 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1439 goto error;
1440 }
1441 close(fd);
1442 fd = -1;
1443 reply->type = HTTP_REPLY_RAW;
1444 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1445 obj = NULL;
1446 cur_arg++;
1447 }
1448 else if (strcmp(args[cur_arg], "string") == 0) {
1449 if (reply->type != HTTP_REPLY_EMPTY) {
1450 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1451 goto error;
1452 }
1453 act_arg = args[cur_arg];
1454 cur_arg++;
1455 if (!*args[cur_arg]) {
1456 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1457 goto error;
1458 }
1459 obj = strdup(args[cur_arg]);
1460 objlen = strlen(args[cur_arg]);
1461 if (!obj) {
1462 memprintf(errmsg, "out of memory");
1463 goto error;
1464 }
1465 reply->type = HTTP_REPLY_RAW;
1466 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1467 obj = NULL;
1468 cur_arg++;
1469 }
1470 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1471 if (reply->type != HTTP_REPLY_EMPTY) {
1472 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1473 goto error;
1474 }
1475 act_arg = args[cur_arg];
1476 cur_arg++;
1477 if (!*args[cur_arg]) {
1478 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1479 goto error;
1480 }
1481 fd = open(args[cur_arg], O_RDONLY);
1482 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1483 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1484 goto error;
1485 }
1486 if (stat.st_size > global.tune.bufsize) {
1487 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1488 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1489 goto error;
1490 }
1491 objlen = stat.st_size;
1492 obj = malloc(objlen + 1);
1493 if (!obj || read(fd, obj, objlen) != objlen) {
1494 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1495 goto error;
1496 }
1497 close(fd);
1498 fd = -1;
1499 obj[objlen] = '\0';
1500 reply->type = HTTP_REPLY_LOGFMT;
1501 cur_arg++;
1502 }
1503 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1504 if (reply->type != HTTP_REPLY_EMPTY) {
1505 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1506 goto error;
1507 }
1508 act_arg = args[cur_arg];
1509 cur_arg++;
1510 if (!*args[cur_arg]) {
1511 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1512 goto error;
1513 }
1514 obj = strdup(args[cur_arg]);
1515 objlen = strlen(args[cur_arg]);
1516 reply->type = HTTP_REPLY_LOGFMT;
1517 cur_arg++;
1518 }
1519 else if (strcmp(args[cur_arg], "hdr") == 0) {
1520 cur_arg++;
1521 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1522 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1523 goto error;
1524 }
1525 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1526 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1527 strcasecmp(args[cur_arg], "content-type") == 0) {
1528 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1529 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1530 cur_arg += 2;
1531 continue;
1532 }
1533 hdr = calloc(1, sizeof(*hdr));
1534 if (!hdr) {
1535 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1536 goto error;
1537 }
1538 LIST_INIT(&hdr->value);
1539 hdr->name = ist(strdup(args[cur_arg]));
1540 if (!isttest(hdr->name)) {
1541 memprintf(errmsg, "out of memory");
1542 goto error;
1543 }
1544 LIST_ADDQ(&reply->hdrs, &hdr->list);
1545 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1546 goto error;
1547
1548 free(px->conf.lfs_file);
1549 px->conf.lfs_file = strdup(px->conf.args.file);
1550 px->conf.lfs_line = px->conf.args.line;
1551 cur_arg += 2;
1552 }
1553 else
1554 break;
1555 }
1556
1557 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1558 if (reply->ctype) {
1559 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1560 " neither errorfile nor payload defined.\n",
1561 px->conf.args.file, px->conf.args.line, reply->ctype);
1562 free(reply->ctype);
1563 reply->ctype = NULL;
1564 }
1565 }
1566 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1567
1568 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1569 /* default errorfile or errorfiles: check the status */
1570 int rc;
1571
1572 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1573 if (http_err_codes[rc] == reply->status)
1574 break;
1575 }
1576
1577 if (rc >= HTTP_ERR_SIZE) {
1578 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1579 reply->status, act_arg);
1580 goto error;
1581 }
1582 }
1583
1584 if (reply->ctype) {
1585 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1586 "with an erorrfile.\n",
1587 px->conf.args.file, px->conf.args.line, reply->ctype);
1588 free(reply->ctype);
1589 reply->ctype = NULL;
1590 }
1591 if (!LIST_ISEMPTY(&reply->hdrs)) {
1592 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1593 "with an erorrfile.\n",
1594 px->conf.args.file, px->conf.args.line);
1595 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1596 LIST_DEL(&hdr->list);
1597 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1598 LIST_DEL(&lf->list);
1599 release_sample_expr(lf->expr);
1600 free(lf->arg);
1601 free(lf);
1602 }
1603 istfree(&hdr->name);
1604 free(hdr);
1605 }
1606 }
1607 }
1608 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1609 if (!reply->ctype && objlen) {
1610 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1611 goto error;
1612 }
1613 if (reply->ctype && !b_data(&reply->body.obj)) {
1614 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1615 "with an emtpy payload.\n",
1616 px->conf.args.file, px->conf.args.line, reply->ctype);
1617 free(reply->ctype);
1618 reply->ctype = NULL;
1619 }
1620 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1621 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1622 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1623 px->conf.args.file, px->conf.args.line);
1624 }
1625 }
1626 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1627 LIST_INIT(&reply->body.fmt);
1628 if (!reply->ctype) {
1629 memprintf(errmsg, "a content type must be defined with a log-format payload");
1630 goto error;
1631 }
1632 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1633 goto error;
1634
1635 free(px->conf.lfs_file);
1636 px->conf.lfs_file = strdup(px->conf.args.file);
1637 px->conf.lfs_line = px->conf.args.line;
1638 }
1639
1640 free(obj);
1641 *orig_arg = cur_arg;
1642 return reply;
1643
1644 error:
1645 free(obj);
1646 if (fd >= 0)
1647 close(fd);
1648 release_http_reply(reply);
1649 return NULL;
1650}
1651
Christopher Faulet07f41f72020-01-16 16:16:06 +01001652/* Parses the "errorloc[302|303]" proxy keyword */
1653static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1654 struct proxy *defpx, const char *file, int line,
1655 char **errmsg)
1656{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001657 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001658 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001659 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001660 int errloc, status;
1661 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001662
1663 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1664 ret = 1;
1665 goto out;
1666 }
1667
1668 if (*(args[1]) == 0 || *(args[2]) == 0) {
1669 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1670 ret = -1;
1671 goto out;
1672 }
1673
1674 status = atol(args[1]);
1675 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1676 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1677 if (!msg) {
1678 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1679 ret = -1;
1680 goto out;
1681 }
Christopher Faulet5809e102020-05-14 17:31:52 +02001682
1683 reply = calloc(1, sizeof(*reply));
1684 if (!reply) {
1685 memprintf(errmsg, "%s : out of memory.", args[0]);
1686 ret = -1;
1687 goto out;
1688 }
1689 reply->type = HTTP_REPLY_ERRMSG;
1690 reply->status = status;
1691 reply->ctype = NULL;
1692 LIST_INIT(&reply->hdrs);
1693 reply->body.errmsg = msg;
1694 LIST_ADDQ(&http_replies_list, &reply->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001695
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001696 conf_err = calloc(1, sizeof(*conf_err));
1697 if (!conf_err) {
1698 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001699 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001700 ret = -1;
1701 goto out;
1702 }
1703 conf_err->type = 1;
1704 conf_err->info.errorfile.status = status;
1705 conf_err->info.errorfile.msg = msg;
Christopher Faulet5809e102020-05-14 17:31:52 +02001706 conf_err->info.errorfile.reply = reply;
1707
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001708 conf_err->file = strdup(file);
1709 conf_err->line = line;
1710 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001711
1712 out:
1713 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001714
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001715}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001716
1717/* Parses the "errorfile" proxy keyword */
1718static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1719 struct proxy *defpx, const char *file, int line,
1720 char **errmsg)
1721{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001722 struct conf_errors *conf_err;
Christopher Faulet5809e102020-05-14 17:31:52 +02001723 struct http_reply *reply;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001724 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001725 int status;
1726 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001727
1728 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1729 ret = 1;
1730 goto out;
1731 }
1732
1733 if (*(args[1]) == 0 || *(args[2]) == 0) {
1734 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1735 ret = -1;
1736 goto out;
1737 }
1738
1739 status = atol(args[1]);
1740 msg = http_parse_errorfile(status, args[2], errmsg);
1741 if (!msg) {
1742 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1743 ret = -1;
1744 goto out;
1745 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001746
Christopher Faulet5809e102020-05-14 17:31:52 +02001747 reply = calloc(1, sizeof(*reply));
1748 if (!reply) {
1749 memprintf(errmsg, "%s : out of memory.", args[0]);
1750 ret = -1;
1751 goto out;
1752 }
1753 reply->type = HTTP_REPLY_ERRMSG;
1754 reply->status = status;
1755 reply->ctype = NULL;
1756 LIST_INIT(&reply->hdrs);
1757 reply->body.errmsg = msg;
1758 LIST_ADDQ(&http_replies_list, &reply->list);
1759
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001760 conf_err = calloc(1, sizeof(*conf_err));
1761 if (!conf_err) {
1762 memprintf(errmsg, "%s : out of memory.", args[0]);
Christopher Faulet5809e102020-05-14 17:31:52 +02001763 free(reply);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001764 ret = -1;
1765 goto out;
1766 }
1767 conf_err->type = 1;
1768 conf_err->info.errorfile.status = status;
1769 conf_err->info.errorfile.msg = msg;
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
1843/* Check "errorfiles" proxy keyword */
1844static int proxy_check_errors(struct proxy *px)
1845{
1846 struct conf_errors *conf_err, *conf_err_back;
1847 struct http_errors *http_errs;
1848 int rc, err = 0;
1849
1850 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1851 if (conf_err->type == 1) {
1852 /* errorfile */
1853 rc = http_get_status_idx(conf_err->info.errorfile.status);
1854 px->errmsg[rc] = conf_err->info.errorfile.msg;
1855 }
1856 else {
1857 /* errorfiles */
1858 list_for_each_entry(http_errs, &http_errors_list, list) {
1859 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1860 break;
1861 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001862
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001863 /* unknown http-errors section */
1864 if (&http_errs->list == &http_errors_list) {
1865 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1866 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1867 err |= ERR_ALERT | ERR_FATAL;
1868 free(conf_err->info.errorfiles.name);
1869 goto next;
1870 }
1871
1872 free(conf_err->info.errorfiles.name);
1873 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1874 if (conf_err->info.errorfiles.status[rc] > 0) {
1875 if (http_errs->errmsg[rc])
1876 px->errmsg[rc] = http_errs->errmsg[rc];
1877 else if (conf_err->info.errorfiles.status[rc] == 2)
1878 ha_warning("config: proxy '%s' : status '%d' not declared in"
1879 " http-errors section '%s' (at %s:%d).\n",
1880 px->id, http_err_codes[rc], http_errs->id,
1881 conf_err->file, conf_err->line);
1882 }
1883 }
1884 }
1885 next:
1886 LIST_DEL(&conf_err->list);
1887 free(conf_err->file);
1888 free(conf_err);
1889 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001890
1891 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001892 return err;
1893}
1894
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001895static int post_check_errors()
1896{
1897 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001898 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001899 struct htx *htx;
1900 int err_code = 0;
1901
1902 node = ebpt_first(&http_error_messages);
1903 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001904 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1905 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001906 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001907 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001908 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1909 ha_warning("config: errorfile '%s' runs over the buffer space"
1910 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001911 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001912 (char *)node->key);
1913 err_code |= ERR_WARN;
1914 }
1915 next:
1916 node = ebpt_next(node);
1917 }
1918
1919 return err_code;
1920}
1921
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001922int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1923{
1924 struct conf_errors *conf_err, *new_conf_err = NULL;
1925 int ret = 0;
1926
1927 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1928 new_conf_err = calloc(1, sizeof(*new_conf_err));
1929 if (!new_conf_err) {
1930 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1931 goto out;
1932 }
1933 new_conf_err->type = conf_err->type;
1934 if (conf_err->type == 1) {
1935 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1936 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1937 }
1938 else {
1939 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1940 if (!new_conf_err->info.errorfiles.name) {
1941 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1942 goto out;
1943 }
1944 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1945 sizeof(conf_err->info.errorfiles.status));
1946 }
1947 new_conf_err->file = strdup(conf_err->file);
1948 new_conf_err->line = conf_err->line;
1949 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1950 new_conf_err = NULL;
1951 }
1952 ret = 1;
1953
1954 out:
1955 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001956 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001957}
1958
1959void proxy_release_conf_errors(struct proxy *px)
1960{
1961 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001962
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001963 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1964 if (conf_err->type == 0)
1965 free(conf_err->info.errorfiles.name);
1966 LIST_DEL(&conf_err->list);
1967 free(conf_err->file);
1968 free(conf_err);
1969 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001970}
1971
1972/*
1973 * Parse an <http-errors> section.
1974 * Returns the error code, 0 if OK, or any combination of :
1975 * - ERR_ABORT: must abort ASAP
1976 * - ERR_FATAL: we can continue parsing but not start the service
1977 * - ERR_WARN: a warning has been emitted
1978 * - ERR_ALERT: an alert has been emitted
1979 * Only the two first ones can stop processing, the two others are just
1980 * indicators.
1981 */
1982static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1983{
1984 static struct http_errors *curr_errs = NULL;
1985 int err_code = 0;
1986 const char *err;
1987 char *errmsg = NULL;
1988
1989 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1990 if (!*args[1]) {
1991 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1992 err_code |= ERR_ALERT | ERR_ABORT;
1993 goto out;
1994 }
1995
1996 err = invalid_char(args[1]);
1997 if (err) {
1998 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1999 file, linenum, *err, args[0], args[1]);
2000 err_code |= ERR_ALERT | ERR_FATAL;
2001 }
2002
2003 list_for_each_entry(curr_errs, &http_errors_list, list) {
2004 /* Error if two errors section owns the same name */
2005 if (strcmp(curr_errs->id, args[1]) == 0) {
2006 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
2007 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
2008 err_code |= ERR_ALERT | ERR_FATAL;
2009 }
2010 }
2011
2012 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
2013 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2014 err_code |= ERR_ALERT | ERR_ABORT;
2015 goto out;
2016 }
2017
2018 LIST_ADDQ(&http_errors_list, &curr_errs->list);
2019 curr_errs->id = strdup(args[1]);
2020 curr_errs->conf.file = strdup(file);
2021 curr_errs->conf.line = linenum;
2022 }
2023 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02002024 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002025 struct buffer *msg;
2026 int status, rc;
2027
2028 if (*(args[1]) == 0 || *(args[2]) == 0) {
2029 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
2030 file, linenum, args[0]);
2031 err_code |= ERR_ALERT | ERR_FATAL;
2032 goto out;
2033 }
2034
2035 status = atol(args[1]);
2036 msg = http_parse_errorfile(status, args[2], &errmsg);
2037 if (!msg) {
2038 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
2039 err_code |= ERR_ALERT | ERR_FATAL;
2040 goto out;
2041 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002042
2043 reply = calloc(1, sizeof(*reply));
2044 if (!reply) {
2045 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2046 err_code |= ERR_ALERT | ERR_FATAL;
2047 goto out;
2048 }
2049 reply->type = HTTP_REPLY_ERRMSG;
2050 reply->status = status;
2051 reply->ctype = NULL;
2052 LIST_INIT(&reply->hdrs);
2053 reply->body.errmsg = msg;
2054
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002055 rc = http_get_status_idx(status);
2056 curr_errs->errmsg[rc] = msg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02002057 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002058 }
2059 else if (*args[0] != 0) {
2060 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2061 err_code |= ERR_ALERT | ERR_FATAL;
2062 goto out;
2063 }
2064
2065out:
2066 free(errmsg);
2067 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002068}
2069
2070static struct cfg_kw_list cfg_kws = {ILH, {
2071 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2072 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2073 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2074 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002075 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002076 { 0, NULL, NULL },
2077}};
2078
2079INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002080REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002081REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002082
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002083REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2084
Christopher Faulet29f72842019-12-11 15:52:32 +01002085/************************************************************************/
2086/* HTX sample fetches */
2087/************************************************************************/
2088
2089/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2090static int
2091smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2092{
2093 if (!smp->strm)
2094 return 0;
2095
2096 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2097 smp->data.type = SMP_T_BOOL;
2098 return 1;
2099}
2100
2101/* Returns the number of blocks in an HTX message. The channel is chosen
2102 * depending on the sample direction. */
2103static int
2104smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2105{
2106 struct channel *chn;
2107 struct htx *htx;
2108
2109 if (!smp->strm)
2110 return 0;
2111
2112 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002113 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002114 if (!htx)
2115 return 0;
2116
2117 smp->data.u.sint = htx_nbblks(htx);
2118 smp->data.type = SMP_T_SINT;
2119 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2120 return 1;
2121}
2122
2123/* Returns the size of an HTX message. The channel is chosen depending on the
2124 * sample direction. */
2125static int
2126smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2127{
2128 struct channel *chn;
2129 struct htx *htx;
2130
2131 if (!smp->strm)
2132 return 0;
2133
2134 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002135 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002136 if (!htx)
2137 return 0;
2138
2139 smp->data.u.sint = htx->size;
2140 smp->data.type = SMP_T_SINT;
2141 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2142 return 1;
2143}
2144
2145/* Returns the data size of an HTX message. The channel is chosen depending on the
2146 * sample direction. */
2147static int
2148smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2149{
2150 struct channel *chn;
2151 struct htx *htx;
2152
2153 if (!smp->strm)
2154 return 0;
2155
2156 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002157 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002158 if (!htx)
2159 return 0;
2160
2161 smp->data.u.sint = htx->data;
2162 smp->data.type = SMP_T_SINT;
2163 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2164 return 1;
2165}
2166
2167/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2168 * depending on the sample direction. */
2169static int
2170smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2171{
2172 struct channel *chn;
2173 struct htx *htx;
2174
2175 if (!smp->strm)
2176 return 0;
2177
2178 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002179 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002180 if (!htx)
2181 return 0;
2182
2183 smp->data.u.sint = htx_used_space(htx);
2184 smp->data.type = SMP_T_SINT;
2185 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2186 return 1;
2187}
2188
2189/* Returns the free space (size-used) of an HTX message. The channel is chosen
2190 * depending on the sample direction. */
2191static int
2192smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2193{
2194 struct channel *chn;
2195 struct htx *htx;
2196
2197 if (!smp->strm)
2198 return 0;
2199
2200 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002201 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002202 if (!htx)
2203 return 0;
2204
2205 smp->data.u.sint = htx_free_space(htx);
2206 smp->data.type = SMP_T_SINT;
2207 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2208 return 1;
2209}
2210
2211/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2212 * channel is chosen depending on the sample direction. */
2213static int
2214smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2215{
2216 struct channel *chn;
2217 struct htx *htx;
2218
2219 if (!smp->strm)
2220 return 0;
2221
2222 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002223 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002224 if (!htx)
2225 return 0;
2226
2227 smp->data.u.sint = htx_free_data_space(htx);
2228 smp->data.type = SMP_T_SINT;
2229 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2230 return 1;
2231}
2232
2233/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2234 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2235 * the sample direction. */
2236static int
2237smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2238{
2239 struct channel *chn;
2240 struct htx *htx;
2241
2242 if (!smp->strm)
2243 return 0;
2244
2245 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002246 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002247 if (!htx)
2248 return 0;
2249
2250 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2251 smp->data.type = SMP_T_BOOL;
2252 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2253 return 1;
2254}
2255
2256/* Returns the type of a specific HTX block, if found in the message. Otherwise
2257 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2258 * "head", "tail" or "first". The channel is chosen depending on the sample
2259 * direction. */
2260static int
2261smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2262{
2263 struct channel *chn;
2264 struct htx *htx;
2265 enum htx_blk_type type;
2266 int32_t pos;
2267
2268 if (!smp->strm || !arg_p)
2269 return 0;
2270
2271 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002272 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002273 if (!htx)
2274 return 0;
2275
2276 pos = arg_p[0].data.sint;
2277 if (pos == -1)
2278 type = htx_get_head_type(htx);
2279 else if (pos == -2)
2280 type = htx_get_tail_type(htx);
2281 else if (pos == -3)
2282 type = htx_get_first_type(htx);
2283 else
2284 type = ((pos >= htx->head && pos <= htx->tail)
2285 ? htx_get_blk_type(htx_get_blk(htx, pos))
2286 : HTX_BLK_UNUSED);
2287
2288 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2289 smp->data.type = SMP_T_STR;
2290 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2291 return 1;
2292}
2293
2294/* Returns the size of a specific HTX block, if found in the message. Otherwise
2295 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2296 * "first". The channel is chosen depending on the sample direction. */
2297static int
2298smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2299{
2300 struct channel *chn;
2301 struct htx *htx;
2302 struct htx_blk *blk;
2303 int32_t pos;
2304
2305 if (!smp->strm || !arg_p)
2306 return 0;
2307
2308 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002309 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002310 if (!htx)
2311 return 0;
2312
2313 pos = arg_p[0].data.sint;
2314 if (pos == -1)
2315 blk = htx_get_head_blk(htx);
2316 else if (pos == -2)
2317 blk = htx_get_tail_blk(htx);
2318 else if (pos == -3)
2319 blk = htx_get_first_blk(htx);
2320 else
2321 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2322
2323 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2324 smp->data.type = SMP_T_SINT;
2325 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2326 return 1;
2327}
2328
2329/* Returns the start-line if the selected HTX block exists and is a
2330 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2331 * supported or "head", "tail" or "first". The channel is chosen depending on
2332 * the sample direction. */
2333static int
2334smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2335{
2336 struct buffer *temp;
2337 struct channel *chn;
2338 struct htx *htx;
2339 struct htx_blk *blk;
2340 struct htx_sl *sl;
2341 int32_t pos;
2342
2343 if (!smp->strm || !arg_p)
2344 return 0;
2345
2346 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002347 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002348 if (!htx)
2349 return 0;
2350
2351 pos = arg_p[0].data.sint;
2352 if (pos == -1)
2353 blk = htx_get_head_blk(htx);
2354 else if (pos == -2)
2355 blk = htx_get_tail_blk(htx);
2356 else if (pos == -3)
2357 blk = htx_get_first_blk(htx);
2358 else
2359 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2360
2361 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2362 smp->data.u.str.size = 0;
2363 smp->data.u.str.area = "";
2364 smp->data.u.str.data = 0;
2365 }
2366 else {
2367 sl = htx_get_blk_ptr(htx, blk);
2368
2369 temp = get_trash_chunk();
2370 chunk_istcat(temp, htx_sl_p1(sl));
2371 temp->area[temp->data++] = ' ';
2372 chunk_istcat(temp, htx_sl_p2(sl));
2373 temp->area[temp->data++] = ' ';
2374 chunk_istcat(temp, htx_sl_p3(sl));
2375
2376 smp->data.u.str = *temp;
2377 }
2378
2379 smp->data.type = SMP_T_STR;
2380 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2381 return 1;
2382}
2383
2384/* Returns the header name if the selected HTX block exists and is a header or a
2385 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2386 * supported or "head", "tail" or "first". The channel is chosen depending on
2387 * the sample direction. */
2388static int
2389smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2390{
2391 struct channel *chn;
2392 struct htx *htx;
2393 struct htx_blk *blk;
2394 int32_t pos;
2395
2396 if (!smp->strm || !arg_p)
2397 return 0;
2398
2399 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002400 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002401 if (!htx)
2402 return 0;
2403
2404 pos = arg_p[0].data.sint;
2405 if (pos == -1)
2406 blk = htx_get_head_blk(htx);
2407 else if (pos == -2)
2408 blk = htx_get_tail_blk(htx);
2409 else if (pos == -3)
2410 blk = htx_get_first_blk(htx);
2411 else
2412 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2413
2414 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2415 smp->data.u.str.size = 0;
2416 smp->data.u.str.area = "";
2417 smp->data.u.str.data = 0;
2418 }
2419 else {
2420 struct ist name = htx_get_blk_name(htx, blk);
2421
2422 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2423 }
2424 smp->data.type = SMP_T_STR;
2425 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2426 return 1;
2427}
2428
2429/* Returns the header value if the selected HTX block exists and is a header or
2430 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2431 * supported or "head", "tail" or "first". The channel is chosen depending on
2432 * the sample direction. */
2433static int
2434smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2435{
2436 struct channel *chn;
2437 struct htx *htx;
2438 struct htx_blk *blk;
2439 int32_t pos;
2440
2441 if (!smp->strm || !arg_p)
2442 return 0;
2443
2444 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002445 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002446 if (!htx)
2447 return 0;
2448
2449 pos = arg_p[0].data.sint;
2450 if (pos == -1)
2451 blk = htx_get_head_blk(htx);
2452 else if (pos == -2)
2453 blk = htx_get_tail_blk(htx);
2454 else if (pos == -3)
2455 blk = htx_get_first_blk(htx);
2456 else
2457 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2458
2459 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2460 smp->data.u.str.size = 0;
2461 smp->data.u.str.area = "";
2462 smp->data.u.str.data = 0;
2463 }
2464 else {
2465 struct ist val = htx_get_blk_value(htx, blk);
2466
2467 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2468 }
2469 smp->data.type = SMP_T_STR;
2470 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2471 return 1;
2472}
2473
2474/* Returns the value if the selected HTX block exists and is a data
2475 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2476 * or "head", "tail" or "first". The channel is chosen depending on the sample
2477 * direction. */
2478static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002479smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002480{
2481 struct channel *chn;
2482 struct htx *htx;
2483 struct htx_blk *blk;
2484 int32_t pos;
2485
2486 if (!smp->strm || !arg_p)
2487 return 0;
2488
2489 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002490 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002491 if (!htx)
2492 return 0;
2493
2494 pos = arg_p[0].data.sint;
2495 if (pos == -1)
2496 blk = htx_get_head_blk(htx);
2497 else if (pos == -2)
2498 blk = htx_get_tail_blk(htx);
2499 else if (pos == -3)
2500 blk = htx_get_first_blk(htx);
2501 else
2502 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2503
2504 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2505 smp->data.u.str.size = 0;
2506 smp->data.u.str.area = "";
2507 smp->data.u.str.data = 0;
2508 }
2509 else {
2510 struct ist val = htx_get_blk_value(htx, blk);
2511
2512 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2513 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002514 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002515 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2516 return 1;
2517}
2518
2519/* This function is used to validate the arguments passed to any "htx_blk" fetch
2520 * keywords. An argument is expected by these keywords. It must be a positive
2521 * integer or on of the following strings: "head", "tail" or "first". It returns
2522 * 0 on error, and a non-zero value if OK.
2523 */
2524int val_blk_arg(struct arg *arg, char **err_msg)
2525{
2526 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2527 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2528 return 0;
2529 }
2530 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2531 free(arg[0].data.str.area);
2532 arg[0].type = ARGT_SINT;
2533 arg[0].data.sint = -1;
2534 }
2535 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2536 free(arg[0].data.str.area);
2537 arg[0].type = ARGT_SINT;
2538 arg[0].data.sint = -2;
2539 }
2540 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2541 free(arg[0].data.str.area);
2542 arg[0].type = ARGT_SINT;
2543 arg[0].data.sint = -3;
2544 }
2545 else {
2546 int pos;
2547
2548 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002549 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002550 memprintf(err_msg, "invalid block position");
2551 return 0;
2552 }
2553 }
2554
2555 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2556 if (pos < 0) {
2557 memprintf(err_msg, "block position must not be negative");
2558 return 0;
2559 }
2560 free(arg[0].data.str.area);
2561 arg[0].type = ARGT_SINT;
2562 arg[0].data.sint = pos;
2563 }
2564
2565 return 1;
2566}
2567
2568
2569/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002570 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002571 */
2572static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002573 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002574
Christopher Faulet01f44452020-01-08 14:23:40 +01002575 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2576 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2577 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2578 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2579 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2580 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2581 { "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 +01002582
Christopher Faulet01f44452020-01-08 14:23:40 +01002583 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2584 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2585 { "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},
2586 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2587 { "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 +01002588 { "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 +01002589
2590 { /* END */ },
2591}};
2592
2593INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);