Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 2 | * include/haproxy/sample.h |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 3 | * Functions for samples management. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation, version 2.1 |
| 11 | * exclusively. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 23 | #ifndef _HAPROXY_SAMPLE_H |
| 24 | #define _HAPROXY_SAMPLE_H |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 26 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/arg-t.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 28 | #include <haproxy/sample-t.h> |
Willy Tarreau | 872f2ea | 2020-06-04 18:46:44 +0200 | [diff] [blame] | 29 | #include <haproxy/stick_table-t.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 30 | |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 31 | extern sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES]; |
| 32 | extern const unsigned int fetch_cap[SMP_SRC_ENTRIES]; |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 33 | extern const char *smp_to_type[SMP_TYPES]; |
| 34 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 35 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al, char **endptr); |
Thierry FOURNIER | 20f4996 | 2013-11-21 10:51:50 +0100 | [diff] [blame] | 36 | struct sample_conv *find_sample_conv(const char *kw, int len); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 37 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 38 | struct stream *strm, unsigned int opt, |
| 39 | struct sample_expr *expr, struct sample *p); |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 40 | struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 41 | struct stream *strm, unsigned int opt, |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 42 | struct sample_expr *expr, int smp_type); |
Willy Tarreau | ea57350 | 2021-10-06 15:29:00 +0200 | [diff] [blame] | 43 | int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type); |
Willy Tarreau | 9cfb71f | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 44 | int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp); |
Willy Tarreau | 05cd2e6 | 2021-10-06 15:20:18 +0200 | [diff] [blame] | 45 | int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 46 | void release_sample_expr(struct sample_expr *expr); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 47 | void sample_register_fetches(struct sample_fetch_kw_list *psl); |
| 48 | void sample_register_convs(struct sample_conv_kw_list *psl); |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 49 | const char *sample_src_names(unsigned int use); |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 50 | const char *sample_ckp_names(unsigned int use); |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 51 | struct sample_fetch *find_sample_fetch(const char *kw, int len); |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 52 | struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx); |
Thierry FOURNIER | 8fd1376 | 2015-03-10 23:56:48 +0100 | [diff] [blame] | 53 | struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx); |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 54 | int smp_resolve_args(struct proxy *p, char **err); |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 55 | int smp_check_date_unit(struct arg *args, char **err); |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 56 | int smp_expr_output_type(struct sample_expr *expr); |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 57 | int c_none(struct sample *smp); |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 58 | int smp_dup(struct sample *smp); |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 59 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 60 | /* |
| 61 | * This function just apply a cast on sample. It returns 0 if the cast is not |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 62 | * available or if the cast fails, otherwise returns 1. It does not modify the |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 63 | * input sample on failure. |
| 64 | */ |
| 65 | static inline |
| 66 | int sample_convert(struct sample *sample, int req_type) |
| 67 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 68 | if (!sample_casts[sample->data.type][req_type]) |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 69 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 70 | if (sample_casts[sample->data.type][req_type] == c_none) |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 71 | return 1; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 72 | return sample_casts[sample->data.type][req_type](sample); |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 75 | static inline |
| 76 | struct sample *smp_set_owner(struct sample *smp, struct proxy *px, |
| 77 | struct session *sess, struct stream *strm, int opt) |
| 78 | { |
| 79 | smp->px = px; |
| 80 | smp->sess = sess; |
| 81 | smp->strm = strm; |
| 82 | smp->opt = opt; |
| 83 | return smp; |
| 84 | } |
| 85 | |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 86 | |
| 87 | /* Returns 1 if a sample may be safely used. It performs a few checks on the |
| 88 | * string length versus size, same for the binary version, and ensures that |
| 89 | * strings are properly terminated by a zero. If this last point is not granted |
| 90 | * but the string is not const, then the \0 is appended. Otherwise it returns 0, |
| 91 | * meaning the caller may need to call smp_dup() before going further. |
| 92 | */ |
| 93 | static inline |
| 94 | int smp_is_safe(struct sample *smp) |
| 95 | { |
| 96 | switch (smp->data.type) { |
Christopher Faulet | 5db105e | 2017-07-24 16:07:12 +0200 | [diff] [blame] | 97 | case SMP_T_METH: |
| 98 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 99 | return 1; |
| 100 | /* Fall through */ |
| 101 | |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 102 | case SMP_T_STR: |
Christopher Faulet | 8dd40fb | 2021-02-18 10:22:48 +0100 | [diff] [blame] | 103 | if (!smp->data.u.str.size || smp->data.u.str.data >= smp->data.u.str.size) |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 104 | return 0; |
| 105 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 106 | if (smp->data.u.str.area[smp->data.u.str.data] == 0) |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 107 | return 1; |
| 108 | |
Christopher Faulet | 8dd40fb | 2021-02-18 10:22:48 +0100 | [diff] [blame] | 109 | if (smp->flags & SMP_F_CONST) |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 110 | return 0; |
| 111 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 112 | smp->data.u.str.area[smp->data.u.str.data] = 0; |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 113 | return 1; |
| 114 | |
| 115 | case SMP_T_BIN: |
Willy Tarreau | b509232 | 2018-08-22 05:07:14 +0200 | [diff] [blame] | 116 | return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size; |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 117 | |
| 118 | default: |
| 119 | return 1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* checks that a sample may freely be used, or duplicates it to normalize it. |
| 124 | * Returns 1 on success, 0 if the sample must not be used. The function also |
| 125 | * checks for NULL to simplify the calling code. |
| 126 | */ |
| 127 | static inline |
| 128 | int smp_make_safe(struct sample *smp) |
| 129 | { |
| 130 | return smp && (smp_is_safe(smp) || smp_dup(smp)); |
| 131 | } |
| 132 | |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 133 | /* Returns 1 if a sample may be safely modified in place. It performs a few |
| 134 | * checks on the string length versus size, same for the binary version, and |
| 135 | * ensures that strings are properly terminated by a zero, and of course that |
| 136 | * the size is allocate and that the SMP_F_CONST flag is not set. If only the |
| 137 | * trailing zero is missing, it is appended. Otherwise it returns 0, meaning |
| 138 | * the caller may need to call smp_dup() before going further. |
| 139 | */ |
| 140 | static inline |
| 141 | int smp_is_rw(struct sample *smp) |
| 142 | { |
| 143 | if (smp->flags & SMP_F_CONST) |
| 144 | return 0; |
| 145 | |
| 146 | switch (smp->data.type) { |
Christopher Faulet | 5db105e | 2017-07-24 16:07:12 +0200 | [diff] [blame] | 147 | case SMP_T_METH: |
| 148 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 149 | return 1; |
| 150 | /* Fall through */ |
| 151 | |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 152 | case SMP_T_STR: |
| 153 | if (!smp->data.u.str.size || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 154 | smp->data.u.str.data >= smp->data.u.str.size) |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 157 | if (smp->data.u.str.area[smp->data.u.str.data] != 0) |
| 158 | smp->data.u.str.area[smp->data.u.str.data] = 0; |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 159 | return 1; |
| 160 | |
| 161 | case SMP_T_BIN: |
| 162 | return smp->data.u.str.size && |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 163 | smp->data.u.str.data <= smp->data.u.str.size; |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 164 | |
| 165 | default: |
| 166 | return 1; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* checks that a sample may freely be modified, or duplicates it to normalize |
| 171 | * it and make it R/W. Returns 1 on success, 0 if the sample must not be used. |
| 172 | * The function also checks for NULL to simplify the calling code. |
| 173 | */ |
| 174 | static inline |
| 175 | int smp_make_rw(struct sample *smp) |
| 176 | { |
| 177 | return smp && (smp_is_rw(smp) || smp_dup(smp)); |
| 178 | } |
| 179 | |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 180 | #endif /* _HAPROXY_SAMPLE_H */ |