blob: dd1be1715cc79be26ca31ae218c4be924e71a310 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP sample conversion
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
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 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020019#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/arg.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020021#include <haproxy/capture-t.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020022#include <haproxy/chunk.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020024#include <haproxy/pool.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020025#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020026#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020027#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020028#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020029
Damien Claisseae6f1252019-10-30 15:57:28 +000030static int smp_check_http_date_unit(struct arg *args, struct sample_conv *conv,
31 const char *file, int line, char **err)
32{
33 return smp_check_date_unit(args, err);
34}
Willy Tarreau79e57332018-10-02 16:01:16 +020035
36/* takes an UINT value on input supposed to represent the time since EPOCH,
37 * adds an optional offset found in args[0] and emits a string representing
Damien Claisseae6f1252019-10-30 15:57:28 +000038 * the date in RFC-1123/5322 format. If optional unit param in args[1] is
39 * provided, decode timestamp in milliseconds ("ms") or microseconds("us"),
40 * and use relevant output date format.
Willy Tarreau79e57332018-10-02 16:01:16 +020041 */
42static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
43{
44 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
45 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
46 struct buffer *temp;
47 struct tm *tm;
Damien Claisseae6f1252019-10-30 15:57:28 +000048 int sec_frac = 0;
49 time_t curr_date;
Willy Tarreau79e57332018-10-02 16:01:16 +020050
51 /* add offset */
Christopher Faulet72dbcfe2021-01-29 11:25:02 +010052 if (args[0].type == ARGT_SINT)
Damien Claisseae6f1252019-10-30 15:57:28 +000053 smp->data.u.sint += args[0].data.sint;
54
55 /* report in milliseconds */
Christopher Faulet72dbcfe2021-01-29 11:25:02 +010056 if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
Damien Claisseae6f1252019-10-30 15:57:28 +000057 sec_frac = smp->data.u.sint % 1000;
58 smp->data.u.sint /= 1000;
59 }
60 /* report in microseconds */
Christopher Faulet72dbcfe2021-01-29 11:25:02 +010061 else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
Damien Claisseae6f1252019-10-30 15:57:28 +000062 sec_frac = smp->data.u.sint % 1000000;
63 smp->data.u.sint /= 1000000;
64 }
65
66 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
67 curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau79e57332018-10-02 16:01:16 +020068
69 tm = gmtime(&curr_date);
70 if (!tm)
71 return 0;
72
73 temp = get_trash_chunk();
Christopher Faulet72dbcfe2021-01-29 11:25:02 +010074 if (args[1].type == ARGT_SINT && args[1].data.sint != TIME_UNIT_S) {
Damien Claisseae6f1252019-10-30 15:57:28 +000075 temp->data = snprintf(temp->area, temp->size - temp->data,
76 "%s, %02d %s %04d %02d:%02d:%02d.%d GMT",
77 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
78 1900+tm->tm_year,
79 tm->tm_hour, tm->tm_min, tm->tm_sec, sec_frac);
80 } else {
81 temp->data = snprintf(temp->area, temp->size - temp->data,
82 "%s, %02d %s %04d %02d:%02d:%02d GMT",
83 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
84 1900+tm->tm_year,
85 tm->tm_hour, tm->tm_min, tm->tm_sec);
86 }
Willy Tarreau79e57332018-10-02 16:01:16 +020087
88 smp->data.u.str = *temp;
89 smp->data.type = SMP_T_STR;
90 return 1;
91}
92
93/* Arguments: The list of expected value, the number of parts returned and the separator */
94static int sample_conv_q_preferred(const struct arg *args, struct sample *smp, void *private)
95{
96 const char *al = smp->data.u.str.area;
97 const char *end = al + smp->data.u.str.data;
98 const char *token;
99 int toklen;
100 int qvalue;
101 const char *str;
102 const char *w;
103 int best_q = 0;
104
105 /* Set the constant to the sample, because the output of the
106 * function will be peek in the constant configuration string.
107 */
108 smp->flags |= SMP_F_CONST;
109 smp->data.u.str.size = 0;
110 smp->data.u.str.area = "";
111 smp->data.u.str.data = 0;
112
113 /* Parse the accept language */
114 while (1) {
115
116 /* Jump spaces, quit if the end is detected. */
117 while (al < end && isspace((unsigned char)*al))
118 al++;
119 if (al >= end)
120 break;
121
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500122 /* Start of the first word. */
Willy Tarreau79e57332018-10-02 16:01:16 +0200123 token = al;
124
125 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
126 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
127 al++;
128 if (al == token)
129 goto expect_comma;
130
131 /* Length of the token. */
132 toklen = al - token;
133 qvalue = 1000;
134
135 /* Check if the token exists in the list. If the token not exists,
136 * jump to the next token.
137 */
138 str = args[0].data.str.area;
139 w = str;
140 while (1) {
141 if (*str == ';' || *str == '\0') {
142 if (http_language_range_match(token, toklen, w, str - w))
143 goto look_for_q;
144 if (*str == '\0')
145 goto expect_comma;
146 w = str + 1;
147 }
148 str++;
149 }
150 goto expect_comma;
151
152look_for_q:
153
154 /* Jump spaces, quit if the end is detected. */
155 while (al < end && isspace((unsigned char)*al))
156 al++;
157 if (al >= end)
158 goto process_value;
159
160 /* If ',' is found, process the result */
161 if (*al == ',')
162 goto process_value;
163
164 /* If the character is different from ';', look
165 * for the end of the header part in best effort.
166 */
167 if (*al != ';')
168 goto expect_comma;
169
170 /* Assumes that the char is ';', now expect "q=". */
171 al++;
172
173 /* Jump spaces, process value if the end is detected. */
174 while (al < end && isspace((unsigned char)*al))
175 al++;
176 if (al >= end)
177 goto process_value;
178
179 /* Expect 'q'. If no 'q', continue in best effort */
180 if (*al != 'q')
181 goto process_value;
182 al++;
183
184 /* Jump spaces, process value if the end is detected. */
185 while (al < end && isspace((unsigned char)*al))
186 al++;
187 if (al >= end)
188 goto process_value;
189
190 /* Expect '='. If no '=', continue in best effort */
191 if (*al != '=')
192 goto process_value;
193 al++;
194
195 /* Jump spaces, process value if the end is detected. */
196 while (al < end && isspace((unsigned char)*al))
197 al++;
198 if (al >= end)
199 goto process_value;
200
201 /* Parse the q value. */
202 qvalue = http_parse_qvalue(al, &al);
203
204process_value:
205
206 /* If the new q value is the best q value, then store the associated
207 * language in the response. If qvalue is the biggest value (1000),
208 * break the process.
209 */
210 if (qvalue > best_q) {
211 smp->data.u.str.area = (char *)w;
212 smp->data.u.str.data = str - w;
213 if (qvalue >= 1000)
214 break;
215 best_q = qvalue;
216 }
217
218expect_comma:
219
220 /* Expect comma or end. If the end is detected, quit the loop. */
221 while (al < end && *al != ',')
222 al++;
223 if (al >= end)
224 break;
225
226 /* Comma is found, jump it and restart the analyzer. */
227 al++;
228 }
229
230 /* Set default value if required. */
231 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
232 smp->data.u.str.area = args[1].data.str.area;
233 smp->data.u.str.data = args[1].data.str.data;
234 }
235
236 /* Return true only if a matching language was found. */
237 return smp->data.u.str.data != 0;
238}
239
240/* This fetch url-decode any input string. */
241static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
242{
Willy Tarreau62ba9ba2020-04-23 17:54:47 +0200243 int in_form = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200244 int len;
245
Joseph Herlant942eea32018-11-15 13:57:22 -0800246 /* If the constant flag is set or if not size is available at
Willy Tarreau79e57332018-10-02 16:01:16 +0200247 * the end of the buffer, copy the string in other buffer
248 * before decoding.
249 */
250 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
251 struct buffer *str = get_trash_chunk();
252 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
253 smp->data.u.str.area = str->area;
254 smp->data.u.str.size = str->size;
255 smp->flags &= ~SMP_F_CONST;
256 }
257
258 /* Add final \0 required by url_decode(), and convert the input string. */
259 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau62ba9ba2020-04-23 17:54:47 +0200260
Christopher Faulet72dbcfe2021-01-29 11:25:02 +0100261 if (args[0].type == ARGT_SINT)
Willy Tarreau62ba9ba2020-04-23 17:54:47 +0200262 in_form = !!args[0].data.sint;
263
264 len = url_decode(smp->data.u.str.area, in_form);
Willy Tarreau79e57332018-10-02 16:01:16 +0200265 if (len < 0)
266 return 0;
267 smp->data.u.str.data = len;
268 return 1;
269}
270
William Dauchy888b0ae2021-01-06 23:39:50 +0100271/* url-encode types and encode maps */
272enum encode_type {
273 ENC_QUERY = 0,
274};
275long query_encode_map[(256 / 8) / sizeof(long)];
276
277/* Check url-encode type */
278static int sample_conv_url_enc_check(struct arg *arg, struct sample_conv *conv,
279 const char *file, int line, char **err)
280{
281 enum encode_type enc_type;
282
283 if (strcmp(arg->data.str.area, "") == 0)
284 enc_type = ENC_QUERY;
285 else if (strcmp(arg->data.str.area, "query") == 0)
286 enc_type = ENC_QUERY;
287 else {
288 memprintf(err, "Unexpected encode type. "
289 "Allowed value is 'query'");
290 return 0;
291 }
292
293 chunk_destroy(&arg->data.str);
294 arg->type = ARGT_SINT;
295 arg->data.sint = enc_type;
296 return 1;
297}
298
299/* Initializes some url encode data at boot */
300static void sample_conf_url_enc_init()
301{
302 int i;
303
304 memset(query_encode_map, 0, sizeof(query_encode_map));
305 /* use rfc3986 to determine list of characters to keep unchanged for
306 * query string */
307 for (i = 0; i < 256; i++) {
308 if (!((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
309 || (i >= '0' && i <= '9') ||
310 i == '-' || i == '.' || i == '_' || i == '~'))
311 ha_bit_set(i, query_encode_map);
312 }
313}
314
315INITCALL0(STG_PREPARE, sample_conf_url_enc_init);
316
317/* This fetch url-encode any input string. Only support query string for now */
318static int sample_conv_url_enc(const struct arg *args, struct sample *smp, void
319 *private)
320{
321 enum encode_type enc_type;
322 struct buffer *trash = get_trash_chunk();
323 long *encode_map;
324 char *ret;
325
326 enc_type = ENC_QUERY;
Christopher Faulet72dbcfe2021-01-29 11:25:02 +0100327 enc_type = args->data.sint;
William Dauchy888b0ae2021-01-06 23:39:50 +0100328
329 /* Add final \0 required by encode_string() */
330 smp->data.u.str.area[smp->data.u.str.data] = '\0';
331
332 if (enc_type == ENC_QUERY)
333 encode_map = query_encode_map;
334 else
335 return 0;
336
337 ret = encode_string(trash->area, trash->area + trash->size, '%',
338 encode_map, smp->data.u.str.area);
339 if (ret == NULL || *ret != '\0')
340 return 0;
341 trash->data = ret - trash->area;
342 smp->data.u.str = *trash;
343 return 1;
344}
345
Willy Tarreau79e57332018-10-02 16:01:16 +0200346static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
347{
Willy Tarreau55758962020-04-29 11:22:08 +0200348 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +0200349 int idx, i;
350 struct cap_hdr *hdr;
351 int len;
352
Christopher Faulet72dbcfe2021-01-29 11:25:02 +0100353 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +0200354 return 0;
355
Willy Tarreau55758962020-04-29 11:22:08 +0200356 if (!smp->strm)
357 return 0;
358
359 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +0200360 idx = args->data.sint;
361
362 /* Check the availibity of the capture id. */
363 if (idx > fe->nb_req_cap - 1)
364 return 0;
365
366 /* Look for the original configuration. */
367 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
368 hdr != NULL && i != idx ;
369 i--, hdr = hdr->next);
370 if (!hdr)
371 return 0;
372
373 /* check for the memory allocation */
374 if (smp->strm->req_cap[hdr->index] == NULL)
375 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
376 if (smp->strm->req_cap[hdr->index] == NULL)
377 return 0;
378
379 /* Check length. */
380 len = smp->data.u.str.data;
381 if (len > hdr->len)
382 len = hdr->len;
383
384 /* Capture input data. */
385 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
386 smp->strm->req_cap[idx][len] = '\0';
387
388 return 1;
389}
390
391static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
392{
Willy Tarreau55758962020-04-29 11:22:08 +0200393 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +0200394 int idx, i;
395 struct cap_hdr *hdr;
396 int len;
397
Christopher Faulet72dbcfe2021-01-29 11:25:02 +0100398 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 return 0;
400
Willy Tarreau55758962020-04-29 11:22:08 +0200401 if (!smp->strm)
402 return 0;
403
404 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +0200405 idx = args->data.sint;
406
407 /* Check the availibity of the capture id. */
408 if (idx > fe->nb_rsp_cap - 1)
409 return 0;
410
411 /* Look for the original configuration. */
412 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
413 hdr != NULL && i != idx ;
414 i--, hdr = hdr->next);
415 if (!hdr)
416 return 0;
417
418 /* check for the memory allocation */
419 if (smp->strm->res_cap[hdr->index] == NULL)
420 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
421 if (smp->strm->res_cap[hdr->index] == NULL)
422 return 0;
423
424 /* Check length. */
425 len = smp->data.u.str.data;
426 if (len > hdr->len)
427 len = hdr->len;
428
429 /* Capture input data. */
430 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
431 smp->strm->res_cap[idx][len] = '\0';
432
433 return 1;
434}
435
436/************************************************************************/
437/* All supported converter keywords must be declared here. */
438/************************************************************************/
439
440/* Note: must not be declared <const> as its list will be overwritten */
441static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Damien Claisseae6f1252019-10-30 15:57:28 +0000442 { "http_date", sample_conv_http_date, ARG2(0,SINT,STR), smp_check_http_date_unit, SMP_T_SINT, SMP_T_STR},
Willy Tarreau79e57332018-10-02 16:01:16 +0200443 { "language", sample_conv_q_preferred, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
444 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
445 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau62ba9ba2020-04-23 17:54:47 +0200446 { "url_dec", sample_conv_url_dec, ARG1(0,SINT), NULL, SMP_T_STR, SMP_T_STR},
William Dauchy888b0ae2021-01-06 23:39:50 +0100447 { "url_enc", sample_conv_url_enc, ARG1(1,STR), sample_conv_url_enc_check, SMP_T_STR, SMP_T_STR},
Willy Tarreau79e57332018-10-02 16:01:16 +0200448 { NULL, NULL, 0, 0, 0 },
449}};
450
Willy Tarreau0108d902018-11-25 19:14:37 +0100451INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau79e57332018-10-02 16:01:16 +0200452
453/*
454 * Local variables:
455 * c-indent-level: 8
456 * c-basic-offset: 8
457 * End:
458 */