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