Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 2 | * Sample management functions. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <arpa/inet.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 18 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 19 | #include <types/global.h> |
| 20 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 21 | #include <common/chunk.h> |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 22 | #include <common/hash.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 23 | #include <common/standard.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 24 | #include <common/uri_auth.h> |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 25 | #include <common/base64.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 27 | #include <proto/arg.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 28 | #include <proto/auth.h> |
| 29 | #include <proto/log.h> |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 30 | #include <proto/proto_http.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 31 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 32 | #include <proto/sample.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 33 | #include <proto/stick_table.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 34 | |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 35 | #ifdef USE_51DEGREES |
| 36 | #include <51Degrees.h> |
| 37 | #endif |
| 38 | |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 39 | /* sample type names */ |
| 40 | const char *smp_to_type[SMP_TYPES] = { |
Thierry FOURNIER | 9c627e8 | 2015-06-03 20:12:35 +0200 | [diff] [blame] | 41 | [SMP_T_ANY] = "any", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 42 | [SMP_T_BOOL] = "bool", |
| 43 | [SMP_T_UINT] = "uint", |
| 44 | [SMP_T_SINT] = "sint", |
| 45 | [SMP_T_ADDR] = "addr", |
| 46 | [SMP_T_IPV4] = "ipv4", |
| 47 | [SMP_T_IPV6] = "ipv6", |
| 48 | [SMP_T_STR] = "str", |
| 49 | [SMP_T_BIN] = "bin", |
Thierry FOURNIER | 4c2479e | 2015-06-03 20:12:04 +0200 | [diff] [blame] | 50 | [SMP_T_METH] = "meth", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 53 | /* static sample used in sample_process() when <p> is NULL */ |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 54 | static struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 56 | /* list head of all known sample fetch keywords */ |
| 57 | static struct sample_fetch_kw_list sample_fetches = { |
| 58 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 61 | /* list head of all known sample format conversion keywords */ |
| 62 | static struct sample_conv_kw_list sample_convs = { |
| 63 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 66 | const unsigned int fetch_cap[SMP_SRC_ENTRIES] = { |
| 67 | [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 68 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 69 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 70 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 71 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 72 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 73 | SMP_VAL_FE_LOG_END), |
| 74 | |
| 75 | [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 76 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 77 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 78 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 79 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 80 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 81 | SMP_VAL_FE_LOG_END), |
| 82 | |
| 83 | [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 84 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 85 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 86 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 87 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 88 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 89 | SMP_VAL_FE_LOG_END), |
| 90 | |
| 91 | [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 92 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 93 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 94 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 95 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 96 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 97 | SMP_VAL_FE_LOG_END), |
| 98 | |
| 99 | [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 100 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 101 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 102 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 103 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 104 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 105 | SMP_VAL_FE_LOG_END), |
| 106 | |
| 107 | [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 108 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 109 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 110 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 111 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 112 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 113 | SMP_VAL_FE_LOG_END), |
| 114 | |
| 115 | [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 116 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 117 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 118 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 119 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 120 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 121 | SMP_VAL___________), |
| 122 | |
| 123 | [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 124 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 125 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 126 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 127 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 128 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 129 | SMP_VAL___________), |
| 130 | |
| 131 | [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 132 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 133 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 134 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 135 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 136 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 137 | SMP_VAL_FE_LOG_END), |
| 138 | |
| 139 | [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 140 | SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 141 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 142 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 143 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 144 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 145 | SMP_VAL___________), |
| 146 | |
| 147 | [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 148 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 149 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 150 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 151 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 152 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 153 | SMP_VAL_FE_LOG_END), |
| 154 | |
| 155 | [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 156 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 157 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 158 | SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 159 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 160 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 161 | SMP_VAL_FE_LOG_END), |
| 162 | |
| 163 | [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 164 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 165 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 166 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 167 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 168 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 169 | SMP_VAL_FE_LOG_END), |
| 170 | |
| 171 | [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 172 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 173 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 174 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 175 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 176 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 177 | SMP_VAL_FE_LOG_END), |
| 178 | |
| 179 | [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 180 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 181 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 182 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 183 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 184 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 185 | SMP_VAL___________), |
| 186 | |
| 187 | [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 188 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 189 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 190 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 191 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 192 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 193 | SMP_VAL___________), |
| 194 | |
| 195 | [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 196 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 197 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 198 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 199 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 200 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 201 | SMP_VAL_FE_LOG_END), |
| 202 | |
| 203 | [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 204 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 205 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 206 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 207 | SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 208 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 209 | SMP_VAL___________), |
| 210 | |
| 211 | [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 212 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 213 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 214 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 215 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 216 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 217 | SMP_VAL_FE_LOG_END), |
| 218 | |
| 219 | [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 220 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 221 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 222 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 223 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 224 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 225 | SMP_VAL_FE_LOG_END), |
| 226 | |
| 227 | [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 228 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 229 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 230 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 231 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 232 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 233 | SMP_VAL_FE_LOG_END), |
| 234 | |
| 235 | [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 236 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 237 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 238 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 239 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 240 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 241 | SMP_VAL_FE_LOG_END), |
| 242 | }; |
| 243 | |
| 244 | static const char *fetch_src_names[SMP_SRC_ENTRIES] = { |
| 245 | [SMP_SRC_INTRN] = "internal state", |
| 246 | [SMP_SRC_LISTN] = "listener", |
| 247 | [SMP_SRC_FTEND] = "frontend", |
| 248 | [SMP_SRC_L4CLI] = "client address", |
| 249 | [SMP_SRC_L5CLI] = "client-side connection", |
| 250 | [SMP_SRC_TRACK] = "track counters", |
| 251 | [SMP_SRC_L6REQ] = "request buffer", |
| 252 | [SMP_SRC_HRQHV] = "HTTP request headers", |
| 253 | [SMP_SRC_HRQHP] = "HTTP request", |
| 254 | [SMP_SRC_HRQBO] = "HTTP request body", |
| 255 | [SMP_SRC_BKEND] = "backend", |
| 256 | [SMP_SRC_SERVR] = "server", |
| 257 | [SMP_SRC_L4SRV] = "server address", |
| 258 | [SMP_SRC_L5SRV] = "server-side connection", |
| 259 | [SMP_SRC_L6RES] = "response buffer", |
| 260 | [SMP_SRC_HRSHV] = "HTTP response headers", |
| 261 | [SMP_SRC_HRSHP] = "HTTP response", |
| 262 | [SMP_SRC_HRSBO] = "HTTP response body", |
| 263 | [SMP_SRC_RQFIN] = "request buffer statistics", |
| 264 | [SMP_SRC_RSFIN] = "response buffer statistics", |
| 265 | [SMP_SRC_TXFIN] = "transaction statistics", |
| 266 | [SMP_SRC_SSFIN] = "session statistics", |
| 267 | }; |
| 268 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 269 | static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = { |
| 270 | [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule", |
| 271 | [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule", |
| 272 | [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule", |
| 273 | [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule", |
| 274 | [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule", |
| 275 | [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule", |
| 276 | [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule", |
| 277 | [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule", |
| 278 | [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule", |
| 279 | [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule", |
| 280 | [SMP_CKP_BE_SRV_CON] = "server source selection", |
| 281 | [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule", |
| 282 | [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule", |
| 283 | [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule", |
| 284 | [SMP_CKP_BE_STO_RUL] = "backend stick-store rule", |
| 285 | [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule", |
| 286 | [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule", |
| 287 | [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule", |
| 288 | [SMP_CKP_FE_LOG_END] = "logs", |
| 289 | }; |
| 290 | |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 291 | /* This function returns the type of the data returned by the sample_expr. |
| 292 | * It assumes that the <expr> and all of its converters are properly |
| 293 | * initialized. |
| 294 | */ |
| 295 | inline |
| 296 | int smp_expr_output_type(struct sample_expr *expr) |
| 297 | { |
| 298 | struct sample_conv_expr *smp_expr; |
| 299 | |
| 300 | if (!LIST_ISEMPTY(&expr->conv_exprs)) { |
| 301 | smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list); |
| 302 | return smp_expr->conv->out_type; |
| 303 | } |
| 304 | return expr->fetch->out_type; |
| 305 | } |
| 306 | |
| 307 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 308 | /* fill the trash with a comma-delimited list of source names for the <use> bit |
| 309 | * field which must be composed of a non-null set of SMP_USE_* flags. The return |
| 310 | * value is the pointer to the string in the trash buffer. |
| 311 | */ |
| 312 | const char *sample_src_names(unsigned int use) |
| 313 | { |
| 314 | int bit; |
| 315 | |
| 316 | trash.len = 0; |
| 317 | trash.str[0] = '\0'; |
| 318 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) { |
| 319 | if (!(use & ~((1 << bit) - 1))) |
| 320 | break; /* no more bits */ |
| 321 | |
| 322 | if (!(use & (1 << bit))) |
| 323 | continue; /* bit not set */ |
| 324 | |
| 325 | trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s", |
| 326 | (use & ((1 << bit) - 1)) ? "," : "", |
| 327 | fetch_src_names[bit]); |
| 328 | } |
| 329 | return trash.str; |
| 330 | } |
| 331 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 332 | /* return a pointer to the correct sample checkpoint name, or "unknown" when |
| 333 | * the flags are invalid. Only the lowest bit is used, higher bits are ignored |
| 334 | * if set. |
| 335 | */ |
| 336 | const char *sample_ckp_names(unsigned int use) |
| 337 | { |
| 338 | int bit; |
| 339 | |
| 340 | for (bit = 0; bit < SMP_CKP_ENTRIES; bit++) |
| 341 | if (use & (1 << bit)) |
| 342 | return fetch_ckp_names[bit]; |
| 343 | return "unknown sample check place, please report this bug"; |
| 344 | } |
| 345 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 346 | /* |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 347 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords |
| 348 | * for next parsing sessions. The fetch keywords capabilities are also computed |
| 349 | * from their ->use field. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 350 | */ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 351 | void sample_register_fetches(struct sample_fetch_kw_list *kwl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 352 | { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 353 | struct sample_fetch *sf; |
| 354 | int bit; |
| 355 | |
| 356 | for (sf = kwl->kw; sf->kw != NULL; sf++) { |
| 357 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) |
| 358 | if (sf->use & (1 << bit)) |
| 359 | sf->val |= fetch_cap[bit]; |
| 360 | } |
| 361 | LIST_ADDQ(&sample_fetches.list, &kwl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 365 | * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 366 | * parsing sessions. |
| 367 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 368 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 369 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 370 | LIST_ADDQ(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 374 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 375 | * string of <len> in buffer <kw>. |
| 376 | * |
| 377 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 378 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 379 | { |
| 380 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 381 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 382 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 383 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 384 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 385 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 386 | kwl->kw[index].kw[len] == '\0') |
| 387 | return &kwl->kw[index]; |
| 388 | } |
| 389 | } |
| 390 | return NULL; |
| 391 | } |
| 392 | |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 393 | /* This fucntion browse the list of available saple fetch. <current> is |
| 394 | * the last used sample fetch. If it is the first call, it must set to NULL. |
| 395 | * <idx> is the index of the next sampleèfetch entry. It is used as private |
| 396 | * value. It is useles to initiate it. |
| 397 | * |
| 398 | * It returns always the newt fetch_sample entry, and NULL when the end of |
| 399 | * the list is reached. |
| 400 | */ |
| 401 | struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx) |
| 402 | { |
| 403 | struct sample_fetch_kw_list *kwl; |
| 404 | struct sample_fetch *base; |
| 405 | |
| 406 | if (!current) { |
| 407 | /* Get first kwl entry. */ |
| 408 | kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list); |
| 409 | (*idx) = 0; |
| 410 | } else { |
| 411 | /* Get kwl corresponding to the curret entry. */ |
| 412 | base = current + 1 - (*idx); |
| 413 | kwl = container_of(base, struct sample_fetch_kw_list, kw); |
| 414 | } |
| 415 | |
| 416 | while (1) { |
| 417 | |
| 418 | /* Check if kwl is the last entry. */ |
| 419 | if (&kwl->list == &sample_fetches.list) |
| 420 | return NULL; |
| 421 | |
| 422 | /* idx contain the next keyword. If it is available, return it. */ |
| 423 | if (kwl->kw[*idx].kw) { |
| 424 | (*idx)++; |
| 425 | return &kwl->kw[(*idx)-1]; |
| 426 | } |
| 427 | |
| 428 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 429 | kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list); |
| 430 | |
| 431 | /* Set index to 0, ans do one other loop. */ |
| 432 | (*idx) = 0; |
| 433 | } |
| 434 | } |
| 435 | |
Thierry FOURNIER | 8fd1376 | 2015-03-10 23:56:48 +0100 | [diff] [blame] | 436 | /* This function browses the list of available converters. <current> is |
| 437 | * the last used converter. If it is the first call, it must set to NULL. |
| 438 | * <idx> is the index of the next converter entry. It is used as private |
| 439 | * value. It is useless to initiate it. |
| 440 | * |
| 441 | * It returns always the next sample_conv entry, and NULL when the end of |
| 442 | * the list is reached. |
| 443 | */ |
| 444 | struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx) |
| 445 | { |
| 446 | struct sample_conv_kw_list *kwl; |
| 447 | struct sample_conv *base; |
| 448 | |
| 449 | if (!current) { |
| 450 | /* Get first kwl entry. */ |
| 451 | kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list); |
| 452 | (*idx) = 0; |
| 453 | } else { |
| 454 | /* Get kwl corresponding to the curret entry. */ |
| 455 | base = current + 1 - (*idx); |
| 456 | kwl = container_of(base, struct sample_conv_kw_list, kw); |
| 457 | } |
| 458 | |
| 459 | while (1) { |
| 460 | /* Check if kwl is the last entry. */ |
| 461 | if (&kwl->list == &sample_convs.list) |
| 462 | return NULL; |
| 463 | |
| 464 | /* idx contain the next keyword. If it is available, return it. */ |
| 465 | if (kwl->kw[*idx].kw) { |
| 466 | (*idx)++; |
| 467 | return &kwl->kw[(*idx)-1]; |
| 468 | } |
| 469 | |
| 470 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 471 | kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list); |
| 472 | |
| 473 | /* Set index to 0, ans do one other loop. */ |
| 474 | (*idx) = 0; |
| 475 | } |
| 476 | } |
| 477 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 478 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 479 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 480 | * string of <len> in buffer <kw>. |
| 481 | * |
| 482 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 483 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 484 | { |
| 485 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 486 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 487 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 488 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 489 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 490 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 491 | kwl->kw[index].kw[len] == '\0') |
| 492 | return &kwl->kw[index]; |
| 493 | } |
| 494 | } |
| 495 | return NULL; |
| 496 | } |
| 497 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 498 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 499 | /* Sample casts functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 500 | /* Note: these functions do *NOT* set the output type on the */ |
| 501 | /* sample, the caller is responsible for doing this on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 502 | /******************************************************************/ |
| 503 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 504 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 505 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 506 | smp->data.uint = ntohl(smp->data.ipv4.s_addr); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 507 | smp->type = SMP_T_UINT; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 508 | return 1; |
| 509 | } |
| 510 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 511 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 512 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 513 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 514 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 515 | if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 516 | return 0; |
| 517 | |
| 518 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 519 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 520 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 521 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 522 | |
| 523 | return 1; |
| 524 | } |
| 525 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 526 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 527 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 528 | v4tov6(&smp->data.ipv6, &smp->data.ipv4); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 529 | smp->type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 530 | return 1; |
| 531 | } |
| 532 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 533 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 534 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 535 | struct chunk *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 536 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 537 | if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 538 | return 0; |
| 539 | |
| 540 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 541 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 542 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 543 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 544 | return 1; |
| 545 | } |
| 546 | |
| 547 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 548 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 549 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 550 | return v6tov4(&smp->data.ipv4, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 551 | } |
| 552 | */ |
| 553 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 554 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 555 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 556 | smp->data.ipv4.s_addr = htonl(smp->data.uint); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 557 | smp->type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 558 | return 1; |
| 559 | } |
| 560 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 561 | static int c_str2addr(struct sample *smp) |
| 562 | { |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 563 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 564 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 565 | return 0; |
| 566 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 567 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 568 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 569 | } |
| 570 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 571 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 572 | return 1; |
| 573 | } |
| 574 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 575 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 576 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 577 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 578 | return 0; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 579 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 580 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 581 | return 1; |
| 582 | } |
| 583 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 584 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 585 | { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 586 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 587 | return 0; |
| 588 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 589 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 590 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 593 | /* |
| 594 | * The NULL char always enforces the end of string if it is met. |
| 595 | * Data is never changed, so we can ignore the CONST case |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 596 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 597 | static int c_bin2str(struct sample *smp) |
| 598 | { |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 599 | int i; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 600 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 601 | for (i = 0; i < smp->data.str.len; i++) { |
| 602 | if (!smp->data.str.str[i]) { |
| 603 | smp->data.str.len = i; |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 604 | break; |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 605 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 606 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 607 | return 1; |
| 608 | } |
| 609 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 610 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 611 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 612 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 613 | char *pos; |
| 614 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 615 | pos = ultoa_r(smp->data.uint, trash->str, trash->size); |
Thierry FOURNIER | 0f81144 | 2015-06-06 19:16:52 +0200 | [diff] [blame] | 616 | if (!pos) |
| 617 | return 0; |
| 618 | |
| 619 | trash->size = trash->size - (pos - trash->str); |
| 620 | trash->str = pos; |
| 621 | trash->len = strlen(pos); |
| 622 | smp->data.str = *trash; |
| 623 | smp->type = SMP_T_STR; |
| 624 | smp->flags &= ~SMP_F_CONST; |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | static int c_sint2str(struct sample *smp) |
| 629 | { |
| 630 | struct chunk *trash = get_trash_chunk(); |
| 631 | char *pos; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 632 | |
Thierry FOURNIER | 0f81144 | 2015-06-06 19:16:52 +0200 | [diff] [blame] | 633 | pos = sltoa_r(smp->data.sint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 634 | if (!pos) |
| 635 | return 0; |
| 636 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 637 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 638 | trash->str = pos; |
| 639 | trash->len = strlen(pos); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 640 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 641 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 642 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 643 | return 1; |
| 644 | } |
| 645 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 646 | /* This function duplicates data and removes the flag "const". */ |
| 647 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 648 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 649 | struct chunk *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 650 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 651 | /* If the const flag is not set, we don't need to duplicate the |
| 652 | * pattern as it can be modified in place. |
| 653 | */ |
| 654 | if (!(smp->flags & SMP_F_CONST)) |
| 655 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 656 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 657 | switch (smp->type) { |
| 658 | case SMP_T_BOOL: |
| 659 | case SMP_T_UINT: |
| 660 | case SMP_T_SINT: |
| 661 | case SMP_T_ADDR: |
| 662 | case SMP_T_IPV4: |
| 663 | case SMP_T_IPV6: |
| 664 | /* These type are not const. */ |
| 665 | break; |
| 666 | case SMP_T_STR: |
| 667 | case SMP_T_BIN: |
| 668 | /* Duplicate data. */ |
| 669 | trash = get_trash_chunk(); |
| 670 | trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size; |
| 671 | memcpy(trash->str, smp->data.str.str, trash->len); |
| 672 | smp->data.str = *trash; |
| 673 | break; |
| 674 | default: |
| 675 | /* Other cases are unexpected. */ |
| 676 | return 0; |
| 677 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 678 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 679 | /* remove const flag */ |
| 680 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 681 | return 1; |
| 682 | } |
| 683 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 684 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 685 | { |
| 686 | return 1; |
| 687 | } |
| 688 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 689 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 690 | { |
| 691 | int i; |
| 692 | uint32_t ret = 0; |
| 693 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 694 | if (smp->data.str.len == 0) |
| 695 | return 0; |
| 696 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 697 | for (i = 0; i < smp->data.str.len; i++) { |
| 698 | uint32_t val = smp->data.str.str[i] - '0'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 699 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 700 | if (val > 9) { |
| 701 | if (i == 0) |
| 702 | return 0; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 703 | break; |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 704 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 705 | |
| 706 | ret = ret * 10 + val; |
| 707 | } |
| 708 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 709 | smp->data.uint = ret; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 710 | smp->type = SMP_T_UINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 711 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 712 | return 1; |
| 713 | } |
| 714 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 715 | static int c_str2meth(struct sample *smp) |
| 716 | { |
| 717 | enum http_meth_t meth; |
| 718 | int len; |
| 719 | |
| 720 | meth = find_http_meth(smp->data.str.str, smp->data.str.len); |
| 721 | if (meth == HTTP_METH_OTHER) { |
| 722 | len = smp->data.str.len; |
| 723 | smp->data.meth.str.str = smp->data.str.str; |
| 724 | smp->data.meth.str.len = len; |
| 725 | } |
| 726 | else |
| 727 | smp->flags &= ~SMP_F_CONST; |
| 728 | smp->data.meth.meth = meth; |
| 729 | smp->type = SMP_T_METH; |
| 730 | return 1; |
| 731 | } |
| 732 | |
| 733 | static int c_meth2str(struct sample *smp) |
| 734 | { |
| 735 | int len; |
| 736 | enum http_meth_t meth; |
| 737 | |
| 738 | if (smp->data.meth.meth == HTTP_METH_OTHER) { |
| 739 | /* The method is unknown. Copy the original pointer. */ |
| 740 | len = smp->data.meth.str.len; |
| 741 | smp->data.str.str = smp->data.meth.str.str; |
| 742 | smp->data.str.len = len; |
| 743 | smp->type = SMP_T_STR; |
| 744 | } |
| 745 | else if (smp->data.meth.meth < HTTP_METH_OTHER) { |
| 746 | /* The method is known, copy the pointer containing the string. */ |
| 747 | meth = smp->data.meth.meth; |
| 748 | smp->data.str.str = http_known_methods[meth].name; |
| 749 | smp->data.str.len = http_known_methods[meth].len; |
| 750 | smp->flags |= SMP_F_CONST; |
| 751 | smp->type = SMP_T_STR; |
| 752 | } |
| 753 | else { |
| 754 | /* Unknown method */ |
| 755 | return 0; |
| 756 | } |
| 757 | return 1; |
| 758 | } |
| 759 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 760 | static int c_addr2bin(struct sample *smp) |
| 761 | { |
| 762 | struct chunk *chk = get_trash_chunk(); |
| 763 | |
| 764 | if (smp->type == SMP_T_IPV4) { |
| 765 | chk->len = 4; |
| 766 | memcpy(chk->str, &smp->data.ipv4, chk->len); |
| 767 | } |
| 768 | else if (smp->type == SMP_T_IPV6) { |
| 769 | chk->len = 16; |
| 770 | memcpy(chk->str, &smp->data.ipv6, chk->len); |
| 771 | } |
| 772 | else |
| 773 | return 0; |
| 774 | |
| 775 | smp->data.str = *chk; |
| 776 | smp->type = SMP_T_BIN; |
| 777 | return 1; |
| 778 | } |
| 779 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 780 | static int c_int2bin(struct sample *smp) |
| 781 | { |
| 782 | struct chunk *chk = get_trash_chunk(); |
| 783 | |
| 784 | *(unsigned int *)chk->str = htonl(smp->data.uint); |
| 785 | chk->len = 4; |
| 786 | |
| 787 | smp->data.str = *chk; |
| 788 | smp->type = SMP_T_BIN; |
| 789 | return 1; |
| 790 | } |
| 791 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 792 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 793 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 794 | /* Sample casts matrix: */ |
| 795 | /* sample_casts[from type][to type] */ |
| 796 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 797 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 798 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 799 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | 9c627e8 | 2015-06-03 20:12:35 +0200 | [diff] [blame] | 800 | /* to: ANY BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 801 | /* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, }, |
| 802 | /* BOOL */ { c_none, c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, }, |
| 803 | /* UINT */ { c_none, c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, }, |
| 804 | /* SINT */ { c_none, c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_sint2str, c_int2bin, NULL, }, |
| 805 | /* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
| 806 | /* IPV4 */ { c_none, NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, }, |
| 807 | /* IPV6 */ { c_none, NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, }, |
| 808 | /* STR */ { c_none, c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 809 | /* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 810 | /* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 811 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 812 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 813 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 814 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 815 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 816 | * Returns a pointer on allocated sample expression structure. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 817 | * The caller must have set al->ctx. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 818 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 819 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 820 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 821 | const char *begw; /* beginning of word */ |
| 822 | const char *endw; /* end of word */ |
| 823 | const char *endt; /* end of term */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 824 | struct sample_expr *expr; |
| 825 | struct sample_fetch *fetch; |
| 826 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 827 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 828 | char *fkw = NULL; |
| 829 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 830 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 831 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 832 | begw = str[*idx]; |
| 833 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 834 | |
| 835 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 836 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 837 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 838 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 839 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 840 | /* keep a copy of the current fetch keyword for error reporting */ |
| 841 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 842 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 843 | fetch = find_sample_fetch(begw, endw - begw); |
| 844 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 845 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 846 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 847 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 848 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 849 | endt = endw; |
| 850 | if (*endt == '(') { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 851 | /* look for the end of this term and skip the opening parenthesis */ |
| 852 | endt = ++endw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 853 | while (*endt && *endt != ')') |
| 854 | endt++; |
| 855 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 856 | memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 857 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 858 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 859 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 860 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 861 | /* At this point, we have : |
| 862 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 863 | * - endw : end of the keyword, first character not part of keyword |
| 864 | * nor the opening parenthesis (so first character of args |
| 865 | * if present). |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 866 | * - endt : end of the term (=endw or last parenthesis if args are present) |
| 867 | */ |
| 868 | |
| 869 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 870 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 871 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 872 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 873 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 874 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 875 | expr = calloc(1, sizeof(struct sample_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 876 | if (!expr) |
| 877 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 878 | |
| 879 | LIST_INIT(&(expr->conv_exprs)); |
| 880 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 881 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 882 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 883 | /* Note that we call the argument parser even with an empty string, |
| 884 | * this allows it to automatically create entries for mandatory |
| 885 | * implicit arguments (eg: local proxy name). |
| 886 | */ |
| 887 | al->kw = expr->fetch->kw; |
| 888 | al->conv = NULL; |
| 889 | if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 890 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 891 | goto out_error; |
| 892 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 893 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 894 | if (!expr->arg_p) { |
| 895 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 896 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 897 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 898 | memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 899 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 902 | /* Now process the converters if any. We have two supported syntaxes |
| 903 | * for the converters, which can be combined : |
| 904 | * - comma-delimited list of converters just after the keyword and args ; |
| 905 | * - one converter per keyword |
| 906 | * The combination allows to have each keyword being a comma-delimited |
| 907 | * series of converters. |
| 908 | * |
| 909 | * We want to process the former first, then the latter. For this we start |
| 910 | * from the beginning of the supposed place in the exiting conv chain, which |
| 911 | * starts at the last comma (endt). |
| 912 | */ |
| 913 | |
| 914 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 915 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 916 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 917 | if (*endt == ')') /* skip last closing parenthesis */ |
| 918 | endt++; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 919 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 920 | if (*endt && *endt != ',') { |
| 921 | if (ckw) |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 922 | memprintf(err_msg, "missing comma after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 923 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 924 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 925 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 926 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 927 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 928 | while (*endt == ',') /* then trailing commas */ |
| 929 | endt++; |
| 930 | |
| 931 | begw = endt; /* start of conv keyword */ |
| 932 | |
| 933 | if (!*begw) { |
| 934 | /* none ? skip to next string */ |
| 935 | (*idx)++; |
| 936 | begw = str[*idx]; |
| 937 | if (!begw || !*begw) |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 942 | |
| 943 | free(ckw); |
| 944 | ckw = my_strndup(begw, endw - begw); |
| 945 | |
| 946 | conv = find_sample_conv(begw, endw - begw); |
| 947 | if (!conv) { |
| 948 | /* we found an isolated keyword that we don't know, it's not ours */ |
| 949 | if (begw == str[*idx]) |
| 950 | break; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 951 | memprintf(err_msg, "unknown conv method '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 952 | goto out_error; |
| 953 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 954 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 955 | endt = endw; |
| 956 | if (*endt == '(') { |
| 957 | /* look for the end of this term */ |
| 958 | while (*endt && *endt != ')') |
| 959 | endt++; |
| 960 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 961 | memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 962 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 963 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 967 | memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 968 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 969 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 970 | |
| 971 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 972 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 973 | memprintf(err_msg, "conv method '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 974 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 975 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 976 | |
| 977 | prev_type = conv->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 978 | conv_expr = calloc(1, sizeof(struct sample_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 979 | if (!conv_expr) |
| 980 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 981 | |
| 982 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 983 | conv_expr->conv = conv; |
| 984 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 985 | if (endt != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 986 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 987 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 988 | if (!conv->arg_mask) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 989 | memprintf(err_msg, "conv method '%s' does not support any args", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 990 | goto out_error; |
| 991 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 992 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 993 | al->kw = expr->fetch->kw; |
| 994 | al->conv = conv_expr->conv->kw; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 995 | if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 996 | memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 997 | goto out_error; |
| 998 | } |
| 999 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 1000 | if (!conv_expr->arg_p) |
| 1001 | conv_expr->arg_p = empty_arg_list; |
| 1002 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 1003 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 1004 | memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1005 | goto out_error; |
| 1006 | } |
| 1007 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1008 | else if (ARGM(conv->arg_mask)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 1009 | memprintf(err_msg, "missing args for conv method '%s'", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1010 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1011 | } |
| 1012 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1013 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1014 | out: |
| 1015 | free(fkw); |
| 1016 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1017 | return expr; |
| 1018 | |
| 1019 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1020 | /* TODO: prune_sample_expr(expr); */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1021 | expr = NULL; |
| 1022 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1026 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1027 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1028 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 1029 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1030 | * If <p> is not null, function returns results in structure pointed by <p>. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1031 | * If <p> is null, functions returns a pointer on a static sample structure. |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1032 | * |
| 1033 | * Note: the fetch functions are required to properly set the return type. The |
| 1034 | * conversion functions must do so too. However the cast functions do not need |
| 1035 | * to since they're made to cast mutiple types according to what is required. |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1036 | * |
| 1037 | * The caller may indicate in <opt> if it considers the result final or not. |
| 1038 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 1039 | * if the result is stable or not, according to the following table : |
| 1040 | * |
| 1041 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1042 | * NULL 0 * Not present and will never be (eg: header) |
| 1043 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 1044 | * NULL 1 1 Not present yet, will not change anymore |
| 1045 | * smp 0 * Present and will not change (eg: header) |
| 1046 | * smp 1 0 Present, may change (eg: request length) |
| 1047 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1048 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1049 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 1050 | struct stream *strm, unsigned int opt, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1051 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1052 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1053 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1054 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1055 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 1056 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1057 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1058 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1059 | |
Thierry FOURNIER | 6879ad3 | 2015-05-11 11:54:58 +0200 | [diff] [blame] | 1060 | p->px = px; |
| 1061 | p->sess = sess; |
| 1062 | p->strm = strm; |
Thierry FOURNIER | 1d33b88 | 2015-05-11 15:25:29 +0200 | [diff] [blame] | 1063 | p->opt = opt; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1064 | if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1065 | return NULL; |
| 1066 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1067 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1068 | /* we want to ensure that p->type can be casted into |
| 1069 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 1070 | * - NULL => not castable. |
| 1071 | * - c_none => nothing to do (let's optimize it) |
| 1072 | * - other => apply cast and prepare to fail |
| 1073 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1074 | if (!sample_casts[p->type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1075 | return NULL; |
| 1076 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1077 | if (sample_casts[p->type][conv_expr->conv->in_type] != c_none && |
| 1078 | !sample_casts[p->type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1079 | return NULL; |
| 1080 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1081 | /* OK cast succeeded */ |
| 1082 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1083 | if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1084 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1085 | } |
| 1086 | return p; |
| 1087 | } |
| 1088 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1089 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1090 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
| 1091 | * errors or 0 if everything is fine. |
| 1092 | */ |
| 1093 | int smp_resolve_args(struct proxy *p) |
| 1094 | { |
| 1095 | struct arg_list *cur, *bak; |
| 1096 | const char *ctx, *where; |
| 1097 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 1098 | struct userlist *ul; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1099 | struct my_regex *reg; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1100 | struct arg *arg; |
| 1101 | int cfgerr = 0; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1102 | int rflags; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1103 | |
| 1104 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 1105 | struct proxy *px; |
| 1106 | struct server *srv; |
| 1107 | char *pname, *sname; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1108 | char *err; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1109 | |
| 1110 | arg = cur->arg; |
| 1111 | |
| 1112 | /* prepare output messages */ |
| 1113 | conv_pre = conv_pos = conv_ctx = ""; |
| 1114 | if (cur->conv) { |
| 1115 | conv_ctx = cur->conv; |
| 1116 | conv_pre = "conversion keyword '"; |
| 1117 | conv_pos = "' for "; |
| 1118 | } |
| 1119 | |
| 1120 | where = "in"; |
| 1121 | ctx = "sample fetch keyword"; |
| 1122 | switch (cur->ctx) { |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1123 | case ARGC_STK: where = "in stick rule in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1124 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1125 | case ARGC_LOG: where = "in log-format string in"; break; |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 1126 | case ARGC_HRQ: where = "in http-request header format string in"; break; |
| 1127 | case ARGC_HRS: where = "in http-response header format string in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1128 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 1129 | case ARGC_RDR: where = "in redirect format string in"; break; |
Willy Tarreau | 3a4ac42 | 2014-06-13 16:17:14 +0200 | [diff] [blame] | 1130 | case ARGC_CAP: where = "in capture rule in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1131 | case ARGC_ACL: ctx = "ACL keyword"; break; |
| 1132 | } |
| 1133 | |
| 1134 | /* set a few default settings */ |
| 1135 | px = p; |
| 1136 | pname = p->id; |
| 1137 | |
| 1138 | switch (arg->type) { |
| 1139 | case ARGT_SRV: |
| 1140 | if (!arg->data.str.len) { |
| 1141 | Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1142 | cur->file, cur->line, |
| 1143 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1144 | cfgerr++; |
| 1145 | continue; |
| 1146 | } |
| 1147 | |
| 1148 | /* we support two formats : "bck/srv" and "srv" */ |
| 1149 | sname = strrchr(arg->data.str.str, '/'); |
| 1150 | |
| 1151 | if (sname) { |
| 1152 | *sname++ = '\0'; |
| 1153 | pname = arg->data.str.str; |
| 1154 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1155 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1156 | if (!px) { |
| 1157 | Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1158 | cur->file, cur->line, pname, |
| 1159 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1160 | cfgerr++; |
| 1161 | break; |
| 1162 | } |
| 1163 | } |
| 1164 | else |
| 1165 | sname = arg->data.str.str; |
| 1166 | |
| 1167 | srv = findserver(px, sname); |
| 1168 | if (!srv) { |
| 1169 | Alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1170 | cur->file, cur->line, sname, pname, |
| 1171 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1172 | cfgerr++; |
| 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | free(arg->data.str.str); |
| 1177 | arg->data.str.str = NULL; |
| 1178 | arg->unresolved = 0; |
| 1179 | arg->data.srv = srv; |
| 1180 | break; |
| 1181 | |
| 1182 | case ARGT_FE: |
| 1183 | if (arg->data.str.len) { |
| 1184 | pname = arg->data.str.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1185 | px = proxy_fe_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | if (!px) { |
| 1189 | Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1190 | cur->file, cur->line, pname, |
| 1191 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1192 | cfgerr++; |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | if (!(px->cap & PR_CAP_FE)) { |
| 1197 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n", |
| 1198 | cur->file, cur->line, pname, |
| 1199 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1200 | cfgerr++; |
| 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | free(arg->data.str.str); |
| 1205 | arg->data.str.str = NULL; |
| 1206 | arg->unresolved = 0; |
| 1207 | arg->data.prx = px; |
| 1208 | break; |
| 1209 | |
| 1210 | case ARGT_BE: |
| 1211 | if (arg->data.str.len) { |
| 1212 | pname = arg->data.str.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1213 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | if (!px) { |
| 1217 | Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1218 | cur->file, cur->line, pname, |
| 1219 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1220 | cfgerr++; |
| 1221 | break; |
| 1222 | } |
| 1223 | |
| 1224 | if (!(px->cap & PR_CAP_BE)) { |
| 1225 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n", |
| 1226 | cur->file, cur->line, pname, |
| 1227 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1228 | cfgerr++; |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | free(arg->data.str.str); |
| 1233 | arg->data.str.str = NULL; |
| 1234 | arg->unresolved = 0; |
| 1235 | arg->data.prx = px; |
| 1236 | break; |
| 1237 | |
| 1238 | case ARGT_TAB: |
| 1239 | if (arg->data.str.len) { |
| 1240 | pname = arg->data.str.str; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 1241 | px = proxy_tbl_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | if (!px) { |
| 1245 | Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1246 | cur->file, cur->line, pname, |
| 1247 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1248 | cfgerr++; |
| 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | if (!px->table.size) { |
| 1253 | Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1254 | cur->file, cur->line, pname, |
| 1255 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1256 | cfgerr++; |
| 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | free(arg->data.str.str); |
| 1261 | arg->data.str.str = NULL; |
| 1262 | arg->unresolved = 0; |
| 1263 | arg->data.prx = px; |
| 1264 | break; |
| 1265 | |
| 1266 | case ARGT_USR: |
| 1267 | if (!arg->data.str.len) { |
| 1268 | Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1269 | cur->file, cur->line, |
| 1270 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1271 | cfgerr++; |
| 1272 | break; |
| 1273 | } |
| 1274 | |
| 1275 | if (p->uri_auth && p->uri_auth->userlist && |
| 1276 | !strcmp(p->uri_auth->userlist->name, arg->data.str.str)) |
| 1277 | ul = p->uri_auth->userlist; |
| 1278 | else |
| 1279 | ul = auth_find_userlist(arg->data.str.str); |
| 1280 | |
| 1281 | if (!ul) { |
| 1282 | Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1283 | cur->file, cur->line, arg->data.str.str, |
| 1284 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1285 | cfgerr++; |
| 1286 | break; |
| 1287 | } |
| 1288 | |
| 1289 | free(arg->data.str.str); |
| 1290 | arg->data.str.str = NULL; |
| 1291 | arg->unresolved = 0; |
| 1292 | arg->data.usr = ul; |
| 1293 | break; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1294 | |
| 1295 | case ARGT_REG: |
| 1296 | if (!arg->data.str.len) { |
| 1297 | Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1298 | cur->file, cur->line, |
| 1299 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1300 | cfgerr++; |
| 1301 | continue; |
| 1302 | } |
| 1303 | |
| 1304 | reg = calloc(1, sizeof(*reg)); |
| 1305 | if (!reg) { |
| 1306 | Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1307 | cur->file, cur->line, |
| 1308 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1309 | cfgerr++; |
| 1310 | continue; |
| 1311 | } |
| 1312 | |
| 1313 | rflags = 0; |
| 1314 | rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0; |
| 1315 | err = NULL; |
| 1316 | |
Willy Tarreau | d817e46 | 2015-01-23 20:23:17 +0100 | [diff] [blame] | 1317 | if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) { |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1318 | Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n", |
| 1319 | cur->file, cur->line, |
| 1320 | arg->data.str.str, |
| 1321 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err); |
| 1322 | cfgerr++; |
| 1323 | continue; |
| 1324 | } |
| 1325 | |
| 1326 | free(arg->data.str.str); |
| 1327 | arg->data.str.str = NULL; |
| 1328 | arg->unresolved = 0; |
| 1329 | arg->data.reg = reg; |
| 1330 | break; |
| 1331 | |
| 1332 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | LIST_DEL(&cur->list); |
| 1336 | free(cur); |
| 1337 | } /* end of args processing */ |
| 1338 | |
| 1339 | return cfgerr; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1343 | * Process a fetch + format conversion as defined by the sample expression |
| 1344 | * <expr> on request or response considering the <opt> parameter. The output is |
| 1345 | * always of type string. If a stable sample can be fetched, or an unstable one |
| 1346 | * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and |
| 1347 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1348 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1349 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1350 | * take actions (eg: wait longer). If a sample could not be found or could not |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1351 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1352 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1353 | * still hope to get it after waiting longer, and is not converted to string. |
| 1354 | * The possible output combinations are the following : |
| 1355 | * |
| 1356 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1357 | * NULL * * Not present and will never be (eg: header) |
| 1358 | * smp 0 * Final value converted (eg: header) |
| 1359 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1360 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1361 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1362 | struct sample *sample_fetch_string(struct proxy *px, struct session *sess, |
| 1363 | struct stream *strm, unsigned int opt, |
| 1364 | struct sample_expr *expr) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1365 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1366 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1368 | memset(smp, 0, sizeof(*smp)); |
| 1369 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1370 | if (!sample_process(px, sess, strm, opt, expr, smp)) { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1371 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1372 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1373 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1374 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1375 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1376 | if (!sample_casts[smp->type][SMP_T_STR]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1377 | return NULL; |
| 1378 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1379 | if (!sample_casts[smp->type][SMP_T_STR](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1380 | return NULL; |
| 1381 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1382 | smp->type = SMP_T_STR; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1383 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1384 | return smp; |
| 1385 | } |
| 1386 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1387 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1388 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1389 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1390 | /*****************************************************************/ |
| 1391 | |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1392 | #ifdef DEBUG_EXPR |
| 1393 | static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private) |
| 1394 | { |
| 1395 | int i; |
| 1396 | struct sample tmp; |
| 1397 | |
| 1398 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 1399 | fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->type]); |
| 1400 | if (!sample_casts[smp->type][SMP_T_STR]) { |
| 1401 | fprintf(stderr, "(undisplayable)"); |
| 1402 | } else { |
| 1403 | |
| 1404 | /* Copy sample fetch. This put the sample as const, the |
| 1405 | * cast will copy data if a transformation is required. |
| 1406 | */ |
| 1407 | memcpy(&tmp, smp, sizeof(struct sample)); |
| 1408 | tmp.flags = SMP_F_CONST; |
| 1409 | |
| 1410 | if (!sample_casts[smp->type][SMP_T_STR](&tmp)) |
| 1411 | fprintf(stderr, "(undisplayable)"); |
| 1412 | |
| 1413 | else { |
| 1414 | /* Display the displayable chars*. */ |
| 1415 | fprintf(stderr, "<"); |
| 1416 | for (i = 0; i < tmp.data.str.len; i++) { |
| 1417 | if (isprint(tmp.data.str.str[i])) |
| 1418 | fputc(tmp.data.str.str[i], stderr); |
| 1419 | else |
| 1420 | fputc('.', stderr); |
| 1421 | } |
| 1422 | } |
| 1423 | fprintf(stderr, ">\n"); |
| 1424 | } |
| 1425 | } |
| 1426 | return 1; |
| 1427 | } |
| 1428 | #endif |
| 1429 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1430 | static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1431 | { |
| 1432 | struct chunk *trash = get_trash_chunk(); |
| 1433 | int b64_len; |
| 1434 | |
| 1435 | trash->len = 0; |
| 1436 | b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size); |
| 1437 | if (b64_len < 0) |
| 1438 | return 0; |
| 1439 | |
| 1440 | trash->len = b64_len; |
| 1441 | smp->data.str = *trash; |
| 1442 | smp->type = SMP_T_STR; |
| 1443 | smp->flags &= ~SMP_F_CONST; |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1447 | static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1448 | { |
| 1449 | struct chunk *trash = get_trash_chunk(); |
| 1450 | unsigned char c; |
| 1451 | int ptr = 0; |
| 1452 | |
| 1453 | trash->len = 0; |
| 1454 | while (ptr < smp->data.str.len && trash->len <= trash->size - 2) { |
| 1455 | c = smp->data.str.str[ptr++]; |
| 1456 | trash->str[trash->len++] = hextab[(c >> 4) & 0xF]; |
| 1457 | trash->str[trash->len++] = hextab[c & 0xF]; |
| 1458 | } |
| 1459 | smp->data.str = *trash; |
| 1460 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1461 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1462 | return 1; |
| 1463 | } |
| 1464 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1465 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1466 | static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1467 | { |
| 1468 | smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len); |
| 1469 | if (arg_p && arg_p->data.uint) |
| 1470 | smp->data.uint = full_hash(smp->data.uint); |
| 1471 | smp->type = SMP_T_UINT; |
| 1472 | return 1; |
| 1473 | } |
| 1474 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1475 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1476 | { |
| 1477 | int i; |
| 1478 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1479 | if (!smp_dup(smp)) |
| 1480 | return 0; |
| 1481 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1482 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1483 | return 0; |
| 1484 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1485 | for (i = 0; i < smp->data.str.len; i++) { |
| 1486 | if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z')) |
| 1487 | smp->data.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1488 | } |
| 1489 | return 1; |
| 1490 | } |
| 1491 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1492 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1493 | { |
| 1494 | int i; |
| 1495 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1496 | if (!smp_dup(smp)) |
| 1497 | return 0; |
| 1498 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1499 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1500 | return 0; |
| 1501 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1502 | for (i = 0; i < smp->data.str.len; i++) { |
| 1503 | if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z')) |
| 1504 | smp->data.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1505 | } |
| 1506 | return 1; |
| 1507 | } |
| 1508 | |
Willy Tarreau | f995410 | 2012-04-20 14:03:29 +0200 | [diff] [blame] | 1509 | /* takes the netmask in arg_p */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1510 | static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1511 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1512 | smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr; |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1513 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1514 | return 1; |
| 1515 | } |
| 1516 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1517 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1518 | * adds an optional offset found in args[1] and emits a string representing |
| 1519 | * the local time in the format specified in args[1] using strftime(). |
| 1520 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1521 | static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1522 | { |
| 1523 | struct chunk *temp; |
| 1524 | time_t curr_date = smp->data.uint; |
| 1525 | |
| 1526 | /* add offset */ |
| 1527 | if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT) |
| 1528 | curr_date += args[1].data.sint; |
| 1529 | |
| 1530 | temp = get_trash_chunk(); |
| 1531 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date)); |
| 1532 | smp->data.str = *temp; |
| 1533 | smp->type = SMP_T_STR; |
| 1534 | return 1; |
| 1535 | } |
| 1536 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1537 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1538 | static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1539 | { |
| 1540 | smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len); |
| 1541 | if (arg_p && arg_p->data.uint) |
| 1542 | smp->data.uint = full_hash(smp->data.uint); |
| 1543 | smp->type = SMP_T_UINT; |
| 1544 | return 1; |
| 1545 | } |
| 1546 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1547 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1548 | * adds an optional offset found in args[1] and emits a string representing |
| 1549 | * the UTC date in the format specified in args[1] using strftime(). |
| 1550 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1551 | static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1552 | { |
| 1553 | struct chunk *temp; |
| 1554 | time_t curr_date = smp->data.uint; |
| 1555 | |
| 1556 | /* add offset */ |
| 1557 | if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT) |
| 1558 | curr_date += args[1].data.sint; |
| 1559 | |
| 1560 | temp = get_trash_chunk(); |
| 1561 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date)); |
| 1562 | smp->data.str = *temp; |
| 1563 | smp->type = SMP_T_STR; |
| 1564 | return 1; |
| 1565 | } |
| 1566 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1567 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1568 | static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1569 | { |
| 1570 | smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len); |
| 1571 | if (arg_p && arg_p->data.uint) |
| 1572 | smp->data.uint = full_hash(smp->data.uint); |
| 1573 | smp->type = SMP_T_UINT; |
| 1574 | return 1; |
| 1575 | } |
| 1576 | |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 1577 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1578 | static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 1579 | { |
| 1580 | smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len); |
| 1581 | if (arg_p && arg_p->data.uint) |
| 1582 | smp->data.uint = full_hash(smp->data.uint); |
| 1583 | smp->type = SMP_T_UINT; |
| 1584 | return 1; |
| 1585 | } |
| 1586 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1587 | /* This function escape special json characters. The returned string can be |
| 1588 | * safely set between two '"' and used as json string. The json string is |
| 1589 | * defined like this: |
| 1590 | * |
| 1591 | * any Unicode character except '"' or '\' or control character |
| 1592 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 1593 | * |
| 1594 | * The enum input_type contain all the allowed mode for decoding the input |
| 1595 | * string. |
| 1596 | */ |
| 1597 | enum input_type { |
| 1598 | IT_ASCII = 0, |
| 1599 | IT_UTF8, |
| 1600 | IT_UTF8S, |
| 1601 | IT_UTF8P, |
| 1602 | IT_UTF8PS, |
| 1603 | }; |
| 1604 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 1605 | const char *file, int line, char **err) |
| 1606 | { |
| 1607 | if (!arg) { |
| 1608 | memprintf(err, "Unexpected empty arg list"); |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | if (arg->type != ARGT_STR) { |
| 1613 | memprintf(err, "Unexpected arg type"); |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | if (strcmp(arg->data.str.str, "") == 0) { |
| 1618 | arg->type = ARGT_UINT; |
| 1619 | arg->data.uint = IT_ASCII; |
| 1620 | return 1; |
| 1621 | } |
| 1622 | |
| 1623 | else if (strcmp(arg->data.str.str, "ascii") == 0) { |
| 1624 | arg->type = ARGT_UINT; |
| 1625 | arg->data.uint = IT_ASCII; |
| 1626 | return 1; |
| 1627 | } |
| 1628 | |
| 1629 | else if (strcmp(arg->data.str.str, "utf8") == 0) { |
| 1630 | arg->type = ARGT_UINT; |
| 1631 | arg->data.uint = IT_UTF8; |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
| 1635 | else if (strcmp(arg->data.str.str, "utf8s") == 0) { |
| 1636 | arg->type = ARGT_UINT; |
| 1637 | arg->data.uint = IT_UTF8S; |
| 1638 | return 1; |
| 1639 | } |
| 1640 | |
| 1641 | else if (strcmp(arg->data.str.str, "utf8p") == 0) { |
| 1642 | arg->type = ARGT_UINT; |
| 1643 | arg->data.uint = IT_UTF8P; |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
| 1647 | else if (strcmp(arg->data.str.str, "utf8ps") == 0) { |
| 1648 | arg->type = ARGT_UINT; |
| 1649 | arg->data.uint = IT_UTF8PS; |
| 1650 | return 1; |
| 1651 | } |
| 1652 | |
| 1653 | memprintf(err, "Unexpected input code type at file '%s', line %d. " |
| 1654 | "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line); |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1658 | static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1659 | { |
| 1660 | struct chunk *temp; |
| 1661 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 1662 | const char *str; |
| 1663 | int len; |
| 1664 | enum input_type input_type = IT_ASCII; |
| 1665 | unsigned int c; |
| 1666 | unsigned int ret; |
| 1667 | char *p; |
| 1668 | |
| 1669 | if (arg_p) |
| 1670 | input_type = arg_p->data.uint; |
| 1671 | |
| 1672 | temp = get_trash_chunk(); |
| 1673 | temp->len = 0; |
| 1674 | |
| 1675 | p = smp->data.str.str; |
| 1676 | while (p < smp->data.str.str + smp->data.str.len) { |
| 1677 | |
| 1678 | if (input_type == IT_ASCII) { |
| 1679 | /* Read input as ASCII. */ |
| 1680 | c = *(unsigned char *)p; |
| 1681 | p++; |
| 1682 | } |
| 1683 | else { |
| 1684 | /* Read input as UTF8. */ |
| 1685 | ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c); |
| 1686 | p += utf8_return_length(ret); |
| 1687 | |
| 1688 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1689 | return 0; |
| 1690 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1691 | continue; |
| 1692 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1693 | return 0; |
| 1694 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1695 | continue; |
| 1696 | |
| 1697 | /* Check too big values. */ |
| 1698 | if ((unsigned int)c > 0xffff) { |
| 1699 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 1700 | return 0; |
| 1701 | continue; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | /* Convert character. */ |
| 1706 | if (c == '"') { |
| 1707 | len = 2; |
| 1708 | str = "\\\""; |
| 1709 | } |
| 1710 | else if (c == '\\') { |
| 1711 | len = 2; |
| 1712 | str = "\\\\"; |
| 1713 | } |
| 1714 | else if (c == '/') { |
| 1715 | len = 2; |
| 1716 | str = "\\/"; |
| 1717 | } |
| 1718 | else if (c == '\b') { |
| 1719 | len = 2; |
| 1720 | str = "\\b"; |
| 1721 | } |
| 1722 | else if (c == '\f') { |
| 1723 | len = 2; |
| 1724 | str = "\\f"; |
| 1725 | } |
| 1726 | else if (c == '\r') { |
| 1727 | len = 2; |
| 1728 | str = "\\r"; |
| 1729 | } |
| 1730 | else if (c == '\n') { |
| 1731 | len = 2; |
| 1732 | str = "\\n"; |
| 1733 | } |
| 1734 | else if (c == '\t') { |
| 1735 | len = 2; |
| 1736 | str = "\\t"; |
| 1737 | } |
| 1738 | else if (c > 0xff || !isprint(c)) { |
| 1739 | /* isprint generate a segfault if c is too big. The man says that |
| 1740 | * c must have the value of an unsigned char or EOF. |
| 1741 | */ |
| 1742 | len = 6; |
| 1743 | _str[0] = '\\'; |
| 1744 | _str[1] = 'u'; |
| 1745 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 1746 | str = _str; |
| 1747 | } |
| 1748 | else { |
| 1749 | len = 1; |
| 1750 | str = (char *)&c; |
| 1751 | } |
| 1752 | |
| 1753 | /* Check length */ |
| 1754 | if (temp->len + len > temp->size) |
| 1755 | return 0; |
| 1756 | |
| 1757 | /* Copy string. */ |
| 1758 | memcpy(temp->str + temp->len, str, len); |
| 1759 | temp->len += len; |
| 1760 | } |
| 1761 | |
| 1762 | smp->flags &= ~SMP_F_CONST; |
| 1763 | smp->data.str = *temp; |
| 1764 | smp->type = SMP_T_STR; |
| 1765 | |
| 1766 | return 1; |
| 1767 | } |
| 1768 | |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1769 | /* This sample function is designed to extract some bytes from an input buffer. |
| 1770 | * First arg is the offset. |
| 1771 | * Optional second arg is the length to truncate */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1772 | static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1773 | { |
| 1774 | if (smp->data.str.len <= arg_p[0].data.uint) { |
| 1775 | smp->data.str.len = 0; |
| 1776 | return 1; |
| 1777 | } |
| 1778 | |
| 1779 | if (smp->data.str.size) |
| 1780 | smp->data.str.size -= arg_p[0].data.uint; |
| 1781 | smp->data.str.len -= arg_p[0].data.uint; |
| 1782 | smp->data.str.str += arg_p[0].data.uint; |
| 1783 | |
| 1784 | if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len)) |
| 1785 | smp->data.str.len = arg_p[1].data.uint; |
| 1786 | |
| 1787 | return 1; |
| 1788 | } |
| 1789 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1790 | static int sample_conv_field_check(struct arg *args, struct sample_conv *conv, |
| 1791 | const char *file, int line, char **err) |
| 1792 | { |
| 1793 | struct arg *arg = args; |
| 1794 | |
| 1795 | if (!arg) { |
| 1796 | memprintf(err, "Unexpected empty arg list"); |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | if (arg->type != ARGT_UINT) { |
| 1801 | memprintf(err, "Unexpected arg type"); |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | if (!arg->data.uint) { |
| 1806 | memprintf(err, "Unexpected value 0 for index"); |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
| 1810 | arg++; |
| 1811 | |
| 1812 | if (arg->type != ARGT_STR) { |
| 1813 | memprintf(err, "Unexpected arg type"); |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | if (!arg->data.str.len) { |
| 1818 | memprintf(err, "Empty separators list"); |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | return 1; |
| 1823 | } |
| 1824 | |
| 1825 | /* This sample function is designed to a return selected part of a string (field). |
| 1826 | * First arg is the index of the field (start at 1) |
| 1827 | * Second arg is a char list of separators (type string) |
| 1828 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1829 | static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1830 | { |
| 1831 | unsigned int field; |
| 1832 | char *start, *end; |
| 1833 | int i; |
| 1834 | |
| 1835 | if (!arg_p[0].data.uint) |
| 1836 | return 0; |
| 1837 | |
| 1838 | field = 1; |
| 1839 | end = start = smp->data.str.str; |
| 1840 | while (end - smp->data.str.str < smp->data.str.len) { |
| 1841 | |
| 1842 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 1843 | if (*end == arg_p[1].data.str.str[i]) { |
| 1844 | if (field == arg_p[0].data.uint) |
| 1845 | goto found; |
| 1846 | start = end+1; |
| 1847 | field++; |
| 1848 | break; |
| 1849 | } |
| 1850 | } |
| 1851 | end++; |
| 1852 | } |
| 1853 | |
| 1854 | /* Field not found */ |
| 1855 | if (field != arg_p[0].data.uint) { |
| 1856 | smp->data.str.len = 0; |
| 1857 | return 1; |
| 1858 | } |
| 1859 | found: |
| 1860 | smp->data.str.len = end - start; |
| 1861 | /* If ret string is len 0, no need to |
| 1862 | change pointers or to update size */ |
| 1863 | if (!smp->data.str.len) |
| 1864 | return 1; |
| 1865 | |
| 1866 | smp->data.str.str = start; |
| 1867 | |
| 1868 | /* Compute remaining size if needed |
| 1869 | Note: smp->data.str.size cannot be set to 0 */ |
| 1870 | if (smp->data.str.size) |
| 1871 | smp->data.str.size -= start - smp->data.str.str; |
| 1872 | |
| 1873 | return 1; |
| 1874 | } |
| 1875 | |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1876 | /* This sample function is designed to return a word from a string. |
| 1877 | * First arg is the index of the word (start at 1) |
| 1878 | * Second arg is a char list of words separators (type string) |
| 1879 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1880 | static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1881 | { |
| 1882 | unsigned int word; |
| 1883 | char *start, *end; |
| 1884 | int i, issep, inword; |
| 1885 | |
| 1886 | if (!arg_p[0].data.uint) |
| 1887 | return 0; |
| 1888 | |
| 1889 | word = 0; |
| 1890 | inword = 0; |
| 1891 | end = start = smp->data.str.str; |
| 1892 | while (end - smp->data.str.str < smp->data.str.len) { |
| 1893 | issep = 0; |
| 1894 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 1895 | if (*end == arg_p[1].data.str.str[i]) { |
| 1896 | issep = 1; |
| 1897 | break; |
| 1898 | } |
| 1899 | } |
| 1900 | if (!inword) { |
| 1901 | if (!issep) { |
| 1902 | word++; |
| 1903 | start = end; |
| 1904 | inword = 1; |
| 1905 | } |
| 1906 | } |
| 1907 | else if (issep) { |
| 1908 | if (word == arg_p[0].data.uint) |
| 1909 | goto found; |
| 1910 | inword = 0; |
| 1911 | } |
| 1912 | end++; |
| 1913 | } |
| 1914 | |
| 1915 | /* Field not found */ |
| 1916 | if (word != arg_p[0].data.uint) { |
| 1917 | smp->data.str.len = 0; |
| 1918 | return 1; |
| 1919 | } |
| 1920 | found: |
| 1921 | smp->data.str.len = end - start; |
| 1922 | /* If ret string is len 0, no need to |
| 1923 | change pointers or to update size */ |
| 1924 | if (!smp->data.str.len) |
| 1925 | return 1; |
| 1926 | |
| 1927 | smp->data.str.str = start; |
| 1928 | |
| 1929 | /* Compute remaining size if needed |
| 1930 | Note: smp->data.str.size cannot be set to 0 */ |
| 1931 | if (smp->data.str.size) |
| 1932 | smp->data.str.size -= start - smp->data.str.str; |
| 1933 | |
| 1934 | return 1; |
| 1935 | } |
| 1936 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 1937 | static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv, |
| 1938 | const char *file, int line, char **err) |
| 1939 | { |
| 1940 | struct arg *arg = args; |
| 1941 | char *p; |
| 1942 | int len; |
| 1943 | |
| 1944 | /* arg0 is a regex, it uses type_flag for ICASE and global match */ |
| 1945 | arg[0].type_flags = 0; |
| 1946 | |
| 1947 | if (arg[2].type != ARGT_STR) |
| 1948 | return 1; |
| 1949 | |
| 1950 | p = arg[2].data.str.str; |
| 1951 | len = arg[2].data.str.len; |
| 1952 | while (len) { |
| 1953 | if (*p == 'i') { |
| 1954 | arg[0].type_flags |= ARGF_REG_ICASE; |
| 1955 | } |
| 1956 | else if (*p == 'g') { |
| 1957 | arg[0].type_flags |= ARGF_REG_GLOB; |
| 1958 | } |
| 1959 | else { |
| 1960 | memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | p++; |
| 1964 | len--; |
| 1965 | } |
| 1966 | return 1; |
| 1967 | } |
| 1968 | |
| 1969 | /* This sample function is designed to do the equivalent of s/match/replace/ on |
| 1970 | * the input string. It applies a regex and restarts from the last matched |
| 1971 | * location until nothing matches anymore. First arg is the regex to apply to |
| 1972 | * the input string, second arg is the replacement expression. |
| 1973 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1974 | static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 1975 | { |
| 1976 | char *start, *end; |
| 1977 | struct my_regex *reg = arg_p[0].data.reg; |
| 1978 | regmatch_t pmatch[MAX_MATCH]; |
| 1979 | struct chunk *trash = get_trash_chunk(); |
| 1980 | int flag, max; |
| 1981 | int found; |
| 1982 | |
| 1983 | start = smp->data.str.str; |
| 1984 | end = start + smp->data.str.len; |
| 1985 | |
| 1986 | flag = 0; |
| 1987 | while (1) { |
| 1988 | /* check for last round which is used to copy remaining parts |
| 1989 | * when not running in global replacement mode. |
| 1990 | */ |
| 1991 | found = 0; |
| 1992 | if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) { |
| 1993 | /* Note: we can have start == end on empty strings or at the end */ |
| 1994 | found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag); |
| 1995 | } |
| 1996 | |
| 1997 | if (!found) |
| 1998 | pmatch[0].rm_so = end - start; |
| 1999 | |
| 2000 | /* copy the heading non-matching part (which may also be the tail if nothing matches) */ |
| 2001 | max = trash->size - trash->len; |
| 2002 | if (max && pmatch[0].rm_so > 0) { |
| 2003 | if (max > pmatch[0].rm_so) |
| 2004 | max = pmatch[0].rm_so; |
| 2005 | memcpy(trash->str + trash->len, start, max); |
| 2006 | trash->len += max; |
| 2007 | } |
| 2008 | |
| 2009 | if (!found) |
| 2010 | break; |
| 2011 | |
| 2012 | /* replace the matching part */ |
| 2013 | max = trash->size - trash->len; |
| 2014 | if (max) { |
| 2015 | if (max > arg_p[1].data.str.len) |
| 2016 | max = arg_p[1].data.str.len; |
| 2017 | memcpy(trash->str + trash->len, arg_p[1].data.str.str, max); |
| 2018 | trash->len += max; |
| 2019 | } |
| 2020 | |
| 2021 | /* stop here if we're done with this string */ |
| 2022 | if (start >= end) |
| 2023 | break; |
| 2024 | |
| 2025 | /* We have a special case for matches of length 0 (eg: "x*y*"). |
| 2026 | * These ones are considered to match in front of a character, |
| 2027 | * so we have to copy that character and skip to the next one. |
| 2028 | */ |
| 2029 | if (!pmatch[0].rm_eo) { |
| 2030 | if (trash->len < trash->size) |
| 2031 | trash->str[trash->len++] = start[pmatch[0].rm_eo]; |
| 2032 | pmatch[0].rm_eo++; |
| 2033 | } |
| 2034 | |
| 2035 | start += pmatch[0].rm_eo; |
| 2036 | flag |= REG_NOTBOL; |
| 2037 | } |
| 2038 | |
| 2039 | smp->data.str = *trash; |
| 2040 | return 1; |
| 2041 | } |
| 2042 | |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2043 | /* Takes a UINT on input, applies a binary twos complement and returns the UINT |
| 2044 | * result. |
| 2045 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2046 | static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2047 | { |
| 2048 | smp->data.uint = ~smp->data.uint; |
| 2049 | return 1; |
| 2050 | } |
| 2051 | |
| 2052 | /* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and |
| 2053 | * returns the UINT result. |
| 2054 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2055 | static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2056 | { |
| 2057 | smp->data.uint &= arg_p->data.uint; |
| 2058 | return 1; |
| 2059 | } |
| 2060 | |
| 2061 | /* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and |
| 2062 | * returns the UINT result. |
| 2063 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2064 | static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2065 | { |
| 2066 | smp->data.uint |= arg_p->data.uint; |
| 2067 | return 1; |
| 2068 | } |
| 2069 | |
| 2070 | /* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and |
| 2071 | * returns the UINT result. |
| 2072 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2073 | static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2074 | { |
| 2075 | smp->data.uint ^= arg_p->data.uint; |
| 2076 | return 1; |
| 2077 | } |
| 2078 | |
| 2079 | /* Takes a UINT on input, applies an arithmetic "add" with the UINT in arg_p, |
| 2080 | * and returns the UINT result. |
| 2081 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2082 | static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2083 | { |
| 2084 | smp->data.uint += arg_p->data.uint; |
| 2085 | return 1; |
| 2086 | } |
| 2087 | |
| 2088 | /* Takes a UINT on input, applies an arithmetic "sub" with the UINT in arg_p, |
| 2089 | * and returns the UINT result. |
| 2090 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2091 | static int sample_conv_arith_sub(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2092 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2093 | { |
| 2094 | smp->data.uint -= arg_p->data.uint; |
| 2095 | return 1; |
| 2096 | } |
| 2097 | |
| 2098 | /* Takes a UINT on input, applies an arithmetic "mul" with the UINT in arg_p, |
| 2099 | * and returns the UINT result. |
| 2100 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2101 | static int sample_conv_arith_mul(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2102 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2103 | { |
| 2104 | smp->data.uint *= arg_p->data.uint; |
| 2105 | return 1; |
| 2106 | } |
| 2107 | |
| 2108 | /* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p, |
| 2109 | * and returns the UINT result. If arg_p makes the result overflow, then the |
| 2110 | * largest possible quantity is returned. |
| 2111 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2112 | static int sample_conv_arith_div(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2113 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2114 | { |
| 2115 | if (arg_p->data.uint) |
| 2116 | smp->data.uint /= arg_p->data.uint; |
| 2117 | else |
| 2118 | smp->data.uint = ~0; |
| 2119 | return 1; |
| 2120 | } |
| 2121 | |
| 2122 | /* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p, |
| 2123 | * and returns the UINT result. If arg_p makes the result overflow, then zero |
| 2124 | * is returned. |
| 2125 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2126 | static int sample_conv_arith_mod(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2127 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2128 | { |
| 2129 | if (arg_p->data.uint) |
| 2130 | smp->data.uint %= arg_p->data.uint; |
| 2131 | else |
| 2132 | smp->data.uint = 0; |
| 2133 | return 1; |
| 2134 | } |
| 2135 | |
| 2136 | /* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT |
| 2137 | * result. |
| 2138 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2139 | static int sample_conv_arith_neg(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2140 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2141 | { |
| 2142 | smp->data.uint = -smp->data.uint; |
| 2143 | return 1; |
| 2144 | } |
| 2145 | |
| 2146 | /* Takes a UINT on input, returns true is the value is non-null, otherwise |
| 2147 | * false. The output is a BOOL. |
| 2148 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2149 | static int sample_conv_arith_bool(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2150 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2151 | { |
| 2152 | smp->data.uint = !!smp->data.uint; |
| 2153 | smp->type = SMP_T_BOOL; |
| 2154 | return 1; |
| 2155 | } |
| 2156 | |
| 2157 | /* Takes a UINT on input, returns false is the value is non-null, otherwise |
| 2158 | * truee. The output is a BOOL. |
| 2159 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2160 | static int sample_conv_arith_not(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2161 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2162 | { |
| 2163 | smp->data.uint = !smp->data.uint; |
| 2164 | smp->type = SMP_T_BOOL; |
| 2165 | return 1; |
| 2166 | } |
| 2167 | |
| 2168 | /* Takes a UINT on input, returns true is the value is odd, otherwise false. |
| 2169 | * The output is a BOOL. |
| 2170 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2171 | static int sample_conv_arith_odd(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2172 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2173 | { |
| 2174 | smp->data.uint = smp->data.uint & 1; |
| 2175 | smp->type = SMP_T_BOOL; |
| 2176 | return 1; |
| 2177 | } |
| 2178 | |
| 2179 | /* Takes a UINT on input, returns true is the value is even, otherwise false. |
| 2180 | * The output is a BOOL. |
| 2181 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2182 | static int sample_conv_arith_even(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2183 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2184 | { |
| 2185 | smp->data.uint = !(smp->data.uint & 1); |
| 2186 | smp->type = SMP_T_BOOL; |
| 2187 | return 1; |
| 2188 | } |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2189 | |
| 2190 | #ifdef USE_51DEGREES |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2191 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 2192 | static int |
| 2193 | sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private) |
| 2194 | { |
| 2195 | int i; // used in loops. |
| 2196 | int device_offset; |
| 2197 | int property_index; |
| 2198 | char no_data[] = "NoData"; // response when no data could be found. |
| 2199 | struct chunk *tmp; |
| 2200 | |
| 2201 | // use a temporary trash buffer and copy data in it |
| 2202 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 2203 | |
| 2204 | // perform detection. |
| 2205 | device_offset = fiftyoneDegreesGetDeviceOffset(smp->data.str.str); |
| 2206 | |
| 2207 | i = 0; |
| 2208 | tmp = get_trash_chunk(); |
| 2209 | chunk_reset(tmp); |
| 2210 | |
| 2211 | // loop through property names passed to the filter and fetch them from the dataset. |
| 2212 | while (args[i].data.str.str) { |
| 2213 | // try to find request property in dataset. |
| 2214 | property_index = fiftyoneDegreesGetPropertyIndex(args[i].data.str.str); |
| 2215 | if (property_index > 0) { |
| 2216 | chunk_appendf(tmp, "%s", fiftyoneDegreesGetValue(device_offset, property_index)); |
| 2217 | } |
| 2218 | else { |
| 2219 | chunk_appendf(tmp, "%s", no_data); |
| 2220 | } |
| 2221 | // add seperator |
| 2222 | if (global._51d_property_seperator) |
| 2223 | chunk_appendf(tmp, "%c", global._51d_property_seperator); |
| 2224 | else |
| 2225 | chunk_appendf(tmp, ","); |
| 2226 | ++i; |
| 2227 | } |
| 2228 | |
| 2229 | if (tmp->len) { |
| 2230 | --tmp->len; |
| 2231 | tmp->str[tmp->len] = '\0'; |
| 2232 | } |
| 2233 | |
| 2234 | smp->data.str.str = tmp->str; |
| 2235 | smp->data.str.len = strlen(smp->data.str.str); |
| 2236 | |
| 2237 | return 1; |
| 2238 | } |
| 2239 | #endif // FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 2240 | |
| 2241 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 2242 | static int |
| 2243 | sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private) |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2244 | { |
| 2245 | int i, j, found; // used in loops. |
| 2246 | fiftyoneDegreesWorkset* ws; // workset for detection. |
| 2247 | char no_data[] = "NoData"; // response when no data could be found. |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2248 | const char* property_name; |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2249 | struct chunk *tmp; |
| 2250 | |
| 2251 | // use a temporary trash buffer and copy data in it |
| 2252 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 2253 | |
| 2254 | // create workset. this will later contain detection results. |
| 2255 | ws = fiftyoneDegreesCreateWorkset(&global._51d_data_set); |
| 2256 | if (!ws) |
| 2257 | return 0; |
| 2258 | |
| 2259 | // perform detection. |
| 2260 | fiftyoneDegreesMatch(ws, smp->data.str.str); |
| 2261 | |
| 2262 | i = 0; |
| 2263 | tmp = get_trash_chunk(); |
| 2264 | chunk_reset(tmp); |
| 2265 | // loop through property names passed to the filter and fetch them from the dataset. |
| 2266 | while (args[i].data.str.str) { |
| 2267 | found = j = 0; |
| 2268 | // try to find request property in dataset. |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2269 | for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) { |
| 2270 | property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]); |
| 2271 | if (strcmp(property_name, args[i].data.str.str) == 0) { |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2272 | found = 1; |
| 2273 | fiftyoneDegreesSetValues(ws, j); |
| 2274 | chunk_appendf(tmp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values)); |
| 2275 | break; |
| 2276 | } |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2277 | } |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2278 | if (!found) { |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2279 | chunk_appendf(tmp, "%s", no_data); |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2280 | } |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2281 | |
| 2282 | // add seperator |
| 2283 | if (global._51d_property_seperator) |
| 2284 | chunk_appendf(tmp, "%c", global._51d_property_seperator); |
| 2285 | else |
| 2286 | chunk_appendf(tmp, ","); |
| 2287 | ++i; |
| 2288 | } |
| 2289 | |
| 2290 | if (tmp->len) { |
| 2291 | --tmp->len; |
| 2292 | tmp->str[tmp->len] = '\0'; |
| 2293 | } |
| 2294 | |
| 2295 | smp->data.str.str = tmp->str; |
| 2296 | smp->data.str.len = strlen(smp->data.str.str); |
| 2297 | |
| 2298 | fiftyoneDegreesFreeWorkset(ws); |
| 2299 | |
| 2300 | return 1; |
| 2301 | } |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2302 | #endif // FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 2303 | #endif // USE_51DEGREES |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2304 | |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2305 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2306 | /************************************************************************/ |
| 2307 | /* All supported sample fetch functions must be declared here */ |
| 2308 | /************************************************************************/ |
| 2309 | |
| 2310 | /* force TRUE to be returned at the fetch level */ |
| 2311 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2312 | smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2313 | { |
| 2314 | smp->type = SMP_T_BOOL; |
| 2315 | smp->data.uint = 1; |
| 2316 | return 1; |
| 2317 | } |
| 2318 | |
| 2319 | /* force FALSE to be returned at the fetch level */ |
| 2320 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2321 | smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2322 | { |
| 2323 | smp->type = SMP_T_BOOL; |
| 2324 | smp->data.uint = 0; |
| 2325 | return 1; |
| 2326 | } |
| 2327 | |
| 2328 | /* retrieve environment variable $1 as a string */ |
| 2329 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2330 | smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2331 | { |
| 2332 | char *env; |
| 2333 | |
| 2334 | if (!args || args[0].type != ARGT_STR) |
| 2335 | return 0; |
| 2336 | |
| 2337 | env = getenv(args[0].data.str.str); |
| 2338 | if (!env) |
| 2339 | return 0; |
| 2340 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2341 | smp->type = SMP_T_STR; |
| 2342 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2343 | smp->data.str.str = env; |
| 2344 | smp->data.str.len = strlen(env); |
| 2345 | return 1; |
| 2346 | } |
| 2347 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2348 | /* retrieve the current local date in epoch time, and applies an optional offset |
| 2349 | * of args[0] seconds. |
| 2350 | */ |
| 2351 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2352 | smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2353 | { |
| 2354 | smp->data.uint = date.tv_sec; |
| 2355 | |
| 2356 | /* add offset */ |
| 2357 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 2358 | smp->data.uint += args[0].data.sint; |
| 2359 | |
| 2360 | smp->type = SMP_T_UINT; |
| 2361 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2362 | return 1; |
| 2363 | } |
| 2364 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2365 | /* returns the number of processes */ |
| 2366 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2367 | smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2368 | { |
| 2369 | smp->type = SMP_T_UINT; |
| 2370 | smp->data.uint = global.nbproc; |
| 2371 | return 1; |
| 2372 | } |
| 2373 | |
| 2374 | /* returns the number of the current process (between 1 and nbproc */ |
| 2375 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2376 | smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2377 | { |
| 2378 | smp->type = SMP_T_UINT; |
| 2379 | smp->data.uint = relative_pid; |
| 2380 | return 1; |
| 2381 | } |
| 2382 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2383 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 2384 | * range specified in argument. |
| 2385 | */ |
| 2386 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2387 | smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2388 | { |
| 2389 | smp->data.uint = random(); |
| 2390 | |
| 2391 | /* reduce if needed. Don't do a modulo, use all bits! */ |
| 2392 | if (args && args[0].type == ARGT_UINT) |
Vincent Bernat | 1228dc0 | 2014-12-10 10:31:37 +0100 | [diff] [blame] | 2393 | smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2394 | |
| 2395 | smp->type = SMP_T_UINT; |
| 2396 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2397 | return 1; |
| 2398 | } |
| 2399 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2400 | /* returns true if the current process is stopping */ |
| 2401 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2402 | smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2403 | { |
| 2404 | smp->type = SMP_T_BOOL; |
| 2405 | smp->data.uint = stopping; |
| 2406 | return 1; |
| 2407 | } |
| 2408 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2409 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2410 | * Note: fetches that may return multiple types must be declared as the lowest |
| 2411 | * common denominator, the type that can be casted into all other ones. For |
| 2412 | * instance IPv4/IPv6 must be declared IPv4. |
| 2413 | */ |
| 2414 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 2415 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 2416 | { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2417 | { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN }, |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2418 | { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2419 | { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN }, |
| 2420 | { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2421 | { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2422 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2423 | { /* END */ }, |
| 2424 | }}; |
| 2425 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2426 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2427 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 2428 | #ifdef DEBUG_EXPR |
| 2429 | { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY }, |
| 2430 | #endif |
| 2431 | |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 2432 | { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2433 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 2434 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 2435 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2436 | { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 }, |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 2437 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
| 2438 | { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2439 | { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 2440 | { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
| 2441 | { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
| 2442 | { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2443 | { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR }, |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2444 | { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN }, |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2445 | { "field", sample_conv_field, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2446 | { "word", sample_conv_word, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2447 | { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2448 | |
| 2449 | { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2450 | { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2451 | { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2452 | { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2453 | { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL }, |
| 2454 | { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL }, |
| 2455 | { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL }, |
| 2456 | { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL }, |
| 2457 | { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2458 | { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2459 | { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2460 | { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2461 | { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT }, |
| 2462 | { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT }, |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2463 | #ifdef USE_51DEGREES |
Thomas Holmes | 4d441a7 | 2015-05-29 12:54:25 +0100 | [diff] [blame] | 2464 | { "51d", sample_fiftyone_degrees,ARG5(1,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR }, |
Thomas Holmes | 951d44d | 2015-05-13 11:27:35 +0100 | [diff] [blame] | 2465 | #endif |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2466 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 2467 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2468 | }}; |
| 2469 | |
| 2470 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2471 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2472 | { |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2473 | /* register sample fetch and format conversion keywords */ |
| 2474 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2475 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2476 | } |