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 | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 37 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 38 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 40 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 41 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 43 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 45 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 46 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 47 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 48 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 49 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 50 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 51 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 53 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 54 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 55 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 56 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 57 | #include <haproxy/ssl_ckch.h> |
| 58 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 59 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 60 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 68 | #include <haproxy/event_hdl.h> |
Aurelien DARRAGON | c99f3ad | 2023-04-12 15:47:16 +0200 | [diff] [blame] | 69 | #include <haproxy/check.h> |
Aurelien DARRAGON | 5bed48f | 2023-04-21 17:32:46 +0200 | [diff] [blame] | 70 | #include <haproxy/mailers.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 71 | |
Tristan | 2632d04 | 2023-10-23 13:07:39 +0100 | [diff] [blame] | 72 | /* Global LUA flags */ |
| 73 | |
| 74 | enum hlua_log_opt { |
| 75 | /* tune.lua.log.loggers */ |
| 76 | HLUA_LOG_LOGGERS_ON = 0x00000001, /* forward logs to current loggers */ |
| 77 | |
| 78 | /* tune.lua.log.stderr */ |
| 79 | HLUA_LOG_STDERR_ON = 0x00000010, /* forward logs to stderr */ |
| 80 | HLUA_LOG_STDERR_AUTO = 0x00000020, /* forward logs to stderr if no loggers */ |
| 81 | HLUA_LOG_STDERR_MASK = 0x00000030, |
| 82 | }; |
| 83 | /* default log options, made of flags in hlua_log_opt */ |
| 84 | static uint hlua_log_opts = HLUA_LOG_LOGGERS_ON | HLUA_LOG_STDERR_ON; |
| 85 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 86 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 87 | * macro is used only for identifying the function that can |
| 88 | * not return because a longjmp is executed. |
| 89 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 90 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 91 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 92 | */ |
| 93 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 94 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 95 | #define MAY_LJMP(func) func |
| 96 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 97 | /* This couple of function executes securely some Lua calls outside of |
| 98 | * the lua runtime environment. Each Lua call can return a longjmp |
| 99 | * if it encounter a memory error. |
| 100 | * |
| 101 | * Lua documentation extract: |
| 102 | * |
| 103 | * If an error happens outside any protected environment, Lua calls |
| 104 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 105 | * exiting the host application. Your panic function can avoid this |
| 106 | * exit by never returning (e.g., doing a long jump to your own |
| 107 | * recovery point outside Lua). |
| 108 | * |
| 109 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 110 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 111 | * stack. However, there is no guarantee about stack space. To push |
| 112 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 113 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 114 | * |
| 115 | * We must check all the Lua entry point. This includes: |
| 116 | * - The include/proto/hlua.h exported functions |
| 117 | * - the task wrapper function |
| 118 | * - The action wrapper function |
| 119 | * - The converters wrapper function |
| 120 | * - The sample-fetch wrapper functions |
| 121 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 122 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 123 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | * |
| 125 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 126 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 127 | * because they must be exists in the program stack when the longjmp |
| 128 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 129 | * |
| 130 | * Note that the Lua processing is not really thread safe. It provides |
| 131 | * heavy system which consists to add our own lock function in the Lua |
| 132 | * code and recompile the library. This system will probably not accepted |
| 133 | * by maintainers of various distribs. |
| 134 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 135 | * 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] | 136 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 137 | * of function and a lua_unlock() at the end of the function. So I |
| 138 | * conclude that the Lua thread safe mode just perform a mutex around |
| 139 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 140 | * easier for distro maintainers. |
| 141 | * |
| 142 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 143 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 144 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 145 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 146 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 147 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 148 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 149 | 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] | 150 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 151 | /* This is the chained list of struct hlua_function referenced |
| 152 | * for haproxy action, sample-fetches, converters, cli and |
| 153 | * applet bindings. It is used for a post-initialisation control. |
| 154 | */ |
| 155 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 156 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 157 | /* This variable is used only during initialization to identify the Lua state |
| 158 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 159 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 160 | */ |
| 161 | static int hlua_state_id; |
| 162 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 163 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 164 | static char ***per_thread_load = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 165 | |
| 166 | lua_State *hlua_init_state(int thread_id); |
| 167 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 168 | /* This function takes the Lua global lock. Keep this function's visibility |
| 169 | * global so that it can appear in stack dumps and performance profiles! |
| 170 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 171 | static inline void lua_take_global_lock() |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 172 | { |
| 173 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 174 | } |
| 175 | |
| 176 | static inline void lua_drop_global_lock() |
| 177 | { |
| 178 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 179 | } |
| 180 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 181 | /* lua lock helpers: only lock when required |
| 182 | * |
| 183 | * state_id == 0: we're operating on the main lua stack (shared between |
| 184 | * os threads), so we need to acquire the main lock |
| 185 | * |
| 186 | * If the thread already owns the lock (_hlua_locked != 0), skip the lock |
| 187 | * attempt. This could happen if we run under protected lua environment. |
| 188 | * Not doing this could result in deadlocks because of nested locking |
| 189 | * attempts from the same thread |
| 190 | */ |
| 191 | static THREAD_LOCAL int _hlua_locked = 0; |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 192 | static inline void hlua_lock(struct hlua *hlua) |
| 193 | { |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 194 | if (hlua->state_id != 0) |
| 195 | return; |
| 196 | if (!_hlua_locked) |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 197 | lua_take_global_lock(); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 198 | _hlua_locked += 1; |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 199 | } |
| 200 | static inline void hlua_unlock(struct hlua *hlua) |
| 201 | { |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 202 | if (hlua->state_id != 0) |
| 203 | return; |
| 204 | BUG_ON(_hlua_locked <= 0); |
| 205 | _hlua_locked--; |
| 206 | /* drop the lock once the lock count reaches 0 */ |
| 207 | if (!_hlua_locked) |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 208 | lua_drop_global_lock(); |
| 209 | } |
| 210 | |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 211 | /* below is an helper function to retrieve string on on Lua stack at <index> |
| 212 | * in a safe way (function may not LJMP). It can be useful to retrieve errors |
| 213 | * at the top of the stack from an unprotected environment. |
| 214 | * |
| 215 | * The returned string will is only valid as long as the value at <index> is |
| 216 | * not removed from the stack. |
| 217 | * |
| 218 | * It is assumed that the calling function is allowed to manipulate <L> |
| 219 | */ |
| 220 | __LJMP static int _hlua_tostring_safe(lua_State *L) |
| 221 | { |
| 222 | const char **str = lua_touserdata(L, 1); |
| 223 | const char *cur_str = MAY_LJMP(lua_tostring(L, 2)); |
| 224 | |
| 225 | if (cur_str) |
| 226 | *str = cur_str; |
| 227 | return 0; |
| 228 | } |
| 229 | static const char *hlua_tostring_safe(lua_State *L, int index) |
| 230 | { |
| 231 | const char *str = NULL; |
| 232 | |
| 233 | if (!lua_checkstack(L, 4)) |
| 234 | return NULL; |
| 235 | |
| 236 | /* before any stack modification, save the targeted value on the top of |
| 237 | * the stack: this will allow us to use relative index to target it. |
| 238 | */ |
| 239 | lua_pushvalue(L, index); |
| 240 | |
| 241 | /* push our custom _hlua_tostring_safe() function on the stack, then push |
| 242 | * our own string pointer and targeted value (at <index>) as argument |
| 243 | */ |
| 244 | lua_pushcfunction(L, _hlua_tostring_safe); |
| 245 | lua_pushlightuserdata(L, &str); // 1st func argument = string pointer |
| 246 | lua_pushvalue(L, -3); // 2nd func argument = targeted value |
| 247 | |
| 248 | lua_remove(L, -4); // remove <index> copy as we're done using it |
| 249 | |
| 250 | /* call our custom function with proper arguments using pcall() to catch |
| 251 | * exceptions (if any) |
| 252 | */ |
| 253 | switch (lua_pcall(L, 2, 0, 0)) { |
| 254 | case LUA_OK: |
| 255 | break; |
| 256 | default: |
| 257 | /* error was caught */ |
| 258 | return NULL; |
| 259 | } |
| 260 | return str; |
| 261 | } |
| 262 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 263 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 264 | ({ \ |
| 265 | int ret; \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 266 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 267 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 268 | lua_atpanic(__L, hlua_panic_safe); \ |
| 269 | ret = 0; \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 270 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 271 | } else { \ |
| 272 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 273 | ret = 1; \ |
| 274 | } \ |
| 275 | ret; \ |
| 276 | }) |
| 277 | |
| 278 | /* If we are the last function catching Lua errors, we |
| 279 | * must reset the panic function. |
| 280 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 281 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 282 | do { \ |
| 283 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 284 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 285 | } while(0) |
| 286 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 287 | #define SET_SAFE_LJMP(__HLUA) \ |
| 288 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 289 | |
| 290 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 291 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 292 | |
| 293 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 294 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 295 | |
| 296 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 297 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 298 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 299 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 300 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 301 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 302 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 303 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 304 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 305 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 306 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 307 | /* The main Lua execution context. The 0 index is the |
| 308 | * common state shared by all threads. |
| 309 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 310 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 311 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 312 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 313 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 314 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 315 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 316 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 317 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 318 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 319 | struct hlua_reg_filter { |
| 320 | char *name; |
| 321 | int flt_ref[MAX_THREADS + 1]; |
| 322 | int fun_ref[MAX_THREADS + 1]; |
| 323 | struct list l; |
| 324 | }; |
| 325 | |
| 326 | struct hlua_flt_config { |
| 327 | struct hlua_reg_filter *reg; |
| 328 | int ref[MAX_THREADS + 1]; |
| 329 | char **args; |
| 330 | }; |
| 331 | |
| 332 | struct hlua_flt_ctx { |
| 333 | int ref; /* ref to the filter lua object */ |
| 334 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 335 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 336 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 337 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 338 | }; |
| 339 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 340 | /* appctx context used by the cosockets */ |
| 341 | struct hlua_csk_ctx { |
| 342 | int connected; |
| 343 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 344 | struct list wake_on_read; |
| 345 | struct list wake_on_write; |
| 346 | struct appctx *appctx; |
Christopher Faulet | 2678657 | 2023-09-27 17:39:44 +0200 | [diff] [blame] | 347 | struct server *srv; |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 348 | int timeout; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 349 | int die; |
| 350 | }; |
| 351 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 352 | /* appctx context used by TCP services */ |
| 353 | struct hlua_tcp_ctx { |
| 354 | struct hlua *hlua; |
| 355 | int flags; |
| 356 | struct task *task; |
| 357 | }; |
| 358 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 359 | /* appctx context used by HTTP services */ |
| 360 | struct hlua_http_ctx { |
| 361 | struct hlua *hlua; |
| 362 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 363 | int flags; |
| 364 | int status; |
| 365 | const char *reason; |
| 366 | struct task *task; |
| 367 | }; |
| 368 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 369 | /* used by registered CLI keywords */ |
| 370 | struct hlua_cli_ctx { |
| 371 | struct hlua *hlua; |
| 372 | struct task *task; |
| 373 | struct hlua_function *fcn; |
| 374 | }; |
| 375 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 376 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 377 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 378 | static int hlua_filter_from_payload(struct filter *filter); |
| 379 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 380 | /* This is the chained list of struct hlua_flt referenced |
| 381 | * for haproxy filters. It is used for a post-initialisation control. |
| 382 | */ |
| 383 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 384 | |
| 385 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 386 | /* This is the memory pool containing struct lua for applets |
| 387 | * (including cli). |
| 388 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 389 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 390 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 391 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 392 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 393 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 394 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 395 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 396 | #endif |
| 397 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 398 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 399 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 400 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 401 | /* The following variables contains the reference of the different |
| 402 | * Lua classes. These references are useful for identify metadata |
| 403 | * associated with an object. |
| 404 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 405 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 406 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 407 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 408 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 409 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 410 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 411 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 412 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 413 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 414 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 415 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 416 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 417 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 418 | /* Lua max execution timeouts. By default, stream-related |
| 419 | * lua coroutines (e.g.: actions) have a short timeout. |
| 420 | * On the other hand tasks coroutines don't have a timeout because |
| 421 | * a task may remain alive during all the haproxy execution. |
| 422 | * |
| 423 | * Timeouts are expressed in milliseconds, they are meant to be used |
| 424 | * with hlua timer's API exclusively. |
| 425 | * 0 means no timeout |
| 426 | */ |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 427 | static uint32_t hlua_timeout_burst = 1000; /* burst timeout. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 428 | static uint32_t hlua_timeout_session = 4000; /* session timeout. */ |
| 429 | static uint32_t hlua_timeout_task = 0; /* task timeout. */ |
| 430 | static uint32_t hlua_timeout_applet = 4000; /* applet timeout. */ |
| 431 | |
| 432 | /* hlua multipurpose timer: |
| 433 | * used to compute burst lua time (within a single hlua_ctx_resume()) |
| 434 | * and cumulative lua time for a given coroutine, and to check |
| 435 | * the lua coroutine against the configured timeouts |
| 436 | */ |
| 437 | |
| 438 | /* fetch per-thread cpu_time with ms precision (may wrap) */ |
| 439 | static inline uint32_t _hlua_time_ms() |
| 440 | { |
| 441 | /* We're interested in the current cpu time in ms, which will be returned |
| 442 | * as a uint32_t to save some space. |
| 443 | * We must take the following into account: |
| 444 | * |
| 445 | * - now_cpu_time_fast() which returns the time in nanoseconds as a uint64_t |
| 446 | * will wrap every 585 years. |
| 447 | * - uint32_t may only contain 4294967295ms (~=49.7 days), so _hlua_time_ms() |
| 448 | * itself will also wrap every 49.7 days. |
| 449 | * |
| 450 | * While we can safely ignore the now_cpu_time_fast() wrap, we must |
| 451 | * take care of the uint32_t wrap by making sure to exclusively |
| 452 | * manipulate the time using uint32_t everywhere _hlua_time_ms() |
| 453 | * is involved. |
| 454 | */ |
| 455 | return (uint32_t)(now_cpu_time_fast() / 1000000ULL); |
| 456 | } |
| 457 | |
| 458 | /* computes time spent in a single lua execution (in ms) */ |
| 459 | static inline uint32_t _hlua_time_burst(const struct hlua_timer *timer) |
| 460 | { |
| 461 | uint32_t burst_ms; |
| 462 | |
| 463 | /* wrapping is expected and properly |
| 464 | * handled thanks to _hlua_time_ms() and burst_ms |
| 465 | * being of the same type |
| 466 | */ |
| 467 | burst_ms = _hlua_time_ms() - timer->start; |
| 468 | return burst_ms; |
| 469 | } |
| 470 | |
| 471 | static inline void hlua_timer_init(struct hlua_timer *timer, unsigned int max) |
| 472 | { |
| 473 | timer->cumulative = 0; |
| 474 | timer->burst = 0; |
| 475 | timer->max = max; |
| 476 | } |
| 477 | |
| 478 | /* reset the timer ctx between 2 yields */ |
| 479 | static inline void hlua_timer_reset(struct hlua_timer *timer) |
| 480 | { |
| 481 | timer->cumulative += timer->burst; |
| 482 | timer->burst = 0; |
| 483 | } |
| 484 | |
| 485 | /* start the timer right before a new execution */ |
| 486 | static inline void hlua_timer_start(struct hlua_timer *timer) |
| 487 | { |
| 488 | timer->start = _hlua_time_ms(); |
| 489 | } |
| 490 | |
| 491 | /* update hlua timer when finishing an execution */ |
| 492 | static inline void hlua_timer_stop(struct hlua_timer *timer) |
| 493 | { |
| 494 | timer->burst += _hlua_time_burst(timer); |
| 495 | } |
| 496 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 497 | /* check the timers for current hlua context: |
| 498 | * - first check for burst timeout (max execution time for the current |
| 499 | hlua resume, ie: time between effective yields) |
| 500 | * - then check for yield cumulative timeout |
| 501 | * |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 502 | * Returns 1 if the check succeeded and 0 if it failed |
| 503 | * (ie: timeout exceeded) |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 504 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 505 | static inline int hlua_timer_check(const struct hlua_timer *timer) |
| 506 | { |
| 507 | uint32_t pburst = _hlua_time_burst(timer); /* pending burst time in ms */ |
| 508 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 509 | if (hlua_timeout_burst && (timer->burst + pburst) > hlua_timeout_burst) |
| 510 | return 0; /* burst timeout exceeded */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 511 | if (timer->max && (timer->cumulative + timer->burst + pburst) > timer->max) |
| 512 | return 0; /* cumulative timeout exceeded */ |
| 513 | return 1; /* ok */ |
| 514 | } |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 515 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 516 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 517 | * it is used for preventing infinite loops. |
| 518 | * |
| 519 | * I test the scheer with an infinite loop containing one incrementation |
| 520 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 521 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 522 | * to one interrupt each 10 000 instructions. |
| 523 | * |
| 524 | * configured | Number of |
| 525 | * instructions | loops executed |
| 526 | * between two | in milions |
| 527 | * forced yields | |
| 528 | * ---------------+--------------- |
| 529 | * 10 | 160 |
| 530 | * 500 | 670 |
| 531 | * 1000 | 680 |
| 532 | * 5000 | 700 |
| 533 | * 7000 | 700 |
| 534 | * 8000 | 700 |
| 535 | * 9000 | 710 <- ceil |
| 536 | * 10000 | 710 |
| 537 | * 100000 | 710 |
| 538 | * 1000000 | 710 |
| 539 | * |
| 540 | */ |
| 541 | static unsigned int hlua_nb_instruction = 10000; |
| 542 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 543 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 544 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 545 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 546 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 547 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 548 | */ |
| 549 | struct hlua_mem_allocator { |
| 550 | size_t allocated; |
| 551 | size_t limit; |
| 552 | }; |
| 553 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 554 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 555 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 556 | /* hlua event subscription */ |
| 557 | struct hlua_event_sub { |
| 558 | int fcn_ref; |
| 559 | int state_id; |
| 560 | struct hlua *hlua; |
| 561 | struct task *task; |
| 562 | event_hdl_async_equeue equeue; |
| 563 | struct event_hdl_sub *sub; |
| 564 | uint8_t paused; |
| 565 | }; |
| 566 | |
| 567 | /* This is the memory pool containing struct hlua_event_sub |
| 568 | * for event subscriptions from lua |
| 569 | */ |
| 570 | DECLARE_STATIC_POOL(pool_head_hlua_event_sub, "hlua_esub", sizeof(struct hlua_event_sub)); |
| 571 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | /* These functions converts types between HAProxy internal args or |
| 573 | * sample and LUA types. Another function permits to check if the |
| 574 | * LUA stack contains arguments according with an required ARG_T |
| 575 | * format. |
| 576 | */ |
Aurelien DARRAGON | e5c048a | 2023-05-17 10:51:50 +0200 | [diff] [blame] | 577 | __LJMP static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 578 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 579 | __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] | 580 | uint64_t mask, struct proxy *p); |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 581 | __LJMP static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Aurelien DARRAGON | 742b1a8 | 2023-05-17 10:38:50 +0200 | [diff] [blame] | 582 | __LJMP static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 583 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 584 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 585 | __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] | 586 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 587 | struct prepend_path { |
| 588 | struct list l; |
| 589 | char *type; |
| 590 | char *path; |
| 591 | }; |
| 592 | |
| 593 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 594 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 595 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 596 | do { \ |
| 597 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 598 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 599 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 600 | } while (0) |
| 601 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 602 | static inline struct hlua_function *new_hlua_function() |
| 603 | { |
| 604 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 605 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 606 | |
| 607 | fcn = calloc(1, sizeof(*fcn)); |
| 608 | if (!fcn) |
| 609 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 610 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 611 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 612 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 613 | return fcn; |
| 614 | } |
| 615 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 616 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 617 | { |
| 618 | if (!fcn) |
| 619 | return; |
| 620 | if (fcn->name) |
| 621 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 622 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 623 | ha_free(&fcn); |
| 624 | } |
| 625 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 626 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 627 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 628 | { |
| 629 | if (fcn->function_ref[0] == -1) |
| 630 | return tid + 1; |
| 631 | return 0; |
| 632 | } |
| 633 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 634 | /* Create a new registered filter. Only its name is filled */ |
| 635 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 636 | { |
| 637 | struct hlua_reg_filter *reg_flt; |
| 638 | int i; |
| 639 | |
| 640 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 641 | if (!reg_flt) |
| 642 | return NULL; |
| 643 | reg_flt->name = strdup(name); |
| 644 | if (!reg_flt->name) { |
| 645 | free(reg_flt); |
| 646 | return NULL; |
| 647 | } |
| 648 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 649 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 650 | reg_flt->flt_ref[i] = -1; |
| 651 | reg_flt->fun_ref[i] = -1; |
| 652 | } |
| 653 | return reg_flt; |
| 654 | } |
| 655 | |
| 656 | /* Release a registered filter */ |
| 657 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 658 | { |
| 659 | if (!reg_flt) |
| 660 | return; |
| 661 | if (reg_flt->name) |
| 662 | ha_free(®_flt->name); |
| 663 | LIST_DELETE(®_flt->l); |
| 664 | ha_free(®_flt); |
| 665 | } |
| 666 | |
| 667 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 668 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 669 | { |
| 670 | if (reg_flt->fun_ref[0] == -1) |
| 671 | return tid + 1; |
| 672 | return 0; |
| 673 | } |
| 674 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 675 | /* Used to check an Lua function type in the stack. It creates and |
| 676 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 677 | * error if the argument is not a "function". |
| 678 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 679 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 680 | __LJMP int hlua_checkfunction(lua_State *L, int argno) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 681 | { |
| 682 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 683 | 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] | 684 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 685 | } |
| 686 | lua_pushvalue(L, argno); |
| 687 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 688 | } |
| 689 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 690 | /* Used to check an Lua table type in the stack. It creates and |
| 691 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 692 | * error if the argument is not a "table". |
| 693 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 694 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 695 | __LJMP int hlua_checktable(lua_State *L, int argno) |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 696 | { |
| 697 | if (!lua_istable(L, argno)) { |
| 698 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 699 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 700 | } |
| 701 | lua_pushvalue(L, argno); |
| 702 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 703 | } |
| 704 | |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 705 | /* Get a reference to the object that is at the top of the stack |
| 706 | * The referenced object will be popped from the stack |
| 707 | * |
| 708 | * The function returns the reference to the object which must |
| 709 | * be cleared using hlua_unref() when no longer used |
| 710 | */ |
| 711 | __LJMP int hlua_ref(lua_State *L) |
| 712 | { |
| 713 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 714 | } |
| 715 | |
| 716 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 717 | * on <L> stack |
| 718 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 719 | * |
| 720 | * When the reference is no longer used, it should be released by calling |
| 721 | * hlua_unref() |
| 722 | * |
| 723 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 724 | * parent state that the one used to get the reference. |
| 725 | */ |
| 726 | void hlua_pushref(lua_State *L, int ref) |
| 727 | { |
| 728 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 729 | } |
| 730 | |
| 731 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 732 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 733 | * |
| 734 | * This will allow the reference to be reused and the referred object |
| 735 | * to be garbage collected. |
| 736 | * |
| 737 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 738 | * parent state that the one used to get the reference. |
| 739 | */ |
| 740 | void hlua_unref(lua_State *L, int ref) |
| 741 | { |
| 742 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 743 | } |
| 744 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 745 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 746 | { |
| 747 | lua_Debug ar; |
| 748 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 749 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 750 | |
| 751 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 752 | /* Fill fields: |
| 753 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 754 | * 'l': fills in the field currentline; |
| 755 | * 'n': fills in the field name and namewhat; |
| 756 | * 't': fills in the field istailcall; |
| 757 | */ |
| 758 | lua_getinfo(L, "Slnt", &ar); |
| 759 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 760 | /* skip these empty entries, usually they come from deep C functions */ |
| 761 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 762 | continue; |
| 763 | |
| 764 | /* Add separator */ |
| 765 | if (b_data(msg)) |
| 766 | chunk_appendf(msg, "%s", sep); |
| 767 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 768 | /* Append code localisation */ |
| 769 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 770 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 771 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 772 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * Get function name |
| 776 | * |
| 777 | * if namewhat is no empty, name is defined. |
| 778 | * what contains "Lua" for Lua function, "C" for C function, |
| 779 | * or "main" for main code. |
| 780 | */ |
| 781 | 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] | 782 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 783 | |
| 784 | 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] | 785 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 786 | |
| 787 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 788 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 789 | |
| 790 | else /* nothing left... */ |
| 791 | chunk_appendf(msg, "?"); |
| 792 | |
| 793 | |
| 794 | /* Display tailed call */ |
| 795 | if (ar.istailcall) |
| 796 | chunk_appendf(msg, " ..."); |
| 797 | } |
| 798 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 799 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 803 | /* This function check the number of arguments available in the |
| 804 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 805 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 806 | */ |
| 807 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 808 | { |
| 809 | if (lua_gettop(L) == nb) |
| 810 | return; |
| 811 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 812 | } |
| 813 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 814 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 815 | * and the line number where the error is encountered. |
| 816 | */ |
| 817 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 818 | { |
| 819 | va_list argp; |
| 820 | va_start(argp, fmt); |
| 821 | luaL_where(L, 1); |
| 822 | lua_pushvfstring(L, fmt, argp); |
| 823 | va_end(argp); |
| 824 | lua_concat(L, 2); |
| 825 | return 1; |
| 826 | } |
| 827 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 828 | /* This functions is used with sample fetch and converters. It |
| 829 | * converts the HAProxy configuration argument in a lua stack |
| 830 | * values. |
| 831 | * |
| 832 | * It takes an array of "arg", and each entry of the array is |
| 833 | * converted and pushed in the LUA stack. |
| 834 | */ |
Aurelien DARRAGON | e5c048a | 2023-05-17 10:51:50 +0200 | [diff] [blame] | 835 | __LJMP static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 836 | { |
| 837 | switch (arg->type) { |
| 838 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 839 | case ARGT_TIME: |
| 840 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 841 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 842 | break; |
| 843 | |
| 844 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 845 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 846 | break; |
| 847 | |
| 848 | case ARGT_IPV4: |
| 849 | case ARGT_IPV6: |
| 850 | case ARGT_MSK4: |
| 851 | case ARGT_MSK6: |
| 852 | case ARGT_FE: |
| 853 | case ARGT_BE: |
| 854 | case ARGT_TAB: |
| 855 | case ARGT_SRV: |
| 856 | case ARGT_USR: |
| 857 | case ARGT_MAP: |
| 858 | default: |
| 859 | lua_pushnil(L); |
| 860 | break; |
| 861 | } |
| 862 | return 1; |
| 863 | } |
| 864 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 865 | /* 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] | 866 | * 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] | 867 | * with sample fetch wrappers. The input arguments are given to the |
| 868 | * lua wrapper and converted as arg list by the function. |
Aurelien DARRAGON | 0aaf6c4 | 2023-05-17 15:33:59 +0200 | [diff] [blame] | 869 | * |
| 870 | * Note: although lua_tolstring() may raise a memory error according to |
| 871 | * lua documentation, in practise this could only happen when using to |
| 872 | * use lua_tolstring() on a number (lua will try to push the number as a |
| 873 | * string on the stack, and this may result in memory failure), so here we |
| 874 | * assume that hlua_lua2arg() will never raise an exception since it is |
| 875 | * exclusively used with lua string inputs. |
| 876 | * |
| 877 | * Note2: You should be extra careful when using <arg> argument, since |
| 878 | * string arguments rely on lua_tolstring() which returns a pointer to lua |
| 879 | * object that may be garbage collected at any time when removed from lua |
| 880 | * stack, thus you should make sure that <arg> is only used from a local |
| 881 | * scope within lua context (and not exported or stored in a lua-independent |
| 882 | * ctx) and that related lua object still exists when accessing arg data. |
| 883 | * See: https://www.lua.org/manual/5.4/manual.html#4.1.3 |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 884 | */ |
| 885 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 886 | { |
| 887 | switch (lua_type(L, ud)) { |
| 888 | |
| 889 | case LUA_TNUMBER: |
| 890 | case LUA_TBOOLEAN: |
| 891 | arg->type = ARGT_SINT; |
| 892 | arg->data.sint = lua_tointeger(L, ud); |
| 893 | break; |
| 894 | |
| 895 | case LUA_TSTRING: |
| 896 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 897 | 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] | 898 | /* 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] | 899 | 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] | 900 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 901 | break; |
| 902 | |
| 903 | case LUA_TUSERDATA: |
| 904 | case LUA_TNIL: |
| 905 | case LUA_TTABLE: |
| 906 | case LUA_TFUNCTION: |
| 907 | case LUA_TTHREAD: |
| 908 | case LUA_TLIGHTUSERDATA: |
| 909 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 910 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 911 | break; |
| 912 | } |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | /* the following functions are used to convert a struct sample |
| 917 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 918 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 919 | */ |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 920 | __LJMP static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 921 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 922 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 923 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 924 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 925 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 926 | break; |
| 927 | |
| 928 | case SMP_T_BIN: |
| 929 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 930 | 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] | 931 | break; |
| 932 | |
| 933 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 934 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 935 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 936 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 937 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 938 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 939 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 940 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 941 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 942 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 943 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 944 | 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] | 945 | break; |
| 946 | default: |
| 947 | lua_pushnil(L); |
| 948 | break; |
| 949 | } |
| 950 | break; |
| 951 | |
| 952 | case SMP_T_IPV4: |
| 953 | case SMP_T_IPV6: |
| 954 | 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] | 955 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 956 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 957 | 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] | 958 | else |
| 959 | lua_pushnil(L); |
| 960 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 961 | default: |
| 962 | lua_pushnil(L); |
| 963 | break; |
| 964 | } |
| 965 | return 1; |
| 966 | } |
| 967 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 968 | /* the following functions are used to convert a struct sample |
| 969 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 970 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 971 | */ |
Aurelien DARRAGON | 742b1a8 | 2023-05-17 10:38:50 +0200 | [diff] [blame] | 972 | __LJMP static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 973 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 974 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 975 | |
| 976 | case SMP_T_BIN: |
| 977 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 978 | 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] | 979 | break; |
| 980 | |
| 981 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 982 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 983 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 984 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 985 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 986 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 987 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 988 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 989 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 990 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 991 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 992 | 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] | 993 | break; |
| 994 | default: |
| 995 | lua_pushstring(L, ""); |
| 996 | break; |
| 997 | } |
| 998 | break; |
| 999 | |
| 1000 | case SMP_T_SINT: |
| 1001 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 1002 | case SMP_T_IPV4: |
| 1003 | case SMP_T_IPV6: |
| 1004 | 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] | 1005 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 1006 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1007 | 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] | 1008 | else |
| 1009 | lua_pushstring(L, ""); |
| 1010 | break; |
| 1011 | default: |
| 1012 | lua_pushstring(L, ""); |
| 1013 | break; |
| 1014 | } |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
Aurelien DARRAGON | a3624cb | 2023-05-17 15:44:45 +0200 | [diff] [blame] | 1018 | /* The following function is used to convert a Lua type to a |
| 1019 | * struct sample. This is useful to provide data from LUA code to |
| 1020 | * a converter. |
| 1021 | * |
| 1022 | * Note: although lua_tolstring() may raise a memory error according to |
| 1023 | * lua documentation, in practise this could only happen when using to |
| 1024 | * use lua_tolstring() on a number (lua will try to push the number as a |
| 1025 | * string on the stack, and this may result in memory failure), so here we |
| 1026 | * assume that hlua_lua2arg() will never raise an exception since it is |
| 1027 | * exclusively used with lua string inputs. |
| 1028 | * |
| 1029 | * Note2: You should be extra careful when using <smp> argument, since |
| 1030 | * string arguments rely on lua_tolstring() which returns a pointer to lua |
| 1031 | * object that may be garbage collected at any time when removed from lua |
| 1032 | * stack, thus you should make sure that <smp> is only used from a local |
| 1033 | * scope within lua context (not exported or stored in a lua-independent |
| 1034 | * ctx) and that related lua object still exists when accessing arg data. |
| 1035 | * See: https://www.lua.org/manual/5.4/manual.html#4.1.3 |
| 1036 | * |
| 1037 | * If you don't comply with this usage restriction, then you should consider |
| 1038 | * duplicating the smp using smp_dup() to make it portable (little overhead), |
| 1039 | * as this will ensure that the smp always points to valid memory block. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1040 | */ |
| 1041 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 1042 | { |
| 1043 | switch (lua_type(L, ud)) { |
| 1044 | |
| 1045 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1046 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1047 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1048 | break; |
| 1049 | |
| 1050 | |
| 1051 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1052 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1053 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1054 | break; |
| 1055 | |
| 1056 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1057 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1058 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 1059 | 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] | 1060 | /* 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] | 1061 | 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] | 1062 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1063 | break; |
| 1064 | |
| 1065 | case LUA_TUSERDATA: |
| 1066 | case LUA_TNIL: |
| 1067 | case LUA_TTABLE: |
| 1068 | case LUA_TFUNCTION: |
| 1069 | case LUA_TTHREAD: |
| 1070 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 1071 | case LUA_TNONE: |
| 1072 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1073 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1074 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | return 1; |
| 1078 | } |
| 1079 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 1080 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1081 | * 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] | 1082 | * 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] | 1083 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1084 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1085 | * 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] | 1086 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1087 | __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] | 1088 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1089 | { |
| 1090 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1091 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1092 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1093 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1094 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1095 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1096 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1097 | |
| 1098 | idx = 0; |
| 1099 | min_arg = ARGM(mask); |
| 1100 | mask >>= ARGM_BITS; |
| 1101 | |
| 1102 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1103 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1104 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1105 | /* Check for mandatory arguments. */ |
| 1106 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1107 | if (idx < min_arg) { |
| 1108 | |
| 1109 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1110 | if (idx > 0) { |
| 1111 | msg = "Mandatory argument expected"; |
| 1112 | goto error; |
| 1113 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1114 | |
| 1115 | /* If first argument have a certain type, some default values |
| 1116 | * may be used. See the function smp_resolve_args(). |
| 1117 | */ |
| 1118 | switch (mask & ARGT_MASK) { |
| 1119 | |
| 1120 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1121 | if (!(p->cap & PR_CAP_FE)) { |
| 1122 | msg = "Mandatory argument expected"; |
| 1123 | goto error; |
| 1124 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1125 | argp[idx].data.prx = p; |
| 1126 | argp[idx].type = ARGT_FE; |
| 1127 | argp[idx+1].type = ARGT_STOP; |
| 1128 | break; |
| 1129 | |
| 1130 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1131 | if (!(p->cap & PR_CAP_BE)) { |
| 1132 | msg = "Mandatory argument expected"; |
| 1133 | goto error; |
| 1134 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1135 | argp[idx].data.prx = p; |
| 1136 | argp[idx].type = ARGT_BE; |
| 1137 | argp[idx+1].type = ARGT_STOP; |
| 1138 | break; |
| 1139 | |
| 1140 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 1141 | if (!p->table) { |
| 1142 | msg = "Mandatory argument expected"; |
| 1143 | goto error; |
| 1144 | } |
| 1145 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1146 | argp[idx].type = ARGT_TAB; |
| 1147 | argp[idx+1].type = ARGT_STOP; |
| 1148 | break; |
| 1149 | |
| 1150 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1151 | msg = "Mandatory argument expected"; |
| 1152 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1156 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1159 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1160 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 1161 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1162 | msg = "Last argument expected"; |
| 1163 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 1167 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1168 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1171 | /* Convert some argument types. All string in argp[] are for not |
| 1172 | * duplicated yet. |
| 1173 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1174 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1175 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1176 | if (argp[idx].type != ARGT_SINT) { |
| 1177 | msg = "integer expected"; |
| 1178 | goto error; |
| 1179 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1180 | argp[idx].type = ARGT_SINT; |
| 1181 | break; |
| 1182 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1183 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1184 | if (argp[idx].type != ARGT_SINT) { |
| 1185 | msg = "integer expected"; |
| 1186 | goto error; |
| 1187 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 1188 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1189 | break; |
| 1190 | |
| 1191 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1192 | if (argp[idx].type != ARGT_SINT) { |
| 1193 | msg = "integer expected"; |
| 1194 | goto error; |
| 1195 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 1196 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1197 | break; |
| 1198 | |
| 1199 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1200 | if (argp[idx].type != ARGT_STR) { |
| 1201 | msg = "string expected"; |
| 1202 | goto error; |
| 1203 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1204 | 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] | 1205 | if (!argp[idx].data.prx) { |
| 1206 | msg = "frontend doesn't exist"; |
| 1207 | goto error; |
| 1208 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1209 | argp[idx].type = ARGT_FE; |
| 1210 | break; |
| 1211 | |
| 1212 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1213 | if (argp[idx].type != ARGT_STR) { |
| 1214 | msg = "string expected"; |
| 1215 | goto error; |
| 1216 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1217 | 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] | 1218 | if (!argp[idx].data.prx) { |
| 1219 | msg = "backend doesn't exist"; |
| 1220 | goto error; |
| 1221 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1222 | argp[idx].type = ARGT_BE; |
| 1223 | break; |
| 1224 | |
| 1225 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1226 | if (argp[idx].type != ARGT_STR) { |
| 1227 | msg = "string expected"; |
| 1228 | goto error; |
| 1229 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1230 | 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] | 1231 | if (!argp[idx].data.t) { |
| 1232 | msg = "table doesn't exist"; |
| 1233 | goto error; |
| 1234 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1235 | argp[idx].type = ARGT_TAB; |
| 1236 | break; |
| 1237 | |
| 1238 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1239 | if (argp[idx].type != ARGT_STR) { |
| 1240 | msg = "string expected"; |
| 1241 | goto error; |
| 1242 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1243 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1244 | if (sname) { |
| 1245 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1246 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1247 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1248 | if (!px) { |
| 1249 | msg = "backend doesn't exist"; |
| 1250 | goto error; |
| 1251 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1252 | } |
| 1253 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1254 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1255 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1256 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1257 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1258 | if (!argp[idx].data.srv) { |
| 1259 | msg = "server doesn't exist"; |
| 1260 | goto error; |
| 1261 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1262 | argp[idx].type = ARGT_SRV; |
| 1263 | break; |
| 1264 | |
| 1265 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1266 | if (argp[idx].type != ARGT_STR) { |
| 1267 | msg = "string expected"; |
| 1268 | goto error; |
| 1269 | } |
| 1270 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1271 | msg = "invalid IPv4 address"; |
| 1272 | goto error; |
| 1273 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1274 | argp[idx].type = ARGT_IPV4; |
| 1275 | break; |
| 1276 | |
| 1277 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1278 | if (argp[idx].type == ARGT_SINT) |
| 1279 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1280 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1281 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1282 | msg = "invalid IPv4 mask"; |
| 1283 | goto error; |
| 1284 | } |
| 1285 | } |
| 1286 | else { |
| 1287 | msg = "integer or string expected"; |
| 1288 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1289 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1290 | argp[idx].type = ARGT_MSK4; |
| 1291 | break; |
| 1292 | |
| 1293 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1294 | if (argp[idx].type != ARGT_STR) { |
| 1295 | msg = "string expected"; |
| 1296 | goto error; |
| 1297 | } |
| 1298 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1299 | msg = "invalid IPv6 address"; |
| 1300 | goto error; |
| 1301 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1302 | argp[idx].type = ARGT_IPV6; |
| 1303 | break; |
| 1304 | |
| 1305 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1306 | if (argp[idx].type == ARGT_SINT) |
| 1307 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1308 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1309 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1310 | msg = "invalid IPv6 mask"; |
| 1311 | goto error; |
| 1312 | } |
| 1313 | } |
| 1314 | else { |
| 1315 | msg = "integer or string expected"; |
| 1316 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1317 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1318 | argp[idx].type = ARGT_MSK6; |
| 1319 | break; |
| 1320 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1321 | case ARGT_REG: |
| 1322 | if (argp[idx].type != ARGT_STR) { |
| 1323 | msg = "string expected"; |
| 1324 | goto error; |
| 1325 | } |
| 1326 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1327 | if (!reg) { |
| 1328 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1329 | argp[idx].data.str.area, err); |
| 1330 | free(err); |
| 1331 | goto error; |
| 1332 | } |
| 1333 | argp[idx].type = ARGT_REG; |
| 1334 | argp[idx].data.reg = reg; |
| 1335 | break; |
| 1336 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1337 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1338 | if (argp[idx].type != ARGT_STR) { |
| 1339 | msg = "string expected"; |
| 1340 | goto error; |
| 1341 | } |
| 1342 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1343 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1344 | ul = p->uri_auth->userlist; |
| 1345 | else |
| 1346 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1347 | |
| 1348 | if (!ul) { |
| 1349 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1350 | goto error; |
| 1351 | } |
| 1352 | argp[idx].type = ARGT_USR; |
| 1353 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1354 | break; |
| 1355 | |
| 1356 | case ARGT_STR: |
| 1357 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1358 | msg = "unable to duplicate string arg"; |
| 1359 | goto error; |
| 1360 | } |
| 1361 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1362 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1363 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1364 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1365 | msg = "type not yet supported"; |
| 1366 | goto error; |
| 1367 | break; |
| 1368 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* Check for type of argument. */ |
| 1372 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1373 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1374 | arg_type_names[(mask & ARGT_MASK)], |
| 1375 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1376 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* Next argument. */ |
| 1380 | mask >>= ARGT_BITS; |
| 1381 | idx++; |
| 1382 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1383 | return 0; |
| 1384 | |
| 1385 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1386 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1387 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1388 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1389 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1392 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1393 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1394 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1395 | * |
| 1396 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1397 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1398 | */ |
Aurelien DARRAGON | 40cd44f | 2023-05-04 13:59:48 +0200 | [diff] [blame] | 1399 | inline struct hlua *hlua_gethlua(lua_State *L) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1401 | struct hlua **hlua = lua_getextraspace(L); |
| 1402 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1403 | } |
| 1404 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1405 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1406 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1407 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1410 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1411 | * currently is executing from within a Lua function. One line per entry will |
| 1412 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1413 | * Lua function is not detected, NULL is returned. |
| 1414 | */ |
| 1415 | const char *hlua_show_current_location(const char *pfx) |
| 1416 | { |
| 1417 | lua_State *L; |
| 1418 | lua_Debug ar; |
| 1419 | |
| 1420 | /* global or per-thread stack initializing ? */ |
| 1421 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1422 | return hlua_traceback(L, pfx); |
| 1423 | |
| 1424 | /* per-thread stack running ? */ |
| 1425 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1426 | return hlua_traceback(L, pfx); |
| 1427 | |
| 1428 | /* global stack running ? */ |
| 1429 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1430 | return hlua_traceback(L, pfx); |
| 1431 | |
| 1432 | return NULL; |
| 1433 | } |
| 1434 | |
Tristan | 2632d04 | 2023-10-23 13:07:39 +0100 | [diff] [blame] | 1435 | /* This function is used to send logs. It tries to send them to: |
| 1436 | * - the log target applicable in the current context, AND |
| 1437 | * - stderr if not in quiet mode or explicitly disabled |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1438 | */ |
| 1439 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1440 | { |
| 1441 | struct tm tm; |
| 1442 | char *p; |
| 1443 | |
| 1444 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1445 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1446 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1447 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1448 | /* Break the message if exceed the buffer size. */ |
| 1449 | *(p-4) = ' '; |
| 1450 | *(p-3) = '.'; |
| 1451 | *(p-2) = '.'; |
| 1452 | *(p-1) = '.'; |
| 1453 | break; |
| 1454 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1455 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1456 | *p = *msg; |
| 1457 | else |
| 1458 | *p = '.'; |
| 1459 | } |
| 1460 | *p = '\0'; |
| 1461 | |
Tristan | 2632d04 | 2023-10-23 13:07:39 +0100 | [diff] [blame] | 1462 | if (hlua_log_opts & HLUA_LOG_LOGGERS_ON) |
| 1463 | send_log(px, level, "%s\n", trash.area); |
| 1464 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1465 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Tristan | 2632d04 | 2023-10-23 13:07:39 +0100 | [diff] [blame] | 1466 | if (!(hlua_log_opts & HLUA_LOG_STDERR_MASK)) |
| 1467 | return; |
| 1468 | |
| 1469 | /* when logging via stderr is set to 'auto', it behaves like 'off' unless one of: |
| 1470 | * - logging via loggers is disabled |
| 1471 | * - this is a non-proxy context and there is no global logger configured |
| 1472 | * - this is a proxy context and the proxy has no logger configured |
| 1473 | */ |
| 1474 | if ((hlua_log_opts & (HLUA_LOG_STDERR_MASK | HLUA_LOG_LOGGERS_ON)) == (HLUA_LOG_STDERR_AUTO | HLUA_LOG_LOGGERS_ON)) { |
| 1475 | /* AUTO=OFF in non-proxy context only if at least one global logger is defined */ |
| 1476 | if ((px == NULL) && (!LIST_ISEMPTY(&global.logsrvs))) |
| 1477 | return; |
| 1478 | |
| 1479 | /* AUTO=OFF in proxy context only if at least one logger is configured for the proxy */ |
| 1480 | if ((px != NULL) && (!LIST_ISEMPTY(&px->logsrvs))) |
| 1481 | return; |
| 1482 | } |
| 1483 | |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1484 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1485 | return; |
| 1486 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1487 | get_localtime(date.tv_sec, &tm); |
| 1488 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1489 | 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] | 1490 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1491 | fflush(stderr); |
| 1492 | } |
| 1493 | } |
| 1494 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1495 | /* This function just ensure that the yield will be always |
| 1496 | * returned with a timeout and permit to set some flags |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 1497 | * <timeout> is a tick value |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1498 | */ |
Aurelien DARRAGON | bcdf07e | 2023-07-13 10:18:04 +0200 | [diff] [blame] | 1499 | __LJMP void hlua_yieldk(lua_State *L, int nresults, lua_KContext ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1500 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1501 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1502 | struct hlua *hlua; |
| 1503 | |
| 1504 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1505 | hlua = hlua_gethlua(L); |
| 1506 | if (!hlua) { |
| 1507 | return; |
| 1508 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1509 | |
| 1510 | /* Set the wake timeout. If timeout is required, we set |
| 1511 | * the expiration time. |
| 1512 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1513 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1514 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1515 | hlua->flags |= flags; |
| 1516 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1517 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1518 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1521 | /* This function initialises the Lua environment stored in the stream. |
| 1522 | * 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] | 1523 | * 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] | 1524 | * |
| 1525 | * This function is particular. it initialises a new Lua thread. If the |
| 1526 | * initialisation fails (example: out of memory error), the lua function |
| 1527 | * throws an error (longjmp). |
| 1528 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1529 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1530 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1531 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1532 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1533 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1534 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1535 | { |
| 1536 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1537 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1538 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1539 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1540 | lua->state_id = state_id; |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1541 | hlua_timer_init(&lua->timer, 0); /* default value, no timeout */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1542 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1543 | MT_LIST_INIT(&lua->hc_list); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1544 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1545 | lua->Tref = LUA_REFNIL; |
| 1546 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1547 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1548 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | if (!lua->T) { |
| 1550 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1551 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1552 | return 0; |
| 1553 | } |
| 1554 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1555 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1556 | lua->task = task; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1557 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1558 | return 1; |
| 1559 | } |
| 1560 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1561 | /* kill all associated httpclient to this hlua task |
| 1562 | * We must take extra precautions as we're manipulating lua-exposed |
| 1563 | * objects without the main lua lock. |
| 1564 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1565 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1566 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1567 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1568 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1569 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1570 | * another thread sharing the same main lua stack (lua coroutine) |
| 1571 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1572 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1573 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1574 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1575 | * be thread safe in Lua. (From lua co-author: |
| 1576 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1577 | * |
| 1578 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1579 | * since in this case coroutines exclusively run on the same thread |
| 1580 | * (main stack is not shared between OS threads). |
| 1581 | */ |
| 1582 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1583 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1584 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1589 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1590 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | 60ab0f7 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1591 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1592 | */ |
| 1593 | void hlua_ctx_destroy(struct hlua *lua) |
| 1594 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1595 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1596 | return; |
| 1597 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1598 | if (!lua->T) |
| 1599 | goto end; |
| 1600 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1601 | /* clean all running httpclient */ |
| 1602 | hlua_httpclient_destroy_all(lua); |
| 1603 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1604 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1605 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1606 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1607 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1608 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1609 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1610 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1611 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1612 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1613 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1614 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1615 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1616 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1617 | * without error, we run the GC on the thread pointer. Its freed all |
| 1618 | * the unused memory. |
| 1619 | * If the thread is finnish with an error or is currently yielded, |
| 1620 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1621 | * so e run the GC on the main thread. |
| 1622 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1623 | * the garbage collection. |
| 1624 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1625 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1626 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1627 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1628 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1629 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1630 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1631 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1632 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1633 | |
| 1634 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1635 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* This function is used to restore the Lua context when a coroutine |
| 1639 | * fails. This function copy the common memory between old coroutine |
| 1640 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1641 | * replaced by the new coroutine. |
| 1642 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1643 | * as string error message and it is copied in the new stack. |
| 1644 | */ |
| 1645 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1646 | { |
| 1647 | lua_State *T; |
| 1648 | int new_ref; |
| 1649 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1650 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1651 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1652 | if (!T) |
| 1653 | return 0; |
| 1654 | |
| 1655 | /* Copy last error message. */ |
| 1656 | if (keep_msg) |
| 1657 | lua_xmove(lua->T, T, 1); |
| 1658 | |
| 1659 | /* Copy data between the coroutines. */ |
| 1660 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1661 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1662 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* Destroy old data. */ |
| 1665 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1666 | |
| 1667 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1668 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1669 | |
| 1670 | /* Fill the struct with the new coroutine values. */ |
| 1671 | lua->Mref = new_ref; |
| 1672 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1673 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1674 | |
| 1675 | /* Set context. */ |
| 1676 | hlua_sethlua(lua); |
| 1677 | |
| 1678 | return 1; |
| 1679 | } |
| 1680 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1681 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1682 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1683 | struct hlua *hlua; |
| 1684 | |
| 1685 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1686 | hlua = hlua_gethlua(L); |
| 1687 | if (!hlua) |
| 1688 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1689 | |
Aurelien DARRAGON | cf0f792 | 2023-04-07 16:34:20 +0200 | [diff] [blame] | 1690 | if (hlua->T != L) { |
| 1691 | /* We don't want to enforce a yield on a sub coroutine, since |
| 1692 | * we have no guarantees that the yield will be handled properly. |
| 1693 | * Indeed, only the hlua->T coroutine is being handled through |
| 1694 | * hlua_ctx_resume() function. |
| 1695 | * |
| 1696 | * Instead, we simply check for timeouts and wait for the sub |
| 1697 | * coroutine to finish.. |
| 1698 | */ |
| 1699 | goto check_timeout; |
| 1700 | } |
| 1701 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1702 | /* Lua cannot yield when its returning from a function, |
| 1703 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1704 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1705 | */ |
| 1706 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1707 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1708 | return; |
| 1709 | } |
| 1710 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1711 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1712 | * If the state is not yieldable, trying yield causes an error. |
| 1713 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1714 | if (lua_isyieldable(L)) { |
| 1715 | /* note: for converters/fetches.. where yielding is not allowed |
| 1716 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1717 | * instead of rescheduling hlua->task. |
| 1718 | * also: hlua_ctx_resume() will take care of checking execution |
| 1719 | * timeout and re-applying the hook as needed. |
| 1720 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1721 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1722 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1723 | * |
| 1724 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1725 | * |
| 1726 | * Moreover, it seems that we don't want to continue after the yield |
| 1727 | * because the end of the function is about handling unyieldable function, |
| 1728 | * which is not the case here. |
| 1729 | * |
| 1730 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1731 | * in the function. |
| 1732 | */ |
| 1733 | return; |
| 1734 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1735 | |
Aurelien DARRAGON | cf0f792 | 2023-04-07 16:34:20 +0200 | [diff] [blame] | 1736 | check_timeout: |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1737 | /* If we cannot yield, check the timeout. */ |
| 1738 | if (!hlua_timer_check(&hlua->timer)) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1739 | lua_pushfstring(L, "execution timeout"); |
| 1740 | WILL_LJMP(lua_error(L)); |
| 1741 | } |
| 1742 | |
| 1743 | /* Try to interrupt the process at the end of the current |
| 1744 | * unyieldable function. |
| 1745 | */ |
| 1746 | 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] | 1747 | } |
| 1748 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1749 | /* This function start or resumes the Lua stack execution. If the flag |
| 1750 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1751 | * The function return an error. |
| 1752 | * |
| 1753 | * The function can returns 4 values: |
| 1754 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1755 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1756 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1757 | * - 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] | 1758 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1759 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1760 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1761 | * 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] | 1762 | * LUA code. |
| 1763 | */ |
| 1764 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1765 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1766 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1767 | int nres; |
| 1768 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1769 | int ret; |
| 1770 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1771 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1772 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1773 | /* Lock the whole Lua execution. This lock must be before the |
| 1774 | * label "resume_execution". |
| 1775 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1776 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1777 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1778 | /* reset the timer as we might be re-entering the function to |
| 1779 | * resume the coroutine after a successful yield |
| 1780 | * (cumulative time will be updated) |
| 1781 | */ |
| 1782 | hlua_timer_reset(&lua->timer); |
| 1783 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1784 | resume_execution: |
| 1785 | |
| 1786 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1787 | * instructions. it is used for preventing infinite loops. |
| 1788 | */ |
| 1789 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1790 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1791 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1792 | HLUA_SET_RUN(lua); |
| 1793 | HLUA_CLR_CTRLYIELD(lua); |
| 1794 | HLUA_CLR_WAKERESWR(lua); |
| 1795 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1796 | HLUA_CLR_NOYIELD(lua); |
| 1797 | if (!yield_allowed) |
| 1798 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1799 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1800 | /* reset wake_time. */ |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1801 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1802 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1803 | /* start the timer as we're about to start lua processing */ |
| 1804 | hlua_timer_start(&lua->timer); |
| 1805 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1806 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1807 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1808 | 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] | 1809 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1810 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1811 | #endif |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* out of lua processing, stop the timer */ |
| 1814 | hlua_timer_stop(&lua->timer); |
| 1815 | |
Aurelien DARRAGON | fa76a10 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1816 | /* reset nargs because those possibly passed to the lua_resume() call |
| 1817 | * were already consumed, and since we may call lua_resume() again |
| 1818 | * after a successful yield, we don't want to pass stale nargs hint |
| 1819 | * to the Lua API. As such, nargs should be set explicitly before each |
| 1820 | * lua_resume() (or hlua_ctx_resume()) invocation if needed. |
| 1821 | */ |
| 1822 | lua->nargs = 0; |
| 1823 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1824 | switch (ret) { |
| 1825 | |
| 1826 | case LUA_OK: |
| 1827 | ret = HLUA_E_OK; |
| 1828 | break; |
| 1829 | |
| 1830 | case LUA_YIELD: |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1831 | /* Check if the execution timeout is expired. If it is the case, we |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1832 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1833 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1834 | if (!hlua_timer_check(&lua->timer)) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1835 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1836 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1837 | break; |
| 1838 | } |
| 1839 | /* Process the forced yield. if the general yield is not allowed or |
| 1840 | * if no task were associated this the current Lua execution |
| 1841 | * coroutine, we resume the execution. Else we want to return in the |
| 1842 | * scheduler and we want to be waked up again, to continue the |
| 1843 | * current Lua execution. So we schedule our own task. |
| 1844 | */ |
| 1845 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1846 | if (!yield_allowed || !lua->task) |
| 1847 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1848 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1849 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1850 | if (!yield_allowed) { |
| 1851 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1852 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1853 | break; |
| 1854 | } |
| 1855 | ret = HLUA_E_AGAIN; |
| 1856 | break; |
| 1857 | |
| 1858 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1859 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1860 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1861 | * because the errors ares the only one mean to return immediately |
| 1862 | * from and lua execution. |
| 1863 | */ |
| 1864 | if (lua->flags & HLUA_EXIT) { |
| 1865 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1866 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1867 | break; |
| 1868 | } |
| 1869 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1870 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1871 | if (!lua_checkstack(lua->T, 1)) { |
| 1872 | ret = HLUA_E_ERR; |
| 1873 | break; |
| 1874 | } |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1875 | msg = hlua_tostring_safe(lua->T, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1876 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1877 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1878 | 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] | 1879 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1880 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Aurelien DARRAGON | 93361a5 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1881 | |
| 1882 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1883 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1884 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1885 | ret = HLUA_E_ERRMSG; |
| 1886 | break; |
| 1887 | |
| 1888 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1889 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1890 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1891 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1892 | break; |
| 1893 | |
| 1894 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1895 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1896 | if (!lua_checkstack(lua->T, 1)) { |
| 1897 | ret = HLUA_E_ERR; |
| 1898 | break; |
| 1899 | } |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1900 | msg = hlua_tostring_safe(lua->T, -1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1901 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1902 | 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] | 1903 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1904 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Aurelien DARRAGON | 93361a5 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1905 | |
| 1906 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1907 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1908 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1909 | ret = HLUA_E_ERRMSG; |
| 1910 | break; |
| 1911 | |
| 1912 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1913 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1914 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1915 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1916 | break; |
| 1917 | } |
| 1918 | |
| 1919 | switch (ret) { |
| 1920 | case HLUA_E_AGAIN: |
| 1921 | break; |
| 1922 | |
| 1923 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1924 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1925 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1926 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1927 | break; |
| 1928 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1929 | case HLUA_E_ETMOUT: |
| 1930 | case HLUA_E_NOMEM: |
| 1931 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1932 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1933 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1934 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1935 | hlua_ctx_renew(lua, 0); |
| 1936 | break; |
| 1937 | |
| 1938 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1939 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1940 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1941 | break; |
| 1942 | } |
| 1943 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1944 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1945 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1946 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1947 | return ret; |
| 1948 | } |
| 1949 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1950 | /* This function exit the current code. */ |
| 1951 | __LJMP static int hlua_done(lua_State *L) |
| 1952 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1953 | struct hlua *hlua; |
| 1954 | |
| 1955 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1956 | hlua = hlua_gethlua(L); |
| 1957 | if (!hlua) |
| 1958 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1959 | |
| 1960 | hlua->flags |= HLUA_EXIT; |
| 1961 | WILL_LJMP(lua_error(L)); |
| 1962 | |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1966 | /* This function is an LUA binding. It provides a function |
| 1967 | * for deleting ACL from a referenced ACL file. |
| 1968 | */ |
| 1969 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1970 | { |
| 1971 | const char *name; |
| 1972 | const char *key; |
| 1973 | struct pat_ref *ref; |
| 1974 | |
| 1975 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1976 | |
| 1977 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1978 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1979 | |
| 1980 | ref = pat_ref_lookup(name); |
| 1981 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1982 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1983 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1984 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1985 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1986 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1987 | return 0; |
| 1988 | } |
| 1989 | |
| 1990 | /* This function is an LUA binding. It provides a function |
| 1991 | * for deleting map entry from a referenced map file. |
| 1992 | */ |
| 1993 | static int hlua_del_map(lua_State *L) |
| 1994 | { |
| 1995 | const char *name; |
| 1996 | const char *key; |
| 1997 | struct pat_ref *ref; |
| 1998 | |
| 1999 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 2000 | |
| 2001 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2002 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 2003 | |
| 2004 | ref = pat_ref_lookup(name); |
| 2005 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 2006 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2007 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 2008 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2009 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 2010 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | /* This function is an LUA binding. It provides a function |
| 2015 | * for adding ACL pattern from a referenced ACL file. |
| 2016 | */ |
| 2017 | static int hlua_add_acl(lua_State *L) |
| 2018 | { |
| 2019 | const char *name; |
| 2020 | const char *key; |
| 2021 | struct pat_ref *ref; |
| 2022 | |
| 2023 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 2024 | |
| 2025 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2026 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 2027 | |
| 2028 | ref = pat_ref_lookup(name); |
| 2029 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 2030 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2031 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 2032 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2033 | if (pat_ref_find_elt(ref, key) == NULL) |
| 2034 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 2035 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2036 | return 0; |
| 2037 | } |
| 2038 | |
| 2039 | /* This function is an LUA binding. It provides a function |
| 2040 | * for setting map pattern and sample from a referenced map |
| 2041 | * file. |
| 2042 | */ |
| 2043 | static int hlua_set_map(lua_State *L) |
| 2044 | { |
| 2045 | const char *name; |
| 2046 | const char *key; |
| 2047 | const char *value; |
| 2048 | struct pat_ref *ref; |
| 2049 | |
| 2050 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 2051 | |
| 2052 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2053 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 2054 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 2055 | |
| 2056 | ref = pat_ref_lookup(name); |
| 2057 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 2058 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2059 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 2060 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2061 | if (pat_ref_find_elt(ref, key) != NULL) |
| 2062 | pat_ref_set(ref, key, value, NULL); |
| 2063 | else |
| 2064 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 2065 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 2066 | return 0; |
| 2067 | } |
| 2068 | |
Aurelien DARRAGON | 5bed48f | 2023-04-21 17:32:46 +0200 | [diff] [blame] | 2069 | /* This function disables the sending of email through the |
| 2070 | * legacy email sending function which is implemented using |
| 2071 | * checks. |
| 2072 | * |
| 2073 | * It may not be used during runtime. |
| 2074 | */ |
| 2075 | __LJMP static int hlua_disable_legacy_mailers(lua_State *L) |
| 2076 | { |
| 2077 | if (hlua_gethlua(L)) |
| 2078 | WILL_LJMP(luaL_error(L, "disable_legacy_mailers: " |
| 2079 | "not available outside of init or body context")); |
| 2080 | send_email_disabled = 1; |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2084 | /* A class is a lot of memory that contain data. This data can be a table, |
| 2085 | * 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] | 2086 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2087 | * the name of the object (_G[<name>] = <metable> ). |
| 2088 | * |
| 2089 | * A metable is a table that modify the standard behavior of a standard |
| 2090 | * access to the associated data. The entries of this new metatable are |
| 2091 | * defined as is: |
| 2092 | * |
| 2093 | * http://lua-users.org/wiki/MetatableEvents |
| 2094 | * |
| 2095 | * __index |
| 2096 | * |
| 2097 | * we access an absent field in a table, the result is nil. This is |
| 2098 | * true, but it is not the whole truth. Actually, such access triggers |
| 2099 | * the interpreter to look for an __index metamethod: If there is no |
| 2100 | * such method, as usually happens, then the access results in nil; |
| 2101 | * otherwise, the metamethod will provide the result. |
| 2102 | * |
| 2103 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 2104 | * the key does not appear in the table, but the metatable has an __index |
| 2105 | * property: |
| 2106 | * |
| 2107 | * - if the value is a function, the function is called, passing in the |
| 2108 | * table and the key; the return value of that function is returned as |
| 2109 | * the result. |
| 2110 | * |
| 2111 | * - if the value is another table, the value of the key in that table is |
| 2112 | * asked for and returned (and if it doesn't exist in that table, but that |
| 2113 | * table's metatable has an __index property, then it continues on up) |
| 2114 | * |
| 2115 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 2116 | * |
| 2117 | * http://www.lua.org/pil/13.4.1.html |
| 2118 | * |
| 2119 | * __newindex |
| 2120 | * |
| 2121 | * Like __index, but control property assignment. |
| 2122 | * |
| 2123 | * __mode - Control weak references. A string value with one or both |
| 2124 | * of the characters 'k' and 'v' which specifies that the the |
| 2125 | * keys and/or values in the table are weak references. |
| 2126 | * |
| 2127 | * __call - Treat a table like a function. When a table is followed by |
| 2128 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 2129 | * a __call key pointing to a function, that function is invoked |
| 2130 | * (passing any specified arguments) and the return value is |
| 2131 | * returned. |
| 2132 | * |
| 2133 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 2134 | * called, if the metatable for myTable has a __metatable |
| 2135 | * key, the value of that key is returned instead of the |
| 2136 | * actual metatable. |
| 2137 | * |
| 2138 | * __tostring - Control string representation. When the builtin |
| 2139 | * "tostring( myTable )" function is called, if the metatable |
| 2140 | * for myTable has a __tostring property set to a function, |
| 2141 | * that function is invoked (passing myTable to it) and the |
| 2142 | * return value is used as the string representation. |
| 2143 | * |
| 2144 | * __len - Control table length. When the table length is requested using |
| 2145 | * the length operator ( '#' ), if the metatable for myTable has |
| 2146 | * a __len key pointing to a function, that function is invoked |
| 2147 | * (passing myTable to it) and the return value used as the value |
| 2148 | * of "#myTable". |
| 2149 | * |
| 2150 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 2151 | * collected, if the metatable has a __gc field pointing to a |
| 2152 | * function, that function is first invoked, passing the userdata |
| 2153 | * to it. The __gc metamethod is not called for tables. |
| 2154 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 2155 | * |
| 2156 | * Special metamethods for redefining standard operators: |
| 2157 | * http://www.lua.org/pil/13.1.html |
| 2158 | * |
| 2159 | * __add "+" |
| 2160 | * __sub "-" |
| 2161 | * __mul "*" |
| 2162 | * __div "/" |
| 2163 | * __unm "!" |
| 2164 | * __pow "^" |
| 2165 | * __concat ".." |
| 2166 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2167 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2168 | * http://www.lua.org/pil/13.2.html |
| 2169 | * |
| 2170 | * __eq "==" |
| 2171 | * __lt "<" |
| 2172 | * __le "<=" |
| 2173 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | |
| 2175 | /* |
| 2176 | * |
| 2177 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2178 | * Class Map |
| 2179 | * |
| 2180 | * |
| 2181 | */ |
| 2182 | |
| 2183 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 2184 | * a class session, otherwise it throws an error. |
| 2185 | */ |
| 2186 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 2187 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2188 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /* This function is the map constructor. It don't need |
| 2192 | * the class Map object. It creates and return a new Map |
| 2193 | * object. It must be called only during "body" or "init" |
| 2194 | * context because it process some filesystem accesses. |
| 2195 | */ |
| 2196 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 2197 | { |
| 2198 | const char *fn; |
| 2199 | int match = PAT_MATCH_STR; |
| 2200 | struct sample_conv conv; |
| 2201 | const char *file = ""; |
| 2202 | int line = 0; |
| 2203 | lua_Debug ar; |
| 2204 | char *err = NULL; |
| 2205 | struct arg args[2]; |
| 2206 | |
| 2207 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 2208 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 2209 | |
| 2210 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2211 | |
| 2212 | if (lua_gettop(L) >= 2) { |
| 2213 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2214 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 2215 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 2216 | } |
| 2217 | |
| 2218 | /* Get Lua filename and line number. */ |
| 2219 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 2220 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 2221 | if (ar.currentline > 0) { /* is there info? */ |
| 2222 | file = ar.short_src; |
| 2223 | line = ar.currentline; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | /* fill fake sample_conv struct. */ |
| 2228 | conv.kw = ""; /* unused. */ |
| 2229 | conv.process = NULL; /* unused. */ |
| 2230 | conv.arg_mask = 0; /* unused. */ |
| 2231 | conv.val_args = NULL; /* unused. */ |
| 2232 | conv.out_type = SMP_T_STR; |
| 2233 | conv.private = (void *)(long)match; |
| 2234 | switch (match) { |
| 2235 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 2236 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 2237 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 2238 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 2239 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 2240 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 2241 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2242 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2243 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 2244 | default: |
| 2245 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 2246 | } |
| 2247 | |
| 2248 | /* fill fake args. */ |
| 2249 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 2250 | args[0].data.str.area = strdup(fn); |
| 2251 | args[0].data.str.data = strlen(fn); |
| 2252 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2253 | args[1].type = ARGT_STOP; |
| 2254 | |
| 2255 | /* load the map. */ |
| 2256 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2257 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2258 | * free the err variable. |
| 2259 | */ |
| 2260 | luaL_where(L, 1); |
| 2261 | lua_pushfstring(L, "'new': %s.", err); |
| 2262 | lua_concat(L, 2); |
| 2263 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2264 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2265 | WILL_LJMP(lua_error(L)); |
| 2266 | } |
| 2267 | |
| 2268 | /* create the lua object. */ |
| 2269 | lua_newtable(L); |
| 2270 | lua_pushlightuserdata(L, args[0].data.map); |
| 2271 | lua_rawseti(L, -2, 0); |
| 2272 | |
| 2273 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 2274 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 2275 | lua_setmetatable(L, -2); |
| 2276 | |
| 2277 | |
| 2278 | return 1; |
| 2279 | } |
| 2280 | |
| 2281 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 2282 | { |
| 2283 | struct map_descriptor *desc; |
| 2284 | struct pattern *pat; |
| 2285 | struct sample smp; |
| 2286 | |
| 2287 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2288 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2289 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2290 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2291 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2292 | } |
| 2293 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2294 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2295 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2296 | 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] | 2297 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2301 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2302 | if (str) |
| 2303 | lua_pushstring(L, ""); |
| 2304 | else |
| 2305 | lua_pushnil(L); |
| 2306 | return 1; |
| 2307 | } |
| 2308 | |
| 2309 | /* 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] | 2310 | 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] | 2311 | return 1; |
| 2312 | } |
| 2313 | |
| 2314 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2315 | { |
| 2316 | return _hlua_map_lookup(L, 0); |
| 2317 | } |
| 2318 | |
| 2319 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2320 | { |
| 2321 | return _hlua_map_lookup(L, 1); |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | * |
| 2326 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | * Class Socket |
| 2328 | * |
| 2329 | * |
| 2330 | */ |
| 2331 | |
| 2332 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2333 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2334 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /* 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] | 2338 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | * received. |
| 2340 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2341 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2342 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2343 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2344 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2346 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
Aurelien DARRAGON | a2c5321 | 2023-05-02 19:10:24 +0200 | [diff] [blame] | 2347 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2348 | notification_wake(&ctx->wake_on_read); |
| 2349 | notification_wake(&ctx->wake_on_write); |
| 2350 | return; |
| 2351 | } |
| 2352 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2353 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2354 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2355 | notification_wake(&ctx->wake_on_read); |
| 2356 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2357 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2358 | } |
| 2359 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2360 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2361 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2362 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2363 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2364 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2365 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2366 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2367 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2368 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2369 | * to be notified whenever the connection completes. |
| 2370 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2371 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2372 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2373 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2374 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2375 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2376 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | |
| 2378 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2379 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2381 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2382 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2383 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
| 2385 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 0dbc16b | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2386 | if (!channel_is_empty(sc_oc(sc))) { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2387 | notification_wake(&ctx->wake_on_read); |
Christopher Faulet | 0dbc16b | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2388 | applet_wont_consume(appctx); |
| 2389 | } |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2390 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2391 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2392 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2393 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2394 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2395 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | } |
| 2397 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2398 | static int hlua_socket_init(struct appctx *appctx) |
| 2399 | { |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 2400 | struct hlua_csk_ctx *csk_ctx = appctx->svcctx; |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2401 | struct stream *s; |
| 2402 | |
| 2403 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2404 | goto error; |
| 2405 | |
| 2406 | s = appctx_strm(appctx); |
| 2407 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2408 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2409 | * and retrieve data from the server. The connection is initialized |
| 2410 | * with the "struct server". |
| 2411 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2412 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2413 | |
| 2414 | /* Force destination server. */ |
| 2415 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Christopher Faulet | 9a278fd | 2023-09-27 17:47:45 +0200 | [diff] [blame] | 2416 | s->target = &csk_ctx->srv->obj_type; |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2417 | |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 2418 | if (csk_ctx->timeout) { |
| 2419 | s->sess->fe->timeout.connect = csk_ctx->timeout; |
| 2420 | s->scf->ioto = csk_ctx->timeout; |
| 2421 | s->scb->ioto = csk_ctx->timeout; |
| 2422 | } |
| 2423 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2424 | return 0; |
| 2425 | |
| 2426 | error: |
| 2427 | return -1; |
| 2428 | } |
| 2429 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2430 | /* This function is called when the "struct stream" is destroyed. |
| 2431 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2432 | * Wake all the pending signals. |
| 2433 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2434 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2436 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | struct xref *peer; |
| 2438 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2439 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2440 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2441 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2442 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | |
| 2444 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2445 | notification_wake(&ctx->wake_on_read); |
| 2446 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2450 | * uses this object. If the stream does not exists, just quit. |
| 2451 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2452 | * pending signal can rest in the read and write lists. destroy |
| 2453 | * it. |
| 2454 | */ |
| 2455 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2456 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2457 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2458 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2459 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2461 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2462 | |
| 2463 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2464 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2465 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2467 | |
| 2468 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2470 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2471 | xref_disconnect(&socket->xref, peer); |
Christopher Faulet | 5b62e09 | 2024-02-16 15:00:54 +0100 | [diff] [blame] | 2472 | |
| 2473 | if (se_fl_test(ctx->appctx->sedesc, SE_FL_ORPHAN)) { |
| 2474 | /* The applet was never initialized, just release it */ |
| 2475 | appctx_free(ctx->appctx); |
| 2476 | } |
| 2477 | else { |
| 2478 | /* Otherwise, notify it that is must die and wake it up */ |
| 2479 | ctx->die = 1; |
| 2480 | appctx_wakeup(ctx->appctx); |
| 2481 | } |
| 2482 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | return 0; |
| 2484 | } |
| 2485 | |
| 2486 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2487 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2489 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2491 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2492 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2494 | struct hlua *hlua; |
| 2495 | |
| 2496 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2497 | hlua = hlua_gethlua(L); |
| 2498 | if (!hlua) |
| 2499 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2501 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2502 | |
| 2503 | /* Check if we run on the same thread than the xreator thread. |
| 2504 | * We cannot access to the socket if the thread is different. |
| 2505 | */ |
| 2506 | if (socket->tid != tid) |
| 2507 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2508 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2509 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2510 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2512 | |
| 2513 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2514 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2516 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2517 | ctx->die = 1; |
| 2518 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2520 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | return 0; |
| 2523 | } |
| 2524 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2525 | /* The close function calls close_helper. |
| 2526 | */ |
| 2527 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2528 | { |
| 2529 | MAY_LJMP(check_args(L, 1, "close")); |
| 2530 | return hlua_socket_close_helper(L); |
| 2531 | } |
| 2532 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | /* This Lua function assumes that the stack contain three parameters. |
| 2534 | * 1 - USERDATA containing a struct socket |
| 2535 | * 2 - INTEGER with values of the macro defined below |
| 2536 | * If the integer is -1, we must read at most one line. |
| 2537 | * If the integer is -2, we ust read all the data until the |
| 2538 | * end of the stream. |
| 2539 | * If the integer is positive value, we must read a number of |
| 2540 | * bytes corresponding to this value. |
| 2541 | */ |
| 2542 | #define HLSR_READ_LINE (-1) |
| 2543 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2544 | __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] | 2545 | { |
| 2546 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2547 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2548 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2549 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2551 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2552 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2553 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2554 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2555 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2556 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2557 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2558 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2559 | struct stream *s; |
| 2560 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2561 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2563 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2564 | hlua = hlua_gethlua(L); |
| 2565 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | /* Check if this lua stack is schedulable. */ |
| 2567 | if (!hlua || !hlua->task) |
| 2568 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2569 | "'frontend', 'backend' or 'task'")); |
| 2570 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2571 | /* Check if we run on the same thread than the xreator thread. |
| 2572 | * We cannot access to the socket if the thread is different. |
| 2573 | */ |
| 2574 | if (socket->tid != tid) |
| 2575 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2576 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2579 | if (!peer) |
| 2580 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2581 | |
| 2582 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Christopher Faulet | 714476c | 2023-09-27 17:22:41 +0200 | [diff] [blame] | 2583 | if (!csk_ctx->connected) |
| 2584 | goto connection_closed; |
| 2585 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2586 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2587 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2589 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2591 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2592 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | if (nblk < 0) /* Connection close. */ |
| 2594 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2595 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2597 | |
| 2598 | /* remove final \r\n. */ |
| 2599 | if (nblk == 1) { |
| 2600 | if (blk1[len1-1] == '\n') { |
| 2601 | len1--; |
| 2602 | skip_at_end++; |
| 2603 | if (blk1[len1-1] == '\r') { |
| 2604 | len1--; |
| 2605 | skip_at_end++; |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | else { |
| 2610 | if (blk2[len2-1] == '\n') { |
| 2611 | len2--; |
| 2612 | skip_at_end++; |
| 2613 | if (blk2[len2-1] == '\r') { |
| 2614 | len2--; |
| 2615 | skip_at_end++; |
| 2616 | } |
| 2617 | } |
| 2618 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2623 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2624 | if (nblk < 0) /* Connection close. */ |
| 2625 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2626 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2627 | goto connection_empty; |
| 2628 | } |
| 2629 | |
| 2630 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2632 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | if (nblk < 0) /* Connection close. */ |
| 2634 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2635 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | goto connection_empty; |
| 2637 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2638 | missing_bytes = wanted - socket->b.n; |
| 2639 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2641 | len1 = missing_bytes; |
| 2642 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2643 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | len = len1; |
| 2647 | |
| 2648 | luaL_addlstring(&socket->b, blk1, len1); |
| 2649 | if (nblk == 2) { |
| 2650 | len += len2; |
| 2651 | luaL_addlstring(&socket->b, blk2, len2); |
| 2652 | } |
| 2653 | |
| 2654 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2655 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | |
| 2657 | /* Don't wait anything. */ |
Christopher Faulet | 0dbc16b | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2658 | applet_will_consume(appctx); |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2659 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | |
| 2661 | /* If the pattern reclaim to read all the data |
| 2662 | * in the connection, got out. |
| 2663 | */ |
| 2664 | if (wanted == HLSR_READ_ALL) |
| 2665 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2666 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | goto connection_empty; |
| 2668 | |
| 2669 | /* Return result. */ |
| 2670 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2671 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | return 1; |
| 2673 | |
| 2674 | connection_closed: |
| 2675 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2676 | xref_unlock(&socket->xref, peer); |
| 2677 | |
| 2678 | no_peer: |
| 2679 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2680 | /* If the buffer containds data. */ |
| 2681 | if (socket->b.n > 0) { |
| 2682 | luaL_pushresult(&socket->b); |
| 2683 | return 1; |
| 2684 | } |
| 2685 | lua_pushnil(L); |
| 2686 | lua_pushstring(L, "connection closed."); |
| 2687 | return 2; |
| 2688 | |
| 2689 | connection_empty: |
| 2690 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2691 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2694 | } |
| 2695 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2696 | 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] | 2697 | return 0; |
| 2698 | } |
| 2699 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2700 | /* This Lua function gets two parameters. The first one can be string |
| 2701 | * or a number. If the string is "*l", the user requires one line. If |
| 2702 | * 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] | 2703 | * If the value is a number, the user require a number of bytes equal |
| 2704 | * to the value. The default value is "*l" (a line). |
| 2705 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2706 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | * integer takes this values: |
| 2708 | * -1 : read a line |
| 2709 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2710 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2712 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | * concatenated with the read data. |
| 2714 | */ |
| 2715 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2716 | { |
| 2717 | int wanted = HLSR_READ_LINE; |
| 2718 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2719 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | char *error; |
| 2721 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2722 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | |
| 2724 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2725 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2726 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2727 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2729 | /* Check if we run on the same thread than the xreator thread. |
| 2730 | * We cannot access to the socket if the thread is different. |
| 2731 | */ |
| 2732 | if (socket->tid != tid) |
| 2733 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2734 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | /* check for pattern. */ |
| 2736 | if (lua_gettop(L) >= 2) { |
| 2737 | type = lua_type(L, 2); |
| 2738 | if (type == LUA_TSTRING) { |
| 2739 | pattern = lua_tostring(L, 2); |
| 2740 | if (strcmp(pattern, "*a") == 0) |
| 2741 | wanted = HLSR_READ_ALL; |
| 2742 | else if (strcmp(pattern, "*l") == 0) |
| 2743 | wanted = HLSR_READ_LINE; |
| 2744 | else { |
| 2745 | wanted = strtoll(pattern, &error, 10); |
| 2746 | if (*error != '\0') |
| 2747 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2748 | } |
| 2749 | } |
| 2750 | else if (type == LUA_TNUMBER) { |
| 2751 | wanted = lua_tointeger(L, 2); |
| 2752 | if (wanted < 0) |
| 2753 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | /* Set pattern. */ |
| 2758 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2759 | |
| 2760 | /* Check if we would replace the top by itself. */ |
| 2761 | if (lua_gettop(L) != 2) |
| 2762 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2763 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2764 | /* Save index of the top of the stack because since buffers are used, it |
| 2765 | * may change |
| 2766 | */ |
| 2767 | lastarg = lua_gettop(L); |
| 2768 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2769 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | luaL_buffinit(L, &socket->b); |
| 2771 | |
| 2772 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2773 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2774 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2775 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2776 | pattern = lua_tolstring(L, 3, &len); |
| 2777 | luaL_addlstring(&socket->b, pattern, len); |
| 2778 | } |
| 2779 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2780 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2784 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2785 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2786 | 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] | 2787 | { |
| 2788 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2789 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2790 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2791 | struct appctx *appctx; |
| 2792 | size_t buf_len; |
| 2793 | const char *buf; |
| 2794 | int len; |
| 2795 | int send_len; |
| 2796 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2797 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2798 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2799 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2800 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2801 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2802 | hlua = hlua_gethlua(L); |
| 2803 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2804 | /* Check if this lua stack is schedulable. */ |
| 2805 | if (!hlua || !hlua->task) |
| 2806 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2807 | "'frontend', 'backend' or 'task'")); |
| 2808 | |
| 2809 | /* Get object */ |
| 2810 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2811 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2812 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2813 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2814 | /* Check if we run on the same thread than the xreator thread. |
| 2815 | * We cannot access to the socket if the thread is different. |
| 2816 | */ |
| 2817 | if (socket->tid != tid) |
| 2818 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2819 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2820 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2821 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2822 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2823 | lua_pushinteger(L, -1); |
| 2824 | return 1; |
| 2825 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2826 | |
| 2827 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Christopher Faulet | 714476c | 2023-09-27 17:22:41 +0200 | [diff] [blame] | 2828 | if (!csk_ctx->connected) { |
| 2829 | xref_unlock(&socket->xref, peer); |
| 2830 | lua_pushinteger(L, -1); |
| 2831 | return 1; |
| 2832 | } |
| 2833 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2834 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2835 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2836 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2837 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2838 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2839 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2840 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2841 | lua_pushinteger(L, -1); |
| 2842 | return 1; |
| 2843 | } |
| 2844 | |
| 2845 | /* Update the input buffer data. */ |
| 2846 | buf += sent; |
| 2847 | send_len = buf_len - sent; |
| 2848 | |
| 2849 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2850 | if (sent >= buf_len) { |
| 2851 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2852 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2853 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2855 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2856 | * the request buffer if its not required. |
| 2857 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2858 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2859 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2860 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2861 | } |
| 2862 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2863 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2864 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2865 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2866 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2867 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2868 | |
| 2869 | /* send data */ |
| 2870 | if (len < send_len) |
| 2871 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2872 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2873 | |
| 2874 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2875 | * the data" (-2) are not expected because the available length |
| 2876 | * is tested. |
| 2877 | * Other unknown error are also not expected. |
| 2878 | */ |
| 2879 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2880 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2881 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2882 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2883 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2884 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2885 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2886 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2887 | return 1; |
| 2888 | } |
| 2889 | |
| 2890 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2891 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2892 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2893 | /* Update length sent. */ |
| 2894 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2895 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2896 | |
| 2897 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2898 | if (sent + len >= buf_len) { |
| 2899 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2900 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2901 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2902 | |
| 2903 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2904 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2905 | xref_unlock(&socket->xref, peer); |
| 2906 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2907 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2908 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2909 | 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] | 2910 | return 0; |
| 2911 | } |
| 2912 | |
| 2913 | /* This function initiate the send of data. It just check the input |
| 2914 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2915 | * 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] | 2916 | * "hlua_socket_write_yield" that can yield. |
| 2917 | * |
| 2918 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2919 | * the associated object. The second is a string buffer. The third is |
| 2920 | * a facultative integer that represents where is the buffer position |
| 2921 | * of the start of the data that can send. The first byte is the |
| 2922 | * position "1". The default value is "1". The fourth argument is a |
| 2923 | * facultative integer that represents where is the buffer position |
| 2924 | * of the end of the data that can send. The default is the last byte. |
| 2925 | */ |
| 2926 | static int hlua_socket_send(struct lua_State *L) |
| 2927 | { |
| 2928 | int i; |
| 2929 | int j; |
| 2930 | const char *buf; |
| 2931 | size_t buf_len; |
| 2932 | |
| 2933 | /* Check number of arguments. */ |
| 2934 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2935 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2936 | |
| 2937 | /* Get the string. */ |
| 2938 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2939 | |
| 2940 | /* Get and check j. */ |
| 2941 | if (lua_gettop(L) == 4) { |
| 2942 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2943 | if (j < 0) |
| 2944 | j = buf_len + j + 1; |
| 2945 | if (j > buf_len) |
| 2946 | j = buf_len + 1; |
| 2947 | lua_pop(L, 1); |
| 2948 | } |
| 2949 | else |
| 2950 | j = buf_len; |
| 2951 | |
| 2952 | /* Get and check i. */ |
| 2953 | if (lua_gettop(L) == 3) { |
| 2954 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2955 | if (i < 0) |
| 2956 | i = buf_len + i + 1; |
| 2957 | if (i > buf_len) |
| 2958 | i = buf_len + 1; |
| 2959 | lua_pop(L, 1); |
| 2960 | } else |
| 2961 | i = 1; |
| 2962 | |
| 2963 | /* Check bth i and j. */ |
| 2964 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2965 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2966 | return 1; |
| 2967 | } |
| 2968 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2969 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | return 1; |
| 2971 | } |
| 2972 | if (i == 0) |
| 2973 | i = 1; |
| 2974 | if (j == 0) |
| 2975 | j = 1; |
| 2976 | |
| 2977 | /* Pop the string. */ |
| 2978 | lua_pop(L, 1); |
| 2979 | |
| 2980 | /* Update the buffer length. */ |
| 2981 | buf += i - 1; |
| 2982 | buf_len = j - i + 1; |
| 2983 | lua_pushlstring(L, buf, buf_len); |
| 2984 | |
| 2985 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2986 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2987 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2988 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2989 | } |
| 2990 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2991 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2992 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2993 | { |
| 2994 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2995 | int ret; |
| 2996 | int len; |
| 2997 | char *p; |
| 2998 | |
| 2999 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 3000 | if (ret <= 0) { |
| 3001 | lua_pushnil(L); |
| 3002 | return 1; |
| 3003 | } |
| 3004 | |
| 3005 | if (ret == AF_UNIX) { |
| 3006 | lua_pushstring(L, buffer+1); |
| 3007 | return 1; |
| 3008 | } |
| 3009 | else if (ret == AF_INET6) { |
| 3010 | buffer[0] = '['; |
| 3011 | len = strlen(buffer); |
| 3012 | buffer[len] = ']'; |
| 3013 | len++; |
| 3014 | buffer[len] = ':'; |
| 3015 | len++; |
| 3016 | p = buffer; |
| 3017 | } |
| 3018 | else if (ret == AF_INET) { |
| 3019 | p = buffer + 1; |
| 3020 | len = strlen(p); |
| 3021 | p[len] = ':'; |
| 3022 | len++; |
| 3023 | } |
| 3024 | else { |
| 3025 | lua_pushnil(L); |
| 3026 | return 1; |
| 3027 | } |
| 3028 | |
| 3029 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 3030 | lua_pushnil(L); |
| 3031 | return 1; |
| 3032 | } |
| 3033 | |
| 3034 | lua_pushstring(L, p); |
| 3035 | return 1; |
| 3036 | } |
| 3037 | |
| 3038 | /* Returns information about the peer of the connection. */ |
| 3039 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 3040 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3041 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3042 | struct xref *peer; |
Christopher Faulet | 714476c | 2023-09-27 17:22:41 +0200 | [diff] [blame] | 3043 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3044 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3045 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 3046 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3047 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3048 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3049 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 3050 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3051 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3052 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3053 | /* Check if we run on the same thread than the xreator thread. |
| 3054 | * We cannot access to the socket if the thread is different. |
| 3055 | */ |
| 3056 | if (socket->tid != tid) |
| 3057 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3058 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3059 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3060 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3061 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3062 | lua_pushnil(L); |
| 3063 | return 1; |
| 3064 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3065 | |
Christopher Faulet | 714476c | 2023-09-27 17:22:41 +0200 | [diff] [blame] | 3066 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3067 | if (!csk_ctx->connected) { |
| 3068 | xref_unlock(&socket->xref, peer); |
| 3069 | lua_pushnil(L); |
| 3070 | return 1; |
| 3071 | } |
| 3072 | |
| 3073 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 3074 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3075 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 3076 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3077 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 3078 | lua_pushnil(L); |
| 3079 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 3082 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3083 | xref_unlock(&socket->xref, peer); |
| 3084 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | /* Returns information about my connection side. */ |
| 3088 | static int hlua_socket_getsockname(struct lua_State *L) |
| 3089 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3090 | struct hlua_socket *socket; |
| 3091 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3092 | struct appctx *appctx; |
| 3093 | struct xref *peer; |
Christopher Faulet | 714476c | 2023-09-27 17:22:41 +0200 | [diff] [blame] | 3094 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3095 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3096 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3097 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3098 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 3099 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3100 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3101 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3102 | /* Check if we run on the same thread than the xreator thread. |
| 3103 | * We cannot access to the socket if the thread is different. |
| 3104 | */ |
| 3105 | if (socket->tid != tid) |
| 3106 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3107 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3108 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3109 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3110 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3111 | lua_pushnil(L); |
| 3112 | return 1; |
| 3113 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3114 | |
Christopher Faulet | 714476c | 2023-09-27 17:22:41 +0200 | [diff] [blame] | 3115 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3116 | if (!csk_ctx->connected) { |
| 3117 | xref_unlock(&socket->xref, peer); |
| 3118 | lua_pushnil(L); |
| 3119 | return 1; |
| 3120 | } |
| 3121 | |
| 3122 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3123 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3124 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 3125 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 3126 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3127 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3128 | lua_pushnil(L); |
| 3129 | return 1; |
| 3130 | } |
| 3131 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 3132 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3133 | xref_unlock(&socket->xref, peer); |
| 3134 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 3138 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3139 | .obj_type = OBJ_TYPE_APPLET, |
| 3140 | .name = "<LUA_TCP>", |
| 3141 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3142 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3143 | .release = hlua_socket_release, |
| 3144 | }; |
| 3145 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3146 | __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] | 3147 | { |
| 3148 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3149 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3150 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3151 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3152 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3153 | struct stream *s; |
| 3154 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3155 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3156 | hlua = hlua_gethlua(L); |
| 3157 | if (!hlua) |
| 3158 | return 0; |
| 3159 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3160 | /* Check if we run on the same thread than the xreator thread. |
| 3161 | * We cannot access to the socket if the thread is different. |
| 3162 | */ |
| 3163 | if (socket->tid != tid) |
| 3164 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3165 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3166 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3167 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3168 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3169 | lua_pushnil(L); |
| 3170 | lua_pushstring(L, "Can't connect"); |
| 3171 | return 2; |
| 3172 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3173 | |
| 3174 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3175 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3176 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3177 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3178 | /* Check if we run on the same thread than the xreator thread. |
| 3179 | * We cannot access to the socket if the thread is different. |
| 3180 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3181 | if (socket->tid != tid) { |
| 3182 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3183 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3184 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3185 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3186 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3187 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3188 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3189 | lua_pushnil(L); |
| 3190 | lua_pushstring(L, "Can't connect"); |
| 3191 | return 2; |
| 3192 | } |
| 3193 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 3194 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3195 | |
| 3196 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3197 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3198 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3199 | lua_pushinteger(L, 1); |
| 3200 | return 1; |
| 3201 | } |
| 3202 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3203 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3204 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3205 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3206 | } |
| 3207 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3208 | 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] | 3209 | return 0; |
| 3210 | } |
| 3211 | |
| 3212 | /* This function fail or initite the connection. */ |
| 3213 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 3214 | { |
| 3215 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3216 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3217 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3218 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3219 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3220 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3221 | int low, high; |
| 3222 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3223 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3224 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3225 | |
Christopher Faulet | 6db178d | 2023-09-27 17:42:38 +0200 | [diff] [blame] | 3226 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3227 | hlua = hlua_gethlua(L); |
| 3228 | if (!hlua) |
| 3229 | return 0; |
| 3230 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3231 | if (lua_gettop(L) < 2) |
| 3232 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3233 | |
| 3234 | /* Get args. */ |
| 3235 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3236 | |
| 3237 | /* Check if we run on the same thread than the xreator thread. |
| 3238 | * We cannot access to the socket if the thread is different. |
| 3239 | */ |
| 3240 | if (socket->tid != tid) |
| 3241 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3242 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3243 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3244 | if (lua_gettop(L) >= 3) { |
| 3245 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3246 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3247 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3248 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 3249 | * that are not enclosed within square brackets. |
| 3250 | */ |
| 3251 | if (port > 0) { |
| 3252 | luaL_buffinit(L, &b); |
| 3253 | luaL_addstring(&b, ip); |
| 3254 | luaL_addchar(&b, ':'); |
| 3255 | luaL_pushresult(&b); |
| 3256 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 3257 | } |
| 3258 | } |
| 3259 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3260 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3261 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3262 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3263 | lua_pushnil(L); |
| 3264 | return 1; |
| 3265 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3266 | |
Christopher Faulet | 2678657 | 2023-09-27 17:39:44 +0200 | [diff] [blame] | 3267 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3268 | if (!csk_ctx->srv) |
| 3269 | csk_ctx->srv = socket_tcp; |
| 3270 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3271 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 3272 | 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] | 3273 | if (!addr) { |
| 3274 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3275 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3276 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 3277 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3278 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3279 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3280 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3281 | if (port == -1) { |
| 3282 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3283 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3284 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3285 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 3286 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3287 | if (port == -1) { |
| 3288 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3289 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3290 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3291 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3292 | } |
| 3293 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3294 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3295 | appctx = csk_ctx->appctx; |
Christopher Faulet | 9a278fd | 2023-09-27 17:47:45 +0200 | [diff] [blame] | 3296 | if (appctx_sc(appctx)) { |
| 3297 | xref_unlock(&socket->xref, peer); |
| 3298 | WILL_LJMP(luaL_error(L, "connect: connect already performed\n")); |
| 3299 | } |
| 3300 | |
| 3301 | if (appctx_init(appctx) == -1) { |
| 3302 | xref_unlock(&socket->xref, peer); |
| 3303 | WILL_LJMP(luaL_error(L, "connect: fail to init applet.")); |
| 3304 | } |
| 3305 | |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 3306 | sc = appctx_sc(appctx); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3307 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3308 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3309 | xref_unlock(&socket->xref, peer); |
| 3310 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 3311 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3312 | |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3313 | /* inform the stream that we want to be notified whenever the |
| 3314 | * connection completes. |
| 3315 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 3316 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 3317 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 3318 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3319 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3320 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3321 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3322 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3323 | } |
| 3324 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 3325 | |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 3326 | /* Return yield waiting for connection. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3327 | 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] | 3328 | |
| 3329 | return 0; |
| 3330 | } |
| 3331 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3332 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3333 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 3334 | { |
| 3335 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3336 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3337 | |
| 3338 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 3339 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3340 | |
| 3341 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3342 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3343 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3344 | lua_pushnil(L); |
| 3345 | return 1; |
| 3346 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3347 | |
Christopher Faulet | 2678657 | 2023-09-27 17:39:44 +0200 | [diff] [blame] | 3348 | container_of(peer, struct hlua_csk_ctx, xref)->srv = socket_ssl; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3349 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3350 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3351 | return MAY_LJMP(hlua_socket_connect(L)); |
| 3352 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3353 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3354 | |
| 3355 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3356 | { |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
| 3360 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3361 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3362 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3363 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3364 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3365 | struct xref *peer; |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 3366 | struct hlua_csk_ctx *csk_ctx; |
| 3367 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3368 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3369 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3370 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3371 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3372 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3373 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3374 | /* convert the timeout to millis */ |
| 3375 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3376 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3377 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3378 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3379 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3380 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3381 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3382 | 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] | 3383 | |
| 3384 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3385 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3386 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3387 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3388 | /* Check if we run on the same thread than the xreator thread. |
| 3389 | * We cannot access to the socket if the thread is different. |
| 3390 | */ |
| 3391 | if (socket->tid != tid) |
| 3392 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3393 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3394 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3395 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3396 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3397 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3398 | WILL_LJMP(lua_error(L)); |
| 3399 | return 0; |
| 3400 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3401 | |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 3402 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3403 | csk_ctx->timeout = tmout; |
| 3404 | |
| 3405 | appctx = csk_ctx->appctx; |
| 3406 | if (!appctx_sc(appctx)) |
| 3407 | goto end; |
| 3408 | |
| 3409 | s = appctx_strm(csk_ctx->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3410 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3411 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3412 | s->scf->ioto = tmout; |
| 3413 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3414 | |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 3415 | s->task->expire = (tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire); |
| 3416 | s->task->expire = tick_first(s->task->expire, tick_add_ifset(now_ms, tmout)); |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3417 | task_queue(s->task); |
| 3418 | |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 3419 | end: |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3420 | xref_unlock(&socket->xref, peer); |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3421 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3422 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3426 | { |
| 3427 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3428 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3429 | struct appctx *appctx; |
Christopher Faulet | 5b62e09 | 2024-02-16 15:00:54 +0100 | [diff] [blame] | 3430 | struct hlua *hlua; |
| 3431 | |
| 3432 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3433 | hlua = hlua_gethlua(L); |
| 3434 | if (!hlua) |
| 3435 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3436 | |
| 3437 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3438 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3439 | hlua_pusherror(L, "socket: full stack"); |
| 3440 | goto out_fail_conf; |
| 3441 | } |
| 3442 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3443 | /* Create the object: obj[0] = userdata. */ |
| 3444 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3445 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3446 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3447 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3448 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3449 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3450 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3451 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3452 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3453 | goto out_fail_conf; |
| 3454 | } |
| 3455 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3456 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3457 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3458 | lua_setmetatable(L, -2); |
| 3459 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3460 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3461 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3462 | if (!appctx) { |
| 3463 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3464 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3465 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3466 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3467 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3468 | ctx->die = 0; |
Christopher Faulet | 2678657 | 2023-09-27 17:39:44 +0200 | [diff] [blame] | 3469 | ctx->srv = NULL; |
Christopher Faulet | cb06855 | 2023-09-27 17:34:24 +0200 | [diff] [blame] | 3470 | ctx->timeout = 0; |
Christopher Faulet | 75d1bfb | 2023-09-27 17:17:48 +0200 | [diff] [blame] | 3471 | ctx->appctx = appctx; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3472 | LIST_INIT(&ctx->wake_on_write); |
| 3473 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3474 | |
Christopher Faulet | 5b62e09 | 2024-02-16 15:00:54 +0100 | [diff] [blame] | 3475 | hlua->gc_count++; |
| 3476 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3477 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3478 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3479 | return 1; |
| 3480 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3481 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3482 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3483 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3484 | WILL_LJMP(lua_error(L)); |
| 3485 | return 0; |
| 3486 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3487 | |
| 3488 | /* |
| 3489 | * |
| 3490 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3491 | * Class Channel |
| 3492 | * |
| 3493 | * |
| 3494 | */ |
| 3495 | |
| 3496 | /* Returns the struct hlua_channel join to the class channel in the |
| 3497 | * stack entry "ud" or throws an argument error. |
| 3498 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3499 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3500 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3501 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3502 | } |
| 3503 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3504 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3505 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3506 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3507 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3508 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3509 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3510 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3511 | return 0; |
| 3512 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3513 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3514 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3515 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3516 | |
| 3517 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3518 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3519 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3520 | return 1; |
| 3521 | } |
| 3522 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3523 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3524 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3525 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3526 | * initialized. |
| 3527 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3528 | static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3529 | { |
| 3530 | struct filter *filter = NULL; |
| 3531 | |
| 3532 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3533 | struct hlua_flt_ctx *flt_ctx; |
| 3534 | |
| 3535 | filter = lua_touserdata (L, -1); |
| 3536 | flt_ctx = filter->ctx; |
| 3537 | if (hlua_filter_from_payload(filter)) { |
| 3538 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3539 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | lua_pop(L, 1); |
| 3544 | return filter; |
| 3545 | } |
| 3546 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3547 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3548 | * 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] | 3549 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3550 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3551 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3552 | */ |
| 3553 | 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] | 3554 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3555 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3556 | luaL_Buffer b; |
| 3557 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3558 | block1 = len; |
| 3559 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3560 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3561 | block2 = len - block1; |
| 3562 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3563 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3564 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3565 | if (block2) |
| 3566 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3567 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3568 | return len; |
| 3569 | } |
| 3570 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3571 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3572 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3573 | * number of bytes copied. |
| 3574 | */ |
| 3575 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3576 | { |
| 3577 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3578 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3579 | /* Nothing to do, just return */ |
| 3580 | if (unlikely(istlen(str) == 0)) |
| 3581 | goto end; |
| 3582 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3583 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3584 | ret = -1; |
| 3585 | goto end; |
| 3586 | } |
| 3587 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3588 | |
| 3589 | end: |
| 3590 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3591 | } |
| 3592 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3593 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3594 | */ |
| 3595 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3596 | { |
| 3597 | size_t end = offset + len; |
| 3598 | |
| 3599 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3600 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3601 | b_data(&chn->buf) - end, -len); |
| 3602 | b_sub(&chn->buf, len); |
| 3603 | } |
| 3604 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3605 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3606 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3607 | * offset by default). If there is not enough input data and more data can be |
| 3608 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3609 | * |
| 3610 | * From an action, All input data are considered. For a filter, the offset and |
| 3611 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3612 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3613 | __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] | 3614 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3615 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3616 | struct filter *filter; |
| 3617 | size_t input, output; |
| 3618 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3619 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3620 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3621 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3622 | output = co_data(chn); |
| 3623 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3624 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3625 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3626 | if (filter && !hlua_filter_from_payload(filter)) |
| 3627 | WILL_LJMP(lua_error(L)); |
| 3628 | |
| 3629 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3630 | if (lua_gettop(L) > 1) { |
| 3631 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3632 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3633 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3634 | offset += output; |
| 3635 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3636 | lua_pushfstring(L, "offset out of range."); |
| 3637 | WILL_LJMP(lua_error(L)); |
| 3638 | } |
| 3639 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3640 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3641 | if (lua_gettop(L) == 3) { |
| 3642 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3643 | if (!len) |
| 3644 | goto dup; |
| 3645 | if (len == -1) |
| 3646 | len = global.tune.bufsize; |
| 3647 | if (len < 0) { |
| 3648 | lua_pushfstring(L, "length out of range."); |
| 3649 | WILL_LJMP(lua_error(L)); |
| 3650 | } |
| 3651 | } |
| 3652 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3653 | /* Wait for more data if possible if no length was specified and there |
| 3654 | * is no data or not enough data was received. |
| 3655 | */ |
| 3656 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3657 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3658 | /* Yield waiting for more data, as requested */ |
| 3659 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3660 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3661 | |
| 3662 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3663 | if (!len) { |
| 3664 | lua_pushnil(L); |
| 3665 | return -1; |
| 3666 | } |
| 3667 | |
| 3668 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3669 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3673 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3674 | return 1; |
| 3675 | } |
| 3676 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3677 | /* Copies the first line (including the trailing LF) of input data in the |
| 3678 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3679 | * a length (all remaining input data starting for the offset by default). If |
| 3680 | * there is not enough input data and more data can be received, the function |
| 3681 | * yields. If a length is explicitly specified, no more data are |
| 3682 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3683 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3684 | * |
| 3685 | * From an action, All input data are considered. For a filter, the offset and |
| 3686 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3687 | */ |
| 3688 | __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] | 3689 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3690 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3691 | struct filter *filter; |
| 3692 | size_t l, input, output; |
| 3693 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3694 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3695 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3696 | output = co_data(chn); |
| 3697 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3698 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3699 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3700 | if (filter && !hlua_filter_from_payload(filter)) |
| 3701 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3702 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3703 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3704 | if (lua_gettop(L) > 1) { |
| 3705 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3706 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3707 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3708 | offset += output; |
| 3709 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3710 | lua_pushfstring(L, "offset out of range."); |
| 3711 | WILL_LJMP(lua_error(L)); |
| 3712 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3713 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3714 | |
| 3715 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3716 | if (lua_gettop(L) == 3) { |
| 3717 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3718 | if (!len) |
| 3719 | goto dup; |
| 3720 | if (len == -1) |
| 3721 | len = global.tune.bufsize; |
| 3722 | if (len < 0) { |
| 3723 | lua_pushfstring(L, "length out of range."); |
| 3724 | WILL_LJMP(lua_error(L)); |
| 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3729 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3730 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3731 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3732 | len = l+1; |
| 3733 | goto dup; |
| 3734 | } |
| 3735 | } |
| 3736 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3737 | /* Wait for more data if possible if no line is found and no length was |
| 3738 | * specified or not enough data was received. |
| 3739 | */ |
| 3740 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3741 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3742 | /* Yield waiting for more data */ |
| 3743 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3744 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3745 | |
| 3746 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3747 | if (!len) { |
| 3748 | lua_pushnil(L); |
| 3749 | return -1; |
| 3750 | } |
| 3751 | |
| 3752 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3753 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3757 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3758 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3759 | } |
| 3760 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3761 | /* [ DEPRECATED ] |
| 3762 | * |
| 3763 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3764 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3765 | * |
| 3766 | * From an action, All input data are considered. For a filter, the offset and |
| 3767 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3768 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3769 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3770 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3771 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3772 | struct filter *filter; |
| 3773 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3774 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3775 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3776 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3777 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3778 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3779 | WILL_LJMP(lua_error(L)); |
| 3780 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3781 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3782 | offset = co_data(chn); |
| 3783 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3784 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3785 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3786 | if (filter && !hlua_filter_from_payload(filter)) |
| 3787 | WILL_LJMP(lua_error(L)); |
| 3788 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3789 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3790 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3791 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3792 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3793 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3794 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3795 | return 1; |
| 3796 | } |
| 3797 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3798 | /* [ DEPRECATED ] |
| 3799 | * |
| 3800 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3801 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3802 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3803 | * |
| 3804 | * From an action, All input data are considered. For a filter, the offset and |
| 3805 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3806 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3807 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3808 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3809 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3810 | struct filter *filter; |
| 3811 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3812 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3813 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3814 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3815 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3816 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3817 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3818 | WILL_LJMP(lua_error(L)); |
| 3819 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3820 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3821 | offset = co_data(chn); |
| 3822 | len = ci_data(chn); |
| 3823 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3824 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3825 | if (filter && !hlua_filter_from_payload(filter)) |
| 3826 | WILL_LJMP(lua_error(L)); |
| 3827 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3828 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3829 | lua_pushnil(L); |
| 3830 | return 1; |
| 3831 | } |
| 3832 | |
| 3833 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3834 | _hlua_channel_delete(chn, offset, ret); |
| 3835 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3836 | } |
| 3837 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3838 | /* This functions consumes and returns one line. If the channel is closed, |
| 3839 | * 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] | 3840 | * 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] | 3841 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3842 | * |
| 3843 | * From an action, All input data are considered. For a filter, the offset and |
| 3844 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3845 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3846 | __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] | 3847 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3848 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3849 | struct filter *filter; |
| 3850 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3851 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3852 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3853 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3854 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3855 | offset = co_data(chn); |
| 3856 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3857 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3858 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3859 | if (filter && !hlua_filter_from_payload(filter)) |
| 3860 | WILL_LJMP(lua_error(L)); |
| 3861 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3862 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3863 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3864 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | for (l = 0; l < len; l++) { |
| 3868 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3869 | len = l+1; |
| 3870 | goto dup; |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3875 | /* Yield waiting for more data */ |
| 3876 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3877 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3878 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3879 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3880 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3881 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3882 | return 1; |
| 3883 | } |
| 3884 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3885 | /* [ DEPRECATED ] |
| 3886 | * |
| 3887 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3888 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3889 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3890 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3891 | struct channel *chn; |
| 3892 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3893 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3894 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3895 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3896 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3897 | WILL_LJMP(lua_error(L)); |
| 3898 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3899 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3900 | } |
| 3901 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3902 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3903 | * available input data are returned. The offset may be negactive to start from |
| 3904 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3905 | * size. |
| 3906 | */ |
| 3907 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3908 | { |
| 3909 | struct channel *chn; |
| 3910 | |
| 3911 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3912 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3913 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3914 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3915 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3916 | WILL_LJMP(lua_error(L)); |
| 3917 | } |
| 3918 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3919 | } |
| 3920 | |
| 3921 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3922 | * available input data are returned. The offset may be negactive to start from |
| 3923 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3924 | * size. |
| 3925 | */ |
| 3926 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3927 | { |
| 3928 | struct channel *chn; |
| 3929 | |
| 3930 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3931 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3932 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3933 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3934 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3935 | WILL_LJMP(lua_error(L)); |
| 3936 | } |
| 3937 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3938 | } |
| 3939 | |
| 3940 | /* Appends a string into the input side of channel. It returns the length of the |
| 3941 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3942 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3943 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3944 | * |
| 3945 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3946 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3947 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3948 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3949 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3950 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3951 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3952 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3953 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3954 | |
| 3955 | MAY_LJMP(check_args(L, 2, "append")); |
| 3956 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3957 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3958 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3959 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3960 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3961 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3962 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3963 | offset = co_data(chn); |
| 3964 | len = ci_data(chn); |
| 3965 | |
| 3966 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3967 | if (filter && !hlua_filter_from_payload(filter)) |
| 3968 | WILL_LJMP(lua_error(L)); |
| 3969 | |
| 3970 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3971 | if (ret > 0 && filter) { |
| 3972 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3973 | |
| 3974 | flt_update_offsets(filter, chn, ret); |
| 3975 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3976 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3977 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3978 | return 1; |
| 3979 | } |
| 3980 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3981 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3982 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3983 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3984 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3985 | * |
| 3986 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3987 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3988 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3989 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3990 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3991 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3992 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3993 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3994 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3995 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3996 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3997 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3998 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3999 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4000 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4001 | WILL_LJMP(lua_error(L)); |
| 4002 | } |
| 4003 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4004 | offset = co_data(chn); |
| 4005 | len = ci_data(chn); |
| 4006 | |
| 4007 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4008 | if (filter && !hlua_filter_from_payload(filter)) |
| 4009 | WILL_LJMP(lua_error(L)); |
| 4010 | |
| 4011 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 4012 | if (ret > 0 && filter) { |
| 4013 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4014 | |
| 4015 | flt_update_offsets(filter, chn, ret); |
| 4016 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 4017 | } |
| 4018 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4019 | lua_pushinteger(L, ret); |
| 4020 | return 1; |
| 4021 | } |
| 4022 | |
| 4023 | /* Inserts a given amount of input data at the given offset by a string |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 4024 | * content. By default the string is appended in front of input data. It |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4025 | * returns the length of the written string, or -1 if the channel is closed or |
| 4026 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4027 | * |
| 4028 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4029 | */ |
| 4030 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 4031 | { |
| 4032 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4033 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4034 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4035 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4036 | int ret, offset; |
| 4037 | |
| 4038 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 4039 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 4040 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4041 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4042 | |
| 4043 | output = co_data(chn); |
| 4044 | input = ci_data(chn); |
| 4045 | |
| 4046 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4047 | if (filter && !hlua_filter_from_payload(filter)) |
| 4048 | WILL_LJMP(lua_error(L)); |
| 4049 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 4050 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4051 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4052 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4053 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 4054 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4055 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 4056 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4057 | lua_pushfstring(L, "offset out of range."); |
| 4058 | WILL_LJMP(lua_error(L)); |
| 4059 | } |
| 4060 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4061 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4062 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4063 | WILL_LJMP(lua_error(L)); |
| 4064 | } |
| 4065 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4066 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 4067 | if (ret > 0 && filter) { |
| 4068 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4069 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4070 | flt_update_offsets(filter, chn, ret); |
| 4071 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4072 | } |
| 4073 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4074 | lua_pushinteger(L, ret); |
| 4075 | return 1; |
| 4076 | } |
| 4077 | /* Replaces a given amount of input data at the given offset by a string |
| 4078 | * content. By default all remaining data are removed (offset = 0 and len = |
| 4079 | * -1). It returns the length of the written string, or -1 if the channel is |
| 4080 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4081 | * |
| 4082 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4083 | */ |
| 4084 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 4085 | { |
| 4086 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4087 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4088 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4089 | size_t sz, input, output; |
| 4090 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4091 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4092 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 4093 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 4094 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4095 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4096 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 4097 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4098 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4099 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4100 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4101 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4102 | output = co_data(chn); |
| 4103 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4104 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4105 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4106 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4107 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4108 | |
| 4109 | offset = output; |
| 4110 | if (lua_gettop(L) > 2) { |
| 4111 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4112 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 4113 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4114 | offset += output; |
| 4115 | if (offset < output || offset > input + output) { |
| 4116 | lua_pushfstring(L, "offset out of range."); |
| 4117 | WILL_LJMP(lua_error(L)); |
| 4118 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4121 | len = output + input - offset; |
| 4122 | if (lua_gettop(L) == 4) { |
| 4123 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 4124 | if (!len) |
| 4125 | goto set; |
| 4126 | if (len == -1) |
| 4127 | len = output + input - offset; |
| 4128 | if (len < 0 || offset + len > output + input) { |
| 4129 | lua_pushfstring(L, "length out of range."); |
| 4130 | WILL_LJMP(lua_error(L)); |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 4135 | /* 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] | 4136 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 4137 | lua_pushinteger(L, -1); |
| 4138 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4139 | _hlua_channel_delete(chn, offset, len); |
| 4140 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 4141 | if (filter) { |
| 4142 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4143 | |
| 4144 | len -= (ret > 0 ? ret : 0); |
| 4145 | flt_update_offsets(filter, chn, -len); |
| 4146 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 4147 | } |
| 4148 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4149 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 4150 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4151 | return 1; |
| 4152 | } |
| 4153 | |
| 4154 | /* Removes a given amount of input data at the given offset. By default all |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4155 | * input data are removed (offset = 0 and len = -1). It returns the amount of |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4156 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4157 | * |
| 4158 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4159 | */ |
| 4160 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 4161 | { |
| 4162 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4163 | struct filter *filter; |
| 4164 | size_t input, output; |
| 4165 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4166 | |
| 4167 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 4168 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 4169 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4170 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4171 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4172 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4173 | WILL_LJMP(lua_error(L)); |
| 4174 | } |
| 4175 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4176 | output = co_data(chn); |
| 4177 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4178 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4179 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4180 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4181 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4182 | |
| 4183 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 4184 | if (lua_gettop(L) > 1) { |
| 4185 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4186 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 4187 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4188 | offset += output; |
| 4189 | if (offset < output || offset > input + output) { |
| 4190 | lua_pushfstring(L, "offset out of range."); |
| 4191 | WILL_LJMP(lua_error(L)); |
| 4192 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4193 | } |
| 4194 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4195 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 4196 | if (lua_gettop(L) == 3) { |
| 4197 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4198 | if (!len) |
| 4199 | goto end; |
| 4200 | if (len == -1) |
| 4201 | len = output + input - offset; |
| 4202 | if (len < 0 || offset + len > output + input) { |
| 4203 | lua_pushfstring(L, "length out of range."); |
| 4204 | WILL_LJMP(lua_error(L)); |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | _hlua_channel_delete(chn, offset, len); |
| 4209 | if (filter) { |
| 4210 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4211 | |
| 4212 | flt_update_offsets(filter, chn, -len); |
| 4213 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 4214 | } |
| 4215 | |
| 4216 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4217 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 4218 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4219 | } |
| 4220 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4221 | /* Append data in the output side of the buffer. This data is immediately |
| 4222 | * sent. The function returns the amount of data written. If the buffer |
| 4223 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4224 | * if the channel is closed. |
| 4225 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4226 | __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] | 4227 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4228 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4229 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4230 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4231 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4232 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4233 | struct hlua *hlua; |
| 4234 | |
| 4235 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4236 | hlua = hlua_gethlua(L); |
| 4237 | if (!hlua) { |
| 4238 | lua_pushnil(L); |
| 4239 | return 1; |
| 4240 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4241 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4242 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4243 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 4244 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4245 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4246 | offset = co_data(chn); |
| 4247 | len = ci_data(chn); |
| 4248 | |
| 4249 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4250 | if (filter && !hlua_filter_from_payload(filter)) |
| 4251 | WILL_LJMP(lua_error(L)); |
| 4252 | |
| 4253 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4254 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4255 | lua_pushinteger(L, -1); |
| 4256 | return 1; |
| 4257 | } |
| 4258 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4259 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4260 | if (len > sz -l) { |
| 4261 | if (filter) { |
| 4262 | lua_pushinteger(L, -1); |
| 4263 | return 1; |
| 4264 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4265 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4266 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4267 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4268 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4269 | if (ret == -1) { |
| 4270 | lua_pop(L, 1); |
| 4271 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4272 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4273 | } |
| 4274 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4275 | if (filter) { |
| 4276 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4277 | |
| 4278 | |
| 4279 | flt_update_offsets(filter, chn, ret); |
| 4280 | FLT_OFF(filter, chn) += ret; |
| 4281 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 4282 | } |
| 4283 | else |
| 4284 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4285 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4286 | l += ret; |
| 4287 | lua_pop(L, 1); |
| 4288 | lua_pushinteger(L, l); |
| 4289 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4290 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4291 | if (l < sz) { |
| 4292 | /* Yield only if the channel's output is not empty. |
| 4293 | * Otherwise it means we cannot add more data. */ |
| 4294 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4295 | return 1; |
| 4296 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4297 | /* If we are waiting for space in the response buffer, we |
| 4298 | * must set the flag WAKERESWR. This flag required the task |
| 4299 | * wake up if any activity is detected on the response buffer. |
| 4300 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4301 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4302 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4303 | else |
| 4304 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4305 | 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] | 4306 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4307 | |
| 4308 | return 1; |
| 4309 | } |
| 4310 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4311 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4312 | * yield the LUA process, and resume it without checking the |
| 4313 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4314 | * |
| 4315 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4316 | */ |
| 4317 | __LJMP static int hlua_channel_send(lua_State *L) |
| 4318 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4319 | struct channel *chn; |
| 4320 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4321 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4322 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4323 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4324 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4325 | WILL_LJMP(lua_error(L)); |
| 4326 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4327 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4328 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | /* This function forward and amount of butes. The data pass from |
| 4332 | * the input side of the buffer to the output side, and can be |
| 4333 | * forwarded. This function never fails. |
| 4334 | * |
| 4335 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4336 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4337 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4338 | __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] | 4339 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4340 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4341 | struct filter *filter; |
| 4342 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4343 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4344 | struct hlua *hlua; |
| 4345 | |
| 4346 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4347 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4348 | if (!hlua) { |
| 4349 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4350 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4351 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4352 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4353 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4354 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4355 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 4356 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4357 | offset = co_data(chn); |
| 4358 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4359 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4360 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4361 | if (filter && !hlua_filter_from_payload(filter)) |
| 4362 | WILL_LJMP(lua_error(L)); |
| 4363 | |
| 4364 | max = fwd - l; |
| 4365 | if (max > len) |
| 4366 | max = len; |
| 4367 | |
| 4368 | if (filter) { |
| 4369 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4370 | |
| 4371 | FLT_OFF(filter, chn) += max; |
| 4372 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 4373 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 4374 | } |
| 4375 | else |
| 4376 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4377 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4378 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4379 | lua_pop(L, 1); |
| 4380 | lua_pushinteger(L, l); |
| 4381 | |
| 4382 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4383 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4384 | /* The the input channel or the output channel are closed, we |
| 4385 | * must return the amount of data forwarded. |
| 4386 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4387 | 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] | 4388 | return 1; |
| 4389 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4390 | /* If we are waiting for space data in the response buffer, we |
| 4391 | * must set the flag WAKERESWR. This flag required the task |
| 4392 | * wake up if any activity is detected on the response buffer. |
| 4393 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4394 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4395 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4396 | else |
| 4397 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4398 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4399 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4400 | 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] | 4401 | } |
| 4402 | |
| 4403 | return 1; |
| 4404 | } |
| 4405 | |
| 4406 | /* Just check the input and prepare the stack for the previous |
| 4407 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4408 | * |
| 4409 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4410 | */ |
| 4411 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4412 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4413 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4414 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4415 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4416 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4417 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4418 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4419 | WILL_LJMP(lua_error(L)); |
| 4420 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4421 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4422 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | /* Just returns the number of bytes available in the input |
| 4426 | * side of the buffer. This function never fails. |
| 4427 | */ |
| 4428 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4429 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4430 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4431 | struct filter *filter; |
| 4432 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4433 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4434 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4435 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4436 | |
| 4437 | output = co_data(chn); |
| 4438 | input = ci_data(chn); |
| 4439 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4440 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4441 | lua_pushinteger(L, input); |
| 4442 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4443 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4444 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4445 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4446 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4447 | return 1; |
| 4448 | } |
| 4449 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4450 | /* Returns true if the channel is full. */ |
| 4451 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4452 | { |
| 4453 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4454 | |
| 4455 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4456 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4457 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4458 | * applet). |
| 4459 | */ |
| 4460 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4461 | return 1; |
| 4462 | } |
| 4463 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4464 | /* Returns true if the channel may still receive data. */ |
| 4465 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4466 | { |
| 4467 | struct channel *chn; |
| 4468 | |
| 4469 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4470 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4471 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4472 | return 1; |
| 4473 | } |
| 4474 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4475 | /* Returns true if the channel is the response channel. */ |
| 4476 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4477 | { |
| 4478 | struct channel *chn; |
| 4479 | |
| 4480 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4481 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4482 | |
| 4483 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4484 | return 1; |
| 4485 | } |
| 4486 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4487 | /* Just returns the number of bytes available in the output |
| 4488 | * side of the buffer. This function never fails. |
| 4489 | */ |
| 4490 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4491 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4492 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4493 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4494 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4495 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4496 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4497 | |
| 4498 | output = co_data(chn); |
| 4499 | input = ci_data(chn); |
| 4500 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4501 | |
| 4502 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4503 | return 1; |
| 4504 | } |
| 4505 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4506 | /* |
| 4507 | * |
| 4508 | * |
| 4509 | * Class Fetches |
| 4510 | * |
| 4511 | * |
| 4512 | */ |
| 4513 | |
| 4514 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4515 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4516 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4517 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4518 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4519 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | /* This function creates and push in the stack a fetch object according |
| 4523 | * with a current TXN. |
| 4524 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4525 | 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] | 4526 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4527 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4528 | |
| 4529 | /* Check stack size. */ |
| 4530 | if (!lua_checkstack(L, 3)) |
| 4531 | return 0; |
| 4532 | |
| 4533 | /* Create the object: obj[0] = userdata. |
| 4534 | * Note that the base of the Fetches object is the |
| 4535 | * transaction object. |
| 4536 | */ |
| 4537 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4538 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4539 | lua_rawseti(L, -2, 0); |
| 4540 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4541 | hsmp->s = txn->s; |
| 4542 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4543 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4544 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4545 | |
| 4546 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4547 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4548 | lua_setmetatable(L, -2); |
| 4549 | |
| 4550 | return 1; |
| 4551 | } |
| 4552 | |
| 4553 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4554 | * It uses closure argument to store the associated sample-fetch. It |
| 4555 | * returns only one argument or throws an error. An error is thrown |
| 4556 | * only if an error is encountered during the argument parsing. If |
| 4557 | * the "sample-fetch" function fails, nil is returned. |
| 4558 | */ |
| 4559 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4560 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4561 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4562 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4563 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4564 | int i; |
| 4565 | struct sample smp; |
| 4566 | |
| 4567 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4568 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4569 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4570 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4571 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4572 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4573 | /* Check execution authorization. */ |
| 4574 | if (f->use & SMP_USE_HTTP_ANY && |
| 4575 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4576 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4577 | "is not available in Lua services", f->kw); |
| 4578 | WILL_LJMP(lua_error(L)); |
| 4579 | } |
| 4580 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4581 | /* Get extra arguments. */ |
| 4582 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4583 | if (i >= ARGM_NBARGS) |
| 4584 | break; |
| 4585 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4586 | } |
| 4587 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4588 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4589 | |
| 4590 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4591 | 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] | 4592 | |
| 4593 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4594 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4595 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4596 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | /* Initialise the sample. */ |
| 4600 | memset(&smp, 0, sizeof(smp)); |
| 4601 | |
| 4602 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4603 | 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] | 4604 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4605 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4606 | lua_pushstring(L, ""); |
| 4607 | else |
| 4608 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4609 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4613 | if (hsmp->flags & HLUA_F_AS_STRING) |
Aurelien DARRAGON | 742b1a8 | 2023-05-17 10:38:50 +0200 | [diff] [blame] | 4614 | MAY_LJMP(hlua_smp2lua_str(L, &smp)); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4615 | else |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 4616 | MAY_LJMP(hlua_smp2lua(L, &smp)); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4617 | |
| 4618 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4619 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4620 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4621 | |
| 4622 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4623 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4624 | WILL_LJMP(lua_error(L)); |
| 4625 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4626 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4627 | |
| 4628 | /* |
| 4629 | * |
| 4630 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4631 | * Class Converters |
| 4632 | * |
| 4633 | * |
| 4634 | */ |
| 4635 | |
| 4636 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4637 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4638 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4639 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4640 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4641 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | /* This function creates and push in the stack a Converters object |
| 4645 | * according with a current TXN. |
| 4646 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4647 | 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] | 4648 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4649 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4650 | |
| 4651 | /* Check stack size. */ |
| 4652 | if (!lua_checkstack(L, 3)) |
| 4653 | return 0; |
| 4654 | |
| 4655 | /* Create the object: obj[0] = userdata. |
| 4656 | * Note that the base of the Converters object is the |
| 4657 | * same than the TXN object. |
| 4658 | */ |
| 4659 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4660 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4661 | lua_rawseti(L, -2, 0); |
| 4662 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4663 | hsmp->s = txn->s; |
| 4664 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4665 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4666 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4667 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4668 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4669 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4670 | lua_setmetatable(L, -2); |
| 4671 | |
| 4672 | return 1; |
| 4673 | } |
| 4674 | |
| 4675 | /* This function is an LUA binding. It is called with each converter. |
| 4676 | * It uses closure argument to store the associated converter. It |
| 4677 | * returns only one argument or throws an error. An error is thrown |
| 4678 | * only if an error is encountered during the argument parsing. If |
| 4679 | * the converter function function fails, nil is returned. |
| 4680 | */ |
| 4681 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4682 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4683 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4684 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4685 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4686 | int i; |
| 4687 | struct sample smp; |
| 4688 | |
| 4689 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4690 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4691 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4692 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4693 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4694 | |
| 4695 | /* Get extra arguments. */ |
| 4696 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4697 | if (i >= ARGM_NBARGS) |
| 4698 | break; |
| 4699 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4700 | } |
| 4701 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4702 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4703 | |
| 4704 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4705 | 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] | 4706 | |
| 4707 | /* Run the special args checker. */ |
| 4708 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4709 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4710 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4714 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4715 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4716 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4717 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4718 | } |
| 4719 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4720 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4721 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4722 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4723 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4724 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4725 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4726 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4727 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4728 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4729 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4730 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4731 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4735 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4736 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4737 | lua_pushstring(L, ""); |
| 4738 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4739 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4740 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4744 | if (hsmp->flags & HLUA_F_AS_STRING) |
Aurelien DARRAGON | 742b1a8 | 2023-05-17 10:38:50 +0200 | [diff] [blame] | 4745 | MAY_LJMP(hlua_smp2lua_str(L, &smp)); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4746 | else |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 4747 | MAY_LJMP(hlua_smp2lua(L, &smp)); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4748 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4749 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4750 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4751 | |
| 4752 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4753 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4754 | WILL_LJMP(lua_error(L)); |
| 4755 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4756 | } |
| 4757 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4758 | /* |
| 4759 | * |
| 4760 | * |
| 4761 | * Class AppletTCP |
| 4762 | * |
| 4763 | * |
| 4764 | */ |
| 4765 | |
| 4766 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4767 | * a class stream, otherwise it throws an error. |
| 4768 | */ |
| 4769 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4770 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4771 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | /* This function creates and push in the stack an Applet object |
| 4775 | * according with a current TXN. |
| 4776 | */ |
| 4777 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4778 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4779 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4780 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4781 | struct proxy *p; |
| 4782 | |
| 4783 | ALREADY_CHECKED(s); |
| 4784 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4785 | |
| 4786 | /* Check stack size. */ |
| 4787 | if (!lua_checkstack(L, 3)) |
| 4788 | return 0; |
| 4789 | |
| 4790 | /* Create the object: obj[0] = userdata. |
| 4791 | * Note that the base of the Converters object is the |
| 4792 | * same than the TXN object. |
| 4793 | */ |
| 4794 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4795 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4796 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4797 | luactx->appctx = ctx; |
| 4798 | luactx->htxn.s = s; |
| 4799 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4800 | |
| 4801 | /* Create the "f" field that contains a list of fetches. */ |
| 4802 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4803 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4804 | return 0; |
| 4805 | lua_settable(L, -3); |
| 4806 | |
| 4807 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4808 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4809 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4810 | return 0; |
| 4811 | lua_settable(L, -3); |
| 4812 | |
| 4813 | /* Create the "c" field that contains a list of converters. */ |
| 4814 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4815 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4816 | return 0; |
| 4817 | lua_settable(L, -3); |
| 4818 | |
| 4819 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4820 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4821 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4822 | return 0; |
| 4823 | lua_settable(L, -3); |
| 4824 | |
| 4825 | /* Pop a class stream metatable and affect it to the table. */ |
| 4826 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4827 | lua_setmetatable(L, -2); |
| 4828 | |
| 4829 | return 1; |
| 4830 | } |
| 4831 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4832 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4833 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4834 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4835 | struct stream *s; |
| 4836 | const char *name; |
| 4837 | size_t len; |
| 4838 | struct sample smp; |
| 4839 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4840 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4841 | 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] | 4842 | |
| 4843 | /* It is useles to retrieve the stream, but this function |
| 4844 | * runs only in a stream context. |
| 4845 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4846 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4847 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4848 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4849 | |
| 4850 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4851 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4852 | hlua_lua2smp(L, 3, &smp); |
| 4853 | |
Aurelien DARRAGON | 1c07da4 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4854 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4855 | * already takes care of duplicating dynamic var data. |
| 4856 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4857 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4858 | |
| 4859 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4860 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4861 | else |
| 4862 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4863 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4864 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4868 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4869 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4870 | struct stream *s; |
| 4871 | const char *name; |
| 4872 | size_t len; |
| 4873 | struct sample smp; |
| 4874 | |
| 4875 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4876 | |
| 4877 | /* It is useles to retrieve the stream, but this function |
| 4878 | * runs only in a stream context. |
| 4879 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4880 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4881 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4882 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4883 | |
| 4884 | /* Unset the variable. */ |
| 4885 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4886 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4887 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4891 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4892 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4893 | struct stream *s; |
| 4894 | const char *name; |
| 4895 | size_t len; |
| 4896 | struct sample smp; |
| 4897 | |
| 4898 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4899 | |
| 4900 | /* It is useles to retrieve the stream, but this function |
| 4901 | * runs only in a stream context. |
| 4902 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4903 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4904 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4905 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4906 | |
| 4907 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4908 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4909 | lua_pushnil(L); |
| 4910 | return 1; |
| 4911 | } |
| 4912 | |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 4913 | return MAY_LJMP(hlua_smp2lua(L, &smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4916 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4917 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4918 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4919 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4920 | struct hlua *hlua; |
| 4921 | |
| 4922 | /* 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] | 4923 | if (!s->hlua) |
| 4924 | return 0; |
| 4925 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4926 | |
| 4927 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4928 | |
| 4929 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4930 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4931 | |
| 4932 | /* Get and store new value. */ |
| 4933 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4934 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4935 | |
| 4936 | return 0; |
| 4937 | } |
| 4938 | |
| 4939 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4940 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4941 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4942 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4943 | struct hlua *hlua; |
| 4944 | |
| 4945 | /* 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] | 4946 | if (!s->hlua) { |
| 4947 | lua_pushnil(L); |
| 4948 | return 1; |
| 4949 | } |
| 4950 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4951 | |
| 4952 | /* Push configuration index in the stack. */ |
| 4953 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4954 | |
| 4955 | return 1; |
| 4956 | } |
| 4957 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4958 | /* If expected data not yet available, it returns a yield. This function |
| 4959 | * consumes the data in the buffer. It returns a string containing the |
| 4960 | * data. This string can be empty. |
| 4961 | */ |
| 4962 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4963 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4964 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4965 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4966 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4967 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4968 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4969 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4970 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4971 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4972 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4973 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4974 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4975 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4976 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4977 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4978 | 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] | 4979 | } |
| 4980 | |
| 4981 | /* End of data: commit the total strings and return. */ |
| 4982 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4983 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4984 | return 1; |
| 4985 | } |
| 4986 | |
| 4987 | /* Ensure that the block 2 length is usable. */ |
| 4988 | if (ret == 1) |
| 4989 | len2 = 0; |
| 4990 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4991 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4992 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4993 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4994 | |
| 4995 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4996 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4997 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4998 | return 1; |
| 4999 | } |
| 5000 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5001 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5002 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 5003 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5004 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5005 | |
| 5006 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5007 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5008 | |
| 5009 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 5010 | } |
| 5011 | |
| 5012 | /* If expected data not yet available, it returns a yield. This function |
| 5013 | * consumes the data in the buffer. It returns a string containing the |
| 5014 | * data. This string can be empty. |
| 5015 | */ |
| 5016 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 5017 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5018 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5019 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 5020 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5021 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 5022 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 5023 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 5024 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 5025 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5026 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5027 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5028 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5029 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5030 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5031 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5032 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5033 | 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] | 5034 | } |
| 5035 | |
| 5036 | /* End of data: commit the total strings and return. */ |
| 5037 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5038 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5039 | return 1; |
| 5040 | } |
| 5041 | |
| 5042 | /* Ensure that the block 2 length is usable. */ |
| 5043 | if (ret == 1) |
| 5044 | len2 = 0; |
| 5045 | |
| 5046 | if (len == -1) { |
| 5047 | |
| 5048 | /* If len == -1, catenate all the data avalaile and |
| 5049 | * yield because we want to get all the data until |
| 5050 | * the end of data stream. |
| 5051 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5052 | luaL_addlstring(&luactx->b, blk1, len1); |
| 5053 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5054 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5055 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5056 | 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] | 5057 | |
| 5058 | } else { |
| 5059 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5060 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5061 | if (len1 > len) |
| 5062 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5063 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5064 | len -= len1; |
| 5065 | |
| 5066 | /* Copy the second block. */ |
| 5067 | if (len2 > len) |
| 5068 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5069 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5070 | len -= len2; |
| 5071 | |
| 5072 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5073 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5074 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5075 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5076 | if (len > 0) { |
| 5077 | lua_pushinteger(L, len); |
| 5078 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5079 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5080 | 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] | 5081 | } |
| 5082 | |
| 5083 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5084 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5085 | return 1; |
| 5086 | } |
| 5087 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5088 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5089 | hlua_pusherror(L, "Lua: internal error"); |
| 5090 | WILL_LJMP(lua_error(L)); |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5094 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5095 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 5096 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5097 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5098 | int len = -1; |
| 5099 | |
| 5100 | if (lua_gettop(L) > 2) |
| 5101 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5102 | if (lua_gettop(L) >= 2) { |
| 5103 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5104 | lua_pop(L, 1); |
| 5105 | } |
| 5106 | |
| 5107 | /* Confirm or set the required length */ |
| 5108 | lua_pushinteger(L, len); |
| 5109 | |
| 5110 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5111 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5112 | |
| 5113 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 5114 | } |
| 5115 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5116 | /* Append data in the output side of the buffer. This data is immediately |
| 5117 | * sent. The function returns the amount of data written. If the buffer |
| 5118 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5119 | * if the channel is closed. |
| 5120 | */ |
| 5121 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 5122 | { |
| 5123 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5124 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5125 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5126 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5127 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5128 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5129 | int max; |
| 5130 | |
| 5131 | /* Get the max amount of data which can write as input in the channel. */ |
| 5132 | max = channel_recv_max(chn); |
| 5133 | if (max > (len - l)) |
| 5134 | max = len - l; |
| 5135 | |
| 5136 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 5137 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5138 | |
| 5139 | /* update counters. */ |
| 5140 | l += max; |
| 5141 | lua_pop(L, 1); |
| 5142 | lua_pushinteger(L, l); |
| 5143 | |
| 5144 | /* If some data is not send, declares the situation to the |
| 5145 | * applet, and returns a yield. |
| 5146 | */ |
| 5147 | if (l < len) { |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 5148 | sc_need_room(sc, channel_recv_max(chn) + 1); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5149 | 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] | 5150 | } |
| 5151 | |
| 5152 | return 1; |
| 5153 | } |
| 5154 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5155 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5156 | * yield the LUA process, and resume it without checking the |
| 5157 | * input arguments. |
| 5158 | */ |
| 5159 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 5160 | { |
| 5161 | MAY_LJMP(check_args(L, 2, "send")); |
| 5162 | lua_pushinteger(L, 0); |
| 5163 | |
| 5164 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 5165 | } |
| 5166 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5167 | /* |
| 5168 | * |
| 5169 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5170 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5171 | * |
| 5172 | * |
| 5173 | */ |
| 5174 | |
| 5175 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5176 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5177 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5178 | __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] | 5179 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5180 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5181 | } |
| 5182 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5184 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5185 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 5186 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5187 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5188 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5189 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5190 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5191 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 5192 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 5193 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5194 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5195 | struct htx *htx; |
| 5196 | struct htx_blk *blk; |
| 5197 | struct htx_sl *sl; |
| 5198 | struct ist path; |
| 5199 | unsigned long long len = 0; |
| 5200 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 5201 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5202 | |
| 5203 | /* Check stack size. */ |
| 5204 | if (!lua_checkstack(L, 3)) |
| 5205 | return 0; |
| 5206 | |
| 5207 | /* Create the object: obj[0] = userdata. |
| 5208 | * Note that the base of the Converters object is the |
| 5209 | * same than the TXN object. |
| 5210 | */ |
| 5211 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5212 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5213 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5214 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5215 | http_ctx->status = 200; /* Default status code returned. */ |
| 5216 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5217 | luactx->htxn.s = s; |
| 5218 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5219 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5220 | /* Create the "f" field that contains a list of fetches. */ |
| 5221 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5222 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5223 | return 0; |
| 5224 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5225 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5226 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5227 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5228 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5229 | return 0; |
| 5230 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5231 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5232 | /* Create the "c" field that contains a list of converters. */ |
| 5233 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5234 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5235 | return 0; |
| 5236 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5237 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5238 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5239 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5240 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5241 | return 0; |
| 5242 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5243 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5244 | htx = htxbuf(&s->req.buf); |
| 5245 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 5246 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5247 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5248 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5249 | /* Stores the request method. */ |
| 5250 | lua_pushstring(L, "method"); |
| 5251 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5252 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5253 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5254 | /* Stores the http version. */ |
| 5255 | lua_pushstring(L, "version"); |
| 5256 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5257 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5258 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5259 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 5260 | * the array on the top of the stack. |
| 5261 | */ |
| 5262 | lua_pushstring(L, "headers"); |
| 5263 | htxn.s = s; |
| 5264 | htxn.p = px; |
| 5265 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5266 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5267 | return 0; |
| 5268 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5269 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 5270 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 5271 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 5272 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5273 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5274 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5275 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 5276 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5277 | q = p; |
| 5278 | while (q < end && *q != '?') |
| 5279 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5280 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5281 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5282 | lua_pushstring(L, "path"); |
| 5283 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5284 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5285 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5286 | /* Stores the query string. */ |
| 5287 | lua_pushstring(L, "qs"); |
| 5288 | if (*q == '?') |
| 5289 | q++; |
| 5290 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5291 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5292 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5293 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5294 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5295 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5296 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5297 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5298 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5299 | break; |
| 5300 | if (type == HTX_BLK_DATA) |
| 5301 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5302 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 5303 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5304 | len += htx->extra; |
| 5305 | |
| 5306 | /* Stores the request path. */ |
| 5307 | lua_pushstring(L, "length"); |
| 5308 | lua_pushinteger(L, len); |
| 5309 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5310 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5311 | /* Create an empty array of HTTP request headers. */ |
| 5312 | lua_pushstring(L, "response"); |
| 5313 | lua_newtable(L); |
| 5314 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5315 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5316 | /* Pop a class stream metatable and affect it to the table. */ |
| 5317 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 5318 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5319 | |
| 5320 | return 1; |
| 5321 | } |
| 5322 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5323 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 5324 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5325 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5326 | struct stream *s; |
| 5327 | const char *name; |
| 5328 | size_t len; |
| 5329 | struct sample smp; |
| 5330 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5331 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5332 | 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] | 5333 | |
| 5334 | /* It is useles to retrieve the stream, but this function |
| 5335 | * runs only in a stream context. |
| 5336 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5337 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5338 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5339 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5340 | |
| 5341 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5342 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5343 | hlua_lua2smp(L, 3, &smp); |
| 5344 | |
Aurelien DARRAGON | 1c07da4 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5345 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5346 | * already takes care of duplicating dynamic var data. |
| 5347 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5348 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5349 | |
| 5350 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5351 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5352 | else |
| 5353 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5354 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5355 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 5359 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5360 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5361 | struct stream *s; |
| 5362 | const char *name; |
| 5363 | size_t len; |
| 5364 | struct sample smp; |
| 5365 | |
| 5366 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5367 | |
| 5368 | /* It is useles to retrieve the stream, but this function |
| 5369 | * runs only in a stream context. |
| 5370 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5371 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5372 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5373 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5374 | |
| 5375 | /* Unset the variable. */ |
| 5376 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5377 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5378 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 5382 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5383 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5384 | struct stream *s; |
| 5385 | const char *name; |
| 5386 | size_t len; |
| 5387 | struct sample smp; |
| 5388 | |
| 5389 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5390 | |
| 5391 | /* It is useles to retrieve the stream, but this function |
| 5392 | * runs only in a stream context. |
| 5393 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5394 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5395 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5396 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5397 | |
| 5398 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5399 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5400 | lua_pushnil(L); |
| 5401 | return 1; |
| 5402 | } |
| 5403 | |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 5404 | return MAY_LJMP(hlua_smp2lua(L, &smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5405 | } |
| 5406 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5407 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5408 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5409 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5410 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5411 | struct hlua *hlua; |
| 5412 | |
| 5413 | /* 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] | 5414 | if (!s->hlua) |
| 5415 | return 0; |
| 5416 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5417 | |
| 5418 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5419 | |
| 5420 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5421 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5422 | |
| 5423 | /* Get and store new value. */ |
| 5424 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5425 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5426 | |
| 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5431 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5432 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5433 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5434 | struct hlua *hlua; |
| 5435 | |
| 5436 | /* 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] | 5437 | if (!s->hlua) { |
| 5438 | lua_pushnil(L); |
| 5439 | return 1; |
| 5440 | } |
| 5441 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5442 | |
| 5443 | /* Push configuration index in the stack. */ |
| 5444 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5445 | |
| 5446 | return 1; |
| 5447 | } |
| 5448 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5449 | /* If expected data not yet available, it returns a yield. This function |
| 5450 | * consumes the data in the buffer. It returns a string containing the |
| 5451 | * data. This string can be empty. |
| 5452 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5453 | __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] | 5454 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5455 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5456 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5457 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5458 | struct htx *htx; |
| 5459 | struct htx_blk *blk; |
| 5460 | size_t count; |
| 5461 | int stop = 0; |
| 5462 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5463 | htx = htx_from_buf(&req->buf); |
| 5464 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5465 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5466 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5467 | while (count && !stop && blk) { |
| 5468 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5469 | uint32_t sz = htx_get_blksz(blk); |
| 5470 | struct ist v; |
| 5471 | uint32_t vlen; |
| 5472 | char *nl; |
| 5473 | |
| 5474 | vlen = sz; |
| 5475 | if (vlen > count) { |
| 5476 | if (type != HTX_BLK_DATA) |
| 5477 | break; |
| 5478 | vlen = count; |
| 5479 | } |
| 5480 | |
| 5481 | switch (type) { |
| 5482 | case HTX_BLK_UNUSED: |
| 5483 | break; |
| 5484 | |
| 5485 | case HTX_BLK_DATA: |
| 5486 | v = htx_get_blk_value(htx, blk); |
| 5487 | v.len = vlen; |
| 5488 | nl = istchr(v, '\n'); |
| 5489 | if (nl != NULL) { |
| 5490 | stop = 1; |
| 5491 | vlen = nl - v.ptr + 1; |
| 5492 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5493 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5494 | break; |
| 5495 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5496 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5497 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5498 | stop = 1; |
| 5499 | break; |
| 5500 | |
| 5501 | default: |
| 5502 | break; |
| 5503 | } |
| 5504 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5505 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5506 | count -= vlen; |
| 5507 | if (sz == vlen) |
| 5508 | blk = htx_remove_blk(htx, blk); |
| 5509 | else { |
| 5510 | htx_cut_data_blk(htx, blk, vlen); |
| 5511 | break; |
| 5512 | } |
| 5513 | } |
| 5514 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5515 | /* The message was fully consumed and no more data are expected |
| 5516 | * (EOM flag set). |
| 5517 | */ |
Christopher Faulet | c9e8a32 | 2023-06-12 09:16:27 +0200 | [diff] [blame] | 5518 | if (htx_is_empty(htx) && (sc_opposite(sc)->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5519 | stop = 1; |
| 5520 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5521 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5522 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5523 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5524 | 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] | 5525 | } |
| 5526 | |
| 5527 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5528 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5529 | return 1; |
| 5530 | } |
| 5531 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5532 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5533 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5534 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5535 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5536 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5537 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5538 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5539 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5540 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5541 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5542 | } |
| 5543 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5544 | /* If expected data not yet available, it returns a yield. This function |
| 5545 | * consumes the data in the buffer. It returns a string containing the |
| 5546 | * data. This string can be empty. |
| 5547 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5548 | __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] | 5549 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5550 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5551 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5552 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5553 | struct htx *htx; |
| 5554 | struct htx_blk *blk; |
| 5555 | size_t count; |
| 5556 | int len; |
| 5557 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5558 | htx = htx_from_buf(&req->buf); |
| 5559 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5560 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5561 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5562 | while (count && len && blk) { |
| 5563 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5564 | uint32_t sz = htx_get_blksz(blk); |
| 5565 | struct ist v; |
| 5566 | uint32_t vlen; |
| 5567 | |
| 5568 | vlen = sz; |
| 5569 | if (len > 0 && vlen > len) |
| 5570 | vlen = len; |
| 5571 | if (vlen > count) { |
| 5572 | if (type != HTX_BLK_DATA) |
| 5573 | break; |
| 5574 | vlen = count; |
| 5575 | } |
| 5576 | |
| 5577 | switch (type) { |
| 5578 | case HTX_BLK_UNUSED: |
| 5579 | break; |
| 5580 | |
| 5581 | case HTX_BLK_DATA: |
| 5582 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5583 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5584 | break; |
| 5585 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5586 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5587 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5588 | len = 0; |
| 5589 | break; |
| 5590 | |
| 5591 | default: |
| 5592 | break; |
| 5593 | } |
| 5594 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5595 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5596 | count -= vlen; |
| 5597 | if (len > 0) |
| 5598 | len -= vlen; |
| 5599 | if (sz == vlen) |
| 5600 | blk = htx_remove_blk(htx, blk); |
| 5601 | else { |
| 5602 | htx_cut_data_blk(htx, blk, vlen); |
| 5603 | break; |
| 5604 | } |
| 5605 | } |
| 5606 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5607 | /* The message was fully consumed and no more data are expected |
| 5608 | * (EOM flag set). |
| 5609 | */ |
Christopher Faulet | c9e8a32 | 2023-06-12 09:16:27 +0200 | [diff] [blame] | 5610 | if (htx_is_empty(htx) && (sc_opposite(sc)->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5611 | len = 0; |
| 5612 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5613 | htx_to_buf(htx, &req->buf); |
| 5614 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5615 | /* If we are no other data available, yield waiting for new data. */ |
| 5616 | if (len) { |
| 5617 | if (len > 0) { |
| 5618 | lua_pushinteger(L, len); |
| 5619 | lua_replace(L, 2); |
| 5620 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5621 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5622 | 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] | 5623 | } |
| 5624 | |
| 5625 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5626 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5627 | return 1; |
| 5628 | } |
| 5629 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5630 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5631 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5632 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5633 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5634 | int len = -1; |
| 5635 | |
| 5636 | /* Check arguments. */ |
| 5637 | if (lua_gettop(L) > 2) |
| 5638 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5639 | if (lua_gettop(L) >= 2) { |
| 5640 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5641 | lua_pop(L, 1); |
| 5642 | } |
| 5643 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5644 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5645 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5646 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5647 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5648 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5649 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | /* Append data in the output side of the buffer. This data is immediately |
| 5653 | * sent. The function returns the amount of data written. If the buffer |
| 5654 | * cannot contain the data, the function yields. The function returns -1 |
| 5655 | * if the channel is closed. |
| 5656 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5657 | __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] | 5658 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5659 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5660 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5661 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5662 | struct htx *htx = htx_from_buf(&res->buf); |
| 5663 | const char *data; |
| 5664 | size_t len; |
| 5665 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5666 | int max; |
| 5667 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5668 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5669 | if (!max) |
| 5670 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5671 | |
| 5672 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5673 | |
| 5674 | /* Get the max amount of data which can write as input in the channel. */ |
| 5675 | if (max > (len - l)) |
| 5676 | max = len - l; |
| 5677 | |
| 5678 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5679 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5680 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5681 | |
| 5682 | /* update counters. */ |
| 5683 | l += max; |
| 5684 | lua_pop(L, 1); |
| 5685 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5686 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5687 | /* If some data is not send, declares the situation to the |
| 5688 | * applet, and returns a yield. |
| 5689 | */ |
| 5690 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5691 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5692 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 5693 | sc_need_room(sc, channel_recv_max(res) + 1); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5694 | 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] | 5695 | } |
| 5696 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5697 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5698 | return 1; |
| 5699 | } |
| 5700 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5701 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5702 | * yield the LUA process, and resume it without checking the |
| 5703 | * input arguments. |
| 5704 | */ |
| 5705 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5706 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5707 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5708 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5709 | |
| 5710 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5711 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5712 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5713 | WILL_LJMP(lua_error(L)); |
| 5714 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5715 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5716 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5717 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5718 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5719 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5723 | { |
| 5724 | const char *name; |
| 5725 | int ret; |
| 5726 | |
| 5727 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5728 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5729 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5730 | |
| 5731 | /* Push in the stack the "response" entry. */ |
| 5732 | ret = lua_getfield(L, 1, "response"); |
| 5733 | if (ret != LUA_TTABLE) { |
| 5734 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5735 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5736 | WILL_LJMP(lua_error(L)); |
| 5737 | } |
| 5738 | |
| 5739 | /* check if the header is already registered if it is not |
| 5740 | * the case, register it. |
| 5741 | */ |
| 5742 | ret = lua_getfield(L, -1, name); |
| 5743 | if (ret == LUA_TNIL) { |
| 5744 | |
| 5745 | /* Entry not found. */ |
| 5746 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5747 | |
| 5748 | /* Insert the new header name in the array in the top of the stack. |
| 5749 | * It left the new array in the top of the stack. |
| 5750 | */ |
| 5751 | lua_newtable(L); |
| 5752 | lua_pushvalue(L, 2); |
| 5753 | lua_pushvalue(L, -2); |
| 5754 | lua_settable(L, -4); |
| 5755 | |
| 5756 | } else if (ret != LUA_TTABLE) { |
| 5757 | |
| 5758 | /* corruption error. */ |
| 5759 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5760 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5761 | WILL_LJMP(lua_error(L)); |
| 5762 | } |
| 5763 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5764 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5765 | * the header value as new entry. |
| 5766 | */ |
| 5767 | lua_pushvalue(L, 3); |
| 5768 | ret = lua_rawlen(L, -2); |
| 5769 | lua_rawseti(L, -2, ret + 1); |
| 5770 | lua_pushboolean(L, 1); |
| 5771 | return 1; |
| 5772 | } |
| 5773 | |
| 5774 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5775 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5776 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5777 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5778 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5779 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5780 | |
| 5781 | if (status < 100 || status > 599) { |
| 5782 | lua_pushboolean(L, 0); |
| 5783 | return 1; |
| 5784 | } |
| 5785 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5786 | http_ctx->status = status; |
| 5787 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5788 | lua_pushboolean(L, 1); |
| 5789 | return 1; |
| 5790 | } |
| 5791 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5792 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5793 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5794 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5795 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5796 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5797 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5798 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5799 | struct htx *htx; |
| 5800 | struct htx_sl *sl; |
| 5801 | struct h1m h1m; |
| 5802 | const char *status, *reason; |
| 5803 | const char *name, *value; |
| 5804 | size_t nlen, vlen; |
| 5805 | unsigned int flags; |
| 5806 | |
| 5807 | /* Send the message at once. */ |
| 5808 | htx = htx_from_buf(&res->buf); |
| 5809 | h1m_init_res(&h1m); |
| 5810 | |
| 5811 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5812 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5813 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5814 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5815 | reason = http_get_reason(http_ctx->status); |
| 5816 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5817 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5818 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5819 | } |
| 5820 | else { |
| 5821 | flags = HTX_SL_F_IS_RESP; |
| 5822 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5823 | } |
| 5824 | if (!sl) { |
| 5825 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5826 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5827 | WILL_LJMP(lua_error(L)); |
| 5828 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5829 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5830 | |
| 5831 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5832 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5833 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5834 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5835 | WILL_LJMP(lua_error(L)); |
| 5836 | } |
| 5837 | |
| 5838 | /* Browse the list of headers. */ |
| 5839 | lua_pushnil(L); |
| 5840 | while(lua_next(L, -2) != 0) { |
| 5841 | /* We expect a string as -2. */ |
| 5842 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5843 | 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] | 5844 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5845 | lua_typename(L, lua_type(L, -2))); |
| 5846 | WILL_LJMP(lua_error(L)); |
| 5847 | } |
| 5848 | name = lua_tolstring(L, -2, &nlen); |
| 5849 | |
| 5850 | /* We expect an array as -1. */ |
| 5851 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5852 | 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] | 5853 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5854 | name, |
| 5855 | lua_typename(L, lua_type(L, -1))); |
| 5856 | WILL_LJMP(lua_error(L)); |
| 5857 | } |
| 5858 | |
| 5859 | /* Browse the table who is on the top of the stack. */ |
| 5860 | lua_pushnil(L); |
| 5861 | while(lua_next(L, -2) != 0) { |
| 5862 | int id; |
| 5863 | |
| 5864 | /* We expect a number as -2. */ |
| 5865 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5866 | 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] | 5867 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5868 | name, |
| 5869 | lua_typename(L, lua_type(L, -2))); |
| 5870 | WILL_LJMP(lua_error(L)); |
| 5871 | } |
| 5872 | id = lua_tointeger(L, -2); |
| 5873 | |
| 5874 | /* We expect a string as -2. */ |
| 5875 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5876 | 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] | 5877 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5878 | name, id, |
| 5879 | lua_typename(L, lua_type(L, -1))); |
| 5880 | WILL_LJMP(lua_error(L)); |
| 5881 | } |
| 5882 | value = lua_tolstring(L, -1, &vlen); |
| 5883 | |
| 5884 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5885 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5886 | int ret; |
| 5887 | |
| 5888 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5889 | if (ret < 0) { |
| 5890 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5891 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5892 | name); |
| 5893 | WILL_LJMP(lua_error(L)); |
| 5894 | } |
| 5895 | else if (ret == 0) |
| 5896 | goto next; /* Skip it */ |
| 5897 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5898 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5899 | struct ist v = ist2(value, vlen); |
| 5900 | int ret; |
| 5901 | |
| 5902 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5903 | if (ret < 0) { |
| 5904 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5905 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5906 | name); |
| 5907 | WILL_LJMP(lua_error(L)); |
| 5908 | } |
| 5909 | else if (ret == 0) |
| 5910 | goto next; /* Skip it */ |
| 5911 | } |
| 5912 | |
| 5913 | /* Add a new header */ |
| 5914 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5915 | 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] | 5916 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5917 | name); |
| 5918 | WILL_LJMP(lua_error(L)); |
| 5919 | } |
| 5920 | next: |
| 5921 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5922 | lua_pop(L, 1); |
| 5923 | } |
| 5924 | |
| 5925 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5926 | lua_pop(L, 1); |
| 5927 | } |
| 5928 | |
| 5929 | if (h1m.flags & H1_MF_CHNK) |
| 5930 | h1m.flags &= ~H1_MF_CLEN; |
| 5931 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5932 | h1m.flags |= H1_MF_XFER_LEN; |
| 5933 | |
| 5934 | /* Uset HTX start-line flags */ |
| 5935 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5936 | flags |= HTX_SL_F_XFER_ENC; |
| 5937 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5938 | flags |= HTX_SL_F_XFER_LEN; |
| 5939 | if (h1m.flags & H1_MF_CHNK) |
| 5940 | flags |= HTX_SL_F_CHNK; |
| 5941 | else if (h1m.flags & H1_MF_CLEN) |
| 5942 | flags |= HTX_SL_F_CLEN; |
| 5943 | if (h1m.body_len == 0) |
| 5944 | flags |= HTX_SL_F_BODYLESS; |
| 5945 | } |
| 5946 | sl->flags |= flags; |
| 5947 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5948 | /* 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] | 5949 | * and the status code implies the presence of a message body, we must |
| 5950 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5951 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5952 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5953 | */ |
| 5954 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5955 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5956 | /* Add a new header */ |
| 5957 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5958 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5959 | 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] | 5960 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5961 | WILL_LJMP(lua_error(L)); |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | /* Finalize headers. */ |
| 5966 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5967 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5968 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5969 | WILL_LJMP(lua_error(L)); |
| 5970 | } |
| 5971 | |
| 5972 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5973 | b_reset(&res->buf); |
| 5974 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5975 | WILL_LJMP(lua_error(L)); |
| 5976 | } |
| 5977 | |
| 5978 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5979 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5980 | |
| 5981 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5982 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5983 | return 0; |
| 5984 | |
| 5985 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5986 | /* We will build the status line and the headers of the HTTP response. |
| 5987 | * We will try send at once if its not possible, we give back the hand |
| 5988 | * waiting for more room. |
| 5989 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5990 | __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] | 5991 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5992 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5993 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5994 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5995 | |
| 5996 | if (co_data(res)) { |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 5997 | sc_need_room(sc, -1); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5998 | 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] | 5999 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6000 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6004 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6005 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6006 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6007 | } |
| 6008 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6009 | /* |
| 6010 | * |
| 6011 | * |
| 6012 | * Class HTTP |
| 6013 | * |
| 6014 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6015 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6016 | |
| 6017 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 6018 | * a class stream, otherwise it throws an error. |
| 6019 | */ |
| 6020 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6021 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6022 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 6023 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6024 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6025 | /* This function creates and push in the stack a HTTP object |
| 6026 | * according with a current TXN. |
| 6027 | */ |
| 6028 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 6029 | { |
| 6030 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6031 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6032 | /* Check stack size. */ |
| 6033 | if (!lua_checkstack(L, 3)) |
| 6034 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6035 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6036 | /* Create the object: obj[0] = userdata. |
| 6037 | * Note that the base of the Converters object is the |
| 6038 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6039 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6040 | lua_newtable(L); |
| 6041 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 6042 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6043 | |
| 6044 | htxn->s = txn->s; |
| 6045 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 6046 | htxn->dir = txn->dir; |
| 6047 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6048 | |
| 6049 | /* Pop a class stream metatable and affect it to the table. */ |
| 6050 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 6051 | lua_setmetatable(L, -2); |
| 6052 | |
| 6053 | return 1; |
| 6054 | } |
| 6055 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6056 | /* This function creates and returns an array containing the status-line |
| 6057 | * elements. This function does not fails. |
| 6058 | */ |
| 6059 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 6060 | { |
| 6061 | /* Create the table. */ |
| 6062 | lua_newtable(L); |
| 6063 | |
| 6064 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 6065 | lua_pushstring(L, "version"); |
| 6066 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 6067 | lua_settable(L, -3); |
| 6068 | lua_pushstring(L, "code"); |
| 6069 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 6070 | lua_settable(L, -3); |
| 6071 | lua_pushstring(L, "reason"); |
| 6072 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 6073 | lua_settable(L, -3); |
| 6074 | } |
| 6075 | else { |
| 6076 | lua_pushstring(L, "method"); |
| 6077 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 6078 | lua_settable(L, -3); |
| 6079 | lua_pushstring(L, "uri"); |
| 6080 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 6081 | lua_settable(L, -3); |
| 6082 | lua_pushstring(L, "version"); |
| 6083 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 6084 | lua_settable(L, -3); |
| 6085 | } |
| 6086 | return 1; |
| 6087 | } |
| 6088 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6089 | /* This function creates ans returns an array of HTTP headers. |
| 6090 | * This function does not fails. It is used as wrapper with the |
| 6091 | * 2 following functions. |
| 6092 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 6093 | __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] | 6094 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6095 | struct htx *htx; |
| 6096 | int32_t pos; |
| 6097 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6098 | /* Create the table. */ |
| 6099 | lua_newtable(L); |
| 6100 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6101 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6102 | htx = htxbuf(&msg->chn->buf); |
| 6103 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 6104 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 6105 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6106 | struct ist n, v; |
| 6107 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 6108 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6109 | if (type == HTX_BLK_HDR) { |
| 6110 | n = htx_get_blk_name(htx,blk); |
| 6111 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 6112 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6113 | else if (type == HTX_BLK_EOH) |
| 6114 | break; |
| 6115 | else |
| 6116 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 6117 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6118 | /* Check for existing entry: |
| 6119 | * assume that the table is on the top of the stack, and |
| 6120 | * push the key in the stack, the function lua_gettable() |
| 6121 | * perform the lookup. |
| 6122 | */ |
| 6123 | lua_pushlstring(L, n.ptr, n.len); |
| 6124 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 6125 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6126 | switch (lua_type(L, -1)) { |
| 6127 | case LUA_TNIL: |
| 6128 | /* Table not found, create it. */ |
| 6129 | lua_pop(L, 1); /* remove the nil value. */ |
| 6130 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 6131 | lua_newtable(L); /* create and push empty table. */ |
| 6132 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 6133 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 6134 | 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] | 6135 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 6136 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6137 | case LUA_TTABLE: |
| 6138 | /* Entry found: push the value in the table. */ |
| 6139 | len = lua_rawlen(L, -1); |
| 6140 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 6141 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 6142 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 6143 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6144 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6145 | default: |
| 6146 | /* Other cases are errors. */ |
| 6147 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 6148 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6149 | } |
| 6150 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6151 | return 1; |
| 6152 | } |
| 6153 | |
| 6154 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 6155 | { |
| 6156 | struct hlua_txn *htxn; |
| 6157 | |
| 6158 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 6159 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6160 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6161 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6162 | WILL_LJMP(lua_error(L)); |
| 6163 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 6164 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 6168 | { |
| 6169 | struct hlua_txn *htxn; |
| 6170 | |
| 6171 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 6172 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6173 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6174 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6175 | WILL_LJMP(lua_error(L)); |
| 6176 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 6177 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | /* This function replace full header, or just a value in |
| 6181 | * the request or in the response. It is a wrapper fir the |
| 6182 | * 4 following functions. |
| 6183 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6184 | __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] | 6185 | { |
| 6186 | size_t name_len; |
| 6187 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6188 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6189 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6190 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 6191 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6192 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 6193 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6194 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 6195 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6196 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6197 | 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] | 6198 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6199 | return 0; |
| 6200 | } |
| 6201 | |
| 6202 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 6203 | { |
| 6204 | struct hlua_txn *htxn; |
| 6205 | |
| 6206 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 6207 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6208 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6209 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6210 | WILL_LJMP(lua_error(L)); |
| 6211 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6212 | 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] | 6213 | } |
| 6214 | |
| 6215 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 6216 | { |
| 6217 | struct hlua_txn *htxn; |
| 6218 | |
| 6219 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 6220 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6221 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6222 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6223 | WILL_LJMP(lua_error(L)); |
| 6224 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6225 | 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] | 6226 | } |
| 6227 | |
| 6228 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 6229 | { |
| 6230 | struct hlua_txn *htxn; |
| 6231 | |
| 6232 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 6233 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6234 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6235 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6236 | WILL_LJMP(lua_error(L)); |
| 6237 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6238 | 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] | 6239 | } |
| 6240 | |
| 6241 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 6242 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6243 | struct hlua_txn *htxn; |
| 6244 | |
| 6245 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 6246 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6247 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6248 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6249 | WILL_LJMP(lua_error(L)); |
| 6250 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6251 | 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] | 6252 | } |
| 6253 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6254 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6255 | * It is a wrapper for the 2 following functions. |
| 6256 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6257 | __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] | 6258 | { |
| 6259 | size_t len; |
| 6260 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6261 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6262 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6263 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6264 | ctx.blk = NULL; |
| 6265 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 6266 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6267 | return 0; |
| 6268 | } |
| 6269 | |
| 6270 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 6271 | { |
| 6272 | struct hlua_txn *htxn; |
| 6273 | |
| 6274 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 6275 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6276 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6277 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6278 | WILL_LJMP(lua_error(L)); |
| 6279 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6280 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 6284 | { |
| 6285 | struct hlua_txn *htxn; |
| 6286 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6287 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6288 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6289 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6290 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6291 | WILL_LJMP(lua_error(L)); |
| 6292 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6293 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | /* This function adds an header. It is a wrapper used by |
| 6297 | * the 2 following functions. |
| 6298 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6299 | __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] | 6300 | { |
| 6301 | size_t name_len; |
| 6302 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6303 | size_t value_len; |
| 6304 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6305 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6306 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6307 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 6308 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6309 | return 0; |
| 6310 | } |
| 6311 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6312 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 6313 | { |
| 6314 | struct hlua_txn *htxn; |
| 6315 | |
| 6316 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 6317 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6318 | |
| 6319 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6320 | WILL_LJMP(lua_error(L)); |
| 6321 | |
| 6322 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6323 | } |
| 6324 | |
| 6325 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 6326 | { |
| 6327 | struct hlua_txn *htxn; |
| 6328 | |
| 6329 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 6330 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6331 | |
| 6332 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6333 | WILL_LJMP(lua_error(L)); |
| 6334 | |
| 6335 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6336 | } |
| 6337 | |
| 6338 | static int hlua_http_req_set_hdr(lua_State *L) |
| 6339 | { |
| 6340 | struct hlua_txn *htxn; |
| 6341 | |
| 6342 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 6343 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6344 | |
| 6345 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6346 | WILL_LJMP(lua_error(L)); |
| 6347 | |
| 6348 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 6349 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6350 | } |
| 6351 | |
| 6352 | static int hlua_http_res_set_hdr(lua_State *L) |
| 6353 | { |
| 6354 | struct hlua_txn *htxn; |
| 6355 | |
| 6356 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 6357 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6358 | |
| 6359 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6360 | WILL_LJMP(lua_error(L)); |
| 6361 | |
| 6362 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 6363 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6364 | } |
| 6365 | |
| 6366 | /* This function set the method. */ |
| 6367 | static int hlua_http_req_set_meth(lua_State *L) |
| 6368 | { |
| 6369 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6370 | size_t name_len; |
| 6371 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6372 | |
| 6373 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6374 | WILL_LJMP(lua_error(L)); |
| 6375 | |
| 6376 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 6377 | return 1; |
| 6378 | } |
| 6379 | |
| 6380 | /* This function set the method. */ |
| 6381 | static int hlua_http_req_set_path(lua_State *L) |
| 6382 | { |
| 6383 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6384 | size_t name_len; |
| 6385 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6386 | |
| 6387 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6388 | WILL_LJMP(lua_error(L)); |
| 6389 | |
| 6390 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 6391 | return 1; |
| 6392 | } |
| 6393 | |
| 6394 | /* This function set the query-string. */ |
| 6395 | static int hlua_http_req_set_query(lua_State *L) |
| 6396 | { |
| 6397 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6398 | size_t name_len; |
| 6399 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6400 | |
| 6401 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6402 | WILL_LJMP(lua_error(L)); |
| 6403 | |
| 6404 | /* Check length. */ |
| 6405 | if (name_len > trash.size - 1) { |
| 6406 | lua_pushboolean(L, 0); |
| 6407 | return 1; |
| 6408 | } |
| 6409 | |
| 6410 | /* Add the mark question as prefix. */ |
| 6411 | chunk_reset(&trash); |
| 6412 | trash.area[trash.data++] = '?'; |
| 6413 | memcpy(trash.area + trash.data, name, name_len); |
| 6414 | trash.data += name_len; |
| 6415 | |
| 6416 | lua_pushboolean(L, |
| 6417 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6418 | return 1; |
| 6419 | } |
| 6420 | |
| 6421 | /* This function set the uri. */ |
| 6422 | static int hlua_http_req_set_uri(lua_State *L) |
| 6423 | { |
| 6424 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6425 | size_t name_len; |
| 6426 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6427 | |
| 6428 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6429 | WILL_LJMP(lua_error(L)); |
| 6430 | |
| 6431 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6432 | return 1; |
| 6433 | } |
| 6434 | |
| 6435 | /* This function set the response code & optionally reason. */ |
| 6436 | static int hlua_http_res_set_status(lua_State *L) |
| 6437 | { |
| 6438 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6439 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6440 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6441 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6442 | |
| 6443 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6444 | WILL_LJMP(lua_error(L)); |
| 6445 | |
| 6446 | http_res_set_status(code, reason, htxn->s); |
| 6447 | return 0; |
| 6448 | } |
| 6449 | |
| 6450 | /* |
| 6451 | * |
| 6452 | * |
| 6453 | * Class HTTPMessage |
| 6454 | * |
| 6455 | * |
| 6456 | */ |
| 6457 | |
| 6458 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6459 | * otherwise it throws an error. |
| 6460 | */ |
| 6461 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6462 | { |
| 6463 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6464 | } |
| 6465 | |
| 6466 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6467 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6468 | static int hlua_http_msg_new(lua_State *L, struct http_msg *msg) |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6469 | { |
| 6470 | /* Check stack size. */ |
| 6471 | if (!lua_checkstack(L, 3)) |
| 6472 | return 0; |
| 6473 | |
| 6474 | lua_newtable(L); |
| 6475 | lua_pushlightuserdata(L, msg); |
| 6476 | lua_rawseti(L, -2, 0); |
| 6477 | |
| 6478 | /* Create the "channel" field that contains the request channel object. */ |
| 6479 | lua_pushstring(L, "channel"); |
| 6480 | if (!hlua_channel_new(L, msg->chn)) |
| 6481 | return 0; |
| 6482 | lua_rawset(L, -3); |
| 6483 | |
| 6484 | /* Pop a class stream metatable and affect it to the table. */ |
| 6485 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6486 | lua_setmetatable(L, -2); |
| 6487 | |
| 6488 | return 1; |
| 6489 | } |
| 6490 | |
| 6491 | /* Helper function returning a filter attached to the HTTP message at the |
| 6492 | * position <ud> in the stack, filling the current offset and length of the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6493 | * filter. If no filter is attached, NULL is returned and <offset> and <len> are |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6494 | * filled with output and input length respectively. |
| 6495 | */ |
| 6496 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6497 | { |
| 6498 | struct channel *chn = msg->chn; |
| 6499 | struct htx *htx = htxbuf(&chn->buf); |
| 6500 | struct filter *filter = NULL; |
| 6501 | |
| 6502 | *offset = co_data(msg->chn); |
| 6503 | *len = htx->data - co_data(msg->chn); |
| 6504 | |
| 6505 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6506 | filter = lua_touserdata (L, -1); |
| 6507 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6508 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6509 | |
| 6510 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6511 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6512 | } |
| 6513 | } |
| 6514 | |
| 6515 | lua_pop(L, 1); |
| 6516 | return filter; |
| 6517 | } |
| 6518 | |
| 6519 | /* Returns true if the channel attached to the HTTP message is the response |
| 6520 | * channel. |
| 6521 | */ |
| 6522 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6523 | { |
| 6524 | struct http_msg *msg; |
| 6525 | |
| 6526 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6527 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6528 | |
| 6529 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6530 | return 1; |
| 6531 | } |
| 6532 | |
| 6533 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6534 | * on hlua_http_get_stline(). |
| 6535 | */ |
| 6536 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6537 | { |
| 6538 | struct http_msg *msg; |
| 6539 | struct htx *htx; |
| 6540 | struct htx_sl *sl; |
| 6541 | |
| 6542 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6543 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6544 | |
| 6545 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6546 | WILL_LJMP(lua_error(L)); |
| 6547 | |
| 6548 | htx = htxbuf(&msg->chn->buf); |
| 6549 | sl = http_get_stline(htx); |
| 6550 | if (!sl) |
| 6551 | return 0; |
| 6552 | return hlua_http_get_stline(L, sl); |
| 6553 | } |
| 6554 | |
| 6555 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6556 | * hlua_http_get_headers(). |
| 6557 | */ |
| 6558 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6559 | { |
| 6560 | struct http_msg *msg; |
| 6561 | |
| 6562 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6563 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6564 | |
| 6565 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6566 | WILL_LJMP(lua_error(L)); |
| 6567 | |
| 6568 | return hlua_http_get_headers(L, msg); |
| 6569 | } |
| 6570 | |
| 6571 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6572 | * name. It relies on hlua_http_del_hdr(). |
| 6573 | */ |
| 6574 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6575 | { |
| 6576 | struct http_msg *msg; |
| 6577 | |
| 6578 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6579 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6580 | |
| 6581 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6582 | WILL_LJMP(lua_error(L)); |
| 6583 | |
| 6584 | return hlua_http_del_hdr(L, msg); |
| 6585 | } |
| 6586 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6587 | /* Matches the full value line of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6588 | * message given its name against a regex and replaces it if it matches. It |
| 6589 | * relies on hlua_http_rep_hdr(). |
| 6590 | */ |
| 6591 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6592 | { |
| 6593 | struct http_msg *msg; |
| 6594 | |
| 6595 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6596 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6597 | |
| 6598 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6599 | WILL_LJMP(lua_error(L)); |
| 6600 | |
| 6601 | return hlua_http_rep_hdr(L, msg, 1); |
| 6602 | } |
| 6603 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6604 | /* Matches all comma-separated values of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6605 | * message given its name against a regex and replaces it if it matches. It |
| 6606 | * relies on hlua_http_rep_hdr(). |
| 6607 | */ |
| 6608 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6609 | { |
| 6610 | struct http_msg *msg; |
| 6611 | |
| 6612 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6613 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6614 | |
| 6615 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6616 | WILL_LJMP(lua_error(L)); |
| 6617 | |
| 6618 | return hlua_http_rep_hdr(L, msg, 0); |
| 6619 | } |
| 6620 | |
| 6621 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6622 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6623 | { |
| 6624 | struct http_msg *msg; |
| 6625 | |
| 6626 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6627 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6628 | |
| 6629 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6630 | WILL_LJMP(lua_error(L)); |
| 6631 | |
| 6632 | return hlua_http_add_hdr(L, msg); |
| 6633 | } |
| 6634 | |
| 6635 | /* Add an header in the HTTP message removing existing headers with the same |
| 6636 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6637 | */ |
| 6638 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6639 | { |
| 6640 | struct http_msg *msg; |
| 6641 | |
| 6642 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6643 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6644 | |
| 6645 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6646 | WILL_LJMP(lua_error(L)); |
| 6647 | |
| 6648 | hlua_http_del_hdr(L, msg); |
| 6649 | return hlua_http_add_hdr(L, msg); |
| 6650 | } |
| 6651 | |
| 6652 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6653 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6654 | { |
| 6655 | struct stream *s; |
| 6656 | struct http_msg *msg; |
| 6657 | const char *name; |
| 6658 | size_t name_len; |
| 6659 | |
| 6660 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6661 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6662 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6663 | |
| 6664 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6665 | WILL_LJMP(lua_error(L)); |
| 6666 | |
| 6667 | s = chn_strm(msg->chn); |
| 6668 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6669 | return 1; |
| 6670 | } |
| 6671 | |
| 6672 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6673 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6674 | { |
| 6675 | struct stream *s; |
| 6676 | struct http_msg *msg; |
| 6677 | const char *name; |
| 6678 | size_t name_len; |
| 6679 | |
| 6680 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6681 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6682 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6683 | |
| 6684 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6685 | WILL_LJMP(lua_error(L)); |
| 6686 | |
| 6687 | s = chn_strm(msg->chn); |
| 6688 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6689 | return 1; |
| 6690 | } |
| 6691 | |
| 6692 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6693 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6694 | { |
| 6695 | struct stream *s; |
| 6696 | struct http_msg *msg; |
| 6697 | const char *name; |
| 6698 | size_t name_len; |
| 6699 | |
| 6700 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6701 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6702 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6703 | |
| 6704 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6705 | WILL_LJMP(lua_error(L)); |
| 6706 | |
| 6707 | /* Check length. */ |
| 6708 | if (name_len > trash.size - 1) { |
| 6709 | lua_pushboolean(L, 0); |
| 6710 | return 1; |
| 6711 | } |
| 6712 | |
| 6713 | /* Add the mark question as prefix. */ |
| 6714 | chunk_reset(&trash); |
| 6715 | trash.area[trash.data++] = '?'; |
| 6716 | memcpy(trash.area + trash.data, name, name_len); |
| 6717 | trash.data += name_len; |
| 6718 | |
| 6719 | s = chn_strm(msg->chn); |
| 6720 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6721 | return 1; |
| 6722 | } |
| 6723 | |
| 6724 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6725 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6726 | { |
| 6727 | struct stream *s; |
| 6728 | struct http_msg *msg; |
| 6729 | const char *name; |
| 6730 | size_t name_len; |
| 6731 | |
| 6732 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6733 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6734 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6735 | |
| 6736 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6737 | WILL_LJMP(lua_error(L)); |
| 6738 | |
| 6739 | s = chn_strm(msg->chn); |
| 6740 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6741 | return 1; |
| 6742 | } |
| 6743 | |
| 6744 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6745 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6746 | { |
| 6747 | struct http_msg *msg; |
| 6748 | unsigned int code; |
| 6749 | const char *reason; |
| 6750 | size_t reason_len; |
| 6751 | |
| 6752 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6753 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6754 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6755 | |
| 6756 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6757 | WILL_LJMP(lua_error(L)); |
| 6758 | |
| 6759 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6760 | return 1; |
| 6761 | } |
| 6762 | |
| 6763 | /* Returns true if the HTTP message is full. */ |
| 6764 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6765 | { |
| 6766 | struct http_msg *msg; |
| 6767 | |
| 6768 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6769 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6770 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6771 | return 1; |
| 6772 | } |
| 6773 | |
| 6774 | /* Returns true if the HTTP message may still receive data. */ |
| 6775 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6776 | { |
| 6777 | struct http_msg *msg; |
| 6778 | struct htx *htx; |
| 6779 | |
| 6780 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6781 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6782 | htx = htxbuf(&msg->chn->buf); |
| 6783 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6784 | return 1; |
| 6785 | } |
| 6786 | |
| 6787 | /* Returns true if the HTTP message EOM was received */ |
| 6788 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6789 | { |
| 6790 | struct http_msg *msg; |
| 6791 | struct htx *htx; |
| 6792 | |
| 6793 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6794 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6795 | htx = htxbuf(&msg->chn->buf); |
| 6796 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6797 | return 1; |
| 6798 | } |
| 6799 | |
| 6800 | /* Returns the number of bytes available in the input side of the HTTP |
| 6801 | * message. This function never fails. |
| 6802 | */ |
| 6803 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6804 | { |
| 6805 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6806 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6807 | |
| 6808 | MAY_LJMP(check_args(L, 1, "input")); |
| 6809 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6810 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6811 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6812 | return 1; |
| 6813 | } |
| 6814 | |
| 6815 | /* Returns the number of bytes available in the output side of the HTTP |
| 6816 | * message. This function never fails. |
| 6817 | */ |
| 6818 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6819 | { |
| 6820 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6821 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6822 | |
| 6823 | MAY_LJMP(check_args(L, 1, "output")); |
| 6824 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6825 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6826 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6827 | return 1; |
| 6828 | } |
| 6829 | |
| 6830 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6831 | * starting at the offset <offset> and put it in a string LUA variables. It |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6832 | * returns the built string length. It stops on the first non-DATA HTX |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6833 | * block. This function is called during the payload filtering, so the headers |
| 6834 | * are already scheduled for output (from the filter point of view). |
| 6835 | */ |
| 6836 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6837 | { |
| 6838 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6839 | struct htx_blk *blk; |
| 6840 | struct htx_ret htxret; |
| 6841 | luaL_Buffer b; |
| 6842 | int ret = 0; |
| 6843 | |
| 6844 | luaL_buffinit(L, &b); |
| 6845 | htxret = htx_find_offset(htx, offset); |
| 6846 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6847 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6848 | struct ist v; |
| 6849 | |
| 6850 | switch (type) { |
| 6851 | case HTX_BLK_UNUSED: |
| 6852 | break; |
| 6853 | |
| 6854 | case HTX_BLK_DATA: |
| 6855 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6856 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6857 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6858 | |
| 6859 | luaL_addlstring(&b, v.ptr, v.len); |
| 6860 | ret += v.len; |
| 6861 | break; |
| 6862 | |
| 6863 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6864 | if (!ret) |
| 6865 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6866 | goto end; |
| 6867 | } |
| 6868 | offset = 0; |
| 6869 | } |
| 6870 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6871 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6872 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6873 | goto no_data; |
| 6874 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6875 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6876 | |
| 6877 | no_data: |
| 6878 | /* Remove the empty string and push nil on the stack */ |
| 6879 | lua_pop(L, 1); |
| 6880 | lua_pushnil(L); |
| 6881 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6885 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6886 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6887 | * the filter context. |
| 6888 | */ |
| 6889 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6890 | { |
| 6891 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6892 | struct htx_ret htxret; |
| 6893 | int /*max, */ret = 0; |
| 6894 | |
| 6895 | /* Nothing to do, just return */ |
| 6896 | if (unlikely(istlen(str) == 0)) |
| 6897 | goto end; |
| 6898 | |
| 6899 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6900 | ret = -1; |
| 6901 | goto end; |
| 6902 | } |
| 6903 | |
| 6904 | htxret = htx_find_offset(htx, offset); |
| 6905 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6906 | if (!htx_add_last_data(htx, str)) |
| 6907 | goto end; |
| 6908 | } |
| 6909 | else { |
| 6910 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6911 | v.ptr += htxret.ret; |
| 6912 | v.len = 0; |
| 6913 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6914 | goto end; |
| 6915 | } |
| 6916 | ret = str.len; |
| 6917 | if (ret) { |
| 6918 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6919 | flt_update_offsets(filter, msg->chn, ret); |
| 6920 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6921 | } |
| 6922 | |
| 6923 | end: |
| 6924 | htx_to_buf(htx, &msg->chn->buf); |
| 6925 | return ret; |
| 6926 | } |
| 6927 | |
| 6928 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6929 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6930 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6931 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6932 | * update the filter context. |
| 6933 | */ |
| 6934 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6935 | { |
| 6936 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6937 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6938 | struct htx_blk *blk; |
| 6939 | struct htx_ret htxret; |
| 6940 | size_t ret = 0; |
| 6941 | |
| 6942 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6943 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6944 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6945 | * of special blocks like HTX_BLK_EOT. |
| 6946 | * We simply truncate after offset |
| 6947 | * (truncate targeted blk and discard the following ones) |
| 6948 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6949 | htx_truncate(htx, offset); |
| 6950 | ret = len; |
| 6951 | goto end; |
| 6952 | } |
| 6953 | |
| 6954 | htxret = htx_find_offset(htx, offset); |
| 6955 | blk = htxret.blk; |
| 6956 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6957 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6958 | struct ist v; |
| 6959 | |
| 6960 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6961 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6962 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6963 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6964 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6965 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6966 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6967 | /* trimming data in blk: discard everything after the offset |
| 6968 | * (replace 'v' with 'IST_NULL') |
| 6969 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6970 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6971 | if (blk && v.len < len) { |
| 6972 | /* In this case, caller wants to keep removing data, |
| 6973 | * but we need to spare current blk |
| 6974 | * because it was already trimmed |
| 6975 | */ |
| 6976 | blk = htx_get_next_blk(htx, blk); |
| 6977 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6978 | len -= v.len; |
| 6979 | ret += v.len; |
| 6980 | } |
| 6981 | |
| 6982 | |
| 6983 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6984 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6985 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6986 | uint32_t sz = htx_get_blksz(blk); |
| 6987 | |
| 6988 | switch (type) { |
| 6989 | case HTX_BLK_UNUSED: |
| 6990 | break; |
| 6991 | |
| 6992 | case HTX_BLK_DATA: |
| 6993 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6994 | /* don't discard whole blk, only part of it |
| 6995 | * (from the beginning) |
| 6996 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6997 | htx_cut_data_blk(htx, blk, len); |
| 6998 | ret += len; |
| 6999 | goto end; |
| 7000 | } |
| 7001 | break; |
| 7002 | |
| 7003 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 7004 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7005 | goto end; |
| 7006 | } |
| 7007 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 7008 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7009 | len -= sz; |
| 7010 | ret += sz; |
| 7011 | blk = htx_remove_blk(htx, blk); |
| 7012 | } |
| 7013 | |
| 7014 | end: |
| 7015 | flt_update_offsets(filter, msg->chn, -ret); |
| 7016 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7017 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 7018 | * to loose the EOM flag if the message is empty. |
| 7019 | */ |
| 7020 | } |
| 7021 | |
| 7022 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 7023 | * to duplicate raw data, this one can only be called inside a filter, from |
| 7024 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 7025 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 7026 | * the stack. |
| 7027 | */ |
| 7028 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 7029 | { |
| 7030 | struct http_msg *msg; |
| 7031 | struct filter *filter; |
| 7032 | size_t output, input; |
| 7033 | int offset, len; |
| 7034 | |
| 7035 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 7036 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 7037 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7038 | |
| 7039 | if (msg->msg_state < HTTP_MSG_DATA) |
| 7040 | WILL_LJMP(lua_error(L)); |
| 7041 | |
| 7042 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 7043 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7044 | WILL_LJMP(lua_error(L)); |
| 7045 | |
| 7046 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 7047 | lua_pushnil(L); |
| 7048 | return 1; |
| 7049 | } |
| 7050 | |
| 7051 | offset = output; |
| 7052 | if (lua_gettop(L) > 1) { |
| 7053 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7054 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7055 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7056 | offset += output; |
| 7057 | if (offset < output || offset > input + output) { |
| 7058 | lua_pushfstring(L, "offset out of range."); |
| 7059 | WILL_LJMP(lua_error(L)); |
| 7060 | } |
| 7061 | } |
| 7062 | len = output + input - offset; |
| 7063 | if (lua_gettop(L) == 3) { |
| 7064 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 7065 | if (!len) |
| 7066 | goto dup; |
| 7067 | if (len == -1) |
| 7068 | len = global.tune.bufsize; |
| 7069 | if (len < 0) { |
| 7070 | lua_pushfstring(L, "length out of range."); |
| 7071 | WILL_LJMP(lua_error(L)); |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | dup: |
| 7076 | _hlua_http_msg_dup(msg, L, offset, len); |
| 7077 | return 1; |
| 7078 | } |
| 7079 | |
| 7080 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 7081 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 7082 | * nothing was copied. Unlike the channel function used to append data, this one |
| 7083 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 7084 | * yield. An exception is returned if it is called from another callback. |
| 7085 | */ |
| 7086 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 7087 | { |
| 7088 | struct http_msg *msg; |
| 7089 | struct filter *filter; |
| 7090 | const char *str; |
| 7091 | size_t offset, len, sz; |
| 7092 | int ret; |
| 7093 | |
| 7094 | MAY_LJMP(check_args(L, 2, "append")); |
| 7095 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7096 | |
| 7097 | if (msg->msg_state < HTTP_MSG_DATA) |
| 7098 | WILL_LJMP(lua_error(L)); |
| 7099 | |
| 7100 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7101 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7102 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7103 | WILL_LJMP(lua_error(L)); |
| 7104 | |
| 7105 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 7106 | lua_pushinteger(L, ret); |
| 7107 | return 1; |
| 7108 | } |
| 7109 | |
| 7110 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 7111 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 7112 | * channel function used to prepend data, this one can only be called inside a |
| 7113 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7114 | * returned if it is called from another callback. |
| 7115 | */ |
| 7116 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 7117 | { |
| 7118 | struct http_msg *msg; |
| 7119 | struct filter *filter; |
| 7120 | const char *str; |
| 7121 | size_t offset, len, sz; |
| 7122 | int ret; |
| 7123 | |
| 7124 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 7125 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7126 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7127 | if (msg->msg_state < HTTP_MSG_DATA) |
| 7128 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7129 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7130 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7131 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7132 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7133 | WILL_LJMP(lua_error(L)); |
| 7134 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7135 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7136 | lua_pushinteger(L, ret); |
| 7137 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7138 | } |
| 7139 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7140 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 7141 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 7142 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 7143 | * this one can only be called inside a filter, from http_payload callback. So |
| 7144 | * it cannot yield. An exception is returned if it is called from another |
| 7145 | * callback. |
| 7146 | */ |
| 7147 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7148 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7149 | struct http_msg *msg; |
| 7150 | struct filter *filter; |
| 7151 | const char *str; |
| 7152 | size_t input, output, sz; |
| 7153 | int offset; |
| 7154 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7155 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7156 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 7157 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7158 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7159 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7160 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7161 | WILL_LJMP(lua_error(L)); |
| 7162 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7163 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 7164 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7165 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7166 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7167 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 7168 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7169 | if (lua_gettop(L) > 2) { |
| 7170 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 7171 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7172 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7173 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 7174 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7175 | lua_pushfstring(L, "offset out of range."); |
| 7176 | WILL_LJMP(lua_error(L)); |
| 7177 | } |
| 7178 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7179 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7180 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7181 | lua_pushinteger(L, ret); |
| 7182 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7183 | } |
| 7184 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7185 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 7186 | * default all DATA blocks are removed. It returns the amount of data |
| 7187 | * removed. Unlike the channel function used to remove data, this one can only |
| 7188 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 7189 | * exception is returned if it is called from another callback. |
| 7190 | */ |
| 7191 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7192 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7193 | struct http_msg *msg; |
| 7194 | struct filter *filter; |
| 7195 | size_t input, output; |
| 7196 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7197 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7198 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 7199 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7200 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7201 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7202 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7203 | WILL_LJMP(lua_error(L)); |
| 7204 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 7205 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7206 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7207 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7208 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 7209 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 7210 | if (lua_gettop(L) > 1) { |
| 7211 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7212 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7213 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7214 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 7215 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7216 | lua_pushfstring(L, "offset out of range."); |
| 7217 | WILL_LJMP(lua_error(L)); |
| 7218 | } |
| 7219 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7220 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7221 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 7222 | if (lua_gettop(L) == 3) { |
| 7223 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7224 | if (!len) |
| 7225 | goto end; |
| 7226 | if (len == -1) |
| 7227 | len = output + input - offset; |
| 7228 | if (len < 0 || offset + len > output + input) { |
| 7229 | lua_pushfstring(L, "length out of range."); |
| 7230 | WILL_LJMP(lua_error(L)); |
| 7231 | } |
| 7232 | } |
| 7233 | |
| 7234 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7235 | |
| 7236 | end: |
| 7237 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7238 | return 1; |
| 7239 | } |
| 7240 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 7241 | /* Replaces a given amount of data at the given offset by a string. By default, |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7242 | * all remaining data are removed, accordingly to the filter context. It returns |
| 7243 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 7244 | * function used to replace data, this one can only be called inside a filter, |
| 7245 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 7246 | * it is called from another callback. |
| 7247 | */ |
| 7248 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7249 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7250 | struct http_msg *msg; |
| 7251 | struct filter *filter; |
| 7252 | struct htx *htx; |
| 7253 | const char *str; |
| 7254 | size_t input, output, sz; |
| 7255 | int offset, len; |
| 7256 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 7257 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7258 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 7259 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 7260 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7261 | |
| 7262 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7263 | WILL_LJMP(lua_error(L)); |
| 7264 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7265 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7266 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 7267 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7268 | WILL_LJMP(lua_error(L)); |
| 7269 | |
| 7270 | offset = output; |
| 7271 | if (lua_gettop(L) > 2) { |
| 7272 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 7273 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7274 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7275 | offset += output; |
| 7276 | if (offset < output || offset > input + output) { |
| 7277 | lua_pushfstring(L, "offset out of range."); |
| 7278 | WILL_LJMP(lua_error(L)); |
| 7279 | } |
| 7280 | } |
| 7281 | |
| 7282 | len = output + input - offset; |
| 7283 | if (lua_gettop(L) == 4) { |
| 7284 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7285 | if (!len) |
| 7286 | goto set; |
| 7287 | if (len == -1) |
| 7288 | len = output + input - offset; |
| 7289 | if (len < 0 || offset + len > output + input) { |
| 7290 | lua_pushfstring(L, "length out of range."); |
| 7291 | WILL_LJMP(lua_error(L)); |
| 7292 | } |
| 7293 | } |
| 7294 | |
| 7295 | set: |
| 7296 | /* Be sure we can copied the string once input data will be removed. */ |
| 7297 | htx = htx_from_buf(&msg->chn->buf); |
| 7298 | if (sz > htx_free_data_space(htx) + len) |
| 7299 | lua_pushinteger(L, -1); |
| 7300 | else { |
| 7301 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7302 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7303 | lua_pushinteger(L, ret); |
| 7304 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7305 | return 1; |
| 7306 | } |
| 7307 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7308 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 7309 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 7310 | * the channel function used to send data, this one can only be called inside a |
| 7311 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7312 | * returned if it is called from another callback. |
| 7313 | */ |
| 7314 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7315 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7316 | struct http_msg *msg; |
| 7317 | struct filter *filter; |
| 7318 | struct htx *htx; |
| 7319 | const char *str; |
| 7320 | size_t offset, len, sz; |
| 7321 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7322 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7323 | MAY_LJMP(check_args(L, 2, "send")); |
| 7324 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7325 | |
| 7326 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7327 | WILL_LJMP(lua_error(L)); |
| 7328 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7329 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7330 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7331 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7332 | WILL_LJMP(lua_error(L)); |
| 7333 | |
| 7334 | /* Return an error if the channel's output is closed */ |
| 7335 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7336 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7337 | return 1; |
| 7338 | } |
| 7339 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7340 | htx = htx_from_buf(&msg->chn->buf); |
| 7341 | if (sz > htx_free_data_space(htx)) { |
| 7342 | lua_pushinteger(L, -1); |
| 7343 | return 1; |
| 7344 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7345 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7346 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7347 | if (ret > 0) { |
| 7348 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7349 | |
| 7350 | FLT_OFF(filter, msg->chn) += ret; |
| 7351 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7352 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7353 | } |
| 7354 | |
| 7355 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7356 | return 1; |
| 7357 | } |
| 7358 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7359 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 7360 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 7361 | * channel function used to forward data, this one can only be called inside a |
| 7362 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7363 | * returned if it is called from another callback. All other functions deal with |
| 7364 | * DATA block, this one not. |
| 7365 | */ |
| 7366 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7367 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7368 | struct http_msg *msg; |
| 7369 | struct filter *filter; |
| 7370 | size_t offset, len; |
| 7371 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7372 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7373 | MAY_LJMP(check_args(L, 2, "forward")); |
| 7374 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7375 | |
| 7376 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7377 | WILL_LJMP(lua_error(L)); |
| 7378 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7379 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7380 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7381 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7382 | WILL_LJMP(lua_error(L)); |
| 7383 | |
| 7384 | /* Nothing to do, just return */ |
| 7385 | if (!fwd) |
| 7386 | goto end; |
| 7387 | |
| 7388 | /* Return an error if the channel's output is closed */ |
| 7389 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7390 | ret = -1; |
| 7391 | goto end; |
| 7392 | } |
| 7393 | |
| 7394 | ret = fwd; |
| 7395 | if (ret > len) |
| 7396 | ret = len; |
| 7397 | |
| 7398 | if (ret) { |
| 7399 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7400 | |
| 7401 | FLT_OFF(filter, msg->chn) += ret; |
| 7402 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7403 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7404 | } |
| 7405 | |
| 7406 | end: |
| 7407 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7408 | return 1; |
| 7409 | } |
| 7410 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7411 | /* Set EOM flag on the HTX message. |
| 7412 | * |
| 7413 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7414 | * really know how to do without this feature. |
| 7415 | */ |
| 7416 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7417 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7418 | struct http_msg *msg; |
| 7419 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7420 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7421 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7422 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7423 | htx = htxbuf(&msg->chn->buf); |
| 7424 | htx->flags |= HTX_FL_EOM; |
| 7425 | return 0; |
| 7426 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7427 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7428 | /* Unset EOM flag on the HTX message. |
| 7429 | * |
| 7430 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7431 | * really know how to do without this feature. |
| 7432 | */ |
| 7433 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7434 | { |
| 7435 | struct http_msg *msg; |
| 7436 | struct htx *htx; |
| 7437 | |
| 7438 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7439 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7440 | htx = htxbuf(&msg->chn->buf); |
| 7441 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7442 | return 0; |
| 7443 | } |
| 7444 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7445 | /* |
| 7446 | * |
| 7447 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7448 | * Class HTTPClient |
| 7449 | * |
| 7450 | * |
| 7451 | */ |
| 7452 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7453 | { |
| 7454 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7455 | } |
| 7456 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7457 | |
| 7458 | /* stops the httpclient and ask it to kill itself */ |
| 7459 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7460 | { |
| 7461 | struct hlua_httpclient *hlua_hc; |
| 7462 | |
| 7463 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7464 | |
| 7465 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7466 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7467 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7468 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7469 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7470 | hlua_hc->hc = NULL; |
| 7471 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7472 | |
| 7473 | return 0; |
| 7474 | } |
| 7475 | |
| 7476 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7477 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7478 | { |
| 7479 | struct hlua_httpclient *hlua_hc; |
| 7480 | struct hlua *hlua; |
| 7481 | |
| 7482 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7483 | hlua = hlua_gethlua(L); |
| 7484 | if (!hlua) |
| 7485 | return 0; |
| 7486 | |
| 7487 | /* Check stack size. */ |
| 7488 | if (!lua_checkstack(L, 3)) { |
| 7489 | hlua_pusherror(L, "httpclient: full stack"); |
| 7490 | goto err; |
| 7491 | } |
| 7492 | /* Create the object: obj[0] = userdata. */ |
| 7493 | lua_newtable(L); |
| 7494 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7495 | lua_rawseti(L, -2, 0); |
| 7496 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7497 | |
| 7498 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7499 | if (!hlua_hc->hc) |
| 7500 | goto err; |
| 7501 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7502 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7503 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7504 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7505 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7506 | lua_setmetatable(L, -2); |
| 7507 | |
| 7508 | return 1; |
| 7509 | |
| 7510 | err: |
| 7511 | WILL_LJMP(lua_error(L)); |
| 7512 | return 0; |
| 7513 | } |
| 7514 | |
| 7515 | |
| 7516 | /* |
| 7517 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7518 | * httpclient receives some data |
| 7519 | * |
| 7520 | */ |
| 7521 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7522 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7523 | { |
| 7524 | struct hlua *hlua = hc->caller; |
| 7525 | |
| 7526 | if (!hlua || !hlua->task) |
| 7527 | return; |
| 7528 | |
| 7529 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7530 | } |
| 7531 | |
| 7532 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7533 | * Fill the lua stack with headers from the httpclient response |
| 7534 | * This works the same way as the hlua_http_get_headers() function |
| 7535 | */ |
| 7536 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7537 | { |
| 7538 | struct http_hdr *hdr; |
| 7539 | |
| 7540 | lua_newtable(L); |
| 7541 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7542 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7543 | struct ist n, v; |
| 7544 | int len; |
| 7545 | |
| 7546 | n = hdr->n; |
| 7547 | v = hdr->v; |
| 7548 | |
| 7549 | /* Check for existing entry: |
| 7550 | * assume that the table is on the top of the stack, and |
| 7551 | * push the key in the stack, the function lua_gettable() |
| 7552 | * perform the lookup. |
| 7553 | */ |
| 7554 | |
| 7555 | lua_pushlstring(L, n.ptr, n.len); |
| 7556 | lua_gettable(L, -2); |
| 7557 | |
| 7558 | switch (lua_type(L, -1)) { |
| 7559 | case LUA_TNIL: |
| 7560 | /* Table not found, create it. */ |
| 7561 | lua_pop(L, 1); /* remove the nil value. */ |
| 7562 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7563 | lua_newtable(L); /* create and push empty table. */ |
| 7564 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7565 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7566 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7567 | break; |
| 7568 | |
| 7569 | case LUA_TTABLE: |
| 7570 | /* Entry found: push the value in the table. */ |
| 7571 | len = lua_rawlen(L, -1); |
| 7572 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7573 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7574 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7575 | break; |
| 7576 | |
| 7577 | default: |
| 7578 | /* Other cases are errors. */ |
| 7579 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7580 | WILL_LJMP(lua_error(L)); |
| 7581 | } |
| 7582 | } |
| 7583 | return 1; |
| 7584 | } |
| 7585 | |
| 7586 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7587 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7588 | * |
| 7589 | * Caller must free the result |
| 7590 | */ |
| 7591 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7592 | { |
| 7593 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7594 | struct http_hdr *result = NULL; |
| 7595 | uint32_t hdr_num = 0; |
| 7596 | |
| 7597 | lua_pushnil(L); |
| 7598 | while (lua_next(L, -2) != 0) { |
| 7599 | struct ist name, value; |
| 7600 | const char *n, *v; |
| 7601 | size_t nlen, vlen; |
| 7602 | |
| 7603 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7604 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7605 | goto next_hdr; |
| 7606 | } |
| 7607 | |
| 7608 | n = lua_tolstring(L, -2, &nlen); |
| 7609 | name = ist2(n, nlen); |
| 7610 | |
| 7611 | /* Loop on header's values */ |
| 7612 | lua_pushnil(L); |
| 7613 | while (lua_next(L, -2)) { |
| 7614 | if (!lua_isstring(L, -1)) { |
| 7615 | /* Skip the value if it is not a string */ |
| 7616 | goto next_value; |
| 7617 | } |
| 7618 | |
| 7619 | v = lua_tolstring(L, -1, &vlen); |
| 7620 | value = ist2(v, vlen); |
| 7621 | name = ist2(n, nlen); |
| 7622 | |
| 7623 | hdrs[hdr_num].n = istdup(name); |
| 7624 | hdrs[hdr_num].v = istdup(value); |
| 7625 | |
| 7626 | hdr_num++; |
| 7627 | |
| 7628 | next_value: |
| 7629 | lua_pop(L, 1); |
| 7630 | } |
| 7631 | |
| 7632 | next_hdr: |
| 7633 | lua_pop(L, 1); |
| 7634 | |
| 7635 | } |
| 7636 | |
| 7637 | if (hdr_num) { |
| 7638 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7639 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7640 | if (!result) |
| 7641 | goto skip_headers; |
| 7642 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7643 | |
| 7644 | result[hdr_num].n = IST_NULL; |
| 7645 | result[hdr_num].v = IST_NULL; |
| 7646 | } |
| 7647 | |
| 7648 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7649 | |
| 7650 | return result; |
| 7651 | } |
| 7652 | |
| 7653 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7654 | /* |
| 7655 | * For each yield, checks if there is some data in the httpclient and push them |
| 7656 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7657 | * the stack |
| 7658 | */ |
| 7659 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7660 | { |
| 7661 | struct buffer *tr; |
| 7662 | int res; |
| 7663 | struct hlua *hlua = hlua_gethlua(L); |
| 7664 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7665 | |
| 7666 | |
| 7667 | tr = get_trash_chunk(); |
| 7668 | |
| 7669 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7670 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7671 | |
| 7672 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7673 | |
| 7674 | luaL_pushresult(&hlua_hc->b); |
| 7675 | lua_settable(L, -3); |
| 7676 | |
| 7677 | lua_pushstring(L, "status"); |
| 7678 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7679 | lua_settable(L, -3); |
| 7680 | |
| 7681 | |
| 7682 | lua_pushstring(L, "reason"); |
| 7683 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7684 | lua_settable(L, -3); |
| 7685 | |
| 7686 | lua_pushstring(L, "headers"); |
| 7687 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7688 | lua_settable(L, -3); |
| 7689 | |
| 7690 | return 1; |
| 7691 | } |
| 7692 | |
| 7693 | if (httpclient_data(hlua_hc->hc)) |
| 7694 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7695 | |
| 7696 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7697 | |
| 7698 | return 0; |
| 7699 | } |
| 7700 | |
| 7701 | /* |
| 7702 | * Call this when trying to stream a body during a request |
| 7703 | */ |
| 7704 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7705 | { |
| 7706 | struct hlua *hlua; |
| 7707 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7708 | const char *body_str = NULL; |
| 7709 | int ret; |
| 7710 | int end = 0; |
| 7711 | size_t buf_len; |
| 7712 | size_t to_send = 0; |
| 7713 | |
| 7714 | hlua = hlua_gethlua(L); |
| 7715 | |
| 7716 | if (!hlua || !hlua->task) |
| 7717 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7718 | "'frontend', 'backend' or 'task'")); |
| 7719 | |
| 7720 | ret = lua_getfield(L, -1, "body"); |
| 7721 | if (ret != LUA_TSTRING) |
| 7722 | goto rcv; |
| 7723 | |
| 7724 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7725 | lua_pop(L, 1); |
| 7726 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7727 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7728 | |
| 7729 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7730 | end = 1; |
| 7731 | |
| 7732 | /* the end flag is always set since we are using the whole remaining size */ |
| 7733 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7734 | |
| 7735 | if (buf_len > hlua_hc->sent) { |
| 7736 | /* still need to process the buffer */ |
| 7737 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7738 | } else { |
| 7739 | goto rcv; |
| 7740 | /* we sent the whole request buffer we can recv */ |
| 7741 | } |
| 7742 | return 0; |
| 7743 | |
| 7744 | rcv: |
| 7745 | |
| 7746 | /* we return a "res" object */ |
| 7747 | lua_newtable(L); |
| 7748 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7749 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7750 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7751 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7752 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7753 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7754 | |
| 7755 | return 1; |
| 7756 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7757 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7758 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7759 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7760 | */ |
| 7761 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7762 | __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7763 | { |
| 7764 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7765 | struct http_hdr *hdrs = NULL; |
| 7766 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7767 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7768 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7769 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7770 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7771 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7772 | |
| 7773 | hlua = hlua_gethlua(L); |
| 7774 | |
| 7775 | if (!hlua || !hlua->task) |
| 7776 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7777 | "'frontend', 'backend' or 'task'")); |
| 7778 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7779 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7780 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7781 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7782 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7783 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7784 | lua_pushnil(L); /* first key */ |
| 7785 | while (lua_next(L, 2)) { |
| 7786 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7787 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7788 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7789 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7790 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7791 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7792 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7793 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7794 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7795 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7796 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7797 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7798 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7799 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7800 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7801 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7802 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7803 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7804 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7805 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7806 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7807 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7808 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7809 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7810 | } else { |
| 7811 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7812 | } |
| 7813 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7814 | lua_pop(L, 1); |
| 7815 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7816 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7817 | if (!url_str) { |
| 7818 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7819 | return 0; |
| 7820 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7821 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7822 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7823 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7824 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7825 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7826 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7827 | |
| 7828 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7829 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7830 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7831 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7832 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7833 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7834 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7835 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7836 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7837 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7838 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7839 | ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7840 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7841 | /* free the temporary headers array */ |
| 7842 | hdrs_i = hdrs; |
| 7843 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7844 | istfree(&hdrs_i->n); |
| 7845 | istfree(&hdrs_i->v); |
| 7846 | hdrs_i++; |
| 7847 | } |
| 7848 | ha_free(&hdrs); |
| 7849 | |
| 7850 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7851 | if (ret != ERR_NONE) { |
| 7852 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7853 | return 0; |
| 7854 | } |
| 7855 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7856 | if (!httpclient_start(hlua_hc->hc)) |
| 7857 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7858 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7859 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7860 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7861 | return 0; |
| 7862 | } |
| 7863 | |
| 7864 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7865 | * Sends an HTTP HEAD request and wait for a response |
| 7866 | * |
| 7867 | * httpclient:head(url, headers, payload) |
| 7868 | */ |
| 7869 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7870 | { |
| 7871 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7872 | } |
| 7873 | |
| 7874 | /* |
| 7875 | * Send an HTTP GET request and wait for a response |
| 7876 | * |
| 7877 | * httpclient:get(url, headers, payload) |
| 7878 | */ |
| 7879 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7880 | { |
| 7881 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7882 | |
| 7883 | } |
| 7884 | |
| 7885 | /* |
| 7886 | * Sends an HTTP PUT request and wait for a response |
| 7887 | * |
| 7888 | * httpclient:put(url, headers, payload) |
| 7889 | */ |
| 7890 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7891 | { |
| 7892 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7893 | } |
| 7894 | |
| 7895 | /* |
| 7896 | * Send an HTTP POST request and wait for a response |
| 7897 | * |
| 7898 | * httpclient:post(url, headers, payload) |
| 7899 | */ |
| 7900 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7901 | { |
| 7902 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7903 | } |
| 7904 | |
| 7905 | |
| 7906 | /* |
| 7907 | * Sends an HTTP DELETE request and wait for a response |
| 7908 | * |
| 7909 | * httpclient:delete(url, headers, payload) |
| 7910 | */ |
| 7911 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7912 | { |
| 7913 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7914 | } |
| 7915 | |
| 7916 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7917 | * |
| 7918 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7919 | * Class TXN |
| 7920 | * |
| 7921 | * |
| 7922 | */ |
| 7923 | |
| 7924 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7925 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7926 | */ |
| 7927 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7928 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7929 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7930 | } |
| 7931 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7932 | __LJMP static int hlua_set_var(lua_State *L) |
| 7933 | { |
| 7934 | struct hlua_txn *htxn; |
| 7935 | const char *name; |
| 7936 | size_t len; |
| 7937 | struct sample smp; |
| 7938 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7939 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7940 | 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] | 7941 | |
| 7942 | /* It is useles to retrieve the stream, but this function |
| 7943 | * runs only in a stream context. |
| 7944 | */ |
| 7945 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7946 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7947 | |
| 7948 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7949 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7950 | hlua_lua2smp(L, 3, &smp); |
| 7951 | |
Aurelien DARRAGON | 1c07da4 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 7952 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 7953 | * already takes care of duplicating dynamic var data. |
| 7954 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7955 | 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] | 7956 | |
| 7957 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7958 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7959 | else |
| 7960 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7961 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7962 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7963 | } |
| 7964 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7965 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7966 | { |
| 7967 | struct hlua_txn *htxn; |
| 7968 | const char *name; |
| 7969 | size_t len; |
| 7970 | struct sample smp; |
| 7971 | |
| 7972 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7973 | |
| 7974 | /* It is useles to retrieve the stream, but this function |
| 7975 | * runs only in a stream context. |
| 7976 | */ |
| 7977 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7978 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7979 | |
| 7980 | /* Unset the variable. */ |
| 7981 | 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] | 7982 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7983 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7984 | } |
| 7985 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7986 | __LJMP static int hlua_get_var(lua_State *L) |
| 7987 | { |
| 7988 | struct hlua_txn *htxn; |
| 7989 | const char *name; |
| 7990 | size_t len; |
| 7991 | struct sample smp; |
| 7992 | |
| 7993 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7994 | |
| 7995 | /* It is useles to retrieve the stream, but this function |
| 7996 | * runs only in a stream context. |
| 7997 | */ |
| 7998 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7999 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 8000 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 8001 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 8002 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 8003 | lua_pushnil(L); |
| 8004 | return 1; |
| 8005 | } |
| 8006 | |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 8007 | return MAY_LJMP(hlua_smp2lua(L, &smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 8008 | } |
| 8009 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8010 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8011 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8012 | struct hlua *hlua; |
| 8013 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8014 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 8015 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8016 | /* It is useles to retrieve the stream, but this function |
| 8017 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8018 | */ |
| 8019 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8020 | |
| 8021 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8022 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8023 | if (!hlua) |
| 8024 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8025 | |
| 8026 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 8027 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8028 | |
| 8029 | /* Get and store new value. */ |
| 8030 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 8031 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 8032 | |
| 8033 | return 0; |
| 8034 | } |
| 8035 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8036 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8037 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8038 | struct hlua *hlua; |
| 8039 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8040 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 8041 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8042 | /* It is useles to retrieve the stream, but this function |
| 8043 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8044 | */ |
| 8045 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8046 | |
| 8047 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8048 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8049 | if (!hlua) { |
| 8050 | lua_pushnil(L); |
| 8051 | return 1; |
| 8052 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8053 | |
| 8054 | /* Push configuration index in the stack. */ |
| 8055 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 8056 | |
| 8057 | return 1; |
| 8058 | } |
| 8059 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8060 | /* Create stack entry containing a class TXN. This function |
| 8061 | * return 0 if the stack does not contains free slots, |
| 8062 | * otherwise it returns 1. |
| 8063 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8064 | 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] | 8065 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 8066 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8067 | |
| 8068 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 8069 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8070 | return 0; |
| 8071 | |
| 8072 | /* NOTE: The allocation never fails. The failure |
| 8073 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8074 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8075 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 8076 | /* Create the object: obj[0] = userdata. */ |
| 8077 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 8078 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 8079 | lua_rawseti(L, -2, 0); |
| 8080 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 8081 | htxn->s = s; |
| 8082 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 8083 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8084 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8085 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8086 | /* Create the "f" field that contains a list of fetches. */ |
| 8087 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 8088 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 8089 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8090 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 8091 | |
| 8092 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 8093 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 8094 | 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] | 8095 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8096 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8097 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8098 | /* Create the "c" field that contains a list of converters. */ |
| 8099 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 8100 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 8101 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8102 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 8103 | |
| 8104 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 8105 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 8106 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8107 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8108 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8109 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 8110 | /* Create the "req" field that contains the request channel object. */ |
| 8111 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 8112 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 8113 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8114 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 8115 | |
| 8116 | /* Create the "res" field that contains the response channel object. */ |
| 8117 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 8118 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 8119 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8120 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 8121 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8122 | /* Creates the HTTP object is the current proxy allows http. */ |
| 8123 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 8124 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 8125 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8126 | return 0; |
| 8127 | } |
| 8128 | else |
| 8129 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8130 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8131 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 8132 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 8133 | /* HTTPMessage object are created when a lua TXN is created from |
| 8134 | * a filter context only |
| 8135 | */ |
| 8136 | |
| 8137 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 8138 | lua_pushstring(L, "http_req"); |
| 8139 | if (p->mode == PR_MODE_HTTP) { |
| 8140 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 8141 | return 0; |
| 8142 | } |
| 8143 | else |
| 8144 | lua_pushnil(L); |
| 8145 | lua_rawset(L, -3); |
| 8146 | |
| 8147 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 8148 | lua_pushstring(L, "http_res"); |
| 8149 | if (p->mode == PR_MODE_HTTP) { |
| 8150 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 8151 | return 0; |
| 8152 | } |
| 8153 | else |
| 8154 | lua_pushnil(L); |
| 8155 | lua_rawset(L, -3); |
| 8156 | } |
| 8157 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8158 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8159 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 8160 | lua_setmetatable(L, -2); |
| 8161 | |
| 8162 | return 1; |
| 8163 | } |
| 8164 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8165 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 8166 | { |
| 8167 | const char *msg; |
| 8168 | struct hlua_txn *htxn; |
| 8169 | |
| 8170 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 8171 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8172 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8173 | |
| 8174 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 8175 | return 0; |
| 8176 | } |
| 8177 | |
| 8178 | __LJMP static int hlua_txn_log(lua_State *L) |
| 8179 | { |
| 8180 | int level; |
| 8181 | const char *msg; |
| 8182 | struct hlua_txn *htxn; |
| 8183 | |
| 8184 | MAY_LJMP(check_args(L, 3, "log")); |
| 8185 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8186 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8187 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8188 | |
| 8189 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8190 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8191 | |
| 8192 | hlua_sendlog(htxn->s->be, level, msg); |
| 8193 | return 0; |
| 8194 | } |
| 8195 | |
| 8196 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 8197 | { |
| 8198 | const char *msg; |
| 8199 | struct hlua_txn *htxn; |
| 8200 | |
| 8201 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 8202 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8203 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8204 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 8205 | return 0; |
| 8206 | } |
| 8207 | |
| 8208 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 8209 | { |
| 8210 | const char *msg; |
| 8211 | struct hlua_txn *htxn; |
| 8212 | |
| 8213 | MAY_LJMP(check_args(L, 2, "Info")); |
| 8214 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8215 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8216 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 8217 | return 0; |
| 8218 | } |
| 8219 | |
| 8220 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 8221 | { |
| 8222 | const char *msg; |
| 8223 | struct hlua_txn *htxn; |
| 8224 | |
| 8225 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 8226 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8227 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8228 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 8229 | return 0; |
| 8230 | } |
| 8231 | |
| 8232 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 8233 | { |
| 8234 | const char *msg; |
| 8235 | struct hlua_txn *htxn; |
| 8236 | |
| 8237 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 8238 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8239 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8240 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 8241 | return 0; |
| 8242 | } |
| 8243 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8244 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 8245 | { |
| 8246 | struct hlua_txn *htxn; |
| 8247 | int ll; |
| 8248 | |
| 8249 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 8250 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8251 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8252 | |
Christopher Faulet | 12a5ee7 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 8253 | if (ll < -1 || ll > NB_LOG_LEVELS) |
| 8254 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be one of the following value:" |
| 8255 | " core.silent(-1), core.emerg(0), core.alert(1), core.crit(2), core.error(3)," |
| 8256 | " core.warning(4), core.notice(5), core.info(6) or core.debug(7)")); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8257 | |
Christopher Faulet | 12a5ee7 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 8258 | htxn->s->logs.level = (ll == -1) ? ll : ll + 1; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8259 | return 0; |
| 8260 | } |
| 8261 | |
| 8262 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 8263 | { |
| 8264 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8265 | int tos; |
| 8266 | |
| 8267 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 8268 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8269 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8270 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 8271 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8272 | return 0; |
| 8273 | } |
| 8274 | |
| 8275 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 8276 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8277 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8278 | int mark; |
| 8279 | |
| 8280 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 8281 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8282 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8283 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 8284 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8285 | return 0; |
| 8286 | } |
| 8287 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8288 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 8289 | { |
| 8290 | struct hlua_txn *htxn; |
| 8291 | |
| 8292 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 8293 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8294 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8295 | return 0; |
| 8296 | } |
| 8297 | |
| 8298 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 8299 | { |
| 8300 | struct hlua_txn *htxn; |
| 8301 | |
| 8302 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 8303 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8304 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8305 | return 0; |
| 8306 | } |
| 8307 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8308 | /* 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] | 8309 | * 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] | 8310 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 8311 | * error. The Reply must be on top of the stack. |
| 8312 | */ |
| 8313 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 8314 | { |
| 8315 | struct htx *htx; |
| 8316 | struct htx_sl *sl; |
| 8317 | struct h1m h1m; |
| 8318 | const char *status, *reason, *body; |
| 8319 | size_t status_len, reason_len, body_len; |
| 8320 | int ret, code, flags; |
| 8321 | |
| 8322 | code = 200; |
| 8323 | status = "200"; |
| 8324 | status_len = 3; |
| 8325 | ret = lua_getfield(L, -1, "status"); |
| 8326 | if (ret == LUA_TNUMBER) { |
| 8327 | code = lua_tointeger(L, -1); |
| 8328 | status = lua_tolstring(L, -1, &status_len); |
| 8329 | } |
| 8330 | lua_pop(L, 1); |
| 8331 | |
| 8332 | reason = http_get_reason(code); |
| 8333 | reason_len = strlen(reason); |
| 8334 | ret = lua_getfield(L, -1, "reason"); |
| 8335 | if (ret == LUA_TSTRING) |
| 8336 | reason = lua_tolstring(L, -1, &reason_len); |
| 8337 | lua_pop(L, 1); |
| 8338 | |
| 8339 | body = NULL; |
| 8340 | body_len = 0; |
| 8341 | ret = lua_getfield(L, -1, "body"); |
| 8342 | if (ret == LUA_TSTRING) |
| 8343 | body = lua_tolstring(L, -1, &body_len); |
| 8344 | lua_pop(L, 1); |
| 8345 | |
| 8346 | /* Prepare the response before inserting the headers */ |
| 8347 | h1m_init_res(&h1m); |
| 8348 | htx = htx_from_buf(&s->res.buf); |
| 8349 | channel_htx_truncate(&s->res, htx); |
| 8350 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 8351 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 8352 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 8353 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8354 | } |
| 8355 | else { |
| 8356 | flags = HTX_SL_F_IS_RESP; |
| 8357 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 8358 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8359 | } |
| 8360 | if (!sl) |
| 8361 | goto fail; |
| 8362 | sl->info.res.status = code; |
| 8363 | |
| 8364 | /* Push in the stack the "headers" entry. */ |
| 8365 | ret = lua_getfield(L, -1, "headers"); |
| 8366 | if (ret != LUA_TTABLE) |
| 8367 | goto skip_headers; |
| 8368 | |
| 8369 | lua_pushnil(L); |
| 8370 | while (lua_next(L, -2) != 0) { |
| 8371 | struct ist name, value; |
| 8372 | const char *n, *v; |
| 8373 | size_t nlen, vlen; |
| 8374 | |
| 8375 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 8376 | /* Skip element if the key is not a string or if the value is not a table */ |
| 8377 | goto next_hdr; |
| 8378 | } |
| 8379 | |
| 8380 | n = lua_tolstring(L, -2, &nlen); |
| 8381 | name = ist2(n, nlen); |
| 8382 | if (isteqi(name, ist("content-length"))) { |
| 8383 | /* Always skip content-length header. It will be added |
| 8384 | * later with the correct len |
| 8385 | */ |
| 8386 | goto next_hdr; |
| 8387 | } |
| 8388 | |
| 8389 | /* Loop on header's values */ |
| 8390 | lua_pushnil(L); |
| 8391 | while (lua_next(L, -2)) { |
| 8392 | if (!lua_isstring(L, -1)) { |
| 8393 | /* Skip the value if it is not a string */ |
| 8394 | goto next_value; |
| 8395 | } |
| 8396 | |
| 8397 | v = lua_tolstring(L, -1, &vlen); |
| 8398 | value = ist2(v, vlen); |
| 8399 | |
| 8400 | if (isteqi(name, ist("transfer-encoding"))) |
| 8401 | h1_parse_xfer_enc_header(&h1m, value); |
| 8402 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8403 | goto fail; |
| 8404 | |
| 8405 | next_value: |
| 8406 | lua_pop(L, 1); |
| 8407 | } |
| 8408 | |
| 8409 | next_hdr: |
| 8410 | lua_pop(L, 1); |
| 8411 | } |
| 8412 | skip_headers: |
| 8413 | lua_pop(L, 1); |
| 8414 | |
| 8415 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8416 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8417 | const char *clen = ultoa(body_len); |
| 8418 | |
| 8419 | h1m.flags |= H1_MF_CLEN; |
| 8420 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8421 | goto fail; |
| 8422 | } |
| 8423 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8424 | h1m.flags |= H1_MF_XFER_LEN; |
| 8425 | |
| 8426 | /* Update HTX start-line flags */ |
| 8427 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8428 | flags |= HTX_SL_F_XFER_ENC; |
| 8429 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8430 | flags |= HTX_SL_F_XFER_LEN; |
| 8431 | if (h1m.flags & H1_MF_CHNK) |
| 8432 | flags |= HTX_SL_F_CHNK; |
| 8433 | else if (h1m.flags & H1_MF_CLEN) |
| 8434 | flags |= HTX_SL_F_CLEN; |
| 8435 | if (h1m.body_len == 0) |
| 8436 | flags |= HTX_SL_F_BODYLESS; |
| 8437 | } |
| 8438 | sl->flags |= flags; |
| 8439 | |
| 8440 | |
| 8441 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8442 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8443 | goto fail; |
| 8444 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8445 | htx->flags |= HTX_FL_EOM; |
| 8446 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8447 | /* Now, forward the response and terminate the transaction */ |
| 8448 | s->txn->status = code; |
| 8449 | htx_to_buf(htx, &s->res.buf); |
| 8450 | if (!http_forward_proxy_resp(s, 1)) |
| 8451 | goto fail; |
| 8452 | |
| 8453 | return 1; |
| 8454 | |
| 8455 | fail: |
| 8456 | channel_htx_truncate(&s->res, htx); |
| 8457 | return 0; |
| 8458 | } |
| 8459 | |
| 8460 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8461 | * processing is just aborted. Nothing is returned to the client and all |
| 8462 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8463 | * is forwarded to the client before terminating the transaction. On success, |
| 8464 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8465 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8466 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8467 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8468 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8469 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8470 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8471 | struct stream *s; |
| 8472 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8473 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8474 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8475 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8476 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8477 | * just end the execution of the current lua code. */ |
| 8478 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8479 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8480 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8481 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8482 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8483 | struct channel *req = &s->req; |
| 8484 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8485 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8486 | channel_auto_read(req); |
| 8487 | channel_abort(req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8488 | channel_erase(req); |
| 8489 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8490 | channel_auto_read(res); |
| 8491 | channel_auto_close(res); |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 8492 | sc_schedule_abort(s->scb); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8493 | |
| 8494 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8495 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8496 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8497 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8498 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8499 | /* No reply or invalid reply */ |
| 8500 | s->txn->status = 0; |
| 8501 | http_reply_and_close(s, 0, NULL); |
| 8502 | } |
| 8503 | else { |
| 8504 | /* Remove extra args to have the reply on top of the stack */ |
| 8505 | if (lua_gettop(L) > 2) |
| 8506 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8507 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8508 | if (!hlua_txn_forward_reply(L, s)) { |
| 8509 | if (!(s->flags & SF_ERR_MASK)) |
| 8510 | s->flags |= SF_ERR_PRXCOND; |
| 8511 | lua_pushinteger(L, ACT_RET_ERR); |
| 8512 | WILL_LJMP(hlua_done(L)); |
| 8513 | return 0; /* Never reached */ |
| 8514 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8515 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8516 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8517 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8518 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8519 | /* let's log the request time */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 8520 | s->logs.request_ts = now_ns; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8521 | 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] | 8522 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8523 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8524 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8525 | done: |
| 8526 | if (!(s->flags & SF_ERR_MASK)) |
| 8527 | s->flags |= SF_ERR_LOCAL; |
| 8528 | if (!(s->flags & SF_FINST_MASK)) |
| 8529 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8530 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8531 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8532 | lua_pushinteger(L, -1); |
| 8533 | else |
| 8534 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8535 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8536 | return 0; |
| 8537 | } |
| 8538 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8539 | /* |
| 8540 | * |
| 8541 | * |
| 8542 | * Class REPLY |
| 8543 | * |
| 8544 | * |
| 8545 | */ |
| 8546 | |
| 8547 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8548 | * free slots, the function fails and returns 0; |
| 8549 | */ |
| 8550 | static int hlua_txn_reply_new(lua_State *L) |
| 8551 | { |
| 8552 | struct hlua_txn *htxn; |
| 8553 | const char *reason, *body = NULL; |
| 8554 | int ret, status; |
| 8555 | |
| 8556 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8557 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8558 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8559 | WILL_LJMP(lua_error(L)); |
| 8560 | } |
| 8561 | |
| 8562 | /* Default value */ |
| 8563 | status = 200; |
| 8564 | reason = http_get_reason(status); |
| 8565 | |
| 8566 | if (lua_istable(L, 2)) { |
| 8567 | /* load status and reason from the table argument at index 2 */ |
| 8568 | ret = lua_getfield(L, 2, "status"); |
| 8569 | if (ret == LUA_TNIL) |
| 8570 | goto reason; |
| 8571 | else if (ret != LUA_TNUMBER) { |
| 8572 | /* invalid status: ignore the reason */ |
| 8573 | goto body; |
| 8574 | } |
| 8575 | status = lua_tointeger(L, -1); |
| 8576 | |
| 8577 | reason: |
| 8578 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8579 | ret = lua_getfield(L, 2, "reason"); |
| 8580 | if (ret == LUA_TSTRING) |
| 8581 | reason = lua_tostring(L, -1); |
| 8582 | |
| 8583 | body: |
| 8584 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8585 | ret = lua_getfield(L, 2, "body"); |
| 8586 | if (ret == LUA_TSTRING) |
| 8587 | body = lua_tostring(L, -1); |
| 8588 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8589 | } |
| 8590 | |
| 8591 | /* Create the Reply table */ |
| 8592 | lua_newtable(L); |
| 8593 | |
| 8594 | /* Add status element */ |
| 8595 | lua_pushstring(L, "status"); |
| 8596 | lua_pushinteger(L, status); |
| 8597 | lua_settable(L, -3); |
| 8598 | |
| 8599 | /* Add reason element */ |
| 8600 | reason = http_get_reason(status); |
| 8601 | lua_pushstring(L, "reason"); |
| 8602 | lua_pushstring(L, reason); |
| 8603 | lua_settable(L, -3); |
| 8604 | |
| 8605 | /* Add body element, nil if undefined */ |
| 8606 | lua_pushstring(L, "body"); |
| 8607 | if (body) |
| 8608 | lua_pushstring(L, body); |
| 8609 | else |
| 8610 | lua_pushnil(L); |
| 8611 | lua_settable(L, -3); |
| 8612 | |
| 8613 | /* Add headers element */ |
| 8614 | lua_pushstring(L, "headers"); |
| 8615 | lua_newtable(L); |
| 8616 | |
| 8617 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8618 | if (lua_istable(L, 2)) { |
| 8619 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8620 | ret = lua_getfield(L, 2, "headers"); |
| 8621 | if (ret == LUA_TTABLE) { |
| 8622 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8623 | lua_pushnil(L); |
| 8624 | while (lua_next(L, -2) != 0) { |
| 8625 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8626 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8627 | /* invalid value type, skip it */ |
| 8628 | lua_pop(L, 1); |
| 8629 | continue; |
| 8630 | } |
| 8631 | |
| 8632 | |
| 8633 | /* Duplicate the key and swap it with the value. */ |
| 8634 | lua_pushvalue(L, -2); |
| 8635 | lua_insert(L, -2); |
| 8636 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8637 | |
| 8638 | lua_newtable(L); |
| 8639 | lua_insert(L, -2); |
| 8640 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8641 | |
| 8642 | if (lua_isstring(L, -1)) { |
| 8643 | /* push the value in the inner table */ |
| 8644 | lua_rawseti(L, -2, 1); |
| 8645 | } |
| 8646 | else { /* table */ |
| 8647 | lua_pushnil(L); |
| 8648 | while (lua_next(L, -2) != 0) { |
| 8649 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8650 | if (!lua_isstring(L, -1)) { |
| 8651 | /* invalid value type, skip it*/ |
| 8652 | lua_pop(L, 1); |
| 8653 | continue; |
| 8654 | } |
| 8655 | /* push the value in the inner table */ |
| 8656 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8657 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8658 | } |
| 8659 | lua_pop(L, 1); |
| 8660 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8661 | } |
| 8662 | |
| 8663 | /* push (k,v) on the stack in the headers table: |
| 8664 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8665 | */ |
| 8666 | lua_settable(L, -5); |
| 8667 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8668 | } |
| 8669 | } |
| 8670 | lua_pop(L, 1); |
| 8671 | } |
| 8672 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8673 | lua_settable(L, -3); |
| 8674 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8675 | |
| 8676 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8677 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8678 | lua_setmetatable(L, -2); |
| 8679 | return 1; |
| 8680 | } |
| 8681 | |
| 8682 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8683 | * provided, the default one corresponding to the status code is used. |
| 8684 | */ |
| 8685 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8686 | { |
| 8687 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8688 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8689 | |
| 8690 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8691 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8692 | |
| 8693 | if (status < 100 || status > 599) { |
| 8694 | lua_pushboolean(L, 0); |
| 8695 | return 1; |
| 8696 | } |
| 8697 | if (!reason) |
| 8698 | reason = http_get_reason(status); |
| 8699 | |
| 8700 | lua_pushinteger(L, status); |
| 8701 | lua_setfield(L, 1, "status"); |
| 8702 | |
| 8703 | lua_pushstring(L, reason); |
| 8704 | lua_setfield(L, 1, "reason"); |
| 8705 | |
| 8706 | lua_pushboolean(L, 1); |
| 8707 | return 1; |
| 8708 | } |
| 8709 | |
| 8710 | /* Add a header into the reply object. Each header name is associated to an |
| 8711 | * array of values in the "headers" table. If the header name is not found, a |
| 8712 | * new entry is created. |
| 8713 | */ |
| 8714 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8715 | { |
| 8716 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8717 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8718 | int ret; |
| 8719 | |
| 8720 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8721 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8722 | |
| 8723 | /* Push in the stack the "headers" entry. */ |
| 8724 | ret = lua_getfield(L, 1, "headers"); |
| 8725 | if (ret != LUA_TTABLE) { |
| 8726 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8727 | WILL_LJMP(lua_error(L)); |
| 8728 | } |
| 8729 | |
| 8730 | /* check if the header is already registered. If not, register it. */ |
| 8731 | ret = lua_getfield(L, -1, name); |
| 8732 | if (ret == LUA_TNIL) { |
| 8733 | /* Entry not found. */ |
| 8734 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8735 | |
| 8736 | /* Insert the new header name in the array in the top of the stack. |
| 8737 | * It left the new array in the top of the stack. |
| 8738 | */ |
| 8739 | lua_newtable(L); |
| 8740 | lua_pushstring(L, name); |
| 8741 | lua_pushvalue(L, -2); |
| 8742 | lua_settable(L, -4); |
| 8743 | } |
| 8744 | else if (ret != LUA_TTABLE) { |
| 8745 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8746 | WILL_LJMP(lua_error(L)); |
| 8747 | } |
| 8748 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8749 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8750 | * the header value as new entry. |
| 8751 | */ |
| 8752 | lua_pushstring(L, value); |
| 8753 | ret = lua_rawlen(L, -2); |
| 8754 | lua_rawseti(L, -2, ret + 1); |
| 8755 | |
| 8756 | lua_pushboolean(L, 1); |
| 8757 | return 1; |
| 8758 | } |
| 8759 | |
| 8760 | /* Remove all occurrences of a given header name. */ |
| 8761 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8762 | { |
| 8763 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8764 | int ret; |
| 8765 | |
| 8766 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8767 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8768 | |
| 8769 | /* Push in the stack the "headers" entry. */ |
| 8770 | ret = lua_getfield(L, 1, "headers"); |
| 8771 | if (ret != LUA_TTABLE) { |
| 8772 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8773 | WILL_LJMP(lua_error(L)); |
| 8774 | } |
| 8775 | |
| 8776 | lua_pushstring(L, name); |
| 8777 | lua_pushnil(L); |
| 8778 | lua_settable(L, -3); |
| 8779 | |
| 8780 | lua_pushboolean(L, 1); |
| 8781 | return 1; |
| 8782 | } |
| 8783 | |
| 8784 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8785 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8786 | { |
| 8787 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8788 | |
| 8789 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8790 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8791 | |
| 8792 | lua_pushstring(L, payload); |
| 8793 | lua_setfield(L, 1, "body"); |
| 8794 | |
| 8795 | lua_pushboolean(L, 1); |
| 8796 | return 1; |
| 8797 | } |
| 8798 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8799 | __LJMP static int hlua_log(lua_State *L) |
| 8800 | { |
| 8801 | int level; |
| 8802 | const char *msg; |
| 8803 | |
| 8804 | MAY_LJMP(check_args(L, 2, "log")); |
| 8805 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8806 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8807 | |
| 8808 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8809 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8810 | |
| 8811 | hlua_sendlog(NULL, level, msg); |
| 8812 | return 0; |
| 8813 | } |
| 8814 | |
| 8815 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8816 | { |
| 8817 | const char *msg; |
| 8818 | |
| 8819 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8820 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8821 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8822 | return 0; |
| 8823 | } |
| 8824 | |
| 8825 | __LJMP static int hlua_log_info(lua_State *L) |
| 8826 | { |
| 8827 | const char *msg; |
| 8828 | |
| 8829 | MAY_LJMP(check_args(L, 1, "info")); |
| 8830 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8831 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8832 | return 0; |
| 8833 | } |
| 8834 | |
| 8835 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8836 | { |
| 8837 | const char *msg; |
| 8838 | |
| 8839 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8840 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8841 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8842 | return 0; |
| 8843 | } |
| 8844 | |
| 8845 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8846 | { |
| 8847 | const char *msg; |
| 8848 | |
| 8849 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8850 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8851 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8852 | return 0; |
| 8853 | } |
| 8854 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8855 | __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] | 8856 | { |
| 8857 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8858 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8859 | 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] | 8860 | return 0; |
| 8861 | } |
| 8862 | |
| 8863 | __LJMP static int hlua_sleep(lua_State *L) |
| 8864 | { |
| 8865 | unsigned int delay; |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 8866 | int wakeup_ms; // tick value |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8867 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8868 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8869 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8870 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8871 | wakeup_ms = tick_add(now_ms, delay); |
| 8872 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8873 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8874 | 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] | 8875 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8876 | } |
| 8877 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8878 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8879 | { |
| 8880 | unsigned int delay; |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 8881 | int wakeup_ms; // tick value |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8882 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8883 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8884 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8885 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8886 | wakeup_ms = tick_add(now_ms, delay); |
| 8887 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8888 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8889 | 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] | 8890 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8891 | } |
| 8892 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8893 | /* This functionis an LUA binding. it permits to give back |
| 8894 | * the hand at the HAProxy scheduler. It is used when the |
| 8895 | * LUA processing consumes a lot of time. |
| 8896 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8897 | __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] | 8898 | { |
| 8899 | return 0; |
| 8900 | } |
| 8901 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8902 | __LJMP static int hlua_yield(lua_State *L) |
| 8903 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8904 | 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] | 8905 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8906 | } |
| 8907 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8908 | /* This function change the nice of the currently executed |
| 8909 | * task. It is used set low or high priority at the current |
| 8910 | * task. |
| 8911 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8912 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8913 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8914 | struct hlua *hlua; |
| 8915 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8916 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8917 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8918 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8919 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8920 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8921 | hlua = hlua_gethlua(L); |
| 8922 | |
| 8923 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8924 | if (!hlua || !hlua->task) |
| 8925 | return 0; |
| 8926 | |
| 8927 | if (nice < -1024) |
| 8928 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8929 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8930 | nice = 1024; |
| 8931 | |
| 8932 | hlua->task->nice = nice; |
| 8933 | return 0; |
| 8934 | } |
| 8935 | |
Aurelien DARRAGON | 8cd620b | 2023-04-07 17:37:46 +0200 | [diff] [blame] | 8936 | /* safe lua coroutine.create() function: |
| 8937 | * |
| 8938 | * This is a simple wrapper for coroutine.create() that |
| 8939 | * ensures the current hlua state ctx is available from |
| 8940 | * the new subroutine state |
| 8941 | */ |
| 8942 | __LJMP static int hlua_coroutine_create(lua_State *L) |
| 8943 | { |
| 8944 | lua_State *new; /* new coroutine state */ |
| 8945 | struct hlua **hlua_store; |
| 8946 | struct hlua *hlua = hlua_gethlua(L); |
| 8947 | |
| 8948 | new = lua_newthread(L); |
| 8949 | if (!new) |
| 8950 | return 0; |
| 8951 | |
| 8952 | hlua_store = lua_getextraspace(new); |
| 8953 | /* Expose current hlua ctx on new lua thread |
| 8954 | * (hlua_gethlua() will properly return the last "known" |
| 8955 | * hlua ctx instead of NULL when it is called from such coroutines) |
| 8956 | */ |
| 8957 | *hlua_store = hlua; |
| 8958 | |
| 8959 | /* new lua thread is on the top of the stack, we |
| 8960 | * need to duplicate first stack argument (<f> from coroutine.create(<f>)) |
| 8961 | * on the top of the stack to be able to use xmove() to move it on the new |
| 8962 | * stack |
| 8963 | */ |
| 8964 | lua_pushvalue(L, 1); |
| 8965 | /* move <f> function to the new stack */ |
| 8966 | lua_xmove(L, new, 1); |
| 8967 | /* new lua thread is back at the top of the stack */ |
| 8968 | return 1; |
| 8969 | } |
| 8970 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8971 | /* 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] | 8972 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8973 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8974 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8975 | * |
| 8976 | * Task wrapper are longjmp safe because the only one Lua code |
| 8977 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8978 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8979 | 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] | 8980 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8981 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8982 | enum hlua_exec status; |
| 8983 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8984 | if (task->tid < 0) |
| 8985 | task->tid = tid; |
| 8986 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8987 | /* If it is the first call to the task, we must initialize the |
| 8988 | * execution timeouts. |
| 8989 | */ |
| 8990 | if (!HLUA_IS_RUNNING(hlua)) |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 8991 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8992 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8993 | /* Execute the Lua code. */ |
| 8994 | status = hlua_ctx_resume(hlua, 1); |
| 8995 | |
| 8996 | switch (status) { |
| 8997 | /* finished or yield */ |
| 8998 | case HLUA_E_OK: |
| 8999 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9000 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 9001 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9002 | break; |
| 9003 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9004 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 9005 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 9006 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9007 | break; |
| 9008 | |
| 9009 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 9010 | case HLUA_E_ETMOUT: |
| 9011 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 9012 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9013 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9014 | SEND_ERR(NULL, "Lua task: %s.\n", hlua_tostring_safe(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 9015 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9016 | case HLUA_E_ERR: |
| 9017 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9018 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 9019 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9020 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9021 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 9022 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9023 | break; |
| 9024 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 9025 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9026 | } |
| 9027 | |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 9028 | /* Helper function to prepare the lua ctx for a given stream |
| 9029 | * |
Aurelien DARRAGON | 0249366 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 9030 | * ctx will be enforced in <state_id> parent stack on initial creation. |
| 9031 | * If s->hlua->state_id differs from <state_id>, which may happen at |
| 9032 | * runtime since existing stream hlua ctx will be reused for other |
| 9033 | * "independent" (but stream-related) lua executions, hlua will be |
| 9034 | * recreated with the expected state id. |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 9035 | * |
| 9036 | * Returns 1 for success and 0 for failure |
| 9037 | */ |
| 9038 | static int hlua_stream_ctx_prepare(struct stream *s, int state_id) |
| 9039 | { |
| 9040 | /* In the execution wrappers linked with a stream, the |
| 9041 | * Lua context can be not initialized. This behavior |
| 9042 | * permits to save performances because a systematic |
| 9043 | * Lua initialization cause 5% performances loss. |
| 9044 | */ |
Aurelien DARRAGON | 0249366 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 9045 | ctx_renew: |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 9046 | if (!s->hlua) { |
| 9047 | struct hlua *hlua; |
| 9048 | |
| 9049 | hlua = pool_alloc(pool_head_hlua); |
| 9050 | if (!hlua) |
| 9051 | return 0; |
| 9052 | HLUA_INIT(hlua); |
| 9053 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 9054 | pool_free(pool_head_hlua, hlua); |
| 9055 | return 0; |
| 9056 | } |
| 9057 | s->hlua = hlua; |
| 9058 | } |
Aurelien DARRAGON | 0249366 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 9059 | else if (s->hlua->state_id != state_id) { |
| 9060 | /* ctx already created, but not in proper state. |
| 9061 | * It should only happen after the previous execution is |
| 9062 | * finished, otherwise it's probably a bug since we don't |
| 9063 | * want to abort unfinished job.. |
| 9064 | */ |
| 9065 | BUG_ON(HLUA_IS_RUNNING(s->hlua)); |
| 9066 | hlua_ctx_destroy(s->hlua); |
| 9067 | s->hlua = NULL; |
| 9068 | goto ctx_renew; |
| 9069 | } |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 9070 | return 1; |
| 9071 | } |
| 9072 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9073 | /* This function is an LUA binding that register LUA function to be |
| 9074 | * executed after the HAProxy configuration parsing and before the |
| 9075 | * HAProxy scheduler starts. This function expect only one LUA |
| 9076 | * argument that is a function. This function returns nothing, but |
| 9077 | * throws if an error is encountered. |
| 9078 | */ |
| 9079 | __LJMP static int hlua_register_init(lua_State *L) |
| 9080 | { |
| 9081 | struct hlua_init_function *init; |
| 9082 | int ref; |
| 9083 | |
| 9084 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 9085 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9086 | if (hlua_gethlua(L)) { |
| 9087 | /* runtime processing */ |
| 9088 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 9089 | } |
| 9090 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9091 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 9092 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9093 | init = calloc(1, sizeof(*init)); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9094 | if (!init) { |
| 9095 | hlua_unref(L, ref); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9096 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9097 | } |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9098 | |
| 9099 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 9100 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9101 | return 0; |
| 9102 | } |
| 9103 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 9104 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9105 | * executed in parallel of the main HAroxy activity. The task is |
| 9106 | * created and it is set in the HAProxy scheduler. It can be called |
| 9107 | * from the "init" section, "post init" or during the runtime. |
| 9108 | * |
| 9109 | * Lua prototype: |
| 9110 | * |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9111 | * <none> core.register_task(<function>[, <arg1>[, <arg2>[, ...[, <arg4>]]]]) |
| 9112 | * |
| 9113 | * <arg1..4> are optional arguments that will be provided to <function> |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9114 | */ |
Aurelien DARRAGON | e0b1635 | 2023-04-21 17:38:37 +0200 | [diff] [blame] | 9115 | __LJMP static int hlua_register_task(lua_State *L) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9116 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 9117 | struct hlua *hlua = NULL; |
| 9118 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9119 | int ref; |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9120 | int nb_arg; |
| 9121 | int it; |
| 9122 | int arg_ref[4]; /* optional arguments */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 9123 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9124 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9125 | nb_arg = lua_gettop(L); |
| 9126 | if (nb_arg < 1) |
| 9127 | WILL_LJMP(luaL_error(L, "register_task: <func> argument is required")); |
| 9128 | else if (nb_arg > 5) |
| 9129 | WILL_LJMP(luaL_error(L, "register_task: no more that 4 optional arguments may be provided")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9130 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9131 | /* first arg: function ref */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9132 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 9133 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9134 | /* extract optional args (if any) */ |
| 9135 | it = 0; |
| 9136 | while (--nb_arg) { |
| 9137 | lua_pushvalue(L, 2 + it); |
| 9138 | arg_ref[it] = hlua_ref(L); /* get arg reference */ |
| 9139 | it += 1; |
| 9140 | } |
| 9141 | nb_arg = it; |
| 9142 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 9143 | /* Get the reference state. If the reference is NULL, L is the master |
| 9144 | * state, otherwise hlua->T is. |
| 9145 | */ |
| 9146 | hlua = hlua_gethlua(L); |
| 9147 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 9148 | /* we are in runtime processing */ |
| 9149 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 9150 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 9151 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9152 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 9153 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9154 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9155 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 9156 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9157 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9158 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9159 | /* We are in the common lua state, execute the task anywhere, |
| 9160 | * otherwise, inherit the current thread identifier |
| 9161 | */ |
| 9162 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9163 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9164 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9165 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 9166 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 9167 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 9168 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9169 | task->context = hlua; |
| 9170 | task->process = hlua_process_task; |
| 9171 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9172 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 9173 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9174 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9175 | /* Ensure there is enough space on the stack for the function |
| 9176 | * plus optional arguments |
| 9177 | */ |
| 9178 | if (!lua_checkstack(hlua->T, (1 + nb_arg))) |
| 9179 | goto alloc_error; |
| 9180 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9181 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9182 | hlua_pushref(hlua->T, ref); |
Aurelien DARRAGON | be58d66 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 9183 | /* function ref not needed anymore since it was pushed to the substack */ |
| 9184 | hlua_unref(L, ref); |
| 9185 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9186 | hlua->nargs = nb_arg; |
| 9187 | |
| 9188 | /* push optional arguments to the function */ |
| 9189 | for (it = 0; it < nb_arg; it++) { |
| 9190 | /* push arg to the stack */ |
| 9191 | hlua_pushref(hlua->T, arg_ref[it]); |
| 9192 | /* arg ref not needed anymore since it was pushed to the substack */ |
| 9193 | hlua_unref(L, arg_ref[it]); |
| 9194 | } |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9195 | |
| 9196 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 9197 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9198 | |
| 9199 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 9200 | |
| 9201 | alloc_error: |
| 9202 | task_destroy(task); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9203 | hlua_unref(L, ref); |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 9204 | for (it = 0; it < nb_arg; it++) { |
| 9205 | hlua_unref(L, arg_ref[it]); |
| 9206 | } |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 9207 | hlua_ctx_destroy(hlua); |
| 9208 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9209 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 9210 | } |
| 9211 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9212 | /* called from unsafe location */ |
| 9213 | static void hlua_event_subscription_destroy(struct hlua_event_sub *hlua_sub) |
| 9214 | { |
| 9215 | /* hlua cleanup */ |
| 9216 | |
| 9217 | hlua_lock(hlua_sub->hlua); |
| 9218 | /* registry is shared between coroutines */ |
| 9219 | hlua_unref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 9220 | hlua_unlock(hlua_sub->hlua); |
| 9221 | |
| 9222 | hlua_ctx_destroy(hlua_sub->hlua); |
| 9223 | |
| 9224 | /* free */ |
| 9225 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 9226 | } |
| 9227 | |
| 9228 | /* single event handler: hlua ctx is shared between multiple events handlers |
| 9229 | * issued from the same subscription. Thus, it is not destroyed when the event |
| 9230 | * is processed: it is destroyed when no more events are expected for the |
| 9231 | * subscription (ie: when the subscription ends). |
| 9232 | * |
| 9233 | * Moreover, events are processed sequentially within the subscription: |
| 9234 | * one event must be fully processed before another one may be processed. |
| 9235 | * This ensures proper consistency for lua event handling from an ordering |
| 9236 | * point of view. This is especially useful with server events for example |
| 9237 | * where ADD/DEL/UP/DOWN events ordering really matters to trigger specific |
| 9238 | * actions from lua (e.g.: sending emails or making API calls). |
| 9239 | * |
| 9240 | * Due to this design, each lua event handler is pleased to process the event |
| 9241 | * as fast as possible to prevent the event queue from growing up. |
| 9242 | * Strictly speaking, there is no runtime limit for the callback function |
| 9243 | * (timeout set to default task timeout), but if the event queue goes past |
| 9244 | * the limit of unconsumed events an error will be reported and the |
| 9245 | * susbscription will pause itself for as long as it takes for the handler to |
| 9246 | * catch up (events will be lost as a result). |
| 9247 | * If the event handler does not need the sequential ordering and wants to |
| 9248 | * process multiple events at a time, it may spawn a new side-task using |
| 9249 | * 'core.register_task' to delegate the event handling and make parallel event |
| 9250 | * processing within the same subscription set. |
| 9251 | */ |
| 9252 | static void hlua_event_handler(struct hlua *hlua) |
| 9253 | { |
| 9254 | enum hlua_exec status; |
| 9255 | |
| 9256 | /* If it is the first call to the task, we must initialize the |
| 9257 | * execution timeouts. |
| 9258 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9259 | if (!HLUA_IS_RUNNING(hlua)) |
| 9260 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9261 | |
| 9262 | /* make sure to reset the task expiry before each hlua_ctx_resume() |
| 9263 | * since the task is re-used for multiple cb function calls |
| 9264 | * We couldn't risk to have t->expire pointing to a past date because |
| 9265 | * it was set during last function invocation but was never reset since |
| 9266 | * (ie: E_AGAIN) |
| 9267 | */ |
| 9268 | hlua->task->expire = TICK_ETERNITY; |
| 9269 | |
| 9270 | /* Execute the Lua code. */ |
| 9271 | status = hlua_ctx_resume(hlua, 1); |
| 9272 | |
| 9273 | switch (status) { |
| 9274 | /* finished or yield */ |
| 9275 | case HLUA_E_OK: |
| 9276 | break; |
| 9277 | |
| 9278 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 9279 | notification_gc(&hlua->com); |
| 9280 | hlua->task->expire = hlua->wake_time; |
| 9281 | break; |
| 9282 | |
| 9283 | /* finished with error. */ |
| 9284 | case HLUA_E_ETMOUT: |
| 9285 | SEND_ERR(NULL, "Lua event_hdl: execution timeout.\n"); |
| 9286 | break; |
| 9287 | |
| 9288 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9289 | SEND_ERR(NULL, "Lua event_hdl: %s.\n", hlua_tostring_safe(hlua->T, -1)); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9290 | break; |
| 9291 | |
| 9292 | case HLUA_E_ERR: |
| 9293 | default: |
| 9294 | SEND_ERR(NULL, "Lua event_hdl: unknown error.\n"); |
| 9295 | break; |
| 9296 | } |
| 9297 | } |
| 9298 | |
Aurelien DARRAGON | c99f3ad | 2023-04-12 15:47:16 +0200 | [diff] [blame] | 9299 | __LJMP static void hlua_event_hdl_cb_push_event_checkres(lua_State *L, |
| 9300 | struct event_hdl_cb_data_server_checkres *check) |
| 9301 | { |
| 9302 | lua_pushstring(L, "agent"); |
| 9303 | lua_pushboolean(L, check->agent); |
| 9304 | lua_settable(L, -3); |
| 9305 | lua_pushstring(L, "result"); |
| 9306 | switch (check->result) { |
| 9307 | case CHK_RES_FAILED: |
| 9308 | lua_pushstring(L, "FAILED"); |
| 9309 | break; |
| 9310 | case CHK_RES_PASSED: |
| 9311 | lua_pushstring(L, "PASSED"); |
| 9312 | break; |
| 9313 | case CHK_RES_CONDPASS: |
| 9314 | lua_pushstring(L, "CONDPASS"); |
| 9315 | break; |
| 9316 | default: |
| 9317 | lua_pushnil(L); |
| 9318 | break; |
| 9319 | } |
| 9320 | lua_settable(L, -3); |
| 9321 | |
| 9322 | lua_pushstring(L, "duration"); |
| 9323 | lua_pushinteger(L, check->duration); |
| 9324 | lua_settable(L, -3); |
| 9325 | |
| 9326 | lua_pushstring(L, "reason"); |
| 9327 | lua_newtable(L); |
| 9328 | |
| 9329 | lua_pushstring(L, "short"); |
| 9330 | lua_pushstring(L, get_check_status_info(check->reason.status)); |
| 9331 | lua_settable(L, -3); |
| 9332 | lua_pushstring(L, "desc"); |
| 9333 | lua_pushstring(L, get_check_status_description(check->reason.status)); |
| 9334 | lua_settable(L, -3); |
| 9335 | if (check->reason.status >= HCHK_STATUS_L57DATA) { |
| 9336 | /* code only available when the check reached data analysis stage */ |
| 9337 | lua_pushstring(L, "code"); |
| 9338 | lua_pushinteger(L, check->reason.code); |
| 9339 | lua_settable(L, -3); |
| 9340 | } |
| 9341 | |
| 9342 | lua_settable(L, -3); /* reason table */ |
| 9343 | |
| 9344 | lua_pushstring(L, "health"); |
| 9345 | lua_newtable(L); |
| 9346 | |
| 9347 | lua_pushstring(L, "cur"); |
| 9348 | lua_pushinteger(L, check->health.cur); |
| 9349 | lua_settable(L, -3); |
| 9350 | lua_pushstring(L, "rise"); |
| 9351 | lua_pushinteger(L, check->health.rise); |
| 9352 | lua_settable(L, -3); |
| 9353 | lua_pushstring(L, "fall"); |
| 9354 | lua_pushinteger(L, check->health.fall); |
| 9355 | lua_settable(L, -3); |
| 9356 | |
| 9357 | lua_settable(L, -3); /* health table */ |
| 9358 | } |
| 9359 | |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9360 | /* This function pushes various arguments such as event type and event data to |
| 9361 | * the lua function that will be called to consume the event. |
| 9362 | */ |
| 9363 | __LJMP static void hlua_event_hdl_cb_push_args(struct hlua_event_sub *hlua_sub, |
| 9364 | struct event_hdl_async_event *e) |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9365 | { |
| 9366 | struct hlua *hlua = hlua_sub->hlua; |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9367 | struct event_hdl_sub_type event = e->type; |
| 9368 | void *data = e->data; |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9369 | |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9370 | /* push event type */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9371 | hlua->nargs = 1; |
| 9372 | lua_pushstring(hlua->T, event_hdl_sub_type_to_string(event)); |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9373 | |
| 9374 | /* push event data (according to event type) */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9375 | if (event_hdl_sub_family_equal(EVENT_HDL_SUB_SERVER, event)) { |
| 9376 | struct event_hdl_cb_data_server *e_server = data; |
| 9377 | struct proxy *px; |
| 9378 | struct server *server; |
| 9379 | |
| 9380 | hlua->nargs += 1; |
| 9381 | lua_newtable(hlua->T); |
| 9382 | /* Add server name */ |
| 9383 | lua_pushstring(hlua->T, "name"); |
| 9384 | lua_pushstring(hlua->T, e_server->safe.name); |
| 9385 | lua_settable(hlua->T, -3); |
| 9386 | /* Add server puid */ |
| 9387 | lua_pushstring(hlua->T, "puid"); |
| 9388 | lua_pushinteger(hlua->T, e_server->safe.puid); |
| 9389 | lua_settable(hlua->T, -3); |
| 9390 | /* Add server rid */ |
| 9391 | lua_pushstring(hlua->T, "rid"); |
| 9392 | lua_pushinteger(hlua->T, e_server->safe.rid); |
| 9393 | lua_settable(hlua->T, -3); |
| 9394 | /* Add server proxy name */ |
| 9395 | lua_pushstring(hlua->T, "proxy_name"); |
| 9396 | lua_pushstring(hlua->T, e_server->safe.proxy_name); |
| 9397 | lua_settable(hlua->T, -3); |
Aurelien DARRAGON | 55f84c7 | 2023-03-22 17:49:04 +0100 | [diff] [blame] | 9398 | /* Add server proxy uuid */ |
| 9399 | lua_pushstring(hlua->T, "proxy_uuid"); |
| 9400 | lua_pushinteger(hlua->T, e_server->safe.proxy_uuid); |
| 9401 | lua_settable(hlua->T, -3); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9402 | |
Aurelien DARRAGON | c99f3ad | 2023-04-12 15:47:16 +0200 | [diff] [blame] | 9403 | /* special events, fetch additional info with explicit type casting */ |
| 9404 | if (event_hdl_sub_type_equal(EVENT_HDL_SUB_SERVER_STATE, event)) { |
| 9405 | struct event_hdl_cb_data_server_state *state = data; |
| 9406 | int it; |
| 9407 | |
| 9408 | if (!lua_checkstack(hlua->T, 20)) |
| 9409 | WILL_LJMP(luaL_error(hlua->T, "Lua out of memory error.")); |
| 9410 | |
| 9411 | /* state subclass */ |
| 9412 | lua_pushstring(hlua->T, "state"); |
| 9413 | lua_newtable(hlua->T); |
| 9414 | |
| 9415 | lua_pushstring(hlua->T, "admin"); |
| 9416 | lua_pushboolean(hlua->T, state->safe.type); |
| 9417 | lua_settable(hlua->T, -3); |
| 9418 | |
| 9419 | /* is it because of a check ? */ |
| 9420 | if (!state->safe.type && |
| 9421 | (state->safe.op_st_chg.cause == SRV_OP_STCHGC_HEALTH || |
| 9422 | state->safe.op_st_chg.cause == SRV_OP_STCHGC_AGENT)) { |
| 9423 | /* yes, provide check result */ |
| 9424 | lua_pushstring(hlua->T, "check"); |
| 9425 | lua_newtable(hlua->T); |
| 9426 | hlua_event_hdl_cb_push_event_checkres(hlua->T, &state->safe.op_st_chg.check); |
| 9427 | lua_settable(hlua->T, -3); /* check table */ |
| 9428 | } |
| 9429 | |
| 9430 | lua_pushstring(hlua->T, "cause"); |
| 9431 | if (state->safe.type) |
| 9432 | lua_pushstring(hlua->T, srv_adm_st_chg_cause(state->safe.adm_st_chg.cause)); |
| 9433 | else |
| 9434 | lua_pushstring(hlua->T, srv_op_st_chg_cause(state->safe.op_st_chg.cause)); |
| 9435 | lua_settable(hlua->T, -3); |
| 9436 | |
| 9437 | /* old_state, new_state */ |
| 9438 | for (it = 0; it < 2; it++) { |
| 9439 | enum srv_state srv_state = (!it) ? state->safe.old_state : state->safe.new_state; |
| 9440 | |
| 9441 | lua_pushstring(hlua->T, (!it) ? "old_state" : "new_state"); |
| 9442 | switch (srv_state) { |
| 9443 | case SRV_ST_STOPPED: |
| 9444 | lua_pushstring(hlua->T, "STOPPED"); |
| 9445 | break; |
| 9446 | case SRV_ST_STOPPING: |
| 9447 | lua_pushstring(hlua->T, "STOPPING"); |
| 9448 | break; |
| 9449 | case SRV_ST_STARTING: |
| 9450 | lua_pushstring(hlua->T, "STARTING"); |
| 9451 | break; |
| 9452 | case SRV_ST_RUNNING: |
| 9453 | lua_pushstring(hlua->T, "RUNNING"); |
| 9454 | break; |
| 9455 | default: |
| 9456 | lua_pushnil(hlua->T); |
| 9457 | break; |
| 9458 | } |
| 9459 | lua_settable(hlua->T, -3); |
| 9460 | } |
| 9461 | |
| 9462 | /* requeued */ |
| 9463 | lua_pushstring(hlua->T, "requeued"); |
| 9464 | lua_pushinteger(hlua->T, state->safe.requeued); |
| 9465 | lua_settable(hlua->T, -3); |
| 9466 | |
| 9467 | lua_settable(hlua->T, -3); /* state table */ |
| 9468 | } |
Aurelien DARRAGON | 948dd3d | 2023-04-26 11:27:09 +0200 | [diff] [blame] | 9469 | else if (event_hdl_sub_type_equal(EVENT_HDL_SUB_SERVER_ADMIN, event)) { |
| 9470 | struct event_hdl_cb_data_server_admin *admin = data; |
| 9471 | int it; |
| 9472 | |
| 9473 | if (!lua_checkstack(hlua->T, 20)) |
| 9474 | WILL_LJMP(luaL_error(hlua->T, "Lua out of memory error.")); |
| 9475 | |
| 9476 | /* admin subclass */ |
| 9477 | lua_pushstring(hlua->T, "admin"); |
| 9478 | lua_newtable(hlua->T); |
| 9479 | |
| 9480 | lua_pushstring(hlua->T, "cause"); |
| 9481 | lua_pushstring(hlua->T, srv_adm_st_chg_cause(admin->safe.cause)); |
| 9482 | lua_settable(hlua->T, -3); |
| 9483 | |
| 9484 | /* old_admin, new_admin */ |
| 9485 | for (it = 0; it < 2; it++) { |
| 9486 | enum srv_admin srv_admin = (!it) ? admin->safe.old_admin : admin->safe.new_admin; |
| 9487 | |
| 9488 | lua_pushstring(hlua->T, (!it) ? "old_admin" : "new_admin"); |
| 9489 | |
| 9490 | /* admin state matrix */ |
| 9491 | lua_newtable(hlua->T); |
| 9492 | |
| 9493 | lua_pushstring(hlua->T, "MAINT"); |
| 9494 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_MAINT); |
| 9495 | lua_settable(hlua->T, -3); |
| 9496 | lua_pushstring(hlua->T, "FMAINT"); |
| 9497 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_FMAINT); |
| 9498 | lua_settable(hlua->T, -3); |
| 9499 | lua_pushstring(hlua->T, "IMAINT"); |
| 9500 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_IMAINT); |
| 9501 | lua_settable(hlua->T, -3); |
| 9502 | lua_pushstring(hlua->T, "RMAINT"); |
| 9503 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_RMAINT); |
| 9504 | lua_settable(hlua->T, -3); |
| 9505 | lua_pushstring(hlua->T, "CMAINT"); |
| 9506 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_CMAINT); |
| 9507 | lua_settable(hlua->T, -3); |
| 9508 | |
| 9509 | lua_pushstring(hlua->T, "DRAIN"); |
| 9510 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_DRAIN); |
| 9511 | lua_settable(hlua->T, -3); |
| 9512 | lua_pushstring(hlua->T, "FDRAIN"); |
| 9513 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_FDRAIN); |
| 9514 | lua_settable(hlua->T, -3); |
| 9515 | lua_pushstring(hlua->T, "IDRAIN"); |
| 9516 | lua_pushboolean(hlua->T, srv_admin & SRV_ADMF_IDRAIN); |
| 9517 | lua_settable(hlua->T, -3); |
| 9518 | |
| 9519 | lua_settable(hlua->T, -3); /* matrix table */ |
| 9520 | } |
| 9521 | /* requeued */ |
| 9522 | lua_pushstring(hlua->T, "requeued"); |
| 9523 | lua_pushinteger(hlua->T, admin->safe.requeued); |
| 9524 | lua_settable(hlua->T, -3); |
| 9525 | |
| 9526 | lua_settable(hlua->T, -3); /* admin table */ |
| 9527 | } |
Aurelien DARRAGON | 0bd53b2 | 2023-03-30 15:53:33 +0200 | [diff] [blame] | 9528 | else if (event_hdl_sub_type_equal(EVENT_HDL_SUB_SERVER_CHECK, event)) { |
| 9529 | struct event_hdl_cb_data_server_check *check = data; |
| 9530 | |
| 9531 | if (!lua_checkstack(hlua->T, 20)) |
| 9532 | WILL_LJMP(luaL_error(hlua->T, "Lua out of memory error.")); |
| 9533 | |
| 9534 | /* check subclass */ |
| 9535 | lua_pushstring(hlua->T, "check"); |
| 9536 | lua_newtable(hlua->T); |
| 9537 | |
| 9538 | /* check result snapshot */ |
| 9539 | hlua_event_hdl_cb_push_event_checkres(hlua->T, &check->safe.res); |
| 9540 | |
| 9541 | lua_settable(hlua->T, -3); /* check table */ |
| 9542 | } |
Aurelien DARRAGON | c99f3ad | 2023-04-12 15:47:16 +0200 | [diff] [blame] | 9543 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9544 | /* attempt to provide reference server object |
| 9545 | * (if it wasn't removed yet, SERVER_DEL will never succeed here) |
| 9546 | */ |
Aurelien DARRAGON | 3d9bf4e | 2023-03-22 17:46:12 +0100 | [diff] [blame] | 9547 | px = proxy_find_by_id(e_server->safe.proxy_uuid, PR_CAP_BE, 0); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9548 | BUG_ON(!px); |
| 9549 | server = findserver_unique_id(px, e_server->safe.puid, e_server->safe.rid); |
| 9550 | if (server) { |
| 9551 | lua_pushstring(hlua->T, "reference"); |
| 9552 | hlua_fcn_new_server(hlua->T, server); |
| 9553 | lua_settable(hlua->T, -3); |
| 9554 | } |
| 9555 | } |
| 9556 | /* sub mgmt */ |
| 9557 | hlua->nargs += 1; |
| 9558 | hlua_fcn_new_event_sub(hlua->T, hlua_sub->sub); |
Aurelien DARRAGON | 096b383 | 2023-04-20 11:32:46 +0200 | [diff] [blame] | 9559 | |
| 9560 | /* when? */ |
| 9561 | hlua->nargs += 1; |
| 9562 | lua_pushinteger(hlua->T, e->when.tv_sec); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9563 | } |
| 9564 | |
| 9565 | /* events runner: if there's an ongoing hlua event handling process, finish it |
| 9566 | * then, check if there are new events waiting to be processed |
| 9567 | * (events are processed sequentially) |
| 9568 | * |
| 9569 | * We have a safety measure to warn/guard if the event queue is growing up |
| 9570 | * too much due to many events being generated and lua handler is unable to |
| 9571 | * keep up the pace (e.g.: when the event queue grows past 100 unconsumed events). |
| 9572 | * TODO: make it tunable |
| 9573 | */ |
| 9574 | static struct task *hlua_event_runner(struct task *task, void *context, unsigned int state) |
| 9575 | { |
| 9576 | struct hlua_event_sub *hlua_sub = context; |
| 9577 | struct event_hdl_async_event *event; |
| 9578 | const char *error = NULL; |
| 9579 | |
| 9580 | if (!hlua_sub->paused && event_hdl_async_equeue_size(&hlua_sub->equeue) > 100) { |
Aurelien DARRAGON | 7428ada | 2023-05-15 18:46:44 +0200 | [diff] [blame] | 9581 | const char *trace = NULL; |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9582 | |
| 9583 | /* We reached the limit of pending events in the queue: we should |
| 9584 | * warn the user, and temporarily pause the subscription to give a chance |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9585 | * to the handler to catch up? (it also prevents resource shortage since |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9586 | * the queue could grow indefinitely otherwise) |
| 9587 | * TODO: find a way to inform the handler that it missed some events |
| 9588 | * (example: stats within the subscription in event_hdl api exposed via lua api?) |
| 9589 | * |
| 9590 | * Nonetheless, reaching this limit means that the handler is not fast enough |
| 9591 | * and/or that it subscribed to events that happen too frequently and did not |
| 9592 | * expect it. This could come from an inadequate design in the user's script. |
| 9593 | */ |
| 9594 | event_hdl_pause(hlua_sub->sub); |
| 9595 | hlua_sub->paused = 1; |
| 9596 | |
Aurelien DARRAGON | 7428ada | 2023-05-15 18:46:44 +0200 | [diff] [blame] | 9597 | if (SET_SAFE_LJMP(hlua_sub->hlua)) { |
| 9598 | /* The following Lua call may fail. */ |
| 9599 | trace = hlua_traceback(hlua_sub->hlua->T, ", "); |
| 9600 | /* At this point the execution is safe. */ |
| 9601 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9602 | } else { |
| 9603 | /* Lua error was raised while fetching lua trace from current ctx */ |
| 9604 | SEND_ERR(NULL, "Lua event_hdl: unexpected error (memory failure?).\n"); |
| 9605 | } |
| 9606 | ha_warning("Lua event_hdl: pausing the subscription because the handler fails " |
| 9607 | "to keep up the pace (%u unconsumed events) from %s.\n", |
| 9608 | event_hdl_async_equeue_size(&hlua_sub->equeue), |
| 9609 | (trace) ? trace : "[unknown]"); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9610 | } |
| 9611 | |
| 9612 | if (HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9613 | /* ongoing hlua event handler, resume it */ |
| 9614 | hlua_event_handler(hlua_sub->hlua); |
| 9615 | } else if ((event = event_hdl_async_equeue_pop(&hlua_sub->equeue))) { /* check for new events */ |
| 9616 | if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) { |
| 9617 | /* ending event: no more events to come */ |
| 9618 | event_hdl_async_free_event(event); |
| 9619 | task_destroy(task); |
| 9620 | hlua_event_subscription_destroy(hlua_sub); |
| 9621 | return NULL; |
| 9622 | } |
| 9623 | /* new event: start processing it */ |
| 9624 | |
| 9625 | /* The following Lua calls can fail. */ |
| 9626 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) { |
| 9627 | if (lua_type(hlua_sub->hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9628 | error = hlua_tostring_safe(hlua_sub->hlua->T, -1); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9629 | else |
| 9630 | error = "critical error"; |
| 9631 | ha_alert("Lua event_hdl: %s.\n", error); |
| 9632 | goto skip_event; |
| 9633 | } |
| 9634 | |
| 9635 | /* Check stack available size. */ |
| 9636 | if (!lua_checkstack(hlua_sub->hlua->T, 5)) { |
| 9637 | ha_alert("Lua event_hdl: full stack.\n"); |
| 9638 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9639 | goto skip_event; |
| 9640 | } |
| 9641 | |
| 9642 | /* Restore the function in the stack. */ |
| 9643 | hlua_pushref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 9644 | |
| 9645 | /* push args */ |
| 9646 | hlua_sub->hlua->nargs = 0; |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9647 | MAY_LJMP(hlua_event_hdl_cb_push_args(hlua_sub, event)); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9648 | |
| 9649 | /* At this point the execution is safe. */ |
| 9650 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9651 | |
| 9652 | /* At this point the event was successfully translated into hlua ctx, |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9653 | * or hlua error occurred, so we can safely discard it |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9654 | */ |
| 9655 | event_hdl_async_free_event(event); |
| 9656 | event = NULL; |
| 9657 | |
| 9658 | hlua_event_handler(hlua_sub->hlua); |
| 9659 | skip_event: |
| 9660 | if (event) |
| 9661 | event_hdl_async_free_event(event); |
| 9662 | |
| 9663 | } |
| 9664 | |
| 9665 | if (!HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9666 | /* we just finished the processing of one event.. |
| 9667 | * check for new events before becoming idle |
| 9668 | */ |
| 9669 | if (!event_hdl_async_equeue_isempty(&hlua_sub->equeue)) { |
| 9670 | /* more events to process, make sure the task |
| 9671 | * will be resumed ASAP to process pending events |
| 9672 | */ |
| 9673 | task_wakeup(task, TASK_WOKEN_OTHER); |
| 9674 | } |
| 9675 | else if (hlua_sub->paused) { |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9676 | /* empty queue, the handler caught up: resume the subscription */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9677 | event_hdl_resume(hlua_sub->sub); |
| 9678 | hlua_sub->paused = 0; |
| 9679 | } |
| 9680 | } |
| 9681 | |
| 9682 | return task; |
| 9683 | } |
| 9684 | |
| 9685 | /* Must be called directly under lua protected/safe environment |
| 9686 | * (not from external callback) |
| 9687 | * <fcn_ref> should NOT be dropped after the function successfully returns: |
| 9688 | * it will be done automatically in hlua_event_subscription_destroy() when the |
| 9689 | * subscription ends. |
| 9690 | * |
| 9691 | * Returns the new subscription on success and NULL on failure (memory error) |
| 9692 | */ |
| 9693 | static struct event_hdl_sub *hlua_event_subscribe(event_hdl_sub_list *list, struct event_hdl_sub_type e_type, |
| 9694 | int state_id, int fcn_ref) |
| 9695 | { |
| 9696 | struct hlua_event_sub *hlua_sub; |
| 9697 | struct task *task = NULL; |
| 9698 | |
| 9699 | hlua_sub = pool_alloc(pool_head_hlua_event_sub); |
| 9700 | if (!hlua_sub) |
| 9701 | goto mem_error; |
| 9702 | hlua_sub->task = NULL; |
| 9703 | hlua_sub->hlua = NULL; |
| 9704 | hlua_sub->paused = 0; |
| 9705 | if ((task = task_new_here()) == NULL) { |
| 9706 | ha_alert("out of memory while allocating hlua event task"); |
| 9707 | goto mem_error; |
| 9708 | } |
| 9709 | task->process = hlua_event_runner; |
| 9710 | task->context = hlua_sub; |
| 9711 | event_hdl_async_equeue_init(&hlua_sub->equeue); |
| 9712 | hlua_sub->task = task; |
| 9713 | hlua_sub->fcn_ref = fcn_ref; |
| 9714 | hlua_sub->state_id = state_id; |
| 9715 | hlua_sub->hlua = pool_alloc(pool_head_hlua); |
| 9716 | if (!hlua_sub->hlua) |
| 9717 | goto mem_error; |
| 9718 | HLUA_INIT(hlua_sub->hlua); |
| 9719 | if (!hlua_ctx_init(hlua_sub->hlua, hlua_sub->state_id, task)) |
| 9720 | goto mem_error; |
| 9721 | |
| 9722 | hlua_sub->sub = event_hdl_subscribe_ptr(list, e_type, |
| 9723 | EVENT_HDL_ASYNC_TASK(&hlua_sub->equeue, |
| 9724 | task, |
| 9725 | hlua_sub, |
| 9726 | NULL)); |
| 9727 | if (!hlua_sub->sub) |
| 9728 | goto mem_error; |
| 9729 | |
| 9730 | return hlua_sub->sub; /* returns pointer to event_hdl_sub struct */ |
| 9731 | |
| 9732 | mem_error: |
| 9733 | if (hlua_sub) { |
Tim Duesterhus | fe83f58 | 2023-04-22 17:47:34 +0200 | [diff] [blame] | 9734 | task_destroy(hlua_sub->task); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9735 | if (hlua_sub->hlua) |
| 9736 | hlua_ctx_destroy(hlua_sub->hlua); |
| 9737 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 9738 | } |
| 9739 | |
| 9740 | return NULL; |
| 9741 | } |
| 9742 | |
| 9743 | /* looks for an array of strings referring to a composition of event_hdl subscription |
| 9744 | * types at <index> in <L> stack |
| 9745 | */ |
| 9746 | __LJMP static struct event_hdl_sub_type hlua_check_event_sub_types(lua_State *L, int index) |
| 9747 | { |
| 9748 | struct event_hdl_sub_type subscriptions; |
| 9749 | const char *msg; |
| 9750 | |
| 9751 | if (lua_type(L, index) != LUA_TTABLE) { |
| 9752 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9753 | luaL_argerror(L, index, msg); |
| 9754 | } |
| 9755 | |
| 9756 | subscriptions = EVENT_HDL_SUB_NONE; |
| 9757 | |
| 9758 | /* browse the argument as an array. */ |
| 9759 | lua_pushnil(L); |
| 9760 | while (lua_next(L, index) != 0) { |
| 9761 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9762 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9763 | luaL_argerror(L, index, msg); |
| 9764 | } |
| 9765 | |
| 9766 | if (event_hdl_sub_type_equal(EVENT_HDL_SUB_NONE, event_hdl_string_to_sub_type(lua_tostring(L, -1)))) { |
| 9767 | msg = lua_pushfstring(L, "'%s' event type is unknown", lua_tostring(L, -1)); |
| 9768 | luaL_argerror(L, index, msg); |
| 9769 | } |
| 9770 | |
| 9771 | /* perform subscriptions |= current sub */ |
| 9772 | subscriptions = event_hdl_sub_type_add(subscriptions, event_hdl_string_to_sub_type(lua_tostring(L, -1))); |
| 9773 | |
| 9774 | /* pop the current value. */ |
| 9775 | lua_pop(L, 1); |
| 9776 | } |
| 9777 | |
| 9778 | return subscriptions; |
| 9779 | } |
| 9780 | |
| 9781 | /* Wrapper for hlua_fcn_new_event_sub(): catch errors raised by |
| 9782 | * the function to prevent LJMP |
| 9783 | * |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9784 | * If no error occurred, the function returns 1, else it returns 0 and |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9785 | * the error message is pushed at the top of the stack |
| 9786 | */ |
| 9787 | __LJMP static int _hlua_new_event_sub_safe(lua_State *L) |
| 9788 | { |
| 9789 | struct event_hdl_sub *sub = lua_touserdata(L, 1); |
| 9790 | |
| 9791 | /* this function may raise errors */ |
| 9792 | return MAY_LJMP(hlua_fcn_new_event_sub(L, sub)); |
| 9793 | } |
| 9794 | static int hlua_new_event_sub_safe(lua_State *L, struct event_hdl_sub *sub) |
| 9795 | { |
| 9796 | if (!lua_checkstack(L, 2)) |
| 9797 | return 0; |
| 9798 | lua_pushcfunction(L, _hlua_new_event_sub_safe); |
| 9799 | lua_pushlightuserdata(L, sub); |
| 9800 | switch (lua_pcall(L, 1, 1, 0)) { |
| 9801 | case LUA_OK: |
| 9802 | return 1; |
| 9803 | default: |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9804 | /* error was caught */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9805 | return 0; |
| 9806 | } |
| 9807 | } |
| 9808 | |
| 9809 | /* This function is a LUA helper used for registering lua event callbacks. |
| 9810 | * It expects an event subscription array and the function to be executed |
| 9811 | * when subscribed events occur (stack arguments). |
| 9812 | * It can be called from the "init" section, "post init" or during the runtime. |
| 9813 | * |
| 9814 | * <sub_list> is the subscription list where the subscription will be attempted |
| 9815 | * |
| 9816 | * Pushes the newly allocated subscription on the stack on success |
| 9817 | */ |
| 9818 | __LJMP int hlua_event_sub(lua_State *L, event_hdl_sub_list *sub_list) |
| 9819 | { |
| 9820 | struct hlua *hlua; |
| 9821 | struct event_hdl_sub *sub; |
| 9822 | struct event_hdl_sub_type subscriptions; |
| 9823 | int fcn_ref; |
| 9824 | int state_id; |
| 9825 | |
| 9826 | MAY_LJMP(check_args(L, 2, "event_sub")); |
| 9827 | |
| 9828 | /* Get the reference state */ |
| 9829 | hlua = hlua_gethlua(L); |
| 9830 | if (hlua) |
| 9831 | /* we are in runtime processing, any thread may subscribe to events: |
| 9832 | * subscription events will be handled by the thread who performed |
| 9833 | * the registration. |
| 9834 | */ |
| 9835 | state_id = hlua->state_id; |
| 9836 | else { |
| 9837 | /* we are in initialization mode, only thread 0 (actual calling thread) |
| 9838 | * may subscribe to events to prevent the same handler (from different lua |
| 9839 | * stacks) from being registered multiple times |
| 9840 | * |
| 9841 | * hlua_state_id == 0: monostack (lua-load) |
| 9842 | * hlua_state_id > 0: hlua_state_id=tid+1, multi-stack (lua-load-per-thread) |
| 9843 | * (thus if hlua_state_id > 1, it means we are not in primary thread ctx) |
| 9844 | */ |
| 9845 | if (hlua_state_id > 1) |
| 9846 | return 0; /* skip registration */ |
| 9847 | state_id = hlua_state_id; |
| 9848 | } |
| 9849 | |
| 9850 | /* First argument : event subscriptions. */ |
| 9851 | subscriptions = MAY_LJMP(hlua_check_event_sub_types(L, 1)); |
| 9852 | |
| 9853 | if (event_hdl_sub_type_equal(subscriptions, EVENT_HDL_SUB_NONE)) { |
| 9854 | WILL_LJMP(luaL_error(L, "event_sub: no valid event types were provided")); |
| 9855 | return 0; /* Never reached */ |
| 9856 | } |
| 9857 | |
| 9858 | /* Second argument : lua function. */ |
| 9859 | fcn_ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9860 | |
| 9861 | /* try to subscribe */ |
| 9862 | sub = hlua_event_subscribe(sub_list, subscriptions, state_id, fcn_ref); |
| 9863 | if (!sub) { |
| 9864 | hlua_unref(L, fcn_ref); |
| 9865 | WILL_LJMP(luaL_error(L, "event_sub: lua out of memory error")); |
| 9866 | return 0; /* Never reached */ |
| 9867 | } |
| 9868 | |
| 9869 | /* push the subscription to the stack |
| 9870 | * |
| 9871 | * Here we use the safe function so that lua errors will be |
| 9872 | * handled explicitly to prevent 'sub' from being lost |
| 9873 | */ |
| 9874 | if (!hlua_new_event_sub_safe(L, sub)) { |
| 9875 | /* Some events could already be pending in the handler's queue. |
| 9876 | * However it is wiser to cancel the subscription since we are unable to |
| 9877 | * provide a valid reference to it. |
| 9878 | * Pending events will be delivered (unless lua keeps raising errors). |
| 9879 | */ |
| 9880 | event_hdl_unsubscribe(sub); /* cancel the subscription */ |
| 9881 | WILL_LJMP(luaL_error(L, "event_sub: cannot push the subscription (%s)", lua_tostring(L, -1))); |
| 9882 | return 0; /* Never reached */ |
| 9883 | } |
| 9884 | event_hdl_drop(sub); /* sub has been duplicated, discard old ref */ |
| 9885 | |
| 9886 | return 1; |
| 9887 | } |
| 9888 | |
| 9889 | /* This function is a LUA wrapper used for registering global lua event callbacks |
| 9890 | * The new subscription is pushed onto the stack on success |
| 9891 | * Returns the number of arguments pushed to the stack (1 for success) |
| 9892 | */ |
| 9893 | __LJMP static int hlua_event_global_sub(lua_State *L) |
| 9894 | { |
| 9895 | /* NULL <sub_list> = global subscription list */ |
| 9896 | return MAY_LJMP(hlua_event_sub(L, NULL)); |
| 9897 | } |
| 9898 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9899 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 9900 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 9901 | * resume converters. |
| 9902 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9903 | 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] | 9904 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9905 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9906 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9907 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9908 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9909 | if (!stream) |
| 9910 | return 0; |
| 9911 | |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 9912 | if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) { |
| 9913 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9914 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9915 | } |
| 9916 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9917 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9918 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9919 | |
| 9920 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9921 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9922 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9923 | error = hlua_tostring_safe(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9924 | else |
| 9925 | error = "critical error"; |
| 9926 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9927 | return 0; |
| 9928 | } |
| 9929 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9930 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9931 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9932 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9933 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9934 | return 0; |
| 9935 | } |
| 9936 | |
| 9937 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9938 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9939 | |
| 9940 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9941 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9942 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9943 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9944 | return 0; |
| 9945 | } |
Aurelien DARRAGON | 4121772 | 2023-05-17 10:44:47 +0200 | [diff] [blame] | 9946 | MAY_LJMP(hlua_smp2lua(stream->hlua->T, smp)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9947 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9948 | |
| 9949 | /* push keywords in the stack. */ |
| 9950 | if (arg_p) { |
| 9951 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9952 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9953 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9954 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9955 | return 0; |
| 9956 | } |
Aurelien DARRAGON | e5c048a | 2023-05-17 10:51:50 +0200 | [diff] [blame] | 9957 | MAY_LJMP(hlua_arg2lua(stream->hlua->T, arg_p)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9958 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9959 | } |
| 9960 | } |
| 9961 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9962 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9963 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9964 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9965 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9966 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9967 | } |
| 9968 | |
| 9969 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9970 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9971 | /* finished. */ |
| 9972 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9973 | /* If the stack is empty, the function fails. */ |
| 9974 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9975 | return 0; |
| 9976 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9977 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9978 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | 1c07da4 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 9979 | /* dup the smp before popping the related lua value and |
| 9980 | * returning it to haproxy |
| 9981 | */ |
| 9982 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9983 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9984 | return 1; |
| 9985 | |
| 9986 | /* yield. */ |
| 9987 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9988 | 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] | 9989 | return 0; |
| 9990 | |
| 9991 | /* finished with error. */ |
| 9992 | case HLUA_E_ERRMSG: |
| 9993 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9994 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9995 | fcn->name, hlua_tostring_safe(stream->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9996 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9997 | return 0; |
| 9998 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9999 | case HLUA_E_ETMOUT: |
| 10000 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 10001 | return 0; |
| 10002 | |
| 10003 | case HLUA_E_NOMEM: |
| 10004 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 10005 | return 0; |
| 10006 | |
| 10007 | case HLUA_E_YIELD: |
| 10008 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 10009 | return 0; |
| 10010 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10011 | case HLUA_E_ERR: |
| 10012 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10013 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 10014 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10015 | |
| 10016 | default: |
| 10017 | return 0; |
| 10018 | } |
| 10019 | } |
| 10020 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10021 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 10022 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10023 | * resume sample-fetches. This function will be called by the sample |
| 10024 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10025 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10026 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 10027 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10028 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10029 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10030 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10031 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 10032 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10033 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10034 | if (!stream) |
| 10035 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10036 | |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 10037 | if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) { |
| 10038 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 10039 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 10040 | } |
| 10041 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10042 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10043 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10044 | |
| 10045 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10046 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10047 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10048 | error = hlua_tostring_safe(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10049 | else |
| 10050 | error = "critical error"; |
| 10051 | 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] | 10052 | return 0; |
| 10053 | } |
| 10054 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10055 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10056 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10057 | 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] | 10058 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10059 | return 0; |
| 10060 | } |
| 10061 | |
| 10062 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10063 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10064 | |
| 10065 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 10066 | 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] | 10067 | 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] | 10068 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10069 | return 0; |
| 10070 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10071 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10072 | |
| 10073 | /* push keywords in the stack. */ |
| 10074 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 10075 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10076 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10077 | 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] | 10078 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10079 | return 0; |
| 10080 | } |
Aurelien DARRAGON | e5c048a | 2023-05-17 10:51:50 +0200 | [diff] [blame] | 10081 | MAY_LJMP(hlua_arg2lua(stream->hlua->T, arg_p)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10082 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10083 | } |
| 10084 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10085 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10086 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10087 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10088 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10089 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10090 | } |
| 10091 | |
| 10092 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10093 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10094 | /* finished. */ |
| 10095 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 10096 | /* If the stack is empty, the function fails. */ |
| 10097 | if (lua_gettop(stream->hlua->T) <= 0) |
| 10098 | return 0; |
| 10099 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10100 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10101 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | 1c07da4 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 10102 | /* dup the smp before popping the related lua value and |
| 10103 | * returning it to haproxy |
| 10104 | */ |
| 10105 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10106 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10107 | |
| 10108 | /* Set the end of execution flag. */ |
| 10109 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 10110 | return 1; |
| 10111 | |
| 10112 | /* yield. */ |
| 10113 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10114 | 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] | 10115 | return 0; |
| 10116 | |
| 10117 | /* finished with error. */ |
| 10118 | case HLUA_E_ERRMSG: |
| 10119 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10120 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10121 | fcn->name, hlua_tostring_safe(stream->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10122 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10123 | return 0; |
| 10124 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10125 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10126 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 10127 | return 0; |
| 10128 | |
| 10129 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10130 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 10131 | return 0; |
| 10132 | |
| 10133 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10134 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 10135 | return 0; |
| 10136 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10137 | case HLUA_E_ERR: |
| 10138 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10139 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 10140 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10141 | |
| 10142 | default: |
| 10143 | return 0; |
| 10144 | } |
| 10145 | } |
| 10146 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10147 | /* This function is an LUA binding used for registering |
| 10148 | * "sample-conv" functions. It expects a converter name used |
| 10149 | * in the haproxy configuration file, and an LUA function. |
| 10150 | */ |
| 10151 | __LJMP static int hlua_register_converters(lua_State *L) |
| 10152 | { |
| 10153 | struct sample_conv_kw_list *sck; |
| 10154 | const char *name; |
| 10155 | int ref; |
| 10156 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10157 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10158 | struct sample_conv *sc; |
| 10159 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10160 | |
| 10161 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 10162 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10163 | if (hlua_gethlua(L)) { |
| 10164 | /* runtime processing */ |
| 10165 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 10166 | } |
| 10167 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10168 | /* First argument : converter name. */ |
| 10169 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10170 | |
| 10171 | /* Second argument : lua function. */ |
| 10172 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 10173 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10174 | /* Check if the converter is already registered */ |
| 10175 | trash = get_trash_chunk(); |
| 10176 | chunk_printf(trash, "lua.%s", name); |
| 10177 | sc = find_sample_conv(trash->area, trash->data); |
| 10178 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10179 | fcn = sc->private; |
| 10180 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10181 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 10182 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10183 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10184 | } |
| 10185 | fcn->function_ref[hlua_state_id] = ref; |
| 10186 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10187 | } |
| 10188 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10189 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10190 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10191 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10192 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10193 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10194 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10195 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10196 | |
| 10197 | /* Fill fcn. */ |
| 10198 | fcn->name = strdup(name); |
| 10199 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10200 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10201 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10202 | |
| 10203 | /* List head */ |
| 10204 | sck->list.n = sck->list.p = NULL; |
| 10205 | |
| 10206 | /* converter keyword. */ |
| 10207 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10208 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10209 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10210 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10211 | |
| 10212 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 10213 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 10214 | 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] | 10215 | sck->kw[0].val_args = NULL; |
| 10216 | sck->kw[0].in_type = SMP_T_STR; |
| 10217 | sck->kw[0].out_type = SMP_T_STR; |
| 10218 | sck->kw[0].private = fcn; |
| 10219 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10220 | /* Register this new converter */ |
| 10221 | sample_register_convs(sck); |
| 10222 | |
| 10223 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10224 | |
| 10225 | alloc_error: |
| 10226 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10227 | hlua_unref(L, ref); |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 10228 | ha_free(&sck); |
| 10229 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10230 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 10231 | } |
| 10232 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10233 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10234 | * "sample-fetch" functions. It expects a converter name used |
| 10235 | * in the haproxy configuration file, and an LUA function. |
| 10236 | */ |
| 10237 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 10238 | { |
| 10239 | const char *name; |
| 10240 | int ref; |
| 10241 | int len; |
| 10242 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10243 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10244 | struct sample_fetch *sf; |
| 10245 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10246 | |
| 10247 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 10248 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10249 | if (hlua_gethlua(L)) { |
| 10250 | /* runtime processing */ |
| 10251 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 10252 | } |
| 10253 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10254 | /* First argument : sample-fetch name. */ |
| 10255 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10256 | |
| 10257 | /* Second argument : lua function. */ |
| 10258 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 10259 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10260 | /* Check if the sample-fetch is already registered */ |
| 10261 | trash = get_trash_chunk(); |
| 10262 | chunk_printf(trash, "lua.%s", name); |
| 10263 | sf = find_sample_fetch(trash->area, trash->data); |
| 10264 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10265 | fcn = sf->private; |
| 10266 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10267 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 10268 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10269 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10270 | } |
| 10271 | fcn->function_ref[hlua_state_id] = ref; |
| 10272 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10273 | } |
| 10274 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10275 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10276 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10277 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10278 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10279 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10280 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10281 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10282 | |
| 10283 | /* Fill fcn. */ |
| 10284 | fcn->name = strdup(name); |
| 10285 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10286 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10287 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10288 | |
| 10289 | /* List head */ |
| 10290 | sfk->list.n = sfk->list.p = NULL; |
| 10291 | |
| 10292 | /* sample-fetch keyword. */ |
| 10293 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10294 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10295 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10296 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10297 | |
| 10298 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 10299 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 10300 | 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] | 10301 | sfk->kw[0].val_args = NULL; |
| 10302 | sfk->kw[0].out_type = SMP_T_STR; |
| 10303 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 10304 | sfk->kw[0].val = 0; |
| 10305 | sfk->kw[0].private = fcn; |
| 10306 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10307 | /* Register this new fetch. */ |
| 10308 | sample_register_fetches(sfk); |
| 10309 | |
| 10310 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10311 | |
| 10312 | alloc_error: |
| 10313 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10314 | hlua_unref(L, ref); |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 10315 | ha_free(&sfk); |
| 10316 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10317 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 10318 | } |
| 10319 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 10320 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10321 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 10322 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10323 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 10324 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10325 | unsigned int delay; |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 10326 | int wakeup_ms; // tick value |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10327 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 10328 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 10329 | hlua = hlua_gethlua(L); |
| 10330 | if (!hlua) { |
| 10331 | return 0; |
| 10332 | } |
| 10333 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10334 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 10335 | |
| 10336 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 10337 | wakeup_ms = tick_add(now_ms, delay); |
| 10338 | hlua->wake_time = wakeup_ms; |
| 10339 | return 0; |
| 10340 | } |
| 10341 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10342 | /* This function is a wrapper to execute each LUA function declared as an action |
| 10343 | * wrapper during the initialisation period. This function may return any |
| 10344 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 10345 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 10346 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10347 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10348 | 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] | 10349 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10350 | { |
| 10351 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 10352 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10353 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10354 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10355 | |
| 10356 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 10357 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 10358 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 10359 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 10360 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10361 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10362 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10363 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10364 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10365 | |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 10366 | if (!hlua_stream_ctx_prepare(s, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn))) { |
| 10367 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 10368 | rule->arg.hlua_rule->fcn->name); |
| 10369 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 10370 | } |
| 10371 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10372 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10373 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10374 | |
| 10375 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10376 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10377 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10378 | error = hlua_tostring_safe(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10379 | else |
| 10380 | error = "critical error"; |
| 10381 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10382 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10383 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10384 | } |
| 10385 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10386 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10387 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10388 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10389 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10390 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10391 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10392 | } |
| 10393 | |
| 10394 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10395 | hlua_pushref(s->hlua->T, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10396 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 10397 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 10398 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10399 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10400 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10401 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10402 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10403 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10404 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10405 | |
| 10406 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10407 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10408 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10409 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10410 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10411 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10412 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10413 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10414 | lua_pushstring(s->hlua->T, *arg); |
| 10415 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10416 | } |
| 10417 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10418 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10419 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10420 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10421 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10422 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10423 | } |
| 10424 | |
| 10425 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 10426 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10427 | /* finished. */ |
| 10428 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10429 | /* Catch the return value */ |
| 10430 | if (lua_gettop(s->hlua->T) > 0) |
| 10431 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10432 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10433 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 10434 | if (act_ret == ACT_RET_YIELD) { |
| 10435 | if (flags & ACT_OPT_FINAL) |
| 10436 | goto err_yield; |
| 10437 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10438 | if (dir == SMP_OPT_DIR_REQ) |
| 10439 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 10440 | s->hlua->wake_time); |
| 10441 | else |
| 10442 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 10443 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10444 | } |
| 10445 | goto end; |
| 10446 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10447 | /* yield. */ |
| 10448 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 10449 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10450 | if (dir == SMP_OPT_DIR_REQ) |
| 10451 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 10452 | s->hlua->wake_time); |
| 10453 | else |
| 10454 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 10455 | s->hlua->wake_time); |
| 10456 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10457 | /* Some actions can be wake up when a "write" event |
| 10458 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10459 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10460 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 10461 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10462 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10463 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10464 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10465 | act_ret = ACT_RET_YIELD; |
| 10466 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10467 | |
| 10468 | /* finished with error. */ |
| 10469 | case HLUA_E_ERRMSG: |
| 10470 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10471 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10472 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10473 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10474 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10475 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10476 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10477 | 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] | 10478 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10479 | |
| 10480 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10481 | 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] | 10482 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10483 | |
| 10484 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 10485 | err_yield: |
| 10486 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | cf5b75e | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 10487 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10488 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10489 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10490 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10491 | case HLUA_E_ERR: |
| 10492 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10493 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10494 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10495 | |
| 10496 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10497 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10498 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10499 | |
| 10500 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 10501 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10502 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10503 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10504 | } |
| 10505 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 10506 | 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] | 10507 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 10508 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10509 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10510 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 10511 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 10512 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10513 | } |
| 10514 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10515 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10516 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10517 | struct hlua_tcp_ctx *tcp_ctx = applet_reserve_svcctx(ctx, sizeof(*tcp_ctx)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10518 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10519 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10520 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10521 | struct task *task; |
| 10522 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10523 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10524 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10525 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10526 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10527 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10528 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10529 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10530 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10531 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10532 | tcp_ctx->hlua = hlua; |
| 10533 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10534 | |
| 10535 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10536 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10537 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10538 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10539 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10540 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10541 | } |
| 10542 | task->nice = 0; |
| 10543 | task->context = ctx; |
| 10544 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10545 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10546 | |
| 10547 | /* In the execution wrappers linked with a stream, the |
| 10548 | * Lua context can be not initialized. This behavior |
| 10549 | * permits to save performances because a systematic |
| 10550 | * Lua initialization cause 5% performances loss. |
| 10551 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10552 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10553 | SEND_ERR(strm->be, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10554 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10555 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10556 | } |
| 10557 | |
| 10558 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10559 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10560 | |
| 10561 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10562 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10563 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10564 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10565 | else |
| 10566 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10567 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10568 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10569 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10570 | } |
| 10571 | |
| 10572 | /* Check stack available size. */ |
| 10573 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10574 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10575 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10576 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10577 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10578 | } |
| 10579 | |
| 10580 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10581 | hlua_pushref(hlua->T, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10582 | |
| 10583 | /* Create and and push object stream in the stack. */ |
| 10584 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10585 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10586 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10587 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10588 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10589 | } |
| 10590 | hlua->nargs = 1; |
| 10591 | |
| 10592 | /* push keywords in the stack. */ |
| 10593 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10594 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10595 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10596 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10597 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10598 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10599 | } |
| 10600 | lua_pushstring(hlua->T, *arg); |
| 10601 | hlua->nargs++; |
| 10602 | } |
| 10603 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10604 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10605 | |
| 10606 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10607 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 10608 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10609 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10610 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10611 | } |
| 10612 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10613 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10614 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10615 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10616 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10617 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10618 | struct act_rule *rule = ctx->rule; |
| 10619 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10620 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10621 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10622 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10623 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10624 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10625 | /* The applet execution is already done. */ |
| 10626 | if (tcp_ctx->flags & APPLET_DONE) |
| 10627 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10628 | |
| 10629 | /* Execute the function. */ |
| 10630 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10631 | /* finished. */ |
| 10632 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10633 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10634 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 10635 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10636 | |
| 10637 | /* yield. */ |
| 10638 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 10639 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10640 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10641 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10642 | |
| 10643 | /* finished with error. */ |
| 10644 | case HLUA_E_ERRMSG: |
| 10645 | /* Display log. */ |
| 10646 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10647 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10648 | lua_pop(hlua->T, 1); |
| 10649 | goto error; |
| 10650 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10651 | case HLUA_E_ETMOUT: |
| 10652 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10653 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10654 | goto error; |
| 10655 | |
| 10656 | case HLUA_E_NOMEM: |
| 10657 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10658 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10659 | goto error; |
| 10660 | |
| 10661 | case HLUA_E_YIELD: /* unexpected */ |
| 10662 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10663 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10664 | goto error; |
| 10665 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10666 | case HLUA_E_ERR: |
| 10667 | /* Display log. */ |
| 10668 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10669 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10670 | goto error; |
| 10671 | |
| 10672 | default: |
| 10673 | goto error; |
| 10674 | } |
| 10675 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10676 | out: |
| 10677 | /* eat the whole request */ |
| 10678 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 10679 | return; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10680 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10681 | error: |
| 10682 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10683 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10684 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10685 | } |
| 10686 | |
| 10687 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 10688 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10689 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 10690 | |
| 10691 | task_destroy(tcp_ctx->task); |
| 10692 | tcp_ctx->task = NULL; |
| 10693 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 10694 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10695 | } |
| 10696 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10697 | /* The function returns 0 if the initialisation is complete or -1 if |
| 10698 | * an errors occurs. It also reserves the appctx for an hlua_http_ctx. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10699 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10700 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10701 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10702 | struct hlua_http_ctx *http_ctx = applet_reserve_svcctx(ctx, sizeof(*http_ctx)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10703 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10704 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10705 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10706 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10707 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10708 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10709 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10710 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10711 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10712 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10713 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10714 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10715 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10716 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10717 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10718 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10719 | http_ctx->hlua = hlua; |
| 10720 | http_ctx->left_bytes = -1; |
| 10721 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10722 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10723 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10724 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10725 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10726 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10727 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10728 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10729 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10730 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10731 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10732 | } |
| 10733 | task->nice = 0; |
| 10734 | task->context = ctx; |
| 10735 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10736 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10737 | |
| 10738 | /* In the execution wrappers linked with a stream, the |
| 10739 | * Lua context can be not initialized. This behavior |
| 10740 | * permits to save performances because a systematic |
| 10741 | * Lua initialization cause 5% performances loss. |
| 10742 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10743 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10744 | SEND_ERR(strm->be, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10745 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10746 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10747 | } |
| 10748 | |
| 10749 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10750 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10751 | |
| 10752 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10753 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10754 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10755 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10756 | else |
| 10757 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10758 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10759 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10760 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10761 | } |
| 10762 | |
| 10763 | /* Check stack available size. */ |
| 10764 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10765 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10766 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10767 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10768 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10769 | } |
| 10770 | |
| 10771 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10772 | hlua_pushref(hlua->T, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10773 | |
| 10774 | /* Create and and push object stream in the stack. */ |
| 10775 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10776 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10777 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10778 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10779 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10780 | } |
| 10781 | hlua->nargs = 1; |
| 10782 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10783 | /* push keywords in the stack. */ |
| 10784 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10785 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10786 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10787 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10788 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10789 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10790 | } |
| 10791 | lua_pushstring(hlua->T, *arg); |
| 10792 | hlua->nargs++; |
| 10793 | } |
| 10794 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10795 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10796 | |
| 10797 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10798 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10799 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10800 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10801 | } |
| 10802 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10803 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10804 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10805 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10806 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10807 | struct stream *strm = __sc_strm(sc); |
| 10808 | struct channel *req = sc_oc(sc); |
| 10809 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10810 | struct act_rule *rule = ctx->rule; |
| 10811 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10812 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10813 | struct htx *req_htx, *res_htx; |
| 10814 | |
| 10815 | res_htx = htx_from_buf(&res->buf); |
| 10816 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10817 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10818 | goto out; |
| 10819 | |
| 10820 | /* The applet execution is already done. */ |
| 10821 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10822 | goto out; |
| 10823 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10824 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10825 | if (!b_size(&res->buf)) { |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 10826 | sc_need_room(sc, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10827 | goto out; |
| 10828 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10829 | |
| 10830 | /* Set the currently running flag. */ |
| 10831 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10832 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 10833 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10834 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10835 | goto out; |
| 10836 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10837 | } |
| 10838 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10839 | /* Execute the function. */ |
| 10840 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10841 | /* finished. */ |
| 10842 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10843 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10844 | break; |
| 10845 | |
| 10846 | /* yield. */ |
| 10847 | case HLUA_E_AGAIN: |
| 10848 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10849 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10850 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10851 | |
| 10852 | /* finished with error. */ |
| 10853 | case HLUA_E_ERRMSG: |
| 10854 | /* Display log. */ |
| 10855 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 10856 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10857 | lua_pop(hlua->T, 1); |
| 10858 | goto error; |
| 10859 | |
| 10860 | case HLUA_E_ETMOUT: |
| 10861 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10862 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10863 | goto error; |
| 10864 | |
| 10865 | case HLUA_E_NOMEM: |
| 10866 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10867 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10868 | goto error; |
| 10869 | |
| 10870 | case HLUA_E_YIELD: /* unexpected */ |
| 10871 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10872 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10873 | goto error; |
| 10874 | |
| 10875 | case HLUA_E_ERR: |
| 10876 | /* Display log. */ |
| 10877 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10878 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10879 | goto error; |
| 10880 | |
| 10881 | default: |
| 10882 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10883 | } |
| 10884 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10885 | if (http_ctx->flags & APPLET_DONE) { |
| 10886 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10887 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10888 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10889 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10890 | goto error; |
| 10891 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10892 | /* no more data are expected. If the response buffer is empty |
| 10893 | * for a chunked message, be sure to add something (EOT block in |
| 10894 | * this case) to have something to send. It is important to be |
| 10895 | * sure the EOM flags will be handled by the endpoint. |
| 10896 | */ |
| 10897 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 10898 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 10899 | sc_need_room(sc, sizeof(struct htx_blk)+1); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10900 | goto out; |
| 10901 | } |
| 10902 | channel_add_input(res, 1); |
| 10903 | } |
| 10904 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 10905 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10906 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10907 | strm->txn->status = http_ctx->status; |
| 10908 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10909 | } |
| 10910 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10911 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10912 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10913 | /* eat the whole request */ |
| 10914 | if (co_data(req)) { |
| 10915 | req_htx = htx_from_buf(&req->buf); |
| 10916 | co_htx_skip(req, req_htx, co_data(req)); |
| 10917 | htx_to_buf(req_htx, &req->buf); |
| 10918 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10919 | return; |
| 10920 | |
| 10921 | error: |
| 10922 | |
| 10923 | /* If we are in HTTP mode, and we are not send any |
| 10924 | * data, return a 500 server error in best effort: |
| 10925 | * if there is no room available in the buffer, |
| 10926 | * just close the connection. |
| 10927 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10928 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 10929 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10930 | |
| 10931 | channel_erase(res); |
| 10932 | res->buf.data = b_data(err); |
| 10933 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 10934 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 10935 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10936 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10937 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10938 | else |
| 10939 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 10940 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10941 | if (!(strm->flags & SF_ERR_MASK)) |
| 10942 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10943 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10944 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10945 | } |
| 10946 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10947 | static void hlua_applet_http_release(struct appctx *ctx) |
| 10948 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10949 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 10950 | |
| 10951 | task_destroy(http_ctx->task); |
| 10952 | http_ctx->task = NULL; |
| 10953 | hlua_ctx_destroy(http_ctx->hlua); |
| 10954 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10955 | } |
| 10956 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10957 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10958 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10959 | * |
| 10960 | * This function can fail with an abort() due to an Lua critical error. |
| 10961 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10962 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10963 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10964 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 10965 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10966 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10967 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10968 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10969 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10970 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10971 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10972 | if (!rule->arg.hlua_rule) { |
| 10973 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10974 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10975 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10976 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10977 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 10978 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 10979 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10980 | if (!rule->arg.hlua_rule->args) { |
| 10981 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10982 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10983 | } |
| 10984 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10985 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10986 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10987 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10988 | /* Expect some arguments */ |
| 10989 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10990 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10991 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10992 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10993 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10994 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10995 | if (!rule->arg.hlua_rule->args[i]) { |
| 10996 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10997 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10998 | } |
| 10999 | (*cur_arg)++; |
| 11000 | } |
| 11001 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 11002 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11003 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 11004 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11005 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 11006 | |
| 11007 | error: |
| 11008 | if (rule->arg.hlua_rule) { |
| 11009 | if (rule->arg.hlua_rule->args) { |
| 11010 | for (i = 0; i < fcn->nargs; i++) |
| 11011 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 11012 | ha_free(&rule->arg.hlua_rule->args); |
| 11013 | } |
| 11014 | ha_free(&rule->arg.hlua_rule); |
| 11015 | } |
| 11016 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 11017 | } |
| 11018 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11019 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 11020 | struct act_rule *rule, char **err) |
| 11021 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 11022 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11023 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 11024 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11025 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 11026 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11027 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 11028 | * the call of this analyzer. |
| 11029 | */ |
| 11030 | if (rule->from != ACT_F_HTTP_REQ) { |
| 11031 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 11032 | return ACT_RET_PRS_ERR; |
| 11033 | } |
| 11034 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11035 | /* Memory for the rule. */ |
| 11036 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 11037 | if (!rule->arg.hlua_rule) { |
| 11038 | memprintf(err, "out of memory error"); |
| 11039 | return ACT_RET_PRS_ERR; |
| 11040 | } |
| 11041 | |
| 11042 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 11043 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11044 | |
| 11045 | /* TODO: later accept arguments. */ |
| 11046 | rule->arg.hlua_rule->args = NULL; |
| 11047 | |
| 11048 | /* Add applet pointer in the rule. */ |
| 11049 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 11050 | rule->applet.name = fcn->name; |
| 11051 | rule->applet.init = hlua_applet_http_init; |
| 11052 | rule->applet.fct = hlua_applet_http_fct; |
| 11053 | rule->applet.release = hlua_applet_http_release; |
| 11054 | rule->applet.timeout = hlua_timeout_applet; |
| 11055 | |
| 11056 | return ACT_RET_PRS_OK; |
| 11057 | } |
| 11058 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11059 | /* This function is an LUA binding used for registering |
| 11060 | * "sample-conv" functions. It expects a converter name used |
| 11061 | * in the haproxy configuration file, and an LUA function. |
| 11062 | */ |
| 11063 | __LJMP static int hlua_register_action(lua_State *L) |
| 11064 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11065 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11066 | const char *name; |
| 11067 | int ref; |
| 11068 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11069 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 11070 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11071 | struct buffer *trash; |
| 11072 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11073 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 11074 | /* Initialise the number of expected arguments at 0. */ |
| 11075 | nargs = 0; |
| 11076 | |
| 11077 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 11078 | 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] | 11079 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11080 | if (hlua_gethlua(L)) { |
| 11081 | /* runtime processing */ |
| 11082 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 11083 | } |
| 11084 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11085 | /* First argument : converter name. */ |
| 11086 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11087 | |
| 11088 | /* Second argument : environment. */ |
| 11089 | if (lua_type(L, 2) != LUA_TTABLE) |
| 11090 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 11091 | |
| 11092 | /* Third argument : lua function. */ |
| 11093 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11094 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11095 | /* 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] | 11096 | if (lua_gettop(L) >= 4) |
| 11097 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 11098 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11099 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11100 | lua_pushnil(L); |
| 11101 | while (lua_next(L, 2) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11102 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11103 | hlua_unref(L, ref); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11104 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11105 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11106 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11107 | /* Check if action exists */ |
| 11108 | trash = get_trash_chunk(); |
| 11109 | chunk_printf(trash, "lua.%s", name); |
| 11110 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 11111 | akw = tcp_req_cont_action(trash->area); |
| 11112 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 11113 | akw = tcp_res_cont_action(trash->area); |
| 11114 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 11115 | akw = action_http_req_custom(trash->area); |
| 11116 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 11117 | akw = action_http_res_custom(trash->area); |
| 11118 | } else { |
| 11119 | akw = NULL; |
| 11120 | } |
| 11121 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11122 | fcn = akw->private; |
| 11123 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 11124 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 11125 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11126 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11127 | } |
| 11128 | fcn->function_ref[hlua_state_id] = ref; |
| 11129 | |
| 11130 | /* pop the environment string. */ |
| 11131 | lua_pop(L, 1); |
| 11132 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11133 | } |
| 11134 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11135 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 11136 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 11137 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11138 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11139 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 11140 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11141 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11142 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11143 | |
| 11144 | /* Fill fcn. */ |
| 11145 | fcn->name = strdup(name); |
| 11146 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11147 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11148 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11149 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11150 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 11151 | fcn->nargs = nargs; |
| 11152 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11153 | /* List head */ |
| 11154 | akl->list.n = akl->list.p = NULL; |
| 11155 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11156 | /* action keyword. */ |
| 11157 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 11158 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11159 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11160 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11161 | |
| 11162 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 11163 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 11164 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11165 | akl->kw[0].private = fcn; |
| 11166 | akl->kw[0].parse = action_register_lua; |
| 11167 | |
| 11168 | /* select the action registering point. */ |
| 11169 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 11170 | tcp_req_cont_keywords_register(akl); |
| 11171 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 11172 | tcp_res_cont_keywords_register(akl); |
| 11173 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 11174 | http_req_keywords_register(akl); |
| 11175 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 11176 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11177 | else { |
| 11178 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11179 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11180 | if (akl) |
| 11181 | ha_free((char **)&(akl->kw[0].kw)); |
| 11182 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 11183 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11184 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 11185 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11186 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11187 | |
| 11188 | /* pop the environment string. */ |
| 11189 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11190 | |
| 11191 | /* reset for next loop */ |
| 11192 | akl = NULL; |
| 11193 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11194 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11195 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11196 | |
| 11197 | alloc_error: |
| 11198 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11199 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 11200 | ha_free(&akl); |
| 11201 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11202 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11203 | } |
| 11204 | |
| 11205 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 11206 | struct act_rule *rule, char **err) |
| 11207 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 11208 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11209 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 11210 | if (px->mode == PR_MODE_HTTP) { |
| 11211 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11212 | return ACT_RET_PRS_ERR; |
| 11213 | } |
| 11214 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11215 | /* Memory for the rule. */ |
| 11216 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 11217 | if (!rule->arg.hlua_rule) { |
| 11218 | memprintf(err, "out of memory error"); |
| 11219 | return ACT_RET_PRS_ERR; |
| 11220 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11221 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11222 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 11223 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11224 | |
| 11225 | /* TODO: later accept arguments. */ |
| 11226 | rule->arg.hlua_rule->args = NULL; |
| 11227 | |
| 11228 | /* Add applet pointer in the rule. */ |
| 11229 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 11230 | rule->applet.name = fcn->name; |
| 11231 | rule->applet.init = hlua_applet_tcp_init; |
| 11232 | rule->applet.fct = hlua_applet_tcp_fct; |
| 11233 | rule->applet.release = hlua_applet_tcp_release; |
| 11234 | rule->applet.timeout = hlua_timeout_applet; |
| 11235 | |
| 11236 | return 0; |
| 11237 | } |
| 11238 | |
| 11239 | /* This function is an LUA binding used for registering |
| 11240 | * "sample-conv" functions. It expects a converter name used |
| 11241 | * in the haproxy configuration file, and an LUA function. |
| 11242 | */ |
| 11243 | __LJMP static int hlua_register_service(lua_State *L) |
| 11244 | { |
| 11245 | struct action_kw_list *akl; |
| 11246 | const char *name; |
| 11247 | const char *env; |
| 11248 | int ref; |
| 11249 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11250 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11251 | struct buffer *trash; |
| 11252 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11253 | |
| 11254 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 11255 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11256 | if (hlua_gethlua(L)) { |
| 11257 | /* runtime processing */ |
| 11258 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 11259 | } |
| 11260 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11261 | /* First argument : converter name. */ |
| 11262 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11263 | |
| 11264 | /* Second argument : environment. */ |
| 11265 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 11266 | |
| 11267 | /* Third argument : lua function. */ |
| 11268 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11269 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11270 | /* Check for service already registered */ |
| 11271 | trash = get_trash_chunk(); |
| 11272 | chunk_printf(trash, "lua.%s", name); |
| 11273 | akw = service_find(trash->area); |
| 11274 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11275 | fcn = akw->private; |
| 11276 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 11277 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 11278 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11279 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11280 | } |
| 11281 | fcn->function_ref[hlua_state_id] = ref; |
| 11282 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11283 | } |
| 11284 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11285 | /* Allocate and fill the sample fetch keyword struct. */ |
| 11286 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 11287 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11288 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 11289 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11290 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11291 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11292 | |
| 11293 | /* Fill fcn. */ |
| 11294 | len = strlen("<lua.>") + strlen(name) + 1; |
| 11295 | fcn->name = calloc(1, len); |
| 11296 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11297 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11298 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11299 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11300 | |
| 11301 | /* List head */ |
| 11302 | akl->list.n = akl->list.p = NULL; |
| 11303 | |
| 11304 | /* converter keyword. */ |
| 11305 | len = strlen("lua.") + strlen(name) + 1; |
| 11306 | akl->kw[0].kw = calloc(1, len); |
| 11307 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11308 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11309 | |
| 11310 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 11311 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 11312 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11313 | if (strcmp(env, "tcp") == 0) |
| 11314 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11315 | else if (strcmp(env, "http") == 0) |
| 11316 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11317 | else { |
| 11318 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11319 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11320 | if (akl) |
| 11321 | ha_free((char **)&(akl->kw[0].kw)); |
| 11322 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 11323 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 11324 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11325 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11326 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 11327 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11328 | akl->kw[0].private = fcn; |
| 11329 | |
| 11330 | /* End of array. */ |
| 11331 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 11332 | |
| 11333 | /* Register this new converter */ |
| 11334 | service_keywords_register(akl); |
| 11335 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11336 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11337 | |
| 11338 | alloc_error: |
| 11339 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11340 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 11341 | ha_free(&akl); |
| 11342 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11343 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 11344 | } |
| 11345 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11346 | /* This function initialises Lua cli handler. It copies the |
| 11347 | * arguments in the Lua stack and create channel IO objects. |
| 11348 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 11349 | 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] | 11350 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11351 | struct hlua_cli_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11352 | struct hlua *hlua; |
| 11353 | struct hlua_function *fcn; |
| 11354 | int i; |
| 11355 | const char *error; |
| 11356 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11357 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11358 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 11359 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11360 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 11361 | if (!hlua) { |
| 11362 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 11363 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 11364 | } |
| 11365 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11366 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11367 | |
| 11368 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11369 | * 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] | 11370 | * applet_http. It is absolutely compatible. |
| 11371 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11372 | ctx->task = task_new_here(); |
| 11373 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 11374 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 11375 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11376 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11377 | ctx->task->nice = 0; |
| 11378 | ctx->task->context = appctx; |
| 11379 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11380 | |
| 11381 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11382 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), ctx->task)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11383 | 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] | 11384 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11385 | } |
| 11386 | |
| 11387 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 11388 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11389 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11390 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11391 | else |
| 11392 | error = "critical error"; |
| 11393 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 11394 | goto error; |
| 11395 | } |
| 11396 | |
| 11397 | /* Check stack available size. */ |
| 11398 | if (!lua_checkstack(hlua->T, 2)) { |
| 11399 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 11400 | goto error; |
| 11401 | } |
| 11402 | |
| 11403 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11404 | hlua_pushref(hlua->T, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11405 | |
| 11406 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 11407 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11408 | */ |
| 11409 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 11410 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 11411 | goto error; |
| 11412 | } |
| 11413 | hlua->nargs = 1; |
| 11414 | |
| 11415 | /* push keywords in the stack. */ |
| 11416 | for (i = 0; *args[i]; i++) { |
| 11417 | /* Check stack available size. */ |
| 11418 | if (!lua_checkstack(hlua->T, 1)) { |
| 11419 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 11420 | goto error; |
| 11421 | } |
| 11422 | lua_pushstring(hlua->T, args[i]); |
| 11423 | hlua->nargs++; |
| 11424 | } |
| 11425 | |
| 11426 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 11427 | hlua_timer_init(&hlua->timer, hlua_timeout_session); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11428 | |
| 11429 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 11430 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11431 | |
| 11432 | /* It's ok */ |
| 11433 | return 0; |
| 11434 | |
| 11435 | /* It's not ok. */ |
| 11436 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 11437 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11438 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11439 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11440 | return 1; |
| 11441 | } |
| 11442 | |
| 11443 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 11444 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11445 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11446 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 11447 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11448 | struct hlua_function *fcn; |
| 11449 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11450 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 11451 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11452 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11453 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11454 | /* Execute the function. */ |
| 11455 | switch (hlua_ctx_resume(hlua, 1)) { |
| 11456 | |
| 11457 | /* finished. */ |
| 11458 | case HLUA_E_OK: |
| 11459 | return 1; |
| 11460 | |
| 11461 | /* yield. */ |
| 11462 | case HLUA_E_AGAIN: |
| 11463 | /* We want write. */ |
| 11464 | if (HLUA_IS_WAKERESWR(hlua)) |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 11465 | sc_need_room(sc, -1); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11466 | /* Set the timeout. */ |
| 11467 | if (hlua->wake_time != TICK_ETERNITY) |
| 11468 | task_schedule(hlua->task, hlua->wake_time); |
| 11469 | return 0; |
| 11470 | |
| 11471 | /* finished with error. */ |
| 11472 | case HLUA_E_ERRMSG: |
| 11473 | /* Display log. */ |
| 11474 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11475 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11476 | lua_pop(hlua->T, 1); |
| 11477 | return 1; |
| 11478 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 11479 | case HLUA_E_ETMOUT: |
| 11480 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 11481 | fcn->name); |
| 11482 | return 1; |
| 11483 | |
| 11484 | case HLUA_E_NOMEM: |
| 11485 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 11486 | fcn->name); |
| 11487 | return 1; |
| 11488 | |
| 11489 | case HLUA_E_YIELD: /* unexpected */ |
| 11490 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 11491 | fcn->name); |
| 11492 | return 1; |
| 11493 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11494 | case HLUA_E_ERR: |
| 11495 | /* Display log. */ |
| 11496 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 11497 | fcn->name); |
| 11498 | return 1; |
| 11499 | |
| 11500 | default: |
| 11501 | return 1; |
| 11502 | } |
| 11503 | |
| 11504 | return 1; |
| 11505 | } |
| 11506 | |
| 11507 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 11508 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11509 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 11510 | |
| 11511 | hlua_ctx_destroy(ctx->hlua); |
| 11512 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11513 | } |
| 11514 | |
| 11515 | /* This function is an LUA binding used for registering |
| 11516 | * new keywords in the cli. It expects a list of keywords |
| 11517 | * which are the "path". It is limited to 5 keywords. A |
| 11518 | * description of the command, a function to be executed |
| 11519 | * for the parsing and a function for io handlers. |
| 11520 | */ |
| 11521 | __LJMP static int hlua_register_cli(lua_State *L) |
| 11522 | { |
| 11523 | struct cli_kw_list *cli_kws; |
| 11524 | const char *message; |
| 11525 | int ref_io; |
| 11526 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11527 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11528 | int index; |
| 11529 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11530 | struct buffer *trash; |
| 11531 | const char *kw[5]; |
| 11532 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11533 | const char *errmsg; |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11534 | char *end; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11535 | |
| 11536 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 11537 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11538 | if (hlua_gethlua(L)) { |
| 11539 | /* runtime processing */ |
| 11540 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 11541 | } |
| 11542 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11543 | /* First argument : an array of maximum 5 keywords. */ |
| 11544 | if (!lua_istable(L, 1)) |
| 11545 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 11546 | |
| 11547 | /* Second argument : string with contextual message. */ |
| 11548 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 11549 | |
| 11550 | /* Third and fourth argument : lua function. */ |
| 11551 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11552 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11553 | /* Check for CLI service already registered */ |
| 11554 | trash = get_trash_chunk(); |
| 11555 | index = 0; |
| 11556 | lua_pushnil(L); |
| 11557 | memset(kw, 0, sizeof(kw)); |
| 11558 | while (lua_next(L, 1) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11559 | if (index >= CLI_PREFIX_KW_NB) { |
| 11560 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11561 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11562 | } |
| 11563 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11564 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11565 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11566 | } |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11567 | kw[index] = lua_tostring(L, -1); |
| 11568 | if (index == 0) |
| 11569 | chunk_printf(trash, "%s", kw[index]); |
| 11570 | else |
| 11571 | chunk_appendf(trash, " %s", kw[index]); |
| 11572 | index++; |
| 11573 | lua_pop(L, 1); |
| 11574 | } |
| 11575 | cli_kw = cli_find_kw_exact((char **)kw); |
| 11576 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11577 | fcn = cli_kw->private; |
| 11578 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 11579 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 11580 | "This will become a hard error in version 2.5.\n", trash->area); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11581 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11582 | } |
| 11583 | fcn->function_ref[hlua_state_id] = ref_io; |
| 11584 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11585 | } |
| 11586 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11587 | /* Allocate and fill the sample fetch keyword struct. */ |
| 11588 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11589 | if (!cli_kws) { |
| 11590 | errmsg = "Lua out of memory error."; |
| 11591 | goto error; |
| 11592 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 11593 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11594 | if (!fcn) { |
| 11595 | errmsg = "Lua out of memory error."; |
| 11596 | goto error; |
| 11597 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11598 | |
| 11599 | /* Fill path. */ |
| 11600 | index = 0; |
| 11601 | lua_pushnil(L); |
| 11602 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11603 | if (index >= 5) { |
| 11604 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 11605 | goto error; |
| 11606 | } |
| 11607 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11608 | errmsg = "1st argument must be a table filled with strings"; |
| 11609 | goto error; |
| 11610 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11611 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11612 | if (!cli_kws->kw[0].str_kw[index]) { |
| 11613 | errmsg = "Lua out of memory error."; |
| 11614 | goto error; |
| 11615 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11616 | index++; |
| 11617 | lua_pop(L, 1); |
| 11618 | } |
| 11619 | |
| 11620 | /* Copy help message. */ |
| 11621 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11622 | if (!cli_kws->kw[0].usage) { |
| 11623 | errmsg = "Lua out of memory error."; |
| 11624 | goto error; |
| 11625 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11626 | |
| 11627 | /* Fill fcn io handler. */ |
| 11628 | len = strlen("<lua.cli>") + 1; |
| 11629 | for (i = 0; i < index; i++) |
| 11630 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 11631 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11632 | if (!fcn->name) { |
| 11633 | errmsg = "Lua out of memory error."; |
| 11634 | goto error; |
| 11635 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11636 | |
| 11637 | end = fcn->name; |
| 11638 | len = 8; |
| 11639 | memcpy(end, "<lua.cli", len); |
| 11640 | end += len; |
| 11641 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11642 | for (i = 0; i < index; i++) { |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11643 | *(end++) = '.'; |
| 11644 | len = strlen(cli_kws->kw[0].str_kw[i]); |
| 11645 | memcpy(end, cli_kws->kw[0].str_kw[i], len); |
| 11646 | end += len; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11647 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11648 | *(end++) = '>'; |
| 11649 | *(end++) = 0; |
| 11650 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11651 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11652 | |
| 11653 | /* Fill last entries. */ |
| 11654 | cli_kws->kw[0].private = fcn; |
| 11655 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 11656 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 11657 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 11658 | |
| 11659 | /* Register this new converter */ |
| 11660 | cli_register_kw(cli_kws); |
| 11661 | |
| 11662 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11663 | |
| 11664 | error: |
| 11665 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11666 | hlua_unref(L, ref_io); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11667 | if (cli_kws) { |
| 11668 | for (i = 0; i < index; i++) |
| 11669 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 11670 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 11671 | } |
| 11672 | ha_free(&cli_kws); |
| 11673 | WILL_LJMP(luaL_error(L, errmsg)); |
| 11674 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11675 | } |
| 11676 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11677 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11678 | { |
| 11679 | struct hlua_flt_config *conf = fconf->conf; |
| 11680 | lua_State *L; |
| 11681 | int error, pos, state_id, flt_ref; |
| 11682 | |
| 11683 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11684 | L = hlua_states[state_id]; |
| 11685 | pos = lua_gettop(L); |
| 11686 | |
| 11687 | /* The filter parsing function */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11688 | hlua_pushref(L, conf->reg->fun_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11689 | |
| 11690 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11691 | hlua_pushref(L, conf->reg->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11692 | |
| 11693 | /* Duplicate the filter class so each filter will have its own copy */ |
| 11694 | lua_newtable(L); |
| 11695 | lua_pushnil(L); |
| 11696 | |
| 11697 | while (lua_next(L, pos+2)) { |
| 11698 | lua_pushvalue(L, -2); |
| 11699 | lua_insert(L, -2); |
| 11700 | lua_settable(L, -4); |
| 11701 | } |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11702 | flt_ref = hlua_ref(L); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11703 | |
| 11704 | /* Remove the original lua filter class from the stack */ |
| 11705 | lua_pop(L, 1); |
| 11706 | |
| 11707 | /* Push the copy on the stack */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11708 | hlua_pushref(L, flt_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11709 | |
| 11710 | /* extra args are pushed in a table */ |
| 11711 | lua_newtable(L); |
| 11712 | for (pos = 0; conf->args[pos]; pos++) { |
| 11713 | /* Check stack available size. */ |
| 11714 | if (!lua_checkstack(L, 1)) { |
| 11715 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 11716 | goto error; |
| 11717 | } |
| 11718 | lua_pushstring(L, conf->args[pos]); |
| 11719 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 11720 | } |
| 11721 | |
| 11722 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 11723 | switch (error) { |
| 11724 | case LUA_OK: |
| 11725 | /* replace the filter ref */ |
| 11726 | conf->ref[state_id] = flt_ref; |
| 11727 | break; |
| 11728 | case LUA_ERRRUN: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11729 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, hlua_tostring_safe(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11730 | goto error; |
| 11731 | case LUA_ERRMEM: |
| 11732 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 11733 | goto error; |
| 11734 | case LUA_ERRERR: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11735 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, hlua_tostring_safe(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11736 | goto error; |
| 11737 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 11738 | case LUA_ERRGCMM: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11739 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, hlua_tostring_safe(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11740 | goto error; |
| 11741 | #endif |
| 11742 | default: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11743 | ha_alert("Lua filter '%s' : unknown error : %s", conf->reg->name, hlua_tostring_safe(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11744 | goto error; |
| 11745 | } |
| 11746 | |
| 11747 | lua_settop(L, 0); |
| 11748 | return 0; |
| 11749 | |
| 11750 | error: |
| 11751 | lua_settop(L, 0); |
| 11752 | return -1; |
| 11753 | } |
| 11754 | |
| 11755 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11756 | { |
| 11757 | struct hlua_flt_config *conf = fconf->conf; |
| 11758 | lua_State *L; |
| 11759 | int state_id; |
| 11760 | |
| 11761 | if (!conf) |
| 11762 | return; |
| 11763 | |
| 11764 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11765 | L = hlua_states[state_id]; |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11766 | hlua_unref(L, conf->ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11767 | } |
| 11768 | |
| 11769 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 11770 | { |
| 11771 | struct hlua_flt_config *conf = fconf->conf; |
| 11772 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11773 | |
| 11774 | /* Rely on per-thread init for global scripts */ |
| 11775 | if (!state_id) |
| 11776 | return hlua_filter_init_per_thread(px, fconf); |
| 11777 | return 0; |
| 11778 | } |
| 11779 | |
| 11780 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 11781 | { |
| 11782 | |
| 11783 | if (fconf->conf) { |
| 11784 | struct hlua_flt_config *conf = fconf->conf; |
| 11785 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11786 | int pos; |
| 11787 | |
| 11788 | /* Rely on per-thread deinit for global scripts */ |
| 11789 | if (!state_id) |
| 11790 | hlua_filter_deinit_per_thread(px, fconf); |
| 11791 | |
| 11792 | for (pos = 0; conf->args[pos]; pos++) |
| 11793 | free(conf->args[pos]); |
| 11794 | free(conf->args); |
| 11795 | } |
| 11796 | ha_free(&fconf->conf); |
| 11797 | ha_free((char **)&fconf->id); |
| 11798 | ha_free(&fconf->ops); |
| 11799 | } |
| 11800 | |
| 11801 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 11802 | { |
| 11803 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11804 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 11805 | int ret = 1; |
| 11806 | |
Aurelien DARRAGON | b25d5d3 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 11807 | if (!hlua_stream_ctx_prepare(s, reg_flt_to_stack_id(conf->reg))) { |
| 11808 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11809 | conf->reg->name); |
| 11810 | ret = 0; |
| 11811 | goto end; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11812 | } |
| 11813 | |
| 11814 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 11815 | if (!flt_ctx) { |
| 11816 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11817 | conf->reg->name); |
| 11818 | ret = 0; |
| 11819 | goto end; |
| 11820 | } |
Aurelien DARRAGON | 205f1ac | 2024-03-04 09:39:58 +0100 | [diff] [blame] | 11821 | |
| 11822 | if ((flt_ctx->hlua[0] = pool_alloc(pool_head_hlua))) |
| 11823 | HLUA_INIT(flt_ctx->hlua[0]); |
| 11824 | if ((flt_ctx->hlua[1] = pool_alloc(pool_head_hlua))) |
| 11825 | HLUA_INIT(flt_ctx->hlua[1]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11826 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 11827 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11828 | conf->reg->name); |
| 11829 | ret = 0; |
| 11830 | goto end; |
| 11831 | } |
Aurelien DARRAGON | 205f1ac | 2024-03-04 09:39:58 +0100 | [diff] [blame] | 11832 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11833 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task) || |
| 11834 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task)) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11835 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11836 | conf->reg->name); |
| 11837 | ret = 0; |
| 11838 | goto end; |
| 11839 | } |
| 11840 | |
| 11841 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 11842 | /* The following Lua calls can fail. */ |
| 11843 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 11844 | const char *error; |
| 11845 | |
| 11846 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11847 | error = hlua_tostring_safe(s->hlua->T, -1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11848 | else |
| 11849 | error = "critical error"; |
| 11850 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11851 | ret = 0; |
| 11852 | goto end; |
| 11853 | } |
| 11854 | |
| 11855 | /* Check stack size. */ |
| 11856 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 11857 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
Tim Duesterhus | 2281738 | 2021-09-11 23:17:25 +0200 | [diff] [blame] | 11858 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11859 | ret = 0; |
| 11860 | goto end; |
| 11861 | } |
| 11862 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11863 | hlua_pushref(s->hlua->T, conf->ref[s->hlua->state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11864 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 11865 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 11866 | conf->reg->name); |
| 11867 | RESET_SAFE_LJMP(s->hlua); |
| 11868 | ret = 0; |
| 11869 | goto end; |
| 11870 | } |
| 11871 | lua_insert(s->hlua->T, -2); |
| 11872 | |
| 11873 | /* Push the copy on the stack */ |
| 11874 | s->hlua->nargs = 1; |
| 11875 | |
| 11876 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 11877 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11878 | |
| 11879 | /* At this point the execution is safe. */ |
| 11880 | RESET_SAFE_LJMP(s->hlua); |
| 11881 | } |
| 11882 | |
| 11883 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 11884 | case HLUA_E_OK: |
| 11885 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 11886 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 11887 | ret = 0; |
| 11888 | goto end; |
| 11889 | } |
| 11890 | |
| 11891 | /* Attached the filter pointer to the ctx */ |
| 11892 | lua_pushstring(s->hlua->T, "__filter"); |
| 11893 | lua_pushlightuserdata(s->hlua->T, filter); |
| 11894 | lua_settable(s->hlua->T, -3); |
| 11895 | |
| 11896 | /* Save a ref on the filter ctx */ |
| 11897 | lua_pushvalue(s->hlua->T, 1); |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11898 | flt_ctx->ref = hlua_ref(s->hlua->T); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11899 | filter->ctx = flt_ctx; |
| 11900 | break; |
| 11901 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11902 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, hlua_tostring_safe(s->hlua->T, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11903 | ret = -1; |
| 11904 | goto end; |
| 11905 | case HLUA_E_ETMOUT: |
| 11906 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 11907 | ret = 0; |
| 11908 | goto end; |
| 11909 | case HLUA_E_NOMEM: |
| 11910 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11911 | ret = 0; |
| 11912 | goto end; |
| 11913 | case HLUA_E_AGAIN: |
| 11914 | case HLUA_E_YIELD: |
| 11915 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11916 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 11917 | ret = 0; |
| 11918 | goto end; |
| 11919 | case HLUA_E_ERR: |
| 11920 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 11921 | ret = 0; |
| 11922 | goto end; |
| 11923 | default: |
| 11924 | ret = 0; |
| 11925 | goto end; |
| 11926 | } |
| 11927 | |
| 11928 | end: |
Aurelien DARRAGON | 01d1b73 | 2024-03-04 11:25:47 +0100 | [diff] [blame^] | 11929 | if (s->hlua) { |
| 11930 | hlua_lock(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11931 | lua_settop(s->hlua->T, 0); |
Aurelien DARRAGON | 01d1b73 | 2024-03-04 11:25:47 +0100 | [diff] [blame^] | 11932 | hlua_unlock(s->hlua); |
| 11933 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11934 | if (ret <= 0) { |
| 11935 | if (flt_ctx) { |
| 11936 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11937 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11938 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11939 | } |
| 11940 | } |
| 11941 | return ret; |
| 11942 | } |
| 11943 | |
| 11944 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 11945 | { |
| 11946 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11947 | |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11948 | hlua_unref(s->hlua->T, flt_ctx->ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11949 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11950 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11951 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11952 | filter->ctx = NULL; |
| 11953 | } |
| 11954 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 11955 | static int hlua_filter_from_payload(struct filter *filter) |
| 11956 | { |
| 11957 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11958 | |
| 11959 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 11960 | } |
| 11961 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11962 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 11963 | int dir, unsigned int flags) |
| 11964 | { |
| 11965 | struct hlua *flt_hlua; |
| 11966 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11967 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11968 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 11969 | int ret = 1; |
| 11970 | |
| 11971 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 11972 | if (!flt_hlua) |
| 11973 | goto end; |
| 11974 | |
| 11975 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
Aurelien DARRAGON | f8b137b | 2024-03-04 11:06:24 +0100 | [diff] [blame] | 11976 | int extra_idx; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11977 | |
| 11978 | /* The following Lua calls can fail. */ |
| 11979 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 11980 | const char *error; |
| 11981 | |
| 11982 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 11983 | error = hlua_tostring_safe(flt_hlua->T, -1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11984 | else |
| 11985 | error = "critical error"; |
| 11986 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11987 | goto end; |
| 11988 | } |
| 11989 | |
Aurelien DARRAGON | f8b137b | 2024-03-04 11:06:24 +0100 | [diff] [blame] | 11990 | extra_idx = lua_gettop(flt_hlua->T); |
| 11991 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11992 | /* Check stack size. */ |
| 11993 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 11994 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11995 | RESET_SAFE_LJMP(flt_hlua); |
| 11996 | goto end; |
| 11997 | } |
| 11998 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11999 | hlua_pushref(flt_hlua->T, flt_ctx->ref); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12000 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 12001 | RESET_SAFE_LJMP(flt_hlua); |
| 12002 | goto end; |
| 12003 | } |
| 12004 | lua_insert(flt_hlua->T, -2); |
| 12005 | |
| 12006 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 12007 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 12008 | RESET_SAFE_LJMP(flt_hlua); |
| 12009 | goto end; |
| 12010 | } |
| 12011 | flt_hlua->nargs = 2; |
| 12012 | |
| 12013 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 12014 | if (dir == SMP_OPT_DIR_REQ) |
| 12015 | lua_getfield(flt_hlua->T, -1, "req"); |
| 12016 | else |
| 12017 | lua_getfield(flt_hlua->T, -1, "res"); |
| 12018 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 12019 | lua_pushstring(flt_hlua->T, "__filter"); |
| 12020 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 12021 | lua_settable(flt_hlua->T, -3); |
| 12022 | } |
| 12023 | flt_hlua->nargs++; |
| 12024 | } |
| 12025 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 12026 | if (dir == SMP_OPT_DIR_REQ) |
| 12027 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 12028 | else |
| 12029 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 12030 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 12031 | lua_pushstring(flt_hlua->T, "__filter"); |
| 12032 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 12033 | lua_settable(flt_hlua->T, -3); |
| 12034 | } |
| 12035 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12036 | } |
| 12037 | |
| 12038 | /* Check stack size. */ |
| 12039 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 12040 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 12041 | RESET_SAFE_LJMP(flt_hlua); |
| 12042 | goto end; |
| 12043 | } |
| 12044 | |
| 12045 | while (extra_idx--) { |
| 12046 | lua_pushvalue(flt_hlua->T, 1); |
| 12047 | lua_remove(flt_hlua->T, 1); |
| 12048 | flt_hlua->nargs++; |
| 12049 | } |
| 12050 | |
| 12051 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 12052 | hlua_timer_init(&flt_hlua->timer, hlua_timeout_session); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12053 | |
| 12054 | /* At this point the execution is safe. */ |
| 12055 | RESET_SAFE_LJMP(flt_hlua); |
| 12056 | } |
| 12057 | |
| 12058 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 12059 | case HLUA_E_OK: |
| 12060 | /* Catch the return value if it required */ |
| 12061 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 12062 | ret = lua_tointeger(flt_hlua->T, -1); |
| 12063 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 12064 | } |
| 12065 | |
| 12066 | /* Set timeout in the required channel. */ |
| 12067 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 12068 | if (dir == SMP_OPT_DIR_REQ) |
| 12069 | s->req.analyse_exp = flt_hlua->wake_time; |
| 12070 | else |
| 12071 | s->res.analyse_exp = flt_hlua->wake_time; |
| 12072 | } |
| 12073 | break; |
| 12074 | case HLUA_E_AGAIN: |
| 12075 | /* Set timeout in the required channel. */ |
| 12076 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 12077 | if (dir == SMP_OPT_DIR_REQ) |
| 12078 | s->req.analyse_exp = flt_hlua->wake_time; |
| 12079 | else |
| 12080 | s->res.analyse_exp = flt_hlua->wake_time; |
| 12081 | } |
| 12082 | /* Some actions can be wake up when a "write" event |
| 12083 | * is detected on a response channel. This is useful |
| 12084 | * only for actions targeted on the requests. |
| 12085 | */ |
| 12086 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 12087 | s->res.flags |= CF_WAKE_WRITE; |
| 12088 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 12089 | s->req.flags |= CF_WAKE_WRITE; |
| 12090 | ret = 0; |
| 12091 | goto end; |
| 12092 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12093 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, hlua_tostring_safe(flt_hlua->T, -1)); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12094 | ret = -1; |
| 12095 | goto end; |
| 12096 | case HLUA_E_ETMOUT: |
| 12097 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 12098 | goto end; |
| 12099 | case HLUA_E_NOMEM: |
| 12100 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 12101 | goto end; |
| 12102 | case HLUA_E_YIELD: |
| 12103 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 12104 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 12105 | goto end; |
| 12106 | case HLUA_E_ERR: |
| 12107 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 12108 | goto end; |
| 12109 | default: |
| 12110 | goto end; |
| 12111 | } |
| 12112 | |
| 12113 | |
| 12114 | end: |
| 12115 | return ret; |
| 12116 | } |
| 12117 | |
| 12118 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 12119 | { |
| 12120 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 12121 | |
| 12122 | flt_ctx->flags = 0; |
| 12123 | return hlua_filter_callback(s, filter, "start_analyze", |
| 12124 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 12125 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 12126 | } |
| 12127 | |
| 12128 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 12129 | { |
| 12130 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 12131 | |
| 12132 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 12133 | return hlua_filter_callback(s, filter, "end_analyze", |
| 12134 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 12135 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 12136 | } |
| 12137 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 12138 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 12139 | { |
| 12140 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 12141 | |
| 12142 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 12143 | return hlua_filter_callback(s, filter, "http_headers", |
| 12144 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 12145 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 12146 | } |
| 12147 | |
| 12148 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 12149 | unsigned int offset, unsigned int len) |
| 12150 | { |
| 12151 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 12152 | struct hlua *flt_hlua; |
| 12153 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 12154 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 12155 | int ret; |
| 12156 | |
| 12157 | flt_hlua = flt_ctx->hlua[idx]; |
| 12158 | flt_ctx->cur_off[idx] = offset; |
| 12159 | flt_ctx->cur_len[idx] = len; |
| 12160 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 12161 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 12162 | if (ret != -1) { |
| 12163 | ret = flt_ctx->cur_len[idx]; |
| 12164 | if (lua_gettop(flt_hlua->T) > 0) { |
| 12165 | ret = lua_tointeger(flt_hlua->T, -1); |
| 12166 | if (ret > flt_ctx->cur_len[idx]) |
| 12167 | ret = flt_ctx->cur_len[idx]; |
| 12168 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 12169 | } |
| 12170 | } |
| 12171 | return ret; |
| 12172 | } |
| 12173 | |
| 12174 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 12175 | { |
| 12176 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 12177 | |
| 12178 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 12179 | return hlua_filter_callback(s, filter, "http_end", |
| 12180 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 12181 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 12182 | } |
| 12183 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12184 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 12185 | unsigned int offset, unsigned int len) |
| 12186 | { |
| 12187 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 12188 | struct hlua *flt_hlua; |
| 12189 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 12190 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 12191 | int ret; |
| 12192 | |
| 12193 | flt_hlua = flt_ctx->hlua[idx]; |
| 12194 | flt_ctx->cur_off[idx] = offset; |
| 12195 | flt_ctx->cur_len[idx] = len; |
| 12196 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 12197 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 12198 | if (ret != -1) { |
| 12199 | ret = flt_ctx->cur_len[idx]; |
| 12200 | if (lua_gettop(flt_hlua->T) > 0) { |
| 12201 | ret = lua_tointeger(flt_hlua->T, -1); |
| 12202 | if (ret > flt_ctx->cur_len[idx]) |
| 12203 | ret = flt_ctx->cur_len[idx]; |
| 12204 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 12205 | } |
| 12206 | } |
| 12207 | return ret; |
| 12208 | } |
| 12209 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12210 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 12211 | struct flt_conf *fconf, char **err, void *private) |
| 12212 | { |
| 12213 | struct hlua_reg_filter *reg_flt = private; |
| 12214 | lua_State *L; |
| 12215 | struct hlua_flt_config *conf = NULL; |
| 12216 | const char *flt_id = NULL; |
| 12217 | int state_id, pos, flt_flags = 0; |
| 12218 | struct flt_ops *hlua_flt_ops = NULL; |
| 12219 | |
| 12220 | state_id = reg_flt_to_stack_id(reg_flt); |
| 12221 | L = hlua_states[state_id]; |
| 12222 | |
| 12223 | /* Initialize the filter ops with default callbacks */ |
| 12224 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12225 | if (!hlua_flt_ops) |
| 12226 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12227 | hlua_flt_ops->init = hlua_filter_init; |
| 12228 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 12229 | if (state_id) { |
| 12230 | /* Set per-thread callback if script is loaded per-thread */ |
| 12231 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 12232 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 12233 | } |
| 12234 | hlua_flt_ops->attach = hlua_filter_new; |
| 12235 | hlua_flt_ops->detach = hlua_filter_delete; |
| 12236 | |
| 12237 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 12238 | hlua_pushref(L, reg_flt->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12239 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12240 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 12241 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 12242 | lua_pop(L, 1); |
| 12243 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 12244 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 12245 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 12246 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 12247 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 12248 | lua_pop(L, 1); |
| 12249 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 12250 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 12251 | lua_pop(L, 1); |
| 12252 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 12253 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 12254 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12255 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 12256 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 12257 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12258 | |
| 12259 | /* Get id and flags of the filter class */ |
| 12260 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 12261 | flt_id = lua_tostring(L, -1); |
| 12262 | lua_pop(L, 1); |
| 12263 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 12264 | flt_flags = lua_tointeger(L, -1); |
| 12265 | lua_pop(L, 1); |
| 12266 | |
| 12267 | /* Create the filter config */ |
| 12268 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12269 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12270 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12271 | conf->reg = reg_flt; |
| 12272 | |
| 12273 | /* duplicate args */ |
| 12274 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 12275 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12276 | if (!conf->args) |
| 12277 | goto error; |
| 12278 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12279 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12280 | if (!conf->args[pos]) |
| 12281 | goto error; |
| 12282 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12283 | conf->args[pos] = NULL; |
| 12284 | *cur_arg += pos + 1; |
| 12285 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12286 | if (flt_id) { |
| 12287 | fconf->id = strdup(flt_id); |
| 12288 | if (!fconf->id) |
| 12289 | goto error; |
| 12290 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12291 | fconf->flags = flt_flags; |
| 12292 | fconf->conf = conf; |
| 12293 | fconf->ops = hlua_flt_ops; |
| 12294 | |
| 12295 | lua_settop(L, 0); |
| 12296 | return 0; |
| 12297 | |
| 12298 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12299 | memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12300 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12301 | if (conf && conf->args) { |
| 12302 | for (pos = 0; conf->args[pos]; pos++) |
| 12303 | free(conf->args[pos]); |
| 12304 | free(conf->args); |
| 12305 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12306 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 12307 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12308 | lua_settop(L, 0); |
| 12309 | return -1; |
| 12310 | } |
| 12311 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12312 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 12313 | { |
| 12314 | struct filter *filter; |
| 12315 | struct channel *chn; |
| 12316 | |
| 12317 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 12318 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 12319 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 12320 | |
| 12321 | lua_getfield(L, 1, "__filter"); |
| 12322 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 12323 | filter = lua_touserdata (L, -1); |
| 12324 | lua_pop(L, 1); |
| 12325 | |
| 12326 | register_data_filter(chn_strm(chn), chn, filter); |
| 12327 | return 1; |
| 12328 | } |
| 12329 | |
| 12330 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 12331 | { |
| 12332 | struct filter *filter; |
| 12333 | struct channel *chn; |
| 12334 | |
| 12335 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 12336 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 12337 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 12338 | |
| 12339 | lua_getfield(L, 1, "__filter"); |
| 12340 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 12341 | filter = lua_touserdata (L, -1); |
| 12342 | lua_pop(L, 1); |
| 12343 | |
| 12344 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 12345 | return 1; |
| 12346 | } |
| 12347 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12348 | /* This function is an LUA binding used for registering a filter. It expects a |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12349 | * filter name used in the haproxy configuration file and a LUA function to |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12350 | * parse configuration arguments. |
| 12351 | */ |
| 12352 | __LJMP static int hlua_register_filter(lua_State *L) |
| 12353 | { |
| 12354 | struct buffer *trash; |
| 12355 | struct flt_kw_list *fkl; |
| 12356 | struct flt_kw *fkw; |
| 12357 | const char *name; |
| 12358 | struct hlua_reg_filter *reg_flt= NULL; |
| 12359 | int flt_ref, fun_ref; |
| 12360 | int len; |
| 12361 | |
| 12362 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 12363 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 12364 | if (hlua_gethlua(L)) { |
| 12365 | /* runtime processing */ |
| 12366 | WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context")); |
| 12367 | } |
| 12368 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12369 | /* First argument : filter name. */ |
| 12370 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 12371 | |
| 12372 | /* Second argument : The filter class */ |
| 12373 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 12374 | |
| 12375 | /* Third argument : lua function. */ |
| 12376 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 12377 | |
| 12378 | trash = get_trash_chunk(); |
| 12379 | chunk_printf(trash, "lua.%s", name); |
| 12380 | fkw = flt_find_kw(trash->area); |
| 12381 | if (fkw != NULL) { |
| 12382 | reg_flt = fkw->private; |
| 12383 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 12384 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 12385 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 12386 | if (reg_flt->flt_ref[hlua_state_id] != -1) |
| 12387 | hlua_unref(L, reg_flt->flt_ref[hlua_state_id]); |
| 12388 | if (reg_flt->fun_ref[hlua_state_id] != -1) |
| 12389 | hlua_unref(L, reg_flt->fun_ref[hlua_state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12390 | } |
| 12391 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 12392 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 12393 | return 0; |
| 12394 | } |
| 12395 | |
| 12396 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 12397 | if (!fkl) |
| 12398 | goto alloc_error; |
| 12399 | fkl->scope = "HLUA"; |
| 12400 | |
| 12401 | reg_flt = new_hlua_reg_filter(name); |
| 12402 | if (!reg_flt) |
| 12403 | goto alloc_error; |
| 12404 | |
| 12405 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 12406 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 12407 | |
| 12408 | /* The filter keyword */ |
| 12409 | len = strlen("lua.") + strlen(name) + 1; |
| 12410 | fkl->kw[0].kw = calloc(1, len); |
| 12411 | if (!fkl->kw[0].kw) |
| 12412 | goto alloc_error; |
| 12413 | |
| 12414 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 12415 | |
| 12416 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 12417 | fkl->kw[0].private = reg_flt; |
| 12418 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 12419 | |
| 12420 | /* Register this new filter */ |
| 12421 | flt_register_keywords(fkl); |
| 12422 | |
| 12423 | return 0; |
| 12424 | |
| 12425 | alloc_error: |
| 12426 | release_hlua_reg_filter(reg_flt); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 12427 | hlua_unref(L, flt_ref); |
| 12428 | hlua_unref(L, fun_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12429 | ha_free(&fkl); |
| 12430 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 12431 | return 0; /* Never reached */ |
| 12432 | } |
| 12433 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12434 | 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] | 12435 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12436 | char **err, unsigned int *timeout) |
| 12437 | { |
| 12438 | const char *error; |
| 12439 | |
| 12440 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 12441 | if (error == PARSE_TIME_OVER) { |
| 12442 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 12443 | args[1], args[0]); |
| 12444 | return -1; |
| 12445 | } |
| 12446 | else if (error == PARSE_TIME_UNDER) { |
| 12447 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 12448 | args[1], args[0]); |
| 12449 | return -1; |
| 12450 | } |
| 12451 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12452 | memprintf(err, "%s: invalid timeout", args[0]); |
| 12453 | return -1; |
| 12454 | } |
| 12455 | return 0; |
| 12456 | } |
| 12457 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 12458 | static int hlua_burst_timeout(char **args, int section_type, struct proxy *curpx, |
| 12459 | const struct proxy *defpx, const char *file, int line, |
| 12460 | char **err) |
| 12461 | { |
| 12462 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12463 | file, line, err, &hlua_timeout_burst); |
| 12464 | } |
| 12465 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12466 | 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] | 12467 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12468 | char **err) |
| 12469 | { |
| 12470 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12471 | file, line, err, &hlua_timeout_session); |
| 12472 | } |
| 12473 | |
| 12474 | 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] | 12475 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12476 | char **err) |
| 12477 | { |
| 12478 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12479 | file, line, err, &hlua_timeout_task); |
| 12480 | } |
| 12481 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12482 | 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] | 12483 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12484 | char **err) |
| 12485 | { |
| 12486 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12487 | file, line, err, &hlua_timeout_applet); |
| 12488 | } |
| 12489 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12490 | 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] | 12491 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12492 | char **err) |
| 12493 | { |
| 12494 | char *error; |
| 12495 | |
| 12496 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 12497 | if (*error != '\0') { |
| 12498 | memprintf(err, "%s: invalid number", args[0]); |
| 12499 | return -1; |
| 12500 | } |
| 12501 | return 0; |
| 12502 | } |
| 12503 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12504 | 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] | 12505 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12506 | char **err) |
| 12507 | { |
| 12508 | char *error; |
| 12509 | |
| 12510 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12511 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).", args[0]); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12512 | return -1; |
| 12513 | } |
| 12514 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 12515 | if (*error != '\0') { |
| 12516 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 12517 | return -1; |
| 12518 | } |
| 12519 | return 0; |
| 12520 | } |
| 12521 | |
Tristan | 2632d04 | 2023-10-23 13:07:39 +0100 | [diff] [blame] | 12522 | static int hlua_cfg_parse_log_loggers(char **args, int section_type, struct proxy *curpx, |
| 12523 | const struct proxy *defpx, const char *file, int line, |
| 12524 | char **err) |
| 12525 | { |
| 12526 | if (too_many_args(1, args, err, NULL)) |
| 12527 | return -1; |
| 12528 | |
| 12529 | if (strcmp(args[1], "on") == 0) |
| 12530 | hlua_log_opts |= HLUA_LOG_LOGGERS_ON; |
| 12531 | else if (strcmp(args[1], "off") == 0) |
| 12532 | hlua_log_opts &= ~HLUA_LOG_LOGGERS_ON; |
| 12533 | else { |
| 12534 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 12535 | return -1; |
| 12536 | } |
| 12537 | return 0; |
| 12538 | } |
| 12539 | |
| 12540 | static int hlua_cfg_parse_log_stderr(char **args, int section_type, struct proxy *curpx, |
| 12541 | const struct proxy *defpx, const char *file, int line, |
| 12542 | char **err) |
| 12543 | { |
| 12544 | if (too_many_args(1, args, err, NULL)) |
| 12545 | return -1; |
| 12546 | |
| 12547 | if (strcmp(args[1], "on") == 0) |
| 12548 | hlua_log_opts = (hlua_log_opts & ~HLUA_LOG_STDERR_MASK) | HLUA_LOG_STDERR_ON; |
| 12549 | else if (strcmp(args[1], "auto") == 0) |
| 12550 | hlua_log_opts = (hlua_log_opts & ~HLUA_LOG_STDERR_MASK) | HLUA_LOG_STDERR_AUTO; |
| 12551 | else if (strcmp(args[1], "off") == 0) |
| 12552 | hlua_log_opts &= ~HLUA_LOG_STDERR_MASK; |
| 12553 | else { |
| 12554 | memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]); |
| 12555 | return -1; |
| 12556 | } |
| 12557 | return 0; |
| 12558 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12559 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12560 | /* This function is called by the main configuration key "lua-load". It loads and |
| 12561 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 12562 | * the main lua entry point. |
| 12563 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12564 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 12565 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12566 | * |
| 12567 | * In some error case, LUA set an error message in top of the stack. This function |
| 12568 | * 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] | 12569 | * |
| 12570 | * This function can fail with an abort() due to an Lua critical error. |
| 12571 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 12572 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12573 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12574 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12575 | { |
| 12576 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12577 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12578 | |
| 12579 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12580 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12581 | if (error) { |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12582 | memprintf(err, "error in Lua file '%s': %s", args[0], hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12583 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12584 | return -1; |
| 12585 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12586 | |
| 12587 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 12588 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 12589 | /* Check stack size. */ |
| 12590 | if (!lua_checkstack(L, 1)) { |
| 12591 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 12592 | return -1; |
| 12593 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12594 | lua_pushstring(L, args[nargs]); |
| 12595 | } |
| 12596 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12597 | |
| 12598 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12599 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12600 | switch (error) { |
| 12601 | case LUA_OK: |
| 12602 | break; |
| 12603 | case LUA_ERRRUN: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12604 | memprintf(err, "Lua runtime error: %s", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12605 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12606 | return -1; |
| 12607 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12608 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12609 | return -1; |
| 12610 | case LUA_ERRERR: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12611 | memprintf(err, "Lua message handler error: %s", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12612 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12613 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12614 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12615 | case LUA_ERRGCMM: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12616 | memprintf(err, "Lua garbage collector error: %s", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12617 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12618 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12619 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12620 | default: |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12621 | memprintf(err, "Lua unknown error: %s", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12622 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12623 | return -1; |
| 12624 | } |
| 12625 | |
| 12626 | return 0; |
| 12627 | } |
| 12628 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12629 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 12630 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12631 | char **err) |
| 12632 | { |
| 12633 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12634 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12635 | return -1; |
| 12636 | } |
| 12637 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12638 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12639 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12640 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12641 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12642 | } |
| 12643 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12644 | 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] | 12645 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12646 | char **err) |
| 12647 | { |
| 12648 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12649 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12650 | |
| 12651 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12652 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12653 | return -1; |
| 12654 | } |
| 12655 | |
| 12656 | if (per_thread_load == NULL) { |
| 12657 | /* allocate the first entry large enough to store the final NULL */ |
| 12658 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 12659 | if (per_thread_load == NULL) { |
| 12660 | memprintf(err, "out of memory error"); |
| 12661 | return -1; |
| 12662 | } |
| 12663 | } |
| 12664 | |
| 12665 | /* count used entries */ |
| 12666 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 12667 | ; |
| 12668 | |
| 12669 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 12670 | if (per_thread_load == NULL) { |
| 12671 | memprintf(err, "out of memory error"); |
| 12672 | return -1; |
| 12673 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12674 | per_thread_load[len + 1] = NULL; |
| 12675 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12676 | /* count args excepting the first, allocate array and copy args */ |
| 12677 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 12678 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12679 | if (per_thread_load[len] == NULL) { |
| 12680 | memprintf(err, "out of memory error"); |
| 12681 | return -1; |
| 12682 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12683 | for (i = 1; *(args[i]) != 0; i++) { |
| 12684 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 12685 | if (per_thread_load[len][i - 1] == NULL) { |
| 12686 | memprintf(err, "out of memory error"); |
| 12687 | return -1; |
| 12688 | } |
| 12689 | } |
| 12690 | per_thread_load[len][i - 1] = strdup(""); |
| 12691 | if (per_thread_load[len][i - 1] == NULL) { |
| 12692 | memprintf(err, "out of memory error"); |
| 12693 | return -1; |
| 12694 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12695 | |
| 12696 | /* loading for thread 1 only */ |
| 12697 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12698 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12699 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12700 | } |
| 12701 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12702 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 12703 | * in the given <ctx>. |
| 12704 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12705 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12706 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12707 | lua_getglobal(L, "package"); /* push package variable */ |
| 12708 | lua_pushstring(L, path); /* push given path */ |
| 12709 | lua_pushstring(L, ";"); /* push semicolon */ |
| 12710 | lua_getfield(L, -3, type); /* push old path */ |
| 12711 | lua_concat(L, 3); /* concatenate to new path */ |
| 12712 | lua_setfield(L, -2, type); /* store new path */ |
| 12713 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12714 | |
| 12715 | return 0; |
| 12716 | } |
| 12717 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12718 | 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] | 12719 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12720 | char **err) |
| 12721 | { |
| 12722 | char *path; |
| 12723 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12724 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12725 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12726 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12727 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12728 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12729 | } |
| 12730 | |
| 12731 | if (!(*args[1])) { |
| 12732 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12733 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12734 | } |
| 12735 | path = args[1]; |
| 12736 | |
| 12737 | if (*args[2]) { |
| 12738 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 12739 | 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] | 12740 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12741 | } |
| 12742 | type = args[2]; |
| 12743 | } |
| 12744 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12745 | p = calloc(1, sizeof(*p)); |
| 12746 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12747 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12748 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12749 | } |
| 12750 | p->path = strdup(path); |
| 12751 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12752 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12753 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12754 | } |
| 12755 | p->type = strdup(type); |
| 12756 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12757 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12758 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12759 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 12760 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12761 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12762 | /* Handle the global state and the per-thread state for the first |
| 12763 | * thread. The remaining threads will be initialized based on |
| 12764 | * prepend_path_list. |
| 12765 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12766 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12767 | lua_State *L = hlua_states[i]; |
| 12768 | const char *error; |
| 12769 | |
| 12770 | if (setjmp(safe_ljmp_env) != 0) { |
| 12771 | lua_atpanic(L, hlua_panic_safe); |
| 12772 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 12773 | error = hlua_tostring_safe(L, -1); |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12774 | else |
| 12775 | error = "critical error"; |
| 12776 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 12777 | exit(1); |
| 12778 | } else { |
| 12779 | lua_atpanic(L, hlua_panic_ljmp); |
| 12780 | } |
| 12781 | |
| 12782 | hlua_prepend_path(L, type, path); |
| 12783 | |
| 12784 | lua_atpanic(L, hlua_panic_safe); |
| 12785 | } |
| 12786 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12787 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12788 | |
| 12789 | err2: |
| 12790 | free(p->type); |
| 12791 | free(p->path); |
| 12792 | err: |
| 12793 | free(p); |
| 12794 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12795 | } |
| 12796 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12797 | /* configuration keywords declaration */ |
| 12798 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12799 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12800 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12801 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12802 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 12803 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 12804 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 12805 | { CFG_GLOBAL, "tune.lua.burst-timeout", hlua_burst_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12806 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12807 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Tristan | 2632d04 | 2023-10-23 13:07:39 +0100 | [diff] [blame] | 12808 | { CFG_GLOBAL, "tune.lua.log.loggers", hlua_cfg_parse_log_loggers }, |
| 12809 | { CFG_GLOBAL, "tune.lua.log.stderr", hlua_cfg_parse_log_stderr }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12810 | { 0, NULL, NULL }, |
| 12811 | }}; |
| 12812 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12813 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12814 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12815 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 12816 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12817 | /* |
| 12818 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 12819 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 12820 | * difference is that the yield in lua and for the CLI is not handled the same |
| 12821 | * way. |
| 12822 | */ |
| 12823 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 12824 | { |
| 12825 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 12826 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 12827 | struct ckch_inst *ckchi = *lua_ckchi; |
| 12828 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 12829 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 12830 | struct hlua *hlua; |
| 12831 | char *err = NULL; |
| 12832 | int y = 1; |
| 12833 | |
| 12834 | hlua = hlua_gethlua(L); |
| 12835 | |
| 12836 | /* get the first ckchi to copy */ |
| 12837 | if (ckchi == NULL) |
| 12838 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 12839 | |
| 12840 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 12841 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 12842 | struct ckch_inst *new_inst; |
| 12843 | |
| 12844 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 12845 | if (y % 10 == 0) { |
| 12846 | |
| 12847 | *lua_ckchi = ckchi; |
| 12848 | |
| 12849 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12850 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12851 | } |
| 12852 | |
| 12853 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 12854 | goto error; |
| 12855 | |
| 12856 | /* link the new ckch_inst to the duplicate */ |
| 12857 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 12858 | y++; |
| 12859 | } |
| 12860 | |
| 12861 | /* The generation is finished, we can insert everything */ |
| 12862 | ckch_store_replace(old_ckchs, new_ckchs); |
| 12863 | |
| 12864 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 12865 | |
| 12866 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12867 | |
| 12868 | return 0; |
| 12869 | |
| 12870 | error: |
| 12871 | ckch_store_free(new_ckchs); |
| 12872 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12873 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12874 | free(err); |
| 12875 | |
| 12876 | return 0; |
| 12877 | } |
| 12878 | |
| 12879 | /* |
| 12880 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 12881 | * from the table in parameter. |
| 12882 | * |
| 12883 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 12884 | * means it does not need to have a transaction since everything is done in the |
| 12885 | * same function. |
| 12886 | * |
| 12887 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 12888 | * |
| 12889 | */ |
| 12890 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12891 | { |
| 12892 | struct hlua *hlua; |
| 12893 | struct ckch_inst **lua_ckchi; |
| 12894 | struct ckch_store **lua_ckchs; |
| 12895 | struct ckch_store *old_ckchs = NULL; |
| 12896 | struct ckch_store *new_ckchs = NULL; |
| 12897 | int errcode = 0; |
| 12898 | char *err = NULL; |
| 12899 | struct cert_exts *cert_ext = NULL; |
| 12900 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12901 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12902 | int ret; |
| 12903 | |
| 12904 | if (lua_type(L, -1) != LUA_TTABLE) |
| 12905 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 12906 | |
| 12907 | hlua = hlua_gethlua(L); |
| 12908 | |
| 12909 | /* FIXME: this should not return an error but should come back later */ |
| 12910 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12911 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 12912 | |
| 12913 | ret = lua_getfield(L, -1, "filename"); |
| 12914 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12915 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12916 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12917 | goto end; |
| 12918 | } |
| 12919 | filename = (char *)lua_tostring(L, -1); |
| 12920 | |
| 12921 | |
| 12922 | /* look for the filename in the tree */ |
| 12923 | old_ckchs = ckchs_lookup(filename); |
| 12924 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12925 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12926 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12927 | goto end; |
| 12928 | } |
| 12929 | /* TODO: handle extra_files_noext */ |
| 12930 | |
| 12931 | new_ckchs = ckchs_dup(old_ckchs); |
| 12932 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12933 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12934 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12935 | goto end; |
| 12936 | } |
| 12937 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12938 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12939 | |
| 12940 | /* loop on the field in the table, which have the same name as the |
| 12941 | * possible extensions of files */ |
| 12942 | lua_pushnil(L); |
| 12943 | while (lua_next(L, 1)) { |
| 12944 | int i; |
| 12945 | const char *field = lua_tostring(L, -2); |
| 12946 | char *payload = (char *)lua_tostring(L, -1); |
| 12947 | |
| 12948 | if (!field || strcmp(field, "filename") == 0) { |
| 12949 | lua_pop(L, 1); |
| 12950 | continue; |
| 12951 | } |
| 12952 | |
| 12953 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 12954 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 12955 | cert_ext = &cert_exts[i]; |
| 12956 | break; |
| 12957 | } |
| 12958 | } |
| 12959 | |
| 12960 | /* this is the default type, the field is not supported */ |
| 12961 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12962 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12963 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12964 | goto end; |
| 12965 | } |
| 12966 | |
Frédéric Lécaille | 1409dea | 2023-12-06 11:42:42 +0100 | [diff] [blame] | 12967 | /* Reset the OCSP CID */ |
| 12968 | if (cert_ext->type == CERT_TYPE_PEM || cert_ext->type == CERT_TYPE_KEY || |
| 12969 | cert_ext->type == CERT_TYPE_ISSUER) { |
| 12970 | OCSP_CERTID_free(new_ckchs->data->ocsp_cid); |
| 12971 | new_ckchs->data->ocsp_cid = NULL; |
| 12972 | } |
| 12973 | |
| 12974 | /* apply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12975 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12976 | memprintf(&err, "%sCan't load the payload for '%s'", err ? err : "", cert_ext->ext); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12977 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12978 | goto end; |
| 12979 | } |
| 12980 | lua_pop(L, 1); |
| 12981 | } |
| 12982 | |
| 12983 | /* store the pointers on the lua stack */ |
| 12984 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 12985 | lua_ckchs[0] = old_ckchs; |
| 12986 | lua_ckchs[1] = new_ckchs; |
| 12987 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 12988 | *lua_ckchi = NULL; |
| 12989 | |
| 12990 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12991 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12992 | |
| 12993 | end: |
| 12994 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12995 | |
| 12996 | if (errcode & ERR_CODE) { |
| 12997 | ckch_store_free(new_ckchs); |
| 12998 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12999 | } |
| 13000 | free(err); |
| 13001 | |
| 13002 | return 0; |
| 13003 | } |
| 13004 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 13005 | #else |
| 13006 | |
| 13007 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 13008 | { |
| 13009 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 13010 | |
| 13011 | return 0; |
| 13012 | } |
| 13013 | #endif /* ! USE_OPENSSL */ |
| 13014 | |
| 13015 | |
| 13016 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 13017 | /* This function can fail with an abort() due to an Lua critical error. |
| 13018 | * We are in the initialisation process of HAProxy, this abort() is |
| 13019 | * tolerated. |
| 13020 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13021 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13022 | { |
| 13023 | struct hlua_init_function *init; |
| 13024 | const char *msg; |
| 13025 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 13026 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13027 | const char *kind; |
| 13028 | const char *trace; |
| 13029 | int return_status = 1; |
| 13030 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 13031 | int nres; |
| 13032 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13033 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 13034 | /* disable memory limit checks if limit is not set */ |
| 13035 | if (!hlua_global_allocator.limit) |
| 13036 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 13037 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 13038 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 13039 | if (setjmp(safe_ljmp_env) != 0) { |
| 13040 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13041 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 13042 | error = hlua_tostring_safe(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 13043 | else |
| 13044 | error = "critical error"; |
| 13045 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 13046 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 13047 | } else { |
| 13048 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 13049 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 13050 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 13051 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 13052 | hlua_pushref(L, init->function_ref); |
Aurelien DARRAGON | 16d047b | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 13053 | /* function ref should be released right away since it was pushed |
| 13054 | * on the stack and will not be used anymore |
| 13055 | */ |
| 13056 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13057 | |
| 13058 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 3ddcd1b | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 13059 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13060 | #else |
Aurelien DARRAGON | 3ddcd1b | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 13061 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13062 | #endif |
| 13063 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13064 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13065 | |
| 13066 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13067 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 13068 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13069 | |
| 13070 | case LUA_ERRERR: |
| 13071 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 13072 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13073 | case LUA_ERRRUN: |
| 13074 | if (!kind) |
| 13075 | kind = "runtime error"; |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 13076 | msg = hlua_tostring_safe(L, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 13077 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13078 | if (msg) |
| 13079 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 13080 | else |
| 13081 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
Aurelien DARRAGON | 93361a5 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 13082 | |
| 13083 | lua_settop(L, 0); /* Empty the stack. */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13084 | return_status = 0; |
| 13085 | break; |
| 13086 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13087 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13088 | /* Unknown error */ |
| 13089 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 13090 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13091 | case LUA_YIELD: |
| 13092 | /* yield is not configured at this step, this state doesn't happen */ |
| 13093 | if (!kind) |
| 13094 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 13095 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13096 | case LUA_ERRMEM: |
| 13097 | if (!kind) |
| 13098 | kind = "out of memory error"; |
Aurelien DARRAGON | 3e81aed | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 13099 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 13100 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13101 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 13102 | return_status = 0; |
| 13103 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13104 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13105 | if (!return_status) |
| 13106 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13107 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 13108 | |
| 13109 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 13110 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13111 | } |
| 13112 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13113 | int hlua_post_init() |
| 13114 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13115 | int ret; |
| 13116 | int i; |
| 13117 | int errors; |
| 13118 | char *err = NULL; |
| 13119 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13120 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13121 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 13122 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13123 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13124 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13125 | int saved_used_backed = global.ssl_used_backend; |
| 13126 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13127 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13128 | global.ssl_used_backend = saved_used_backed; |
| 13129 | } |
| 13130 | #endif |
| 13131 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 13132 | /* Perform post init of common thread */ |
| 13133 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13134 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13135 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 13136 | if (ret == 0) |
| 13137 | return 0; |
| 13138 | |
| 13139 | /* init remaining lua states and load files */ |
| 13140 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 13141 | |
| 13142 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13143 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13144 | |
| 13145 | /* Init lua state */ |
| 13146 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 13147 | |
| 13148 | /* Load lua files */ |
| 13149 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 13150 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 13151 | if (ret != 0) { |
| 13152 | ha_alert("Lua init: %s\n", err); |
| 13153 | return 0; |
| 13154 | } |
| 13155 | } |
| 13156 | } |
| 13157 | |
| 13158 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13159 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13160 | |
| 13161 | /* Execute post init for all states */ |
| 13162 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 13163 | |
| 13164 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13165 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13166 | |
| 13167 | /* run post init */ |
| 13168 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 13169 | if (ret == 0) |
| 13170 | return 0; |
| 13171 | } |
| 13172 | |
| 13173 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13174 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13175 | |
| 13176 | /* control functions registering. Each function must have: |
| 13177 | * - only the function_ref[0] set positive and all other to -1 |
| 13178 | * - only the function_ref[0] set to -1 and all other positive |
| 13179 | * This ensure a same reference is not used both in shared |
| 13180 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 13181 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13182 | * complicated to found for the end user. |
| 13183 | */ |
| 13184 | errors = 0; |
| 13185 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 13186 | ret = 0; |
| 13187 | for (i = 1; i < global.nbthread + 1; i++) { |
| 13188 | if (fcn->function_ref[i] == -1) |
| 13189 | ret--; |
| 13190 | else |
| 13191 | ret++; |
| 13192 | } |
| 13193 | if (abs(ret) != global.nbthread) { |
| 13194 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 13195 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 13196 | errors++; |
| 13197 | continue; |
| 13198 | } |
| 13199 | |
| 13200 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 13201 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 13202 | "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] | 13203 | "exclusive.\n", fcn->name); |
| 13204 | errors++; |
| 13205 | } |
| 13206 | } |
| 13207 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13208 | /* Do the same with registered filters */ |
| 13209 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 13210 | ret = 0; |
| 13211 | for (i = 1; i < global.nbthread + 1; i++) { |
| 13212 | if (reg_flt->flt_ref[i] == -1) |
| 13213 | ret--; |
| 13214 | else |
| 13215 | ret++; |
| 13216 | } |
| 13217 | if (abs(ret) != global.nbthread) { |
| 13218 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 13219 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 13220 | errors++; |
| 13221 | continue; |
| 13222 | } |
| 13223 | |
| 13224 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 13225 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 13226 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 13227 | "exclusive.\n", fcn->name); |
| 13228 | errors++; |
| 13229 | } |
| 13230 | } |
| 13231 | |
| 13232 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13233 | if (errors > 0) |
| 13234 | return 0; |
| 13235 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 13236 | /* 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] | 13237 | * -1 in order to have probably a segfault if someone use it |
| 13238 | */ |
| 13239 | hlua_state_id = -1; |
| 13240 | |
| 13241 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 13242 | } |
| 13243 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 13244 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 13245 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 13246 | * 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] | 13247 | * allocation. <nsize> is the requested new size. A new allocation is |
| 13248 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 13249 | * zero. This one verifies that the limits are respected but is optimized |
| 13250 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 13251 | * |
| 13252 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 13253 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 13254 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 13255 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 13256 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 13257 | */ |
| 13258 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 13259 | { |
| 13260 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 13261 | size_t limit, old, new; |
| 13262 | |
| 13263 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 13264 | * for accounting anymore. |
| 13265 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 13266 | if (likely(~zone->limit == 0)) { |
| 13267 | if (!nsize) |
| 13268 | ha_free(&ptr); |
| 13269 | else |
| 13270 | ptr = realloc(ptr, nsize); |
| 13271 | return ptr; |
| 13272 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 13273 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 13274 | if (!ptr) |
| 13275 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 13276 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 13277 | /* enforce strict limits across all threads */ |
| 13278 | limit = zone->limit; |
| 13279 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 13280 | do { |
| 13281 | new = old + nsize - osize; |
| 13282 | if (unlikely(nsize && limit && new > limit)) |
| 13283 | return NULL; |
| 13284 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 13285 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 13286 | if (!nsize) |
| 13287 | ha_free(&ptr); |
| 13288 | else |
| 13289 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 13290 | |
| 13291 | if (unlikely(!ptr && nsize)) // failed |
| 13292 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 13293 | |
| 13294 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 13295 | return ptr; |
| 13296 | } |
| 13297 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 13298 | /* 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] | 13299 | * We are in the initialisation process of HAProxy, this abort() is |
| 13300 | * tolerated. |
| 13301 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 13302 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 13303 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13304 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13305 | int idx; |
| 13306 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13307 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13308 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 13309 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 13310 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13311 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13312 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13313 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 13314 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13315 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 13316 | |
firexinghe | 64d7afb | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 13317 | if (!L) { |
| 13318 | fprintf(stderr, |
| 13319 | "Lua init: critical error: lua_newstate() returned NULL." |
| 13320 | " This may possibly be caused by a memory allocation error.\n"); |
| 13321 | exit(1); |
| 13322 | } |
| 13323 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 13324 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13325 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 13326 | *context = NULL; |
| 13327 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 13328 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 13329 | * the Lua function can fail with an abort. We are in the initialisation |
| 13330 | * process of HAProxy, this abort() is tolerated. |
| 13331 | */ |
| 13332 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 13333 | /* Call post initialisation function in safe environment. */ |
| 13334 | if (setjmp(safe_ljmp_env) != 0) { |
| 13335 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13336 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | cf51189 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 13337 | error_msg = hlua_tostring_safe(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 13338 | else |
| 13339 | error_msg = "critical error"; |
| 13340 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 13341 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 13342 | } else { |
| 13343 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 13344 | } |
| 13345 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 13346 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13347 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 13348 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 13349 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 13350 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13351 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 13352 | #endif |
| 13353 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13354 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 13355 | #endif |
| 13356 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 13357 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13358 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 13359 | /* Apply configured prepend path */ |
| 13360 | list_for_each_entry(pp, &prepend_path_list, l) |
| 13361 | hlua_prepend_path(L, pp->type, pp->path); |
| 13362 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13363 | /* |
Aurelien DARRAGON | 8cd620b | 2023-04-07 17:37:46 +0200 | [diff] [blame] | 13364 | * Override some lua functions. |
| 13365 | * |
| 13366 | */ |
| 13367 | |
| 13368 | /* push our "safe" coroutine.create() function */ |
| 13369 | lua_getglobal(L, "coroutine"); |
| 13370 | lua_pushcclosure(L, hlua_coroutine_create, 0); |
| 13371 | lua_setfield(L, -2, "create"); |
| 13372 | |
| 13373 | /* |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13374 | * |
| 13375 | * Create "core" object. |
| 13376 | * |
| 13377 | */ |
| 13378 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 13379 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13380 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13381 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 13382 | /* set the thread id */ |
| 13383 | hlua_class_const_int(L, "thread", thread_num); |
| 13384 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13385 | /* Push the loglevel constants. */ |
Christopher Faulet | 12a5ee7 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 13386 | hlua_class_const_int(L, "silent", -1); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 13387 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13388 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 13389 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13390 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13391 | hlua_class_function(L, "register_init", hlua_register_init); |
| 13392 | hlua_class_function(L, "register_task", hlua_register_task); |
| 13393 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 13394 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 13395 | hlua_class_function(L, "register_action", hlua_register_action); |
| 13396 | hlua_class_function(L, "register_service", hlua_register_service); |
| 13397 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13398 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13399 | hlua_class_function(L, "yield", hlua_yield); |
| 13400 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 13401 | hlua_class_function(L, "sleep", hlua_sleep); |
| 13402 | hlua_class_function(L, "msleep", hlua_msleep); |
| 13403 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 13404 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 13405 | hlua_class_function(L, "set_map", hlua_set_map); |
| 13406 | hlua_class_function(L, "del_map", hlua_del_map); |
| 13407 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13408 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 13409 | hlua_class_function(L, "event_sub", hlua_event_global_sub); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13410 | hlua_class_function(L, "log", hlua_log); |
| 13411 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 13412 | hlua_class_function(L, "Info", hlua_log_info); |
| 13413 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 13414 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 13415 | hlua_class_function(L, "done", hlua_done); |
Aurelien DARRAGON | 5bed48f | 2023-04-21 17:32:46 +0200 | [diff] [blame] | 13416 | hlua_class_function(L, "disable_legacy_mailers", hlua_disable_legacy_mailers); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13417 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 13418 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13419 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13420 | |
| 13421 | /* |
| 13422 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 13423 | * Create "act" object. |
| 13424 | * |
| 13425 | */ |
| 13426 | |
| 13427 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13428 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 13429 | |
| 13430 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13431 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 13432 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 13433 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 13434 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 13435 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 13436 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 13437 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 13438 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 13439 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13440 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 13441 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13442 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 13443 | |
| 13444 | /* |
| 13445 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13446 | * Create "Filter" object. |
| 13447 | * |
| 13448 | */ |
| 13449 | |
| 13450 | /* This table entry is the object "filter" base. */ |
| 13451 | lua_newtable(L); |
| 13452 | |
| 13453 | /* push flags and constants */ |
| 13454 | hlua_class_const_int(L, "CONTINUE", 1); |
| 13455 | hlua_class_const_int(L, "WAIT", 0); |
| 13456 | hlua_class_const_int(L, "ERROR", -1); |
| 13457 | |
| 13458 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 13459 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 13460 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 13461 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 13462 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 13463 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13464 | lua_setglobal(L, "filter"); |
| 13465 | |
| 13466 | /* |
| 13467 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13468 | * Register class Map |
| 13469 | * |
| 13470 | */ |
| 13471 | |
| 13472 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13473 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13474 | |
| 13475 | /* register pattern types. */ |
| 13476 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13477 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 13478 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13479 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13480 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 13481 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13482 | |
| 13483 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13484 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13485 | |
| 13486 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13487 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13488 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13489 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13490 | lua_pushstring(L, "__index"); |
| 13491 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13492 | |
| 13493 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13494 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 13495 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13496 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13497 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13498 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 13499 | /* Register previous table in the registry with reference and named entry. |
| 13500 | * The function hlua_register_metatable() pops the stack, so we |
| 13501 | * previously create a copy of the table. |
| 13502 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13503 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 13504 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13505 | |
| 13506 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13507 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13508 | |
| 13509 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13510 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13511 | |
| 13512 | /* |
| 13513 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 13514 | * Register "CertCache" class |
| 13515 | * |
| 13516 | */ |
| 13517 | |
| 13518 | /* Create and fill the metatable. */ |
| 13519 | lua_newtable(L); |
| 13520 | /* Register */ |
| 13521 | hlua_class_function(L, "set", hlua_ckch_set); |
| 13522 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 13523 | |
| 13524 | /* |
| 13525 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13526 | * Register class Channel |
| 13527 | * |
| 13528 | */ |
| 13529 | |
| 13530 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13531 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13532 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13533 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13534 | lua_pushstring(L, "__index"); |
| 13535 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13536 | |
| 13537 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13538 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 13539 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 13540 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 13541 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13542 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13543 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 13544 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13545 | hlua_class_function(L, "send", hlua_channel_send); |
| 13546 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13547 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 13548 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 13549 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13550 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 13551 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13552 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13553 | /* Deprecated API */ |
| 13554 | hlua_class_function(L, "get", hlua_channel_get); |
| 13555 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 13556 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 13557 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 13558 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 13559 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13560 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13561 | |
| 13562 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13563 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13564 | |
| 13565 | /* |
| 13566 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13567 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13568 | * |
| 13569 | */ |
| 13570 | |
| 13571 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13572 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13573 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13574 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13575 | lua_pushstring(L, "__index"); |
| 13576 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13577 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13578 | /* Browse existing fetches and create the associated |
| 13579 | * object method. |
| 13580 | */ |
| 13581 | sf = NULL; |
| 13582 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13583 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13584 | * by an underscore. |
| 13585 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13586 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13587 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13588 | if (*p == '.' || *p == '-' || *p == '+') |
| 13589 | *p = '_'; |
| 13590 | |
| 13591 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13592 | lua_pushstring(L, trash.area); |
| 13593 | lua_pushlightuserdata(L, sf); |
| 13594 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 13595 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13596 | } |
| 13597 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13598 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13599 | |
| 13600 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13601 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13602 | |
| 13603 | /* |
| 13604 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13605 | * Register class Converters |
| 13606 | * |
| 13607 | */ |
| 13608 | |
| 13609 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13610 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13611 | |
| 13612 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13613 | lua_pushstring(L, "__index"); |
| 13614 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13615 | |
| 13616 | /* Browse existing converters and create the associated |
| 13617 | * object method. |
| 13618 | */ |
| 13619 | sc = NULL; |
| 13620 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13621 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13622 | * by an underscore. |
| 13623 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13624 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13625 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13626 | if (*p == '.' || *p == '-' || *p == '+') |
| 13627 | *p = '_'; |
| 13628 | |
| 13629 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13630 | lua_pushstring(L, trash.area); |
| 13631 | lua_pushlightuserdata(L, sc); |
| 13632 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 13633 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13634 | } |
| 13635 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13636 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13637 | |
| 13638 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13639 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13640 | |
| 13641 | /* |
| 13642 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13643 | * Register class HTTP |
| 13644 | * |
| 13645 | */ |
| 13646 | |
| 13647 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13648 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13649 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13650 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13651 | lua_pushstring(L, "__index"); |
| 13652 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13653 | |
| 13654 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13655 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 13656 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 13657 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 13658 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 13659 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 13660 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 13661 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 13662 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 13663 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 13664 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13665 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13666 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 13667 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 13668 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 13669 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 13670 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 13671 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 13672 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13673 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13674 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13675 | |
| 13676 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13677 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13678 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13679 | /* |
| 13680 | * |
| 13681 | * Register class HTTPMessage |
| 13682 | * |
| 13683 | */ |
| 13684 | |
| 13685 | /* Create and fill the metatable. */ |
| 13686 | lua_newtable(L); |
| 13687 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 13688 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13689 | lua_pushstring(L, "__index"); |
| 13690 | lua_newtable(L); |
| 13691 | |
| 13692 | /* Register Lua functions. */ |
| 13693 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 13694 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 13695 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 13696 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 13697 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 13698 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 13699 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 13700 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 13701 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 13702 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 13703 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 13704 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 13705 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 13706 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 13707 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 13708 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 13709 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 13710 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 13711 | |
| 13712 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 13713 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 13714 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 13715 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 13716 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 13717 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 13718 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 13719 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 13720 | |
| 13721 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 13722 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 13723 | |
| 13724 | lua_rawset(L, -3); |
| 13725 | |
| 13726 | /* Register previous table in the registry with reference and named entry. */ |
| 13727 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13728 | |
| 13729 | /* |
| 13730 | * |
| 13731 | * Register class HTTPClient |
| 13732 | * |
| 13733 | */ |
| 13734 | |
| 13735 | /* Create and fill the metatable. */ |
| 13736 | lua_newtable(L); |
| 13737 | lua_pushstring(L, "__index"); |
| 13738 | lua_newtable(L); |
| 13739 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 13740 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 13741 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 13742 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 13743 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13744 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 13745 | /* Register the garbage collector entry. */ |
| 13746 | lua_pushstring(L, "__gc"); |
| 13747 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 13748 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 13749 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13750 | |
| 13751 | |
| 13752 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13753 | /* |
| 13754 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13755 | * Register class AppletTCP |
| 13756 | * |
| 13757 | */ |
| 13758 | |
| 13759 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13760 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13761 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13762 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13763 | lua_pushstring(L, "__index"); |
| 13764 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13765 | |
| 13766 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13767 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 13768 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 13769 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 13770 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 13771 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 13772 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 13773 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 13774 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13775 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13776 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13777 | |
| 13778 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13779 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13780 | |
| 13781 | /* |
| 13782 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13783 | * Register class AppletHTTP |
| 13784 | * |
| 13785 | */ |
| 13786 | |
| 13787 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13788 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13789 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13790 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13791 | lua_pushstring(L, "__index"); |
| 13792 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13793 | |
| 13794 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13795 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 13796 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 13797 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 13798 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 13799 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 13800 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 13801 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 13802 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 13803 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 13804 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 13805 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13806 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13807 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13808 | |
| 13809 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13810 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13811 | |
| 13812 | /* |
| 13813 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13814 | * Register class TXN |
| 13815 | * |
| 13816 | */ |
| 13817 | |
| 13818 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13819 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13820 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13821 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13822 | lua_pushstring(L, "__index"); |
| 13823 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13824 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13825 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13826 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 13827 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 13828 | hlua_class_function(L, "set_var", hlua_set_var); |
| 13829 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 13830 | hlua_class_function(L, "get_var", hlua_get_var); |
| 13831 | hlua_class_function(L, "done", hlua_txn_done); |
| 13832 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 13833 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 13834 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 13835 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 13836 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 13837 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 13838 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 13839 | hlua_class_function(L, "log", hlua_txn_log); |
| 13840 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 13841 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 13842 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 13843 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13844 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13845 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13846 | |
| 13847 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13848 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13849 | |
| 13850 | /* |
| 13851 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13852 | * Register class reply |
| 13853 | * |
| 13854 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13855 | lua_newtable(L); |
| 13856 | lua_pushstring(L, "__index"); |
| 13857 | lua_newtable(L); |
| 13858 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 13859 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 13860 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 13861 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 13862 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 13863 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13864 | |
| 13865 | |
| 13866 | /* |
| 13867 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13868 | * Register class Socket |
| 13869 | * |
| 13870 | */ |
| 13871 | |
| 13872 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13873 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13874 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13875 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13876 | lua_pushstring(L, "__index"); |
| 13877 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13878 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13879 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13880 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13881 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13882 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 13883 | hlua_class_function(L, "send", hlua_socket_send); |
| 13884 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 13885 | hlua_class_function(L, "close", hlua_socket_close); |
| 13886 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 13887 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 13888 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 13889 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13890 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13891 | 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] | 13892 | |
| 13893 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13894 | lua_pushstring(L, "__gc"); |
| 13895 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 13896 | 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] | 13897 | |
| 13898 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13899 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13900 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13901 | lua_atpanic(L, hlua_panic_safe); |
| 13902 | |
| 13903 | return L; |
| 13904 | } |
| 13905 | |
| 13906 | void hlua_init(void) { |
| 13907 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13908 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13909 | #ifdef USE_OPENSSL |
| 13910 | struct srv_kw *kw; |
| 13911 | int tmp_error; |
| 13912 | char *error; |
| 13913 | char *args[] = { /* SSL client configuration. */ |
| 13914 | "ssl", |
| 13915 | "verify", |
| 13916 | "none", |
| 13917 | NULL |
| 13918 | }; |
| 13919 | #endif |
| 13920 | |
| 13921 | /* Init post init function list head */ |
| 13922 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 13923 | LIST_INIT(&hlua_init_functions[i]); |
| 13924 | |
| 13925 | /* Init state for common/shared lua parts */ |
| 13926 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13927 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13928 | hlua_states[0] = hlua_init_state(0); |
| 13929 | |
| 13930 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 13931 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13932 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13933 | hlua_states[1] = hlua_init_state(1); |
| 13934 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13935 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 13936 | 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] | 13937 | if (!socket_proxy) { |
| 13938 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 13939 | exit(1); |
| 13940 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13941 | |
| 13942 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13943 | socket_tcp = new_server(socket_proxy); |
| 13944 | if (!socket_tcp) { |
| 13945 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 13946 | exit(1); |
| 13947 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13948 | |
| 13949 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13950 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13951 | socket_ssl = new_server(socket_proxy); |
| 13952 | if (!socket_ssl) { |
| 13953 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 13954 | exit(1); |
| 13955 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13956 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13957 | socket_ssl->use_ssl = 1; |
| 13958 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13959 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13960 | for (i = 0; args[i] != NULL; i++) { |
| 13961 | 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] | 13962 | /* |
| 13963 | * |
| 13964 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 13965 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13966 | * features like client certificates and ssl_verify. |
| 13967 | * |
| 13968 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13969 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13970 | if (tmp_error != 0) { |
| 13971 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 13972 | abort(); /* This must be never arrives because the command line |
| 13973 | not editable by the user. */ |
| 13974 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13975 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13976 | } |
| 13977 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13978 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 13979 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 13980 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13981 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13982 | static void hlua_deinit() |
| 13983 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13984 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13985 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 13986 | |
| 13987 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 13988 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13989 | |
| 13990 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 13991 | if (hlua_states[thr]) |
| 13992 | lua_close(hlua_states[thr]); |
| 13993 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13994 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13995 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13996 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13997 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13998 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13999 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 14000 | |
| 14001 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 14002 | } |
| 14003 | |
| 14004 | REGISTER_POST_DEINIT(hlua_deinit); |
| 14005 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 14006 | static void hlua_register_build_options(void) |
| 14007 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 14008 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 14009 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 14010 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 14011 | hap_register_build_opts(ptr, 1); |
| 14012 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 14013 | |
| 14014 | INITCALL0(STG_REGISTER, hlua_register_build_options); |