blob: cffbbffc9594c0c557168f6748777e4a2c1480c8 [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 Faulet297fbb42019-05-13 14:41:27 +020056/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020057 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020058 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020059 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020060struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020061{
Christopher Faulet297fbb42019-05-13 14:41:27 +020062 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010063
Christopher Faulet29f17582019-05-23 11:03:26 +020064 BUG_ON(htx->first == -1);
65 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020066 if (!blk)
67 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020068 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 +020069 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020070}
71
Christopher Faulet727a3f12020-02-07 16:39:41 +010072/* Returns the headers size in the HTX message */
73size_t http_get_hdrs_size(struct htx *htx)
74{
75 struct htx_blk *blk;
76 size_t sz = 0;
77
78 blk = htx_get_first_blk(htx);
79 if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH)
80 return sz;
81
82 for (; blk; blk = htx_get_next_blk(htx, blk)) {
83 sz += htx_get_blksz(blk);
84 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
85 break;
86 }
87 return sz;
88}
89
Christopher Faulet47596d32018-10-22 09:17:28 +020090/* Finds the first or next occurrence of header <name> in the HTX message <htx>
91 * using the context <ctx>. This structure holds everything necessary to use the
92 * header and find next occurrence. If its <blk> member is NULL, the header is
93 * searched from the beginning. Otherwise, the next occurrence is returned. The
94 * function returns 1 when it finds a value, and 0 when there is no more. It is
95 * designed to work with headers defined as comma-separated lists. If <full> is
96 * set, it works on full-line headers in whose comma is not a delimiter but is
97 * part of the syntax. A special case, if ctx->value is NULL when searching for
98 * a new values of a header, the current header is rescanned. This allows
99 * rescanning after a header deletion.
100 */
101int http_find_header(const struct htx *htx, const struct ist name,
102 struct http_hdr_ctx *ctx, int full)
103{
104 struct htx_blk *blk = ctx->blk;
105 struct ist n, v;
106 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +0200107
108 if (blk) {
109 char *p;
110
Tim Duesterhused526372020-03-05 17:56:33 +0100111 if (!isttest(ctx->value))
Christopher Faulet47596d32018-10-22 09:17:28 +0200112 goto rescan_hdr;
113 if (full)
114 goto next_blk;
115 v = htx_get_blk_value(htx, blk);
116 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
117 v.len -= (p - v.ptr);
118 v.ptr = p;
119 if (!v.len)
120 goto next_blk;
121 /* Skip comma */
122 if (*(v.ptr) == ',') {
123 v.ptr++;
124 v.len--;
125 }
126
127 goto return_hdr;
128 }
129
Christopher Faulet192c6a22019-06-11 16:32:24 +0200130 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +0200131 return 0;
132
Christopher Fauleta3f15502019-05-13 15:27:23 +0200133 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200134 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +0200135 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100136 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
137 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200138 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200139 continue;
Christopher Faulet47596d32018-10-22 09:17:28 +0200140 if (name.len) {
141 /* If no name was passed, we want any header. So skip the comparison */
142 n = htx_get_blk_name(htx, blk);
143 if (!isteqi(n, name))
144 goto next_blk;
145 }
146 v = htx_get_blk_value(htx, blk);
147
148 return_hdr:
149 ctx->lws_before = 0;
150 ctx->lws_after = 0;
151 while (v.len && HTTP_IS_LWS(*v.ptr)) {
152 v.ptr++;
153 v.len--;
154 ctx->lws_before++;
155 }
156 if (!full)
157 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
158 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
159 v.len--;
160 ctx->lws_after++;
161 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200162 ctx->blk = blk;
163 ctx->value = v;
164 return 1;
165
166 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200167 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200168 }
169
170 ctx->blk = NULL;
171 ctx->value = ist("");
172 ctx->lws_before = ctx->lws_after = 0;
173 return 0;
174}
175
176/* Adds a header block int the HTX message <htx>, just before the EOH block. It
177 * returns 1 on success, otherwise it returns 0.
178 */
179int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
180{
181 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200182 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200183 enum htx_blk_type type = htx_get_tail_type(htx);
184 int32_t prev;
185
186 blk = htx_add_header(htx, n, v);
187 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200188 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200189
190 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200191 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200192
193 /* <blk> is the head, swap it iteratively with its predecessor to place
194 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200195 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200196 struct htx_blk *pblk = htx_get_blk(htx, prev);
197 enum htx_blk_type type = htx_get_blk_type(pblk);
198
199 /* Swap .addr and .info fields */
200 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
201 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
202
203 if (blk->addr == pblk->addr)
204 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200205
206 /* Stop when end-of-header is reached */
207 if (type == HTX_BLK_EOH)
208 break;
209
210 blk = pblk;
211 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200212
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200213 end:
214 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100215 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200216 if (!http_update_authority(htx, sl, v))
217 goto fail;
218 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200219 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200220
221 fail:
222 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200223}
224
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100225/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200226 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200227 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100228int 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 +0200229{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200230 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200231
Christopher Faulet29f17582019-05-23 11:03:26 +0200232 blk = htx_get_first_blk(htx);
233 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200234 return 0;
235 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200236}
237
Christopher Faulete010c802018-10-24 10:36:45 +0200238/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
239 * on success, otherwise 0.
240 */
241int http_replace_req_meth(struct htx *htx, const struct ist meth)
242{
243 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200244 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100245 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200246
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100247 if (!sl)
248 return 0;
249
Christopher Faulete010c802018-10-24 10:36:45 +0200250 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100251 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
252 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200253
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100254 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
255 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200256
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100257 /* create the new start line */
258 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
259 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200260}
261
262/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
263 * success, otherwise 0.
264 */
265int http_replace_req_uri(struct htx *htx, const struct ist uri)
266{
267 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200268 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100269 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200270
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100271 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200272 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100273
Christopher Faulete010c802018-10-24 10:36:45 +0200274 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100275 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
276 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200277
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100278 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
279 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200280
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100281 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200282 if (!http_replace_stline(htx, meth, uri, vsn))
283 goto fail;
284
285 sl = http_get_stline(htx);
286 if (!http_update_host(htx, sl, uri))
287 goto fail;
288
289 return 1;
290 fail:
291 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200292}
293
294/* Replace the request path in the HTX message <htx> by <path>. The host part
295 * and the query string are preserved. It returns 1 on success, otherwise 0.
296 */
297int http_replace_req_path(struct htx *htx, const struct ist path)
298{
299 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200300 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100301 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200302 size_t plen = 0;
303
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100304 if (!sl)
305 return 0;
306
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100307 uri = htx_sl_req_uri(sl);
308 p = http_get_path(uri);
Tim Duesterhused526372020-03-05 17:56:33 +0100309 if (!isttest(p))
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100310 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200311 while (plen < p.len && *(p.ptr + plen) != '?')
312 plen++;
313
314 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100315 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
316 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200317
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100318 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
319 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
320
321 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200322 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
323 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100324 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200325
326 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100327 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200328}
329
330/* Replace the request query-string in the HTX message <htx> by <query>. The
331 * host part and the path are preserved. It returns 1 on success, otherwise
332 * 0.
333 */
334int http_replace_req_query(struct htx *htx, const struct ist query)
335{
336 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200337 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100338 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200339 int offset = 1;
340
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100341 if (!sl)
342 return 0;
343
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100344 uri = htx_sl_req_uri(sl);
345 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200346 while (q.len > 0 && *(q.ptr) != '?') {
347 q.ptr++;
348 q.len--;
349 }
350
351 /* skip the question mark or indicate that we must insert it
352 * (but only if the format string is not empty then).
353 */
354 if (q.len) {
355 q.ptr++;
356 q.len--;
357 }
358 else if (query.len > 1)
359 offset = 0;
360
361 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
363 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200364
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100365 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
366 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200367
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100368 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
369 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
370 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200371
372 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100373 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200374}
375
376/* Replace the response status in the HTX message <htx> by <status>. It returns
377 * 1 on success, otherwise 0.
378*/
379int http_replace_res_status(struct htx *htx, const struct ist status)
380{
381 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200382 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200384
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100385 if (!sl)
386 return 0;
387
Christopher Faulete010c802018-10-24 10:36:45 +0200388 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100389 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
390 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200391
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100392 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
393 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200394
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100395 /* create the new start line */
396 sl->info.res.status = strl2ui(status.ptr, status.len);
397 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200398}
399
400/* Replace the response reason in the HTX message <htx> by <reason>. It returns
401 * 1 on success, otherwise 0.
402*/
403int http_replace_res_reason(struct htx *htx, const struct ist reason)
404{
405 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200406 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100407 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200408
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100409 if (!sl)
410 return 0;
411
Christopher Faulete010c802018-10-24 10:36:45 +0200412 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100413 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
414 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200415
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100416 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
417 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200418
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100419 /* create the new start line */
420 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200421}
422
Christopher Faulet47596d32018-10-22 09:17:28 +0200423/* Replaces a part of a header value referenced in the context <ctx> by
424 * <data>. It returns 1 on success, otherwise it returns 0. The context is
425 * updated if necessary.
426 */
427int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
428{
429 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200430 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200431 char *start;
432 struct ist v;
433 uint32_t len, off;
434
435 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200436 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200437
438 v = htx_get_blk_value(htx, blk);
439 start = ctx->value.ptr - ctx->lws_before;
440 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
441 off = start - v.ptr;
442
443 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
444 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200445 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200446
447 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200448
449 sl = http_get_stline(htx);
450 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
451 struct ist n = htx_get_blk_name(htx, blk);
452
453 if (isteq(n, ist("host"))) {
454 if (!http_update_authority(htx, sl, v))
455 goto fail;
456 ctx->blk = NULL;
457 http_find_header(htx, ist("host"), ctx, 1);
458 blk = ctx->blk;
459 v = htx_get_blk_value(htx, blk);
460 }
461 }
462
Christopher Faulet47596d32018-10-22 09:17:28 +0200463 ctx->blk = blk;
464 ctx->value.ptr = v.ptr + off;
465 ctx->value.len = data.len;
466 ctx->lws_before = ctx->lws_after = 0;
467
468 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200469 fail:
470 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200471}
472
473/* Fully replaces a header referenced in the context <ctx> by the name <name>
474 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
475 * context is updated if necessary.
476 */
477int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
478 const struct ist name, const struct ist value)
479{
480 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200481 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200482
483 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200484 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200485
486 blk = htx_replace_header(htx, blk, name, value);
487 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200488 goto fail;
489
490 sl = http_get_stline(htx);
Christopher Faulet3e1f7f42020-02-28 09:47:07 +0100491 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) {
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200492 if (!http_update_authority(htx, sl, value))
493 goto fail;
494 ctx->blk = NULL;
495 http_find_header(htx, ist("host"), ctx, 1);
496 blk = ctx->blk;
497 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200498
499 ctx->blk = blk;
500 ctx->value = ist(NULL);
501 ctx->lws_before = ctx->lws_after = 0;
502
503 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200504 fail:
505 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200506}
507
508/* Remove one value of a header. This only works on a <ctx> returned by
509 * http_find_header function. The value is removed, as well as surrounding commas
510 * if any. If the removed value was alone, the whole header is removed. The
511 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
512 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
513 * form that can be handled by http_find_header() to find next occurrence.
514 */
515int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
516{
517 struct htx_blk *blk = ctx->blk;
518 char *start;
519 struct ist v;
520 uint32_t len;
521
522 if (!blk)
523 return 0;
524
525 start = ctx->value.ptr - ctx->lws_before;
526 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
527
528 v = htx_get_blk_value(htx, blk);
529 if (len == v.len) {
530 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200531 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200532 ctx->blk = blk;
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100533 ctx->value = IST_NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +0200534 ctx->lws_before = ctx->lws_after = 0;
535 }
536 else {
537 ctx->blk = htx_get_blk(htx, htx->tail);
538 ctx->value = htx_get_blk_value(htx, ctx->blk);
539 ctx->lws_before = ctx->lws_after = 0;
540 }
541 return 1;
542 }
543
544 /* This was not the only value of this header. We have to remove the
545 * part pointed by ctx->value. If it is the last entry of the list, we
546 * remove the last separator.
547 */
548 if (start == v.ptr) {
549 /* It's the first header part but not the only one. So remove
550 * the comma after it. */
551 len++;
552 }
553 else {
554 /* There is at least one header part before the removed one. So
555 * remove the comma between them. */
556 start--;
557 len++;
558 }
559 /* Update the block content and its len */
560 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200561 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200562
563 /* Finally update the ctx */
564 ctx->value.ptr = start;
565 ctx->value.len = 0;
566 ctx->lws_before = ctx->lws_after = 0;
567
568 return 1;
569}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200570
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200571/* Updates the authority part of the uri with the value <host>. It happens when
572 * the header host is modified. It returns 0 on failure and 1 on success. It is
573 * the caller responsibility to provide the start-line and to be sure the uri
574 * contains an authority. Thus, if no authority is found in the uri, an error is
575 * returned.
576 */
Christopher Faulet1543d442020-04-28 19:57:29 +0200577int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200578{
579 struct buffer *temp = get_trash_chunk();
580 struct ist meth, vsn, uri, authority;
581
582 uri = htx_sl_req_uri(sl);
583 authority = http_get_authority(uri, 1);
Christopher Faulet34b18e42020-02-18 11:02:21 +0100584 if (!authority.len)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200585 return 0;
586
Christopher Faulet34b18e42020-02-18 11:02:21 +0100587 /* Don't update the uri if there is no change */
588 if (isteq(host, authority))
589 return 1;
590
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200591 /* Start by copying old method and version */
592 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
593 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
594
595 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
596 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
597
598 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
599 chunk_memcat(temp, host.ptr, host.len);
600 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
601 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
602
603 return http_replace_stline(htx, meth, uri, vsn);
604
605}
606
607/* Update the header host by extracting the authority of the uri <uri>. flags of
608 * the start-line are also updated accordingly. For orgin-form and asterisk-form
609 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
610 * removed from the flags of the start-line. Otherwise, this flag is set and the
611 * authority is used to set the value of the header host. This function returns
612 * 0 on failure and 1 on success.
613*/
Christopher Faulet1543d442020-04-28 19:57:29 +0200614int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200615{
616 struct ist authority;
617 struct http_hdr_ctx ctx;
618
619 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
620 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
621 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
622 }
623 else {
624 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
625 if (sl->info.req.meth != HTTP_METH_CONNECT) {
626 // absolute-form (RFC7320 #5.3.2)
627 sl->flags |= HTX_SL_F_HAS_SCHM;
628 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
629 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
630
631 authority = http_get_authority(uri, 1);
632 if (!authority.len)
633 goto fail;
634 }
635 else {
636 // authority-form (RFC7320 #5.3.3)
637 authority = uri;
638 }
639
640 /* Replace header host value */
641 ctx.blk = NULL;
642 while (http_find_header(htx, ist("host"), &ctx, 1)) {
643 if (!http_replace_header_value(htx, &ctx, authority))
644 goto fail;
645 }
646
647 }
648 return 1;
649 fail:
650 return 0;
651}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200652
653/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
654 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
655 * performed over the whole headers. Otherwise it must contain a valid header
656 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
657 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
658 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
659 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
660 * -1. The value fetch stops at commas, so this function is suited for use with
661 * list headers.
662 * The return value is 0 if nothing was found, or non-zero otherwise.
663 */
664unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
665 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
666{
667 struct http_hdr_ctx local_ctx;
668 struct ist val_hist[MAX_HDR_HISTORY];
669 unsigned int hist_idx;
670 int found;
671
672 if (!ctx) {
673 local_ctx.blk = NULL;
674 ctx = &local_ctx;
675 }
676
677 if (occ >= 0) {
678 /* search from the beginning */
679 while (http_find_header(htx, hdr, ctx, 0)) {
680 occ--;
681 if (occ <= 0) {
682 *vptr = ctx->value.ptr;
683 *vlen = ctx->value.len;
684 return 1;
685 }
686 }
687 return 0;
688 }
689
690 /* negative occurrence, we scan all the list then walk back */
691 if (-occ > MAX_HDR_HISTORY)
692 return 0;
693
694 found = hist_idx = 0;
695 while (http_find_header(htx, hdr, ctx, 0)) {
696 val_hist[hist_idx] = ctx->value;
697 if (++hist_idx >= MAX_HDR_HISTORY)
698 hist_idx = 0;
699 found++;
700 }
701 if (-occ > found)
702 return 0;
703
704 /* OK now we have the last occurrence in [hist_idx-1], and we need to
705 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
706 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
707 * to remain in the 0..9 range.
708 */
709 hist_idx += occ + MAX_HDR_HISTORY;
710 if (hist_idx >= MAX_HDR_HISTORY)
711 hist_idx -= MAX_HDR_HISTORY;
712 *vptr = val_hist[hist_idx].ptr;
713 *vlen = val_hist[hist_idx].len;
714 return 1;
715}
716
717/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
718 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
719 * performed over the whole headers. Otherwise it must contain a valid header
720 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
721 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
722 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
723 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
724 * -1. This function differs from http_get_hdr() in that it only returns full
725 * line header values and does not stop at commas.
726 * The return value is 0 if nothing was found, or non-zero otherwise.
727 */
728unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
729 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
730{
731 struct http_hdr_ctx local_ctx;
732 struct ist val_hist[MAX_HDR_HISTORY];
733 unsigned int hist_idx;
734 int found;
735
736 if (!ctx) {
737 local_ctx.blk = NULL;
738 ctx = &local_ctx;
739 }
740
741 if (occ >= 0) {
742 /* search from the beginning */
743 while (http_find_header(htx, hdr, ctx, 1)) {
744 occ--;
745 if (occ <= 0) {
746 *vptr = ctx->value.ptr;
747 *vlen = ctx->value.len;
748 return 1;
749 }
750 }
751 return 0;
752 }
753
754 /* negative occurrence, we scan all the list then walk back */
755 if (-occ > MAX_HDR_HISTORY)
756 return 0;
757
758 found = hist_idx = 0;
759 while (http_find_header(htx, hdr, ctx, 1)) {
760 val_hist[hist_idx] = ctx->value;
761 if (++hist_idx >= MAX_HDR_HISTORY)
762 hist_idx = 0;
763 found++;
764 }
765 if (-occ > found)
766 return 0;
767
768 /* OK now we have the last occurrence in [hist_idx-1], and we need to
769 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
770 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
771 * to remain in the 0..9 range.
772 */
773 hist_idx += occ + MAX_HDR_HISTORY;
774 if (hist_idx >= MAX_HDR_HISTORY)
775 hist_idx -= MAX_HDR_HISTORY;
776 *vptr = val_hist[hist_idx].ptr;
777 *vlen = val_hist[hist_idx].len;
778 return 1;
779}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100780
Christopher Faulet90cc4812019-07-22 16:49:30 +0200781int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100782{
783 struct htx *htx;
784 struct htx_sl *sl;
785 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200786 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100787 union h1_sl h1sl;
788 unsigned int flags = HTX_SL_F_IS_RESP;
789 int ret = 0;
790
Christopher Faulet90cc4812019-07-22 16:49:30 +0200791 b_reset(buf);
792 if (!raw.len) {
793 buf->size = 0;
794 buf->area = malloc(raw.len);
795 return 1;
796 }
797
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100798 buf->size = global.tune.bufsize;
799 buf->area = (char *)malloc(buf->size);
800 if (!buf->area)
801 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100802
803 h1m_init_res(&h1m);
804 h1m.flags |= H1_MF_NO_PHDR;
805 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
806 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
807 if (ret <= 0)
808 goto error;
809
810 if (unlikely(h1sl.st.v.len != 8))
811 goto error;
812 if ((*(h1sl.st.v.ptr + 5) > '1') ||
813 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
814 h1m.flags |= H1_MF_VER_11;
815
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200816 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
817 goto error;
818
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100819 if (h1m.flags & H1_MF_VER_11)
820 flags |= HTX_SL_F_VER_11;
821 if (h1m.flags & H1_MF_XFER_ENC)
822 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200823 if (h1m.flags & H1_MF_CLEN) {
824 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
825 if (h1m.body_len == 0)
826 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100827 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200828 if (h1m.flags & H1_MF_CHNK)
829 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100830
831 htx = htx_from_buf(buf);
832 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
833 if (!sl || !htx_add_all_headers(htx, hdrs))
834 goto error;
835 sl->info.res.status = h1sl.st.status;
836
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200837 while (raw.len > ret) {
838 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
839 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100840 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200841 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100842 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200843
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100844 if (!htx_add_endof(htx, HTX_BLK_EOM))
845 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200846
Christopher Faulet90cc4812019-07-22 16:49:30 +0200847 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100848
849error:
850 if (buf->size)
851 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200852 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100853}
854
855static int http_htx_init(void)
856{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100857 struct buffer chk;
858 struct ist raw;
859 int rc;
860 int err_code = 0;
861
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100862 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
863 if (!http_err_msgs[rc]) {
864 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
865 err_code |= ERR_ALERT | ERR_FATAL;
866 continue;
867 }
868
869 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
870 if (!http_str_to_htx(&chk, raw)) {
871 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
872 http_err_codes[rc]);
873 err_code |= ERR_ALERT | ERR_FATAL;
874 }
Christopher Fauletf7346382019-07-17 22:02:08 +0200875 http_err_chunks[rc] = chk;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100876 }
877end:
878 return err_code;
879}
880
Christopher Faulet58857752020-01-15 15:19:50 +0100881static void http_htx_deinit(void)
882{
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100883 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +0100884 struct ebpt_node *node, *next;
885 struct http_error *http_err;
886
887 node = ebpt_first(&http_error_messages);
888 while (node) {
889 next = ebpt_next(node);
890 ebpt_delete(node);
891 http_err = container_of(node, typeof(*http_err), node);
892 chunk_destroy(&http_err->msg);
893 free(node->key);
894 free(http_err);
895 node = next;
896 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100897
898 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
899 free(http_errs->conf.file);
900 free(http_errs->id);
901 LIST_DEL(&http_errs->list);
902 free(http_errs);
903 }
Christopher Faulet58857752020-01-15 15:19:50 +0100904}
905
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100906REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +0100907REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +0100908
Christopher Faulet58857752020-01-15 15:19:50 +0100909/* Reads content of the error file <file> and convert it into an HTX message. On
910 * success, the HTX message is returned. On error, NULL is returned and an error
911 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +0100912 */
Christopher Faulet58857752020-01-15 15:19:50 +0100913struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +0100914{
Christopher Faulet58857752020-01-15 15:19:50 +0100915 struct buffer *buf = NULL;
916 struct buffer chk;
917 struct ebpt_node *node;
918 struct http_error *http_err;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100919 struct stat stat;
920 char *err = NULL;
921 int errnum, errlen;
922 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +0100923
924 /* already loaded */
925 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
926 if (node) {
927 http_err = container_of(node, typeof(*http_err), node);
928 buf = &http_err->msg;
929 goto out;
930 }
Christopher Faulet5031ef52020-01-15 11:22:07 +0100931
Christopher Faulet58857752020-01-15 15:19:50 +0100932 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +0100933 fd = open(file, O_RDONLY);
934 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
935 memprintf(errmsg, "error opening file '%s'.", file);
936 goto out;
937 }
938
939 if (stat.st_size <= global.tune.bufsize)
940 errlen = stat.st_size;
941 else {
942 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
943 file, global.tune.bufsize);
944 errlen = global.tune.bufsize;
945 }
946
947 err = malloc(errlen);
948 if (!err) {
949 memprintf(errmsg, "out of memory.");
950 goto out;
951 }
952
953 errnum = read(fd, err, errlen);
954 if (errnum != errlen) {
955 memprintf(errmsg, "error reading file '%s'.", file);
956 goto out;
957 }
958
Christopher Faulet58857752020-01-15 15:19:50 +0100959 /* Create the node corresponding to the error file */
960 http_err = calloc(1, sizeof(*http_err));
961 if (!http_err) {
962 memprintf(errmsg, "out of memory.");
963 goto out;
964 }
965 http_err->node.key = strdup(file);
966 if (!http_err->node.key) {
967 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +0100968 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +0100969 goto out;
970 }
971
972 /* Convert the error file into an HTX message */
973 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +0100974 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Faulet58857752020-01-15 15:19:50 +0100975 free(http_err->node.key);
976 free(http_err);
Christopher Faulet5031ef52020-01-15 11:22:07 +0100977 goto out;
978 }
979
Christopher Faulet58857752020-01-15 15:19:50 +0100980 /* Insert the node in the tree and return the HTX message */
981 http_err->msg = chk;
982 ebis_insert(&http_error_messages, &http_err->node);
983 buf = &http_err->msg;
984
Christopher Faulet5031ef52020-01-15 11:22:07 +0100985 out:
986 if (fd >= 0)
987 close(fd);
988 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +0100989 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100990}
991
Ilya Shipitsind4259502020-04-08 01:07:56 +0500992/* Convert the raw http message <msg> into an HTX message. On success, the HTX
Christopher Faulet58857752020-01-15 15:19:50 +0100993 * message is returned. On error, NULL is returned and an error message is
994 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +0100995 */
Christopher Faulet58857752020-01-15 15:19:50 +0100996struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +0100997{
Christopher Faulet58857752020-01-15 15:19:50 +0100998 struct buffer *buf = NULL;
999 struct buffer chk;
1000 struct ebpt_node *node;
1001 struct http_error *http_err;
1002
1003 /* already loaded */
1004 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
1005 if (node) {
1006 http_err = container_of(node, typeof(*http_err), node);
1007 buf = &http_err->msg;
1008 goto out;
1009 }
1010 /* Create the node corresponding to the error file */
1011 http_err = calloc(1, sizeof(*http_err));
1012 if (!http_err) {
1013 memprintf(errmsg, "out of memory.");
1014 goto out;
1015 }
1016 http_err->node.key = strdup(key);
1017 if (!http_err->node.key) {
1018 memprintf(errmsg, "out of memory.");
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001019 free(http_err);
Christopher Faulet58857752020-01-15 15:19:50 +01001020 goto out;
1021 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001022
1023 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +01001024 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +01001025 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Faulet58857752020-01-15 15:19:50 +01001026 free(http_err->node.key);
1027 free(http_err);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001028 goto out;
1029 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001030
Christopher Faulet58857752020-01-15 15:19:50 +01001031 /* Insert the node in the tree and return the HTX message */
1032 http_err->msg = chk;
1033 ebis_insert(&http_error_messages, &http_err->node);
1034 buf = &http_err->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001035 out:
Christopher Faulet58857752020-01-15 15:19:50 +01001036 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001037}
1038
Christopher Faulet5031ef52020-01-15 11:22:07 +01001039/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +01001040 * <status>. It returns NULL if there is any error, otherwise it return the
1041 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001042 */
Christopher Faulet58857752020-01-15 15:19:50 +01001043struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001044{
Christopher Faulet58857752020-01-15 15:19:50 +01001045 struct buffer *buf = NULL;
1046 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001047
1048 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1049 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001050 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001051 break;
1052 }
1053 }
1054
1055 if (rc >= HTTP_ERR_SIZE)
1056 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001057 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001058}
1059
1060/* This function creates HTX error message corresponding to a redirect message
1061 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001062 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1063 * returns NULL if there is any error, otherwise it return the corresponding HTX
1064 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001065 */
Christopher Faulet58857752020-01-15 15:19:50 +01001066struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001067{
Christopher Faulet58857752020-01-15 15:19:50 +01001068 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001069 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001070 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001071 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001072
1073 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1074 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001075 /* Create the error key */
1076 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1077 memprintf(errmsg, "out of memory.");
1078 goto out;
1079 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001080 /* Create the error message */
1081 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1082 errlen = strlen(msg) + strlen(url) + 5;
1083 err = malloc(errlen);
1084 if (!err) {
1085 memprintf(errmsg, "out of memory.");
1086 goto out;
1087 }
1088 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1089
1090 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001091 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001092 break;
1093 }
1094 }
1095
1096 if (rc >= HTTP_ERR_SIZE)
1097 memprintf(errmsg, "status code '%d' not handled.", status);
1098out:
Christopher Faulet58857752020-01-15 15:19:50 +01001099 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001100 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001101 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001102}
1103
Christopher Faulet07f41f72020-01-16 16:16:06 +01001104/* Parses the "errorloc[302|303]" proxy keyword */
1105static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1106 struct proxy *defpx, const char *file, int line,
1107 char **errmsg)
1108{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001109 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001110 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001111 int errloc, status;
1112 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001113
1114 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1115 ret = 1;
1116 goto out;
1117 }
1118
1119 if (*(args[1]) == 0 || *(args[2]) == 0) {
1120 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1121 ret = -1;
1122 goto out;
1123 }
1124
1125 status = atol(args[1]);
1126 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1127 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1128 if (!msg) {
1129 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1130 ret = -1;
1131 goto out;
1132 }
1133
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001134 conf_err = calloc(1, sizeof(*conf_err));
1135 if (!conf_err) {
1136 memprintf(errmsg, "%s : out of memory.", args[0]);
1137 ret = -1;
1138 goto out;
1139 }
1140 conf_err->type = 1;
1141 conf_err->info.errorfile.status = status;
1142 conf_err->info.errorfile.msg = msg;
1143 conf_err->file = strdup(file);
1144 conf_err->line = line;
1145 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001146
1147 out:
1148 return ret;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001149
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001150}
Christopher Faulet07f41f72020-01-16 16:16:06 +01001151
1152/* Parses the "errorfile" proxy keyword */
1153static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1154 struct proxy *defpx, const char *file, int line,
1155 char **errmsg)
1156{
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001157 struct conf_errors *conf_err;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001158 struct buffer *msg;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001159 int status;
1160 int ret = 0;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001161
1162 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1163 ret = 1;
1164 goto out;
1165 }
1166
1167 if (*(args[1]) == 0 || *(args[2]) == 0) {
1168 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1169 ret = -1;
1170 goto out;
1171 }
1172
1173 status = atol(args[1]);
1174 msg = http_parse_errorfile(status, args[2], errmsg);
1175 if (!msg) {
1176 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1177 ret = -1;
1178 goto out;
1179 }
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001180
1181 conf_err = calloc(1, sizeof(*conf_err));
1182 if (!conf_err) {
1183 memprintf(errmsg, "%s : out of memory.", args[0]);
1184 ret = -1;
1185 goto out;
1186 }
1187 conf_err->type = 1;
1188 conf_err->info.errorfile.status = status;
1189 conf_err->info.errorfile.msg = msg;
1190 conf_err->file = strdup(file);
1191 conf_err->line = line;
1192 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1193
1194 out:
1195 return ret;
1196
1197}
1198
1199/* Parses the "errorfiles" proxy keyword */
1200static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx,
1201 struct proxy *defpx, const char *file, int line,
1202 char **err)
1203{
1204 struct conf_errors *conf_err = NULL;
1205 char *name = NULL;
1206 int rc, ret = 0;
1207
1208 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1209 ret = 1;
1210 goto out;
1211 }
1212
1213 if (!*(args[1])) {
1214 memprintf(err, "%s : expects <name> as argument.", args[0]);
1215 ret = -1;
1216 goto out;
1217 }
1218
1219 name = strdup(args[1]);
1220 conf_err = calloc(1, sizeof(*conf_err));
1221 if (!name || !conf_err) {
1222 memprintf(err, "%s : out of memory.", args[0]);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001223 goto error;
1224 }
1225 conf_err->type = 0;
1226
1227 conf_err->info.errorfiles.name = name;
1228 if (!*(args[2])) {
1229 for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
1230 conf_err->info.errorfiles.status[rc] = 1;
1231 }
1232 else {
1233 int cur_arg, status;
1234 for (cur_arg = 2; *(args[cur_arg]); cur_arg++) {
1235 status = atol(args[cur_arg]);
1236
1237 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1238 if (http_err_codes[rc] == status) {
1239 conf_err->info.errorfiles.status[rc] = 2;
1240 break;
1241 }
1242 }
1243 if (rc >= HTTP_ERR_SIZE) {
1244 memprintf(err, "%s : status code '%d' not handled.", args[0], status);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001245 goto error;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001246 }
1247 }
1248 }
1249 conf_err->file = strdup(file);
1250 conf_err->line = line;
1251 LIST_ADDQ(&curpx->conf.errors, &conf_err->list);
1252 out:
1253 return ret;
1254
1255 error:
1256 free(name);
1257 free(conf_err);
Christopher Faulet7cde96c2020-01-21 10:10:11 +01001258 ret = -1;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001259 goto out;
1260}
1261
1262/* Check "errorfiles" proxy keyword */
1263static int proxy_check_errors(struct proxy *px)
1264{
1265 struct conf_errors *conf_err, *conf_err_back;
1266 struct http_errors *http_errs;
1267 int rc, err = 0;
1268
1269 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1270 if (conf_err->type == 1) {
1271 /* errorfile */
1272 rc = http_get_status_idx(conf_err->info.errorfile.status);
1273 px->errmsg[rc] = conf_err->info.errorfile.msg;
1274 }
1275 else {
1276 /* errorfiles */
1277 list_for_each_entry(http_errs, &http_errors_list, list) {
1278 if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0)
1279 break;
1280 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001281
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001282 /* unknown http-errors section */
1283 if (&http_errs->list == &http_errors_list) {
1284 ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n",
1285 px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line);
1286 err |= ERR_ALERT | ERR_FATAL;
1287 free(conf_err->info.errorfiles.name);
1288 goto next;
1289 }
1290
1291 free(conf_err->info.errorfiles.name);
1292 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1293 if (conf_err->info.errorfiles.status[rc] > 0) {
1294 if (http_errs->errmsg[rc])
1295 px->errmsg[rc] = http_errs->errmsg[rc];
1296 else if (conf_err->info.errorfiles.status[rc] == 2)
1297 ha_warning("config: proxy '%s' : status '%d' not declared in"
1298 " http-errors section '%s' (at %s:%d).\n",
1299 px->id, http_err_codes[rc], http_errs->id,
1300 conf_err->file, conf_err->line);
1301 }
1302 }
1303 }
1304 next:
1305 LIST_DEL(&conf_err->list);
1306 free(conf_err->file);
1307 free(conf_err);
1308 }
Christopher Faulet07f41f72020-01-16 16:16:06 +01001309
1310 out:
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001311 return err;
1312}
1313
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001314static int post_check_errors()
1315{
1316 struct ebpt_node *node;
1317 struct http_error *http_err;
1318 struct htx *htx;
1319 int err_code = 0;
1320
1321 node = ebpt_first(&http_error_messages);
1322 while (node) {
1323 http_err = container_of(node, typeof(*http_err), node);
1324 if (b_is_null(&http_err->msg))
1325 goto next;
1326 htx = htxbuf(&http_err->msg);
1327 if (htx_free_data_space(htx) < global.tune.maxrewrite) {
1328 ha_warning("config: errorfile '%s' runs over the buffer space"
1329 " reserved to headers rewritting. It may lead to internal errors if "
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001330 " http-after-response rules are evaluated on this message.\n",
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001331 (char *)node->key);
1332 err_code |= ERR_WARN;
1333 }
1334 next:
1335 node = ebpt_next(node);
1336 }
1337
1338 return err_code;
1339}
1340
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001341int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg)
1342{
1343 struct conf_errors *conf_err, *new_conf_err = NULL;
1344 int ret = 0;
1345
1346 list_for_each_entry(conf_err, &defpx->conf.errors, list) {
1347 new_conf_err = calloc(1, sizeof(*new_conf_err));
1348 if (!new_conf_err) {
1349 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1350 goto out;
1351 }
1352 new_conf_err->type = conf_err->type;
1353 if (conf_err->type == 1) {
1354 new_conf_err->info.errorfile.status = conf_err->info.errorfile.status;
1355 new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg;
1356 }
1357 else {
1358 new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name);
1359 if (!new_conf_err->info.errorfiles.name) {
1360 memprintf(errmsg, "unable to duplicate default errors (out of memory).");
1361 goto out;
1362 }
1363 memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status,
1364 sizeof(conf_err->info.errorfiles.status));
1365 }
1366 new_conf_err->file = strdup(conf_err->file);
1367 new_conf_err->line = conf_err->line;
1368 LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list);
1369 new_conf_err = NULL;
1370 }
1371 ret = 1;
1372
1373 out:
1374 free(new_conf_err);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001375 return ret;
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001376}
1377
1378void proxy_release_conf_errors(struct proxy *px)
1379{
1380 struct conf_errors *conf_err, *conf_err_back;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001381
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001382 list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) {
1383 if (conf_err->type == 0)
1384 free(conf_err->info.errorfiles.name);
1385 LIST_DEL(&conf_err->list);
1386 free(conf_err->file);
1387 free(conf_err);
1388 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001389}
1390
1391/*
1392 * Parse an <http-errors> section.
1393 * Returns the error code, 0 if OK, or any combination of :
1394 * - ERR_ABORT: must abort ASAP
1395 * - ERR_FATAL: we can continue parsing but not start the service
1396 * - ERR_WARN: a warning has been emitted
1397 * - ERR_ALERT: an alert has been emitted
1398 * Only the two first ones can stop processing, the two others are just
1399 * indicators.
1400 */
1401static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1402{
1403 static struct http_errors *curr_errs = NULL;
1404 int err_code = 0;
1405 const char *err;
1406 char *errmsg = NULL;
1407
1408 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1409 if (!*args[1]) {
1410 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1411 err_code |= ERR_ALERT | ERR_ABORT;
1412 goto out;
1413 }
1414
1415 err = invalid_char(args[1]);
1416 if (err) {
1417 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1418 file, linenum, *err, args[0], args[1]);
1419 err_code |= ERR_ALERT | ERR_FATAL;
1420 }
1421
1422 list_for_each_entry(curr_errs, &http_errors_list, list) {
1423 /* Error if two errors section owns the same name */
1424 if (strcmp(curr_errs->id, args[1]) == 0) {
1425 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1426 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1427 err_code |= ERR_ALERT | ERR_FATAL;
1428 }
1429 }
1430
1431 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1432 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1433 err_code |= ERR_ALERT | ERR_ABORT;
1434 goto out;
1435 }
1436
1437 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1438 curr_errs->id = strdup(args[1]);
1439 curr_errs->conf.file = strdup(file);
1440 curr_errs->conf.line = linenum;
1441 }
1442 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1443 struct buffer *msg;
1444 int status, rc;
1445
1446 if (*(args[1]) == 0 || *(args[2]) == 0) {
1447 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1448 file, linenum, args[0]);
1449 err_code |= ERR_ALERT | ERR_FATAL;
1450 goto out;
1451 }
1452
1453 status = atol(args[1]);
1454 msg = http_parse_errorfile(status, args[2], &errmsg);
1455 if (!msg) {
1456 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1457 err_code |= ERR_ALERT | ERR_FATAL;
1458 goto out;
1459 }
1460 rc = http_get_status_idx(status);
1461 curr_errs->errmsg[rc] = msg;
1462 }
1463 else if (*args[0] != 0) {
1464 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1465 err_code |= ERR_ALERT | ERR_FATAL;
1466 goto out;
1467 }
1468
1469out:
1470 free(errmsg);
1471 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001472}
1473
1474static struct cfg_kw_list cfg_kws = {ILH, {
1475 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1476 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1477 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1478 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001479 { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles },
Christopher Faulet07f41f72020-01-16 16:16:06 +01001480 { 0, NULL, NULL },
1481}};
1482
1483INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001484REGISTER_POST_PROXY_CHECK(proxy_check_errors);
Christopher Faulet0a589fd2020-01-22 14:47:04 +01001485REGISTER_POST_CHECK(post_check_errors);
Christopher Faulet07f41f72020-01-16 16:16:06 +01001486
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001487REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1488
Christopher Faulet29f72842019-12-11 15:52:32 +01001489/************************************************************************/
1490/* HTX sample fetches */
1491/************************************************************************/
1492
1493/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1494static int
1495smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1496{
1497 if (!smp->strm)
1498 return 0;
1499
1500 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1501 smp->data.type = SMP_T_BOOL;
1502 return 1;
1503}
1504
1505/* Returns the number of blocks in an HTX message. The channel is chosen
1506 * depending on the sample direction. */
1507static int
1508smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1509{
1510 struct channel *chn;
1511 struct htx *htx;
1512
1513 if (!smp->strm)
1514 return 0;
1515
1516 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001517 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001518 if (!htx)
1519 return 0;
1520
1521 smp->data.u.sint = htx_nbblks(htx);
1522 smp->data.type = SMP_T_SINT;
1523 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1524 return 1;
1525}
1526
1527/* Returns the size of an HTX message. The channel is chosen depending on the
1528 * sample direction. */
1529static int
1530smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1531{
1532 struct channel *chn;
1533 struct htx *htx;
1534
1535 if (!smp->strm)
1536 return 0;
1537
1538 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001539 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001540 if (!htx)
1541 return 0;
1542
1543 smp->data.u.sint = htx->size;
1544 smp->data.type = SMP_T_SINT;
1545 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1546 return 1;
1547}
1548
1549/* Returns the data size of an HTX message. The channel is chosen depending on the
1550 * sample direction. */
1551static int
1552smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1553{
1554 struct channel *chn;
1555 struct htx *htx;
1556
1557 if (!smp->strm)
1558 return 0;
1559
1560 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001561 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001562 if (!htx)
1563 return 0;
1564
1565 smp->data.u.sint = htx->data;
1566 smp->data.type = SMP_T_SINT;
1567 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1568 return 1;
1569}
1570
1571/* Returns the used space (data+meta) of an HTX message. The channel is chosen
1572 * depending on the sample direction. */
1573static int
1574smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1575{
1576 struct channel *chn;
1577 struct htx *htx;
1578
1579 if (!smp->strm)
1580 return 0;
1581
1582 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001583 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001584 if (!htx)
1585 return 0;
1586
1587 smp->data.u.sint = htx_used_space(htx);
1588 smp->data.type = SMP_T_SINT;
1589 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1590 return 1;
1591}
1592
1593/* Returns the free space (size-used) of an HTX message. The channel is chosen
1594 * depending on the sample direction. */
1595static int
1596smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1597{
1598 struct channel *chn;
1599 struct htx *htx;
1600
1601 if (!smp->strm)
1602 return 0;
1603
1604 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001605 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001606 if (!htx)
1607 return 0;
1608
1609 smp->data.u.sint = htx_free_space(htx);
1610 smp->data.type = SMP_T_SINT;
1611 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1612 return 1;
1613}
1614
1615/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
1616 * channel is chosen depending on the sample direction. */
1617static int
1618smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1619{
1620 struct channel *chn;
1621 struct htx *htx;
1622
1623 if (!smp->strm)
1624 return 0;
1625
1626 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001627 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001628 if (!htx)
1629 return 0;
1630
1631 smp->data.u.sint = htx_free_data_space(htx);
1632 smp->data.type = SMP_T_SINT;
1633 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1634 return 1;
1635}
1636
1637/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
1638 * 0. Concretely, it only checks the tail. The channel is chosen depending on
1639 * the sample direction. */
1640static int
1641smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1642{
1643 struct channel *chn;
1644 struct htx *htx;
1645
1646 if (!smp->strm)
1647 return 0;
1648
1649 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001650 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001651 if (!htx)
1652 return 0;
1653
1654 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
1655 smp->data.type = SMP_T_BOOL;
1656 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1657 return 1;
1658}
1659
1660/* Returns the type of a specific HTX block, if found in the message. Otherwise
1661 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
1662 * "head", "tail" or "first". The channel is chosen depending on the sample
1663 * direction. */
1664static int
1665smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1666{
1667 struct channel *chn;
1668 struct htx *htx;
1669 enum htx_blk_type type;
1670 int32_t pos;
1671
1672 if (!smp->strm || !arg_p)
1673 return 0;
1674
1675 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001676 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001677 if (!htx)
1678 return 0;
1679
1680 pos = arg_p[0].data.sint;
1681 if (pos == -1)
1682 type = htx_get_head_type(htx);
1683 else if (pos == -2)
1684 type = htx_get_tail_type(htx);
1685 else if (pos == -3)
1686 type = htx_get_first_type(htx);
1687 else
1688 type = ((pos >= htx->head && pos <= htx->tail)
1689 ? htx_get_blk_type(htx_get_blk(htx, pos))
1690 : HTX_BLK_UNUSED);
1691
1692 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
1693 smp->data.type = SMP_T_STR;
1694 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1695 return 1;
1696}
1697
1698/* Returns the size of a specific HTX block, if found in the message. Otherwise
1699 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
1700 * "first". The channel is chosen depending on the sample direction. */
1701static int
1702smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1703{
1704 struct channel *chn;
1705 struct htx *htx;
1706 struct htx_blk *blk;
1707 int32_t pos;
1708
1709 if (!smp->strm || !arg_p)
1710 return 0;
1711
1712 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001713 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001714 if (!htx)
1715 return 0;
1716
1717 pos = arg_p[0].data.sint;
1718 if (pos == -1)
1719 blk = htx_get_head_blk(htx);
1720 else if (pos == -2)
1721 blk = htx_get_tail_blk(htx);
1722 else if (pos == -3)
1723 blk = htx_get_first_blk(htx);
1724 else
1725 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1726
1727 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
1728 smp->data.type = SMP_T_SINT;
1729 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1730 return 1;
1731}
1732
1733/* Returns the start-line if the selected HTX block exists and is a
1734 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
1735 * supported or "head", "tail" or "first". The channel is chosen depending on
1736 * the sample direction. */
1737static int
1738smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1739{
1740 struct buffer *temp;
1741 struct channel *chn;
1742 struct htx *htx;
1743 struct htx_blk *blk;
1744 struct htx_sl *sl;
1745 int32_t pos;
1746
1747 if (!smp->strm || !arg_p)
1748 return 0;
1749
1750 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001751 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001752 if (!htx)
1753 return 0;
1754
1755 pos = arg_p[0].data.sint;
1756 if (pos == -1)
1757 blk = htx_get_head_blk(htx);
1758 else if (pos == -2)
1759 blk = htx_get_tail_blk(htx);
1760 else if (pos == -3)
1761 blk = htx_get_first_blk(htx);
1762 else
1763 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1764
1765 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
1766 smp->data.u.str.size = 0;
1767 smp->data.u.str.area = "";
1768 smp->data.u.str.data = 0;
1769 }
1770 else {
1771 sl = htx_get_blk_ptr(htx, blk);
1772
1773 temp = get_trash_chunk();
1774 chunk_istcat(temp, htx_sl_p1(sl));
1775 temp->area[temp->data++] = ' ';
1776 chunk_istcat(temp, htx_sl_p2(sl));
1777 temp->area[temp->data++] = ' ';
1778 chunk_istcat(temp, htx_sl_p3(sl));
1779
1780 smp->data.u.str = *temp;
1781 }
1782
1783 smp->data.type = SMP_T_STR;
1784 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1785 return 1;
1786}
1787
1788/* Returns the header name if the selected HTX block exists and is a header or a
1789 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1790 * supported or "head", "tail" or "first". The channel is chosen depending on
1791 * the sample direction. */
1792static int
1793smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1794{
1795 struct channel *chn;
1796 struct htx *htx;
1797 struct htx_blk *blk;
1798 int32_t pos;
1799
1800 if (!smp->strm || !arg_p)
1801 return 0;
1802
1803 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001804 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001805 if (!htx)
1806 return 0;
1807
1808 pos = arg_p[0].data.sint;
1809 if (pos == -1)
1810 blk = htx_get_head_blk(htx);
1811 else if (pos == -2)
1812 blk = htx_get_tail_blk(htx);
1813 else if (pos == -3)
1814 blk = htx_get_first_blk(htx);
1815 else
1816 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1817
1818 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1819 smp->data.u.str.size = 0;
1820 smp->data.u.str.area = "";
1821 smp->data.u.str.data = 0;
1822 }
1823 else {
1824 struct ist name = htx_get_blk_name(htx, blk);
1825
1826 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
1827 }
1828 smp->data.type = SMP_T_STR;
1829 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1830 return 1;
1831}
1832
1833/* Returns the header value if the selected HTX block exists and is a header or
1834 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1835 * supported or "head", "tail" or "first". The channel is chosen depending on
1836 * the sample direction. */
1837static int
1838smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1839{
1840 struct channel *chn;
1841 struct htx *htx;
1842 struct htx_blk *blk;
1843 int32_t pos;
1844
1845 if (!smp->strm || !arg_p)
1846 return 0;
1847
1848 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001849 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001850 if (!htx)
1851 return 0;
1852
1853 pos = arg_p[0].data.sint;
1854 if (pos == -1)
1855 blk = htx_get_head_blk(htx);
1856 else if (pos == -2)
1857 blk = htx_get_tail_blk(htx);
1858 else if (pos == -3)
1859 blk = htx_get_first_blk(htx);
1860 else
1861 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1862
1863 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1864 smp->data.u.str.size = 0;
1865 smp->data.u.str.area = "";
1866 smp->data.u.str.data = 0;
1867 }
1868 else {
1869 struct ist val = htx_get_blk_value(htx, blk);
1870
1871 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1872 }
1873 smp->data.type = SMP_T_STR;
1874 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1875 return 1;
1876}
1877
1878/* Returns the value if the selected HTX block exists and is a data
1879 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
1880 * or "head", "tail" or "first". The channel is chosen depending on the sample
1881 * direction. */
1882static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01001883smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01001884{
1885 struct channel *chn;
1886 struct htx *htx;
1887 struct htx_blk *blk;
1888 int32_t pos;
1889
1890 if (!smp->strm || !arg_p)
1891 return 0;
1892
1893 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001894 htx = smp_prefetch_htx(smp, chn, NULL, 0);
Christopher Faulet29f72842019-12-11 15:52:32 +01001895 if (!htx)
1896 return 0;
1897
1898 pos = arg_p[0].data.sint;
1899 if (pos == -1)
1900 blk = htx_get_head_blk(htx);
1901 else if (pos == -2)
1902 blk = htx_get_tail_blk(htx);
1903 else if (pos == -3)
1904 blk = htx_get_first_blk(htx);
1905 else
1906 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1907
1908 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
1909 smp->data.u.str.size = 0;
1910 smp->data.u.str.area = "";
1911 smp->data.u.str.data = 0;
1912 }
1913 else {
1914 struct ist val = htx_get_blk_value(htx, blk);
1915
1916 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1917 }
Christopher Faulet8178e402020-01-08 14:38:58 +01001918 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01001919 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1920 return 1;
1921}
1922
1923/* This function is used to validate the arguments passed to any "htx_blk" fetch
1924 * keywords. An argument is expected by these keywords. It must be a positive
1925 * integer or on of the following strings: "head", "tail" or "first". It returns
1926 * 0 on error, and a non-zero value if OK.
1927 */
1928int val_blk_arg(struct arg *arg, char **err_msg)
1929{
1930 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
1931 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
1932 return 0;
1933 }
1934 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
1935 free(arg[0].data.str.area);
1936 arg[0].type = ARGT_SINT;
1937 arg[0].data.sint = -1;
1938 }
1939 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
1940 free(arg[0].data.str.area);
1941 arg[0].type = ARGT_SINT;
1942 arg[0].data.sint = -2;
1943 }
1944 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
1945 free(arg[0].data.str.area);
1946 arg[0].type = ARGT_SINT;
1947 arg[0].data.sint = -3;
1948 }
1949 else {
1950 int pos;
1951
1952 for (pos = 0; pos < arg[0].data.str.data; pos++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001953 if (!isdigit((unsigned char)arg[0].data.str.area[pos])) {
Christopher Faulet29f72842019-12-11 15:52:32 +01001954 memprintf(err_msg, "invalid block position");
1955 return 0;
1956 }
1957 }
1958
1959 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
1960 if (pos < 0) {
1961 memprintf(err_msg, "block position must not be negative");
1962 return 0;
1963 }
1964 free(arg[0].data.str.area);
1965 arg[0].type = ARGT_SINT;
1966 arg[0].data.sint = pos;
1967 }
1968
1969 return 1;
1970}
1971
1972
1973/* Note: must not be declared <const> as its list will be overwritten.
Ilya Shipitsind4259502020-04-08 01:07:56 +05001974 * Note: htx sample fetches should only used for development purpose.
Christopher Faulet29f72842019-12-11 15:52:32 +01001975 */
1976static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01001977 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01001978
Christopher Faulet01f44452020-01-08 14:23:40 +01001979 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1980 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1981 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1982 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1983 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1984 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1985 { "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 +01001986
Christopher Faulet01f44452020-01-08 14:23:40 +01001987 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1988 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1989 { "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},
1990 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1991 { "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 +01001992 { "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 +01001993
1994 { /* END */ },
1995}};
1996
1997INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);