Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 36 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 37 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 39 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 40 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 51 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 52 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 53 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 54 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 55 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 56 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 57 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 59 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 60 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 61 | #include <haproxy/xref.h> |
| 62 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 63 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 64 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 65 | * macro is used only for identifying the function that can |
| 66 | * not return because a longjmp is executed. |
| 67 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 68 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 69 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 70 | */ |
| 71 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 72 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 73 | #define MAY_LJMP(func) func |
| 74 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 75 | /* This couple of function executes securely some Lua calls outside of |
| 76 | * the lua runtime environment. Each Lua call can return a longjmp |
| 77 | * if it encounter a memory error. |
| 78 | * |
| 79 | * Lua documentation extract: |
| 80 | * |
| 81 | * If an error happens outside any protected environment, Lua calls |
| 82 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 83 | * exiting the host application. Your panic function can avoid this |
| 84 | * exit by never returning (e.g., doing a long jump to your own |
| 85 | * recovery point outside Lua). |
| 86 | * |
| 87 | * The panic function runs as if it were a message handler (see |
| 88 | * §2.3); in particular, the error message is at the top of the |
| 89 | * stack. However, there is no guarantee about stack space. To push |
| 90 | * anything on the stack, the panic function must first check the |
| 91 | * available space (see §4.2). |
| 92 | * |
| 93 | * We must check all the Lua entry point. This includes: |
| 94 | * - The include/proto/hlua.h exported functions |
| 95 | * - the task wrapper function |
| 96 | * - The action wrapper function |
| 97 | * - The converters wrapper function |
| 98 | * - The sample-fetch wrapper functions |
| 99 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 100 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 101 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 102 | * |
| 103 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 104 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 105 | * because they must be exists in the program stack when the longjmp |
| 106 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 107 | * |
| 108 | * Note that the Lua processing is not really thread safe. It provides |
| 109 | * heavy system which consists to add our own lock function in the Lua |
| 110 | * code and recompile the library. This system will probably not accepted |
| 111 | * by maintainers of various distribs. |
| 112 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 113 | * 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] | 114 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 115 | * of function and a lua_unlock() at the end of the function. So I |
| 116 | * conclude that the Lua thread safe mode just perform a mutex around |
| 117 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 118 | * easier for distro maintainers. |
| 119 | * |
| 120 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 121 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 122 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 124 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 125 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 69c66e3 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 127 | 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] | 128 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 129 | /* This is the chained list of struct hlua_function referenced |
| 130 | * for haproxy action, sample-fetches, converters, cli and |
| 131 | * applet bindings. It is used for a post-initialisation control. |
| 132 | */ |
| 133 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 134 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 135 | /* This variable is used only during initialization to identify the Lua state |
| 136 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 137 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 138 | */ |
| 139 | static int hlua_state_id; |
| 140 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 141 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 142 | static char **per_thread_load = NULL; |
| 143 | |
| 144 | lua_State *hlua_init_state(int thread_id); |
| 145 | |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 146 | /* This function takes the Lua global lock. Keep this function's visibility |
| 147 | * global so that it can appear in stack dumps and performance profiles! |
| 148 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 149 | static inline void lua_take_global_lock() |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 150 | { |
| 151 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 152 | } |
| 153 | |
| 154 | static inline void lua_drop_global_lock() |
| 155 | { |
| 156 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 157 | } |
| 158 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 159 | /* lua lock helpers: only lock when required |
| 160 | * |
| 161 | * state_id == 0: we're operating on the main lua stack (shared between |
| 162 | * os threads), so we need to acquire the main lock |
| 163 | * |
| 164 | * If the thread already owns the lock (_hlua_locked != 0), skip the lock |
| 165 | * attempt. This could happen if we run under protected lua environment. |
| 166 | * Not doing this could result in deadlocks because of nested locking |
| 167 | * attempts from the same thread |
| 168 | */ |
| 169 | static THREAD_LOCAL int _hlua_locked = 0; |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 170 | static inline void hlua_lock(struct hlua *hlua) |
| 171 | { |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 172 | if (hlua->state_id != 0) |
| 173 | return; |
| 174 | if (!_hlua_locked) |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 175 | lua_take_global_lock(); |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 176 | _hlua_locked += 1; |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 177 | } |
| 178 | static inline void hlua_unlock(struct hlua *hlua) |
| 179 | { |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 180 | if (hlua->state_id != 0) |
| 181 | return; |
| 182 | BUG_ON(_hlua_locked <= 0); |
| 183 | _hlua_locked--; |
| 184 | /* drop the lock once the lock count reaches 0 */ |
| 185 | if (!_hlua_locked) |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 186 | lua_drop_global_lock(); |
| 187 | } |
| 188 | |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 189 | /* below is an helper function to retrieve string on on Lua stack at <index> |
| 190 | * in a safe way (function may not LJMP). It can be useful to retrieve errors |
| 191 | * at the top of the stack from an unprotected environment. |
| 192 | * |
| 193 | * The returned string will is only valid as long as the value at <index> is |
| 194 | * not removed from the stack. |
| 195 | * |
| 196 | * It is assumed that the calling function is allowed to manipulate <L> |
| 197 | */ |
| 198 | __LJMP static int _hlua_tostring_safe(lua_State *L) |
| 199 | { |
| 200 | const char **str = lua_touserdata(L, 1); |
| 201 | const char *cur_str = MAY_LJMP(lua_tostring(L, 2)); |
| 202 | |
| 203 | if (cur_str) |
| 204 | *str = cur_str; |
| 205 | return 0; |
| 206 | } |
| 207 | static const char *hlua_tostring_safe(lua_State *L, int index) |
| 208 | { |
| 209 | const char *str = NULL; |
| 210 | |
| 211 | if (!lua_checkstack(L, 4)) |
| 212 | return NULL; |
| 213 | |
| 214 | /* before any stack modification, save the targeted value on the top of |
| 215 | * the stack: this will allow us to use relative index to target it. |
| 216 | */ |
| 217 | lua_pushvalue(L, index); |
| 218 | |
| 219 | /* push our custom _hlua_tostring_safe() function on the stack, then push |
| 220 | * our own string pointer and targeted value (at <index>) as argument |
| 221 | */ |
| 222 | lua_pushcfunction(L, _hlua_tostring_safe); |
| 223 | lua_pushlightuserdata(L, &str); // 1st func argument = string pointer |
| 224 | lua_pushvalue(L, -3); // 2nd func argument = targeted value |
| 225 | |
| 226 | lua_remove(L, -4); // remove <index> copy as we're done using it |
| 227 | |
| 228 | /* call our custom function with proper arguments using pcall() to catch |
| 229 | * exceptions (if any) |
| 230 | */ |
| 231 | switch (lua_pcall(L, 2, 0, 0)) { |
| 232 | case LUA_OK: |
| 233 | break; |
| 234 | default: |
| 235 | /* error was caught */ |
| 236 | return NULL; |
| 237 | } |
| 238 | return str; |
| 239 | } |
| 240 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 241 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 242 | ({ \ |
| 243 | int ret; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 244 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 245 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 246 | lua_atpanic(__L, hlua_panic_safe); \ |
| 247 | ret = 0; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 248 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 249 | } else { \ |
| 250 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 251 | ret = 1; \ |
| 252 | } \ |
| 253 | ret; \ |
| 254 | }) |
| 255 | |
| 256 | /* If we are the last function catching Lua errors, we |
| 257 | * must reset the panic function. |
| 258 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 259 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 260 | do { \ |
| 261 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 262 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 263 | } while(0) |
| 264 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 265 | #define SET_SAFE_LJMP(__HLUA) \ |
| 266 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 267 | |
| 268 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 269 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 270 | |
| 271 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 272 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 273 | |
| 274 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 275 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 277 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 278 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 279 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 280 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 281 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 282 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 283 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 284 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 285 | /* The main Lua execution context. The 0 index is the |
| 286 | * common state shared by all threads. |
| 287 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 288 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 289 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 290 | /* This is the memory pool containing struct lua for applets |
| 291 | * (including cli). |
| 292 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 293 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 294 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 295 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 296 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 297 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 298 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 299 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 300 | #endif |
| 301 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 302 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 303 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 304 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 305 | /* The following variables contains the reference of the different |
| 306 | * Lua classes. These references are useful for identify metadata |
| 307 | * associated with an object. |
| 308 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 309 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 310 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 311 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 312 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 313 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 314 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 315 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 316 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 317 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 318 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 319 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 320 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 321 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 322 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 323 | * because a task may remain alive during all the haproxy execution. |
| 324 | */ |
| 325 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 326 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 327 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 328 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 329 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 330 | * it is used for preventing infinite loops. |
| 331 | * |
| 332 | * I test the scheer with an infinite loop containing one incrementation |
| 333 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 334 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 335 | * to one interrupt each 10 000 instructions. |
| 336 | * |
| 337 | * configured | Number of |
| 338 | * instructions | loops executed |
| 339 | * between two | in milions |
| 340 | * forced yields | |
| 341 | * ---------------+--------------- |
| 342 | * 10 | 160 |
| 343 | * 500 | 670 |
| 344 | * 1000 | 680 |
| 345 | * 5000 | 700 |
| 346 | * 7000 | 700 |
| 347 | * 8000 | 700 |
| 348 | * 9000 | 710 <- ceil |
| 349 | * 10000 | 710 |
| 350 | * 100000 | 710 |
| 351 | * 1000000 | 710 |
| 352 | * |
| 353 | */ |
| 354 | static unsigned int hlua_nb_instruction = 10000; |
| 355 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 356 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 357 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 358 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 359 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 360 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 361 | */ |
| 362 | struct hlua_mem_allocator { |
| 363 | size_t allocated; |
| 364 | size_t limit; |
| 365 | }; |
| 366 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 367 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 368 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 369 | /* These functions converts types between HAProxy internal args or |
| 370 | * sample and LUA types. Another function permits to check if the |
| 371 | * LUA stack contains arguments according with an required ARG_T |
| 372 | * format. |
| 373 | */ |
| 374 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 375 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 376 | __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] | 377 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 378 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 379 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 381 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 382 | __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] | 383 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 384 | struct prepend_path { |
| 385 | struct list l; |
| 386 | char *type; |
| 387 | char *path; |
| 388 | }; |
| 389 | |
| 390 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 391 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 392 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 393 | do { \ |
| 394 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 395 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 396 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 397 | } while (0) |
| 398 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 399 | static inline struct hlua_function *new_hlua_function() |
| 400 | { |
| 401 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 402 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 403 | |
| 404 | fcn = calloc(1, sizeof(*fcn)); |
| 405 | if (!fcn) |
| 406 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 407 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 408 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 409 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 410 | return fcn; |
| 411 | } |
| 412 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 413 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 414 | { |
| 415 | if (!fcn) |
| 416 | return; |
| 417 | if (fcn->name) |
| 418 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 419 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 420 | ha_free(&fcn); |
| 421 | } |
| 422 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 423 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 424 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 425 | { |
| 426 | if (fcn->function_ref[0] == -1) |
| 427 | return tid + 1; |
| 428 | return 0; |
| 429 | } |
| 430 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 431 | /* Used to check an Lua function type in the stack. It creates and |
| 432 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 433 | * error if the argument is not a "function". |
| 434 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 435 | */ |
| 436 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 437 | { |
| 438 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 439 | 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] | 440 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 441 | } |
| 442 | lua_pushvalue(L, argno); |
| 443 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 444 | } |
| 445 | |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 446 | /* Used to check an Lua table type in the stack. It creates and |
| 447 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 448 | * error if the argument is not a "table". |
| 449 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 450 | */ |
| 451 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 452 | { |
| 453 | if (!lua_istable(L, argno)) { |
| 454 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 455 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 456 | } |
| 457 | lua_pushvalue(L, argno); |
| 458 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 459 | } |
| 460 | |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 461 | /* Get a reference to the object that is at the top of the stack |
| 462 | * The referenced object will be popped from the stack |
| 463 | * |
| 464 | * The function returns the reference to the object which must |
| 465 | * be cleared using hlua_unref() when no longer used |
| 466 | */ |
| 467 | __LJMP int hlua_ref(lua_State *L) |
| 468 | { |
| 469 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 470 | } |
| 471 | |
| 472 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 473 | * on <L> stack |
| 474 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 475 | * |
| 476 | * When the reference is no longer used, it should be released by calling |
| 477 | * hlua_unref() |
| 478 | * |
| 479 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 480 | * parent state that the one used to get the reference. |
| 481 | */ |
| 482 | void hlua_pushref(lua_State *L, int ref) |
| 483 | { |
| 484 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 485 | } |
| 486 | |
| 487 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 488 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 489 | * |
| 490 | * This will allow the reference to be reused and the referred object |
| 491 | * to be garbage collected. |
| 492 | * |
| 493 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 494 | * parent state that the one used to get the reference. |
| 495 | */ |
| 496 | void hlua_unref(lua_State *L, int ref) |
| 497 | { |
| 498 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 499 | } |
| 500 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 501 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 502 | { |
| 503 | lua_Debug ar; |
| 504 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 505 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 506 | |
| 507 | while (lua_getstack(L, level++, &ar)) { |
| 508 | |
| 509 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 510 | if (b_data(msg)) |
| 511 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 512 | |
| 513 | /* Fill fields: |
| 514 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 515 | * 'l': fills in the field currentline; |
| 516 | * 'n': fills in the field name and namewhat; |
| 517 | * 't': fills in the field istailcall; |
| 518 | */ |
| 519 | lua_getinfo(L, "Slnt", &ar); |
| 520 | |
| 521 | /* Append code localisation */ |
| 522 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 523 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 524 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 525 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Get function name |
| 529 | * |
| 530 | * if namewhat is no empty, name is defined. |
| 531 | * what contains "Lua" for Lua function, "C" for C function, |
| 532 | * or "main" for main code. |
| 533 | */ |
| 534 | 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] | 535 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 536 | |
| 537 | 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] | 538 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 539 | |
| 540 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 541 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 542 | |
| 543 | else /* nothing left... */ |
| 544 | chunk_appendf(msg, "?"); |
| 545 | |
| 546 | |
| 547 | /* Display tailed call */ |
| 548 | if (ar.istailcall) |
| 549 | chunk_appendf(msg, " ..."); |
| 550 | } |
| 551 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 552 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 556 | /* This function check the number of arguments available in the |
| 557 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 558 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 559 | */ |
| 560 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 561 | { |
| 562 | if (lua_gettop(L) == nb) |
| 563 | return; |
| 564 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 565 | } |
| 566 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 567 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 568 | * and the line number where the error is encountered. |
| 569 | */ |
| 570 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 571 | { |
| 572 | va_list argp; |
| 573 | va_start(argp, fmt); |
| 574 | luaL_where(L, 1); |
| 575 | lua_pushvfstring(L, fmt, argp); |
| 576 | va_end(argp); |
| 577 | lua_concat(L, 2); |
| 578 | return 1; |
| 579 | } |
| 580 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | /* This functions is used with sample fetch and converters. It |
| 582 | * converts the HAProxy configuration argument in a lua stack |
| 583 | * values. |
| 584 | * |
| 585 | * It takes an array of "arg", and each entry of the array is |
| 586 | * converted and pushed in the LUA stack. |
| 587 | */ |
| 588 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 589 | { |
| 590 | switch (arg->type) { |
| 591 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | case ARGT_TIME: |
| 593 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 594 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | break; |
| 596 | |
| 597 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 598 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 599 | break; |
| 600 | |
| 601 | case ARGT_IPV4: |
| 602 | case ARGT_IPV6: |
| 603 | case ARGT_MSK4: |
| 604 | case ARGT_MSK6: |
| 605 | case ARGT_FE: |
| 606 | case ARGT_BE: |
| 607 | case ARGT_TAB: |
| 608 | case ARGT_SRV: |
| 609 | case ARGT_USR: |
| 610 | case ARGT_MAP: |
| 611 | default: |
| 612 | lua_pushnil(L); |
| 613 | break; |
| 614 | } |
| 615 | return 1; |
| 616 | } |
| 617 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 618 | /* 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] | 619 | * 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] | 620 | * with sample fetch wrappers. The input arguments are given to the |
| 621 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | */ |
| 623 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 624 | { |
| 625 | switch (lua_type(L, ud)) { |
| 626 | |
| 627 | case LUA_TNUMBER: |
| 628 | case LUA_TBOOLEAN: |
| 629 | arg->type = ARGT_SINT; |
| 630 | arg->data.sint = lua_tointeger(L, ud); |
| 631 | break; |
| 632 | |
| 633 | case LUA_TSTRING: |
| 634 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 635 | 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] | 636 | /* 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] | 637 | 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] | 638 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case LUA_TUSERDATA: |
| 642 | case LUA_TNIL: |
| 643 | case LUA_TTABLE: |
| 644 | case LUA_TFUNCTION: |
| 645 | case LUA_TTHREAD: |
| 646 | case LUA_TLIGHTUSERDATA: |
| 647 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 648 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | /* the following functions are used to convert a struct sample |
| 655 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 656 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 657 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 658 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 659 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 660 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 661 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 663 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | case SMP_T_BIN: |
| 667 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 668 | 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] | 669 | break; |
| 670 | |
| 671 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 672 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 673 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 674 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 675 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 676 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 677 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 678 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 679 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 680 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 681 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 682 | 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] | 683 | break; |
| 684 | default: |
| 685 | lua_pushnil(L); |
| 686 | break; |
| 687 | } |
| 688 | break; |
| 689 | |
| 690 | case SMP_T_IPV4: |
| 691 | case SMP_T_IPV6: |
| 692 | 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] | 693 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 694 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 695 | 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] | 696 | else |
| 697 | lua_pushnil(L); |
| 698 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 699 | default: |
| 700 | lua_pushnil(L); |
| 701 | break; |
| 702 | } |
| 703 | return 1; |
| 704 | } |
| 705 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 706 | /* the following functions are used to convert a struct sample |
| 707 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 708 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 709 | */ |
| 710 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 711 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 712 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 713 | |
| 714 | case SMP_T_BIN: |
| 715 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 716 | 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] | 717 | break; |
| 718 | |
| 719 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 720 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 721 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 722 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 723 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 724 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 725 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 726 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 727 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 728 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 729 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 730 | 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] | 731 | break; |
| 732 | default: |
| 733 | lua_pushstring(L, ""); |
| 734 | break; |
| 735 | } |
| 736 | break; |
| 737 | |
| 738 | case SMP_T_SINT: |
| 739 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 740 | case SMP_T_IPV4: |
| 741 | case SMP_T_IPV6: |
| 742 | 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] | 743 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 744 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 745 | 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] | 746 | else |
| 747 | lua_pushstring(L, ""); |
| 748 | break; |
| 749 | default: |
| 750 | lua_pushstring(L, ""); |
| 751 | break; |
| 752 | } |
| 753 | return 1; |
| 754 | } |
| 755 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 756 | /* the following functions are used to convert an Lua type in a |
| 757 | * struct sample. This is useful to provide data from a converter |
| 758 | * to the LUA code. |
| 759 | */ |
| 760 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 761 | { |
| 762 | switch (lua_type(L, ud)) { |
| 763 | |
| 764 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 765 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 766 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 767 | break; |
| 768 | |
| 769 | |
| 770 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 771 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 772 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 773 | break; |
| 774 | |
| 775 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 776 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 777 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 778 | 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] | 779 | /* 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] | 780 | 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] | 781 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 782 | break; |
| 783 | |
| 784 | case LUA_TUSERDATA: |
| 785 | case LUA_TNIL: |
| 786 | case LUA_TTABLE: |
| 787 | case LUA_TFUNCTION: |
| 788 | case LUA_TTHREAD: |
| 789 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 790 | case LUA_TNONE: |
| 791 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 792 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 793 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | return 1; |
| 797 | } |
| 798 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 799 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 800 | * 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] | 801 | * 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] | 802 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 803 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 804 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 805 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 806 | __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] | 807 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 808 | { |
| 809 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 810 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 811 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 812 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 813 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 814 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 815 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 816 | |
| 817 | idx = 0; |
| 818 | min_arg = ARGM(mask); |
| 819 | mask >>= ARGM_BITS; |
| 820 | |
| 821 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 823 | |
| 824 | /* Check oversize. */ |
| 825 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 826 | msg = "Malformed argument mask"; |
| 827 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* Check for mandatory arguments. */ |
| 831 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 832 | if (idx < min_arg) { |
| 833 | |
| 834 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 835 | if (idx > 0) { |
| 836 | msg = "Mandatory argument expected"; |
| 837 | goto error; |
| 838 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 839 | |
| 840 | /* If first argument have a certain type, some default values |
| 841 | * may be used. See the function smp_resolve_args(). |
| 842 | */ |
| 843 | switch (mask & ARGT_MASK) { |
| 844 | |
| 845 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 846 | if (!(p->cap & PR_CAP_FE)) { |
| 847 | msg = "Mandatory argument expected"; |
| 848 | goto error; |
| 849 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 850 | argp[idx].data.prx = p; |
| 851 | argp[idx].type = ARGT_FE; |
| 852 | argp[idx+1].type = ARGT_STOP; |
| 853 | break; |
| 854 | |
| 855 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 856 | if (!(p->cap & PR_CAP_BE)) { |
| 857 | msg = "Mandatory argument expected"; |
| 858 | goto error; |
| 859 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 860 | argp[idx].data.prx = p; |
| 861 | argp[idx].type = ARGT_BE; |
| 862 | argp[idx+1].type = ARGT_STOP; |
| 863 | break; |
| 864 | |
| 865 | case ARGT_TAB: |
Olivier Houchard | a265827 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 866 | if (!p->table) { |
| 867 | msg = "Mandatory argument expected"; |
| 868 | goto error; |
| 869 | } |
| 870 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 871 | argp[idx].type = ARGT_TAB; |
| 872 | argp[idx+1].type = ARGT_STOP; |
| 873 | break; |
| 874 | |
| 875 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 876 | msg = "Mandatory argument expected"; |
| 877 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 878 | break; |
| 879 | } |
| 880 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 881 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 884 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 885 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 886 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 887 | msg = "Last argument expected"; |
| 888 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 892 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 893 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 894 | } |
| 895 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 896 | /* Convert some argument types. All string in argp[] are for not |
| 897 | * duplicated yet. |
| 898 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 899 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 900 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 901 | if (argp[idx].type != ARGT_SINT) { |
| 902 | msg = "integer expected"; |
| 903 | goto error; |
| 904 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 905 | argp[idx].type = ARGT_SINT; |
| 906 | break; |
| 907 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 908 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 909 | if (argp[idx].type != ARGT_SINT) { |
| 910 | msg = "integer expected"; |
| 911 | goto error; |
| 912 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 913 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 914 | break; |
| 915 | |
| 916 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 917 | if (argp[idx].type != ARGT_SINT) { |
| 918 | msg = "integer expected"; |
| 919 | goto error; |
| 920 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 921 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 922 | break; |
| 923 | |
| 924 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 925 | if (argp[idx].type != ARGT_STR) { |
| 926 | msg = "string expected"; |
| 927 | goto error; |
| 928 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 929 | 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] | 930 | if (!argp[idx].data.prx) { |
| 931 | msg = "frontend doesn't exist"; |
| 932 | goto error; |
| 933 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 934 | argp[idx].type = ARGT_FE; |
| 935 | break; |
| 936 | |
| 937 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 938 | if (argp[idx].type != ARGT_STR) { |
| 939 | msg = "string expected"; |
| 940 | goto error; |
| 941 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 942 | 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] | 943 | if (!argp[idx].data.prx) { |
| 944 | msg = "backend doesn't exist"; |
| 945 | goto error; |
| 946 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | argp[idx].type = ARGT_BE; |
| 948 | break; |
| 949 | |
| 950 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 951 | if (argp[idx].type != ARGT_STR) { |
| 952 | msg = "string expected"; |
| 953 | goto error; |
| 954 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 955 | 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] | 956 | if (!argp[idx].data.t) { |
| 957 | msg = "table doesn't exist"; |
| 958 | goto error; |
| 959 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 960 | argp[idx].type = ARGT_TAB; |
| 961 | break; |
| 962 | |
| 963 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | if (argp[idx].type != ARGT_STR) { |
| 965 | msg = "string expected"; |
| 966 | goto error; |
| 967 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 969 | if (sname) { |
| 970 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 971 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 972 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | if (!px) { |
| 974 | msg = "backend doesn't exist"; |
| 975 | goto error; |
| 976 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | } |
| 978 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 979 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 980 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 981 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 982 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 983 | if (!argp[idx].data.srv) { |
| 984 | msg = "server doesn't exist"; |
| 985 | goto error; |
| 986 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 987 | argp[idx].type = ARGT_SRV; |
| 988 | break; |
| 989 | |
| 990 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 991 | if (argp[idx].type != ARGT_STR) { |
| 992 | msg = "string expected"; |
| 993 | goto error; |
| 994 | } |
| 995 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 996 | msg = "invalid IPv4 address"; |
| 997 | goto error; |
| 998 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 999 | argp[idx].type = ARGT_IPV4; |
| 1000 | break; |
| 1001 | |
| 1002 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1003 | if (argp[idx].type == ARGT_SINT) |
| 1004 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1005 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1006 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1007 | msg = "invalid IPv4 mask"; |
| 1008 | goto error; |
| 1009 | } |
| 1010 | } |
| 1011 | else { |
| 1012 | msg = "integer or string expected"; |
| 1013 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1014 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1015 | argp[idx].type = ARGT_MSK4; |
| 1016 | break; |
| 1017 | |
| 1018 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1019 | if (argp[idx].type != ARGT_STR) { |
| 1020 | msg = "string expected"; |
| 1021 | goto error; |
| 1022 | } |
| 1023 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1024 | msg = "invalid IPv6 address"; |
| 1025 | goto error; |
| 1026 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1027 | argp[idx].type = ARGT_IPV6; |
| 1028 | break; |
| 1029 | |
| 1030 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1031 | if (argp[idx].type == ARGT_SINT) |
| 1032 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1033 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1034 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1035 | msg = "invalid IPv6 mask"; |
| 1036 | goto error; |
| 1037 | } |
| 1038 | } |
| 1039 | else { |
| 1040 | msg = "integer or string expected"; |
| 1041 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1042 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1043 | argp[idx].type = ARGT_MSK6; |
| 1044 | break; |
| 1045 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1046 | case ARGT_REG: |
| 1047 | if (argp[idx].type != ARGT_STR) { |
| 1048 | msg = "string expected"; |
| 1049 | goto error; |
| 1050 | } |
| 1051 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1052 | if (!reg) { |
| 1053 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1054 | argp[idx].data.str.area, err); |
| 1055 | free(err); |
| 1056 | goto error; |
| 1057 | } |
| 1058 | argp[idx].type = ARGT_REG; |
| 1059 | argp[idx].data.reg = reg; |
| 1060 | break; |
| 1061 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1062 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1063 | if (argp[idx].type != ARGT_STR) { |
| 1064 | msg = "string expected"; |
| 1065 | goto error; |
| 1066 | } |
| 1067 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1068 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1069 | ul = p->uri_auth->userlist; |
| 1070 | else |
| 1071 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1072 | |
| 1073 | if (!ul) { |
| 1074 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | argp[idx].type = ARGT_USR; |
| 1078 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1079 | break; |
| 1080 | |
| 1081 | case ARGT_STR: |
| 1082 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1083 | msg = "unable to duplicate string arg"; |
| 1084 | goto error; |
| 1085 | } |
| 1086 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1087 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1088 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1089 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1090 | msg = "type not yet supported"; |
| 1091 | goto error; |
| 1092 | break; |
| 1093 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Check for type of argument. */ |
| 1097 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1098 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1099 | arg_type_names[(mask & ARGT_MASK)], |
| 1100 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1101 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* Next argument. */ |
| 1105 | mask >>= ARGT_BITS; |
| 1106 | idx++; |
| 1107 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1108 | return 0; |
| 1109 | |
| 1110 | error: |
Olivier Houchard | da25dac | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1111 | argp[idx].type = ARGT_STOP; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1112 | for (i = 0; i < idx; i++) { |
| 1113 | if (argp[i].type == ARGT_STR) |
| 1114 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1115 | else if (argp[i].type == ARGT_REG) |
| 1116 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1117 | } |
| 1118 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1119 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1123 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1124 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | * |
| 1126 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1127 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1128 | */ |
| 1129 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1130 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1131 | struct hlua **hlua = lua_getextraspace(L); |
| 1132 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1133 | } |
| 1134 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1135 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1136 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1137 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1140 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1141 | * and on the default syslog server. |
| 1142 | */ |
| 1143 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1144 | { |
| 1145 | struct tm tm; |
| 1146 | char *p; |
| 1147 | |
| 1148 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1149 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1150 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1151 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1152 | /* Break the message if exceed the buffer size. */ |
| 1153 | *(p-4) = ' '; |
| 1154 | *(p-3) = '.'; |
| 1155 | *(p-2) = '.'; |
| 1156 | *(p-1) = '.'; |
| 1157 | break; |
| 1158 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1159 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1160 | *p = *msg; |
| 1161 | else |
| 1162 | *p = '.'; |
| 1163 | } |
| 1164 | *p = '\0'; |
| 1165 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1166 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1167 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1168 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1169 | return; |
| 1170 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1171 | get_localtime(date.tv_sec, &tm); |
| 1172 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1173 | 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] | 1174 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1175 | fflush(stderr); |
| 1176 | } |
| 1177 | } |
| 1178 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1179 | /* This function just ensure that the yield will be always |
| 1180 | * returned with a timeout and permit to set some flags |
| 1181 | */ |
Aurelien DARRAGON | f5d3b31 | 2023-07-13 10:18:04 +0200 | [diff] [blame] | 1182 | __LJMP void hlua_yieldk(lua_State *L, int nresults, lua_KContext ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1183 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1184 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1185 | struct hlua *hlua; |
| 1186 | |
| 1187 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1188 | hlua = hlua_gethlua(L); |
| 1189 | if (!hlua) { |
| 1190 | return; |
| 1191 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1192 | |
| 1193 | /* Set the wake timeout. If timeout is required, we set |
| 1194 | * the expiration time. |
| 1195 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1196 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1197 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1198 | hlua->flags |= flags; |
| 1199 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1200 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1201 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1204 | /* This function initialises the Lua environment stored in the stream. |
| 1205 | * 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] | 1206 | * 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] | 1207 | * |
| 1208 | * This function is particular. it initialises a new Lua thread. If the |
| 1209 | * initialisation fails (example: out of memory error), the lua function |
| 1210 | * throws an error (longjmp). |
| 1211 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1212 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1213 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1214 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1215 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1217 | 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] | 1218 | { |
| 1219 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1220 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1221 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1222 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1223 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1224 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1225 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1226 | lua->Tref = LUA_REFNIL; |
| 1227 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1228 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1229 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1230 | if (!lua->T) { |
| 1231 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1232 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1233 | return 0; |
| 1234 | } |
| 1235 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1236 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1237 | lua->task = task; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1238 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1239 | return 1; |
| 1240 | } |
| 1241 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1242 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1243 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | d5c8a10 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1244 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1245 | */ |
| 1246 | void hlua_ctx_destroy(struct hlua *lua) |
| 1247 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1248 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1249 | return; |
| 1250 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1251 | if (!lua->T) |
| 1252 | goto end; |
| 1253 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1254 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1255 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1256 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1257 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1258 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1260 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1261 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1262 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1263 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1264 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1265 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1266 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1267 | * without error, we run the GC on the thread pointer. Its freed all |
| 1268 | * the unused memory. |
| 1269 | * If the thread is finnish with an error or is currently yielded, |
| 1270 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1271 | * so e run the GC on the main thread. |
| 1272 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1273 | * the garbage collection. |
| 1274 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1275 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1276 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1277 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1278 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1279 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1280 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1281 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1282 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1283 | |
| 1284 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1285 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /* This function is used to restore the Lua context when a coroutine |
| 1289 | * fails. This function copy the common memory between old coroutine |
| 1290 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1291 | * replaced by the new coroutine. |
| 1292 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1293 | * as string error message and it is copied in the new stack. |
| 1294 | */ |
| 1295 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1296 | { |
| 1297 | lua_State *T; |
| 1298 | int new_ref; |
| 1299 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1300 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1301 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1302 | if (!T) |
| 1303 | return 0; |
| 1304 | |
| 1305 | /* Copy last error message. */ |
| 1306 | if (keep_msg) |
| 1307 | lua_xmove(lua->T, T, 1); |
| 1308 | |
| 1309 | /* Copy data between the coroutines. */ |
| 1310 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1311 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1312 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1313 | |
| 1314 | /* Destroy old data. */ |
| 1315 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1316 | |
| 1317 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1318 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1319 | |
| 1320 | /* Fill the struct with the new coroutine values. */ |
| 1321 | lua->Mref = new_ref; |
| 1322 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1323 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1324 | |
| 1325 | /* Set context. */ |
| 1326 | hlua_sethlua(lua); |
| 1327 | |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1331 | /* Helper function to get the lua ctx for a given stream and state_id */ |
| 1332 | static inline struct hlua *hlua_stream_ctx_get(struct stream *s, int state_id) |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1333 | { |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1334 | /* state_id == 0 -> global runtime ctx |
| 1335 | * state_id != 0 -> per-thread runtime ctx |
| 1336 | */ |
| 1337 | return s->hlua[!!state_id]; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1340 | /* Helper function to prepare the lua ctx for a given stream and state id |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1341 | * |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1342 | * It uses the global or per-thread ctx depending on the expected |
| 1343 | * <state_id>. |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1344 | * |
| 1345 | * Returns hlua ctx on success and NULL on failure |
| 1346 | */ |
| 1347 | static struct hlua *hlua_stream_ctx_prepare(struct stream *s, int state_id) |
| 1348 | { |
| 1349 | /* In the execution wrappers linked with a stream, the |
| 1350 | * Lua context can be not initialized. This behavior |
| 1351 | * permits to save performances because a systematic |
| 1352 | * Lua initialization cause 5% performances loss. |
| 1353 | */ |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1354 | if (!s->hlua[!!state_id]) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1355 | struct hlua *hlua; |
| 1356 | |
| 1357 | hlua = pool_alloc(pool_head_hlua); |
| 1358 | if (!hlua) |
| 1359 | return NULL; |
| 1360 | HLUA_INIT(hlua); |
| 1361 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 1362 | pool_free(pool_head_hlua, hlua); |
| 1363 | return NULL; |
| 1364 | } |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1365 | s->hlua[!!state_id] = hlua; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1366 | } |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1367 | return s->hlua[!!state_id]; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1370 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1371 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1372 | struct hlua *hlua; |
| 1373 | |
| 1374 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1375 | hlua = hlua_gethlua(L); |
| 1376 | if (!hlua) |
| 1377 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1378 | |
| 1379 | /* Lua cannot yield when its returning from a function, |
| 1380 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1381 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1382 | */ |
| 1383 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1384 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| 1388 | /* restore the interrupt condition. */ |
| 1389 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1390 | |
| 1391 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1392 | * If the state is not yieldable, trying yield causes an error. |
| 1393 | */ |
| 1394 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1395 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1396 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1397 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1398 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1399 | hlua->run_time += now_ms - hlua->start_time; |
| 1400 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1401 | lua_pushfstring(L, "execution timeout"); |
| 1402 | WILL_LJMP(lua_error(L)); |
| 1403 | } |
| 1404 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1405 | /* Update the start time. */ |
| 1406 | hlua->start_time = now_ms; |
| 1407 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1408 | /* Try to interrupt the process at the end of the current |
| 1409 | * unyieldable function. |
| 1410 | */ |
| 1411 | 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] | 1412 | } |
| 1413 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1414 | /* This function start or resumes the Lua stack execution. If the flag |
| 1415 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1416 | * The function return an error. |
| 1417 | * |
| 1418 | * The function can returns 4 values: |
| 1419 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1420 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1421 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1422 | * - 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] | 1423 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1424 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1425 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1426 | * 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] | 1427 | * LUA code. |
| 1428 | */ |
| 1429 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1430 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1431 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1432 | int nres; |
| 1433 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1434 | int ret; |
| 1435 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1436 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1437 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1438 | /* Initialise run time counter. */ |
| 1439 | if (!HLUA_IS_RUNNING(lua)) |
| 1440 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1441 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1442 | /* Lock the whole Lua execution. This lock must be before the |
| 1443 | * label "resume_execution". |
| 1444 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1445 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1446 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1447 | resume_execution: |
| 1448 | |
| 1449 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1450 | * instructions. it is used for preventing infinite loops. |
| 1451 | */ |
| 1452 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1453 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1454 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1455 | HLUA_SET_RUN(lua); |
| 1456 | HLUA_CLR_CTRLYIELD(lua); |
| 1457 | HLUA_CLR_WAKERESWR(lua); |
| 1458 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1459 | HLUA_CLR_NOYIELD(lua); |
| 1460 | if (!yield_allowed) |
| 1461 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1462 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1463 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1464 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1465 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1466 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1467 | HLUA_SET_BUSY(lua); |
| 1468 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1469 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1470 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1471 | 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] | 1472 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1473 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1474 | #endif |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1475 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1476 | HLUA_CLR_BUSY(lua); |
| 1477 | |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1478 | /* reset nargs because those possibly passed to the lua_resume() call |
| 1479 | * were already consumed, and since we may call lua_resume() again |
| 1480 | * after a successful yield, we don't want to pass stale nargs hint |
| 1481 | * to the Lua API. As such, nargs should be set explicitly before each |
| 1482 | * lua_resume() (or hlua_ctx_resume()) invocation if needed. |
| 1483 | */ |
| 1484 | lua->nargs = 0; |
| 1485 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1486 | switch (ret) { |
| 1487 | |
| 1488 | case LUA_OK: |
| 1489 | ret = HLUA_E_OK; |
| 1490 | break; |
| 1491 | |
| 1492 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1493 | /* Check if the execution timeout is expired. It it is the case, we |
| 1494 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1495 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1496 | tv_update_date(0, 1); |
| 1497 | lua->run_time += now_ms - lua->start_time; |
| 1498 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1499 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1500 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1501 | break; |
| 1502 | } |
| 1503 | /* Process the forced yield. if the general yield is not allowed or |
| 1504 | * if no task were associated this the current Lua execution |
| 1505 | * coroutine, we resume the execution. Else we want to return in the |
| 1506 | * scheduler and we want to be waked up again, to continue the |
| 1507 | * current Lua execution. So we schedule our own task. |
| 1508 | */ |
| 1509 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1510 | if (!yield_allowed || !lua->task) |
| 1511 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1512 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1513 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | if (!yield_allowed) { |
| 1515 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1516 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1517 | break; |
| 1518 | } |
| 1519 | ret = HLUA_E_AGAIN; |
| 1520 | break; |
| 1521 | |
| 1522 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1523 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1524 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1525 | * because the errors ares the only one mean to return immediately |
| 1526 | * from and lua execution. |
| 1527 | */ |
| 1528 | if (lua->flags & HLUA_EXIT) { |
| 1529 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1530 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1531 | break; |
| 1532 | } |
| 1533 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1534 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1535 | if (!lua_checkstack(lua->T, 1)) { |
| 1536 | ret = HLUA_E_ERR; |
| 1537 | break; |
| 1538 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1539 | msg = hlua_tostring_safe(lua->T, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1540 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1541 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1542 | 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] | 1543 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1544 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1547 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1548 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | ret = HLUA_E_ERRMSG; |
| 1550 | break; |
| 1551 | |
| 1552 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1553 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1554 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1555 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1556 | break; |
| 1557 | |
| 1558 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1559 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1560 | if (!lua_checkstack(lua->T, 1)) { |
| 1561 | ret = HLUA_E_ERR; |
| 1562 | break; |
| 1563 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1564 | msg = hlua_tostring_safe(lua->T, -1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1565 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1566 | 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] | 1567 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1568 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1569 | |
| 1570 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1571 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1572 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1573 | ret = HLUA_E_ERRMSG; |
| 1574 | break; |
| 1575 | |
| 1576 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1577 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1578 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1579 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | switch (ret) { |
| 1584 | case HLUA_E_AGAIN: |
| 1585 | break; |
| 1586 | |
| 1587 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1588 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1589 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1590 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1591 | break; |
| 1592 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1593 | case HLUA_E_ETMOUT: |
| 1594 | case HLUA_E_NOMEM: |
| 1595 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1596 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1597 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1598 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1599 | hlua_ctx_renew(lua, 0); |
| 1600 | break; |
| 1601 | |
| 1602 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1603 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1604 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1605 | break; |
| 1606 | } |
| 1607 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1608 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1609 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1610 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1611 | return ret; |
| 1612 | } |
| 1613 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1614 | /* This function exit the current code. */ |
| 1615 | __LJMP static int hlua_done(lua_State *L) |
| 1616 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1617 | struct hlua *hlua; |
| 1618 | |
| 1619 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1620 | hlua = hlua_gethlua(L); |
| 1621 | if (!hlua) |
| 1622 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1623 | |
| 1624 | hlua->flags |= HLUA_EXIT; |
| 1625 | WILL_LJMP(lua_error(L)); |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1630 | /* This function is an LUA binding. It provides a function |
| 1631 | * for deleting ACL from a referenced ACL file. |
| 1632 | */ |
| 1633 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1634 | { |
| 1635 | const char *name; |
| 1636 | const char *key; |
| 1637 | struct pat_ref *ref; |
| 1638 | |
| 1639 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1640 | |
| 1641 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1642 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1643 | |
| 1644 | ref = pat_ref_lookup(name); |
| 1645 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1646 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1647 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1648 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1649 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1650 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* This function is an LUA binding. It provides a function |
| 1655 | * for deleting map entry from a referenced map file. |
| 1656 | */ |
| 1657 | static int hlua_del_map(lua_State *L) |
| 1658 | { |
| 1659 | const char *name; |
| 1660 | const char *key; |
| 1661 | struct pat_ref *ref; |
| 1662 | |
| 1663 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1664 | |
| 1665 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1666 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1667 | |
| 1668 | ref = pat_ref_lookup(name); |
| 1669 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1670 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1671 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1672 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1673 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1674 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | /* This function is an LUA binding. It provides a function |
| 1679 | * for adding ACL pattern from a referenced ACL file. |
| 1680 | */ |
| 1681 | static int hlua_add_acl(lua_State *L) |
| 1682 | { |
| 1683 | const char *name; |
| 1684 | const char *key; |
| 1685 | struct pat_ref *ref; |
| 1686 | |
| 1687 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1688 | |
| 1689 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1690 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1691 | |
| 1692 | ref = pat_ref_lookup(name); |
| 1693 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1694 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1695 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1696 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1697 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1698 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1699 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | /* This function is an LUA binding. It provides a function |
| 1704 | * for setting map pattern and sample from a referenced map |
| 1705 | * file. |
| 1706 | */ |
| 1707 | static int hlua_set_map(lua_State *L) |
| 1708 | { |
| 1709 | const char *name; |
| 1710 | const char *key; |
| 1711 | const char *value; |
| 1712 | struct pat_ref *ref; |
| 1713 | |
| 1714 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1715 | |
| 1716 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1717 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1718 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1719 | |
| 1720 | ref = pat_ref_lookup(name); |
| 1721 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1722 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1723 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1724 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1725 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1726 | pat_ref_set(ref, key, value, NULL); |
| 1727 | else |
| 1728 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1729 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1730 | return 0; |
| 1731 | } |
| 1732 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1733 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1734 | * 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] | 1735 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1736 | * the name of the object (_G[<name>] = <metable> ). |
| 1737 | * |
| 1738 | * A metable is a table that modify the standard behavior of a standard |
| 1739 | * access to the associated data. The entries of this new metatable are |
| 1740 | * defined as is: |
| 1741 | * |
| 1742 | * http://lua-users.org/wiki/MetatableEvents |
| 1743 | * |
| 1744 | * __index |
| 1745 | * |
| 1746 | * we access an absent field in a table, the result is nil. This is |
| 1747 | * true, but it is not the whole truth. Actually, such access triggers |
| 1748 | * the interpreter to look for an __index metamethod: If there is no |
| 1749 | * such method, as usually happens, then the access results in nil; |
| 1750 | * otherwise, the metamethod will provide the result. |
| 1751 | * |
| 1752 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1753 | * the key does not appear in the table, but the metatable has an __index |
| 1754 | * property: |
| 1755 | * |
| 1756 | * - if the value is a function, the function is called, passing in the |
| 1757 | * table and the key; the return value of that function is returned as |
| 1758 | * the result. |
| 1759 | * |
| 1760 | * - if the value is another table, the value of the key in that table is |
| 1761 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1762 | * table's metatable has an __index property, then it continues on up) |
| 1763 | * |
| 1764 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1765 | * |
| 1766 | * http://www.lua.org/pil/13.4.1.html |
| 1767 | * |
| 1768 | * __newindex |
| 1769 | * |
| 1770 | * Like __index, but control property assignment. |
| 1771 | * |
| 1772 | * __mode - Control weak references. A string value with one or both |
| 1773 | * of the characters 'k' and 'v' which specifies that the the |
| 1774 | * keys and/or values in the table are weak references. |
| 1775 | * |
| 1776 | * __call - Treat a table like a function. When a table is followed by |
| 1777 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1778 | * a __call key pointing to a function, that function is invoked |
| 1779 | * (passing any specified arguments) and the return value is |
| 1780 | * returned. |
| 1781 | * |
| 1782 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1783 | * called, if the metatable for myTable has a __metatable |
| 1784 | * key, the value of that key is returned instead of the |
| 1785 | * actual metatable. |
| 1786 | * |
| 1787 | * __tostring - Control string representation. When the builtin |
| 1788 | * "tostring( myTable )" function is called, if the metatable |
| 1789 | * for myTable has a __tostring property set to a function, |
| 1790 | * that function is invoked (passing myTable to it) and the |
| 1791 | * return value is used as the string representation. |
| 1792 | * |
| 1793 | * __len - Control table length. When the table length is requested using |
| 1794 | * the length operator ( '#' ), if the metatable for myTable has |
| 1795 | * a __len key pointing to a function, that function is invoked |
| 1796 | * (passing myTable to it) and the return value used as the value |
| 1797 | * of "#myTable". |
| 1798 | * |
| 1799 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1800 | * collected, if the metatable has a __gc field pointing to a |
| 1801 | * function, that function is first invoked, passing the userdata |
| 1802 | * to it. The __gc metamethod is not called for tables. |
| 1803 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1804 | * |
| 1805 | * Special metamethods for redefining standard operators: |
| 1806 | * http://www.lua.org/pil/13.1.html |
| 1807 | * |
| 1808 | * __add "+" |
| 1809 | * __sub "-" |
| 1810 | * __mul "*" |
| 1811 | * __div "/" |
| 1812 | * __unm "!" |
| 1813 | * __pow "^" |
| 1814 | * __concat ".." |
| 1815 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1816 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1817 | * http://www.lua.org/pil/13.2.html |
| 1818 | * |
| 1819 | * __eq "==" |
| 1820 | * __lt "<" |
| 1821 | * __le "<=" |
| 1822 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | |
| 1824 | /* |
| 1825 | * |
| 1826 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1827 | * Class Map |
| 1828 | * |
| 1829 | * |
| 1830 | */ |
| 1831 | |
| 1832 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1833 | * a class session, otherwise it throws an error. |
| 1834 | */ |
| 1835 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1836 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1837 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* This function is the map constructor. It don't need |
| 1841 | * the class Map object. It creates and return a new Map |
| 1842 | * object. It must be called only during "body" or "init" |
| 1843 | * context because it process some filesystem accesses. |
| 1844 | */ |
| 1845 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1846 | { |
| 1847 | const char *fn; |
| 1848 | int match = PAT_MATCH_STR; |
| 1849 | struct sample_conv conv; |
| 1850 | const char *file = ""; |
| 1851 | int line = 0; |
| 1852 | lua_Debug ar; |
| 1853 | char *err = NULL; |
| 1854 | struct arg args[2]; |
| 1855 | |
| 1856 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1857 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1858 | |
| 1859 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1860 | |
| 1861 | if (lua_gettop(L) >= 2) { |
| 1862 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1863 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1864 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1865 | } |
| 1866 | |
| 1867 | /* Get Lua filename and line number. */ |
| 1868 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1869 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1870 | if (ar.currentline > 0) { /* is there info? */ |
| 1871 | file = ar.short_src; |
| 1872 | line = ar.currentline; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | /* fill fake sample_conv struct. */ |
| 1877 | conv.kw = ""; /* unused. */ |
| 1878 | conv.process = NULL; /* unused. */ |
| 1879 | conv.arg_mask = 0; /* unused. */ |
| 1880 | conv.val_args = NULL; /* unused. */ |
| 1881 | conv.out_type = SMP_T_STR; |
| 1882 | conv.private = (void *)(long)match; |
| 1883 | switch (match) { |
| 1884 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1885 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1886 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1887 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1888 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1889 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1890 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1891 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1892 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1893 | default: |
| 1894 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1895 | } |
| 1896 | |
| 1897 | /* fill fake args. */ |
| 1898 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1899 | args[0].data.str.area = strdup(fn); |
| 1900 | args[0].data.str.data = strlen(fn); |
| 1901 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1902 | args[1].type = ARGT_STOP; |
| 1903 | |
| 1904 | /* load the map. */ |
| 1905 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1906 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1907 | * free the err variable. |
| 1908 | */ |
Aurelien DARRAGON | ca0cdfe | 2024-06-04 12:48:45 +0200 | [diff] [blame^] | 1909 | hlua_pusherror(L, "'new': %s.", err); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1910 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1911 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1912 | WILL_LJMP(lua_error(L)); |
| 1913 | } |
| 1914 | |
| 1915 | /* create the lua object. */ |
| 1916 | lua_newtable(L); |
| 1917 | lua_pushlightuserdata(L, args[0].data.map); |
| 1918 | lua_rawseti(L, -2, 0); |
| 1919 | |
| 1920 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1921 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1922 | lua_setmetatable(L, -2); |
| 1923 | |
| 1924 | |
| 1925 | return 1; |
| 1926 | } |
| 1927 | |
| 1928 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1929 | { |
| 1930 | struct map_descriptor *desc; |
| 1931 | struct pattern *pat; |
| 1932 | struct sample smp; |
| 1933 | |
| 1934 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1935 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1936 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1937 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1938 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1939 | } |
| 1940 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1941 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1942 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1943 | 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] | 1944 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1948 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1949 | if (str) |
| 1950 | lua_pushstring(L, ""); |
| 1951 | else |
| 1952 | lua_pushnil(L); |
| 1953 | return 1; |
| 1954 | } |
| 1955 | |
| 1956 | /* 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] | 1957 | 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] | 1958 | return 1; |
| 1959 | } |
| 1960 | |
| 1961 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1962 | { |
| 1963 | return _hlua_map_lookup(L, 0); |
| 1964 | } |
| 1965 | |
| 1966 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1967 | { |
| 1968 | return _hlua_map_lookup(L, 1); |
| 1969 | } |
| 1970 | |
| 1971 | /* |
| 1972 | * |
| 1973 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | * Class Socket |
| 1975 | * |
| 1976 | * |
| 1977 | */ |
| 1978 | |
| 1979 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1980 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1981 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* 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] | 1985 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | * received. |
| 1987 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1988 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1990 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1992 | if (appctx->ctx.hlua_cosocket.die) { |
| 1993 | si_shutw(si); |
| 1994 | si_shutr(si); |
| 1995 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1996 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1997 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1998 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1999 | } |
| 2000 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2001 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2002 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2003 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2004 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2005 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2006 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2007 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2008 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2009 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2010 | * to be notified whenever the connection completes. |
| 2011 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2012 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2013 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 2014 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2015 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2016 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2017 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | |
| 2019 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2020 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2022 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 2023 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2024 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | |
| 2026 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2027 | if (!channel_is_empty(si_oc(si))) { |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2028 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2029 | si_cant_get(si); |
| 2030 | } |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2031 | |
| 2032 | /* Some data were injected in the buffer, notify the stream |
| 2033 | * interface. |
| 2034 | */ |
| 2035 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 2036 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2039 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2040 | */ |
| 2041 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2042 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2045 | /* This function is called when the "struct stream" is destroyed. |
| 2046 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | * Wake all the pending signals. |
| 2048 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2049 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2051 | struct xref *peer; |
| 2052 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2053 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2054 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 2055 | if (peer) |
| 2056 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | |
| 2058 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2059 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 2060 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2064 | * uses this object. If the stream does not exists, just quit. |
| 2065 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | * pending signal can rest in the read and write lists. destroy |
| 2067 | * it. |
| 2068 | */ |
| 2069 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2070 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2071 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2073 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2075 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2076 | |
| 2077 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2079 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2081 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2083 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2084 | appctx->ctx.hlua_cosocket.die = 1; |
| 2085 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2087 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2088 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2093 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2095 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2097 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2099 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2100 | struct hlua *hlua; |
| 2101 | |
| 2102 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2103 | hlua = hlua_gethlua(L); |
| 2104 | if (!hlua) |
| 2105 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2107 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2108 | |
| 2109 | /* Check if we run on the same thread than the xreator thread. |
| 2110 | * We cannot access to the socket if the thread is different. |
| 2111 | */ |
| 2112 | if (socket->tid != tid) |
| 2113 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2114 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2115 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2116 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2118 | |
| 2119 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2120 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2121 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2122 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2123 | appctx->ctx.hlua_cosocket.die = 1; |
| 2124 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2125 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2126 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2127 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | return 0; |
| 2129 | } |
| 2130 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2131 | /* The close function calls close_helper. |
| 2132 | */ |
| 2133 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2134 | { |
| 2135 | MAY_LJMP(check_args(L, 1, "close")); |
| 2136 | return hlua_socket_close_helper(L); |
| 2137 | } |
| 2138 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | /* This Lua function assumes that the stack contain three parameters. |
| 2140 | * 1 - USERDATA containing a struct socket |
| 2141 | * 2 - INTEGER with values of the macro defined below |
| 2142 | * If the integer is -1, we must read at most one line. |
| 2143 | * If the integer is -2, we ust read all the data until the |
| 2144 | * end of the stream. |
| 2145 | * If the integer is positive value, we must read a number of |
| 2146 | * bytes corresponding to this value. |
| 2147 | */ |
| 2148 | #define HLSR_READ_LINE (-1) |
| 2149 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2150 | __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] | 2151 | { |
| 2152 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2153 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2154 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2156 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2158 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2159 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2160 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2161 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2162 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2163 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2164 | struct stream_interface *si; |
| 2165 | struct stream *s; |
| 2166 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2167 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2169 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2170 | hlua = hlua_gethlua(L); |
| 2171 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | /* Check if this lua stack is schedulable. */ |
| 2173 | if (!hlua || !hlua->task) |
| 2174 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2175 | "'frontend', 'backend' or 'task'")); |
| 2176 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2177 | /* Check if we run on the same thread than the xreator thread. |
| 2178 | * We cannot access to the socket if the thread is different. |
| 2179 | */ |
| 2180 | if (socket->tid != tid) |
| 2181 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2182 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2183 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2184 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2185 | if (!peer) |
| 2186 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2187 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2188 | si = appctx->owner; |
| 2189 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2191 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2194 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | if (nblk < 0) /* Connection close. */ |
| 2196 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2197 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2199 | |
| 2200 | /* remove final \r\n. */ |
| 2201 | if (nblk == 1) { |
| 2202 | if (blk1[len1-1] == '\n') { |
| 2203 | len1--; |
| 2204 | skip_at_end++; |
| 2205 | if (blk1[len1-1] == '\r') { |
| 2206 | len1--; |
| 2207 | skip_at_end++; |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | else { |
| 2212 | if (blk2[len2-1] == '\n') { |
| 2213 | len2--; |
| 2214 | skip_at_end++; |
| 2215 | if (blk2[len2-1] == '\r') { |
| 2216 | len2--; |
| 2217 | skip_at_end++; |
| 2218 | } |
| 2219 | } |
| 2220 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2225 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | if (nblk < 0) /* Connection close. */ |
| 2227 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2228 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | goto connection_empty; |
| 2230 | } |
| 2231 | |
| 2232 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2234 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | if (nblk < 0) /* Connection close. */ |
| 2236 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2237 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | goto connection_empty; |
| 2239 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2240 | missing_bytes = wanted - socket->b.n; |
| 2241 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2243 | len1 = missing_bytes; |
| 2244 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2245 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | len = len1; |
| 2249 | |
| 2250 | luaL_addlstring(&socket->b, blk1, len1); |
| 2251 | if (nblk == 2) { |
| 2252 | len += len2; |
| 2253 | luaL_addlstring(&socket->b, blk2, len2); |
| 2254 | } |
| 2255 | |
| 2256 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2257 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | |
| 2259 | /* Don't wait anything. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2260 | si_want_get(si); |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2261 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
| 2263 | /* If the pattern reclaim to read all the data |
| 2264 | * in the connection, got out. |
| 2265 | */ |
| 2266 | if (wanted == HLSR_READ_ALL) |
| 2267 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2268 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | goto connection_empty; |
| 2270 | |
| 2271 | /* Return result. */ |
| 2272 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2273 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2274 | return 1; |
| 2275 | |
| 2276 | connection_closed: |
| 2277 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | xref_unlock(&socket->xref, peer); |
| 2279 | |
| 2280 | no_peer: |
| 2281 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | /* If the buffer containds data. */ |
| 2283 | if (socket->b.n > 0) { |
| 2284 | luaL_pushresult(&socket->b); |
| 2285 | return 1; |
| 2286 | } |
| 2287 | lua_pushnil(L); |
| 2288 | lua_pushstring(L, "connection closed."); |
| 2289 | return 2; |
| 2290 | |
| 2291 | connection_empty: |
| 2292 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2293 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2294 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | } |
| 2297 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2298 | 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] | 2299 | return 0; |
| 2300 | } |
| 2301 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2302 | /* This Lua function gets two parameters. The first one can be string |
| 2303 | * or a number. If the string is "*l", the user requires one line. If |
| 2304 | * 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] | 2305 | * If the value is a number, the user require a number of bytes equal |
| 2306 | * to the value. The default value is "*l" (a line). |
| 2307 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2308 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | * integer takes this values: |
| 2310 | * -1 : read a line |
| 2311 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2312 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2314 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | * concatenated with the read data. |
| 2316 | */ |
| 2317 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2318 | { |
| 2319 | int wanted = HLSR_READ_LINE; |
| 2320 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2321 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | char *error; |
| 2323 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2324 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2325 | |
| 2326 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2327 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2328 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2329 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2331 | /* Check if we run on the same thread than the xreator thread. |
| 2332 | * We cannot access to the socket if the thread is different. |
| 2333 | */ |
| 2334 | if (socket->tid != tid) |
| 2335 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2336 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | /* check for pattern. */ |
| 2338 | if (lua_gettop(L) >= 2) { |
| 2339 | type = lua_type(L, 2); |
| 2340 | if (type == LUA_TSTRING) { |
| 2341 | pattern = lua_tostring(L, 2); |
| 2342 | if (strcmp(pattern, "*a") == 0) |
| 2343 | wanted = HLSR_READ_ALL; |
| 2344 | else if (strcmp(pattern, "*l") == 0) |
| 2345 | wanted = HLSR_READ_LINE; |
| 2346 | else { |
| 2347 | wanted = strtoll(pattern, &error, 10); |
| 2348 | if (*error != '\0') |
| 2349 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2350 | } |
| 2351 | } |
| 2352 | else if (type == LUA_TNUMBER) { |
| 2353 | wanted = lua_tointeger(L, 2); |
| 2354 | if (wanted < 0) |
| 2355 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | /* Set pattern. */ |
| 2360 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2361 | |
| 2362 | /* Check if we would replace the top by itself. */ |
| 2363 | if (lua_gettop(L) != 2) |
| 2364 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2366 | /* Save index of the top of the stack because since buffers are used, it |
| 2367 | * may change |
| 2368 | */ |
| 2369 | lastarg = lua_gettop(L); |
| 2370 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2371 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | luaL_buffinit(L, &socket->b); |
| 2373 | |
| 2374 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2375 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2377 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2378 | pattern = lua_tolstring(L, 3, &len); |
| 2379 | luaL_addlstring(&socket->b, pattern, len); |
| 2380 | } |
| 2381 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2382 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2386 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2388 | 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] | 2389 | { |
| 2390 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2391 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | struct appctx *appctx; |
| 2393 | size_t buf_len; |
| 2394 | const char *buf; |
| 2395 | int len; |
| 2396 | int send_len; |
| 2397 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | struct xref *peer; |
| 2399 | struct stream_interface *si; |
| 2400 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2402 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2403 | hlua = hlua_gethlua(L); |
| 2404 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | /* Check if this lua stack is schedulable. */ |
| 2406 | if (!hlua || !hlua->task) |
| 2407 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2408 | "'frontend', 'backend' or 'task'")); |
| 2409 | |
| 2410 | /* Get object */ |
| 2411 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2412 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2413 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2415 | /* Check if we run on the same thread than the xreator thread. |
| 2416 | * We cannot access to the socket if the thread is different. |
| 2417 | */ |
| 2418 | if (socket->tid != tid) |
| 2419 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2420 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2421 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2422 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2424 | lua_pushinteger(L, -1); |
| 2425 | return 1; |
| 2426 | } |
| 2427 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2428 | si = appctx->owner; |
| 2429 | s = si_strm(si); |
| 2430 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2432 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2433 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | lua_pushinteger(L, -1); |
| 2435 | return 1; |
| 2436 | } |
| 2437 | |
| 2438 | /* Update the input buffer data. */ |
| 2439 | buf += sent; |
| 2440 | send_len = buf_len - sent; |
| 2441 | |
| 2442 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | if (sent >= buf_len) { |
| 2444 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2448 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2449 | * the request buffer if its not required. |
| 2450 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2451 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2452 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2453 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2454 | } |
| 2455 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2456 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2457 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2458 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2460 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | |
| 2462 | /* send data */ |
| 2463 | if (len < send_len) |
| 2464 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2465 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | |
| 2467 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2468 | * the data" (-2) are not expected because the available length |
| 2469 | * is tested. |
| 2470 | * Other unknown error are also not expected. |
| 2471 | */ |
| 2472 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2473 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2474 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2475 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2476 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2478 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2479 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | return 1; |
| 2481 | } |
| 2482 | |
| 2483 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2484 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2485 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2486 | s->req.rex = TICK_ETERNITY; |
| 2487 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | |
| 2489 | /* Update length sent. */ |
| 2490 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2491 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2492 | |
| 2493 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2494 | if (sent + len >= buf_len) { |
| 2495 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2497 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | |
| 2499 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2500 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2501 | xref_unlock(&socket->xref, peer); |
| 2502 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2503 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2504 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2505 | 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] | 2506 | return 0; |
| 2507 | } |
| 2508 | |
| 2509 | /* This function initiate the send of data. It just check the input |
| 2510 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2511 | * 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] | 2512 | * "hlua_socket_write_yield" that can yield. |
| 2513 | * |
| 2514 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2515 | * the associated object. The second is a string buffer. The third is |
| 2516 | * a facultative integer that represents where is the buffer position |
| 2517 | * of the start of the data that can send. The first byte is the |
| 2518 | * position "1". The default value is "1". The fourth argument is a |
| 2519 | * facultative integer that represents where is the buffer position |
| 2520 | * of the end of the data that can send. The default is the last byte. |
| 2521 | */ |
| 2522 | static int hlua_socket_send(struct lua_State *L) |
| 2523 | { |
| 2524 | int i; |
| 2525 | int j; |
| 2526 | const char *buf; |
| 2527 | size_t buf_len; |
| 2528 | |
| 2529 | /* Check number of arguments. */ |
| 2530 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2531 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2532 | |
| 2533 | /* Get the string. */ |
| 2534 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2535 | |
| 2536 | /* Get and check j. */ |
| 2537 | if (lua_gettop(L) == 4) { |
| 2538 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2539 | if (j < 0) |
| 2540 | j = buf_len + j + 1; |
| 2541 | if (j > buf_len) |
| 2542 | j = buf_len + 1; |
| 2543 | lua_pop(L, 1); |
| 2544 | } |
| 2545 | else |
| 2546 | j = buf_len; |
| 2547 | |
| 2548 | /* Get and check i. */ |
| 2549 | if (lua_gettop(L) == 3) { |
| 2550 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2551 | if (i < 0) |
| 2552 | i = buf_len + i + 1; |
| 2553 | if (i > buf_len) |
| 2554 | i = buf_len + 1; |
| 2555 | lua_pop(L, 1); |
| 2556 | } else |
| 2557 | i = 1; |
| 2558 | |
| 2559 | /* Check bth i and j. */ |
| 2560 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2561 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | return 1; |
| 2563 | } |
| 2564 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2565 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | return 1; |
| 2567 | } |
| 2568 | if (i == 0) |
| 2569 | i = 1; |
| 2570 | if (j == 0) |
| 2571 | j = 1; |
| 2572 | |
| 2573 | /* Pop the string. */ |
| 2574 | lua_pop(L, 1); |
| 2575 | |
| 2576 | /* Update the buffer length. */ |
| 2577 | buf += i - 1; |
| 2578 | buf_len = j - i + 1; |
| 2579 | lua_pushlstring(L, buf, buf_len); |
| 2580 | |
| 2581 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2582 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2584 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2587 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2589 | { |
| 2590 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2591 | int ret; |
| 2592 | int len; |
| 2593 | char *p; |
| 2594 | |
| 2595 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2596 | if (ret <= 0) { |
| 2597 | lua_pushnil(L); |
| 2598 | return 1; |
| 2599 | } |
| 2600 | |
| 2601 | if (ret == AF_UNIX) { |
| 2602 | lua_pushstring(L, buffer+1); |
| 2603 | return 1; |
| 2604 | } |
| 2605 | else if (ret == AF_INET6) { |
| 2606 | buffer[0] = '['; |
| 2607 | len = strlen(buffer); |
| 2608 | buffer[len] = ']'; |
| 2609 | len++; |
| 2610 | buffer[len] = ':'; |
| 2611 | len++; |
| 2612 | p = buffer; |
| 2613 | } |
| 2614 | else if (ret == AF_INET) { |
| 2615 | p = buffer + 1; |
| 2616 | len = strlen(p); |
| 2617 | p[len] = ':'; |
| 2618 | len++; |
| 2619 | } |
| 2620 | else { |
| 2621 | lua_pushnil(L); |
| 2622 | return 1; |
| 2623 | } |
| 2624 | |
| 2625 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2626 | lua_pushnil(L); |
| 2627 | return 1; |
| 2628 | } |
| 2629 | |
| 2630 | lua_pushstring(L, p); |
| 2631 | return 1; |
| 2632 | } |
| 2633 | |
| 2634 | /* Returns information about the peer of the connection. */ |
| 2635 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2636 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2637 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2638 | struct xref *peer; |
| 2639 | struct appctx *appctx; |
| 2640 | struct stream_interface *si; |
| 2641 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2642 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2643 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2645 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2646 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2648 | /* Check if we run on the same thread than the xreator thread. |
| 2649 | * We cannot access to the socket if the thread is different. |
| 2650 | */ |
| 2651 | if (socket->tid != tid) |
| 2652 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2653 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2654 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2655 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2656 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | lua_pushnil(L); |
| 2658 | return 1; |
| 2659 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2661 | si = appctx->owner; |
| 2662 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2664 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2665 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2666 | lua_pushnil(L); |
| 2667 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | } |
| 2669 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2670 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2671 | xref_unlock(&socket->xref, peer); |
| 2672 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | /* Returns information about my connection side. */ |
| 2676 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2677 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2678 | struct hlua_socket *socket; |
| 2679 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2680 | struct appctx *appctx; |
| 2681 | struct xref *peer; |
| 2682 | struct stream_interface *si; |
| 2683 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2684 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2685 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2687 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2688 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2690 | /* Check if we run on the same thread than the xreator thread. |
| 2691 | * We cannot access to the socket if the thread is different. |
| 2692 | */ |
| 2693 | if (socket->tid != tid) |
| 2694 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2695 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2696 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2697 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2698 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | lua_pushnil(L); |
| 2700 | return 1; |
| 2701 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2702 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2703 | si = appctx->owner; |
| 2704 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2706 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2707 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2708 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2709 | lua_pushnil(L); |
| 2710 | return 1; |
| 2711 | } |
| 2712 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2713 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2714 | xref_unlock(&socket->xref, peer); |
| 2715 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2719 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | .obj_type = OBJ_TYPE_APPLET, |
| 2721 | .name = "<LUA_TCP>", |
| 2722 | .fct = hlua_socket_handler, |
| 2723 | .release = hlua_socket_release, |
| 2724 | }; |
| 2725 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2726 | __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] | 2727 | { |
| 2728 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2729 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2730 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2732 | struct stream_interface *si; |
| 2733 | struct stream *s; |
| 2734 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2735 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2736 | hlua = hlua_gethlua(L); |
| 2737 | if (!hlua) |
| 2738 | return 0; |
| 2739 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2740 | /* Check if we run on the same thread than the xreator thread. |
| 2741 | * We cannot access to the socket if the thread is different. |
| 2742 | */ |
| 2743 | if (socket->tid != tid) |
| 2744 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2745 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2746 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2747 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2749 | lua_pushnil(L); |
| 2750 | lua_pushstring(L, "Can't connect"); |
| 2751 | return 2; |
| 2752 | } |
| 2753 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2754 | si = appctx->owner; |
| 2755 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2756 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2757 | /* Check if we run on the same thread than the xreator thread. |
| 2758 | * We cannot access to the socket if the thread is different. |
| 2759 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2760 | if (socket->tid != tid) { |
| 2761 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2762 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2763 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2764 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2765 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2766 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2767 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2768 | lua_pushnil(L); |
| 2769 | lua_pushstring(L, "Can't connect"); |
| 2770 | return 2; |
| 2771 | } |
| 2772 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2773 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2776 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2777 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2778 | lua_pushinteger(L, 1); |
| 2779 | return 1; |
| 2780 | } |
| 2781 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2782 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2783 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2784 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2785 | } |
| 2786 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2787 | 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] | 2788 | return 0; |
| 2789 | } |
| 2790 | |
| 2791 | /* This function fail or initite the connection. */ |
| 2792 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2793 | { |
| 2794 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2795 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2796 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2797 | struct hlua *hlua; |
| 2798 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2799 | int low, high; |
| 2800 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2801 | struct xref *peer; |
| 2802 | struct stream_interface *si; |
| 2803 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2804 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2805 | if (lua_gettop(L) < 2) |
| 2806 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2807 | |
| 2808 | /* Get args. */ |
| 2809 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2810 | |
| 2811 | /* Check if we run on the same thread than the xreator thread. |
| 2812 | * We cannot access to the socket if the thread is different. |
| 2813 | */ |
| 2814 | if (socket->tid != tid) |
| 2815 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2816 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2817 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2818 | if (lua_gettop(L) >= 3) { |
| 2819 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2820 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2822 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2823 | * that are not enclosed within square brackets. |
| 2824 | */ |
| 2825 | if (port > 0) { |
| 2826 | luaL_buffinit(L, &b); |
| 2827 | luaL_addstring(&b, ip); |
| 2828 | luaL_addchar(&b, ':'); |
| 2829 | luaL_pushresult(&b); |
| 2830 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2831 | } |
| 2832 | } |
| 2833 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2834 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2835 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2836 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2837 | lua_pushnil(L); |
| 2838 | return 1; |
| 2839 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2840 | |
| 2841 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2842 | 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] | 2843 | if (!addr) { |
| 2844 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2845 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2846 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2847 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2848 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2849 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2850 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2851 | if (port == -1) { |
| 2852 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2853 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2854 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2855 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2856 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2857 | if (port == -1) { |
| 2858 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2859 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2860 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2861 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2862 | } |
| 2863 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2864 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2865 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2866 | si = appctx->owner; |
| 2867 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2868 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2869 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2870 | xref_unlock(&socket->xref, peer); |
| 2871 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2872 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2873 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2874 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2875 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2876 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2877 | if (!hlua) |
| 2878 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2879 | |
| 2880 | /* inform the stream that we want to be notified whenever the |
| 2881 | * connection completes. |
| 2882 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2883 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2884 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2885 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2886 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2887 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2888 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2889 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2890 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2891 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2892 | } |
| 2893 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2894 | |
| 2895 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2896 | /* Return yield waiting for connection. */ |
| 2897 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2898 | 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] | 2899 | |
| 2900 | return 0; |
| 2901 | } |
| 2902 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2903 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2904 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2905 | { |
| 2906 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2907 | struct xref *peer; |
| 2908 | struct appctx *appctx; |
| 2909 | struct stream_interface *si; |
| 2910 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2911 | |
| 2912 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2913 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2914 | |
| 2915 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2916 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2917 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2918 | lua_pushnil(L); |
| 2919 | return 1; |
| 2920 | } |
| 2921 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2922 | si = appctx->owner; |
| 2923 | s = si_strm(si); |
| 2924 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2925 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2926 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2927 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2928 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2929 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2930 | |
| 2931 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2932 | { |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2937 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2938 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2939 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2940 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2941 | struct xref *peer; |
| 2942 | struct appctx *appctx; |
| 2943 | struct stream_interface *si; |
| 2944 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2945 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2946 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2947 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2948 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2949 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2950 | /* convert the timeout to millis */ |
| 2951 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2952 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2953 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2954 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2955 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2956 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2957 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2958 | 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] | 2959 | |
| 2960 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2961 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2962 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2963 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2964 | /* Check if we run on the same thread than the xreator thread. |
| 2965 | * We cannot access to the socket if the thread is different. |
| 2966 | */ |
| 2967 | if (socket->tid != tid) |
| 2968 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2969 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2970 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2971 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2972 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2973 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2974 | WILL_LJMP(lua_error(L)); |
| 2975 | return 0; |
| 2976 | } |
| 2977 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2978 | si = appctx->owner; |
| 2979 | s = si_strm(si); |
| 2980 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2981 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2982 | s->req.rto = tmout; |
| 2983 | s->req.wto = tmout; |
| 2984 | s->res.rto = tmout; |
| 2985 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2986 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2987 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2988 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2989 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2990 | |
| 2991 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2992 | task_queue(s->task); |
| 2993 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2994 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2995 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2996 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2997 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3001 | { |
| 3002 | struct hlua_socket *socket; |
| 3003 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 3004 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3005 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3006 | |
| 3007 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3008 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3009 | hlua_pusherror(L, "socket: full stack"); |
| 3010 | goto out_fail_conf; |
| 3011 | } |
| 3012 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3013 | /* Create the object: obj[0] = userdata. */ |
| 3014 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3015 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3016 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3017 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3018 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3019 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3020 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3021 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3022 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3023 | goto out_fail_conf; |
| 3024 | } |
| 3025 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3026 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3027 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3028 | lua_setmetatable(L, -2); |
| 3029 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3030 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 3031 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3032 | if (!appctx) { |
| 3033 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 3034 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3035 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3036 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3037 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 3038 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3039 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 3040 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3041 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3042 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 3043 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3044 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3045 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3046 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 3049 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3050 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3051 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3052 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3053 | } |
| 3054 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3055 | /* Initialise cross reference between stream and Lua socket object. */ |
| 3056 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3057 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3058 | /* Configure "right" stream interface. this "si" is used to connect |
| 3059 | * and retrieve data from the server. The connection is initialized |
| 3060 | * with the "struct server". |
| 3061 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3062 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3063 | |
| 3064 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 3065 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3066 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3067 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3068 | return 1; |
| 3069 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3070 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 3071 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3072 | out_fail_sess: |
| 3073 | appctx_free(appctx); |
| 3074 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3075 | WILL_LJMP(lua_error(L)); |
| 3076 | return 0; |
| 3077 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3078 | |
| 3079 | /* |
| 3080 | * |
| 3081 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | * Class Channel |
| 3083 | * |
| 3084 | * |
| 3085 | */ |
| 3086 | |
| 3087 | /* Returns the struct hlua_channel join to the class channel in the |
| 3088 | * stack entry "ud" or throws an argument error. |
| 3089 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3090 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3092 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | } |
| 3094 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3095 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3096 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3097 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3098 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3101 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | return 0; |
| 3103 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3104 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3105 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3106 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3107 | |
| 3108 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3109 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3110 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | return 1; |
| 3112 | } |
| 3113 | |
| 3114 | /* Duplicate all the data present in the input channel and put it |
| 3115 | * in a string LUA variables. Returns -1 and push a nil value in |
| 3116 | * the stack if the channel is closed and all the data are consumed, |
| 3117 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3118 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3120 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | { |
| 3122 | char *blk1; |
| 3123 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3124 | size_t len1; |
| 3125 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | int ret; |
| 3127 | luaL_Buffer b; |
| 3128 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3129 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3130 | if (unlikely(ret <= 0)) { |
| 3131 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3132 | lua_pushnil(L); |
| 3133 | return -1; |
| 3134 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3135 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | luaL_buffinit(L, &b); |
| 3139 | luaL_addlstring(&b, blk1, len1); |
| 3140 | if (unlikely(ret == 2)) |
| 3141 | luaL_addlstring(&b, blk2, len2); |
| 3142 | luaL_pushresult(&b); |
| 3143 | |
| 3144 | if (unlikely(ret == 2)) |
| 3145 | return len1 + len2; |
| 3146 | return len1; |
| 3147 | } |
| 3148 | |
| 3149 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3150 | * a yield. This function keep the data in the buffer. |
| 3151 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3152 | __LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3154 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3156 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3157 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3158 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3159 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3160 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3161 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3162 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3164 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | return 1; |
| 3166 | } |
| 3167 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3168 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3169 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3170 | { |
| 3171 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3172 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3173 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3174 | } |
| 3175 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3177 | * a yield. This function consumes the data in the buffer. It returns |
| 3178 | * a string containing the data or a nil pointer if no data are available |
| 3179 | * and the channel is closed. |
| 3180 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3181 | __LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3183 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3184 | int ret; |
| 3185 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3186 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3187 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3188 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3189 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3190 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3191 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3192 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3193 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3194 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3195 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3196 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3197 | if (unlikely(ret == -1)) |
| 3198 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3199 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3200 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3201 | return 1; |
| 3202 | } |
| 3203 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3204 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3205 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3206 | { |
| 3207 | MAY_LJMP(check_args(L, 1, "get")); |
| 3208 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3209 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3210 | } |
| 3211 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3212 | /* This functions consumes and returns one line. If the channel is closed, |
| 3213 | * 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] | 3214 | * 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] | 3215 | * value. |
| 3216 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3217 | __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] | 3218 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | char *blk1; |
| 3220 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3221 | size_t len1; |
| 3222 | size_t len2; |
| 3223 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3224 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3225 | int ret; |
| 3226 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3227 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3228 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3229 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3230 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3231 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3232 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3233 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3234 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3235 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3236 | if (ret == 0) { |
| 3237 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3238 | _hlua_channel_dup(chn, L); |
| 3239 | return 1; |
| 3240 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3241 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3242 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3243 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3244 | if (ret == -1) { |
| 3245 | lua_pushnil(L); |
| 3246 | return 1; |
| 3247 | } |
| 3248 | |
| 3249 | luaL_buffinit(L, &b); |
| 3250 | luaL_addlstring(&b, blk1, len1); |
| 3251 | len = len1; |
| 3252 | if (unlikely(ret == 2)) { |
| 3253 | luaL_addlstring(&b, blk2, len2); |
| 3254 | len += len2; |
| 3255 | } |
| 3256 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3257 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3258 | return 1; |
| 3259 | } |
| 3260 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3261 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3262 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3263 | { |
| 3264 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3265 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3266 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3267 | } |
| 3268 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3269 | /* This function takes a string as argument, and append it at the input side of |
| 3270 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3271 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3272 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3273 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3274 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3276 | struct channel *chn; |
| 3277 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3279 | |
| 3280 | MAY_LJMP(check_args(L, 2, "append")); |
| 3281 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3282 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3283 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3284 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3285 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3286 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3287 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3288 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3289 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3290 | lua_pushinteger(L, -1); |
| 3291 | else |
| 3292 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3293 | return 1; |
| 3294 | } |
| 3295 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3296 | /* The function replaces input data from the channel by the string passed as |
| 3297 | * argument. Before clearing the buffer, we take care the copy will be |
| 3298 | * possible. It returns the amount of data written (the string length) on |
| 3299 | * success or -1 if the copy is not performed. This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3300 | */ |
| 3301 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3302 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3303 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3304 | const char *str; |
| 3305 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3306 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3307 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3308 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3309 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3310 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3311 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3312 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3313 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3314 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3315 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3316 | /* Be sure we can copied the string once input data will be removed. */ |
| 3317 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3318 | lua_pushinteger(L, -1); |
| 3319 | else { |
| 3320 | b_set_data(&chn->buf, co_data(chn)); |
| 3321 | if (ci_putblk(chn, str, len) < 0) |
| 3322 | lua_pushinteger(L, -1); |
| 3323 | else |
| 3324 | lua_pushinteger(L, len); |
| 3325 | } |
| 3326 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3327 | } |
| 3328 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3329 | /* Append data in the output side of the buffer. This data is immediately |
| 3330 | * sent. The function returns the amount of data written. If the buffer |
| 3331 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3332 | * if the channel is closed. |
| 3333 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3334 | __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] | 3335 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3336 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3337 | size_t len; |
| 3338 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3339 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3340 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3341 | struct hlua *hlua; |
| 3342 | |
| 3343 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3344 | hlua = hlua_gethlua(L); |
| 3345 | if (!hlua) { |
| 3346 | lua_pushnil(L); |
| 3347 | return 1; |
| 3348 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3349 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3350 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3351 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3352 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3353 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3354 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3355 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3356 | lua_pushinteger(L, -1); |
| 3357 | return 1; |
| 3358 | } |
| 3359 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3360 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3361 | * the request buffer if its not required. |
| 3362 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3363 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3364 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3365 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3366 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3367 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3368 | } |
| 3369 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3370 | /* The written data will be immediately sent, so we can check |
| 3371 | * the available space without taking in account the reserve. |
| 3372 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3373 | * data, because the buffer will be flushed. |
| 3374 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3375 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3376 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3377 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3378 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3379 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3380 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3381 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3382 | return 1; |
| 3383 | |
| 3384 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3385 | if (max > len - l) |
| 3386 | max = len - l; |
| 3387 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3388 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3389 | * detects a non contiguous buffer and realign it. |
| 3390 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3391 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3392 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3393 | |
| 3394 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3395 | max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3396 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3397 | /* buffer replace considers that the input part is filled. |
| 3398 | * so, I must forward these new data in the output part. |
| 3399 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3400 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3401 | |
| 3402 | l += max; |
| 3403 | lua_pop(L, 1); |
| 3404 | lua_pushinteger(L, l); |
| 3405 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3406 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3407 | /* If there is no space available, and the output buffer is empty. |
| 3408 | * in this case, we cannot add more data, so we cannot yield, |
| 3409 | * we return the amount of copied data. |
| 3410 | */ |
| 3411 | max = b_room(&chn->buf); |
| 3412 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3413 | return 1; |
| 3414 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3415 | /* If we are waiting for space in the response buffer, we |
| 3416 | * must set the flag WAKERESWR. This flag required the task |
| 3417 | * wake up if any activity is detected on the response buffer. |
| 3418 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3419 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3420 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3421 | else |
| 3422 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3423 | 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] | 3424 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3425 | |
| 3426 | return 1; |
| 3427 | } |
| 3428 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3429 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3430 | * yield the LUA process, and resume it without checking the |
| 3431 | * input arguments. |
| 3432 | */ |
| 3433 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3434 | { |
| 3435 | MAY_LJMP(check_args(L, 2, "send")); |
| 3436 | lua_pushinteger(L, 0); |
| 3437 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3438 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | /* This function forward and amount of butes. The data pass from |
| 3442 | * the input side of the buffer to the output side, and can be |
| 3443 | * forwarded. This function never fails. |
| 3444 | * |
| 3445 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3446 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3447 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3448 | __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] | 3449 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3450 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3451 | int len; |
| 3452 | int l; |
| 3453 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3454 | struct hlua *hlua; |
| 3455 | |
| 3456 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3457 | hlua = hlua_gethlua(L); |
| 3458 | if (!hlua) |
| 3459 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3460 | |
| 3461 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3462 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3463 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3464 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3465 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3466 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3467 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3468 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3469 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3470 | |
| 3471 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3472 | if (max > ci_data(chn)) |
| 3473 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3474 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3475 | l += max; |
| 3476 | |
| 3477 | lua_pop(L, 1); |
| 3478 | lua_pushinteger(L, l); |
| 3479 | |
| 3480 | /* Check if it miss bytes to forward. */ |
| 3481 | if (l < len) { |
| 3482 | /* The the input channel or the output channel are closed, we |
| 3483 | * must return the amount of data forwarded. |
| 3484 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3485 | 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] | 3486 | return 1; |
| 3487 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3488 | /* If we are waiting for space data in the response buffer, we |
| 3489 | * must set the flag WAKERESWR. This flag required the task |
| 3490 | * wake up if any activity is detected on the response buffer. |
| 3491 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3492 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3493 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3494 | else |
| 3495 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3496 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3497 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3498 | 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] | 3499 | } |
| 3500 | |
| 3501 | return 1; |
| 3502 | } |
| 3503 | |
| 3504 | /* Just check the input and prepare the stack for the previous |
| 3505 | * function "hlua_channel_forward_yield" |
| 3506 | */ |
| 3507 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3508 | { |
| 3509 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3510 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3511 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3512 | |
| 3513 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3514 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | /* Just returns the number of bytes available in the input |
| 3518 | * side of the buffer. This function never fails. |
| 3519 | */ |
| 3520 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3521 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3522 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3523 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3524 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3525 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3526 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3527 | struct htx *htx = htxbuf(&chn->buf); |
| 3528 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3529 | } |
| 3530 | else |
| 3531 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3532 | return 1; |
| 3533 | } |
| 3534 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3535 | /* Returns true if the channel is full. */ |
| 3536 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3537 | { |
| 3538 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3539 | |
| 3540 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3541 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3542 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3543 | * applet). |
| 3544 | */ |
| 3545 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3546 | return 1; |
| 3547 | } |
| 3548 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3549 | /* Returns true if the channel is the response channel. */ |
| 3550 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3551 | { |
| 3552 | struct channel *chn; |
| 3553 | |
| 3554 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3555 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3556 | |
| 3557 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3558 | return 1; |
| 3559 | } |
| 3560 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3561 | /* Just returns the number of bytes available in the output |
| 3562 | * side of the buffer. This function never fails. |
| 3563 | */ |
| 3564 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3565 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3566 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3567 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3568 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3569 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3570 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3571 | return 1; |
| 3572 | } |
| 3573 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3574 | /* |
| 3575 | * |
| 3576 | * |
| 3577 | * Class Fetches |
| 3578 | * |
| 3579 | * |
| 3580 | */ |
| 3581 | |
| 3582 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3583 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3584 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3585 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3586 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3587 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | /* This function creates and push in the stack a fetch object according |
| 3591 | * with a current TXN. |
| 3592 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3593 | 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] | 3594 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3595 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3596 | |
| 3597 | /* Check stack size. */ |
| 3598 | if (!lua_checkstack(L, 3)) |
| 3599 | return 0; |
| 3600 | |
| 3601 | /* Create the object: obj[0] = userdata. |
| 3602 | * Note that the base of the Fetches object is the |
| 3603 | * transaction object. |
| 3604 | */ |
| 3605 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3606 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3607 | lua_rawseti(L, -2, 0); |
| 3608 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3609 | hsmp->s = txn->s; |
| 3610 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3611 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3612 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3613 | |
| 3614 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3615 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3616 | lua_setmetatable(L, -2); |
| 3617 | |
| 3618 | return 1; |
| 3619 | } |
| 3620 | |
| 3621 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3622 | * It uses closure argument to store the associated sample-fetch. It |
| 3623 | * returns only one argument or throws an error. An error is thrown |
| 3624 | * only if an error is encountered during the argument parsing. If |
| 3625 | * the "sample-fetch" function fails, nil is returned. |
| 3626 | */ |
| 3627 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3628 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3629 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3630 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3631 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3632 | int i; |
| 3633 | struct sample smp; |
| 3634 | |
| 3635 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3636 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3637 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3638 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3639 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3640 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3641 | /* Check execution authorization. */ |
| 3642 | if (f->use & SMP_USE_HTTP_ANY && |
| 3643 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3644 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3645 | "is not available in Lua services", f->kw); |
| 3646 | WILL_LJMP(lua_error(L)); |
| 3647 | } |
| 3648 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3649 | /* Get extra arguments. */ |
| 3650 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3651 | if (i >= ARGM_NBARGS) |
| 3652 | break; |
| 3653 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3654 | } |
| 3655 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3656 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3657 | |
| 3658 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3659 | 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] | 3660 | |
| 3661 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3662 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3663 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3664 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | /* Initialise the sample. */ |
| 3668 | memset(&smp, 0, sizeof(smp)); |
| 3669 | |
| 3670 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3671 | 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] | 3672 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3673 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3674 | lua_pushstring(L, ""); |
| 3675 | else |
| 3676 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3677 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3681 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3682 | hlua_smp2lua_str(L, &smp); |
| 3683 | else |
| 3684 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3685 | |
| 3686 | end: |
| 3687 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3688 | if (args[i].type == ARGT_STR) |
| 3689 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3690 | else if (args[i].type == ARGT_REG) |
| 3691 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3692 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3693 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3694 | |
| 3695 | error: |
| 3696 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3697 | if (args[i].type == ARGT_STR) |
| 3698 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3699 | else if (args[i].type == ARGT_REG) |
| 3700 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3701 | } |
| 3702 | WILL_LJMP(lua_error(L)); |
| 3703 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3704 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3705 | |
| 3706 | /* |
| 3707 | * |
| 3708 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3709 | * Class Converters |
| 3710 | * |
| 3711 | * |
| 3712 | */ |
| 3713 | |
| 3714 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3715 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3716 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3717 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3718 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3719 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | /* This function creates and push in the stack a Converters object |
| 3723 | * according with a current TXN. |
| 3724 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3725 | 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] | 3726 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3727 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3728 | |
| 3729 | /* Check stack size. */ |
| 3730 | if (!lua_checkstack(L, 3)) |
| 3731 | return 0; |
| 3732 | |
| 3733 | /* Create the object: obj[0] = userdata. |
| 3734 | * Note that the base of the Converters object is the |
| 3735 | * same than the TXN object. |
| 3736 | */ |
| 3737 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3738 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3739 | lua_rawseti(L, -2, 0); |
| 3740 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3741 | hsmp->s = txn->s; |
| 3742 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3743 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3744 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3745 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3746 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3747 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3748 | lua_setmetatable(L, -2); |
| 3749 | |
| 3750 | return 1; |
| 3751 | } |
| 3752 | |
| 3753 | /* This function is an LUA binding. It is called with each converter. |
| 3754 | * It uses closure argument to store the associated converter. It |
| 3755 | * returns only one argument or throws an error. An error is thrown |
| 3756 | * only if an error is encountered during the argument parsing. If |
| 3757 | * the converter function function fails, nil is returned. |
| 3758 | */ |
| 3759 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3760 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3761 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3762 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3763 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3764 | int i; |
| 3765 | struct sample smp; |
| 3766 | |
| 3767 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3768 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3769 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3770 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3771 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3772 | |
| 3773 | /* Get extra arguments. */ |
| 3774 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3775 | if (i >= ARGM_NBARGS) |
| 3776 | break; |
| 3777 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3778 | } |
| 3779 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3780 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3781 | |
| 3782 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3783 | 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] | 3784 | |
| 3785 | /* Run the special args checker. */ |
| 3786 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3787 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3788 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3792 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3793 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3794 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3795 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3796 | } |
| 3797 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3798 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3799 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3800 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3801 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3802 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3803 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3804 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3805 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3806 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3807 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3808 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3809 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3813 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3814 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3815 | lua_pushstring(L, ""); |
| 3816 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3817 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3818 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3822 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3823 | hlua_smp2lua_str(L, &smp); |
| 3824 | else |
| 3825 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3826 | end: |
| 3827 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3828 | if (args[i].type == ARGT_STR) |
| 3829 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3830 | else if (args[i].type == ARGT_REG) |
| 3831 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3832 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3833 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3834 | |
| 3835 | error: |
| 3836 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3837 | if (args[i].type == ARGT_STR) |
| 3838 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3839 | else if (args[i].type == ARGT_REG) |
| 3840 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3841 | } |
| 3842 | WILL_LJMP(lua_error(L)); |
| 3843 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3844 | } |
| 3845 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3846 | /* |
| 3847 | * |
| 3848 | * |
| 3849 | * Class AppletTCP |
| 3850 | * |
| 3851 | * |
| 3852 | */ |
| 3853 | |
| 3854 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3855 | * a class stream, otherwise it throws an error. |
| 3856 | */ |
| 3857 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3858 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3859 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /* This function creates and push in the stack an Applet object |
| 3863 | * according with a current TXN. |
| 3864 | */ |
| 3865 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3866 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3867 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3868 | struct stream_interface *si = ctx->owner; |
| 3869 | struct stream *s = si_strm(si); |
| 3870 | struct proxy *p = s->be; |
| 3871 | |
| 3872 | /* Check stack size. */ |
| 3873 | if (!lua_checkstack(L, 3)) |
| 3874 | return 0; |
| 3875 | |
| 3876 | /* Create the object: obj[0] = userdata. |
| 3877 | * Note that the base of the Converters object is the |
| 3878 | * same than the TXN object. |
| 3879 | */ |
| 3880 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3881 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3882 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3883 | luactx->appctx = ctx; |
| 3884 | luactx->htxn.s = s; |
| 3885 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3886 | |
| 3887 | /* Create the "f" field that contains a list of fetches. */ |
| 3888 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3889 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3890 | return 0; |
| 3891 | lua_settable(L, -3); |
| 3892 | |
| 3893 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3894 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3895 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3896 | return 0; |
| 3897 | lua_settable(L, -3); |
| 3898 | |
| 3899 | /* Create the "c" field that contains a list of converters. */ |
| 3900 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3901 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3902 | return 0; |
| 3903 | lua_settable(L, -3); |
| 3904 | |
| 3905 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3906 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3907 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3908 | return 0; |
| 3909 | lua_settable(L, -3); |
| 3910 | |
| 3911 | /* Pop a class stream metatable and affect it to the table. */ |
| 3912 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3913 | lua_setmetatable(L, -2); |
| 3914 | |
| 3915 | return 1; |
| 3916 | } |
| 3917 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3918 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3919 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3920 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3921 | struct stream *s; |
| 3922 | const char *name; |
| 3923 | size_t len; |
| 3924 | struct sample smp; |
| 3925 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3926 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3927 | 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] | 3928 | |
| 3929 | /* It is useles to retrieve the stream, but this function |
| 3930 | * runs only in a stream context. |
| 3931 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3932 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3933 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3934 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3935 | |
| 3936 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3937 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3938 | hlua_lua2smp(L, 3, &smp); |
| 3939 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 3940 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 3941 | * already takes care of duplicating dynamic var data. |
| 3942 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3943 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3944 | |
| 3945 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3946 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3947 | else |
| 3948 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3949 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3950 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3954 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3955 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3956 | struct stream *s; |
| 3957 | const char *name; |
| 3958 | size_t len; |
| 3959 | struct sample smp; |
| 3960 | |
| 3961 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3962 | |
| 3963 | /* It is useles to retrieve the stream, but this function |
| 3964 | * runs only in a stream context. |
| 3965 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3966 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3967 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3968 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3969 | |
| 3970 | /* Unset the variable. */ |
| 3971 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3972 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3973 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3977 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3978 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3979 | struct stream *s; |
| 3980 | const char *name; |
| 3981 | size_t len; |
| 3982 | struct sample smp; |
| 3983 | |
| 3984 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3985 | |
| 3986 | /* It is useles to retrieve the stream, but this function |
| 3987 | * runs only in a stream context. |
| 3988 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3989 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3990 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3991 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3992 | |
| 3993 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3994 | if (!vars_get_by_name(name, len, &smp)) { |
| 3995 | lua_pushnil(L); |
| 3996 | return 1; |
| 3997 | } |
| 3998 | |
| 3999 | return hlua_smp2lua(L, &smp); |
| 4000 | } |
| 4001 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4002 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4003 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4004 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4005 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4006 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apptcp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4007 | |
| 4008 | /* Note that this hlua struct is from the session and not from the applet. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 4009 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4010 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4011 | |
| 4012 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4013 | |
| 4014 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4015 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4016 | |
| 4017 | /* Get and store new value. */ |
| 4018 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4019 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4020 | |
| 4021 | return 0; |
| 4022 | } |
| 4023 | |
| 4024 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4025 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4026 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4027 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4028 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apptcp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4029 | |
| 4030 | /* Note that this hlua struct is from the session and not from the applet. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 4031 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4032 | lua_pushnil(L); |
| 4033 | return 1; |
| 4034 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4035 | |
| 4036 | /* Push configuration index in the stack. */ |
| 4037 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4038 | |
| 4039 | return 1; |
| 4040 | } |
| 4041 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4042 | /* If expected data not yet available, it returns a yield. This function |
| 4043 | * consumes the data in the buffer. It returns a string containing the |
| 4044 | * data. This string can be empty. |
| 4045 | */ |
| 4046 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4047 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4048 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4049 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4050 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4051 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4052 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4053 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4054 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4055 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4056 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4057 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4058 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4059 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4060 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4061 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4062 | 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] | 4063 | } |
| 4064 | |
| 4065 | /* End of data: commit the total strings and return. */ |
| 4066 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4067 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4068 | return 1; |
| 4069 | } |
| 4070 | |
| 4071 | /* Ensure that the block 2 length is usable. */ |
| 4072 | if (ret == 1) |
| 4073 | len2 = 0; |
| 4074 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4075 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4076 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4077 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4078 | |
| 4079 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4080 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4081 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4082 | return 1; |
| 4083 | } |
| 4084 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4085 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4086 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4087 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4088 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4089 | |
| 4090 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4091 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4092 | |
| 4093 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4094 | } |
| 4095 | |
| 4096 | /* If expected data not yet available, it returns a yield. This function |
| 4097 | * consumes the data in the buffer. It returns a string containing the |
| 4098 | * data. This string can be empty. |
| 4099 | */ |
| 4100 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4101 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4102 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4103 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4104 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4105 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4106 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4107 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4108 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4109 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4110 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4111 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4112 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4113 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4114 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4115 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4116 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4117 | 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] | 4118 | } |
| 4119 | |
| 4120 | /* End of data: commit the total strings and return. */ |
| 4121 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4122 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4123 | return 1; |
| 4124 | } |
| 4125 | |
| 4126 | /* Ensure that the block 2 length is usable. */ |
| 4127 | if (ret == 1) |
| 4128 | len2 = 0; |
| 4129 | |
| 4130 | if (len == -1) { |
| 4131 | |
| 4132 | /* If len == -1, catenate all the data avalaile and |
| 4133 | * yield because we want to get all the data until |
| 4134 | * the end of data stream. |
| 4135 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4136 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4137 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4138 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4139 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4140 | 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] | 4141 | |
| 4142 | } else { |
| 4143 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4144 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4145 | if (len1 > len) |
| 4146 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4147 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4148 | len -= len1; |
| 4149 | |
| 4150 | /* Copy the second block. */ |
| 4151 | if (len2 > len) |
| 4152 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4153 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4154 | len -= len2; |
| 4155 | |
| 4156 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4157 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4158 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4159 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4160 | if (len > 0) { |
| 4161 | lua_pushinteger(L, len); |
| 4162 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4163 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4164 | 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] | 4165 | } |
| 4166 | |
| 4167 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4168 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4169 | return 1; |
| 4170 | } |
| 4171 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4172 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4173 | hlua_pusherror(L, "Lua: internal error"); |
| 4174 | WILL_LJMP(lua_error(L)); |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4178 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4179 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4180 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4181 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4182 | int len = -1; |
| 4183 | |
| 4184 | if (lua_gettop(L) > 2) |
| 4185 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4186 | if (lua_gettop(L) >= 2) { |
| 4187 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4188 | lua_pop(L, 1); |
| 4189 | } |
| 4190 | |
| 4191 | /* Confirm or set the required length */ |
| 4192 | lua_pushinteger(L, len); |
| 4193 | |
| 4194 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4195 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4196 | |
| 4197 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4198 | } |
| 4199 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4200 | /* Append data in the output side of the buffer. This data is immediately |
| 4201 | * sent. The function returns the amount of data written. If the buffer |
| 4202 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4203 | * if the channel is closed. |
| 4204 | */ |
| 4205 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4206 | { |
| 4207 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4208 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4209 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4210 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4211 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4212 | struct channel *chn = si_ic(si); |
| 4213 | int max; |
| 4214 | |
| 4215 | /* Get the max amount of data which can write as input in the channel. */ |
| 4216 | max = channel_recv_max(chn); |
| 4217 | if (max > (len - l)) |
| 4218 | max = len - l; |
| 4219 | |
| 4220 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4221 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4222 | |
| 4223 | /* update counters. */ |
| 4224 | l += max; |
| 4225 | lua_pop(L, 1); |
| 4226 | lua_pushinteger(L, l); |
| 4227 | |
| 4228 | /* If some data is not send, declares the situation to the |
| 4229 | * applet, and returns a yield. |
| 4230 | */ |
| 4231 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4232 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4233 | 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] | 4234 | } |
| 4235 | |
| 4236 | return 1; |
| 4237 | } |
| 4238 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4239 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4240 | * yield the LUA process, and resume it without checking the |
| 4241 | * input arguments. |
| 4242 | */ |
| 4243 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4244 | { |
| 4245 | MAY_LJMP(check_args(L, 2, "send")); |
| 4246 | lua_pushinteger(L, 0); |
| 4247 | |
| 4248 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4249 | } |
| 4250 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4251 | /* |
| 4252 | * |
| 4253 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4254 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4255 | * |
| 4256 | * |
| 4257 | */ |
| 4258 | |
| 4259 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4260 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4261 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4262 | __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] | 4263 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4264 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4265 | } |
| 4266 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4267 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4268 | * according with a current TXN. |
| 4269 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4270 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4271 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4272 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4273 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4274 | struct stream_interface *si = ctx->owner; |
| 4275 | struct stream *s = si_strm(si); |
| 4276 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4277 | struct htx *htx; |
| 4278 | struct htx_blk *blk; |
| 4279 | struct htx_sl *sl; |
| 4280 | struct ist path; |
| 4281 | unsigned long long len = 0; |
| 4282 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4283 | |
| 4284 | /* Check stack size. */ |
| 4285 | if (!lua_checkstack(L, 3)) |
| 4286 | return 0; |
| 4287 | |
| 4288 | /* Create the object: obj[0] = userdata. |
| 4289 | * Note that the base of the Converters object is the |
| 4290 | * same than the TXN object. |
| 4291 | */ |
| 4292 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4293 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4294 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4295 | luactx->appctx = ctx; |
| 4296 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4297 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4298 | luactx->htxn.s = s; |
| 4299 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4300 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4301 | /* Create the "f" field that contains a list of fetches. */ |
| 4302 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4303 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4304 | return 0; |
| 4305 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4306 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4307 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4308 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4309 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4310 | return 0; |
| 4311 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4312 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4313 | /* Create the "c" field that contains a list of converters. */ |
| 4314 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4315 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4316 | return 0; |
| 4317 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4318 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4319 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4320 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4321 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4322 | return 0; |
| 4323 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4324 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4325 | htx = htxbuf(&s->req.buf); |
| 4326 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4327 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4328 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4329 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4330 | /* Stores the request method. */ |
| 4331 | lua_pushstring(L, "method"); |
| 4332 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4333 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4334 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4335 | /* Stores the http version. */ |
| 4336 | lua_pushstring(L, "version"); |
| 4337 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4338 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4339 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4340 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4341 | * the array on the top of the stack. |
| 4342 | */ |
| 4343 | lua_pushstring(L, "headers"); |
| 4344 | htxn.s = s; |
| 4345 | htxn.p = px; |
| 4346 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4347 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4348 | return 0; |
| 4349 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4350 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4351 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4352 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4353 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4354 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4355 | p = path.ptr; |
| 4356 | end = path.ptr + path.len; |
| 4357 | q = p; |
| 4358 | while (q < end && *q != '?') |
| 4359 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4360 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4361 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4362 | lua_pushstring(L, "path"); |
| 4363 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4364 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4365 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4366 | /* Stores the query string. */ |
| 4367 | lua_pushstring(L, "qs"); |
| 4368 | if (*q == '?') |
| 4369 | q++; |
| 4370 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4371 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4372 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4373 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4374 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4375 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4376 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4377 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4378 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4379 | break; |
| 4380 | if (type == HTX_BLK_DATA) |
| 4381 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4382 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4383 | if (htx->extra != ULLONG_MAX) |
| 4384 | len += htx->extra; |
| 4385 | |
| 4386 | /* Stores the request path. */ |
| 4387 | lua_pushstring(L, "length"); |
| 4388 | lua_pushinteger(L, len); |
| 4389 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4390 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4391 | /* Create an empty array of HTTP request headers. */ |
| 4392 | lua_pushstring(L, "response"); |
| 4393 | lua_newtable(L); |
| 4394 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4395 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4396 | /* Pop a class stream metatable and affect it to the table. */ |
| 4397 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4398 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4399 | |
| 4400 | return 1; |
| 4401 | } |
| 4402 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4403 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4404 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4405 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4406 | struct stream *s; |
| 4407 | const char *name; |
| 4408 | size_t len; |
| 4409 | struct sample smp; |
| 4410 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4411 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4412 | 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] | 4413 | |
| 4414 | /* It is useles to retrieve the stream, but this function |
| 4415 | * runs only in a stream context. |
| 4416 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4417 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4418 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4419 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4420 | |
| 4421 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4422 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4423 | hlua_lua2smp(L, 3, &smp); |
| 4424 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4425 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4426 | * already takes care of duplicating dynamic var data. |
| 4427 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4428 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4429 | |
| 4430 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4431 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4432 | else |
| 4433 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4434 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4435 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4439 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4440 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4441 | struct stream *s; |
| 4442 | const char *name; |
| 4443 | size_t len; |
| 4444 | struct sample smp; |
| 4445 | |
| 4446 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4447 | |
| 4448 | /* It is useles to retrieve the stream, but this function |
| 4449 | * runs only in a stream context. |
| 4450 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4451 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4452 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4453 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4454 | |
| 4455 | /* Unset the variable. */ |
| 4456 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4457 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4458 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4462 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4463 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4464 | struct stream *s; |
| 4465 | const char *name; |
| 4466 | size_t len; |
| 4467 | struct sample smp; |
| 4468 | |
| 4469 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4470 | |
| 4471 | /* It is useles to retrieve the stream, but this function |
| 4472 | * runs only in a stream context. |
| 4473 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4474 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4475 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4476 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4477 | |
| 4478 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4479 | if (!vars_get_by_name(name, len, &smp)) { |
| 4480 | lua_pushnil(L); |
| 4481 | return 1; |
| 4482 | } |
| 4483 | |
| 4484 | return hlua_smp2lua(L, &smp); |
| 4485 | } |
| 4486 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4487 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4488 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4489 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4490 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4491 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apphttp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4492 | |
| 4493 | /* Note that this hlua struct is from the session and not from the applet. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 4494 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4495 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4496 | |
| 4497 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4498 | |
| 4499 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4500 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4501 | |
| 4502 | /* Get and store new value. */ |
| 4503 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4504 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4505 | |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
| 4509 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4510 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4511 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4512 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4513 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apphttp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4514 | |
| 4515 | /* Note that this hlua struct is from the session and not from the applet. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 4516 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4517 | lua_pushnil(L); |
| 4518 | return 1; |
| 4519 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4520 | |
| 4521 | /* Push configuration index in the stack. */ |
| 4522 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4523 | |
| 4524 | return 1; |
| 4525 | } |
| 4526 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4527 | /* If expected data not yet available, it returns a yield. This function |
| 4528 | * consumes the data in the buffer. It returns a string containing the |
| 4529 | * data. This string can be empty. |
| 4530 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4531 | __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] | 4532 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4533 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4534 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4535 | struct channel *req = si_oc(si); |
| 4536 | struct htx *htx; |
| 4537 | struct htx_blk *blk; |
| 4538 | size_t count; |
| 4539 | int stop = 0; |
| 4540 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4541 | htx = htx_from_buf(&req->buf); |
| 4542 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4543 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4544 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4545 | while (count && !stop && blk) { |
| 4546 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4547 | uint32_t sz = htx_get_blksz(blk); |
| 4548 | struct ist v; |
| 4549 | uint32_t vlen; |
| 4550 | char *nl; |
| 4551 | |
| 4552 | vlen = sz; |
| 4553 | if (vlen > count) { |
| 4554 | if (type != HTX_BLK_DATA) |
| 4555 | break; |
| 4556 | vlen = count; |
| 4557 | } |
| 4558 | |
| 4559 | switch (type) { |
| 4560 | case HTX_BLK_UNUSED: |
| 4561 | break; |
| 4562 | |
| 4563 | case HTX_BLK_DATA: |
| 4564 | v = htx_get_blk_value(htx, blk); |
| 4565 | v.len = vlen; |
| 4566 | nl = istchr(v, '\n'); |
| 4567 | if (nl != NULL) { |
| 4568 | stop = 1; |
| 4569 | vlen = nl - v.ptr + 1; |
| 4570 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4571 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4572 | break; |
| 4573 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4574 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4575 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4576 | stop = 1; |
| 4577 | break; |
| 4578 | |
| 4579 | default: |
| 4580 | break; |
| 4581 | } |
| 4582 | |
| 4583 | co_set_data(req, co_data(req) - vlen); |
| 4584 | count -= vlen; |
| 4585 | if (sz == vlen) |
| 4586 | blk = htx_remove_blk(htx, blk); |
| 4587 | else { |
| 4588 | htx_cut_data_blk(htx, blk, vlen); |
| 4589 | break; |
| 4590 | } |
| 4591 | } |
| 4592 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4593 | /* The message was fully consumed and no more data are expected |
| 4594 | * (EOM flag set). |
| 4595 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4596 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4597 | stop = 1; |
| 4598 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4599 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4600 | if (!stop) { |
| 4601 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4602 | 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] | 4603 | } |
| 4604 | |
| 4605 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4607 | return 1; |
| 4608 | } |
| 4609 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4610 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4611 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4612 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4613 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4614 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4615 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4616 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4617 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4618 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4620 | } |
| 4621 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4622 | /* If expected data not yet available, it returns a yield. This function |
| 4623 | * consumes the data in the buffer. It returns a string containing the |
| 4624 | * data. This string can be empty. |
| 4625 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4626 | __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] | 4627 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4628 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4629 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4630 | struct channel *req = si_oc(si); |
| 4631 | struct htx *htx; |
| 4632 | struct htx_blk *blk; |
| 4633 | size_t count; |
| 4634 | int len; |
| 4635 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4636 | htx = htx_from_buf(&req->buf); |
| 4637 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4638 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4639 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4640 | while (count && len && blk) { |
| 4641 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4642 | uint32_t sz = htx_get_blksz(blk); |
| 4643 | struct ist v; |
| 4644 | uint32_t vlen; |
| 4645 | |
| 4646 | vlen = sz; |
| 4647 | if (len > 0 && vlen > len) |
| 4648 | vlen = len; |
| 4649 | if (vlen > count) { |
| 4650 | if (type != HTX_BLK_DATA) |
| 4651 | break; |
| 4652 | vlen = count; |
| 4653 | } |
| 4654 | |
| 4655 | switch (type) { |
| 4656 | case HTX_BLK_UNUSED: |
| 4657 | break; |
| 4658 | |
| 4659 | case HTX_BLK_DATA: |
| 4660 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4661 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4662 | break; |
| 4663 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4664 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4665 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4666 | len = 0; |
| 4667 | break; |
| 4668 | |
| 4669 | default: |
| 4670 | break; |
| 4671 | } |
| 4672 | |
| 4673 | co_set_data(req, co_data(req) - vlen); |
| 4674 | count -= vlen; |
| 4675 | if (len > 0) |
| 4676 | len -= vlen; |
| 4677 | if (sz == vlen) |
| 4678 | blk = htx_remove_blk(htx, blk); |
| 4679 | else { |
| 4680 | htx_cut_data_blk(htx, blk, vlen); |
| 4681 | break; |
| 4682 | } |
| 4683 | } |
| 4684 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4685 | /* The message was fully consumed and no more data are expected |
| 4686 | * (EOM flag set). |
| 4687 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4688 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4689 | len = 0; |
| 4690 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4691 | htx_to_buf(htx, &req->buf); |
| 4692 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4693 | /* If we are no other data available, yield waiting for new data. */ |
| 4694 | if (len) { |
| 4695 | if (len > 0) { |
| 4696 | lua_pushinteger(L, len); |
| 4697 | lua_replace(L, 2); |
| 4698 | } |
| 4699 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4700 | 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] | 4701 | } |
| 4702 | |
| 4703 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4704 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4705 | return 1; |
| 4706 | } |
| 4707 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4708 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4709 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4710 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4711 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4712 | int len = -1; |
| 4713 | |
| 4714 | /* Check arguments. */ |
| 4715 | if (lua_gettop(L) > 2) |
| 4716 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4717 | if (lua_gettop(L) >= 2) { |
| 4718 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4719 | lua_pop(L, 1); |
| 4720 | } |
| 4721 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4722 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4723 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4724 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4725 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4726 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4727 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | /* Append data in the output side of the buffer. This data is immediately |
| 4731 | * sent. The function returns the amount of data written. If the buffer |
| 4732 | * cannot contain the data, the function yields. The function returns -1 |
| 4733 | * if the channel is closed. |
| 4734 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4735 | __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] | 4736 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4737 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4738 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4739 | struct channel *res = si_ic(si); |
| 4740 | struct htx *htx = htx_from_buf(&res->buf); |
| 4741 | const char *data; |
| 4742 | size_t len; |
| 4743 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4744 | int max; |
| 4745 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4746 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4747 | if (!max) |
| 4748 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4749 | |
| 4750 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4751 | |
| 4752 | /* Get the max amount of data which can write as input in the channel. */ |
| 4753 | if (max > (len - l)) |
| 4754 | max = len - l; |
| 4755 | |
| 4756 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4757 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4758 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4759 | |
| 4760 | /* update counters. */ |
| 4761 | l += max; |
| 4762 | lua_pop(L, 1); |
| 4763 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4764 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4765 | /* If some data is not send, declares the situation to the |
| 4766 | * applet, and returns a yield. |
| 4767 | */ |
| 4768 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4769 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4770 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4771 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4772 | 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] | 4773 | } |
| 4774 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4775 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4776 | return 1; |
| 4777 | } |
| 4778 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4779 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4780 | * yield the LUA process, and resume it without checking the |
| 4781 | * input arguments. |
| 4782 | */ |
| 4783 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4784 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4785 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4786 | |
| 4787 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4788 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4789 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4790 | WILL_LJMP(lua_error(L)); |
| 4791 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4792 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4793 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4794 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4795 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4796 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4800 | { |
| 4801 | const char *name; |
| 4802 | int ret; |
| 4803 | |
| 4804 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4805 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4806 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4807 | |
| 4808 | /* Push in the stack the "response" entry. */ |
| 4809 | ret = lua_getfield(L, 1, "response"); |
| 4810 | if (ret != LUA_TTABLE) { |
| 4811 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4812 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4813 | WILL_LJMP(lua_error(L)); |
| 4814 | } |
| 4815 | |
| 4816 | /* check if the header is already registered if it is not |
| 4817 | * the case, register it. |
| 4818 | */ |
| 4819 | ret = lua_getfield(L, -1, name); |
| 4820 | if (ret == LUA_TNIL) { |
| 4821 | |
| 4822 | /* Entry not found. */ |
| 4823 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4824 | |
| 4825 | /* Insert the new header name in the array in the top of the stack. |
| 4826 | * It left the new array in the top of the stack. |
| 4827 | */ |
| 4828 | lua_newtable(L); |
| 4829 | lua_pushvalue(L, 2); |
| 4830 | lua_pushvalue(L, -2); |
| 4831 | lua_settable(L, -4); |
| 4832 | |
| 4833 | } else if (ret != LUA_TTABLE) { |
| 4834 | |
| 4835 | /* corruption error. */ |
| 4836 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4837 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4838 | WILL_LJMP(lua_error(L)); |
| 4839 | } |
| 4840 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4841 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4842 | * the header value as new entry. |
| 4843 | */ |
| 4844 | lua_pushvalue(L, 3); |
| 4845 | ret = lua_rawlen(L, -2); |
| 4846 | lua_rawseti(L, -2, ret + 1); |
| 4847 | lua_pushboolean(L, 1); |
| 4848 | return 1; |
| 4849 | } |
| 4850 | |
| 4851 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4852 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4853 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4854 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4855 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4856 | |
| 4857 | if (status < 100 || status > 599) { |
| 4858 | lua_pushboolean(L, 0); |
| 4859 | return 1; |
| 4860 | } |
| 4861 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4862 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4863 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4864 | lua_pushboolean(L, 1); |
| 4865 | return 1; |
| 4866 | } |
| 4867 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4868 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4869 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4870 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4871 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4872 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4873 | struct channel *res = si_ic(si); |
| 4874 | struct htx *htx; |
| 4875 | struct htx_sl *sl; |
| 4876 | struct h1m h1m; |
| 4877 | const char *status, *reason; |
| 4878 | const char *name, *value; |
| 4879 | size_t nlen, vlen; |
| 4880 | unsigned int flags; |
| 4881 | |
| 4882 | /* Send the message at once. */ |
| 4883 | htx = htx_from_buf(&res->buf); |
| 4884 | h1m_init_res(&h1m); |
| 4885 | |
| 4886 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4887 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4888 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4889 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4890 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4891 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4892 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4893 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4894 | } |
| 4895 | else { |
| 4896 | flags = HTX_SL_F_IS_RESP; |
| 4897 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4898 | } |
| 4899 | if (!sl) { |
| 4900 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4901 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4902 | WILL_LJMP(lua_error(L)); |
| 4903 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4904 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4905 | |
| 4906 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4907 | lua_pushvalue(L, 0); |
| 4908 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4909 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4910 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4911 | WILL_LJMP(lua_error(L)); |
| 4912 | } |
| 4913 | |
| 4914 | /* Browse the list of headers. */ |
| 4915 | lua_pushnil(L); |
| 4916 | while(lua_next(L, -2) != 0) { |
| 4917 | /* We expect a string as -2. */ |
| 4918 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4919 | 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] | 4920 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4921 | lua_typename(L, lua_type(L, -2))); |
| 4922 | WILL_LJMP(lua_error(L)); |
| 4923 | } |
| 4924 | name = lua_tolstring(L, -2, &nlen); |
| 4925 | |
| 4926 | /* We expect an array as -1. */ |
| 4927 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4928 | 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] | 4929 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4930 | name, |
| 4931 | lua_typename(L, lua_type(L, -1))); |
| 4932 | WILL_LJMP(lua_error(L)); |
| 4933 | } |
| 4934 | |
| 4935 | /* Browse the table who is on the top of the stack. */ |
| 4936 | lua_pushnil(L); |
| 4937 | while(lua_next(L, -2) != 0) { |
| 4938 | int id; |
| 4939 | |
| 4940 | /* We expect a number as -2. */ |
| 4941 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4942 | 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] | 4943 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4944 | name, |
| 4945 | lua_typename(L, lua_type(L, -2))); |
| 4946 | WILL_LJMP(lua_error(L)); |
| 4947 | } |
| 4948 | id = lua_tointeger(L, -2); |
| 4949 | |
| 4950 | /* We expect a string as -2. */ |
| 4951 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4952 | 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] | 4953 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4954 | name, id, |
| 4955 | lua_typename(L, lua_type(L, -1))); |
| 4956 | WILL_LJMP(lua_error(L)); |
| 4957 | } |
| 4958 | value = lua_tolstring(L, -1, &vlen); |
| 4959 | |
| 4960 | /* Simple Protocol checks. */ |
| 4961 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4962 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4963 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4964 | struct ist v = ist2(value, vlen); |
| 4965 | int ret; |
| 4966 | |
| 4967 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4968 | if (ret < 0) { |
| 4969 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4970 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4971 | name); |
| 4972 | WILL_LJMP(lua_error(L)); |
| 4973 | } |
| 4974 | else if (ret == 0) |
| 4975 | goto next; /* Skip it */ |
| 4976 | } |
| 4977 | |
| 4978 | /* Add a new header */ |
| 4979 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4980 | 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] | 4981 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4982 | name); |
| 4983 | WILL_LJMP(lua_error(L)); |
| 4984 | } |
| 4985 | next: |
| 4986 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4987 | lua_pop(L, 1); |
| 4988 | } |
| 4989 | |
| 4990 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4991 | lua_pop(L, 1); |
| 4992 | } |
| 4993 | |
| 4994 | if (h1m.flags & H1_MF_CHNK) |
| 4995 | h1m.flags &= ~H1_MF_CLEN; |
| 4996 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4997 | h1m.flags |= H1_MF_XFER_LEN; |
| 4998 | |
| 4999 | /* Uset HTX start-line flags */ |
| 5000 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5001 | flags |= HTX_SL_F_XFER_ENC; |
| 5002 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5003 | flags |= HTX_SL_F_XFER_LEN; |
| 5004 | if (h1m.flags & H1_MF_CHNK) |
| 5005 | flags |= HTX_SL_F_CHNK; |
| 5006 | else if (h1m.flags & H1_MF_CLEN) |
| 5007 | flags |= HTX_SL_F_CLEN; |
| 5008 | if (h1m.body_len == 0) |
| 5009 | flags |= HTX_SL_F_BODYLESS; |
| 5010 | } |
| 5011 | sl->flags |= flags; |
| 5012 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5013 | /* 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] | 5014 | * and the status code implies the presence of a message body, we must |
| 5015 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5016 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5017 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5018 | */ |
| 5019 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5020 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5021 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5022 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5023 | /* Add a new header */ |
| 5024 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5025 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5026 | 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] | 5027 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5028 | WILL_LJMP(lua_error(L)); |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | /* Finalize headers. */ |
| 5033 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5034 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5035 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5036 | WILL_LJMP(lua_error(L)); |
| 5037 | } |
| 5038 | |
| 5039 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5040 | b_reset(&res->buf); |
| 5041 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5042 | WILL_LJMP(lua_error(L)); |
| 5043 | } |
| 5044 | |
| 5045 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5046 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5047 | |
| 5048 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5049 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5050 | return 0; |
| 5051 | |
| 5052 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5053 | /* We will build the status line and the headers of the HTTP response. |
| 5054 | * We will try send at once if its not possible, we give back the hand |
| 5055 | * waiting for more room. |
| 5056 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5057 | __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] | 5058 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5059 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5060 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | struct channel *res = si_ic(si); |
| 5062 | |
| 5063 | if (co_data(res)) { |
| 5064 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5065 | 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] | 5066 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5067 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5071 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5072 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5073 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5074 | } |
| 5075 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5076 | /* |
| 5077 | * |
| 5078 | * |
| 5079 | * Class HTTP |
| 5080 | * |
| 5081 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5082 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5083 | |
| 5084 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5085 | * a class stream, otherwise it throws an error. |
| 5086 | */ |
| 5087 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5088 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5089 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5090 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5091 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5092 | /* This function creates and push in the stack a HTTP object |
| 5093 | * according with a current TXN. |
| 5094 | */ |
| 5095 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5096 | { |
| 5097 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5098 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5099 | /* Check stack size. */ |
| 5100 | if (!lua_checkstack(L, 3)) |
| 5101 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5102 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5103 | /* Create the object: obj[0] = userdata. |
| 5104 | * Note that the base of the Converters object is the |
| 5105 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5106 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5107 | lua_newtable(L); |
| 5108 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5109 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5110 | |
| 5111 | htxn->s = txn->s; |
| 5112 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5113 | htxn->dir = txn->dir; |
| 5114 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5115 | |
| 5116 | /* Pop a class stream metatable and affect it to the table. */ |
| 5117 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5118 | lua_setmetatable(L, -2); |
| 5119 | |
| 5120 | return 1; |
| 5121 | } |
| 5122 | |
| 5123 | /* This function creates ans returns an array of HTTP headers. |
| 5124 | * This function does not fails. It is used as wrapper with the |
| 5125 | * 2 following functions. |
| 5126 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5127 | __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] | 5128 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5129 | struct htx *htx; |
| 5130 | int32_t pos; |
| 5131 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5132 | /* Create the table. */ |
| 5133 | lua_newtable(L); |
| 5134 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5135 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5136 | htx = htxbuf(&msg->chn->buf); |
| 5137 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5138 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5139 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5140 | struct ist n, v; |
| 5141 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5142 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5143 | if (type == HTX_BLK_HDR) { |
| 5144 | n = htx_get_blk_name(htx,blk); |
| 5145 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5146 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5147 | else if (type == HTX_BLK_EOH) |
| 5148 | break; |
| 5149 | else |
| 5150 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5151 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5152 | /* Check for existing entry: |
| 5153 | * assume that the table is on the top of the stack, and |
| 5154 | * push the key in the stack, the function lua_gettable() |
| 5155 | * perform the lookup. |
| 5156 | */ |
| 5157 | lua_pushlstring(L, n.ptr, n.len); |
| 5158 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5159 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5160 | switch (lua_type(L, -1)) { |
| 5161 | case LUA_TNIL: |
| 5162 | /* Table not found, create it. */ |
| 5163 | lua_pop(L, 1); /* remove the nil value. */ |
| 5164 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5165 | lua_newtable(L); /* create and push empty table. */ |
| 5166 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5167 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5168 | 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] | 5169 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5171 | case LUA_TTABLE: |
| 5172 | /* Entry found: push the value in the table. */ |
| 5173 | len = lua_rawlen(L, -1); |
| 5174 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5175 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5176 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5177 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5178 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5179 | default: |
| 5180 | /* Other cases are errors. */ |
| 5181 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5182 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | } |
| 5184 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5185 | return 1; |
| 5186 | } |
| 5187 | |
| 5188 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5189 | { |
| 5190 | struct hlua_txn *htxn; |
| 5191 | |
| 5192 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5193 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5194 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5195 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5196 | WILL_LJMP(lua_error(L)); |
| 5197 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5198 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5202 | { |
| 5203 | struct hlua_txn *htxn; |
| 5204 | |
| 5205 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5206 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5207 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5208 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5209 | WILL_LJMP(lua_error(L)); |
| 5210 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5211 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | /* This function replace full header, or just a value in |
| 5215 | * the request or in the response. It is a wrapper fir the |
| 5216 | * 4 following functions. |
| 5217 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5218 | __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] | 5219 | { |
| 5220 | size_t name_len; |
| 5221 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5222 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5223 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5224 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5225 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5226 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5227 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5228 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5229 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5230 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5231 | 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] | 5232 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5233 | return 0; |
| 5234 | } |
| 5235 | |
| 5236 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5237 | { |
| 5238 | struct hlua_txn *htxn; |
| 5239 | |
| 5240 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5241 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5242 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5243 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5244 | WILL_LJMP(lua_error(L)); |
| 5245 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5246 | 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] | 5247 | } |
| 5248 | |
| 5249 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5250 | { |
| 5251 | struct hlua_txn *htxn; |
| 5252 | |
| 5253 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5254 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5255 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5256 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5257 | WILL_LJMP(lua_error(L)); |
| 5258 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5259 | 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] | 5260 | } |
| 5261 | |
| 5262 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5263 | { |
| 5264 | struct hlua_txn *htxn; |
| 5265 | |
| 5266 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5267 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5268 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5269 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5270 | WILL_LJMP(lua_error(L)); |
| 5271 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5272 | 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] | 5273 | } |
| 5274 | |
| 5275 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5276 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5277 | struct hlua_txn *htxn; |
| 5278 | |
| 5279 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5280 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5281 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5282 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5283 | WILL_LJMP(lua_error(L)); |
| 5284 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5285 | 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] | 5286 | } |
| 5287 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5288 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5289 | * It is a wrapper for the 2 following functions. |
| 5290 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5291 | __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] | 5292 | { |
| 5293 | size_t len; |
| 5294 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5295 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5296 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5297 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5298 | ctx.blk = NULL; |
| 5299 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5300 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5301 | return 0; |
| 5302 | } |
| 5303 | |
| 5304 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5305 | { |
| 5306 | struct hlua_txn *htxn; |
| 5307 | |
| 5308 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5309 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5310 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5311 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5312 | WILL_LJMP(lua_error(L)); |
| 5313 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5314 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5318 | { |
| 5319 | struct hlua_txn *htxn; |
| 5320 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5321 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5322 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5323 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5324 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5325 | WILL_LJMP(lua_error(L)); |
| 5326 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5327 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | /* This function adds an header. It is a wrapper used by |
| 5331 | * the 2 following functions. |
| 5332 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5333 | __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] | 5334 | { |
| 5335 | size_t name_len; |
| 5336 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5337 | size_t value_len; |
| 5338 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5339 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5340 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5341 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5342 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5343 | return 0; |
| 5344 | } |
| 5345 | |
| 5346 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5347 | { |
| 5348 | struct hlua_txn *htxn; |
| 5349 | |
| 5350 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5351 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5352 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5353 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5354 | WILL_LJMP(lua_error(L)); |
| 5355 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5356 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5360 | { |
| 5361 | struct hlua_txn *htxn; |
| 5362 | |
| 5363 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5364 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5365 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5366 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5367 | WILL_LJMP(lua_error(L)); |
| 5368 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5369 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5373 | { |
| 5374 | struct hlua_txn *htxn; |
| 5375 | |
| 5376 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5377 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5378 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5379 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5380 | WILL_LJMP(lua_error(L)); |
| 5381 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5382 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5383 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5387 | { |
| 5388 | struct hlua_txn *htxn; |
| 5389 | |
| 5390 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5391 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5392 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5393 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5394 | WILL_LJMP(lua_error(L)); |
| 5395 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5396 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5397 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | /* This function set the method. */ |
| 5401 | static int hlua_http_req_set_meth(lua_State *L) |
| 5402 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5403 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5404 | size_t name_len; |
| 5405 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5406 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5407 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5408 | WILL_LJMP(lua_error(L)); |
| 5409 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5410 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5411 | return 1; |
| 5412 | } |
| 5413 | |
| 5414 | /* This function set the method. */ |
| 5415 | static int hlua_http_req_set_path(lua_State *L) |
| 5416 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5417 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5418 | size_t name_len; |
| 5419 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5420 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5421 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5422 | WILL_LJMP(lua_error(L)); |
| 5423 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5424 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5425 | return 1; |
| 5426 | } |
| 5427 | |
| 5428 | /* This function set the query-string. */ |
| 5429 | static int hlua_http_req_set_query(lua_State *L) |
| 5430 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5431 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5432 | size_t name_len; |
| 5433 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5434 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5435 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5436 | WILL_LJMP(lua_error(L)); |
| 5437 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5438 | /* Check length. */ |
| 5439 | if (name_len > trash.size - 1) { |
| 5440 | lua_pushboolean(L, 0); |
| 5441 | return 1; |
| 5442 | } |
| 5443 | |
| 5444 | /* Add the mark question as prefix. */ |
| 5445 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5446 | trash.area[trash.data++] = '?'; |
| 5447 | memcpy(trash.area + trash.data, name, name_len); |
| 5448 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5449 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5450 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5451 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5452 | return 1; |
| 5453 | } |
| 5454 | |
| 5455 | /* This function set the uri. */ |
| 5456 | static int hlua_http_req_set_uri(lua_State *L) |
| 5457 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5458 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5459 | size_t name_len; |
| 5460 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5461 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5462 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5463 | WILL_LJMP(lua_error(L)); |
| 5464 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5465 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5466 | return 1; |
| 5467 | } |
| 5468 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5469 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5470 | static int hlua_http_res_set_status(lua_State *L) |
| 5471 | { |
| 5472 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5473 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5474 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5475 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5476 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5477 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5478 | WILL_LJMP(lua_error(L)); |
| 5479 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5480 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5481 | return 0; |
| 5482 | } |
| 5483 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5484 | /* |
| 5485 | * |
| 5486 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5487 | * Class TXN |
| 5488 | * |
| 5489 | * |
| 5490 | */ |
| 5491 | |
| 5492 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5493 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5494 | */ |
| 5495 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5496 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5497 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5498 | } |
| 5499 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5500 | __LJMP static int hlua_set_var(lua_State *L) |
| 5501 | { |
| 5502 | struct hlua_txn *htxn; |
| 5503 | const char *name; |
| 5504 | size_t len; |
| 5505 | struct sample smp; |
| 5506 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5507 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5508 | 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] | 5509 | |
| 5510 | /* It is useles to retrieve the stream, but this function |
| 5511 | * runs only in a stream context. |
| 5512 | */ |
| 5513 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5514 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5515 | |
| 5516 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5517 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5518 | hlua_lua2smp(L, 3, &smp); |
| 5519 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5520 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5521 | * already takes care of duplicating dynamic var data. |
| 5522 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5523 | 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] | 5524 | |
| 5525 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5526 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5527 | else |
| 5528 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5529 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5530 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5531 | } |
| 5532 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5533 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5534 | { |
| 5535 | struct hlua_txn *htxn; |
| 5536 | const char *name; |
| 5537 | size_t len; |
| 5538 | struct sample smp; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5541 | |
| 5542 | /* It is useles to retrieve the stream, but this function |
| 5543 | * runs only in a stream context. |
| 5544 | */ |
| 5545 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5546 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5547 | |
| 5548 | /* Unset the variable. */ |
| 5549 | 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] | 5550 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5551 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5552 | } |
| 5553 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5554 | __LJMP static int hlua_get_var(lua_State *L) |
| 5555 | { |
| 5556 | struct hlua_txn *htxn; |
| 5557 | const char *name; |
| 5558 | size_t len; |
| 5559 | struct sample smp; |
| 5560 | |
| 5561 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5562 | |
| 5563 | /* It is useles to retrieve the stream, but this function |
| 5564 | * runs only in a stream context. |
| 5565 | */ |
| 5566 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5567 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5568 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5569 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 5570 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5571 | lua_pushnil(L); |
| 5572 | return 1; |
| 5573 | } |
| 5574 | |
| 5575 | return hlua_smp2lua(L, &smp); |
| 5576 | } |
| 5577 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5578 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5579 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5580 | struct hlua *hlua; |
| 5581 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5582 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5583 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5584 | /* It is useles to retrieve the stream, but this function |
| 5585 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5586 | */ |
| 5587 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5588 | |
| 5589 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5590 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5591 | if (!hlua) |
| 5592 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5593 | |
| 5594 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5595 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5596 | |
| 5597 | /* Get and store new value. */ |
| 5598 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5599 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5600 | |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5604 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5605 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5606 | struct hlua *hlua; |
| 5607 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5608 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5609 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5610 | /* It is useles to retrieve the stream, but this function |
| 5611 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5612 | */ |
| 5613 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5614 | |
| 5615 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5616 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5617 | if (!hlua) { |
| 5618 | lua_pushnil(L); |
| 5619 | return 1; |
| 5620 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5621 | |
| 5622 | /* Push configuration index in the stack. */ |
| 5623 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5624 | |
| 5625 | return 1; |
| 5626 | } |
| 5627 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5628 | /* Create stack entry containing a class TXN. This function |
| 5629 | * return 0 if the stack does not contains free slots, |
| 5630 | * otherwise it returns 1. |
| 5631 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5632 | 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] | 5633 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5634 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5635 | |
| 5636 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5637 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5638 | return 0; |
| 5639 | |
| 5640 | /* NOTE: The allocation never fails. The failure |
| 5641 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5642 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5643 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5644 | /* Create the object: obj[0] = userdata. */ |
| 5645 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5646 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5647 | lua_rawseti(L, -2, 0); |
| 5648 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5649 | htxn->s = s; |
| 5650 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5651 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5652 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5653 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5654 | /* Create the "f" field that contains a list of fetches. */ |
| 5655 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5656 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5657 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5658 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5659 | |
| 5660 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5661 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5662 | 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] | 5663 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5664 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5665 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5666 | /* Create the "c" field that contains a list of converters. */ |
| 5667 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5668 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5669 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5670 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5671 | |
| 5672 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5673 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5674 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5675 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5676 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5677 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5678 | /* Create the "req" field that contains the request channel object. */ |
| 5679 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5680 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5681 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5682 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5683 | |
| 5684 | /* Create the "res" field that contains the response channel object. */ |
| 5685 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5686 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5687 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5688 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5689 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5690 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5691 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5692 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5693 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5694 | return 0; |
| 5695 | } |
| 5696 | else |
| 5697 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5698 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5699 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5700 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5701 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5702 | lua_setmetatable(L, -2); |
| 5703 | |
| 5704 | return 1; |
| 5705 | } |
| 5706 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5707 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5708 | { |
| 5709 | const char *msg; |
| 5710 | struct hlua_txn *htxn; |
| 5711 | |
| 5712 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5713 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5714 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5715 | |
| 5716 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5717 | return 0; |
| 5718 | } |
| 5719 | |
| 5720 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5721 | { |
| 5722 | int level; |
| 5723 | const char *msg; |
| 5724 | struct hlua_txn *htxn; |
| 5725 | |
| 5726 | MAY_LJMP(check_args(L, 3, "log")); |
| 5727 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5728 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5729 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5730 | |
| 5731 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5732 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5733 | |
| 5734 | hlua_sendlog(htxn->s->be, level, msg); |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
| 5738 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5739 | { |
| 5740 | const char *msg; |
| 5741 | struct hlua_txn *htxn; |
| 5742 | |
| 5743 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5744 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5745 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5746 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5747 | return 0; |
| 5748 | } |
| 5749 | |
| 5750 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5751 | { |
| 5752 | const char *msg; |
| 5753 | struct hlua_txn *htxn; |
| 5754 | |
| 5755 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5756 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5757 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5758 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5759 | return 0; |
| 5760 | } |
| 5761 | |
| 5762 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5763 | { |
| 5764 | const char *msg; |
| 5765 | struct hlua_txn *htxn; |
| 5766 | |
| 5767 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5768 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5769 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5770 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5771 | return 0; |
| 5772 | } |
| 5773 | |
| 5774 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5775 | { |
| 5776 | const char *msg; |
| 5777 | struct hlua_txn *htxn; |
| 5778 | |
| 5779 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5780 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5781 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5782 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5783 | return 0; |
| 5784 | } |
| 5785 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5786 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5787 | { |
| 5788 | struct hlua_txn *htxn; |
| 5789 | int ll; |
| 5790 | |
| 5791 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5792 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5793 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5794 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5795 | if (ll < -1 || ll > NB_LOG_LEVELS) |
| 5796 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be one of the following value:" |
| 5797 | " core.silent(-1), core.emerg(0), core.alert(1), core.crit(2), core.error(3)," |
| 5798 | " 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] | 5799 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5800 | htxn->s->logs.level = (ll == -1) ? ll : ll + 1; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5801 | return 0; |
| 5802 | } |
| 5803 | |
| 5804 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5805 | { |
| 5806 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5807 | int tos; |
| 5808 | |
| 5809 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5810 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5811 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5812 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5813 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5814 | return 0; |
| 5815 | } |
| 5816 | |
| 5817 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5818 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5819 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5820 | int mark; |
| 5821 | |
| 5822 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5823 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5824 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5825 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5826 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5827 | return 0; |
| 5828 | } |
| 5829 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5830 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5831 | { |
| 5832 | struct hlua_txn *htxn; |
| 5833 | |
| 5834 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5835 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5836 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5837 | return 0; |
| 5838 | } |
| 5839 | |
| 5840 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5841 | { |
| 5842 | struct hlua_txn *htxn; |
| 5843 | |
| 5844 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5845 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5846 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5847 | return 0; |
| 5848 | } |
| 5849 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5850 | /* 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] | 5851 | * 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] | 5852 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5853 | * error. The Reply must be on top of the stack. |
| 5854 | */ |
| 5855 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5856 | { |
| 5857 | struct htx *htx; |
| 5858 | struct htx_sl *sl; |
| 5859 | struct h1m h1m; |
| 5860 | const char *status, *reason, *body; |
| 5861 | size_t status_len, reason_len, body_len; |
| 5862 | int ret, code, flags; |
| 5863 | |
| 5864 | code = 200; |
| 5865 | status = "200"; |
| 5866 | status_len = 3; |
| 5867 | ret = lua_getfield(L, -1, "status"); |
| 5868 | if (ret == LUA_TNUMBER) { |
| 5869 | code = lua_tointeger(L, -1); |
| 5870 | status = lua_tolstring(L, -1, &status_len); |
| 5871 | } |
| 5872 | lua_pop(L, 1); |
| 5873 | |
| 5874 | reason = http_get_reason(code); |
| 5875 | reason_len = strlen(reason); |
| 5876 | ret = lua_getfield(L, -1, "reason"); |
| 5877 | if (ret == LUA_TSTRING) |
| 5878 | reason = lua_tolstring(L, -1, &reason_len); |
| 5879 | lua_pop(L, 1); |
| 5880 | |
| 5881 | body = NULL; |
| 5882 | body_len = 0; |
| 5883 | ret = lua_getfield(L, -1, "body"); |
| 5884 | if (ret == LUA_TSTRING) |
| 5885 | body = lua_tolstring(L, -1, &body_len); |
| 5886 | lua_pop(L, 1); |
| 5887 | |
| 5888 | /* Prepare the response before inserting the headers */ |
| 5889 | h1m_init_res(&h1m); |
| 5890 | htx = htx_from_buf(&s->res.buf); |
| 5891 | channel_htx_truncate(&s->res, htx); |
| 5892 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5893 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5894 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5895 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5896 | } |
| 5897 | else { |
| 5898 | flags = HTX_SL_F_IS_RESP; |
| 5899 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5900 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5901 | } |
| 5902 | if (!sl) |
| 5903 | goto fail; |
| 5904 | sl->info.res.status = code; |
| 5905 | |
| 5906 | /* Push in the stack the "headers" entry. */ |
| 5907 | ret = lua_getfield(L, -1, "headers"); |
| 5908 | if (ret != LUA_TTABLE) |
| 5909 | goto skip_headers; |
| 5910 | |
| 5911 | lua_pushnil(L); |
| 5912 | while (lua_next(L, -2) != 0) { |
| 5913 | struct ist name, value; |
| 5914 | const char *n, *v; |
| 5915 | size_t nlen, vlen; |
| 5916 | |
| 5917 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5918 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5919 | goto next_hdr; |
| 5920 | } |
| 5921 | |
| 5922 | n = lua_tolstring(L, -2, &nlen); |
| 5923 | name = ist2(n, nlen); |
| 5924 | if (isteqi(name, ist("content-length"))) { |
| 5925 | /* Always skip content-length header. It will be added |
| 5926 | * later with the correct len |
| 5927 | */ |
| 5928 | goto next_hdr; |
| 5929 | } |
| 5930 | |
| 5931 | /* Loop on header's values */ |
| 5932 | lua_pushnil(L); |
| 5933 | while (lua_next(L, -2)) { |
| 5934 | if (!lua_isstring(L, -1)) { |
| 5935 | /* Skip the value if it is not a string */ |
| 5936 | goto next_value; |
| 5937 | } |
| 5938 | |
| 5939 | v = lua_tolstring(L, -1, &vlen); |
| 5940 | value = ist2(v, vlen); |
| 5941 | |
| 5942 | if (isteqi(name, ist("transfer-encoding"))) |
| 5943 | h1_parse_xfer_enc_header(&h1m, value); |
| 5944 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5945 | goto fail; |
| 5946 | |
| 5947 | next_value: |
| 5948 | lua_pop(L, 1); |
| 5949 | } |
| 5950 | |
| 5951 | next_hdr: |
| 5952 | lua_pop(L, 1); |
| 5953 | } |
| 5954 | skip_headers: |
| 5955 | lua_pop(L, 1); |
| 5956 | |
| 5957 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5958 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5959 | const char *clen = ultoa(body_len); |
| 5960 | |
| 5961 | h1m.flags |= H1_MF_CLEN; |
| 5962 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5963 | goto fail; |
| 5964 | } |
| 5965 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5966 | h1m.flags |= H1_MF_XFER_LEN; |
| 5967 | |
| 5968 | /* Update HTX start-line flags */ |
| 5969 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5970 | flags |= HTX_SL_F_XFER_ENC; |
| 5971 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5972 | flags |= HTX_SL_F_XFER_LEN; |
| 5973 | if (h1m.flags & H1_MF_CHNK) |
| 5974 | flags |= HTX_SL_F_CHNK; |
| 5975 | else if (h1m.flags & H1_MF_CLEN) |
| 5976 | flags |= HTX_SL_F_CLEN; |
| 5977 | if (h1m.body_len == 0) |
| 5978 | flags |= HTX_SL_F_BODYLESS; |
| 5979 | } |
| 5980 | sl->flags |= flags; |
| 5981 | |
| 5982 | |
| 5983 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5984 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5985 | goto fail; |
| 5986 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5987 | htx->flags |= HTX_FL_EOM; |
| 5988 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5989 | /* Now, forward the response and terminate the transaction */ |
| 5990 | s->txn->status = code; |
| 5991 | htx_to_buf(htx, &s->res.buf); |
| 5992 | if (!http_forward_proxy_resp(s, 1)) |
| 5993 | goto fail; |
| 5994 | |
| 5995 | return 1; |
| 5996 | |
| 5997 | fail: |
| 5998 | channel_htx_truncate(&s->res, htx); |
| 5999 | return 0; |
| 6000 | } |
| 6001 | |
| 6002 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 6003 | * processing is just aborted. Nothing is returned to the client and all |
| 6004 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 6005 | * is forwarded to the client before terminating the transaction. On success, |
| 6006 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 6007 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 6008 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6009 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6010 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6011 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6012 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6013 | struct stream *s; |
| 6014 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6015 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6016 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6017 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6018 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 6019 | * just end the execution of the current lua code. */ |
| 6020 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6021 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6022 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6023 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6024 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6025 | struct channel *req = &s->req; |
| 6026 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6027 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6028 | channel_auto_read(req); |
| 6029 | channel_abort(req); |
| 6030 | channel_auto_close(req); |
| 6031 | channel_erase(req); |
| 6032 | |
| 6033 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 6034 | channel_auto_read(res); |
| 6035 | channel_auto_close(res); |
| 6036 | channel_shutr_now(res); |
| 6037 | |
| 6038 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 6039 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6040 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6041 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6042 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 6043 | /* No reply or invalid reply */ |
| 6044 | s->txn->status = 0; |
| 6045 | http_reply_and_close(s, 0, NULL); |
| 6046 | } |
| 6047 | else { |
| 6048 | /* Remove extra args to have the reply on top of the stack */ |
| 6049 | if (lua_gettop(L) > 2) |
| 6050 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6051 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6052 | if (!hlua_txn_forward_reply(L, s)) { |
| 6053 | if (!(s->flags & SF_ERR_MASK)) |
| 6054 | s->flags |= SF_ERR_PRXCOND; |
| 6055 | lua_pushinteger(L, ACT_RET_ERR); |
| 6056 | WILL_LJMP(hlua_done(L)); |
| 6057 | return 0; /* Never reached */ |
| 6058 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6059 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6060 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6061 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 6062 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 6063 | /* let's log the request time */ |
| 6064 | s->logs.tv_request = now; |
| 6065 | 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] | 6066 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6067 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6068 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6069 | done: |
| 6070 | if (!(s->flags & SF_ERR_MASK)) |
| 6071 | s->flags |= SF_ERR_LOCAL; |
| 6072 | if (!(s->flags & SF_FINST_MASK)) |
| 6073 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6074 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 6075 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6076 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6077 | return 0; |
| 6078 | } |
| 6079 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6080 | /* |
| 6081 | * |
| 6082 | * |
| 6083 | * Class REPLY |
| 6084 | * |
| 6085 | * |
| 6086 | */ |
| 6087 | |
| 6088 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 6089 | * free slots, the function fails and returns 0; |
| 6090 | */ |
| 6091 | static int hlua_txn_reply_new(lua_State *L) |
| 6092 | { |
| 6093 | struct hlua_txn *htxn; |
| 6094 | const char *reason, *body = NULL; |
| 6095 | int ret, status; |
| 6096 | |
| 6097 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6098 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6099 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6100 | WILL_LJMP(lua_error(L)); |
| 6101 | } |
| 6102 | |
| 6103 | /* Default value */ |
| 6104 | status = 200; |
| 6105 | reason = http_get_reason(status); |
| 6106 | |
| 6107 | if (lua_istable(L, 2)) { |
| 6108 | /* load status and reason from the table argument at index 2 */ |
| 6109 | ret = lua_getfield(L, 2, "status"); |
| 6110 | if (ret == LUA_TNIL) |
| 6111 | goto reason; |
| 6112 | else if (ret != LUA_TNUMBER) { |
| 6113 | /* invalid status: ignore the reason */ |
| 6114 | goto body; |
| 6115 | } |
| 6116 | status = lua_tointeger(L, -1); |
| 6117 | |
| 6118 | reason: |
| 6119 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6120 | ret = lua_getfield(L, 2, "reason"); |
| 6121 | if (ret == LUA_TSTRING) |
| 6122 | reason = lua_tostring(L, -1); |
| 6123 | |
| 6124 | body: |
| 6125 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6126 | ret = lua_getfield(L, 2, "body"); |
| 6127 | if (ret == LUA_TSTRING) |
| 6128 | body = lua_tostring(L, -1); |
| 6129 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6130 | } |
| 6131 | |
| 6132 | /* Create the Reply table */ |
| 6133 | lua_newtable(L); |
| 6134 | |
| 6135 | /* Add status element */ |
| 6136 | lua_pushstring(L, "status"); |
| 6137 | lua_pushinteger(L, status); |
| 6138 | lua_settable(L, -3); |
| 6139 | |
| 6140 | /* Add reason element */ |
| 6141 | reason = http_get_reason(status); |
| 6142 | lua_pushstring(L, "reason"); |
| 6143 | lua_pushstring(L, reason); |
| 6144 | lua_settable(L, -3); |
| 6145 | |
| 6146 | /* Add body element, nil if undefined */ |
| 6147 | lua_pushstring(L, "body"); |
| 6148 | if (body) |
| 6149 | lua_pushstring(L, body); |
| 6150 | else |
| 6151 | lua_pushnil(L); |
| 6152 | lua_settable(L, -3); |
| 6153 | |
| 6154 | /* Add headers element */ |
| 6155 | lua_pushstring(L, "headers"); |
| 6156 | lua_newtable(L); |
| 6157 | |
| 6158 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6159 | if (lua_istable(L, 2)) { |
| 6160 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6161 | ret = lua_getfield(L, 2, "headers"); |
| 6162 | if (ret == LUA_TTABLE) { |
| 6163 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6164 | lua_pushnil(L); |
| 6165 | while (lua_next(L, -2) != 0) { |
| 6166 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6167 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6168 | /* invalid value type, skip it */ |
| 6169 | lua_pop(L, 1); |
| 6170 | continue; |
| 6171 | } |
| 6172 | |
| 6173 | |
| 6174 | /* Duplicate the key and swap it with the value. */ |
| 6175 | lua_pushvalue(L, -2); |
| 6176 | lua_insert(L, -2); |
| 6177 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6178 | |
| 6179 | lua_newtable(L); |
| 6180 | lua_insert(L, -2); |
| 6181 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6182 | |
| 6183 | if (lua_isstring(L, -1)) { |
| 6184 | /* push the value in the inner table */ |
| 6185 | lua_rawseti(L, -2, 1); |
| 6186 | } |
| 6187 | else { /* table */ |
| 6188 | lua_pushnil(L); |
| 6189 | while (lua_next(L, -2) != 0) { |
| 6190 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6191 | if (!lua_isstring(L, -1)) { |
| 6192 | /* invalid value type, skip it*/ |
| 6193 | lua_pop(L, 1); |
| 6194 | continue; |
| 6195 | } |
| 6196 | /* push the value in the inner table */ |
| 6197 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6198 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6199 | } |
| 6200 | lua_pop(L, 1); |
| 6201 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6202 | } |
| 6203 | |
| 6204 | /* push (k,v) on the stack in the headers table: |
| 6205 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6206 | */ |
| 6207 | lua_settable(L, -5); |
| 6208 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6209 | } |
| 6210 | } |
| 6211 | lua_pop(L, 1); |
| 6212 | } |
| 6213 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6214 | lua_settable(L, -3); |
| 6215 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6216 | |
| 6217 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6218 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6219 | lua_setmetatable(L, -2); |
| 6220 | return 1; |
| 6221 | } |
| 6222 | |
| 6223 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6224 | * provided, the default one corresponding to the status code is used. |
| 6225 | */ |
| 6226 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6227 | { |
| 6228 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6229 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6230 | |
| 6231 | /* First argument (self) must be a table */ |
| 6232 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6233 | |
| 6234 | if (status < 100 || status > 599) { |
| 6235 | lua_pushboolean(L, 0); |
| 6236 | return 1; |
| 6237 | } |
| 6238 | if (!reason) |
| 6239 | reason = http_get_reason(status); |
| 6240 | |
| 6241 | lua_pushinteger(L, status); |
| 6242 | lua_setfield(L, 1, "status"); |
| 6243 | |
| 6244 | lua_pushstring(L, reason); |
| 6245 | lua_setfield(L, 1, "reason"); |
| 6246 | |
| 6247 | lua_pushboolean(L, 1); |
| 6248 | return 1; |
| 6249 | } |
| 6250 | |
| 6251 | /* Add a header into the reply object. Each header name is associated to an |
| 6252 | * array of values in the "headers" table. If the header name is not found, a |
| 6253 | * new entry is created. |
| 6254 | */ |
| 6255 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6256 | { |
| 6257 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6258 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6259 | int ret; |
| 6260 | |
| 6261 | /* First argument (self) must be a table */ |
| 6262 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6263 | |
| 6264 | /* Push in the stack the "headers" entry. */ |
| 6265 | ret = lua_getfield(L, 1, "headers"); |
| 6266 | if (ret != LUA_TTABLE) { |
| 6267 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6268 | WILL_LJMP(lua_error(L)); |
| 6269 | } |
| 6270 | |
| 6271 | /* check if the header is already registered. If not, register it. */ |
| 6272 | ret = lua_getfield(L, -1, name); |
| 6273 | if (ret == LUA_TNIL) { |
| 6274 | /* Entry not found. */ |
| 6275 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6276 | |
| 6277 | /* Insert the new header name in the array in the top of the stack. |
| 6278 | * It left the new array in the top of the stack. |
| 6279 | */ |
| 6280 | lua_newtable(L); |
| 6281 | lua_pushstring(L, name); |
| 6282 | lua_pushvalue(L, -2); |
| 6283 | lua_settable(L, -4); |
| 6284 | } |
| 6285 | else if (ret != LUA_TTABLE) { |
| 6286 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6287 | WILL_LJMP(lua_error(L)); |
| 6288 | } |
| 6289 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6290 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6291 | * the header value as new entry. |
| 6292 | */ |
| 6293 | lua_pushstring(L, value); |
| 6294 | ret = lua_rawlen(L, -2); |
| 6295 | lua_rawseti(L, -2, ret + 1); |
| 6296 | |
| 6297 | lua_pushboolean(L, 1); |
| 6298 | return 1; |
| 6299 | } |
| 6300 | |
| 6301 | /* Remove all occurrences of a given header name. */ |
| 6302 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6303 | { |
| 6304 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6305 | int ret; |
| 6306 | |
| 6307 | /* First argument (self) must be a table */ |
| 6308 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6309 | |
| 6310 | /* Push in the stack the "headers" entry. */ |
| 6311 | ret = lua_getfield(L, 1, "headers"); |
| 6312 | if (ret != LUA_TTABLE) { |
| 6313 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6314 | WILL_LJMP(lua_error(L)); |
| 6315 | } |
| 6316 | |
| 6317 | lua_pushstring(L, name); |
| 6318 | lua_pushnil(L); |
| 6319 | lua_settable(L, -3); |
| 6320 | |
| 6321 | lua_pushboolean(L, 1); |
| 6322 | return 1; |
| 6323 | } |
| 6324 | |
| 6325 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6326 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6327 | { |
| 6328 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6329 | |
| 6330 | /* First argument (self) must be a table */ |
| 6331 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6332 | |
| 6333 | lua_pushstring(L, payload); |
| 6334 | lua_setfield(L, 1, "body"); |
| 6335 | |
| 6336 | lua_pushboolean(L, 1); |
| 6337 | return 1; |
| 6338 | } |
| 6339 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6340 | __LJMP static int hlua_log(lua_State *L) |
| 6341 | { |
| 6342 | int level; |
| 6343 | const char *msg; |
| 6344 | |
| 6345 | MAY_LJMP(check_args(L, 2, "log")); |
| 6346 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6347 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6348 | |
| 6349 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6350 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6351 | |
| 6352 | hlua_sendlog(NULL, level, msg); |
| 6353 | return 0; |
| 6354 | } |
| 6355 | |
| 6356 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6357 | { |
| 6358 | const char *msg; |
| 6359 | |
| 6360 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6361 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6362 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6363 | return 0; |
| 6364 | } |
| 6365 | |
| 6366 | __LJMP static int hlua_log_info(lua_State *L) |
| 6367 | { |
| 6368 | const char *msg; |
| 6369 | |
| 6370 | MAY_LJMP(check_args(L, 1, "info")); |
| 6371 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6372 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6373 | return 0; |
| 6374 | } |
| 6375 | |
| 6376 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6377 | { |
| 6378 | const char *msg; |
| 6379 | |
| 6380 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6381 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6382 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6383 | return 0; |
| 6384 | } |
| 6385 | |
| 6386 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6387 | { |
| 6388 | const char *msg; |
| 6389 | |
| 6390 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6391 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6392 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6393 | return 0; |
| 6394 | } |
| 6395 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6396 | __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] | 6397 | { |
| 6398 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6399 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6400 | 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] | 6401 | return 0; |
| 6402 | } |
| 6403 | |
| 6404 | __LJMP static int hlua_sleep(lua_State *L) |
| 6405 | { |
| 6406 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6407 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6408 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6409 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6410 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6411 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6412 | wakeup_ms = tick_add(now_ms, delay); |
| 6413 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6414 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6415 | 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] | 6416 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6417 | } |
| 6418 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6419 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6420 | { |
| 6421 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6422 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6423 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6424 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6425 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6426 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6427 | wakeup_ms = tick_add(now_ms, delay); |
| 6428 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6429 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6430 | 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] | 6431 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6432 | } |
| 6433 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6434 | /* This functionis an LUA binding. it permits to give back |
| 6435 | * the hand at the HAProxy scheduler. It is used when the |
| 6436 | * LUA processing consumes a lot of time. |
| 6437 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6438 | __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] | 6439 | { |
| 6440 | return 0; |
| 6441 | } |
| 6442 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6443 | __LJMP static int hlua_yield(lua_State *L) |
| 6444 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6445 | 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] | 6446 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6447 | } |
| 6448 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6449 | /* This function change the nice of the currently executed |
| 6450 | * task. It is used set low or high priority at the current |
| 6451 | * task. |
| 6452 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6453 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6454 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6455 | struct hlua *hlua; |
| 6456 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6457 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6458 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6459 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6460 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6461 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6462 | hlua = hlua_gethlua(L); |
| 6463 | |
| 6464 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6465 | if (!hlua || !hlua->task) |
| 6466 | return 0; |
| 6467 | |
| 6468 | if (nice < -1024) |
| 6469 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6470 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6471 | nice = 1024; |
| 6472 | |
| 6473 | hlua->task->nice = nice; |
| 6474 | return 0; |
| 6475 | } |
| 6476 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6477 | /* 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] | 6478 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6479 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6480 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6481 | * |
| 6482 | * Task wrapper are longjmp safe because the only one Lua code |
| 6483 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6484 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6485 | 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] | 6486 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6487 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6488 | enum hlua_exec status; |
| 6489 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6490 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6491 | task_set_affinity(task, tid_bit); |
| 6492 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6493 | /* If it is the first call to the task, we must initialize the |
| 6494 | * execution timeouts. |
| 6495 | */ |
| 6496 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6497 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6498 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6499 | /* Execute the Lua code. */ |
| 6500 | status = hlua_ctx_resume(hlua, 1); |
| 6501 | |
| 6502 | switch (status) { |
| 6503 | /* finished or yield */ |
| 6504 | case HLUA_E_OK: |
| 6505 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6506 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6507 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6508 | break; |
| 6509 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6510 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6511 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6512 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6513 | break; |
| 6514 | |
| 6515 | /* finished with error. */ |
| 6516 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6517 | hlua_lock(hlua); |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 6518 | SEND_ERR(NULL, "Lua task: %s.\n", hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6519 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6520 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6521 | task = NULL; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6522 | hlua_unlock(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6523 | break; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6524 | case HLUA_E_ERR: |
| 6525 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6526 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6527 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6528 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6529 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6530 | break; |
| 6531 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6532 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6533 | } |
| 6534 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6535 | /* This function is an LUA binding that register LUA function to be |
| 6536 | * executed after the HAProxy configuration parsing and before the |
| 6537 | * HAProxy scheduler starts. This function expect only one LUA |
| 6538 | * argument that is a function. This function returns nothing, but |
| 6539 | * throws if an error is encountered. |
| 6540 | */ |
| 6541 | __LJMP static int hlua_register_init(lua_State *L) |
| 6542 | { |
| 6543 | struct hlua_init_function *init; |
| 6544 | int ref; |
| 6545 | |
| 6546 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6547 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6548 | if (hlua_gethlua(L)) { |
| 6549 | /* runtime processing */ |
| 6550 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6551 | } |
| 6552 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6553 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6554 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6555 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6556 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6557 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6558 | |
| 6559 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6560 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6561 | return 0; |
| 6562 | } |
| 6563 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6564 | /* This functio is an LUA binding. It permits to register a task |
| 6565 | * executed in parallel of the main HAroxy activity. The task is |
| 6566 | * created and it is set in the HAProxy scheduler. It can be called |
| 6567 | * from the "init" section, "post init" or during the runtime. |
| 6568 | * |
| 6569 | * Lua prototype: |
| 6570 | * |
| 6571 | * <none> core.register_task(<function>) |
| 6572 | */ |
| 6573 | static int hlua_register_task(lua_State *L) |
| 6574 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6575 | struct hlua *hlua = NULL; |
| 6576 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6577 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6578 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6579 | |
| 6580 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6581 | |
| 6582 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6583 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6584 | /* Get the reference state. If the reference is NULL, L is the master |
| 6585 | * state, otherwise hlua->T is. |
| 6586 | */ |
| 6587 | hlua = hlua_gethlua(L); |
| 6588 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6589 | /* we are in runtime processing */ |
| 6590 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6591 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6592 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6593 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6594 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6595 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6596 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6597 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6598 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6599 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6600 | /* We are in the common lua state, execute the task anywhere, |
| 6601 | * otherwise, inherit the current thread identifier |
| 6602 | */ |
| 6603 | if (state_id == 0) |
| 6604 | task = task_new(MAX_THREADS_MASK); |
| 6605 | else |
| 6606 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6607 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6608 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6609 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6610 | task->context = hlua; |
| 6611 | task->process = hlua_process_task; |
| 6612 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 6613 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6614 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6615 | |
| 6616 | /* Restore the function in the stack. */ |
| 6617 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6618 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6619 | hlua_unref(L, ref); |
| 6620 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6621 | hlua->nargs = 0; |
| 6622 | |
| 6623 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6624 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6625 | |
| 6626 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6627 | |
| 6628 | alloc_error: |
| 6629 | task_destroy(task); |
| 6630 | hlua_ctx_destroy(hlua); |
| 6631 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6632 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6633 | } |
| 6634 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6635 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6636 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6637 | * resume converters. |
| 6638 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6639 | 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] | 6640 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6641 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6642 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6643 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6644 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6645 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6646 | if (!stream) |
| 6647 | return 0; |
| 6648 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6649 | if (!(hlua = hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn)))) { |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6650 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6651 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6652 | } |
| 6653 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6654 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6655 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6656 | |
| 6657 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6658 | if (!SET_SAFE_LJMP(hlua)) { |
| 6659 | hlua_lock(hlua); |
| 6660 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6661 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6662 | else |
| 6663 | error = "critical error"; |
| 6664 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6665 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6666 | return 0; |
| 6667 | } |
| 6668 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6669 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6670 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6671 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6672 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6673 | return 0; |
| 6674 | } |
| 6675 | |
| 6676 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6677 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6678 | |
| 6679 | /* convert input sample and pust-it in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6680 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6681 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6682 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6683 | return 0; |
| 6684 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6685 | hlua_smp2lua(hlua->T, smp); |
| 6686 | hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6687 | |
| 6688 | /* push keywords in the stack. */ |
| 6689 | if (arg_p) { |
| 6690 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6691 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6692 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6693 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6694 | return 0; |
| 6695 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6696 | hlua_arg2lua(hlua->T, arg_p); |
| 6697 | hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6698 | } |
| 6699 | } |
| 6700 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6701 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6702 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6703 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6704 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6705 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6706 | } |
| 6707 | |
| 6708 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6709 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6710 | /* finished. */ |
| 6711 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6712 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6713 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6714 | if (lua_gettop(hlua->T) <= 0) { |
| 6715 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6716 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6717 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6718 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6719 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6720 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6721 | /* dup the smp before popping the related lua value and |
| 6722 | * returning it to haproxy |
| 6723 | */ |
| 6724 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6725 | lua_pop(hlua->T, 1); |
| 6726 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6727 | return 1; |
| 6728 | |
| 6729 | /* yield. */ |
| 6730 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6731 | 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] | 6732 | return 0; |
| 6733 | |
| 6734 | /* finished with error. */ |
| 6735 | case HLUA_E_ERRMSG: |
| 6736 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6737 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6738 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6739 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6740 | lua_pop(hlua->T, 1); |
| 6741 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6742 | return 0; |
| 6743 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6744 | case HLUA_E_ETMOUT: |
| 6745 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6746 | return 0; |
| 6747 | |
| 6748 | case HLUA_E_NOMEM: |
| 6749 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6750 | return 0; |
| 6751 | |
| 6752 | case HLUA_E_YIELD: |
| 6753 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6754 | return 0; |
| 6755 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6756 | case HLUA_E_ERR: |
| 6757 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6758 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6759 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6760 | |
| 6761 | default: |
| 6762 | return 0; |
| 6763 | } |
| 6764 | } |
| 6765 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6766 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6767 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6768 | * resume sample-fetches. This function will be called by the sample |
| 6769 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6770 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6771 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6772 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6773 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6774 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6775 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6776 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6777 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6778 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6779 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6780 | if (!stream) |
| 6781 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6782 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6783 | if (!(hlua = hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn)))) { |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6784 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6785 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6786 | } |
| 6787 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6788 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6789 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6790 | |
| 6791 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6792 | if (!SET_SAFE_LJMP(hlua)) { |
| 6793 | hlua_lock(hlua); |
| 6794 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6795 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6796 | else |
| 6797 | error = "critical error"; |
| 6798 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6799 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6800 | return 0; |
| 6801 | } |
| 6802 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6803 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6804 | if (!lua_checkstack(hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6805 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6806 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6807 | return 0; |
| 6808 | } |
| 6809 | |
| 6810 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6811 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6812 | |
| 6813 | /* push arguments in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6814 | if (!hlua_txn_new(hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6815 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6816 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6817 | return 0; |
| 6818 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6819 | hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6820 | |
| 6821 | /* push keywords in the stack. */ |
| 6822 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6823 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6824 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6825 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6826 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6827 | return 0; |
| 6828 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6829 | hlua_arg2lua(hlua->T, arg_p); |
| 6830 | hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6831 | } |
| 6832 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6833 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6834 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6835 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6836 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6837 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6838 | } |
| 6839 | |
| 6840 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6841 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6842 | /* finished. */ |
| 6843 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6844 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6845 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6846 | if (lua_gettop(hlua->T) <= 0) { |
| 6847 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6848 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6849 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6850 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6851 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6852 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6853 | /* dup the smp before popping the related lua value and |
| 6854 | * returning it to haproxy |
| 6855 | */ |
| 6856 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6857 | lua_pop(hlua->T, 1); |
| 6858 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6859 | |
| 6860 | /* Set the end of execution flag. */ |
| 6861 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6862 | return 1; |
| 6863 | |
| 6864 | /* yield. */ |
| 6865 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6866 | 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] | 6867 | return 0; |
| 6868 | |
| 6869 | /* finished with error. */ |
| 6870 | case HLUA_E_ERRMSG: |
| 6871 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6872 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6873 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6874 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6875 | lua_pop(hlua->T, 1); |
| 6876 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6877 | return 0; |
| 6878 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6879 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6880 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6881 | return 0; |
| 6882 | |
| 6883 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6884 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6885 | return 0; |
| 6886 | |
| 6887 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6888 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6889 | return 0; |
| 6890 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6891 | case HLUA_E_ERR: |
| 6892 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6893 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6894 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6895 | |
| 6896 | default: |
| 6897 | return 0; |
| 6898 | } |
| 6899 | } |
| 6900 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6901 | /* This function is an LUA binding used for registering |
| 6902 | * "sample-conv" functions. It expects a converter name used |
| 6903 | * in the haproxy configuration file, and an LUA function. |
| 6904 | */ |
| 6905 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6906 | { |
| 6907 | struct sample_conv_kw_list *sck; |
| 6908 | const char *name; |
| 6909 | int ref; |
| 6910 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6911 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6912 | struct sample_conv *sc; |
| 6913 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6914 | |
| 6915 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6916 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6917 | if (hlua_gethlua(L)) { |
| 6918 | /* runtime processing */ |
| 6919 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6920 | } |
| 6921 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6922 | /* First argument : converter name. */ |
| 6923 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6924 | |
| 6925 | /* Second argument : lua function. */ |
| 6926 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6927 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6928 | /* Check if the converter is already registered */ |
| 6929 | trash = get_trash_chunk(); |
| 6930 | chunk_printf(trash, "lua.%s", name); |
| 6931 | sc = find_sample_conv(trash->area, trash->data); |
| 6932 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6933 | fcn = sc->private; |
| 6934 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6935 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6936 | "This will become a hard error in version 2.5.\n", name); |
| 6937 | } |
| 6938 | fcn->function_ref[hlua_state_id] = ref; |
| 6939 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6940 | } |
| 6941 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6942 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6943 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6944 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6945 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6946 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6947 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6948 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6949 | |
| 6950 | /* Fill fcn. */ |
| 6951 | fcn->name = strdup(name); |
| 6952 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6953 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6954 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6955 | |
| 6956 | /* List head */ |
| 6957 | sck->list.n = sck->list.p = NULL; |
| 6958 | |
| 6959 | /* converter keyword. */ |
| 6960 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6961 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6962 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6963 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6964 | |
| 6965 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6966 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6967 | 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] | 6968 | sck->kw[0].val_args = NULL; |
| 6969 | sck->kw[0].in_type = SMP_T_STR; |
| 6970 | sck->kw[0].out_type = SMP_T_STR; |
| 6971 | sck->kw[0].private = fcn; |
| 6972 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6973 | /* Register this new converter */ |
| 6974 | sample_register_convs(sck); |
| 6975 | |
| 6976 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6977 | |
| 6978 | alloc_error: |
| 6979 | release_hlua_function(fcn); |
| 6980 | ha_free(&sck); |
| 6981 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6982 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6983 | } |
| 6984 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6985 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6986 | * "sample-fetch" functions. It expects a converter name used |
| 6987 | * in the haproxy configuration file, and an LUA function. |
| 6988 | */ |
| 6989 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6990 | { |
| 6991 | const char *name; |
| 6992 | int ref; |
| 6993 | int len; |
| 6994 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6995 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6996 | struct sample_fetch *sf; |
| 6997 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6998 | |
| 6999 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 7000 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7001 | if (hlua_gethlua(L)) { |
| 7002 | /* runtime processing */ |
| 7003 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 7004 | } |
| 7005 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7006 | /* First argument : sample-fetch name. */ |
| 7007 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7008 | |
| 7009 | /* Second argument : lua function. */ |
| 7010 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 7011 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7012 | /* Check if the sample-fetch is already registered */ |
| 7013 | trash = get_trash_chunk(); |
| 7014 | chunk_printf(trash, "lua.%s", name); |
| 7015 | sf = find_sample_fetch(trash->area, trash->data); |
| 7016 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7017 | fcn = sf->private; |
| 7018 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7019 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 7020 | "This will become a hard error in version 2.5.\n", name); |
| 7021 | } |
| 7022 | fcn->function_ref[hlua_state_id] = ref; |
| 7023 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7024 | } |
| 7025 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7026 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7027 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7028 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7029 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7030 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7031 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7032 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7033 | |
| 7034 | /* Fill fcn. */ |
| 7035 | fcn->name = strdup(name); |
| 7036 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7037 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7038 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7039 | |
| 7040 | /* List head */ |
| 7041 | sfk->list.n = sfk->list.p = NULL; |
| 7042 | |
| 7043 | /* sample-fetch keyword. */ |
| 7044 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7045 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7046 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7047 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7048 | |
| 7049 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 7050 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7051 | 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] | 7052 | sfk->kw[0].val_args = NULL; |
| 7053 | sfk->kw[0].out_type = SMP_T_STR; |
| 7054 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 7055 | sfk->kw[0].val = 0; |
| 7056 | sfk->kw[0].private = fcn; |
| 7057 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7058 | /* Register this new fetch. */ |
| 7059 | sample_register_fetches(sfk); |
| 7060 | |
| 7061 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7062 | |
| 7063 | alloc_error: |
| 7064 | release_hlua_function(fcn); |
| 7065 | ha_free(&sfk); |
| 7066 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7067 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7068 | } |
| 7069 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7070 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7071 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7072 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7073 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7074 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7075 | unsigned int delay; |
| 7076 | unsigned int wakeup_ms; |
| 7077 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7078 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7079 | hlua = hlua_gethlua(L); |
| 7080 | if (!hlua) { |
| 7081 | return 0; |
| 7082 | } |
| 7083 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7084 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7085 | |
| 7086 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7087 | wakeup_ms = tick_add(now_ms, delay); |
| 7088 | hlua->wake_time = wakeup_ms; |
| 7089 | return 0; |
| 7090 | } |
| 7091 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7092 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7093 | * wrapper during the initialisation period. This function may return any |
| 7094 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7095 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7096 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7097 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7098 | 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] | 7099 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7100 | { |
| 7101 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7102 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7103 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7104 | const char *error; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7105 | struct hlua *hlua = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7106 | |
| 7107 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7108 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7109 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7110 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7111 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7112 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7113 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7114 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7115 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7116 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7117 | if (!(hlua = hlua_stream_ctx_prepare(s, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn)))) { |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 7118 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 7119 | rule->arg.hlua_rule->fcn->name); |
| 7120 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7121 | } |
| 7122 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7123 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7124 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7125 | |
| 7126 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7127 | if (!SET_SAFE_LJMP(hlua)) { |
| 7128 | hlua_lock(hlua); |
| 7129 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7130 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7131 | else |
| 7132 | error = "critical error"; |
| 7133 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7134 | rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7135 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7136 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7137 | } |
| 7138 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7139 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7140 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7141 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7142 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7143 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7144 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7145 | } |
| 7146 | |
| 7147 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7148 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7149 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7150 | /* Create and and push object stream in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7151 | if (!hlua_txn_new(hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7152 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7153 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7154 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7155 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7156 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7157 | hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7158 | |
| 7159 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7160 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7161 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7162 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7163 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7164 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7165 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7166 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7167 | lua_pushstring(hlua->T, *arg); |
| 7168 | hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7169 | } |
| 7170 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7171 | /* Now the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7172 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7173 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7174 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7175 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7179 | switch (hlua_ctx_resume(hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7180 | /* finished. */ |
| 7181 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7182 | /* Catch the return value */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7183 | hlua_lock(hlua); |
| 7184 | if (lua_gettop(hlua->T) > 0) |
| 7185 | act_ret = lua_tointeger(hlua->T, -1); |
| 7186 | hlua_unlock(hlua); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7187 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7188 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7189 | if (act_ret == ACT_RET_YIELD) { |
| 7190 | if (flags & ACT_OPT_FINAL) |
| 7191 | goto err_yield; |
| 7192 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7193 | if (dir == SMP_OPT_DIR_REQ) |
| 7194 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7195 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7196 | else |
| 7197 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7198 | hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7199 | } |
| 7200 | goto end; |
| 7201 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7202 | /* yield. */ |
| 7203 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7204 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7205 | if (dir == SMP_OPT_DIR_REQ) |
| 7206 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7207 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7208 | else |
| 7209 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7210 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7211 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7212 | /* Some actions can be wake up when a "write" event |
| 7213 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7214 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7215 | */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7216 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7217 | s->res.flags |= CF_WAKE_WRITE; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7218 | if (HLUA_IS_WAKEREQWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7219 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7220 | act_ret = ACT_RET_YIELD; |
| 7221 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7222 | |
| 7223 | /* finished with error. */ |
| 7224 | case HLUA_E_ERRMSG: |
| 7225 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7226 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7227 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7228 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 7229 | lua_pop(hlua->T, 1); |
| 7230 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7231 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7232 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7233 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7234 | 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] | 7235 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7236 | |
| 7237 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7238 | 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] | 7239 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7240 | |
| 7241 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7242 | err_yield: |
| 7243 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | 602c4af | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 7244 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7245 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7246 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7247 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7248 | case HLUA_E_ERR: |
| 7249 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7250 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7251 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7252 | |
| 7253 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7254 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7255 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7256 | |
| 7257 | end: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7258 | if (act_ret != ACT_RET_YIELD && hlua) |
| 7259 | hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7260 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7261 | } |
| 7262 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7263 | 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] | 7264 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7265 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7266 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7267 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7268 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7269 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7270 | } |
| 7271 | |
| 7272 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7273 | { |
| 7274 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7275 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7276 | struct task *task; |
| 7277 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7278 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7279 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7280 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7281 | if (!hlua) { |
| 7282 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7283 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7284 | return 0; |
| 7285 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7286 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7287 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7288 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7289 | |
| 7290 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7291 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7292 | if (!task) { |
| 7293 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7294 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7295 | return 0; |
| 7296 | } |
| 7297 | task->nice = 0; |
| 7298 | task->context = ctx; |
| 7299 | task->process = hlua_applet_wakeup; |
| 7300 | ctx->ctx.hlua_apptcp.task = task; |
| 7301 | |
| 7302 | /* In the execution wrappers linked with a stream, the |
| 7303 | * Lua context can be not initialized. This behavior |
| 7304 | * permits to save performances because a systematic |
| 7305 | * Lua initialization cause 5% performances loss. |
| 7306 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7307 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7308 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7309 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7310 | return 0; |
| 7311 | } |
| 7312 | |
| 7313 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7314 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7315 | |
| 7316 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7317 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7318 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7319 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7320 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7321 | else |
| 7322 | error = "critical error"; |
| 7323 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7324 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7325 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7326 | return 0; |
| 7327 | } |
| 7328 | |
| 7329 | /* Check stack available size. */ |
| 7330 | if (!lua_checkstack(hlua->T, 1)) { |
| 7331 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7332 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7333 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7334 | return 0; |
| 7335 | } |
| 7336 | |
| 7337 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7338 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7339 | |
| 7340 | /* Create and and push object stream in the stack. */ |
| 7341 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7342 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7343 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7344 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7345 | return 0; |
| 7346 | } |
| 7347 | hlua->nargs = 1; |
| 7348 | |
| 7349 | /* push keywords in the stack. */ |
| 7350 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7351 | if (!lua_checkstack(hlua->T, 1)) { |
| 7352 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7353 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7354 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7355 | return 0; |
| 7356 | } |
| 7357 | lua_pushstring(hlua->T, *arg); |
| 7358 | hlua->nargs++; |
| 7359 | } |
| 7360 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7361 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7362 | |
| 7363 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7364 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7365 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7366 | |
| 7367 | return 1; |
| 7368 | } |
| 7369 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7370 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7371 | { |
| 7372 | struct stream_interface *si = ctx->owner; |
| 7373 | struct stream *strm = si_strm(si); |
| 7374 | struct channel *res = si_ic(si); |
| 7375 | struct act_rule *rule = ctx->rule; |
| 7376 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7377 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7378 | |
| 7379 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7380 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7381 | /* eat the whole request */ |
| 7382 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7383 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7384 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7385 | |
| 7386 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7387 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7388 | return; |
| 7389 | |
| 7390 | /* Execute the function. */ |
| 7391 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7392 | /* finished. */ |
| 7393 | case HLUA_E_OK: |
| 7394 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7395 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7396 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7397 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7398 | res->flags |= CF_READ_NULL; |
| 7399 | si_shutr(si); |
| 7400 | return; |
| 7401 | |
| 7402 | /* yield. */ |
| 7403 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7404 | if (hlua->wake_time != TICK_ETERNITY) |
| 7405 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7406 | return; |
| 7407 | |
| 7408 | /* finished with error. */ |
| 7409 | case HLUA_E_ERRMSG: |
| 7410 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7411 | hlua_lock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7412 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7413 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7414 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7415 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7416 | goto error; |
| 7417 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7418 | case HLUA_E_ETMOUT: |
| 7419 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7420 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7421 | goto error; |
| 7422 | |
| 7423 | case HLUA_E_NOMEM: |
| 7424 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7425 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7426 | goto error; |
| 7427 | |
| 7428 | case HLUA_E_YIELD: /* unexpected */ |
| 7429 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7430 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7431 | goto error; |
| 7432 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7433 | case HLUA_E_ERR: |
| 7434 | /* Display log. */ |
| 7435 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7436 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7437 | goto error; |
| 7438 | |
| 7439 | default: |
| 7440 | goto error; |
| 7441 | } |
| 7442 | |
| 7443 | error: |
| 7444 | |
| 7445 | /* For all other cases, just close the stream. */ |
| 7446 | si_shutw(si); |
| 7447 | si_shutr(si); |
| 7448 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7449 | } |
| 7450 | |
| 7451 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7452 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7453 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7454 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7455 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7456 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7457 | } |
| 7458 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7459 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7460 | * an errors occurs and -1 if more data are required for initializing |
| 7461 | * the applet. |
| 7462 | */ |
| 7463 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7464 | { |
| 7465 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7466 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7467 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7468 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7469 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7470 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7472 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7473 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7474 | if (!hlua) { |
| 7475 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7476 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7477 | return 0; |
| 7478 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7479 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7480 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7481 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7482 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7483 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7484 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7485 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7486 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7487 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7488 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7489 | if (!task) { |
| 7490 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7491 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7492 | return 0; |
| 7493 | } |
| 7494 | task->nice = 0; |
| 7495 | task->context = ctx; |
| 7496 | task->process = hlua_applet_wakeup; |
| 7497 | ctx->ctx.hlua_apphttp.task = task; |
| 7498 | |
| 7499 | /* In the execution wrappers linked with a stream, the |
| 7500 | * Lua context can be not initialized. This behavior |
| 7501 | * permits to save performances because a systematic |
| 7502 | * Lua initialization cause 5% performances loss. |
| 7503 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7504 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7505 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7506 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7507 | return 0; |
| 7508 | } |
| 7509 | |
| 7510 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7511 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7512 | |
| 7513 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7514 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7515 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7516 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7517 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7518 | else |
| 7519 | error = "critical error"; |
| 7520 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7521 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7522 | hlua_unlock(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7523 | return 0; |
| 7524 | } |
| 7525 | |
| 7526 | /* Check stack available size. */ |
| 7527 | if (!lua_checkstack(hlua->T, 1)) { |
| 7528 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7529 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7530 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7531 | return 0; |
| 7532 | } |
| 7533 | |
| 7534 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7535 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7536 | |
| 7537 | /* Create and and push object stream in the stack. */ |
| 7538 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7539 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7540 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7541 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7542 | return 0; |
| 7543 | } |
| 7544 | hlua->nargs = 1; |
| 7545 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7546 | /* push keywords in the stack. */ |
| 7547 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7548 | if (!lua_checkstack(hlua->T, 1)) { |
| 7549 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7550 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7551 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7552 | return 0; |
| 7553 | } |
| 7554 | lua_pushstring(hlua->T, *arg); |
| 7555 | hlua->nargs++; |
| 7556 | } |
| 7557 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7558 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7559 | |
| 7560 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7561 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7562 | |
| 7563 | return 1; |
| 7564 | } |
| 7565 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7566 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7567 | { |
| 7568 | struct stream_interface *si = ctx->owner; |
| 7569 | struct stream *strm = si_strm(si); |
| 7570 | struct channel *req = si_oc(si); |
| 7571 | struct channel *res = si_ic(si); |
| 7572 | struct act_rule *rule = ctx->rule; |
| 7573 | struct proxy *px = strm->be; |
| 7574 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7575 | struct htx *req_htx, *res_htx; |
| 7576 | |
| 7577 | res_htx = htx_from_buf(&res->buf); |
| 7578 | |
| 7579 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7580 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7581 | goto out; |
| 7582 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7583 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7584 | if (!b_size(&res->buf)) { |
| 7585 | si_rx_room_blk(si); |
| 7586 | goto out; |
| 7587 | } |
| 7588 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7589 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7590 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7591 | |
| 7592 | /* Set the currently running flag. */ |
| 7593 | if (!HLUA_IS_RUNNING(hlua) && |
| 7594 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7595 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7596 | si_cant_get(si); |
| 7597 | goto out; |
| 7598 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7599 | } |
| 7600 | |
| 7601 | /* Executes The applet if it is not done. */ |
| 7602 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7603 | |
| 7604 | /* Execute the function. */ |
| 7605 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7606 | /* finished. */ |
| 7607 | case HLUA_E_OK: |
| 7608 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7609 | break; |
| 7610 | |
| 7611 | /* yield. */ |
| 7612 | case HLUA_E_AGAIN: |
| 7613 | if (hlua->wake_time != TICK_ETERNITY) |
| 7614 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7615 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7616 | |
| 7617 | /* finished with error. */ |
| 7618 | case HLUA_E_ERRMSG: |
| 7619 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7620 | hlua_lock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7621 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7622 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7623 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7624 | hlua_unlock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7625 | goto error; |
| 7626 | |
| 7627 | case HLUA_E_ETMOUT: |
| 7628 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7629 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7630 | goto error; |
| 7631 | |
| 7632 | case HLUA_E_NOMEM: |
| 7633 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7634 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7635 | goto error; |
| 7636 | |
| 7637 | case HLUA_E_YIELD: /* unexpected */ |
| 7638 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7639 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7640 | goto error; |
| 7641 | |
| 7642 | case HLUA_E_ERR: |
| 7643 | /* Display log. */ |
| 7644 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7645 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7646 | goto error; |
| 7647 | |
| 7648 | default: |
| 7649 | goto error; |
| 7650 | } |
| 7651 | } |
| 7652 | |
| 7653 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7654 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7655 | goto done; |
| 7656 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7657 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7658 | goto error; |
| 7659 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7660 | /* no more data are expected. If the response buffer is empty |
| 7661 | * for a chunked message, be sure to add something (EOT block in |
| 7662 | * this case) to have something to send. It is important to be |
| 7663 | * sure the EOM flags will be handled by the endpoint. |
| 7664 | */ |
| 7665 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7666 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7667 | si_rx_room_blk(si); |
| 7668 | goto out; |
| 7669 | } |
| 7670 | channel_add_input(res, 1); |
| 7671 | } |
| 7672 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7673 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7674 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7675 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7676 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | done: |
| 7680 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7681 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7682 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7683 | si_shutr(si); |
| 7684 | } |
| 7685 | |
| 7686 | /* eat the whole request */ |
| 7687 | if (co_data(req)) { |
| 7688 | req_htx = htx_from_buf(&req->buf); |
| 7689 | co_htx_skip(req, req_htx, co_data(req)); |
| 7690 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7691 | } |
| 7692 | } |
| 7693 | |
| 7694 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7695 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7696 | return; |
| 7697 | |
| 7698 | error: |
| 7699 | |
| 7700 | /* If we are in HTTP mode, and we are not send any |
| 7701 | * data, return a 500 server error in best effort: |
| 7702 | * if there is no room available in the buffer, |
| 7703 | * just close the connection. |
| 7704 | */ |
| 7705 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7706 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7707 | |
| 7708 | channel_erase(res); |
| 7709 | res->buf.data = b_data(err); |
| 7710 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7711 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7712 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7713 | } |
| 7714 | if (!(strm->flags & SF_ERR_MASK)) |
| 7715 | strm->flags |= SF_ERR_RESOURCE; |
| 7716 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7717 | goto done; |
| 7718 | } |
| 7719 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7720 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7721 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7722 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7723 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7724 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7725 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7726 | } |
| 7727 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7728 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7729 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7730 | * |
| 7731 | * This function can fail with an abort() due to an Lua critical error. |
| 7732 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7733 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7734 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7735 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7736 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7737 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7738 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7739 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7740 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7741 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7742 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7743 | if (!rule->arg.hlua_rule) { |
| 7744 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7745 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7746 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7747 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7748 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7749 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7750 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7751 | if (!rule->arg.hlua_rule->args) { |
| 7752 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7753 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7754 | } |
| 7755 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7756 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7757 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7758 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7759 | /* Expect some arguments */ |
| 7760 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7761 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7762 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7763 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7764 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7765 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7766 | if (!rule->arg.hlua_rule->args[i]) { |
| 7767 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7768 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7769 | } |
| 7770 | (*cur_arg)++; |
| 7771 | } |
| 7772 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7773 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7774 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7775 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7776 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7777 | |
| 7778 | error: |
| 7779 | if (rule->arg.hlua_rule) { |
| 7780 | if (rule->arg.hlua_rule->args) { |
| 7781 | for (i = 0; i < fcn->nargs; i++) |
| 7782 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7783 | ha_free(&rule->arg.hlua_rule->args); |
| 7784 | } |
| 7785 | ha_free(&rule->arg.hlua_rule); |
| 7786 | } |
| 7787 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7788 | } |
| 7789 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7790 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7791 | struct act_rule *rule, char **err) |
| 7792 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7793 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7794 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7795 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7796 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7797 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7798 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7799 | * the call of this analyzer. |
| 7800 | */ |
| 7801 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7802 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7803 | return ACT_RET_PRS_ERR; |
| 7804 | } |
| 7805 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7806 | /* Memory for the rule. */ |
| 7807 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7808 | if (!rule->arg.hlua_rule) { |
| 7809 | memprintf(err, "out of memory error"); |
| 7810 | return ACT_RET_PRS_ERR; |
| 7811 | } |
| 7812 | |
| 7813 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7814 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7815 | |
| 7816 | /* TODO: later accept arguments. */ |
| 7817 | rule->arg.hlua_rule->args = NULL; |
| 7818 | |
| 7819 | /* Add applet pointer in the rule. */ |
| 7820 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7821 | rule->applet.name = fcn->name; |
| 7822 | rule->applet.init = hlua_applet_http_init; |
| 7823 | rule->applet.fct = hlua_applet_http_fct; |
| 7824 | rule->applet.release = hlua_applet_http_release; |
| 7825 | rule->applet.timeout = hlua_timeout_applet; |
| 7826 | |
| 7827 | return ACT_RET_PRS_OK; |
| 7828 | } |
| 7829 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7830 | /* This function is an LUA binding used for registering |
| 7831 | * "sample-conv" functions. It expects a converter name used |
| 7832 | * in the haproxy configuration file, and an LUA function. |
| 7833 | */ |
| 7834 | __LJMP static int hlua_register_action(lua_State *L) |
| 7835 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7836 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7837 | const char *name; |
| 7838 | int ref; |
| 7839 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7840 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7841 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7842 | struct buffer *trash; |
| 7843 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7844 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7845 | /* Initialise the number of expected arguments at 0. */ |
| 7846 | nargs = 0; |
| 7847 | |
| 7848 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7849 | 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] | 7850 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7851 | if (hlua_gethlua(L)) { |
| 7852 | /* runtime processing */ |
| 7853 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7854 | } |
| 7855 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7856 | /* First argument : converter name. */ |
| 7857 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7858 | |
| 7859 | /* Second argument : environment. */ |
| 7860 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7861 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7862 | |
| 7863 | /* Third argument : lua function. */ |
| 7864 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7865 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7866 | /* 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] | 7867 | if (lua_gettop(L) >= 4) |
| 7868 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7869 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7870 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7871 | lua_pushnil(L); |
| 7872 | while (lua_next(L, 2) != 0) { |
| 7873 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7874 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7875 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7876 | /* Check if action exists */ |
| 7877 | trash = get_trash_chunk(); |
| 7878 | chunk_printf(trash, "lua.%s", name); |
| 7879 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7880 | akw = tcp_req_cont_action(trash->area); |
| 7881 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7882 | akw = tcp_res_cont_action(trash->area); |
| 7883 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7884 | akw = action_http_req_custom(trash->area); |
| 7885 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7886 | akw = action_http_res_custom(trash->area); |
| 7887 | } else { |
| 7888 | akw = NULL; |
| 7889 | } |
| 7890 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7891 | fcn = akw->private; |
| 7892 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7893 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7894 | "This will become a hard error in version 2.5.\n", name); |
| 7895 | } |
| 7896 | fcn->function_ref[hlua_state_id] = ref; |
| 7897 | |
| 7898 | /* pop the environment string. */ |
| 7899 | lua_pop(L, 1); |
| 7900 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7901 | } |
| 7902 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7903 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7904 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7905 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7906 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7907 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7908 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7909 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7910 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7911 | |
| 7912 | /* Fill fcn. */ |
| 7913 | fcn->name = strdup(name); |
| 7914 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7915 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7916 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7917 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7918 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7919 | fcn->nargs = nargs; |
| 7920 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7921 | /* List head */ |
| 7922 | akl->list.n = akl->list.p = NULL; |
| 7923 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7924 | /* action keyword. */ |
| 7925 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7926 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7927 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7928 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7929 | |
| 7930 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7931 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7932 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7933 | akl->kw[0].private = fcn; |
| 7934 | akl->kw[0].parse = action_register_lua; |
| 7935 | |
| 7936 | /* select the action registering point. */ |
| 7937 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7938 | tcp_req_cont_keywords_register(akl); |
| 7939 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7940 | tcp_res_cont_keywords_register(akl); |
| 7941 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7942 | http_req_keywords_register(akl); |
| 7943 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7944 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7945 | else { |
| 7946 | release_hlua_function(fcn); |
| 7947 | if (akl) |
| 7948 | ha_free((char **)&(akl->kw[0].kw)); |
| 7949 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7950 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7951 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7952 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7953 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7954 | |
| 7955 | /* pop the environment string. */ |
| 7956 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7957 | |
| 7958 | /* reset for next loop */ |
| 7959 | akl = NULL; |
| 7960 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7961 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7962 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7963 | |
| 7964 | alloc_error: |
| 7965 | release_hlua_function(fcn); |
| 7966 | ha_free(&akl); |
| 7967 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7968 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7969 | } |
| 7970 | |
| 7971 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7972 | struct act_rule *rule, char **err) |
| 7973 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7974 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7975 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7976 | if (px->mode == PR_MODE_HTTP) { |
| 7977 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7978 | return ACT_RET_PRS_ERR; |
| 7979 | } |
| 7980 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7981 | /* Memory for the rule. */ |
| 7982 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7983 | if (!rule->arg.hlua_rule) { |
| 7984 | memprintf(err, "out of memory error"); |
| 7985 | return ACT_RET_PRS_ERR; |
| 7986 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7987 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7988 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7989 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7990 | |
| 7991 | /* TODO: later accept arguments. */ |
| 7992 | rule->arg.hlua_rule->args = NULL; |
| 7993 | |
| 7994 | /* Add applet pointer in the rule. */ |
| 7995 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7996 | rule->applet.name = fcn->name; |
| 7997 | rule->applet.init = hlua_applet_tcp_init; |
| 7998 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7999 | rule->applet.release = hlua_applet_tcp_release; |
| 8000 | rule->applet.timeout = hlua_timeout_applet; |
| 8001 | |
| 8002 | return 0; |
| 8003 | } |
| 8004 | |
| 8005 | /* This function is an LUA binding used for registering |
| 8006 | * "sample-conv" functions. It expects a converter name used |
| 8007 | * in the haproxy configuration file, and an LUA function. |
| 8008 | */ |
| 8009 | __LJMP static int hlua_register_service(lua_State *L) |
| 8010 | { |
| 8011 | struct action_kw_list *akl; |
| 8012 | const char *name; |
| 8013 | const char *env; |
| 8014 | int ref; |
| 8015 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8016 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8017 | struct buffer *trash; |
| 8018 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8019 | |
| 8020 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 8021 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8022 | if (hlua_gethlua(L)) { |
| 8023 | /* runtime processing */ |
| 8024 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 8025 | } |
| 8026 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8027 | /* First argument : converter name. */ |
| 8028 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8029 | |
| 8030 | /* Second argument : environment. */ |
| 8031 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8032 | |
| 8033 | /* Third argument : lua function. */ |
| 8034 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8035 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8036 | /* Check for service already registered */ |
| 8037 | trash = get_trash_chunk(); |
| 8038 | chunk_printf(trash, "lua.%s", name); |
| 8039 | akw = service_find(trash->area); |
| 8040 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8041 | fcn = akw->private; |
| 8042 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8043 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 8044 | "This will become a hard error in version 2.5.\n", name); |
| 8045 | } |
| 8046 | fcn->function_ref[hlua_state_id] = ref; |
| 8047 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8048 | } |
| 8049 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8050 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8051 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 8052 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8053 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8054 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8055 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8056 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8057 | |
| 8058 | /* Fill fcn. */ |
| 8059 | len = strlen("<lua.>") + strlen(name) + 1; |
| 8060 | fcn->name = calloc(1, len); |
| 8061 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8062 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8063 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8064 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8065 | |
| 8066 | /* List head */ |
| 8067 | akl->list.n = akl->list.p = NULL; |
| 8068 | |
| 8069 | /* converter keyword. */ |
| 8070 | len = strlen("lua.") + strlen(name) + 1; |
| 8071 | akl->kw[0].kw = calloc(1, len); |
| 8072 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8073 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8074 | |
| 8075 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8076 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 8077 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8078 | if (strcmp(env, "tcp") == 0) |
| 8079 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8080 | else if (strcmp(env, "http") == 0) |
| 8081 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8082 | else { |
| 8083 | release_hlua_function(fcn); |
| 8084 | if (akl) |
| 8085 | ha_free((char **)&(akl->kw[0].kw)); |
| 8086 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8087 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 8088 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8089 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8090 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8091 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8092 | akl->kw[0].private = fcn; |
| 8093 | |
| 8094 | /* End of array. */ |
| 8095 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8096 | |
| 8097 | /* Register this new converter */ |
| 8098 | service_keywords_register(akl); |
| 8099 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8100 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8101 | |
| 8102 | alloc_error: |
| 8103 | release_hlua_function(fcn); |
| 8104 | ha_free(&akl); |
| 8105 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8106 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8107 | } |
| 8108 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8109 | /* This function initialises Lua cli handler. It copies the |
| 8110 | * arguments in the Lua stack and create channel IO objects. |
| 8111 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8112 | 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] | 8113 | { |
| 8114 | struct hlua *hlua; |
| 8115 | struct hlua_function *fcn; |
| 8116 | int i; |
| 8117 | const char *error; |
| 8118 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8119 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8120 | appctx->ctx.hlua_cli.fcn = private; |
| 8121 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8122 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8123 | if (!hlua) { |
| 8124 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8125 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8126 | } |
| 8127 | HLUA_INIT(hlua); |
| 8128 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8129 | |
| 8130 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8131 | * 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] | 8132 | * applet_http. It is absolutely compatible. |
| 8133 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8134 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8135 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8136 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8137 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8138 | } |
| 8139 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8140 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8141 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8142 | |
| 8143 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8144 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8145 | 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] | 8146 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8147 | } |
| 8148 | |
| 8149 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8150 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8151 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8152 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8153 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8154 | else |
| 8155 | error = "critical error"; |
| 8156 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8157 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8158 | goto error; |
| 8159 | } |
| 8160 | |
| 8161 | /* Check stack available size. */ |
| 8162 | if (!lua_checkstack(hlua->T, 2)) { |
| 8163 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8164 | goto error; |
| 8165 | } |
| 8166 | |
| 8167 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8168 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8169 | |
| 8170 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8171 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8172 | */ |
| 8173 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8174 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8175 | goto error; |
| 8176 | } |
| 8177 | hlua->nargs = 1; |
| 8178 | |
| 8179 | /* push keywords in the stack. */ |
| 8180 | for (i = 0; *args[i]; i++) { |
| 8181 | /* Check stack available size. */ |
| 8182 | if (!lua_checkstack(hlua->T, 1)) { |
| 8183 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8184 | goto error; |
| 8185 | } |
| 8186 | lua_pushstring(hlua->T, args[i]); |
| 8187 | hlua->nargs++; |
| 8188 | } |
| 8189 | |
| 8190 | /* We must initialize the execution timeouts. */ |
| 8191 | hlua->max_time = hlua_timeout_session; |
| 8192 | |
| 8193 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8194 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8195 | |
| 8196 | /* It's ok */ |
| 8197 | return 0; |
| 8198 | |
| 8199 | /* It's not ok. */ |
| 8200 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8201 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8202 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8203 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8204 | return 1; |
| 8205 | } |
| 8206 | |
| 8207 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8208 | { |
| 8209 | struct hlua *hlua; |
| 8210 | struct stream_interface *si; |
| 8211 | struct hlua_function *fcn; |
| 8212 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8213 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8214 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8215 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8216 | |
| 8217 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8218 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8219 | return 1; |
| 8220 | |
| 8221 | /* Execute the function. */ |
| 8222 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8223 | |
| 8224 | /* finished. */ |
| 8225 | case HLUA_E_OK: |
| 8226 | return 1; |
| 8227 | |
| 8228 | /* yield. */ |
| 8229 | case HLUA_E_AGAIN: |
| 8230 | /* We want write. */ |
| 8231 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8232 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8233 | /* Set the timeout. */ |
| 8234 | if (hlua->wake_time != TICK_ETERNITY) |
| 8235 | task_schedule(hlua->task, hlua->wake_time); |
| 8236 | return 0; |
| 8237 | |
| 8238 | /* finished with error. */ |
| 8239 | case HLUA_E_ERRMSG: |
| 8240 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8241 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8242 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8243 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8244 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8245 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8246 | return 1; |
| 8247 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8248 | case HLUA_E_ETMOUT: |
| 8249 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8250 | fcn->name); |
| 8251 | return 1; |
| 8252 | |
| 8253 | case HLUA_E_NOMEM: |
| 8254 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8255 | fcn->name); |
| 8256 | return 1; |
| 8257 | |
| 8258 | case HLUA_E_YIELD: /* unexpected */ |
| 8259 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8260 | fcn->name); |
| 8261 | return 1; |
| 8262 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8263 | case HLUA_E_ERR: |
| 8264 | /* Display log. */ |
| 8265 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8266 | fcn->name); |
| 8267 | return 1; |
| 8268 | |
| 8269 | default: |
| 8270 | return 1; |
| 8271 | } |
| 8272 | |
| 8273 | return 1; |
| 8274 | } |
| 8275 | |
| 8276 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8277 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8278 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8279 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8280 | } |
| 8281 | |
| 8282 | /* This function is an LUA binding used for registering |
| 8283 | * new keywords in the cli. It expects a list of keywords |
| 8284 | * which are the "path". It is limited to 5 keywords. A |
| 8285 | * description of the command, a function to be executed |
| 8286 | * for the parsing and a function for io handlers. |
| 8287 | */ |
| 8288 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8289 | { |
| 8290 | struct cli_kw_list *cli_kws; |
| 8291 | const char *message; |
| 8292 | int ref_io; |
| 8293 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8294 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8295 | int index; |
| 8296 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8297 | struct buffer *trash; |
| 8298 | const char *kw[5]; |
| 8299 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8300 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8301 | |
| 8302 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8303 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8304 | if (hlua_gethlua(L)) { |
| 8305 | /* runtime processing */ |
| 8306 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8307 | } |
| 8308 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8309 | /* First argument : an array of maximum 5 keywords. */ |
| 8310 | if (!lua_istable(L, 1)) |
| 8311 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8312 | |
| 8313 | /* Second argument : string with contextual message. */ |
| 8314 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8315 | |
| 8316 | /* Third and fourth argument : lua function. */ |
| 8317 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8318 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8319 | /* Check for CLI service already registered */ |
| 8320 | trash = get_trash_chunk(); |
| 8321 | index = 0; |
| 8322 | lua_pushnil(L); |
| 8323 | memset(kw, 0, sizeof(kw)); |
| 8324 | while (lua_next(L, 1) != 0) { |
| 8325 | if (index >= CLI_PREFIX_KW_NB) |
| 8326 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8327 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8328 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8329 | kw[index] = lua_tostring(L, -1); |
| 8330 | if (index == 0) |
| 8331 | chunk_printf(trash, "%s", kw[index]); |
| 8332 | else |
| 8333 | chunk_appendf(trash, " %s", kw[index]); |
| 8334 | index++; |
| 8335 | lua_pop(L, 1); |
| 8336 | } |
| 8337 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8338 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8339 | fcn = cli_kw->private; |
| 8340 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8341 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8342 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8343 | } |
| 8344 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8345 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8346 | } |
| 8347 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8348 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8349 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8350 | if (!cli_kws) { |
| 8351 | errmsg = "Lua out of memory error."; |
| 8352 | goto error; |
| 8353 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8354 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8355 | if (!fcn) { |
| 8356 | errmsg = "Lua out of memory error."; |
| 8357 | goto error; |
| 8358 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8359 | |
| 8360 | /* Fill path. */ |
| 8361 | index = 0; |
| 8362 | lua_pushnil(L); |
| 8363 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8364 | if (index >= 5) { |
| 8365 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8366 | goto error; |
| 8367 | } |
| 8368 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8369 | errmsg = "1st argument must be a table filled with strings"; |
| 8370 | goto error; |
| 8371 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8372 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8373 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8374 | errmsg = "Lua out of memory error."; |
| 8375 | goto error; |
| 8376 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8377 | index++; |
| 8378 | lua_pop(L, 1); |
| 8379 | } |
| 8380 | |
| 8381 | /* Copy help message. */ |
| 8382 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8383 | if (!cli_kws->kw[0].usage) { |
| 8384 | errmsg = "Lua out of memory error."; |
| 8385 | goto error; |
| 8386 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8387 | |
| 8388 | /* Fill fcn io handler. */ |
| 8389 | len = strlen("<lua.cli>") + 1; |
| 8390 | for (i = 0; i < index; i++) |
| 8391 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8392 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8393 | if (!fcn->name) { |
| 8394 | errmsg = "Lua out of memory error."; |
| 8395 | goto error; |
| 8396 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8397 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8398 | for (i = 0; i < index; i++) { |
| 8399 | strncat((char *)fcn->name, ".", len); |
| 8400 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8401 | } |
| 8402 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8403 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8404 | |
| 8405 | /* Fill last entries. */ |
| 8406 | cli_kws->kw[0].private = fcn; |
| 8407 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8408 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8409 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8410 | |
| 8411 | /* Register this new converter */ |
| 8412 | cli_register_kw(cli_kws); |
| 8413 | |
| 8414 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8415 | |
| 8416 | error: |
| 8417 | release_hlua_function(fcn); |
| 8418 | if (cli_kws) { |
| 8419 | for (i = 0; i < index; i++) |
| 8420 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8421 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8422 | } |
| 8423 | ha_free(&cli_kws); |
| 8424 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8425 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8426 | } |
| 8427 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8428 | 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] | 8429 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8430 | char **err, unsigned int *timeout) |
| 8431 | { |
| 8432 | const char *error; |
| 8433 | |
| 8434 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8435 | if (error == PARSE_TIME_OVER) { |
| 8436 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8437 | args[1], args[0]); |
| 8438 | return -1; |
| 8439 | } |
| 8440 | else if (error == PARSE_TIME_UNDER) { |
| 8441 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8442 | args[1], args[0]); |
| 8443 | return -1; |
| 8444 | } |
| 8445 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8446 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8447 | return -1; |
| 8448 | } |
| 8449 | return 0; |
| 8450 | } |
| 8451 | |
| 8452 | 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] | 8453 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8454 | char **err) |
| 8455 | { |
| 8456 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8457 | file, line, err, &hlua_timeout_session); |
| 8458 | } |
| 8459 | |
| 8460 | 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] | 8461 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8462 | char **err) |
| 8463 | { |
| 8464 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8465 | file, line, err, &hlua_timeout_task); |
| 8466 | } |
| 8467 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8468 | 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] | 8469 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8470 | char **err) |
| 8471 | { |
| 8472 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8473 | file, line, err, &hlua_timeout_applet); |
| 8474 | } |
| 8475 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8476 | 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] | 8477 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8478 | char **err) |
| 8479 | { |
| 8480 | char *error; |
| 8481 | |
| 8482 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8483 | if (*error != '\0') { |
| 8484 | memprintf(err, "%s: invalid number", args[0]); |
| 8485 | return -1; |
| 8486 | } |
| 8487 | return 0; |
| 8488 | } |
| 8489 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8490 | 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] | 8491 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8492 | char **err) |
| 8493 | { |
| 8494 | char *error; |
| 8495 | |
| 8496 | if (*(args[1]) == 0) { |
| 8497 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8498 | return -1; |
| 8499 | } |
| 8500 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8501 | if (*error != '\0') { |
| 8502 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8503 | return -1; |
| 8504 | } |
| 8505 | return 0; |
| 8506 | } |
| 8507 | |
| 8508 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8509 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8510 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8511 | * the main lua entry point. |
| 8512 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8513 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8514 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8515 | * |
| 8516 | * In some error case, LUA set an error message in top of the stack. This function |
| 8517 | * 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] | 8518 | * |
| 8519 | * This function can fail with an abort() due to an Lua critical error. |
| 8520 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8521 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8522 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8523 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8524 | { |
| 8525 | int error; |
| 8526 | |
| 8527 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8528 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8529 | if (error) { |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8530 | memprintf(err, "error in Lua file '%s': %s", filename, hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8531 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8532 | return -1; |
| 8533 | } |
| 8534 | |
| 8535 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8536 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8537 | switch (error) { |
| 8538 | case LUA_OK: |
| 8539 | break; |
| 8540 | case LUA_ERRRUN: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8541 | memprintf(err, "Lua runtime error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8542 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8543 | return -1; |
| 8544 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8545 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8546 | return -1; |
| 8547 | case LUA_ERRERR: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8548 | memprintf(err, "Lua message handler error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8549 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8550 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8551 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8552 | case LUA_ERRGCMM: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8553 | memprintf(err, "Lua garbage collector error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8554 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8555 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8556 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8557 | default: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8558 | memprintf(err, "Lua unknown error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8559 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8560 | return -1; |
| 8561 | } |
| 8562 | |
| 8563 | return 0; |
| 8564 | } |
| 8565 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8566 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8567 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8568 | char **err) |
| 8569 | { |
| 8570 | if (*(args[1]) == 0) { |
| 8571 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8572 | return -1; |
| 8573 | } |
| 8574 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8575 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8576 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8577 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8578 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8579 | } |
| 8580 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8581 | 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] | 8582 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8583 | char **err) |
| 8584 | { |
| 8585 | int len; |
| 8586 | |
| 8587 | if (*(args[1]) == 0) { |
| 8588 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8589 | return -1; |
| 8590 | } |
| 8591 | |
| 8592 | if (per_thread_load == NULL) { |
| 8593 | /* allocate the first entry large enough to store the final NULL */ |
| 8594 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8595 | if (per_thread_load == NULL) { |
| 8596 | memprintf(err, "out of memory error"); |
| 8597 | return -1; |
| 8598 | } |
| 8599 | } |
| 8600 | |
| 8601 | /* count used entries */ |
| 8602 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8603 | ; |
| 8604 | |
| 8605 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8606 | if (per_thread_load == NULL) { |
| 8607 | memprintf(err, "out of memory error"); |
| 8608 | return -1; |
| 8609 | } |
| 8610 | |
| 8611 | per_thread_load[len] = strdup(args[1]); |
| 8612 | per_thread_load[len + 1] = NULL; |
| 8613 | |
| 8614 | if (per_thread_load[len] == NULL) { |
| 8615 | memprintf(err, "out of memory error"); |
| 8616 | return -1; |
| 8617 | } |
| 8618 | |
| 8619 | /* loading for thread 1 only */ |
| 8620 | hlua_state_id = 1; |
| 8621 | ha_set_tid(0); |
| 8622 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8623 | } |
| 8624 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8625 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8626 | * in the given <ctx>. |
| 8627 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8628 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8629 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8630 | lua_getglobal(L, "package"); /* push package variable */ |
| 8631 | lua_pushstring(L, path); /* push given path */ |
| 8632 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8633 | lua_getfield(L, -3, type); /* push old path */ |
| 8634 | lua_concat(L, 3); /* concatenate to new path */ |
| 8635 | lua_setfield(L, -2, type); /* store new path */ |
| 8636 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8637 | |
| 8638 | return 0; |
| 8639 | } |
| 8640 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8641 | 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] | 8642 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8643 | char **err) |
| 8644 | { |
| 8645 | char *path; |
| 8646 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8647 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8648 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8649 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8650 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8651 | } |
| 8652 | |
| 8653 | if (!(*args[1])) { |
| 8654 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8655 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8656 | } |
| 8657 | path = args[1]; |
| 8658 | |
| 8659 | if (*args[2]) { |
| 8660 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8661 | 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] | 8662 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8663 | } |
| 8664 | type = args[2]; |
| 8665 | } |
| 8666 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8667 | p = calloc(1, sizeof(*p)); |
| 8668 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8669 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8670 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8671 | } |
| 8672 | p->path = strdup(path); |
| 8673 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8674 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8675 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8676 | } |
| 8677 | p->type = strdup(type); |
| 8678 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8679 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8680 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8681 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8682 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8683 | |
| 8684 | hlua_prepend_path(hlua_states[0], type, path); |
| 8685 | hlua_prepend_path(hlua_states[1], type, path); |
| 8686 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8687 | |
| 8688 | err2: |
| 8689 | free(p->type); |
| 8690 | free(p->path); |
| 8691 | err: |
| 8692 | free(p); |
| 8693 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8694 | } |
| 8695 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8696 | /* configuration keywords declaration */ |
| 8697 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8698 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8699 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8700 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8701 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8702 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8703 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8704 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8705 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8706 | { 0, NULL, NULL }, |
| 8707 | }}; |
| 8708 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8709 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8710 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8711 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8712 | /* This function can fail with an abort() due to an Lua critical error. |
| 8713 | * We are in the initialisation process of HAProxy, this abort() is |
| 8714 | * tolerated. |
| 8715 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8716 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8717 | { |
| 8718 | struct hlua_init_function *init; |
| 8719 | const char *msg; |
| 8720 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8721 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8722 | const char *kind; |
| 8723 | const char *trace; |
| 8724 | int return_status = 1; |
| 8725 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8726 | int nres; |
| 8727 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8728 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8729 | /* disable memory limit checks if limit is not set */ |
| 8730 | if (!hlua_global_allocator.limit) |
| 8731 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8732 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8733 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8734 | if (setjmp(safe_ljmp_env) != 0) { |
| 8735 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8736 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8737 | error = hlua_tostring_safe(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8738 | else |
| 8739 | error = "critical error"; |
| 8740 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8741 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8742 | } else { |
| 8743 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8744 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8745 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8746 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8747 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8748 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8749 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 8750 | /* function ref should be released right away since it was pushed |
| 8751 | * on the stack and will not be used anymore |
| 8752 | */ |
| 8753 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8754 | |
| 8755 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8756 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8757 | #else |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8758 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8759 | #endif |
| 8760 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8761 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8762 | |
| 8763 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8764 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8765 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8766 | |
| 8767 | case LUA_ERRERR: |
| 8768 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8769 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8770 | case LUA_ERRRUN: |
| 8771 | if (!kind) |
| 8772 | kind = "runtime error"; |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8773 | msg = hlua_tostring_safe(L, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8774 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8775 | if (msg) |
| 8776 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8777 | else |
| 8778 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 8779 | |
| 8780 | lua_settop(L, 0); /* Empty the stack. */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8781 | return_status = 0; |
| 8782 | break; |
| 8783 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8784 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8785 | /* Unknown error */ |
| 8786 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8787 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8788 | case LUA_YIELD: |
| 8789 | /* yield is not configured at this step, this state doesn't happen */ |
| 8790 | if (!kind) |
| 8791 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8792 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8793 | case LUA_ERRMEM: |
| 8794 | if (!kind) |
| 8795 | kind = "out of memory error"; |
Aurelien DARRAGON | 93f5d8d | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 8796 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8797 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8798 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8799 | return_status = 0; |
| 8800 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8801 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8802 | if (!return_status) |
| 8803 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8804 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8805 | |
| 8806 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8807 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8808 | } |
| 8809 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8810 | int hlua_post_init() |
| 8811 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8812 | int ret; |
| 8813 | int i; |
| 8814 | int errors; |
| 8815 | char *err = NULL; |
| 8816 | struct hlua_function *fcn; |
| 8817 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8818 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8819 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8820 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8821 | int saved_used_backed = global.ssl_used_backend; |
| 8822 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8823 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8824 | global.ssl_used_backend = saved_used_backed; |
| 8825 | } |
| 8826 | #endif |
| 8827 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8828 | /* Perform post init of common thread */ |
| 8829 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8830 | ha_set_tid(0); |
| 8831 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8832 | if (ret == 0) |
| 8833 | return 0; |
| 8834 | |
| 8835 | /* init remaining lua states and load files */ |
| 8836 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8837 | |
| 8838 | /* set thread context */ |
| 8839 | ha_set_tid(hlua_state_id - 1); |
| 8840 | |
| 8841 | /* Init lua state */ |
| 8842 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8843 | |
| 8844 | /* Load lua files */ |
| 8845 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8846 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8847 | if (ret != 0) { |
| 8848 | ha_alert("Lua init: %s\n", err); |
| 8849 | return 0; |
| 8850 | } |
| 8851 | } |
| 8852 | } |
| 8853 | |
| 8854 | /* Reset thread context */ |
| 8855 | ha_set_tid(0); |
| 8856 | |
| 8857 | /* Execute post init for all states */ |
| 8858 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8859 | |
| 8860 | /* set thread context */ |
| 8861 | ha_set_tid(hlua_state_id - 1); |
| 8862 | |
| 8863 | /* run post init */ |
| 8864 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8865 | if (ret == 0) |
| 8866 | return 0; |
| 8867 | } |
| 8868 | |
| 8869 | /* Reset thread context */ |
| 8870 | ha_set_tid(0); |
| 8871 | |
| 8872 | /* control functions registering. Each function must have: |
| 8873 | * - only the function_ref[0] set positive and all other to -1 |
| 8874 | * - only the function_ref[0] set to -1 and all other positive |
| 8875 | * This ensure a same reference is not used both in shared |
| 8876 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8877 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8878 | * complicated to found for the end user. |
| 8879 | */ |
| 8880 | errors = 0; |
| 8881 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8882 | ret = 0; |
| 8883 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8884 | if (fcn->function_ref[i] == -1) |
| 8885 | ret--; |
| 8886 | else |
| 8887 | ret++; |
| 8888 | } |
| 8889 | if (abs(ret) != global.nbthread) { |
| 8890 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8891 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8892 | errors++; |
| 8893 | continue; |
| 8894 | } |
| 8895 | |
| 8896 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8897 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8898 | "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] | 8899 | "exclusive.\n", fcn->name); |
| 8900 | errors++; |
| 8901 | } |
| 8902 | } |
| 8903 | |
| 8904 | if (errors > 0) |
| 8905 | return 0; |
| 8906 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8907 | /* 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] | 8908 | * -1 in order to have probably a segfault if someone use it |
| 8909 | */ |
| 8910 | hlua_state_id = -1; |
| 8911 | |
| 8912 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8913 | } |
| 8914 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8915 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8916 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8917 | * 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] | 8918 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8919 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8920 | * zero. This one verifies that the limits are respected but is optimized |
| 8921 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8922 | * |
| 8923 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8924 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8925 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8926 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8927 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8928 | */ |
| 8929 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8930 | { |
| 8931 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8932 | size_t limit, old, new; |
| 8933 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8934 | if (unlikely(!ptr && !nsize)) |
| 8935 | return NULL; |
| 8936 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8937 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8938 | * for accounting anymore. |
| 8939 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8940 | if (likely(~zone->limit == 0)) { |
| 8941 | if (!nsize) |
| 8942 | ha_free(&ptr); |
| 8943 | else |
| 8944 | ptr = realloc(ptr, nsize); |
| 8945 | return ptr; |
| 8946 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8947 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8948 | if (!ptr) |
| 8949 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8950 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8951 | /* enforce strict limits across all threads */ |
| 8952 | limit = zone->limit; |
| 8953 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8954 | do { |
| 8955 | new = old + nsize - osize; |
| 8956 | if (unlikely(nsize && limit && new > limit)) |
| 8957 | return NULL; |
| 8958 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8959 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8960 | if (!nsize) |
| 8961 | ha_free(&ptr); |
| 8962 | else |
| 8963 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8964 | |
| 8965 | if (unlikely(!ptr && nsize)) // failed |
| 8966 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8967 | |
| 8968 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8969 | return ptr; |
| 8970 | } |
| 8971 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8972 | /* 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] | 8973 | * We are in the initialisation process of HAProxy, this abort() is |
| 8974 | * tolerated. |
| 8975 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8976 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8977 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8978 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8979 | int idx; |
| 8980 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8981 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8982 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8983 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8984 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8985 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8986 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8987 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8988 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8989 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8990 | |
firexinghe | bdd336c | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 8991 | if (!L) { |
| 8992 | fprintf(stderr, |
| 8993 | "Lua init: critical error: lua_newstate() returned NULL." |
| 8994 | " This may possibly be caused by a memory allocation error.\n"); |
| 8995 | exit(1); |
| 8996 | } |
| 8997 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8998 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8999 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9000 | *context = NULL; |
| 9001 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9002 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9003 | * the Lua function can fail with an abort. We are in the initialisation |
| 9004 | * process of HAProxy, this abort() is tolerated. |
| 9005 | */ |
| 9006 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9007 | /* Call post initialisation function in safe environment. */ |
| 9008 | if (setjmp(safe_ljmp_env) != 0) { |
| 9009 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9010 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9011 | error_msg = hlua_tostring_safe(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9012 | else |
| 9013 | error_msg = "critical error"; |
| 9014 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 9015 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9016 | } else { |
| 9017 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9018 | } |
| 9019 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9020 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9021 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9022 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 9023 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 9024 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9025 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9026 | #endif |
| 9027 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9028 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9029 | #endif |
| 9030 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 9031 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9032 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9033 | /* Apply configured prepend path */ |
| 9034 | list_for_each_entry(pp, &prepend_path_list, l) |
| 9035 | hlua_prepend_path(L, pp->type, pp->path); |
| 9036 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9037 | /* |
| 9038 | * |
| 9039 | * Create "core" object. |
| 9040 | * |
| 9041 | */ |
| 9042 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 9043 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9044 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9045 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9046 | /* set the thread id */ |
| 9047 | hlua_class_const_int(L, "thread", thread_num); |
| 9048 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9049 | /* Push the loglevel constants. */ |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 9050 | hlua_class_const_int(L, "silent", -1); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 9051 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9052 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9053 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9054 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9055 | hlua_class_function(L, "register_init", hlua_register_init); |
| 9056 | hlua_class_function(L, "register_task", hlua_register_task); |
| 9057 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 9058 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 9059 | hlua_class_function(L, "register_action", hlua_register_action); |
| 9060 | hlua_class_function(L, "register_service", hlua_register_service); |
| 9061 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 9062 | hlua_class_function(L, "yield", hlua_yield); |
| 9063 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 9064 | hlua_class_function(L, "sleep", hlua_sleep); |
| 9065 | hlua_class_function(L, "msleep", hlua_msleep); |
| 9066 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 9067 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 9068 | hlua_class_function(L, "set_map", hlua_set_map); |
| 9069 | hlua_class_function(L, "del_map", hlua_del_map); |
| 9070 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 9071 | hlua_class_function(L, "log", hlua_log); |
| 9072 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 9073 | hlua_class_function(L, "Info", hlua_log_info); |
| 9074 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 9075 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 9076 | hlua_class_function(L, "done", hlua_done); |
| 9077 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9078 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9080 | |
| 9081 | /* |
| 9082 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9083 | * Create "act" object. |
| 9084 | * |
| 9085 | */ |
| 9086 | |
| 9087 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9088 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9089 | |
| 9090 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9091 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 9092 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 9093 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 9094 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9095 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9096 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9097 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9098 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9099 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9100 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9101 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9102 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9103 | |
| 9104 | /* |
| 9105 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9106 | * Register class Map |
| 9107 | * |
| 9108 | */ |
| 9109 | |
| 9110 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9111 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9112 | |
| 9113 | /* register pattern types. */ |
| 9114 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9115 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9116 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9117 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9118 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9119 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9120 | |
| 9121 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9122 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9123 | |
| 9124 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9125 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9126 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9127 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9128 | lua_pushstring(L, "__index"); |
| 9129 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9130 | |
| 9131 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9132 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9133 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9134 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9135 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9136 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9137 | /* Register previous table in the registry with reference and named entry. |
| 9138 | * The function hlua_register_metatable() pops the stack, so we |
| 9139 | * previously create a copy of the table. |
| 9140 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9141 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9142 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9143 | |
| 9144 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9145 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9146 | |
| 9147 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9148 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9149 | |
| 9150 | /* |
| 9151 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9152 | * Register class Channel |
| 9153 | * |
| 9154 | */ |
| 9155 | |
| 9156 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9157 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9158 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9159 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9160 | lua_pushstring(L, "__index"); |
| 9161 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9162 | |
| 9163 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9164 | hlua_class_function(L, "get", hlua_channel_get); |
| 9165 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9166 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9167 | hlua_class_function(L, "set", hlua_channel_set); |
| 9168 | hlua_class_function(L, "append", hlua_channel_append); |
| 9169 | hlua_class_function(L, "send", hlua_channel_send); |
| 9170 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9171 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9172 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9173 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9174 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9175 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9176 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9177 | |
| 9178 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9179 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9180 | |
| 9181 | /* |
| 9182 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9183 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9184 | * |
| 9185 | */ |
| 9186 | |
| 9187 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9188 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9189 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9190 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9191 | lua_pushstring(L, "__index"); |
| 9192 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9193 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9194 | /* Browse existing fetches and create the associated |
| 9195 | * object method. |
| 9196 | */ |
| 9197 | sf = NULL; |
| 9198 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9199 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9200 | * by an underscore. |
| 9201 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9202 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9203 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9204 | if (*p == '.' || *p == '-' || *p == '+') |
| 9205 | *p = '_'; |
| 9206 | |
| 9207 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9208 | lua_pushstring(L, trash.area); |
| 9209 | lua_pushlightuserdata(L, sf); |
| 9210 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9211 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9212 | } |
| 9213 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9214 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9215 | |
| 9216 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9217 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9218 | |
| 9219 | /* |
| 9220 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9221 | * Register class Converters |
| 9222 | * |
| 9223 | */ |
| 9224 | |
| 9225 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9226 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9227 | |
| 9228 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9229 | lua_pushstring(L, "__index"); |
| 9230 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9231 | |
| 9232 | /* Browse existing converters and create the associated |
| 9233 | * object method. |
| 9234 | */ |
| 9235 | sc = NULL; |
| 9236 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9237 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9238 | * by an underscore. |
| 9239 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9240 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9241 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9242 | if (*p == '.' || *p == '-' || *p == '+') |
| 9243 | *p = '_'; |
| 9244 | |
| 9245 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9246 | lua_pushstring(L, trash.area); |
| 9247 | lua_pushlightuserdata(L, sc); |
| 9248 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9249 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9250 | } |
| 9251 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9252 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9253 | |
| 9254 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9255 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9256 | |
| 9257 | /* |
| 9258 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9259 | * Register class HTTP |
| 9260 | * |
| 9261 | */ |
| 9262 | |
| 9263 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9264 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9265 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9266 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9267 | lua_pushstring(L, "__index"); |
| 9268 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9269 | |
| 9270 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9271 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9272 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9273 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9274 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9275 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9276 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9277 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9278 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9279 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9280 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9281 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9282 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9283 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9284 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9285 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9286 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9287 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9288 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9289 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9290 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9291 | |
| 9292 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9293 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9294 | |
| 9295 | /* |
| 9296 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9297 | * Register class AppletTCP |
| 9298 | * |
| 9299 | */ |
| 9300 | |
| 9301 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9302 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9303 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9304 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9305 | lua_pushstring(L, "__index"); |
| 9306 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9307 | |
| 9308 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9309 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9310 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9311 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9312 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9313 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9314 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9315 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9316 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9317 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9318 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9319 | |
| 9320 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9321 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9322 | |
| 9323 | /* |
| 9324 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9325 | * Register class AppletHTTP |
| 9326 | * |
| 9327 | */ |
| 9328 | |
| 9329 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9330 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9331 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9332 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9333 | lua_pushstring(L, "__index"); |
| 9334 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9335 | |
| 9336 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9337 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9338 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9339 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9340 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9341 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9342 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9343 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9344 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9345 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9346 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9347 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9348 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9349 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9350 | |
| 9351 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9352 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9353 | |
| 9354 | /* |
| 9355 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9356 | * Register class TXN |
| 9357 | * |
| 9358 | */ |
| 9359 | |
| 9360 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9361 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9362 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9363 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9364 | lua_pushstring(L, "__index"); |
| 9365 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9366 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9367 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9368 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9369 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9370 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9371 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9372 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9373 | hlua_class_function(L, "done", hlua_txn_done); |
| 9374 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9375 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9376 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9377 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9378 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9379 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9380 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9381 | hlua_class_function(L, "log", hlua_txn_log); |
| 9382 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9383 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9384 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9385 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9386 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9387 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9388 | |
| 9389 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9390 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9391 | |
| 9392 | /* |
| 9393 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9394 | * Register class reply |
| 9395 | * |
| 9396 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9397 | lua_newtable(L); |
| 9398 | lua_pushstring(L, "__index"); |
| 9399 | lua_newtable(L); |
| 9400 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9401 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9402 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9403 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9404 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9405 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9406 | |
| 9407 | |
| 9408 | /* |
| 9409 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9410 | * Register class Socket |
| 9411 | * |
| 9412 | */ |
| 9413 | |
| 9414 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9415 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9416 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9417 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9418 | lua_pushstring(L, "__index"); |
| 9419 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9420 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9421 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9422 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9423 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9424 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9425 | hlua_class_function(L, "send", hlua_socket_send); |
| 9426 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9427 | hlua_class_function(L, "close", hlua_socket_close); |
| 9428 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9429 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9430 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9431 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9432 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9433 | 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] | 9434 | |
| 9435 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9436 | lua_pushstring(L, "__gc"); |
| 9437 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9438 | 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] | 9439 | |
| 9440 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9441 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9442 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9443 | lua_atpanic(L, hlua_panic_safe); |
| 9444 | |
| 9445 | return L; |
| 9446 | } |
| 9447 | |
| 9448 | void hlua_init(void) { |
| 9449 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9450 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9451 | #ifdef USE_OPENSSL |
| 9452 | struct srv_kw *kw; |
| 9453 | int tmp_error; |
| 9454 | char *error; |
| 9455 | char *args[] = { /* SSL client configuration. */ |
| 9456 | "ssl", |
| 9457 | "verify", |
| 9458 | "none", |
| 9459 | NULL |
| 9460 | }; |
| 9461 | #endif |
| 9462 | |
| 9463 | /* Init post init function list head */ |
| 9464 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9465 | LIST_INIT(&hlua_init_functions[i]); |
| 9466 | |
| 9467 | /* Init state for common/shared lua parts */ |
| 9468 | hlua_state_id = 0; |
| 9469 | ha_set_tid(0); |
| 9470 | hlua_states[0] = hlua_init_state(0); |
| 9471 | |
| 9472 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9473 | hlua_state_id = 1; |
| 9474 | ha_set_tid(0); |
| 9475 | hlua_states[1] = hlua_init_state(1); |
| 9476 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9477 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9478 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9479 | if (!socket_proxy) { |
| 9480 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9481 | exit(1); |
| 9482 | } |
| 9483 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9484 | |
| 9485 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9486 | socket_tcp = new_server(socket_proxy); |
| 9487 | if (!socket_tcp) { |
| 9488 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9489 | exit(1); |
| 9490 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9491 | |
| 9492 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9493 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9494 | socket_ssl = new_server(socket_proxy); |
| 9495 | if (!socket_ssl) { |
| 9496 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9497 | exit(1); |
| 9498 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9499 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9500 | socket_ssl->use_ssl = 1; |
| 9501 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9502 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9503 | for (i = 0; args[i] != NULL; i++) { |
| 9504 | 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] | 9505 | /* |
| 9506 | * |
| 9507 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9508 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9509 | * features like client certificates and ssl_verify. |
| 9510 | * |
| 9511 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9512 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9513 | if (tmp_error != 0) { |
| 9514 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9515 | abort(); /* This must be never arrives because the command line |
| 9516 | not editable by the user. */ |
| 9517 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9518 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9519 | } |
| 9520 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9521 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9522 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9523 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9524 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9525 | static void hlua_deinit() |
| 9526 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9527 | int thr; |
| 9528 | |
| 9529 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9530 | if (hlua_states[thr]) |
| 9531 | lua_close(hlua_states[thr]); |
| 9532 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9533 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9534 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9535 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9536 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9537 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9538 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9539 | |
| 9540 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9541 | } |
| 9542 | |
| 9543 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9544 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9545 | static void hlua_register_build_options(void) |
| 9546 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9547 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9548 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9549 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9550 | hap_register_build_opts(ptr, 1); |
| 9551 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9552 | |
| 9553 | INITCALL0(STG_REGISTER, hlua_register_build_options); |