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