blob: 517bd0de343f912a2d5596e7ca56c0263c63333c [file] [log] [blame]
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001/***
2 * Copyright 2020 HAProxy Technologies
3 *
4 * This file is part of the HAProxy OpenTracing filter.
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 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "include.h"
21
22
23#ifdef DEBUG_OT
24
25/***
26 * NAME
27 * flt_ot_http_headers_dump -
28 *
29 * ARGUMENTS
30 * chn -
31 *
32 * DESCRIPTION
33 * -
34 *
35 * RETURN VALUE
36 * This function does not return a value.
37 */
38void flt_ot_http_headers_dump(const struct channel *chn)
39{
40 const struct htx *htx;
41 int32_t pos;
42
43 FLT_OT_FUNC("%p", chn);
44
45 if (chn == NULL)
46 FLT_OT_RETURN();
47
48 htx = htxbuf(&(chn->buf));
49
50 if (htx_is_empty(htx))
51 FLT_OT_RETURN();
52
53 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
54 struct htx_blk *blk = htx_get_blk(htx, pos);
55 enum htx_blk_type type = htx_get_blk_type(blk);
56
57 if (type == HTX_BLK_HDR) {
58 struct ist n = htx_get_blk_name(htx, blk);
59 struct ist v = htx_get_blk_value(htx, blk);
60
61 FLT_OT_DBG(2, "'%.*s: %.*s'", (int)n.len, n.ptr, (int)v.len, v.ptr);
62 }
63 else if (type == HTX_BLK_EOH)
64 break;
65 }
66
67 FLT_OT_RETURN();
68}
69
70#endif /* DEBUG_OT */
71
72
73/***
74 * NAME
75 * flt_ot_http_headers_get -
76 *
77 * ARGUMENTS
78 * chn -
79 * prefix -
80 * len -
81 * err -
82 *
83 * DESCRIPTION
84 * This function is very similar to function http_action_set_header(), from
85 * the HAProxy source.
86 *
87 * RETURN VALUE
88 * -
89 */
90struct otc_text_map *flt_ot_http_headers_get(struct channel *chn, const char *prefix, size_t len, char **err)
91{
92 const struct htx *htx;
93 size_t prefix_len = (!FLT_OT_STR_ISVALID(prefix) || (len == 0)) ? 0 : (len + 1);
94 int32_t pos;
95 struct otc_text_map *retptr = NULL;
96
97 FLT_OT_FUNC("%p, \"%s\", %zu, %p:%p", chn, prefix, len, FLT_OT_DPTR_ARGS(err));
98
99 if (chn == NULL)
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100100 FLT_OT_RETURN_PTR(retptr);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100101
Miroslav Zagoraca8bdf2b2021-04-14 11:47:28 +0200102 /*
103 * The keyword 'inject' allows you to define the name of the OpenTracing
104 * context without using a prefix. In that case all HTTP headers are
105 * transferred because it is not possible to separate them from the
106 * OpenTracing context (this separation is usually done via a prefix).
107 *
108 * When using the 'extract' keyword, the context name must be specified.
109 * To allow all HTTP headers to be extracted, the first character of
110 * that name must be set to FLT_OT_PARSE_CTX_IGNORE_NAME.
111 */
112 if (FLT_OT_STR_ISVALID(prefix) && (*prefix == FLT_OT_PARSE_CTX_IGNORE_NAME))
113 prefix_len = 0;
114
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100115 htx = htxbuf(&(chn->buf));
116
117 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
118 struct htx_blk *blk = htx_get_blk(htx, pos);
119 enum htx_blk_type type = htx_get_blk_type(blk);
120
121 if (type == HTX_BLK_HDR) {
122 struct ist v, n = htx_get_blk_name(htx, blk);
123
124 if ((prefix_len == 0) || ((n.len >= prefix_len) && (strncasecmp(n.ptr, prefix, len) == 0))) {
125 if (retptr == NULL) {
126 retptr = otc_text_map_new(NULL, 8);
127 if (retptr == NULL) {
128 FLT_OT_ERR("failed to create HTTP header data");
129
130 break;
131 }
132 }
133
134 v = htx_get_blk_value(htx, blk);
135
136 /*
Miroslav Zagorace956c9c2021-09-12 08:15:37 +0200137 * In case the data of the HTTP header is not
138 * specified, v.ptr will have some non-null
139 * value and v.len will be equal to 0. The
140 * otc_text_map_add() function will not
141 * interpret this well. In this case v.ptr
142 * is set to an empty string.
143 */
144 if (v.len == 0)
145 v = ist("");
146
147 /*
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100148 * Here, an HTTP header (which is actually part
149 * of the span context is added to the text_map.
150 *
151 * Before adding, the prefix is removed from the
152 * HTTP header name.
153 */
Miroslav Zagorace956c9c2021-09-12 08:15:37 +0200154 if (otc_text_map_add(retptr, n.ptr + prefix_len, n.len - prefix_len, v.ptr, v.len, OTC_TEXT_MAP_DUP_KEY | OTC_TEXT_MAP_DUP_VALUE) == -1) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100155 FLT_OT_ERR("failed to add HTTP header data");
156
157 otc_text_map_destroy(&retptr, OTC_TEXT_MAP_FREE_KEY | OTC_TEXT_MAP_FREE_VALUE);
158
159 break;
160 }
161 }
162 }
163 else if (type == HTX_BLK_EOH)
164 break;
165 }
166
167 ot_text_map_show(retptr);
168
169 if ((retptr != NULL) && (retptr->count == 0)) {
170 FLT_OT_DBG(2, "WARNING: no HTTP headers found");
171
172 otc_text_map_destroy(&retptr, OTC_TEXT_MAP_FREE_KEY | OTC_TEXT_MAP_FREE_VALUE);
173 }
174
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100175 FLT_OT_RETURN_PTR(retptr);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100176}
177
178
179/***
180 * NAME
181 * flt_ot_http_header_set -
182 *
183 * ARGUMENTS
184 * chn -
185 * prefix -
186 * name -
187 * value -
188 * err -
189 *
190 * DESCRIPTION
191 * This function is very similar to function http_action_set_header(), from
192 * the HAProxy source.
193 *
194 * RETURN VALUE
195 * -
196 */
197int flt_ot_http_header_set(struct channel *chn, const char *prefix, const char *name, const char *value, char **err)
198{
199 struct http_hdr_ctx ctx = { .blk = NULL };
200 struct ist ist_name;
201 struct buffer *buffer = NULL;
202 struct htx *htx;
203 int retval = -1;
204
205 FLT_OT_FUNC("%p, \"%s\", \"%s\", \"%s\", %p:%p", chn, prefix, name, value, FLT_OT_DPTR_ARGS(err));
206
207 if ((chn == NULL) || (!FLT_OT_STR_ISVALID(prefix) && !FLT_OT_STR_ISVALID(name)))
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100208 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100209
210 htx = htxbuf(&(chn->buf));
211
212 /*
213 * Very rare (about 1% of cases), htx is empty.
214 * In order to avoid segmentation fault, we exit this function.
215 */
216 if (htx_is_empty(htx)) {
217 FLT_OT_ERR("HTX is empty");
218
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100219 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100220 }
221
222 if (!FLT_OT_STR_ISVALID(prefix)) {
Tim Duesterhusb113b5c2021-09-15 13:58:44 +0200223 ist_name = ist2((char *)name, strlen(name));
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100224 }
225 else if (!FLT_OT_STR_ISVALID(name)) {
Tim Duesterhusb113b5c2021-09-15 13:58:44 +0200226 ist_name = ist2((char *)prefix, strlen(prefix));
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100227 }
228 else {
229 buffer = flt_ot_trash_alloc(0, err);
230 if (buffer == NULL)
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100231 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100232
233 (void)chunk_printf(buffer, "%s-%s", prefix, name);
234
Tim Duesterhusb113b5c2021-09-15 13:58:44 +0200235 ist_name = ist2(buffer->area, buffer->data);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100236 }
237
238 /* Remove all occurrences of the header. */
239 while (http_find_header(htx, ist(""), &ctx, 1) == 1) {
240 struct ist n = htx_get_blk_name(htx, ctx.blk);
241#ifdef DEBUG_OT
242 struct ist v = htx_get_blk_value(htx, ctx.blk);
243#endif
244
245 /*
246 * If the <name> parameter is not set, then remove all headers
247 * that start with the contents of the <prefix> parameter.
248 */
249 if (!FLT_OT_STR_ISVALID(name))
250 n.len = ist_name.len;
251
252 if (isteqi(n, ist_name))
253 if (http_remove_header(htx, &ctx) == 1)
254 FLT_OT_DBG(3, "HTTP header '%.*s: %.*s' removed", (int)n.len, n.ptr, (int)v.len, v.ptr);
255 }
256
257 /*
258 * If the value pointer has a value of NULL, the HTTP header is not set
259 * after deletion.
260 */
261 if (value == NULL) {
262 /* Do nothing. */
263 }
264 else if (http_add_header(htx, ist_name, ist(value)) == 1) {
265 retval = 0;
266
267 FLT_OT_DBG(3, "HTTP header '%s: %s' added", ist_name.ptr, value);
268 }
269 else {
270 FLT_OT_ERR("failed to set HTTP header '%s: %s'", ist_name.ptr, value);
271 }
272
273 flt_ot_trash_free(&buffer);
274
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100275 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100276}
277
278
279/***
280 * NAME
281 * flt_ot_http_headers_remove -
282 *
283 * ARGUMENTS
284 * chn -
285 * prefix -
286 * err -
287 *
288 * DESCRIPTION
289 * -
290 *
291 * RETURN VALUE
292 * -
293 */
294int flt_ot_http_headers_remove(struct channel *chn, const char *prefix, char **err)
295{
296 int retval;
297
298 FLT_OT_FUNC("%p, \"%s\", %p:%p", chn, prefix, FLT_OT_DPTR_ARGS(err));
299
300 retval = flt_ot_http_header_set(chn, prefix, NULL, NULL, err);
301
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100302 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100303}
304
305/*
306 * Local variables:
307 * c-indent-level: 8
308 * c-basic-offset: 8
309 * End:
310 *
311 * vi: noexpandtab shiftwidth=8 tabstop=8
312 */