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