blob: 35bb056252e2b8bd7a246529f2a44ecda78e25e7 [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);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100218 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200219 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);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100494 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200495 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);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100587 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200588 return 0;
589
Christopher Faulet34b18e42020-02-18 11:02:21 +0100590 /* Don't update the uri if there is no change */
591 if (isteq(host, authority))
592 return 1;
593
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200594 /* Start by copying old method and version */
595 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
596 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
597
598 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
599 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
600
601 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
602 chunk_memcat(temp, host.ptr, host.len);
603 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
604 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
605
606 return http_replace_stline(htx, meth, uri, vsn);
607
608}
609
610/* Update the header host by extracting the authority of the uri <uri>. flags of
611 * the start-line are also updated accordingly. For orgin-form and asterisk-form
612 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
613 * removed from the flags of the start-line. Otherwise, this flag is set and the
614 * authority is used to set the value of the header host. This function returns
615 * 0 on failure and 1 on success.
616*/
617static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
618{
619 struct ist authority;
620 struct http_hdr_ctx ctx;
621
622 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
623 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
624 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
625 }
626 else {
627 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
628 if (sl->info.req.meth != HTTP_METH_CONNECT) {
629 // absolute-form (RFC7320 #5.3.2)
630 sl->flags |= HTX_SL_F_HAS_SCHM;
631 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
632 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
633
634 authority = http_get_authority(uri, 1);
635 if (!authority.len)
636 goto fail;
637 }
638 else {
639 // authority-form (RFC7320 #5.3.3)
640 authority = uri;
641 }
642
643 /* Replace header host value */
644 ctx.blk = NULL;
645 while (http_find_header(htx, ist("host"), &ctx, 1)) {
646 if (!http_replace_header_value(htx, &ctx, authority))
647 goto fail;
648 }
649
650 }
651 return 1;
652 fail:
653 return 0;
654}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200655
656/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
657 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
658 * performed over the whole headers. Otherwise it must contain a valid header
659 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
660 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
661 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
662 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
663 * -1. The value fetch stops at commas, so this function is suited for use with
664 * list headers.
665 * The return value is 0 if nothing was found, or non-zero otherwise.
666 */
667unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
668 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
669{
670 struct http_hdr_ctx local_ctx;
671 struct ist val_hist[MAX_HDR_HISTORY];
672 unsigned int hist_idx;
673 int found;
674
675 if (!ctx) {
676 local_ctx.blk = NULL;
677 ctx = &local_ctx;
678 }
679
680 if (occ >= 0) {
681 /* search from the beginning */
682 while (http_find_header(htx, hdr, ctx, 0)) {
683 occ--;
684 if (occ <= 0) {
685 *vptr = ctx->value.ptr;
686 *vlen = ctx->value.len;
687 return 1;
688 }
689 }
690 return 0;
691 }
692
693 /* negative occurrence, we scan all the list then walk back */
694 if (-occ > MAX_HDR_HISTORY)
695 return 0;
696
697 found = hist_idx = 0;
698 while (http_find_header(htx, hdr, ctx, 0)) {
699 val_hist[hist_idx] = ctx->value;
700 if (++hist_idx >= MAX_HDR_HISTORY)
701 hist_idx = 0;
702 found++;
703 }
704 if (-occ > found)
705 return 0;
706
707 /* OK now we have the last occurrence in [hist_idx-1], and we need to
708 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
709 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
710 * to remain in the 0..9 range.
711 */
712 hist_idx += occ + MAX_HDR_HISTORY;
713 if (hist_idx >= MAX_HDR_HISTORY)
714 hist_idx -= MAX_HDR_HISTORY;
715 *vptr = val_hist[hist_idx].ptr;
716 *vlen = val_hist[hist_idx].len;
717 return 1;
718}
719
720/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
721 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
722 * performed over the whole headers. Otherwise it must contain a valid header
723 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
724 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
725 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
726 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
727 * -1. This function differs from http_get_hdr() in that it only returns full
728 * line header values and does not stop at commas.
729 * The return value is 0 if nothing was found, or non-zero otherwise.
730 */
731unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
732 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
733{
734 struct http_hdr_ctx local_ctx;
735 struct ist val_hist[MAX_HDR_HISTORY];
736 unsigned int hist_idx;
737 int found;
738
739 if (!ctx) {
740 local_ctx.blk = NULL;
741 ctx = &local_ctx;
742 }
743
744 if (occ >= 0) {
745 /* search from the beginning */
746 while (http_find_header(htx, hdr, ctx, 1)) {
747 occ--;
748 if (occ <= 0) {
749 *vptr = ctx->value.ptr;
750 *vlen = ctx->value.len;
751 return 1;
752 }
753 }
754 return 0;
755 }
756
757 /* negative occurrence, we scan all the list then walk back */
758 if (-occ > MAX_HDR_HISTORY)
759 return 0;
760
761 found = hist_idx = 0;
762 while (http_find_header(htx, hdr, ctx, 1)) {
763 val_hist[hist_idx] = ctx->value;
764 if (++hist_idx >= MAX_HDR_HISTORY)
765 hist_idx = 0;
766 found++;
767 }
768 if (-occ > found)
769 return 0;
770
771 /* OK now we have the last occurrence in [hist_idx-1], and we need to
772 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
773 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
774 * to remain in the 0..9 range.
775 */
776 hist_idx += occ + MAX_HDR_HISTORY;
777 if (hist_idx >= MAX_HDR_HISTORY)
778 hist_idx -= MAX_HDR_HISTORY;
779 *vptr = val_hist[hist_idx].ptr;
780 *vlen = val_hist[hist_idx].len;
781 return 1;
782}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100783
Christopher Faulet90cc4812019-07-22 16:49:30 +0200784int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100785{
786 struct htx *htx;
787 struct htx_sl *sl;
788 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200789 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100790 union h1_sl h1sl;
791 unsigned int flags = HTX_SL_F_IS_RESP;
792 int ret = 0;
793
Christopher Faulet90cc4812019-07-22 16:49:30 +0200794 b_reset(buf);
795 if (!raw.len) {
796 buf->size = 0;
797 buf->area = malloc(raw.len);
798 return 1;
799 }
800
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100801 buf->size = global.tune.bufsize;
802 buf->area = (char *)malloc(buf->size);
803 if (!buf->area)
804 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100805
806 h1m_init_res(&h1m);
807 h1m.flags |= H1_MF_NO_PHDR;
808 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
809 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
810 if (ret <= 0)
811 goto error;
812
813 if (unlikely(h1sl.st.v.len != 8))
814 goto error;
815 if ((*(h1sl.st.v.ptr + 5) > '1') ||
816 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
817 h1m.flags |= H1_MF_VER_11;
818
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200819 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
820 goto error;
821
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100822 if (h1m.flags & H1_MF_VER_11)
823 flags |= HTX_SL_F_VER_11;
824 if (h1m.flags & H1_MF_XFER_ENC)
825 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200826 if (h1m.flags & H1_MF_CLEN) {
827 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
828 if (h1m.body_len == 0)
829 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100830 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200831 if (h1m.flags & H1_MF_CHNK)
832 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100833
834 htx = htx_from_buf(buf);
835 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
836 if (!sl || !htx_add_all_headers(htx, hdrs))
837 goto error;
838 sl->info.res.status = h1sl.st.status;
839
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200840 while (raw.len > ret) {
841 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
842 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100843 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200844 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100845 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200846
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100847 if (!htx_add_endof(htx, HTX_BLK_EOM))
848 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200849
Christopher Faulet90cc4812019-07-22 16:49:30 +0200850 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100851
852error:
853 if (buf->size)
854 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200855 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100856}
857
858static int http_htx_init(void)
859{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100860 struct buffer chk;
861 struct ist raw;
862 int rc;
863 int err_code = 0;
864
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100865 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
866 if (!http_err_msgs[rc]) {
867 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
868 err_code |= ERR_ALERT | ERR_FATAL;
869 continue;
870 }
871
872 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
873 if (!http_str_to_htx(&chk, raw)) {
874 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
875 http_err_codes[rc]);
876 err_code |= ERR_ALERT | ERR_FATAL;
877 }
Christopher Fauletf7346382019-07-17 22:02:08 +0200878 http_err_chunks[rc] = chk;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100879 }
880end:
881 return err_code;
882}
883
Christopher Faulet58857752020-01-15 15:19:50 +0100884static void http_htx_deinit(void)
885{
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100886 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +0100887 struct ebpt_node *node, *next;
888 struct http_error *http_err;
889
890 node = ebpt_first(&http_error_messages);
891 while (node) {
892 next = ebpt_next(node);
893 ebpt_delete(node);
894 http_err = container_of(node, typeof(*http_err), node);
895 chunk_destroy(&http_err->msg);
896 free(node->key);
897 free(http_err);
898 node = next;
899 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100900
901 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
902 free(http_errs->conf.file);
903 free(http_errs->id);
904 LIST_DEL(&http_errs->list);
905 free(http_errs);
906 }
Christopher Faulet58857752020-01-15 15:19:50 +0100907}
908
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100909REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +0100910REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +0100911
Christopher Faulet58857752020-01-15 15:19:50 +0100912/* Reads content of the error file <file> and convert it into an HTX message. On
913 * success, the HTX message is returned. On error, NULL is returned and an error
914 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +0100915 */
Christopher Faulet58857752020-01-15 15:19:50 +0100916struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +0100917{
Christopher Faulet58857752020-01-15 15:19:50 +0100918 struct buffer *buf = NULL;
919 struct buffer chk;
920 struct ebpt_node *node;
921 struct http_error *http_err;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100922 struct stat stat;
923 char *err = NULL;
924 int errnum, errlen;
925 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +0100926
927 /* already loaded */
928 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
929 if (node) {
930 http_err = container_of(node, typeof(*http_err), node);
931 buf = &http_err->msg;
932 goto out;
933 }
Christopher Faulet5031ef52020-01-15 11:22:07 +0100934
Christopher Faulet58857752020-01-15 15:19:50 +0100935 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +0100936 fd = open(file, O_RDONLY);
937 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
938 memprintf(errmsg, "error opening file '%s'.", file);
939 goto out;
940 }
941
942 if (stat.st_size <= global.tune.bufsize)
943 errlen = stat.st_size;
944 else {
945 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
946 file, global.tune.bufsize);
947 errlen = global.tune.bufsize;
948 }
949
950 err = malloc(errlen);
951 if (!err) {
952 memprintf(errmsg, "out of memory.");
953 goto out;
954 }
955
956 errnum = read(fd, err, errlen);
957 if (errnum != errlen) {
958 memprintf(errmsg, "error reading file '%s'.", file);
959 goto out;
960 }
961
Christopher Faulet58857752020-01-15 15:19:50 +0100962 /* Create the node corresponding to the error file */
963 http_err = calloc(1, sizeof(*http_err));
964 if (!http_err) {
965 memprintf(errmsg, "out of memory.");
966 goto out;
967 }
968 http_err->node.key = strdup(file);
969 if (!http_err->node.key) {
970 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +0100971 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +0100972 goto out;
973 }
974
975 /* Convert the error file into an HTX message */
976 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +0100977 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Faulet58857752020-01-15 15:19:50 +0100978 free(http_err->node.key);
979 free(http_err);
Christopher Faulet5031ef52020-01-15 11:22:07 +0100980 goto out;
981 }
982
Christopher Faulet58857752020-01-15 15:19:50 +0100983 /* Insert the node in the tree and return the HTX message */
984 http_err->msg = chk;
985 ebis_insert(&http_error_messages, &http_err->node);
986 buf = &http_err->msg;
987
Christopher Faulet5031ef52020-01-15 11:22:07 +0100988 out:
989 if (fd >= 0)
990 close(fd);
991 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +0100992 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100993}
994
Christopher Faulet58857752020-01-15 15:19:50 +0100995/* Convert the raw http message <msg> into an HTX message. On sucess, the HTX
996 * message is returned. On error, NULL is returned and an error message is
997 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +0100998 */
Christopher Faulet58857752020-01-15 15:19:50 +0100999struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001000{
Christopher Faulet58857752020-01-15 15:19:50 +01001001 struct buffer *buf = NULL;
1002 struct buffer chk;
1003 struct ebpt_node *node;
1004 struct http_error *http_err;
1005
1006 /* already loaded */
1007 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1008 if (node) {
1009 http_err = container_of(node, typeof(*http_err), node);
1010 buf = &http_err->msg;
1011 goto out;
1012 }
1013 /* Create the node corresponding to the error file */
1014 http_err = calloc(1, sizeof(*http_err));
1015 if (!http_err) {
1016 memprintf(errmsg, "out of memory.");
1017 goto out;
1018 }
1019 http_err->node.key = strdup(key);
1020 if (!http_err->node.key) {
1021 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001022 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +01001023 goto out;
1024 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001025
1026 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001027 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001028 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Faulet58857752020-01-15 15:19:50 +01001029 free(http_err->node.key);
1030 free(http_err);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001031 goto out;
1032 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001033
Christopher Faulet58857752020-01-15 15:19:50 +01001034 /* Insert the node in the tree and return the HTX message */
1035 http_err->msg = chk;
1036 ebis_insert(&http_error_messages, &http_err->node);
1037 buf = &http_err->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001038 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001039 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001040}
1041
Christopher Faulet5031ef52020-01-15 11:22:07 +01001042/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001043 * <status>. It returns NULL if there is any error, otherwise it return the
1044 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001045 */
Christopher Faulet58857752020-01-15 15:19:50 +01001046struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001047{
Christopher Faulet58857752020-01-15 15:19:50 +01001048 struct buffer *buf = NULL;
1049 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001050
1051 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1052 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001053 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001054 break;
1055 }
1056 }
1057
1058 if (rc >= HTTP_ERR_SIZE)
1059 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001060 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001061}
1062
1063/* This function creates HTX error message corresponding to a redirect message
1064 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001065 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1066 * returns NULL if there is any error, otherwise it return the corresponding HTX
1067 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001068 */
Christopher Faulet58857752020-01-15 15:19:50 +01001069struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001070{
Christopher Faulet58857752020-01-15 15:19:50 +01001071 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001072 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001073 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001074 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001075
1076 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1077 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001078 /* Create the error key */
1079 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1080 memprintf(errmsg, "out of memory.");
1081 goto out;
1082 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001083 /* Create the error message */
1084 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1085 errlen = strlen(msg) + strlen(url) + 5;
1086 err = malloc(errlen);
1087 if (!err) {
1088 memprintf(errmsg, "out of memory.");
1089 goto out;
1090 }
1091 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1092
1093 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001094 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001095 break;
1096 }
1097 }
1098
1099 if (rc >= HTTP_ERR_SIZE)
1100 memprintf(errmsg, "status code '%d' not handled.", status);
1101out:
Christopher Faulet58857752020-01-15 15:19:50 +01001102 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001103 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001104 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001105}
1106
Christopher Faulet07f41f72020-01-16 16:16:06 +01001107/* Parses the "errorloc[302|303]" proxy keyword */
1108static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1109 struct proxy *defpx, const char *file, int line,
1110 char **errmsg)
1111{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001112 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001113 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001114 int errloc, status;
1115 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001116
1117 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1118 ret = 1;
1119 goto out;
1120 }
1121
1122 if (*(args[1]) == 0 || *(args[2]) == 0) {
1123 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1124 ret = -1;
1125 goto out;
1126 }
1127
1128 status = atol(args[1]);
1129 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1130 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1131 if (!msg) {
1132 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1133 ret = -1;
1134 goto out;
1135 }
1136
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001137 conf_err = calloc(1, sizeof(*conf_err));
1138 if (!conf_err) {
1139 memprintf(errmsg, "%s : out of memory.", args[0]);
1140 ret = -1;
1141 goto out;
1142 }
1143 conf_err->type = 1;
1144 conf_err->info.errorfile.status = status;
1145 conf_err->info.errorfile.msg = msg;
1146 conf_err->file = strdup(file);
1147 conf_err->line = line;
1148 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001149
1150 out:
1151 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001152
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001153}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001154
1155/* Parses the "errorfile" proxy keyword */
1156static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1157 struct proxy *defpx, const char *file, int line,
1158 char **errmsg)
1159{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001160 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001161 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001162 int status;
1163 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001164
1165 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1166 ret = 1;
1167 goto out;
1168 }
1169
1170 if (*(args[1]) == 0 || *(args[2]) == 0) {
1171 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1172 ret = -1;
1173 goto out;
1174 }
1175
1176 status = atol(args[1]);
1177 msg = http_parse_errorfile(status, args[2], errmsg);
1178 if (!msg) {
1179 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1180 ret = -1;
1181 goto out;
1182 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001183
1184 conf_err = calloc(1, sizeof(*conf_err));
1185 if (!conf_err) {
1186 memprintf(errmsg, "%s : out of memory.", args[0]);
1187 ret = -1;
1188 goto out;
1189 }
1190 conf_err->type = 1;
1191 conf_err->info.errorfile.status = status;
1192 conf_err->info.errorfile.msg = msg;
1193 conf_err->file = strdup(file);
1194 conf_err->line = line;
1195 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1196
1197 out:
1198 return ret;
1199
1200}
1201
1202/* Parses the "errorfiles" proxy keyword */
1203static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1204 struct proxy *defpx, const char *file, int line,
1205 char **err)
1206{
1207 struct conf_errors *conf_err = NULL;
1208 char *name = NULL;
1209 int rc, ret = 0;
1210
1211 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1212 ret = 1;
1213 goto out;
1214 }
1215
1216 if (!*(args[1])) {
1217 memprintf(err, "%s : expects <name> as argument.", args[0]);
1218 ret = -1;
1219 goto out;
1220 }
1221
1222 name = strdup(args[1]);
1223 conf_err = calloc(1, sizeof(*conf_err));
1224 if (!name || !conf_err) {
1225 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001226 goto error;
1227 }
1228 conf_err->type = 0;
1229
1230 conf_err->info.errorfiles.name = name;
1231 if (!*(args[2])) {
1232 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1233 conf_err->info.errorfiles.status[rc] = 1;
1234 }
1235 else {
1236 int cur_arg, status;
1237 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1238 status = atol(args[cur_arg]);
1239
1240 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1241 if (http_err_codes[rc] == status) {
1242 conf_err->info.errorfiles.status[rc] = 2;
1243 break;
1244 }
1245 }
1246 if (rc >= HTTP_ERR_SIZE) {
1247 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001248 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001249 }
1250 }
1251 }
1252 conf_err->file = strdup(file);
1253 conf_err->line = line;
1254 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1255 out:
1256 return ret;
1257
1258 error:
1259 free(name);
1260 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001261 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001262 goto out;
1263}
1264
1265/* Check "errorfiles" proxy keyword */
1266static int proxy_check_errors(struct proxy *px)
1267{
1268 struct conf_errors *conf_err, *conf_err_back;
1269 struct http_errors *http_errs;
1270 int rc, err = 0;
1271
1272 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1273 if (conf_err->type == 1) {
1274 /* errorfile */
1275 rc = http_get_status_idx(conf_err->info.errorfile.status);
1276 px->errmsg[rc] = conf_err->info.errorfile.msg;
1277 }
1278 else {
1279 /* errorfiles */
1280 list_for_each_entry(http_errs, &http_errors_list, list) {
1281 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1282 break;
1283 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001284
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001285 /* unknown http-errors section */
1286 if (&http_errs->list == &http_errors_list) {
1287 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1288 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1289 err |= ERR_ALERT | ERR_FATAL;
1290 free(conf_err->info.errorfiles.name);
1291 goto next;
1292 }
1293
1294 free(conf_err->info.errorfiles.name);
1295 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1296 if (conf_err->info.errorfiles.status[rc] > 0) {
1297 if (http_errs->errmsg[rc])
1298 px->errmsg[rc] = http_errs->errmsg[rc];
1299 else if (conf_err->info.errorfiles.status[rc] == 2)
1300 ha_warning("config: proxy '%s' : status '%d' not declared in"
1301 " http-errors section '%s' (at %s:%d).\n",
1302 px->id, http_err_codes[rc], http_errs->id,
1303 conf_err->file, conf_err->line);
1304 }
1305 }
1306 }
1307 next:
1308 LIST_DEL(&conf_err->list);
1309 free(conf_err->file);
1310 free(conf_err);
1311 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001312
1313 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001314 return err;
1315}
1316
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001317static int post_check_errors()
1318{
1319 struct ebpt_node *node;
1320 struct http_error *http_err;
1321 struct htx *htx;
1322 int err_code = 0;
1323
1324 node = ebpt_first(&http_error_messages);
1325 while (node) {
1326 http_err = container_of(node, typeof(*http_err), node);
1327 if (b_is_null(&http_err->msg))
1328 goto next;
1329 htx = htxbuf(&http_err->msg);
1330 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1331 ha_warning("config: errorfile '%s' runs over the buffer space"
1332 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001333 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001334 (char *)node->key);
1335 err_code |= ERR_WARN;
1336 }
1337 next:
1338 node = ebpt_next(node);
1339 }
1340
1341 return err_code;
1342}
1343
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001344int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1345{
1346 struct conf_errors *conf_err, *new_conf_err = NULL;
1347 int ret = 0;
1348
1349 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1350 new_conf_err = calloc(1, sizeof(*new_conf_err));
1351 if (!new_conf_err) {
1352 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1353 goto out;
1354 }
1355 new_conf_err->type = conf_err->type;
1356 if (conf_err->type == 1) {
1357 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1358 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1359 }
1360 else {
1361 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1362 if (!new_conf_err->info.errorfiles.name) {
1363 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1364 goto out;
1365 }
1366 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1367 sizeof(conf_err->info.errorfiles.status));
1368 }
1369 new_conf_err->file = strdup(conf_err->file);
1370 new_conf_err->line = conf_err->line;
1371 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1372 new_conf_err = NULL;
1373 }
1374 ret = 1;
1375
1376 out:
1377 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001378 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001379}
1380
1381void proxy_release_conf_errors(struct proxy *px)
1382{
1383 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001384
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001385 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1386 if (conf_err->type == 0)
1387 free(conf_err->info.errorfiles.name);
1388 LIST_DEL(&conf_err->list);
1389 free(conf_err->file);
1390 free(conf_err);
1391 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001392}
1393
1394/*
1395 * Parse an <http-errors> section.
1396 * Returns the error code, 0 if OK, or any combination of :
1397 * - ERR_ABORT: must abort ASAP
1398 * - ERR_FATAL: we can continue parsing but not start the service
1399 * - ERR_WARN: a warning has been emitted
1400 * - ERR_ALERT: an alert has been emitted
1401 * Only the two first ones can stop processing, the two others are just
1402 * indicators.
1403 */
1404static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1405{
1406 static struct http_errors *curr_errs = NULL;
1407 int err_code = 0;
1408 const char *err;
1409 char *errmsg = NULL;
1410
1411 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1412 if (!*args[1]) {
1413 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1414 err_code |= ERR_ALERT | ERR_ABORT;
1415 goto out;
1416 }
1417
1418 err = invalid_char(args[1]);
1419 if (err) {
1420 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1421 file, linenum, *err, args[0], args[1]);
1422 err_code |= ERR_ALERT | ERR_FATAL;
1423 }
1424
1425 list_for_each_entry(curr_errs, &http_errors_list, list) {
1426 /* Error if two errors section owns the same name */
1427 if (strcmp(curr_errs->id, args[1]) == 0) {
1428 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1429 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1430 err_code |= ERR_ALERT | ERR_FATAL;
1431 }
1432 }
1433
1434 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1435 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1436 err_code |= ERR_ALERT | ERR_ABORT;
1437 goto out;
1438 }
1439
1440 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1441 curr_errs->id = strdup(args[1]);
1442 curr_errs->conf.file = strdup(file);
1443 curr_errs->conf.line = linenum;
1444 }
1445 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1446 struct buffer *msg;
1447 int status, rc;
1448
1449 if (*(args[1]) == 0 || *(args[2]) == 0) {
1450 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1451 file, linenum, args[0]);
1452 err_code |= ERR_ALERT | ERR_FATAL;
1453 goto out;
1454 }
1455
1456 status = atol(args[1]);
1457 msg = http_parse_errorfile(status, args[2], &errmsg);
1458 if (!msg) {
1459 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1460 err_code |= ERR_ALERT | ERR_FATAL;
1461 goto out;
1462 }
1463 rc = http_get_status_idx(status);
1464 curr_errs->errmsg[rc] = msg;
1465 }
1466 else if (*args[0] != 0) {
1467 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1468 err_code |= ERR_ALERT | ERR_FATAL;
1469 goto out;
1470 }
1471
1472out:
1473 free(errmsg);
1474 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001475}
1476
1477static struct cfg_kw_list cfg_kws = {ILH, {
1478 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1479 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1480 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1481 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001482 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01001483 { 0, NULL, NULL },
1484}};
1485
1486INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001487REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001488REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001489
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001490REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1491
Christopher Faulet29f72842019-12-11 15:52:32 +01001492/************************************************************************/
1493/* HTX sample fetches */
1494/************************************************************************/
1495
1496/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1497static int
1498smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1499{
1500 if (!smp->strm)
1501 return 0;
1502
1503 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1504 smp->data.type = SMP_T_BOOL;
1505 return 1;
1506}
1507
1508/* Returns the number of blocks in an HTX message. The channel is chosen
1509 * depending on the sample direction. */
1510static int
1511smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1512{
1513 struct channel *chn;
1514 struct htx *htx;
1515
1516 if (!smp->strm)
1517 return 0;
1518
1519 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1520 htx = smp_prefetch_htx(smp, chn, 0);
1521 if (!htx)
1522 return 0;
1523
1524 smp->data.u.sint = htx_nbblks(htx);
1525 smp->data.type = SMP_T_SINT;
1526 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1527 return 1;
1528}
1529
1530/* Returns the size of an HTX message. The channel is chosen depending on the
1531 * sample direction. */
1532static int
1533smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1534{
1535 struct channel *chn;
1536 struct htx *htx;
1537
1538 if (!smp->strm)
1539 return 0;
1540
1541 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1542 htx = smp_prefetch_htx(smp, chn, 0);
1543 if (!htx)
1544 return 0;
1545
1546 smp->data.u.sint = htx->size;
1547 smp->data.type = SMP_T_SINT;
1548 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1549 return 1;
1550}
1551
1552/* Returns the data size of an HTX message. The channel is chosen depending on the
1553 * sample direction. */
1554static int
1555smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1556{
1557 struct channel *chn;
1558 struct htx *htx;
1559
1560 if (!smp->strm)
1561 return 0;
1562
1563 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1564 htx = smp_prefetch_htx(smp, chn, 0);
1565 if (!htx)
1566 return 0;
1567
1568 smp->data.u.sint = htx->data;
1569 smp->data.type = SMP_T_SINT;
1570 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1571 return 1;
1572}
1573
1574/* Returns the used space (data+meta) of an HTX message. The channel is chosen
1575 * depending on the sample direction. */
1576static int
1577smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1578{
1579 struct channel *chn;
1580 struct htx *htx;
1581
1582 if (!smp->strm)
1583 return 0;
1584
1585 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1586 htx = smp_prefetch_htx(smp, chn, 0);
1587 if (!htx)
1588 return 0;
1589
1590 smp->data.u.sint = htx_used_space(htx);
1591 smp->data.type = SMP_T_SINT;
1592 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1593 return 1;
1594}
1595
1596/* Returns the free space (size-used) of an HTX message. The channel is chosen
1597 * depending on the sample direction. */
1598static int
1599smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1600{
1601 struct channel *chn;
1602 struct htx *htx;
1603
1604 if (!smp->strm)
1605 return 0;
1606
1607 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1608 htx = smp_prefetch_htx(smp, chn, 0);
1609 if (!htx)
1610 return 0;
1611
1612 smp->data.u.sint = htx_free_space(htx);
1613 smp->data.type = SMP_T_SINT;
1614 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1615 return 1;
1616}
1617
1618/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
1619 * channel is chosen depending on the sample direction. */
1620static int
1621smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1622{
1623 struct channel *chn;
1624 struct htx *htx;
1625
1626 if (!smp->strm)
1627 return 0;
1628
1629 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1630 htx = smp_prefetch_htx(smp, chn, 0);
1631 if (!htx)
1632 return 0;
1633
1634 smp->data.u.sint = htx_free_data_space(htx);
1635 smp->data.type = SMP_T_SINT;
1636 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1637 return 1;
1638}
1639
1640/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
1641 * 0. Concretely, it only checks the tail. The channel is chosen depending on
1642 * the sample direction. */
1643static int
1644smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1645{
1646 struct channel *chn;
1647 struct htx *htx;
1648
1649 if (!smp->strm)
1650 return 0;
1651
1652 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1653 htx = smp_prefetch_htx(smp, chn, 0);
1654 if (!htx)
1655 return 0;
1656
1657 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
1658 smp->data.type = SMP_T_BOOL;
1659 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1660 return 1;
1661}
1662
1663/* Returns the type of a specific HTX block, if found in the message. Otherwise
1664 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
1665 * "head", "tail" or "first". The channel is chosen depending on the sample
1666 * direction. */
1667static int
1668smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1669{
1670 struct channel *chn;
1671 struct htx *htx;
1672 enum htx_blk_type type;
1673 int32_t pos;
1674
1675 if (!smp->strm || !arg_p)
1676 return 0;
1677
1678 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1679 htx = smp_prefetch_htx(smp, chn, 0);
1680 if (!htx)
1681 return 0;
1682
1683 pos = arg_p[0].data.sint;
1684 if (pos == -1)
1685 type = htx_get_head_type(htx);
1686 else if (pos == -2)
1687 type = htx_get_tail_type(htx);
1688 else if (pos == -3)
1689 type = htx_get_first_type(htx);
1690 else
1691 type = ((pos >= htx->head && pos <= htx->tail)
1692 ? htx_get_blk_type(htx_get_blk(htx, pos))
1693 : HTX_BLK_UNUSED);
1694
1695 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
1696 smp->data.type = SMP_T_STR;
1697 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1698 return 1;
1699}
1700
1701/* Returns the size of a specific HTX block, if found in the message. Otherwise
1702 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
1703 * "first". The channel is chosen depending on the sample direction. */
1704static int
1705smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1706{
1707 struct channel *chn;
1708 struct htx *htx;
1709 struct htx_blk *blk;
1710 int32_t pos;
1711
1712 if (!smp->strm || !arg_p)
1713 return 0;
1714
1715 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1716 htx = smp_prefetch_htx(smp, chn, 0);
1717 if (!htx)
1718 return 0;
1719
1720 pos = arg_p[0].data.sint;
1721 if (pos == -1)
1722 blk = htx_get_head_blk(htx);
1723 else if (pos == -2)
1724 blk = htx_get_tail_blk(htx);
1725 else if (pos == -3)
1726 blk = htx_get_first_blk(htx);
1727 else
1728 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1729
1730 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
1731 smp->data.type = SMP_T_SINT;
1732 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1733 return 1;
1734}
1735
1736/* Returns the start-line if the selected HTX block exists and is a
1737 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
1738 * supported or "head", "tail" or "first". The channel is chosen depending on
1739 * the sample direction. */
1740static int
1741smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1742{
1743 struct buffer *temp;
1744 struct channel *chn;
1745 struct htx *htx;
1746 struct htx_blk *blk;
1747 struct htx_sl *sl;
1748 int32_t pos;
1749
1750 if (!smp->strm || !arg_p)
1751 return 0;
1752
1753 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1754 htx = smp_prefetch_htx(smp, chn, 0);
1755 if (!htx)
1756 return 0;
1757
1758 pos = arg_p[0].data.sint;
1759 if (pos == -1)
1760 blk = htx_get_head_blk(htx);
1761 else if (pos == -2)
1762 blk = htx_get_tail_blk(htx);
1763 else if (pos == -3)
1764 blk = htx_get_first_blk(htx);
1765 else
1766 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1767
1768 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
1769 smp->data.u.str.size = 0;
1770 smp->data.u.str.area = "";
1771 smp->data.u.str.data = 0;
1772 }
1773 else {
1774 sl = htx_get_blk_ptr(htx, blk);
1775
1776 temp = get_trash_chunk();
1777 chunk_istcat(temp, htx_sl_p1(sl));
1778 temp->area[temp->data++] = ' ';
1779 chunk_istcat(temp, htx_sl_p2(sl));
1780 temp->area[temp->data++] = ' ';
1781 chunk_istcat(temp, htx_sl_p3(sl));
1782
1783 smp->data.u.str = *temp;
1784 }
1785
1786 smp->data.type = SMP_T_STR;
1787 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1788 return 1;
1789}
1790
1791/* Returns the header name if the selected HTX block exists and is a header or a
1792 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1793 * supported or "head", "tail" or "first". The channel is chosen depending on
1794 * the sample direction. */
1795static int
1796smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1797{
1798 struct channel *chn;
1799 struct htx *htx;
1800 struct htx_blk *blk;
1801 int32_t pos;
1802
1803 if (!smp->strm || !arg_p)
1804 return 0;
1805
1806 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1807 htx = smp_prefetch_htx(smp, chn, 0);
1808 if (!htx)
1809 return 0;
1810
1811 pos = arg_p[0].data.sint;
1812 if (pos == -1)
1813 blk = htx_get_head_blk(htx);
1814 else if (pos == -2)
1815 blk = htx_get_tail_blk(htx);
1816 else if (pos == -3)
1817 blk = htx_get_first_blk(htx);
1818 else
1819 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1820
1821 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1822 smp->data.u.str.size = 0;
1823 smp->data.u.str.area = "";
1824 smp->data.u.str.data = 0;
1825 }
1826 else {
1827 struct ist name = htx_get_blk_name(htx, blk);
1828
1829 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
1830 }
1831 smp->data.type = SMP_T_STR;
1832 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1833 return 1;
1834}
1835
1836/* Returns the header value if the selected HTX block exists and is a header or
1837 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1838 * supported or "head", "tail" or "first". The channel is chosen depending on
1839 * the sample direction. */
1840static int
1841smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1842{
1843 struct channel *chn;
1844 struct htx *htx;
1845 struct htx_blk *blk;
1846 int32_t pos;
1847
1848 if (!smp->strm || !arg_p)
1849 return 0;
1850
1851 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1852 htx = smp_prefetch_htx(smp, chn, 0);
1853 if (!htx)
1854 return 0;
1855
1856 pos = arg_p[0].data.sint;
1857 if (pos == -1)
1858 blk = htx_get_head_blk(htx);
1859 else if (pos == -2)
1860 blk = htx_get_tail_blk(htx);
1861 else if (pos == -3)
1862 blk = htx_get_first_blk(htx);
1863 else
1864 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1865
1866 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1867 smp->data.u.str.size = 0;
1868 smp->data.u.str.area = "";
1869 smp->data.u.str.data = 0;
1870 }
1871 else {
1872 struct ist val = htx_get_blk_value(htx, blk);
1873
1874 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1875 }
1876 smp->data.type = SMP_T_STR;
1877 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1878 return 1;
1879}
1880
1881/* Returns the value if the selected HTX block exists and is a data
1882 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
1883 * or "head", "tail" or "first". The channel is chosen depending on the sample
1884 * direction. */
1885static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01001886smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01001887{
1888 struct channel *chn;
1889 struct htx *htx;
1890 struct htx_blk *blk;
1891 int32_t pos;
1892
1893 if (!smp->strm || !arg_p)
1894 return 0;
1895
1896 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1897 htx = smp_prefetch_htx(smp, chn, 0);
1898 if (!htx)
1899 return 0;
1900
1901 pos = arg_p[0].data.sint;
1902 if (pos == -1)
1903 blk = htx_get_head_blk(htx);
1904 else if (pos == -2)
1905 blk = htx_get_tail_blk(htx);
1906 else if (pos == -3)
1907 blk = htx_get_first_blk(htx);
1908 else
1909 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1910
1911 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
1912 smp->data.u.str.size = 0;
1913 smp->data.u.str.area = "";
1914 smp->data.u.str.data = 0;
1915 }
1916 else {
1917 struct ist val = htx_get_blk_value(htx, blk);
1918
1919 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1920 }
Christopher Faulet8178e402020-01-08 14:38:58 +01001921 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01001922 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1923 return 1;
1924}
1925
1926/* This function is used to validate the arguments passed to any "htx_blk" fetch
1927 * keywords. An argument is expected by these keywords. It must be a positive
1928 * integer or on of the following strings: "head", "tail" or "first". It returns
1929 * 0 on error, and a non-zero value if OK.
1930 */
1931int val_blk_arg(struct arg *arg, char **err_msg)
1932{
1933 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
1934 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
1935 return 0;
1936 }
1937 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
1938 free(arg[0].data.str.area);
1939 arg[0].type = ARGT_SINT;
1940 arg[0].data.sint = -1;
1941 }
1942 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
1943 free(arg[0].data.str.area);
1944 arg[0].type = ARGT_SINT;
1945 arg[0].data.sint = -2;
1946 }
1947 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
1948 free(arg[0].data.str.area);
1949 arg[0].type = ARGT_SINT;
1950 arg[0].data.sint = -3;
1951 }
1952 else {
1953 int pos;
1954
1955 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001956 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01001957 memprintf(err_msg, "invalid block position");
1958 return 0;
1959 }
1960 }
1961
1962 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
1963 if (pos < 0) {
1964 memprintf(err_msg, "block position must not be negative");
1965 return 0;
1966 }
1967 free(arg[0].data.str.area);
1968 arg[0].type = ARGT_SINT;
1969 arg[0].data.sint = pos;
1970 }
1971
1972 return 1;
1973}
1974
1975
1976/* Note: must not be declared <const> as its list will be overwritten.
1977 * Note: htx sample fetches should only used for developpement purpose.
1978 */
1979static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01001980 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01001981
Christopher Faulet01f44452020-01-08 14:23:40 +01001982 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1983 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1984 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1985 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1986 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1987 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1988 { "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 +01001989
Christopher Faulet01f44452020-01-08 14:23:40 +01001990 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1991 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1992 { "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},
1993 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1994 { "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 +01001995 { "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 +01001996
1997 { /* END */ },
1998}};
1999
2000INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);