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 | |
| 387 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 388 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 389 | * string of <len> in buffer <kw>. |
| 390 | * |
| 391 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 392 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 393 | { |
| 394 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 395 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 396 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 397 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 398 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 399 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 400 | kwl->kw[index].kw[len] == '\0') |
| 401 | return &kwl->kw[index]; |
| 402 | } |
| 403 | } |
| 404 | return NULL; |
| 405 | } |
| 406 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 407 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 408 | /* Sample casts functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 409 | /* Note: these functions do *NOT* set the output type on the */ |
| 410 | /* sample, the caller is responsible for doing this on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 411 | /******************************************************************/ |
| 412 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 413 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 414 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 415 | smp->data.uint = ntohl(smp->data.ipv4.s_addr); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 416 | smp->type = SMP_T_UINT; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 417 | return 1; |
| 418 | } |
| 419 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 420 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 421 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 422 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 423 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 424 | 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] | 425 | return 0; |
| 426 | |
| 427 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 428 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 429 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 430 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 431 | |
| 432 | return 1; |
| 433 | } |
| 434 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 435 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 436 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 437 | v4tov6(&smp->data.ipv6, &smp->data.ipv4); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 438 | smp->type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 439 | return 1; |
| 440 | } |
| 441 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 442 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 443 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 444 | struct chunk *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 445 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 446 | 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] | 447 | return 0; |
| 448 | |
| 449 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 450 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 451 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 452 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 457 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 458 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 459 | return v6tov4(&smp->data.ipv4, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 460 | } |
| 461 | */ |
| 462 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 463 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 464 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 465 | smp->data.ipv4.s_addr = htonl(smp->data.uint); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 466 | smp->type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 467 | return 1; |
| 468 | } |
| 469 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 470 | static int c_str2addr(struct sample *smp) |
| 471 | { |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 472 | 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] | 473 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 474 | return 0; |
| 475 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 476 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 477 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 478 | } |
| 479 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 480 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 481 | return 1; |
| 482 | } |
| 483 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 484 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 485 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 486 | 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] | 487 | return 0; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 488 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 489 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 490 | return 1; |
| 491 | } |
| 492 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 493 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 494 | { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 495 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 496 | return 0; |
| 497 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 498 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 499 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 502 | /* The sample is always copied into a new one so that smp->size is always |
| 503 | * valid. The NULL char always enforces the end of string if it is met. |
| 504 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 505 | static int c_bin2str(struct sample *smp) |
| 506 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 507 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 508 | unsigned char c; |
| 509 | int ptr = 0; |
| 510 | |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 511 | while (ptr < smp->data.str.len) { |
| 512 | c = smp->data.str.str[ptr]; |
| 513 | if (!c) |
| 514 | break; |
| 515 | trash->str[ptr] = c; |
| 516 | ptr++; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 517 | } |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 518 | trash->len = ptr; |
| 519 | trash->str[ptr] = 0; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 520 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 521 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 522 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 523 | return 1; |
| 524 | } |
| 525 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 526 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 527 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 528 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 529 | char *pos; |
| 530 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 531 | pos = ultoa_r(smp->data.uint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 532 | |
| 533 | if (!pos) |
| 534 | return 0; |
| 535 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 536 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 537 | trash->str = pos; |
| 538 | trash->len = strlen(pos); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 539 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 540 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 541 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 542 | return 1; |
| 543 | } |
| 544 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 545 | /* This function duplicates data and removes the flag "const". */ |
| 546 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 547 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 548 | struct chunk *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 549 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 550 | /* If the const flag is not set, we don't need to duplicate the |
| 551 | * pattern as it can be modified in place. |
| 552 | */ |
| 553 | if (!(smp->flags & SMP_F_CONST)) |
| 554 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 555 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 556 | switch (smp->type) { |
| 557 | case SMP_T_BOOL: |
| 558 | case SMP_T_UINT: |
| 559 | case SMP_T_SINT: |
| 560 | case SMP_T_ADDR: |
| 561 | case SMP_T_IPV4: |
| 562 | case SMP_T_IPV6: |
| 563 | /* These type are not const. */ |
| 564 | break; |
| 565 | case SMP_T_STR: |
| 566 | case SMP_T_BIN: |
| 567 | /* Duplicate data. */ |
| 568 | trash = get_trash_chunk(); |
| 569 | trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size; |
| 570 | memcpy(trash->str, smp->data.str.str, trash->len); |
| 571 | smp->data.str = *trash; |
| 572 | break; |
| 573 | default: |
| 574 | /* Other cases are unexpected. */ |
| 575 | return 0; |
| 576 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 577 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 578 | /* remove const flag */ |
| 579 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 580 | return 1; |
| 581 | } |
| 582 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 583 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 584 | { |
| 585 | return 1; |
| 586 | } |
| 587 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 588 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 589 | { |
| 590 | int i; |
| 591 | uint32_t ret = 0; |
| 592 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 593 | if (smp->data.str.len == 0) |
| 594 | return 0; |
| 595 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 596 | for (i = 0; i < smp->data.str.len; i++) { |
| 597 | uint32_t val = smp->data.str.str[i] - '0'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 598 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 599 | if (val > 9) { |
| 600 | if (i == 0) |
| 601 | return 0; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 602 | break; |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 603 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 604 | |
| 605 | ret = ret * 10 + val; |
| 606 | } |
| 607 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 608 | smp->data.uint = ret; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 609 | smp->type = SMP_T_UINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 610 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 611 | return 1; |
| 612 | } |
| 613 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 614 | static int c_str2meth(struct sample *smp) |
| 615 | { |
| 616 | enum http_meth_t meth; |
| 617 | int len; |
| 618 | |
| 619 | meth = find_http_meth(smp->data.str.str, smp->data.str.len); |
| 620 | if (meth == HTTP_METH_OTHER) { |
| 621 | len = smp->data.str.len; |
| 622 | smp->data.meth.str.str = smp->data.str.str; |
| 623 | smp->data.meth.str.len = len; |
| 624 | } |
| 625 | else |
| 626 | smp->flags &= ~SMP_F_CONST; |
| 627 | smp->data.meth.meth = meth; |
| 628 | smp->type = SMP_T_METH; |
| 629 | return 1; |
| 630 | } |
| 631 | |
| 632 | static int c_meth2str(struct sample *smp) |
| 633 | { |
| 634 | int len; |
| 635 | enum http_meth_t meth; |
| 636 | |
| 637 | if (smp->data.meth.meth == HTTP_METH_OTHER) { |
| 638 | /* The method is unknown. Copy the original pointer. */ |
| 639 | len = smp->data.meth.str.len; |
| 640 | smp->data.str.str = smp->data.meth.str.str; |
| 641 | smp->data.str.len = len; |
| 642 | smp->type = SMP_T_STR; |
| 643 | } |
| 644 | else if (smp->data.meth.meth < HTTP_METH_OTHER) { |
| 645 | /* The method is known, copy the pointer containing the string. */ |
| 646 | meth = smp->data.meth.meth; |
| 647 | smp->data.str.str = http_known_methods[meth].name; |
| 648 | smp->data.str.len = http_known_methods[meth].len; |
| 649 | smp->flags |= SMP_F_CONST; |
| 650 | smp->type = SMP_T_STR; |
| 651 | } |
| 652 | else { |
| 653 | /* Unknown method */ |
| 654 | return 0; |
| 655 | } |
| 656 | return 1; |
| 657 | } |
| 658 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 659 | static int c_addr2bin(struct sample *smp) |
| 660 | { |
| 661 | struct chunk *chk = get_trash_chunk(); |
| 662 | |
| 663 | if (smp->type == SMP_T_IPV4) { |
| 664 | chk->len = 4; |
| 665 | memcpy(chk->str, &smp->data.ipv4, chk->len); |
| 666 | } |
| 667 | else if (smp->type == SMP_T_IPV6) { |
| 668 | chk->len = 16; |
| 669 | memcpy(chk->str, &smp->data.ipv6, chk->len); |
| 670 | } |
| 671 | else |
| 672 | return 0; |
| 673 | |
| 674 | smp->data.str = *chk; |
| 675 | smp->type = SMP_T_BIN; |
| 676 | return 1; |
| 677 | } |
| 678 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 679 | static int c_int2bin(struct sample *smp) |
| 680 | { |
| 681 | struct chunk *chk = get_trash_chunk(); |
| 682 | |
| 683 | *(unsigned int *)chk->str = htonl(smp->data.uint); |
| 684 | chk->len = 4; |
| 685 | |
| 686 | smp->data.str = *chk; |
| 687 | smp->type = SMP_T_BIN; |
| 688 | return 1; |
| 689 | } |
| 690 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 691 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 692 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 693 | /* Sample casts matrix: */ |
| 694 | /* sample_casts[from type][to type] */ |
| 695 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 696 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 697 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 698 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 699 | /* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 700 | /* 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] | 701 | /* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, }, |
| 702 | /* 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] | 703 | /* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 704 | /* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, }, |
| 705 | /* 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] | 706 | /* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 707 | /* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 708 | /* 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] | 709 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 710 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 711 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 712 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 713 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 714 | * Returns a pointer on allocated sample expression structure. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 715 | * The caller must have set al->ctx. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 716 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 717 | 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] | 718 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 719 | const char *begw; /* beginning of word */ |
| 720 | const char *endw; /* end of word */ |
| 721 | const char *endt; /* end of term */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 722 | struct sample_expr *expr; |
| 723 | struct sample_fetch *fetch; |
| 724 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 725 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 726 | char *fkw = NULL; |
| 727 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 728 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 729 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 730 | begw = str[*idx]; |
| 731 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 732 | |
| 733 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 734 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 735 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 736 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 737 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 738 | /* keep a copy of the current fetch keyword for error reporting */ |
| 739 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 740 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 741 | fetch = find_sample_fetch(begw, endw - begw); |
| 742 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 743 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 744 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 745 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 746 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 747 | endt = endw; |
| 748 | if (*endt == '(') { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 749 | /* look for the end of this term and skip the opening parenthesis */ |
| 750 | endt = ++endw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 751 | while (*endt && *endt != ')') |
| 752 | endt++; |
| 753 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 754 | memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 755 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 756 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 757 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 758 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 759 | /* At this point, we have : |
| 760 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 761 | * - endw : end of the keyword, first character not part of keyword |
| 762 | * nor the opening parenthesis (so first character of args |
| 763 | * if present). |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 764 | * - endt : end of the term (=endw or last parenthesis if args are present) |
| 765 | */ |
| 766 | |
| 767 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 768 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 769 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 770 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 771 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 772 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 773 | expr = calloc(1, sizeof(struct sample_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 774 | if (!expr) |
| 775 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 776 | |
| 777 | LIST_INIT(&(expr->conv_exprs)); |
| 778 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 779 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 780 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 781 | /* Note that we call the argument parser even with an empty string, |
| 782 | * this allows it to automatically create entries for mandatory |
| 783 | * implicit arguments (eg: local proxy name). |
| 784 | */ |
| 785 | al->kw = expr->fetch->kw; |
| 786 | al->conv = NULL; |
| 787 | if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 788 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 789 | goto out_error; |
| 790 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 791 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 792 | if (!expr->arg_p) { |
| 793 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 794 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 795 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 796 | 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] | 797 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 800 | /* Now process the converters if any. We have two supported syntaxes |
| 801 | * for the converters, which can be combined : |
| 802 | * - comma-delimited list of converters just after the keyword and args ; |
| 803 | * - one converter per keyword |
| 804 | * The combination allows to have each keyword being a comma-delimited |
| 805 | * series of converters. |
| 806 | * |
| 807 | * We want to process the former first, then the latter. For this we start |
| 808 | * from the beginning of the supposed place in the exiting conv chain, which |
| 809 | * starts at the last comma (endt). |
| 810 | */ |
| 811 | |
| 812 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 813 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 814 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 815 | if (*endt == ')') /* skip last closing parenthesis */ |
| 816 | endt++; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 817 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 818 | if (*endt && *endt != ',') { |
| 819 | if (ckw) |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 820 | memprintf(err_msg, "missing comma after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 821 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 822 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 823 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 824 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 825 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 826 | while (*endt == ',') /* then trailing commas */ |
| 827 | endt++; |
| 828 | |
| 829 | begw = endt; /* start of conv keyword */ |
| 830 | |
| 831 | if (!*begw) { |
| 832 | /* none ? skip to next string */ |
| 833 | (*idx)++; |
| 834 | begw = str[*idx]; |
| 835 | if (!begw || !*begw) |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 840 | |
| 841 | free(ckw); |
| 842 | ckw = my_strndup(begw, endw - begw); |
| 843 | |
| 844 | conv = find_sample_conv(begw, endw - begw); |
| 845 | if (!conv) { |
| 846 | /* we found an isolated keyword that we don't know, it's not ours */ |
| 847 | if (begw == str[*idx]) |
| 848 | break; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 849 | memprintf(err_msg, "unknown conv method '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 850 | goto out_error; |
| 851 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 852 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 853 | endt = endw; |
| 854 | if (*endt == '(') { |
| 855 | /* look for the end of this term */ |
| 856 | while (*endt && *endt != ')') |
| 857 | endt++; |
| 858 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 859 | memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 860 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 861 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 865 | memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 866 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 867 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 868 | |
| 869 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 870 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 871 | memprintf(err_msg, "conv method '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 872 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 873 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 874 | |
| 875 | prev_type = conv->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 876 | conv_expr = calloc(1, sizeof(struct sample_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 877 | if (!conv_expr) |
| 878 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 879 | |
| 880 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 881 | conv_expr->conv = conv; |
| 882 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 883 | if (endt != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 884 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 885 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 886 | if (!conv->arg_mask) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 887 | memprintf(err_msg, "conv method '%s' does not support any args", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 888 | goto out_error; |
| 889 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 890 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 891 | al->kw = expr->fetch->kw; |
| 892 | al->conv = conv_expr->conv->kw; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 893 | if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 894 | 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] | 895 | goto out_error; |
| 896 | } |
| 897 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 898 | if (!conv_expr->arg_p) |
| 899 | conv_expr->arg_p = empty_arg_list; |
| 900 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 901 | 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] | 902 | 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] | 903 | goto out_error; |
| 904 | } |
| 905 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 906 | else if (ARGM(conv->arg_mask)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 907 | memprintf(err_msg, "missing args for conv method '%s'", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 908 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 909 | } |
| 910 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 911 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 912 | out: |
| 913 | free(fkw); |
| 914 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 915 | return expr; |
| 916 | |
| 917 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 918 | /* TODO: prune_sample_expr(expr); */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 919 | expr = NULL; |
| 920 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 924 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 925 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 926 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 927 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 928 | * 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] | 929 | * 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] | 930 | * |
| 931 | * Note: the fetch functions are required to properly set the return type. The |
| 932 | * conversion functions must do so too. However the cast functions do not need |
| 933 | * 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] | 934 | * |
| 935 | * The caller may indicate in <opt> if it considers the result final or not. |
| 936 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 937 | * if the result is stable or not, according to the following table : |
| 938 | * |
| 939 | * return MAY_CHANGE FINAL Meaning for the sample |
| 940 | * NULL 0 * Not present and will never be (eg: header) |
| 941 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 942 | * NULL 1 1 Not present yet, will not change anymore |
| 943 | * smp 0 * Present and will not change (eg: header) |
| 944 | * smp 1 0 Present, may change (eg: request length) |
| 945 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 946 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 947 | struct sample *sample_process(struct proxy *px, struct session *l4, void *l7, |
| 948 | unsigned int opt, |
| 949 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 950 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 951 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 952 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 953 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 954 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 955 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 956 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 957 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 958 | if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 959 | return NULL; |
| 960 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 961 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 962 | /* we want to ensure that p->type can be casted into |
| 963 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 964 | * - NULL => not castable. |
| 965 | * - c_none => nothing to do (let's optimize it) |
| 966 | * - other => apply cast and prepare to fail |
| 967 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 968 | if (!sample_casts[p->type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 969 | return NULL; |
| 970 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 971 | if (sample_casts[p->type][conv_expr->conv->in_type] != c_none && |
| 972 | !sample_casts[p->type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 973 | return NULL; |
| 974 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 975 | /* OK cast succeeded */ |
| 976 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 977 | if (!conv_expr->conv->process(conv_expr->arg_p, p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 978 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 979 | } |
| 980 | return p; |
| 981 | } |
| 982 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 983 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 984 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
| 985 | * errors or 0 if everything is fine. |
| 986 | */ |
| 987 | int smp_resolve_args(struct proxy *p) |
| 988 | { |
| 989 | struct arg_list *cur, *bak; |
| 990 | const char *ctx, *where; |
| 991 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 992 | struct userlist *ul; |
| 993 | struct arg *arg; |
| 994 | int cfgerr = 0; |
| 995 | |
| 996 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 997 | struct proxy *px; |
| 998 | struct server *srv; |
| 999 | char *pname, *sname; |
| 1000 | |
| 1001 | arg = cur->arg; |
| 1002 | |
| 1003 | /* prepare output messages */ |
| 1004 | conv_pre = conv_pos = conv_ctx = ""; |
| 1005 | if (cur->conv) { |
| 1006 | conv_ctx = cur->conv; |
| 1007 | conv_pre = "conversion keyword '"; |
| 1008 | conv_pos = "' for "; |
| 1009 | } |
| 1010 | |
| 1011 | where = "in"; |
| 1012 | ctx = "sample fetch keyword"; |
| 1013 | switch (cur->ctx) { |
| 1014 | case ARGC_STK:where = "in stick rule in"; break; |
| 1015 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1016 | case ARGC_LOG: where = "in log-format string in"; break; |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 1017 | case ARGC_HRQ: where = "in http-request header format string in"; break; |
| 1018 | case ARGC_HRS: where = "in http-response header format string in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1019 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 1020 | case ARGC_RDR: where = "in redirect format string in"; break; |
Willy Tarreau | 3a4ac42 | 2014-06-13 16:17:14 +0200 | [diff] [blame] | 1021 | case ARGC_CAP: where = "in capture rule in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1022 | case ARGC_ACL: ctx = "ACL keyword"; break; |
| 1023 | } |
| 1024 | |
| 1025 | /* set a few default settings */ |
| 1026 | px = p; |
| 1027 | pname = p->id; |
| 1028 | |
| 1029 | switch (arg->type) { |
| 1030 | case ARGT_SRV: |
| 1031 | if (!arg->data.str.len) { |
| 1032 | Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1033 | cur->file, cur->line, |
| 1034 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1035 | cfgerr++; |
| 1036 | continue; |
| 1037 | } |
| 1038 | |
| 1039 | /* we support two formats : "bck/srv" and "srv" */ |
| 1040 | sname = strrchr(arg->data.str.str, '/'); |
| 1041 | |
| 1042 | if (sname) { |
| 1043 | *sname++ = '\0'; |
| 1044 | pname = arg->data.str.str; |
| 1045 | |
| 1046 | px = findproxy(pname, PR_CAP_BE); |
| 1047 | if (!px) { |
| 1048 | Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1049 | cur->file, cur->line, pname, |
| 1050 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1051 | cfgerr++; |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | else |
| 1056 | sname = arg->data.str.str; |
| 1057 | |
| 1058 | srv = findserver(px, sname); |
| 1059 | if (!srv) { |
| 1060 | 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", |
| 1061 | cur->file, cur->line, sname, pname, |
| 1062 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1063 | cfgerr++; |
| 1064 | break; |
| 1065 | } |
| 1066 | |
| 1067 | free(arg->data.str.str); |
| 1068 | arg->data.str.str = NULL; |
| 1069 | arg->unresolved = 0; |
| 1070 | arg->data.srv = srv; |
| 1071 | break; |
| 1072 | |
| 1073 | case ARGT_FE: |
| 1074 | if (arg->data.str.len) { |
| 1075 | pname = arg->data.str.str; |
| 1076 | px = findproxy(pname, PR_CAP_FE); |
| 1077 | } |
| 1078 | |
| 1079 | if (!px) { |
| 1080 | Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1081 | cur->file, cur->line, pname, |
| 1082 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1083 | cfgerr++; |
| 1084 | break; |
| 1085 | } |
| 1086 | |
| 1087 | if (!(px->cap & PR_CAP_FE)) { |
| 1088 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n", |
| 1089 | cur->file, cur->line, pname, |
| 1090 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1091 | cfgerr++; |
| 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | free(arg->data.str.str); |
| 1096 | arg->data.str.str = NULL; |
| 1097 | arg->unresolved = 0; |
| 1098 | arg->data.prx = px; |
| 1099 | break; |
| 1100 | |
| 1101 | case ARGT_BE: |
| 1102 | if (arg->data.str.len) { |
| 1103 | pname = arg->data.str.str; |
| 1104 | px = findproxy(pname, PR_CAP_BE); |
| 1105 | } |
| 1106 | |
| 1107 | if (!px) { |
| 1108 | Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1109 | cur->file, cur->line, pname, |
| 1110 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1111 | cfgerr++; |
| 1112 | break; |
| 1113 | } |
| 1114 | |
| 1115 | if (!(px->cap & PR_CAP_BE)) { |
| 1116 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n", |
| 1117 | cur->file, cur->line, pname, |
| 1118 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1119 | cfgerr++; |
| 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | free(arg->data.str.str); |
| 1124 | arg->data.str.str = NULL; |
| 1125 | arg->unresolved = 0; |
| 1126 | arg->data.prx = px; |
| 1127 | break; |
| 1128 | |
| 1129 | case ARGT_TAB: |
| 1130 | if (arg->data.str.len) { |
| 1131 | pname = arg->data.str.str; |
| 1132 | px = find_stktable(pname); |
| 1133 | } |
| 1134 | |
| 1135 | if (!px) { |
| 1136 | Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1137 | cur->file, cur->line, pname, |
| 1138 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1139 | cfgerr++; |
| 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | if (!px->table.size) { |
| 1144 | Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1145 | cur->file, cur->line, pname, |
| 1146 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1147 | cfgerr++; |
| 1148 | break; |
| 1149 | } |
| 1150 | |
| 1151 | free(arg->data.str.str); |
| 1152 | arg->data.str.str = NULL; |
| 1153 | arg->unresolved = 0; |
| 1154 | arg->data.prx = px; |
| 1155 | break; |
| 1156 | |
| 1157 | case ARGT_USR: |
| 1158 | if (!arg->data.str.len) { |
| 1159 | Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1160 | cur->file, cur->line, |
| 1161 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1162 | cfgerr++; |
| 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | if (p->uri_auth && p->uri_auth->userlist && |
| 1167 | !strcmp(p->uri_auth->userlist->name, arg->data.str.str)) |
| 1168 | ul = p->uri_auth->userlist; |
| 1169 | else |
| 1170 | ul = auth_find_userlist(arg->data.str.str); |
| 1171 | |
| 1172 | if (!ul) { |
| 1173 | Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1174 | cur->file, cur->line, arg->data.str.str, |
| 1175 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1176 | cfgerr++; |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| 1180 | free(arg->data.str.str); |
| 1181 | arg->data.str.str = NULL; |
| 1182 | arg->unresolved = 0; |
| 1183 | arg->data.usr = ul; |
| 1184 | break; |
| 1185 | } |
| 1186 | |
| 1187 | LIST_DEL(&cur->list); |
| 1188 | free(cur); |
| 1189 | } /* end of args processing */ |
| 1190 | |
| 1191 | return cfgerr; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1195 | * Process a fetch + format conversion as defined by the sample expression |
| 1196 | * <expr> on request or response considering the <opt> parameter. The output is |
| 1197 | * always of type string. If a stable sample can be fetched, or an unstable one |
| 1198 | * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and |
| 1199 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1200 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1201 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1202 | * 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] | 1203 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1204 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1205 | * still hope to get it after waiting longer, and is not converted to string. |
| 1206 | * The possible output combinations are the following : |
| 1207 | * |
| 1208 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1209 | * NULL * * Not present and will never be (eg: header) |
| 1210 | * smp 0 * Final value converted (eg: header) |
| 1211 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1212 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1213 | */ |
| 1214 | struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7, |
| 1215 | unsigned int opt, struct sample_expr *expr) |
| 1216 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1217 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1218 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1219 | memset(smp, 0, sizeof(*smp)); |
| 1220 | |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1221 | if (!sample_process(px, l4, l7, opt, expr, smp)) { |
| 1222 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1223 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1224 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1225 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1226 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1227 | if (!sample_casts[smp->type][SMP_T_STR]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1228 | return NULL; |
| 1229 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1230 | if (!sample_casts[smp->type][SMP_T_STR](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1231 | return NULL; |
| 1232 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1233 | smp->type = SMP_T_STR; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1234 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1235 | return smp; |
| 1236 | } |
| 1237 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1238 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1239 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1240 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1241 | /*****************************************************************/ |
| 1242 | |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1243 | static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp) |
| 1244 | { |
| 1245 | struct chunk *trash = get_trash_chunk(); |
| 1246 | int b64_len; |
| 1247 | |
| 1248 | trash->len = 0; |
| 1249 | b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size); |
| 1250 | if (b64_len < 0) |
| 1251 | return 0; |
| 1252 | |
| 1253 | trash->len = b64_len; |
| 1254 | smp->data.str = *trash; |
| 1255 | smp->type = SMP_T_STR; |
| 1256 | smp->flags &= ~SMP_F_CONST; |
| 1257 | return 1; |
| 1258 | } |
| 1259 | |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1260 | static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp) |
| 1261 | { |
| 1262 | struct chunk *trash = get_trash_chunk(); |
| 1263 | unsigned char c; |
| 1264 | int ptr = 0; |
| 1265 | |
| 1266 | trash->len = 0; |
| 1267 | while (ptr < smp->data.str.len && trash->len <= trash->size - 2) { |
| 1268 | c = smp->data.str.str[ptr++]; |
| 1269 | trash->str[trash->len++] = hextab[(c >> 4) & 0xF]; |
| 1270 | trash->str[trash->len++] = hextab[c & 0xF]; |
| 1271 | } |
| 1272 | smp->data.str = *trash; |
| 1273 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1274 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1275 | return 1; |
| 1276 | } |
| 1277 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1278 | /* hashes the binary input into a 32-bit unsigned int */ |
| 1279 | static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp) |
| 1280 | { |
| 1281 | smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len); |
| 1282 | if (arg_p && arg_p->data.uint) |
| 1283 | smp->data.uint = full_hash(smp->data.uint); |
| 1284 | smp->type = SMP_T_UINT; |
| 1285 | return 1; |
| 1286 | } |
| 1287 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1288 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1289 | { |
| 1290 | int i; |
| 1291 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1292 | if (!smp_dup(smp)) |
| 1293 | return 0; |
| 1294 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1295 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1296 | return 0; |
| 1297 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1298 | for (i = 0; i < smp->data.str.len; i++) { |
| 1299 | if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z')) |
| 1300 | smp->data.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1301 | } |
| 1302 | return 1; |
| 1303 | } |
| 1304 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1305 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1306 | { |
| 1307 | int i; |
| 1308 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1309 | if (!smp_dup(smp)) |
| 1310 | return 0; |
| 1311 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1312 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1313 | return 0; |
| 1314 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1315 | for (i = 0; i < smp->data.str.len; i++) { |
| 1316 | if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z')) |
| 1317 | smp->data.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1318 | } |
| 1319 | return 1; |
| 1320 | } |
| 1321 | |
Willy Tarreau | f995410 | 2012-04-20 14:03:29 +0200 | [diff] [blame] | 1322 | /* takes the netmask in arg_p */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1323 | static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1324 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1325 | smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr; |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1326 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1327 | return 1; |
| 1328 | } |
| 1329 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1330 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1331 | * adds an optional offset found in args[1] and emits a string representing |
| 1332 | * the local time in the format specified in args[1] using strftime(). |
| 1333 | */ |
| 1334 | static int sample_conv_ltime(const struct arg *args, struct sample *smp) |
| 1335 | { |
| 1336 | struct chunk *temp; |
| 1337 | time_t curr_date = smp->data.uint; |
| 1338 | |
| 1339 | /* add offset */ |
| 1340 | if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT) |
| 1341 | curr_date += args[1].data.sint; |
| 1342 | |
| 1343 | temp = get_trash_chunk(); |
| 1344 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date)); |
| 1345 | smp->data.str = *temp; |
| 1346 | smp->type = SMP_T_STR; |
| 1347 | return 1; |
| 1348 | } |
| 1349 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1350 | /* hashes the binary input into a 32-bit unsigned int */ |
| 1351 | static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp) |
| 1352 | { |
| 1353 | smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len); |
| 1354 | if (arg_p && arg_p->data.uint) |
| 1355 | smp->data.uint = full_hash(smp->data.uint); |
| 1356 | smp->type = SMP_T_UINT; |
| 1357 | return 1; |
| 1358 | } |
| 1359 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1360 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1361 | * adds an optional offset found in args[1] and emits a string representing |
| 1362 | * the UTC date in the format specified in args[1] using strftime(). |
| 1363 | */ |
| 1364 | static int sample_conv_utime(const struct arg *args, struct sample *smp) |
| 1365 | { |
| 1366 | struct chunk *temp; |
| 1367 | time_t curr_date = smp->data.uint; |
| 1368 | |
| 1369 | /* add offset */ |
| 1370 | if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT) |
| 1371 | curr_date += args[1].data.sint; |
| 1372 | |
| 1373 | temp = get_trash_chunk(); |
| 1374 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date)); |
| 1375 | smp->data.str = *temp; |
| 1376 | smp->type = SMP_T_STR; |
| 1377 | return 1; |
| 1378 | } |
| 1379 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1380 | /* hashes the binary input into a 32-bit unsigned int */ |
| 1381 | static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp) |
| 1382 | { |
| 1383 | smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len); |
| 1384 | if (arg_p && arg_p->data.uint) |
| 1385 | smp->data.uint = full_hash(smp->data.uint); |
| 1386 | smp->type = SMP_T_UINT; |
| 1387 | return 1; |
| 1388 | } |
| 1389 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1390 | /* This function escape special json characters. The returned string can be |
| 1391 | * safely set between two '"' and used as json string. The json string is |
| 1392 | * defined like this: |
| 1393 | * |
| 1394 | * any Unicode character except '"' or '\' or control character |
| 1395 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 1396 | * |
| 1397 | * The enum input_type contain all the allowed mode for decoding the input |
| 1398 | * string. |
| 1399 | */ |
| 1400 | enum input_type { |
| 1401 | IT_ASCII = 0, |
| 1402 | IT_UTF8, |
| 1403 | IT_UTF8S, |
| 1404 | IT_UTF8P, |
| 1405 | IT_UTF8PS, |
| 1406 | }; |
| 1407 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 1408 | const char *file, int line, char **err) |
| 1409 | { |
| 1410 | if (!arg) { |
| 1411 | memprintf(err, "Unexpected empty arg list"); |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | if (arg->type != ARGT_STR) { |
| 1416 | memprintf(err, "Unexpected arg type"); |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | if (strcmp(arg->data.str.str, "") == 0) { |
| 1421 | arg->type = ARGT_UINT; |
| 1422 | arg->data.uint = IT_ASCII; |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |
| 1426 | else if (strcmp(arg->data.str.str, "ascii") == 0) { |
| 1427 | arg->type = ARGT_UINT; |
| 1428 | arg->data.uint = IT_ASCII; |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
| 1432 | else if (strcmp(arg->data.str.str, "utf8") == 0) { |
| 1433 | arg->type = ARGT_UINT; |
| 1434 | arg->data.uint = IT_UTF8; |
| 1435 | return 1; |
| 1436 | } |
| 1437 | |
| 1438 | else if (strcmp(arg->data.str.str, "utf8s") == 0) { |
| 1439 | arg->type = ARGT_UINT; |
| 1440 | arg->data.uint = IT_UTF8S; |
| 1441 | return 1; |
| 1442 | } |
| 1443 | |
| 1444 | else if (strcmp(arg->data.str.str, "utf8p") == 0) { |
| 1445 | arg->type = ARGT_UINT; |
| 1446 | arg->data.uint = IT_UTF8P; |
| 1447 | return 1; |
| 1448 | } |
| 1449 | |
| 1450 | else if (strcmp(arg->data.str.str, "utf8ps") == 0) { |
| 1451 | arg->type = ARGT_UINT; |
| 1452 | arg->data.uint = IT_UTF8PS; |
| 1453 | return 1; |
| 1454 | } |
| 1455 | |
| 1456 | memprintf(err, "Unexpected input code type at file '%s', line %d. " |
| 1457 | "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line); |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | static int sample_conv_json(const struct arg *arg_p, struct sample *smp) |
| 1462 | { |
| 1463 | struct chunk *temp; |
| 1464 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 1465 | const char *str; |
| 1466 | int len; |
| 1467 | enum input_type input_type = IT_ASCII; |
| 1468 | unsigned int c; |
| 1469 | unsigned int ret; |
| 1470 | char *p; |
| 1471 | |
| 1472 | if (arg_p) |
| 1473 | input_type = arg_p->data.uint; |
| 1474 | |
| 1475 | temp = get_trash_chunk(); |
| 1476 | temp->len = 0; |
| 1477 | |
| 1478 | p = smp->data.str.str; |
| 1479 | while (p < smp->data.str.str + smp->data.str.len) { |
| 1480 | |
| 1481 | if (input_type == IT_ASCII) { |
| 1482 | /* Read input as ASCII. */ |
| 1483 | c = *(unsigned char *)p; |
| 1484 | p++; |
| 1485 | } |
| 1486 | else { |
| 1487 | /* Read input as UTF8. */ |
| 1488 | ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c); |
| 1489 | p += utf8_return_length(ret); |
| 1490 | |
| 1491 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1492 | return 0; |
| 1493 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1494 | continue; |
| 1495 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1496 | return 0; |
| 1497 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1498 | continue; |
| 1499 | |
| 1500 | /* Check too big values. */ |
| 1501 | if ((unsigned int)c > 0xffff) { |
| 1502 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 1503 | return 0; |
| 1504 | continue; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /* Convert character. */ |
| 1509 | if (c == '"') { |
| 1510 | len = 2; |
| 1511 | str = "\\\""; |
| 1512 | } |
| 1513 | else if (c == '\\') { |
| 1514 | len = 2; |
| 1515 | str = "\\\\"; |
| 1516 | } |
| 1517 | else if (c == '/') { |
| 1518 | len = 2; |
| 1519 | str = "\\/"; |
| 1520 | } |
| 1521 | else if (c == '\b') { |
| 1522 | len = 2; |
| 1523 | str = "\\b"; |
| 1524 | } |
| 1525 | else if (c == '\f') { |
| 1526 | len = 2; |
| 1527 | str = "\\f"; |
| 1528 | } |
| 1529 | else if (c == '\r') { |
| 1530 | len = 2; |
| 1531 | str = "\\r"; |
| 1532 | } |
| 1533 | else if (c == '\n') { |
| 1534 | len = 2; |
| 1535 | str = "\\n"; |
| 1536 | } |
| 1537 | else if (c == '\t') { |
| 1538 | len = 2; |
| 1539 | str = "\\t"; |
| 1540 | } |
| 1541 | else if (c > 0xff || !isprint(c)) { |
| 1542 | /* isprint generate a segfault if c is too big. The man says that |
| 1543 | * c must have the value of an unsigned char or EOF. |
| 1544 | */ |
| 1545 | len = 6; |
| 1546 | _str[0] = '\\'; |
| 1547 | _str[1] = 'u'; |
| 1548 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 1549 | str = _str; |
| 1550 | } |
| 1551 | else { |
| 1552 | len = 1; |
| 1553 | str = (char *)&c; |
| 1554 | } |
| 1555 | |
| 1556 | /* Check length */ |
| 1557 | if (temp->len + len > temp->size) |
| 1558 | return 0; |
| 1559 | |
| 1560 | /* Copy string. */ |
| 1561 | memcpy(temp->str + temp->len, str, len); |
| 1562 | temp->len += len; |
| 1563 | } |
| 1564 | |
| 1565 | smp->flags &= ~SMP_F_CONST; |
| 1566 | smp->data.str = *temp; |
| 1567 | smp->type = SMP_T_STR; |
| 1568 | |
| 1569 | return 1; |
| 1570 | } |
| 1571 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1572 | /************************************************************************/ |
| 1573 | /* All supported sample fetch functions must be declared here */ |
| 1574 | /************************************************************************/ |
| 1575 | |
| 1576 | /* force TRUE to be returned at the fetch level */ |
| 1577 | static int |
| 1578 | smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1579 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1580 | { |
| 1581 | smp->type = SMP_T_BOOL; |
| 1582 | smp->data.uint = 1; |
| 1583 | return 1; |
| 1584 | } |
| 1585 | |
| 1586 | /* force FALSE to be returned at the fetch level */ |
| 1587 | static int |
| 1588 | smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1589 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1590 | { |
| 1591 | smp->type = SMP_T_BOOL; |
| 1592 | smp->data.uint = 0; |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | /* retrieve environment variable $1 as a string */ |
| 1597 | static int |
| 1598 | smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1599 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1600 | { |
| 1601 | char *env; |
| 1602 | |
| 1603 | if (!args || args[0].type != ARGT_STR) |
| 1604 | return 0; |
| 1605 | |
| 1606 | env = getenv(args[0].data.str.str); |
| 1607 | if (!env) |
| 1608 | return 0; |
| 1609 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1610 | smp->type = SMP_T_STR; |
| 1611 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1612 | smp->data.str.str = env; |
| 1613 | smp->data.str.len = strlen(env); |
| 1614 | return 1; |
| 1615 | } |
| 1616 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 1617 | /* retrieve the current local date in epoch time, and applies an optional offset |
| 1618 | * of args[0] seconds. |
| 1619 | */ |
| 1620 | static int |
| 1621 | smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1622 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 1623 | { |
| 1624 | smp->data.uint = date.tv_sec; |
| 1625 | |
| 1626 | /* add offset */ |
| 1627 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 1628 | smp->data.uint += args[0].data.sint; |
| 1629 | |
| 1630 | smp->type = SMP_T_UINT; |
| 1631 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1635 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 1636 | * range specified in argument. |
| 1637 | */ |
| 1638 | static int |
| 1639 | smp_fetch_rand(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
| 1640 | const struct arg *args, struct sample *smp, const char *kw) |
| 1641 | { |
| 1642 | smp->data.uint = random(); |
| 1643 | |
| 1644 | /* reduce if needed. Don't do a modulo, use all bits! */ |
| 1645 | if (args && args[0].type == ARGT_UINT) |
| 1646 | smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) >> 32; |
| 1647 | |
| 1648 | smp->type = SMP_T_UINT; |
| 1649 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 1650 | return 1; |
| 1651 | } |
| 1652 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1653 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1654 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1655 | * common denominator, the type that can be casted into all other ones. For |
| 1656 | * instance IPv4/IPv6 must be declared IPv4. |
| 1657 | */ |
| 1658 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 1659 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 1660 | { "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] | 1661 | { "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] | 1662 | { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 1663 | { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1664 | { /* END */ }, |
| 1665 | }}; |
| 1666 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1667 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1668 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1669 | { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1670 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 1671 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1672 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1673 | { "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] | 1674 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
| 1675 | { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1676 | { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
| 1677 | { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT }, |
| 1678 | { "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] | 1679 | { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 1680 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1681 | }}; |
| 1682 | |
| 1683 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1684 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1685 | { |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 1686 | /* register sample fetch and format conversion keywords */ |
| 1687 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1688 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1689 | } |