summaryrefslogtreecommitdiffstats
path: root/src/http_conv.c
blob: cf515a877d765e440985b716aabcba9be57076ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * HTTP sample conversion
 *
 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

#include <sys/types.h>

#include <ctype.h>
#include <string.h>
#include <time.h>

#include <haproxy/api.h>
#include <haproxy/arg.h>
#include <haproxy/capture-t.h>
#include <haproxy/chunk.h>
#include <haproxy/http.h>
#include <haproxy/pool.h>
#include <haproxy/sample.h>
#include <haproxy/stream.h>
#include <haproxy/tools.h>
#include <haproxy/version.h>

static int smp_check_http_date_unit(struct arg *args, struct sample_conv *conv,
                                    const char *file, int line, char **err)
{
    return smp_check_date_unit(args, err);
}

/* takes an UINT value on input supposed to represent the time since EPOCH,
 * adds an optional offset found in args[0] and emits a string representing
 * the date in RFC-1123/5322 format. If optional unit param in args[1] is
 * provided, decode timestamp in milliseconds ("ms") or microseconds("us"),
 * and use relevant output date format.
 */
static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
{
	const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
	const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	struct buffer *temp;
	struct tm tm;
	int sec_frac = 0;
	time_t curr_date;

	/* add offset */
	if (args[0].type == ARGT_SINT)
		smp->data.u.sint += args[0].data.sint;

        /* report in milliseconds */
        if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
		sec_frac = smp->data.u.sint % 1000;
                smp->data.u.sint /= 1000;
        }
        /* report in microseconds */
        else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
		sec_frac = smp->data.u.sint % 1000000;
                smp->data.u.sint /= 1000000;
        }

	/* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
	curr_date = smp->data.u.sint & 0x007fffffffffffffLL;

	get_gmtime(curr_date, &tm);

	temp = get_trash_chunk();
	if (args[1].type == ARGT_SINT && args[1].data.sint != TIME_UNIT_S) {
	    temp->data = snprintf(temp->area, temp->size - temp->data,
	                          "%s, %02d %s %04d %02d:%02d:%02d.%d GMT",
			          day[tm.tm_wday], tm.tm_mday, mon[tm.tm_mon],
			          1900+tm.tm_year,
			          tm.tm_hour, tm.tm_min, tm.tm_sec, sec_frac);
	} else {
	    temp->data = snprintf(temp->area, temp->size - temp->data,
	                          "%s, %02d %s %04d %02d:%02d:%02d GMT",
			          day[tm.tm_wday], tm.tm_mday, mon[tm.tm_mon],
			          1900+tm.tm_year,
			          tm.tm_hour, tm.tm_min, tm.tm_sec);
        }

	smp->data.u.str = *temp;
	smp->data.type = SMP_T_STR;
	return 1;
}

/* Arguments: The list of expected value, the number of parts returned and the separator */
static int sample_conv_q_preferred(const struct arg *args, struct sample *smp, void *private)
{
	const char *al = smp->data.u.str.area;
	const char *end = al + smp->data.u.str.data;
	const char *token;
	int toklen;
	int qvalue;
	const char *str;
	const char *w;
	int best_q = 0;

	/* Set the constant to the sample, because the output of the
	 * function will be peek in the constant configuration string.
	 */
	smp->flags |= SMP_F_CONST;
	smp->data.u.str.size = 0;
	smp->data.u.str.area = "";
	smp->data.u.str.data = 0;

	/* Parse the accept language */
	while (1) {

		/* Jump spaces, quit if the end is detected. */
		while (al < end && isspace((unsigned char)*al))
			al++;
		if (al >= end)
			break;

		/* Start of the first word. */
		token = al;

		/* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
		while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
			al++;
		if (al == token)
			goto expect_comma;

		/* Length of the token. */
		toklen = al - token;
		qvalue = 1000;

		/* Check if the token exists in the list. If the token not exists,
		 * jump to the next token.
		 */
		str = args[0].data.str.area;
		w = str;
		while (1) {
			if (*str == ';' || *str == '\0') {
				if (http_language_range_match(token, toklen, w, str - w))
					goto look_for_q;
				if (*str == '\0')
					goto expect_comma;
				w = str + 1;
			}
			str++;
		}
		goto expect_comma;

look_for_q:

		/* Jump spaces, quit if the end is detected. */
		while (al < end && isspace((unsigned char)*al))
			al++;
		if (al >= end)
			goto process_value;

		/* If ',' is found, process the result */
		if (*al == ',')
			goto process_value;

		/* If the character is different from ';', look
		 * for the end of the header part in best effort.
		 */
		if (*al != ';')
			goto expect_comma;

		/* Assumes that the char is ';', now expect "q=". */
		al++;

		/* Jump spaces, process value if the end is detected. */
		while (al < end && isspace((unsigned char)*al))
			al++;
		if (al >= end)
			goto process_value;

		/* Expect 'q'. If no 'q', continue in best effort */
		if (*al != 'q')
			goto process_value;
		al++;

		/* Jump spaces, process value if the end is detected. */
		while (al < end && isspace((unsigned char)*al))
			al++;
		if (al >= end)
			goto process_value;

		/* Expect '='. If no '=', continue in best effort */
		if (*al != '=')
			goto process_value;
		al++;

		/* Jump spaces, process value if the end is detected. */
		while (al < end && isspace((unsigned char)*al))
			al++;
		if (al >= end)
			goto process_value;

		/* Parse the q value. */
		qvalue = http_parse_qvalue(al, &al);

process_value:

		/* If the new q value is the best q value, then store the associated
		 * language in the response. If qvalue is the biggest value (1000),
		 * break the process.
		 */
		if (qvalue > best_q) {
			smp->data.u.str.area = (char *)w;
			smp->data.u.str.data = str - w;
			if (qvalue >= 1000)
				break;
			best_q = qvalue;
		}

expect_comma:

		/* Expect comma or end. If the end is detected, quit the loop. */
		while (al < end && *al != ',')
			al++;
		if (al >= end)
			break;

		/* Comma is found, jump it and restart the analyzer. */
		al++;
	}

	/* Set default value if required. */
	if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
		smp->data.u.str.area = args[1].data.str.area;
		smp->data.u.str.data = args[1].data.str.data;
	}

	/* Return true only if a matching language was found. */
	return smp->data.u.str.data != 0;
}

