blob: f2f0a0c18a862d638838e9abcb2b8fee1dd6ac0a [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
Christopher Faulet727a3f12020-02-07 16:39:41 +010075/* Returns the headers size in the HTX message */
76size_t http_get_hdrs_size(struct htx *htx)
77{
78 struct htx_blk *blk;
79 size_t sz = 0;
80
81 blk = htx_get_first_blk(htx);
82 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
83 return sz;
84
85 for (; blk; blk = htx_get_next_blk(htx, blk)) {
86 sz += htx_get_blksz(blk);
87 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
88 break;
89 }
90 return sz;
91}
92
Christopher Faulet47596d32018-10-22 09:17:28 +020093/* Finds the first or next occurrence of header <name> in the HTX message <htx>
94 * using the context <ctx>. This structure holds everything necessary to use the
95 * header and find next occurrence. If its <blk> member is NULL, the header is
96 * searched from the beginning. Otherwise, the next occurrence is returned. The
97 * function returns 1 when it finds a value, and 0 when there is no more. It is
98 * designed to work with headers defined as comma-separated lists. If <full> is
99 * set, it works on full-line headers in whose comma is not a delimiter but is
100 * part of the syntax. A special case, if ctx->value is NULL when searching for
101 * a new values of a header, the current header is rescanned. This allows
102 * rescanning after a header deletion.
103 */
104int http_find_header(const struct htx *htx, const struct ist name,
105 struct http_hdr_ctx *ctx, int full)
106{
107 struct htx_blk *blk = ctx->blk;
108 struct ist n, v;
109 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200110
111 if (blk) {
112 char *p;
113
Christopher Faulet47596d32018-10-22 09:17:28 +0200114 if (!ctx->value.ptr)
115 goto rescan_hdr;
116 if (full)
117 goto next_blk;
118 v = htx_get_blk_value(htx, blk);
119 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
120 v.len -= (p - v.ptr);
121 v.ptr = p;
122 if (!v.len)
123 goto next_blk;
124 /* Skip comma */
125 if (*(v.ptr) == ',') {
126 v.ptr++;
127 v.len--;
128 }
129
130 goto return_hdr;
131 }
132
Christopher Faulet192c6a22019-06-11 16:32:24 +0200133 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200134 return 0;
135
Christopher Fauleta3f15502019-05-13 15:27:23 +0200136 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200137 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100139 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
140 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200141 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200142 continue;
Christopher Faulet47596d32018-10-22 09:17:28 +0200143 if (name.len) {
144 /* If no name was passed, we want any header. So skip the comparison */
145 n = htx_get_blk_name(htx, blk);
146 if (!isteqi(n, name))
147 goto next_blk;
148 }
149 v = htx_get_blk_value(htx, blk);
150
151 return_hdr:
152 ctx->lws_before = 0;
153 ctx->lws_after = 0;
154 while (v.len && HTTP_IS_LWS(*v.ptr)) {
155 v.ptr++;
156 v.len--;
157 ctx->lws_before++;
158 }
159 if (!full)
160 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
161 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
162 v.len--;
163 ctx->lws_after++;
164 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200165 ctx->blk = blk;
166 ctx->value = v;
167 return 1;
168
169 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200170 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200171 }
172
173 ctx->blk = NULL;
174 ctx->value = ist("");
175 ctx->lws_before = ctx->lws_after = 0;
176 return 0;
177}
178
179/* Adds a header block int the HTX message <htx>, just before the EOH block. It
180 * returns 1 on success, otherwise it returns 0.
181 */
182int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
183{
184 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200185 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200186 enum htx_blk_type type = htx_get_tail_type(htx);
187 int32_t prev;
188
189 blk = htx_add_header(htx, n, v);
190 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200191 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200192
193 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200194 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200195
196 /* <blk> is the head, swap it iteratively with its predecessor to place
197 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200198 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200199 struct htx_blk *pblk = htx_get_blk(htx, prev);
200 enum htx_blk_type type = htx_get_blk_type(pblk);
201
202 /* Swap .addr and .info fields */
203 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
204 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
205
206 if (blk->addr == pblk->addr)
207 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200208
209 /* Stop when end-of-header is reached */
210 if (type == HTX_BLK_EOH)
211 break;
212
213 blk = pblk;
214 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200215
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200216 end:
217 sl = http_get_stline(htx);
218 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(n, ist("host"))) {
219 if (!http_update_authority(htx, sl, v))
220 goto fail;
221 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200222 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200223
224 fail:
225 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200226}
227
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100228/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200229 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200230 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100231int 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 +0200232{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200233 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200234
Christopher Faulet29f17582019-05-23 11:03:26 +0200235 blk = htx_get_first_blk(htx);
236 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200237 return 0;
238 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200239}
240
Christopher Faulete010c802018-10-24 10:36:45 +0200241/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
242 * on success, otherwise 0.
243 */
244int http_replace_req_meth(struct htx *htx, const struct ist meth)
245{
246 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200247 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100248 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200249
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100250 if (!sl)
251 return 0;
252
Christopher Faulete010c802018-10-24 10:36:45 +0200253 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100254 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
255 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200256
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100257 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
258 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200259
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100260 /* create the new start line */
261 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
262 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200263}
264
265/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
266 * success, otherwise 0.
267 */
268int http_replace_req_uri(struct htx *htx, const struct ist uri)
269{
270 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200271 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100272 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200273
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100274 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200275 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100276
Christopher Faulete010c802018-10-24 10:36:45 +0200277 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100278 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
279 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200280
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100281 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
282 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200283
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100284 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200285 if (!http_replace_stline(htx, meth, uri, vsn))
286 goto fail;
287
288 sl = http_get_stline(htx);
289 if (!http_update_host(htx, sl, uri))
290 goto fail;
291
292 return 1;
293 fail:
294 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200295}
296
297/* Replace the request path in the HTX message <htx> by <path>. The host part
298 * and the query string are preserved. It returns 1 on success, otherwise 0.
299 */
300int http_replace_req_path(struct htx *htx, const struct ist path)
301{
302 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200303 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100304 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200305 size_t plen = 0;
306
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100307 if (!sl)
308 return 0;
309
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100310 uri = htx_sl_req_uri(sl);
311 p = http_get_path(uri);
Christopher Faulete010c802018-10-24 10:36:45 +0200312 if (!p.ptr)
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100313 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200314 while (plen < p.len && *(p.ptr + plen) != '?')
315 plen++;
316
317 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100318 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
319 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200320
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100321 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
322 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
323
324 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200325 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
326 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100327 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200328
329 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200331}
332
333/* Replace the request query-string in the HTX message <htx> by <query>. The
334 * host part and the path are preserved. It returns 1 on success, otherwise
335 * 0.
336 */
337int http_replace_req_query(struct htx *htx, const struct ist query)
338{
339 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200340 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100341 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200342 int offset = 1;
343
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100344 if (!sl)
345 return 0;
346
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100347 uri = htx_sl_req_uri(sl);
348 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200349 while (q.len > 0 && *(q.ptr) != '?') {
350 q.ptr++;
351 q.len--;
352 }
353
354 /* skip the question mark or indicate that we must insert it
355 * (but only if the format string is not empty then).
356 */
357 if (q.len) {
358 q.ptr++;
359 q.len--;
360 }
361 else if (query.len > 1)
362 offset = 0;
363
364 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100365 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
366 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200367
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100368 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
369 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200370
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100371 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
372 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
373 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200374
375 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100376 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200377}
378
379/* Replace the response status in the HTX message <htx> by <status>. It returns
380 * 1 on success, otherwise 0.
381*/
382int http_replace_res_status(struct htx *htx, const struct ist status)
383{
384 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200385 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100386 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200387
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100388 if (!sl)
389 return 0;
390
Christopher Faulete010c802018-10-24 10:36:45 +0200391 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100392 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
393 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200394
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100395 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
396 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200397
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100398 /* create the new start line */
399 sl->info.res.status = strl2ui(status.ptr, status.len);
400 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200401}
402
403/* Replace the response reason in the HTX message <htx> by <reason>. It returns
404 * 1 on success, otherwise 0.
405*/
406int http_replace_res_reason(struct htx *htx, const struct ist reason)
407{
408 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200409 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100410 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200411
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100412 if (!sl)
413 return 0;
414
Christopher Faulete010c802018-10-24 10:36:45 +0200415 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100416 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
417 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200418
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100419 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
420 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200421
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100422 /* create the new start line */
423 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200424}
425
Christopher Faulet47596d32018-10-22 09:17:28 +0200426/* Replaces a part of a header value referenced in the context <ctx> by
427 * <data>. It returns 1 on success, otherwise it returns 0. The context is
428 * updated if necessary.
429 */
430int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
431{
432 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200433 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200434 char *start;
435 struct ist v;
436 uint32_t len, off;
437
438 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200439 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200440
441 v = htx_get_blk_value(htx, blk);
442 start = ctx->value.ptr - ctx->lws_before;
443 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
444 off = start - v.ptr;
445
446 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
447 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200448 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200449
450 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200451
452 sl = http_get_stline(htx);
453 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
454 struct ist n = htx_get_blk_name(htx, blk);
455
456 if (isteq(n, ist("host"))) {
457 if (!http_update_authority(htx, sl, v))
458 goto fail;
459 ctx->blk = NULL;
460 http_find_header(htx, ist("host"), ctx, 1);
461 blk = ctx->blk;
462 v = htx_get_blk_value(htx, blk);
463 }
464 }
465
Christopher Faulet47596d32018-10-22 09:17:28 +0200466 ctx->blk = blk;
467 ctx->value.ptr = v.ptr + off;
468 ctx->value.len = data.len;
469 ctx->lws_before = ctx->lws_after = 0;
470
471 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200472 fail:
473 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200474}
475
476/* Fully replaces a header referenced in the context <ctx> by the name <name>
477 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
478 * context is updated if necessary.
479 */
480int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
481 const struct ist name, const struct ist value)
482{
483 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200484 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200485
486 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200487 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200488
489 blk = htx_replace_header(htx, blk, name, value);
490 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200491 goto fail;
492
493 sl = http_get_stline(htx);
494 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(name, ist("host"))) {
495 if (!http_update_authority(htx, sl, value))
496 goto fail;
497 ctx->blk = NULL;
498 http_find_header(htx, ist("host"), ctx, 1);
499 blk = ctx->blk;
500 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200501
502 ctx->blk = blk;
503 ctx->value = ist(NULL);
504 ctx->lws_before = ctx->lws_after = 0;
505
506 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200507 fail:
508 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200509}
510
511/* Remove one value of a header. This only works on a <ctx> returned by
512 * http_find_header function. The value is removed, as well as surrounding commas
513 * if any. If the removed value was alone, the whole header is removed. The
514 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
515 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
516 * form that can be handled by http_find_header() to find next occurrence.
517 */
518int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
519{
520 struct htx_blk *blk = ctx->blk;
521 char *start;
522 struct ist v;
523 uint32_t len;
524
525 if (!blk)
526 return 0;
527
528 start = ctx->value.ptr - ctx->lws_before;
529 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
530
531 v = htx_get_blk_value(htx, blk);
532 if (len == v.len) {
533 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200534 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200535 ctx->blk = blk;
536 ctx->value = ist2(NULL, 0);
537 ctx->lws_before = ctx->lws_after = 0;
538 }
539 else {
540 ctx->blk = htx_get_blk(htx, htx->tail);
541 ctx->value = htx_get_blk_value(htx, ctx->blk);
542 ctx->lws_before = ctx->lws_after = 0;
543 }
544 return 1;
545 }
546
547 /* This was not the only value of this header. We have to remove the
548 * part pointed by ctx->value. If it is the last entry of the list, we
549 * remove the last separator.
550 */
551 if (start == v.ptr) {
552 /* It's the first header part but not the only one. So remove
553 * the comma after it. */
554 len++;
555 }
556 else {
557 /* There is at least one header part before the removed one. So
558 * remove the comma between them. */
559 start--;
560 len++;
561 }
562 /* Update the block content and its len */
563 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200564 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200565
566 /* Finally update the ctx */
567 ctx->value.ptr = start;
568 ctx->value.len = 0;
569 ctx->lws_before = ctx->lws_after = 0;
570
571 return 1;
572}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200573
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200574/* Updates the authority part of the uri with the value <host>. It happens when
575 * the header host is modified. It returns 0 on failure and 1 on success. It is
576 * the caller responsibility to provide the start-line and to be sure the uri
577 * contains an authority. Thus, if no authority is found in the uri, an error is
578 * returned.
579 */
580static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
581{
582 struct buffer *temp = get_trash_chunk();
583 struct ist meth, vsn, uri, authority;
584
585 uri = htx_sl_req_uri(sl);
586 authority = http_get_authority(uri, 1);
587 if (!authority.len || isteq(host, authority))
588 return 0;
589
590 /* Start by copying old method and version */
591 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
592 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
593
594 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
595 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
596
597 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
598 chunk_memcat(temp, host.ptr, host.len);
599 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
600 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
601
602 return http_replace_stline(htx, meth, uri, vsn);
603
604}
605
606/* Update the header host by extracting the authority of the uri <uri>. flags of
607 * the start-line are also updated accordingly. For orgin-form and asterisk-form
608 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
609 * removed from the flags of the start-line. Otherwise, this flag is set and the
610 * authority is used to set the value of the header host. This function returns
611 * 0 on failure and 1 on success.
612*/
613static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
614{
615 struct ist authority;
616 struct http_hdr_ctx ctx;
617
618 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
619 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
620 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
621 }
622 else {
623 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
624 if (sl->info.req.meth != HTTP_METH_CONNECT) {
625 // absolute-form (RFC7320 #5.3.2)
626 sl->flags |= HTX_SL_F_HAS_SCHM;
627 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
628 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
629
630 authority = http_get_authority(uri, 1);
631 if (!authority.len)
632 goto fail;
633 }
634 else {
635 // authority-form (RFC7320 #5.3.3)
636 authority = uri;
637 }
638
639 /* Replace header host value */
640 ctx.blk = NULL;
641 while (http_find_header(htx, ist("host"), &ctx, 1)) {
642 if (!http_replace_header_value(htx, &ctx, authority))
643 goto fail;
644 }
645
646 }
647 return 1;
648 fail:
649 return 0;
650}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200651
652/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
653 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
654 * performed over the whole headers. Otherwise it must contain a valid header
655 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
656 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
657 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
658 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
659 * -1. The value fetch stops at commas, so this function is suited for use with
660 * list headers.
661 * The return value is 0 if nothing was found, or non-zero otherwise.
662 */
663unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
664 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
665{
666 struct http_hdr_ctx local_ctx;
667 struct ist val_hist[MAX_HDR_HISTORY];
668 unsigned int hist_idx;
669 int found;
670
671 if (!ctx) {
672 local_ctx.blk = NULL;
673 ctx = &local_ctx;
674 }
675
676 if (occ >= 0) {
677 /* search from the beginning */
678 while (http_find_header(htx, hdr, ctx, 0)) {
679 occ--;
680 if (occ <= 0) {
681 *vptr = ctx->value.ptr;
682 *vlen = ctx->value.len;
683 return 1;
684 }
685 }
686 return 0;
687 }
688
689 /* negative occurrence, we scan all the list then walk back */
690 if (-occ > MAX_HDR_HISTORY)
691 return 0;
692
693 found = hist_idx = 0;
694 while (http_find_header(htx, hdr, ctx, 0)) {
695 val_hist[hist_idx] = ctx->value;
696 if (++hist_idx >= MAX_HDR_HISTORY)
697 hist_idx = 0;
698 found++;
699 }
700 if (-occ > found)
701 return 0;
702
703 /* OK now we have the last occurrence in [hist_idx-1], and we need to
704 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
705 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
706 * to remain in the 0..9 range.
707 */
708 hist_idx += occ + MAX_HDR_HISTORY;
709 if (hist_idx >= MAX_HDR_HISTORY)
710 hist_idx -= MAX_HDR_HISTORY;
711 *vptr = val_hist[hist_idx].ptr;
712 *vlen = val_hist[hist_idx].len;
713 return 1;
714}
715
716/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
717 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
718 * performed over the whole headers. Otherwise it must contain a valid header
719 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
720 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
721 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
722 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
723 * -1. This function differs from http_get_hdr() in that it only returns full
724 * line header values and does not stop at commas.
725 * The return value is 0 if nothing was found, or non-zero otherwise.
726 */
727unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
728 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
729{
730 struct http_hdr_ctx local_ctx;
731 struct ist val_hist[MAX_HDR_HISTORY];
732 unsigned int hist_idx;
733 int found;
734
735 if (!ctx) {
736 local_ctx.blk = NULL;
737 ctx = &local_ctx;
738 }
739
740 if (occ >= 0) {
741 /* search from the beginning */
742 while (http_find_header(htx, hdr, ctx, 1)) {
743 occ--;
744 if (occ <= 0) {
745 *vptr = ctx->value.ptr;
746 *vlen = ctx->value.len;
747 return 1;
748 }
749 }
750 return 0;
751 }
752
753 /* negative occurrence, we scan all the list then walk back */
754 if (-occ > MAX_HDR_HISTORY)
755 return 0;
756
757 found = hist_idx = 0;
758 while (http_find_header(htx, hdr, ctx, 1)) {
759 val_hist[hist_idx] = ctx->value;
760 if (++hist_idx >= MAX_HDR_HISTORY)
761 hist_idx = 0;
762 found++;
763 }
764 if (-occ > found)
765 return 0;
766
767 /* OK now we have the last occurrence in [hist_idx-1], and we need to
768 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
769 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
770 * to remain in the 0..9 range.
771 */
772 hist_idx += occ + MAX_HDR_HISTORY;
773 if (hist_idx >= MAX_HDR_HISTORY)
774 hist_idx -= MAX_HDR_HISTORY;
775 *vptr = val_hist[hist_idx].ptr;
776 *vlen = val_hist[hist_idx].len;
777 return 1;
778}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100779
Christopher Faulet90cc4812019-07-22 16:49:30 +0200780int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100781{
782 struct htx *htx;
783 struct htx_sl *sl;
784 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200785 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100786 union h1_sl h1sl;
787 unsigned int flags = HTX_SL_F_IS_RESP;
788 int ret = 0;
789
Christopher Faulet90cc4812019-07-22 16:49:30 +0200790 b_reset(buf);
791 if (!raw.len) {
792 buf->size = 0;
793 buf->area = malloc(raw.len);
794 return 1;
795 }
796
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100797 buf->size = global.tune.bufsize;
798 buf->area = (char *)malloc(buf->size);
799 if (!buf->area)
800 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100801
802 h1m_init_res(&h1m);
803 h1m.flags |= H1_MF_NO_PHDR;
804 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
805 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
806 if (ret <= 0)
807 goto error;
808
809 if (unlikely(h1sl.st.v.len != 8))
810 goto error;
811 if ((*(h1sl.st.v.ptr + 5) > '1') ||
812 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
813 h1m.flags |= H1_MF_VER_11;
814
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200815 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
816 goto error;
817
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100818 if (h1m.flags & H1_MF_VER_11)
819 flags |= HTX_SL_F_VER_11;
820 if (h1m.flags & H1_MF_XFER_ENC)
821 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200822 if (h1m.flags & H1_MF_CLEN) {
823 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
824 if (h1m.body_len == 0)
825 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100826 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200827 if (h1m.flags & H1_MF_CHNK)
828 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100829
830 htx = htx_from_buf(buf);
831 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
832 if (!sl || !htx_add_all_headers(htx, hdrs))
833 goto error;
834 sl->info.res.status = h1sl.st.status;
835
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200836 while (raw.len > ret) {
837 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
838 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100839 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200840 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100841 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200842
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100843 if (!htx_add_endof(htx, HTX_BLK_EOM))
844 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200845
Christopher Faulet90cc4812019-07-22 16:49:30 +0200846 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100847
848error:
849 if (buf->size)
850 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200851 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100852}
853
854static int http_htx_init(void)
855{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100856 struct buffer chk;
857 struct ist raw;
858 int rc;
859 int err_code = 0;
860
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100861 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
862 if (!http_err_msgs[rc]) {
863 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
864 err_code |= ERR_ALERT | ERR_FATAL;
865 continue;
866 }
867
868 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
869 if (!http_str_to_htx(&chk, raw)) {
870 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
871 http_err_codes[rc]);
872 err_code |= ERR_ALERT | ERR_FATAL;
873 }
Christopher Fauletf7346382019-07-17 22:02:08 +0200874 http_err_chunks[rc] = chk;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100875 }
876end:
877 return err_code;
878}
879
Christopher Faulet58857752020-01-15 15:19:50 +0100880static void http_htx_deinit(void)
881{
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100882 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +0100883 struct ebpt_node *node, *next;
884 struct http_error *http_err;
885
886 node = ebpt_first(&http_error_messages);
887 while (node) {
888 next = ebpt_next(node);
889 ebpt_delete(node);
890 http_err = container_of(node, typeof(*http_err), node);
891 chunk_destroy(&http_err->msg);
892 free(node->key);
893 free(http_err);
894 node = next;
895 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100896
897 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
898 free(http_errs->conf.file);
899 free(http_errs->id);
900 LIST_DEL(&http_errs->list);
901 free(http_errs);
902 }
Christopher Faulet58857752020-01-15 15:19:50 +0100903}
904
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100905REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +0100906REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +0100907
Christopher Faulet58857752020-01-15 15:19:50 +0100908/* Reads content of the error file <file> and convert it into an HTX message. On
909 * success, the HTX message is returned. On error, NULL is returned and an error
910 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +0100911 */
Christopher Faulet58857752020-01-15 15:19:50 +0100912struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +0100913{
Christopher Faulet58857752020-01-15 15:19:50 +0100914 struct buffer *buf = NULL;
915 struct buffer chk;
916 struct ebpt_node *node;
917 struct http_error *http_err;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100918 struct stat stat;
919 char *err = NULL;
920 int errnum, errlen;
921 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +0100922
923 /* already loaded */
924 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
925 if (node) {
926 http_err = container_of(node, typeof(*http_err), node);
927 buf = &http_err->msg;
928 goto out;
929 }
Christopher Faulet5031ef52020-01-15 11:22:07 +0100930
Christopher Faulet58857752020-01-15 15:19:50 +0100931 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +0100932 fd = open(file, O_RDONLY);
933 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
934 memprintf(errmsg, "error opening file '%s'.", file);
935 goto out;
936 }
937
938 if (stat.st_size <= global.tune.bufsize)
939 errlen = stat.st_size;
940 else {
941 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
942 file, global.tune.bufsize);
943 errlen = global.tune.bufsize;
944 }
945
946 err = malloc(errlen);
947 if (!err) {
948 memprintf(errmsg, "out of memory.");
949 goto out;
950 }
951
952 errnum = read(fd, err, errlen);
953 if (errnum != errlen) {
954 memprintf(errmsg, "error reading file '%s'.", file);
955 goto out;
956 }
957
Christopher Faulet58857752020-01-15 15:19:50 +0100958 /* Create the node corresponding to the error file */
959 http_err = calloc(1, sizeof(*http_err));
960 if (!http_err) {
961 memprintf(errmsg, "out of memory.");
962 goto out;
963 }
964 http_err->node.key = strdup(file);
965 if (!http_err->node.key) {
966 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +0100967 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +0100968 goto out;
969 }
970
971 /* Convert the error file into an HTX message */
972 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +0100973 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Faulet58857752020-01-15 15:19:50 +0100974 free(http_err->node.key);
975 free(http_err);
Christopher Faulet5031ef52020-01-15 11:22:07 +0100976 goto out;
977 }
978
Christopher Faulet58857752020-01-15 15:19:50 +0100979 /* Insert the node in the tree and return the HTX message */
980 http_err->msg = chk;
981 ebis_insert(&http_error_messages, &http_err->node);
982 buf = &http_err->msg;
983
Christopher Faulet5031ef52020-01-15 11:22:07 +0100984 out:
985 if (fd >= 0)
986 close(fd);
987 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +0100988 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100989}
990
Christopher Faulet58857752020-01-15 15:19:50 +0100991/* Convert the raw http message <msg> into an HTX message. On sucess, the HTX
992 * message is returned. On error, NULL is returned and an error message is
993 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +0100994 */
Christopher Faulet58857752020-01-15 15:19:50 +0100995struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +0100996{
Christopher Faulet58857752020-01-15 15:19:50 +0100997 struct buffer *buf = NULL;
998 struct buffer chk;
999 struct ebpt_node *node;
1000 struct http_error *http_err;
1001
1002 /* already loaded */
1003 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1004 if (node) {
1005 http_err = container_of(node, typeof(*http_err), node);
1006 buf = &http_err->msg;
1007 goto out;
1008 }
1009 /* Create the node corresponding to the error file */
1010 http_err = calloc(1, sizeof(*http_err));
1011 if (!http_err) {
1012 memprintf(errmsg, "out of memory.");
1013 goto out;
1014 }
1015 http_err->node.key = strdup(key);
1016 if (!http_err->node.key) {
1017 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001018 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +01001019 goto out;
1020 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001021
1022 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001023 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001024 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Faulet58857752020-01-15 15:19:50 +01001025 free(http_err->node.key);
1026 free(http_err);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001027 goto out;
1028 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001029
Christopher Faulet58857752020-01-15 15:19:50 +01001030 /* Insert the node in the tree and return the HTX message */
1031 http_err->msg = chk;
1032 ebis_insert(&http_error_messages, &http_err->node);
1033 buf = &http_err->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001034 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001035 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001036}
1037
Christopher Faulet5031ef52020-01-15 11:22:07 +01001038/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001039 * <status>. It returns NULL if there is any error, otherwise it return the
1040 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001041 */
Christopher Faulet58857752020-01-15 15:19:50 +01001042struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001043{
Christopher Faulet58857752020-01-15 15:19:50 +01001044 struct buffer *buf = NULL;
1045 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001046
1047 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1048 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001049 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001050 break;
1051 }
1052 }
1053
1054 if (rc >= HTTP_ERR_SIZE)
1055 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001056 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001057}
1058
1059/* This function creates HTX error message corresponding to a redirect message
1060 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001061 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1062 * returns NULL if there is any error, otherwise it return the corresponding HTX
1063 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001064 */
Christopher Faulet58857752020-01-15 15:19:50 +01001065struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001066{
Christopher Faulet58857752020-01-15 15:19:50 +01001067 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001068 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001069 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001070 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001071
1072 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1073 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001074 /* Create the error key */
1075 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1076 memprintf(errmsg, "out of memory.");
1077 goto out;
1078 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001079 /* Create the error message */
1080 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1081 errlen = strlen(msg) + strlen(url) + 5;
1082 err = malloc(errlen);
1083 if (!err) {
1084 memprintf(errmsg, "out of memory.");
1085 goto out;
1086 }
1087 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1088
1089 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001090 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001091 break;
1092 }
1093 }
1094
1095 if (rc >= HTTP_ERR_SIZE)
1096 memprintf(errmsg, "status code '%d' not handled.", status);
1097out:
Christopher Faulet58857752020-01-15 15:19:50 +01001098 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001099 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001100 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001101}
1102
Christopher Faulet07f41f72020-01-16 16:16:06 +01001103/* Parses the "errorloc[302|303]" proxy keyword */
1104static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1105 struct proxy *defpx, const char *file, int line,
1106 char **errmsg)
1107{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001108 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001109 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001110 int errloc, status;
1111 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001112
1113 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1114 ret = 1;
1115 goto out;
1116 }
1117
1118 if (*(args[1]) == 0 || *(args[2]) == 0) {
1119 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1120 ret = -1;
1121 goto out;
1122 }
1123
1124 status = atol(args[1]);
1125 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1126 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1127 if (!msg) {
1128 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1129 ret = -1;
1130 goto out;
1131 }
1132
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001133 conf_err = calloc(1, sizeof(*conf_err));
1134 if (!conf_err) {
1135 memprintf(errmsg, "%s : out of memory.", args[0]);
1136 ret = -1;
1137 goto out;
1138 }
1139 conf_err->type = 1;
1140 conf_err->info.errorfile.status = status;
1141 conf_err->info.errorfile.msg = msg;
1142 conf_err->file = strdup(file);
1143 conf_err->line = line;
1144 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001145
1146 out:
1147 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001148
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001149}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001150
1151/* Parses the "errorfile" proxy keyword */
1152static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1153 struct proxy *defpx, const char *file, int line,
1154 char **errmsg)
1155{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001156 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001157 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001158 int status;
1159 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001160
1161 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1162 ret = 1;
1163 goto out;
1164 }
1165
1166 if (*(args[1]) == 0 || *(args[2]) == 0) {
1167 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1168 ret = -1;
1169 goto out;
1170 }
1171
1172 status = atol(args[1]);
1173 msg = http_parse_errorfile(status, args[2], errmsg);
1174 if (!msg) {
1175 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1176 ret = -1;
1177 goto out;
1178 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001179
1180 conf_err = calloc(1, sizeof(*conf_err));
1181 if (!conf_err) {
1182 memprintf(errmsg, "%s : out of memory.", args[0]);
1183 ret = -1;
1184 goto out;
1185 }
1186 conf_err->type = 1;
1187 conf_err->info.errorfile.status = status;
1188 conf_err->info.errorfile.msg = msg;
1189 conf_err->file = strdup(file);
1190 conf_err->line = line;
1191 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1192
1193 out:
1194 return ret;
1195
1196}
1197
1198/* Parses the "errorfiles" proxy keyword */
1199static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1200 struct proxy *defpx, const char *file, int line,
1201 char **err)
1202{
1203 struct conf_errors *conf_err = NULL;
1204 char *name = NULL;
1205 int rc, ret = 0;
1206
1207 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1208 ret = 1;
1209 goto out;
1210 }
1211
1212 if (!*(args[1])) {
1213 memprintf(err, "%s : expects <name> as argument.", args[0]);
1214 ret = -1;
1215 goto out;
1216 }
1217
1218 name = strdup(args[1]);
1219 conf_err = calloc(1, sizeof(*conf_err));
1220 if (!name || !conf_err) {
1221 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001222 goto error;
1223 }
1224 conf_err->type = 0;
1225
1226 conf_err->info.errorfiles.name = name;
1227 if (!*(args[2])) {
1228 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1229 conf_err->info.errorfiles.status[rc] = 1;
1230 }
1231 else {
1232 int cur_arg, status;
1233 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1234 status = atol(args[cur_arg]);
1235
1236 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1237 if (http_err_codes[rc] == status) {
1238 conf_err->info.errorfiles.status[rc] = 2;
1239 break;
1240 }
1241 }
1242 if (rc >= HTTP_ERR_SIZE) {
1243 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001244 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001245 }
1246 }
1247 }
1248 conf_err->file = strdup(file);
1249 conf_err->line = line;
1250 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1251 out:
1252 return ret;
1253
1254 error:
1255 free(name);
1256 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001257 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001258 goto out;
1259}
1260
1261/* Check "errorfiles" proxy keyword */
1262static int proxy_check_errors(struct proxy *px)
1263{
1264 struct conf_errors *conf_err, *conf_err_back;
1265 struct http_errors *http_errs;
1266 int rc, err = 0;
1267
1268 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1269 if (conf_err->type == 1) {
1270 /* errorfile */
1271 rc = http_get_status_idx(conf_err->info.errorfile.status);
1272 px->errmsg[rc] = conf_err->info.errorfile.msg;
1273 }
1274 else {
1275 /* errorfiles */
1276 list_for_each_entry(http_errs, &http_errors_list, list) {
1277 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1278 break;
1279 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001280
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001281 /* unknown http-errors section */
1282 if (&http_errs->list == &http_errors_list) {
1283 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1284 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1285 err |= ERR_ALERT | ERR_FATAL;
1286 free(conf_err->info.errorfiles.name);
1287 goto next;
1288 }
1289
1290 free(conf_err->info.errorfiles.name);
1291 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1292 if (conf_err->info.errorfiles.status[rc] > 0) {
1293 if (http_errs->errmsg[rc])
1294 px->errmsg[rc] = http_errs->errmsg[rc];
1295 else if (conf_err->info.errorfiles.status[rc] == 2)
1296 ha_warning("config: proxy '%s' : status '%d' not declared in"
1297 " http-errors section '%s' (at %s:%d).\n",
1298 px->id, http_err_codes[rc], http_errs->id,
1299 conf_err->file, conf_err->line);
1300 }
1301 }
1302 }
1303 next:
1304 LIST_DEL(&conf_err->list);
1305 free(conf_err->file);
1306 free(conf_err);
1307 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001308
1309 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001310 return err;
1311}
1312
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001313static int post_check_errors()
1314{
1315 struct ebpt_node *node;
1316 struct http_error *http_err;
1317 struct htx *htx;
1318 int err_code = 0;
1319
1320 node = ebpt_first(&http_error_messages);
1321 while (node) {
1322 http_err = container_of(node, typeof(*http_err), node);
1323 if (b_is_null(&http_err->msg))
1324 goto next;
1325 htx = htxbuf(&http_err->msg);
1326 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1327 ha_warning("config: errorfile '%s' runs over the buffer space"
1328 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001329 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001330 (char *)node->key);
1331 err_code |= ERR_WARN;
1332 }
1333 next:
1334 node = ebpt_next(node);
1335 }
1336
1337 return err_code;
1338}
1339
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001340int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1341{
1342 struct conf_errors *conf_err, *new_conf_err = NULL;
1343 int ret = 0;
1344
1345 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1346 new_conf_err = calloc(1, sizeof(*new_conf_err));
1347 if (!new_conf_err) {
1348 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1349 goto out;
1350 }
1351 new_conf_err->type = conf_err->type;
1352 if (conf_err->type == 1) {
1353 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1354 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1355 }
1356 else {
1357 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1358 if (!new_conf_err->info.errorfiles.name) {
1359 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1360 goto out;
1361 }
1362 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1363 sizeof(conf_err->info.errorfiles.status));
1364 }
1365 new_conf_err->file = strdup(conf_err->file);
1366 new_conf_err->line = conf_err->line;
1367 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1368 new_conf_err = NULL;
1369 }
1370 ret = 1;
1371
1372 out:
1373 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001374 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001375}
1376
1377void proxy_release_conf_errors(struct proxy *px)
1378{
1379 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001380
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001381 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1382 if (conf_err->type == 0)
1383 free(conf_err->info.errorfiles.name);
1384 LIST_DEL(&conf_err->list);
1385 free(conf_err->file);
1386 free(conf_err);
1387 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001388}
1389
1390/*
1391 * Parse an <http-errors> section.
1392 * Returns the error code, 0 if OK, or any combination of :
1393 * - ERR_ABORT: must abort ASAP
1394 * - ERR_FATAL: we can continue parsing but not start the service
1395 * - ERR_WARN: a warning has been emitted
1396 * - ERR_ALERT: an alert has been emitted
1397 * Only the two first ones can stop processing, the two others are just
1398 * indicators.
1399 */
1400static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1401{
1402 static struct http_errors *curr_errs = NULL;
1403 int err_code = 0;
1404 const char *err;
1405 char *errmsg = NULL;
1406
1407 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1408 if (!*args[1]) {
1409 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1410 err_code |= ERR_ALERT | ERR_ABORT;
1411 goto out;
1412 }
1413
1414 err = invalid_char(args[1]);
1415 if (err) {
1416 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1417 file, linenum, *err, args[0], args[1]);
1418 err_code |= ERR_ALERT | ERR_FATAL;
1419 }
1420
1421 list_for_each_entry(curr_errs, &http_errors_list, list) {
1422 /* Error if two errors section owns the same name */
1423 if (strcmp(curr_errs->id, args[1]) == 0) {
1424 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1425 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1426 err_code |= ERR_ALERT | ERR_FATAL;
1427 }
1428 }
1429
1430 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1431 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1432 err_code |= ERR_ALERT | ERR_ABORT;
1433 goto out;
1434 }
1435
1436 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1437 curr_errs->id = strdup(args[1]);
1438 curr_errs->conf.file = strdup(file);
1439 curr_errs->conf.line = linenum;
1440 }
1441 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1442 struct buffer *msg;
1443 int status, rc;
1444
1445 if (*(args[1]) == 0 || *(args[2]) == 0) {
1446 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1447 file, linenum, args[0]);
1448 err_code |= ERR_ALERT | ERR_FATAL;
1449 goto out;
1450 }
1451
1452 status = atol(args[1]);
1453 msg = http_parse_errorfile(status, args[2], &errmsg);
1454 if (!msg) {
1455 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1456 err_code |= ERR_ALERT | ERR_FATAL;
1457 goto out;
1458 }
1459 rc = http_get_status_idx(status);
1460 curr_errs->errmsg[rc] = msg;
1461 }
1462 else if (*args[0] != 0) {
1463 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1464 err_code |= ERR_ALERT | ERR_FATAL;
1465 goto out;
1466 }
1467
1468out:
1469 free(errmsg);
1470 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001471}
1472
1473static struct cfg_kw_list cfg_kws = {ILH, {
1474 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1475 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1476 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1477 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001478 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01001479 { 0, NULL, NULL },
1480}};
1481
1482INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001483REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001484REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001485
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001486REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1487
Christopher Faulet29f72842019-12-11 15:52:32 +01001488/************************************************************************/
1489/* HTX sample fetches */
1490/************************************************************************/
1491
1492/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1493static int
1494smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1495{
1496 if (!smp->strm)
1497 return 0;
1498
1499 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1500 smp->data.type = SMP_T_BOOL;
1501 return 1;
1502}
1503
1504/* Returns the number of blocks in an HTX message. The channel is chosen
1505 * depending on the sample direction. */
1506static int
1507smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1508{
1509 struct channel *chn;
1510 struct htx *htx;
1511
1512 if (!smp->strm)
1513 return 0;
1514
1515 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1516 htx = smp_prefetch_htx(smp, chn, 0);
1517 if (!htx)
1518 return 0;
1519
1520 smp->data.u.sint = htx_nbblks(htx);
1521 smp->data.type = SMP_T_SINT;
1522 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1523 return 1;
1524}
1525
1526/* Returns the size of an HTX message. The channel is chosen depending on the
1527 * sample direction. */
1528static int
1529smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1530{
1531 struct channel *chn;
1532 struct htx *htx;
1533
1534 if (!smp->strm)
1535 return 0;
1536
1537 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1538 htx = smp_prefetch_htx(smp, chn, 0);
1539 if (!htx)
1540 return 0;
1541
1542 smp->data.u.sint = htx->size;
1543 smp->data.type = SMP_T_SINT;
1544 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1545 return 1;
1546}
1547
1548/* Returns the data size of an HTX message. The channel is chosen depending on the
1549 * sample direction. */
1550static int
1551smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1552{
1553 struct channel *chn;
1554 struct htx *htx;
1555
1556 if (!smp->strm)
1557 return 0;
1558
1559 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1560 htx = smp_prefetch_htx(smp, chn, 0);
1561 if (!htx)
1562 return 0;
1563
1564 smp->data.u.sint = htx->data;
1565 smp->data.type = SMP_T_SINT;
1566 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1567 return 1;
1568}
1569
1570/* Returns the used space (data+meta) of an HTX message. The channel is chosen
1571 * depending on the sample direction. */
1572static int
1573smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1574{
1575 struct channel *chn;
1576 struct htx *htx;
1577
1578 if (!smp->strm)
1579 return 0;
1580
1581 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1582 htx = smp_prefetch_htx(smp, chn, 0);
1583 if (!htx)
1584 return 0;
1585
1586 smp->data.u.sint = htx_used_space(htx);
1587 smp->data.type = SMP_T_SINT;
1588 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1589 return 1;
1590}
1591
1592/* Returns the free space (size-used) of an HTX message. The channel is chosen
1593 * depending on the sample direction. */
1594static int
1595smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1596{
1597 struct channel *chn;
1598 struct htx *htx;
1599
1600 if (!smp->strm)
1601 return 0;
1602
1603 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1604 htx = smp_prefetch_htx(smp, chn, 0);
1605 if (!htx)
1606 return 0;
1607
1608 smp->data.u.sint = htx_free_space(htx);
1609 smp->data.type = SMP_T_SINT;
1610 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1611 return 1;
1612}
1613
1614/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
1615 * channel is chosen depending on the sample direction. */
1616static int
1617smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1618{
1619 struct channel *chn;
1620 struct htx *htx;
1621
1622 if (!smp->strm)
1623 return 0;
1624
1625 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1626 htx = smp_prefetch_htx(smp, chn, 0);
1627 if (!htx)
1628 return 0;
1629
1630 smp->data.u.sint = htx_free_data_space(htx);
1631 smp->data.type = SMP_T_SINT;
1632 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1633 return 1;
1634}
1635
1636/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
1637 * 0. Concretely, it only checks the tail. The channel is chosen depending on
1638 * the sample direction. */
1639static int
1640smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1641{
1642 struct channel *chn;
1643 struct htx *htx;
1644
1645 if (!smp->strm)
1646 return 0;
1647
1648 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1649 htx = smp_prefetch_htx(smp, chn, 0);
1650 if (!htx)
1651 return 0;
1652
1653 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
1654 smp->data.type = SMP_T_BOOL;
1655 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1656 return 1;
1657}
1658
1659/* Returns the type of a specific HTX block, if found in the message. Otherwise
1660 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
1661 * "head", "tail" or "first". The channel is chosen depending on the sample
1662 * direction. */
1663static int
1664smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1665{
1666 struct channel *chn;
1667 struct htx *htx;
1668 enum htx_blk_type type;
1669 int32_t pos;
1670
1671 if (!smp->strm || !arg_p)
1672 return 0;
1673
1674 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1675 htx = smp_prefetch_htx(smp, chn, 0);
1676 if (!htx)
1677 return 0;
1678
1679 pos = arg_p[0].data.sint;
1680 if (pos == -1)
1681 type = htx_get_head_type(htx);
1682 else if (pos == -2)
1683 type = htx_get_tail_type(htx);
1684 else if (pos == -3)
1685 type = htx_get_first_type(htx);
1686 else
1687 type = ((pos >= htx->head && pos <= htx->tail)
1688 ? htx_get_blk_type(htx_get_blk(htx, pos))
1689 : HTX_BLK_UNUSED);
1690
1691 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
1692 smp->data.type = SMP_T_STR;
1693 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1694 return 1;
1695}
1696
1697/* Returns the size of a specific HTX block, if found in the message. Otherwise
1698 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
1699 * "first". The channel is chosen depending on the sample direction. */
1700static int
1701smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1702{
1703 struct channel *chn;
1704 struct htx *htx;
1705 struct htx_blk *blk;
1706 int32_t pos;
1707
1708 if (!smp->strm || !arg_p)
1709 return 0;
1710
1711 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1712 htx = smp_prefetch_htx(smp, chn, 0);
1713 if (!htx)
1714 return 0;
1715
1716 pos = arg_p[0].data.sint;
1717 if (pos == -1)
1718 blk = htx_get_head_blk(htx);
1719 else if (pos == -2)
1720 blk = htx_get_tail_blk(htx);
1721 else if (pos == -3)
1722 blk = htx_get_first_blk(htx);
1723 else
1724 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1725
1726 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
1727 smp->data.type = SMP_T_SINT;
1728 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1729 return 1;
1730}
1731
1732/* Returns the start-line if the selected HTX block exists and is a
1733 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
1734 * supported or "head", "tail" or "first". The channel is chosen depending on
1735 * the sample direction. */
1736static int
1737smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1738{
1739 struct buffer *temp;
1740 struct channel *chn;
1741 struct htx *htx;
1742 struct htx_blk *blk;
1743 struct htx_sl *sl;
1744 int32_t pos;
1745
1746 if (!smp->strm || !arg_p)
1747 return 0;
1748
1749 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1750 htx = smp_prefetch_htx(smp, chn, 0);
1751 if (!htx)
1752 return 0;
1753
1754 pos = arg_p[0].data.sint;
1755 if (pos == -1)
1756 blk = htx_get_head_blk(htx);
1757 else if (pos == -2)
1758 blk = htx_get_tail_blk(htx);
1759 else if (pos == -3)
1760 blk = htx_get_first_blk(htx);
1761 else
1762 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1763
1764 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
1765 smp->data.u.str.size = 0;
1766 smp->data.u.str.area = "";
1767 smp->data.u.str.data = 0;
1768 }
1769 else {
1770 sl = htx_get_blk_ptr(htx, blk);
1771
1772 temp = get_trash_chunk();
1773 chunk_istcat(temp, htx_sl_p1(sl));
1774 temp->area[temp->data++] = ' ';
1775 chunk_istcat(temp, htx_sl_p2(sl));
1776 temp->area[temp->data++] = ' ';
1777 chunk_istcat(temp, htx_sl_p3(sl));
1778
1779 smp->data.u.str = *temp;
1780 }
1781
1782 smp->data.type = SMP_T_STR;
1783 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1784 return 1;
1785}
1786
1787/* Returns the header name if the selected HTX block exists and is a header or a
1788 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1789 * supported or "head", "tail" or "first". The channel is chosen depending on
1790 * the sample direction. */
1791static int
1792smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1793{
1794 struct channel *chn;
1795 struct htx *htx;
1796 struct htx_blk *blk;
1797 int32_t pos;
1798
1799 if (!smp->strm || !arg_p)
1800 return 0;
1801
1802 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1803 htx = smp_prefetch_htx(smp, chn, 0);
1804 if (!htx)
1805 return 0;
1806
1807 pos = arg_p[0].data.sint;
1808 if (pos == -1)
1809 blk = htx_get_head_blk(htx);
1810 else if (pos == -2)
1811 blk = htx_get_tail_blk(htx);
1812 else if (pos == -3)
1813 blk = htx_get_first_blk(htx);
1814 else
1815 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1816
1817 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1818 smp->data.u.str.size = 0;
1819 smp->data.u.str.area = "";
1820 smp->data.u.str.data = 0;
1821 }
1822 else {
1823 struct ist name = htx_get_blk_name(htx, blk);
1824
1825 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
1826 }
1827 smp->data.type = SMP_T_STR;
1828 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1829 return 1;
1830}
1831
1832/* Returns the header value if the selected HTX block exists and is a header or
1833 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1834 * supported or "head", "tail" or "first". The channel is chosen depending on
1835 * the sample direction. */
1836static int
1837smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1838{
1839 struct channel *chn;
1840 struct htx *htx;
1841 struct htx_blk *blk;
1842 int32_t pos;
1843
1844 if (!smp->strm || !arg_p)
1845 return 0;
1846
1847 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1848 htx = smp_prefetch_htx(smp, chn, 0);
1849 if (!htx)
1850 return 0;
1851
1852 pos = arg_p[0].data.sint;
1853 if (pos == -1)
1854 blk = htx_get_head_blk(htx);
1855 else if (pos == -2)
1856 blk = htx_get_tail_blk(htx);
1857 else if (pos == -3)
1858 blk = htx_get_first_blk(htx);
1859 else
1860 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1861
1862 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1863 smp->data.u.str.size = 0;
1864 smp->data.u.str.area = "";
1865 smp->data.u.str.data = 0;
1866 }
1867 else {
1868 struct ist val = htx_get_blk_value(htx, blk);
1869
1870 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1871 }
1872 smp->data.type = SMP_T_STR;
1873 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1874 return 1;
1875}
1876
1877/* Returns the value if the selected HTX block exists and is a data
1878 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
1879 * or "head", "tail" or "first". The channel is chosen depending on the sample
1880 * direction. */
1881static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01001882smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01001883{
1884 struct channel *chn;
1885 struct htx *htx;
1886 struct htx_blk *blk;
1887 int32_t pos;
1888
1889 if (!smp->strm || !arg_p)
1890 return 0;
1891
1892 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1893 htx = smp_prefetch_htx(smp, chn, 0);
1894 if (!htx)
1895 return 0;
1896
1897 pos = arg_p[0].data.sint;
1898 if (pos == -1)
1899 blk = htx_get_head_blk(htx);
1900 else if (pos == -2)
1901 blk = htx_get_tail_blk(htx);
1902 else if (pos == -3)
1903 blk = htx_get_first_blk(htx);
1904 else
1905 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1906
1907 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
1908 smp->data.u.str.size = 0;
1909 smp->data.u.str.area = "";
1910 smp->data.u.str.data = 0;
1911 }
1912 else {
1913 struct ist val = htx_get_blk_value(htx, blk);
1914
1915 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1916 }
Christopher Faulet8178e402020-01-08 14:38:58 +01001917 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01001918 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1919 return 1;
1920}
1921
1922/* This function is used to validate the arguments passed to any "htx_blk" fetch
1923 * keywords. An argument is expected by these keywords. It must be a positive
1924 * integer or on of the following strings: "head", "tail" or "first". It returns
1925 * 0 on error, and a non-zero value if OK.
1926 */
1927int val_blk_arg(struct arg *arg, char **err_msg)
1928{
1929 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
1930 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
1931 return 0;
1932 }
1933 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
1934 free(arg[0].data.str.area);
1935 arg[0].type = ARGT_SINT;
1936 arg[0].data.sint = -1;
1937 }
1938 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
1939 free(arg[0].data.str.area);
1940 arg[0].type = ARGT_SINT;
1941 arg[0].data.sint = -2;
1942 }
1943 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
1944 free(arg[0].data.str.area);
1945 arg[0].type = ARGT_SINT;
1946 arg[0].data.sint = -3;
1947 }
1948 else {
1949 int pos;
1950
1951 for (pos = 0; pos < arg[0].data.str.data; pos++) {
1952 if (!isdigit(arg[0].data.str.area[pos])) {
1953 memprintf(err_msg, "invalid block position");
1954 return 0;
1955 }
1956 }
1957
1958 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
1959 if (pos < 0) {
1960 memprintf(err_msg, "block position must not be negative");
1961 return 0;
1962 }
1963 free(arg[0].data.str.area);
1964 arg[0].type = ARGT_SINT;
1965 arg[0].data.sint = pos;
1966 }
1967
1968 return 1;
1969}
1970
1971
1972/* Note: must not be declared <const> as its list will be overwritten.
1973 * Note: htx sample fetches should only used for developpement purpose.
1974 */
1975static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01001976 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01001977
Christopher Faulet01f44452020-01-08 14:23:40 +01001978 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1979 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1980 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1981 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1982 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1983 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1984 { "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 +01001985
Christopher Faulet01f44452020-01-08 14:23:40 +01001986 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1987 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1988 { "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},
1989 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1990 { "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 +01001991 { "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 +01001992
1993 { /* END */ },
1994}};
1995
1996INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);