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 | * include/proto/sample.h |
| 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 | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 23 | #ifndef _PROTO_SAMPLE_H |
| 24 | #define _PROTO_SAMPLE_H |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 26 | #include <types/sample.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 27 | #include <types/stick_table.h> |
| 28 | |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 29 | extern const char *smp_to_type[SMP_TYPES]; |
| 30 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 31 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al); |
Thierry FOURNIER | 20f4996 | 2013-11-21 10:51:50 +0100 | [diff] [blame] | 32 | struct sample_conv *find_sample_conv(const char *kw, int len); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 33 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 34 | struct stream *strm, unsigned int opt, |
| 35 | struct sample_expr *expr, struct sample *p); |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 36 | struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 37 | struct stream *strm, unsigned int opt, |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 38 | struct sample_expr *expr, int smp_type); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 39 | void release_sample_expr(struct sample_expr *expr); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 40 | void sample_register_fetches(struct sample_fetch_kw_list *psl); |
| 41 | void sample_register_convs(struct sample_conv_kw_list *psl); |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 42 | const char *sample_src_names(unsigned int use); |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 43 | const char *sample_ckp_names(unsigned int use); |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 44 | struct sample_fetch *find_sample_fetch(const char *kw, int len); |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 45 | struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx); |
Thierry FOURNIER | 8fd1376 | 2015-03-10 23:56:48 +0100 | [diff] [blame] | 46 | struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 47 | int smp_resolve_args(struct proxy *p); |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 48 | int smp_expr_output_type(struct sample_expr *expr); |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 49 | int c_none(struct sample *smp); |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 50 | int smp_dup(struct sample *smp); |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 51 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 52 | /* |
| 53 | * 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] | 54 | * 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] | 55 | * input sample on failure. |
| 56 | */ |
| 57 | static inline |
| 58 | int sample_convert(struct sample *sample, int req_type) |
| 59 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 60 | if (!sample_casts[sample->data.type][req_type]) |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 61 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 62 | if (sample_casts[sample->data.type][req_type] == c_none) |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 63 | return 1; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 64 | return sample_casts[sample->data.type][req_type](sample); |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 67 | static inline |
| 68 | struct sample *smp_set_owner(struct sample *smp, struct proxy *px, |
| 69 | struct session *sess, struct stream *strm, int opt) |
| 70 | { |
| 71 | smp->px = px; |
| 72 | smp->sess = sess; |
| 73 | smp->strm = strm; |
| 74 | smp->opt = opt; |
| 75 | return smp; |
| 76 | } |
| 77 | |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 78 | |
| 79 | /* Returns 1 if a sample may be safely used. It performs a few checks on the |
| 80 | * string length versus size, same for the binary version, and ensures that |
| 81 | * strings are properly terminated by a zero. If this last point is not granted |
| 82 | * but the string is not const, then the \0 is appended. Otherwise it returns 0, |
| 83 | * meaning the caller may need to call smp_dup() before going further. |
| 84 | */ |
| 85 | static inline |
| 86 | int smp_is_safe(struct sample *smp) |
| 87 | { |
| 88 | switch (smp->data.type) { |
Christopher Faulet | 5db105e | 2017-07-24 16:07:12 +0200 | [diff] [blame] | 89 | case SMP_T_METH: |
| 90 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 91 | return 1; |
| 92 | /* Fall through */ |
| 93 | |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 94 | case SMP_T_STR: |
Willy Tarreau | b509232 | 2018-08-22 05:07:14 +0200 | [diff] [blame] | 95 | 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] | 96 | return 0; |
| 97 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 98 | if (smp->data.u.str.area[smp->data.u.str.data] == 0) |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 99 | return 1; |
| 100 | |
| 101 | if (!smp->data.u.str.size || (smp->flags & SMP_F_CONST)) |
| 102 | return 0; |
| 103 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 104 | smp->data.u.str.area[smp->data.u.str.data] = 0; |
Willy Tarreau | 2c59479 | 2016-08-09 11:37:54 +0200 | [diff] [blame] | 105 | return 1; |
| 106 | |
| 107 | case SMP_T_BIN: |
Willy Tarreau | b509232 | 2018-08-22 05:07:14 +0200 | [diff] [blame] | 108 | 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] | 109 | |
| 110 | default: |
| 111 | return 1; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* checks that a sample may freely be used, or duplicates it to normalize it. |
| 116 | * Returns 1 on success, 0 if the sample must not be used. The function also |
| 117 | * checks for NULL to simplify the calling code. |
| 118 | */ |
| 119 | static inline |
| 120 | int smp_make_safe(struct sample *smp) |
| 121 | { |
| 122 | return smp && (smp_is_safe(smp) || smp_dup(smp)); |
| 123 | } |
| 124 | |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 125 | /* Returns 1 if a sample may be safely modified in place. It performs a few |
| 126 | * checks on the string length versus size, same for the binary version, and |
| 127 | * ensures that strings are properly terminated by a zero, and of course that |
| 128 | * the size is allocate and that the SMP_F_CONST flag is not set. If only the |
| 129 | * trailing zero is missing, it is appended. Otherwise it returns 0, meaning |
| 130 | * the caller may need to call smp_dup() before going further. |
| 131 | */ |
| 132 | static inline |
| 133 | int smp_is_rw(struct sample *smp) |
| 134 | { |
| 135 | if (smp->flags & SMP_F_CONST) |
| 136 | return 0; |
| 137 | |
| 138 | switch (smp->data.type) { |
Christopher Faulet | 5db105e | 2017-07-24 16:07:12 +0200 | [diff] [blame] | 139 | case SMP_T_METH: |
| 140 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 141 | return 1; |
| 142 | /* Fall through */ |
| 143 | |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 144 | case SMP_T_STR: |
| 145 | if (!smp->data.u.str.size || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 146 | smp->data.u.str.data >= smp->data.u.str.size) |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 149 | if (smp->data.u.str.area[smp->data.u.str.data] != 0) |
| 150 | smp->data.u.str.area[smp->data.u.str.data] = 0; |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 151 | return 1; |
| 152 | |
| 153 | case SMP_T_BIN: |
| 154 | return smp->data.u.str.size && |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 155 | smp->data.u.str.data <= smp->data.u.str.size; |
Willy Tarreau | 77128f5 | 2016-08-09 11:49:20 +0200 | [diff] [blame] | 156 | |
| 157 | default: |
| 158 | return 1; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* checks that a sample may freely be modified, or duplicates it to normalize |
| 163 | * it and make it R/W. Returns 1 on success, 0 if the sample must not be used. |
| 164 | * The function also checks for NULL to simplify the calling code. |
| 165 | */ |
| 166 | static inline |
| 167 | int smp_make_rw(struct sample *smp) |
| 168 | { |
| 169 | return smp && (smp_is_rw(smp) || smp_dup(smp)); |
| 170 | } |
| 171 | |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 172 | #endif /* _PROTO_SAMPLE_H */ |