Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 36 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 37 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 40 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 41 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 43 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 44 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 45 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 46 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 47 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 48 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 49 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 50 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 51 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 52 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 53 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 54 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 55 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 56 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 57 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 58 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 59 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 60 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 61 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 62 | #include <haproxy/xref.h> |
| 63 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 64 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 65 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 66 | * macro is used only for identifying the function that can |
| 67 | * not return because a longjmp is executed. |
| 68 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 69 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 70 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 71 | */ |
| 72 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 73 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 74 | #define MAY_LJMP(func) func |
| 75 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 76 | /* This couple of function executes securely some Lua calls outside of |
| 77 | * the lua runtime environment. Each Lua call can return a longjmp |
| 78 | * if it encounter a memory error. |
| 79 | * |
| 80 | * Lua documentation extract: |
| 81 | * |
| 82 | * If an error happens outside any protected environment, Lua calls |
| 83 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 84 | * exiting the host application. Your panic function can avoid this |
| 85 | * exit by never returning (e.g., doing a long jump to your own |
| 86 | * recovery point outside Lua). |
| 87 | * |
| 88 | * The panic function runs as if it were a message handler (see |
| 89 | * §2.3); in particular, the error message is at the top of the |
| 90 | * stack. However, there is no guarantee about stack space. To push |
| 91 | * anything on the stack, the panic function must first check the |
| 92 | * available space (see §4.2). |
| 93 | * |
| 94 | * We must check all the Lua entry point. This includes: |
| 95 | * - The include/proto/hlua.h exported functions |
| 96 | * - the task wrapper function |
| 97 | * - The action wrapper function |
| 98 | * - The converters wrapper function |
| 99 | * - The sample-fetch wrapper functions |
| 100 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 101 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 102 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 103 | * |
| 104 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 105 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 106 | * because they must be exists in the program stack when the longjmp |
| 107 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 108 | * |
| 109 | * Note that the Lua processing is not really thread safe. It provides |
| 110 | * heavy system which consists to add our own lock function in the Lua |
| 111 | * code and recompile the library. This system will probably not accepted |
| 112 | * by maintainers of various distribs. |
| 113 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 114 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 115 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 116 | * of function and a lua_unlock() at the end of the function. So I |
| 117 | * conclude that the Lua thread safe mode just perform a mutex around |
| 118 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 119 | * easier for distro maintainers. |
| 120 | * |
| 121 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 122 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 123 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 125 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 126 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 128 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 130 | /* This is the chained list of struct hlua_function referenced |
| 131 | * for haproxy action, sample-fetches, converters, cli and |
| 132 | * applet bindings. It is used for a post-initialisation control. |
| 133 | */ |
| 134 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 135 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 136 | /* This variable is used only during initialization to identify the Lua state |
| 137 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 138 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 139 | */ |
| 140 | static int hlua_state_id; |
| 141 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 142 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 143 | static char **per_thread_load = NULL; |
| 144 | |
| 145 | lua_State *hlua_init_state(int thread_id); |
| 146 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 147 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 148 | ({ \ |
| 149 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 150 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 151 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 152 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 153 | lua_atpanic(__L, hlua_panic_safe); \ |
| 154 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 155 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 156 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 157 | } else { \ |
| 158 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 159 | ret = 1; \ |
| 160 | } \ |
| 161 | ret; \ |
| 162 | }) |
| 163 | |
| 164 | /* If we are the last function catching Lua errors, we |
| 165 | * must reset the panic function. |
| 166 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 167 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 168 | do { \ |
| 169 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 170 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 171 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 172 | } while(0) |
| 173 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 174 | #define SET_SAFE_LJMP(__HLUA) \ |
| 175 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 176 | |
| 177 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 178 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 179 | |
| 180 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 181 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 182 | |
| 183 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 184 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 186 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 187 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 188 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 189 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 190 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 191 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 192 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 193 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 194 | /* The main Lua execution context. The 0 index is the |
| 195 | * common state shared by all threads. |
| 196 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 197 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 198 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame^] | 199 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 200 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 201 | struct hlua_reg_filter { |
| 202 | char *name; |
| 203 | int flt_ref[MAX_THREADS + 1]; |
| 204 | int fun_ref[MAX_THREADS + 1]; |
| 205 | struct list l; |
| 206 | }; |
| 207 | |
| 208 | struct hlua_flt_config { |
| 209 | struct hlua_reg_filter *reg; |
| 210 | int ref[MAX_THREADS + 1]; |
| 211 | char **args; |
| 212 | }; |
| 213 | |
| 214 | struct hlua_flt_ctx { |
| 215 | int ref; /* ref to the filter lua object */ |
| 216 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 217 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 218 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 219 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 220 | }; |
| 221 | |
| 222 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 223 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame^] | 224 | static int hlua_filter_from_payload(struct filter *filter); |
| 225 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 226 | /* This is the chained list of struct hlua_flt referenced |
| 227 | * for haproxy filters. It is used for a post-initialisation control. |
| 228 | */ |
| 229 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 230 | |
| 231 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 232 | /* This is the memory pool containing struct lua for applets |
| 233 | * (including cli). |
| 234 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 235 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 236 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 237 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 238 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 239 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 240 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 241 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 242 | #endif |
| 243 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 244 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 245 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 246 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 247 | /* The following variables contains the reference of the different |
| 248 | * Lua classes. These references are useful for identify metadata |
| 249 | * associated with an object. |
| 250 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 251 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 252 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 253 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 254 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 255 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 256 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 257 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 258 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 259 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 260 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 261 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 262 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 263 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 264 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 265 | * because a task may remain alive during all the haproxy execution. |
| 266 | */ |
| 267 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 268 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 269 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 270 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 271 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 272 | * it is used for preventing infinite loops. |
| 273 | * |
| 274 | * I test the scheer with an infinite loop containing one incrementation |
| 275 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 276 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 277 | * to one interrupt each 10 000 instructions. |
| 278 | * |
| 279 | * configured | Number of |
| 280 | * instructions | loops executed |
| 281 | * between two | in milions |
| 282 | * forced yields | |
| 283 | * ---------------+--------------- |
| 284 | * 10 | 160 |
| 285 | * 500 | 670 |
| 286 | * 1000 | 680 |
| 287 | * 5000 | 700 |
| 288 | * 7000 | 700 |
| 289 | * 8000 | 700 |
| 290 | * 9000 | 710 <- ceil |
| 291 | * 10000 | 710 |
| 292 | * 100000 | 710 |
| 293 | * 1000000 | 710 |
| 294 | * |
| 295 | */ |
| 296 | static unsigned int hlua_nb_instruction = 10000; |
| 297 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 298 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 299 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 300 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 301 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 302 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 303 | */ |
| 304 | struct hlua_mem_allocator { |
| 305 | size_t allocated; |
| 306 | size_t limit; |
| 307 | }; |
| 308 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 309 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 310 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 311 | /* These functions converts types between HAProxy internal args or |
| 312 | * sample and LUA types. Another function permits to check if the |
| 313 | * LUA stack contains arguments according with an required ARG_T |
| 314 | * format. |
| 315 | */ |
| 316 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 317 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 318 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 319 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 320 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 321 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 322 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 323 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 324 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 325 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 326 | struct prepend_path { |
| 327 | struct list l; |
| 328 | char *type; |
| 329 | char *path; |
| 330 | }; |
| 331 | |
| 332 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 333 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 334 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 335 | do { \ |
| 336 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 337 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 338 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 339 | } while (0) |
| 340 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 341 | static inline struct hlua_function *new_hlua_function() |
| 342 | { |
| 343 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 344 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 345 | |
| 346 | fcn = calloc(1, sizeof(*fcn)); |
| 347 | if (!fcn) |
| 348 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 349 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 350 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 351 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 352 | return fcn; |
| 353 | } |
| 354 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 355 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 356 | { |
| 357 | if (!fcn) |
| 358 | return; |
| 359 | if (fcn->name) |
| 360 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 361 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 362 | ha_free(&fcn); |
| 363 | } |
| 364 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 365 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 366 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 367 | { |
| 368 | if (fcn->function_ref[0] == -1) |
| 369 | return tid + 1; |
| 370 | return 0; |
| 371 | } |
| 372 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 373 | /* Create a new registered filter. Only its name is filled */ |
| 374 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 375 | { |
| 376 | struct hlua_reg_filter *reg_flt; |
| 377 | int i; |
| 378 | |
| 379 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 380 | if (!reg_flt) |
| 381 | return NULL; |
| 382 | reg_flt->name = strdup(name); |
| 383 | if (!reg_flt->name) { |
| 384 | free(reg_flt); |
| 385 | return NULL; |
| 386 | } |
| 387 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 388 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 389 | reg_flt->flt_ref[i] = -1; |
| 390 | reg_flt->fun_ref[i] = -1; |
| 391 | } |
| 392 | return reg_flt; |
| 393 | } |
| 394 | |
| 395 | /* Release a registered filter */ |
| 396 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 397 | { |
| 398 | if (!reg_flt) |
| 399 | return; |
| 400 | if (reg_flt->name) |
| 401 | ha_free(®_flt->name); |
| 402 | LIST_DELETE(®_flt->l); |
| 403 | ha_free(®_flt); |
| 404 | } |
| 405 | |
| 406 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 407 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 408 | { |
| 409 | if (reg_flt->fun_ref[0] == -1) |
| 410 | return tid + 1; |
| 411 | return 0; |
| 412 | } |
| 413 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 414 | /* Used to check an Lua function type in the stack. It creates and |
| 415 | * returns a reference of the function. This function throws an |
| 416 | * error if the rgument is not a "function". |
| 417 | */ |
| 418 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 419 | { |
| 420 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 421 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 422 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 423 | } |
| 424 | lua_pushvalue(L, argno); |
| 425 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 426 | } |
| 427 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 428 | /* Used to check an Lua table type in the stack. It creates and |
| 429 | * returns a reference of the table. This function throws an |
| 430 | * error if the rgument is not a "table". |
| 431 | */ |
| 432 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 433 | { |
| 434 | if (!lua_istable(L, argno)) { |
| 435 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 436 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 437 | } |
| 438 | lua_pushvalue(L, argno); |
| 439 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 440 | } |
| 441 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 442 | /* Return the string that is of the top of the stack. */ |
| 443 | const char *hlua_get_top_error_string(lua_State *L) |
| 444 | { |
| 445 | if (lua_gettop(L) < 1) |
| 446 | return "unknown error"; |
| 447 | if (lua_type(L, -1) != LUA_TSTRING) |
| 448 | return "unknown error"; |
| 449 | return lua_tostring(L, -1); |
| 450 | } |
| 451 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 452 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 453 | { |
| 454 | lua_Debug ar; |
| 455 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 456 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 457 | |
| 458 | while (lua_getstack(L, level++, &ar)) { |
| 459 | |
| 460 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 461 | if (b_data(msg)) |
| 462 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 463 | |
| 464 | /* Fill fields: |
| 465 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 466 | * 'l': fills in the field currentline; |
| 467 | * 'n': fills in the field name and namewhat; |
| 468 | * 't': fills in the field istailcall; |
| 469 | */ |
| 470 | lua_getinfo(L, "Slnt", &ar); |
| 471 | |
| 472 | /* Append code localisation */ |
| 473 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 474 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 475 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 476 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * Get function name |
| 480 | * |
| 481 | * if namewhat is no empty, name is defined. |
| 482 | * what contains "Lua" for Lua function, "C" for C function, |
| 483 | * or "main" for main code. |
| 484 | */ |
| 485 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 486 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 487 | |
| 488 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 489 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 490 | |
| 491 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 492 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 493 | |
| 494 | else /* nothing left... */ |
| 495 | chunk_appendf(msg, "?"); |
| 496 | |
| 497 | |
| 498 | /* Display tailed call */ |
| 499 | if (ar.istailcall) |
| 500 | chunk_appendf(msg, " ..."); |
| 501 | } |
| 502 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 503 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 507 | /* This function check the number of arguments available in the |
| 508 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 509 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 510 | */ |
| 511 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 512 | { |
| 513 | if (lua_gettop(L) == nb) |
| 514 | return; |
| 515 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 516 | } |
| 517 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 518 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 519 | * and the line number where the error is encountered. |
| 520 | */ |
| 521 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 522 | { |
| 523 | va_list argp; |
| 524 | va_start(argp, fmt); |
| 525 | luaL_where(L, 1); |
| 526 | lua_pushvfstring(L, fmt, argp); |
| 527 | va_end(argp); |
| 528 | lua_concat(L, 2); |
| 529 | return 1; |
| 530 | } |
| 531 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 532 | /* This functions is used with sample fetch and converters. It |
| 533 | * converts the HAProxy configuration argument in a lua stack |
| 534 | * values. |
| 535 | * |
| 536 | * It takes an array of "arg", and each entry of the array is |
| 537 | * converted and pushed in the LUA stack. |
| 538 | */ |
| 539 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 540 | { |
| 541 | switch (arg->type) { |
| 542 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | case ARGT_TIME: |
| 544 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 545 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 549 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | case ARGT_IPV4: |
| 553 | case ARGT_IPV6: |
| 554 | case ARGT_MSK4: |
| 555 | case ARGT_MSK6: |
| 556 | case ARGT_FE: |
| 557 | case ARGT_BE: |
| 558 | case ARGT_TAB: |
| 559 | case ARGT_SRV: |
| 560 | case ARGT_USR: |
| 561 | case ARGT_MAP: |
| 562 | default: |
| 563 | lua_pushnil(L); |
| 564 | break; |
| 565 | } |
| 566 | return 1; |
| 567 | } |
| 568 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 569 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 571 | * with sample fetch wrappers. The input arguments are given to the |
| 572 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 573 | */ |
| 574 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 575 | { |
| 576 | switch (lua_type(L, ud)) { |
| 577 | |
| 578 | case LUA_TNUMBER: |
| 579 | case LUA_TBOOLEAN: |
| 580 | arg->type = ARGT_SINT; |
| 581 | arg->data.sint = lua_tointeger(L, ud); |
| 582 | break; |
| 583 | |
| 584 | case LUA_TSTRING: |
| 585 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 586 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 587 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 588 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 589 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 590 | break; |
| 591 | |
| 592 | case LUA_TUSERDATA: |
| 593 | case LUA_TNIL: |
| 594 | case LUA_TTABLE: |
| 595 | case LUA_TFUNCTION: |
| 596 | case LUA_TTHREAD: |
| 597 | case LUA_TLIGHTUSERDATA: |
| 598 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 599 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | return 1; |
| 603 | } |
| 604 | |
| 605 | /* the following functions are used to convert a struct sample |
| 606 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 607 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 609 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 611 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 612 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 614 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 615 | break; |
| 616 | |
| 617 | case SMP_T_BIN: |
| 618 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 619 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | break; |
| 621 | |
| 622 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 623 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 624 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 625 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 626 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 627 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 628 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 629 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 630 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 631 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 632 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 633 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | break; |
| 635 | default: |
| 636 | lua_pushnil(L); |
| 637 | break; |
| 638 | } |
| 639 | break; |
| 640 | |
| 641 | case SMP_T_IPV4: |
| 642 | case SMP_T_IPV6: |
| 643 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 644 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 645 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 646 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 647 | else |
| 648 | lua_pushnil(L); |
| 649 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 650 | default: |
| 651 | lua_pushnil(L); |
| 652 | break; |
| 653 | } |
| 654 | return 1; |
| 655 | } |
| 656 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 657 | /* the following functions are used to convert a struct sample |
| 658 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 659 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 660 | */ |
| 661 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 662 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 663 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 664 | |
| 665 | case SMP_T_BIN: |
| 666 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 667 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 668 | break; |
| 669 | |
| 670 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 671 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 672 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 673 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 674 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 675 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 676 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 677 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 678 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 679 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 680 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 681 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 682 | break; |
| 683 | default: |
| 684 | lua_pushstring(L, ""); |
| 685 | break; |
| 686 | } |
| 687 | break; |
| 688 | |
| 689 | case SMP_T_SINT: |
| 690 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 691 | case SMP_T_IPV4: |
| 692 | case SMP_T_IPV6: |
| 693 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 694 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 695 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 696 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 697 | else |
| 698 | lua_pushstring(L, ""); |
| 699 | break; |
| 700 | default: |
| 701 | lua_pushstring(L, ""); |
| 702 | break; |
| 703 | } |
| 704 | return 1; |
| 705 | } |
| 706 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 707 | /* the following functions are used to convert an Lua type in a |
| 708 | * struct sample. This is useful to provide data from a converter |
| 709 | * to the LUA code. |
| 710 | */ |
| 711 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 712 | { |
| 713 | switch (lua_type(L, ud)) { |
| 714 | |
| 715 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 716 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 717 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 718 | break; |
| 719 | |
| 720 | |
| 721 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 722 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 723 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 724 | break; |
| 725 | |
| 726 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 727 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 728 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 729 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 730 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 731 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 732 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 733 | break; |
| 734 | |
| 735 | case LUA_TUSERDATA: |
| 736 | case LUA_TNIL: |
| 737 | case LUA_TTABLE: |
| 738 | case LUA_TFUNCTION: |
| 739 | case LUA_TTHREAD: |
| 740 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 741 | case LUA_TNONE: |
| 742 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 743 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 744 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | return 1; |
| 748 | } |
| 749 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 750 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 751 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 752 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 753 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 754 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 755 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 756 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 757 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 758 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 759 | { |
| 760 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 761 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 762 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 763 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 764 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 765 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 766 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 767 | |
| 768 | idx = 0; |
| 769 | min_arg = ARGM(mask); |
| 770 | mask >>= ARGM_BITS; |
| 771 | |
| 772 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 773 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 774 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 775 | /* Check for mandatory arguments. */ |
| 776 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 777 | if (idx < min_arg) { |
| 778 | |
| 779 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 780 | if (idx > 0) { |
| 781 | msg = "Mandatory argument expected"; |
| 782 | goto error; |
| 783 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 784 | |
| 785 | /* If first argument have a certain type, some default values |
| 786 | * may be used. See the function smp_resolve_args(). |
| 787 | */ |
| 788 | switch (mask & ARGT_MASK) { |
| 789 | |
| 790 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 791 | if (!(p->cap & PR_CAP_FE)) { |
| 792 | msg = "Mandatory argument expected"; |
| 793 | goto error; |
| 794 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 795 | argp[idx].data.prx = p; |
| 796 | argp[idx].type = ARGT_FE; |
| 797 | argp[idx+1].type = ARGT_STOP; |
| 798 | break; |
| 799 | |
| 800 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 801 | if (!(p->cap & PR_CAP_BE)) { |
| 802 | msg = "Mandatory argument expected"; |
| 803 | goto error; |
| 804 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 805 | argp[idx].data.prx = p; |
| 806 | argp[idx].type = ARGT_BE; |
| 807 | argp[idx+1].type = ARGT_STOP; |
| 808 | break; |
| 809 | |
| 810 | case ARGT_TAB: |
| 811 | argp[idx].data.prx = p; |
| 812 | argp[idx].type = ARGT_TAB; |
| 813 | argp[idx+1].type = ARGT_STOP; |
| 814 | break; |
| 815 | |
| 816 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | msg = "Mandatory argument expected"; |
| 818 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 819 | break; |
| 820 | } |
| 821 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 825 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 826 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 827 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 828 | msg = "Last argument expected"; |
| 829 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 833 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 834 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 837 | /* Convert some argument types. All string in argp[] are for not |
| 838 | * duplicated yet. |
| 839 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 840 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 841 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 842 | if (argp[idx].type != ARGT_SINT) { |
| 843 | msg = "integer expected"; |
| 844 | goto error; |
| 845 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 846 | argp[idx].type = ARGT_SINT; |
| 847 | break; |
| 848 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 849 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 850 | if (argp[idx].type != ARGT_SINT) { |
| 851 | msg = "integer expected"; |
| 852 | goto error; |
| 853 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 854 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 855 | break; |
| 856 | |
| 857 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 858 | if (argp[idx].type != ARGT_SINT) { |
| 859 | msg = "integer expected"; |
| 860 | goto error; |
| 861 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 862 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 863 | break; |
| 864 | |
| 865 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 866 | if (argp[idx].type != ARGT_STR) { |
| 867 | msg = "string expected"; |
| 868 | goto error; |
| 869 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 870 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 871 | if (!argp[idx].data.prx) { |
| 872 | msg = "frontend doesn't exist"; |
| 873 | goto error; |
| 874 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 875 | argp[idx].type = ARGT_FE; |
| 876 | break; |
| 877 | |
| 878 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 879 | if (argp[idx].type != ARGT_STR) { |
| 880 | msg = "string expected"; |
| 881 | goto error; |
| 882 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 883 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | if (!argp[idx].data.prx) { |
| 885 | msg = "backend doesn't exist"; |
| 886 | goto error; |
| 887 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 888 | argp[idx].type = ARGT_BE; |
| 889 | break; |
| 890 | |
| 891 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 892 | if (argp[idx].type != ARGT_STR) { |
| 893 | msg = "string expected"; |
| 894 | goto error; |
| 895 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 896 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 897 | if (!argp[idx].data.t) { |
| 898 | msg = "table doesn't exist"; |
| 899 | goto error; |
| 900 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 901 | argp[idx].type = ARGT_TAB; |
| 902 | break; |
| 903 | |
| 904 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 905 | if (argp[idx].type != ARGT_STR) { |
| 906 | msg = "string expected"; |
| 907 | goto error; |
| 908 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 909 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 910 | if (sname) { |
| 911 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 912 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 913 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 914 | if (!px) { |
| 915 | msg = "backend doesn't exist"; |
| 916 | goto error; |
| 917 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 918 | } |
| 919 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 920 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 921 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 922 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 923 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 924 | if (!argp[idx].data.srv) { |
| 925 | msg = "server doesn't exist"; |
| 926 | goto error; |
| 927 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 928 | argp[idx].type = ARGT_SRV; |
| 929 | break; |
| 930 | |
| 931 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 932 | if (argp[idx].type != ARGT_STR) { |
| 933 | msg = "string expected"; |
| 934 | goto error; |
| 935 | } |
| 936 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 937 | msg = "invalid IPv4 address"; |
| 938 | goto error; |
| 939 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 940 | argp[idx].type = ARGT_IPV4; |
| 941 | break; |
| 942 | |
| 943 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 944 | if (argp[idx].type == ARGT_SINT) |
| 945 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 946 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 947 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 948 | msg = "invalid IPv4 mask"; |
| 949 | goto error; |
| 950 | } |
| 951 | } |
| 952 | else { |
| 953 | msg = "integer or string expected"; |
| 954 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 955 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 956 | argp[idx].type = ARGT_MSK4; |
| 957 | break; |
| 958 | |
| 959 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 960 | if (argp[idx].type != ARGT_STR) { |
| 961 | msg = "string expected"; |
| 962 | goto error; |
| 963 | } |
| 964 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 965 | msg = "invalid IPv6 address"; |
| 966 | goto error; |
| 967 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 968 | argp[idx].type = ARGT_IPV6; |
| 969 | break; |
| 970 | |
| 971 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 972 | if (argp[idx].type == ARGT_SINT) |
| 973 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 974 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 975 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 976 | msg = "invalid IPv6 mask"; |
| 977 | goto error; |
| 978 | } |
| 979 | } |
| 980 | else { |
| 981 | msg = "integer or string expected"; |
| 982 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 983 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 984 | argp[idx].type = ARGT_MSK6; |
| 985 | break; |
| 986 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 987 | case ARGT_REG: |
| 988 | if (argp[idx].type != ARGT_STR) { |
| 989 | msg = "string expected"; |
| 990 | goto error; |
| 991 | } |
| 992 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 993 | if (!reg) { |
| 994 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 995 | argp[idx].data.str.area, err); |
| 996 | free(err); |
| 997 | goto error; |
| 998 | } |
| 999 | argp[idx].type = ARGT_REG; |
| 1000 | argp[idx].data.reg = reg; |
| 1001 | break; |
| 1002 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1003 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1004 | if (argp[idx].type != ARGT_STR) { |
| 1005 | msg = "string expected"; |
| 1006 | goto error; |
| 1007 | } |
| 1008 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1009 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1010 | ul = p->uri_auth->userlist; |
| 1011 | else |
| 1012 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1013 | |
| 1014 | if (!ul) { |
| 1015 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1016 | goto error; |
| 1017 | } |
| 1018 | argp[idx].type = ARGT_USR; |
| 1019 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1020 | break; |
| 1021 | |
| 1022 | case ARGT_STR: |
| 1023 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1024 | msg = "unable to duplicate string arg"; |
| 1025 | goto error; |
| 1026 | } |
| 1027 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1028 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1029 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1030 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1031 | msg = "type not yet supported"; |
| 1032 | goto error; |
| 1033 | break; |
| 1034 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* Check for type of argument. */ |
| 1038 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1039 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1040 | arg_type_names[(mask & ARGT_MASK)], |
| 1041 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1042 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* Next argument. */ |
| 1046 | mask >>= ARGT_BITS; |
| 1047 | idx++; |
| 1048 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1049 | return 0; |
| 1050 | |
| 1051 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1052 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1053 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1054 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1057 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1058 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1059 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1060 | * |
| 1061 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1062 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1063 | */ |
| 1064 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1065 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1066 | struct hlua **hlua = lua_getextraspace(L); |
| 1067 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | } |
| 1069 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1070 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1071 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1072 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1075 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1076 | * and on the default syslog server. |
| 1077 | */ |
| 1078 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1079 | { |
| 1080 | struct tm tm; |
| 1081 | char *p; |
| 1082 | |
| 1083 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1084 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1085 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1086 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1087 | /* Break the message if exceed the buffer size. */ |
| 1088 | *(p-4) = ' '; |
| 1089 | *(p-3) = '.'; |
| 1090 | *(p-2) = '.'; |
| 1091 | *(p-1) = '.'; |
| 1092 | break; |
| 1093 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1094 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1095 | *p = *msg; |
| 1096 | else |
| 1097 | *p = '.'; |
| 1098 | } |
| 1099 | *p = '\0'; |
| 1100 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1101 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1102 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1103 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1104 | return; |
| 1105 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1106 | get_localtime(date.tv_sec, &tm); |
| 1107 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1108 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1109 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1110 | fflush(stderr); |
| 1111 | } |
| 1112 | } |
| 1113 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1114 | /* This function just ensure that the yield will be always |
| 1115 | * returned with a timeout and permit to set some flags |
| 1116 | */ |
| 1117 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1118 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1119 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1120 | struct hlua *hlua; |
| 1121 | |
| 1122 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1123 | hlua = hlua_gethlua(L); |
| 1124 | if (!hlua) { |
| 1125 | return; |
| 1126 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1127 | |
| 1128 | /* Set the wake timeout. If timeout is required, we set |
| 1129 | * the expiration time. |
| 1130 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1131 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1132 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1133 | hlua->flags |= flags; |
| 1134 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1135 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1136 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1139 | /* This function initialises the Lua environment stored in the stream. |
| 1140 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1141 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1142 | * |
| 1143 | * This function is particular. it initialises a new Lua thread. If the |
| 1144 | * initialisation fails (example: out of memory error), the lua function |
| 1145 | * throws an error (longjmp). |
| 1146 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1147 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1148 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1149 | * threads appear, the safe environment set a lock to ensure only one |
| 1150 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1151 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1152 | * |
| 1153 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1154 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1155 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1156 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1157 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1158 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1159 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1161 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | { |
| 1163 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1164 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1165 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1166 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1167 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1168 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1169 | if (!already_safe) { |
| 1170 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1171 | lua->Tref = LUA_REFNIL; |
| 1172 | return 0; |
| 1173 | } |
| 1174 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1175 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | if (!lua->T) { |
| 1177 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1178 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1179 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1183 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1184 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1185 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1186 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1187 | return 1; |
| 1188 | } |
| 1189 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1190 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1191 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1192 | * is not freed. |
| 1193 | */ |
| 1194 | void hlua_ctx_destroy(struct hlua *lua) |
| 1195 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1196 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1197 | return; |
| 1198 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1199 | if (!lua->T) |
| 1200 | goto end; |
| 1201 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1202 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1203 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1204 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1205 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1206 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1208 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1209 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1210 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1211 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1212 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1213 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1214 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1215 | * without error, we run the GC on the thread pointer. Its freed all |
| 1216 | * the unused memory. |
| 1217 | * If the thread is finnish with an error or is currently yielded, |
| 1218 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1219 | * so e run the GC on the main thread. |
| 1220 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1221 | * the garbage collection. |
| 1222 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1223 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1224 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1225 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1226 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1227 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1228 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1229 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1230 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1231 | |
| 1232 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1233 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* This function is used to restore the Lua context when a coroutine |
| 1237 | * fails. This function copy the common memory between old coroutine |
| 1238 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1239 | * replaced by the new coroutine. |
| 1240 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1241 | * as string error message and it is copied in the new stack. |
| 1242 | */ |
| 1243 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1244 | { |
| 1245 | lua_State *T; |
| 1246 | int new_ref; |
| 1247 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1248 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1249 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1250 | if (!T) |
| 1251 | return 0; |
| 1252 | |
| 1253 | /* Copy last error message. */ |
| 1254 | if (keep_msg) |
| 1255 | lua_xmove(lua->T, T, 1); |
| 1256 | |
| 1257 | /* Copy data between the coroutines. */ |
| 1258 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1259 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1260 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1261 | |
| 1262 | /* Destroy old data. */ |
| 1263 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1264 | |
| 1265 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1266 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | |
| 1268 | /* Fill the struct with the new coroutine values. */ |
| 1269 | lua->Mref = new_ref; |
| 1270 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1271 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1272 | |
| 1273 | /* Set context. */ |
| 1274 | hlua_sethlua(lua); |
| 1275 | |
| 1276 | return 1; |
| 1277 | } |
| 1278 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1279 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1280 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1281 | struct hlua *hlua; |
| 1282 | |
| 1283 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1284 | hlua = hlua_gethlua(L); |
| 1285 | if (!hlua) |
| 1286 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1287 | |
| 1288 | /* Lua cannot yield when its returning from a function, |
| 1289 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1290 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1291 | */ |
| 1292 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1293 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | /* restore the interrupt condition. */ |
| 1298 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1299 | |
| 1300 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1301 | * If the state is not yieldable, trying yield causes an error. |
| 1302 | */ |
| 1303 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1304 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1305 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1306 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1307 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1308 | hlua->run_time += now_ms - hlua->start_time; |
| 1309 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1310 | lua_pushfstring(L, "execution timeout"); |
| 1311 | WILL_LJMP(lua_error(L)); |
| 1312 | } |
| 1313 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1314 | /* Update the start time. */ |
| 1315 | hlua->start_time = now_ms; |
| 1316 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1317 | /* Try to interrupt the process at the end of the current |
| 1318 | * unyieldable function. |
| 1319 | */ |
| 1320 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1323 | /* This function start or resumes the Lua stack execution. If the flag |
| 1324 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1325 | * The function return an error. |
| 1326 | * |
| 1327 | * The function can returns 4 values: |
| 1328 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1329 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1330 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1331 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1332 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1333 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1334 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1335 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1336 | * LUA code. |
| 1337 | */ |
| 1338 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1339 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1340 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1341 | int nres; |
| 1342 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1343 | int ret; |
| 1344 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1345 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1346 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1347 | /* Initialise run time counter. */ |
| 1348 | if (!HLUA_IS_RUNNING(lua)) |
| 1349 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1350 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1351 | /* Lock the whole Lua execution. This lock must be before the |
| 1352 | * label "resume_execution". |
| 1353 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1354 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1355 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1356 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1357 | resume_execution: |
| 1358 | |
| 1359 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1360 | * instructions. it is used for preventing infinite loops. |
| 1361 | */ |
| 1362 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1363 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1364 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1365 | HLUA_SET_RUN(lua); |
| 1366 | HLUA_CLR_CTRLYIELD(lua); |
| 1367 | HLUA_CLR_WAKERESWR(lua); |
| 1368 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1369 | HLUA_CLR_NOYIELD(lua); |
| 1370 | if (!yield_allowed) |
| 1371 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1372 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1373 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1374 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1375 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1376 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1377 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1378 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1379 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1380 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1381 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1382 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1383 | switch (ret) { |
| 1384 | |
| 1385 | case LUA_OK: |
| 1386 | ret = HLUA_E_OK; |
| 1387 | break; |
| 1388 | |
| 1389 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1390 | /* Check if the execution timeout is expired. It it is the case, we |
| 1391 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1392 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1393 | tv_update_date(0, 1); |
| 1394 | lua->run_time += now_ms - lua->start_time; |
| 1395 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1396 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1397 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | /* Process the forced yield. if the general yield is not allowed or |
| 1401 | * if no task were associated this the current Lua execution |
| 1402 | * coroutine, we resume the execution. Else we want to return in the |
| 1403 | * scheduler and we want to be waked up again, to continue the |
| 1404 | * current Lua execution. So we schedule our own task. |
| 1405 | */ |
| 1406 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1407 | if (!yield_allowed || !lua->task) |
| 1408 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1409 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1410 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1411 | if (!yield_allowed) { |
| 1412 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1413 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1414 | break; |
| 1415 | } |
| 1416 | ret = HLUA_E_AGAIN; |
| 1417 | break; |
| 1418 | |
| 1419 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1420 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1421 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1422 | * because the errors ares the only one mean to return immediately |
| 1423 | * from and lua execution. |
| 1424 | */ |
| 1425 | if (lua->flags & HLUA_EXIT) { |
| 1426 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1427 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1428 | break; |
| 1429 | } |
| 1430 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1431 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1432 | if (!lua_checkstack(lua->T, 1)) { |
| 1433 | ret = HLUA_E_ERR; |
| 1434 | break; |
| 1435 | } |
| 1436 | msg = lua_tostring(lua->T, -1); |
| 1437 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1438 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1439 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1440 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1441 | lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1442 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1443 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1444 | ret = HLUA_E_ERRMSG; |
| 1445 | break; |
| 1446 | |
| 1447 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1448 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1449 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1450 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1451 | break; |
| 1452 | |
| 1453 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1454 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1455 | if (!lua_checkstack(lua->T, 1)) { |
| 1456 | ret = HLUA_E_ERR; |
| 1457 | break; |
| 1458 | } |
| 1459 | msg = lua_tostring(lua->T, -1); |
| 1460 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1461 | lua_pop(lua->T, 1); |
| 1462 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1463 | lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1464 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1465 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1466 | ret = HLUA_E_ERRMSG; |
| 1467 | break; |
| 1468 | |
| 1469 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1470 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1471 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1472 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | |
| 1476 | switch (ret) { |
| 1477 | case HLUA_E_AGAIN: |
| 1478 | break; |
| 1479 | |
| 1480 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1481 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1482 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1483 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1484 | break; |
| 1485 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1486 | case HLUA_E_ETMOUT: |
| 1487 | case HLUA_E_NOMEM: |
| 1488 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1489 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1490 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1491 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1492 | hlua_ctx_renew(lua, 0); |
| 1493 | break; |
| 1494 | |
| 1495 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1496 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1497 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1501 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1502 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1503 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1504 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1505 | return ret; |
| 1506 | } |
| 1507 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1508 | /* This function exit the current code. */ |
| 1509 | __LJMP static int hlua_done(lua_State *L) |
| 1510 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1511 | struct hlua *hlua; |
| 1512 | |
| 1513 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1514 | hlua = hlua_gethlua(L); |
| 1515 | if (!hlua) |
| 1516 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1517 | |
| 1518 | hlua->flags |= HLUA_EXIT; |
| 1519 | WILL_LJMP(lua_error(L)); |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1524 | /* This function is an LUA binding. It provides a function |
| 1525 | * for deleting ACL from a referenced ACL file. |
| 1526 | */ |
| 1527 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1528 | { |
| 1529 | const char *name; |
| 1530 | const char *key; |
| 1531 | struct pat_ref *ref; |
| 1532 | |
| 1533 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1534 | |
| 1535 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1536 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1537 | |
| 1538 | ref = pat_ref_lookup(name); |
| 1539 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1540 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1541 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1542 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1543 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1544 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | /* This function is an LUA binding. It provides a function |
| 1549 | * for deleting map entry from a referenced map file. |
| 1550 | */ |
| 1551 | static int hlua_del_map(lua_State *L) |
| 1552 | { |
| 1553 | const char *name; |
| 1554 | const char *key; |
| 1555 | struct pat_ref *ref; |
| 1556 | |
| 1557 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1558 | |
| 1559 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1560 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1561 | |
| 1562 | ref = pat_ref_lookup(name); |
| 1563 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1564 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1565 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1566 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1567 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1568 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | /* This function is an LUA binding. It provides a function |
| 1573 | * for adding ACL pattern from a referenced ACL file. |
| 1574 | */ |
| 1575 | static int hlua_add_acl(lua_State *L) |
| 1576 | { |
| 1577 | const char *name; |
| 1578 | const char *key; |
| 1579 | struct pat_ref *ref; |
| 1580 | |
| 1581 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1582 | |
| 1583 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1584 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1585 | |
| 1586 | ref = pat_ref_lookup(name); |
| 1587 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1588 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1589 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1590 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1591 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1592 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1593 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | /* This function is an LUA binding. It provides a function |
| 1598 | * for setting map pattern and sample from a referenced map |
| 1599 | * file. |
| 1600 | */ |
| 1601 | static int hlua_set_map(lua_State *L) |
| 1602 | { |
| 1603 | const char *name; |
| 1604 | const char *key; |
| 1605 | const char *value; |
| 1606 | struct pat_ref *ref; |
| 1607 | |
| 1608 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1609 | |
| 1610 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1611 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1612 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1613 | |
| 1614 | ref = pat_ref_lookup(name); |
| 1615 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1616 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1617 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1618 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1619 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1620 | pat_ref_set(ref, key, value, NULL); |
| 1621 | else |
| 1622 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1623 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
| 1626 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1627 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1628 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1629 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1630 | * the name of the object (_G[<name>] = <metable> ). |
| 1631 | * |
| 1632 | * A metable is a table that modify the standard behavior of a standard |
| 1633 | * access to the associated data. The entries of this new metatable are |
| 1634 | * defined as is: |
| 1635 | * |
| 1636 | * http://lua-users.org/wiki/MetatableEvents |
| 1637 | * |
| 1638 | * __index |
| 1639 | * |
| 1640 | * we access an absent field in a table, the result is nil. This is |
| 1641 | * true, but it is not the whole truth. Actually, such access triggers |
| 1642 | * the interpreter to look for an __index metamethod: If there is no |
| 1643 | * such method, as usually happens, then the access results in nil; |
| 1644 | * otherwise, the metamethod will provide the result. |
| 1645 | * |
| 1646 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1647 | * the key does not appear in the table, but the metatable has an __index |
| 1648 | * property: |
| 1649 | * |
| 1650 | * - if the value is a function, the function is called, passing in the |
| 1651 | * table and the key; the return value of that function is returned as |
| 1652 | * the result. |
| 1653 | * |
| 1654 | * - if the value is another table, the value of the key in that table is |
| 1655 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1656 | * table's metatable has an __index property, then it continues on up) |
| 1657 | * |
| 1658 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1659 | * |
| 1660 | * http://www.lua.org/pil/13.4.1.html |
| 1661 | * |
| 1662 | * __newindex |
| 1663 | * |
| 1664 | * Like __index, but control property assignment. |
| 1665 | * |
| 1666 | * __mode - Control weak references. A string value with one or both |
| 1667 | * of the characters 'k' and 'v' which specifies that the the |
| 1668 | * keys and/or values in the table are weak references. |
| 1669 | * |
| 1670 | * __call - Treat a table like a function. When a table is followed by |
| 1671 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1672 | * a __call key pointing to a function, that function is invoked |
| 1673 | * (passing any specified arguments) and the return value is |
| 1674 | * returned. |
| 1675 | * |
| 1676 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1677 | * called, if the metatable for myTable has a __metatable |
| 1678 | * key, the value of that key is returned instead of the |
| 1679 | * actual metatable. |
| 1680 | * |
| 1681 | * __tostring - Control string representation. When the builtin |
| 1682 | * "tostring( myTable )" function is called, if the metatable |
| 1683 | * for myTable has a __tostring property set to a function, |
| 1684 | * that function is invoked (passing myTable to it) and the |
| 1685 | * return value is used as the string representation. |
| 1686 | * |
| 1687 | * __len - Control table length. When the table length is requested using |
| 1688 | * the length operator ( '#' ), if the metatable for myTable has |
| 1689 | * a __len key pointing to a function, that function is invoked |
| 1690 | * (passing myTable to it) and the return value used as the value |
| 1691 | * of "#myTable". |
| 1692 | * |
| 1693 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1694 | * collected, if the metatable has a __gc field pointing to a |
| 1695 | * function, that function is first invoked, passing the userdata |
| 1696 | * to it. The __gc metamethod is not called for tables. |
| 1697 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1698 | * |
| 1699 | * Special metamethods for redefining standard operators: |
| 1700 | * http://www.lua.org/pil/13.1.html |
| 1701 | * |
| 1702 | * __add "+" |
| 1703 | * __sub "-" |
| 1704 | * __mul "*" |
| 1705 | * __div "/" |
| 1706 | * __unm "!" |
| 1707 | * __pow "^" |
| 1708 | * __concat ".." |
| 1709 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1710 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1711 | * http://www.lua.org/pil/13.2.html |
| 1712 | * |
| 1713 | * __eq "==" |
| 1714 | * __lt "<" |
| 1715 | * __le "<=" |
| 1716 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | |
| 1718 | /* |
| 1719 | * |
| 1720 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1721 | * Class Map |
| 1722 | * |
| 1723 | * |
| 1724 | */ |
| 1725 | |
| 1726 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1727 | * a class session, otherwise it throws an error. |
| 1728 | */ |
| 1729 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1730 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1731 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* This function is the map constructor. It don't need |
| 1735 | * the class Map object. It creates and return a new Map |
| 1736 | * object. It must be called only during "body" or "init" |
| 1737 | * context because it process some filesystem accesses. |
| 1738 | */ |
| 1739 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1740 | { |
| 1741 | const char *fn; |
| 1742 | int match = PAT_MATCH_STR; |
| 1743 | struct sample_conv conv; |
| 1744 | const char *file = ""; |
| 1745 | int line = 0; |
| 1746 | lua_Debug ar; |
| 1747 | char *err = NULL; |
| 1748 | struct arg args[2]; |
| 1749 | |
| 1750 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1751 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1752 | |
| 1753 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1754 | |
| 1755 | if (lua_gettop(L) >= 2) { |
| 1756 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1757 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1758 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1759 | } |
| 1760 | |
| 1761 | /* Get Lua filename and line number. */ |
| 1762 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1763 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1764 | if (ar.currentline > 0) { /* is there info? */ |
| 1765 | file = ar.short_src; |
| 1766 | line = ar.currentline; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | /* fill fake sample_conv struct. */ |
| 1771 | conv.kw = ""; /* unused. */ |
| 1772 | conv.process = NULL; /* unused. */ |
| 1773 | conv.arg_mask = 0; /* unused. */ |
| 1774 | conv.val_args = NULL; /* unused. */ |
| 1775 | conv.out_type = SMP_T_STR; |
| 1776 | conv.private = (void *)(long)match; |
| 1777 | switch (match) { |
| 1778 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1779 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1780 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1781 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1782 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1783 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1784 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1785 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1786 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1787 | default: |
| 1788 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1789 | } |
| 1790 | |
| 1791 | /* fill fake args. */ |
| 1792 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1793 | args[0].data.str.area = strdup(fn); |
| 1794 | args[0].data.str.data = strlen(fn); |
| 1795 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1796 | args[1].type = ARGT_STOP; |
| 1797 | |
| 1798 | /* load the map. */ |
| 1799 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1800 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1801 | * free the err variable. |
| 1802 | */ |
| 1803 | luaL_where(L, 1); |
| 1804 | lua_pushfstring(L, "'new': %s.", err); |
| 1805 | lua_concat(L, 2); |
| 1806 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1807 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1808 | WILL_LJMP(lua_error(L)); |
| 1809 | } |
| 1810 | |
| 1811 | /* create the lua object. */ |
| 1812 | lua_newtable(L); |
| 1813 | lua_pushlightuserdata(L, args[0].data.map); |
| 1814 | lua_rawseti(L, -2, 0); |
| 1815 | |
| 1816 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1817 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1818 | lua_setmetatable(L, -2); |
| 1819 | |
| 1820 | |
| 1821 | return 1; |
| 1822 | } |
| 1823 | |
| 1824 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1825 | { |
| 1826 | struct map_descriptor *desc; |
| 1827 | struct pattern *pat; |
| 1828 | struct sample smp; |
| 1829 | |
| 1830 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1831 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1832 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1833 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1834 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1835 | } |
| 1836 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1837 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1838 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1839 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 91dc0c0 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1840 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1844 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1845 | if (str) |
| 1846 | lua_pushstring(L, ""); |
| 1847 | else |
| 1848 | lua_pushnil(L); |
| 1849 | return 1; |
| 1850 | } |
| 1851 | |
| 1852 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1853 | lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1854 | return 1; |
| 1855 | } |
| 1856 | |
| 1857 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1858 | { |
| 1859 | return _hlua_map_lookup(L, 0); |
| 1860 | } |
| 1861 | |
| 1862 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1863 | { |
| 1864 | return _hlua_map_lookup(L, 1); |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * |
| 1869 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1870 | * Class Socket |
| 1871 | * |
| 1872 | * |
| 1873 | */ |
| 1874 | |
| 1875 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1876 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1877 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /* This function is the handler called for each I/O on the established |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1881 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | * received. |
| 1883 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1884 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1886 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1888 | if (appctx->ctx.hlua_cosocket.die) { |
| 1889 | si_shutw(si); |
| 1890 | si_shutr(si); |
| 1891 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1892 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1893 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1894 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1895 | } |
| 1896 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1897 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1898 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1899 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1900 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1901 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1902 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1903 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1904 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1905 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1906 | * to be notified whenever the connection completes. |
| 1907 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1908 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1909 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1910 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1911 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1912 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1913 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
| 1915 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1916 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1917 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1918 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1919 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1920 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | |
| 1922 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1923 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1924 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1925 | |
| 1926 | /* Some data were injected in the buffer, notify the stream |
| 1927 | * interface. |
| 1928 | */ |
| 1929 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1930 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1931 | |
| 1932 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1933 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1934 | */ |
| 1935 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1936 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | } |
| 1938 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1939 | /* This function is called when the "struct stream" is destroyed. |
| 1940 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | * Wake all the pending signals. |
| 1942 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1943 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1945 | struct xref *peer; |
| 1946 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1948 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1949 | if (peer) |
| 1950 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1953 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1954 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1958 | * uses this object. If the stream does not exists, just quit. |
| 1959 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | * pending signal can rest in the read and write lists. destroy |
| 1961 | * it. |
| 1962 | */ |
| 1963 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1964 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1965 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1966 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1967 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1969 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1970 | |
| 1971 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1972 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1973 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1975 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1977 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1978 | appctx->ctx.hlua_cosocket.die = 1; |
| 1979 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1981 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1982 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1987 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1989 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1990 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1991 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1993 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1994 | struct hlua *hlua; |
| 1995 | |
| 1996 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1997 | hlua = hlua_gethlua(L); |
| 1998 | if (!hlua) |
| 1999 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2001 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2002 | |
| 2003 | /* Check if we run on the same thread than the xreator thread. |
| 2004 | * We cannot access to the socket if the thread is different. |
| 2005 | */ |
| 2006 | if (socket->tid != tid) |
| 2007 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2008 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2009 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2010 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2012 | |
| 2013 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2014 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2016 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2017 | appctx->ctx.hlua_cosocket.die = 1; |
| 2018 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2020 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2021 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | return 0; |
| 2023 | } |
| 2024 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2025 | /* The close function calls close_helper. |
| 2026 | */ |
| 2027 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2028 | { |
| 2029 | MAY_LJMP(check_args(L, 1, "close")); |
| 2030 | return hlua_socket_close_helper(L); |
| 2031 | } |
| 2032 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2033 | /* This Lua function assumes that the stack contain three parameters. |
| 2034 | * 1 - USERDATA containing a struct socket |
| 2035 | * 2 - INTEGER with values of the macro defined below |
| 2036 | * If the integer is -1, we must read at most one line. |
| 2037 | * If the integer is -2, we ust read all the data until the |
| 2038 | * end of the stream. |
| 2039 | * If the integer is positive value, we must read a number of |
| 2040 | * bytes corresponding to this value. |
| 2041 | */ |
| 2042 | #define HLSR_READ_LINE (-1) |
| 2043 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2044 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | { |
| 2046 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2047 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2048 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2050 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2052 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2053 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2054 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2055 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2056 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2057 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2058 | struct stream_interface *si; |
| 2059 | struct stream *s; |
| 2060 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2061 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2063 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2064 | hlua = hlua_gethlua(L); |
| 2065 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | /* Check if this lua stack is schedulable. */ |
| 2067 | if (!hlua || !hlua->task) |
| 2068 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2069 | "'frontend', 'backend' or 'task'")); |
| 2070 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2071 | /* Check if we run on the same thread than the xreator thread. |
| 2072 | * We cannot access to the socket if the thread is different. |
| 2073 | */ |
| 2074 | if (socket->tid != tid) |
| 2075 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2076 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2077 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2079 | if (!peer) |
| 2080 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2081 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2082 | si = appctx->owner; |
| 2083 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2085 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2088 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | if (nblk < 0) /* Connection close. */ |
| 2090 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2091 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2093 | |
| 2094 | /* remove final \r\n. */ |
| 2095 | if (nblk == 1) { |
| 2096 | if (blk1[len1-1] == '\n') { |
| 2097 | len1--; |
| 2098 | skip_at_end++; |
| 2099 | if (blk1[len1-1] == '\r') { |
| 2100 | len1--; |
| 2101 | skip_at_end++; |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | else { |
| 2106 | if (blk2[len2-1] == '\n') { |
| 2107 | len2--; |
| 2108 | skip_at_end++; |
| 2109 | if (blk2[len2-1] == '\r') { |
| 2110 | len2--; |
| 2111 | skip_at_end++; |
| 2112 | } |
| 2113 | } |
| 2114 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2119 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | if (nblk < 0) /* Connection close. */ |
| 2121 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2122 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | goto connection_empty; |
| 2124 | } |
| 2125 | |
| 2126 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2128 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2129 | if (nblk < 0) /* Connection close. */ |
| 2130 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2131 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | goto connection_empty; |
| 2133 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2134 | missing_bytes = wanted - socket->b.n; |
| 2135 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2136 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2137 | len1 = missing_bytes; |
| 2138 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2139 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | len = len1; |
| 2143 | |
| 2144 | luaL_addlstring(&socket->b, blk1, len1); |
| 2145 | if (nblk == 2) { |
| 2146 | len += len2; |
| 2147 | luaL_addlstring(&socket->b, blk2, len2); |
| 2148 | } |
| 2149 | |
| 2150 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2151 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2152 | |
| 2153 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2154 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | |
| 2156 | /* If the pattern reclaim to read all the data |
| 2157 | * in the connection, got out. |
| 2158 | */ |
| 2159 | if (wanted == HLSR_READ_ALL) |
| 2160 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2161 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | goto connection_empty; |
| 2163 | |
| 2164 | /* Return result. */ |
| 2165 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2166 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | return 1; |
| 2168 | |
| 2169 | connection_closed: |
| 2170 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2171 | xref_unlock(&socket->xref, peer); |
| 2172 | |
| 2173 | no_peer: |
| 2174 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | /* If the buffer containds data. */ |
| 2176 | if (socket->b.n > 0) { |
| 2177 | luaL_pushresult(&socket->b); |
| 2178 | return 1; |
| 2179 | } |
| 2180 | lua_pushnil(L); |
| 2181 | lua_pushstring(L, "connection closed."); |
| 2182 | return 2; |
| 2183 | |
| 2184 | connection_empty: |
| 2185 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2186 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2187 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2189 | } |
| 2190 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2191 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | return 0; |
| 2193 | } |
| 2194 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2195 | /* This Lua function gets two parameters. The first one can be string |
| 2196 | * or a number. If the string is "*l", the user requires one line. If |
| 2197 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | * If the value is a number, the user require a number of bytes equal |
| 2199 | * to the value. The default value is "*l" (a line). |
| 2200 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2201 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | * integer takes this values: |
| 2203 | * -1 : read a line |
| 2204 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2205 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2206 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2207 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | * concatenated with the read data. |
| 2209 | */ |
| 2210 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2211 | { |
| 2212 | int wanted = HLSR_READ_LINE; |
| 2213 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2214 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | char *error; |
| 2216 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2217 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | |
| 2219 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2220 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2221 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2222 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2224 | /* Check if we run on the same thread than the xreator thread. |
| 2225 | * We cannot access to the socket if the thread is different. |
| 2226 | */ |
| 2227 | if (socket->tid != tid) |
| 2228 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2229 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | /* check for pattern. */ |
| 2231 | if (lua_gettop(L) >= 2) { |
| 2232 | type = lua_type(L, 2); |
| 2233 | if (type == LUA_TSTRING) { |
| 2234 | pattern = lua_tostring(L, 2); |
| 2235 | if (strcmp(pattern, "*a") == 0) |
| 2236 | wanted = HLSR_READ_ALL; |
| 2237 | else if (strcmp(pattern, "*l") == 0) |
| 2238 | wanted = HLSR_READ_LINE; |
| 2239 | else { |
| 2240 | wanted = strtoll(pattern, &error, 10); |
| 2241 | if (*error != '\0') |
| 2242 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2243 | } |
| 2244 | } |
| 2245 | else if (type == LUA_TNUMBER) { |
| 2246 | wanted = lua_tointeger(L, 2); |
| 2247 | if (wanted < 0) |
| 2248 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | /* Set pattern. */ |
| 2253 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2254 | |
| 2255 | /* Check if we would replace the top by itself. */ |
| 2256 | if (lua_gettop(L) != 2) |
| 2257 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2259 | /* Save index of the top of the stack because since buffers are used, it |
| 2260 | * may change |
| 2261 | */ |
| 2262 | lastarg = lua_gettop(L); |
| 2263 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2264 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | luaL_buffinit(L, &socket->b); |
| 2266 | |
| 2267 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2268 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2270 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2271 | pattern = lua_tolstring(L, 3, &len); |
| 2272 | luaL_addlstring(&socket->b, pattern, len); |
| 2273 | } |
| 2274 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2275 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2279 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2281 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | { |
| 2283 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2284 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | struct appctx *appctx; |
| 2286 | size_t buf_len; |
| 2287 | const char *buf; |
| 2288 | int len; |
| 2289 | int send_len; |
| 2290 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2291 | struct xref *peer; |
| 2292 | struct stream_interface *si; |
| 2293 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2295 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2296 | hlua = hlua_gethlua(L); |
| 2297 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | /* Check if this lua stack is schedulable. */ |
| 2299 | if (!hlua || !hlua->task) |
| 2300 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2301 | "'frontend', 'backend' or 'task'")); |
| 2302 | |
| 2303 | /* Get object */ |
| 2304 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2305 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2306 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2308 | /* Check if we run on the same thread than the xreator thread. |
| 2309 | * We cannot access to the socket if the thread is different. |
| 2310 | */ |
| 2311 | if (socket->tid != tid) |
| 2312 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2313 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2314 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2315 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2316 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2317 | lua_pushinteger(L, -1); |
| 2318 | return 1; |
| 2319 | } |
| 2320 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2321 | si = appctx->owner; |
| 2322 | s = si_strm(si); |
| 2323 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2326 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | lua_pushinteger(L, -1); |
| 2328 | return 1; |
| 2329 | } |
| 2330 | |
| 2331 | /* Update the input buffer data. */ |
| 2332 | buf += sent; |
| 2333 | send_len = buf_len - sent; |
| 2334 | |
| 2335 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2336 | if (sent >= buf_len) { |
| 2337 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2339 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2341 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2342 | * the request buffer if its not required. |
| 2343 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2344 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2345 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2346 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2349 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2350 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2351 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2353 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
| 2355 | /* send data */ |
| 2356 | if (len < send_len) |
| 2357 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2358 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | |
| 2360 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2361 | * the data" (-2) are not expected because the available length |
| 2362 | * is tested. |
| 2363 | * Other unknown error are also not expected. |
| 2364 | */ |
| 2365 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2366 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2367 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2368 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2369 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2370 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2371 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2372 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | return 1; |
| 2374 | } |
| 2375 | |
| 2376 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2377 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2378 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2379 | s->req.rex = TICK_ETERNITY; |
| 2380 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | |
| 2382 | /* Update length sent. */ |
| 2383 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2384 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2385 | |
| 2386 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2387 | if (sent + len >= buf_len) { |
| 2388 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2390 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | |
| 2392 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2393 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2394 | xref_unlock(&socket->xref, peer); |
| 2395 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2396 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2398 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | return 0; |
| 2400 | } |
| 2401 | |
| 2402 | /* This function initiate the send of data. It just check the input |
| 2403 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2404 | * amount of data written to the buffer. This is used by the function |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | * "hlua_socket_write_yield" that can yield. |
| 2406 | * |
| 2407 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2408 | * the associated object. The second is a string buffer. The third is |
| 2409 | * a facultative integer that represents where is the buffer position |
| 2410 | * of the start of the data that can send. The first byte is the |
| 2411 | * position "1". The default value is "1". The fourth argument is a |
| 2412 | * facultative integer that represents where is the buffer position |
| 2413 | * of the end of the data that can send. The default is the last byte. |
| 2414 | */ |
| 2415 | static int hlua_socket_send(struct lua_State *L) |
| 2416 | { |
| 2417 | int i; |
| 2418 | int j; |
| 2419 | const char *buf; |
| 2420 | size_t buf_len; |
| 2421 | |
| 2422 | /* Check number of arguments. */ |
| 2423 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2424 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2425 | |
| 2426 | /* Get the string. */ |
| 2427 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2428 | |
| 2429 | /* Get and check j. */ |
| 2430 | if (lua_gettop(L) == 4) { |
| 2431 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2432 | if (j < 0) |
| 2433 | j = buf_len + j + 1; |
| 2434 | if (j > buf_len) |
| 2435 | j = buf_len + 1; |
| 2436 | lua_pop(L, 1); |
| 2437 | } |
| 2438 | else |
| 2439 | j = buf_len; |
| 2440 | |
| 2441 | /* Get and check i. */ |
| 2442 | if (lua_gettop(L) == 3) { |
| 2443 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2444 | if (i < 0) |
| 2445 | i = buf_len + i + 1; |
| 2446 | if (i > buf_len) |
| 2447 | i = buf_len + 1; |
| 2448 | lua_pop(L, 1); |
| 2449 | } else |
| 2450 | i = 1; |
| 2451 | |
| 2452 | /* Check bth i and j. */ |
| 2453 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2454 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | return 1; |
| 2456 | } |
| 2457 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2458 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | return 1; |
| 2460 | } |
| 2461 | if (i == 0) |
| 2462 | i = 1; |
| 2463 | if (j == 0) |
| 2464 | j = 1; |
| 2465 | |
| 2466 | /* Pop the string. */ |
| 2467 | lua_pop(L, 1); |
| 2468 | |
| 2469 | /* Update the buffer length. */ |
| 2470 | buf += i - 1; |
| 2471 | buf_len = j - i + 1; |
| 2472 | lua_pushlstring(L, buf, buf_len); |
| 2473 | |
| 2474 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2475 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2477 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2480 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2482 | { |
| 2483 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2484 | int ret; |
| 2485 | int len; |
| 2486 | char *p; |
| 2487 | |
| 2488 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2489 | if (ret <= 0) { |
| 2490 | lua_pushnil(L); |
| 2491 | return 1; |
| 2492 | } |
| 2493 | |
| 2494 | if (ret == AF_UNIX) { |
| 2495 | lua_pushstring(L, buffer+1); |
| 2496 | return 1; |
| 2497 | } |
| 2498 | else if (ret == AF_INET6) { |
| 2499 | buffer[0] = '['; |
| 2500 | len = strlen(buffer); |
| 2501 | buffer[len] = ']'; |
| 2502 | len++; |
| 2503 | buffer[len] = ':'; |
| 2504 | len++; |
| 2505 | p = buffer; |
| 2506 | } |
| 2507 | else if (ret == AF_INET) { |
| 2508 | p = buffer + 1; |
| 2509 | len = strlen(p); |
| 2510 | p[len] = ':'; |
| 2511 | len++; |
| 2512 | } |
| 2513 | else { |
| 2514 | lua_pushnil(L); |
| 2515 | return 1; |
| 2516 | } |
| 2517 | |
| 2518 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2519 | lua_pushnil(L); |
| 2520 | return 1; |
| 2521 | } |
| 2522 | |
| 2523 | lua_pushstring(L, p); |
| 2524 | return 1; |
| 2525 | } |
| 2526 | |
| 2527 | /* Returns information about the peer of the connection. */ |
| 2528 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2529 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2530 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2531 | struct xref *peer; |
| 2532 | struct appctx *appctx; |
| 2533 | struct stream_interface *si; |
| 2534 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2535 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2536 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2537 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2538 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2539 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2540 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2541 | /* Check if we run on the same thread than the xreator thread. |
| 2542 | * We cannot access to the socket if the thread is different. |
| 2543 | */ |
| 2544 | if (socket->tid != tid) |
| 2545 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2546 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2547 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2548 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2549 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | lua_pushnil(L); |
| 2551 | return 1; |
| 2552 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2553 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2554 | si = appctx->owner; |
| 2555 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2556 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2557 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2559 | lua_pushnil(L); |
| 2560 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | } |
| 2562 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2563 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2564 | xref_unlock(&socket->xref, peer); |
| 2565 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | /* Returns information about my connection side. */ |
| 2569 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2570 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2571 | struct hlua_socket *socket; |
| 2572 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2573 | struct appctx *appctx; |
| 2574 | struct xref *peer; |
| 2575 | struct stream_interface *si; |
| 2576 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2577 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2578 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2579 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2580 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2581 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2583 | /* Check if we run on the same thread than the xreator thread. |
| 2584 | * We cannot access to the socket if the thread is different. |
| 2585 | */ |
| 2586 | if (socket->tid != tid) |
| 2587 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2588 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2589 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2590 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2591 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2592 | lua_pushnil(L); |
| 2593 | return 1; |
| 2594 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2595 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2596 | si = appctx->owner; |
| 2597 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2599 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2600 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2601 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | lua_pushnil(L); |
| 2603 | return 1; |
| 2604 | } |
| 2605 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2606 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2607 | xref_unlock(&socket->xref, peer); |
| 2608 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2612 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2613 | .obj_type = OBJ_TYPE_APPLET, |
| 2614 | .name = "<LUA_TCP>", |
| 2615 | .fct = hlua_socket_handler, |
| 2616 | .release = hlua_socket_release, |
| 2617 | }; |
| 2618 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2619 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | { |
| 2621 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2622 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2623 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2624 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2625 | struct stream_interface *si; |
| 2626 | struct stream *s; |
| 2627 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2628 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2629 | hlua = hlua_gethlua(L); |
| 2630 | if (!hlua) |
| 2631 | return 0; |
| 2632 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2633 | /* Check if we run on the same thread than the xreator thread. |
| 2634 | * We cannot access to the socket if the thread is different. |
| 2635 | */ |
| 2636 | if (socket->tid != tid) |
| 2637 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2638 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2640 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2641 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | lua_pushnil(L); |
| 2643 | lua_pushstring(L, "Can't connect"); |
| 2644 | return 2; |
| 2645 | } |
| 2646 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2647 | si = appctx->owner; |
| 2648 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2650 | /* Check if we run on the same thread than the xreator thread. |
| 2651 | * We cannot access to the socket if the thread is different. |
| 2652 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2653 | if (socket->tid != tid) { |
| 2654 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2655 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2656 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2657 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2659 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2660 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | lua_pushnil(L); |
| 2662 | lua_pushstring(L, "Can't connect"); |
| 2663 | return 2; |
| 2664 | } |
| 2665 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2666 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | |
| 2668 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2669 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2670 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | lua_pushinteger(L, 1); |
| 2672 | return 1; |
| 2673 | } |
| 2674 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2676 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2678 | } |
| 2679 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2680 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2681 | return 0; |
| 2682 | } |
| 2683 | |
| 2684 | /* This function fail or initite the connection. */ |
| 2685 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2686 | { |
| 2687 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2688 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2690 | struct hlua *hlua; |
| 2691 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2692 | int low, high; |
| 2693 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2694 | struct xref *peer; |
| 2695 | struct stream_interface *si; |
| 2696 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2697 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2698 | if (lua_gettop(L) < 2) |
| 2699 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2700 | |
| 2701 | /* Get args. */ |
| 2702 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2703 | |
| 2704 | /* Check if we run on the same thread than the xreator thread. |
| 2705 | * We cannot access to the socket if the thread is different. |
| 2706 | */ |
| 2707 | if (socket->tid != tid) |
| 2708 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2709 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2711 | if (lua_gettop(L) >= 3) { |
| 2712 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2713 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2714 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2715 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2716 | * that are not enclosed within square brackets. |
| 2717 | */ |
| 2718 | if (port > 0) { |
| 2719 | luaL_buffinit(L, &b); |
| 2720 | luaL_addstring(&b, ip); |
| 2721 | luaL_addchar(&b, ':'); |
| 2722 | luaL_pushresult(&b); |
| 2723 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2724 | } |
| 2725 | } |
| 2726 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2727 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2728 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2729 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2730 | lua_pushnil(L); |
| 2731 | return 1; |
| 2732 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2733 | |
| 2734 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2735 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2736 | if (!addr) { |
| 2737 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2738 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2739 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2740 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2741 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2742 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2743 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2744 | if (port == -1) { |
| 2745 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2746 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2747 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2748 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2749 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2750 | if (port == -1) { |
| 2751 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2752 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2753 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2754 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2755 | } |
| 2756 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2757 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2758 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2759 | si = appctx->owner; |
| 2760 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2761 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2762 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2763 | xref_unlock(&socket->xref, peer); |
| 2764 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2765 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2766 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2767 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2768 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2769 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2770 | if (!hlua) |
| 2771 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2772 | |
| 2773 | /* inform the stream that we want to be notified whenever the |
| 2774 | * connection completes. |
| 2775 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2776 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2777 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2778 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2780 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2781 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2782 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2783 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2784 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2785 | } |
| 2786 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2787 | |
| 2788 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2789 | /* Return yield waiting for connection. */ |
| 2790 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2791 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2792 | |
| 2793 | return 0; |
| 2794 | } |
| 2795 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2796 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2797 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2798 | { |
| 2799 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2800 | struct xref *peer; |
| 2801 | struct appctx *appctx; |
| 2802 | struct stream_interface *si; |
| 2803 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2804 | |
| 2805 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2806 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2807 | |
| 2808 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2809 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2810 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2811 | lua_pushnil(L); |
| 2812 | return 1; |
| 2813 | } |
| 2814 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2815 | si = appctx->owner; |
| 2816 | s = si_strm(si); |
| 2817 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2818 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2819 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2820 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2821 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2822 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2823 | |
| 2824 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2825 | { |
| 2826 | return 0; |
| 2827 | } |
| 2828 | |
| 2829 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2830 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2831 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2832 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2833 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2834 | struct xref *peer; |
| 2835 | struct appctx *appctx; |
| 2836 | struct stream_interface *si; |
| 2837 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2838 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2839 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2840 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2841 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2842 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2843 | /* convert the timeout to millis */ |
| 2844 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2845 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2846 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2847 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2848 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2849 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2850 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2851 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2852 | |
| 2853 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2854 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2855 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2856 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2857 | /* Check if we run on the same thread than the xreator thread. |
| 2858 | * We cannot access to the socket if the thread is different. |
| 2859 | */ |
| 2860 | if (socket->tid != tid) |
| 2861 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2862 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2863 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2864 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2865 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2866 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2867 | WILL_LJMP(lua_error(L)); |
| 2868 | return 0; |
| 2869 | } |
| 2870 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2871 | si = appctx->owner; |
| 2872 | s = si_strm(si); |
| 2873 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2874 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2875 | s->req.rto = tmout; |
| 2876 | s->req.wto = tmout; |
| 2877 | s->res.rto = tmout; |
| 2878 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2879 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2880 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2881 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2882 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2883 | |
| 2884 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2885 | task_queue(s->task); |
| 2886 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2887 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2888 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2889 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2890 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2894 | { |
| 2895 | struct hlua_socket *socket; |
| 2896 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2897 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2898 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2899 | |
| 2900 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2901 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2902 | hlua_pusherror(L, "socket: full stack"); |
| 2903 | goto out_fail_conf; |
| 2904 | } |
| 2905 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2906 | /* Create the object: obj[0] = userdata. */ |
| 2907 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2908 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2909 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2910 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2911 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2912 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2913 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2914 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2915 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2916 | goto out_fail_conf; |
| 2917 | } |
| 2918 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2919 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2920 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2921 | lua_setmetatable(L, -2); |
| 2922 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2923 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2924 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2925 | if (!appctx) { |
| 2926 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2927 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2928 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2929 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2930 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2931 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2932 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2933 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2934 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2935 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2936 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2937 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2938 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2939 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2940 | } |
| 2941 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2942 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2943 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2944 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2945 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2946 | } |
| 2947 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2948 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2949 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2950 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2951 | /* Configure "right" stream interface. this "si" is used to connect |
| 2952 | * and retrieve data from the server. The connection is initialized |
| 2953 | * with the "struct server". |
| 2954 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2955 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2956 | |
| 2957 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2958 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2959 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2960 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2961 | return 1; |
| 2962 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2963 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2964 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2965 | out_fail_sess: |
| 2966 | appctx_free(appctx); |
| 2967 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2968 | WILL_LJMP(lua_error(L)); |
| 2969 | return 0; |
| 2970 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2971 | |
| 2972 | /* |
| 2973 | * |
| 2974 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | * Class Channel |
| 2976 | * |
| 2977 | * |
| 2978 | */ |
| 2979 | |
| 2980 | /* Returns the struct hlua_channel join to the class channel in the |
| 2981 | * stack entry "ud" or throws an argument error. |
| 2982 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2983 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2984 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2985 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2986 | } |
| 2987 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2988 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2989 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2991 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2994 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | return 0; |
| 2996 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2997 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2998 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2999 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | |
| 3001 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3002 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3003 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | return 1; |
| 3005 | } |
| 3006 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame^] | 3007 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3008 | * in the stack, filling the current offset and length of the filter. If no |
| 3009 | * filter is attached, NULL is returned and <offet> and <len> are not |
| 3010 | * initialized. |
| 3011 | */ |
| 3012 | static __maybe_unused struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
| 3013 | { |
| 3014 | struct filter *filter = NULL; |
| 3015 | |
| 3016 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3017 | struct hlua_flt_ctx *flt_ctx; |
| 3018 | |
| 3019 | filter = lua_touserdata (L, -1); |
| 3020 | flt_ctx = filter->ctx; |
| 3021 | if (hlua_filter_from_payload(filter)) { |
| 3022 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3023 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | lua_pop(L, 1); |
| 3028 | return filter; |
| 3029 | } |
| 3030 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3031 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3032 | * offset <offset>, and put it in a LUA string variable. It is the caller |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3033 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3034 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3035 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3036 | */ |
| 3037 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3039 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3040 | luaL_Buffer b; |
| 3041 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3042 | block1 = len; |
| 3043 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3044 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3045 | block2 = len - block1; |
| 3046 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3048 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3049 | if (block2) |
| 3050 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3052 | return len; |
| 3053 | } |
| 3054 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3055 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3056 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3057 | * number of bytes copied. |
| 3058 | */ |
| 3059 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3060 | { |
| 3061 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3063 | /* Nothing to do, just return */ |
| 3064 | if (unlikely(istlen(str) == 0)) |
| 3065 | goto end; |
| 3066 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3067 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3068 | ret = -1; |
| 3069 | goto end; |
| 3070 | } |
| 3071 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3072 | |
| 3073 | end: |
| 3074 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | } |
| 3076 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3077 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3078 | */ |
| 3079 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3080 | { |
| 3081 | size_t end = offset + len; |
| 3082 | |
| 3083 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3084 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3085 | b_data(&chn->buf) - end, -len); |
| 3086 | b_sub(&chn->buf, len); |
| 3087 | } |
| 3088 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3089 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3090 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3091 | * offset by default). If there is not enough input data and more data can be |
| 3092 | * received, this function yields. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3094 | __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3096 | struct channel *chn; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3097 | int offset = 0, len = 0; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3098 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3099 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3100 | |
| 3101 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3102 | lua_pushnil(L); |
| 3103 | return 1; |
| 3104 | } |
| 3105 | |
| 3106 | if (lua_gettop(L) > 1) { |
| 3107 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3108 | if (offset < 0) |
| 3109 | offset = MAX(0, ci_data(chn) + offset); |
| 3110 | if (offset > ci_data(chn)) { |
| 3111 | lua_pushfstring(L, "offset out of range."); |
| 3112 | WILL_LJMP(lua_error(L)); |
| 3113 | } |
| 3114 | } |
| 3115 | len = ci_data(chn) - offset; |
| 3116 | if (lua_gettop(L) == 3) { |
| 3117 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3118 | if (!len) |
| 3119 | goto dup; |
| 3120 | if (len == -1) |
| 3121 | len = global.tune.bufsize; |
| 3122 | if (len < 0) { |
| 3123 | lua_pushfstring(L, "length out of range."); |
| 3124 | WILL_LJMP(lua_error(L)); |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | if (offset + len > ci_data(chn)) { |
| 3129 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3130 | /* Yield waiting for more data, as requested */ |
| 3131 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3132 | } |
| 3133 | len = ci_data(chn) - offset; |
| 3134 | } |
| 3135 | |
| 3136 | dup: |
| 3137 | _hlua_channel_dup(chn, L, co_data(chn) + offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | return 1; |
| 3139 | } |
| 3140 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3141 | /* Copies the first line (including the trailing LF) of input data in the |
| 3142 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3143 | * a length (all remaining input data starting for the offset by default). If |
| 3144 | * there is not enough input data and more data can be received, the function |
| 3145 | * yields. If a length is explicitly specified, no more data are |
| 3146 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3147 | * function yields. |
| 3148 | */ |
| 3149 | __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3150 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3151 | struct channel *chn; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3152 | int l, offset = 0, len = 0; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3153 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3154 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3155 | |
| 3156 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3157 | lua_pushnil(L); |
| 3158 | return 1; |
| 3159 | } |
| 3160 | |
| 3161 | if (lua_gettop(L) > 1) { |
| 3162 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3163 | if (offset < 0) |
| 3164 | offset = MAX(0, ci_data(chn) + offset); |
| 3165 | if (offset > ci_data(chn)) { |
| 3166 | lua_pushfstring(L, "offset out of range."); |
| 3167 | WILL_LJMP(lua_error(L)); |
| 3168 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3169 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3170 | len = ci_data(chn) - offset; |
| 3171 | if (lua_gettop(L) == 3) { |
| 3172 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3173 | if (!len) |
| 3174 | goto dup; |
| 3175 | if (len == -1) |
| 3176 | len = global.tune.bufsize; |
| 3177 | if (len < 0) { |
| 3178 | lua_pushfstring(L, "length out of range."); |
| 3179 | WILL_LJMP(lua_error(L)); |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | for (l = 0; l < len; l++) { |
| 3184 | if (l + offset >= ci_data(chn)) |
| 3185 | break; |
| 3186 | if (*(b_peek(&chn->buf, co_data(chn) + offset + l)) == '\n') { |
| 3187 | len = l+1; |
| 3188 | goto dup; |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | if (offset + len > ci_data(chn)) { |
| 3193 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3194 | /* Yield waiting for more data */ |
| 3195 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3196 | } |
| 3197 | len = ci_data(chn) - offset; |
| 3198 | } |
| 3199 | |
| 3200 | dup: |
| 3201 | _hlua_channel_dup(chn, L, co_data(chn) + offset, len); |
| 3202 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3203 | } |
| 3204 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | /* [ DEPRECATED ] |
| 3206 | * |
| 3207 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3208 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3209 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3211 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3212 | struct channel *chn; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3213 | int offset = 0, len = 0; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3214 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3215 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3216 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3217 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3218 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3219 | WILL_LJMP(lua_error(L)); |
| 3220 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3221 | offset = co_data(chn); |
| 3222 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3223 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3224 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3225 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3226 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3227 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3228 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3229 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3230 | return 1; |
| 3231 | } |
| 3232 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3233 | /* [ DEPRECATED ] |
| 3234 | * |
| 3235 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3236 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3237 | * _hlua_channel_delete(). |
| 3238 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3239 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3240 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3241 | struct channel *chn; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3242 | int offset = 0, len = 0; |
| 3243 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3244 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3245 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3246 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3247 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3248 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3249 | WILL_LJMP(lua_error(L)); |
| 3250 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3251 | offset = co_data(chn); |
| 3252 | len = ci_data(chn); |
| 3253 | |
| 3254 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3255 | lua_pushnil(L); |
| 3256 | return 1; |
| 3257 | } |
| 3258 | |
| 3259 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3260 | _hlua_channel_delete(chn, offset, ret); |
| 3261 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3264 | /* This functions consumes and returns one line. If the channel is closed, |
| 3265 | * and the last data does not contains a final '\n', the data are returned |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3266 | * without the final '\n'. When no more data are available, it returns nil |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3267 | * value. |
| 3268 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3269 | __LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3270 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3271 | struct channel *chn; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3272 | size_t l; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3273 | int offset = 0, len = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3275 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3276 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3277 | offset = co_data(chn); |
| 3278 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3279 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3280 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3281 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3282 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | for (l = 0; l < len; l++) { |
| 3286 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3287 | len = l+1; |
| 3288 | goto dup; |
| 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3293 | /* Yield waiting for more data */ |
| 3294 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3295 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3296 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3297 | dup: |
| 3298 | ret = _hlua_channel_dup(chn, L, co_data(chn) + offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3299 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3300 | return 1; |
| 3301 | } |
| 3302 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3303 | /* [ DEPRECATED ] |
| 3304 | * |
| 3305 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3306 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3307 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3308 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3309 | struct channel *chn; |
| 3310 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3311 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3312 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3313 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3314 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3315 | WILL_LJMP(lua_error(L)); |
| 3316 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3317 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3318 | } |
| 3319 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3320 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3321 | * available input data are returned. The offset may be negactive to start from |
| 3322 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3323 | * size. |
| 3324 | */ |
| 3325 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3326 | { |
| 3327 | struct channel *chn; |
| 3328 | |
| 3329 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3330 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3331 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3332 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3333 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3334 | WILL_LJMP(lua_error(L)); |
| 3335 | } |
| 3336 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3337 | } |
| 3338 | |
| 3339 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3340 | * available input data are returned. The offset may be negactive to start from |
| 3341 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3342 | * size. |
| 3343 | */ |
| 3344 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3345 | { |
| 3346 | struct channel *chn; |
| 3347 | |
| 3348 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3349 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3350 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3351 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3352 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3353 | WILL_LJMP(lua_error(L)); |
| 3354 | } |
| 3355 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3356 | } |
| 3357 | |
| 3358 | /* Appends a string into the input side of channel. It returns the length of the |
| 3359 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3360 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3361 | * does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3362 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3363 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3364 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3365 | struct channel *chn; |
| 3366 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3367 | size_t len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3368 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3369 | |
| 3370 | MAY_LJMP(check_args(L, 2, "append")); |
| 3371 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3372 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3373 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3374 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3375 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3376 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3377 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3378 | ret = _hlua_channel_insert(chn, L, ist2(str, len), co_data(chn) + ci_data(chn)); |
| 3379 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3380 | return 1; |
| 3381 | } |
| 3382 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3383 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3384 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3385 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3386 | * does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3387 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3388 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3389 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3390 | struct channel *chn; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3391 | const char *str; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3392 | size_t sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3393 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3394 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3395 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3396 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3397 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3398 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3399 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3400 | WILL_LJMP(lua_error(L)); |
| 3401 | } |
| 3402 | |
| 3403 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn)); |
| 3404 | lua_pushinteger(L, ret); |
| 3405 | return 1; |
| 3406 | } |
| 3407 | |
| 3408 | /* Inserts a given amount of input data at the given offset by a string |
| 3409 | * content. By default the string is appended at the end of input data. It |
| 3410 | * returns the length of the written string, or -1 if the channel is closed or |
| 3411 | * if the buffer size is too little for the data. |
| 3412 | */ |
| 3413 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3414 | { |
| 3415 | struct channel *chn; |
| 3416 | const char *str; |
| 3417 | size_t sz; |
| 3418 | int ret, offset; |
| 3419 | |
| 3420 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3421 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3422 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3423 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3424 | offset = ci_data(chn); |
| 3425 | if (lua_gettop(L) > 2) |
| 3426 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3427 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3428 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3429 | WILL_LJMP(lua_error(L)); |
| 3430 | } |
| 3431 | |
| 3432 | if (offset < 0) |
| 3433 | offset = MAX(0, ci_data(chn) + offset); |
| 3434 | |
| 3435 | if (offset > ci_data(chn)) { |
| 3436 | lua_pushfstring(L, "offset out of range."); |
| 3437 | WILL_LJMP(lua_error(L)); |
| 3438 | } |
| 3439 | |
| 3440 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn) + offset); |
| 3441 | lua_pushinteger(L, ret); |
| 3442 | return 1; |
| 3443 | } |
| 3444 | /* Replaces a given amount of input data at the given offset by a string |
| 3445 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3446 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3447 | * closed or if the buffer size is too little for the data. |
| 3448 | */ |
| 3449 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3450 | { |
| 3451 | struct channel *chn; |
| 3452 | const char *str; |
| 3453 | size_t sz; |
| 3454 | int ret, offset = 0, len = -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3455 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3456 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3457 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3458 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3459 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3460 | if (lua_gettop(L) > 2) |
| 3461 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3462 | if (lua_gettop(L) == 4) |
| 3463 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3464 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3465 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3466 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3467 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3468 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3469 | if (offset < 0) |
| 3470 | offset = MAX(0, ci_data(chn) + offset); |
| 3471 | if (len < 0) |
| 3472 | len = ci_data(chn) - offset; |
| 3473 | |
| 3474 | if (offset + len > ci_data(chn)) { |
| 3475 | lua_pushfstring(L, "offset or length out of range."); |
| 3476 | WILL_LJMP(lua_error(L)); |
| 3477 | } |
| 3478 | |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3479 | /* Be sure we can copied the string once input data will be removed. */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3480 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3481 | lua_pushinteger(L, -1); |
| 3482 | else { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3483 | _hlua_channel_delete(chn, co_data(chn) + offset, len); |
| 3484 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn) + offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3485 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3486 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3487 | return 1; |
| 3488 | } |
| 3489 | |
| 3490 | /* Removes a given amount of input data at the given offset. By default all |
| 3491 | * intput data are removed (offset = 0 and len = -1). It returns the amount of |
| 3492 | * the removed data. |
| 3493 | */ |
| 3494 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3495 | { |
| 3496 | struct channel *chn; |
| 3497 | int offset = 0, len = -1; |
| 3498 | |
| 3499 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3500 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3501 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3502 | if (lua_gettop(L) > 1) |
| 3503 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3504 | if (lua_gettop(L) == 3) |
| 3505 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3506 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3507 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3508 | WILL_LJMP(lua_error(L)); |
| 3509 | } |
| 3510 | |
| 3511 | if (offset < 0) |
| 3512 | offset = MAX(0, ci_data(chn) + offset); |
| 3513 | if (len < 0) |
| 3514 | len = ci_data(chn) - offset; |
| 3515 | |
| 3516 | if (offset + len > ci_data(chn)) { |
| 3517 | lua_pushfstring(L, "offset or length out of range."); |
| 3518 | WILL_LJMP(lua_error(L)); |
| 3519 | } |
| 3520 | |
| 3521 | _hlua_channel_delete(chn, co_data(chn) + offset, len); |
| 3522 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3523 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3524 | } |
| 3525 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3526 | /* Append data in the output side of the buffer. This data is immediately |
| 3527 | * sent. The function returns the amount of data written. If the buffer |
| 3528 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3529 | * if the channel is closed. |
| 3530 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3531 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3532 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3533 | struct channel *chn; |
| 3534 | const char *str; |
| 3535 | size_t sz, len; |
| 3536 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3537 | struct hlua *hlua; |
| 3538 | |
| 3539 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3540 | hlua = hlua_gethlua(L); |
| 3541 | if (!hlua) { |
| 3542 | lua_pushnil(L); |
| 3543 | return 1; |
| 3544 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3545 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3546 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3547 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3548 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3549 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3550 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3551 | lua_pushinteger(L, -1); |
| 3552 | return 1; |
| 3553 | } |
| 3554 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3555 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3556 | * the request buffer if its not required. |
| 3557 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3558 | if (chn->buf.size == 0) { |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3559 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3560 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3561 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3562 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3563 | } |
| 3564 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3565 | len = c_room(chn); |
| 3566 | if (len > sz - l) |
| 3567 | len = sz - l; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3568 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3569 | ret = _hlua_channel_insert(chn, L, ist2(str+l, len), co_data(chn)); |
| 3570 | if (ret == -1) { |
| 3571 | lua_pop(L, 1); |
| 3572 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3573 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3574 | } |
| 3575 | if (ret) { |
| 3576 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3577 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3578 | l += ret; |
| 3579 | lua_pop(L, 1); |
| 3580 | lua_pushinteger(L, l); |
| 3581 | } |
| 3582 | if (l < sz) { |
| 3583 | /* Yield only if the channel's output is not empty. |
| 3584 | * Otherwise it means we cannot add more data. */ |
| 3585 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3586 | return 1; |
| 3587 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3588 | /* If we are waiting for space in the response buffer, we |
| 3589 | * must set the flag WAKERESWR. This flag required the task |
| 3590 | * wake up if any activity is detected on the response buffer. |
| 3591 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3592 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3593 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3594 | else |
| 3595 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3596 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3597 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3598 | |
| 3599 | return 1; |
| 3600 | } |
| 3601 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3602 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3603 | * yield the LUA process, and resume it without checking the |
| 3604 | * input arguments. |
| 3605 | */ |
| 3606 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3607 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3608 | struct channel *chn; |
| 3609 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3610 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3611 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3612 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3613 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3614 | WILL_LJMP(lua_error(L)); |
| 3615 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3616 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3617 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | /* This function forward and amount of butes. The data pass from |
| 3621 | * the input side of the buffer to the output side, and can be |
| 3622 | * forwarded. This function never fails. |
| 3623 | * |
| 3624 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3625 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3626 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3627 | __LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3628 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3629 | struct channel *chn; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3630 | size_t len; |
| 3631 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3632 | struct hlua *hlua; |
| 3633 | |
| 3634 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3635 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3636 | if (!hlua) { |
| 3637 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3638 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3639 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3640 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3641 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3642 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3643 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3644 | |
| 3645 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3646 | if (max > ci_data(chn)) |
| 3647 | max = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3648 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3649 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3650 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3651 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3652 | lua_pop(L, 1); |
| 3653 | lua_pushinteger(L, l); |
| 3654 | |
| 3655 | /* Check if it miss bytes to forward. */ |
| 3656 | if (l < len) { |
| 3657 | /* The the input channel or the output channel are closed, we |
| 3658 | * must return the amount of data forwarded. |
| 3659 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3660 | if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3661 | return 1; |
| 3662 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3663 | /* If we are waiting for space data in the response buffer, we |
| 3664 | * must set the flag WAKERESWR. This flag required the task |
| 3665 | * wake up if any activity is detected on the response buffer. |
| 3666 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3667 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3668 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3669 | else |
| 3670 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3671 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3672 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3673 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | return 1; |
| 3677 | } |
| 3678 | |
| 3679 | /* Just check the input and prepare the stack for the previous |
| 3680 | * function "hlua_channel_forward_yield" |
| 3681 | */ |
| 3682 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3683 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3684 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3685 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3686 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3687 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3688 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3689 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3690 | WILL_LJMP(lua_error(L)); |
| 3691 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3692 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3693 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | /* Just returns the number of bytes available in the input |
| 3697 | * side of the buffer. This function never fails. |
| 3698 | */ |
| 3699 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3700 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3701 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3702 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3703 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3704 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3705 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3706 | struct htx *htx = htxbuf(&chn->buf); |
| 3707 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3708 | } |
| 3709 | else |
| 3710 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3711 | return 1; |
| 3712 | } |
| 3713 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3714 | /* Returns true if the channel is full. */ |
| 3715 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3716 | { |
| 3717 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3718 | |
| 3719 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3720 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3721 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3722 | * applet). |
| 3723 | */ |
| 3724 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3725 | return 1; |
| 3726 | } |
| 3727 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3728 | /* Returns true if the channel may still receive data. */ |
| 3729 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3730 | { |
| 3731 | struct channel *chn; |
| 3732 | |
| 3733 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3734 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3735 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3736 | return 1; |
| 3737 | } |
| 3738 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3739 | /* Returns true if the channel is the response channel. */ |
| 3740 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3741 | { |
| 3742 | struct channel *chn; |
| 3743 | |
| 3744 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3745 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3746 | |
| 3747 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3748 | return 1; |
| 3749 | } |
| 3750 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3751 | /* Just returns the number of bytes available in the output |
| 3752 | * side of the buffer. This function never fails. |
| 3753 | */ |
| 3754 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3755 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3756 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3757 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3758 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3759 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3760 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3761 | return 1; |
| 3762 | } |
| 3763 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3764 | /* |
| 3765 | * |
| 3766 | * |
| 3767 | * Class Fetches |
| 3768 | * |
| 3769 | * |
| 3770 | */ |
| 3771 | |
| 3772 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3773 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3774 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3775 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3776 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3777 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | /* This function creates and push in the stack a fetch object according |
| 3781 | * with a current TXN. |
| 3782 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3783 | static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3784 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3785 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3786 | |
| 3787 | /* Check stack size. */ |
| 3788 | if (!lua_checkstack(L, 3)) |
| 3789 | return 0; |
| 3790 | |
| 3791 | /* Create the object: obj[0] = userdata. |
| 3792 | * Note that the base of the Fetches object is the |
| 3793 | * transaction object. |
| 3794 | */ |
| 3795 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3796 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3797 | lua_rawseti(L, -2, 0); |
| 3798 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3799 | hsmp->s = txn->s; |
| 3800 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3801 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3802 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3803 | |
| 3804 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3805 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3806 | lua_setmetatable(L, -2); |
| 3807 | |
| 3808 | return 1; |
| 3809 | } |
| 3810 | |
| 3811 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3812 | * It uses closure argument to store the associated sample-fetch. It |
| 3813 | * returns only one argument or throws an error. An error is thrown |
| 3814 | * only if an error is encountered during the argument parsing. If |
| 3815 | * the "sample-fetch" function fails, nil is returned. |
| 3816 | */ |
| 3817 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3818 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3819 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3820 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3821 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3822 | int i; |
| 3823 | struct sample smp; |
| 3824 | |
| 3825 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3826 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3827 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3828 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3829 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3830 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3831 | /* Check execution authorization. */ |
| 3832 | if (f->use & SMP_USE_HTTP_ANY && |
| 3833 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3834 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3835 | "is not available in Lua services", f->kw); |
| 3836 | WILL_LJMP(lua_error(L)); |
| 3837 | } |
| 3838 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3839 | /* Get extra arguments. */ |
| 3840 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3841 | if (i >= ARGM_NBARGS) |
| 3842 | break; |
| 3843 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3844 | } |
| 3845 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3846 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3847 | |
| 3848 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3849 | MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3850 | |
| 3851 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3852 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3853 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3854 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | /* Initialise the sample. */ |
| 3858 | memset(&smp, 0, sizeof(smp)); |
| 3859 | |
| 3860 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3861 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3862 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3863 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3864 | lua_pushstring(L, ""); |
| 3865 | else |
| 3866 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3867 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3871 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3872 | hlua_smp2lua_str(L, &smp); |
| 3873 | else |
| 3874 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3875 | |
| 3876 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3877 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3878 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3879 | |
| 3880 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3881 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3882 | WILL_LJMP(lua_error(L)); |
| 3883 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3884 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3885 | |
| 3886 | /* |
| 3887 | * |
| 3888 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3889 | * Class Converters |
| 3890 | * |
| 3891 | * |
| 3892 | */ |
| 3893 | |
| 3894 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3895 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3896 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3897 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3898 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3899 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* This function creates and push in the stack a Converters object |
| 3903 | * according with a current TXN. |
| 3904 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3905 | static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3906 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3907 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3908 | |
| 3909 | /* Check stack size. */ |
| 3910 | if (!lua_checkstack(L, 3)) |
| 3911 | return 0; |
| 3912 | |
| 3913 | /* Create the object: obj[0] = userdata. |
| 3914 | * Note that the base of the Converters object is the |
| 3915 | * same than the TXN object. |
| 3916 | */ |
| 3917 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3918 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3919 | lua_rawseti(L, -2, 0); |
| 3920 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3921 | hsmp->s = txn->s; |
| 3922 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3923 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3924 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3925 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3926 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3927 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3928 | lua_setmetatable(L, -2); |
| 3929 | |
| 3930 | return 1; |
| 3931 | } |
| 3932 | |
| 3933 | /* This function is an LUA binding. It is called with each converter. |
| 3934 | * It uses closure argument to store the associated converter. It |
| 3935 | * returns only one argument or throws an error. An error is thrown |
| 3936 | * only if an error is encountered during the argument parsing. If |
| 3937 | * the converter function function fails, nil is returned. |
| 3938 | */ |
| 3939 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3940 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3941 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3942 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3943 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3944 | int i; |
| 3945 | struct sample smp; |
| 3946 | |
| 3947 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3948 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3949 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3950 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3951 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3952 | |
| 3953 | /* Get extra arguments. */ |
| 3954 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3955 | if (i >= ARGM_NBARGS) |
| 3956 | break; |
| 3957 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3958 | } |
| 3959 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3960 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3961 | |
| 3962 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3963 | MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3964 | |
| 3965 | /* Run the special args checker. */ |
| 3966 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3967 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3968 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3972 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3973 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3974 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3975 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3976 | } |
| 3977 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3978 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3979 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3980 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3981 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3982 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3983 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3984 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3985 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3986 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3987 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3988 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3989 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3993 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3994 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3995 | lua_pushstring(L, ""); |
| 3996 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3997 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3998 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4002 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4003 | hlua_smp2lua_str(L, &smp); |
| 4004 | else |
| 4005 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4006 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4007 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4008 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4009 | |
| 4010 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4011 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4012 | WILL_LJMP(lua_error(L)); |
| 4013 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4014 | } |
| 4015 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4016 | /* |
| 4017 | * |
| 4018 | * |
| 4019 | * Class AppletTCP |
| 4020 | * |
| 4021 | * |
| 4022 | */ |
| 4023 | |
| 4024 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4025 | * a class stream, otherwise it throws an error. |
| 4026 | */ |
| 4027 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4028 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4029 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | /* This function creates and push in the stack an Applet object |
| 4033 | * according with a current TXN. |
| 4034 | */ |
| 4035 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4036 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4037 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4038 | struct stream_interface *si = ctx->owner; |
| 4039 | struct stream *s = si_strm(si); |
| 4040 | struct proxy *p = s->be; |
| 4041 | |
| 4042 | /* Check stack size. */ |
| 4043 | if (!lua_checkstack(L, 3)) |
| 4044 | return 0; |
| 4045 | |
| 4046 | /* Create the object: obj[0] = userdata. |
| 4047 | * Note that the base of the Converters object is the |
| 4048 | * same than the TXN object. |
| 4049 | */ |
| 4050 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4051 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4052 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4053 | luactx->appctx = ctx; |
| 4054 | luactx->htxn.s = s; |
| 4055 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4056 | |
| 4057 | /* Create the "f" field that contains a list of fetches. */ |
| 4058 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4059 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4060 | return 0; |
| 4061 | lua_settable(L, -3); |
| 4062 | |
| 4063 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4064 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4065 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4066 | return 0; |
| 4067 | lua_settable(L, -3); |
| 4068 | |
| 4069 | /* Create the "c" field that contains a list of converters. */ |
| 4070 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4071 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4072 | return 0; |
| 4073 | lua_settable(L, -3); |
| 4074 | |
| 4075 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4076 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4077 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4078 | return 0; |
| 4079 | lua_settable(L, -3); |
| 4080 | |
| 4081 | /* Pop a class stream metatable and affect it to the table. */ |
| 4082 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4083 | lua_setmetatable(L, -2); |
| 4084 | |
| 4085 | return 1; |
| 4086 | } |
| 4087 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4088 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4089 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4090 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4091 | struct stream *s; |
| 4092 | const char *name; |
| 4093 | size_t len; |
| 4094 | struct sample smp; |
| 4095 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4096 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4097 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4098 | |
| 4099 | /* It is useles to retrieve the stream, but this function |
| 4100 | * runs only in a stream context. |
| 4101 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4102 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4103 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4104 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4105 | |
| 4106 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4107 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4108 | hlua_lua2smp(L, 3, &smp); |
| 4109 | |
| 4110 | /* Store the sample in a variable. */ |
| 4111 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4112 | |
| 4113 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4114 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4115 | else |
| 4116 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4117 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4118 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4122 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4123 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4124 | struct stream *s; |
| 4125 | const char *name; |
| 4126 | size_t len; |
| 4127 | struct sample smp; |
| 4128 | |
| 4129 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4130 | |
| 4131 | /* It is useles to retrieve the stream, but this function |
| 4132 | * runs only in a stream context. |
| 4133 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4134 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4135 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4136 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4137 | |
| 4138 | /* Unset the variable. */ |
| 4139 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4140 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4141 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4145 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4146 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4147 | struct stream *s; |
| 4148 | const char *name; |
| 4149 | size_t len; |
| 4150 | struct sample smp; |
| 4151 | |
| 4152 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4153 | |
| 4154 | /* It is useles to retrieve the stream, but this function |
| 4155 | * runs only in a stream context. |
| 4156 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4157 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4158 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4159 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4160 | |
| 4161 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4162 | if (!vars_get_by_name(name, len, &smp)) { |
| 4163 | lua_pushnil(L); |
| 4164 | return 1; |
| 4165 | } |
| 4166 | |
| 4167 | return hlua_smp2lua(L, &smp); |
| 4168 | } |
| 4169 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4170 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4171 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4172 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4173 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4174 | struct hlua *hlua; |
| 4175 | |
| 4176 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4177 | if (!s->hlua) |
| 4178 | return 0; |
| 4179 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4180 | |
| 4181 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4182 | |
| 4183 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4184 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4185 | |
| 4186 | /* Get and store new value. */ |
| 4187 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4188 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4189 | |
| 4190 | return 0; |
| 4191 | } |
| 4192 | |
| 4193 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4194 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4195 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4196 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4197 | struct hlua *hlua; |
| 4198 | |
| 4199 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4200 | if (!s->hlua) { |
| 4201 | lua_pushnil(L); |
| 4202 | return 1; |
| 4203 | } |
| 4204 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4205 | |
| 4206 | /* Push configuration index in the stack. */ |
| 4207 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4208 | |
| 4209 | return 1; |
| 4210 | } |
| 4211 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4212 | /* If expected data not yet available, it returns a yield. This function |
| 4213 | * consumes the data in the buffer. It returns a string containing the |
| 4214 | * data. This string can be empty. |
| 4215 | */ |
| 4216 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4217 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4218 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4219 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4220 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4221 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4222 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4223 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4224 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4225 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4226 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4227 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4228 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4229 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4230 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4231 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4232 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | /* End of data: commit the total strings and return. */ |
| 4236 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4237 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4238 | return 1; |
| 4239 | } |
| 4240 | |
| 4241 | /* Ensure that the block 2 length is usable. */ |
| 4242 | if (ret == 1) |
| 4243 | len2 = 0; |
| 4244 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4245 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4246 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4247 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4248 | |
| 4249 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4250 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4251 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4252 | return 1; |
| 4253 | } |
| 4254 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4255 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4256 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4257 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4258 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4259 | |
| 4260 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4261 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4262 | |
| 4263 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4264 | } |
| 4265 | |
| 4266 | /* If expected data not yet available, it returns a yield. This function |
| 4267 | * consumes the data in the buffer. It returns a string containing the |
| 4268 | * data. This string can be empty. |
| 4269 | */ |
| 4270 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4271 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4272 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4273 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4274 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4275 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4276 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4277 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4278 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4279 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4280 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4281 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4282 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4283 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4284 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4285 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4286 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4287 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /* End of data: commit the total strings and return. */ |
| 4291 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4292 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4293 | return 1; |
| 4294 | } |
| 4295 | |
| 4296 | /* Ensure that the block 2 length is usable. */ |
| 4297 | if (ret == 1) |
| 4298 | len2 = 0; |
| 4299 | |
| 4300 | if (len == -1) { |
| 4301 | |
| 4302 | /* If len == -1, catenate all the data avalaile and |
| 4303 | * yield because we want to get all the data until |
| 4304 | * the end of data stream. |
| 4305 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4306 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4307 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4308 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4309 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4310 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4311 | |
| 4312 | } else { |
| 4313 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4314 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4315 | if (len1 > len) |
| 4316 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4317 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4318 | len -= len1; |
| 4319 | |
| 4320 | /* Copy the second block. */ |
| 4321 | if (len2 > len) |
| 4322 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4323 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4324 | len -= len2; |
| 4325 | |
| 4326 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4327 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4328 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4329 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4330 | if (len > 0) { |
| 4331 | lua_pushinteger(L, len); |
| 4332 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4333 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4334 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4338 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4339 | return 1; |
| 4340 | } |
| 4341 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4342 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4343 | hlua_pusherror(L, "Lua: internal error"); |
| 4344 | WILL_LJMP(lua_error(L)); |
| 4345 | return 0; |
| 4346 | } |
| 4347 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4348 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4349 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4350 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4351 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4352 | int len = -1; |
| 4353 | |
| 4354 | if (lua_gettop(L) > 2) |
| 4355 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4356 | if (lua_gettop(L) >= 2) { |
| 4357 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4358 | lua_pop(L, 1); |
| 4359 | } |
| 4360 | |
| 4361 | /* Confirm or set the required length */ |
| 4362 | lua_pushinteger(L, len); |
| 4363 | |
| 4364 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4365 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4366 | |
| 4367 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4368 | } |
| 4369 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4370 | /* Append data in the output side of the buffer. This data is immediately |
| 4371 | * sent. The function returns the amount of data written. If the buffer |
| 4372 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4373 | * if the channel is closed. |
| 4374 | */ |
| 4375 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4376 | { |
| 4377 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4378 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4379 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4380 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4381 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4382 | struct channel *chn = si_ic(si); |
| 4383 | int max; |
| 4384 | |
| 4385 | /* Get the max amount of data which can write as input in the channel. */ |
| 4386 | max = channel_recv_max(chn); |
| 4387 | if (max > (len - l)) |
| 4388 | max = len - l; |
| 4389 | |
| 4390 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4391 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4392 | |
| 4393 | /* update counters. */ |
| 4394 | l += max; |
| 4395 | lua_pop(L, 1); |
| 4396 | lua_pushinteger(L, l); |
| 4397 | |
| 4398 | /* If some data is not send, declares the situation to the |
| 4399 | * applet, and returns a yield. |
| 4400 | */ |
| 4401 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4402 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4403 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | return 1; |
| 4407 | } |
| 4408 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4409 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4410 | * yield the LUA process, and resume it without checking the |
| 4411 | * input arguments. |
| 4412 | */ |
| 4413 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4414 | { |
| 4415 | MAY_LJMP(check_args(L, 2, "send")); |
| 4416 | lua_pushinteger(L, 0); |
| 4417 | |
| 4418 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4419 | } |
| 4420 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4421 | /* |
| 4422 | * |
| 4423 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4424 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4425 | * |
| 4426 | * |
| 4427 | */ |
| 4428 | |
| 4429 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4430 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4431 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4432 | __LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4433 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4434 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4435 | } |
| 4436 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4437 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4438 | * according with a current TXN. |
| 4439 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4440 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4441 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4442 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4443 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4444 | struct stream_interface *si = ctx->owner; |
| 4445 | struct stream *s = si_strm(si); |
| 4446 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4447 | struct htx *htx; |
| 4448 | struct htx_blk *blk; |
| 4449 | struct htx_sl *sl; |
| 4450 | struct ist path; |
| 4451 | unsigned long long len = 0; |
| 4452 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4453 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4454 | |
| 4455 | /* Check stack size. */ |
| 4456 | if (!lua_checkstack(L, 3)) |
| 4457 | return 0; |
| 4458 | |
| 4459 | /* Create the object: obj[0] = userdata. |
| 4460 | * Note that the base of the Converters object is the |
| 4461 | * same than the TXN object. |
| 4462 | */ |
| 4463 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4464 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4465 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4466 | luactx->appctx = ctx; |
| 4467 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4468 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4469 | luactx->htxn.s = s; |
| 4470 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4471 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4472 | /* Create the "f" field that contains a list of fetches. */ |
| 4473 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4474 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4475 | return 0; |
| 4476 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4477 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4478 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4479 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4480 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4481 | return 0; |
| 4482 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4483 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | /* Create the "c" field that contains a list of converters. */ |
| 4485 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4487 | return 0; |
| 4488 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4489 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4491 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4492 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4493 | return 0; |
| 4494 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4495 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4496 | htx = htxbuf(&s->req.buf); |
| 4497 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4498 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4499 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4500 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4501 | /* Stores the request method. */ |
| 4502 | lua_pushstring(L, "method"); |
| 4503 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4504 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4505 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4506 | /* Stores the http version. */ |
| 4507 | lua_pushstring(L, "version"); |
| 4508 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4509 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4510 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4511 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4512 | * the array on the top of the stack. |
| 4513 | */ |
| 4514 | lua_pushstring(L, "headers"); |
| 4515 | htxn.s = s; |
| 4516 | htxn.p = px; |
| 4517 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4518 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4519 | return 0; |
| 4520 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4521 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4522 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4523 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4524 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4525 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4526 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4527 | p = path.ptr; |
| 4528 | end = path.ptr + path.len; |
| 4529 | q = p; |
| 4530 | while (q < end && *q != '?') |
| 4531 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4532 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4533 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4534 | lua_pushstring(L, "path"); |
| 4535 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4536 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4537 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4538 | /* Stores the query string. */ |
| 4539 | lua_pushstring(L, "qs"); |
| 4540 | if (*q == '?') |
| 4541 | q++; |
| 4542 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4543 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4544 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4545 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4546 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4547 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4548 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4549 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4550 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4551 | break; |
| 4552 | if (type == HTX_BLK_DATA) |
| 4553 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4554 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4555 | if (htx->extra != ULLONG_MAX) |
| 4556 | len += htx->extra; |
| 4557 | |
| 4558 | /* Stores the request path. */ |
| 4559 | lua_pushstring(L, "length"); |
| 4560 | lua_pushinteger(L, len); |
| 4561 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4562 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4563 | /* Create an empty array of HTTP request headers. */ |
| 4564 | lua_pushstring(L, "response"); |
| 4565 | lua_newtable(L); |
| 4566 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4567 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4568 | /* Pop a class stream metatable and affect it to the table. */ |
| 4569 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4570 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4571 | |
| 4572 | return 1; |
| 4573 | } |
| 4574 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4575 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4576 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4577 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4578 | struct stream *s; |
| 4579 | const char *name; |
| 4580 | size_t len; |
| 4581 | struct sample smp; |
| 4582 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4583 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4584 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4585 | |
| 4586 | /* It is useles to retrieve the stream, but this function |
| 4587 | * runs only in a stream context. |
| 4588 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4589 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4590 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4591 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4592 | |
| 4593 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4594 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4595 | hlua_lua2smp(L, 3, &smp); |
| 4596 | |
| 4597 | /* Store the sample in a variable. */ |
| 4598 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4599 | |
| 4600 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4601 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4602 | else |
| 4603 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4604 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4605 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4609 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4610 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4611 | struct stream *s; |
| 4612 | const char *name; |
| 4613 | size_t len; |
| 4614 | struct sample smp; |
| 4615 | |
| 4616 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4617 | |
| 4618 | /* It is useles to retrieve the stream, but this function |
| 4619 | * runs only in a stream context. |
| 4620 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4621 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4622 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4623 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4624 | |
| 4625 | /* Unset the variable. */ |
| 4626 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4627 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4628 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4632 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4633 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4634 | struct stream *s; |
| 4635 | const char *name; |
| 4636 | size_t len; |
| 4637 | struct sample smp; |
| 4638 | |
| 4639 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4640 | |
| 4641 | /* It is useles to retrieve the stream, but this function |
| 4642 | * runs only in a stream context. |
| 4643 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4644 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4645 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4646 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4647 | |
| 4648 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4649 | if (!vars_get_by_name(name, len, &smp)) { |
| 4650 | lua_pushnil(L); |
| 4651 | return 1; |
| 4652 | } |
| 4653 | |
| 4654 | return hlua_smp2lua(L, &smp); |
| 4655 | } |
| 4656 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4657 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4658 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4659 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4660 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4661 | struct hlua *hlua; |
| 4662 | |
| 4663 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4664 | if (!s->hlua) |
| 4665 | return 0; |
| 4666 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4667 | |
| 4668 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4669 | |
| 4670 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4671 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4672 | |
| 4673 | /* Get and store new value. */ |
| 4674 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4675 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4676 | |
| 4677 | return 0; |
| 4678 | } |
| 4679 | |
| 4680 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4681 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4682 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4683 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4684 | struct hlua *hlua; |
| 4685 | |
| 4686 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4687 | if (!s->hlua) { |
| 4688 | lua_pushnil(L); |
| 4689 | return 1; |
| 4690 | } |
| 4691 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4692 | |
| 4693 | /* Push configuration index in the stack. */ |
| 4694 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4695 | |
| 4696 | return 1; |
| 4697 | } |
| 4698 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4699 | /* If expected data not yet available, it returns a yield. This function |
| 4700 | * consumes the data in the buffer. It returns a string containing the |
| 4701 | * data. This string can be empty. |
| 4702 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4703 | __LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4704 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4705 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4706 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4707 | struct channel *req = si_oc(si); |
| 4708 | struct htx *htx; |
| 4709 | struct htx_blk *blk; |
| 4710 | size_t count; |
| 4711 | int stop = 0; |
| 4712 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4713 | htx = htx_from_buf(&req->buf); |
| 4714 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4715 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4716 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4717 | while (count && !stop && blk) { |
| 4718 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4719 | uint32_t sz = htx_get_blksz(blk); |
| 4720 | struct ist v; |
| 4721 | uint32_t vlen; |
| 4722 | char *nl; |
| 4723 | |
| 4724 | vlen = sz; |
| 4725 | if (vlen > count) { |
| 4726 | if (type != HTX_BLK_DATA) |
| 4727 | break; |
| 4728 | vlen = count; |
| 4729 | } |
| 4730 | |
| 4731 | switch (type) { |
| 4732 | case HTX_BLK_UNUSED: |
| 4733 | break; |
| 4734 | |
| 4735 | case HTX_BLK_DATA: |
| 4736 | v = htx_get_blk_value(htx, blk); |
| 4737 | v.len = vlen; |
| 4738 | nl = istchr(v, '\n'); |
| 4739 | if (nl != NULL) { |
| 4740 | stop = 1; |
| 4741 | vlen = nl - v.ptr + 1; |
| 4742 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4743 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4744 | break; |
| 4745 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4746 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4747 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4748 | stop = 1; |
| 4749 | break; |
| 4750 | |
| 4751 | default: |
| 4752 | break; |
| 4753 | } |
| 4754 | |
| 4755 | co_set_data(req, co_data(req) - vlen); |
| 4756 | count -= vlen; |
| 4757 | if (sz == vlen) |
| 4758 | blk = htx_remove_blk(htx, blk); |
| 4759 | else { |
| 4760 | htx_cut_data_blk(htx, blk, vlen); |
| 4761 | break; |
| 4762 | } |
| 4763 | } |
| 4764 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4765 | /* The message was fully consumed and no more data are expected |
| 4766 | * (EOM flag set). |
| 4767 | */ |
| 4768 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4769 | stop = 1; |
| 4770 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4771 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4772 | if (!stop) { |
| 4773 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4774 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4778 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | return 1; |
| 4780 | } |
| 4781 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4782 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4783 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4784 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4785 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4786 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4787 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4788 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4789 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4790 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4791 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4792 | } |
| 4793 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4794 | /* If expected data not yet available, it returns a yield. This function |
| 4795 | * consumes the data in the buffer. It returns a string containing the |
| 4796 | * data. This string can be empty. |
| 4797 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4798 | __LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4799 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4800 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4801 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4802 | struct channel *req = si_oc(si); |
| 4803 | struct htx *htx; |
| 4804 | struct htx_blk *blk; |
| 4805 | size_t count; |
| 4806 | int len; |
| 4807 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4808 | htx = htx_from_buf(&req->buf); |
| 4809 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4810 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4811 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4812 | while (count && len && blk) { |
| 4813 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4814 | uint32_t sz = htx_get_blksz(blk); |
| 4815 | struct ist v; |
| 4816 | uint32_t vlen; |
| 4817 | |
| 4818 | vlen = sz; |
| 4819 | if (len > 0 && vlen > len) |
| 4820 | vlen = len; |
| 4821 | if (vlen > count) { |
| 4822 | if (type != HTX_BLK_DATA) |
| 4823 | break; |
| 4824 | vlen = count; |
| 4825 | } |
| 4826 | |
| 4827 | switch (type) { |
| 4828 | case HTX_BLK_UNUSED: |
| 4829 | break; |
| 4830 | |
| 4831 | case HTX_BLK_DATA: |
| 4832 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4833 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4834 | break; |
| 4835 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4836 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4837 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4838 | len = 0; |
| 4839 | break; |
| 4840 | |
| 4841 | default: |
| 4842 | break; |
| 4843 | } |
| 4844 | |
| 4845 | co_set_data(req, co_data(req) - vlen); |
| 4846 | count -= vlen; |
| 4847 | if (len > 0) |
| 4848 | len -= vlen; |
| 4849 | if (sz == vlen) |
| 4850 | blk = htx_remove_blk(htx, blk); |
| 4851 | else { |
| 4852 | htx_cut_data_blk(htx, blk, vlen); |
| 4853 | break; |
| 4854 | } |
| 4855 | } |
| 4856 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4857 | /* The message was fully consumed and no more data are expected |
| 4858 | * (EOM flag set). |
| 4859 | */ |
| 4860 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4861 | len = 0; |
| 4862 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4863 | htx_to_buf(htx, &req->buf); |
| 4864 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4865 | /* If we are no other data available, yield waiting for new data. */ |
| 4866 | if (len) { |
| 4867 | if (len > 0) { |
| 4868 | lua_pushinteger(L, len); |
| 4869 | lua_replace(L, 2); |
| 4870 | } |
| 4871 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4872 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4876 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4877 | return 1; |
| 4878 | } |
| 4879 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4880 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4881 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4882 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4883 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4884 | int len = -1; |
| 4885 | |
| 4886 | /* Check arguments. */ |
| 4887 | if (lua_gettop(L) > 2) |
| 4888 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4889 | if (lua_gettop(L) >= 2) { |
| 4890 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4891 | lua_pop(L, 1); |
| 4892 | } |
| 4893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4894 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4895 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4896 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4897 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4899 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | /* Append data in the output side of the buffer. This data is immediately |
| 4903 | * sent. The function returns the amount of data written. If the buffer |
| 4904 | * cannot contain the data, the function yields. The function returns -1 |
| 4905 | * if the channel is closed. |
| 4906 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4907 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4908 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4909 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4910 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4911 | struct channel *res = si_ic(si); |
| 4912 | struct htx *htx = htx_from_buf(&res->buf); |
| 4913 | const char *data; |
| 4914 | size_t len; |
| 4915 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4916 | int max; |
| 4917 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4918 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4919 | if (!max) |
| 4920 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4921 | |
| 4922 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4923 | |
| 4924 | /* Get the max amount of data which can write as input in the channel. */ |
| 4925 | if (max > (len - l)) |
| 4926 | max = len - l; |
| 4927 | |
| 4928 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4929 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4930 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4931 | |
| 4932 | /* update counters. */ |
| 4933 | l += max; |
| 4934 | lua_pop(L, 1); |
| 4935 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4936 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4937 | /* If some data is not send, declares the situation to the |
| 4938 | * applet, and returns a yield. |
| 4939 | */ |
| 4940 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4941 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4942 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4943 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4944 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4945 | } |
| 4946 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4947 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | return 1; |
| 4949 | } |
| 4950 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4951 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4952 | * yield the LUA process, and resume it without checking the |
| 4953 | * input arguments. |
| 4954 | */ |
| 4955 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4956 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4957 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4958 | |
| 4959 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4960 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4961 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4962 | WILL_LJMP(lua_error(L)); |
| 4963 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4964 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4965 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4966 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4967 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4968 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4972 | { |
| 4973 | const char *name; |
| 4974 | int ret; |
| 4975 | |
| 4976 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4977 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4978 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4979 | |
| 4980 | /* Push in the stack the "response" entry. */ |
| 4981 | ret = lua_getfield(L, 1, "response"); |
| 4982 | if (ret != LUA_TTABLE) { |
| 4983 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4984 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4985 | WILL_LJMP(lua_error(L)); |
| 4986 | } |
| 4987 | |
| 4988 | /* check if the header is already registered if it is not |
| 4989 | * the case, register it. |
| 4990 | */ |
| 4991 | ret = lua_getfield(L, -1, name); |
| 4992 | if (ret == LUA_TNIL) { |
| 4993 | |
| 4994 | /* Entry not found. */ |
| 4995 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4996 | |
| 4997 | /* Insert the new header name in the array in the top of the stack. |
| 4998 | * It left the new array in the top of the stack. |
| 4999 | */ |
| 5000 | lua_newtable(L); |
| 5001 | lua_pushvalue(L, 2); |
| 5002 | lua_pushvalue(L, -2); |
| 5003 | lua_settable(L, -4); |
| 5004 | |
| 5005 | } else if (ret != LUA_TTABLE) { |
| 5006 | |
| 5007 | /* corruption error. */ |
| 5008 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5009 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5010 | WILL_LJMP(lua_error(L)); |
| 5011 | } |
| 5012 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5013 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5014 | * the header value as new entry. |
| 5015 | */ |
| 5016 | lua_pushvalue(L, 3); |
| 5017 | ret = lua_rawlen(L, -2); |
| 5018 | lua_rawseti(L, -2, ret + 1); |
| 5019 | lua_pushboolean(L, 1); |
| 5020 | return 1; |
| 5021 | } |
| 5022 | |
| 5023 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5024 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5025 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5026 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5027 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5028 | |
| 5029 | if (status < 100 || status > 599) { |
| 5030 | lua_pushboolean(L, 0); |
| 5031 | return 1; |
| 5032 | } |
| 5033 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5034 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5035 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5036 | lua_pushboolean(L, 1); |
| 5037 | return 1; |
| 5038 | } |
| 5039 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5040 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5041 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5042 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5043 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5044 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5045 | struct channel *res = si_ic(si); |
| 5046 | struct htx *htx; |
| 5047 | struct htx_sl *sl; |
| 5048 | struct h1m h1m; |
| 5049 | const char *status, *reason; |
| 5050 | const char *name, *value; |
| 5051 | size_t nlen, vlen; |
| 5052 | unsigned int flags; |
| 5053 | |
| 5054 | /* Send the message at once. */ |
| 5055 | htx = htx_from_buf(&res->buf); |
| 5056 | h1m_init_res(&h1m); |
| 5057 | |
| 5058 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5059 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5060 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5062 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5063 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5064 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5065 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5066 | } |
| 5067 | else { |
| 5068 | flags = HTX_SL_F_IS_RESP; |
| 5069 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5070 | } |
| 5071 | if (!sl) { |
| 5072 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5073 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5074 | WILL_LJMP(lua_error(L)); |
| 5075 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5076 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5077 | |
| 5078 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5079 | lua_pushvalue(L, 0); |
| 5080 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5081 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5082 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5083 | WILL_LJMP(lua_error(L)); |
| 5084 | } |
| 5085 | |
| 5086 | /* Browse the list of headers. */ |
| 5087 | lua_pushnil(L); |
| 5088 | while(lua_next(L, -2) != 0) { |
| 5089 | /* We expect a string as -2. */ |
| 5090 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5091 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5092 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5093 | lua_typename(L, lua_type(L, -2))); |
| 5094 | WILL_LJMP(lua_error(L)); |
| 5095 | } |
| 5096 | name = lua_tolstring(L, -2, &nlen); |
| 5097 | |
| 5098 | /* We expect an array as -1. */ |
| 5099 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5100 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5101 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5102 | name, |
| 5103 | lua_typename(L, lua_type(L, -1))); |
| 5104 | WILL_LJMP(lua_error(L)); |
| 5105 | } |
| 5106 | |
| 5107 | /* Browse the table who is on the top of the stack. */ |
| 5108 | lua_pushnil(L); |
| 5109 | while(lua_next(L, -2) != 0) { |
| 5110 | int id; |
| 5111 | |
| 5112 | /* We expect a number as -2. */ |
| 5113 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5114 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5115 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5116 | name, |
| 5117 | lua_typename(L, lua_type(L, -2))); |
| 5118 | WILL_LJMP(lua_error(L)); |
| 5119 | } |
| 5120 | id = lua_tointeger(L, -2); |
| 5121 | |
| 5122 | /* We expect a string as -2. */ |
| 5123 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5124 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5125 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5126 | name, id, |
| 5127 | lua_typename(L, lua_type(L, -1))); |
| 5128 | WILL_LJMP(lua_error(L)); |
| 5129 | } |
| 5130 | value = lua_tolstring(L, -1, &vlen); |
| 5131 | |
| 5132 | /* Simple Protocol checks. */ |
| 5133 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5134 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5135 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5136 | struct ist v = ist2(value, vlen); |
| 5137 | int ret; |
| 5138 | |
| 5139 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5140 | if (ret < 0) { |
| 5141 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5142 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5143 | name); |
| 5144 | WILL_LJMP(lua_error(L)); |
| 5145 | } |
| 5146 | else if (ret == 0) |
| 5147 | goto next; /* Skip it */ |
| 5148 | } |
| 5149 | |
| 5150 | /* Add a new header */ |
| 5151 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5152 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5153 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5154 | name); |
| 5155 | WILL_LJMP(lua_error(L)); |
| 5156 | } |
| 5157 | next: |
| 5158 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5159 | lua_pop(L, 1); |
| 5160 | } |
| 5161 | |
| 5162 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5163 | lua_pop(L, 1); |
| 5164 | } |
| 5165 | |
| 5166 | if (h1m.flags & H1_MF_CHNK) |
| 5167 | h1m.flags &= ~H1_MF_CLEN; |
| 5168 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5169 | h1m.flags |= H1_MF_XFER_LEN; |
| 5170 | |
| 5171 | /* Uset HTX start-line flags */ |
| 5172 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5173 | flags |= HTX_SL_F_XFER_ENC; |
| 5174 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5175 | flags |= HTX_SL_F_XFER_LEN; |
| 5176 | if (h1m.flags & H1_MF_CHNK) |
| 5177 | flags |= HTX_SL_F_CHNK; |
| 5178 | else if (h1m.flags & H1_MF_CLEN) |
| 5179 | flags |= HTX_SL_F_CLEN; |
| 5180 | if (h1m.body_len == 0) |
| 5181 | flags |= HTX_SL_F_BODYLESS; |
| 5182 | } |
| 5183 | sl->flags |= flags; |
| 5184 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5185 | /* If we don't have a content-length set, and the HTTP version is 1.1 |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5186 | * and the status code implies the presence of a message body, we must |
| 5187 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5188 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5189 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5190 | */ |
| 5191 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5192 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5193 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5194 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5195 | /* Add a new header */ |
| 5196 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5197 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5198 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5199 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5200 | WILL_LJMP(lua_error(L)); |
| 5201 | } |
| 5202 | } |
| 5203 | |
| 5204 | /* Finalize headers. */ |
| 5205 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5206 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5207 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5208 | WILL_LJMP(lua_error(L)); |
| 5209 | } |
| 5210 | |
| 5211 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5212 | b_reset(&res->buf); |
| 5213 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5214 | WILL_LJMP(lua_error(L)); |
| 5215 | } |
| 5216 | |
| 5217 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5218 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5219 | |
| 5220 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5221 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5222 | return 0; |
| 5223 | |
| 5224 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5225 | /* We will build the status line and the headers of the HTTP response. |
| 5226 | * We will try send at once if its not possible, we give back the hand |
| 5227 | * waiting for more room. |
| 5228 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5229 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5230 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5231 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5232 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5233 | struct channel *res = si_ic(si); |
| 5234 | |
| 5235 | if (co_data(res)) { |
| 5236 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5237 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5238 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5239 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5243 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5244 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5245 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5246 | } |
| 5247 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5248 | /* |
| 5249 | * |
| 5250 | * |
| 5251 | * Class HTTP |
| 5252 | * |
| 5253 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5254 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5255 | |
| 5256 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5257 | * a class stream, otherwise it throws an error. |
| 5258 | */ |
| 5259 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5260 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5261 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5262 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5263 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5264 | /* This function creates and push in the stack a HTTP object |
| 5265 | * according with a current TXN. |
| 5266 | */ |
| 5267 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5268 | { |
| 5269 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5270 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5271 | /* Check stack size. */ |
| 5272 | if (!lua_checkstack(L, 3)) |
| 5273 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5274 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5275 | /* Create the object: obj[0] = userdata. |
| 5276 | * Note that the base of the Converters object is the |
| 5277 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5278 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5279 | lua_newtable(L); |
| 5280 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5281 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5282 | |
| 5283 | htxn->s = txn->s; |
| 5284 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5285 | htxn->dir = txn->dir; |
| 5286 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5287 | |
| 5288 | /* Pop a class stream metatable and affect it to the table. */ |
| 5289 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5290 | lua_setmetatable(L, -2); |
| 5291 | |
| 5292 | return 1; |
| 5293 | } |
| 5294 | |
| 5295 | /* This function creates ans returns an array of HTTP headers. |
| 5296 | * This function does not fails. It is used as wrapper with the |
| 5297 | * 2 following functions. |
| 5298 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5299 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5300 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5301 | struct htx *htx; |
| 5302 | int32_t pos; |
| 5303 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5304 | /* Create the table. */ |
| 5305 | lua_newtable(L); |
| 5306 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5307 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5308 | htx = htxbuf(&msg->chn->buf); |
| 5309 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5310 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5311 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5312 | struct ist n, v; |
| 5313 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5314 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5315 | if (type == HTX_BLK_HDR) { |
| 5316 | n = htx_get_blk_name(htx,blk); |
| 5317 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5318 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5319 | else if (type == HTX_BLK_EOH) |
| 5320 | break; |
| 5321 | else |
| 5322 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5323 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5324 | /* Check for existing entry: |
| 5325 | * assume that the table is on the top of the stack, and |
| 5326 | * push the key in the stack, the function lua_gettable() |
| 5327 | * perform the lookup. |
| 5328 | */ |
| 5329 | lua_pushlstring(L, n.ptr, n.len); |
| 5330 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5331 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5332 | switch (lua_type(L, -1)) { |
| 5333 | case LUA_TNIL: |
| 5334 | /* Table not found, create it. */ |
| 5335 | lua_pop(L, 1); /* remove the nil value. */ |
| 5336 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5337 | lua_newtable(L); /* create and push empty table. */ |
| 5338 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5339 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5340 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5341 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5342 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5343 | case LUA_TTABLE: |
| 5344 | /* Entry found: push the value in the table. */ |
| 5345 | len = lua_rawlen(L, -1); |
| 5346 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5347 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5348 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5349 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5350 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5351 | default: |
| 5352 | /* Other cases are errors. */ |
| 5353 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5354 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5355 | } |
| 5356 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5357 | return 1; |
| 5358 | } |
| 5359 | |
| 5360 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5361 | { |
| 5362 | struct hlua_txn *htxn; |
| 5363 | |
| 5364 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5365 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5366 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5367 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5368 | WILL_LJMP(lua_error(L)); |
| 5369 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5370 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5374 | { |
| 5375 | struct hlua_txn *htxn; |
| 5376 | |
| 5377 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5378 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5379 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5380 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5381 | WILL_LJMP(lua_error(L)); |
| 5382 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5383 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | /* This function replace full header, or just a value in |
| 5387 | * the request or in the response. It is a wrapper fir the |
| 5388 | * 4 following functions. |
| 5389 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5390 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5391 | { |
| 5392 | size_t name_len; |
| 5393 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5394 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5395 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5396 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5397 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5398 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5399 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5400 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5401 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5402 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5403 | http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5404 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5405 | return 0; |
| 5406 | } |
| 5407 | |
| 5408 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5409 | { |
| 5410 | struct hlua_txn *htxn; |
| 5411 | |
| 5412 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5413 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5414 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5415 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5416 | WILL_LJMP(lua_error(L)); |
| 5417 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5418 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5422 | { |
| 5423 | struct hlua_txn *htxn; |
| 5424 | |
| 5425 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5426 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5427 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5428 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5429 | WILL_LJMP(lua_error(L)); |
| 5430 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5431 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5435 | { |
| 5436 | struct hlua_txn *htxn; |
| 5437 | |
| 5438 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5439 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5440 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5441 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5442 | WILL_LJMP(lua_error(L)); |
| 5443 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5444 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5448 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5449 | struct hlua_txn *htxn; |
| 5450 | |
| 5451 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5452 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5453 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5454 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5455 | WILL_LJMP(lua_error(L)); |
| 5456 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5457 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5458 | } |
| 5459 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5460 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5461 | * It is a wrapper for the 2 following functions. |
| 5462 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5463 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5464 | { |
| 5465 | size_t len; |
| 5466 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5467 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5468 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5469 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5470 | ctx.blk = NULL; |
| 5471 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5472 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5473 | return 0; |
| 5474 | } |
| 5475 | |
| 5476 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5477 | { |
| 5478 | struct hlua_txn *htxn; |
| 5479 | |
| 5480 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5481 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5482 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5483 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5484 | WILL_LJMP(lua_error(L)); |
| 5485 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5486 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5490 | { |
| 5491 | struct hlua_txn *htxn; |
| 5492 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5493 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5494 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5495 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5496 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5497 | WILL_LJMP(lua_error(L)); |
| 5498 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5499 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | /* This function adds an header. It is a wrapper used by |
| 5503 | * the 2 following functions. |
| 5504 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5505 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5506 | { |
| 5507 | size_t name_len; |
| 5508 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5509 | size_t value_len; |
| 5510 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5511 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5512 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5513 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5514 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5519 | { |
| 5520 | struct hlua_txn *htxn; |
| 5521 | |
| 5522 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5523 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5524 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5525 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5526 | WILL_LJMP(lua_error(L)); |
| 5527 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5528 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5532 | { |
| 5533 | struct hlua_txn *htxn; |
| 5534 | |
| 5535 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5536 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5537 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5538 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5539 | WILL_LJMP(lua_error(L)); |
| 5540 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5541 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5545 | { |
| 5546 | struct hlua_txn *htxn; |
| 5547 | |
| 5548 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5549 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5550 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5551 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5552 | WILL_LJMP(lua_error(L)); |
| 5553 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5554 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5555 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5559 | { |
| 5560 | struct hlua_txn *htxn; |
| 5561 | |
| 5562 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5563 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5564 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5565 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5566 | WILL_LJMP(lua_error(L)); |
| 5567 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5568 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5569 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | /* This function set the method. */ |
| 5573 | static int hlua_http_req_set_meth(lua_State *L) |
| 5574 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5575 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5576 | size_t name_len; |
| 5577 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5578 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5579 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5580 | WILL_LJMP(lua_error(L)); |
| 5581 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5582 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5583 | return 1; |
| 5584 | } |
| 5585 | |
| 5586 | /* This function set the method. */ |
| 5587 | static int hlua_http_req_set_path(lua_State *L) |
| 5588 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5589 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5590 | size_t name_len; |
| 5591 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5592 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5593 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5594 | WILL_LJMP(lua_error(L)); |
| 5595 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5596 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5597 | return 1; |
| 5598 | } |
| 5599 | |
| 5600 | /* This function set the query-string. */ |
| 5601 | static int hlua_http_req_set_query(lua_State *L) |
| 5602 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5603 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5604 | size_t name_len; |
| 5605 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5606 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5607 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5608 | WILL_LJMP(lua_error(L)); |
| 5609 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5610 | /* Check length. */ |
| 5611 | if (name_len > trash.size - 1) { |
| 5612 | lua_pushboolean(L, 0); |
| 5613 | return 1; |
| 5614 | } |
| 5615 | |
| 5616 | /* Add the mark question as prefix. */ |
| 5617 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5618 | trash.area[trash.data++] = '?'; |
| 5619 | memcpy(trash.area + trash.data, name, name_len); |
| 5620 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5621 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5622 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5623 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5624 | return 1; |
| 5625 | } |
| 5626 | |
| 5627 | /* This function set the uri. */ |
| 5628 | static int hlua_http_req_set_uri(lua_State *L) |
| 5629 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5630 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5631 | size_t name_len; |
| 5632 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5633 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5634 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5635 | WILL_LJMP(lua_error(L)); |
| 5636 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5637 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5638 | return 1; |
| 5639 | } |
| 5640 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5641 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5642 | static int hlua_http_res_set_status(lua_State *L) |
| 5643 | { |
| 5644 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5645 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5646 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5647 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5648 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5649 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5650 | WILL_LJMP(lua_error(L)); |
| 5651 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5652 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5653 | return 0; |
| 5654 | } |
| 5655 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5656 | /* |
| 5657 | * |
| 5658 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5659 | * Class TXN |
| 5660 | * |
| 5661 | * |
| 5662 | */ |
| 5663 | |
| 5664 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5665 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5666 | */ |
| 5667 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5668 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5669 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5670 | } |
| 5671 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5672 | __LJMP static int hlua_set_var(lua_State *L) |
| 5673 | { |
| 5674 | struct hlua_txn *htxn; |
| 5675 | const char *name; |
| 5676 | size_t len; |
| 5677 | struct sample smp; |
| 5678 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5679 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5680 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5681 | |
| 5682 | /* It is useles to retrieve the stream, but this function |
| 5683 | * runs only in a stream context. |
| 5684 | */ |
| 5685 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5686 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5687 | |
| 5688 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5689 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5690 | hlua_lua2smp(L, 3, &smp); |
| 5691 | |
| 5692 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5693 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5694 | |
| 5695 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5696 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5697 | else |
| 5698 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5699 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5700 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5701 | } |
| 5702 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5703 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5704 | { |
| 5705 | struct hlua_txn *htxn; |
| 5706 | const char *name; |
| 5707 | size_t len; |
| 5708 | struct sample smp; |
| 5709 | |
| 5710 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5711 | |
| 5712 | /* It is useles to retrieve the stream, but this function |
| 5713 | * runs only in a stream context. |
| 5714 | */ |
| 5715 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5716 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5717 | |
| 5718 | /* Unset the variable. */ |
| 5719 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5720 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5721 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5722 | } |
| 5723 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5724 | __LJMP static int hlua_get_var(lua_State *L) |
| 5725 | { |
| 5726 | struct hlua_txn *htxn; |
| 5727 | const char *name; |
| 5728 | size_t len; |
| 5729 | struct sample smp; |
| 5730 | |
| 5731 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5732 | |
| 5733 | /* It is useles to retrieve the stream, but this function |
| 5734 | * runs only in a stream context. |
| 5735 | */ |
| 5736 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5737 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5738 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5739 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 5740 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5741 | lua_pushnil(L); |
| 5742 | return 1; |
| 5743 | } |
| 5744 | |
| 5745 | return hlua_smp2lua(L, &smp); |
| 5746 | } |
| 5747 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5748 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5749 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5750 | struct hlua *hlua; |
| 5751 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5752 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5753 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5754 | /* It is useles to retrieve the stream, but this function |
| 5755 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5756 | */ |
| 5757 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5758 | |
| 5759 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5760 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5761 | if (!hlua) |
| 5762 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5763 | |
| 5764 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5765 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5766 | |
| 5767 | /* Get and store new value. */ |
| 5768 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5769 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5770 | |
| 5771 | return 0; |
| 5772 | } |
| 5773 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5774 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5775 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5776 | struct hlua *hlua; |
| 5777 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5778 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5779 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5780 | /* It is useles to retrieve the stream, but this function |
| 5781 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5782 | */ |
| 5783 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5784 | |
| 5785 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5786 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5787 | if (!hlua) { |
| 5788 | lua_pushnil(L); |
| 5789 | return 1; |
| 5790 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5791 | |
| 5792 | /* Push configuration index in the stack. */ |
| 5793 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5794 | |
| 5795 | return 1; |
| 5796 | } |
| 5797 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5798 | /* Create stack entry containing a class TXN. This function |
| 5799 | * return 0 if the stack does not contains free slots, |
| 5800 | * otherwise it returns 1. |
| 5801 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5802 | static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5803 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5804 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5805 | |
| 5806 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5807 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5808 | return 0; |
| 5809 | |
| 5810 | /* NOTE: The allocation never fails. The failure |
| 5811 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5812 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5813 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5814 | /* Create the object: obj[0] = userdata. */ |
| 5815 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5816 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5817 | lua_rawseti(L, -2, 0); |
| 5818 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5819 | htxn->s = s; |
| 5820 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5821 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5822 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5823 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5824 | /* Create the "f" field that contains a list of fetches. */ |
| 5825 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5826 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5827 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5828 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5829 | |
| 5830 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5831 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5832 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING)) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5833 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5834 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5835 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5836 | /* Create the "c" field that contains a list of converters. */ |
| 5837 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5838 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5839 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5840 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5841 | |
| 5842 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5843 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5844 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5845 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5846 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5847 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5848 | /* Create the "req" field that contains the request channel object. */ |
| 5849 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5850 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5851 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5852 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5853 | |
| 5854 | /* Create the "res" field that contains the response channel object. */ |
| 5855 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5856 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5857 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5858 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5859 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5860 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5861 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5862 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5863 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5864 | return 0; |
| 5865 | } |
| 5866 | else |
| 5867 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5868 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5869 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5870 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5871 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5872 | lua_setmetatable(L, -2); |
| 5873 | |
| 5874 | return 1; |
| 5875 | } |
| 5876 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5877 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5878 | { |
| 5879 | const char *msg; |
| 5880 | struct hlua_txn *htxn; |
| 5881 | |
| 5882 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5883 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5884 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5885 | |
| 5886 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5887 | return 0; |
| 5888 | } |
| 5889 | |
| 5890 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5891 | { |
| 5892 | int level; |
| 5893 | const char *msg; |
| 5894 | struct hlua_txn *htxn; |
| 5895 | |
| 5896 | MAY_LJMP(check_args(L, 3, "log")); |
| 5897 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5898 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5899 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5900 | |
| 5901 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5902 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5903 | |
| 5904 | hlua_sendlog(htxn->s->be, level, msg); |
| 5905 | return 0; |
| 5906 | } |
| 5907 | |
| 5908 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5909 | { |
| 5910 | const char *msg; |
| 5911 | struct hlua_txn *htxn; |
| 5912 | |
| 5913 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5914 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5915 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5916 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5917 | return 0; |
| 5918 | } |
| 5919 | |
| 5920 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5921 | { |
| 5922 | const char *msg; |
| 5923 | struct hlua_txn *htxn; |
| 5924 | |
| 5925 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5926 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5927 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5928 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5929 | return 0; |
| 5930 | } |
| 5931 | |
| 5932 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5933 | { |
| 5934 | const char *msg; |
| 5935 | struct hlua_txn *htxn; |
| 5936 | |
| 5937 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5938 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5939 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5940 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5941 | return 0; |
| 5942 | } |
| 5943 | |
| 5944 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5945 | { |
| 5946 | const char *msg; |
| 5947 | struct hlua_txn *htxn; |
| 5948 | |
| 5949 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5950 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5951 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5952 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5953 | return 0; |
| 5954 | } |
| 5955 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5956 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5957 | { |
| 5958 | struct hlua_txn *htxn; |
| 5959 | int ll; |
| 5960 | |
| 5961 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5962 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5963 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5964 | |
| 5965 | if (ll < 0 || ll > 7) |
| 5966 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5967 | |
| 5968 | htxn->s->logs.level = ll; |
| 5969 | return 0; |
| 5970 | } |
| 5971 | |
| 5972 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5973 | { |
| 5974 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5975 | int tos; |
| 5976 | |
| 5977 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5978 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5979 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5980 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5981 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5982 | return 0; |
| 5983 | } |
| 5984 | |
| 5985 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5986 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5987 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5988 | int mark; |
| 5989 | |
| 5990 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5991 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5992 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5993 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5994 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5995 | return 0; |
| 5996 | } |
| 5997 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5998 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5999 | { |
| 6000 | struct hlua_txn *htxn; |
| 6001 | |
| 6002 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6003 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6004 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6005 | return 0; |
| 6006 | } |
| 6007 | |
| 6008 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6009 | { |
| 6010 | struct hlua_txn *htxn; |
| 6011 | |
| 6012 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6013 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6014 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6015 | return 0; |
| 6016 | } |
| 6017 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6018 | /* Forward the Reply object to the client. This function converts the reply in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 6019 | * HTX an push it to into the response channel. It is response to forward the |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6020 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 6021 | * error. The Reply must be on top of the stack. |
| 6022 | */ |
| 6023 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 6024 | { |
| 6025 | struct htx *htx; |
| 6026 | struct htx_sl *sl; |
| 6027 | struct h1m h1m; |
| 6028 | const char *status, *reason, *body; |
| 6029 | size_t status_len, reason_len, body_len; |
| 6030 | int ret, code, flags; |
| 6031 | |
| 6032 | code = 200; |
| 6033 | status = "200"; |
| 6034 | status_len = 3; |
| 6035 | ret = lua_getfield(L, -1, "status"); |
| 6036 | if (ret == LUA_TNUMBER) { |
| 6037 | code = lua_tointeger(L, -1); |
| 6038 | status = lua_tolstring(L, -1, &status_len); |
| 6039 | } |
| 6040 | lua_pop(L, 1); |
| 6041 | |
| 6042 | reason = http_get_reason(code); |
| 6043 | reason_len = strlen(reason); |
| 6044 | ret = lua_getfield(L, -1, "reason"); |
| 6045 | if (ret == LUA_TSTRING) |
| 6046 | reason = lua_tolstring(L, -1, &reason_len); |
| 6047 | lua_pop(L, 1); |
| 6048 | |
| 6049 | body = NULL; |
| 6050 | body_len = 0; |
| 6051 | ret = lua_getfield(L, -1, "body"); |
| 6052 | if (ret == LUA_TSTRING) |
| 6053 | body = lua_tolstring(L, -1, &body_len); |
| 6054 | lua_pop(L, 1); |
| 6055 | |
| 6056 | /* Prepare the response before inserting the headers */ |
| 6057 | h1m_init_res(&h1m); |
| 6058 | htx = htx_from_buf(&s->res.buf); |
| 6059 | channel_htx_truncate(&s->res, htx); |
| 6060 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 6061 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 6062 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 6063 | ist2(status, status_len), ist2(reason, reason_len)); |
| 6064 | } |
| 6065 | else { |
| 6066 | flags = HTX_SL_F_IS_RESP; |
| 6067 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 6068 | ist2(status, status_len), ist2(reason, reason_len)); |
| 6069 | } |
| 6070 | if (!sl) |
| 6071 | goto fail; |
| 6072 | sl->info.res.status = code; |
| 6073 | |
| 6074 | /* Push in the stack the "headers" entry. */ |
| 6075 | ret = lua_getfield(L, -1, "headers"); |
| 6076 | if (ret != LUA_TTABLE) |
| 6077 | goto skip_headers; |
| 6078 | |
| 6079 | lua_pushnil(L); |
| 6080 | while (lua_next(L, -2) != 0) { |
| 6081 | struct ist name, value; |
| 6082 | const char *n, *v; |
| 6083 | size_t nlen, vlen; |
| 6084 | |
| 6085 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 6086 | /* Skip element if the key is not a string or if the value is not a table */ |
| 6087 | goto next_hdr; |
| 6088 | } |
| 6089 | |
| 6090 | n = lua_tolstring(L, -2, &nlen); |
| 6091 | name = ist2(n, nlen); |
| 6092 | if (isteqi(name, ist("content-length"))) { |
| 6093 | /* Always skip content-length header. It will be added |
| 6094 | * later with the correct len |
| 6095 | */ |
| 6096 | goto next_hdr; |
| 6097 | } |
| 6098 | |
| 6099 | /* Loop on header's values */ |
| 6100 | lua_pushnil(L); |
| 6101 | while (lua_next(L, -2)) { |
| 6102 | if (!lua_isstring(L, -1)) { |
| 6103 | /* Skip the value if it is not a string */ |
| 6104 | goto next_value; |
| 6105 | } |
| 6106 | |
| 6107 | v = lua_tolstring(L, -1, &vlen); |
| 6108 | value = ist2(v, vlen); |
| 6109 | |
| 6110 | if (isteqi(name, ist("transfer-encoding"))) |
| 6111 | h1_parse_xfer_enc_header(&h1m, value); |
| 6112 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 6113 | goto fail; |
| 6114 | |
| 6115 | next_value: |
| 6116 | lua_pop(L, 1); |
| 6117 | } |
| 6118 | |
| 6119 | next_hdr: |
| 6120 | lua_pop(L, 1); |
| 6121 | } |
| 6122 | skip_headers: |
| 6123 | lua_pop(L, 1); |
| 6124 | |
| 6125 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 6126 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 6127 | const char *clen = ultoa(body_len); |
| 6128 | |
| 6129 | h1m.flags |= H1_MF_CLEN; |
| 6130 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 6131 | goto fail; |
| 6132 | } |
| 6133 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 6134 | h1m.flags |= H1_MF_XFER_LEN; |
| 6135 | |
| 6136 | /* Update HTX start-line flags */ |
| 6137 | if (h1m.flags & H1_MF_XFER_ENC) |
| 6138 | flags |= HTX_SL_F_XFER_ENC; |
| 6139 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 6140 | flags |= HTX_SL_F_XFER_LEN; |
| 6141 | if (h1m.flags & H1_MF_CHNK) |
| 6142 | flags |= HTX_SL_F_CHNK; |
| 6143 | else if (h1m.flags & H1_MF_CLEN) |
| 6144 | flags |= HTX_SL_F_CLEN; |
| 6145 | if (h1m.body_len == 0) |
| 6146 | flags |= HTX_SL_F_BODYLESS; |
| 6147 | } |
| 6148 | sl->flags |= flags; |
| 6149 | |
| 6150 | |
| 6151 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 6152 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6153 | goto fail; |
| 6154 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 6155 | htx->flags |= HTX_FL_EOM; |
| 6156 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6157 | /* Now, forward the response and terminate the transaction */ |
| 6158 | s->txn->status = code; |
| 6159 | htx_to_buf(htx, &s->res.buf); |
| 6160 | if (!http_forward_proxy_resp(s, 1)) |
| 6161 | goto fail; |
| 6162 | |
| 6163 | return 1; |
| 6164 | |
| 6165 | fail: |
| 6166 | channel_htx_truncate(&s->res, htx); |
| 6167 | return 0; |
| 6168 | } |
| 6169 | |
| 6170 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 6171 | * processing is just aborted. Nothing is returned to the client and all |
| 6172 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 6173 | * is forwarded to the client before terminating the transaction. On success, |
| 6174 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 6175 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 6176 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6177 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6178 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6179 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6180 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6181 | struct stream *s; |
| 6182 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6183 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6184 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6185 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6186 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 6187 | * just end the execution of the current lua code. */ |
| 6188 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6189 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6190 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6191 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6192 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6193 | struct channel *req = &s->req; |
| 6194 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6195 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6196 | channel_auto_read(req); |
| 6197 | channel_abort(req); |
| 6198 | channel_auto_close(req); |
| 6199 | channel_erase(req); |
| 6200 | |
| 6201 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 6202 | channel_auto_read(res); |
| 6203 | channel_auto_close(res); |
| 6204 | channel_shutr_now(res); |
| 6205 | |
| 6206 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 6207 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6208 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6209 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6210 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 6211 | /* No reply or invalid reply */ |
| 6212 | s->txn->status = 0; |
| 6213 | http_reply_and_close(s, 0, NULL); |
| 6214 | } |
| 6215 | else { |
| 6216 | /* Remove extra args to have the reply on top of the stack */ |
| 6217 | if (lua_gettop(L) > 2) |
| 6218 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6219 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6220 | if (!hlua_txn_forward_reply(L, s)) { |
| 6221 | if (!(s->flags & SF_ERR_MASK)) |
| 6222 | s->flags |= SF_ERR_PRXCOND; |
| 6223 | lua_pushinteger(L, ACT_RET_ERR); |
| 6224 | WILL_LJMP(hlua_done(L)); |
| 6225 | return 0; /* Never reached */ |
| 6226 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6227 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6228 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6229 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 6230 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 6231 | /* let's log the request time */ |
| 6232 | s->logs.tv_request = now; |
| 6233 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 6234 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6235 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6236 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6237 | done: |
| 6238 | if (!(s->flags & SF_ERR_MASK)) |
| 6239 | s->flags |= SF_ERR_LOCAL; |
| 6240 | if (!(s->flags & SF_FINST_MASK)) |
| 6241 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6242 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 6243 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6244 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6245 | return 0; |
| 6246 | } |
| 6247 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6248 | /* |
| 6249 | * |
| 6250 | * |
| 6251 | * Class REPLY |
| 6252 | * |
| 6253 | * |
| 6254 | */ |
| 6255 | |
| 6256 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 6257 | * free slots, the function fails and returns 0; |
| 6258 | */ |
| 6259 | static int hlua_txn_reply_new(lua_State *L) |
| 6260 | { |
| 6261 | struct hlua_txn *htxn; |
| 6262 | const char *reason, *body = NULL; |
| 6263 | int ret, status; |
| 6264 | |
| 6265 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6266 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6267 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6268 | WILL_LJMP(lua_error(L)); |
| 6269 | } |
| 6270 | |
| 6271 | /* Default value */ |
| 6272 | status = 200; |
| 6273 | reason = http_get_reason(status); |
| 6274 | |
| 6275 | if (lua_istable(L, 2)) { |
| 6276 | /* load status and reason from the table argument at index 2 */ |
| 6277 | ret = lua_getfield(L, 2, "status"); |
| 6278 | if (ret == LUA_TNIL) |
| 6279 | goto reason; |
| 6280 | else if (ret != LUA_TNUMBER) { |
| 6281 | /* invalid status: ignore the reason */ |
| 6282 | goto body; |
| 6283 | } |
| 6284 | status = lua_tointeger(L, -1); |
| 6285 | |
| 6286 | reason: |
| 6287 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6288 | ret = lua_getfield(L, 2, "reason"); |
| 6289 | if (ret == LUA_TSTRING) |
| 6290 | reason = lua_tostring(L, -1); |
| 6291 | |
| 6292 | body: |
| 6293 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6294 | ret = lua_getfield(L, 2, "body"); |
| 6295 | if (ret == LUA_TSTRING) |
| 6296 | body = lua_tostring(L, -1); |
| 6297 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6298 | } |
| 6299 | |
| 6300 | /* Create the Reply table */ |
| 6301 | lua_newtable(L); |
| 6302 | |
| 6303 | /* Add status element */ |
| 6304 | lua_pushstring(L, "status"); |
| 6305 | lua_pushinteger(L, status); |
| 6306 | lua_settable(L, -3); |
| 6307 | |
| 6308 | /* Add reason element */ |
| 6309 | reason = http_get_reason(status); |
| 6310 | lua_pushstring(L, "reason"); |
| 6311 | lua_pushstring(L, reason); |
| 6312 | lua_settable(L, -3); |
| 6313 | |
| 6314 | /* Add body element, nil if undefined */ |
| 6315 | lua_pushstring(L, "body"); |
| 6316 | if (body) |
| 6317 | lua_pushstring(L, body); |
| 6318 | else |
| 6319 | lua_pushnil(L); |
| 6320 | lua_settable(L, -3); |
| 6321 | |
| 6322 | /* Add headers element */ |
| 6323 | lua_pushstring(L, "headers"); |
| 6324 | lua_newtable(L); |
| 6325 | |
| 6326 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6327 | if (lua_istable(L, 2)) { |
| 6328 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6329 | ret = lua_getfield(L, 2, "headers"); |
| 6330 | if (ret == LUA_TTABLE) { |
| 6331 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6332 | lua_pushnil(L); |
| 6333 | while (lua_next(L, -2) != 0) { |
| 6334 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6335 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6336 | /* invalid value type, skip it */ |
| 6337 | lua_pop(L, 1); |
| 6338 | continue; |
| 6339 | } |
| 6340 | |
| 6341 | |
| 6342 | /* Duplicate the key and swap it with the value. */ |
| 6343 | lua_pushvalue(L, -2); |
| 6344 | lua_insert(L, -2); |
| 6345 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6346 | |
| 6347 | lua_newtable(L); |
| 6348 | lua_insert(L, -2); |
| 6349 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6350 | |
| 6351 | if (lua_isstring(L, -1)) { |
| 6352 | /* push the value in the inner table */ |
| 6353 | lua_rawseti(L, -2, 1); |
| 6354 | } |
| 6355 | else { /* table */ |
| 6356 | lua_pushnil(L); |
| 6357 | while (lua_next(L, -2) != 0) { |
| 6358 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6359 | if (!lua_isstring(L, -1)) { |
| 6360 | /* invalid value type, skip it*/ |
| 6361 | lua_pop(L, 1); |
| 6362 | continue; |
| 6363 | } |
| 6364 | /* push the value in the inner table */ |
| 6365 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6366 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6367 | } |
| 6368 | lua_pop(L, 1); |
| 6369 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6370 | } |
| 6371 | |
| 6372 | /* push (k,v) on the stack in the headers table: |
| 6373 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6374 | */ |
| 6375 | lua_settable(L, -5); |
| 6376 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6377 | } |
| 6378 | } |
| 6379 | lua_pop(L, 1); |
| 6380 | } |
| 6381 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6382 | lua_settable(L, -3); |
| 6383 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6384 | |
| 6385 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6386 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6387 | lua_setmetatable(L, -2); |
| 6388 | return 1; |
| 6389 | } |
| 6390 | |
| 6391 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6392 | * provided, the default one corresponding to the status code is used. |
| 6393 | */ |
| 6394 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6395 | { |
| 6396 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6397 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6398 | |
| 6399 | /* First argument (self) must be a table */ |
| 6400 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6401 | |
| 6402 | if (status < 100 || status > 599) { |
| 6403 | lua_pushboolean(L, 0); |
| 6404 | return 1; |
| 6405 | } |
| 6406 | if (!reason) |
| 6407 | reason = http_get_reason(status); |
| 6408 | |
| 6409 | lua_pushinteger(L, status); |
| 6410 | lua_setfield(L, 1, "status"); |
| 6411 | |
| 6412 | lua_pushstring(L, reason); |
| 6413 | lua_setfield(L, 1, "reason"); |
| 6414 | |
| 6415 | lua_pushboolean(L, 1); |
| 6416 | return 1; |
| 6417 | } |
| 6418 | |
| 6419 | /* Add a header into the reply object. Each header name is associated to an |
| 6420 | * array of values in the "headers" table. If the header name is not found, a |
| 6421 | * new entry is created. |
| 6422 | */ |
| 6423 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6424 | { |
| 6425 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6426 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6427 | int ret; |
| 6428 | |
| 6429 | /* First argument (self) must be a table */ |
| 6430 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6431 | |
| 6432 | /* Push in the stack the "headers" entry. */ |
| 6433 | ret = lua_getfield(L, 1, "headers"); |
| 6434 | if (ret != LUA_TTABLE) { |
| 6435 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6436 | WILL_LJMP(lua_error(L)); |
| 6437 | } |
| 6438 | |
| 6439 | /* check if the header is already registered. If not, register it. */ |
| 6440 | ret = lua_getfield(L, -1, name); |
| 6441 | if (ret == LUA_TNIL) { |
| 6442 | /* Entry not found. */ |
| 6443 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6444 | |
| 6445 | /* Insert the new header name in the array in the top of the stack. |
| 6446 | * It left the new array in the top of the stack. |
| 6447 | */ |
| 6448 | lua_newtable(L); |
| 6449 | lua_pushstring(L, name); |
| 6450 | lua_pushvalue(L, -2); |
| 6451 | lua_settable(L, -4); |
| 6452 | } |
| 6453 | else if (ret != LUA_TTABLE) { |
| 6454 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6455 | WILL_LJMP(lua_error(L)); |
| 6456 | } |
| 6457 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6458 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6459 | * the header value as new entry. |
| 6460 | */ |
| 6461 | lua_pushstring(L, value); |
| 6462 | ret = lua_rawlen(L, -2); |
| 6463 | lua_rawseti(L, -2, ret + 1); |
| 6464 | |
| 6465 | lua_pushboolean(L, 1); |
| 6466 | return 1; |
| 6467 | } |
| 6468 | |
| 6469 | /* Remove all occurrences of a given header name. */ |
| 6470 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6471 | { |
| 6472 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6473 | int ret; |
| 6474 | |
| 6475 | /* First argument (self) must be a table */ |
| 6476 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6477 | |
| 6478 | /* Push in the stack the "headers" entry. */ |
| 6479 | ret = lua_getfield(L, 1, "headers"); |
| 6480 | if (ret != LUA_TTABLE) { |
| 6481 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6482 | WILL_LJMP(lua_error(L)); |
| 6483 | } |
| 6484 | |
| 6485 | lua_pushstring(L, name); |
| 6486 | lua_pushnil(L); |
| 6487 | lua_settable(L, -3); |
| 6488 | |
| 6489 | lua_pushboolean(L, 1); |
| 6490 | return 1; |
| 6491 | } |
| 6492 | |
| 6493 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6494 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6495 | { |
| 6496 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6497 | |
| 6498 | /* First argument (self) must be a table */ |
| 6499 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6500 | |
| 6501 | lua_pushstring(L, payload); |
| 6502 | lua_setfield(L, 1, "body"); |
| 6503 | |
| 6504 | lua_pushboolean(L, 1); |
| 6505 | return 1; |
| 6506 | } |
| 6507 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6508 | __LJMP static int hlua_log(lua_State *L) |
| 6509 | { |
| 6510 | int level; |
| 6511 | const char *msg; |
| 6512 | |
| 6513 | MAY_LJMP(check_args(L, 2, "log")); |
| 6514 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6515 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6516 | |
| 6517 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6518 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6519 | |
| 6520 | hlua_sendlog(NULL, level, msg); |
| 6521 | return 0; |
| 6522 | } |
| 6523 | |
| 6524 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6525 | { |
| 6526 | const char *msg; |
| 6527 | |
| 6528 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6529 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6530 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6531 | return 0; |
| 6532 | } |
| 6533 | |
| 6534 | __LJMP static int hlua_log_info(lua_State *L) |
| 6535 | { |
| 6536 | const char *msg; |
| 6537 | |
| 6538 | MAY_LJMP(check_args(L, 1, "info")); |
| 6539 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6540 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6541 | return 0; |
| 6542 | } |
| 6543 | |
| 6544 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6545 | { |
| 6546 | const char *msg; |
| 6547 | |
| 6548 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6549 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6550 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6551 | return 0; |
| 6552 | } |
| 6553 | |
| 6554 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6555 | { |
| 6556 | const char *msg; |
| 6557 | |
| 6558 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6559 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6560 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6561 | return 0; |
| 6562 | } |
| 6563 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6564 | __LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6565 | { |
| 6566 | int wakeup_ms = lua_tointeger(L, -1); |
| 6567 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6568 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6569 | return 0; |
| 6570 | } |
| 6571 | |
| 6572 | __LJMP static int hlua_sleep(lua_State *L) |
| 6573 | { |
| 6574 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6575 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6576 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6577 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6578 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6579 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6580 | wakeup_ms = tick_add(now_ms, delay); |
| 6581 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6582 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6583 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6584 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6585 | } |
| 6586 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6587 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6588 | { |
| 6589 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6590 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6591 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6592 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6593 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6594 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6595 | wakeup_ms = tick_add(now_ms, delay); |
| 6596 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6597 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6598 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6599 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6600 | } |
| 6601 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6602 | /* This functionis an LUA binding. it permits to give back |
| 6603 | * the hand at the HAProxy scheduler. It is used when the |
| 6604 | * LUA processing consumes a lot of time. |
| 6605 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6606 | __LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6607 | { |
| 6608 | return 0; |
| 6609 | } |
| 6610 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6611 | __LJMP static int hlua_yield(lua_State *L) |
| 6612 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6613 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6614 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6615 | } |
| 6616 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6617 | /* This function change the nice of the currently executed |
| 6618 | * task. It is used set low or high priority at the current |
| 6619 | * task. |
| 6620 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6621 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6622 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6623 | struct hlua *hlua; |
| 6624 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6625 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6626 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6627 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6628 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6629 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6630 | hlua = hlua_gethlua(L); |
| 6631 | |
| 6632 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6633 | if (!hlua || !hlua->task) |
| 6634 | return 0; |
| 6635 | |
| 6636 | if (nice < -1024) |
| 6637 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6638 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6639 | nice = 1024; |
| 6640 | |
| 6641 | hlua->task->nice = nice; |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6645 | /* This function is used as a callback of a task. It is called by the |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6646 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6647 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6648 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6649 | * |
| 6650 | * Task wrapper are longjmp safe because the only one Lua code |
| 6651 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6652 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6653 | struct task *hlua_process_task(struct task *task, void *context, unsigned int state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6654 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6655 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6656 | enum hlua_exec status; |
| 6657 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6658 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6659 | task_set_affinity(task, tid_bit); |
| 6660 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6661 | /* If it is the first call to the task, we must initialize the |
| 6662 | * execution timeouts. |
| 6663 | */ |
| 6664 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6665 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6666 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6667 | /* Execute the Lua code. */ |
| 6668 | status = hlua_ctx_resume(hlua, 1); |
| 6669 | |
| 6670 | switch (status) { |
| 6671 | /* finished or yield */ |
| 6672 | case HLUA_E_OK: |
| 6673 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6674 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6675 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6676 | break; |
| 6677 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6678 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6679 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6680 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6681 | break; |
| 6682 | |
| 6683 | /* finished with error. */ |
| 6684 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6685 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6686 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6687 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6688 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6689 | break; |
| 6690 | |
| 6691 | case HLUA_E_ERR: |
| 6692 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6693 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6694 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6695 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6696 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6697 | break; |
| 6698 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6699 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6700 | } |
| 6701 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6702 | /* This function is an LUA binding that register LUA function to be |
| 6703 | * executed after the HAProxy configuration parsing and before the |
| 6704 | * HAProxy scheduler starts. This function expect only one LUA |
| 6705 | * argument that is a function. This function returns nothing, but |
| 6706 | * throws if an error is encountered. |
| 6707 | */ |
| 6708 | __LJMP static int hlua_register_init(lua_State *L) |
| 6709 | { |
| 6710 | struct hlua_init_function *init; |
| 6711 | int ref; |
| 6712 | |
| 6713 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6714 | |
| 6715 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6716 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6717 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6718 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6719 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6720 | |
| 6721 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6722 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6723 | return 0; |
| 6724 | } |
| 6725 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6726 | /* This functio is an LUA binding. It permits to register a task |
| 6727 | * executed in parallel of the main HAroxy activity. The task is |
| 6728 | * created and it is set in the HAProxy scheduler. It can be called |
| 6729 | * from the "init" section, "post init" or during the runtime. |
| 6730 | * |
| 6731 | * Lua prototype: |
| 6732 | * |
| 6733 | * <none> core.register_task(<function>) |
| 6734 | */ |
| 6735 | static int hlua_register_task(lua_State *L) |
| 6736 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6737 | struct hlua *hlua = NULL; |
| 6738 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6739 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6740 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6741 | |
| 6742 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6743 | |
| 6744 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6745 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6746 | /* Get the reference state. If the reference is NULL, L is the master |
| 6747 | * state, otherwise hlua->T is. |
| 6748 | */ |
| 6749 | hlua = hlua_gethlua(L); |
| 6750 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6751 | /* we are in runtime processing */ |
| 6752 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6753 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6754 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6755 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6756 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6757 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6758 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6759 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6760 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6761 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6762 | /* We are in the common lua state, execute the task anywhere, |
| 6763 | * otherwise, inherit the current thread identifier |
| 6764 | */ |
| 6765 | if (state_id == 0) |
| 6766 | task = task_new(MAX_THREADS_MASK); |
| 6767 | else |
| 6768 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6769 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6770 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6771 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6772 | task->context = hlua; |
| 6773 | task->process = hlua_process_task; |
| 6774 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6775 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6776 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6777 | |
| 6778 | /* Restore the function in the stack. */ |
| 6779 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6780 | hlua->nargs = 0; |
| 6781 | |
| 6782 | /* Schedule task. */ |
| 6783 | task_schedule(task, now_ms); |
| 6784 | |
| 6785 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6786 | |
| 6787 | alloc_error: |
| 6788 | task_destroy(task); |
| 6789 | hlua_ctx_destroy(hlua); |
| 6790 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6791 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6792 | } |
| 6793 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6794 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6795 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6796 | * resume converters. |
| 6797 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6798 | static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6799 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6800 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6801 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6802 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6803 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6804 | if (!stream) |
| 6805 | return 0; |
| 6806 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6807 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6808 | * Lua context can be not initialized. This behavior |
| 6809 | * permits to save performances because a systematic |
| 6810 | * Lua initialization cause 5% performances loss. |
| 6811 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6812 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6813 | struct hlua *hlua; |
| 6814 | |
| 6815 | hlua = pool_alloc(pool_head_hlua); |
| 6816 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6817 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6818 | return 0; |
| 6819 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6820 | HLUA_INIT(hlua); |
| 6821 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6822 | if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6823 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6824 | return 0; |
| 6825 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6826 | } |
| 6827 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6828 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6829 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6830 | |
| 6831 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6832 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6833 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6834 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6835 | else |
| 6836 | error = "critical error"; |
| 6837 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6838 | return 0; |
| 6839 | } |
| 6840 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6841 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6842 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6843 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6844 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6845 | return 0; |
| 6846 | } |
| 6847 | |
| 6848 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6849 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6850 | |
| 6851 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6852 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6853 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6854 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6855 | return 0; |
| 6856 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6857 | hlua_smp2lua(stream->hlua->T, smp); |
| 6858 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6859 | |
| 6860 | /* push keywords in the stack. */ |
| 6861 | if (arg_p) { |
| 6862 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6863 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6864 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6865 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6866 | return 0; |
| 6867 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6868 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6869 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6870 | } |
| 6871 | } |
| 6872 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6873 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6874 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6875 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6876 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6877 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6881 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6882 | /* finished. */ |
| 6883 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6884 | /* If the stack is empty, the function fails. */ |
| 6885 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6886 | return 0; |
| 6887 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6888 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6889 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6890 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6891 | return 1; |
| 6892 | |
| 6893 | /* yield. */ |
| 6894 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6895 | SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6896 | return 0; |
| 6897 | |
| 6898 | /* finished with error. */ |
| 6899 | case HLUA_E_ERRMSG: |
| 6900 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6901 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6902 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6903 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6904 | return 0; |
| 6905 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6906 | case HLUA_E_ETMOUT: |
| 6907 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6908 | return 0; |
| 6909 | |
| 6910 | case HLUA_E_NOMEM: |
| 6911 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6912 | return 0; |
| 6913 | |
| 6914 | case HLUA_E_YIELD: |
| 6915 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6916 | return 0; |
| 6917 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6918 | case HLUA_E_ERR: |
| 6919 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6920 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6921 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6922 | |
| 6923 | default: |
| 6924 | return 0; |
| 6925 | } |
| 6926 | } |
| 6927 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6928 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6929 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6930 | * resume sample-fetches. This function will be called by the sample |
| 6931 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6932 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6933 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6934 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6935 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6936 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6937 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6938 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6939 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6940 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6941 | if (!stream) |
| 6942 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6943 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6944 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6945 | * Lua context can be not initialized. This behavior |
| 6946 | * permits to save performances because a systematic |
| 6947 | * Lua initialization cause 5% performances loss. |
| 6948 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6949 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6950 | struct hlua *hlua; |
| 6951 | |
| 6952 | hlua = pool_alloc(pool_head_hlua); |
| 6953 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6954 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6955 | return 0; |
| 6956 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6957 | hlua->T = NULL; |
| 6958 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6959 | if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6960 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6961 | return 0; |
| 6962 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6963 | } |
| 6964 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6965 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6966 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6967 | |
| 6968 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6969 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6971 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6972 | else |
| 6973 | error = "critical error"; |
| 6974 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6975 | return 0; |
| 6976 | } |
| 6977 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6978 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6979 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6980 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6981 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6982 | return 0; |
| 6983 | } |
| 6984 | |
| 6985 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6986 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6987 | |
| 6988 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6989 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6990 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6991 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6992 | return 0; |
| 6993 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6994 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6995 | |
| 6996 | /* push keywords in the stack. */ |
| 6997 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6998 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6999 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7000 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7001 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7002 | return 0; |
| 7003 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7004 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 7005 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7006 | } |
| 7007 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7008 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7009 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7010 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7011 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7012 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7013 | } |
| 7014 | |
| 7015 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7016 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7017 | /* finished. */ |
| 7018 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 7019 | /* If the stack is empty, the function fails. */ |
| 7020 | if (lua_gettop(stream->hlua->T) <= 0) |
| 7021 | return 0; |
| 7022 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7023 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7024 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 7025 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7026 | |
| 7027 | /* Set the end of execution flag. */ |
| 7028 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 7029 | return 1; |
| 7030 | |
| 7031 | /* yield. */ |
| 7032 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7033 | SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7034 | return 0; |
| 7035 | |
| 7036 | /* finished with error. */ |
| 7037 | case HLUA_E_ERRMSG: |
| 7038 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7039 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7040 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 7041 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7042 | return 0; |
| 7043 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7044 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7045 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 7046 | return 0; |
| 7047 | |
| 7048 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7049 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 7050 | return 0; |
| 7051 | |
| 7052 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7053 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 7054 | return 0; |
| 7055 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7056 | case HLUA_E_ERR: |
| 7057 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7058 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 7059 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7060 | |
| 7061 | default: |
| 7062 | return 0; |
| 7063 | } |
| 7064 | } |
| 7065 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7066 | /* This function is an LUA binding used for registering |
| 7067 | * "sample-conv" functions. It expects a converter name used |
| 7068 | * in the haproxy configuration file, and an LUA function. |
| 7069 | */ |
| 7070 | __LJMP static int hlua_register_converters(lua_State *L) |
| 7071 | { |
| 7072 | struct sample_conv_kw_list *sck; |
| 7073 | const char *name; |
| 7074 | int ref; |
| 7075 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7076 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7077 | struct sample_conv *sc; |
| 7078 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7079 | |
| 7080 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 7081 | |
| 7082 | /* First argument : converter name. */ |
| 7083 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7084 | |
| 7085 | /* Second argument : lua function. */ |
| 7086 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 7087 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7088 | /* Check if the converter is already registered */ |
| 7089 | trash = get_trash_chunk(); |
| 7090 | chunk_printf(trash, "lua.%s", name); |
| 7091 | sc = find_sample_conv(trash->area, trash->data); |
| 7092 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7093 | fcn = sc->private; |
| 7094 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7095 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 7096 | "This will become a hard error in version 2.5.\n", name); |
| 7097 | } |
| 7098 | fcn->function_ref[hlua_state_id] = ref; |
| 7099 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7100 | } |
| 7101 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7102 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7103 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7104 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7105 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7106 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7107 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7108 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7109 | |
| 7110 | /* Fill fcn. */ |
| 7111 | fcn->name = strdup(name); |
| 7112 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7113 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7114 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7115 | |
| 7116 | /* List head */ |
| 7117 | sck->list.n = sck->list.p = NULL; |
| 7118 | |
| 7119 | /* converter keyword. */ |
| 7120 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7121 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7122 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7123 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7124 | |
| 7125 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 7126 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7127 | sck->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7128 | sck->kw[0].val_args = NULL; |
| 7129 | sck->kw[0].in_type = SMP_T_STR; |
| 7130 | sck->kw[0].out_type = SMP_T_STR; |
| 7131 | sck->kw[0].private = fcn; |
| 7132 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7133 | /* Register this new converter */ |
| 7134 | sample_register_convs(sck); |
| 7135 | |
| 7136 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7137 | |
| 7138 | alloc_error: |
| 7139 | release_hlua_function(fcn); |
| 7140 | ha_free(&sck); |
| 7141 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7142 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7143 | } |
| 7144 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7145 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7146 | * "sample-fetch" functions. It expects a converter name used |
| 7147 | * in the haproxy configuration file, and an LUA function. |
| 7148 | */ |
| 7149 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 7150 | { |
| 7151 | const char *name; |
| 7152 | int ref; |
| 7153 | int len; |
| 7154 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7155 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7156 | struct sample_fetch *sf; |
| 7157 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7158 | |
| 7159 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 7160 | |
| 7161 | /* First argument : sample-fetch name. */ |
| 7162 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7163 | |
| 7164 | /* Second argument : lua function. */ |
| 7165 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 7166 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7167 | /* Check if the sample-fetch is already registered */ |
| 7168 | trash = get_trash_chunk(); |
| 7169 | chunk_printf(trash, "lua.%s", name); |
| 7170 | sf = find_sample_fetch(trash->area, trash->data); |
| 7171 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7172 | fcn = sf->private; |
| 7173 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7174 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 7175 | "This will become a hard error in version 2.5.\n", name); |
| 7176 | } |
| 7177 | fcn->function_ref[hlua_state_id] = ref; |
| 7178 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7179 | } |
| 7180 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7181 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7182 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7183 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7184 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7185 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7186 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7187 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7188 | |
| 7189 | /* Fill fcn. */ |
| 7190 | fcn->name = strdup(name); |
| 7191 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7192 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7193 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7194 | |
| 7195 | /* List head */ |
| 7196 | sfk->list.n = sfk->list.p = NULL; |
| 7197 | |
| 7198 | /* sample-fetch keyword. */ |
| 7199 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7200 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7201 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7202 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7203 | |
| 7204 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 7205 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7206 | sfk->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7207 | sfk->kw[0].val_args = NULL; |
| 7208 | sfk->kw[0].out_type = SMP_T_STR; |
| 7209 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 7210 | sfk->kw[0].val = 0; |
| 7211 | sfk->kw[0].private = fcn; |
| 7212 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7213 | /* Register this new fetch. */ |
| 7214 | sample_register_fetches(sfk); |
| 7215 | |
| 7216 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7217 | |
| 7218 | alloc_error: |
| 7219 | release_hlua_function(fcn); |
| 7220 | ha_free(&sfk); |
| 7221 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7222 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7223 | } |
| 7224 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7225 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7226 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7227 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7228 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7229 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7230 | unsigned int delay; |
| 7231 | unsigned int wakeup_ms; |
| 7232 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7233 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7234 | hlua = hlua_gethlua(L); |
| 7235 | if (!hlua) { |
| 7236 | return 0; |
| 7237 | } |
| 7238 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7239 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7240 | |
| 7241 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7242 | wakeup_ms = tick_add(now_ms, delay); |
| 7243 | hlua->wake_time = wakeup_ms; |
| 7244 | return 0; |
| 7245 | } |
| 7246 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7247 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7248 | * wrapper during the initialisation period. This function may return any |
| 7249 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7250 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7251 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7252 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7253 | static enum act_return hlua_action(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 7254 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7255 | { |
| 7256 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7257 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7258 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7259 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7260 | |
| 7261 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7262 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7263 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7264 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7265 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7266 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7267 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7268 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7269 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7270 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7271 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7272 | * Lua context can be not initialized. This behavior |
| 7273 | * permits to save performances because a systematic |
| 7274 | * Lua initialization cause 5% performances loss. |
| 7275 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7276 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 7277 | struct hlua *hlua; |
| 7278 | |
| 7279 | hlua = pool_alloc(pool_head_hlua); |
| 7280 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7281 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7282 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7283 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7284 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 7285 | HLUA_INIT(hlua); |
| 7286 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7287 | if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7288 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7289 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7290 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7291 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7292 | } |
| 7293 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7294 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7295 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7296 | |
| 7297 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7298 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7299 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 7300 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7301 | else |
| 7302 | error = "critical error"; |
| 7303 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7304 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7305 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7306 | } |
| 7307 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7308 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7309 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7310 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7311 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7312 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7313 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7314 | } |
| 7315 | |
| 7316 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7317 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7318 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7319 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7320 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7321 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7322 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7323 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7324 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7325 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7326 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7327 | |
| 7328 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7329 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7330 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7331 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7332 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7333 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7334 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7335 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7336 | lua_pushstring(s->hlua->T, *arg); |
| 7337 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7338 | } |
| 7339 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7340 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7341 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7342 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7343 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7344 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7348 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7349 | /* finished. */ |
| 7350 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7351 | /* Catch the return value */ |
| 7352 | if (lua_gettop(s->hlua->T) > 0) |
| 7353 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7354 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7355 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7356 | if (act_ret == ACT_RET_YIELD) { |
| 7357 | if (flags & ACT_OPT_FINAL) |
| 7358 | goto err_yield; |
| 7359 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7360 | if (dir == SMP_OPT_DIR_REQ) |
| 7361 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7362 | s->hlua->wake_time); |
| 7363 | else |
| 7364 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7365 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7366 | } |
| 7367 | goto end; |
| 7368 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7369 | /* yield. */ |
| 7370 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7371 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7372 | if (dir == SMP_OPT_DIR_REQ) |
| 7373 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7374 | s->hlua->wake_time); |
| 7375 | else |
| 7376 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7377 | s->hlua->wake_time); |
| 7378 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7379 | /* Some actions can be wake up when a "write" event |
| 7380 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7381 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7382 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7383 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7384 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7385 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7386 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7387 | act_ret = ACT_RET_YIELD; |
| 7388 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7389 | |
| 7390 | /* finished with error. */ |
| 7391 | case HLUA_E_ERRMSG: |
| 7392 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7393 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7394 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7395 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7396 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7397 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7398 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7399 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7400 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7401 | |
| 7402 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7403 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7404 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7405 | |
| 7406 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7407 | err_yield: |
| 7408 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7409 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7410 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7411 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7412 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7413 | case HLUA_E_ERR: |
| 7414 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7415 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7416 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7417 | |
| 7418 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7419 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7420 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7421 | |
| 7422 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7423 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7424 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7425 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7426 | } |
| 7427 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7428 | struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7429 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7430 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7431 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7432 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7433 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7434 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7435 | } |
| 7436 | |
| 7437 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7438 | { |
| 7439 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7440 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7441 | struct task *task; |
| 7442 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7443 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7444 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7445 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7446 | if (!hlua) { |
| 7447 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7448 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7449 | return 0; |
| 7450 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7451 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7452 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7453 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7454 | |
| 7455 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7456 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7457 | if (!task) { |
| 7458 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7459 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7460 | return 0; |
| 7461 | } |
| 7462 | task->nice = 0; |
| 7463 | task->context = ctx; |
| 7464 | task->process = hlua_applet_wakeup; |
| 7465 | ctx->ctx.hlua_apptcp.task = task; |
| 7466 | |
| 7467 | /* In the execution wrappers linked with a stream, the |
| 7468 | * Lua context can be not initialized. This behavior |
| 7469 | * permits to save performances because a systematic |
| 7470 | * Lua initialization cause 5% performances loss. |
| 7471 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7472 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7473 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7474 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7475 | return 0; |
| 7476 | } |
| 7477 | |
| 7478 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7479 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7480 | |
| 7481 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7482 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7483 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7484 | error = lua_tostring(hlua->T, -1); |
| 7485 | else |
| 7486 | error = "critical error"; |
| 7487 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7488 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7489 | return 0; |
| 7490 | } |
| 7491 | |
| 7492 | /* Check stack available size. */ |
| 7493 | if (!lua_checkstack(hlua->T, 1)) { |
| 7494 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7495 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7496 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7497 | return 0; |
| 7498 | } |
| 7499 | |
| 7500 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7501 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7502 | |
| 7503 | /* Create and and push object stream in the stack. */ |
| 7504 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7505 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7506 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7507 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7508 | return 0; |
| 7509 | } |
| 7510 | hlua->nargs = 1; |
| 7511 | |
| 7512 | /* push keywords in the stack. */ |
| 7513 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7514 | if (!lua_checkstack(hlua->T, 1)) { |
| 7515 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7516 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7517 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7518 | return 0; |
| 7519 | } |
| 7520 | lua_pushstring(hlua->T, *arg); |
| 7521 | hlua->nargs++; |
| 7522 | } |
| 7523 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7524 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7525 | |
| 7526 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7527 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7528 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7529 | |
| 7530 | return 1; |
| 7531 | } |
| 7532 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7533 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7534 | { |
| 7535 | struct stream_interface *si = ctx->owner; |
| 7536 | struct stream *strm = si_strm(si); |
| 7537 | struct channel *res = si_ic(si); |
| 7538 | struct act_rule *rule = ctx->rule; |
| 7539 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7540 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7541 | |
| 7542 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7543 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7544 | /* eat the whole request */ |
| 7545 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7546 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7547 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7548 | |
| 7549 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7550 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7551 | return; |
| 7552 | |
| 7553 | /* Execute the function. */ |
| 7554 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7555 | /* finished. */ |
| 7556 | case HLUA_E_OK: |
| 7557 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7558 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7559 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7560 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7561 | res->flags |= CF_READ_NULL; |
| 7562 | si_shutr(si); |
| 7563 | return; |
| 7564 | |
| 7565 | /* yield. */ |
| 7566 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7567 | if (hlua->wake_time != TICK_ETERNITY) |
| 7568 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7569 | return; |
| 7570 | |
| 7571 | /* finished with error. */ |
| 7572 | case HLUA_E_ERRMSG: |
| 7573 | /* Display log. */ |
| 7574 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7575 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7576 | lua_pop(hlua->T, 1); |
| 7577 | goto error; |
| 7578 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7579 | case HLUA_E_ETMOUT: |
| 7580 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7581 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7582 | goto error; |
| 7583 | |
| 7584 | case HLUA_E_NOMEM: |
| 7585 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7586 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7587 | goto error; |
| 7588 | |
| 7589 | case HLUA_E_YIELD: /* unexpected */ |
| 7590 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7591 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7592 | goto error; |
| 7593 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7594 | case HLUA_E_ERR: |
| 7595 | /* Display log. */ |
| 7596 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7597 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7598 | goto error; |
| 7599 | |
| 7600 | default: |
| 7601 | goto error; |
| 7602 | } |
| 7603 | |
| 7604 | error: |
| 7605 | |
| 7606 | /* For all other cases, just close the stream. */ |
| 7607 | si_shutw(si); |
| 7608 | si_shutr(si); |
| 7609 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7610 | } |
| 7611 | |
| 7612 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7613 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7614 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7615 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7616 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7617 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7618 | } |
| 7619 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7620 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7621 | * an errors occurs and -1 if more data are required for initializing |
| 7622 | * the applet. |
| 7623 | */ |
| 7624 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7625 | { |
| 7626 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7627 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7628 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7629 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7630 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7631 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7632 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7633 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7634 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7635 | if (!hlua) { |
| 7636 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7637 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7638 | return 0; |
| 7639 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7640 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7641 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7642 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7643 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7644 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7645 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7646 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7647 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7648 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7649 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7650 | if (!task) { |
| 7651 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7652 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7653 | return 0; |
| 7654 | } |
| 7655 | task->nice = 0; |
| 7656 | task->context = ctx; |
| 7657 | task->process = hlua_applet_wakeup; |
| 7658 | ctx->ctx.hlua_apphttp.task = task; |
| 7659 | |
| 7660 | /* In the execution wrappers linked with a stream, the |
| 7661 | * Lua context can be not initialized. This behavior |
| 7662 | * permits to save performances because a systematic |
| 7663 | * Lua initialization cause 5% performances loss. |
| 7664 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7665 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7666 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7667 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7668 | return 0; |
| 7669 | } |
| 7670 | |
| 7671 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7672 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7673 | |
| 7674 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7675 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7676 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7677 | error = lua_tostring(hlua->T, -1); |
| 7678 | else |
| 7679 | error = "critical error"; |
| 7680 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7681 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7682 | return 0; |
| 7683 | } |
| 7684 | |
| 7685 | /* Check stack available size. */ |
| 7686 | if (!lua_checkstack(hlua->T, 1)) { |
| 7687 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7688 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7689 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7690 | return 0; |
| 7691 | } |
| 7692 | |
| 7693 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7694 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7695 | |
| 7696 | /* Create and and push object stream in the stack. */ |
| 7697 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7698 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7699 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7700 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7701 | return 0; |
| 7702 | } |
| 7703 | hlua->nargs = 1; |
| 7704 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7705 | /* push keywords in the stack. */ |
| 7706 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7707 | if (!lua_checkstack(hlua->T, 1)) { |
| 7708 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7709 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7710 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7711 | return 0; |
| 7712 | } |
| 7713 | lua_pushstring(hlua->T, *arg); |
| 7714 | hlua->nargs++; |
| 7715 | } |
| 7716 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7717 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7718 | |
| 7719 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7720 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7721 | |
| 7722 | return 1; |
| 7723 | } |
| 7724 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7725 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7726 | { |
| 7727 | struct stream_interface *si = ctx->owner; |
| 7728 | struct stream *strm = si_strm(si); |
| 7729 | struct channel *req = si_oc(si); |
| 7730 | struct channel *res = si_ic(si); |
| 7731 | struct act_rule *rule = ctx->rule; |
| 7732 | struct proxy *px = strm->be; |
| 7733 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7734 | struct htx *req_htx, *res_htx; |
| 7735 | |
| 7736 | res_htx = htx_from_buf(&res->buf); |
| 7737 | |
| 7738 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7739 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7740 | goto out; |
| 7741 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7742 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7743 | if (!b_size(&res->buf)) { |
| 7744 | si_rx_room_blk(si); |
| 7745 | goto out; |
| 7746 | } |
| 7747 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7748 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7749 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7750 | |
| 7751 | /* Set the currently running flag. */ |
| 7752 | if (!HLUA_IS_RUNNING(hlua) && |
| 7753 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7754 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7755 | si_cant_get(si); |
| 7756 | goto out; |
| 7757 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | /* Executes The applet if it is not done. */ |
| 7761 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7762 | |
| 7763 | /* Execute the function. */ |
| 7764 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7765 | /* finished. */ |
| 7766 | case HLUA_E_OK: |
| 7767 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7768 | break; |
| 7769 | |
| 7770 | /* yield. */ |
| 7771 | case HLUA_E_AGAIN: |
| 7772 | if (hlua->wake_time != TICK_ETERNITY) |
| 7773 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7774 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7775 | |
| 7776 | /* finished with error. */ |
| 7777 | case HLUA_E_ERRMSG: |
| 7778 | /* Display log. */ |
| 7779 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7780 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7781 | lua_pop(hlua->T, 1); |
| 7782 | goto error; |
| 7783 | |
| 7784 | case HLUA_E_ETMOUT: |
| 7785 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7786 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7787 | goto error; |
| 7788 | |
| 7789 | case HLUA_E_NOMEM: |
| 7790 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7791 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7792 | goto error; |
| 7793 | |
| 7794 | case HLUA_E_YIELD: /* unexpected */ |
| 7795 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7796 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7797 | goto error; |
| 7798 | |
| 7799 | case HLUA_E_ERR: |
| 7800 | /* Display log. */ |
| 7801 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7802 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7803 | goto error; |
| 7804 | |
| 7805 | default: |
| 7806 | goto error; |
| 7807 | } |
| 7808 | } |
| 7809 | |
| 7810 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7811 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7812 | goto done; |
| 7813 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7814 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7815 | goto error; |
| 7816 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7817 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7818 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7819 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7820 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7821 | } |
| 7822 | |
| 7823 | done: |
| 7824 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7825 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7826 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7827 | si_shutr(si); |
| 7828 | } |
| 7829 | |
| 7830 | /* eat the whole request */ |
| 7831 | if (co_data(req)) { |
| 7832 | req_htx = htx_from_buf(&req->buf); |
| 7833 | co_htx_skip(req, req_htx, co_data(req)); |
| 7834 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7835 | } |
| 7836 | } |
| 7837 | |
| 7838 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7839 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7840 | return; |
| 7841 | |
| 7842 | error: |
| 7843 | |
| 7844 | /* If we are in HTTP mode, and we are not send any |
| 7845 | * data, return a 500 server error in best effort: |
| 7846 | * if there is no room available in the buffer, |
| 7847 | * just close the connection. |
| 7848 | */ |
| 7849 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7850 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7851 | |
| 7852 | channel_erase(res); |
| 7853 | res->buf.data = b_data(err); |
| 7854 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7855 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7856 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7857 | } |
| 7858 | if (!(strm->flags & SF_ERR_MASK)) |
| 7859 | strm->flags |= SF_ERR_RESOURCE; |
| 7860 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7861 | goto done; |
| 7862 | } |
| 7863 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7864 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7865 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7866 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7867 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7868 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7869 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7870 | } |
| 7871 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7872 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7873 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7874 | * |
| 7875 | * This function can fail with an abort() due to an Lua critical error. |
| 7876 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7877 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7878 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7879 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7880 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7881 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7882 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7883 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7884 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7885 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7886 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7887 | if (!rule->arg.hlua_rule) { |
| 7888 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7889 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7890 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7891 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7892 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7893 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7894 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7895 | if (!rule->arg.hlua_rule->args) { |
| 7896 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7897 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7898 | } |
| 7899 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7900 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7901 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7902 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7903 | /* Expect some arguments */ |
| 7904 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7905 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7906 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7907 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7908 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7909 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7910 | if (!rule->arg.hlua_rule->args[i]) { |
| 7911 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7912 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7913 | } |
| 7914 | (*cur_arg)++; |
| 7915 | } |
| 7916 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7917 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7918 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7919 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7920 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7921 | |
| 7922 | error: |
| 7923 | if (rule->arg.hlua_rule) { |
| 7924 | if (rule->arg.hlua_rule->args) { |
| 7925 | for (i = 0; i < fcn->nargs; i++) |
| 7926 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7927 | ha_free(&rule->arg.hlua_rule->args); |
| 7928 | } |
| 7929 | ha_free(&rule->arg.hlua_rule); |
| 7930 | } |
| 7931 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7932 | } |
| 7933 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7934 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7935 | struct act_rule *rule, char **err) |
| 7936 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7937 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7938 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7939 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7940 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7941 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7942 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7943 | * the call of this analyzer. |
| 7944 | */ |
| 7945 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7946 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7947 | return ACT_RET_PRS_ERR; |
| 7948 | } |
| 7949 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7950 | /* Memory for the rule. */ |
| 7951 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7952 | if (!rule->arg.hlua_rule) { |
| 7953 | memprintf(err, "out of memory error"); |
| 7954 | return ACT_RET_PRS_ERR; |
| 7955 | } |
| 7956 | |
| 7957 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7958 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7959 | |
| 7960 | /* TODO: later accept arguments. */ |
| 7961 | rule->arg.hlua_rule->args = NULL; |
| 7962 | |
| 7963 | /* Add applet pointer in the rule. */ |
| 7964 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7965 | rule->applet.name = fcn->name; |
| 7966 | rule->applet.init = hlua_applet_http_init; |
| 7967 | rule->applet.fct = hlua_applet_http_fct; |
| 7968 | rule->applet.release = hlua_applet_http_release; |
| 7969 | rule->applet.timeout = hlua_timeout_applet; |
| 7970 | |
| 7971 | return ACT_RET_PRS_OK; |
| 7972 | } |
| 7973 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7974 | /* This function is an LUA binding used for registering |
| 7975 | * "sample-conv" functions. It expects a converter name used |
| 7976 | * in the haproxy configuration file, and an LUA function. |
| 7977 | */ |
| 7978 | __LJMP static int hlua_register_action(lua_State *L) |
| 7979 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7980 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7981 | const char *name; |
| 7982 | int ref; |
| 7983 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7984 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7985 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7986 | struct buffer *trash; |
| 7987 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7988 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7989 | /* Initialise the number of expected arguments at 0. */ |
| 7990 | nargs = 0; |
| 7991 | |
| 7992 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7993 | WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7994 | |
| 7995 | /* First argument : converter name. */ |
| 7996 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7997 | |
| 7998 | /* Second argument : environment. */ |
| 7999 | if (lua_type(L, 2) != LUA_TTABLE) |
| 8000 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 8001 | |
| 8002 | /* Third argument : lua function. */ |
| 8003 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8004 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8005 | /* Fourth argument : number of mandatory arguments expected on the configuration line. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 8006 | if (lua_gettop(L) >= 4) |
| 8007 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 8008 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8009 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8010 | lua_pushnil(L); |
| 8011 | while (lua_next(L, 2) != 0) { |
| 8012 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8013 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 8014 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8015 | /* Check if action exists */ |
| 8016 | trash = get_trash_chunk(); |
| 8017 | chunk_printf(trash, "lua.%s", name); |
| 8018 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 8019 | akw = tcp_req_cont_action(trash->area); |
| 8020 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 8021 | akw = tcp_res_cont_action(trash->area); |
| 8022 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 8023 | akw = action_http_req_custom(trash->area); |
| 8024 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 8025 | akw = action_http_res_custom(trash->area); |
| 8026 | } else { |
| 8027 | akw = NULL; |
| 8028 | } |
| 8029 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8030 | fcn = akw->private; |
| 8031 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8032 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 8033 | "This will become a hard error in version 2.5.\n", name); |
| 8034 | } |
| 8035 | fcn->function_ref[hlua_state_id] = ref; |
| 8036 | |
| 8037 | /* pop the environment string. */ |
| 8038 | lua_pop(L, 1); |
| 8039 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8040 | } |
| 8041 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8042 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 8043 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8044 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8045 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8046 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8047 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8048 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8049 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8050 | |
| 8051 | /* Fill fcn. */ |
| 8052 | fcn->name = strdup(name); |
| 8053 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8054 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8055 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8056 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8057 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 8058 | fcn->nargs = nargs; |
| 8059 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8060 | /* List head */ |
| 8061 | akl->list.n = akl->list.p = NULL; |
| 8062 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8063 | /* action keyword. */ |
| 8064 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8065 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8066 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8067 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8068 | |
| 8069 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8070 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8071 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8072 | akl->kw[0].private = fcn; |
| 8073 | akl->kw[0].parse = action_register_lua; |
| 8074 | |
| 8075 | /* select the action registering point. */ |
| 8076 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 8077 | tcp_req_cont_keywords_register(akl); |
| 8078 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 8079 | tcp_res_cont_keywords_register(akl); |
| 8080 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 8081 | http_req_keywords_register(akl); |
| 8082 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 8083 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8084 | else { |
| 8085 | release_hlua_function(fcn); |
| 8086 | if (akl) |
| 8087 | ha_free((char **)&(akl->kw[0].kw)); |
| 8088 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8089 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8090 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 8091 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8092 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8093 | |
| 8094 | /* pop the environment string. */ |
| 8095 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8096 | |
| 8097 | /* reset for next loop */ |
| 8098 | akl = NULL; |
| 8099 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8100 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8101 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8102 | |
| 8103 | alloc_error: |
| 8104 | release_hlua_function(fcn); |
| 8105 | ha_free(&akl); |
| 8106 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8107 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8108 | } |
| 8109 | |
| 8110 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 8111 | struct act_rule *rule, char **err) |
| 8112 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8113 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8114 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 8115 | if (px->mode == PR_MODE_HTTP) { |
| 8116 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8117 | return ACT_RET_PRS_ERR; |
| 8118 | } |
| 8119 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8120 | /* Memory for the rule. */ |
| 8121 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 8122 | if (!rule->arg.hlua_rule) { |
| 8123 | memprintf(err, "out of memory error"); |
| 8124 | return ACT_RET_PRS_ERR; |
| 8125 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8126 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8127 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8128 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8129 | |
| 8130 | /* TODO: later accept arguments. */ |
| 8131 | rule->arg.hlua_rule->args = NULL; |
| 8132 | |
| 8133 | /* Add applet pointer in the rule. */ |
| 8134 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 8135 | rule->applet.name = fcn->name; |
| 8136 | rule->applet.init = hlua_applet_tcp_init; |
| 8137 | rule->applet.fct = hlua_applet_tcp_fct; |
| 8138 | rule->applet.release = hlua_applet_tcp_release; |
| 8139 | rule->applet.timeout = hlua_timeout_applet; |
| 8140 | |
| 8141 | return 0; |
| 8142 | } |
| 8143 | |
| 8144 | /* This function is an LUA binding used for registering |
| 8145 | * "sample-conv" functions. It expects a converter name used |
| 8146 | * in the haproxy configuration file, and an LUA function. |
| 8147 | */ |
| 8148 | __LJMP static int hlua_register_service(lua_State *L) |
| 8149 | { |
| 8150 | struct action_kw_list *akl; |
| 8151 | const char *name; |
| 8152 | const char *env; |
| 8153 | int ref; |
| 8154 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8155 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8156 | struct buffer *trash; |
| 8157 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8158 | |
| 8159 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 8160 | |
| 8161 | /* First argument : converter name. */ |
| 8162 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8163 | |
| 8164 | /* Second argument : environment. */ |
| 8165 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8166 | |
| 8167 | /* Third argument : lua function. */ |
| 8168 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8169 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8170 | /* Check for service already registered */ |
| 8171 | trash = get_trash_chunk(); |
| 8172 | chunk_printf(trash, "lua.%s", name); |
| 8173 | akw = service_find(trash->area); |
| 8174 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8175 | fcn = akw->private; |
| 8176 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8177 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 8178 | "This will become a hard error in version 2.5.\n", name); |
| 8179 | } |
| 8180 | fcn->function_ref[hlua_state_id] = ref; |
| 8181 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8182 | } |
| 8183 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8184 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8185 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 8186 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8187 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8188 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8189 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8190 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8191 | |
| 8192 | /* Fill fcn. */ |
| 8193 | len = strlen("<lua.>") + strlen(name) + 1; |
| 8194 | fcn->name = calloc(1, len); |
| 8195 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8196 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8197 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8198 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8199 | |
| 8200 | /* List head */ |
| 8201 | akl->list.n = akl->list.p = NULL; |
| 8202 | |
| 8203 | /* converter keyword. */ |
| 8204 | len = strlen("lua.") + strlen(name) + 1; |
| 8205 | akl->kw[0].kw = calloc(1, len); |
| 8206 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8207 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8208 | |
| 8209 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8210 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 8211 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8212 | if (strcmp(env, "tcp") == 0) |
| 8213 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8214 | else if (strcmp(env, "http") == 0) |
| 8215 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8216 | else { |
| 8217 | release_hlua_function(fcn); |
| 8218 | if (akl) |
| 8219 | ha_free((char **)&(akl->kw[0].kw)); |
| 8220 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8221 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 8222 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8223 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8224 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8225 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8226 | akl->kw[0].private = fcn; |
| 8227 | |
| 8228 | /* End of array. */ |
| 8229 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8230 | |
| 8231 | /* Register this new converter */ |
| 8232 | service_keywords_register(akl); |
| 8233 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8234 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8235 | |
| 8236 | alloc_error: |
| 8237 | release_hlua_function(fcn); |
| 8238 | ha_free(&akl); |
| 8239 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8240 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8241 | } |
| 8242 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8243 | /* This function initialises Lua cli handler. It copies the |
| 8244 | * arguments in the Lua stack and create channel IO objects. |
| 8245 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8246 | static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private) |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8247 | { |
| 8248 | struct hlua *hlua; |
| 8249 | struct hlua_function *fcn; |
| 8250 | int i; |
| 8251 | const char *error; |
| 8252 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8253 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8254 | appctx->ctx.hlua_cli.fcn = private; |
| 8255 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8256 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8257 | if (!hlua) { |
| 8258 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8259 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8260 | } |
| 8261 | HLUA_INIT(hlua); |
| 8262 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8263 | |
| 8264 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8265 | * We use the same wakeup function than the Lua applet_tcp and |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8266 | * applet_http. It is absolutely compatible. |
| 8267 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8268 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8269 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8270 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8271 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8272 | } |
| 8273 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8274 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8275 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8276 | |
| 8277 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8278 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8279 | SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8280 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8281 | } |
| 8282 | |
| 8283 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8284 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8285 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8286 | error = lua_tostring(hlua->T, -1); |
| 8287 | else |
| 8288 | error = "critical error"; |
| 8289 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 8290 | goto error; |
| 8291 | } |
| 8292 | |
| 8293 | /* Check stack available size. */ |
| 8294 | if (!lua_checkstack(hlua->T, 2)) { |
| 8295 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8296 | goto error; |
| 8297 | } |
| 8298 | |
| 8299 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8300 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8301 | |
| 8302 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8303 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8304 | */ |
| 8305 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8306 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8307 | goto error; |
| 8308 | } |
| 8309 | hlua->nargs = 1; |
| 8310 | |
| 8311 | /* push keywords in the stack. */ |
| 8312 | for (i = 0; *args[i]; i++) { |
| 8313 | /* Check stack available size. */ |
| 8314 | if (!lua_checkstack(hlua->T, 1)) { |
| 8315 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8316 | goto error; |
| 8317 | } |
| 8318 | lua_pushstring(hlua->T, args[i]); |
| 8319 | hlua->nargs++; |
| 8320 | } |
| 8321 | |
| 8322 | /* We must initialize the execution timeouts. */ |
| 8323 | hlua->max_time = hlua_timeout_session; |
| 8324 | |
| 8325 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8326 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8327 | |
| 8328 | /* It's ok */ |
| 8329 | return 0; |
| 8330 | |
| 8331 | /* It's not ok. */ |
| 8332 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8333 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8334 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8335 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8336 | return 1; |
| 8337 | } |
| 8338 | |
| 8339 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8340 | { |
| 8341 | struct hlua *hlua; |
| 8342 | struct stream_interface *si; |
| 8343 | struct hlua_function *fcn; |
| 8344 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8345 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8346 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8347 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8348 | |
| 8349 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8350 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8351 | return 1; |
| 8352 | |
| 8353 | /* Execute the function. */ |
| 8354 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8355 | |
| 8356 | /* finished. */ |
| 8357 | case HLUA_E_OK: |
| 8358 | return 1; |
| 8359 | |
| 8360 | /* yield. */ |
| 8361 | case HLUA_E_AGAIN: |
| 8362 | /* We want write. */ |
| 8363 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8364 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8365 | /* Set the timeout. */ |
| 8366 | if (hlua->wake_time != TICK_ETERNITY) |
| 8367 | task_schedule(hlua->task, hlua->wake_time); |
| 8368 | return 0; |
| 8369 | |
| 8370 | /* finished with error. */ |
| 8371 | case HLUA_E_ERRMSG: |
| 8372 | /* Display log. */ |
| 8373 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8374 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8375 | lua_pop(hlua->T, 1); |
| 8376 | return 1; |
| 8377 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8378 | case HLUA_E_ETMOUT: |
| 8379 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8380 | fcn->name); |
| 8381 | return 1; |
| 8382 | |
| 8383 | case HLUA_E_NOMEM: |
| 8384 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8385 | fcn->name); |
| 8386 | return 1; |
| 8387 | |
| 8388 | case HLUA_E_YIELD: /* unexpected */ |
| 8389 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8390 | fcn->name); |
| 8391 | return 1; |
| 8392 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8393 | case HLUA_E_ERR: |
| 8394 | /* Display log. */ |
| 8395 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8396 | fcn->name); |
| 8397 | return 1; |
| 8398 | |
| 8399 | default: |
| 8400 | return 1; |
| 8401 | } |
| 8402 | |
| 8403 | return 1; |
| 8404 | } |
| 8405 | |
| 8406 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8407 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8408 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8409 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8410 | } |
| 8411 | |
| 8412 | /* This function is an LUA binding used for registering |
| 8413 | * new keywords in the cli. It expects a list of keywords |
| 8414 | * which are the "path". It is limited to 5 keywords. A |
| 8415 | * description of the command, a function to be executed |
| 8416 | * for the parsing and a function for io handlers. |
| 8417 | */ |
| 8418 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8419 | { |
| 8420 | struct cli_kw_list *cli_kws; |
| 8421 | const char *message; |
| 8422 | int ref_io; |
| 8423 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8424 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8425 | int index; |
| 8426 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8427 | struct buffer *trash; |
| 8428 | const char *kw[5]; |
| 8429 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8430 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8431 | |
| 8432 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8433 | |
| 8434 | /* First argument : an array of maximum 5 keywords. */ |
| 8435 | if (!lua_istable(L, 1)) |
| 8436 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8437 | |
| 8438 | /* Second argument : string with contextual message. */ |
| 8439 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8440 | |
| 8441 | /* Third and fourth argument : lua function. */ |
| 8442 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8443 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8444 | /* Check for CLI service already registered */ |
| 8445 | trash = get_trash_chunk(); |
| 8446 | index = 0; |
| 8447 | lua_pushnil(L); |
| 8448 | memset(kw, 0, sizeof(kw)); |
| 8449 | while (lua_next(L, 1) != 0) { |
| 8450 | if (index >= CLI_PREFIX_KW_NB) |
| 8451 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8452 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8453 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8454 | kw[index] = lua_tostring(L, -1); |
| 8455 | if (index == 0) |
| 8456 | chunk_printf(trash, "%s", kw[index]); |
| 8457 | else |
| 8458 | chunk_appendf(trash, " %s", kw[index]); |
| 8459 | index++; |
| 8460 | lua_pop(L, 1); |
| 8461 | } |
| 8462 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8463 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8464 | fcn = cli_kw->private; |
| 8465 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8466 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8467 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8468 | } |
| 8469 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8470 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8471 | } |
| 8472 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8473 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8474 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8475 | if (!cli_kws) { |
| 8476 | errmsg = "Lua out of memory error."; |
| 8477 | goto error; |
| 8478 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8479 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8480 | if (!fcn) { |
| 8481 | errmsg = "Lua out of memory error."; |
| 8482 | goto error; |
| 8483 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8484 | |
| 8485 | /* Fill path. */ |
| 8486 | index = 0; |
| 8487 | lua_pushnil(L); |
| 8488 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8489 | if (index >= 5) { |
| 8490 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8491 | goto error; |
| 8492 | } |
| 8493 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8494 | errmsg = "1st argument must be a table filled with strings"; |
| 8495 | goto error; |
| 8496 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8497 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8498 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8499 | errmsg = "Lua out of memory error."; |
| 8500 | goto error; |
| 8501 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8502 | index++; |
| 8503 | lua_pop(L, 1); |
| 8504 | } |
| 8505 | |
| 8506 | /* Copy help message. */ |
| 8507 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8508 | if (!cli_kws->kw[0].usage) { |
| 8509 | errmsg = "Lua out of memory error."; |
| 8510 | goto error; |
| 8511 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8512 | |
| 8513 | /* Fill fcn io handler. */ |
| 8514 | len = strlen("<lua.cli>") + 1; |
| 8515 | for (i = 0; i < index; i++) |
| 8516 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8517 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8518 | if (!fcn->name) { |
| 8519 | errmsg = "Lua out of memory error."; |
| 8520 | goto error; |
| 8521 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8522 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8523 | for (i = 0; i < index; i++) { |
| 8524 | strncat((char *)fcn->name, ".", len); |
| 8525 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8526 | } |
| 8527 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8528 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8529 | |
| 8530 | /* Fill last entries. */ |
| 8531 | cli_kws->kw[0].private = fcn; |
| 8532 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8533 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8534 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8535 | |
| 8536 | /* Register this new converter */ |
| 8537 | cli_register_kw(cli_kws); |
| 8538 | |
| 8539 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8540 | |
| 8541 | error: |
| 8542 | release_hlua_function(fcn); |
| 8543 | if (cli_kws) { |
| 8544 | for (i = 0; i < index; i++) |
| 8545 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8546 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8547 | } |
| 8548 | ha_free(&cli_kws); |
| 8549 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8550 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8551 | } |
| 8552 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 8553 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 8554 | { |
| 8555 | struct hlua_flt_config *conf = fconf->conf; |
| 8556 | lua_State *L; |
| 8557 | int error, pos, state_id, flt_ref; |
| 8558 | |
| 8559 | state_id = reg_flt_to_stack_id(conf->reg); |
| 8560 | L = hlua_states[state_id]; |
| 8561 | pos = lua_gettop(L); |
| 8562 | |
| 8563 | /* The filter parsing function */ |
| 8564 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 8565 | |
| 8566 | /* Push the filter class on the stack and resolve all callbacks */ |
| 8567 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 8568 | |
| 8569 | /* Duplicate the filter class so each filter will have its own copy */ |
| 8570 | lua_newtable(L); |
| 8571 | lua_pushnil(L); |
| 8572 | |
| 8573 | while (lua_next(L, pos+2)) { |
| 8574 | lua_pushvalue(L, -2); |
| 8575 | lua_insert(L, -2); |
| 8576 | lua_settable(L, -4); |
| 8577 | } |
| 8578 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 8579 | |
| 8580 | /* Remove the original lua filter class from the stack */ |
| 8581 | lua_pop(L, 1); |
| 8582 | |
| 8583 | /* Push the copy on the stack */ |
| 8584 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 8585 | |
| 8586 | /* extra args are pushed in a table */ |
| 8587 | lua_newtable(L); |
| 8588 | for (pos = 0; conf->args[pos]; pos++) { |
| 8589 | /* Check stack available size. */ |
| 8590 | if (!lua_checkstack(L, 1)) { |
| 8591 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 8592 | goto error; |
| 8593 | } |
| 8594 | lua_pushstring(L, conf->args[pos]); |
| 8595 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 8596 | } |
| 8597 | |
| 8598 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 8599 | switch (error) { |
| 8600 | case LUA_OK: |
| 8601 | /* replace the filter ref */ |
| 8602 | conf->ref[state_id] = flt_ref; |
| 8603 | break; |
| 8604 | case LUA_ERRRUN: |
| 8605 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 8606 | goto error; |
| 8607 | case LUA_ERRMEM: |
| 8608 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 8609 | goto error; |
| 8610 | case LUA_ERRERR: |
| 8611 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 8612 | goto error; |
| 8613 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 8614 | case LUA_ERRGCMM: |
| 8615 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 8616 | goto error; |
| 8617 | #endif |
| 8618 | default: |
| 8619 | ha_alert("Lua filter '%s' : unknonwn error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 8620 | goto error; |
| 8621 | } |
| 8622 | |
| 8623 | lua_settop(L, 0); |
| 8624 | return 0; |
| 8625 | |
| 8626 | error: |
| 8627 | lua_settop(L, 0); |
| 8628 | return -1; |
| 8629 | } |
| 8630 | |
| 8631 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 8632 | { |
| 8633 | struct hlua_flt_config *conf = fconf->conf; |
| 8634 | lua_State *L; |
| 8635 | int state_id; |
| 8636 | |
| 8637 | if (!conf) |
| 8638 | return; |
| 8639 | |
| 8640 | state_id = reg_flt_to_stack_id(conf->reg); |
| 8641 | L = hlua_states[state_id]; |
| 8642 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 8643 | } |
| 8644 | |
| 8645 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 8646 | { |
| 8647 | struct hlua_flt_config *conf = fconf->conf; |
| 8648 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 8649 | |
| 8650 | /* Rely on per-thread init for global scripts */ |
| 8651 | if (!state_id) |
| 8652 | return hlua_filter_init_per_thread(px, fconf); |
| 8653 | return 0; |
| 8654 | } |
| 8655 | |
| 8656 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 8657 | { |
| 8658 | |
| 8659 | if (fconf->conf) { |
| 8660 | struct hlua_flt_config *conf = fconf->conf; |
| 8661 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 8662 | int pos; |
| 8663 | |
| 8664 | /* Rely on per-thread deinit for global scripts */ |
| 8665 | if (!state_id) |
| 8666 | hlua_filter_deinit_per_thread(px, fconf); |
| 8667 | |
| 8668 | for (pos = 0; conf->args[pos]; pos++) |
| 8669 | free(conf->args[pos]); |
| 8670 | free(conf->args); |
| 8671 | } |
| 8672 | ha_free(&fconf->conf); |
| 8673 | ha_free((char **)&fconf->id); |
| 8674 | ha_free(&fconf->ops); |
| 8675 | } |
| 8676 | |
| 8677 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 8678 | { |
| 8679 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 8680 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 8681 | int ret = 1; |
| 8682 | |
| 8683 | /* In the execution wrappers linked with a stream, the |
| 8684 | * Lua context can be not initialized. This behavior |
| 8685 | * permits to save performances because a systematic |
| 8686 | * Lua initialization cause 5% performances loss. |
| 8687 | */ |
| 8688 | if (!s->hlua) { |
| 8689 | struct hlua *hlua; |
| 8690 | |
| 8691 | hlua = pool_alloc(pool_head_hlua); |
| 8692 | if (!hlua) { |
| 8693 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 8694 | conf->reg->name); |
| 8695 | ret = 0; |
| 8696 | goto end; |
| 8697 | } |
| 8698 | HLUA_INIT(hlua); |
| 8699 | s->hlua = hlua; |
| 8700 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 8701 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 8702 | conf->reg->name); |
| 8703 | ret = 0; |
| 8704 | goto end; |
| 8705 | } |
| 8706 | } |
| 8707 | |
| 8708 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 8709 | if (!flt_ctx) { |
| 8710 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 8711 | conf->reg->name); |
| 8712 | ret = 0; |
| 8713 | goto end; |
| 8714 | } |
| 8715 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 8716 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 8717 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 8718 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 8719 | conf->reg->name); |
| 8720 | ret = 0; |
| 8721 | goto end; |
| 8722 | } |
| 8723 | HLUA_INIT(flt_ctx->hlua[0]); |
| 8724 | HLUA_INIT(flt_ctx->hlua[1]); |
| 8725 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 8726 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 8727 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 8728 | conf->reg->name); |
| 8729 | ret = 0; |
| 8730 | goto end; |
| 8731 | } |
| 8732 | |
| 8733 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 8734 | /* The following Lua calls can fail. */ |
| 8735 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 8736 | const char *error; |
| 8737 | |
| 8738 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 8739 | error = lua_tostring(s->hlua->T, -1); |
| 8740 | else |
| 8741 | error = "critical error"; |
| 8742 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 8743 | ret = 0; |
| 8744 | goto end; |
| 8745 | } |
| 8746 | |
| 8747 | /* Check stack size. */ |
| 8748 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 8749 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 8750 | ret = 0; |
| 8751 | goto end; |
| 8752 | } |
| 8753 | |
| 8754 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 8755 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 8756 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 8757 | conf->reg->name); |
| 8758 | RESET_SAFE_LJMP(s->hlua); |
| 8759 | ret = 0; |
| 8760 | goto end; |
| 8761 | } |
| 8762 | lua_insert(s->hlua->T, -2); |
| 8763 | |
| 8764 | /* Push the copy on the stack */ |
| 8765 | s->hlua->nargs = 1; |
| 8766 | |
| 8767 | /* We must initialize the execution timeouts. */ |
| 8768 | s->hlua->max_time = hlua_timeout_session; |
| 8769 | |
| 8770 | /* At this point the execution is safe. */ |
| 8771 | RESET_SAFE_LJMP(s->hlua); |
| 8772 | } |
| 8773 | |
| 8774 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 8775 | case HLUA_E_OK: |
| 8776 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 8777 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 8778 | ret = 0; |
| 8779 | goto end; |
| 8780 | } |
| 8781 | |
| 8782 | /* Attached the filter pointer to the ctx */ |
| 8783 | lua_pushstring(s->hlua->T, "__filter"); |
| 8784 | lua_pushlightuserdata(s->hlua->T, filter); |
| 8785 | lua_settable(s->hlua->T, -3); |
| 8786 | |
| 8787 | /* Save a ref on the filter ctx */ |
| 8788 | lua_pushvalue(s->hlua->T, 1); |
| 8789 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 8790 | filter->ctx = flt_ctx; |
| 8791 | break; |
| 8792 | case HLUA_E_ERRMSG: |
| 8793 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 8794 | ret = -1; |
| 8795 | goto end; |
| 8796 | case HLUA_E_ETMOUT: |
| 8797 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 8798 | ret = 0; |
| 8799 | goto end; |
| 8800 | case HLUA_E_NOMEM: |
| 8801 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 8802 | ret = 0; |
| 8803 | goto end; |
| 8804 | case HLUA_E_AGAIN: |
| 8805 | case HLUA_E_YIELD: |
| 8806 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 8807 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 8808 | ret = 0; |
| 8809 | goto end; |
| 8810 | case HLUA_E_ERR: |
| 8811 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 8812 | ret = 0; |
| 8813 | goto end; |
| 8814 | default: |
| 8815 | ret = 0; |
| 8816 | goto end; |
| 8817 | } |
| 8818 | |
| 8819 | end: |
| 8820 | if (s->hlua) |
| 8821 | lua_settop(s->hlua->T, 0); |
| 8822 | if (ret <= 0) { |
| 8823 | if (flt_ctx) { |
| 8824 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 8825 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 8826 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 8827 | } |
| 8828 | } |
| 8829 | return ret; |
| 8830 | } |
| 8831 | |
| 8832 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 8833 | { |
| 8834 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 8835 | |
| 8836 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 8837 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 8838 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 8839 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 8840 | filter->ctx = NULL; |
| 8841 | } |
| 8842 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame^] | 8843 | static int hlua_filter_from_payload(struct filter *filter) |
| 8844 | { |
| 8845 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 8846 | |
| 8847 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 8848 | } |
| 8849 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 8850 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 8851 | struct flt_conf *fconf, char **err, void *private) |
| 8852 | { |
| 8853 | struct hlua_reg_filter *reg_flt = private; |
| 8854 | lua_State *L; |
| 8855 | struct hlua_flt_config *conf = NULL; |
| 8856 | const char *flt_id = NULL; |
| 8857 | int state_id, pos, flt_flags = 0; |
| 8858 | struct flt_ops *hlua_flt_ops = NULL; |
| 8859 | |
| 8860 | state_id = reg_flt_to_stack_id(reg_flt); |
| 8861 | L = hlua_states[state_id]; |
| 8862 | |
| 8863 | /* Initialize the filter ops with default callbacks */ |
| 8864 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
| 8865 | if (!hlua_flt_ops) { |
| 8866 | memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name); |
| 8867 | return -1; |
| 8868 | } |
| 8869 | hlua_flt_ops->init = hlua_filter_init; |
| 8870 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 8871 | if (state_id) { |
| 8872 | /* Set per-thread callback if script is loaded per-thread */ |
| 8873 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 8874 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 8875 | } |
| 8876 | hlua_flt_ops->attach = hlua_filter_new; |
| 8877 | hlua_flt_ops->detach = hlua_filter_delete; |
| 8878 | |
| 8879 | /* Push the filter class on the stack and resolve all callbacks */ |
| 8880 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 8881 | |
| 8882 | /* |
| 8883 | * XXX: no callback supported for now |
| 8884 | */ |
| 8885 | |
| 8886 | /* Get id and flags of the filter class */ |
| 8887 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 8888 | flt_id = lua_tostring(L, -1); |
| 8889 | lua_pop(L, 1); |
| 8890 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 8891 | flt_flags = lua_tointeger(L, -1); |
| 8892 | lua_pop(L, 1); |
| 8893 | |
| 8894 | /* Create the filter config */ |
| 8895 | conf = calloc(1, sizeof(*conf)); |
| 8896 | if (!conf) { |
| 8897 | memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name); |
| 8898 | goto error; |
| 8899 | } |
| 8900 | conf->reg = reg_flt; |
| 8901 | |
| 8902 | /* duplicate args */ |
| 8903 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 8904 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
| 8905 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) |
| 8906 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
| 8907 | conf->args[pos] = NULL; |
| 8908 | *cur_arg += pos + 1; |
| 8909 | |
| 8910 | fconf->id = strdup(flt_id); |
| 8911 | fconf->flags = flt_flags; |
| 8912 | fconf->conf = conf; |
| 8913 | fconf->ops = hlua_flt_ops; |
| 8914 | |
| 8915 | lua_settop(L, 0); |
| 8916 | return 0; |
| 8917 | |
| 8918 | error: |
| 8919 | free(hlua_flt_ops); |
| 8920 | free(conf); |
| 8921 | lua_settop(L, 0); |
| 8922 | return -1; |
| 8923 | } |
| 8924 | |
| 8925 | /* This function is an LUA binding used for registering a filter. It expects a |
| 8926 | * fileter name used in the haproxy configuration file and a LUA function to |
| 8927 | * parse configuration arguments. |
| 8928 | */ |
| 8929 | __LJMP static int hlua_register_filter(lua_State *L) |
| 8930 | { |
| 8931 | struct buffer *trash; |
| 8932 | struct flt_kw_list *fkl; |
| 8933 | struct flt_kw *fkw; |
| 8934 | const char *name; |
| 8935 | struct hlua_reg_filter *reg_flt= NULL; |
| 8936 | int flt_ref, fun_ref; |
| 8937 | int len; |
| 8938 | |
| 8939 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 8940 | |
| 8941 | /* First argument : filter name. */ |
| 8942 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8943 | |
| 8944 | /* Second argument : The filter class */ |
| 8945 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 8946 | |
| 8947 | /* Third argument : lua function. */ |
| 8948 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8949 | |
| 8950 | trash = get_trash_chunk(); |
| 8951 | chunk_printf(trash, "lua.%s", name); |
| 8952 | fkw = flt_find_kw(trash->area); |
| 8953 | if (fkw != NULL) { |
| 8954 | reg_flt = fkw->private; |
| 8955 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 8956 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 8957 | "This will become a hard error in version 2.5.\n", name); |
| 8958 | } |
| 8959 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 8960 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 8961 | return 0; |
| 8962 | } |
| 8963 | |
| 8964 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 8965 | if (!fkl) |
| 8966 | goto alloc_error; |
| 8967 | fkl->scope = "HLUA"; |
| 8968 | |
| 8969 | reg_flt = new_hlua_reg_filter(name); |
| 8970 | if (!reg_flt) |
| 8971 | goto alloc_error; |
| 8972 | |
| 8973 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 8974 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 8975 | |
| 8976 | /* The filter keyword */ |
| 8977 | len = strlen("lua.") + strlen(name) + 1; |
| 8978 | fkl->kw[0].kw = calloc(1, len); |
| 8979 | if (!fkl->kw[0].kw) |
| 8980 | goto alloc_error; |
| 8981 | |
| 8982 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 8983 | |
| 8984 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 8985 | fkl->kw[0].private = reg_flt; |
| 8986 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 8987 | |
| 8988 | /* Register this new filter */ |
| 8989 | flt_register_keywords(fkl); |
| 8990 | |
| 8991 | return 0; |
| 8992 | |
| 8993 | alloc_error: |
| 8994 | release_hlua_reg_filter(reg_flt); |
| 8995 | ha_free(&fkl); |
| 8996 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8997 | return 0; /* Never reached */ |
| 8998 | } |
| 8999 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9000 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9001 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9002 | char **err, unsigned int *timeout) |
| 9003 | { |
| 9004 | const char *error; |
| 9005 | |
| 9006 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 9007 | if (error == PARSE_TIME_OVER) { |
| 9008 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 9009 | args[1], args[0]); |
| 9010 | return -1; |
| 9011 | } |
| 9012 | else if (error == PARSE_TIME_UNDER) { |
| 9013 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 9014 | args[1], args[0]); |
| 9015 | return -1; |
| 9016 | } |
| 9017 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9018 | memprintf(err, "%s: invalid timeout", args[0]); |
| 9019 | return -1; |
| 9020 | } |
| 9021 | return 0; |
| 9022 | } |
| 9023 | |
| 9024 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9025 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9026 | char **err) |
| 9027 | { |
| 9028 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 9029 | file, line, err, &hlua_timeout_session); |
| 9030 | } |
| 9031 | |
| 9032 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9033 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9034 | char **err) |
| 9035 | { |
| 9036 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 9037 | file, line, err, &hlua_timeout_task); |
| 9038 | } |
| 9039 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9040 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9041 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9042 | char **err) |
| 9043 | { |
| 9044 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 9045 | file, line, err, &hlua_timeout_applet); |
| 9046 | } |
| 9047 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 9048 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9049 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 9050 | char **err) |
| 9051 | { |
| 9052 | char *error; |
| 9053 | |
| 9054 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 9055 | if (*error != '\0') { |
| 9056 | memprintf(err, "%s: invalid number", args[0]); |
| 9057 | return -1; |
| 9058 | } |
| 9059 | return 0; |
| 9060 | } |
| 9061 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9062 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9063 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9064 | char **err) |
| 9065 | { |
| 9066 | char *error; |
| 9067 | |
| 9068 | if (*(args[1]) == 0) { |
| 9069 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 9070 | return -1; |
| 9071 | } |
| 9072 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 9073 | if (*error != '\0') { |
| 9074 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 9075 | return -1; |
| 9076 | } |
| 9077 | return 0; |
| 9078 | } |
| 9079 | |
| 9080 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9081 | /* This function is called by the main configuration key "lua-load". It loads and |
| 9082 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 9083 | * the main lua entry point. |
| 9084 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9085 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 9086 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9087 | * |
| 9088 | * In some error case, LUA set an error message in top of the stack. This function |
| 9089 | * returns this error message in the HAProxy logs and pop it from the stack. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9090 | * |
| 9091 | * This function can fail with an abort() due to an Lua critical error. |
| 9092 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9093 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9094 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9095 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9096 | { |
| 9097 | int error; |
| 9098 | |
| 9099 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9100 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9101 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9102 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 9103 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9104 | return -1; |
| 9105 | } |
| 9106 | |
| 9107 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9108 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9109 | switch (error) { |
| 9110 | case LUA_OK: |
| 9111 | break; |
| 9112 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9113 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 9114 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9115 | return -1; |
| 9116 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 9117 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9118 | return -1; |
| 9119 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9120 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 9121 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9122 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 9123 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9124 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9125 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 9126 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9127 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 9128 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9129 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9130 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 9131 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9132 | return -1; |
| 9133 | } |
| 9134 | |
| 9135 | return 0; |
| 9136 | } |
| 9137 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9138 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9139 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9140 | char **err) |
| 9141 | { |
| 9142 | if (*(args[1]) == 0) { |
| 9143 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 9144 | return -1; |
| 9145 | } |
| 9146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9147 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9148 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9149 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 9150 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 9151 | } |
| 9152 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9153 | static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9154 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9155 | char **err) |
| 9156 | { |
| 9157 | int len; |
| 9158 | |
| 9159 | if (*(args[1]) == 0) { |
| 9160 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 9161 | return -1; |
| 9162 | } |
| 9163 | |
| 9164 | if (per_thread_load == NULL) { |
| 9165 | /* allocate the first entry large enough to store the final NULL */ |
| 9166 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 9167 | if (per_thread_load == NULL) { |
| 9168 | memprintf(err, "out of memory error"); |
| 9169 | return -1; |
| 9170 | } |
| 9171 | } |
| 9172 | |
| 9173 | /* count used entries */ |
| 9174 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 9175 | ; |
| 9176 | |
| 9177 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 9178 | if (per_thread_load == NULL) { |
| 9179 | memprintf(err, "out of memory error"); |
| 9180 | return -1; |
| 9181 | } |
| 9182 | |
| 9183 | per_thread_load[len] = strdup(args[1]); |
| 9184 | per_thread_load[len + 1] = NULL; |
| 9185 | |
| 9186 | if (per_thread_load[len] == NULL) { |
| 9187 | memprintf(err, "out of memory error"); |
| 9188 | return -1; |
| 9189 | } |
| 9190 | |
| 9191 | /* loading for thread 1 only */ |
| 9192 | hlua_state_id = 1; |
| 9193 | ha_set_tid(0); |
| 9194 | return hlua_load_state(args[1], hlua_states[1], err); |
| 9195 | } |
| 9196 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 9197 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 9198 | * in the given <ctx>. |
| 9199 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 9200 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 9201 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 9202 | lua_getglobal(L, "package"); /* push package variable */ |
| 9203 | lua_pushstring(L, path); /* push given path */ |
| 9204 | lua_pushstring(L, ";"); /* push semicolon */ |
| 9205 | lua_getfield(L, -3, type); /* push old path */ |
| 9206 | lua_concat(L, 3); /* concatenate to new path */ |
| 9207 | lua_setfield(L, -2, type); /* store new path */ |
| 9208 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 9209 | |
| 9210 | return 0; |
| 9211 | } |
| 9212 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9213 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 9214 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9215 | char **err) |
| 9216 | { |
| 9217 | char *path; |
| 9218 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9219 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9220 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9221 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9222 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9223 | } |
| 9224 | |
| 9225 | if (!(*args[1])) { |
| 9226 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9227 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9228 | } |
| 9229 | path = args[1]; |
| 9230 | |
| 9231 | if (*args[2]) { |
| 9232 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 9233 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9234 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9235 | } |
| 9236 | type = args[2]; |
| 9237 | } |
| 9238 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9239 | p = calloc(1, sizeof(*p)); |
| 9240 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 9241 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9242 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9243 | } |
| 9244 | p->path = strdup(path); |
| 9245 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 9246 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9247 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9248 | } |
| 9249 | p->type = strdup(type); |
| 9250 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 9251 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9252 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9253 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 9254 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9255 | |
| 9256 | hlua_prepend_path(hlua_states[0], type, path); |
| 9257 | hlua_prepend_path(hlua_states[1], type, path); |
| 9258 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 9259 | |
| 9260 | err2: |
| 9261 | free(p->type); |
| 9262 | free(p->path); |
| 9263 | err: |
| 9264 | free(p); |
| 9265 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9266 | } |
| 9267 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9268 | /* configuration keywords declaration */ |
| 9269 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 9270 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9271 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9272 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9273 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 9274 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 9275 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 9276 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9277 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 9278 | { 0, NULL, NULL }, |
| 9279 | }}; |
| 9280 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9281 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 9282 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9283 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9284 | /* This function can fail with an abort() due to an Lua critical error. |
| 9285 | * We are in the initialisation process of HAProxy, this abort() is |
| 9286 | * tolerated. |
| 9287 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9288 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9289 | { |
| 9290 | struct hlua_init_function *init; |
| 9291 | const char *msg; |
| 9292 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9293 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9294 | const char *kind; |
| 9295 | const char *trace; |
| 9296 | int return_status = 1; |
| 9297 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 9298 | int nres; |
| 9299 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9300 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9301 | /* disable memory limit checks if limit is not set */ |
| 9302 | if (!hlua_global_allocator.limit) |
| 9303 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 9304 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9305 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9306 | if (setjmp(safe_ljmp_env) != 0) { |
| 9307 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9308 | if (lua_type(L, -1) == LUA_TSTRING) |
| 9309 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 9310 | else |
| 9311 | error = "critical error"; |
| 9312 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 9313 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9314 | } else { |
| 9315 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 9316 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 9317 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9318 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 9319 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9320 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9321 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9322 | |
| 9323 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9324 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9325 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9326 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9327 | #endif |
| 9328 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9329 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9330 | |
| 9331 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9332 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 9333 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9334 | |
| 9335 | case LUA_ERRERR: |
| 9336 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9337 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9338 | case LUA_ERRRUN: |
| 9339 | if (!kind) |
| 9340 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9341 | msg = lua_tostring(L, -1); |
| 9342 | lua_settop(L, 0); /* Empty the stack. */ |
| 9343 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 9344 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9345 | if (msg) |
| 9346 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 9347 | else |
| 9348 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 9349 | return_status = 0; |
| 9350 | break; |
| 9351 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9352 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9353 | /* Unknown error */ |
| 9354 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9355 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9356 | case LUA_YIELD: |
| 9357 | /* yield is not configured at this step, this state doesn't happen */ |
| 9358 | if (!kind) |
| 9359 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9360 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9361 | case LUA_ERRMEM: |
| 9362 | if (!kind) |
| 9363 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9364 | lua_settop(L, 0); |
| 9365 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 9366 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9367 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 9368 | return_status = 0; |
| 9369 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9370 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9371 | if (!return_status) |
| 9372 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9373 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9374 | |
| 9375 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 9376 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9377 | } |
| 9378 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9379 | int hlua_post_init() |
| 9380 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9381 | int ret; |
| 9382 | int i; |
| 9383 | int errors; |
| 9384 | char *err = NULL; |
| 9385 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 9386 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9387 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9388 | #if USE_OPENSSL |
| 9389 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9390 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9391 | int saved_used_backed = global.ssl_used_backend; |
| 9392 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9393 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9394 | global.ssl_used_backend = saved_used_backed; |
| 9395 | } |
| 9396 | #endif |
| 9397 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9398 | /* Perform post init of common thread */ |
| 9399 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9400 | ha_set_tid(0); |
| 9401 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 9402 | if (ret == 0) |
| 9403 | return 0; |
| 9404 | |
| 9405 | /* init remaining lua states and load files */ |
| 9406 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 9407 | |
| 9408 | /* set thread context */ |
| 9409 | ha_set_tid(hlua_state_id - 1); |
| 9410 | |
| 9411 | /* Init lua state */ |
| 9412 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 9413 | |
| 9414 | /* Load lua files */ |
| 9415 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 9416 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 9417 | if (ret != 0) { |
| 9418 | ha_alert("Lua init: %s\n", err); |
| 9419 | return 0; |
| 9420 | } |
| 9421 | } |
| 9422 | } |
| 9423 | |
| 9424 | /* Reset thread context */ |
| 9425 | ha_set_tid(0); |
| 9426 | |
| 9427 | /* Execute post init for all states */ |
| 9428 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 9429 | |
| 9430 | /* set thread context */ |
| 9431 | ha_set_tid(hlua_state_id - 1); |
| 9432 | |
| 9433 | /* run post init */ |
| 9434 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 9435 | if (ret == 0) |
| 9436 | return 0; |
| 9437 | } |
| 9438 | |
| 9439 | /* Reset thread context */ |
| 9440 | ha_set_tid(0); |
| 9441 | |
| 9442 | /* control functions registering. Each function must have: |
| 9443 | * - only the function_ref[0] set positive and all other to -1 |
| 9444 | * - only the function_ref[0] set to -1 and all other positive |
| 9445 | * This ensure a same reference is not used both in shared |
| 9446 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 9447 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9448 | * complicated to found for the end user. |
| 9449 | */ |
| 9450 | errors = 0; |
| 9451 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 9452 | ret = 0; |
| 9453 | for (i = 1; i < global.nbthread + 1; i++) { |
| 9454 | if (fcn->function_ref[i] == -1) |
| 9455 | ret--; |
| 9456 | else |
| 9457 | ret++; |
| 9458 | } |
| 9459 | if (abs(ret) != global.nbthread) { |
| 9460 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 9461 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 9462 | errors++; |
| 9463 | continue; |
| 9464 | } |
| 9465 | |
| 9466 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 9467 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 9468 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9469 | "exclusive.\n", fcn->name); |
| 9470 | errors++; |
| 9471 | } |
| 9472 | } |
| 9473 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 9474 | /* Do the same with registered filters */ |
| 9475 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 9476 | ret = 0; |
| 9477 | for (i = 1; i < global.nbthread + 1; i++) { |
| 9478 | if (reg_flt->flt_ref[i] == -1) |
| 9479 | ret--; |
| 9480 | else |
| 9481 | ret++; |
| 9482 | } |
| 9483 | if (abs(ret) != global.nbthread) { |
| 9484 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 9485 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 9486 | errors++; |
| 9487 | continue; |
| 9488 | } |
| 9489 | |
| 9490 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 9491 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 9492 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 9493 | "exclusive.\n", fcn->name); |
| 9494 | errors++; |
| 9495 | } |
| 9496 | } |
| 9497 | |
| 9498 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9499 | if (errors > 0) |
| 9500 | return 0; |
| 9501 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 9502 | /* after this point, this global will no longer be used, so set to |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9503 | * -1 in order to have probably a segfault if someone use it |
| 9504 | */ |
| 9505 | hlua_state_id = -1; |
| 9506 | |
| 9507 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 9508 | } |
| 9509 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9510 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 9511 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 9512 | * is the previously allocated size or the kind of object in case of a new |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 9513 | * allocation. <nsize> is the requested new size. A new allocation is |
| 9514 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9515 | * zero. This one verifies that the limits are respected but is optimized |
| 9516 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9517 | */ |
| 9518 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 9519 | { |
| 9520 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9521 | size_t limit, old, new; |
| 9522 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 9523 | if (unlikely(!ptr && !nsize)) |
| 9524 | return NULL; |
| 9525 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9526 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 9527 | * for accounting anymore. |
| 9528 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 9529 | if (likely(~zone->limit == 0)) |
| 9530 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9531 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 9532 | if (!ptr) |
| 9533 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9534 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9535 | /* enforce strict limits across all threads */ |
| 9536 | limit = zone->limit; |
| 9537 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 9538 | do { |
| 9539 | new = old + nsize - osize; |
| 9540 | if (unlikely(nsize && limit && new > limit)) |
| 9541 | return NULL; |
| 9542 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9543 | |
| 9544 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9545 | |
| 9546 | if (unlikely(!ptr && nsize)) // failed |
| 9547 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 9548 | |
| 9549 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9550 | return ptr; |
| 9551 | } |
| 9552 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9553 | /* This function can fail with an abort() due to a Lua critical error. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9554 | * We are in the initialisation process of HAProxy, this abort() is |
| 9555 | * tolerated. |
| 9556 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9557 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9558 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9559 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9560 | int idx; |
| 9561 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9562 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9563 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9564 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9565 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9566 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9567 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9568 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9569 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9570 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9571 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9572 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9573 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9574 | *context = NULL; |
| 9575 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9576 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9577 | * the Lua function can fail with an abort. We are in the initialisation |
| 9578 | * process of HAProxy, this abort() is tolerated. |
| 9579 | */ |
| 9580 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9581 | /* Call post initialisation function in safe environment. */ |
| 9582 | if (setjmp(safe_ljmp_env) != 0) { |
| 9583 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9584 | if (lua_type(L, -1) == LUA_TSTRING) |
| 9585 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9586 | else |
| 9587 | error_msg = "critical error"; |
| 9588 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 9589 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9590 | } else { |
| 9591 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9592 | } |
| 9593 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9594 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9595 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9596 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 9597 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 9598 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9599 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9600 | #endif |
| 9601 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9602 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9603 | #endif |
| 9604 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 9605 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9606 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9607 | /* Apply configured prepend path */ |
| 9608 | list_for_each_entry(pp, &prepend_path_list, l) |
| 9609 | hlua_prepend_path(L, pp->type, pp->path); |
| 9610 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9611 | /* |
| 9612 | * |
| 9613 | * Create "core" object. |
| 9614 | * |
| 9615 | */ |
| 9616 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 9617 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9618 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9619 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9620 | /* set the thread id */ |
| 9621 | hlua_class_const_int(L, "thread", thread_num); |
| 9622 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9623 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 9624 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9625 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9626 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9627 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9628 | hlua_class_function(L, "register_init", hlua_register_init); |
| 9629 | hlua_class_function(L, "register_task", hlua_register_task); |
| 9630 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 9631 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 9632 | hlua_class_function(L, "register_action", hlua_register_action); |
| 9633 | hlua_class_function(L, "register_service", hlua_register_service); |
| 9634 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 9635 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9636 | hlua_class_function(L, "yield", hlua_yield); |
| 9637 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 9638 | hlua_class_function(L, "sleep", hlua_sleep); |
| 9639 | hlua_class_function(L, "msleep", hlua_msleep); |
| 9640 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 9641 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 9642 | hlua_class_function(L, "set_map", hlua_set_map); |
| 9643 | hlua_class_function(L, "del_map", hlua_del_map); |
| 9644 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 9645 | hlua_class_function(L, "log", hlua_log); |
| 9646 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 9647 | hlua_class_function(L, "Info", hlua_log_info); |
| 9648 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 9649 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 9650 | hlua_class_function(L, "done", hlua_done); |
| 9651 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9652 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9653 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9654 | |
| 9655 | /* |
| 9656 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9657 | * Create "act" object. |
| 9658 | * |
| 9659 | */ |
| 9660 | |
| 9661 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9662 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9663 | |
| 9664 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9665 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 9666 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 9667 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 9668 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9669 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9670 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9671 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9672 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9673 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9674 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9675 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9676 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9677 | |
| 9678 | /* |
| 9679 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 9680 | * Create "Filter" object. |
| 9681 | * |
| 9682 | */ |
| 9683 | |
| 9684 | /* This table entry is the object "filter" base. */ |
| 9685 | lua_newtable(L); |
| 9686 | |
| 9687 | /* push flags and constants */ |
| 9688 | hlua_class_const_int(L, "CONTINUE", 1); |
| 9689 | hlua_class_const_int(L, "WAIT", 0); |
| 9690 | hlua_class_const_int(L, "ERROR", -1); |
| 9691 | |
| 9692 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 9693 | |
| 9694 | lua_setglobal(L, "filter"); |
| 9695 | |
| 9696 | /* |
| 9697 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9698 | * Register class Map |
| 9699 | * |
| 9700 | */ |
| 9701 | |
| 9702 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9703 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9704 | |
| 9705 | /* register pattern types. */ |
| 9706 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9707 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9708 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9709 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9710 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9711 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9712 | |
| 9713 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9714 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9715 | |
| 9716 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9717 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9718 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9719 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9720 | lua_pushstring(L, "__index"); |
| 9721 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9722 | |
| 9723 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9724 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9725 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9726 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9727 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9728 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9729 | /* Register previous table in the registry with reference and named entry. |
| 9730 | * The function hlua_register_metatable() pops the stack, so we |
| 9731 | * previously create a copy of the table. |
| 9732 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9733 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9734 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9735 | |
| 9736 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9737 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9738 | |
| 9739 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9740 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9741 | |
| 9742 | /* |
| 9743 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9744 | * Register class Channel |
| 9745 | * |
| 9746 | */ |
| 9747 | |
| 9748 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9749 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9750 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9751 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9752 | lua_pushstring(L, "__index"); |
| 9753 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9754 | |
| 9755 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 9756 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 9757 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 9758 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 9759 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9760 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 9761 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 9762 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9763 | hlua_class_function(L, "send", hlua_channel_send); |
| 9764 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 9765 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 9766 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 9767 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9768 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9769 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9770 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 9771 | /* Deprecated API */ |
| 9772 | hlua_class_function(L, "get", hlua_channel_get); |
| 9773 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9774 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9775 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9776 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9777 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9778 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9779 | |
| 9780 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9781 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9782 | |
| 9783 | /* |
| 9784 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9785 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9786 | * |
| 9787 | */ |
| 9788 | |
| 9789 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9790 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9791 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9792 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9793 | lua_pushstring(L, "__index"); |
| 9794 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9795 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9796 | /* Browse existing fetches and create the associated |
| 9797 | * object method. |
| 9798 | */ |
| 9799 | sf = NULL; |
| 9800 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9801 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9802 | * by an underscore. |
| 9803 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9804 | strncpy(trash.area, sf->kw, trash.size); |
| 9805 | trash.area[trash.size - 1] = '\0'; |
| 9806 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9807 | if (*p == '.' || *p == '-' || *p == '+') |
| 9808 | *p = '_'; |
| 9809 | |
| 9810 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9811 | lua_pushstring(L, trash.area); |
| 9812 | lua_pushlightuserdata(L, sf); |
| 9813 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9814 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9815 | } |
| 9816 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9817 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9818 | |
| 9819 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9820 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9821 | |
| 9822 | /* |
| 9823 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9824 | * Register class Converters |
| 9825 | * |
| 9826 | */ |
| 9827 | |
| 9828 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9829 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9830 | |
| 9831 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9832 | lua_pushstring(L, "__index"); |
| 9833 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9834 | |
| 9835 | /* Browse existing converters and create the associated |
| 9836 | * object method. |
| 9837 | */ |
| 9838 | sc = NULL; |
| 9839 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9840 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9841 | * by an underscore. |
| 9842 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9843 | strncpy(trash.area, sc->kw, trash.size); |
| 9844 | trash.area[trash.size - 1] = '\0'; |
| 9845 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9846 | if (*p == '.' || *p == '-' || *p == '+') |
| 9847 | *p = '_'; |
| 9848 | |
| 9849 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9850 | lua_pushstring(L, trash.area); |
| 9851 | lua_pushlightuserdata(L, sc); |
| 9852 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9853 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9854 | } |
| 9855 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9856 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9857 | |
| 9858 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9859 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9860 | |
| 9861 | /* |
| 9862 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9863 | * Register class HTTP |
| 9864 | * |
| 9865 | */ |
| 9866 | |
| 9867 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9868 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9869 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9870 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9871 | lua_pushstring(L, "__index"); |
| 9872 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9873 | |
| 9874 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9875 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9876 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9877 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9878 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9879 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9880 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9881 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9882 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9883 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9884 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9885 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9886 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9887 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9888 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9889 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9890 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9891 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9892 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9893 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9894 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9895 | |
| 9896 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9897 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9898 | |
| 9899 | /* |
| 9900 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9901 | * Register class AppletTCP |
| 9902 | * |
| 9903 | */ |
| 9904 | |
| 9905 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9906 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9907 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9908 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9909 | lua_pushstring(L, "__index"); |
| 9910 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9911 | |
| 9912 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9913 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9914 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9915 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9916 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9917 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9918 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9919 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9920 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9921 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9922 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9923 | |
| 9924 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9925 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9926 | |
| 9927 | /* |
| 9928 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9929 | * Register class AppletHTTP |
| 9930 | * |
| 9931 | */ |
| 9932 | |
| 9933 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9934 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9935 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9936 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9937 | lua_pushstring(L, "__index"); |
| 9938 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9939 | |
| 9940 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9941 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9942 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9943 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9944 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9945 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9946 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9947 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9948 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9949 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9950 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9951 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9952 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9953 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9954 | |
| 9955 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9956 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9957 | |
| 9958 | /* |
| 9959 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9960 | * Register class TXN |
| 9961 | * |
| 9962 | */ |
| 9963 | |
| 9964 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9965 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9966 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9967 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9968 | lua_pushstring(L, "__index"); |
| 9969 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9970 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9971 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9972 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9973 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9974 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9975 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9976 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9977 | hlua_class_function(L, "done", hlua_txn_done); |
| 9978 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9979 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9980 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9981 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9982 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9983 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9984 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9985 | hlua_class_function(L, "log", hlua_txn_log); |
| 9986 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9987 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9988 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9989 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9990 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9991 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9992 | |
| 9993 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9994 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9995 | |
| 9996 | /* |
| 9997 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9998 | * Register class reply |
| 9999 | * |
| 10000 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10001 | lua_newtable(L); |
| 10002 | lua_pushstring(L, "__index"); |
| 10003 | lua_newtable(L); |
| 10004 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 10005 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 10006 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 10007 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 10008 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 10009 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 10010 | |
| 10011 | |
| 10012 | /* |
| 10013 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10014 | * Register class Socket |
| 10015 | * |
| 10016 | */ |
| 10017 | |
| 10018 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10019 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10020 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10021 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10022 | lua_pushstring(L, "__index"); |
| 10023 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10024 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 10025 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10026 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 10027 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10028 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 10029 | hlua_class_function(L, "send", hlua_socket_send); |
| 10030 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 10031 | hlua_class_function(L, "close", hlua_socket_close); |
| 10032 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 10033 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 10034 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 10035 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10036 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10037 | lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10038 | |
| 10039 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10040 | lua_pushstring(L, "__gc"); |
| 10041 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 10042 | lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10043 | |
| 10044 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 10045 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10046 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 10047 | lua_atpanic(L, hlua_panic_safe); |
| 10048 | |
| 10049 | return L; |
| 10050 | } |
| 10051 | |
| 10052 | void hlua_init(void) { |
| 10053 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 10054 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 10055 | #ifdef USE_OPENSSL |
| 10056 | struct srv_kw *kw; |
| 10057 | int tmp_error; |
| 10058 | char *error; |
| 10059 | char *args[] = { /* SSL client configuration. */ |
| 10060 | "ssl", |
| 10061 | "verify", |
| 10062 | "none", |
| 10063 | NULL |
| 10064 | }; |
| 10065 | #endif |
| 10066 | |
| 10067 | /* Init post init function list head */ |
| 10068 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 10069 | LIST_INIT(&hlua_init_functions[i]); |
| 10070 | |
| 10071 | /* Init state for common/shared lua parts */ |
| 10072 | hlua_state_id = 0; |
| 10073 | ha_set_tid(0); |
| 10074 | hlua_states[0] = hlua_init_state(0); |
| 10075 | |
| 10076 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 10077 | hlua_state_id = 1; |
| 10078 | ha_set_tid(0); |
| 10079 | hlua_states[1] = hlua_init_state(1); |
| 10080 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10081 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 10082 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg); |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 10083 | if (!socket_proxy) { |
| 10084 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 10085 | exit(1); |
| 10086 | } |
| 10087 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10088 | |
| 10089 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10090 | socket_tcp = new_server(socket_proxy); |
| 10091 | if (!socket_tcp) { |
| 10092 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 10093 | exit(1); |
| 10094 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10095 | |
| 10096 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10097 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10098 | socket_ssl = new_server(socket_proxy); |
| 10099 | if (!socket_ssl) { |
| 10100 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 10101 | exit(1); |
| 10102 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10103 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10104 | socket_ssl->use_ssl = 1; |
| 10105 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10106 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 10107 | for (i = 0; args[i] != NULL; i++) { |
| 10108 | if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10109 | /* |
| 10110 | * |
| 10111 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10112 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10113 | * features like client certificates and ssl_verify. |
| 10114 | * |
| 10115 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10116 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10117 | if (tmp_error != 0) { |
| 10118 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 10119 | abort(); /* This must be never arrives because the command line |
| 10120 | not editable by the user. */ |
| 10121 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 10122 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10123 | } |
| 10124 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 10125 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 10126 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 10127 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 10128 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 10129 | static void hlua_deinit() |
| 10130 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 10131 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10132 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 10133 | |
| 10134 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 10135 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 10136 | |
| 10137 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 10138 | if (hlua_states[thr]) |
| 10139 | lua_close(hlua_states[thr]); |
| 10140 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 10141 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10142 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 10143 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 10144 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10145 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 10146 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 10147 | |
| 10148 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 10149 | } |
| 10150 | |
| 10151 | REGISTER_POST_DEINIT(hlua_deinit); |
| 10152 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10153 | static void hlua_register_build_options(void) |
| 10154 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 10155 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10156 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 10157 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 10158 | hap_register_build_opts(ptr, 1); |
| 10159 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10160 | |
| 10161 | INITCALL0(STG_REGISTER, hlua_register_build_options); |