blob: 72b31b76f08c6241bf6fa5602b1a5ff9a81c9f4a [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)
100 FLT_OT_RETURN(retptr);
101
102 htx = htxbuf(&(chn->buf));
103
104 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
105 struct htx_blk *blk = htx_get_blk(htx, pos);
106 enum htx_blk_type type = htx_get_blk_type(blk);
107
108 if (type == HTX_BLK_HDR) {
109 struct ist v, n = htx_get_blk_name(htx, blk);
110
111 if ((prefix_len == 0) || ((n.len >= prefix_len) && (strncasecmp(n.ptr, prefix, len) == 0))) {
112 if (retptr == NULL) {
113 retptr = otc_text_map_new(NULL, 8);
114 if (retptr == NULL) {
115 FLT_OT_ERR("failed to create HTTP header data");
116
117 break;
118 }
119 }
120
121 v = htx_get_blk_value(htx, blk);
122
123 /*
124 * Here, an HTTP header (which is actually part
125 * of the span context is added to the text_map.
126 *
127 * Before adding, the prefix is removed from the
128 * HTTP header name.
129 */
130 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) {
131 FLT_OT_ERR("failed to add HTTP header data");
132
133 otc_text_map_destroy(&retptr, OTC_TEXT_MAP_FREE_KEY | OTC_TEXT_MAP_FREE_VALUE);
134
135 break;
136 }
137 }
138 }
139 else if (type == HTX_BLK_EOH)
140 break;
141 }
142
143 ot_text_map_show(retptr);
144
145 if ((retptr != NULL) && (retptr->count == 0)) {
146 FLT_OT_DBG(2, "WARNING: no HTTP headers found");
147
148 otc_text_map_destroy(&retptr, OTC_TEXT_MAP_FREE_KEY | OTC_TEXT_MAP_FREE_VALUE);
149 }
150
151 FLT_OT_RETURN(retptr);
152}
153
154
155/***
156 * NAME
157 * flt_ot_http_header_set -
158 *
159 * ARGUMENTS
160 * chn -
161 * prefix -
162 * name -
163 * value -
164 * err -
165 *
166 * DESCRIPTION
167 * This function is very similar to function http_action_set_header(), from
168 * the HAProxy source.
169 *
170 * RETURN VALUE
171 * -
172 */
173int flt_ot_http_header_set(struct channel *chn, const char *prefix, const char *name, const char *value, char **err)
174{
175 struct http_hdr_ctx ctx = { .blk = NULL };
176 struct ist ist_name;
177 struct buffer *buffer = NULL;
178 struct htx *htx;
179 int retval = -1;
180
181 FLT_OT_FUNC("%p, \"%s\", \"%s\", \"%s\", %p:%p", chn, prefix, name, value, FLT_OT_DPTR_ARGS(err));
182
183 if ((chn == NULL) || (!FLT_OT_STR_ISVALID(prefix) && !FLT_OT_STR_ISVALID(name)))
184 FLT_OT_RETURN(retval);
185
186 htx = htxbuf(&(chn->buf));
187
188 /*
189 * Very rare (about 1% of cases), htx is empty.
190 * In order to avoid segmentation fault, we exit this function.
191 */
192 if (htx_is_empty(htx)) {
193 FLT_OT_ERR("HTX is empty");
194
195 FLT_OT_RETURN(retval);
196 }
197
198 if (!FLT_OT_STR_ISVALID(prefix)) {
199 ist_name.ptr = (char *)name;
200 ist_name.len = strlen(name);
201 }
202 else if (!FLT_OT_STR_ISVALID(name)) {
203 ist_name.ptr = (char *)prefix;
204 ist_name.len = strlen(prefix);
205 }
206 else {
207 buffer = flt_ot_trash_alloc(0, err);
208 if (buffer == NULL)
209 FLT_OT_RETURN(retval);
210
211 (void)chunk_printf(buffer, "%s-%s", prefix, name);
212
213 ist_name.ptr = buffer->area;
214 ist_name.len = buffer->data;
215 }
216
217 /* Remove all occurrences of the header. */
218 while (http_find_header(htx, ist(""), &ctx, 1) == 1) {
219 struct ist n = htx_get_blk_name(htx, ctx.blk);
220#ifdef DEBUG_OT
221 struct ist v = htx_get_blk_value(htx, ctx.blk);
222#endif
223
224 /*
225 * If the <name> parameter is not set, then remove all headers
226 * that start with the contents of the <prefix> parameter.
227 */
228 if (!FLT_OT_STR_ISVALID(name))
229 n.len = ist_name.len;
230
231 if (isteqi(n, ist_name))
232 if (http_remove_header(htx, &ctx) == 1)
233 FLT_OT_DBG(3, "HTTP header '%.*s: %.*s' removed", (int)n.len, n.ptr, (int)v.len, v.ptr);
234 }
235
236 /*
237 * If the value pointer has a value of NULL, the HTTP header is not set
238 * after deletion.
239 */
240 if (value == NULL) {
241 /* Do nothing. */
242 }
243 else if (http_add_header(htx, ist_name, ist(value)) == 1) {
244 retval = 0;
245
246 FLT_OT_DBG(3, "HTTP header '%s: %s' added", ist_name.ptr, value);
247 }
248 else {
249 FLT_OT_ERR("failed to set HTTP header '%s: %s'", ist_name.ptr, value);
250 }
251
252 flt_ot_trash_free(&buffer);
253
254 FLT_OT_RETURN(retval);
255}
256
257
258/***
259 * NAME
260 * flt_ot_http_headers_remove -
261 *
262 * ARGUMENTS
263 * chn -
264 * prefix -
265 * err -
266 *
267 * DESCRIPTION
268 * -
269 *
270 * RETURN VALUE
271 * -
272 */
273int flt_ot_http_headers_remove(struct channel *chn, const char *prefix, char **err)
274{
275 int retval;
276
277 FLT_OT_FUNC("%p, \"%s\", %p:%p", chn, prefix, FLT_OT_DPTR_ARGS(err));
278
279 retval = flt_ot_http_header_set(chn, prefix, NULL, NULL, err);
280
281 FLT_OT_RETURN(retval);
282}
283
284/*
285 * Local variables:
286 * c-indent-level: 8
287 * c-basic-offset: 8
288 * End:
289 *
290 * vi: noexpandtab shiftwidth=8 tabstop=8
291 */