blob: e848e37e84d15c4260cb20702e47063fda57ad00 [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 Fauletd7b7a1c2019-10-08 15:24:52 +020035static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host);
36static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri);
37
Christopher Faulet297fbb42019-05-13 14:41:27 +020038/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020039 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020040 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020041 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020042struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020043{
Christopher Faulet297fbb42019-05-13 14:41:27 +020044 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010045
Christopher Faulet29f17582019-05-23 11:03:26 +020046 BUG_ON(htx->first == -1);
47 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020048 if (!blk)
49 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020050 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 +020051 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020052}
53
54/* Finds the first or next occurrence of header <name> in the HTX message <htx>
55 * using the context <ctx>. This structure holds everything necessary to use the
56 * header and find next occurrence. If its <blk> member is NULL, the header is
57 * searched from the beginning. Otherwise, the next occurrence is returned. The
58 * function returns 1 when it finds a value, and 0 when there is no more. It is
59 * designed to work with headers defined as comma-separated lists. If <full> is
60 * set, it works on full-line headers in whose comma is not a delimiter but is
61 * part of the syntax. A special case, if ctx->value is NULL when searching for
62 * a new values of a header, the current header is rescanned. This allows
63 * rescanning after a header deletion.
64 */
65int http_find_header(const struct htx *htx, const struct ist name,
66 struct http_hdr_ctx *ctx, int full)
67{
68 struct htx_blk *blk = ctx->blk;
69 struct ist n, v;
70 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +020071
72 if (blk) {
73 char *p;
74
Christopher Faulet47596d32018-10-22 09:17:28 +020075 if (!ctx->value.ptr)
76 goto rescan_hdr;
77 if (full)
78 goto next_blk;
79 v = htx_get_blk_value(htx, blk);
80 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
81 v.len -= (p - v.ptr);
82 v.ptr = p;
83 if (!v.len)
84 goto next_blk;
85 /* Skip comma */
86 if (*(v.ptr) == ',') {
87 v.ptr++;
88 v.len--;
89 }
90
91 goto return_hdr;
92 }
93
Christopher Faulet192c6a22019-06-11 16:32:24 +020094 if (htx_is_empty(htx))
Christopher Faulet47596d32018-10-22 09:17:28 +020095 return 0;
96
Christopher Fauleta3f15502019-05-13 15:27:23 +020097 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +020098 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +020099 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +0100100 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
101 break;
Christopher Faulet47596d32018-10-22 09:17:28 +0200102 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200103 continue;
Christopher Faulet47596d32018-10-22 09:17:28 +0200104 if (name.len) {
105 /* If no name was passed, we want any header. So skip the comparison */
106 n = htx_get_blk_name(htx, blk);
107 if (!isteqi(n, name))
108 goto next_blk;
109 }
110 v = htx_get_blk_value(htx, blk);
111
112 return_hdr:
113 ctx->lws_before = 0;
114 ctx->lws_after = 0;
115 while (v.len && HTTP_IS_LWS(*v.ptr)) {
116 v.ptr++;
117 v.len--;
118 ctx->lws_before++;
119 }
120 if (!full)
121 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
122 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
123 v.len--;
124 ctx->lws_after++;
125 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200126 ctx->blk = blk;
127 ctx->value = v;
128 return 1;
129
130 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200131 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200132 }
133
134 ctx->blk = NULL;
135 ctx->value = ist("");
136 ctx->lws_before = ctx->lws_after = 0;
137 return 0;
138}
139
140/* Adds a header block int the HTX message <htx>, just before the EOH block. It
141 * returns 1 on success, otherwise it returns 0.
142 */
143int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
144{
145 struct htx_blk *blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200146 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200147 enum htx_blk_type type = htx_get_tail_type(htx);
148 int32_t prev;
149
150 blk = htx_add_header(htx, n, v);
151 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200152 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200153
154 if (unlikely(type < HTX_BLK_EOH))
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200155 goto end;
Christopher Faulet47596d32018-10-22 09:17:28 +0200156
157 /* <blk> is the head, swap it iteratively with its predecessor to place
158 * it just before the end-of-header block. So blocks remains ordered. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200159 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200160 struct htx_blk *pblk = htx_get_blk(htx, prev);
161 enum htx_blk_type type = htx_get_blk_type(pblk);
162
163 /* Swap .addr and .info fields */
164 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
165 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
166
167 if (blk->addr == pblk->addr)
168 blk->addr += htx_get_blksz(pblk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200169
170 /* Stop when end-of-header is reached */
171 if (type == HTX_BLK_EOH)
172 break;
173
174 blk = pblk;
175 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200176
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200177 end:
178 sl = http_get_stline(htx);
179 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(n, ist("host"))) {
180 if (!http_update_authority(htx, sl, v))
181 goto fail;
182 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200183 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200184
185 fail:
186 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200187}
188
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100189/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
Christopher Faulet29f17582019-05-23 11:03:26 +0200190 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200191 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100192int 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 +0200193{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200194 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200195
Christopher Faulet29f17582019-05-23 11:03:26 +0200196 blk = htx_get_first_blk(htx);
197 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200198 return 0;
199 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200200}
201
Christopher Faulete010c802018-10-24 10:36:45 +0200202/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
203 * on success, otherwise 0.
204 */
205int http_replace_req_meth(struct htx *htx, const struct ist meth)
206{
207 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200208 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100209 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200210
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100211 if (!sl)
212 return 0;
213
Christopher Faulete010c802018-10-24 10:36:45 +0200214 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100215 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
216 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200217
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100218 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
219 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200220
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100221 /* create the new start line */
222 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
223 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200224}
225
226/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
227 * success, otherwise 0.
228 */
229int http_replace_req_uri(struct htx *htx, const struct ist uri)
230{
231 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200232 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100233 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200234
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100235 if (!sl)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200236 goto fail;
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100237
Christopher Faulete010c802018-10-24 10:36:45 +0200238 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100239 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
240 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200241
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100242 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
243 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200244
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100245 /* create the new start line */
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200246 if (!http_replace_stline(htx, meth, uri, vsn))
247 goto fail;
248
249 sl = http_get_stline(htx);
250 if (!http_update_host(htx, sl, uri))
251 goto fail;
252
253 return 1;
254 fail:
255 return 0;
Christopher Faulete010c802018-10-24 10:36:45 +0200256}
257
258/* Replace the request path in the HTX message <htx> by <path>. The host part
259 * and the query string are preserved. It returns 1 on success, otherwise 0.
260 */
261int http_replace_req_path(struct htx *htx, const struct ist path)
262{
263 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200264 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100265 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200266 size_t plen = 0;
267
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100268 if (!sl)
269 return 0;
270
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271 uri = htx_sl_req_uri(sl);
272 p = http_get_path(uri);
Christopher Faulete010c802018-10-24 10:36:45 +0200273 if (!p.ptr)
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100274 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200275 while (plen < p.len && *(p.ptr + plen) != '?')
276 plen++;
277
278 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100279 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
280 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200281
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100282 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
283 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
284
285 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200286 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
287 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100288 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200289
290 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100291 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200292}
293
294/* Replace the request query-string in the HTX message <htx> by <query>. The
295 * host part and the path are preserved. It returns 1 on success, otherwise
296 * 0.
297 */
298int http_replace_req_query(struct htx *htx, const struct ist query)
299{
300 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200301 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100302 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200303 int offset = 1;
304
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100305 if (!sl)
306 return 0;
307
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100308 uri = htx_sl_req_uri(sl);
309 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200310 while (q.len > 0 && *(q.ptr) != '?') {
311 q.ptr++;
312 q.len--;
313 }
314
315 /* skip the question mark or indicate that we must insert it
316 * (but only if the format string is not empty then).
317 */
318 if (q.len) {
319 q.ptr++;
320 q.len--;
321 }
322 else if (query.len > 1)
323 offset = 0;
324
325 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100326 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
327 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200328
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
330 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200331
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
333 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
334 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200335
336 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100337 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200338}
339
340/* Replace the response status in the HTX message <htx> by <status>. It returns
341 * 1 on success, otherwise 0.
342*/
343int http_replace_res_status(struct htx *htx, const struct ist status)
344{
345 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200346 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100347 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200348
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100349 if (!sl)
350 return 0;
351
Christopher Faulete010c802018-10-24 10:36:45 +0200352 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100353 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
354 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200355
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
357 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200358
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100359 /* create the new start line */
360 sl->info.res.status = strl2ui(status.ptr, status.len);
361 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200362}
363
364/* Replace the response reason in the HTX message <htx> by <reason>. It returns
365 * 1 on success, otherwise 0.
366*/
367int http_replace_res_reason(struct htx *htx, const struct ist reason)
368{
369 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200370 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100371 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200372
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100373 if (!sl)
374 return 0;
375
Christopher Faulete010c802018-10-24 10:36:45 +0200376 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100377 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
378 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200379
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100380 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
381 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200382
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100383 /* create the new start line */
384 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200385}
386
Christopher Faulet47596d32018-10-22 09:17:28 +0200387/* Replaces a part of a header value referenced in the context <ctx> by
388 * <data>. It returns 1 on success, otherwise it returns 0. The context is
389 * updated if necessary.
390 */
391int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
392{
393 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200394 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200395 char *start;
396 struct ist v;
397 uint32_t len, off;
398
399 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200400 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200401
402 v = htx_get_blk_value(htx, blk);
403 start = ctx->value.ptr - ctx->lws_before;
404 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
405 off = start - v.ptr;
406
407 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
408 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200409 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200410
411 v = htx_get_blk_value(htx, blk);
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200412
413 sl = http_get_stline(htx);
414 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
415 struct ist n = htx_get_blk_name(htx, blk);
416
417 if (isteq(n, ist("host"))) {
418 if (!http_update_authority(htx, sl, v))
419 goto fail;
420 ctx->blk = NULL;
421 http_find_header(htx, ist("host"), ctx, 1);
422 blk = ctx->blk;
423 v = htx_get_blk_value(htx, blk);
424 }
425 }
426
Christopher Faulet47596d32018-10-22 09:17:28 +0200427 ctx->blk = blk;
428 ctx->value.ptr = v.ptr + off;
429 ctx->value.len = data.len;
430 ctx->lws_before = ctx->lws_after = 0;
431
432 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200433 fail:
434 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200435}
436
437/* Fully replaces a header referenced in the context <ctx> by the name <name>
438 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
439 * context is updated if necessary.
440 */
441int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
442 const struct ist name, const struct ist value)
443{
444 struct htx_blk *blk = ctx->blk;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200445 struct htx_sl *sl;
Christopher Faulet47596d32018-10-22 09:17:28 +0200446
447 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200448 goto fail;
Christopher Faulet47596d32018-10-22 09:17:28 +0200449
450 blk = htx_replace_header(htx, blk, name, value);
451 if (!blk)
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200452 goto fail;
453
454 sl = http_get_stline(htx);
455 if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(name, ist("host"))) {
456 if (!http_update_authority(htx, sl, value))
457 goto fail;
458 ctx->blk = NULL;
459 http_find_header(htx, ist("host"), ctx, 1);
460 blk = ctx->blk;
461 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200462
463 ctx->blk = blk;
464 ctx->value = ist(NULL);
465 ctx->lws_before = ctx->lws_after = 0;
466
467 return 1;
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200468 fail:
469 return 0;
Christopher Faulet47596d32018-10-22 09:17:28 +0200470}
471
472/* Remove one value of a header. This only works on a <ctx> returned by
473 * http_find_header function. The value is removed, as well as surrounding commas
474 * if any. If the removed value was alone, the whole header is removed. The
475 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
476 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
477 * form that can be handled by http_find_header() to find next occurrence.
478 */
479int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
480{
481 struct htx_blk *blk = ctx->blk;
482 char *start;
483 struct ist v;
484 uint32_t len;
485
486 if (!blk)
487 return 0;
488
489 start = ctx->value.ptr - ctx->lws_before;
490 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
491
492 v = htx_get_blk_value(htx, blk);
493 if (len == v.len) {
494 blk = htx_remove_blk(htx, blk);
Christopher Faulet192c6a22019-06-11 16:32:24 +0200495 if (blk || htx_is_empty(htx)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200496 ctx->blk = blk;
497 ctx->value = ist2(NULL, 0);
498 ctx->lws_before = ctx->lws_after = 0;
499 }
500 else {
501 ctx->blk = htx_get_blk(htx, htx->tail);
502 ctx->value = htx_get_blk_value(htx, ctx->blk);
503 ctx->lws_before = ctx->lws_after = 0;
504 }
505 return 1;
506 }
507
508 /* This was not the only value of this header. We have to remove the
509 * part pointed by ctx->value. If it is the last entry of the list, we
510 * remove the last separator.
511 */
512 if (start == v.ptr) {
513 /* It's the first header part but not the only one. So remove
514 * the comma after it. */
515 len++;
516 }
517 else {
518 /* There is at least one header part before the removed one. So
519 * remove the comma between them. */
520 start--;
521 len++;
522 }
523 /* Update the block content and its len */
524 memmove(start, start+len, v.len-len);
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200525 htx_change_blk_value_len(htx, blk, v.len-len);
Christopher Faulet47596d32018-10-22 09:17:28 +0200526
527 /* Finally update the ctx */
528 ctx->value.ptr = start;
529 ctx->value.len = 0;
530 ctx->lws_before = ctx->lws_after = 0;
531
532 return 1;
533}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200534
Christopher Fauletd7b7a1c2019-10-08 15:24:52 +0200535/* Updates the authority part of the uri with the value <host>. It happens when
536 * the header host is modified. It returns 0 on failure and 1 on success. It is
537 * the caller responsibility to provide the start-line and to be sure the uri
538 * contains an authority. Thus, if no authority is found in the uri, an error is
539 * returned.
540 */
541static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host)
542{
543 struct buffer *temp = get_trash_chunk();
544 struct ist meth, vsn, uri, authority;
545
546 uri = htx_sl_req_uri(sl);
547 authority = http_get_authority(uri, 1);
548 if (!authority.len || isteq(host, authority))
549 return 0;
550
551 /* Start by copying old method and version */
552 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
553 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
554
555 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
556 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
557
558 chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr);
559 chunk_memcat(temp, host.ptr, host.len);
560 chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len));
561 uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */
562
563 return http_replace_stline(htx, meth, uri, vsn);
564
565}
566
567/* Update the header host by extracting the authority of the uri <uri>. flags of
568 * the start-line are also updated accordingly. For orgin-form and asterisk-form
569 * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is
570 * removed from the flags of the start-line. Otherwise, this flag is set and the
571 * authority is used to set the value of the header host. This function returns
572 * 0 on failure and 1 on success.
573*/
574static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri)
575{
576 struct ist authority;
577 struct http_hdr_ctx ctx;
578
579 if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') {
580 // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4)
581 sl->flags &= ~HTX_SL_F_HAS_AUTHORITY;
582 }
583 else {
584 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
585 if (sl->info.req.meth != HTTP_METH_CONNECT) {
586 // absolute-form (RFC7320 #5.3.2)
587 sl->flags |= HTX_SL_F_HAS_SCHM;
588 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
589 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
590
591 authority = http_get_authority(uri, 1);
592 if (!authority.len)
593 goto fail;
594 }
595 else {
596 // authority-form (RFC7320 #5.3.3)
597 authority = uri;
598 }
599
600 /* Replace header host value */
601 ctx.blk = NULL;
602 while (http_find_header(htx, ist("host"), &ctx, 1)) {
603 if (!http_replace_header_value(htx, &ctx, authority))
604 goto fail;
605 }
606
607 }
608 return 1;
609 fail:
610 return 0;
611}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200612
613/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
614 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
615 * performed over the whole headers. Otherwise it must contain a valid header
616 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
617 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
618 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
619 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
620 * -1. The value fetch stops at commas, so this function is suited for use with
621 * list headers.
622 * The return value is 0 if nothing was found, or non-zero otherwise.
623 */
624unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
625 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
626{
627 struct http_hdr_ctx local_ctx;
628 struct ist val_hist[MAX_HDR_HISTORY];
629 unsigned int hist_idx;
630 int found;
631
632 if (!ctx) {
633 local_ctx.blk = NULL;
634 ctx = &local_ctx;
635 }
636
637 if (occ >= 0) {
638 /* search from the beginning */
639 while (http_find_header(htx, hdr, ctx, 0)) {
640 occ--;
641 if (occ <= 0) {
642 *vptr = ctx->value.ptr;
643 *vlen = ctx->value.len;
644 return 1;
645 }
646 }
647 return 0;
648 }
649
650 /* negative occurrence, we scan all the list then walk back */
651 if (-occ > MAX_HDR_HISTORY)
652 return 0;
653
654 found = hist_idx = 0;
655 while (http_find_header(htx, hdr, ctx, 0)) {
656 val_hist[hist_idx] = ctx->value;
657 if (++hist_idx >= MAX_HDR_HISTORY)
658 hist_idx = 0;
659 found++;
660 }
661 if (-occ > found)
662 return 0;
663
664 /* OK now we have the last occurrence in [hist_idx-1], and we need to
665 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
666 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
667 * to remain in the 0..9 range.
668 */
669 hist_idx += occ + MAX_HDR_HISTORY;
670 if (hist_idx >= MAX_HDR_HISTORY)
671 hist_idx -= MAX_HDR_HISTORY;
672 *vptr = val_hist[hist_idx].ptr;
673 *vlen = val_hist[hist_idx].len;
674 return 1;
675}
676
677/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
678 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
679 * performed over the whole headers. Otherwise it must contain a valid header
680 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
681 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
682 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
683 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
684 * -1. This function differs from http_get_hdr() in that it only returns full
685 * line header values and does not stop at commas.
686 * The return value is 0 if nothing was found, or non-zero otherwise.
687 */
688unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
689 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
690{
691 struct http_hdr_ctx local_ctx;
692 struct ist val_hist[MAX_HDR_HISTORY];
693 unsigned int hist_idx;
694 int found;
695
696 if (!ctx) {
697 local_ctx.blk = NULL;
698 ctx = &local_ctx;
699 }
700
701 if (occ >= 0) {
702 /* search from the beginning */
703 while (http_find_header(htx, hdr, ctx, 1)) {
704 occ--;
705 if (occ <= 0) {
706 *vptr = ctx->value.ptr;
707 *vlen = ctx->value.len;
708 return 1;
709 }
710 }
711 return 0;
712 }
713
714 /* negative occurrence, we scan all the list then walk back */
715 if (-occ > MAX_HDR_HISTORY)
716 return 0;
717
718 found = hist_idx = 0;
719 while (http_find_header(htx, hdr, ctx, 1)) {
720 val_hist[hist_idx] = ctx->value;
721 if (++hist_idx >= MAX_HDR_HISTORY)
722 hist_idx = 0;
723 found++;
724 }
725 if (-occ > found)
726 return 0;
727
728 /* OK now we have the last occurrence in [hist_idx-1], and we need to
729 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
730 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
731 * to remain in the 0..9 range.
732 */
733 hist_idx += occ + MAX_HDR_HISTORY;
734 if (hist_idx >= MAX_HDR_HISTORY)
735 hist_idx -= MAX_HDR_HISTORY;
736 *vptr = val_hist[hist_idx].ptr;
737 *vlen = val_hist[hist_idx].len;
738 return 1;
739}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100740
Christopher Faulet90cc4812019-07-22 16:49:30 +0200741int http_str_to_htx(struct buffer *buf, struct ist raw)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100742{
743 struct htx *htx;
744 struct htx_sl *sl;
745 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200746 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100747 union h1_sl h1sl;
748 unsigned int flags = HTX_SL_F_IS_RESP;
749 int ret = 0;
750
Christopher Faulet90cc4812019-07-22 16:49:30 +0200751 b_reset(buf);
752 if (!raw.len) {
753 buf->size = 0;
754 buf->area = malloc(raw.len);
755 return 1;
756 }
757
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100758 buf->size = global.tune.bufsize;
759 buf->area = (char *)malloc(buf->size);
760 if (!buf->area)
761 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100762
763 h1m_init_res(&h1m);
764 h1m.flags |= H1_MF_NO_PHDR;
765 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
766 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
767 if (ret <= 0)
768 goto error;
769
770 if (unlikely(h1sl.st.v.len != 8))
771 goto error;
772 if ((*(h1sl.st.v.ptr + 5) > '1') ||
773 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
774 h1m.flags |= H1_MF_VER_11;
775
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200776 if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102))
777 goto error;
778
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100779 if (h1m.flags & H1_MF_VER_11)
780 flags |= HTX_SL_F_VER_11;
781 if (h1m.flags & H1_MF_XFER_ENC)
782 flags |= HTX_SL_F_XFER_ENC;
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200783 if (h1m.flags & H1_MF_CLEN) {
784 flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
785 if (h1m.body_len == 0)
786 flags |= HTX_SL_F_BODYLESS;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100787 }
Christopher Faulet0d4ce932019-10-16 09:09:04 +0200788 if (h1m.flags & H1_MF_CHNK)
789 goto error; /* Unsupported because there is no body parsing */
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100790
791 htx = htx_from_buf(buf);
792 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
793 if (!sl || !htx_add_all_headers(htx, hdrs))
794 goto error;
795 sl->info.res.status = h1sl.st.status;
796
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200797 while (raw.len > ret) {
798 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
799 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100800 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200801 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100802 }
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200803
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100804 if (!htx_add_endof(htx, HTX_BLK_EOM))
805 goto error;
Christopher Faulet1d5ec092019-06-26 14:23:54 +0200806
Christopher Faulet90cc4812019-07-22 16:49:30 +0200807 return 1;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100808
809error:
810 if (buf->size)
811 free(buf->area);
Christopher Faulet90cc4812019-07-22 16:49:30 +0200812 return 0;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100813}
814
815static int http_htx_init(void)
816{
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100817 struct buffer chk;
818 struct ist raw;
819 int rc;
820 int err_code = 0;
821
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100822 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
823 if (!http_err_msgs[rc]) {
824 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
825 err_code |= ERR_ALERT | ERR_FATAL;
826 continue;
827 }
828
829 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
830 if (!http_str_to_htx(&chk, raw)) {
831 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
832 http_err_codes[rc]);
833 err_code |= ERR_ALERT | ERR_FATAL;
834 }
Christopher Fauletf7346382019-07-17 22:02:08 +0200835 http_err_chunks[rc] = chk;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100836 }
837end:
838 return err_code;
839}
840
Christopher Faulet58857752020-01-15 15:19:50 +0100841static void http_htx_deinit(void)
842{
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100843 struct http_errors *http_errs, *http_errsb;
Christopher Faulet58857752020-01-15 15:19:50 +0100844 struct ebpt_node *node, *next;
845 struct http_error *http_err;
846
847 node = ebpt_first(&http_error_messages);
848 while (node) {
849 next = ebpt_next(node);
850 ebpt_delete(node);
851 http_err = container_of(node, typeof(*http_err), node);
852 chunk_destroy(&http_err->msg);
853 free(node->key);
854 free(http_err);
855 node = next;
856 }
Christopher Faulet35cd81d2020-01-15 11:22:56 +0100857
858 list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) {
859 free(http_errs->conf.file);
860 free(http_errs->id);
861 LIST_DEL(&http_errs->list);
862 free(http_errs);
863 }
Christopher Faulet58857752020-01-15 15:19:50 +0100864}
865
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100866REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);
Christopher Faulet58857752020-01-15 15:19:50 +0100867REGISTER_POST_DEINIT(http_htx_deinit);
Christopher Faulet29f72842019-12-11 15:52:32 +0100868
Christopher Faulet58857752020-01-15 15:19:50 +0100869/* Reads content of the error file <file> and convert it into an HTX message. On
870 * success, the HTX message is returned. On error, NULL is returned and an error
871 * message is written into the <errmsg> buffer.
Christopher Faulet5031ef52020-01-15 11:22:07 +0100872 */
Christopher Faulet58857752020-01-15 15:19:50 +0100873struct buffer *http_load_errorfile(const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +0100874{
Christopher Faulet58857752020-01-15 15:19:50 +0100875 struct buffer *buf = NULL;
876 struct buffer chk;
877 struct ebpt_node *node;
878 struct http_error *http_err;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100879 struct stat stat;
880 char *err = NULL;
881 int errnum, errlen;
882 int fd = -1;
Christopher Faulet58857752020-01-15 15:19:50 +0100883
884 /* already loaded */
885 node = ebis_lookup_len(&http_error_messages, file, strlen(file));
886 if (node) {
887 http_err = container_of(node, typeof(*http_err), node);
888 buf = &http_err->msg;
889 goto out;
890 }
Christopher Faulet5031ef52020-01-15 11:22:07 +0100891
Christopher Faulet58857752020-01-15 15:19:50 +0100892 /* Read the error file content */
Christopher Faulet5031ef52020-01-15 11:22:07 +0100893 fd = open(file, O_RDONLY);
894 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
895 memprintf(errmsg, "error opening file '%s'.", file);
896 goto out;
897 }
898
899 if (stat.st_size <= global.tune.bufsize)
900 errlen = stat.st_size;
901 else {
902 ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n",
903 file, global.tune.bufsize);
904 errlen = global.tune.bufsize;
905 }
906
907 err = malloc(errlen);
908 if (!err) {
909 memprintf(errmsg, "out of memory.");
910 goto out;
911 }
912
913 errnum = read(fd, err, errlen);
914 if (errnum != errlen) {
915 memprintf(errmsg, "error reading file '%s'.", file);
916 goto out;
917 }
918
Christopher Faulet58857752020-01-15 15:19:50 +0100919 /* Create the node corresponding to the error file */
920 http_err = calloc(1, sizeof(*http_err));
921 if (!http_err) {
922 memprintf(errmsg, "out of memory.");
923 goto out;
924 }
925 http_err->node.key = strdup(file);
926 if (!http_err->node.key) {
927 memprintf(errmsg, "out of memory.");
928 goto out;
929 }
930
931 /* Convert the error file into an HTX message */
932 if (!http_str_to_htx(&chk, ist2(err, errlen))) {
Christopher Faulet5031ef52020-01-15 11:22:07 +0100933 memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file);
Christopher Faulet58857752020-01-15 15:19:50 +0100934 free(http_err->node.key);
935 free(http_err);
Christopher Faulet5031ef52020-01-15 11:22:07 +0100936 goto out;
937 }
938
Christopher Faulet58857752020-01-15 15:19:50 +0100939 /* Insert the node in the tree and return the HTX message */
940 http_err->msg = chk;
941 ebis_insert(&http_error_messages, &http_err->node);
942 buf = &http_err->msg;
943
Christopher Faulet5031ef52020-01-15 11:22:07 +0100944 out:
945 if (fd >= 0)
946 close(fd);
947 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +0100948 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +0100949}
950
Christopher Faulet58857752020-01-15 15:19:50 +0100951/* Convert the raw http message <msg> into an HTX message. On sucess, the HTX
952 * message is returned. On error, NULL is returned and an error message is
953 * written into the <errmsg> buffer.
Christopher Fauletbdf65262020-01-16 15:51:59 +0100954 */
Christopher Faulet58857752020-01-15 15:19:50 +0100955struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +0100956{
Christopher Faulet58857752020-01-15 15:19:50 +0100957 struct buffer *buf = NULL;
958 struct buffer chk;
959 struct ebpt_node *node;
960 struct http_error *http_err;
961
962 /* already loaded */
963 node = ebis_lookup_len(&http_error_messages, key, strlen(key));
964 if (node) {
965 http_err = container_of(node, typeof(*http_err), node);
966 buf = &http_err->msg;
967 goto out;
968 }
969 /* Create the node corresponding to the error file */
970 http_err = calloc(1, sizeof(*http_err));
971 if (!http_err) {
972 memprintf(errmsg, "out of memory.");
973 goto out;
974 }
975 http_err->node.key = strdup(key);
976 if (!http_err->node.key) {
977 memprintf(errmsg, "out of memory.");
978 goto out;
979 }
Christopher Fauletbdf65262020-01-16 15:51:59 +0100980
981 /* Convert the error file into an HTX message */
Christopher Faulet58857752020-01-15 15:19:50 +0100982 if (!http_str_to_htx(&chk, msg)) {
Christopher Fauletbdf65262020-01-16 15:51:59 +0100983 memprintf(errmsg, "unable to convert message in HTX.");
Christopher Faulet58857752020-01-15 15:19:50 +0100984 free(http_err->node.key);
985 free(http_err);
Christopher Fauletbdf65262020-01-16 15:51:59 +0100986 goto out;
987 }
Christopher Fauletbdf65262020-01-16 15:51:59 +0100988
Christopher Faulet58857752020-01-15 15:19:50 +0100989 /* Insert the node in the tree and return the HTX message */
990 http_err->msg = chk;
991 ebis_insert(&http_error_messages, &http_err->node);
992 buf = &http_err->msg;
Christopher Fauletbdf65262020-01-16 15:51:59 +0100993 out:
Christopher Faulet58857752020-01-15 15:19:50 +0100994 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +0100995}
996
Christopher Faulet5031ef52020-01-15 11:22:07 +0100997/* This function parses the raw HTTP error file <file> for the status code
Christopher Faulet58857752020-01-15 15:19:50 +0100998 * <status>. It returns NULL if there is any error, otherwise it return the
999 * corresponding HTX message.
Christopher Faulet5031ef52020-01-15 11:22:07 +01001000 */
Christopher Faulet58857752020-01-15 15:19:50 +01001001struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg)
Christopher Faulet5031ef52020-01-15 11:22:07 +01001002{
Christopher Faulet58857752020-01-15 15:19:50 +01001003 struct buffer *buf = NULL;
1004 int rc;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001005
1006 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1007 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001008 buf = http_load_errorfile(file, errmsg);
Christopher Faulet5031ef52020-01-15 11:22:07 +01001009 break;
1010 }
1011 }
1012
1013 if (rc >= HTTP_ERR_SIZE)
1014 memprintf(errmsg, "status code '%d' not handled.", status);
Christopher Faulet58857752020-01-15 15:19:50 +01001015 return buf;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001016}
1017
1018/* This function creates HTX error message corresponding to a redirect message
1019 * for the status code <status>. <url> is used as location url for the
Christopher Faulet58857752020-01-15 15:19:50 +01001020 * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It
1021 * returns NULL if there is any error, otherwise it return the corresponding HTX
1022 * message.
Christopher Fauletbdf65262020-01-16 15:51:59 +01001023 */
Christopher Faulet58857752020-01-15 15:19:50 +01001024struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg)
Christopher Fauletbdf65262020-01-16 15:51:59 +01001025{
Christopher Faulet58857752020-01-15 15:19:50 +01001026 struct buffer *buf = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001027 const char *msg;
Christopher Faulet58857752020-01-15 15:19:50 +01001028 char *key = NULL, *err = NULL;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001029 int rc, errlen;
Christopher Fauletbdf65262020-01-16 15:51:59 +01001030
1031 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
1032 if (http_err_codes[rc] == status) {
Christopher Faulet58857752020-01-15 15:19:50 +01001033 /* Create the error key */
1034 if (!memprintf(&key, "errorloc%d %s", errloc, url)) {
1035 memprintf(errmsg, "out of memory.");
1036 goto out;
1037 }
Christopher Fauletbdf65262020-01-16 15:51:59 +01001038 /* Create the error message */
1039 msg = (errloc == 302 ? HTTP_302 : HTTP_303);
1040 errlen = strlen(msg) + strlen(url) + 5;
1041 err = malloc(errlen);
1042 if (!err) {
1043 memprintf(errmsg, "out of memory.");
1044 goto out;
1045 }
1046 errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url);
1047
1048 /* Load it */
Christopher Faulet58857752020-01-15 15:19:50 +01001049 buf = http_load_errormsg(key, ist2(err, errlen), errmsg);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001050 break;
1051 }
1052 }
1053
1054 if (rc >= HTTP_ERR_SIZE)
1055 memprintf(errmsg, "status code '%d' not handled.", status);
1056out:
Christopher Faulet58857752020-01-15 15:19:50 +01001057 free(key);
Christopher Fauletbdf65262020-01-16 15:51:59 +01001058 free(err);
Christopher Faulet58857752020-01-15 15:19:50 +01001059 return buf;
Christopher Faulet5031ef52020-01-15 11:22:07 +01001060}
1061
Christopher Faulet07f41f72020-01-16 16:16:06 +01001062/* Parses the "errorloc[302|303]" proxy keyword */
1063static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx,
1064 struct proxy *defpx, const char *file, int line,
1065 char **errmsg)
1066{
1067 struct buffer *msg;
1068 int errloc, status, rc, ret = 0;
1069
1070 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1071 ret = 1;
1072 goto out;
1073 }
1074
1075 if (*(args[1]) == 0 || *(args[2]) == 0) {
1076 memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]);
1077 ret = -1;
1078 goto out;
1079 }
1080
1081 status = atol(args[1]);
1082 errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302);
1083 msg = http_parse_errorloc(errloc, status, args[2], errmsg);
1084 if (!msg) {
1085 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1086 ret = -1;
1087 goto out;
1088 }
1089
1090 rc = http_get_status_idx(status);
1091 curpx->errmsg[rc] = msg;
1092
1093 out:
1094 return ret;
1095}
1096
1097
1098/* Parses the "errorfile" proxy keyword */
1099static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx,
1100 struct proxy *defpx, const char *file, int line,
1101 char **errmsg)
1102{
1103 struct buffer *msg;
1104 int status, rc, ret = 0;
1105
1106 if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) {
1107 ret = 1;
1108 goto out;
1109 }
1110
1111 if (*(args[1]) == 0 || *(args[2]) == 0) {
1112 memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]);
1113 ret = -1;
1114 goto out;
1115 }
1116
1117 status = atol(args[1]);
1118 msg = http_parse_errorfile(status, args[2], errmsg);
1119 if (!msg) {
1120 memprintf(errmsg, "%s : %s", args[0], *errmsg);
1121 ret = -1;
1122 goto out;
1123 }
1124
1125 rc = http_get_status_idx(status);
1126 curpx->errmsg[rc] = msg;
1127
1128 out:
1129 return ret;
1130
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001131}
1132
1133/*
1134 * Parse an <http-errors> section.
1135 * Returns the error code, 0 if OK, or any combination of :
1136 * - ERR_ABORT: must abort ASAP
1137 * - ERR_FATAL: we can continue parsing but not start the service
1138 * - ERR_WARN: a warning has been emitted
1139 * - ERR_ALERT: an alert has been emitted
1140 * Only the two first ones can stop processing, the two others are just
1141 * indicators.
1142 */
1143static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm)
1144{
1145 static struct http_errors *curr_errs = NULL;
1146 int err_code = 0;
1147 const char *err;
1148 char *errmsg = NULL;
1149
1150 if (strcmp(args[0], "http-errors") == 0) { /* new errors section */
1151 if (!*args[1]) {
1152 ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum);
1153 err_code |= ERR_ALERT | ERR_ABORT;
1154 goto out;
1155 }
1156
1157 err = invalid_char(args[1]);
1158 if (err) {
1159 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
1160 file, linenum, *err, args[0], args[1]);
1161 err_code |= ERR_ALERT | ERR_FATAL;
1162 }
1163
1164 list_for_each_entry(curr_errs, &http_errors_list, list) {
1165 /* Error if two errors section owns the same name */
1166 if (strcmp(curr_errs->id, args[1]) == 0) {
1167 ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n",
1168 file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line);
1169 err_code |= ERR_ALERT | ERR_FATAL;
1170 }
1171 }
1172
1173 if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) {
1174 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1175 err_code |= ERR_ALERT | ERR_ABORT;
1176 goto out;
1177 }
1178
1179 LIST_ADDQ(&http_errors_list, &curr_errs->list);
1180 curr_errs->id = strdup(args[1]);
1181 curr_errs->conf.file = strdup(file);
1182 curr_errs->conf.line = linenum;
1183 }
1184 else if (!strcmp(args[0], "errorfile")) { /* error message from a file */
1185 struct buffer *msg;
1186 int status, rc;
1187
1188 if (*(args[1]) == 0 || *(args[2]) == 0) {
1189 ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n",
1190 file, linenum, args[0]);
1191 err_code |= ERR_ALERT | ERR_FATAL;
1192 goto out;
1193 }
1194
1195 status = atol(args[1]);
1196 msg = http_parse_errorfile(status, args[2], &errmsg);
1197 if (!msg) {
1198 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1199 err_code |= ERR_ALERT | ERR_FATAL;
1200 goto out;
1201 }
1202 rc = http_get_status_idx(status);
1203 curr_errs->errmsg[rc] = msg;
1204 }
1205 else if (*args[0] != 0) {
1206 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
1207 err_code |= ERR_ALERT | ERR_FATAL;
1208 goto out;
1209 }
1210
1211out:
1212 free(errmsg);
1213 return err_code;
Christopher Faulet07f41f72020-01-16 16:16:06 +01001214}
1215
1216static struct cfg_kw_list cfg_kws = {ILH, {
1217 { CFG_LISTEN, "errorloc", proxy_parse_errorloc },
1218 { CFG_LISTEN, "errorloc302", proxy_parse_errorloc },
1219 { CFG_LISTEN, "errorloc303", proxy_parse_errorloc },
1220 { CFG_LISTEN, "errorfile", proxy_parse_errorfile },
1221 { 0, NULL, NULL },
1222}};
1223
1224INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1225
Christopher Faulet35cd81d2020-01-15 11:22:56 +01001226REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL);
1227
Christopher Faulet29f72842019-12-11 15:52:32 +01001228/************************************************************************/
1229/* HTX sample fetches */
1230/************************************************************************/
1231
1232/* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */
1233static int
1234smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1235{
1236 if (!smp->strm)
1237 return 0;
1238
1239 smp->data.u.sint = !!IS_HTX_STRM(smp->strm);
1240 smp->data.type = SMP_T_BOOL;
1241 return 1;
1242}
1243
1244/* Returns the number of blocks in an HTX message. The channel is chosen
1245 * depending on the sample direction. */
1246static int
1247smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1248{
1249 struct channel *chn;
1250 struct htx *htx;
1251
1252 if (!smp->strm)
1253 return 0;
1254
1255 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1256 htx = smp_prefetch_htx(smp, chn, 0);
1257 if (!htx)
1258 return 0;
1259
1260 smp->data.u.sint = htx_nbblks(htx);
1261 smp->data.type = SMP_T_SINT;
1262 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1263 return 1;
1264}
1265
1266/* Returns the size of an HTX message. The channel is chosen depending on the
1267 * sample direction. */
1268static int
1269smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1270{
1271 struct channel *chn;
1272 struct htx *htx;
1273
1274 if (!smp->strm)
1275 return 0;
1276
1277 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1278 htx = smp_prefetch_htx(smp, chn, 0);
1279 if (!htx)
1280 return 0;
1281
1282 smp->data.u.sint = htx->size;
1283 smp->data.type = SMP_T_SINT;
1284 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1285 return 1;
1286}
1287
1288/* Returns the data size of an HTX message. The channel is chosen depending on the
1289 * sample direction. */
1290static int
1291smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1292{
1293 struct channel *chn;
1294 struct htx *htx;
1295
1296 if (!smp->strm)
1297 return 0;
1298
1299 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1300 htx = smp_prefetch_htx(smp, chn, 0);
1301 if (!htx)
1302 return 0;
1303
1304 smp->data.u.sint = htx->data;
1305 smp->data.type = SMP_T_SINT;
1306 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1307 return 1;
1308}
1309
1310/* Returns the used space (data+meta) of an HTX message. The channel is chosen
1311 * depending on the sample direction. */
1312static int
1313smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1314{
1315 struct channel *chn;
1316 struct htx *htx;
1317
1318 if (!smp->strm)
1319 return 0;
1320
1321 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1322 htx = smp_prefetch_htx(smp, chn, 0);
1323 if (!htx)
1324 return 0;
1325
1326 smp->data.u.sint = htx_used_space(htx);
1327 smp->data.type = SMP_T_SINT;
1328 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1329 return 1;
1330}
1331
1332/* Returns the free space (size-used) of an HTX message. The channel is chosen
1333 * depending on the sample direction. */
1334static int
1335smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1336{
1337 struct channel *chn;
1338 struct htx *htx;
1339
1340 if (!smp->strm)
1341 return 0;
1342
1343 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1344 htx = smp_prefetch_htx(smp, chn, 0);
1345 if (!htx)
1346 return 0;
1347
1348 smp->data.u.sint = htx_free_space(htx);
1349 smp->data.type = SMP_T_SINT;
1350 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1351 return 1;
1352}
1353
1354/* Returns the free space for data (free-sizeof(blk)) of an HTX message. The
1355 * channel is chosen depending on the sample direction. */
1356static int
1357smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1358{
1359 struct channel *chn;
1360 struct htx *htx;
1361
1362 if (!smp->strm)
1363 return 0;
1364
1365 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1366 htx = smp_prefetch_htx(smp, chn, 0);
1367 if (!htx)
1368 return 0;
1369
1370 smp->data.u.sint = htx_free_data_space(htx);
1371 smp->data.type = SMP_T_SINT;
1372 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1373 return 1;
1374}
1375
1376/* Returns 1 if the HTX message contains an EOM block. Otherwise it returns
1377 * 0. Concretely, it only checks the tail. The channel is chosen depending on
1378 * the sample direction. */
1379static int
1380smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1381{
1382 struct channel *chn;
1383 struct htx *htx;
1384
1385 if (!smp->strm)
1386 return 0;
1387
1388 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1389 htx = smp_prefetch_htx(smp, chn, 0);
1390 if (!htx)
1391 return 0;
1392
1393 smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM);
1394 smp->data.type = SMP_T_BOOL;
1395 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1396 return 1;
1397}
1398
1399/* Returns the type of a specific HTX block, if found in the message. Otherwise
1400 * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or
1401 * "head", "tail" or "first". The channel is chosen depending on the sample
1402 * direction. */
1403static int
1404smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1405{
1406 struct channel *chn;
1407 struct htx *htx;
1408 enum htx_blk_type type;
1409 int32_t pos;
1410
1411 if (!smp->strm || !arg_p)
1412 return 0;
1413
1414 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1415 htx = smp_prefetch_htx(smp, chn, 0);
1416 if (!htx)
1417 return 0;
1418
1419 pos = arg_p[0].data.sint;
1420 if (pos == -1)
1421 type = htx_get_head_type(htx);
1422 else if (pos == -2)
1423 type = htx_get_tail_type(htx);
1424 else if (pos == -3)
1425 type = htx_get_first_type(htx);
1426 else
1427 type = ((pos >= htx->head && pos <= htx->tail)
1428 ? htx_get_blk_type(htx_get_blk(htx, pos))
1429 : HTX_BLK_UNUSED);
1430
1431 chunk_initstr(&smp->data.u.str, htx_blk_type_str(type));
1432 smp->data.type = SMP_T_STR;
1433 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1434 return 1;
1435}
1436
1437/* Returns the size of a specific HTX block, if found in the message. Otherwise
1438 * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or
1439 * "first". The channel is chosen depending on the sample direction. */
1440static int
1441smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1442{
1443 struct channel *chn;
1444 struct htx *htx;
1445 struct htx_blk *blk;
1446 int32_t pos;
1447
1448 if (!smp->strm || !arg_p)
1449 return 0;
1450
1451 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1452 htx = smp_prefetch_htx(smp, chn, 0);
1453 if (!htx)
1454 return 0;
1455
1456 pos = arg_p[0].data.sint;
1457 if (pos == -1)
1458 blk = htx_get_head_blk(htx);
1459 else if (pos == -2)
1460 blk = htx_get_tail_blk(htx);
1461 else if (pos == -3)
1462 blk = htx_get_first_blk(htx);
1463 else
1464 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1465
1466 smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0);
1467 smp->data.type = SMP_T_SINT;
1468 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1469 return 1;
1470}
1471
1472/* Returns the start-line if the selected HTX block exists and is a
1473 * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is
1474 * supported or "head", "tail" or "first". The channel is chosen depending on
1475 * the sample direction. */
1476static int
1477smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1478{
1479 struct buffer *temp;
1480 struct channel *chn;
1481 struct htx *htx;
1482 struct htx_blk *blk;
1483 struct htx_sl *sl;
1484 int32_t pos;
1485
1486 if (!smp->strm || !arg_p)
1487 return 0;
1488
1489 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1490 htx = smp_prefetch_htx(smp, chn, 0);
1491 if (!htx)
1492 return 0;
1493
1494 pos = arg_p[0].data.sint;
1495 if (pos == -1)
1496 blk = htx_get_head_blk(htx);
1497 else if (pos == -2)
1498 blk = htx_get_tail_blk(htx);
1499 else if (pos == -3)
1500 blk = htx_get_first_blk(htx);
1501 else
1502 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1503
1504 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) {
1505 smp->data.u.str.size = 0;
1506 smp->data.u.str.area = "";
1507 smp->data.u.str.data = 0;
1508 }
1509 else {
1510 sl = htx_get_blk_ptr(htx, blk);
1511
1512 temp = get_trash_chunk();
1513 chunk_istcat(temp, htx_sl_p1(sl));
1514 temp->area[temp->data++] = ' ';
1515 chunk_istcat(temp, htx_sl_p2(sl));
1516 temp->area[temp->data++] = ' ';
1517 chunk_istcat(temp, htx_sl_p3(sl));
1518
1519 smp->data.u.str = *temp;
1520 }
1521
1522 smp->data.type = SMP_T_STR;
1523 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1524 return 1;
1525}
1526
1527/* Returns the header name if the selected HTX block exists and is a header or a
1528 * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1529 * supported or "head", "tail" or "first". The channel is chosen depending on
1530 * the sample direction. */
1531static int
1532smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1533{
1534 struct channel *chn;
1535 struct htx *htx;
1536 struct htx_blk *blk;
1537 int32_t pos;
1538
1539 if (!smp->strm || !arg_p)
1540 return 0;
1541
1542 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1543 htx = smp_prefetch_htx(smp, chn, 0);
1544 if (!htx)
1545 return 0;
1546
1547 pos = arg_p[0].data.sint;
1548 if (pos == -1)
1549 blk = htx_get_head_blk(htx);
1550 else if (pos == -2)
1551 blk = htx_get_tail_blk(htx);
1552 else if (pos == -3)
1553 blk = htx_get_first_blk(htx);
1554 else
1555 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1556
1557 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1558 smp->data.u.str.size = 0;
1559 smp->data.u.str.area = "";
1560 smp->data.u.str.data = 0;
1561 }
1562 else {
1563 struct ist name = htx_get_blk_name(htx, blk);
1564
1565 chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len);
1566 }
1567 smp->data.type = SMP_T_STR;
1568 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1569 return 1;
1570}
1571
1572/* Returns the header value if the selected HTX block exists and is a header or
1573 * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is
1574 * supported or "head", "tail" or "first". The channel is chosen depending on
1575 * the sample direction. */
1576static int
1577smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
1578{
1579 struct channel *chn;
1580 struct htx *htx;
1581 struct htx_blk *blk;
1582 int32_t pos;
1583
1584 if (!smp->strm || !arg_p)
1585 return 0;
1586
1587 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1588 htx = smp_prefetch_htx(smp, chn, 0);
1589 if (!htx)
1590 return 0;
1591
1592 pos = arg_p[0].data.sint;
1593 if (pos == -1)
1594 blk = htx_get_head_blk(htx);
1595 else if (pos == -2)
1596 blk = htx_get_tail_blk(htx);
1597 else if (pos == -3)
1598 blk = htx_get_first_blk(htx);
1599 else
1600 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1601
1602 if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) {
1603 smp->data.u.str.size = 0;
1604 smp->data.u.str.area = "";
1605 smp->data.u.str.data = 0;
1606 }
1607 else {
1608 struct ist val = htx_get_blk_value(htx, blk);
1609
1610 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1611 }
1612 smp->data.type = SMP_T_STR;
1613 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1614 return 1;
1615}
1616
1617/* Returns the value if the selected HTX block exists and is a data
1618 * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported
1619 * or "head", "tail" or "first". The channel is chosen depending on the sample
1620 * direction. */
1621static int
Christopher Fauletc5db14c2020-01-08 14:51:03 +01001622smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private)
Christopher Faulet29f72842019-12-11 15:52:32 +01001623{
1624 struct channel *chn;
1625 struct htx *htx;
1626 struct htx_blk *blk;
1627 int32_t pos;
1628
1629 if (!smp->strm || !arg_p)
1630 return 0;
1631
1632 chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
1633 htx = smp_prefetch_htx(smp, chn, 0);
1634 if (!htx)
1635 return 0;
1636
1637 pos = arg_p[0].data.sint;
1638 if (pos == -1)
1639 blk = htx_get_head_blk(htx);
1640 else if (pos == -2)
1641 blk = htx_get_tail_blk(htx);
1642 else if (pos == -3)
1643 blk = htx_get_first_blk(htx);
1644 else
1645 blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL);
1646
1647 if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) {
1648 smp->data.u.str.size = 0;
1649 smp->data.u.str.area = "";
1650 smp->data.u.str.data = 0;
1651 }
1652 else {
1653 struct ist val = htx_get_blk_value(htx, blk);
1654
1655 chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len);
1656 }
Christopher Faulet8178e402020-01-08 14:38:58 +01001657 smp->data.type = SMP_T_BIN;
Christopher Faulet29f72842019-12-11 15:52:32 +01001658 smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
1659 return 1;
1660}
1661
1662/* This function is used to validate the arguments passed to any "htx_blk" fetch
1663 * keywords. An argument is expected by these keywords. It must be a positive
1664 * integer or on of the following strings: "head", "tail" or "first". It returns
1665 * 0 on error, and a non-zero value if OK.
1666 */
1667int val_blk_arg(struct arg *arg, char **err_msg)
1668{
1669 if (arg[0].type != ARGT_STR || !arg[0].data.str.data) {
1670 memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)");
1671 return 0;
1672 }
1673 if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) {
1674 free(arg[0].data.str.area);
1675 arg[0].type = ARGT_SINT;
1676 arg[0].data.sint = -1;
1677 }
1678 else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) {
1679 free(arg[0].data.str.area);
1680 arg[0].type = ARGT_SINT;
1681 arg[0].data.sint = -2;
1682 }
1683 else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) {
1684 free(arg[0].data.str.area);
1685 arg[0].type = ARGT_SINT;
1686 arg[0].data.sint = -3;
1687 }
1688 else {
1689 int pos;
1690
1691 for (pos = 0; pos < arg[0].data.str.data; pos++) {
1692 if (!isdigit(arg[0].data.str.area[pos])) {
1693 memprintf(err_msg, "invalid block position");
1694 return 0;
1695 }
1696 }
1697
1698 pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data);
1699 if (pos < 0) {
1700 memprintf(err_msg, "block position must not be negative");
1701 return 0;
1702 }
1703 free(arg[0].data.str.area);
1704 arg[0].type = ARGT_SINT;
1705 arg[0].data.sint = pos;
1706 }
1707
1708 return 1;
1709}
1710
1711
1712/* Note: must not be declared <const> as its list will be overwritten.
1713 * Note: htx sample fetches should only used for developpement purpose.
1714 */
1715static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet01f44452020-01-08 14:23:40 +01001716 { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_L6REQ },
Christopher Faulet29f72842019-12-11 15:52:32 +01001717
Christopher Faulet01f44452020-01-08 14:23:40 +01001718 { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1719 { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1720 { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1721 { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1722 { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1723 { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1724 { "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 +01001725
Christopher Faulet01f44452020-01-08 14:23:40 +01001726 { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1727 { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV},
1728 { "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},
1729 { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV},
1730 { "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 +01001731 { "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 +01001732
1733 { /* END */ },
1734}};
1735
1736INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);