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