Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 19 | #include <haproxy/api.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 20 | #include <haproxy/chunk.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 21 | #include <haproxy/http.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 22 | #include <haproxy/pool.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 23 | #include <haproxy/sample.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 24 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 25 | #include <haproxy/version.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 278161c | 2020-06-04 11:18:28 +0200 | [diff] [blame] | 27 | #include <haproxy/capture-t.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | aa74c4e | 2020-06-04 10:19:23 +0200 | [diff] [blame] | 29 | #include <haproxy/arg.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 30 | #include <proto/stream.h> |
| 31 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 32 | static int smp_check_http_date_unit(struct arg *args, struct sample_conv *conv, |
| 33 | const char *file, int line, char **err) |
| 34 | { |
| 35 | return smp_check_date_unit(args, err); |
| 36 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 37 | |
| 38 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 39 | * adds an optional offset found in args[0] and emits a string representing |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 40 | * the date in RFC-1123/5322 format. If optional unit param in args[1] is |
| 41 | * provided, decode timestamp in milliseconds ("ms") or microseconds("us"), |
| 42 | * and use relevant output date format. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 43 | */ |
| 44 | static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private) |
| 45 | { |
| 46 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
| 47 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 48 | struct buffer *temp; |
| 49 | struct tm *tm; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 50 | int sec_frac = 0; |
| 51 | time_t curr_date; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 52 | |
| 53 | /* add offset */ |
| 54 | if (args && (args[0].type == ARGT_SINT)) |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 55 | smp->data.u.sint += args[0].data.sint; |
| 56 | |
| 57 | /* report in milliseconds */ |
| 58 | if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) { |
| 59 | sec_frac = smp->data.u.sint % 1000; |
| 60 | smp->data.u.sint /= 1000; |
| 61 | } |
| 62 | /* report in microseconds */ |
| 63 | else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) { |
| 64 | sec_frac = smp->data.u.sint % 1000000; |
| 65 | smp->data.u.sint /= 1000000; |
| 66 | } |
| 67 | |
| 68 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
| 69 | curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 70 | |
| 71 | tm = gmtime(&curr_date); |
| 72 | if (!tm) |
| 73 | return 0; |
| 74 | |
| 75 | temp = get_trash_chunk(); |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 76 | if (args && args[1].type == ARGT_SINT && args[1].data.sint != TIME_UNIT_S) { |
| 77 | temp->data = snprintf(temp->area, temp->size - temp->data, |
| 78 | "%s, %02d %s %04d %02d:%02d:%02d.%d GMT", |
| 79 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], |
| 80 | 1900+tm->tm_year, |
| 81 | tm->tm_hour, tm->tm_min, tm->tm_sec, sec_frac); |
| 82 | } else { |
| 83 | temp->data = snprintf(temp->area, temp->size - temp->data, |
| 84 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 85 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], |
| 86 | 1900+tm->tm_year, |
| 87 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 88 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 89 | |
| 90 | smp->data.u.str = *temp; |
| 91 | smp->data.type = SMP_T_STR; |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 96 | static int sample_conv_q_preferred(const struct arg *args, struct sample *smp, void *private) |
| 97 | { |
| 98 | const char *al = smp->data.u.str.area; |
| 99 | const char *end = al + smp->data.u.str.data; |
| 100 | const char *token; |
| 101 | int toklen; |
| 102 | int qvalue; |
| 103 | const char *str; |
| 104 | const char *w; |
| 105 | int best_q = 0; |
| 106 | |
| 107 | /* Set the constant to the sample, because the output of the |
| 108 | * function will be peek in the constant configuration string. |
| 109 | */ |
| 110 | smp->flags |= SMP_F_CONST; |
| 111 | smp->data.u.str.size = 0; |
| 112 | smp->data.u.str.area = ""; |
| 113 | smp->data.u.str.data = 0; |
| 114 | |
| 115 | /* Parse the accept language */ |
| 116 | while (1) { |
| 117 | |
| 118 | /* Jump spaces, quit if the end is detected. */ |
| 119 | while (al < end && isspace((unsigned char)*al)) |
| 120 | al++; |
| 121 | if (al >= end) |
| 122 | break; |
| 123 | |
| 124 | /* Start of the fisrt word. */ |
| 125 | token = al; |
| 126 | |
| 127 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
| 128 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
| 129 | al++; |
| 130 | if (al == token) |
| 131 | goto expect_comma; |
| 132 | |
| 133 | /* Length of the token. */ |
| 134 | toklen = al - token; |
| 135 | qvalue = 1000; |
| 136 | |
| 137 | /* Check if the token exists in the list. If the token not exists, |
| 138 | * jump to the next token. |
| 139 | */ |
| 140 | str = args[0].data.str.area; |
| 141 | w = str; |
| 142 | while (1) { |
| 143 | if (*str == ';' || *str == '\0') { |
| 144 | if (http_language_range_match(token, toklen, w, str - w)) |
| 145 | goto look_for_q; |
| 146 | if (*str == '\0') |
| 147 | goto expect_comma; |
| 148 | w = str + 1; |
| 149 | } |
| 150 | str++; |
| 151 | } |
| 152 | goto expect_comma; |
| 153 | |
| 154 | look_for_q: |
| 155 | |
| 156 | /* Jump spaces, quit if the end is detected. */ |
| 157 | while (al < end && isspace((unsigned char)*al)) |
| 158 | al++; |
| 159 | if (al >= end) |
| 160 | goto process_value; |
| 161 | |
| 162 | /* If ',' is found, process the result */ |
| 163 | if (*al == ',') |
| 164 | goto process_value; |
| 165 | |
| 166 | /* If the character is different from ';', look |
| 167 | * for the end of the header part in best effort. |
| 168 | */ |
| 169 | if (*al != ';') |
| 170 | goto expect_comma; |
| 171 | |
| 172 | /* Assumes that the char is ';', now expect "q=". */ |
| 173 | al++; |
| 174 | |
| 175 | /* Jump spaces, process value if the end is detected. */ |
| 176 | while (al < end && isspace((unsigned char)*al)) |
| 177 | al++; |
| 178 | if (al >= end) |
| 179 | goto process_value; |
| 180 | |
| 181 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 182 | if (*al != 'q') |
| 183 | goto process_value; |
| 184 | al++; |
| 185 | |
| 186 | /* Jump spaces, process value if the end is detected. */ |
| 187 | while (al < end && isspace((unsigned char)*al)) |
| 188 | al++; |
| 189 | if (al >= end) |
| 190 | goto process_value; |
| 191 | |
| 192 | /* Expect '='. If no '=', continue in best effort */ |
| 193 | if (*al != '=') |
| 194 | goto process_value; |
| 195 | al++; |
| 196 | |
| 197 | /* Jump spaces, process value if the end is detected. */ |
| 198 | while (al < end && isspace((unsigned char)*al)) |
| 199 | al++; |
| 200 | if (al >= end) |
| 201 | goto process_value; |
| 202 | |
| 203 | /* Parse the q value. */ |
| 204 | qvalue = http_parse_qvalue(al, &al); |
| 205 | |
| 206 | process_value: |
| 207 | |
| 208 | /* If the new q value is the best q value, then store the associated |
| 209 | * language in the response. If qvalue is the biggest value (1000), |
| 210 | * break the process. |
| 211 | */ |
| 212 | if (qvalue > best_q) { |
| 213 | smp->data.u.str.area = (char *)w; |
| 214 | smp->data.u.str.data = str - w; |
| 215 | if (qvalue >= 1000) |
| 216 | break; |
| 217 | best_q = qvalue; |
| 218 | } |
| 219 | |
| 220 | expect_comma: |
| 221 | |
| 222 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 223 | while (al < end && *al != ',') |
| 224 | al++; |
| 225 | if (al >= end) |
| 226 | break; |
| 227 | |
| 228 | /* Comma is found, jump it and restart the analyzer. */ |
| 229 | al++; |
| 230 | } |
| 231 | |
| 232 | /* Set default value if required. */ |
| 233 | if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) { |
| 234 | smp->data.u.str.area = args[1].data.str.area; |
| 235 | smp->data.u.str.data = args[1].data.str.data; |
| 236 | } |
| 237 | |
| 238 | /* Return true only if a matching language was found. */ |
| 239 | return smp->data.u.str.data != 0; |
| 240 | } |
| 241 | |
| 242 | /* This fetch url-decode any input string. */ |
| 243 | static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private) |
| 244 | { |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 245 | int in_form = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 246 | int len; |
| 247 | |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 248 | /* If the constant flag is set or if not size is available at |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 249 | * the end of the buffer, copy the string in other buffer |
| 250 | * before decoding. |
| 251 | */ |
| 252 | if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) { |
| 253 | struct buffer *str = get_trash_chunk(); |
| 254 | memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data); |
| 255 | smp->data.u.str.area = str->area; |
| 256 | smp->data.u.str.size = str->size; |
| 257 | smp->flags &= ~SMP_F_CONST; |
| 258 | } |
| 259 | |
| 260 | /* Add final \0 required by url_decode(), and convert the input string. */ |
| 261 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 262 | |
| 263 | if (args && (args[0].type == ARGT_SINT)) |
| 264 | in_form = !!args[0].data.sint; |
| 265 | |
| 266 | len = url_decode(smp->data.u.str.area, in_form); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 267 | if (len < 0) |
| 268 | return 0; |
| 269 | smp->data.u.str.data = len; |
| 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 274 | { |
Willy Tarreau | 5575896 | 2020-04-29 11:22:08 +0200 | [diff] [blame] | 275 | struct proxy *fe; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 276 | int idx, i; |
| 277 | struct cap_hdr *hdr; |
| 278 | int len; |
| 279 | |
| 280 | if (!args || args->type != ARGT_SINT) |
| 281 | return 0; |
| 282 | |
Willy Tarreau | 5575896 | 2020-04-29 11:22:08 +0200 | [diff] [blame] | 283 | if (!smp->strm) |
| 284 | return 0; |
| 285 | |
| 286 | fe = strm_fe(smp->strm); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 287 | idx = args->data.sint; |
| 288 | |
| 289 | /* Check the availibity of the capture id. */ |
| 290 | if (idx > fe->nb_req_cap - 1) |
| 291 | return 0; |
| 292 | |
| 293 | /* Look for the original configuration. */ |
| 294 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 295 | hdr != NULL && i != idx ; |
| 296 | i--, hdr = hdr->next); |
| 297 | if (!hdr) |
| 298 | return 0; |
| 299 | |
| 300 | /* check for the memory allocation */ |
| 301 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 302 | smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool); |
| 303 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 304 | return 0; |
| 305 | |
| 306 | /* Check length. */ |
| 307 | len = smp->data.u.str.data; |
| 308 | if (len > hdr->len) |
| 309 | len = hdr->len; |
| 310 | |
| 311 | /* Capture input data. */ |
| 312 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len); |
| 313 | smp->strm->req_cap[idx][len] = '\0'; |
| 314 | |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 319 | { |
Willy Tarreau | 5575896 | 2020-04-29 11:22:08 +0200 | [diff] [blame] | 320 | struct proxy *fe; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 321 | int idx, i; |
| 322 | struct cap_hdr *hdr; |
| 323 | int len; |
| 324 | |
| 325 | if (!args || args->type != ARGT_SINT) |
| 326 | return 0; |
| 327 | |
Willy Tarreau | 5575896 | 2020-04-29 11:22:08 +0200 | [diff] [blame] | 328 | if (!smp->strm) |
| 329 | return 0; |
| 330 | |
| 331 | fe = strm_fe(smp->strm); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 332 | idx = args->data.sint; |
| 333 | |
| 334 | /* Check the availibity of the capture id. */ |
| 335 | if (idx > fe->nb_rsp_cap - 1) |
| 336 | return 0; |
| 337 | |
| 338 | /* Look for the original configuration. */ |
| 339 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 340 | hdr != NULL && i != idx ; |
| 341 | i--, hdr = hdr->next); |
| 342 | if (!hdr) |
| 343 | return 0; |
| 344 | |
| 345 | /* check for the memory allocation */ |
| 346 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 347 | smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool); |
| 348 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 349 | return 0; |
| 350 | |
| 351 | /* Check length. */ |
| 352 | len = smp->data.u.str.data; |
| 353 | if (len > hdr->len) |
| 354 | len = hdr->len; |
| 355 | |
| 356 | /* Capture input data. */ |
| 357 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len); |
| 358 | smp->strm->res_cap[idx][len] = '\0'; |
| 359 | |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | /************************************************************************/ |
| 364 | /* All supported converter keywords must be declared here. */ |
| 365 | /************************************************************************/ |
| 366 | |
| 367 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 368 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 369 | { "http_date", sample_conv_http_date, ARG2(0,SINT,STR), smp_check_http_date_unit, SMP_T_SINT, SMP_T_STR}, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 370 | { "language", sample_conv_q_preferred, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
| 371 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 372 | { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 373 | { "url_dec", sample_conv_url_dec, ARG1(0,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 374 | { NULL, NULL, 0, 0, 0 }, |
| 375 | }}; |
| 376 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 377 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Local variables: |
| 381 | * c-indent-level: 8 |
| 382 | * c-basic-offset: 8 |
| 383 | * End: |
| 384 | */ |