blob: 016fd8f2bc9543eaed7bbe154afed910d3eedc5d [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.");
949 goto out;
950 }
951
952 /* Convert the error file into an HTX message */
953 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +0100954 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Faulet58857752020-01-15 15:19:50 +0100955 free(http_err->node.key);
956 free(http_err);
Christopher Faulet5031ef52020-01-15 11:22:07 +0100957 goto out;
958 }
959
Christopher Faulet58857752020-01-15 15:19:50 +0100960 /* Insert the node in the tree and return the HTX message */
961 http_err->msg = chk;
962 ebis_insert(&http_error_messages, &http_err->node);
963 buf = &http_err->msg;
964
Christopher Faulet5031ef52020-01-15 11:22:07 +0100965 out:
966 if (fd >= 0)
967 close(fd);
968 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +0100969 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100970}
971
Christopher Faulet58857752020-01-15 15:19:50 +0100972/* Convert the raw http message <msg> into an HTX message. On sucess, the HTX
973 * message is returned. On error, NULL is returned and an error message is
974 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +0100975 */
Christopher Faulet58857752020-01-15 15:19:50 +0100976struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +0100977{
Christopher Faulet58857752020-01-15 15:19:50 +0100978 struct buffer *buf = NULL;
979 struct buffer chk;
980 struct ebpt_node *node;
981 struct http_error *http_err;
982
983 /* already loaded */
984 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
985 if (node) {
986 http_err = container_of(node, typeof(*http_err), node);
987 buf = &http_err->msg;
988 goto out;
989 }
990 /* Create the node corresponding to the error file */
991 http_err = calloc(1, sizeof(*http_err));
992 if (!http_err) {
993 memprintf(errmsg, "out of memory.");
994 goto out;
995 }
996 http_err->node.key = strdup(key);
997 if (!http_err->node.key) {
998 memprintf(errmsg, "out of memory.");
999 goto out;
1000 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001001
1002 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001003 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001004 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Faulet58857752020-01-15 15:19:50 +01001005 free(http_err->node.key);
1006 free(http_err);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001007 goto out;
1008 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001009
Christopher Faulet58857752020-01-15 15:19:50 +01001010 /* Insert the node in the tree and return the HTX message */
1011 http_err->msg = chk;
1012 ebis_insert(&http_error_messages, &http_err->node);
1013 buf = &http_err->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001014 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001015 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001016}
1017
Christopher Faulet5031ef52020-01-15 11:22:07 +01001018/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001019 * <status>. It returns NULL if there is any error, otherwise it return the
1020 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001021 */
Christopher Faulet58857752020-01-15 15:19:50 +01001022struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001023{
Christopher Faulet58857752020-01-15 15:19:50 +01001024 struct buffer *buf = NULL;
1025 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001026
1027 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1028 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001029 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001030 break;
1031 }
1032 }
1033
1034 if (rc >= HTTP_ERR_SIZE)
1035 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001036 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001037}
1038
1039/* This function creates HTX error message corresponding to a redirect message
1040 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001041 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1042 * returns NULL if there is any error, otherwise it return the corresponding HTX
1043 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001044 */
Christopher Faulet58857752020-01-15 15:19:50 +01001045struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001046{
Christopher Faulet58857752020-01-15 15:19:50 +01001047 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001048 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001049 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001050 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001051
1052 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1053 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001054 /* Create the error key */
1055 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1056 memprintf(errmsg, "out of memory.");
1057 goto out;
1058 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001059 /* Create the error message */
1060 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1061 errlen = strlen(msg) + strlen(url) + 5;
1062 err = malloc(errlen);
1063 if (!err) {
1064 memprintf(errmsg, "out of memory.");
1065 goto out;
1066 }
1067 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1068
1069 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001070 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001071 break;
1072 }
1073 }
1074
1075 if (rc >= HTTP_ERR_SIZE)
1076 memprintf(errmsg, "status code '%d' not handled.", status);
1077out:
Christopher Faulet58857752020-01-15 15:19:50 +01001078 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001079 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001080 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001081}
1082
Christopher Faulet07f41f72020-01-16 16:16:06 +01001083/* Parses the "errorloc[302|303]" proxy keyword */
1084static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1085 struct proxy *defpx, const char *file, int line,
1086 char **errmsg)
1087{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001088 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001089 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001090 int errloc, status;
1091 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001092
1093 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1094 ret = 1;
1095 goto out;
1096 }
1097
1098 if (*(args[1]) == 0 || *(args[2]) == 0) {
1099 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1100 ret = -1;
1101 goto out;
1102 }
1103
1104 status = atol(args[1]);
1105 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1106 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1107 if (!msg) {
1108 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1109 ret = -1;
1110 goto out;
1111 }
1112
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001113 conf_err = calloc(1, sizeof(*conf_err));
1114 if (!conf_err) {
1115 memprintf(errmsg, "%s : out of memory.", args[0]);
1116 ret = -1;
1117 goto out;
1118 }
1119 conf_err->type = 1;
1120 conf_err->info.errorfile.status = status;
1121 conf_err->info.errorfile.msg = msg;
1122 conf_err->file = strdup(file);
1123 conf_err->line = line;
1124 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001125
1126 out:
1127 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001128
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001129}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001130
1131/* Parses the "errorfile" proxy keyword */
1132static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1133 struct proxy *defpx, const char *file, int line,
1134 char **errmsg)
1135{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001136 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001137 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001138 int status;
1139 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001140
1141 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1142 ret = 1;
1143 goto out;
1144 }
1145
1146 if (*(args[1]) == 0 || *(args[2]) == 0) {
1147 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1148 ret = -1;
1149 goto out;
1150 }
1151
1152 status = atol(args[1]);
1153 msg = http_parse_errorfile(status, args[2], errmsg);
1154 if (!msg) {
1155 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1156 ret = -1;
1157 goto out;
1158 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001159
1160 conf_err = calloc(1, sizeof(*conf_err));
1161 if (!conf_err) {
1162 memprintf(errmsg, "%s : out of memory.", args[0]);
1163 ret = -1;
1164 goto out;
1165 }
1166 conf_err->type = 1;
1167 conf_err->info.errorfile.status = status;
1168 conf_err->info.errorfile.msg = msg;
1169 conf_err->file = strdup(file);
1170 conf_err->line = line;
1171 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1172
1173 out:
1174 return ret;
1175
1176}
1177
1178/* Parses the "errorfiles" proxy keyword */
1179static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1180 struct proxy *defpx, const char *file, int line,
1181 char **err)
1182{
1183 struct conf_errors *conf_err = NULL;
1184 char *name = NULL;
1185 int rc, ret = 0;
1186
1187 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1188 ret = 1;
1189 goto out;
1190 }
1191
1192 if (!*(args[1])) {
1193 memprintf(err, "%s : expects <name> as argument.", args[0]);
1194 ret = -1;
1195 goto out;
1196 }
1197
1198 name = strdup(args[1]);
1199 conf_err = calloc(1, sizeof(*conf_err));
1200 if (!name || !conf_err) {
1201 memprintf(err, "%s : out of memory.", args[0]);
1202 ret = -1;
1203 goto error;
1204 }
1205 conf_err->type = 0;
1206
1207 conf_err->info.errorfiles.name = name;
1208 if (!*(args[2])) {
1209 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1210 conf_err->info.errorfiles.status[rc] = 1;
1211 }
1212 else {
1213 int cur_arg, status;
1214 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1215 status = atol(args[cur_arg]);
1216
1217 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1218 if (http_err_codes[rc] == status) {
1219 conf_err->info.errorfiles.status[rc] = 2;
1220 break;
1221 }
1222 }
1223 if (rc >= HTTP_ERR_SIZE) {
1224 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
1225 ret = -1;
1226 goto out;
1227 }
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);
1239 goto out;
1240}
1241
1242/* Check "errorfiles" proxy keyword */
1243static int proxy_check_errors(struct proxy *px)
1244{
1245 struct conf_errors *conf_err, *conf_err_back;
1246 struct http_errors *http_errs;
1247 int rc, err = 0;
1248
1249 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1250 if (conf_err->type == 1) {
1251 /* errorfile */
1252 rc = http_get_status_idx(conf_err->info.errorfile.status);
1253 px->errmsg[rc] = conf_err->info.errorfile.msg;
1254 }
1255 else {
1256 /* errorfiles */
1257 list_for_each_entry(http_errs, &http_errors_list, list) {
1258 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1259 break;
1260 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001261
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001262 /* unknown http-errors section */
1263 if (&http_errs->list == &http_errors_list) {
1264 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1265 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1266 err |= ERR_ALERT | ERR_FATAL;
1267 free(conf_err->info.errorfiles.name);
1268 goto next;
1269 }
1270
1271 free(conf_err->info.errorfiles.name);
1272 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1273 if (conf_err->info.errorfiles.status[rc] > 0) {
1274 if (http_errs->errmsg[rc])
1275 px->errmsg[rc] = http_errs->errmsg[rc];
1276 else if (conf_err->info.errorfiles.status[rc] == 2)
1277 ha_warning("config: proxy '%s' : status '%d' not declared in"
1278 " http-errors section '%s' (at %s:%d).\n",
1279 px->id, http_err_codes[rc], http_errs->id,
1280 conf_err->file, conf_err->line);
1281 }
1282 }
1283 }
1284 next:
1285 LIST_DEL(&conf_err->list);
1286 free(conf_err->file);
1287 free(conf_err);
1288 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001289
1290 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001291 return err;
1292}
1293
1294int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1295{
1296 struct conf_errors *conf_err, *new_conf_err = NULL;
1297 int ret = 0;
1298
1299 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1300 new_conf_err = calloc(1, sizeof(*new_conf_err));
1301 if (!new_conf_err) {
1302 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1303 goto out;
1304 }
1305 new_conf_err->type = conf_err->type;
1306 if (conf_err->type == 1) {
1307 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1308 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1309 }
1310 else {
1311 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1312 if (!new_conf_err->info.errorfiles.name) {
1313 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1314 goto out;
1315 }
1316 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1317 sizeof(conf_err->info.errorfiles.status));
1318 }
1319 new_conf_err->file = strdup(conf_err->file);
1320 new_conf_err->line = conf_err->line;
1321 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1322 new_conf_err = NULL;
1323 }
1324 ret = 1;
1325
1326 out:
1327 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001328 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001329}
1330
1331void proxy_release_conf_errors(struct proxy *px)
1332{
1333 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001334
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001335 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1336 if (conf_err->type == 0)
1337 free(conf_err->info.errorfiles.name);
1338 LIST_DEL(&conf_err->list);
1339 free(conf_err->file);
1340 free(conf_err);
1341 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001342}
1343
1344/*
1345 * Parse an <http-errors> section.
1346 * Returns the error code, 0 if OK, or any combination of :
1347 * - ERR_ABORT: must abort ASAP
1348 * - ERR_FATAL: we can continue parsing but not start the service
1349 * - ERR_WARN: a warning has been emitted
1350 * - ERR_ALERT: an alert has been emitted
1351 * Only the two first ones can stop processing, the two others are just
1352 * indicators.
1353 */
1354static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1355{
1356 static struct http_errors *curr_errs = NULL;
1357 int err_code = 0;
1358 const char *err;
1359 char *errmsg = NULL;
1360
1361 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1362 if (!*args[1]) {
1363 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1364 err_code |= ERR_ALERT | ERR_ABORT;
1365 goto out;
1366 }
1367
1368 err = invalid_char(args[1]);
1369 if (err) {
1370 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1371 file, linenum, *err, args[0], args[1]);
1372 err_code |= ERR_ALERT | ERR_FATAL;
1373 }
1374
1375 list_for_each_entry(curr_errs, &http_errors_list, list) {
1376 /* Error if two errors section owns the same name */
1377 if (strcmp(curr_errs->id, args[1]) == 0) {
1378 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1379 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1380 err_code |= ERR_ALERT | ERR_FATAL;
1381 }
1382 }
1383
1384 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1385 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1386 err_code |= ERR_ALERT | ERR_ABORT;
1387 goto out;
1388 }
1389
1390 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1391 curr_errs->id = strdup(args[1]);
1392 curr_errs->conf.file = strdup(file);
1393 curr_errs->conf.line = linenum;
1394 }
1395 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1396 struct buffer *msg;
1397 int status, rc;
1398
1399 if (*(args[1]) == 0 || *(args[2]) == 0) {
1400 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1401 file, linenum, args[0]);
1402 err_code |= ERR_ALERT | ERR_FATAL;
1403 goto out;
1404 }
1405
1406 status = atol(args[1]);
1407 msg = http_parse_errorfile(status, args[2], &errmsg);
1408 if (!msg) {
1409 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1410 err_code |= ERR_ALERT | ERR_FATAL;
1411 goto out;
1412 }
1413 rc = http_get_status_idx(status);
1414 curr_errs->errmsg[rc] = msg;
1415 }
1416 else if (*args[0] != 0) {
1417 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1418 err_code |= ERR_ALERT | ERR_FATAL;
1419 goto out;
1420 }
1421
1422out:
1423 free(errmsg);
1424 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001425}
1426
1427static struct cfg_kw_list cfg_kws = {ILH, {
1428 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1429 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1430 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1431 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001432 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01001433 { 0, NULL, NULL },
1434}};
1435
1436INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001437REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001438
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001439REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1440
Christopher Faulet29f72842019-12-11 15:52:32 +01001441/************************************************************************/
1442/* HTX sample fetches */
1443/************************************************************************/
1444
1445/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1446static int
1447smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1448{
1449 if (!smp->strm)
1450 return 0;
1451
1452 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1453 smp->data.type = SMP_T_BOOL;
1454 return 1;
1455}
1456
1457/* Returns the number of blocks in an HTX message. The channel is chosen
1458 * depending on the sample direction. */
1459static int
1460smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1461{
1462 struct channel *chn;
1463 struct htx *htx;
1464
1465 if (!smp->strm)
1466 return 0;
1467
1468 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1469 htx = smp_prefetch_htx(smp, chn, 0);
1470 if (!htx)
1471 return 0;
1472
1473 smp->data.u.sint = htx_nbblks(htx);
1474 smp->data.type = SMP_T_SINT;
1475 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1476 return 1;
1477}
1478
1479/* Returns the size of an HTX message. The channel is chosen depending on the
1480 * sample direction. */
1481static int
1482smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1483{
1484 struct channel *chn;
1485 struct htx *htx;
1486
1487 if (!smp->strm)
1488 return 0;
1489
1490 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1491 htx = smp_prefetch_htx(smp, chn, 0);
1492 if (!htx)
1493 return 0;
1494
1495 smp->data.u.sint = htx->size;
1496 smp->data.type = SMP_T_SINT;
1497 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1498 return 1;
1499}
1500
1501/* Returns the data size of an HTX message. The channel is chosen depending on the
1502 * sample direction. */
1503static int
1504smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1505{
1506 struct channel *chn;
1507 struct htx *htx;
1508
1509 if (!smp->strm)
1510 return 0;
1511
1512 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1513 htx = smp_prefetch_htx(smp, chn, 0);
1514 if (!htx)
1515 return 0;
1516
1517 smp->data.u.sint = htx->data;
1518 smp->data.type = SMP_T_SINT;
1519 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1520 return 1;
1521}
1522
1523/* Returns the used space (data+meta) of an HTX message. The channel is chosen
1524 * depending on the sample direction. */
1525static int
1526smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1527{
1528 struct channel *chn;
1529 struct htx *htx;
1530
1531 if (!smp->strm)
1532 return 0;
1533
1534 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1535 htx = smp_prefetch_htx(smp, chn, 0);
1536 if (!htx)
1537 return 0;
1538
1539 smp->data.u.sint = htx_used_space(htx);
1540 smp->data.type = SMP_T_SINT;
1541 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1542 return 1;
1543}
1544
1545/* Returns the free space (size-used) of an HTX message. The channel is chosen
1546 * depending on the sample direction. */
1547static int
1548smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1549{
1550 struct channel *chn;
1551 struct htx *htx;
1552
1553 if (!smp->strm)
1554 return 0;
1555
1556 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1557 htx = smp_prefetch_htx(smp, chn, 0);
1558 if (!htx)
1559 return 0;
1560
1561 smp->data.u.sint = htx_free_space(htx);
1562 smp->data.type = SMP_T_SINT;
1563 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1564 return 1;
1565}
1566
1567/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
1568 * channel is chosen depending on the sample direction. */
1569static int
1570smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1571{
1572 struct channel *chn;
1573 struct htx *htx;
1574
1575 if (!smp->strm)
1576 return 0;
1577
1578 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1579 htx = smp_prefetch_htx(smp, chn, 0);
1580 if (!htx)
1581 return 0;
1582
1583 smp->data.u.sint = htx_free_data_space(htx);
1584 smp->data.type = SMP_T_SINT;
1585 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1586 return 1;
1587}
1588
1589/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
1590 * 0. Concretely, it only checks the tail. The channel is chosen depending on
1591 * the sample direction. */
1592static int
1593smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1594{
1595 struct channel *chn;
1596 struct htx *htx;
1597
1598 if (!smp->strm)
1599 return 0;
1600
1601 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1602 htx = smp_prefetch_htx(smp, chn, 0);
1603 if (!htx)
1604 return 0;
1605
1606 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
1607 smp->data.type = SMP_T_BOOL;
1608 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1609 return 1;
1610}
1611
1612/* Returns the type of a specific HTX block, if found in the message. Otherwise
1613 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
1614 * "head", "tail" or "first". The channel is chosen depending on the sample
1615 * direction. */
1616static int
1617smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1618{
1619 struct channel *chn;
1620 struct htx *htx;
1621 enum htx_blk_type type;
1622 int32_t pos;
1623
1624 if (!smp->strm || !arg_p)
1625 return 0;
1626
1627 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1628 htx = smp_prefetch_htx(smp, chn, 0);
1629 if (!htx)
1630 return 0;
1631
1632 pos = arg_p[0].data.sint;
1633 if (pos == -1)
1634 type = htx_get_head_type(htx);
1635 else if (pos == -2)
1636 type = htx_get_tail_type(htx);
1637 else if (pos == -3)
1638 type = htx_get_first_type(htx);
1639 else
1640 type = ((pos >= htx->head && pos <= htx->tail)
1641 ? htx_get_blk_type(htx_get_blk(htx, pos))
1642 : HTX_BLK_UNUSED);
1643
1644 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
1645 smp->data.type = SMP_T_STR;
1646 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1647 return 1;
1648}
1649
1650/* Returns the size of a specific HTX block, if found in the message. Otherwise
1651 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
1652 * "first". The channel is chosen depending on the sample direction. */
1653static int
1654smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1655{
1656 struct channel *chn;
1657 struct htx *htx;
1658 struct htx_blk *blk;
1659 int32_t pos;
1660
1661 if (!smp->strm || !arg_p)
1662 return 0;
1663
1664 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1665 htx = smp_prefetch_htx(smp, chn, 0);
1666 if (!htx)
1667 return 0;
1668
1669 pos = arg_p[0].data.sint;
1670 if (pos == -1)
1671 blk = htx_get_head_blk(htx);
1672 else if (pos == -2)
1673 blk = htx_get_tail_blk(htx);
1674 else if (pos == -3)
1675 blk = htx_get_first_blk(htx);
1676 else
1677 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1678
1679 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
1680 smp->data.type = SMP_T_SINT;
1681 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1682 return 1;
1683}
1684
1685/* Returns the start-line if the selected HTX block exists and is a
1686 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
1687 * supported or "head", "tail" or "first". The channel is chosen depending on
1688 * the sample direction. */
1689static int
1690smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1691{
1692 struct buffer *temp;
1693 struct channel *chn;
1694 struct htx *htx;
1695 struct htx_blk *blk;
1696 struct htx_sl *sl;
1697 int32_t pos;
1698
1699 if (!smp->strm || !arg_p)
1700 return 0;
1701
1702 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1703 htx = smp_prefetch_htx(smp, chn, 0);
1704 if (!htx)
1705 return 0;
1706
1707 pos = arg_p[0].data.sint;
1708 if (pos == -1)
1709 blk = htx_get_head_blk(htx);
1710 else if (pos == -2)
1711 blk = htx_get_tail_blk(htx);
1712 else if (pos == -3)
1713 blk = htx_get_first_blk(htx);
1714 else
1715 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1716
1717 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
1718 smp->data.u.str.size = 0;
1719 smp->data.u.str.area = "";
1720 smp->data.u.str.data = 0;
1721 }
1722 else {
1723 sl = htx_get_blk_ptr(htx, blk);
1724
1725 temp = get_trash_chunk();
1726 chunk_istcat(temp, htx_sl_p1(sl));
1727 temp->area[temp->data++] = ' ';
1728 chunk_istcat(temp, htx_sl_p2(sl));
1729 temp->area[temp->data++] = ' ';
1730 chunk_istcat(temp, htx_sl_p3(sl));
1731
1732 smp->data.u.str = *temp;
1733 }
1734
1735 smp->data.type = SMP_T_STR;
1736 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1737 return 1;
1738}
1739
1740/* Returns the header name if the selected HTX block exists and is a header or a
1741 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1742 * supported or "head", "tail" or "first". The channel is chosen depending on
1743 * the sample direction. */
1744static int
1745smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1746{
1747 struct channel *chn;
1748 struct htx *htx;
1749 struct htx_blk *blk;
1750 int32_t pos;
1751
1752 if (!smp->strm || !arg_p)
1753 return 0;
1754
1755 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1756 htx = smp_prefetch_htx(smp, chn, 0);
1757 if (!htx)
1758 return 0;
1759
1760 pos = arg_p[0].data.sint;
1761 if (pos == -1)
1762 blk = htx_get_head_blk(htx);
1763 else if (pos == -2)
1764 blk = htx_get_tail_blk(htx);
1765 else if (pos == -3)
1766 blk = htx_get_first_blk(htx);
1767 else
1768 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1769
1770 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1771 smp->data.u.str.size = 0;
1772 smp->data.u.str.area = "";
1773 smp->data.u.str.data = 0;
1774 }
1775 else {
1776 struct ist name = htx_get_blk_name(htx, blk);
1777
1778 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
1779 }
1780 smp->data.type = SMP_T_STR;
1781 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1782 return 1;
1783}
1784
1785/* Returns the header value if the selected HTX block exists and is a header or
1786 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1787 * supported or "head", "tail" or "first". The channel is chosen depending on
1788 * the sample direction. */
1789static int
1790smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1791{
1792 struct channel *chn;
1793 struct htx *htx;
1794 struct htx_blk *blk;
1795 int32_t pos;
1796
1797 if (!smp->strm || !arg_p)
1798 return 0;
1799
1800 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1801 htx = smp_prefetch_htx(smp, chn, 0);
1802 if (!htx)
1803 return 0;
1804
1805 pos = arg_p[0].data.sint;
1806 if (pos == -1)
1807 blk = htx_get_head_blk(htx);
1808 else if (pos == -2)
1809 blk = htx_get_tail_blk(htx);
1810 else if (pos == -3)
1811 blk = htx_get_first_blk(htx);
1812 else
1813 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1814
1815 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1816 smp->data.u.str.size = 0;
1817 smp->data.u.str.area = "";
1818 smp->data.u.str.data = 0;
1819 }
1820 else {
1821 struct ist val = htx_get_blk_value(htx, blk);
1822
1823 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1824 }
1825 smp->data.type = SMP_T_STR;
1826 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1827 return 1;
1828}
1829
1830/* Returns the value if the selected HTX block exists and is a data
1831 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
1832 * or "head", "tail" or "first". The channel is chosen depending on the sample
1833 * direction. */
1834static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01001835smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01001836{
1837 struct channel *chn;
1838 struct htx *htx;
1839 struct htx_blk *blk;
1840 int32_t pos;
1841
1842 if (!smp->strm || !arg_p)
1843 return 0;
1844
1845 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1846 htx = smp_prefetch_htx(smp, chn, 0);
1847 if (!htx)
1848 return 0;
1849
1850 pos = arg_p[0].data.sint;
1851 if (pos == -1)
1852 blk = htx_get_head_blk(htx);
1853 else if (pos == -2)
1854 blk = htx_get_tail_blk(htx);
1855 else if (pos == -3)
1856 blk = htx_get_first_blk(htx);
1857 else
1858 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1859
1860 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
1861 smp->data.u.str.size = 0;
1862 smp->data.u.str.area = "";
1863 smp->data.u.str.data = 0;
1864 }
1865 else {
1866 struct ist val = htx_get_blk_value(htx, blk);
1867
1868 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1869 }
Christopher Faulet8178e402020-01-08 14:38:58 +01001870 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01001871 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1872 return 1;
1873}
1874
1875/* This function is used to validate the arguments passed to any "htx_blk" fetch
1876 * keywords. An argument is expected by these keywords. It must be a positive
1877 * integer or on of the following strings: "head", "tail" or "first". It returns
1878 * 0 on error, and a non-zero value if OK.
1879 */
1880int val_blk_arg(struct arg *arg, char **err_msg)
1881{
1882 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
1883 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
1884 return 0;
1885 }
1886 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
1887 free(arg[0].data.str.area);
1888 arg[0].type = ARGT_SINT;
1889 arg[0].data.sint = -1;
1890 }
1891 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
1892 free(arg[0].data.str.area);
1893 arg[0].type = ARGT_SINT;
1894 arg[0].data.sint = -2;
1895 }
1896 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
1897 free(arg[0].data.str.area);
1898 arg[0].type = ARGT_SINT;
1899 arg[0].data.sint = -3;
1900 }
1901 else {
1902 int pos;
1903
1904 for (pos = 0; pos < arg[0].data.str.data; pos++) {
1905 if (!isdigit(arg[0].data.str.area[pos])) {
1906 memprintf(err_msg, "invalid block position");
1907 return 0;
1908 }
1909 }
1910
1911 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
1912 if (pos < 0) {
1913 memprintf(err_msg, "block position must not be negative");
1914 return 0;
1915 }
1916 free(arg[0].data.str.area);
1917 arg[0].type = ARGT_SINT;
1918 arg[0].data.sint = pos;
1919 }
1920
1921 return 1;
1922}
1923
1924
1925/* Note: must not be declared <const> as its list will be overwritten.
1926 * Note: htx sample fetches should only used for developpement purpose.
1927 */
1928static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01001929 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01001930
Christopher Faulet01f44452020-01-08 14:23:40 +01001931 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1932 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1933 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1934 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1935 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1936 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1937 { "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 +01001938
Christopher Faulet01f44452020-01-08 14:23:40 +01001939 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1940 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1941 { "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},
1942 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1943 { "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 +01001944 { "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 +01001945
1946 { /* END */ },
1947}};
1948
1949INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);