blob: 7c90a96081bf53718de01b9d549befa42d1bc4ea [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 Fauleta7b677c2018-11-29 16:48:49 +010036
Christopher Faulet76edc0f2020-01-13 15:52:01 +010037/* The declaration of an errorfiles/errorfile directives. Used during config
38 * parsing only. */
39struct conf_errors {
40 char type; /* directive type (0: errorfiles, 1: errorfile) */
41 union {
42 struct {
43 int status; /* the status code associated to this error */
44 struct buffer *msg; /* the HTX error message */
45 } errorfile; /* describe an "errorfile" directive */
46 struct {
47 char *name; /* the http-errors section name */
48 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
49 } errorfiles; /* describe an "errorfiles" directive */
50 } info;
51
52 char *file; /* file where the directive appears */
53 int line; /* line where the directive appears */
54
55 struct list list; /* next conf_errors */
56};
57
Christopher Faulet297fbb42019-05-13 14:41:27 +020058/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020059 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020060 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020061 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020062struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020063{
Christopher Faulet297fbb42019-05-13 14:41:27 +020064 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010065
Christopher Faulet29f17582019-05-23 11:03:26 +020066 BUG_ON(htx->first == -1);
67 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020068 if (!blk)
69 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020070 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 +020071 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020072}
73
Christopher Faulet727a3f12020-02-07 16:39:41 +010074/* Returns the headers size in the HTX message */
75size_t http_get_hdrs_size(struct htx *htx)
76{
77 struct htx_blk *blk;
78 size_t sz = 0;
79
80 blk = htx_get_first_blk(htx);
81 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
82 return sz;
83
84 for (; blk; blk = htx_get_next_blk(htx, blk)) {
85 sz += htx_get_blksz(blk);
86 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
87 break;
88 }
89 return sz;
90}
91
Christopher Faulet8dd33e12020-05-05 07:42:42 +020092/* Finds the first or next occurrence of header matching <pattern> in the HTX
93 * message <htx> using the context <ctx>. This structure holds everything
94 * necessary to use the header and find next occurrence. If its <blk> member is
95 * NULL, the header is searched from the beginning. Otherwise, the next
96 * occurrence is returned. The function returns 1 when it finds a value, and 0
97 * when there is no more. It is designed to work with headers defined as
98 * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on
99 * full-line headers in whose comma is not a delimiter but is part of the
100 * syntax. A special case, if ctx->value is NULL when searching for a new values
101 * of a header, the current header is rescanned. This allows rescanning after a
102 * header deletion.
103 *
104 * The matching method is chosen by checking the flags :
105 *
106 * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching
107 * the regex are evaluated.
108 * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal
109 * to the string are evaluated.
110 * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names
111 * starting by the string are evaluated.
112 * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names
113 * ending by the string are evaluated.
114 * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names
115 * containing the string are evaluated.
Christopher Faulet47596d32018-10-22 09:17:28 +0200116 */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200117
118#define HTTP_FIND_FL_MATCH_STR 0x0001
119#define HTTP_FIND_FL_MATCH_PFX 0x0002
120#define HTTP_FIND_FL_MATCH_SFX 0x0003
121#define HTTP_FIND_FL_MATCH_SUB 0x0004
122#define HTTP_FIND_FL_MATCH_REG 0x0005
123/* 0x0006..0x000f: for other matching methods */
124#define HTTP_FIND_FL_MATCH_TYPE 0x000F
125#define HTTP_FIND_FL_FULL 0x0010
126
127static 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 +0200128{
129 struct htx_blk *blk = ctx->blk;
130 struct ist n, v;
131 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200132
133 if (blk) {
134 char *p;
135
Tim Duesterhused526372020-03-05 17:56:33 +0100136 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200137 goto rescan_hdr;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200138 if (flags & HTTP_FIND_FL_FULL)
Christopher Faulet47596d32018-10-22 09:17:28 +0200139 goto next_blk;
140 v = htx_get_blk_value(htx, blk);
141 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
142 v.len -= (p - v.ptr);
143 v.ptr = p;
144 if (!v.len)
145 goto next_blk;
146 /* Skip comma */
147 if (*(v.ptr) == ',') {
148 v.ptr++;
149 v.len--;
150 }
151
152 goto return_hdr;
153 }
154
Christopher Faulet192c6a22019-06-11 16:32:24 +0200155 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200156 return 0;
157
Christopher Fauleta3f15502019-05-13 15:27:23 +0200158 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200159 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100161 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
162 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200163 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200164 continue;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200165
166 if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) {
167 const struct my_regex *re = pattern;
168
169 n = htx_get_blk_name(htx, blk);
170 if (!regex_exec2(re, n.ptr, n.len))
171 goto next_blk;
172 }
173 else {
174 const struct ist name = *(const struct ist *)(pattern);
175
Christopher Faulet47596d32018-10-22 09:17:28 +0200176 /* If no name was passed, we want any header. So skip the comparison */
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200177 if (!istlen(name))
178 goto match;
179
Christopher Faulet47596d32018-10-22 09:17:28 +0200180 n = htx_get_blk_name(htx, blk);
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200181 switch (flags & HTTP_FIND_FL_MATCH_TYPE) {
182 case HTTP_FIND_FL_MATCH_STR:
183 if (!isteqi(n, name))
184 goto next_blk;
185 break;
186 case HTTP_FIND_FL_MATCH_PFX:
187 if (istlen(n) < istlen(name))
188 goto next_blk;
189
190 n = ist2(istptr(n), istlen(name));
191 if (!isteqi(n, name))
192 goto next_blk;
193 break;
194 case HTTP_FIND_FL_MATCH_SFX:
195 if (istlen(n) < istlen(name))
196 goto next_blk;
197
198 n = ist2(istptr(n) + istlen(n) - istlen(name), istlen(name));
199 if (!isteqi(n, name))
200 goto next_blk;
201 break;
202 case HTTP_FIND_FL_MATCH_SUB:
203 if (strnistr(n.ptr, n.len, name.ptr, n.len) != NULL)
204 goto next_blk;
205 break;
206 default:
Christopher Faulet47596d32018-10-22 09:17:28 +0200207 goto next_blk;
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200208 break;
209 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200210 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200211 match:
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 v = htx_get_blk_value(htx, blk);
213
214 return_hdr:
215 ctx->lws_before = 0;
216 ctx->lws_after = 0;
217 while (v.len && HTTP_IS_LWS(*v.ptr)) {
218 v.ptr++;
219 v.len--;
220 ctx->lws_before++;
221 }
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200222 if (!(flags & HTTP_FIND_FL_FULL))
Christopher Faulet47596d32018-10-22 09:17:28 +0200223 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
224 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
225 v.len--;
226 ctx->lws_after++;
227 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200228 ctx->blk = blk;
229 ctx->value = v;
230 return 1;
231
232 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200233 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200234 }
235
236 ctx->blk = NULL;
237 ctx->value = ist("");
238 ctx->lws_before = ctx->lws_after = 0;
239 return 0;
240}
241
Christopher Faulet8dd33e12020-05-05 07:42:42 +0200242
243/* Header names must match <name> */
244int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
245{
246 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
247}
248
249/* Header names must match <name>. Same than http_find_header */
250int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full)
251{
252 return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0));
253}
254
255
256/* Header names must start with <prefix> */
257int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full)
258{
259 return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0));
260}
261
262/* Header names must end with <suffix> */
263int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full)
264{
265 return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0));
266}
267/* Header names must contain <sub> */
268int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full)
269{
270 return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0));
271}
272
273/* Header names must match <re> regex*/
274int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full)
275{
276 return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0));
277}
278
279
Christopher Faulet47596d32018-10-22 09:17:28 +0200280/* Adds a header block int the HTX message <htx>, just before the EOH block. It
281 * returns 1 on success, otherwise it returns 0.
282 */
283int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
284{
285 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200286 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200287 enum htx_blk_type type = htx_get_tail_type(htx);
288 int32_t prev;
289
290 blk = htx_add_header(htx, n, v);
291 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200292 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200293
294 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200295 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200296
297 /* <blk> is the head, swap it iteratively with its predecessor to place
298 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200299 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200300 struct htx_blk *pblk = htx_get_blk(htx, prev);
301 enum htx_blk_type type = htx_get_blk_type(pblk);
302
303 /* Swap .addr and .info fields */
304 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
305 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
306
307 if (blk->addr == pblk->addr)
308 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200309
310 /* Stop when end-of-header is reached */
311 if (type == HTX_BLK_EOH)
312 break;
313
314 blk = pblk;
315 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200316
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200317 end:
318 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100319 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200320 if (!http_update_authority(htx, sl, v))
321 goto fail;
322 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200323 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200324
325 fail:
326 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200327}
328
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200330 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200331 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332int 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 +0200333{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200334 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200335
Christopher Faulet29f17582019-05-23 11:03:26 +0200336 blk = htx_get_first_blk(htx);
337 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200338 return 0;
339 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200340}
341
Christopher Faulete010c802018-10-24 10:36:45 +0200342/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
343 * on success, otherwise 0.
344 */
345int http_replace_req_meth(struct htx *htx, const struct ist meth)
346{
347 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200348 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100349 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200350
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100351 if (!sl)
352 return 0;
353
Christopher Faulete010c802018-10-24 10:36:45 +0200354 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100355 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
356 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200357
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100358 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
359 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200360
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100361 /* create the new start line */
362 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
363 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200364}
365
366/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
367 * success, otherwise 0.
368 */
369int http_replace_req_uri(struct htx *htx, const struct ist uri)
370{
371 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200372 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100373 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200374
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100375 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200376 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100377
Christopher Faulete010c802018-10-24 10:36:45 +0200378 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100379 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
380 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200381
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100382 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
383 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200384
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100385 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200386 if (!http_replace_stline(htx, meth, uri, vsn))
387 goto fail;
388
389 sl = http_get_stline(htx);
390 if (!http_update_host(htx, sl, uri))
391 goto fail;
392
393 return 1;
394 fail:
395 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200396}
397
398/* Replace the request path in the HTX message <htx> by <path>. The host part
399 * and the query string are preserved. It returns 1 on success, otherwise 0.
400 */
401int http_replace_req_path(struct htx *htx, const struct ist path)
402{
403 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200404 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100405 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200406 size_t plen = 0;
407
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100408 if (!sl)
409 return 0;
410
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100411 uri = htx_sl_req_uri(sl);
412 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100413 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100414 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200415 while (plen < p.len && *(p.ptr + plen) != '?')
416 plen++;
417
418 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100419 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
420 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200421
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100422 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
423 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
424
425 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200426 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
427 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100428 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200429
430 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100431 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200432}
433
434/* Replace the request query-string in the HTX message <htx> by <query>. The
435 * host part and the path are preserved. It returns 1 on success, otherwise
436 * 0.
437 */
438int http_replace_req_query(struct htx *htx, const struct ist query)
439{
440 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200441 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100442 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200443 int offset = 1;
444
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100445 if (!sl)
446 return 0;
447
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100448 uri = htx_sl_req_uri(sl);
449 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200450 while (q.len > 0 && *(q.ptr) != '?') {
451 q.ptr++;
452 q.len--;
453 }
454
455 /* skip the question mark or indicate that we must insert it
456 * (but only if the format string is not empty then).
457 */
458 if (q.len) {
459 q.ptr++;
460 q.len--;
461 }
462 else if (query.len > 1)
463 offset = 0;
464
465 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100466 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
467 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200468
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100469 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
470 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200471
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100472 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
473 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
474 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200475
476 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200478}
479
480/* Replace the response status in the HTX message <htx> by <status>. It returns
481 * 1 on success, otherwise 0.
482*/
483int http_replace_res_status(struct htx *htx, const struct ist status)
484{
485 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200486 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100487 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200488
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100489 if (!sl)
490 return 0;
491
Christopher Faulete010c802018-10-24 10:36:45 +0200492 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100493 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
494 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200495
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100496 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
497 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200498
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100499 /* create the new start line */
500 sl->info.res.status = strl2ui(status.ptr, status.len);
501 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200502}
503
504/* Replace the response reason in the HTX message <htx> by <reason>. It returns
505 * 1 on success, otherwise 0.
506*/
507int http_replace_res_reason(struct htx *htx, const struct ist reason)
508{
509 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200510 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100511 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200512
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100513 if (!sl)
514 return 0;
515
Christopher Faulete010c802018-10-24 10:36:45 +0200516 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100517 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
518 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200519
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100520 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
521 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200522
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100523 /* create the new start line */
524 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200525}
526
Christopher Faulet47596d32018-10-22 09:17:28 +0200527/* Replaces a part of a header value referenced in the context <ctx> by
528 * <data>. It returns 1 on success, otherwise it returns 0. The context is
529 * updated if necessary.
530 */
531int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
532{
533 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200534 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200535 char *start;
536 struct ist v;
537 uint32_t len, off;
538
539 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200540 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200541
542 v = htx_get_blk_value(htx, blk);
543 start = ctx->value.ptr - ctx->lws_before;
544 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
545 off = start - v.ptr;
546
547 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
548 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200549 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200550
551 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200552
553 sl = http_get_stline(htx);
554 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
555 struct ist n = htx_get_blk_name(htx, blk);
556
557 if (isteq(n, ist("host"))) {
558 if (!http_update_authority(htx, sl, v))
559 goto fail;
560 ctx->blk = NULL;
561 http_find_header(htx, ist("host"), ctx, 1);
562 blk = ctx->blk;
563 v = htx_get_blk_value(htx, blk);
564 }
565 }
566
Christopher Faulet47596d32018-10-22 09:17:28 +0200567 ctx->blk = blk;
568 ctx->value.ptr = v.ptr + off;
569 ctx->value.len = data.len;
570 ctx->lws_before = ctx->lws_after = 0;
571
572 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200573 fail:
574 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200575}
576
577/* Fully replaces a header referenced in the context <ctx> by the name <name>
578 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
579 * context is updated if necessary.
580 */
581int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
582 const struct ist name, const struct ist value)
583{
584 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200585 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200586
587 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200588 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200589
590 blk = htx_replace_header(htx, blk, name, value);
591 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200592 goto fail;
593
594 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100595 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200596 if (!http_update_authority(htx, sl, value))
597 goto fail;
598 ctx->blk = NULL;
599 http_find_header(htx, ist("host"), ctx, 1);
600 blk = ctx->blk;
601 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200602
603 ctx->blk = blk;
604 ctx->value = ist(NULL);
605 ctx->lws_before = ctx->lws_after = 0;
606
607 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200608 fail:
609 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200610}
611
612/* Remove one value of a header. This only works on a <ctx> returned by
613 * http_find_header function. The value is removed, as well as surrounding commas
614 * if any. If the removed value was alone, the whole header is removed. The
615 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
616 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
617 * form that can be handled by http_find_header() to find next occurrence.
618 */
619int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
620{
621 struct htx_blk *blk = ctx->blk;
622 char *start;
623 struct ist v;
624 uint32_t len;
625
626 if (!blk)
627 return 0;
628
629 start = ctx->value.ptr - ctx->lws_before;
630 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
631
632 v = htx_get_blk_value(htx, blk);
633 if (len == v.len) {
634 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200635 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200636 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100637 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200638 ctx->lws_before = ctx->lws_after = 0;
639 }
640 else {
641 ctx->blk = htx_get_blk(htx, htx->tail);
642 ctx->value = htx_get_blk_value(htx, ctx->blk);
643 ctx->lws_before = ctx->lws_after = 0;
644 }
645 return 1;
646 }
647
648 /* This was not the only value of this header. We have to remove the
649 * part pointed by ctx->value. If it is the last entry of the list, we
650 * remove the last separator.
651 */
652 if (start == v.ptr) {
653 /* It's the first header part but not the only one. So remove
654 * the comma after it. */
655 len++;
656 }
657 else {
658 /* There is at least one header part before the removed one. So
659 * remove the comma between them. */
660 start--;
661 len++;
662 }
663 /* Update the block content and its len */
664 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200665 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200666
667 /* Finally update the ctx */
668 ctx->value.ptr = start;
669 ctx->value.len = 0;
670 ctx->lws_before = ctx->lws_after = 0;
671
672 return 1;
673}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200674
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200675/* Updates the authority part of the uri with the value <host>. It happens when
676 * the header host is modified. It returns 0 on failure and 1 on success. It is
677 * the caller responsibility to provide the start-line and to be sure the uri
678 * contains an authority. Thus, if no authority is found in the uri, an error is
679 * returned.
680 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200681int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200682{
683 struct buffer *temp = get_trash_chunk();
684 struct ist meth, vsn, uri, authority;
685
686 uri = htx_sl_req_uri(sl);
687 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100688 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200689 return 0;
690
Christopher Faulet34b18e42020-02-18 11:02:21 +0100691 /* Don't update the uri if there is no change */
692 if (isteq(host, authority))
693 return 1;
694
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200695 /* Start by copying old method and version */
696 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
697 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
698
699 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
700 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
701
702 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
703 chunk_memcat(temp, host.ptr, host.len);
704 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
705 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
706
707 return http_replace_stline(htx, meth, uri, vsn);
708
709}
710
711/* Update the header host by extracting the authority of the uri <uri>. flags of
712 * the start-line are also updated accordingly. For orgin-form and asterisk-form
713 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
714 * removed from the flags of the start-line. Otherwise, this flag is set and the
715 * authority is used to set the value of the header host. This function returns
716 * 0 on failure and 1 on success.
717*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200718int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200719{
720 struct ist authority;
721 struct http_hdr_ctx ctx;
722
723 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
724 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
725 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
726 }
727 else {
728 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
729 if (sl->info.req.meth != HTTP_METH_CONNECT) {
730 // absolute-form (RFC7320 #5.3.2)
731 sl->flags |= HTX_SL_F_HAS_SCHM;
732 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
733 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
734
735 authority = http_get_authority(uri, 1);
736 if (!authority.len)
737 goto fail;
738 }
739 else {
740 // authority-form (RFC7320 #5.3.3)
741 authority = uri;
742 }
743
744 /* Replace header host value */
745 ctx.blk = NULL;
746 while (http_find_header(htx, ist("host"), &ctx, 1)) {
747 if (!http_replace_header_value(htx, &ctx, authority))
748 goto fail;
749 }
750
751 }
752 return 1;
753 fail:
754 return 0;
755}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200756
757/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
758 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
759 * performed over the whole headers. Otherwise it must contain a valid header
760 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
761 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
762 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
763 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
764 * -1. The value fetch stops at commas, so this function is suited for use with
765 * list headers.
766 * The return value is 0 if nothing was found, or non-zero otherwise.
767 */
768unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
769 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
770{
771 struct http_hdr_ctx local_ctx;
772 struct ist val_hist[MAX_HDR_HISTORY];
773 unsigned int hist_idx;
774 int found;
775
776 if (!ctx) {
777 local_ctx.blk = NULL;
778 ctx = &local_ctx;
779 }
780
781 if (occ >= 0) {
782 /* search from the beginning */
783 while (http_find_header(htx, hdr, ctx, 0)) {
784 occ--;
785 if (occ <= 0) {
786 *vptr = ctx->value.ptr;
787 *vlen = ctx->value.len;
788 return 1;
789 }
790 }
791 return 0;
792 }
793
794 /* negative occurrence, we scan all the list then walk back */
795 if (-occ > MAX_HDR_HISTORY)
796 return 0;
797
798 found = hist_idx = 0;
799 while (http_find_header(htx, hdr, ctx, 0)) {
800 val_hist[hist_idx] = ctx->value;
801 if (++hist_idx >= MAX_HDR_HISTORY)
802 hist_idx = 0;
803 found++;
804 }
805 if (-occ > found)
806 return 0;
807
808 /* OK now we have the last occurrence in [hist_idx-1], and we need to
809 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
810 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
811 * to remain in the 0..9 range.
812 */
813 hist_idx += occ + MAX_HDR_HISTORY;
814 if (hist_idx >= MAX_HDR_HISTORY)
815 hist_idx -= MAX_HDR_HISTORY;
816 *vptr = val_hist[hist_idx].ptr;
817 *vlen = val_hist[hist_idx].len;
818 return 1;
819}
820
821/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
822 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
823 * performed over the whole headers. Otherwise it must contain a valid header
824 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
825 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
826 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
827 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
828 * -1. This function differs from http_get_hdr() in that it only returns full
829 * line header values and does not stop at commas.
830 * The return value is 0 if nothing was found, or non-zero otherwise.
831 */
832unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
833 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
834{
835 struct http_hdr_ctx local_ctx;
836 struct ist val_hist[MAX_HDR_HISTORY];
837 unsigned int hist_idx;
838 int found;
839
840 if (!ctx) {
841 local_ctx.blk = NULL;
842 ctx = &local_ctx;
843 }
844
845 if (occ >= 0) {
846 /* search from the beginning */
847 while (http_find_header(htx, hdr, ctx, 1)) {
848 occ--;
849 if (occ <= 0) {
850 *vptr = ctx->value.ptr;
851 *vlen = ctx->value.len;
852 return 1;
853 }
854 }
855 return 0;
856 }
857
858 /* negative occurrence, we scan all the list then walk back */
859 if (-occ > MAX_HDR_HISTORY)
860 return 0;
861
862 found = hist_idx = 0;
863 while (http_find_header(htx, hdr, ctx, 1)) {
864 val_hist[hist_idx] = ctx->value;
865 if (++hist_idx >= MAX_HDR_HISTORY)
866 hist_idx = 0;
867 found++;
868 }
869 if (-occ > found)
870 return 0;
871
872 /* OK now we have the last occurrence in [hist_idx-1], and we need to
873 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
874 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
875 * to remain in the 0..9 range.
876 */
877 hist_idx += occ + MAX_HDR_HISTORY;
878 if (hist_idx >= MAX_HDR_HISTORY)
879 hist_idx -= MAX_HDR_HISTORY;
880 *vptr = val_hist[hist_idx].ptr;
881 *vlen = val_hist[hist_idx].len;
882 return 1;
883}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100884
Christopher Faulet90cc4812019-07-22 16:49:30 +0200885int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100886{
887 struct htx *htx;
888 struct htx_sl *sl;
889 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200890 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100891 union h1_sl h1sl;
892 unsigned int flags = HTX_SL_F_IS_RESP;
893 int ret = 0;
894
Christopher Faulet90cc4812019-07-22 16:49:30 +0200895 b_reset(buf);
896 if (!raw.len) {
897 buf->size = 0;
898 buf->area = malloc(raw.len);
899 return 1;
900 }
901
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100902 buf->size = global.tune.bufsize;
903 buf->area = (char *)malloc(buf->size);
904 if (!buf->area)
905 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100906
907 h1m_init_res(&h1m);
908 h1m.flags |= H1_MF_NO_PHDR;
909 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
910 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
911 if (ret <= 0)
912 goto error;
913
914 if (unlikely(h1sl.st.v.len != 8))
915 goto error;
916 if ((*(h1sl.st.v.ptr + 5) > '1') ||
917 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
918 h1m.flags |= H1_MF_VER_11;
919
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200920 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
921 goto error;
922
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100923 if (h1m.flags & H1_MF_VER_11)
924 flags |= HTX_SL_F_VER_11;
925 if (h1m.flags & H1_MF_XFER_ENC)
926 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200927 if (h1m.flags & H1_MF_CLEN) {
928 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
929 if (h1m.body_len == 0)
930 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100931 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200932 if (h1m.flags & H1_MF_CHNK)
933 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100934
935 htx = htx_from_buf(buf);
936 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
937 if (!sl || !htx_add_all_headers(htx, hdrs))
938 goto error;
939 sl->info.res.status = h1sl.st.status;
940
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200941 while (raw.len > ret) {
942 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
943 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100944 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200945 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100946 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200947
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100948 if (!htx_add_endof(htx, HTX_BLK_EOM))
949 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200950
Christopher Faulet90cc4812019-07-22 16:49:30 +0200951 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100952
953error:
954 if (buf->size)
955 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200956 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100957}
958
Christopher Faulet18630642020-05-12 18:57:28 +0200959void release_http_reply(struct http_reply *http_reply)
960{
961 struct logformat_node *lf, *lfb;
962 struct http_reply_hdr *hdr, *hdrb;
963
964 if (!http_reply)
965 return;
966
967 free(http_reply->ctype);
968 http_reply->ctype = NULL;
969 list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) {
970 LIST_DEL(&hdr->list);
971 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
972 LIST_DEL(&lf->list);
973 release_sample_expr(lf->expr);
974 free(lf->arg);
975 free(lf);
976 }
977 istfree(&hdr->name);
978 free(hdr);
979 }
980
981 if (http_reply->type == HTTP_REPLY_ERRFILES) {
982 free(http_reply->body.http_errors);
983 http_reply->body.http_errors = NULL;
984 }
985 else if (http_reply->type == HTTP_REPLY_RAW)
986 chunk_destroy(&http_reply->body.obj);
987 else if (http_reply->type == HTTP_REPLY_LOGFMT) {
988 list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) {
989 LIST_DEL(&lf->list);
990 release_sample_expr(lf->expr);
991 free(lf->arg);
992 free(lf);
993 }
994 }
995}
996
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100997static int http_htx_init(void)
998{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100999 struct buffer chk;
1000 struct ist raw;
1001 int rc;
1002 int err_code = 0;
1003
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001004 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1005 if (!http_err_msgs[rc]) {
1006 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
1007 err_code |= ERR_ALERT | ERR_FATAL;
1008 continue;
1009 }
1010
1011 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
1012 if (!http_str_to_htx(&chk, raw)) {
1013 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
1014 http_err_codes[rc]);
1015 err_code |= ERR_ALERT | ERR_FATAL;
1016 }
Christopher Fauletf7346382019-07-17 22:02:08 +02001017 http_err_chunks[rc] = chk;
Christopher Faulet1b13eca2020-05-14 09:54:26 +02001018 http_err_replies[rc].type = HTTP_REPLY_ERRMSG;
1019 http_err_replies[rc].status = http_err_codes[rc];
1020 http_err_replies[rc].ctype = NULL;
1021 LIST_INIT(&http_err_replies[rc].hdrs);
1022 http_err_replies[rc].body.errmsg = &http_err_chunks[rc];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001023 }
1024end:
1025 return err_code;
1026}
1027
Christopher Faulet58857752020-01-15 15:19:50 +01001028static void http_htx_deinit(void)
1029{
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001030 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +01001031 struct ebpt_node *node, *next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001032 struct http_error_msg *http_errmsg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02001033 int rc;
Christopher Faulet58857752020-01-15 15:19:50 +01001034
1035 node = ebpt_first(&http_error_messages);
1036 while (node) {
1037 next = ebpt_next(node);
1038 ebpt_delete(node);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001039 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1040 chunk_destroy(&http_errmsg->msg);
Christopher Faulet58857752020-01-15 15:19:50 +01001041 free(node->key);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001042 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001043 node = next;
1044 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001045
1046 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
1047 free(http_errs->conf.file);
1048 free(http_errs->id);
Christopher Fauletde30bb72020-05-14 10:03:55 +02001049 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1050 release_http_reply(http_errs->replies[rc]);
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001051 LIST_DEL(&http_errs->list);
1052 free(http_errs);
1053 }
Christopher Faulet58857752020-01-15 15:19:50 +01001054}
1055
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001056REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +01001057REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +01001058
Christopher Faulet58857752020-01-15 15:19:50 +01001059/* Reads content of the error file <file> and convert it into an HTX message. On
1060 * success, the HTX message is returned. On error, NULL is returned and an error
1061 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001062 */
Christopher Faulet58857752020-01-15 15:19:50 +01001063struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001064{
Christopher Faulet58857752020-01-15 15:19:50 +01001065 struct buffer *buf = NULL;
1066 struct buffer chk;
1067 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001068 struct http_error_msg *http_errmsg;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001069 struct stat stat;
1070 char *err = NULL;
1071 int errnum, errlen;
1072 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +01001073
1074 /* already loaded */
1075 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
1076 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001077 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1078 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001079 goto out;
1080 }
Christopher Faulet5031ef52020-01-15 11:22:07 +01001081
Christopher Faulet58857752020-01-15 15:19:50 +01001082 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +01001083 fd = open(file, O_RDONLY);
1084 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1085 memprintf(errmsg, "error opening file '%s'.", file);
1086 goto out;
1087 }
1088
1089 if (stat.st_size <= global.tune.bufsize)
1090 errlen = stat.st_size;
1091 else {
1092 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
1093 file, global.tune.bufsize);
1094 errlen = global.tune.bufsize;
1095 }
1096
1097 err = malloc(errlen);
1098 if (!err) {
1099 memprintf(errmsg, "out of memory.");
1100 goto out;
1101 }
1102
1103 errnum = read(fd, err, errlen);
1104 if (errnum != errlen) {
1105 memprintf(errmsg, "error reading file '%s'.", file);
1106 goto out;
1107 }
1108
Christopher Faulet58857752020-01-15 15:19:50 +01001109 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001110 http_errmsg = calloc(1, sizeof(*http_errmsg));
1111 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001112 memprintf(errmsg, "out of memory.");
1113 goto out;
1114 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001115 http_errmsg->node.key = strdup(file);
1116 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001117 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001118 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001119 goto out;
1120 }
1121
1122 /* Convert the error file into an HTX message */
1123 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +01001124 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001125 free(http_errmsg->node.key);
1126 free(http_errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001127 goto out;
1128 }
1129
Christopher Faulet58857752020-01-15 15:19:50 +01001130 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001131 http_errmsg->msg = chk;
1132 ebis_insert(&http_error_messages, &http_errmsg->node);
1133 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001134
Christopher Faulet5031ef52020-01-15 11:22:07 +01001135 out:
1136 if (fd >= 0)
1137 close(fd);
1138 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001139 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001140}
1141
Ilya Shipitsind4259502020-04-08 01:07:56 +05001142/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +01001143 * message is returned. On error, NULL is returned and an error message is
1144 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001145 */
Christopher Faulet58857752020-01-15 15:19:50 +01001146struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001147{
Christopher Faulet58857752020-01-15 15:19:50 +01001148 struct buffer *buf = NULL;
1149 struct buffer chk;
1150 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001151 struct http_error_msg *http_errmsg;
Christopher Faulet58857752020-01-15 15:19:50 +01001152
1153 /* already loaded */
1154 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1155 if (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001156 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1157 buf = &http_errmsg->msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001158 goto out;
1159 }
1160 /* Create the node corresponding to the error file */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001161 http_errmsg = calloc(1, sizeof(*http_errmsg));
1162 if (!http_errmsg) {
Christopher Faulet58857752020-01-15 15:19:50 +01001163 memprintf(errmsg, "out of memory.");
1164 goto out;
1165 }
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001166 http_errmsg->node.key = strdup(key);
1167 if (!http_errmsg->node.key) {
Christopher Faulet58857752020-01-15 15:19:50 +01001168 memprintf(errmsg, "out of memory.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001169 free(http_errmsg);
Christopher Faulet58857752020-01-15 15:19:50 +01001170 goto out;
1171 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001172
1173 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001174 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001175 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001176 free(http_errmsg->node.key);
1177 free(http_errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001178 goto out;
1179 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001180
Christopher Faulet58857752020-01-15 15:19:50 +01001181 /* Insert the node in the tree and return the HTX message */
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001182 http_errmsg->msg = chk;
1183 ebis_insert(&http_error_messages, &http_errmsg->node);
1184 buf = &http_errmsg->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001185 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001186 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001187}
1188
Christopher Faulet5031ef52020-01-15 11:22:07 +01001189/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001190 * <status>. It returns NULL if there is any error, otherwise it return the
1191 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001192 */
Christopher Faulet58857752020-01-15 15:19:50 +01001193struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001194{
Christopher Faulet58857752020-01-15 15:19:50 +01001195 struct buffer *buf = NULL;
1196 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001197
1198 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1199 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001200 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001201 break;
1202 }
1203 }
1204
1205 if (rc >= HTTP_ERR_SIZE)
1206 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001207 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001208}
1209
1210/* This function creates HTX error message corresponding to a redirect message
1211 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001212 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1213 * returns NULL if there is any error, otherwise it return the corresponding HTX
1214 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001215 */
Christopher Faulet58857752020-01-15 15:19:50 +01001216struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001217{
Christopher Faulet58857752020-01-15 15:19:50 +01001218 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001219 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001220 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001221 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001222
1223 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1224 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001225 /* Create the error key */
1226 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1227 memprintf(errmsg, "out of memory.");
1228 goto out;
1229 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001230 /* Create the error message */
1231 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1232 errlen = strlen(msg) + strlen(url) + 5;
1233 err = malloc(errlen);
1234 if (!err) {
1235 memprintf(errmsg, "out of memory.");
1236 goto out;
1237 }
1238 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1239
1240 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001241 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001242 break;
1243 }
1244 }
1245
1246 if (rc >= HTTP_ERR_SIZE)
1247 memprintf(errmsg, "status code '%d' not handled.", status);
1248out:
Christopher Faulet58857752020-01-15 15:19:50 +01001249 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001250 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001251 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001252}
1253
Christopher Faulet7eea2412020-05-13 15:02:59 +02001254/* Check an "http reply" and, for replies referencing an http-errors section,
1255 * try to find the right section and the right error message in this section. If
1256 * found, the reply is updated. If the http-errors section exists but the error
1257 * message is not found, no error message is set to fallback on the default
1258 * ones. Otherwise (unknown section) an error is returned.
1259 *
1260 * The function returns 1 in success case, otherwise, it returns 0 and errmsg is
1261 * filled.
1262 */
1263int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg)
1264{
1265 struct http_errors *http_errs;
1266 int ret = 1;
1267
1268 if (reply->type != HTTP_REPLY_ERRFILES)
1269 goto end;
1270
1271 list_for_each_entry(http_errs, &http_errors_list, list) {
1272 if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
1273 reply->type = HTTP_REPLY_ERRMSG;
1274 free(reply->body.http_errors);
1275 reply->body.errmsg = http_errs->errmsg[http_get_status_idx(reply->status)];
1276 if (!reply->body.errmsg)
1277 ha_warning("Proxy '%s': status '%d' referenced by an http reply "
1278 "not declared in http-errors section '%s'.\n",
1279 px->id, reply->status, http_errs->id);
1280 break;
1281 }
1282 }
1283
1284 if (&http_errs->list == &http_errors_list) {
1285 memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ",
1286 reply->body.http_errors);
1287 ret = 0;
1288 }
1289
1290 end:
1291 return ret;
1292}
1293
Christopher Faulet47e791e2020-05-13 14:36:55 +02001294/* Parse an "http reply". It returns the reply on success or NULL on error. This
1295 * function creates one of the following http replies :
1296 *
1297 * - HTTP_REPLY_EMPTY : dummy response, no payload
1298 * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one
1299 * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing)
1300 * - HTTP_REPLY_RAW : explicit file object ('file' argument)
1301 * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument)
1302 *
1303 * The content-type must be defined for non-empty payload. It is ignored for
1304 * error messages (implicit or explicit). When an http-errors section is
1305 * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved
1306 * during the configuration validity check or dynamically. It is the caller
1307 * responsibility to choose. If no status code is configured, <default_status>
1308 * is set.
1309 */
1310struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px,
1311 int default_status, char **errmsg)
1312{
1313 struct logformat_node *lf, *lfb;
1314 struct http_reply *reply = NULL;
1315 struct http_reply_hdr *hdr, *hdrb;
1316 struct stat stat;
1317 const char *act_arg = NULL;
1318 char *obj = NULL;
1319 int cur_arg, cap, objlen = 0, fd = -1;
1320
1321
1322 reply = calloc(1, sizeof(*reply));
1323 if (!reply) {
1324 memprintf(errmsg, "out of memory");
1325 goto error;
1326 }
1327 LIST_INIT(&reply->hdrs);
1328 reply->type = HTTP_REPLY_EMPTY;
1329 reply->status = default_status;
1330
1331 cap = ((px->conf.args.ctx == ARGC_HRQ)
1332 ? ((px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR)
1333 : ((px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR));
1334
1335 cur_arg = *orig_arg;
1336 while (*args[cur_arg]) {
1337 if (strcmp(args[cur_arg], "status") == 0) {
1338 cur_arg++;
1339 if (!*args[cur_arg]) {
1340 memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]);
1341 goto error;
1342 }
1343 reply->status = atol(args[cur_arg]);
1344 if (reply->status < 200 || reply->status > 599) {
1345 memprintf(errmsg, "Unexpected status code '%d'", reply->status);
1346 goto error;
1347 }
1348 cur_arg++;
1349 }
1350 else if (strcmp(args[cur_arg], "content-type") == 0) {
1351 cur_arg++;
1352 if (!*args[cur_arg]) {
1353 memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]);
1354 goto error;
1355 }
1356 free(reply->ctype);
1357 reply->ctype = strdup(args[cur_arg]);
1358 cur_arg++;
1359 }
1360 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
1361 if (reply->type != HTTP_REPLY_EMPTY) {
1362 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1363 goto error;
1364 }
1365 act_arg = args[cur_arg];
1366 cur_arg++;
1367 if (!*args[cur_arg]) {
1368 memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]);
1369 goto error;
1370 }
1371 reply->body.http_errors = strdup(args[cur_arg]);
1372 if (!reply->body.http_errors) {
1373 memprintf(errmsg, "out of memory");
1374 goto error;
1375 }
1376 reply->type = HTTP_REPLY_ERRFILES;
1377 cur_arg++;
1378 }
1379 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
1380 if (reply->type != HTTP_REPLY_EMPTY) {
1381 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1382 goto error;
1383 }
1384 act_arg = args[cur_arg];
1385 reply->type = HTTP_REPLY_ERRMSG;
1386 cur_arg++;
1387 }
1388 else if (strcmp(args[cur_arg], "errorfile") == 0) {
1389 if (reply->type != HTTP_REPLY_EMPTY) {
1390 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1391 goto error;
1392 }
1393 act_arg = args[cur_arg];
1394 cur_arg++;
1395 if (!*args[cur_arg]) {
1396 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1397 goto error;
1398 }
1399 reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg);
1400 if (!reply->body.errmsg) {
1401 goto error;
1402 }
1403 reply->type = HTTP_REPLY_ERRMSG;
1404 cur_arg++;
1405 }
1406 else if (strcmp(args[cur_arg], "file") == 0) {
1407 if (reply->type != HTTP_REPLY_EMPTY) {
1408 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1409 goto error;
1410 }
1411 act_arg = args[cur_arg];
1412 cur_arg++;
1413 if (!*args[cur_arg]) {
1414 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1415 goto error;
1416 }
1417 fd = open(args[cur_arg], O_RDONLY);
1418 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1419 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1420 goto error;
1421 }
1422 if (stat.st_size > global.tune.bufsize) {
1423 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1424 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1425 goto error;
1426 }
1427 objlen = stat.st_size;
1428 obj = malloc(objlen);
1429 if (!obj || read(fd, obj, objlen) != objlen) {
1430 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1431 goto error;
1432 }
1433 close(fd);
1434 fd = -1;
1435 reply->type = HTTP_REPLY_RAW;
1436 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1437 obj = NULL;
1438 cur_arg++;
1439 }
1440 else if (strcmp(args[cur_arg], "string") == 0) {
1441 if (reply->type != HTTP_REPLY_EMPTY) {
1442 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1443 goto error;
1444 }
1445 act_arg = args[cur_arg];
1446 cur_arg++;
1447 if (!*args[cur_arg]) {
1448 memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]);
1449 goto error;
1450 }
1451 obj = strdup(args[cur_arg]);
1452 objlen = strlen(args[cur_arg]);
1453 if (!obj) {
1454 memprintf(errmsg, "out of memory");
1455 goto error;
1456 }
1457 reply->type = HTTP_REPLY_RAW;
1458 chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen);
1459 obj = NULL;
1460 cur_arg++;
1461 }
1462 else if (strcmp(args[cur_arg], "lf-file") == 0) {
1463 if (reply->type != HTTP_REPLY_EMPTY) {
1464 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1465 goto error;
1466 }
1467 act_arg = args[cur_arg];
1468 cur_arg++;
1469 if (!*args[cur_arg]) {
1470 memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]);
1471 goto error;
1472 }
1473 fd = open(args[cur_arg], O_RDONLY);
1474 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
1475 memprintf(errmsg, "error opening file '%s'", args[cur_arg]);
1476 goto error;
1477 }
1478 if (stat.st_size > global.tune.bufsize) {
1479 memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)",
1480 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
1481 goto error;
1482 }
1483 objlen = stat.st_size;
1484 obj = malloc(objlen + 1);
1485 if (!obj || read(fd, obj, objlen) != objlen) {
1486 memprintf(errmsg, "error reading file '%s'", args[cur_arg]);
1487 goto error;
1488 }
1489 close(fd);
1490 fd = -1;
1491 obj[objlen] = '\0';
1492 reply->type = HTTP_REPLY_LOGFMT;
1493 cur_arg++;
1494 }
1495 else if (strcmp(args[cur_arg], "lf-string") == 0) {
1496 if (reply->type != HTTP_REPLY_EMPTY) {
1497 memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
1498 goto error;
1499 }
1500 act_arg = args[cur_arg];
1501 cur_arg++;
1502 if (!*args[cur_arg]) {
1503 memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]);
1504 goto error;
1505 }
1506 obj = strdup(args[cur_arg]);
1507 objlen = strlen(args[cur_arg]);
1508 reply->type = HTTP_REPLY_LOGFMT;
1509 cur_arg++;
1510 }
1511 else if (strcmp(args[cur_arg], "hdr") == 0) {
1512 cur_arg++;
1513 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1514 memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
1515 goto error;
1516 }
1517 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
1518 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
1519 strcasecmp(args[cur_arg], "content-type") == 0) {
1520 ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n",
1521 px->conf.args.file, px->conf.args.line, args[cur_arg]);
1522 cur_arg += 2;
1523 continue;
1524 }
1525 hdr = calloc(1, sizeof(*hdr));
1526 if (!hdr) {
1527 memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]);
1528 goto error;
1529 }
1530 LIST_INIT(&hdr->value);
1531 hdr->name = ist(strdup(args[cur_arg]));
1532 if (!isttest(hdr->name)) {
1533 memprintf(errmsg, "out of memory");
1534 goto error;
1535 }
1536 LIST_ADDQ(&reply->hdrs, &hdr->list);
1537 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg))
1538 goto error;
1539
1540 free(px->conf.lfs_file);
1541 px->conf.lfs_file = strdup(px->conf.args.file);
1542 px->conf.lfs_line = px->conf.args.line;
1543 cur_arg += 2;
1544 }
1545 else
1546 break;
1547 }
1548
1549 if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */
1550 if (reply->ctype) {
1551 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because"
1552 " neither errorfile nor payload defined.\n",
1553 px->conf.args.file, px->conf.args.line, reply->ctype);
1554 free(reply->ctype);
1555 reply->ctype = NULL;
1556 }
1557 }
1558 else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */
1559
1560 if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) {
1561 /* default errorfile or errorfiles: check the status */
1562 int rc;
1563
1564 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1565 if (http_err_codes[rc] == reply->status)
1566 break;
1567 }
1568
1569 if (rc >= HTTP_ERR_SIZE) {
1570 memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.",
1571 reply->status, act_arg);
1572 goto error;
1573 }
1574 }
1575
1576 if (reply->ctype) {
1577 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1578 "with an erorrfile.\n",
1579 px->conf.args.file, px->conf.args.line, reply->ctype);
1580 free(reply->ctype);
1581 reply->ctype = NULL;
1582 }
1583 if (!LIST_ISEMPTY(&reply->hdrs)) {
1584 ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used "
1585 "with an erorrfile.\n",
1586 px->conf.args.file, px->conf.args.line);
1587 list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) {
1588 LIST_DEL(&hdr->list);
1589 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1590 LIST_DEL(&lf->list);
1591 release_sample_expr(lf->expr);
1592 free(lf->arg);
1593 free(lf);
1594 }
1595 istfree(&hdr->name);
1596 free(hdr);
1597 }
1598 }
1599 }
1600 else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/
1601 if (!reply->ctype && objlen) {
1602 memprintf(errmsg, "a content type must be defined when non-empty payload is configured");
1603 goto error;
1604 }
1605 if (reply->ctype && !b_data(&reply->body.obj)) {
1606 ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used "
1607 "with an emtpy payload.\n",
1608 px->conf.args.file, px->conf.args.line, reply->ctype);
1609 free(reply->ctype);
1610 reply->ctype = NULL;
1611 }
1612 if (b_room(&reply->body.obj) < global.tune.maxrewrite) {
1613 ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting."
1614 " It may lead to internal errors if strict rewriting mode is enabled.\n",
1615 px->conf.args.file, px->conf.args.line);
1616 }
1617 }
1618 else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
1619 LIST_INIT(&reply->body.fmt);
1620 if (!reply->ctype) {
1621 memprintf(errmsg, "a content type must be defined with a log-format payload");
1622 goto error;
1623 }
1624 if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg))
1625 goto error;
1626
1627 free(px->conf.lfs_file);
1628 px->conf.lfs_file = strdup(px->conf.args.file);
1629 px->conf.lfs_line = px->conf.args.line;
1630 }
1631
1632 free(obj);
1633 *orig_arg = cur_arg;
1634 return reply;
1635
1636 error:
1637 free(obj);
1638 if (fd >= 0)
1639 close(fd);
1640 release_http_reply(reply);
1641 return NULL;
1642}
1643
Christopher Faulet07f41f72020-01-16 16:16:06 +01001644/* Parses the "errorloc[302|303]" proxy keyword */
1645static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1646 struct proxy *defpx, const char *file, int line,
1647 char **errmsg)
1648{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001649 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001650 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001651 int errloc, status;
1652 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001653
1654 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1655 ret = 1;
1656 goto out;
1657 }
1658
1659 if (*(args[1]) == 0 || *(args[2]) == 0) {
1660 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1661 ret = -1;
1662 goto out;
1663 }
1664
1665 status = atol(args[1]);
1666 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1667 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1668 if (!msg) {
1669 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1670 ret = -1;
1671 goto out;
1672 }
1673
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001674 conf_err = calloc(1, sizeof(*conf_err));
1675 if (!conf_err) {
1676 memprintf(errmsg, "%s : out of memory.", args[0]);
1677 ret = -1;
1678 goto out;
1679 }
1680 conf_err->type = 1;
1681 conf_err->info.errorfile.status = status;
1682 conf_err->info.errorfile.msg = msg;
1683 conf_err->file = strdup(file);
1684 conf_err->line = line;
1685 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001686
1687 out:
1688 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001689
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001690}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001691
1692/* Parses the "errorfile" proxy keyword */
1693static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1694 struct proxy *defpx, const char *file, int line,
1695 char **errmsg)
1696{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001697 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001698 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001699 int status;
1700 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001701
1702 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1703 ret = 1;
1704 goto out;
1705 }
1706
1707 if (*(args[1]) == 0 || *(args[2]) == 0) {
1708 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1709 ret = -1;
1710 goto out;
1711 }
1712
1713 status = atol(args[1]);
1714 msg = http_parse_errorfile(status, args[2], errmsg);
1715 if (!msg) {
1716 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1717 ret = -1;
1718 goto out;
1719 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001720
1721 conf_err = calloc(1, sizeof(*conf_err));
1722 if (!conf_err) {
1723 memprintf(errmsg, "%s : out of memory.", args[0]);
1724 ret = -1;
1725 goto out;
1726 }
1727 conf_err->type = 1;
1728 conf_err->info.errorfile.status = status;
1729 conf_err->info.errorfile.msg = msg;
1730 conf_err->file = strdup(file);
1731 conf_err->line = line;
1732 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1733
1734 out:
1735 return ret;
1736
1737}
1738
1739/* Parses the "errorfiles" proxy keyword */
1740static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1741 struct proxy *defpx, const char *file, int line,
1742 char **err)
1743{
1744 struct conf_errors *conf_err = NULL;
1745 char *name = NULL;
1746 int rc, ret = 0;
1747
1748 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1749 ret = 1;
1750 goto out;
1751 }
1752
1753 if (!*(args[1])) {
1754 memprintf(err, "%s : expects <name> as argument.", args[0]);
1755 ret = -1;
1756 goto out;
1757 }
1758
1759 name = strdup(args[1]);
1760 conf_err = calloc(1, sizeof(*conf_err));
1761 if (!name || !conf_err) {
1762 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001763 goto error;
1764 }
1765 conf_err->type = 0;
1766
1767 conf_err->info.errorfiles.name = name;
1768 if (!*(args[2])) {
1769 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1770 conf_err->info.errorfiles.status[rc] = 1;
1771 }
1772 else {
1773 int cur_arg, status;
1774 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1775 status = atol(args[cur_arg]);
1776
1777 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1778 if (http_err_codes[rc] == status) {
1779 conf_err->info.errorfiles.status[rc] = 2;
1780 break;
1781 }
1782 }
1783 if (rc >= HTTP_ERR_SIZE) {
1784 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001785 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001786 }
1787 }
1788 }
1789 conf_err->file = strdup(file);
1790 conf_err->line = line;
1791 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1792 out:
1793 return ret;
1794
1795 error:
1796 free(name);
1797 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001798 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001799 goto out;
1800}
1801
1802/* Check "errorfiles" proxy keyword */
1803static int proxy_check_errors(struct proxy *px)
1804{
1805 struct conf_errors *conf_err, *conf_err_back;
1806 struct http_errors *http_errs;
1807 int rc, err = 0;
1808
1809 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1810 if (conf_err->type == 1) {
1811 /* errorfile */
1812 rc = http_get_status_idx(conf_err->info.errorfile.status);
1813 px->errmsg[rc] = conf_err->info.errorfile.msg;
1814 }
1815 else {
1816 /* errorfiles */
1817 list_for_each_entry(http_errs, &http_errors_list, list) {
1818 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1819 break;
1820 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001821
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001822 /* unknown http-errors section */
1823 if (&http_errs->list == &http_errors_list) {
1824 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1825 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1826 err |= ERR_ALERT | ERR_FATAL;
1827 free(conf_err->info.errorfiles.name);
1828 goto next;
1829 }
1830
1831 free(conf_err->info.errorfiles.name);
1832 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1833 if (conf_err->info.errorfiles.status[rc] > 0) {
1834 if (http_errs->errmsg[rc])
1835 px->errmsg[rc] = http_errs->errmsg[rc];
1836 else if (conf_err->info.errorfiles.status[rc] == 2)
1837 ha_warning("config: proxy '%s' : status '%d' not declared in"
1838 " http-errors section '%s' (at %s:%d).\n",
1839 px->id, http_err_codes[rc], http_errs->id,
1840 conf_err->file, conf_err->line);
1841 }
1842 }
1843 }
1844 next:
1845 LIST_DEL(&conf_err->list);
1846 free(conf_err->file);
1847 free(conf_err);
1848 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001849
1850 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001851 return err;
1852}
1853
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001854static int post_check_errors()
1855{
1856 struct ebpt_node *node;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001857 struct http_error_msg *http_errmsg;
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001858 struct htx *htx;
1859 int err_code = 0;
1860
1861 node = ebpt_first(&http_error_messages);
1862 while (node) {
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001863 http_errmsg = container_of(node, typeof(*http_errmsg), node);
1864 if (b_is_null(&http_errmsg->msg))
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001865 goto next;
Christopher Fauletb6ea17c2020-05-13 21:45:22 +02001866 htx = htxbuf(&http_errmsg->msg);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001867 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1868 ha_warning("config: errorfile '%s' runs over the buffer space"
1869 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001870 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001871 (char *)node->key);
1872 err_code |= ERR_WARN;
1873 }
1874 next:
1875 node = ebpt_next(node);
1876 }
1877
1878 return err_code;
1879}
1880
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001881int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1882{
1883 struct conf_errors *conf_err, *new_conf_err = NULL;
1884 int ret = 0;
1885
1886 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1887 new_conf_err = calloc(1, sizeof(*new_conf_err));
1888 if (!new_conf_err) {
1889 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1890 goto out;
1891 }
1892 new_conf_err->type = conf_err->type;
1893 if (conf_err->type == 1) {
1894 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1895 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1896 }
1897 else {
1898 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1899 if (!new_conf_err->info.errorfiles.name) {
1900 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1901 goto out;
1902 }
1903 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1904 sizeof(conf_err->info.errorfiles.status));
1905 }
1906 new_conf_err->file = strdup(conf_err->file);
1907 new_conf_err->line = conf_err->line;
1908 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1909 new_conf_err = NULL;
1910 }
1911 ret = 1;
1912
1913 out:
1914 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001915 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001916}
1917
1918void proxy_release_conf_errors(struct proxy *px)
1919{
1920 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001921
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001922 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1923 if (conf_err->type == 0)
1924 free(conf_err->info.errorfiles.name);
1925 LIST_DEL(&conf_err->list);
1926 free(conf_err->file);
1927 free(conf_err);
1928 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001929}
1930
1931/*
1932 * Parse an <http-errors> section.
1933 * Returns the error code, 0 if OK, or any combination of :
1934 * - ERR_ABORT: must abort ASAP
1935 * - ERR_FATAL: we can continue parsing but not start the service
1936 * - ERR_WARN: a warning has been emitted
1937 * - ERR_ALERT: an alert has been emitted
1938 * Only the two first ones can stop processing, the two others are just
1939 * indicators.
1940 */
1941static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1942{
1943 static struct http_errors *curr_errs = NULL;
1944 int err_code = 0;
1945 const char *err;
1946 char *errmsg = NULL;
1947
1948 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1949 if (!*args[1]) {
1950 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1951 err_code |= ERR_ALERT | ERR_ABORT;
1952 goto out;
1953 }
1954
1955 err = invalid_char(args[1]);
1956 if (err) {
1957 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1958 file, linenum, *err, args[0], args[1]);
1959 err_code |= ERR_ALERT | ERR_FATAL;
1960 }
1961
1962 list_for_each_entry(curr_errs, &http_errors_list, list) {
1963 /* Error if two errors section owns the same name */
1964 if (strcmp(curr_errs->id, args[1]) == 0) {
1965 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1966 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1967 err_code |= ERR_ALERT | ERR_FATAL;
1968 }
1969 }
1970
1971 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1972 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1973 err_code |= ERR_ALERT | ERR_ABORT;
1974 goto out;
1975 }
1976
1977 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1978 curr_errs->id = strdup(args[1]);
1979 curr_errs->conf.file = strdup(file);
1980 curr_errs->conf.line = linenum;
1981 }
1982 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
Christopher Fauletde30bb72020-05-14 10:03:55 +02001983 struct http_reply *reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001984 struct buffer *msg;
1985 int status, rc;
1986
1987 if (*(args[1]) == 0 || *(args[2]) == 0) {
1988 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1989 file, linenum, args[0]);
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
1993
1994 status = atol(args[1]);
1995 msg = http_parse_errorfile(status, args[2], &errmsg);
1996 if (!msg) {
1997 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1998 err_code |= ERR_ALERT | ERR_FATAL;
1999 goto out;
2000 }
Christopher Fauletde30bb72020-05-14 10:03:55 +02002001
2002 reply = calloc(1, sizeof(*reply));
2003 if (!reply) {
2004 ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]);
2005 err_code |= ERR_ALERT | ERR_FATAL;
2006 goto out;
2007 }
2008 reply->type = HTTP_REPLY_ERRMSG;
2009 reply->status = status;
2010 reply->ctype = NULL;
2011 LIST_INIT(&reply->hdrs);
2012 reply->body.errmsg = msg;
2013
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002014 rc = http_get_status_idx(status);
2015 curr_errs->errmsg[rc] = msg;
Christopher Fauletde30bb72020-05-14 10:03:55 +02002016 curr_errs->replies[rc] = reply;
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002017 }
2018 else if (*args[0] != 0) {
2019 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
2020 err_code |= ERR_ALERT | ERR_FATAL;
2021 goto out;
2022 }
2023
2024out:
2025 free(errmsg);
2026 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01002027}
2028
2029static struct cfg_kw_list cfg_kws = {ILH, {
2030 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
2031 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
2032 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
2033 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002034 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01002035 { 0, NULL, NULL },
2036}};
2037
2038INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01002039REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01002040REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01002041
Christopher Faulet35cd81d2020-01-15 11:22:56 +01002042REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
2043
Christopher Faulet29f72842019-12-11 15:52:32 +01002044/************************************************************************/
2045/* HTX sample fetches */
2046/************************************************************************/
2047
2048/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
2049static int
2050smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2051{
2052 if (!smp->strm)
2053 return 0;
2054
2055 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
2056 smp->data.type = SMP_T_BOOL;
2057 return 1;
2058}
2059
2060/* Returns the number of blocks in an HTX message. The channel is chosen
2061 * depending on the sample direction. */
2062static int
2063smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2064{
2065 struct channel *chn;
2066 struct htx *htx;
2067
2068 if (!smp->strm)
2069 return 0;
2070
2071 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002072 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002073 if (!htx)
2074 return 0;
2075
2076 smp->data.u.sint = htx_nbblks(htx);
2077 smp->data.type = SMP_T_SINT;
2078 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2079 return 1;
2080}
2081
2082/* Returns the size of an HTX message. The channel is chosen depending on the
2083 * sample direction. */
2084static int
2085smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2086{
2087 struct channel *chn;
2088 struct htx *htx;
2089
2090 if (!smp->strm)
2091 return 0;
2092
2093 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002094 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002095 if (!htx)
2096 return 0;
2097
2098 smp->data.u.sint = htx->size;
2099 smp->data.type = SMP_T_SINT;
2100 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2101 return 1;
2102}
2103
2104/* Returns the data size of an HTX message. The channel is chosen depending on the
2105 * sample direction. */
2106static int
2107smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2108{
2109 struct channel *chn;
2110 struct htx *htx;
2111
2112 if (!smp->strm)
2113 return 0;
2114
2115 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002116 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002117 if (!htx)
2118 return 0;
2119
2120 smp->data.u.sint = htx->data;
2121 smp->data.type = SMP_T_SINT;
2122 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2123 return 1;
2124}
2125
2126/* Returns the used space (data+meta) of an HTX message. The channel is chosen
2127 * depending on the sample direction. */
2128static int
2129smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2130{
2131 struct channel *chn;
2132 struct htx *htx;
2133
2134 if (!smp->strm)
2135 return 0;
2136
2137 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002138 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002139 if (!htx)
2140 return 0;
2141
2142 smp->data.u.sint = htx_used_space(htx);
2143 smp->data.type = SMP_T_SINT;
2144 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2145 return 1;
2146}
2147
2148/* Returns the free space (size-used) of an HTX message. The channel is chosen
2149 * depending on the sample direction. */
2150static int
2151smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2152{
2153 struct channel *chn;
2154 struct htx *htx;
2155
2156 if (!smp->strm)
2157 return 0;
2158
2159 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002160 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002161 if (!htx)
2162 return 0;
2163
2164 smp->data.u.sint = htx_free_space(htx);
2165 smp->data.type = SMP_T_SINT;
2166 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2167 return 1;
2168}
2169
2170/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
2171 * channel is chosen depending on the sample direction. */
2172static int
2173smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2174{
2175 struct channel *chn;
2176 struct htx *htx;
2177
2178 if (!smp->strm)
2179 return 0;
2180
2181 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002182 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002183 if (!htx)
2184 return 0;
2185
2186 smp->data.u.sint = htx_free_data_space(htx);
2187 smp->data.type = SMP_T_SINT;
2188 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2189 return 1;
2190}
2191
2192/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
2193 * 0. Concretely, it only checks the tail. The channel is chosen depending on
2194 * the sample direction. */
2195static int
2196smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2197{
2198 struct channel *chn;
2199 struct htx *htx;
2200
2201 if (!smp->strm)
2202 return 0;
2203
2204 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002205 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002206 if (!htx)
2207 return 0;
2208
2209 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
2210 smp->data.type = SMP_T_BOOL;
2211 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2212 return 1;
2213}
2214
2215/* Returns the type of a specific HTX block, if found in the message. Otherwise
2216 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
2217 * "head", "tail" or "first". The channel is chosen depending on the sample
2218 * direction. */
2219static int
2220smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2221{
2222 struct channel *chn;
2223 struct htx *htx;
2224 enum htx_blk_type type;
2225 int32_t pos;
2226
2227 if (!smp->strm || !arg_p)
2228 return 0;
2229
2230 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002231 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002232 if (!htx)
2233 return 0;
2234
2235 pos = arg_p[0].data.sint;
2236 if (pos == -1)
2237 type = htx_get_head_type(htx);
2238 else if (pos == -2)
2239 type = htx_get_tail_type(htx);
2240 else if (pos == -3)
2241 type = htx_get_first_type(htx);
2242 else
2243 type = ((pos >= htx->head && pos <= htx->tail)
2244 ? htx_get_blk_type(htx_get_blk(htx, pos))
2245 : HTX_BLK_UNUSED);
2246
2247 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
2248 smp->data.type = SMP_T_STR;
2249 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2250 return 1;
2251}
2252
2253/* Returns the size of a specific HTX block, if found in the message. Otherwise
2254 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
2255 * "first". The channel is chosen depending on the sample direction. */
2256static int
2257smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2258{
2259 struct channel *chn;
2260 struct htx *htx;
2261 struct htx_blk *blk;
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 blk = htx_get_head_blk(htx);
2275 else if (pos == -2)
2276 blk = htx_get_tail_blk(htx);
2277 else if (pos == -3)
2278 blk = htx_get_first_blk(htx);
2279 else
2280 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2281
2282 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
2283 smp->data.type = SMP_T_SINT;
2284 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2285 return 1;
2286}
2287
2288/* Returns the start-line if the selected HTX block exists and is a
2289 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
2290 * supported or "head", "tail" or "first". The channel is chosen depending on
2291 * the sample direction. */
2292static int
2293smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2294{
2295 struct buffer *temp;
2296 struct channel *chn;
2297 struct htx *htx;
2298 struct htx_blk *blk;
2299 struct htx_sl *sl;
2300 int32_t pos;
2301
2302 if (!smp->strm || !arg_p)
2303 return 0;
2304
2305 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002306 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002307 if (!htx)
2308 return 0;
2309
2310 pos = arg_p[0].data.sint;
2311 if (pos == -1)
2312 blk = htx_get_head_blk(htx);
2313 else if (pos == -2)
2314 blk = htx_get_tail_blk(htx);
2315 else if (pos == -3)
2316 blk = htx_get_first_blk(htx);
2317 else
2318 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2319
2320 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
2321 smp->data.u.str.size = 0;
2322 smp->data.u.str.area = "";
2323 smp->data.u.str.data = 0;
2324 }
2325 else {
2326 sl = htx_get_blk_ptr(htx, blk);
2327
2328 temp = get_trash_chunk();
2329 chunk_istcat(temp, htx_sl_p1(sl));
2330 temp->area[temp->data++] = ' ';
2331 chunk_istcat(temp, htx_sl_p2(sl));
2332 temp->area[temp->data++] = ' ';
2333 chunk_istcat(temp, htx_sl_p3(sl));
2334
2335 smp->data.u.str = *temp;
2336 }
2337
2338 smp->data.type = SMP_T_STR;
2339 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2340 return 1;
2341}
2342
2343/* Returns the header name if the selected HTX block exists and is a header or a
2344 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2345 * supported or "head", "tail" or "first". The channel is chosen depending on
2346 * the sample direction. */
2347static int
2348smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2349{
2350 struct channel *chn;
2351 struct htx *htx;
2352 struct htx_blk *blk;
2353 int32_t pos;
2354
2355 if (!smp->strm || !arg_p)
2356 return 0;
2357
2358 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002359 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002360 if (!htx)
2361 return 0;
2362
2363 pos = arg_p[0].data.sint;
2364 if (pos == -1)
2365 blk = htx_get_head_blk(htx);
2366 else if (pos == -2)
2367 blk = htx_get_tail_blk(htx);
2368 else if (pos == -3)
2369 blk = htx_get_first_blk(htx);
2370 else
2371 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2372
2373 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2374 smp->data.u.str.size = 0;
2375 smp->data.u.str.area = "";
2376 smp->data.u.str.data = 0;
2377 }
2378 else {
2379 struct ist name = htx_get_blk_name(htx, blk);
2380
2381 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
2382 }
2383 smp->data.type = SMP_T_STR;
2384 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2385 return 1;
2386}
2387
2388/* Returns the header value if the selected HTX block exists and is a header or
2389 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
2390 * supported or "head", "tail" or "first". The channel is chosen depending on
2391 * the sample direction. */
2392static int
2393smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
2394{
2395 struct channel *chn;
2396 struct htx *htx;
2397 struct htx_blk *blk;
2398 int32_t pos;
2399
2400 if (!smp->strm || !arg_p)
2401 return 0;
2402
2403 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002404 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002405 if (!htx)
2406 return 0;
2407
2408 pos = arg_p[0].data.sint;
2409 if (pos == -1)
2410 blk = htx_get_head_blk(htx);
2411 else if (pos == -2)
2412 blk = htx_get_tail_blk(htx);
2413 else if (pos == -3)
2414 blk = htx_get_first_blk(htx);
2415 else
2416 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2417
2418 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
2419 smp->data.u.str.size = 0;
2420 smp->data.u.str.area = "";
2421 smp->data.u.str.data = 0;
2422 }
2423 else {
2424 struct ist val = htx_get_blk_value(htx, blk);
2425
2426 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2427 }
2428 smp->data.type = SMP_T_STR;
2429 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2430 return 1;
2431}
2432
2433/* Returns the value if the selected HTX block exists and is a data
2434 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
2435 * or "head", "tail" or "first". The channel is chosen depending on the sample
2436 * direction. */
2437static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01002438smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01002439{
2440 struct channel *chn;
2441 struct htx *htx;
2442 struct htx_blk *blk;
2443 int32_t pos;
2444
2445 if (!smp->strm || !arg_p)
2446 return 0;
2447
2448 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002449 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01002450 if (!htx)
2451 return 0;
2452
2453 pos = arg_p[0].data.sint;
2454 if (pos == -1)
2455 blk = htx_get_head_blk(htx);
2456 else if (pos == -2)
2457 blk = htx_get_tail_blk(htx);
2458 else if (pos == -3)
2459 blk = htx_get_first_blk(htx);
2460 else
2461 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
2462
2463 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
2464 smp->data.u.str.size = 0;
2465 smp->data.u.str.area = "";
2466 smp->data.u.str.data = 0;
2467 }
2468 else {
2469 struct ist val = htx_get_blk_value(htx, blk);
2470
2471 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
2472 }
Christopher Faulet8178e402020-01-08 14:38:58 +01002473 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01002474 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
2475 return 1;
2476}
2477
2478/* This function is used to validate the arguments passed to any "htx_blk" fetch
2479 * keywords. An argument is expected by these keywords. It must be a positive
2480 * integer or on of the following strings: "head", "tail" or "first". It returns
2481 * 0 on error, and a non-zero value if OK.
2482 */
2483int val_blk_arg(struct arg *arg, char **err_msg)
2484{
2485 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
2486 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
2487 return 0;
2488 }
2489 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
2490 free(arg[0].data.str.area);
2491 arg[0].type = ARGT_SINT;
2492 arg[0].data.sint = -1;
2493 }
2494 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
2495 free(arg[0].data.str.area);
2496 arg[0].type = ARGT_SINT;
2497 arg[0].data.sint = -2;
2498 }
2499 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
2500 free(arg[0].data.str.area);
2501 arg[0].type = ARGT_SINT;
2502 arg[0].data.sint = -3;
2503 }
2504 else {
2505 int pos;
2506
2507 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01002508 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01002509 memprintf(err_msg, "invalid block position");
2510 return 0;
2511 }
2512 }
2513
2514 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
2515 if (pos < 0) {
2516 memprintf(err_msg, "block position must not be negative");
2517 return 0;
2518 }
2519 free(arg[0].data.str.area);
2520 arg[0].type = ARGT_SINT;
2521 arg[0].data.sint = pos;
2522 }
2523
2524 return 1;
2525}
2526
2527
2528/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05002529 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01002530 */
2531static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01002532 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01002533
Christopher Faulet01f44452020-01-08 14:23:40 +01002534 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2535 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2536 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2537 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2538 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2539 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2540 { "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 +01002541
Christopher Faulet01f44452020-01-08 14:23:40 +01002542 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2543 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
2544 { "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},
2545 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
2546 { "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 +01002547 { "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 +01002548
2549 { /* END */ },
2550}};
2551
2552INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);