/* This fetch url-decode any input string. */
static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
{
	int in_form = 0;
	int len;

	/* If the constant flag is set or if not size is available at
	 * the end of the buffer, copy the string in other buffer
	  * before decoding.
	 */
	if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
		struct buffer *str = get_trash_chunk();
		memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
		smp->data.u.str.area = str->area;
		smp->data.u.str.size = str->size;
		smp->flags &= ~SMP_F_CONST;
	}

	/* Add final \0 required by url_decode(), and convert the input string. */
	smp->data.u.str.area[smp->data.u.str.data] = '\0';

	if (args[0].type == ARGT_SINT)
		in_form = !!args[0].data.sint;

	len = url_decode(smp->data.u.str.area, in_form);
	if (len < 0)
		return 0;
	smp->data.u.str.data = len;
	return 1;
}

/* url-encode types and encode maps */
enum encode_type {
	ENC_QUERY = 0,
};
long query_encode_map[(256 / 8) / sizeof(long)];

/* Check url-encode type */
static int sample_conv_url_enc_check(struct arg *arg, struct sample_conv *conv,
				     const char *file, int line, char **err)
{
	enum encode_type enc_type;

	if (strcmp(arg->data.str.area, "") == 0)
		enc_type = ENC_QUERY;
	else if (strcmp(arg->data.str.area, "query") == 0)
		enc_type = ENC_QUERY;
	else {
		memprintf(err, "Unexpected encode type. "
			  "Allowed value is 'query'");
		return 0;
	}

	chunk_destroy(&arg->data.str);
	arg->type = ARGT_SINT;
	arg->data.sint = enc_type;
	return 1;
}

