blob: d818170f2b6ae6845ceb4af653dde4c06a407b1f [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 Faulet58857752020-01-15 15:19:50 +010032struct eb_root http_error_messages = EB_ROOT;
Christopher Faulet35cd81d2020-01-15 11:22:56 +010033struct list http_errors_list = LIST_HEAD_INIT(http_errors_list);
Christopher Fauleta7b677c2018-11-29 16:48:49 +010034
Christopher Faulet76edc0f2020-01-13 15:52:01 +010035/* The declaration of an errorfiles/errorfile directives. Used during config
36 * parsing only. */
37struct conf_errors {
38 char type; /* directive type (0: errorfiles, 1: errorfile) */
39 union {
40 struct {
41 int status; /* the status code associated to this error */
42 struct buffer *msg; /* the HTX error message */
43 } errorfile; /* describe an "errorfile" directive */
44 struct {
45 char *name; /* the http-errors section name */
46 char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */
47 } errorfiles; /* describe an "errorfiles" directive */
48 } info;
49
50 char *file; /* file where the directive appears */
51 int line; /* line where the directive appears */
52
53 struct list list; /* next conf_errors */
54};
55
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +020056static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host);
57static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri);
58
Christopher Faulet297fbb42019-05-13 14:41:27 +020059/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020060 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020061 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020062 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020063struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020064{
Christopher Faulet297fbb42019-05-13 14:41:27 +020065 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010066
Christopher Faulet29f17582019-05-23 11:03:26 +020067 BUG_ON(htx->first == -1);
68 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020069 if (!blk)
70 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020071 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +020072 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020073}
74
75/* Finds the first or next occurrence of header <name> in the HTX message <htx>
76 * using the context <ctx>. This structure holds everything necessary to use the
77 * header and find next occurrence. If its <blk> member is NULL, the header is
78 * searched from the beginning. Otherwise, the next occurrence is returned. The
79 * function returns 1 when it finds a value, and 0 when there is no more. It is
80 * designed to work with headers defined as comma-separated lists. If <full> is
81 * set, it works on full-line headers in whose comma is not a delimiter but is
82 * part of the syntax. A special case, if ctx->value is NULL when searching for
83 * a new values of a header, the current header is rescanned. This allows
84 * rescanning after a header deletion.
85 */
86int http_find_header(const struct htx *htx, const struct ist name,
87 struct http_hdr_ctx *ctx, int full)
88{
89 struct htx_blk *blk = ctx->blk;
90 struct ist n, v;
91 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +020092
93 if (blk) {
94 char *p;
95
Christopher Faulet47596d32018-10-22 09:17:28 +020096 if (!ctx->value.ptr)
97 goto rescan_hdr;
98 if (full)
99 goto next_blk;
100 v = htx_get_blk_value(htx, blk);
101 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
102 v.len -= (p - v.ptr);
103 v.ptr = p;
104 if (!v.len)
105 goto next_blk;
106 /* Skip comma */
107 if (*(v.ptr) == ',') {
108 v.ptr++;
109 v.len--;
110 }
111
112 goto return_hdr;
113 }
114
Christopher Faulet192c6a22019-06-11 16:32:24 +0200115 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200116 return 0;
117
Christopher Fauleta3f15502019-05-13 15:27:23 +0200118 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200119 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200120 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100121 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
122 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200123 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200124 continue;
Christopher Faulet47596d32018-10-22 09:17:28 +0200125 if (name.len) {
126 /* If no name was passed, we want any header. So skip the comparison */
127 n = htx_get_blk_name(htx, blk);
128 if (!isteqi(n, name))
129 goto next_blk;
130 }
131 v = htx_get_blk_value(htx, blk);
132
133 return_hdr:
134 ctx->lws_before = 0;
135 ctx->lws_after = 0;
136 while (v.len && HTTP_IS_LWS(*v.ptr)) {
137 v.ptr++;
138 v.len--;
139 ctx->lws_before++;
140 }
141 if (!full)
142 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
143 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
144 v.len--;
145 ctx->lws_after++;
146 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200147 ctx->blk = blk;
148 ctx->value = v;
149 return 1;
150
151 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200152 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200153 }
154
155 ctx->blk = NULL;
156 ctx->value = ist("");
157 ctx->lws_before = ctx->lws_after = 0;
158 return 0;
159}
160
161/* Adds a header block int the HTX message <htx>, just before the EOH block. It
162 * returns 1 on success, otherwise it returns 0.
163 */
164int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
165{
166 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200167 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200168 enum htx_blk_type type = htx_get_tail_type(htx);
169 int32_t prev;
170
171 blk = htx_add_header(htx, n, v);
172 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200173 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200174
175 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200176 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200177
178 /* <blk> is the head, swap it iteratively with its predecessor to place
179 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200180 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200181 struct htx_blk *pblk = htx_get_blk(htx, prev);
182 enum htx_blk_type type = htx_get_blk_type(pblk);
183
184 /* Swap .addr and .info fields */
185 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
186 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
187
188 if (blk->addr == pblk->addr)
189 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200190
191 /* Stop when end-of-header is reached */
192 if (type == HTX_BLK_EOH)
193 break;
194
195 blk = pblk;
196 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200197
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200198 end:
199 sl = http_get_stline(htx);
200 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(n, ist("host"))) {
201 if (!http_update_authority(htx, sl, v))
202 goto fail;
203 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200204 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200205
206 fail:
207 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200208}
209
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100210/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200211 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200212 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100213int 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 +0200214{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200215 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200216
Christopher Faulet29f17582019-05-23 11:03:26 +0200217 blk = htx_get_first_blk(htx);
218 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200219 return 0;
220 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200221}
222
Christopher Faulete010c802018-10-24 10:36:45 +0200223/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
224 * on success, otherwise 0.
225 */
226int http_replace_req_meth(struct htx *htx, const struct ist meth)
227{
228 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200229 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100230 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200231
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100232 if (!sl)
233 return 0;
234
Christopher Faulete010c802018-10-24 10:36:45 +0200235 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100236 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
237 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200238
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100239 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
240 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200241
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100242 /* create the new start line */
243 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
244 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200245}
246
247/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
248 * success, otherwise 0.
249 */
250int http_replace_req_uri(struct htx *htx, const struct ist uri)
251{
252 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200253 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100254 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200255
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100256 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200257 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100258
Christopher Faulete010c802018-10-24 10:36:45 +0200259 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100260 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
261 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200262
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100263 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
264 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200265
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100266 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200267 if (!http_replace_stline(htx, meth, uri, vsn))
268 goto fail;
269
270 sl = http_get_stline(htx);
271 if (!http_update_host(htx, sl, uri))
272 goto fail;
273
274 return 1;
275 fail:
276 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200277}
278
279/* Replace the request path in the HTX message <htx> by <path>. The host part
280 * and the query string are preserved. It returns 1 on success, otherwise 0.
281 */
282int http_replace_req_path(struct htx *htx, const struct ist path)
283{
284 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200285 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100286 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200287 size_t plen = 0;
288
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100289 if (!sl)
290 return 0;
291
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100292 uri = htx_sl_req_uri(sl);
293 p = http_get_path(uri);
Christopher Faulete010c802018-10-24 10:36:45 +0200294 if (!p.ptr)
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100295 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200296 while (plen < p.len && *(p.ptr + plen) != '?')
297 plen++;
298
299 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100300 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
301 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200302
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100303 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
304 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
305
306 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200307 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
308 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100309 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200310
311 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100312 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200313}
314
315/* Replace the request query-string in the HTX message <htx> by <query>. The
316 * host part and the path are preserved. It returns 1 on success, otherwise
317 * 0.
318 */
319int http_replace_req_query(struct htx *htx, const struct ist query)
320{
321 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200322 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100323 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200324 int offset = 1;
325
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100326 if (!sl)
327 return 0;
328
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329 uri = htx_sl_req_uri(sl);
330 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200331 while (q.len > 0 && *(q.ptr) != '?') {
332 q.ptr++;
333 q.len--;
334 }
335
336 /* skip the question mark or indicate that we must insert it
337 * (but only if the format string is not empty then).
338 */
339 if (q.len) {
340 q.ptr++;
341 q.len--;
342 }
343 else if (query.len > 1)
344 offset = 0;
345
346 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100347 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
348 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200349
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
351 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200352
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100353 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
354 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
355 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200356
357 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100358 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200359}
360
361/* Replace the response status in the HTX message <htx> by <status>. It returns
362 * 1 on success, otherwise 0.
363*/
364int http_replace_res_status(struct htx *htx, const struct ist status)
365{
366 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200367 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100368 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200369
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100370 if (!sl)
371 return 0;
372
Christopher Faulete010c802018-10-24 10:36:45 +0200373 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100374 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
375 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200376
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100377 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
378 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200379
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 /* create the new start line */
381 sl->info.res.status = strl2ui(status.ptr, status.len);
382 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200383}
384
385/* Replace the response reason in the HTX message <htx> by <reason>. It returns
386 * 1 on success, otherwise 0.
387*/
388int http_replace_res_reason(struct htx *htx, const struct ist reason)
389{
390 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200391 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100392 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200393
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100394 if (!sl)
395 return 0;
396
Christopher Faulete010c802018-10-24 10:36:45 +0200397 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100398 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
399 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200400
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100401 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
402 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200403
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100404 /* create the new start line */
405 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200406}
407
Christopher Faulet47596d32018-10-22 09:17:28 +0200408/* Replaces a part of a header value referenced in the context <ctx> by
409 * <data>. It returns 1 on success, otherwise it returns 0. The context is
410 * updated if necessary.
411 */
412int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
413{
414 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200415 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200416 char *start;
417 struct ist v;
418 uint32_t len, off;
419
420 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200421 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200422
423 v = htx_get_blk_value(htx, blk);
424 start = ctx->value.ptr - ctx->lws_before;
425 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
426 off = start - v.ptr;
427
428 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
429 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200430 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200431
432 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200433
434 sl = http_get_stline(htx);
435 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
436 struct ist n = htx_get_blk_name(htx, blk);
437
438 if (isteq(n, ist("host"))) {
439 if (!http_update_authority(htx, sl, v))
440 goto fail;
441 ctx->blk = NULL;
442 http_find_header(htx, ist("host"), ctx, 1);
443 blk = ctx->blk;
444 v = htx_get_blk_value(htx, blk);
445 }
446 }
447
Christopher Faulet47596d32018-10-22 09:17:28 +0200448 ctx->blk = blk;
449 ctx->value.ptr = v.ptr + off;
450 ctx->value.len = data.len;
451 ctx->lws_before = ctx->lws_after = 0;
452
453 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200454 fail:
455 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200456}
457
458/* Fully replaces a header referenced in the context <ctx> by the name <name>
459 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
460 * context is updated if necessary.
461 */
462int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
463 const struct ist name, const struct ist value)
464{
465 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200466 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200467
468 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200469 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200470
471 blk = htx_replace_header(htx, blk, name, value);
472 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200473 goto fail;
474
475 sl = http_get_stline(htx);
476 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(name, ist("host"))) {
477 if (!http_update_authority(htx, sl, value))
478 goto fail;
479 ctx->blk = NULL;
480 http_find_header(htx, ist("host"), ctx, 1);
481 blk = ctx->blk;
482 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200483
484 ctx->blk = blk;
485 ctx->value = ist(NULL);
486 ctx->lws_before = ctx->lws_after = 0;
487
488 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200489 fail:
490 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200491}
492
493/* Remove one value of a header. This only works on a <ctx> returned by
494 * http_find_header function. The value is removed, as well as surrounding commas
495 * if any. If the removed value was alone, the whole header is removed. The
496 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
497 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
498 * form that can be handled by http_find_header() to find next occurrence.
499 */
500int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
501{
502 struct htx_blk *blk = ctx->blk;
503 char *start;
504 struct ist v;
505 uint32_t len;
506
507 if (!blk)
508 return 0;
509
510 start = ctx->value.ptr - ctx->lws_before;
511 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
512
513 v = htx_get_blk_value(htx, blk);
514 if (len == v.len) {
515 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200516 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200517 ctx->blk = blk;
518 ctx->value = ist2(NULL, 0);
519 ctx->lws_before = ctx->lws_after = 0;
520 }
521 else {
522 ctx->blk = htx_get_blk(htx, htx->tail);
523 ctx->value = htx_get_blk_value(htx, ctx->blk);
524 ctx->lws_before = ctx->lws_after = 0;
525 }
526 return 1;
527 }
528
529 /* This was not the only value of this header. We have to remove the
530 * part pointed by ctx->value. If it is the last entry of the list, we
531 * remove the last separator.
532 */
533 if (start == v.ptr) {
534 /* It's the first header part but not the only one. So remove
535 * the comma after it. */
536 len++;
537 }
538 else {
539 /* There is at least one header part before the removed one. So
540 * remove the comma between them. */
541 start--;
542 len++;
543 }
544 /* Update the block content and its len */
545 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200546 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200547
548 /* Finally update the ctx */
549 ctx->value.ptr = start;
550 ctx->value.len = 0;
551 ctx->lws_before = ctx->lws_after = 0;
552
553 return 1;
554}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200555
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200556/* Updates the authority part of the uri with the value <host>. It happens when
557 * the header host is modified. It returns 0 on failure and 1 on success. It is
558 * the caller responsibility to provide the start-line and to be sure the uri
559 * contains an authority. Thus, if no authority is found in the uri, an error is
560 * returned.
561 */
562static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
563{
564 struct buffer *temp = get_trash_chunk();
565 struct ist meth, vsn, uri, authority;
566
567 uri = htx_sl_req_uri(sl);
568 authority = http_get_authority(uri, 1);
569 if (!authority.len || isteq(host, authority))
570 return 0;
571
572 /* Start by copying old method and version */
573 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
574 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
575
576 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
577 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
578
579 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
580 chunk_memcat(temp, host.ptr, host.len);
581 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
582 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
583
584 return http_replace_stline(htx, meth, uri, vsn);
585
586}
587
588/* Update the header host by extracting the authority of the uri <uri>. flags of
589 * the start-line are also updated accordingly. For orgin-form and asterisk-form
590 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
591 * removed from the flags of the start-line. Otherwise, this flag is set and the
592 * authority is used to set the value of the header host. This function returns
593 * 0 on failure and 1 on success.
594*/
595static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
596{
597 struct ist authority;
598 struct http_hdr_ctx ctx;
599
600 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
601 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
602 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
603 }
604 else {
605 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
606 if (sl->info.req.meth != HTTP_METH_CONNECT) {
607 // absolute-form (RFC7320 #5.3.2)
608 sl->flags |= HTX_SL_F_HAS_SCHM;
609 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
610 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
611
612 authority = http_get_authority(uri, 1);
613 if (!authority.len)
614 goto fail;
615 }
616 else {
617 // authority-form (RFC7320 #5.3.3)
618 authority = uri;
619 }
620
621 /* Replace header host value */
622 ctx.blk = NULL;
623 while (http_find_header(htx, ist("host"), &ctx, 1)) {
624 if (!http_replace_header_value(htx, &ctx, authority))
625 goto fail;
626 }
627
628 }
629 return 1;
630 fail:
631 return 0;
632}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200633
634/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
635 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
636 * performed over the whole headers. Otherwise it must contain a valid header
637 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
638 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
639 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
640 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
641 * -1. The value fetch stops at commas, so this function is suited for use with
642 * list headers.
643 * The return value is 0 if nothing was found, or non-zero otherwise.
644 */
645unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
646 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
647{
648 struct http_hdr_ctx local_ctx;
649 struct ist val_hist[MAX_HDR_HISTORY];
650 unsigned int hist_idx;
651 int found;
652
653 if (!ctx) {
654 local_ctx.blk = NULL;
655 ctx = &local_ctx;
656 }
657
658 if (occ >= 0) {
659 /* search from the beginning */
660 while (http_find_header(htx, hdr, ctx, 0)) {
661 occ--;
662 if (occ <= 0) {
663 *vptr = ctx->value.ptr;
664 *vlen = ctx->value.len;
665 return 1;
666 }
667 }
668 return 0;
669 }
670
671 /* negative occurrence, we scan all the list then walk back */
672 if (-occ > MAX_HDR_HISTORY)
673 return 0;
674
675 found = hist_idx = 0;
676 while (http_find_header(htx, hdr, ctx, 0)) {
677 val_hist[hist_idx] = ctx->value;
678 if (++hist_idx >= MAX_HDR_HISTORY)
679 hist_idx = 0;
680 found++;
681 }
682 if (-occ > found)
683 return 0;
684
685 /* OK now we have the last occurrence in [hist_idx-1], and we need to
686 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
687 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
688 * to remain in the 0..9 range.
689 */
690 hist_idx += occ + MAX_HDR_HISTORY;
691 if (hist_idx >= MAX_HDR_HISTORY)
692 hist_idx -= MAX_HDR_HISTORY;
693 *vptr = val_hist[hist_idx].ptr;
694 *vlen = val_hist[hist_idx].len;
695 return 1;
696}
697
698/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
699 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
700 * performed over the whole headers. Otherwise it must contain a valid header
701 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
702 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
703 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
704 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
705 * -1. This function differs from http_get_hdr() in that it only returns full
706 * line header values and does not stop at commas.
707 * The return value is 0 if nothing was found, or non-zero otherwise.
708 */
709unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
710 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
711{
712 struct http_hdr_ctx local_ctx;
713 struct ist val_hist[MAX_HDR_HISTORY];
714 unsigned int hist_idx;
715 int found;
716
717 if (!ctx) {
718 local_ctx.blk = NULL;
719 ctx = &local_ctx;
720 }
721
722 if (occ >= 0) {
723 /* search from the beginning */
724 while (http_find_header(htx, hdr, ctx, 1)) {
725 occ--;
726 if (occ <= 0) {
727 *vptr = ctx->value.ptr;
728 *vlen = ctx->value.len;
729 return 1;
730 }
731 }
732 return 0;
733 }
734
735 /* negative occurrence, we scan all the list then walk back */
736 if (-occ > MAX_HDR_HISTORY)
737 return 0;
738
739 found = hist_idx = 0;
740 while (http_find_header(htx, hdr, ctx, 1)) {
741 val_hist[hist_idx] = ctx->value;
742 if (++hist_idx >= MAX_HDR_HISTORY)
743 hist_idx = 0;
744 found++;
745 }
746 if (-occ > found)
747 return 0;
748
749 /* OK now we have the last occurrence in [hist_idx-1], and we need to
750 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
751 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
752 * to remain in the 0..9 range.
753 */
754 hist_idx += occ + MAX_HDR_HISTORY;
755 if (hist_idx >= MAX_HDR_HISTORY)
756 hist_idx -= MAX_HDR_HISTORY;
757 *vptr = val_hist[hist_idx].ptr;
758 *vlen = val_hist[hist_idx].len;
759 return 1;
760}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100761
Christopher Faulet90cc4812019-07-22 16:49:30 +0200762int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100763{
764 struct htx *htx;
765 struct htx_sl *sl;
766 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200767 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100768 union h1_sl h1sl;
769 unsigned int flags = HTX_SL_F_IS_RESP;
770 int ret = 0;
771
Christopher Faulet90cc4812019-07-22 16:49:30 +0200772 b_reset(buf);
773 if (!raw.len) {
774 buf->size = 0;
775 buf->area = malloc(raw.len);
776 return 1;
777 }
778
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100779 buf->size = global.tune.bufsize;
780 buf->area = (char *)malloc(buf->size);
781 if (!buf->area)
782 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100783
784 h1m_init_res(&h1m);
785 h1m.flags |= H1_MF_NO_PHDR;
786 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
787 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
788 if (ret <= 0)
789 goto error;
790
791 if (unlikely(h1sl.st.v.len != 8))
792 goto error;
793 if ((*(h1sl.st.v.ptr + 5) > '1') ||
794 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
795 h1m.flags |= H1_MF_VER_11;
796
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200797 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
798 goto error;
799
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100800 if (h1m.flags & H1_MF_VER_11)
801 flags |= HTX_SL_F_VER_11;
802 if (h1m.flags & H1_MF_XFER_ENC)
803 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200804 if (h1m.flags & H1_MF_CLEN) {
805 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
806 if (h1m.body_len == 0)
807 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100808 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200809 if (h1m.flags & H1_MF_CHNK)
810 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100811
812 htx = htx_from_buf(buf);
813 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
814 if (!sl || !htx_add_all_headers(htx, hdrs))
815 goto error;
816 sl->info.res.status = h1sl.st.status;
817
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200818 while (raw.len > ret) {
819 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
820 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100821 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200822 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100823 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200824
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100825 if (!htx_add_endof(htx, HTX_BLK_EOM))
826 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200827
Christopher Faulet90cc4812019-07-22 16:49:30 +0200828 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100829
830error:
831 if (buf->size)
832 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200833 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100834}
835
836static int http_htx_init(void)
837{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100838 struct buffer chk;
839 struct ist raw;
840 int rc;
841 int err_code = 0;
842
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100843 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
844 if (!http_err_msgs[rc]) {
845 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
846 err_code |= ERR_ALERT | ERR_FATAL;
847 continue;
848 }
849
850 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
851 if (!http_str_to_htx(&chk, raw)) {
852 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
853 http_err_codes[rc]);
854 err_code |= ERR_ALERT | ERR_FATAL;
855 }
Christopher Fauletf7346382019-07-17 22:02:08 +0200856 http_err_chunks[rc] = chk;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100857 }
858end:
859 return err_code;
860}
861
Christopher Faulet58857752020-01-15 15:19:50 +0100862static void http_htx_deinit(void)
863{
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100864 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +0100865 struct ebpt_node *node, *next;
866 struct http_error *http_err;
867
868 node = ebpt_first(&http_error_messages);
869 while (node) {
870 next = ebpt_next(node);
871 ebpt_delete(node);
872 http_err = container_of(node, typeof(*http_err), node);
873 chunk_destroy(&http_err->msg);
874 free(node->key);
875 free(http_err);
876 node = next;
877 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100878
879 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
880 free(http_errs->conf.file);
881 free(http_errs->id);
882 LIST_DEL(&http_errs->list);
883 free(http_errs);
884 }
Christopher Faulet58857752020-01-15 15:19:50 +0100885}
886
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100887REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +0100888REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +0100889
Christopher Faulet58857752020-01-15 15:19:50 +0100890/* Reads content of the error file <file> and convert it into an HTX message. On
891 * success, the HTX message is returned. On error, NULL is returned and an error
892 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +0100893 */
Christopher Faulet58857752020-01-15 15:19:50 +0100894struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +0100895{
Christopher Faulet58857752020-01-15 15:19:50 +0100896 struct buffer *buf = NULL;
897 struct buffer chk;
898 struct ebpt_node *node;
899 struct http_error *http_err;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100900 struct stat stat;
901 char *err = NULL;
902 int errnum, errlen;
903 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +0100904
905 /* already loaded */
906 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
907 if (node) {
908 http_err = container_of(node, typeof(*http_err), node);
909 buf = &http_err->msg;
910 goto out;
911 }
Christopher Faulet5031ef52020-01-15 11:22:07 +0100912
Christopher Faulet58857752020-01-15 15:19:50 +0100913 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +0100914 fd = open(file, O_RDONLY);
915 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
916 memprintf(errmsg, "error opening file '%s'.", file);
917 goto out;
918 }
919
920 if (stat.st_size <= global.tune.bufsize)
921 errlen = stat.st_size;
922 else {
923 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
924 file, global.tune.bufsize);
925 errlen = global.tune.bufsize;
926 }
927
928 err = malloc(errlen);
929 if (!err) {
930 memprintf(errmsg, "out of memory.");
931 goto out;
932 }
933
934 errnum = read(fd, err, errlen);
935 if (errnum != errlen) {
936 memprintf(errmsg, "error reading file '%s'.", file);
937 goto out;
938 }
939
Christopher Faulet58857752020-01-15 15:19:50 +0100940 /* Create the node corresponding to the error file */
941 http_err = calloc(1, sizeof(*http_err));
942 if (!http_err) {
943 memprintf(errmsg, "out of memory.");
944 goto out;
945 }
946 http_err->node.key = strdup(file);
947 if (!http_err->node.key) {
948 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +0100949 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +0100950 goto out;
951 }
952
953 /* Convert the error file into an HTX message */
954 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +0100955 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Faulet58857752020-01-15 15:19:50 +0100956 free(http_err->node.key);
957 free(http_err);
Christopher Faulet5031ef52020-01-15 11:22:07 +0100958 goto out;
959 }
960
Christopher Faulet58857752020-01-15 15:19:50 +0100961 /* Insert the node in the tree and return the HTX message */
962 http_err->msg = chk;
963 ebis_insert(&http_error_messages, &http_err->node);
964 buf = &http_err->msg;
965
Christopher Faulet5031ef52020-01-15 11:22:07 +0100966 out:
967 if (fd >= 0)
968 close(fd);
969 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +0100970 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100971}
972
Christopher Faulet58857752020-01-15 15:19:50 +0100973/* Convert the raw http message <msg> into an HTX message. On sucess, the HTX
974 * message is returned. On error, NULL is returned and an error message is
975 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +0100976 */
Christopher Faulet58857752020-01-15 15:19:50 +0100977struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +0100978{
Christopher Faulet58857752020-01-15 15:19:50 +0100979 struct buffer *buf = NULL;
980 struct buffer chk;
981 struct ebpt_node *node;
982 struct http_error *http_err;
983
984 /* already loaded */
985 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
986 if (node) {
987 http_err = container_of(node, typeof(*http_err), node);
988 buf = &http_err->msg;
989 goto out;
990 }
991 /* Create the node corresponding to the error file */
992 http_err = calloc(1, sizeof(*http_err));
993 if (!http_err) {
994 memprintf(errmsg, "out of memory.");
995 goto out;
996 }
997 http_err->node.key = strdup(key);
998 if (!http_err->node.key) {
999 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001000 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +01001001 goto out;
1002 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001003
1004 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001005 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001006 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Faulet58857752020-01-15 15:19:50 +01001007 free(http_err->node.key);
1008 free(http_err);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001009 goto out;
1010 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001011
Christopher Faulet58857752020-01-15 15:19:50 +01001012 /* Insert the node in the tree and return the HTX message */
1013 http_err->msg = chk;
1014 ebis_insert(&http_error_messages, &http_err->node);
1015 buf = &http_err->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001016 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001017 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001018}
1019
Christopher Faulet5031ef52020-01-15 11:22:07 +01001020/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001021 * <status>. It returns NULL if there is any error, otherwise it return the
1022 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001023 */
Christopher Faulet58857752020-01-15 15:19:50 +01001024struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001025{
Christopher Faulet58857752020-01-15 15:19:50 +01001026 struct buffer *buf = NULL;
1027 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001028
1029 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1030 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001031 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001032 break;
1033 }
1034 }
1035
1036 if (rc >= HTTP_ERR_SIZE)
1037 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001038 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001039}
1040
1041/* This function creates HTX error message corresponding to a redirect message
1042 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001043 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1044 * returns NULL if there is any error, otherwise it return the corresponding HTX
1045 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001046 */
Christopher Faulet58857752020-01-15 15:19:50 +01001047struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001048{
Christopher Faulet58857752020-01-15 15:19:50 +01001049 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001050 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001051 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001052 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001053
1054 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1055 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001056 /* Create the error key */
1057 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1058 memprintf(errmsg, "out of memory.");
1059 goto out;
1060 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001061 /* Create the error message */
1062 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1063 errlen = strlen(msg) + strlen(url) + 5;
1064 err = malloc(errlen);
1065 if (!err) {
1066 memprintf(errmsg, "out of memory.");
1067 goto out;
1068 }
1069 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1070
1071 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001072 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001073 break;
1074 }
1075 }
1076
1077 if (rc >= HTTP_ERR_SIZE)
1078 memprintf(errmsg, "status code '%d' not handled.", status);
1079out:
Christopher Faulet58857752020-01-15 15:19:50 +01001080 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001081 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001082 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001083}
1084
Christopher Faulet07f41f72020-01-16 16:16:06 +01001085/* Parses the "errorloc[302|303]" proxy keyword */
1086static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1087 struct proxy *defpx, const char *file, int line,
1088 char **errmsg)
1089{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001090 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001091 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001092 int errloc, status;
1093 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001094
1095 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1096 ret = 1;
1097 goto out;
1098 }
1099
1100 if (*(args[1]) == 0 || *(args[2]) == 0) {
1101 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1102 ret = -1;
1103 goto out;
1104 }
1105
1106 status = atol(args[1]);
1107 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1108 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1109 if (!msg) {
1110 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1111 ret = -1;
1112 goto out;
1113 }
1114
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001115 conf_err = calloc(1, sizeof(*conf_err));
1116 if (!conf_err) {
1117 memprintf(errmsg, "%s : out of memory.", args[0]);
1118 ret = -1;
1119 goto out;
1120 }
1121 conf_err->type = 1;
1122 conf_err->info.errorfile.status = status;
1123 conf_err->info.errorfile.msg = msg;
1124 conf_err->file = strdup(file);
1125 conf_err->line = line;
1126 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001127
1128 out:
1129 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001130
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001131}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001132
1133/* Parses the "errorfile" proxy keyword */
1134static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1135 struct proxy *defpx, const char *file, int line,
1136 char **errmsg)
1137{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001138 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001139 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001140 int status;
1141 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001142
1143 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1144 ret = 1;
1145 goto out;
1146 }
1147
1148 if (*(args[1]) == 0 || *(args[2]) == 0) {
1149 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1150 ret = -1;
1151 goto out;
1152 }
1153
1154 status = atol(args[1]);
1155 msg = http_parse_errorfile(status, args[2], errmsg);
1156 if (!msg) {
1157 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1158 ret = -1;
1159 goto out;
1160 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001161
1162 conf_err = calloc(1, sizeof(*conf_err));
1163 if (!conf_err) {
1164 memprintf(errmsg, "%s : out of memory.", args[0]);
1165 ret = -1;
1166 goto out;
1167 }
1168 conf_err->type = 1;
1169 conf_err->info.errorfile.status = status;
1170 conf_err->info.errorfile.msg = msg;
1171 conf_err->file = strdup(file);
1172 conf_err->line = line;
1173 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1174
1175 out:
1176 return ret;
1177
1178}
1179
1180/* Parses the "errorfiles" proxy keyword */
1181static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1182 struct proxy *defpx, const char *file, int line,
1183 char **err)
1184{
1185 struct conf_errors *conf_err = NULL;
1186 char *name = NULL;
1187 int rc, ret = 0;
1188
1189 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1190 ret = 1;
1191 goto out;
1192 }
1193
1194 if (!*(args[1])) {
1195 memprintf(err, "%s : expects <name> as argument.", args[0]);
1196 ret = -1;
1197 goto out;
1198 }
1199
1200 name = strdup(args[1]);
1201 conf_err = calloc(1, sizeof(*conf_err));
1202 if (!name || !conf_err) {
1203 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001204 goto error;
1205 }
1206 conf_err->type = 0;
1207
1208 conf_err->info.errorfiles.name = name;
1209 if (!*(args[2])) {
1210 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1211 conf_err->info.errorfiles.status[rc] = 1;
1212 }
1213 else {
1214 int cur_arg, status;
1215 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1216 status = atol(args[cur_arg]);
1217
1218 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1219 if (http_err_codes[rc] == status) {
1220 conf_err->info.errorfiles.status[rc] = 2;
1221 break;
1222 }
1223 }
1224 if (rc >= HTTP_ERR_SIZE) {
1225 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001226 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001227 }
1228 }
1229 }
1230 conf_err->file = strdup(file);
1231 conf_err->line = line;
1232 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1233 out:
1234 return ret;
1235
1236 error:
1237 free(name);
1238 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001239 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001240 goto out;
1241}
1242
1243/* Check "errorfiles" proxy keyword */
1244static int proxy_check_errors(struct proxy *px)
1245{
1246 struct conf_errors *conf_err, *conf_err_back;
1247 struct http_errors *http_errs;
1248 int rc, err = 0;
1249
1250 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1251 if (conf_err->type == 1) {
1252 /* errorfile */
1253 rc = http_get_status_idx(conf_err->info.errorfile.status);
1254 px->errmsg[rc] = conf_err->info.errorfile.msg;
1255 }
1256 else {
1257 /* errorfiles */
1258 list_for_each_entry(http_errs, &http_errors_list, list) {
1259 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1260 break;
1261 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001262
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001263 /* unknown http-errors section */
1264 if (&http_errs->list == &http_errors_list) {
1265 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1266 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1267 err |= ERR_ALERT | ERR_FATAL;
1268 free(conf_err->info.errorfiles.name);
1269 goto next;
1270 }
1271
1272 free(conf_err->info.errorfiles.name);
1273 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1274 if (conf_err->info.errorfiles.status[rc] > 0) {
1275 if (http_errs->errmsg[rc])
1276 px->errmsg[rc] = http_errs->errmsg[rc];
1277 else if (conf_err->info.errorfiles.status[rc] == 2)
1278 ha_warning("config: proxy '%s' : status '%d' not declared in"
1279 " http-errors section '%s' (at %s:%d).\n",
1280 px->id, http_err_codes[rc], http_errs->id,
1281 conf_err->file, conf_err->line);
1282 }
1283 }
1284 }
1285 next:
1286 LIST_DEL(&conf_err->list);
1287 free(conf_err->file);
1288 free(conf_err);
1289 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001290
1291 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001292 return err;
1293}
1294
1295int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1296{
1297 struct conf_errors *conf_err, *new_conf_err = NULL;
1298 int ret = 0;
1299
1300 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1301 new_conf_err = calloc(1, sizeof(*new_conf_err));
1302 if (!new_conf_err) {
1303 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1304 goto out;
1305 }
1306 new_conf_err->type = conf_err->type;
1307 if (conf_err->type == 1) {
1308 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1309 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1310 }
1311 else {
1312 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1313 if (!new_conf_err->info.errorfiles.name) {
1314 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1315 goto out;
1316 }
1317 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1318 sizeof(conf_err->info.errorfiles.status));
1319 }
1320 new_conf_err->file = strdup(conf_err->file);
1321 new_conf_err->line = conf_err->line;
1322 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1323 new_conf_err = NULL;
1324 }
1325 ret = 1;
1326
1327 out:
1328 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001329 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001330}
1331
1332void proxy_release_conf_errors(struct proxy *px)
1333{
1334 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001335
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001336 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1337 if (conf_err->type == 0)
1338 free(conf_err->info.errorfiles.name);
1339 LIST_DEL(&conf_err->list);
1340 free(conf_err->file);
1341 free(conf_err);
1342 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001343}
1344
1345/*
1346 * Parse an <http-errors> section.
1347 * Returns the error code, 0 if OK, or any combination of :
1348 * - ERR_ABORT: must abort ASAP
1349 * - ERR_FATAL: we can continue parsing but not start the service
1350 * - ERR_WARN: a warning has been emitted
1351 * - ERR_ALERT: an alert has been emitted
1352 * Only the two first ones can stop processing, the two others are just
1353 * indicators.
1354 */
1355static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1356{
1357 static struct http_errors *curr_errs = NULL;
1358 int err_code = 0;
1359 const char *err;
1360 char *errmsg = NULL;
1361
1362 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1363 if (!*args[1]) {
1364 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1365 err_code |= ERR_ALERT | ERR_ABORT;
1366 goto out;
1367 }
1368
1369 err = invalid_char(args[1]);
1370 if (err) {
1371 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1372 file, linenum, *err, args[0], args[1]);
1373 err_code |= ERR_ALERT | ERR_FATAL;
1374 }
1375
1376 list_for_each_entry(curr_errs, &http_errors_list, list) {
1377 /* Error if two errors section owns the same name */
1378 if (strcmp(curr_errs->id, args[1]) == 0) {
1379 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1380 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1381 err_code |= ERR_ALERT | ERR_FATAL;
1382 }
1383 }
1384
1385 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1386 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1387 err_code |= ERR_ALERT | ERR_ABORT;
1388 goto out;
1389 }
1390
1391 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1392 curr_errs->id = strdup(args[1]);
1393 curr_errs->conf.file = strdup(file);
1394 curr_errs->conf.line = linenum;
1395 }
1396 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1397 struct buffer *msg;
1398 int status, rc;
1399
1400 if (*(args[1]) == 0 || *(args[2]) == 0) {
1401 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1402 file, linenum, args[0]);
1403 err_code |= ERR_ALERT | ERR_FATAL;
1404 goto out;
1405 }
1406
1407 status = atol(args[1]);
1408 msg = http_parse_errorfile(status, args[2], &errmsg);
1409 if (!msg) {
1410 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1411 err_code |= ERR_ALERT | ERR_FATAL;
1412 goto out;
1413 }
1414 rc = http_get_status_idx(status);
1415 curr_errs->errmsg[rc] = msg;
1416 }
1417 else if (*args[0] != 0) {
1418 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1419 err_code |= ERR_ALERT | ERR_FATAL;
1420 goto out;
1421 }
1422
1423out:
1424 free(errmsg);
1425 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001426}
1427
1428static struct cfg_kw_list cfg_kws = {ILH, {
1429 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1430 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1431 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1432 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001433 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01001434 { 0, NULL, NULL },
1435}};
1436
1437INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001438REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001439
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001440REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1441
Christopher Faulet29f72842019-12-11 15:52:32 +01001442/************************************************************************/
1443/* HTX sample fetches */
1444/************************************************************************/
1445
1446/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1447static int
1448smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1449{
1450 if (!smp->strm)
1451 return 0;
1452
1453 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1454 smp->data.type = SMP_T_BOOL;
1455 return 1;
1456}
1457
1458/* Returns the number of blocks in an HTX message. The channel is chosen
1459 * depending on the sample direction. */
1460static int
1461smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1462{
1463 struct channel *chn;
1464 struct htx *htx;
1465
1466 if (!smp->strm)
1467 return 0;
1468
1469 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1470 htx = smp_prefetch_htx(smp, chn, 0);
1471 if (!htx)
1472 return 0;
1473
1474 smp->data.u.sint = htx_nbblks(htx);
1475 smp->data.type = SMP_T_SINT;
1476 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1477 return 1;
1478}
1479
1480/* Returns the size of an HTX message. The channel is chosen depending on the
1481 * sample direction. */
1482static int
1483smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1484{
1485 struct channel *chn;
1486 struct htx *htx;
1487
1488 if (!smp->strm)
1489 return 0;
1490
1491 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1492 htx = smp_prefetch_htx(smp, chn, 0);
1493 if (!htx)
1494 return 0;
1495
1496 smp->data.u.sint = htx->size;
1497 smp->data.type = SMP_T_SINT;
1498 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1499 return 1;
1500}
1501
1502/* Returns the data size of an HTX message. The channel is chosen depending on the
1503 * sample direction. */
1504static int
1505smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1506{
1507 struct channel *chn;
1508 struct htx *htx;
1509
1510 if (!smp->strm)
1511 return 0;
1512
1513 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1514 htx = smp_prefetch_htx(smp, chn, 0);
1515 if (!htx)
1516 return 0;
1517
1518 smp->data.u.sint = htx->data;
1519 smp->data.type = SMP_T_SINT;
1520 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1521 return 1;
1522}
1523
1524/* Returns the used space (data+meta) of an HTX message. The channel is chosen
1525 * depending on the sample direction. */
1526static int
1527smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1528{
1529 struct channel *chn;
1530 struct htx *htx;
1531
1532 if (!smp->strm)
1533 return 0;
1534
1535 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1536 htx = smp_prefetch_htx(smp, chn, 0);
1537 if (!htx)
1538 return 0;
1539
1540 smp->data.u.sint = htx_used_space(htx);
1541 smp->data.type = SMP_T_SINT;
1542 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1543 return 1;
1544}
1545
1546/* Returns the free space (size-used) of an HTX message. The channel is chosen
1547 * depending on the sample direction. */
1548static int
1549smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1550{
1551 struct channel *chn;
1552 struct htx *htx;
1553
1554 if (!smp->strm)
1555 return 0;
1556
1557 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1558 htx = smp_prefetch_htx(smp, chn, 0);
1559 if (!htx)
1560 return 0;
1561
1562 smp->data.u.sint = htx_free_space(htx);
1563 smp->data.type = SMP_T_SINT;
1564 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1565 return 1;
1566}
1567
1568/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
1569 * channel is chosen depending on the sample direction. */
1570static int
1571smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1572{
1573 struct channel *chn;
1574 struct htx *htx;
1575
1576 if (!smp->strm)
1577 return 0;
1578
1579 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1580 htx = smp_prefetch_htx(smp, chn, 0);
1581 if (!htx)
1582 return 0;
1583
1584 smp->data.u.sint = htx_free_data_space(htx);
1585 smp->data.type = SMP_T_SINT;
1586 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1587 return 1;
1588}
1589
1590/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
1591 * 0. Concretely, it only checks the tail. The channel is chosen depending on
1592 * the sample direction. */
1593static int
1594smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1595{
1596 struct channel *chn;
1597 struct htx *htx;
1598
1599 if (!smp->strm)
1600 return 0;
1601
1602 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1603 htx = smp_prefetch_htx(smp, chn, 0);
1604 if (!htx)
1605 return 0;
1606
1607 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
1608 smp->data.type = SMP_T_BOOL;
1609 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1610 return 1;
1611}
1612
1613/* Returns the type of a specific HTX block, if found in the message. Otherwise
1614 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
1615 * "head", "tail" or "first". The channel is chosen depending on the sample
1616 * direction. */
1617static int
1618smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1619{
1620 struct channel *chn;
1621 struct htx *htx;
1622 enum htx_blk_type type;
1623 int32_t pos;
1624
1625 if (!smp->strm || !arg_p)
1626 return 0;
1627
1628 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1629 htx = smp_prefetch_htx(smp, chn, 0);
1630 if (!htx)
1631 return 0;
1632
1633 pos = arg_p[0].data.sint;
1634 if (pos == -1)
1635 type = htx_get_head_type(htx);
1636 else if (pos == -2)
1637 type = htx_get_tail_type(htx);
1638 else if (pos == -3)
1639 type = htx_get_first_type(htx);
1640 else
1641 type = ((pos >= htx->head && pos <= htx->tail)
1642 ? htx_get_blk_type(htx_get_blk(htx, pos))
1643 : HTX_BLK_UNUSED);
1644
1645 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
1646 smp->data.type = SMP_T_STR;
1647 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1648 return 1;
1649}
1650
1651/* Returns the size of a specific HTX block, if found in the message. Otherwise
1652 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
1653 * "first". The channel is chosen depending on the sample direction. */
1654static int
1655smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1656{
1657 struct channel *chn;
1658 struct htx *htx;
1659 struct htx_blk *blk;
1660 int32_t pos;
1661
1662 if (!smp->strm || !arg_p)
1663 return 0;
1664
1665 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1666 htx = smp_prefetch_htx(smp, chn, 0);
1667 if (!htx)
1668 return 0;
1669
1670 pos = arg_p[0].data.sint;
1671 if (pos == -1)
1672 blk = htx_get_head_blk(htx);
1673 else if (pos == -2)
1674 blk = htx_get_tail_blk(htx);
1675 else if (pos == -3)
1676 blk = htx_get_first_blk(htx);
1677 else
1678 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1679
1680 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
1681 smp->data.type = SMP_T_SINT;
1682 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1683 return 1;
1684}
1685
1686/* Returns the start-line if the selected HTX block exists and is a
1687 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
1688 * supported or "head", "tail" or "first". The channel is chosen depending on
1689 * the sample direction. */
1690static int
1691smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1692{
1693 struct buffer *temp;
1694 struct channel *chn;
1695 struct htx *htx;
1696 struct htx_blk *blk;
1697 struct htx_sl *sl;
1698 int32_t pos;
1699
1700 if (!smp->strm || !arg_p)
1701 return 0;
1702
1703 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1704 htx = smp_prefetch_htx(smp, chn, 0);
1705 if (!htx)
1706 return 0;
1707
1708 pos = arg_p[0].data.sint;
1709 if (pos == -1)
1710 blk = htx_get_head_blk(htx);
1711 else if (pos == -2)
1712 blk = htx_get_tail_blk(htx);
1713 else if (pos == -3)
1714 blk = htx_get_first_blk(htx);
1715 else
1716 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1717
1718 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
1719 smp->data.u.str.size = 0;
1720 smp->data.u.str.area = "";
1721 smp->data.u.str.data = 0;
1722 }
1723 else {
1724 sl = htx_get_blk_ptr(htx, blk);
1725
1726 temp = get_trash_chunk();
1727 chunk_istcat(temp, htx_sl_p1(sl));
1728 temp->area[temp->data++] = ' ';
1729 chunk_istcat(temp, htx_sl_p2(sl));
1730 temp->area[temp->data++] = ' ';
1731 chunk_istcat(temp, htx_sl_p3(sl));
1732
1733 smp->data.u.str = *temp;
1734 }
1735
1736 smp->data.type = SMP_T_STR;
1737 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1738 return 1;
1739}
1740
1741/* Returns the header name if the selected HTX block exists and is a header or a
1742 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1743 * supported or "head", "tail" or "first". The channel is chosen depending on
1744 * the sample direction. */
1745static int
1746smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1747{
1748 struct channel *chn;
1749 struct htx *htx;
1750 struct htx_blk *blk;
1751 int32_t pos;
1752
1753 if (!smp->strm || !arg_p)
1754 return 0;
1755
1756 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1757 htx = smp_prefetch_htx(smp, chn, 0);
1758 if (!htx)
1759 return 0;
1760
1761 pos = arg_p[0].data.sint;
1762 if (pos == -1)
1763 blk = htx_get_head_blk(htx);
1764 else if (pos == -2)
1765 blk = htx_get_tail_blk(htx);
1766 else if (pos == -3)
1767 blk = htx_get_first_blk(htx);
1768 else
1769 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1770
1771 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1772 smp->data.u.str.size = 0;
1773 smp->data.u.str.area = "";
1774 smp->data.u.str.data = 0;
1775 }
1776 else {
1777 struct ist name = htx_get_blk_name(htx, blk);
1778
1779 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
1780 }
1781 smp->data.type = SMP_T_STR;
1782 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1783 return 1;
1784}
1785
1786/* Returns the header value if the selected HTX block exists and is a header or
1787 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1788 * supported or "head", "tail" or "first". The channel is chosen depending on
1789 * the sample direction. */
1790static int
1791smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1792{
1793 struct channel *chn;
1794 struct htx *htx;
1795 struct htx_blk *blk;
1796 int32_t pos;
1797
1798 if (!smp->strm || !arg_p)
1799 return 0;
1800
1801 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1802 htx = smp_prefetch_htx(smp, chn, 0);
1803 if (!htx)
1804 return 0;
1805
1806 pos = arg_p[0].data.sint;
1807 if (pos == -1)
1808 blk = htx_get_head_blk(htx);
1809 else if (pos == -2)
1810 blk = htx_get_tail_blk(htx);
1811 else if (pos == -3)
1812 blk = htx_get_first_blk(htx);
1813 else
1814 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1815
1816 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1817 smp->data.u.str.size = 0;
1818 smp->data.u.str.area = "";
1819 smp->data.u.str.data = 0;
1820 }
1821 else {
1822 struct ist val = htx_get_blk_value(htx, blk);
1823
1824 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1825 }
1826 smp->data.type = SMP_T_STR;
1827 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1828 return 1;
1829}
1830
1831/* Returns the value if the selected HTX block exists and is a data
1832 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
1833 * or "head", "tail" or "first". The channel is chosen depending on the sample
1834 * direction. */
1835static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01001836smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01001837{
1838 struct channel *chn;
1839 struct htx *htx;
1840 struct htx_blk *blk;
1841 int32_t pos;
1842
1843 if (!smp->strm || !arg_p)
1844 return 0;
1845
1846 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1847 htx = smp_prefetch_htx(smp, chn, 0);
1848 if (!htx)
1849 return 0;
1850
1851 pos = arg_p[0].data.sint;
1852 if (pos == -1)
1853 blk = htx_get_head_blk(htx);
1854 else if (pos == -2)
1855 blk = htx_get_tail_blk(htx);
1856 else if (pos == -3)
1857 blk = htx_get_first_blk(htx);
1858 else
1859 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1860
1861 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
1862 smp->data.u.str.size = 0;
1863 smp->data.u.str.area = "";
1864 smp->data.u.str.data = 0;
1865 }
1866 else {
1867 struct ist val = htx_get_blk_value(htx, blk);
1868
1869 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1870 }
Christopher Faulet8178e402020-01-08 14:38:58 +01001871 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01001872 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1873 return 1;
1874}
1875
1876/* This function is used to validate the arguments passed to any "htx_blk" fetch
1877 * keywords. An argument is expected by these keywords. It must be a positive
1878 * integer or on of the following strings: "head", "tail" or "first". It returns
1879 * 0 on error, and a non-zero value if OK.
1880 */
1881int val_blk_arg(struct arg *arg, char **err_msg)
1882{
1883 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
1884 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
1885 return 0;
1886 }
1887 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
1888 free(arg[0].data.str.area);
1889 arg[0].type = ARGT_SINT;
1890 arg[0].data.sint = -1;
1891 }
1892 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
1893 free(arg[0].data.str.area);
1894 arg[0].type = ARGT_SINT;
1895 arg[0].data.sint = -2;
1896 }
1897 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
1898 free(arg[0].data.str.area);
1899 arg[0].type = ARGT_SINT;
1900 arg[0].data.sint = -3;
1901 }
1902 else {
1903 int pos;
1904
1905 for (pos = 0; pos < arg[0].data.str.data; pos++) {
1906 if (!isdigit(arg[0].data.str.area[pos])) {
1907 memprintf(err_msg, "invalid block position");
1908 return 0;
1909 }
1910 }
1911
1912 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
1913 if (pos < 0) {
1914 memprintf(err_msg, "block position must not be negative");
1915 return 0;
1916 }
1917 free(arg[0].data.str.area);
1918 arg[0].type = ARGT_SINT;
1919 arg[0].data.sint = pos;
1920 }
1921
1922 return 1;
1923}
1924
1925
1926/* Note: must not be declared <const> as its list will be overwritten.
1927 * Note: htx sample fetches should only used for developpement purpose.
1928 */
1929static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01001930 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01001931
Christopher Faulet01f44452020-01-08 14:23:40 +01001932 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1933 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1934 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1935 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1936 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1937 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1938 { "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 +01001939
Christopher Faulet01f44452020-01-08 14:23:40 +01001940 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1941 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1942 { "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},
1943 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1944 { "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 +01001945 { "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 +01001946
1947 { /* END */ },
1948}};
1949
1950INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);