blob: ea224699aa3760310ad1f5987901a71589a29ca5 [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 Zagorac5b5b0622022-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 /*
137 * Here, an HTTP header (which is actually part
138 * of the span context is added to the text_map.
139 *
140 * Before adding, the prefix is removed from the
141 * HTTP header name.
142 */
143 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) {
144 FLT_OT_ERR("failed to add HTTP header data");
145
146 otc_text_map_destroy(&retptr, OTC_TEXT_MAP_FREE_KEY | OTC_TEXT_MAP_FREE_VALUE);
147
148 break;
149 }
150 }
151 }
152 else if (type == HTX_BLK_EOH)
153 break;
154 }
155
156 ot_text_map_show(retptr);
157
158 if ((retptr != NULL) && (retptr->count == 0)) {
159 FLT_OT_DBG(2, "WARNING: no HTTP headers found");
160
161 otc_text_map_destroy(&retptr, OTC_TEXT_MAP_FREE_KEY | OTC_TEXT_MAP_FREE_VALUE);
162 }
163
Miroslav Zagorac5b5b0622022-03-04 09:56:00 +0100164 FLT_OT_RETURN_PTR(retptr);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100165}
166
167
168/***
169 * NAME
170 * flt_ot_http_header_set -
171 *
172 * ARGUMENTS
173 * chn -
174 * prefix -
175 * name -
176 * value -
177 * err -
178 *
179 * DESCRIPTION
180 * This function is very similar to function http_action_set_header(), from
181 * the HAProxy source.
182 *
183 * RETURN VALUE
184 * -
185 */
186int flt_ot_http_header_set(struct channel *chn, const char *prefix, const char *name, const char *value, char **err)
187{
188 struct http_hdr_ctx ctx = { .blk = NULL };
189 struct ist ist_name;
190 struct buffer *buffer = NULL;
191 struct htx *htx;
192 int retval = -1;
193
194 FLT_OT_FUNC("%p, \"%s\", \"%s\", \"%s\", %p:%p", chn, prefix, name, value, FLT_OT_DPTR_ARGS(err));
195
196 if ((chn == NULL) || (!FLT_OT_STR_ISVALID(prefix) && !FLT_OT_STR_ISVALID(name)))
Miroslav Zagorac5b5b0622022-03-04 09:56:00 +0100197 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100198
199 htx = htxbuf(&(chn->buf));
200
201 /*
202 * Very rare (about 1% of cases), htx is empty.
203 * In order to avoid segmentation fault, we exit this function.
204 */
205 if (htx_is_empty(htx)) {
206 FLT_OT_ERR("HTX is empty");
207
Miroslav Zagorac5b5b0622022-03-04 09:56:00 +0100208 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100209 }
210
211 if (!FLT_OT_STR_ISVALID(prefix)) {
212 ist_name.ptr = (char *)name;
213 ist_name.len = strlen(name);
214 }
215 else if (!FLT_OT_STR_ISVALID(name)) {
216 ist_name.ptr = (char *)prefix;
217 ist_name.len = strlen(prefix);
218 }
219 else {
220 buffer = flt_ot_trash_alloc(0, err);
221 if (buffer == NULL)
Miroslav Zagorac5b5b0622022-03-04 09:56:00 +0100222 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100223
224 (void)chunk_printf(buffer, "%s-%s", prefix, name);
225
226 ist_name.ptr = buffer->area;
227 ist_name.len = buffer->data;
228 }
229
230 /* Remove all occurrences of the header. */
231 while (http_find_header(htx, ist(""), &ctx, 1) == 1) {
232 struct ist n = htx_get_blk_name(htx, ctx.blk);
233#ifdef DEBUG_OT
234 struct ist v = htx_get_blk_value(htx, ctx.blk);
235#endif
236
237 /*
238 * If the <name> parameter is not set, then remove all headers
239 * that start with the contents of the <prefix> parameter.
240 */
241 if (!FLT_OT_STR_ISVALID(name))
242 n.len = ist_name.len;
243
244 if (isteqi(n, ist_name))
245 if (http_remove_header(htx, &ctx) == 1)
246 FLT_OT_DBG(3, "HTTP header '%.*s: %.*s' removed", (int)n.len, n.ptr, (int)v.len, v.ptr);
247 }
248
249 /*
250 * If the value pointer has a value of NULL, the HTTP header is not set
251 * after deletion.
252 */
253 if (value == NULL) {
254 /* Do nothing. */
255 }
256 else if (http_add_header(htx, ist_name, ist(value)) == 1) {
257 retval = 0;
258
259 FLT_OT_DBG(3, "HTTP header '%s: %s' added", ist_name.ptr, value);
260 }
261 else {
262 FLT_OT_ERR("failed to set HTTP header '%s: %s'", ist_name.ptr, value);
263 }
264
265 flt_ot_trash_free(&buffer);
266
Miroslav Zagorac5b5b0622022-03-04 09:56:00 +0100267 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100268}
269
270
271/***
272 * NAME
273 * flt_ot_http_headers_remove -
274 *
275 * ARGUMENTS
276 * chn -
277 * prefix -
278 * err -
279 *
280 * DESCRIPTION
281 * -
282 *
283 * RETURN VALUE
284 * -
285 */
286int flt_ot_http_headers_remove(struct channel *chn, const char *prefix, char **err)
287{
288 int retval;
289
290 FLT_OT_FUNC("%p, \"%s\", %p:%p", chn, prefix, FLT_OT_DPTR_ARGS(err));
291
292 retval = flt_ot_http_header_set(chn, prefix, NULL, NULL, err);
293
Miroslav Zagorac5b5b0622022-03-04 09:56:00 +0100294 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100295}
296
297/*
298 * Local variables:
299 * c-indent-level: 8
300 * c-basic-offset: 8
301 * End:
302 *
303 * vi: noexpandtab shiftwidth=8 tabstop=8
304 */