/* Initializes some url encode data at boot */
static void sample_conf_url_enc_init()
{
	int i;

	memset(query_encode_map, 0, sizeof(query_encode_map));
	/* use rfc3986 to determine list of characters to keep unchanged for
	 * query string */
	for (i = 0; i < 256; i++) {
		if (!((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
		    || (i >= '0' && i <= '9') ||
		    i == '-' || i == '.' || i == '_' || i == '~'))
			ha_bit_set(i, query_encode_map);
	}
}

INITCALL0(STG_PREPARE, sample_conf_url_enc_init);

/* This fetch url-encode any input string. Only support query string for now */
static int sample_conv_url_enc(const struct arg *args, struct sample *smp, void
		*private)
{
	enum encode_type enc_type;
	struct buffer *trash = get_trash_chunk();
	long *encode_map;
	char *ret;

	enc_type = ENC_QUERY;
	enc_type = args->data.sint;

	if (enc_type == ENC_QUERY)
		encode_map = query_encode_map;
	else
		return 0;

	ret = encode_chunk(trash->area, trash->area + trash->size, '%',
			   encode_map, &smp->data.u.str);
	if (ret == NULL || *ret != '\0')
		return 0;
	trash->data = ret - trash->area;
	smp->data.u.str = *trash;
	return 1;
}

static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
{
	struct proxy *fe;
	int idx, i;
	struct cap_hdr *hdr;
	int len;

	if (args->type != ARGT_SINT)
		return 0;

	if (!smp->strm)
		return 0;

	fe = strm_fe(smp->strm);
	idx = args->data.sint;

	/* Check the availability of the capture id. */
	if (idx > fe->nb_req_cap - 1)
		return 0;

	/* Look for the original configuration. */
	for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
	     hdr != NULL && i != idx ;
	     i--, hdr = hdr->next);
	if (!hdr)
		return 0;

	/* check for the memory allocation */
	if (smp->strm->req_cap[hdr->index] == NULL)
		smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
	if (smp->strm->req_cap[hdr->index] == NULL)
		return 0;

	/* Check length. */
	len = smp->data.u.str.data;
	if (len > hdr->len)
		len = hdr->len;

	/* Capture input data. */
	memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
	smp->strm->req_cap[idx][len] = '\0';

	return 1;
}

static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
{
	struct proxy *fe;
	int idx, i;
	struct cap_hdr *hdr;
	int len;

	if (args->type != ARGT_SINT)
		return 0;

	if (!smp->strm)
		return 0;

	fe = strm_fe(smp->strm);
	idx = args->data.sint;

	/* Check the availability of the capture id. */
	if (idx > fe->nb_rsp_cap - 1)
		return 0;

	/* Look for the original configuration. */
	for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
	     hdr != NULL && i != idx ;
	     i--, hdr = hdr->next);
	if (!hdr)
		return 0;

	/* check for the memory allocation */
	if (smp->strm->res_cap[hdr->index] == NULL)
		smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
	if (smp->strm->res_cap[hdr->index] == NULL)
		return 0;

	/* Check length. */
	len = smp->data.u.str.data;
	if (len > hdr->len)
		len = hdr->len;

	/* Capture input data. */
	memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
	smp->strm->res_cap[idx][len] = '\0';

	return 1;
}

/************************************************************************/
/*        All supported converter keywords must be declared here.       */
/************************************************************************/

/* Note: must not be declared <const> as its list will be overwritten */
static struct sample_conv_kw_list sample_conv_kws = {ILH, {
	{ "http_date",      sample_conv_http_date,    ARG2(0,SINT,STR),     smp_check_http_date_unit,   SMP_T_SINT, SMP_T_STR},
	{ "language",       sample_conv_q_preferred,  ARG2(1,STR,STR),  NULL,   SMP_T_STR,  SMP_T_STR},
	{ "capture-req",    smp_conv_req_capture,     ARG1(1,SINT),     NULL,   SMP_T_STR,  SMP_T_STR},
	{ "capture-res",    smp_conv_res_capture,     ARG1(1,SINT),     NULL,   SMP_T_STR,  SMP_T_STR},
	{ "url_dec",        sample_conv_url_dec,      ARG1(0,SINT),     NULL,   SMP_T_STR,  SMP_T_STR},
	{ "url_enc",        sample_conv_url_enc,      ARG1(1,STR),      sample_conv_url_enc_check, SMP_T_STR,  SMP_T_STR},
	{ NULL, NULL, 0, 0, 0 },
}};

INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);

/*
 * Local variables:
 *  c-indent-level: 8
 *  c-basic-offset: 8
 * End:
 */