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 | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 1331 | /* Helper function to get the lua ctx for a given stream */ |
| 1332 | static inline struct hlua *hlua_stream_ctx_get(struct stream *s) |
| 1333 | { |
| 1334 | return s->hlua; |
| 1335 | } |
| 1336 | |
| 1337 | /* Helper function to prepare the lua ctx for a given stream |
| 1338 | * |
| 1339 | * ctx will be enforced in <state_id> parent stack on initial creation. |
| 1340 | * If s->hlua->state_id differs from <state_id>, which may happen at |
| 1341 | * runtime since existing stream hlua ctx will be reused for other |
| 1342 | * "independent" (but stream-related) lua executions, hlua will be |
| 1343 | * recreated with the expected state id. |
| 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 | */ |
| 1354 | ctx_renew: |
| 1355 | if (!s->hlua) { |
| 1356 | struct hlua *hlua; |
| 1357 | |
| 1358 | hlua = pool_alloc(pool_head_hlua); |
| 1359 | if (!hlua) |
| 1360 | return NULL; |
| 1361 | HLUA_INIT(hlua); |
| 1362 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 1363 | pool_free(pool_head_hlua, hlua); |
| 1364 | return NULL; |
| 1365 | } |
| 1366 | s->hlua = hlua; |
| 1367 | } |
| 1368 | else if (s->hlua->state_id != state_id) { |
| 1369 | /* ctx already created, but not in proper state. |
| 1370 | * It should only happen after the previous execution is |
| 1371 | * finished, otherwise it's probably a bug since we don't |
| 1372 | * want to abort unfinished job.. |
| 1373 | */ |
| 1374 | BUG_ON(HLUA_IS_RUNNING(s->hlua)); |
| 1375 | hlua_ctx_destroy(s->hlua); |
| 1376 | s->hlua = NULL; |
| 1377 | goto ctx_renew; |
| 1378 | } |
| 1379 | return s->hlua; |
| 1380 | } |
| 1381 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1382 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1383 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1384 | struct hlua *hlua; |
| 1385 | |
| 1386 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1387 | hlua = hlua_gethlua(L); |
| 1388 | if (!hlua) |
| 1389 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* Lua cannot yield when its returning from a function, |
| 1392 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1393 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1394 | */ |
| 1395 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1396 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | /* restore the interrupt condition. */ |
| 1401 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1402 | |
| 1403 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1404 | * If the state is not yieldable, trying yield causes an error. |
| 1405 | */ |
| 1406 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1407 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1408 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1409 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1410 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1411 | hlua->run_time += now_ms - hlua->start_time; |
| 1412 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1413 | lua_pushfstring(L, "execution timeout"); |
| 1414 | WILL_LJMP(lua_error(L)); |
| 1415 | } |
| 1416 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1417 | /* Update the start time. */ |
| 1418 | hlua->start_time = now_ms; |
| 1419 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1420 | /* Try to interrupt the process at the end of the current |
| 1421 | * unyieldable function. |
| 1422 | */ |
| 1423 | 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] | 1424 | } |
| 1425 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1426 | /* This function start or resumes the Lua stack execution. If the flag |
| 1427 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1428 | * The function return an error. |
| 1429 | * |
| 1430 | * The function can returns 4 values: |
| 1431 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1432 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1433 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1434 | * - 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] | 1435 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1436 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1437 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1438 | * 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] | 1439 | * LUA code. |
| 1440 | */ |
| 1441 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1442 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1443 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1444 | int nres; |
| 1445 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1446 | int ret; |
| 1447 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1448 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1449 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1450 | /* Initialise run time counter. */ |
| 1451 | if (!HLUA_IS_RUNNING(lua)) |
| 1452 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1453 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1454 | /* Lock the whole Lua execution. This lock must be before the |
| 1455 | * label "resume_execution". |
| 1456 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1457 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1458 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1459 | resume_execution: |
| 1460 | |
| 1461 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1462 | * instructions. it is used for preventing infinite loops. |
| 1463 | */ |
| 1464 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1465 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1466 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1467 | HLUA_SET_RUN(lua); |
| 1468 | HLUA_CLR_CTRLYIELD(lua); |
| 1469 | HLUA_CLR_WAKERESWR(lua); |
| 1470 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1471 | HLUA_CLR_NOYIELD(lua); |
| 1472 | if (!yield_allowed) |
| 1473 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1474 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1475 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1476 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1477 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1478 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1479 | HLUA_SET_BUSY(lua); |
| 1480 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1481 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1482 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1483 | 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] | 1484 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1485 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1486 | #endif |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1487 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1488 | HLUA_CLR_BUSY(lua); |
| 1489 | |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1490 | /* reset nargs because those possibly passed to the lua_resume() call |
| 1491 | * were already consumed, and since we may call lua_resume() again |
| 1492 | * after a successful yield, we don't want to pass stale nargs hint |
| 1493 | * to the Lua API. As such, nargs should be set explicitly before each |
| 1494 | * lua_resume() (or hlua_ctx_resume()) invocation if needed. |
| 1495 | */ |
| 1496 | lua->nargs = 0; |
| 1497 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1498 | switch (ret) { |
| 1499 | |
| 1500 | case LUA_OK: |
| 1501 | ret = HLUA_E_OK; |
| 1502 | break; |
| 1503 | |
| 1504 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1505 | /* Check if the execution timeout is expired. It it is the case, we |
| 1506 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1507 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1508 | tv_update_date(0, 1); |
| 1509 | lua->run_time += now_ms - lua->start_time; |
| 1510 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1511 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1512 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1513 | break; |
| 1514 | } |
| 1515 | /* Process the forced yield. if the general yield is not allowed or |
| 1516 | * if no task were associated this the current Lua execution |
| 1517 | * coroutine, we resume the execution. Else we want to return in the |
| 1518 | * scheduler and we want to be waked up again, to continue the |
| 1519 | * current Lua execution. So we schedule our own task. |
| 1520 | */ |
| 1521 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1522 | if (!yield_allowed || !lua->task) |
| 1523 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1524 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1525 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1526 | if (!yield_allowed) { |
| 1527 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1528 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1529 | break; |
| 1530 | } |
| 1531 | ret = HLUA_E_AGAIN; |
| 1532 | break; |
| 1533 | |
| 1534 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1535 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1536 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1537 | * because the errors ares the only one mean to return immediately |
| 1538 | * from and lua execution. |
| 1539 | */ |
| 1540 | if (lua->flags & HLUA_EXIT) { |
| 1541 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1542 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1543 | break; |
| 1544 | } |
| 1545 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1546 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1547 | if (!lua_checkstack(lua->T, 1)) { |
| 1548 | ret = HLUA_E_ERR; |
| 1549 | break; |
| 1550 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1551 | msg = hlua_tostring_safe(lua->T, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1552 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1553 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1554 | 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] | 1555 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1556 | 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] | 1557 | |
| 1558 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1559 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1560 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1561 | ret = HLUA_E_ERRMSG; |
| 1562 | break; |
| 1563 | |
| 1564 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1565 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1566 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1567 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1568 | break; |
| 1569 | |
| 1570 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1571 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1572 | if (!lua_checkstack(lua->T, 1)) { |
| 1573 | ret = HLUA_E_ERR; |
| 1574 | break; |
| 1575 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1576 | msg = hlua_tostring_safe(lua->T, -1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1577 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1578 | 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] | 1579 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1580 | 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] | 1581 | |
| 1582 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1583 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1584 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1585 | ret = HLUA_E_ERRMSG; |
| 1586 | break; |
| 1587 | |
| 1588 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1589 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1590 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1591 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1592 | break; |
| 1593 | } |
| 1594 | |
| 1595 | switch (ret) { |
| 1596 | case HLUA_E_AGAIN: |
| 1597 | break; |
| 1598 | |
| 1599 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1600 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1601 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1602 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1603 | break; |
| 1604 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1605 | case HLUA_E_ETMOUT: |
| 1606 | case HLUA_E_NOMEM: |
| 1607 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1608 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1609 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1611 | hlua_ctx_renew(lua, 0); |
| 1612 | break; |
| 1613 | |
| 1614 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1615 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1616 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1617 | break; |
| 1618 | } |
| 1619 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1620 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1621 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1622 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1623 | return ret; |
| 1624 | } |
| 1625 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1626 | /* This function exit the current code. */ |
| 1627 | __LJMP static int hlua_done(lua_State *L) |
| 1628 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1629 | struct hlua *hlua; |
| 1630 | |
| 1631 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1632 | hlua = hlua_gethlua(L); |
| 1633 | if (!hlua) |
| 1634 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1635 | |
| 1636 | hlua->flags |= HLUA_EXIT; |
| 1637 | WILL_LJMP(lua_error(L)); |
| 1638 | |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1642 | /* This function is an LUA binding. It provides a function |
| 1643 | * for deleting ACL from a referenced ACL file. |
| 1644 | */ |
| 1645 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1646 | { |
| 1647 | const char *name; |
| 1648 | const char *key; |
| 1649 | struct pat_ref *ref; |
| 1650 | |
| 1651 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1652 | |
| 1653 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1654 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1655 | |
| 1656 | ref = pat_ref_lookup(name); |
| 1657 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1658 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1659 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1660 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1661 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1662 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | /* This function is an LUA binding. It provides a function |
| 1667 | * for deleting map entry from a referenced map file. |
| 1668 | */ |
| 1669 | static int hlua_del_map(lua_State *L) |
| 1670 | { |
| 1671 | const char *name; |
| 1672 | const char *key; |
| 1673 | struct pat_ref *ref; |
| 1674 | |
| 1675 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1676 | |
| 1677 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1678 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1679 | |
| 1680 | ref = pat_ref_lookup(name); |
| 1681 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1682 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1683 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1684 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1685 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1686 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | /* This function is an LUA binding. It provides a function |
| 1691 | * for adding ACL pattern from a referenced ACL file. |
| 1692 | */ |
| 1693 | static int hlua_add_acl(lua_State *L) |
| 1694 | { |
| 1695 | const char *name; |
| 1696 | const char *key; |
| 1697 | struct pat_ref *ref; |
| 1698 | |
| 1699 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1700 | |
| 1701 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1702 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1703 | |
| 1704 | ref = pat_ref_lookup(name); |
| 1705 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1706 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1707 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1708 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1709 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1710 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1711 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | /* This function is an LUA binding. It provides a function |
| 1716 | * for setting map pattern and sample from a referenced map |
| 1717 | * file. |
| 1718 | */ |
| 1719 | static int hlua_set_map(lua_State *L) |
| 1720 | { |
| 1721 | const char *name; |
| 1722 | const char *key; |
| 1723 | const char *value; |
| 1724 | struct pat_ref *ref; |
| 1725 | |
| 1726 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1727 | |
| 1728 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1729 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1730 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1731 | |
| 1732 | ref = pat_ref_lookup(name); |
| 1733 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1734 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1735 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1736 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1737 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1738 | pat_ref_set(ref, key, value, NULL); |
| 1739 | else |
| 1740 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1741 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1745 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1746 | * 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] | 1747 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1748 | * the name of the object (_G[<name>] = <metable> ). |
| 1749 | * |
| 1750 | * A metable is a table that modify the standard behavior of a standard |
| 1751 | * access to the associated data. The entries of this new metatable are |
| 1752 | * defined as is: |
| 1753 | * |
| 1754 | * http://lua-users.org/wiki/MetatableEvents |
| 1755 | * |
| 1756 | * __index |
| 1757 | * |
| 1758 | * we access an absent field in a table, the result is nil. This is |
| 1759 | * true, but it is not the whole truth. Actually, such access triggers |
| 1760 | * the interpreter to look for an __index metamethod: If there is no |
| 1761 | * such method, as usually happens, then the access results in nil; |
| 1762 | * otherwise, the metamethod will provide the result. |
| 1763 | * |
| 1764 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1765 | * the key does not appear in the table, but the metatable has an __index |
| 1766 | * property: |
| 1767 | * |
| 1768 | * - if the value is a function, the function is called, passing in the |
| 1769 | * table and the key; the return value of that function is returned as |
| 1770 | * the result. |
| 1771 | * |
| 1772 | * - if the value is another table, the value of the key in that table is |
| 1773 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1774 | * table's metatable has an __index property, then it continues on up) |
| 1775 | * |
| 1776 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1777 | * |
| 1778 | * http://www.lua.org/pil/13.4.1.html |
| 1779 | * |
| 1780 | * __newindex |
| 1781 | * |
| 1782 | * Like __index, but control property assignment. |
| 1783 | * |
| 1784 | * __mode - Control weak references. A string value with one or both |
| 1785 | * of the characters 'k' and 'v' which specifies that the the |
| 1786 | * keys and/or values in the table are weak references. |
| 1787 | * |
| 1788 | * __call - Treat a table like a function. When a table is followed by |
| 1789 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1790 | * a __call key pointing to a function, that function is invoked |
| 1791 | * (passing any specified arguments) and the return value is |
| 1792 | * returned. |
| 1793 | * |
| 1794 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1795 | * called, if the metatable for myTable has a __metatable |
| 1796 | * key, the value of that key is returned instead of the |
| 1797 | * actual metatable. |
| 1798 | * |
| 1799 | * __tostring - Control string representation. When the builtin |
| 1800 | * "tostring( myTable )" function is called, if the metatable |
| 1801 | * for myTable has a __tostring property set to a function, |
| 1802 | * that function is invoked (passing myTable to it) and the |
| 1803 | * return value is used as the string representation. |
| 1804 | * |
| 1805 | * __len - Control table length. When the table length is requested using |
| 1806 | * the length operator ( '#' ), if the metatable for myTable has |
| 1807 | * a __len key pointing to a function, that function is invoked |
| 1808 | * (passing myTable to it) and the return value used as the value |
| 1809 | * of "#myTable". |
| 1810 | * |
| 1811 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1812 | * collected, if the metatable has a __gc field pointing to a |
| 1813 | * function, that function is first invoked, passing the userdata |
| 1814 | * to it. The __gc metamethod is not called for tables. |
| 1815 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1816 | * |
| 1817 | * Special metamethods for redefining standard operators: |
| 1818 | * http://www.lua.org/pil/13.1.html |
| 1819 | * |
| 1820 | * __add "+" |
| 1821 | * __sub "-" |
| 1822 | * __mul "*" |
| 1823 | * __div "/" |
| 1824 | * __unm "!" |
| 1825 | * __pow "^" |
| 1826 | * __concat ".." |
| 1827 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1828 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1829 | * http://www.lua.org/pil/13.2.html |
| 1830 | * |
| 1831 | * __eq "==" |
| 1832 | * __lt "<" |
| 1833 | * __le "<=" |
| 1834 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | |
| 1836 | /* |
| 1837 | * |
| 1838 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1839 | * Class Map |
| 1840 | * |
| 1841 | * |
| 1842 | */ |
| 1843 | |
| 1844 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1845 | * a class session, otherwise it throws an error. |
| 1846 | */ |
| 1847 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1848 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1849 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* This function is the map constructor. It don't need |
| 1853 | * the class Map object. It creates and return a new Map |
| 1854 | * object. It must be called only during "body" or "init" |
| 1855 | * context because it process some filesystem accesses. |
| 1856 | */ |
| 1857 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1858 | { |
| 1859 | const char *fn; |
| 1860 | int match = PAT_MATCH_STR; |
| 1861 | struct sample_conv conv; |
| 1862 | const char *file = ""; |
| 1863 | int line = 0; |
| 1864 | lua_Debug ar; |
| 1865 | char *err = NULL; |
| 1866 | struct arg args[2]; |
| 1867 | |
| 1868 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1869 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1870 | |
| 1871 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1872 | |
| 1873 | if (lua_gettop(L) >= 2) { |
| 1874 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1875 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1876 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1877 | } |
| 1878 | |
| 1879 | /* Get Lua filename and line number. */ |
| 1880 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1881 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1882 | if (ar.currentline > 0) { /* is there info? */ |
| 1883 | file = ar.short_src; |
| 1884 | line = ar.currentline; |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | /* fill fake sample_conv struct. */ |
| 1889 | conv.kw = ""; /* unused. */ |
| 1890 | conv.process = NULL; /* unused. */ |
| 1891 | conv.arg_mask = 0; /* unused. */ |
| 1892 | conv.val_args = NULL; /* unused. */ |
| 1893 | conv.out_type = SMP_T_STR; |
| 1894 | conv.private = (void *)(long)match; |
| 1895 | switch (match) { |
| 1896 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1897 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1898 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1899 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1900 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1901 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1902 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1903 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1904 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1905 | default: |
| 1906 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1907 | } |
| 1908 | |
| 1909 | /* fill fake args. */ |
| 1910 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1911 | args[0].data.str.area = strdup(fn); |
| 1912 | args[0].data.str.data = strlen(fn); |
| 1913 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1914 | args[1].type = ARGT_STOP; |
| 1915 | |
| 1916 | /* load the map. */ |
| 1917 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1918 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1919 | * free the err variable. |
| 1920 | */ |
| 1921 | luaL_where(L, 1); |
| 1922 | lua_pushfstring(L, "'new': %s.", err); |
| 1923 | lua_concat(L, 2); |
| 1924 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1925 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1926 | WILL_LJMP(lua_error(L)); |
| 1927 | } |
| 1928 | |
| 1929 | /* create the lua object. */ |
| 1930 | lua_newtable(L); |
| 1931 | lua_pushlightuserdata(L, args[0].data.map); |
| 1932 | lua_rawseti(L, -2, 0); |
| 1933 | |
| 1934 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1935 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1936 | lua_setmetatable(L, -2); |
| 1937 | |
| 1938 | |
| 1939 | return 1; |
| 1940 | } |
| 1941 | |
| 1942 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1943 | { |
| 1944 | struct map_descriptor *desc; |
| 1945 | struct pattern *pat; |
| 1946 | struct sample smp; |
| 1947 | |
| 1948 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1949 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1950 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1951 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1952 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1953 | } |
| 1954 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1955 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1956 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1957 | 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] | 1958 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1962 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1963 | if (str) |
| 1964 | lua_pushstring(L, ""); |
| 1965 | else |
| 1966 | lua_pushnil(L); |
| 1967 | return 1; |
| 1968 | } |
| 1969 | |
| 1970 | /* 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] | 1971 | 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] | 1972 | return 1; |
| 1973 | } |
| 1974 | |
| 1975 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1976 | { |
| 1977 | return _hlua_map_lookup(L, 0); |
| 1978 | } |
| 1979 | |
| 1980 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1981 | { |
| 1982 | return _hlua_map_lookup(L, 1); |
| 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * |
| 1987 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | * Class Socket |
| 1989 | * |
| 1990 | * |
| 1991 | */ |
| 1992 | |
| 1993 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1994 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1995 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | /* 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] | 1999 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | * received. |
| 2001 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2002 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2004 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2006 | if (appctx->ctx.hlua_cosocket.die) { |
| 2007 | si_shutw(si); |
| 2008 | si_shutr(si); |
| 2009 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2010 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 2011 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2012 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 2013 | } |
| 2014 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2015 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2016 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2017 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2018 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2019 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2020 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2021 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2022 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2023 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2024 | * to be notified whenever the connection completes. |
| 2025 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2026 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2027 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 2028 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2029 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2030 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2031 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | |
| 2033 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2034 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2036 | /* 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] | 2037 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2038 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | |
| 2040 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2041 | if (!channel_is_empty(si_oc(si))) { |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2042 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2043 | si_cant_get(si); |
| 2044 | } |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2045 | |
| 2046 | /* Some data were injected in the buffer, notify the stream |
| 2047 | * interface. |
| 2048 | */ |
| 2049 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 2050 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2051 | |
| 2052 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2053 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2054 | */ |
| 2055 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2056 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2059 | /* This function is called when the "struct stream" is destroyed. |
| 2060 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | * Wake all the pending signals. |
| 2062 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2063 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2065 | struct xref *peer; |
| 2066 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2068 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 2069 | if (peer) |
| 2070 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2073 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 2074 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2078 | * uses this object. If the stream does not exists, just quit. |
| 2079 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | * pending signal can rest in the read and write lists. destroy |
| 2081 | * it. |
| 2082 | */ |
| 2083 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2084 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2085 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2087 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2089 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2090 | |
| 2091 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2092 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2093 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2095 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2097 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2098 | appctx->ctx.hlua_cosocket.die = 1; |
| 2099 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2101 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2102 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2103 | return 0; |
| 2104 | } |
| 2105 | |
| 2106 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2107 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2109 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2111 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2113 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2114 | struct hlua *hlua; |
| 2115 | |
| 2116 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2117 | hlua = hlua_gethlua(L); |
| 2118 | if (!hlua) |
| 2119 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2121 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2122 | |
| 2123 | /* Check if we run on the same thread than the xreator thread. |
| 2124 | * We cannot access to the socket if the thread is different. |
| 2125 | */ |
| 2126 | if (socket->tid != tid) |
| 2127 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2128 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2129 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2130 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2132 | |
| 2133 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2134 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2135 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2136 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2137 | appctx->ctx.hlua_cosocket.die = 1; |
| 2138 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2140 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2141 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | return 0; |
| 2143 | } |
| 2144 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2145 | /* The close function calls close_helper. |
| 2146 | */ |
| 2147 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2148 | { |
| 2149 | MAY_LJMP(check_args(L, 1, "close")); |
| 2150 | return hlua_socket_close_helper(L); |
| 2151 | } |
| 2152 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | /* This Lua function assumes that the stack contain three parameters. |
| 2154 | * 1 - USERDATA containing a struct socket |
| 2155 | * 2 - INTEGER with values of the macro defined below |
| 2156 | * If the integer is -1, we must read at most one line. |
| 2157 | * If the integer is -2, we ust read all the data until the |
| 2158 | * end of the stream. |
| 2159 | * If the integer is positive value, we must read a number of |
| 2160 | * bytes corresponding to this value. |
| 2161 | */ |
| 2162 | #define HLSR_READ_LINE (-1) |
| 2163 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2164 | __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] | 2165 | { |
| 2166 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2167 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2168 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2170 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2172 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2173 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2174 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2175 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2176 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2177 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2178 | struct stream_interface *si; |
| 2179 | struct stream *s; |
| 2180 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2181 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2183 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2184 | hlua = hlua_gethlua(L); |
| 2185 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | /* Check if this lua stack is schedulable. */ |
| 2187 | if (!hlua || !hlua->task) |
| 2188 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2189 | "'frontend', 'backend' or 'task'")); |
| 2190 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2191 | /* Check if we run on the same thread than the xreator thread. |
| 2192 | * We cannot access to the socket if the thread is different. |
| 2193 | */ |
| 2194 | if (socket->tid != tid) |
| 2195 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2196 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2197 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2198 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2199 | if (!peer) |
| 2200 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2201 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2202 | si = appctx->owner; |
| 2203 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2205 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2206 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2208 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2209 | if (nblk < 0) /* Connection close. */ |
| 2210 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2211 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2212 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2213 | |
| 2214 | /* remove final \r\n. */ |
| 2215 | if (nblk == 1) { |
| 2216 | if (blk1[len1-1] == '\n') { |
| 2217 | len1--; |
| 2218 | skip_at_end++; |
| 2219 | if (blk1[len1-1] == '\r') { |
| 2220 | len1--; |
| 2221 | skip_at_end++; |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | else { |
| 2226 | if (blk2[len2-1] == '\n') { |
| 2227 | len2--; |
| 2228 | skip_at_end++; |
| 2229 | if (blk2[len2-1] == '\r') { |
| 2230 | len2--; |
| 2231 | skip_at_end++; |
| 2232 | } |
| 2233 | } |
| 2234 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2239 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | if (nblk < 0) /* Connection close. */ |
| 2241 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2242 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | goto connection_empty; |
| 2244 | } |
| 2245 | |
| 2246 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2248 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | if (nblk < 0) /* Connection close. */ |
| 2250 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2251 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | goto connection_empty; |
| 2253 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2254 | missing_bytes = wanted - socket->b.n; |
| 2255 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2256 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2257 | len1 = missing_bytes; |
| 2258 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2259 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | len = len1; |
| 2263 | |
| 2264 | luaL_addlstring(&socket->b, blk1, len1); |
| 2265 | if (nblk == 2) { |
| 2266 | len += len2; |
| 2267 | luaL_addlstring(&socket->b, blk2, len2); |
| 2268 | } |
| 2269 | |
| 2270 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2271 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
| 2273 | /* Don't wait anything. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2274 | si_want_get(si); |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2275 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | |
| 2277 | /* If the pattern reclaim to read all the data |
| 2278 | * in the connection, got out. |
| 2279 | */ |
| 2280 | if (wanted == HLSR_READ_ALL) |
| 2281 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2282 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | goto connection_empty; |
| 2284 | |
| 2285 | /* Return result. */ |
| 2286 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2287 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | return 1; |
| 2289 | |
| 2290 | connection_closed: |
| 2291 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2292 | xref_unlock(&socket->xref, peer); |
| 2293 | |
| 2294 | no_peer: |
| 2295 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | /* If the buffer containds data. */ |
| 2297 | if (socket->b.n > 0) { |
| 2298 | luaL_pushresult(&socket->b); |
| 2299 | return 1; |
| 2300 | } |
| 2301 | lua_pushnil(L); |
| 2302 | lua_pushstring(L, "connection closed."); |
| 2303 | return 2; |
| 2304 | |
| 2305 | connection_empty: |
| 2306 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2307 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2308 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2310 | } |
| 2311 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2312 | 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] | 2313 | return 0; |
| 2314 | } |
| 2315 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2316 | /* This Lua function gets two parameters. The first one can be string |
| 2317 | * or a number. If the string is "*l", the user requires one line. If |
| 2318 | * 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] | 2319 | * If the value is a number, the user require a number of bytes equal |
| 2320 | * to the value. The default value is "*l" (a line). |
| 2321 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2322 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | * integer takes this values: |
| 2324 | * -1 : read a line |
| 2325 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2326 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2328 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | * concatenated with the read data. |
| 2330 | */ |
| 2331 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2332 | { |
| 2333 | int wanted = HLSR_READ_LINE; |
| 2334 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2335 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2336 | char *error; |
| 2337 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2338 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | |
| 2340 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2341 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2342 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2343 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2345 | /* Check if we run on the same thread than the xreator thread. |
| 2346 | * We cannot access to the socket if the thread is different. |
| 2347 | */ |
| 2348 | if (socket->tid != tid) |
| 2349 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2350 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | /* check for pattern. */ |
| 2352 | if (lua_gettop(L) >= 2) { |
| 2353 | type = lua_type(L, 2); |
| 2354 | if (type == LUA_TSTRING) { |
| 2355 | pattern = lua_tostring(L, 2); |
| 2356 | if (strcmp(pattern, "*a") == 0) |
| 2357 | wanted = HLSR_READ_ALL; |
| 2358 | else if (strcmp(pattern, "*l") == 0) |
| 2359 | wanted = HLSR_READ_LINE; |
| 2360 | else { |
| 2361 | wanted = strtoll(pattern, &error, 10); |
| 2362 | if (*error != '\0') |
| 2363 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2364 | } |
| 2365 | } |
| 2366 | else if (type == LUA_TNUMBER) { |
| 2367 | wanted = lua_tointeger(L, 2); |
| 2368 | if (wanted < 0) |
| 2369 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | /* Set pattern. */ |
| 2374 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2375 | |
| 2376 | /* Check if we would replace the top by itself. */ |
| 2377 | if (lua_gettop(L) != 2) |
| 2378 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2379 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2380 | /* Save index of the top of the stack because since buffers are used, it |
| 2381 | * may change |
| 2382 | */ |
| 2383 | lastarg = lua_gettop(L); |
| 2384 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2385 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | luaL_buffinit(L, &socket->b); |
| 2387 | |
| 2388 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2389 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2391 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2392 | pattern = lua_tolstring(L, 3, &len); |
| 2393 | luaL_addlstring(&socket->b, pattern, len); |
| 2394 | } |
| 2395 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2396 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2400 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2402 | 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] | 2403 | { |
| 2404 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2405 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | struct appctx *appctx; |
| 2407 | size_t buf_len; |
| 2408 | const char *buf; |
| 2409 | int len; |
| 2410 | int send_len; |
| 2411 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2412 | struct xref *peer; |
| 2413 | struct stream_interface *si; |
| 2414 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2416 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2417 | hlua = hlua_gethlua(L); |
| 2418 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | /* Check if this lua stack is schedulable. */ |
| 2420 | if (!hlua || !hlua->task) |
| 2421 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2422 | "'frontend', 'backend' or 'task'")); |
| 2423 | |
| 2424 | /* Get object */ |
| 2425 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2426 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2427 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2428 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2429 | /* Check if we run on the same thread than the xreator thread. |
| 2430 | * We cannot access to the socket if the thread is different. |
| 2431 | */ |
| 2432 | if (socket->tid != tid) |
| 2433 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2434 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2435 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2436 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2437 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2438 | lua_pushinteger(L, -1); |
| 2439 | return 1; |
| 2440 | } |
| 2441 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2442 | si = appctx->owner; |
| 2443 | s = si_strm(si); |
| 2444 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2448 | lua_pushinteger(L, -1); |
| 2449 | return 1; |
| 2450 | } |
| 2451 | |
| 2452 | /* Update the input buffer data. */ |
| 2453 | buf += sent; |
| 2454 | send_len = buf_len - sent; |
| 2455 | |
| 2456 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2457 | if (sent >= buf_len) { |
| 2458 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2460 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2462 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2463 | * the request buffer if its not required. |
| 2464 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2465 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2466 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2467 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2468 | } |
| 2469 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2470 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2471 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2472 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2474 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
| 2476 | /* send data */ |
| 2477 | if (len < send_len) |
| 2478 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2479 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | |
| 2481 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2482 | * the data" (-2) are not expected because the available length |
| 2483 | * is tested. |
| 2484 | * Other unknown error are also not expected. |
| 2485 | */ |
| 2486 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2487 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2489 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2490 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2492 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2494 | return 1; |
| 2495 | } |
| 2496 | |
| 2497 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2498 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2499 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2500 | s->req.rex = TICK_ETERNITY; |
| 2501 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
| 2503 | /* Update length sent. */ |
| 2504 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2505 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | |
| 2507 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | if (sent + len >= buf_len) { |
| 2509 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2511 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2512 | |
| 2513 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2514 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2515 | xref_unlock(&socket->xref, peer); |
| 2516 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2517 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2518 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2519 | 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] | 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | /* This function initiate the send of data. It just check the input |
| 2524 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2525 | * 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] | 2526 | * "hlua_socket_write_yield" that can yield. |
| 2527 | * |
| 2528 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2529 | * the associated object. The second is a string buffer. The third is |
| 2530 | * a facultative integer that represents where is the buffer position |
| 2531 | * of the start of the data that can send. The first byte is the |
| 2532 | * position "1". The default value is "1". The fourth argument is a |
| 2533 | * facultative integer that represents where is the buffer position |
| 2534 | * of the end of the data that can send. The default is the last byte. |
| 2535 | */ |
| 2536 | static int hlua_socket_send(struct lua_State *L) |
| 2537 | { |
| 2538 | int i; |
| 2539 | int j; |
| 2540 | const char *buf; |
| 2541 | size_t buf_len; |
| 2542 | |
| 2543 | /* Check number of arguments. */ |
| 2544 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2545 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2546 | |
| 2547 | /* Get the string. */ |
| 2548 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2549 | |
| 2550 | /* Get and check j. */ |
| 2551 | if (lua_gettop(L) == 4) { |
| 2552 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2553 | if (j < 0) |
| 2554 | j = buf_len + j + 1; |
| 2555 | if (j > buf_len) |
| 2556 | j = buf_len + 1; |
| 2557 | lua_pop(L, 1); |
| 2558 | } |
| 2559 | else |
| 2560 | j = buf_len; |
| 2561 | |
| 2562 | /* Get and check i. */ |
| 2563 | if (lua_gettop(L) == 3) { |
| 2564 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2565 | if (i < 0) |
| 2566 | i = buf_len + i + 1; |
| 2567 | if (i > buf_len) |
| 2568 | i = buf_len + 1; |
| 2569 | lua_pop(L, 1); |
| 2570 | } else |
| 2571 | i = 1; |
| 2572 | |
| 2573 | /* Check bth i and j. */ |
| 2574 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2575 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2576 | return 1; |
| 2577 | } |
| 2578 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2579 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | return 1; |
| 2581 | } |
| 2582 | if (i == 0) |
| 2583 | i = 1; |
| 2584 | if (j == 0) |
| 2585 | j = 1; |
| 2586 | |
| 2587 | /* Pop the string. */ |
| 2588 | lua_pop(L, 1); |
| 2589 | |
| 2590 | /* Update the buffer length. */ |
| 2591 | buf += i - 1; |
| 2592 | buf_len = j - i + 1; |
| 2593 | lua_pushlstring(L, buf, buf_len); |
| 2594 | |
| 2595 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2596 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2598 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2601 | #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] | 2602 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2603 | { |
| 2604 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2605 | int ret; |
| 2606 | int len; |
| 2607 | char *p; |
| 2608 | |
| 2609 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2610 | if (ret <= 0) { |
| 2611 | lua_pushnil(L); |
| 2612 | return 1; |
| 2613 | } |
| 2614 | |
| 2615 | if (ret == AF_UNIX) { |
| 2616 | lua_pushstring(L, buffer+1); |
| 2617 | return 1; |
| 2618 | } |
| 2619 | else if (ret == AF_INET6) { |
| 2620 | buffer[0] = '['; |
| 2621 | len = strlen(buffer); |
| 2622 | buffer[len] = ']'; |
| 2623 | len++; |
| 2624 | buffer[len] = ':'; |
| 2625 | len++; |
| 2626 | p = buffer; |
| 2627 | } |
| 2628 | else if (ret == AF_INET) { |
| 2629 | p = buffer + 1; |
| 2630 | len = strlen(p); |
| 2631 | p[len] = ':'; |
| 2632 | len++; |
| 2633 | } |
| 2634 | else { |
| 2635 | lua_pushnil(L); |
| 2636 | return 1; |
| 2637 | } |
| 2638 | |
| 2639 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2640 | lua_pushnil(L); |
| 2641 | return 1; |
| 2642 | } |
| 2643 | |
| 2644 | lua_pushstring(L, p); |
| 2645 | return 1; |
| 2646 | } |
| 2647 | |
| 2648 | /* Returns information about the peer of the connection. */ |
| 2649 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2650 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2651 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2652 | struct xref *peer; |
| 2653 | struct appctx *appctx; |
| 2654 | struct stream_interface *si; |
| 2655 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2656 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2657 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2659 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2660 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2662 | /* Check if we run on the same thread than the xreator thread. |
| 2663 | * We cannot access to the socket if the thread is different. |
| 2664 | */ |
| 2665 | if (socket->tid != tid) |
| 2666 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2667 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2668 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2670 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | lua_pushnil(L); |
| 2672 | return 1; |
| 2673 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2674 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2675 | si = appctx->owner; |
| 2676 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2678 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2679 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2680 | lua_pushnil(L); |
| 2681 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2684 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2685 | xref_unlock(&socket->xref, peer); |
| 2686 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | /* Returns information about my connection side. */ |
| 2690 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2691 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2692 | struct hlua_socket *socket; |
| 2693 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2694 | struct appctx *appctx; |
| 2695 | struct xref *peer; |
| 2696 | struct stream_interface *si; |
| 2697 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2698 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2699 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2700 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2701 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2702 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2703 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2704 | /* Check if we run on the same thread than the xreator thread. |
| 2705 | * We cannot access to the socket if the thread is different. |
| 2706 | */ |
| 2707 | if (socket->tid != tid) |
| 2708 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2709 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2710 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2711 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2712 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | lua_pushnil(L); |
| 2714 | return 1; |
| 2715 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2716 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2717 | si = appctx->owner; |
| 2718 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2720 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2721 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2722 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | lua_pushnil(L); |
| 2724 | return 1; |
| 2725 | } |
| 2726 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2727 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2728 | xref_unlock(&socket->xref, peer); |
| 2729 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2733 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2734 | .obj_type = OBJ_TYPE_APPLET, |
| 2735 | .name = "<LUA_TCP>", |
| 2736 | .fct = hlua_socket_handler, |
| 2737 | .release = hlua_socket_release, |
| 2738 | }; |
| 2739 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2740 | __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] | 2741 | { |
| 2742 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2743 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2744 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2745 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2746 | struct stream_interface *si; |
| 2747 | struct stream *s; |
| 2748 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2749 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2750 | hlua = hlua_gethlua(L); |
| 2751 | if (!hlua) |
| 2752 | return 0; |
| 2753 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2754 | /* Check if we run on the same thread than the xreator thread. |
| 2755 | * We cannot access to the socket if the thread is different. |
| 2756 | */ |
| 2757 | if (socket->tid != tid) |
| 2758 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2759 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2760 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2761 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2762 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2763 | lua_pushnil(L); |
| 2764 | lua_pushstring(L, "Can't connect"); |
| 2765 | return 2; |
| 2766 | } |
| 2767 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2768 | si = appctx->owner; |
| 2769 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2771 | /* Check if we run on the same thread than the xreator thread. |
| 2772 | * We cannot access to the socket if the thread is different. |
| 2773 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2774 | if (socket->tid != tid) { |
| 2775 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2776 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2777 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2778 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2780 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2781 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2782 | lua_pushnil(L); |
| 2783 | lua_pushstring(L, "Can't connect"); |
| 2784 | return 2; |
| 2785 | } |
| 2786 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2787 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2788 | |
| 2789 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2790 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2791 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2792 | lua_pushinteger(L, 1); |
| 2793 | return 1; |
| 2794 | } |
| 2795 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2796 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2797 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2798 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2799 | } |
| 2800 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2801 | 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] | 2802 | return 0; |
| 2803 | } |
| 2804 | |
| 2805 | /* This function fail or initite the connection. */ |
| 2806 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2807 | { |
| 2808 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2809 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2811 | struct hlua *hlua; |
| 2812 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2813 | int low, high; |
| 2814 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2815 | struct xref *peer; |
| 2816 | struct stream_interface *si; |
| 2817 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2819 | if (lua_gettop(L) < 2) |
| 2820 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | |
| 2822 | /* Get args. */ |
| 2823 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2824 | |
| 2825 | /* Check if we run on the same thread than the xreator thread. |
| 2826 | * We cannot access to the socket if the thread is different. |
| 2827 | */ |
| 2828 | if (socket->tid != tid) |
| 2829 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2830 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2831 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2832 | if (lua_gettop(L) >= 3) { |
| 2833 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2834 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2835 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2836 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2837 | * that are not enclosed within square brackets. |
| 2838 | */ |
| 2839 | if (port > 0) { |
| 2840 | luaL_buffinit(L, &b); |
| 2841 | luaL_addstring(&b, ip); |
| 2842 | luaL_addchar(&b, ':'); |
| 2843 | luaL_pushresult(&b); |
| 2844 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2845 | } |
| 2846 | } |
| 2847 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2848 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2849 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2850 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2851 | lua_pushnil(L); |
| 2852 | return 1; |
| 2853 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | |
| 2855 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2856 | 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] | 2857 | if (!addr) { |
| 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: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2860 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2861 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2862 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2863 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2864 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2865 | if (port == -1) { |
| 2866 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2867 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2868 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2869 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2870 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2871 | if (port == -1) { |
| 2872 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2873 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2874 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2875 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2876 | } |
| 2877 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2878 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2879 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2880 | si = appctx->owner; |
| 2881 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2882 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2883 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2884 | xref_unlock(&socket->xref, peer); |
| 2885 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2886 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2887 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2888 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2889 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2890 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2891 | if (!hlua) |
| 2892 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2893 | |
| 2894 | /* inform the stream that we want to be notified whenever the |
| 2895 | * connection completes. |
| 2896 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2897 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2898 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2899 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2900 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2901 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2902 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2903 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2904 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2905 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2906 | } |
| 2907 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2908 | |
| 2909 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2910 | /* Return yield waiting for connection. */ |
| 2911 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2912 | 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] | 2913 | |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2917 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2918 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2919 | { |
| 2920 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2921 | struct xref *peer; |
| 2922 | struct appctx *appctx; |
| 2923 | struct stream_interface *si; |
| 2924 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2925 | |
| 2926 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2927 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2928 | |
| 2929 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2930 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2931 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2932 | lua_pushnil(L); |
| 2933 | return 1; |
| 2934 | } |
| 2935 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2936 | si = appctx->owner; |
| 2937 | s = si_strm(si); |
| 2938 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2939 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2940 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2941 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2942 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2943 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2944 | |
| 2945 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2946 | { |
| 2947 | return 0; |
| 2948 | } |
| 2949 | |
| 2950 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2951 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2952 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2953 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2954 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2955 | struct xref *peer; |
| 2956 | struct appctx *appctx; |
| 2957 | struct stream_interface *si; |
| 2958 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2959 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2960 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2961 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2962 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2963 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2964 | /* convert the timeout to millis */ |
| 2965 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2966 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2967 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2968 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2969 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2970 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2971 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2972 | 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] | 2973 | |
| 2974 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2975 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2976 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2977 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2978 | /* Check if we run on the same thread than the xreator thread. |
| 2979 | * We cannot access to the socket if the thread is different. |
| 2980 | */ |
| 2981 | if (socket->tid != tid) |
| 2982 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2983 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2984 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2985 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2986 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2987 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2988 | WILL_LJMP(lua_error(L)); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2992 | si = appctx->owner; |
| 2993 | s = si_strm(si); |
| 2994 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2995 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2996 | s->req.rto = tmout; |
| 2997 | s->req.wto = tmout; |
| 2998 | s->res.rto = tmout; |
| 2999 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3000 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 3001 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 3002 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 3003 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 3004 | |
| 3005 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3006 | task_queue(s->task); |
| 3007 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3008 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3009 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3010 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3011 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3015 | { |
| 3016 | struct hlua_socket *socket; |
| 3017 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 3018 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3019 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3020 | |
| 3021 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3022 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3023 | hlua_pusherror(L, "socket: full stack"); |
| 3024 | goto out_fail_conf; |
| 3025 | } |
| 3026 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3027 | /* Create the object: obj[0] = userdata. */ |
| 3028 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3029 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3030 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3031 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3032 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3033 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3034 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3035 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3036 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3037 | goto out_fail_conf; |
| 3038 | } |
| 3039 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3040 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3041 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3042 | lua_setmetatable(L, -2); |
| 3043 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3044 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 3045 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3046 | if (!appctx) { |
| 3047 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 3048 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3049 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3050 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3051 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 3052 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3053 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 3054 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3055 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3056 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 3057 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3058 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3059 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3060 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3061 | } |
| 3062 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 3063 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3064 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3065 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3066 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3067 | } |
| 3068 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3069 | /* Initialise cross reference between stream and Lua socket object. */ |
| 3070 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3071 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3072 | /* Configure "right" stream interface. this "si" is used to connect |
| 3073 | * and retrieve data from the server. The connection is initialized |
| 3074 | * with the "struct server". |
| 3075 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3076 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3077 | |
| 3078 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 3079 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3080 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3081 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3082 | return 1; |
| 3083 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3084 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 3085 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3086 | out_fail_sess: |
| 3087 | appctx_free(appctx); |
| 3088 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3089 | WILL_LJMP(lua_error(L)); |
| 3090 | return 0; |
| 3091 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | * |
| 3095 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | * Class Channel |
| 3097 | * |
| 3098 | * |
| 3099 | */ |
| 3100 | |
| 3101 | /* Returns the struct hlua_channel join to the class channel in the |
| 3102 | * stack entry "ud" or throws an argument error. |
| 3103 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3104 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3105 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3106 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3109 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3110 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3112 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3113 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3114 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3115 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | return 0; |
| 3117 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3118 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3119 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3120 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | |
| 3122 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3123 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3124 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | return 1; |
| 3126 | } |
| 3127 | |
| 3128 | /* Duplicate all the data present in the input channel and put it |
| 3129 | * in a string LUA variables. Returns -1 and push a nil value in |
| 3130 | * the stack if the channel is closed and all the data are consumed, |
| 3131 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3132 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3134 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3135 | { |
| 3136 | char *blk1; |
| 3137 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3138 | size_t len1; |
| 3139 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | int ret; |
| 3141 | luaL_Buffer b; |
| 3142 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3143 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3144 | if (unlikely(ret <= 0)) { |
| 3145 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3146 | lua_pushnil(L); |
| 3147 | return -1; |
| 3148 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | luaL_buffinit(L, &b); |
| 3153 | luaL_addlstring(&b, blk1, len1); |
| 3154 | if (unlikely(ret == 2)) |
| 3155 | luaL_addlstring(&b, blk2, len2); |
| 3156 | luaL_pushresult(&b); |
| 3157 | |
| 3158 | if (unlikely(ret == 2)) |
| 3159 | return len1 + len2; |
| 3160 | return len1; |
| 3161 | } |
| 3162 | |
| 3163 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3164 | * a yield. This function keep the data in the buffer. |
| 3165 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3166 | __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] | 3167 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3168 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3169 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3170 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3171 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3172 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3173 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3174 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3175 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3176 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3177 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3178 | 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] | 3179 | return 1; |
| 3180 | } |
| 3181 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3182 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3183 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3184 | { |
| 3185 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3186 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3187 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3188 | } |
| 3189 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3191 | * a yield. This function consumes the data in the buffer. It returns |
| 3192 | * a string containing the data or a nil pointer if no data are available |
| 3193 | * and the channel is closed. |
| 3194 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3195 | __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] | 3196 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3197 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3198 | int ret; |
| 3199 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3200 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3201 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3202 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3203 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3204 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3205 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3206 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3207 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3208 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3209 | 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] | 3210 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3211 | if (unlikely(ret == -1)) |
| 3212 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3213 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3214 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3215 | return 1; |
| 3216 | } |
| 3217 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3218 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3219 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3220 | { |
| 3221 | MAY_LJMP(check_args(L, 1, "get")); |
| 3222 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3223 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3224 | } |
| 3225 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3226 | /* This functions consumes and returns one line. If the channel is closed, |
| 3227 | * 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] | 3228 | * 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] | 3229 | * value. |
| 3230 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3231 | __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] | 3232 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3233 | char *blk1; |
| 3234 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3235 | size_t len1; |
| 3236 | size_t len2; |
| 3237 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3238 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3239 | int ret; |
| 3240 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3242 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3243 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3244 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3245 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3246 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3247 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3248 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3249 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3250 | if (ret == 0) { |
| 3251 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3252 | _hlua_channel_dup(chn, L); |
| 3253 | return 1; |
| 3254 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3255 | 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] | 3256 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3257 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3258 | if (ret == -1) { |
| 3259 | lua_pushnil(L); |
| 3260 | return 1; |
| 3261 | } |
| 3262 | |
| 3263 | luaL_buffinit(L, &b); |
| 3264 | luaL_addlstring(&b, blk1, len1); |
| 3265 | len = len1; |
| 3266 | if (unlikely(ret == 2)) { |
| 3267 | luaL_addlstring(&b, blk2, len2); |
| 3268 | len += len2; |
| 3269 | } |
| 3270 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3271 | 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] | 3272 | return 1; |
| 3273 | } |
| 3274 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3275 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3276 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3277 | { |
| 3278 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3279 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3280 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3281 | } |
| 3282 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3283 | /* This function takes a string as argument, and append it at the input side of |
| 3284 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3285 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3286 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3287 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3288 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3289 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3290 | struct channel *chn; |
| 3291 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3292 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3293 | |
| 3294 | MAY_LJMP(check_args(L, 2, "append")); |
| 3295 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3296 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3297 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3298 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3299 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3300 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3301 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3302 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3303 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3304 | lua_pushinteger(L, -1); |
| 3305 | else |
| 3306 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3307 | return 1; |
| 3308 | } |
| 3309 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3310 | /* The function replaces input data from the channel by the string passed as |
| 3311 | * argument. Before clearing the buffer, we take care the copy will be |
| 3312 | * possible. It returns the amount of data written (the string length) on |
| 3313 | * 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] | 3314 | */ |
| 3315 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3316 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3317 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3318 | const char *str; |
| 3319 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3320 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3322 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3323 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3324 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3325 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3326 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3327 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3328 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3329 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3330 | /* Be sure we can copied the string once input data will be removed. */ |
| 3331 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3332 | lua_pushinteger(L, -1); |
| 3333 | else { |
| 3334 | b_set_data(&chn->buf, co_data(chn)); |
| 3335 | if (ci_putblk(chn, str, len) < 0) |
| 3336 | lua_pushinteger(L, -1); |
| 3337 | else |
| 3338 | lua_pushinteger(L, len); |
| 3339 | } |
| 3340 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3341 | } |
| 3342 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3343 | /* Append data in the output side of the buffer. This data is immediately |
| 3344 | * sent. The function returns the amount of data written. If the buffer |
| 3345 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3346 | * if the channel is closed. |
| 3347 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3348 | __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] | 3349 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3350 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3351 | size_t len; |
| 3352 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3353 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3354 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3355 | struct hlua *hlua; |
| 3356 | |
| 3357 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3358 | hlua = hlua_gethlua(L); |
| 3359 | if (!hlua) { |
| 3360 | lua_pushnil(L); |
| 3361 | return 1; |
| 3362 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3363 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3364 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3365 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3366 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3367 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3368 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3369 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3370 | lua_pushinteger(L, -1); |
| 3371 | return 1; |
| 3372 | } |
| 3373 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3374 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3375 | * the request buffer if its not required. |
| 3376 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3377 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3378 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3379 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3380 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3381 | 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] | 3382 | } |
| 3383 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3384 | /* The written data will be immediately sent, so we can check |
| 3385 | * the available space without taking in account the reserve. |
| 3386 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3387 | * data, because the buffer will be flushed. |
| 3388 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3389 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3390 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3391 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3392 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3393 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3394 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3395 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3396 | return 1; |
| 3397 | |
| 3398 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3399 | if (max > len - l) |
| 3400 | max = len - l; |
| 3401 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3402 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3403 | * detects a non contiguous buffer and realign it. |
| 3404 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3405 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3406 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3407 | |
| 3408 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3409 | 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] | 3410 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3411 | /* buffer replace considers that the input part is filled. |
| 3412 | * so, I must forward these new data in the output part. |
| 3413 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3414 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3415 | |
| 3416 | l += max; |
| 3417 | lua_pop(L, 1); |
| 3418 | lua_pushinteger(L, l); |
| 3419 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3420 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3421 | /* If there is no space available, and the output buffer is empty. |
| 3422 | * in this case, we cannot add more data, so we cannot yield, |
| 3423 | * we return the amount of copied data. |
| 3424 | */ |
| 3425 | max = b_room(&chn->buf); |
| 3426 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3427 | return 1; |
| 3428 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3429 | /* If we are waiting for space in the response buffer, we |
| 3430 | * must set the flag WAKERESWR. This flag required the task |
| 3431 | * wake up if any activity is detected on the response buffer. |
| 3432 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3433 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3434 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3435 | else |
| 3436 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3437 | 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] | 3438 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3439 | |
| 3440 | return 1; |
| 3441 | } |
| 3442 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3443 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3444 | * yield the LUA process, and resume it without checking the |
| 3445 | * input arguments. |
| 3446 | */ |
| 3447 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3448 | { |
| 3449 | MAY_LJMP(check_args(L, 2, "send")); |
| 3450 | lua_pushinteger(L, 0); |
| 3451 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3452 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | /* This function forward and amount of butes. The data pass from |
| 3456 | * the input side of the buffer to the output side, and can be |
| 3457 | * forwarded. This function never fails. |
| 3458 | * |
| 3459 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3460 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3461 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3462 | __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] | 3463 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3464 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3465 | int len; |
| 3466 | int l; |
| 3467 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3468 | struct hlua *hlua; |
| 3469 | |
| 3470 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3471 | hlua = hlua_gethlua(L); |
| 3472 | if (!hlua) |
| 3473 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3474 | |
| 3475 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3476 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3477 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3478 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3479 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3480 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3481 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3482 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3483 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3484 | |
| 3485 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3486 | if (max > ci_data(chn)) |
| 3487 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3488 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3489 | l += max; |
| 3490 | |
| 3491 | lua_pop(L, 1); |
| 3492 | lua_pushinteger(L, l); |
| 3493 | |
| 3494 | /* Check if it miss bytes to forward. */ |
| 3495 | if (l < len) { |
| 3496 | /* The the input channel or the output channel are closed, we |
| 3497 | * must return the amount of data forwarded. |
| 3498 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3499 | 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] | 3500 | return 1; |
| 3501 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3502 | /* If we are waiting for space data in the response buffer, we |
| 3503 | * must set the flag WAKERESWR. This flag required the task |
| 3504 | * wake up if any activity is detected on the response buffer. |
| 3505 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3506 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3507 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3508 | else |
| 3509 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3510 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3511 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3512 | 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] | 3513 | } |
| 3514 | |
| 3515 | return 1; |
| 3516 | } |
| 3517 | |
| 3518 | /* Just check the input and prepare the stack for the previous |
| 3519 | * function "hlua_channel_forward_yield" |
| 3520 | */ |
| 3521 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3522 | { |
| 3523 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3524 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3525 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3526 | |
| 3527 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3528 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /* Just returns the number of bytes available in the input |
| 3532 | * side of the buffer. This function never fails. |
| 3533 | */ |
| 3534 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3535 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3536 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3537 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3538 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3539 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3540 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3541 | struct htx *htx = htxbuf(&chn->buf); |
| 3542 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3543 | } |
| 3544 | else |
| 3545 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3546 | return 1; |
| 3547 | } |
| 3548 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3549 | /* Returns true if the channel is full. */ |
| 3550 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3551 | { |
| 3552 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3553 | |
| 3554 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3555 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3556 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3557 | * applet). |
| 3558 | */ |
| 3559 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3560 | return 1; |
| 3561 | } |
| 3562 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3563 | /* Returns true if the channel is the response channel. */ |
| 3564 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3565 | { |
| 3566 | struct channel *chn; |
| 3567 | |
| 3568 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3569 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3570 | |
| 3571 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3572 | return 1; |
| 3573 | } |
| 3574 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3575 | /* Just returns the number of bytes available in the output |
| 3576 | * side of the buffer. This function never fails. |
| 3577 | */ |
| 3578 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3579 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3580 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3581 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3582 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3583 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3584 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3585 | return 1; |
| 3586 | } |
| 3587 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3588 | /* |
| 3589 | * |
| 3590 | * |
| 3591 | * Class Fetches |
| 3592 | * |
| 3593 | * |
| 3594 | */ |
| 3595 | |
| 3596 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3597 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3598 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3599 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3600 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3601 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | /* This function creates and push in the stack a fetch object according |
| 3605 | * with a current TXN. |
| 3606 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3607 | 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] | 3608 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3609 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3610 | |
| 3611 | /* Check stack size. */ |
| 3612 | if (!lua_checkstack(L, 3)) |
| 3613 | return 0; |
| 3614 | |
| 3615 | /* Create the object: obj[0] = userdata. |
| 3616 | * Note that the base of the Fetches object is the |
| 3617 | * transaction object. |
| 3618 | */ |
| 3619 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3620 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3621 | lua_rawseti(L, -2, 0); |
| 3622 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3623 | hsmp->s = txn->s; |
| 3624 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3625 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3626 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3627 | |
| 3628 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3629 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3630 | lua_setmetatable(L, -2); |
| 3631 | |
| 3632 | return 1; |
| 3633 | } |
| 3634 | |
| 3635 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3636 | * It uses closure argument to store the associated sample-fetch. It |
| 3637 | * returns only one argument or throws an error. An error is thrown |
| 3638 | * only if an error is encountered during the argument parsing. If |
| 3639 | * the "sample-fetch" function fails, nil is returned. |
| 3640 | */ |
| 3641 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3642 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3643 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3644 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3645 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3646 | int i; |
| 3647 | struct sample smp; |
| 3648 | |
| 3649 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3650 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3651 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3652 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3653 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3654 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3655 | /* Check execution authorization. */ |
| 3656 | if (f->use & SMP_USE_HTTP_ANY && |
| 3657 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3658 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3659 | "is not available in Lua services", f->kw); |
| 3660 | WILL_LJMP(lua_error(L)); |
| 3661 | } |
| 3662 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3663 | /* Get extra arguments. */ |
| 3664 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3665 | if (i >= ARGM_NBARGS) |
| 3666 | break; |
| 3667 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3668 | } |
| 3669 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3670 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3671 | |
| 3672 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3673 | 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] | 3674 | |
| 3675 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3676 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3677 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3678 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | /* Initialise the sample. */ |
| 3682 | memset(&smp, 0, sizeof(smp)); |
| 3683 | |
| 3684 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3685 | 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] | 3686 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3687 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3688 | lua_pushstring(L, ""); |
| 3689 | else |
| 3690 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3691 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3695 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3696 | hlua_smp2lua_str(L, &smp); |
| 3697 | else |
| 3698 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3699 | |
| 3700 | end: |
| 3701 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3702 | if (args[i].type == ARGT_STR) |
| 3703 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3704 | else if (args[i].type == ARGT_REG) |
| 3705 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3706 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3707 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3708 | |
| 3709 | error: |
| 3710 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3711 | if (args[i].type == ARGT_STR) |
| 3712 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3713 | else if (args[i].type == ARGT_REG) |
| 3714 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3715 | } |
| 3716 | WILL_LJMP(lua_error(L)); |
| 3717 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3718 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3719 | |
| 3720 | /* |
| 3721 | * |
| 3722 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3723 | * Class Converters |
| 3724 | * |
| 3725 | * |
| 3726 | */ |
| 3727 | |
| 3728 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3729 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3730 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3731 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3732 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3733 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | /* This function creates and push in the stack a Converters object |
| 3737 | * according with a current TXN. |
| 3738 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3739 | 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] | 3740 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3741 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3742 | |
| 3743 | /* Check stack size. */ |
| 3744 | if (!lua_checkstack(L, 3)) |
| 3745 | return 0; |
| 3746 | |
| 3747 | /* Create the object: obj[0] = userdata. |
| 3748 | * Note that the base of the Converters object is the |
| 3749 | * same than the TXN object. |
| 3750 | */ |
| 3751 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3752 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3753 | lua_rawseti(L, -2, 0); |
| 3754 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3755 | hsmp->s = txn->s; |
| 3756 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3757 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3758 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3759 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3760 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3761 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3762 | lua_setmetatable(L, -2); |
| 3763 | |
| 3764 | return 1; |
| 3765 | } |
| 3766 | |
| 3767 | /* This function is an LUA binding. It is called with each converter. |
| 3768 | * It uses closure argument to store the associated converter. It |
| 3769 | * returns only one argument or throws an error. An error is thrown |
| 3770 | * only if an error is encountered during the argument parsing. If |
| 3771 | * the converter function function fails, nil is returned. |
| 3772 | */ |
| 3773 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3774 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3775 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3776 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3777 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3778 | int i; |
| 3779 | struct sample smp; |
| 3780 | |
| 3781 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3782 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3783 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3784 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3785 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3786 | |
| 3787 | /* Get extra arguments. */ |
| 3788 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3789 | if (i >= ARGM_NBARGS) |
| 3790 | break; |
| 3791 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3792 | } |
| 3793 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3794 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3795 | |
| 3796 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3797 | 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] | 3798 | |
| 3799 | /* Run the special args checker. */ |
| 3800 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3801 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3802 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3806 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3807 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3808 | hlua_pusherror(L, "error in the input argument"); |
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 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3812 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3813 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3814 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3815 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3816 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3817 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3818 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3819 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3820 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3821 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3822 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3823 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3827 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3828 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3829 | lua_pushstring(L, ""); |
| 3830 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3831 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3832 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3836 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3837 | hlua_smp2lua_str(L, &smp); |
| 3838 | else |
| 3839 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3840 | end: |
| 3841 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3842 | if (args[i].type == ARGT_STR) |
| 3843 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3844 | else if (args[i].type == ARGT_REG) |
| 3845 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3846 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3847 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3848 | |
| 3849 | error: |
| 3850 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3851 | if (args[i].type == ARGT_STR) |
| 3852 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3853 | else if (args[i].type == ARGT_REG) |
| 3854 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3855 | } |
| 3856 | WILL_LJMP(lua_error(L)); |
| 3857 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3858 | } |
| 3859 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3860 | /* |
| 3861 | * |
| 3862 | * |
| 3863 | * Class AppletTCP |
| 3864 | * |
| 3865 | * |
| 3866 | */ |
| 3867 | |
| 3868 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3869 | * a class stream, otherwise it throws an error. |
| 3870 | */ |
| 3871 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3872 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3873 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | /* This function creates and push in the stack an Applet object |
| 3877 | * according with a current TXN. |
| 3878 | */ |
| 3879 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3880 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3881 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3882 | struct stream_interface *si = ctx->owner; |
| 3883 | struct stream *s = si_strm(si); |
| 3884 | struct proxy *p = s->be; |
| 3885 | |
| 3886 | /* Check stack size. */ |
| 3887 | if (!lua_checkstack(L, 3)) |
| 3888 | return 0; |
| 3889 | |
| 3890 | /* Create the object: obj[0] = userdata. |
| 3891 | * Note that the base of the Converters object is the |
| 3892 | * same than the TXN object. |
| 3893 | */ |
| 3894 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3895 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3896 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3897 | luactx->appctx = ctx; |
| 3898 | luactx->htxn.s = s; |
| 3899 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3900 | |
| 3901 | /* Create the "f" field that contains a list of fetches. */ |
| 3902 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3903 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3904 | return 0; |
| 3905 | lua_settable(L, -3); |
| 3906 | |
| 3907 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3908 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3909 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3910 | return 0; |
| 3911 | lua_settable(L, -3); |
| 3912 | |
| 3913 | /* Create the "c" field that contains a list of converters. */ |
| 3914 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3915 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3916 | return 0; |
| 3917 | lua_settable(L, -3); |
| 3918 | |
| 3919 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3920 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3921 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3922 | return 0; |
| 3923 | lua_settable(L, -3); |
| 3924 | |
| 3925 | /* Pop a class stream metatable and affect it to the table. */ |
| 3926 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3927 | lua_setmetatable(L, -2); |
| 3928 | |
| 3929 | return 1; |
| 3930 | } |
| 3931 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3932 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3933 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3934 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3935 | struct stream *s; |
| 3936 | const char *name; |
| 3937 | size_t len; |
| 3938 | struct sample smp; |
| 3939 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3940 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3941 | 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] | 3942 | |
| 3943 | /* It is useles to retrieve the stream, but this function |
| 3944 | * runs only in a stream context. |
| 3945 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3946 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3947 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3948 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3949 | |
| 3950 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3951 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3952 | hlua_lua2smp(L, 3, &smp); |
| 3953 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 3954 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 3955 | * already takes care of duplicating dynamic var data. |
| 3956 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3957 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3958 | |
| 3959 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3960 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3961 | else |
| 3962 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3963 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3964 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3968 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3969 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3970 | struct stream *s; |
| 3971 | const char *name; |
| 3972 | size_t len; |
| 3973 | struct sample smp; |
| 3974 | |
| 3975 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3976 | |
| 3977 | /* It is useles to retrieve the stream, but this function |
| 3978 | * runs only in a stream context. |
| 3979 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3980 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3981 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3982 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3983 | |
| 3984 | /* Unset the variable. */ |
| 3985 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3986 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3987 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3991 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3992 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3993 | struct stream *s; |
| 3994 | const char *name; |
| 3995 | size_t len; |
| 3996 | struct sample smp; |
| 3997 | |
| 3998 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3999 | |
| 4000 | /* It is useles to retrieve the stream, but this function |
| 4001 | * runs only in a stream context. |
| 4002 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4003 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4004 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4005 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4006 | |
| 4007 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4008 | if (!vars_get_by_name(name, len, &smp)) { |
| 4009 | lua_pushnil(L); |
| 4010 | return 1; |
| 4011 | } |
| 4012 | |
| 4013 | return hlua_smp2lua(L, &smp); |
| 4014 | } |
| 4015 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4016 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4017 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4018 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4019 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 4020 | struct hlua *hlua = hlua_stream_ctx_get(s); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4021 | |
| 4022 | /* 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^] | 4023 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4024 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4025 | |
| 4026 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4027 | |
| 4028 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4029 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4030 | |
| 4031 | /* Get and store new value. */ |
| 4032 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4033 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4034 | |
| 4035 | return 0; |
| 4036 | } |
| 4037 | |
| 4038 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4039 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4040 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4041 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 4042 | struct hlua *hlua = hlua_stream_ctx_get(s); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4043 | |
| 4044 | /* 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^] | 4045 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4046 | lua_pushnil(L); |
| 4047 | return 1; |
| 4048 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4049 | |
| 4050 | /* Push configuration index in the stack. */ |
| 4051 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4052 | |
| 4053 | return 1; |
| 4054 | } |
| 4055 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4056 | /* If expected data not yet available, it returns a yield. This function |
| 4057 | * consumes the data in the buffer. It returns a string containing the |
| 4058 | * data. This string can be empty. |
| 4059 | */ |
| 4060 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4061 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4062 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4063 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4064 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4065 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4066 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4067 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4068 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4069 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4070 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4071 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4072 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4073 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4074 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4075 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4076 | 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] | 4077 | } |
| 4078 | |
| 4079 | /* End of data: commit the total strings and return. */ |
| 4080 | if (ret < 0) { |
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 | |
| 4085 | /* Ensure that the block 2 length is usable. */ |
| 4086 | if (ret == 1) |
| 4087 | len2 = 0; |
| 4088 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4089 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4090 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4091 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4092 | |
| 4093 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4094 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4095 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4096 | return 1; |
| 4097 | } |
| 4098 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4099 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4100 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 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)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4103 | |
| 4104 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4105 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4106 | |
| 4107 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4108 | } |
| 4109 | |
| 4110 | /* If expected data not yet available, it returns a yield. This function |
| 4111 | * consumes the data in the buffer. It returns a string containing the |
| 4112 | * data. This string can be empty. |
| 4113 | */ |
| 4114 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4115 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4116 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4117 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4118 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4119 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4120 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4121 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4122 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4123 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4124 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4125 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4126 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4127 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4128 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4129 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4130 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4131 | 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] | 4132 | } |
| 4133 | |
| 4134 | /* End of data: commit the total strings and return. */ |
| 4135 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4136 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4137 | return 1; |
| 4138 | } |
| 4139 | |
| 4140 | /* Ensure that the block 2 length is usable. */ |
| 4141 | if (ret == 1) |
| 4142 | len2 = 0; |
| 4143 | |
| 4144 | if (len == -1) { |
| 4145 | |
| 4146 | /* If len == -1, catenate all the data avalaile and |
| 4147 | * yield because we want to get all the data until |
| 4148 | * the end of data stream. |
| 4149 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4150 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4151 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4152 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4153 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4154 | 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] | 4155 | |
| 4156 | } else { |
| 4157 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4158 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4159 | if (len1 > len) |
| 4160 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4161 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4162 | len -= len1; |
| 4163 | |
| 4164 | /* Copy the second block. */ |
| 4165 | if (len2 > len) |
| 4166 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4167 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4168 | len -= len2; |
| 4169 | |
| 4170 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4171 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4172 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4173 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4174 | if (len > 0) { |
| 4175 | lua_pushinteger(L, len); |
| 4176 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4177 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4178 | 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] | 4179 | } |
| 4180 | |
| 4181 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4182 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4183 | return 1; |
| 4184 | } |
| 4185 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4186 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4187 | hlua_pusherror(L, "Lua: internal error"); |
| 4188 | WILL_LJMP(lua_error(L)); |
| 4189 | return 0; |
| 4190 | } |
| 4191 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4192 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4193 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4194 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4195 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4196 | int len = -1; |
| 4197 | |
| 4198 | if (lua_gettop(L) > 2) |
| 4199 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4200 | if (lua_gettop(L) >= 2) { |
| 4201 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4202 | lua_pop(L, 1); |
| 4203 | } |
| 4204 | |
| 4205 | /* Confirm or set the required length */ |
| 4206 | lua_pushinteger(L, len); |
| 4207 | |
| 4208 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4209 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4210 | |
| 4211 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4212 | } |
| 4213 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4214 | /* Append data in the output side of the buffer. This data is immediately |
| 4215 | * sent. The function returns the amount of data written. If the buffer |
| 4216 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4217 | * if the channel is closed. |
| 4218 | */ |
| 4219 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4220 | { |
| 4221 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4222 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4223 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4224 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4225 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4226 | struct channel *chn = si_ic(si); |
| 4227 | int max; |
| 4228 | |
| 4229 | /* Get the max amount of data which can write as input in the channel. */ |
| 4230 | max = channel_recv_max(chn); |
| 4231 | if (max > (len - l)) |
| 4232 | max = len - l; |
| 4233 | |
| 4234 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4235 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4236 | |
| 4237 | /* update counters. */ |
| 4238 | l += max; |
| 4239 | lua_pop(L, 1); |
| 4240 | lua_pushinteger(L, l); |
| 4241 | |
| 4242 | /* If some data is not send, declares the situation to the |
| 4243 | * applet, and returns a yield. |
| 4244 | */ |
| 4245 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4246 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4247 | 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] | 4248 | } |
| 4249 | |
| 4250 | return 1; |
| 4251 | } |
| 4252 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4253 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4254 | * yield the LUA process, and resume it without checking the |
| 4255 | * input arguments. |
| 4256 | */ |
| 4257 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4258 | { |
| 4259 | MAY_LJMP(check_args(L, 2, "send")); |
| 4260 | lua_pushinteger(L, 0); |
| 4261 | |
| 4262 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4263 | } |
| 4264 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4265 | /* |
| 4266 | * |
| 4267 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4268 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4269 | * |
| 4270 | * |
| 4271 | */ |
| 4272 | |
| 4273 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4274 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4275 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4276 | __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] | 4277 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4278 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4281 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4282 | * according with a current TXN. |
| 4283 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4284 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4285 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4286 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4287 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4288 | struct stream_interface *si = ctx->owner; |
| 4289 | struct stream *s = si_strm(si); |
| 4290 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4291 | struct htx *htx; |
| 4292 | struct htx_blk *blk; |
| 4293 | struct htx_sl *sl; |
| 4294 | struct ist path; |
| 4295 | unsigned long long len = 0; |
| 4296 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4297 | |
| 4298 | /* Check stack size. */ |
| 4299 | if (!lua_checkstack(L, 3)) |
| 4300 | return 0; |
| 4301 | |
| 4302 | /* Create the object: obj[0] = userdata. |
| 4303 | * Note that the base of the Converters object is the |
| 4304 | * same than the TXN object. |
| 4305 | */ |
| 4306 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4307 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4308 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4309 | luactx->appctx = ctx; |
| 4310 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4311 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4312 | luactx->htxn.s = s; |
| 4313 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4314 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4315 | /* Create the "f" field that contains a list of fetches. */ |
| 4316 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4317 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4318 | return 0; |
| 4319 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4320 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4321 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4322 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4323 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4324 | return 0; |
| 4325 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4326 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4327 | /* Create the "c" field that contains a list of converters. */ |
| 4328 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4329 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | return 0; |
| 4331 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4332 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4333 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4334 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4335 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | return 0; |
| 4337 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4338 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4339 | htx = htxbuf(&s->req.buf); |
| 4340 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4341 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4342 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4343 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4344 | /* Stores the request method. */ |
| 4345 | lua_pushstring(L, "method"); |
| 4346 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4347 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4348 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4349 | /* Stores the http version. */ |
| 4350 | lua_pushstring(L, "version"); |
| 4351 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4352 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4353 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4354 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4355 | * the array on the top of the stack. |
| 4356 | */ |
| 4357 | lua_pushstring(L, "headers"); |
| 4358 | htxn.s = s; |
| 4359 | htxn.p = px; |
| 4360 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4361 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4362 | return 0; |
| 4363 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4364 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4365 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4366 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4367 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4368 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4369 | p = path.ptr; |
| 4370 | end = path.ptr + path.len; |
| 4371 | q = p; |
| 4372 | while (q < end && *q != '?') |
| 4373 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4374 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4375 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4376 | lua_pushstring(L, "path"); |
| 4377 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4378 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4379 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4380 | /* Stores the query string. */ |
| 4381 | lua_pushstring(L, "qs"); |
| 4382 | if (*q == '?') |
| 4383 | q++; |
| 4384 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4385 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4386 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4387 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4388 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4389 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4390 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4391 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4392 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4393 | break; |
| 4394 | if (type == HTX_BLK_DATA) |
| 4395 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4396 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4397 | if (htx->extra != ULLONG_MAX) |
| 4398 | len += htx->extra; |
| 4399 | |
| 4400 | /* Stores the request path. */ |
| 4401 | lua_pushstring(L, "length"); |
| 4402 | lua_pushinteger(L, len); |
| 4403 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4404 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4405 | /* Create an empty array of HTTP request headers. */ |
| 4406 | lua_pushstring(L, "response"); |
| 4407 | lua_newtable(L); |
| 4408 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4409 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4410 | /* Pop a class stream metatable and affect it to the table. */ |
| 4411 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4412 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4413 | |
| 4414 | return 1; |
| 4415 | } |
| 4416 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4417 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4418 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4419 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4420 | struct stream *s; |
| 4421 | const char *name; |
| 4422 | size_t len; |
| 4423 | struct sample smp; |
| 4424 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4425 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4426 | 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] | 4427 | |
| 4428 | /* It is useles to retrieve the stream, but this function |
| 4429 | * runs only in a stream context. |
| 4430 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4431 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4432 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4433 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4434 | |
| 4435 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4436 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4437 | hlua_lua2smp(L, 3, &smp); |
| 4438 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4439 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4440 | * already takes care of duplicating dynamic var data. |
| 4441 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4442 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4443 | |
| 4444 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4445 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4446 | else |
| 4447 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4448 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4449 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4453 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4454 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4455 | struct stream *s; |
| 4456 | const char *name; |
| 4457 | size_t len; |
| 4458 | struct sample smp; |
| 4459 | |
| 4460 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4461 | |
| 4462 | /* It is useles to retrieve the stream, but this function |
| 4463 | * runs only in a stream context. |
| 4464 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4465 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4466 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4467 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4468 | |
| 4469 | /* Unset the variable. */ |
| 4470 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4471 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4472 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4476 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4477 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4478 | struct stream *s; |
| 4479 | const char *name; |
| 4480 | size_t len; |
| 4481 | struct sample smp; |
| 4482 | |
| 4483 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4484 | |
| 4485 | /* It is useles to retrieve the stream, but this function |
| 4486 | * runs only in a stream context. |
| 4487 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4488 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4489 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4490 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4491 | |
| 4492 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4493 | if (!vars_get_by_name(name, len, &smp)) { |
| 4494 | lua_pushnil(L); |
| 4495 | return 1; |
| 4496 | } |
| 4497 | |
| 4498 | return hlua_smp2lua(L, &smp); |
| 4499 | } |
| 4500 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4501 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4502 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4503 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4504 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 4505 | struct hlua *hlua = hlua_stream_ctx_get(s); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4506 | |
| 4507 | /* 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^] | 4508 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4509 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4510 | |
| 4511 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4512 | |
| 4513 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4514 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4515 | |
| 4516 | /* Get and store new value. */ |
| 4517 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4518 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4519 | |
| 4520 | return 0; |
| 4521 | } |
| 4522 | |
| 4523 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4524 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4525 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4526 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 4527 | struct hlua *hlua = hlua_stream_ctx_get(s); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4528 | |
| 4529 | /* 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^] | 4530 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4531 | lua_pushnil(L); |
| 4532 | return 1; |
| 4533 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4534 | |
| 4535 | /* Push configuration index in the stack. */ |
| 4536 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4537 | |
| 4538 | return 1; |
| 4539 | } |
| 4540 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4541 | /* If expected data not yet available, it returns a yield. This function |
| 4542 | * consumes the data in the buffer. It returns a string containing the |
| 4543 | * data. This string can be empty. |
| 4544 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4545 | __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] | 4546 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4547 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4548 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4549 | struct channel *req = si_oc(si); |
| 4550 | struct htx *htx; |
| 4551 | struct htx_blk *blk; |
| 4552 | size_t count; |
| 4553 | int stop = 0; |
| 4554 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4555 | htx = htx_from_buf(&req->buf); |
| 4556 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4557 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4558 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4559 | while (count && !stop && blk) { |
| 4560 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4561 | uint32_t sz = htx_get_blksz(blk); |
| 4562 | struct ist v; |
| 4563 | uint32_t vlen; |
| 4564 | char *nl; |
| 4565 | |
| 4566 | vlen = sz; |
| 4567 | if (vlen > count) { |
| 4568 | if (type != HTX_BLK_DATA) |
| 4569 | break; |
| 4570 | vlen = count; |
| 4571 | } |
| 4572 | |
| 4573 | switch (type) { |
| 4574 | case HTX_BLK_UNUSED: |
| 4575 | break; |
| 4576 | |
| 4577 | case HTX_BLK_DATA: |
| 4578 | v = htx_get_blk_value(htx, blk); |
| 4579 | v.len = vlen; |
| 4580 | nl = istchr(v, '\n'); |
| 4581 | if (nl != NULL) { |
| 4582 | stop = 1; |
| 4583 | vlen = nl - v.ptr + 1; |
| 4584 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4585 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4586 | break; |
| 4587 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4588 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4589 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4590 | stop = 1; |
| 4591 | break; |
| 4592 | |
| 4593 | default: |
| 4594 | break; |
| 4595 | } |
| 4596 | |
| 4597 | co_set_data(req, co_data(req) - vlen); |
| 4598 | count -= vlen; |
| 4599 | if (sz == vlen) |
| 4600 | blk = htx_remove_blk(htx, blk); |
| 4601 | else { |
| 4602 | htx_cut_data_blk(htx, blk, vlen); |
| 4603 | break; |
| 4604 | } |
| 4605 | } |
| 4606 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4607 | /* The message was fully consumed and no more data are expected |
| 4608 | * (EOM flag set). |
| 4609 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4610 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4611 | stop = 1; |
| 4612 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4613 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4614 | if (!stop) { |
| 4615 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4616 | 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] | 4617 | } |
| 4618 | |
| 4619 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4620 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4621 | return 1; |
| 4622 | } |
| 4623 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4624 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4625 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4626 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +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)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4629 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4630 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4631 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4632 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4633 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4634 | } |
| 4635 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4636 | /* If expected data not yet available, it returns a yield. This function |
| 4637 | * consumes the data in the buffer. It returns a string containing the |
| 4638 | * data. This string can be empty. |
| 4639 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4640 | __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] | 4641 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4642 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4643 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4644 | struct channel *req = si_oc(si); |
| 4645 | struct htx *htx; |
| 4646 | struct htx_blk *blk; |
| 4647 | size_t count; |
| 4648 | int len; |
| 4649 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4650 | htx = htx_from_buf(&req->buf); |
| 4651 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4652 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4653 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4654 | while (count && len && blk) { |
| 4655 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4656 | uint32_t sz = htx_get_blksz(blk); |
| 4657 | struct ist v; |
| 4658 | uint32_t vlen; |
| 4659 | |
| 4660 | vlen = sz; |
| 4661 | if (len > 0 && vlen > len) |
| 4662 | vlen = len; |
| 4663 | if (vlen > count) { |
| 4664 | if (type != HTX_BLK_DATA) |
| 4665 | break; |
| 4666 | vlen = count; |
| 4667 | } |
| 4668 | |
| 4669 | switch (type) { |
| 4670 | case HTX_BLK_UNUSED: |
| 4671 | break; |
| 4672 | |
| 4673 | case HTX_BLK_DATA: |
| 4674 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4675 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4676 | break; |
| 4677 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4678 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4679 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4680 | len = 0; |
| 4681 | break; |
| 4682 | |
| 4683 | default: |
| 4684 | break; |
| 4685 | } |
| 4686 | |
| 4687 | co_set_data(req, co_data(req) - vlen); |
| 4688 | count -= vlen; |
| 4689 | if (len > 0) |
| 4690 | len -= vlen; |
| 4691 | if (sz == vlen) |
| 4692 | blk = htx_remove_blk(htx, blk); |
| 4693 | else { |
| 4694 | htx_cut_data_blk(htx, blk, vlen); |
| 4695 | break; |
| 4696 | } |
| 4697 | } |
| 4698 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4699 | /* The message was fully consumed and no more data are expected |
| 4700 | * (EOM flag set). |
| 4701 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4702 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4703 | len = 0; |
| 4704 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4705 | htx_to_buf(htx, &req->buf); |
| 4706 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4707 | /* If we are no other data available, yield waiting for new data. */ |
| 4708 | if (len) { |
| 4709 | if (len > 0) { |
| 4710 | lua_pushinteger(L, len); |
| 4711 | lua_replace(L, 2); |
| 4712 | } |
| 4713 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4714 | 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] | 4715 | } |
| 4716 | |
| 4717 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4718 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4719 | return 1; |
| 4720 | } |
| 4721 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4722 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4723 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4724 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4725 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4726 | int len = -1; |
| 4727 | |
| 4728 | /* Check arguments. */ |
| 4729 | if (lua_gettop(L) > 2) |
| 4730 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4731 | if (lua_gettop(L) >= 2) { |
| 4732 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4733 | lua_pop(L, 1); |
| 4734 | } |
| 4735 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4736 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4737 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4738 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4739 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4740 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4741 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | /* Append data in the output side of the buffer. This data is immediately |
| 4745 | * sent. The function returns the amount of data written. If the buffer |
| 4746 | * cannot contain the data, the function yields. The function returns -1 |
| 4747 | * if the channel is closed. |
| 4748 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4749 | __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] | 4750 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4751 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4752 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4753 | struct channel *res = si_ic(si); |
| 4754 | struct htx *htx = htx_from_buf(&res->buf); |
| 4755 | const char *data; |
| 4756 | size_t len; |
| 4757 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4758 | int max; |
| 4759 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4760 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4761 | if (!max) |
| 4762 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4763 | |
| 4764 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4765 | |
| 4766 | /* Get the max amount of data which can write as input in the channel. */ |
| 4767 | if (max > (len - l)) |
| 4768 | max = len - l; |
| 4769 | |
| 4770 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4771 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4772 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4773 | |
| 4774 | /* update counters. */ |
| 4775 | l += max; |
| 4776 | lua_pop(L, 1); |
| 4777 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | /* If some data is not send, declares the situation to the |
| 4780 | * applet, and returns a yield. |
| 4781 | */ |
| 4782 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4783 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4784 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4785 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4786 | 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] | 4787 | } |
| 4788 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4789 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4790 | return 1; |
| 4791 | } |
| 4792 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4793 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4794 | * yield the LUA process, and resume it without checking the |
| 4795 | * input arguments. |
| 4796 | */ |
| 4797 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4798 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4799 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4800 | |
| 4801 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4802 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4804 | WILL_LJMP(lua_error(L)); |
| 4805 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4806 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4807 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4808 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4809 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4810 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4814 | { |
| 4815 | const char *name; |
| 4816 | int ret; |
| 4817 | |
| 4818 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4819 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4820 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4821 | |
| 4822 | /* Push in the stack the "response" entry. */ |
| 4823 | ret = lua_getfield(L, 1, "response"); |
| 4824 | if (ret != LUA_TTABLE) { |
| 4825 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4826 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4827 | WILL_LJMP(lua_error(L)); |
| 4828 | } |
| 4829 | |
| 4830 | /* check if the header is already registered if it is not |
| 4831 | * the case, register it. |
| 4832 | */ |
| 4833 | ret = lua_getfield(L, -1, name); |
| 4834 | if (ret == LUA_TNIL) { |
| 4835 | |
| 4836 | /* Entry not found. */ |
| 4837 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4838 | |
| 4839 | /* Insert the new header name in the array in the top of the stack. |
| 4840 | * It left the new array in the top of the stack. |
| 4841 | */ |
| 4842 | lua_newtable(L); |
| 4843 | lua_pushvalue(L, 2); |
| 4844 | lua_pushvalue(L, -2); |
| 4845 | lua_settable(L, -4); |
| 4846 | |
| 4847 | } else if (ret != LUA_TTABLE) { |
| 4848 | |
| 4849 | /* corruption error. */ |
| 4850 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4851 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4852 | WILL_LJMP(lua_error(L)); |
| 4853 | } |
| 4854 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4855 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4856 | * the header value as new entry. |
| 4857 | */ |
| 4858 | lua_pushvalue(L, 3); |
| 4859 | ret = lua_rawlen(L, -2); |
| 4860 | lua_rawseti(L, -2, ret + 1); |
| 4861 | lua_pushboolean(L, 1); |
| 4862 | return 1; |
| 4863 | } |
| 4864 | |
| 4865 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4866 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4867 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4868 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4869 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4870 | |
| 4871 | if (status < 100 || status > 599) { |
| 4872 | lua_pushboolean(L, 0); |
| 4873 | return 1; |
| 4874 | } |
| 4875 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4876 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4877 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4878 | lua_pushboolean(L, 1); |
| 4879 | return 1; |
| 4880 | } |
| 4881 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4882 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4883 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4884 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4885 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4886 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4887 | struct channel *res = si_ic(si); |
| 4888 | struct htx *htx; |
| 4889 | struct htx_sl *sl; |
| 4890 | struct h1m h1m; |
| 4891 | const char *status, *reason; |
| 4892 | const char *name, *value; |
| 4893 | size_t nlen, vlen; |
| 4894 | unsigned int flags; |
| 4895 | |
| 4896 | /* Send the message at once. */ |
| 4897 | htx = htx_from_buf(&res->buf); |
| 4898 | h1m_init_res(&h1m); |
| 4899 | |
| 4900 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4901 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4902 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4903 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4904 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4905 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4906 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4907 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4908 | } |
| 4909 | else { |
| 4910 | flags = HTX_SL_F_IS_RESP; |
| 4911 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4912 | } |
| 4913 | if (!sl) { |
| 4914 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4915 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4916 | WILL_LJMP(lua_error(L)); |
| 4917 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4918 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4919 | |
| 4920 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4921 | lua_pushvalue(L, 0); |
| 4922 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4923 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4924 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4925 | WILL_LJMP(lua_error(L)); |
| 4926 | } |
| 4927 | |
| 4928 | /* Browse the list of headers. */ |
| 4929 | lua_pushnil(L); |
| 4930 | while(lua_next(L, -2) != 0) { |
| 4931 | /* We expect a string as -2. */ |
| 4932 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4933 | 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] | 4934 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4935 | lua_typename(L, lua_type(L, -2))); |
| 4936 | WILL_LJMP(lua_error(L)); |
| 4937 | } |
| 4938 | name = lua_tolstring(L, -2, &nlen); |
| 4939 | |
| 4940 | /* We expect an array as -1. */ |
| 4941 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4942 | 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] | 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, -1))); |
| 4946 | WILL_LJMP(lua_error(L)); |
| 4947 | } |
| 4948 | |
| 4949 | /* Browse the table who is on the top of the stack. */ |
| 4950 | lua_pushnil(L); |
| 4951 | while(lua_next(L, -2) != 0) { |
| 4952 | int id; |
| 4953 | |
| 4954 | /* We expect a number as -2. */ |
| 4955 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4956 | 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] | 4957 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4958 | name, |
| 4959 | lua_typename(L, lua_type(L, -2))); |
| 4960 | WILL_LJMP(lua_error(L)); |
| 4961 | } |
| 4962 | id = lua_tointeger(L, -2); |
| 4963 | |
| 4964 | /* We expect a string as -2. */ |
| 4965 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4966 | 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] | 4967 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4968 | name, id, |
| 4969 | lua_typename(L, lua_type(L, -1))); |
| 4970 | WILL_LJMP(lua_error(L)); |
| 4971 | } |
| 4972 | value = lua_tolstring(L, -1, &vlen); |
| 4973 | |
| 4974 | /* Simple Protocol checks. */ |
| 4975 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4976 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4977 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4978 | struct ist v = ist2(value, vlen); |
| 4979 | int ret; |
| 4980 | |
| 4981 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4982 | if (ret < 0) { |
| 4983 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4984 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4985 | name); |
| 4986 | WILL_LJMP(lua_error(L)); |
| 4987 | } |
| 4988 | else if (ret == 0) |
| 4989 | goto next; /* Skip it */ |
| 4990 | } |
| 4991 | |
| 4992 | /* Add a new header */ |
| 4993 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4994 | 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] | 4995 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4996 | name); |
| 4997 | WILL_LJMP(lua_error(L)); |
| 4998 | } |
| 4999 | next: |
| 5000 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5001 | lua_pop(L, 1); |
| 5002 | } |
| 5003 | |
| 5004 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5005 | lua_pop(L, 1); |
| 5006 | } |
| 5007 | |
| 5008 | if (h1m.flags & H1_MF_CHNK) |
| 5009 | h1m.flags &= ~H1_MF_CLEN; |
| 5010 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5011 | h1m.flags |= H1_MF_XFER_LEN; |
| 5012 | |
| 5013 | /* Uset HTX start-line flags */ |
| 5014 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5015 | flags |= HTX_SL_F_XFER_ENC; |
| 5016 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5017 | flags |= HTX_SL_F_XFER_LEN; |
| 5018 | if (h1m.flags & H1_MF_CHNK) |
| 5019 | flags |= HTX_SL_F_CHNK; |
| 5020 | else if (h1m.flags & H1_MF_CLEN) |
| 5021 | flags |= HTX_SL_F_CLEN; |
| 5022 | if (h1m.body_len == 0) |
| 5023 | flags |= HTX_SL_F_BODYLESS; |
| 5024 | } |
| 5025 | sl->flags |= flags; |
| 5026 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5027 | /* 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] | 5028 | * and the status code implies the presence of a message body, we must |
| 5029 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5030 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5031 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5032 | */ |
| 5033 | 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] | 5034 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5035 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5036 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | /* Add a new header */ |
| 5038 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5039 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5040 | 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] | 5041 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5042 | WILL_LJMP(lua_error(L)); |
| 5043 | } |
| 5044 | } |
| 5045 | |
| 5046 | /* Finalize headers. */ |
| 5047 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5048 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5049 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5050 | WILL_LJMP(lua_error(L)); |
| 5051 | } |
| 5052 | |
| 5053 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5054 | b_reset(&res->buf); |
| 5055 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5056 | WILL_LJMP(lua_error(L)); |
| 5057 | } |
| 5058 | |
| 5059 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5060 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | |
| 5062 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5063 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5064 | return 0; |
| 5065 | |
| 5066 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5067 | /* We will build the status line and the headers of the HTTP response. |
| 5068 | * We will try send at once if its not possible, we give back the hand |
| 5069 | * waiting for more room. |
| 5070 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5071 | __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] | 5072 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5073 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5074 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5075 | struct channel *res = si_ic(si); |
| 5076 | |
| 5077 | if (co_data(res)) { |
| 5078 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5079 | 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] | 5080 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5081 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5085 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5086 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5087 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5088 | } |
| 5089 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5090 | /* |
| 5091 | * |
| 5092 | * |
| 5093 | * Class HTTP |
| 5094 | * |
| 5095 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5096 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5097 | |
| 5098 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5099 | * a class stream, otherwise it throws an error. |
| 5100 | */ |
| 5101 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
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 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5104 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5105 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5106 | /* This function creates and push in the stack a HTTP object |
| 5107 | * according with a current TXN. |
| 5108 | */ |
| 5109 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5110 | { |
| 5111 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5112 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5113 | /* Check stack size. */ |
| 5114 | if (!lua_checkstack(L, 3)) |
| 5115 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5116 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5117 | /* Create the object: obj[0] = userdata. |
| 5118 | * Note that the base of the Converters object is the |
| 5119 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5120 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5121 | lua_newtable(L); |
| 5122 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5123 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5124 | |
| 5125 | htxn->s = txn->s; |
| 5126 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5127 | htxn->dir = txn->dir; |
| 5128 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5129 | |
| 5130 | /* Pop a class stream metatable and affect it to the table. */ |
| 5131 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5132 | lua_setmetatable(L, -2); |
| 5133 | |
| 5134 | return 1; |
| 5135 | } |
| 5136 | |
| 5137 | /* This function creates ans returns an array of HTTP headers. |
| 5138 | * This function does not fails. It is used as wrapper with the |
| 5139 | * 2 following functions. |
| 5140 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5141 | __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] | 5142 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5143 | struct htx *htx; |
| 5144 | int32_t pos; |
| 5145 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5146 | /* Create the table. */ |
| 5147 | lua_newtable(L); |
| 5148 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5149 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5150 | htx = htxbuf(&msg->chn->buf); |
| 5151 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5152 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5153 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5154 | struct ist n, v; |
| 5155 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5156 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5157 | if (type == HTX_BLK_HDR) { |
| 5158 | n = htx_get_blk_name(htx,blk); |
| 5159 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5160 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5161 | else if (type == HTX_BLK_EOH) |
| 5162 | break; |
| 5163 | else |
| 5164 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5165 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5166 | /* Check for existing entry: |
| 5167 | * assume that the table is on the top of the stack, and |
| 5168 | * push the key in the stack, the function lua_gettable() |
| 5169 | * perform the lookup. |
| 5170 | */ |
| 5171 | lua_pushlstring(L, n.ptr, n.len); |
| 5172 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5173 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5174 | switch (lua_type(L, -1)) { |
| 5175 | case LUA_TNIL: |
| 5176 | /* Table not found, create it. */ |
| 5177 | lua_pop(L, 1); /* remove the nil value. */ |
| 5178 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5179 | lua_newtable(L); /* create and push empty table. */ |
| 5180 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5181 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5182 | 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] | 5183 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5184 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5185 | case LUA_TTABLE: |
| 5186 | /* Entry found: push the value in the table. */ |
| 5187 | len = lua_rawlen(L, -1); |
| 5188 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5189 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5190 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5191 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5192 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5193 | default: |
| 5194 | /* Other cases are errors. */ |
| 5195 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5196 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5197 | } |
| 5198 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5199 | return 1; |
| 5200 | } |
| 5201 | |
| 5202 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5203 | { |
| 5204 | struct hlua_txn *htxn; |
| 5205 | |
| 5206 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5207 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5208 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5209 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5210 | WILL_LJMP(lua_error(L)); |
| 5211 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5212 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5216 | { |
| 5217 | struct hlua_txn *htxn; |
| 5218 | |
| 5219 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5220 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5221 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5222 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5223 | WILL_LJMP(lua_error(L)); |
| 5224 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5225 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | /* This function replace full header, or just a value in |
| 5229 | * the request or in the response. It is a wrapper fir the |
| 5230 | * 4 following functions. |
| 5231 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5232 | __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] | 5233 | { |
| 5234 | size_t name_len; |
| 5235 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5236 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5237 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5238 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5239 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5240 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5241 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5242 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5243 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5244 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5245 | 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] | 5246 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5247 | return 0; |
| 5248 | } |
| 5249 | |
| 5250 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5251 | { |
| 5252 | struct hlua_txn *htxn; |
| 5253 | |
| 5254 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5255 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5256 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5257 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5258 | WILL_LJMP(lua_error(L)); |
| 5259 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5260 | 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] | 5261 | } |
| 5262 | |
| 5263 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5264 | { |
| 5265 | struct hlua_txn *htxn; |
| 5266 | |
| 5267 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5268 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5269 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5270 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5271 | WILL_LJMP(lua_error(L)); |
| 5272 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5273 | 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] | 5274 | } |
| 5275 | |
| 5276 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5277 | { |
| 5278 | struct hlua_txn *htxn; |
| 5279 | |
| 5280 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5281 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5282 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5283 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5284 | WILL_LJMP(lua_error(L)); |
| 5285 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5286 | 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] | 5287 | } |
| 5288 | |
| 5289 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5290 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5291 | struct hlua_txn *htxn; |
| 5292 | |
| 5293 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5294 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5295 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5296 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5297 | WILL_LJMP(lua_error(L)); |
| 5298 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5299 | 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] | 5300 | } |
| 5301 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5302 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5303 | * It is a wrapper for the 2 following functions. |
| 5304 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5305 | __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] | 5306 | { |
| 5307 | size_t len; |
| 5308 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5309 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5310 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5311 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5312 | ctx.blk = NULL; |
| 5313 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5314 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5319 | { |
| 5320 | struct hlua_txn *htxn; |
| 5321 | |
| 5322 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5323 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5324 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5325 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5326 | WILL_LJMP(lua_error(L)); |
| 5327 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5328 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5332 | { |
| 5333 | struct hlua_txn *htxn; |
| 5334 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5335 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5336 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5337 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5338 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5339 | WILL_LJMP(lua_error(L)); |
| 5340 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5341 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | /* This function adds an header. It is a wrapper used by |
| 5345 | * the 2 following functions. |
| 5346 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5347 | __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] | 5348 | { |
| 5349 | size_t name_len; |
| 5350 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5351 | size_t value_len; |
| 5352 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5353 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5354 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5355 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5356 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5357 | return 0; |
| 5358 | } |
| 5359 | |
| 5360 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5361 | { |
| 5362 | struct hlua_txn *htxn; |
| 5363 | |
| 5364 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5365 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5366 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5367 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5368 | WILL_LJMP(lua_error(L)); |
| 5369 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5370 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5374 | { |
| 5375 | struct hlua_txn *htxn; |
| 5376 | |
| 5377 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5378 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5379 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5380 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5381 | WILL_LJMP(lua_error(L)); |
| 5382 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5383 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5387 | { |
| 5388 | struct hlua_txn *htxn; |
| 5389 | |
| 5390 | MAY_LJMP(check_args(L, 3, "req_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_REQ || !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->req); |
| 5397 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5401 | { |
| 5402 | struct hlua_txn *htxn; |
| 5403 | |
| 5404 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5405 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5406 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5407 | if (htxn->dir != SMP_OPT_DIR_RES || !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 | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5410 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5411 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* This function set the method. */ |
| 5415 | static int hlua_http_req_set_meth(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 | 08504f4 | 2015-03-16 14:17:08 +0100 | [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(0, 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 method. */ |
| 5429 | static int hlua_http_req_set_path(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 / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [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 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5438 | 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] | 5439 | return 1; |
| 5440 | } |
| 5441 | |
| 5442 | /* This function set the query-string. */ |
| 5443 | static int hlua_http_req_set_query(lua_State *L) |
| 5444 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5445 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5446 | size_t name_len; |
| 5447 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5448 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5449 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5450 | WILL_LJMP(lua_error(L)); |
| 5451 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5452 | /* Check length. */ |
| 5453 | if (name_len > trash.size - 1) { |
| 5454 | lua_pushboolean(L, 0); |
| 5455 | return 1; |
| 5456 | } |
| 5457 | |
| 5458 | /* Add the mark question as prefix. */ |
| 5459 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5460 | trash.area[trash.data++] = '?'; |
| 5461 | memcpy(trash.area + trash.data, name, name_len); |
| 5462 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5463 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5464 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5465 | 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] | 5466 | return 1; |
| 5467 | } |
| 5468 | |
| 5469 | /* This function set the uri. */ |
| 5470 | static int hlua_http_req_set_uri(lua_State *L) |
| 5471 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5472 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5473 | size_t name_len; |
| 5474 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5475 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5476 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5477 | WILL_LJMP(lua_error(L)); |
| 5478 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5479 | 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] | 5480 | return 1; |
| 5481 | } |
| 5482 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5483 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5484 | static int hlua_http_res_set_status(lua_State *L) |
| 5485 | { |
| 5486 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5487 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5488 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5489 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5490 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5491 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5492 | WILL_LJMP(lua_error(L)); |
| 5493 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5494 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5495 | return 0; |
| 5496 | } |
| 5497 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5498 | /* |
| 5499 | * |
| 5500 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5501 | * Class TXN |
| 5502 | * |
| 5503 | * |
| 5504 | */ |
| 5505 | |
| 5506 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5507 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5508 | */ |
| 5509 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5510 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5511 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5512 | } |
| 5513 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5514 | __LJMP static int hlua_set_var(lua_State *L) |
| 5515 | { |
| 5516 | struct hlua_txn *htxn; |
| 5517 | const char *name; |
| 5518 | size_t len; |
| 5519 | struct sample smp; |
| 5520 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5521 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5522 | 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] | 5523 | |
| 5524 | /* It is useles to retrieve the stream, but this function |
| 5525 | * runs only in a stream context. |
| 5526 | */ |
| 5527 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5528 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5529 | |
| 5530 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5531 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5532 | hlua_lua2smp(L, 3, &smp); |
| 5533 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5534 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5535 | * already takes care of duplicating dynamic var data. |
| 5536 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5537 | 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] | 5538 | |
| 5539 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5540 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5541 | else |
| 5542 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5543 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5544 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5545 | } |
| 5546 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5547 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5548 | { |
| 5549 | struct hlua_txn *htxn; |
| 5550 | const char *name; |
| 5551 | size_t len; |
| 5552 | struct sample smp; |
| 5553 | |
| 5554 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5555 | |
| 5556 | /* It is useles to retrieve the stream, but this function |
| 5557 | * runs only in a stream context. |
| 5558 | */ |
| 5559 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5560 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5561 | |
| 5562 | /* Unset the variable. */ |
| 5563 | 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] | 5564 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5565 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5566 | } |
| 5567 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5568 | __LJMP static int hlua_get_var(lua_State *L) |
| 5569 | { |
| 5570 | struct hlua_txn *htxn; |
| 5571 | const char *name; |
| 5572 | size_t len; |
| 5573 | struct sample smp; |
| 5574 | |
| 5575 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5576 | |
| 5577 | /* It is useles to retrieve the stream, but this function |
| 5578 | * runs only in a stream context. |
| 5579 | */ |
| 5580 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5581 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5582 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5583 | 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] | 5584 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5585 | lua_pushnil(L); |
| 5586 | return 1; |
| 5587 | } |
| 5588 | |
| 5589 | return hlua_smp2lua(L, &smp); |
| 5590 | } |
| 5591 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5592 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5593 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5594 | struct hlua *hlua; |
| 5595 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5596 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5597 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5598 | /* It is useles to retrieve the stream, but this function |
| 5599 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5600 | */ |
| 5601 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5602 | |
| 5603 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5604 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5605 | if (!hlua) |
| 5606 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5607 | |
| 5608 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5609 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5610 | |
| 5611 | /* Get and store new value. */ |
| 5612 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5613 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5614 | |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5618 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5619 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5620 | struct hlua *hlua; |
| 5621 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5622 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5623 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5624 | /* It is useles to retrieve the stream, but this function |
| 5625 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5626 | */ |
| 5627 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5628 | |
| 5629 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5630 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5631 | if (!hlua) { |
| 5632 | lua_pushnil(L); |
| 5633 | return 1; |
| 5634 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5635 | |
| 5636 | /* Push configuration index in the stack. */ |
| 5637 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5638 | |
| 5639 | return 1; |
| 5640 | } |
| 5641 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5642 | /* Create stack entry containing a class TXN. This function |
| 5643 | * return 0 if the stack does not contains free slots, |
| 5644 | * otherwise it returns 1. |
| 5645 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5646 | 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] | 5647 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5648 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5649 | |
| 5650 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5651 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5652 | return 0; |
| 5653 | |
| 5654 | /* NOTE: The allocation never fails. The failure |
| 5655 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5656 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5657 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5658 | /* Create the object: obj[0] = userdata. */ |
| 5659 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5660 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5661 | lua_rawseti(L, -2, 0); |
| 5662 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5663 | htxn->s = s; |
| 5664 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5665 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5666 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5667 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5668 | /* Create the "f" field that contains a list of fetches. */ |
| 5669 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5670 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5671 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5672 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5673 | |
| 5674 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5675 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5676 | 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] | 5677 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5678 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5679 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5680 | /* Create the "c" field that contains a list of converters. */ |
| 5681 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5682 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5683 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5684 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5685 | |
| 5686 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5687 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5688 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5689 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5690 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5691 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5692 | /* Create the "req" field that contains the request channel object. */ |
| 5693 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5694 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5695 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5696 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5697 | |
| 5698 | /* Create the "res" field that contains the response channel object. */ |
| 5699 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5700 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5701 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5702 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5703 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5704 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5705 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5706 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5707 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5708 | return 0; |
| 5709 | } |
| 5710 | else |
| 5711 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5712 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5713 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5714 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5715 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5716 | lua_setmetatable(L, -2); |
| 5717 | |
| 5718 | return 1; |
| 5719 | } |
| 5720 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5721 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5722 | { |
| 5723 | const char *msg; |
| 5724 | struct hlua_txn *htxn; |
| 5725 | |
| 5726 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5727 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5728 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5729 | |
| 5730 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5731 | return 0; |
| 5732 | } |
| 5733 | |
| 5734 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5735 | { |
| 5736 | int level; |
| 5737 | const char *msg; |
| 5738 | struct hlua_txn *htxn; |
| 5739 | |
| 5740 | MAY_LJMP(check_args(L, 3, "log")); |
| 5741 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5742 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5743 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5744 | |
| 5745 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5746 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5747 | |
| 5748 | hlua_sendlog(htxn->s->be, level, msg); |
| 5749 | return 0; |
| 5750 | } |
| 5751 | |
| 5752 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5753 | { |
| 5754 | const char *msg; |
| 5755 | struct hlua_txn *htxn; |
| 5756 | |
| 5757 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5758 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5759 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5760 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5761 | return 0; |
| 5762 | } |
| 5763 | |
| 5764 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5765 | { |
| 5766 | const char *msg; |
| 5767 | struct hlua_txn *htxn; |
| 5768 | |
| 5769 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5770 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5771 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5772 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5773 | return 0; |
| 5774 | } |
| 5775 | |
| 5776 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5777 | { |
| 5778 | const char *msg; |
| 5779 | struct hlua_txn *htxn; |
| 5780 | |
| 5781 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5782 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5783 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5784 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5785 | return 0; |
| 5786 | } |
| 5787 | |
| 5788 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5789 | { |
| 5790 | const char *msg; |
| 5791 | struct hlua_txn *htxn; |
| 5792 | |
| 5793 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5794 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5795 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5796 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5797 | return 0; |
| 5798 | } |
| 5799 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5800 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5801 | { |
| 5802 | struct hlua_txn *htxn; |
| 5803 | int ll; |
| 5804 | |
| 5805 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5806 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5807 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5808 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5809 | if (ll < -1 || ll > NB_LOG_LEVELS) |
| 5810 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be one of the following value:" |
| 5811 | " core.silent(-1), core.emerg(0), core.alert(1), core.crit(2), core.error(3)," |
| 5812 | " 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] | 5813 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5814 | htxn->s->logs.level = (ll == -1) ? ll : ll + 1; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5815 | return 0; |
| 5816 | } |
| 5817 | |
| 5818 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5819 | { |
| 5820 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5821 | int tos; |
| 5822 | |
| 5823 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5824 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5825 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5826 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5827 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5832 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5833 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5834 | int mark; |
| 5835 | |
| 5836 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5837 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5838 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5839 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5840 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5841 | return 0; |
| 5842 | } |
| 5843 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5844 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5845 | { |
| 5846 | struct hlua_txn *htxn; |
| 5847 | |
| 5848 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5849 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5850 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5851 | return 0; |
| 5852 | } |
| 5853 | |
| 5854 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5855 | { |
| 5856 | struct hlua_txn *htxn; |
| 5857 | |
| 5858 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5859 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5860 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5861 | return 0; |
| 5862 | } |
| 5863 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5864 | /* 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] | 5865 | * 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] | 5866 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5867 | * error. The Reply must be on top of the stack. |
| 5868 | */ |
| 5869 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5870 | { |
| 5871 | struct htx *htx; |
| 5872 | struct htx_sl *sl; |
| 5873 | struct h1m h1m; |
| 5874 | const char *status, *reason, *body; |
| 5875 | size_t status_len, reason_len, body_len; |
| 5876 | int ret, code, flags; |
| 5877 | |
| 5878 | code = 200; |
| 5879 | status = "200"; |
| 5880 | status_len = 3; |
| 5881 | ret = lua_getfield(L, -1, "status"); |
| 5882 | if (ret == LUA_TNUMBER) { |
| 5883 | code = lua_tointeger(L, -1); |
| 5884 | status = lua_tolstring(L, -1, &status_len); |
| 5885 | } |
| 5886 | lua_pop(L, 1); |
| 5887 | |
| 5888 | reason = http_get_reason(code); |
| 5889 | reason_len = strlen(reason); |
| 5890 | ret = lua_getfield(L, -1, "reason"); |
| 5891 | if (ret == LUA_TSTRING) |
| 5892 | reason = lua_tolstring(L, -1, &reason_len); |
| 5893 | lua_pop(L, 1); |
| 5894 | |
| 5895 | body = NULL; |
| 5896 | body_len = 0; |
| 5897 | ret = lua_getfield(L, -1, "body"); |
| 5898 | if (ret == LUA_TSTRING) |
| 5899 | body = lua_tolstring(L, -1, &body_len); |
| 5900 | lua_pop(L, 1); |
| 5901 | |
| 5902 | /* Prepare the response before inserting the headers */ |
| 5903 | h1m_init_res(&h1m); |
| 5904 | htx = htx_from_buf(&s->res.buf); |
| 5905 | channel_htx_truncate(&s->res, htx); |
| 5906 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5907 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5908 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5909 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5910 | } |
| 5911 | else { |
| 5912 | flags = HTX_SL_F_IS_RESP; |
| 5913 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5914 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5915 | } |
| 5916 | if (!sl) |
| 5917 | goto fail; |
| 5918 | sl->info.res.status = code; |
| 5919 | |
| 5920 | /* Push in the stack the "headers" entry. */ |
| 5921 | ret = lua_getfield(L, -1, "headers"); |
| 5922 | if (ret != LUA_TTABLE) |
| 5923 | goto skip_headers; |
| 5924 | |
| 5925 | lua_pushnil(L); |
| 5926 | while (lua_next(L, -2) != 0) { |
| 5927 | struct ist name, value; |
| 5928 | const char *n, *v; |
| 5929 | size_t nlen, vlen; |
| 5930 | |
| 5931 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5932 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5933 | goto next_hdr; |
| 5934 | } |
| 5935 | |
| 5936 | n = lua_tolstring(L, -2, &nlen); |
| 5937 | name = ist2(n, nlen); |
| 5938 | if (isteqi(name, ist("content-length"))) { |
| 5939 | /* Always skip content-length header. It will be added |
| 5940 | * later with the correct len |
| 5941 | */ |
| 5942 | goto next_hdr; |
| 5943 | } |
| 5944 | |
| 5945 | /* Loop on header's values */ |
| 5946 | lua_pushnil(L); |
| 5947 | while (lua_next(L, -2)) { |
| 5948 | if (!lua_isstring(L, -1)) { |
| 5949 | /* Skip the value if it is not a string */ |
| 5950 | goto next_value; |
| 5951 | } |
| 5952 | |
| 5953 | v = lua_tolstring(L, -1, &vlen); |
| 5954 | value = ist2(v, vlen); |
| 5955 | |
| 5956 | if (isteqi(name, ist("transfer-encoding"))) |
| 5957 | h1_parse_xfer_enc_header(&h1m, value); |
| 5958 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5959 | goto fail; |
| 5960 | |
| 5961 | next_value: |
| 5962 | lua_pop(L, 1); |
| 5963 | } |
| 5964 | |
| 5965 | next_hdr: |
| 5966 | lua_pop(L, 1); |
| 5967 | } |
| 5968 | skip_headers: |
| 5969 | lua_pop(L, 1); |
| 5970 | |
| 5971 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5972 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5973 | const char *clen = ultoa(body_len); |
| 5974 | |
| 5975 | h1m.flags |= H1_MF_CLEN; |
| 5976 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5977 | goto fail; |
| 5978 | } |
| 5979 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5980 | h1m.flags |= H1_MF_XFER_LEN; |
| 5981 | |
| 5982 | /* Update HTX start-line flags */ |
| 5983 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5984 | flags |= HTX_SL_F_XFER_ENC; |
| 5985 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5986 | flags |= HTX_SL_F_XFER_LEN; |
| 5987 | if (h1m.flags & H1_MF_CHNK) |
| 5988 | flags |= HTX_SL_F_CHNK; |
| 5989 | else if (h1m.flags & H1_MF_CLEN) |
| 5990 | flags |= HTX_SL_F_CLEN; |
| 5991 | if (h1m.body_len == 0) |
| 5992 | flags |= HTX_SL_F_BODYLESS; |
| 5993 | } |
| 5994 | sl->flags |= flags; |
| 5995 | |
| 5996 | |
| 5997 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5998 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5999 | goto fail; |
| 6000 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 6001 | htx->flags |= HTX_FL_EOM; |
| 6002 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6003 | /* Now, forward the response and terminate the transaction */ |
| 6004 | s->txn->status = code; |
| 6005 | htx_to_buf(htx, &s->res.buf); |
| 6006 | if (!http_forward_proxy_resp(s, 1)) |
| 6007 | goto fail; |
| 6008 | |
| 6009 | return 1; |
| 6010 | |
| 6011 | fail: |
| 6012 | channel_htx_truncate(&s->res, htx); |
| 6013 | return 0; |
| 6014 | } |
| 6015 | |
| 6016 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 6017 | * processing is just aborted. Nothing is returned to the client and all |
| 6018 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 6019 | * is forwarded to the client before terminating the transaction. On success, |
| 6020 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 6021 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 6022 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6023 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6024 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6025 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6026 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6027 | struct stream *s; |
| 6028 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6029 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6030 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6031 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6032 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 6033 | * just end the execution of the current lua code. */ |
| 6034 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6035 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6036 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6037 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6038 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6039 | struct channel *req = &s->req; |
| 6040 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6041 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6042 | channel_auto_read(req); |
| 6043 | channel_abort(req); |
| 6044 | channel_auto_close(req); |
| 6045 | channel_erase(req); |
| 6046 | |
| 6047 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 6048 | channel_auto_read(res); |
| 6049 | channel_auto_close(res); |
| 6050 | channel_shutr_now(res); |
| 6051 | |
| 6052 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 6053 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6054 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6055 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6056 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 6057 | /* No reply or invalid reply */ |
| 6058 | s->txn->status = 0; |
| 6059 | http_reply_and_close(s, 0, NULL); |
| 6060 | } |
| 6061 | else { |
| 6062 | /* Remove extra args to have the reply on top of the stack */ |
| 6063 | if (lua_gettop(L) > 2) |
| 6064 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6065 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6066 | if (!hlua_txn_forward_reply(L, s)) { |
| 6067 | if (!(s->flags & SF_ERR_MASK)) |
| 6068 | s->flags |= SF_ERR_PRXCOND; |
| 6069 | lua_pushinteger(L, ACT_RET_ERR); |
| 6070 | WILL_LJMP(hlua_done(L)); |
| 6071 | return 0; /* Never reached */ |
| 6072 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6073 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6074 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6075 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 6076 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 6077 | /* let's log the request time */ |
| 6078 | s->logs.tv_request = now; |
| 6079 | 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] | 6080 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6081 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6082 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6083 | done: |
| 6084 | if (!(s->flags & SF_ERR_MASK)) |
| 6085 | s->flags |= SF_ERR_LOCAL; |
| 6086 | if (!(s->flags & SF_FINST_MASK)) |
| 6087 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6088 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 6089 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6090 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6091 | return 0; |
| 6092 | } |
| 6093 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6094 | /* |
| 6095 | * |
| 6096 | * |
| 6097 | * Class REPLY |
| 6098 | * |
| 6099 | * |
| 6100 | */ |
| 6101 | |
| 6102 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 6103 | * free slots, the function fails and returns 0; |
| 6104 | */ |
| 6105 | static int hlua_txn_reply_new(lua_State *L) |
| 6106 | { |
| 6107 | struct hlua_txn *htxn; |
| 6108 | const char *reason, *body = NULL; |
| 6109 | int ret, status; |
| 6110 | |
| 6111 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6112 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6113 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6114 | WILL_LJMP(lua_error(L)); |
| 6115 | } |
| 6116 | |
| 6117 | /* Default value */ |
| 6118 | status = 200; |
| 6119 | reason = http_get_reason(status); |
| 6120 | |
| 6121 | if (lua_istable(L, 2)) { |
| 6122 | /* load status and reason from the table argument at index 2 */ |
| 6123 | ret = lua_getfield(L, 2, "status"); |
| 6124 | if (ret == LUA_TNIL) |
| 6125 | goto reason; |
| 6126 | else if (ret != LUA_TNUMBER) { |
| 6127 | /* invalid status: ignore the reason */ |
| 6128 | goto body; |
| 6129 | } |
| 6130 | status = lua_tointeger(L, -1); |
| 6131 | |
| 6132 | reason: |
| 6133 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6134 | ret = lua_getfield(L, 2, "reason"); |
| 6135 | if (ret == LUA_TSTRING) |
| 6136 | reason = lua_tostring(L, -1); |
| 6137 | |
| 6138 | body: |
| 6139 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6140 | ret = lua_getfield(L, 2, "body"); |
| 6141 | if (ret == LUA_TSTRING) |
| 6142 | body = lua_tostring(L, -1); |
| 6143 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6144 | } |
| 6145 | |
| 6146 | /* Create the Reply table */ |
| 6147 | lua_newtable(L); |
| 6148 | |
| 6149 | /* Add status element */ |
| 6150 | lua_pushstring(L, "status"); |
| 6151 | lua_pushinteger(L, status); |
| 6152 | lua_settable(L, -3); |
| 6153 | |
| 6154 | /* Add reason element */ |
| 6155 | reason = http_get_reason(status); |
| 6156 | lua_pushstring(L, "reason"); |
| 6157 | lua_pushstring(L, reason); |
| 6158 | lua_settable(L, -3); |
| 6159 | |
| 6160 | /* Add body element, nil if undefined */ |
| 6161 | lua_pushstring(L, "body"); |
| 6162 | if (body) |
| 6163 | lua_pushstring(L, body); |
| 6164 | else |
| 6165 | lua_pushnil(L); |
| 6166 | lua_settable(L, -3); |
| 6167 | |
| 6168 | /* Add headers element */ |
| 6169 | lua_pushstring(L, "headers"); |
| 6170 | lua_newtable(L); |
| 6171 | |
| 6172 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6173 | if (lua_istable(L, 2)) { |
| 6174 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6175 | ret = lua_getfield(L, 2, "headers"); |
| 6176 | if (ret == LUA_TTABLE) { |
| 6177 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6178 | lua_pushnil(L); |
| 6179 | while (lua_next(L, -2) != 0) { |
| 6180 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6181 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6182 | /* invalid value type, skip it */ |
| 6183 | lua_pop(L, 1); |
| 6184 | continue; |
| 6185 | } |
| 6186 | |
| 6187 | |
| 6188 | /* Duplicate the key and swap it with the value. */ |
| 6189 | lua_pushvalue(L, -2); |
| 6190 | lua_insert(L, -2); |
| 6191 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6192 | |
| 6193 | lua_newtable(L); |
| 6194 | lua_insert(L, -2); |
| 6195 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6196 | |
| 6197 | if (lua_isstring(L, -1)) { |
| 6198 | /* push the value in the inner table */ |
| 6199 | lua_rawseti(L, -2, 1); |
| 6200 | } |
| 6201 | else { /* table */ |
| 6202 | lua_pushnil(L); |
| 6203 | while (lua_next(L, -2) != 0) { |
| 6204 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6205 | if (!lua_isstring(L, -1)) { |
| 6206 | /* invalid value type, skip it*/ |
| 6207 | lua_pop(L, 1); |
| 6208 | continue; |
| 6209 | } |
| 6210 | /* push the value in the inner table */ |
| 6211 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6212 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6213 | } |
| 6214 | lua_pop(L, 1); |
| 6215 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6216 | } |
| 6217 | |
| 6218 | /* push (k,v) on the stack in the headers table: |
| 6219 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6220 | */ |
| 6221 | lua_settable(L, -5); |
| 6222 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6223 | } |
| 6224 | } |
| 6225 | lua_pop(L, 1); |
| 6226 | } |
| 6227 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6228 | lua_settable(L, -3); |
| 6229 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6230 | |
| 6231 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6232 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6233 | lua_setmetatable(L, -2); |
| 6234 | return 1; |
| 6235 | } |
| 6236 | |
| 6237 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6238 | * provided, the default one corresponding to the status code is used. |
| 6239 | */ |
| 6240 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6241 | { |
| 6242 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6243 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6244 | |
| 6245 | /* First argument (self) must be a table */ |
| 6246 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6247 | |
| 6248 | if (status < 100 || status > 599) { |
| 6249 | lua_pushboolean(L, 0); |
| 6250 | return 1; |
| 6251 | } |
| 6252 | if (!reason) |
| 6253 | reason = http_get_reason(status); |
| 6254 | |
| 6255 | lua_pushinteger(L, status); |
| 6256 | lua_setfield(L, 1, "status"); |
| 6257 | |
| 6258 | lua_pushstring(L, reason); |
| 6259 | lua_setfield(L, 1, "reason"); |
| 6260 | |
| 6261 | lua_pushboolean(L, 1); |
| 6262 | return 1; |
| 6263 | } |
| 6264 | |
| 6265 | /* Add a header into the reply object. Each header name is associated to an |
| 6266 | * array of values in the "headers" table. If the header name is not found, a |
| 6267 | * new entry is created. |
| 6268 | */ |
| 6269 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6270 | { |
| 6271 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6272 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6273 | int ret; |
| 6274 | |
| 6275 | /* First argument (self) must be a table */ |
| 6276 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6277 | |
| 6278 | /* Push in the stack the "headers" entry. */ |
| 6279 | ret = lua_getfield(L, 1, "headers"); |
| 6280 | if (ret != LUA_TTABLE) { |
| 6281 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6282 | WILL_LJMP(lua_error(L)); |
| 6283 | } |
| 6284 | |
| 6285 | /* check if the header is already registered. If not, register it. */ |
| 6286 | ret = lua_getfield(L, -1, name); |
| 6287 | if (ret == LUA_TNIL) { |
| 6288 | /* Entry not found. */ |
| 6289 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6290 | |
| 6291 | /* Insert the new header name in the array in the top of the stack. |
| 6292 | * It left the new array in the top of the stack. |
| 6293 | */ |
| 6294 | lua_newtable(L); |
| 6295 | lua_pushstring(L, name); |
| 6296 | lua_pushvalue(L, -2); |
| 6297 | lua_settable(L, -4); |
| 6298 | } |
| 6299 | else if (ret != LUA_TTABLE) { |
| 6300 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6301 | WILL_LJMP(lua_error(L)); |
| 6302 | } |
| 6303 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6304 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6305 | * the header value as new entry. |
| 6306 | */ |
| 6307 | lua_pushstring(L, value); |
| 6308 | ret = lua_rawlen(L, -2); |
| 6309 | lua_rawseti(L, -2, ret + 1); |
| 6310 | |
| 6311 | lua_pushboolean(L, 1); |
| 6312 | return 1; |
| 6313 | } |
| 6314 | |
| 6315 | /* Remove all occurrences of a given header name. */ |
| 6316 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6317 | { |
| 6318 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6319 | int ret; |
| 6320 | |
| 6321 | /* First argument (self) must be a table */ |
| 6322 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6323 | |
| 6324 | /* Push in the stack the "headers" entry. */ |
| 6325 | ret = lua_getfield(L, 1, "headers"); |
| 6326 | if (ret != LUA_TTABLE) { |
| 6327 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6328 | WILL_LJMP(lua_error(L)); |
| 6329 | } |
| 6330 | |
| 6331 | lua_pushstring(L, name); |
| 6332 | lua_pushnil(L); |
| 6333 | lua_settable(L, -3); |
| 6334 | |
| 6335 | lua_pushboolean(L, 1); |
| 6336 | return 1; |
| 6337 | } |
| 6338 | |
| 6339 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6340 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6341 | { |
| 6342 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6343 | |
| 6344 | /* First argument (self) must be a table */ |
| 6345 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6346 | |
| 6347 | lua_pushstring(L, payload); |
| 6348 | lua_setfield(L, 1, "body"); |
| 6349 | |
| 6350 | lua_pushboolean(L, 1); |
| 6351 | return 1; |
| 6352 | } |
| 6353 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6354 | __LJMP static int hlua_log(lua_State *L) |
| 6355 | { |
| 6356 | int level; |
| 6357 | const char *msg; |
| 6358 | |
| 6359 | MAY_LJMP(check_args(L, 2, "log")); |
| 6360 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6361 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6362 | |
| 6363 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6364 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6365 | |
| 6366 | hlua_sendlog(NULL, level, msg); |
| 6367 | return 0; |
| 6368 | } |
| 6369 | |
| 6370 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6371 | { |
| 6372 | const char *msg; |
| 6373 | |
| 6374 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6375 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6376 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6377 | return 0; |
| 6378 | } |
| 6379 | |
| 6380 | __LJMP static int hlua_log_info(lua_State *L) |
| 6381 | { |
| 6382 | const char *msg; |
| 6383 | |
| 6384 | MAY_LJMP(check_args(L, 1, "info")); |
| 6385 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6386 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6387 | return 0; |
| 6388 | } |
| 6389 | |
| 6390 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6391 | { |
| 6392 | const char *msg; |
| 6393 | |
| 6394 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6395 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6396 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6397 | return 0; |
| 6398 | } |
| 6399 | |
| 6400 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6401 | { |
| 6402 | const char *msg; |
| 6403 | |
| 6404 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6405 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6406 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6407 | return 0; |
| 6408 | } |
| 6409 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6410 | __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] | 6411 | { |
| 6412 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6413 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6414 | 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] | 6415 | return 0; |
| 6416 | } |
| 6417 | |
| 6418 | __LJMP static int hlua_sleep(lua_State *L) |
| 6419 | { |
| 6420 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6421 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6422 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6423 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6424 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6425 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6426 | wakeup_ms = tick_add(now_ms, delay); |
| 6427 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6428 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6429 | 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] | 6430 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6431 | } |
| 6432 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6433 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6434 | { |
| 6435 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6436 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6437 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6438 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6439 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6440 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6441 | wakeup_ms = tick_add(now_ms, delay); |
| 6442 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6443 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6444 | 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] | 6445 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6446 | } |
| 6447 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6448 | /* This functionis an LUA binding. it permits to give back |
| 6449 | * the hand at the HAProxy scheduler. It is used when the |
| 6450 | * LUA processing consumes a lot of time. |
| 6451 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6452 | __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] | 6453 | { |
| 6454 | return 0; |
| 6455 | } |
| 6456 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6457 | __LJMP static int hlua_yield(lua_State *L) |
| 6458 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6459 | 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] | 6460 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6461 | } |
| 6462 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6463 | /* This function change the nice of the currently executed |
| 6464 | * task. It is used set low or high priority at the current |
| 6465 | * task. |
| 6466 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6467 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6468 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6469 | struct hlua *hlua; |
| 6470 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6471 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6472 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6473 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6474 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6475 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6476 | hlua = hlua_gethlua(L); |
| 6477 | |
| 6478 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6479 | if (!hlua || !hlua->task) |
| 6480 | return 0; |
| 6481 | |
| 6482 | if (nice < -1024) |
| 6483 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6484 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6485 | nice = 1024; |
| 6486 | |
| 6487 | hlua->task->nice = nice; |
| 6488 | return 0; |
| 6489 | } |
| 6490 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6491 | /* 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] | 6492 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6493 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6494 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6495 | * |
| 6496 | * Task wrapper are longjmp safe because the only one Lua code |
| 6497 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6498 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6499 | 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] | 6500 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6501 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6502 | enum hlua_exec status; |
| 6503 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6504 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6505 | task_set_affinity(task, tid_bit); |
| 6506 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6507 | /* If it is the first call to the task, we must initialize the |
| 6508 | * execution timeouts. |
| 6509 | */ |
| 6510 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6511 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6512 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6513 | /* Execute the Lua code. */ |
| 6514 | status = hlua_ctx_resume(hlua, 1); |
| 6515 | |
| 6516 | switch (status) { |
| 6517 | /* finished or yield */ |
| 6518 | case HLUA_E_OK: |
| 6519 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6520 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6521 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6522 | break; |
| 6523 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6524 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6525 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6526 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6527 | break; |
| 6528 | |
| 6529 | /* finished with error. */ |
| 6530 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6531 | hlua_lock(hlua); |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 6532 | 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] | 6533 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6534 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6535 | task = NULL; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6536 | hlua_unlock(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6537 | break; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6538 | case HLUA_E_ERR: |
| 6539 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6540 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6541 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6542 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6543 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6544 | break; |
| 6545 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6546 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6547 | } |
| 6548 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6549 | /* This function is an LUA binding that register LUA function to be |
| 6550 | * executed after the HAProxy configuration parsing and before the |
| 6551 | * HAProxy scheduler starts. This function expect only one LUA |
| 6552 | * argument that is a function. This function returns nothing, but |
| 6553 | * throws if an error is encountered. |
| 6554 | */ |
| 6555 | __LJMP static int hlua_register_init(lua_State *L) |
| 6556 | { |
| 6557 | struct hlua_init_function *init; |
| 6558 | int ref; |
| 6559 | |
| 6560 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6561 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6562 | if (hlua_gethlua(L)) { |
| 6563 | /* runtime processing */ |
| 6564 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6565 | } |
| 6566 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6567 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6568 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6569 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6570 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6571 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6572 | |
| 6573 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6574 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6575 | return 0; |
| 6576 | } |
| 6577 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6578 | /* This functio is an LUA binding. It permits to register a task |
| 6579 | * executed in parallel of the main HAroxy activity. The task is |
| 6580 | * created and it is set in the HAProxy scheduler. It can be called |
| 6581 | * from the "init" section, "post init" or during the runtime. |
| 6582 | * |
| 6583 | * Lua prototype: |
| 6584 | * |
| 6585 | * <none> core.register_task(<function>) |
| 6586 | */ |
| 6587 | static int hlua_register_task(lua_State *L) |
| 6588 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6589 | struct hlua *hlua = NULL; |
| 6590 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6591 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6592 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6593 | |
| 6594 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6595 | |
| 6596 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6597 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6598 | /* Get the reference state. If the reference is NULL, L is the master |
| 6599 | * state, otherwise hlua->T is. |
| 6600 | */ |
| 6601 | hlua = hlua_gethlua(L); |
| 6602 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6603 | /* we are in runtime processing */ |
| 6604 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6605 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6606 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6607 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6608 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6609 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6610 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6611 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6612 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6613 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6614 | /* We are in the common lua state, execute the task anywhere, |
| 6615 | * otherwise, inherit the current thread identifier |
| 6616 | */ |
| 6617 | if (state_id == 0) |
| 6618 | task = task_new(MAX_THREADS_MASK); |
| 6619 | else |
| 6620 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6621 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6622 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6623 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6624 | task->context = hlua; |
| 6625 | task->process = hlua_process_task; |
| 6626 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 6627 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6628 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6629 | |
| 6630 | /* Restore the function in the stack. */ |
| 6631 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6632 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6633 | hlua_unref(L, ref); |
| 6634 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6635 | hlua->nargs = 0; |
| 6636 | |
| 6637 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6638 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6639 | |
| 6640 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6641 | |
| 6642 | alloc_error: |
| 6643 | task_destroy(task); |
| 6644 | hlua_ctx_destroy(hlua); |
| 6645 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6646 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6647 | } |
| 6648 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6649 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6650 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6651 | * resume converters. |
| 6652 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6653 | 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] | 6654 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6655 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6656 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6657 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6658 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6659 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6660 | if (!stream) |
| 6661 | return 0; |
| 6662 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6663 | 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] | 6664 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6665 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6666 | } |
| 6667 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6668 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6669 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6670 | |
| 6671 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6672 | if (!SET_SAFE_LJMP(hlua)) { |
| 6673 | hlua_lock(hlua); |
| 6674 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6675 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6676 | else |
| 6677 | error = "critical error"; |
| 6678 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6679 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6680 | return 0; |
| 6681 | } |
| 6682 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6683 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6684 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6685 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6686 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6687 | return 0; |
| 6688 | } |
| 6689 | |
| 6690 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6691 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6692 | |
| 6693 | /* convert input sample and pust-it in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6694 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6695 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6696 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6697 | return 0; |
| 6698 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6699 | hlua_smp2lua(hlua->T, smp); |
| 6700 | hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6701 | |
| 6702 | /* push keywords in the stack. */ |
| 6703 | if (arg_p) { |
| 6704 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6705 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6706 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6707 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6708 | return 0; |
| 6709 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6710 | hlua_arg2lua(hlua->T, arg_p); |
| 6711 | hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6712 | } |
| 6713 | } |
| 6714 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6715 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6716 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6717 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6718 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6719 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6723 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6724 | /* finished. */ |
| 6725 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6726 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6727 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6728 | if (lua_gettop(hlua->T) <= 0) { |
| 6729 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6730 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6731 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6732 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6733 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6734 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6735 | /* dup the smp before popping the related lua value and |
| 6736 | * returning it to haproxy |
| 6737 | */ |
| 6738 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6739 | lua_pop(hlua->T, 1); |
| 6740 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6741 | return 1; |
| 6742 | |
| 6743 | /* yield. */ |
| 6744 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6745 | 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] | 6746 | return 0; |
| 6747 | |
| 6748 | /* finished with error. */ |
| 6749 | case HLUA_E_ERRMSG: |
| 6750 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6751 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6752 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6753 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6754 | lua_pop(hlua->T, 1); |
| 6755 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6756 | return 0; |
| 6757 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6758 | case HLUA_E_ETMOUT: |
| 6759 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6760 | return 0; |
| 6761 | |
| 6762 | case HLUA_E_NOMEM: |
| 6763 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6764 | return 0; |
| 6765 | |
| 6766 | case HLUA_E_YIELD: |
| 6767 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6768 | return 0; |
| 6769 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6770 | case HLUA_E_ERR: |
| 6771 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6772 | 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] | 6773 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6774 | |
| 6775 | default: |
| 6776 | return 0; |
| 6777 | } |
| 6778 | } |
| 6779 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6780 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6781 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6782 | * resume sample-fetches. This function will be called by the sample |
| 6783 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6784 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6785 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6786 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6787 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6788 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6789 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6790 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6791 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6792 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6793 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6794 | if (!stream) |
| 6795 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6796 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6797 | 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] | 6798 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6799 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6800 | } |
| 6801 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6802 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6803 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6804 | |
| 6805 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6806 | if (!SET_SAFE_LJMP(hlua)) { |
| 6807 | hlua_lock(hlua); |
| 6808 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6809 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6810 | else |
| 6811 | error = "critical error"; |
| 6812 | 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^] | 6813 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6814 | return 0; |
| 6815 | } |
| 6816 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6817 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6818 | if (!lua_checkstack(hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6819 | 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^] | 6820 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6821 | return 0; |
| 6822 | } |
| 6823 | |
| 6824 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6825 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6826 | |
| 6827 | /* push arguments in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6828 | 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] | 6829 | 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^] | 6830 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6831 | return 0; |
| 6832 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6833 | hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6834 | |
| 6835 | /* push keywords in the stack. */ |
| 6836 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6837 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6838 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6839 | 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^] | 6840 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6841 | return 0; |
| 6842 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6843 | hlua_arg2lua(hlua->T, arg_p); |
| 6844 | hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6845 | } |
| 6846 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6847 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6848 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6849 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6850 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6851 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6855 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6856 | /* finished. */ |
| 6857 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6858 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6859 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6860 | if (lua_gettop(hlua->T) <= 0) { |
| 6861 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6862 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6863 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6864 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6865 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6866 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6867 | /* dup the smp before popping the related lua value and |
| 6868 | * returning it to haproxy |
| 6869 | */ |
| 6870 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6871 | lua_pop(hlua->T, 1); |
| 6872 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6873 | |
| 6874 | /* Set the end of execution flag. */ |
| 6875 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6876 | return 1; |
| 6877 | |
| 6878 | /* yield. */ |
| 6879 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6880 | 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] | 6881 | return 0; |
| 6882 | |
| 6883 | /* finished with error. */ |
| 6884 | case HLUA_E_ERRMSG: |
| 6885 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6886 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6887 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 6888 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6889 | lua_pop(hlua->T, 1); |
| 6890 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6891 | return 0; |
| 6892 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6893 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6894 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6895 | return 0; |
| 6896 | |
| 6897 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6898 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6899 | return 0; |
| 6900 | |
| 6901 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6902 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6903 | return 0; |
| 6904 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6905 | case HLUA_E_ERR: |
| 6906 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6907 | 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] | 6908 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6909 | |
| 6910 | default: |
| 6911 | return 0; |
| 6912 | } |
| 6913 | } |
| 6914 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6915 | /* This function is an LUA binding used for registering |
| 6916 | * "sample-conv" functions. It expects a converter name used |
| 6917 | * in the haproxy configuration file, and an LUA function. |
| 6918 | */ |
| 6919 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6920 | { |
| 6921 | struct sample_conv_kw_list *sck; |
| 6922 | const char *name; |
| 6923 | int ref; |
| 6924 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6925 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6926 | struct sample_conv *sc; |
| 6927 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6928 | |
| 6929 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6930 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6931 | if (hlua_gethlua(L)) { |
| 6932 | /* runtime processing */ |
| 6933 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6934 | } |
| 6935 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6936 | /* First argument : converter name. */ |
| 6937 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6938 | |
| 6939 | /* Second argument : lua function. */ |
| 6940 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6941 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6942 | /* Check if the converter is already registered */ |
| 6943 | trash = get_trash_chunk(); |
| 6944 | chunk_printf(trash, "lua.%s", name); |
| 6945 | sc = find_sample_conv(trash->area, trash->data); |
| 6946 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6947 | fcn = sc->private; |
| 6948 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6949 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6950 | "This will become a hard error in version 2.5.\n", name); |
| 6951 | } |
| 6952 | fcn->function_ref[hlua_state_id] = ref; |
| 6953 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6954 | } |
| 6955 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6956 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6957 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6958 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6959 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6960 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6961 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6962 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6963 | |
| 6964 | /* Fill fcn. */ |
| 6965 | fcn->name = strdup(name); |
| 6966 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6967 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6968 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6969 | |
| 6970 | /* List head */ |
| 6971 | sck->list.n = sck->list.p = NULL; |
| 6972 | |
| 6973 | /* converter keyword. */ |
| 6974 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6975 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6976 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6977 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6978 | |
| 6979 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6980 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6981 | 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] | 6982 | sck->kw[0].val_args = NULL; |
| 6983 | sck->kw[0].in_type = SMP_T_STR; |
| 6984 | sck->kw[0].out_type = SMP_T_STR; |
| 6985 | sck->kw[0].private = fcn; |
| 6986 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6987 | /* Register this new converter */ |
| 6988 | sample_register_convs(sck); |
| 6989 | |
| 6990 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6991 | |
| 6992 | alloc_error: |
| 6993 | release_hlua_function(fcn); |
| 6994 | ha_free(&sck); |
| 6995 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6996 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6997 | } |
| 6998 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6999 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7000 | * "sample-fetch" functions. It expects a converter name used |
| 7001 | * in the haproxy configuration file, and an LUA function. |
| 7002 | */ |
| 7003 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 7004 | { |
| 7005 | const char *name; |
| 7006 | int ref; |
| 7007 | int len; |
| 7008 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7009 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7010 | struct sample_fetch *sf; |
| 7011 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7012 | |
| 7013 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 7014 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7015 | if (hlua_gethlua(L)) { |
| 7016 | /* runtime processing */ |
| 7017 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 7018 | } |
| 7019 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7020 | /* First argument : sample-fetch name. */ |
| 7021 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7022 | |
| 7023 | /* Second argument : lua function. */ |
| 7024 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 7025 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7026 | /* Check if the sample-fetch is already registered */ |
| 7027 | trash = get_trash_chunk(); |
| 7028 | chunk_printf(trash, "lua.%s", name); |
| 7029 | sf = find_sample_fetch(trash->area, trash->data); |
| 7030 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7031 | fcn = sf->private; |
| 7032 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7033 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 7034 | "This will become a hard error in version 2.5.\n", name); |
| 7035 | } |
| 7036 | fcn->function_ref[hlua_state_id] = ref; |
| 7037 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7038 | } |
| 7039 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7040 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7041 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7042 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7043 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7044 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7045 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7046 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7047 | |
| 7048 | /* Fill fcn. */ |
| 7049 | fcn->name = strdup(name); |
| 7050 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7051 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7052 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7053 | |
| 7054 | /* List head */ |
| 7055 | sfk->list.n = sfk->list.p = NULL; |
| 7056 | |
| 7057 | /* sample-fetch keyword. */ |
| 7058 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7059 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7060 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7061 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7062 | |
| 7063 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 7064 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7065 | 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] | 7066 | sfk->kw[0].val_args = NULL; |
| 7067 | sfk->kw[0].out_type = SMP_T_STR; |
| 7068 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 7069 | sfk->kw[0].val = 0; |
| 7070 | sfk->kw[0].private = fcn; |
| 7071 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7072 | /* Register this new fetch. */ |
| 7073 | sample_register_fetches(sfk); |
| 7074 | |
| 7075 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7076 | |
| 7077 | alloc_error: |
| 7078 | release_hlua_function(fcn); |
| 7079 | ha_free(&sfk); |
| 7080 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7081 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7082 | } |
| 7083 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7084 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7085 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7086 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7087 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7088 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7089 | unsigned int delay; |
| 7090 | unsigned int wakeup_ms; |
| 7091 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7092 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7093 | hlua = hlua_gethlua(L); |
| 7094 | if (!hlua) { |
| 7095 | return 0; |
| 7096 | } |
| 7097 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7098 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7099 | |
| 7100 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7101 | wakeup_ms = tick_add(now_ms, delay); |
| 7102 | hlua->wake_time = wakeup_ms; |
| 7103 | return 0; |
| 7104 | } |
| 7105 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7106 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7107 | * wrapper during the initialisation period. This function may return any |
| 7108 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7109 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7110 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7111 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7112 | 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] | 7113 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7114 | { |
| 7115 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7116 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7117 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7118 | const char *error; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7119 | struct hlua *hlua = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7120 | |
| 7121 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7122 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7123 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7124 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7125 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7126 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7127 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7128 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7129 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7130 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7131 | 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] | 7132 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 7133 | rule->arg.hlua_rule->fcn->name); |
| 7134 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7135 | } |
| 7136 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7137 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7138 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7139 | |
| 7140 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7141 | if (!SET_SAFE_LJMP(hlua)) { |
| 7142 | hlua_lock(hlua); |
| 7143 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7144 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7145 | else |
| 7146 | error = "critical error"; |
| 7147 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7148 | rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7149 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7150 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7151 | } |
| 7152 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7153 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7154 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7155 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7156 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7157 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7158 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7162 | 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] | 7163 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7164 | /* Create and and push object stream in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7165 | if (!hlua_txn_new(hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7166 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7167 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7168 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7169 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7170 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7171 | hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7172 | |
| 7173 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7174 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7175 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7176 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7177 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7178 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7179 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7180 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7181 | lua_pushstring(hlua->T, *arg); |
| 7182 | hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7183 | } |
| 7184 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7185 | /* Now the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7186 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7187 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7188 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7189 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7193 | switch (hlua_ctx_resume(hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7194 | /* finished. */ |
| 7195 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7196 | /* Catch the return value */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7197 | hlua_lock(hlua); |
| 7198 | if (lua_gettop(hlua->T) > 0) |
| 7199 | act_ret = lua_tointeger(hlua->T, -1); |
| 7200 | hlua_unlock(hlua); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7201 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7202 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7203 | if (act_ret == ACT_RET_YIELD) { |
| 7204 | if (flags & ACT_OPT_FINAL) |
| 7205 | goto err_yield; |
| 7206 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7207 | if (dir == SMP_OPT_DIR_REQ) |
| 7208 | 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^] | 7209 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7210 | else |
| 7211 | 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^] | 7212 | hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7213 | } |
| 7214 | goto end; |
| 7215 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7216 | /* yield. */ |
| 7217 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7218 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7219 | if (dir == SMP_OPT_DIR_REQ) |
| 7220 | 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^] | 7221 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7222 | else |
| 7223 | 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^] | 7224 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7225 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7226 | /* Some actions can be wake up when a "write" event |
| 7227 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7228 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7229 | */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7230 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7231 | s->res.flags |= CF_WAKE_WRITE; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7232 | if (HLUA_IS_WAKEREQWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7233 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7234 | act_ret = ACT_RET_YIELD; |
| 7235 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7236 | |
| 7237 | /* finished with error. */ |
| 7238 | case HLUA_E_ERRMSG: |
| 7239 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7240 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7241 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7242 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 7243 | lua_pop(hlua->T, 1); |
| 7244 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7245 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7246 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7247 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7248 | 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] | 7249 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7250 | |
| 7251 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7252 | 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] | 7253 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7254 | |
| 7255 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7256 | err_yield: |
| 7257 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | 602c4af | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 7258 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7259 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7260 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7261 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7262 | case HLUA_E_ERR: |
| 7263 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7264 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7265 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7266 | |
| 7267 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7268 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7269 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7270 | |
| 7271 | end: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame^] | 7272 | if (act_ret != ACT_RET_YIELD && hlua) |
| 7273 | hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7274 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7275 | } |
| 7276 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7277 | 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] | 7278 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7279 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7280 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7281 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7282 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7283 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7284 | } |
| 7285 | |
| 7286 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7287 | { |
| 7288 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7289 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7290 | struct task *task; |
| 7291 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7292 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7293 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7294 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7295 | if (!hlua) { |
| 7296 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7297 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7298 | return 0; |
| 7299 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7300 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7301 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7302 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7303 | |
| 7304 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7305 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7306 | if (!task) { |
| 7307 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7308 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7309 | return 0; |
| 7310 | } |
| 7311 | task->nice = 0; |
| 7312 | task->context = ctx; |
| 7313 | task->process = hlua_applet_wakeup; |
| 7314 | ctx->ctx.hlua_apptcp.task = task; |
| 7315 | |
| 7316 | /* In the execution wrappers linked with a stream, the |
| 7317 | * Lua context can be not initialized. This behavior |
| 7318 | * permits to save performances because a systematic |
| 7319 | * Lua initialization cause 5% performances loss. |
| 7320 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7321 | 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] | 7322 | 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] | 7323 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7324 | return 0; |
| 7325 | } |
| 7326 | |
| 7327 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7328 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7329 | |
| 7330 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7331 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7332 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7333 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7334 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7335 | else |
| 7336 | error = "critical error"; |
| 7337 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7338 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7339 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7340 | return 0; |
| 7341 | } |
| 7342 | |
| 7343 | /* Check stack available size. */ |
| 7344 | if (!lua_checkstack(hlua->T, 1)) { |
| 7345 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7346 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7347 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7348 | return 0; |
| 7349 | } |
| 7350 | |
| 7351 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7352 | 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] | 7353 | |
| 7354 | /* Create and and push object stream in the stack. */ |
| 7355 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7356 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7357 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7358 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7359 | return 0; |
| 7360 | } |
| 7361 | hlua->nargs = 1; |
| 7362 | |
| 7363 | /* push keywords in the stack. */ |
| 7364 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7365 | if (!lua_checkstack(hlua->T, 1)) { |
| 7366 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7367 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7368 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7369 | return 0; |
| 7370 | } |
| 7371 | lua_pushstring(hlua->T, *arg); |
| 7372 | hlua->nargs++; |
| 7373 | } |
| 7374 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7375 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7376 | |
| 7377 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7378 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7379 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7380 | |
| 7381 | return 1; |
| 7382 | } |
| 7383 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7384 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7385 | { |
| 7386 | struct stream_interface *si = ctx->owner; |
| 7387 | struct stream *strm = si_strm(si); |
| 7388 | struct channel *res = si_ic(si); |
| 7389 | struct act_rule *rule = ctx->rule; |
| 7390 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7391 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7392 | |
| 7393 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7394 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7395 | /* eat the whole request */ |
| 7396 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7397 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7398 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7399 | |
| 7400 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7401 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7402 | return; |
| 7403 | |
| 7404 | /* Execute the function. */ |
| 7405 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7406 | /* finished. */ |
| 7407 | case HLUA_E_OK: |
| 7408 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7409 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7410 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7411 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7412 | res->flags |= CF_READ_NULL; |
| 7413 | si_shutr(si); |
| 7414 | return; |
| 7415 | |
| 7416 | /* yield. */ |
| 7417 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7418 | if (hlua->wake_time != TICK_ETERNITY) |
| 7419 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7420 | return; |
| 7421 | |
| 7422 | /* finished with error. */ |
| 7423 | case HLUA_E_ERRMSG: |
| 7424 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7425 | hlua_lock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7426 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7427 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7428 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7429 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7430 | goto error; |
| 7431 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7432 | case HLUA_E_ETMOUT: |
| 7433 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7434 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7435 | goto error; |
| 7436 | |
| 7437 | case HLUA_E_NOMEM: |
| 7438 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7439 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7440 | goto error; |
| 7441 | |
| 7442 | case HLUA_E_YIELD: /* unexpected */ |
| 7443 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7444 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7445 | goto error; |
| 7446 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7447 | case HLUA_E_ERR: |
| 7448 | /* Display log. */ |
| 7449 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7450 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7451 | goto error; |
| 7452 | |
| 7453 | default: |
| 7454 | goto error; |
| 7455 | } |
| 7456 | |
| 7457 | error: |
| 7458 | |
| 7459 | /* For all other cases, just close the stream. */ |
| 7460 | si_shutw(si); |
| 7461 | si_shutr(si); |
| 7462 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7463 | } |
| 7464 | |
| 7465 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7466 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7467 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7468 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7469 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7470 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7471 | } |
| 7472 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7473 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7474 | * an errors occurs and -1 if more data are required for initializing |
| 7475 | * the applet. |
| 7476 | */ |
| 7477 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7478 | { |
| 7479 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7480 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7481 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7482 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7483 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7484 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7485 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7486 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7487 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7488 | if (!hlua) { |
| 7489 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7490 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7491 | return 0; |
| 7492 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7493 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7494 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7496 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7497 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7498 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7499 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7500 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7501 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7502 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7503 | if (!task) { |
| 7504 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7505 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7506 | return 0; |
| 7507 | } |
| 7508 | task->nice = 0; |
| 7509 | task->context = ctx; |
| 7510 | task->process = hlua_applet_wakeup; |
| 7511 | ctx->ctx.hlua_apphttp.task = task; |
| 7512 | |
| 7513 | /* In the execution wrappers linked with a stream, the |
| 7514 | * Lua context can be not initialized. This behavior |
| 7515 | * permits to save performances because a systematic |
| 7516 | * Lua initialization cause 5% performances loss. |
| 7517 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7518 | 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] | 7519 | 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] | 7520 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7521 | return 0; |
| 7522 | } |
| 7523 | |
| 7524 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7525 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7526 | |
| 7527 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7528 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7529 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7530 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7531 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7532 | else |
| 7533 | error = "critical error"; |
| 7534 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7535 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7536 | hlua_unlock(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7537 | return 0; |
| 7538 | } |
| 7539 | |
| 7540 | /* Check stack available size. */ |
| 7541 | if (!lua_checkstack(hlua->T, 1)) { |
| 7542 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7543 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7544 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7545 | return 0; |
| 7546 | } |
| 7547 | |
| 7548 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7549 | 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] | 7550 | |
| 7551 | /* Create and and push object stream in the stack. */ |
| 7552 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7553 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7554 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7555 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7556 | return 0; |
| 7557 | } |
| 7558 | hlua->nargs = 1; |
| 7559 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7560 | /* push keywords in the stack. */ |
| 7561 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7562 | if (!lua_checkstack(hlua->T, 1)) { |
| 7563 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7564 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7565 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7566 | return 0; |
| 7567 | } |
| 7568 | lua_pushstring(hlua->T, *arg); |
| 7569 | hlua->nargs++; |
| 7570 | } |
| 7571 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7572 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7573 | |
| 7574 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7575 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7576 | |
| 7577 | return 1; |
| 7578 | } |
| 7579 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7580 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7581 | { |
| 7582 | struct stream_interface *si = ctx->owner; |
| 7583 | struct stream *strm = si_strm(si); |
| 7584 | struct channel *req = si_oc(si); |
| 7585 | struct channel *res = si_ic(si); |
| 7586 | struct act_rule *rule = ctx->rule; |
| 7587 | struct proxy *px = strm->be; |
| 7588 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7589 | struct htx *req_htx, *res_htx; |
| 7590 | |
| 7591 | res_htx = htx_from_buf(&res->buf); |
| 7592 | |
| 7593 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7594 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7595 | goto out; |
| 7596 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7597 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7598 | if (!b_size(&res->buf)) { |
| 7599 | si_rx_room_blk(si); |
| 7600 | goto out; |
| 7601 | } |
| 7602 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7603 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7604 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7605 | |
| 7606 | /* Set the currently running flag. */ |
| 7607 | if (!HLUA_IS_RUNNING(hlua) && |
| 7608 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7609 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7610 | si_cant_get(si); |
| 7611 | goto out; |
| 7612 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7613 | } |
| 7614 | |
| 7615 | /* Executes The applet if it is not done. */ |
| 7616 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7617 | |
| 7618 | /* Execute the function. */ |
| 7619 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7620 | /* finished. */ |
| 7621 | case HLUA_E_OK: |
| 7622 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7623 | break; |
| 7624 | |
| 7625 | /* yield. */ |
| 7626 | case HLUA_E_AGAIN: |
| 7627 | if (hlua->wake_time != TICK_ETERNITY) |
| 7628 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7629 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7630 | |
| 7631 | /* finished with error. */ |
| 7632 | case HLUA_E_ERRMSG: |
| 7633 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7634 | hlua_lock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7635 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7636 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7637 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7638 | hlua_unlock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7639 | goto error; |
| 7640 | |
| 7641 | case HLUA_E_ETMOUT: |
| 7642 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7643 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7644 | goto error; |
| 7645 | |
| 7646 | case HLUA_E_NOMEM: |
| 7647 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7648 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7649 | goto error; |
| 7650 | |
| 7651 | case HLUA_E_YIELD: /* unexpected */ |
| 7652 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7653 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7654 | goto error; |
| 7655 | |
| 7656 | case HLUA_E_ERR: |
| 7657 | /* Display log. */ |
| 7658 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7659 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7660 | goto error; |
| 7661 | |
| 7662 | default: |
| 7663 | goto error; |
| 7664 | } |
| 7665 | } |
| 7666 | |
| 7667 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7668 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7669 | goto done; |
| 7670 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7671 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7672 | goto error; |
| 7673 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7674 | /* no more data are expected. If the response buffer is empty |
| 7675 | * for a chunked message, be sure to add something (EOT block in |
| 7676 | * this case) to have something to send. It is important to be |
| 7677 | * sure the EOM flags will be handled by the endpoint. |
| 7678 | */ |
| 7679 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7680 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7681 | si_rx_room_blk(si); |
| 7682 | goto out; |
| 7683 | } |
| 7684 | channel_add_input(res, 1); |
| 7685 | } |
| 7686 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7687 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7688 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7689 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7690 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7691 | } |
| 7692 | |
| 7693 | done: |
| 7694 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7695 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7696 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7697 | si_shutr(si); |
| 7698 | } |
| 7699 | |
| 7700 | /* eat the whole request */ |
| 7701 | if (co_data(req)) { |
| 7702 | req_htx = htx_from_buf(&req->buf); |
| 7703 | co_htx_skip(req, req_htx, co_data(req)); |
| 7704 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7705 | } |
| 7706 | } |
| 7707 | |
| 7708 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7709 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7710 | return; |
| 7711 | |
| 7712 | error: |
| 7713 | |
| 7714 | /* If we are in HTTP mode, and we are not send any |
| 7715 | * data, return a 500 server error in best effort: |
| 7716 | * if there is no room available in the buffer, |
| 7717 | * just close the connection. |
| 7718 | */ |
| 7719 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7720 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7721 | |
| 7722 | channel_erase(res); |
| 7723 | res->buf.data = b_data(err); |
| 7724 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7725 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7726 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7727 | } |
| 7728 | if (!(strm->flags & SF_ERR_MASK)) |
| 7729 | strm->flags |= SF_ERR_RESOURCE; |
| 7730 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7731 | goto done; |
| 7732 | } |
| 7733 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7734 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7735 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7736 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7737 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7738 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7739 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7740 | } |
| 7741 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7742 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7743 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7744 | * |
| 7745 | * This function can fail with an abort() due to an Lua critical error. |
| 7746 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7747 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7748 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7749 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7750 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7751 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7752 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7753 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7754 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7755 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7756 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7757 | if (!rule->arg.hlua_rule) { |
| 7758 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7759 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7760 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7761 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7762 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7763 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7764 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7765 | if (!rule->arg.hlua_rule->args) { |
| 7766 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7767 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7768 | } |
| 7769 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7770 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7771 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7772 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7773 | /* Expect some arguments */ |
| 7774 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7775 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7776 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7777 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7778 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7779 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7780 | if (!rule->arg.hlua_rule->args[i]) { |
| 7781 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7782 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7783 | } |
| 7784 | (*cur_arg)++; |
| 7785 | } |
| 7786 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7787 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7788 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7789 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7790 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7791 | |
| 7792 | error: |
| 7793 | if (rule->arg.hlua_rule) { |
| 7794 | if (rule->arg.hlua_rule->args) { |
| 7795 | for (i = 0; i < fcn->nargs; i++) |
| 7796 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7797 | ha_free(&rule->arg.hlua_rule->args); |
| 7798 | } |
| 7799 | ha_free(&rule->arg.hlua_rule); |
| 7800 | } |
| 7801 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7802 | } |
| 7803 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7804 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7805 | struct act_rule *rule, char **err) |
| 7806 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7807 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7808 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7809 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7810 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7811 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7812 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7813 | * the call of this analyzer. |
| 7814 | */ |
| 7815 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7816 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7817 | return ACT_RET_PRS_ERR; |
| 7818 | } |
| 7819 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7820 | /* Memory for the rule. */ |
| 7821 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7822 | if (!rule->arg.hlua_rule) { |
| 7823 | memprintf(err, "out of memory error"); |
| 7824 | return ACT_RET_PRS_ERR; |
| 7825 | } |
| 7826 | |
| 7827 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7828 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7829 | |
| 7830 | /* TODO: later accept arguments. */ |
| 7831 | rule->arg.hlua_rule->args = NULL; |
| 7832 | |
| 7833 | /* Add applet pointer in the rule. */ |
| 7834 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7835 | rule->applet.name = fcn->name; |
| 7836 | rule->applet.init = hlua_applet_http_init; |
| 7837 | rule->applet.fct = hlua_applet_http_fct; |
| 7838 | rule->applet.release = hlua_applet_http_release; |
| 7839 | rule->applet.timeout = hlua_timeout_applet; |
| 7840 | |
| 7841 | return ACT_RET_PRS_OK; |
| 7842 | } |
| 7843 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7844 | /* This function is an LUA binding used for registering |
| 7845 | * "sample-conv" functions. It expects a converter name used |
| 7846 | * in the haproxy configuration file, and an LUA function. |
| 7847 | */ |
| 7848 | __LJMP static int hlua_register_action(lua_State *L) |
| 7849 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7850 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7851 | const char *name; |
| 7852 | int ref; |
| 7853 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7854 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7855 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7856 | struct buffer *trash; |
| 7857 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7858 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7859 | /* Initialise the number of expected arguments at 0. */ |
| 7860 | nargs = 0; |
| 7861 | |
| 7862 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7863 | 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] | 7864 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7865 | if (hlua_gethlua(L)) { |
| 7866 | /* runtime processing */ |
| 7867 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7868 | } |
| 7869 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7870 | /* First argument : converter name. */ |
| 7871 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7872 | |
| 7873 | /* Second argument : environment. */ |
| 7874 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7875 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7876 | |
| 7877 | /* Third argument : lua function. */ |
| 7878 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7879 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7880 | /* 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] | 7881 | if (lua_gettop(L) >= 4) |
| 7882 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7883 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7884 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7885 | lua_pushnil(L); |
| 7886 | while (lua_next(L, 2) != 0) { |
| 7887 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7888 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7889 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7890 | /* Check if action exists */ |
| 7891 | trash = get_trash_chunk(); |
| 7892 | chunk_printf(trash, "lua.%s", name); |
| 7893 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7894 | akw = tcp_req_cont_action(trash->area); |
| 7895 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7896 | akw = tcp_res_cont_action(trash->area); |
| 7897 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7898 | akw = action_http_req_custom(trash->area); |
| 7899 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7900 | akw = action_http_res_custom(trash->area); |
| 7901 | } else { |
| 7902 | akw = NULL; |
| 7903 | } |
| 7904 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7905 | fcn = akw->private; |
| 7906 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7907 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7908 | "This will become a hard error in version 2.5.\n", name); |
| 7909 | } |
| 7910 | fcn->function_ref[hlua_state_id] = ref; |
| 7911 | |
| 7912 | /* pop the environment string. */ |
| 7913 | lua_pop(L, 1); |
| 7914 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7915 | } |
| 7916 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7917 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7918 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7919 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7920 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7921 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7922 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7923 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7924 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7925 | |
| 7926 | /* Fill fcn. */ |
| 7927 | fcn->name = strdup(name); |
| 7928 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7929 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7930 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7931 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7932 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7933 | fcn->nargs = nargs; |
| 7934 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7935 | /* List head */ |
| 7936 | akl->list.n = akl->list.p = NULL; |
| 7937 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7938 | /* action keyword. */ |
| 7939 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7940 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7941 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7942 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7943 | |
| 7944 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7945 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7946 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7947 | akl->kw[0].private = fcn; |
| 7948 | akl->kw[0].parse = action_register_lua; |
| 7949 | |
| 7950 | /* select the action registering point. */ |
| 7951 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7952 | tcp_req_cont_keywords_register(akl); |
| 7953 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7954 | tcp_res_cont_keywords_register(akl); |
| 7955 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7956 | http_req_keywords_register(akl); |
| 7957 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7958 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7959 | else { |
| 7960 | release_hlua_function(fcn); |
| 7961 | if (akl) |
| 7962 | ha_free((char **)&(akl->kw[0].kw)); |
| 7963 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7964 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7965 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7966 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7967 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7968 | |
| 7969 | /* pop the environment string. */ |
| 7970 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7971 | |
| 7972 | /* reset for next loop */ |
| 7973 | akl = NULL; |
| 7974 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7975 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7976 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7977 | |
| 7978 | alloc_error: |
| 7979 | release_hlua_function(fcn); |
| 7980 | ha_free(&akl); |
| 7981 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7982 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7983 | } |
| 7984 | |
| 7985 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7986 | struct act_rule *rule, char **err) |
| 7987 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7988 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7989 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7990 | if (px->mode == PR_MODE_HTTP) { |
| 7991 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7992 | return ACT_RET_PRS_ERR; |
| 7993 | } |
| 7994 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7995 | /* Memory for the rule. */ |
| 7996 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7997 | if (!rule->arg.hlua_rule) { |
| 7998 | memprintf(err, "out of memory error"); |
| 7999 | return ACT_RET_PRS_ERR; |
| 8000 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8001 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8002 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8003 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8004 | |
| 8005 | /* TODO: later accept arguments. */ |
| 8006 | rule->arg.hlua_rule->args = NULL; |
| 8007 | |
| 8008 | /* Add applet pointer in the rule. */ |
| 8009 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 8010 | rule->applet.name = fcn->name; |
| 8011 | rule->applet.init = hlua_applet_tcp_init; |
| 8012 | rule->applet.fct = hlua_applet_tcp_fct; |
| 8013 | rule->applet.release = hlua_applet_tcp_release; |
| 8014 | rule->applet.timeout = hlua_timeout_applet; |
| 8015 | |
| 8016 | return 0; |
| 8017 | } |
| 8018 | |
| 8019 | /* This function is an LUA binding used for registering |
| 8020 | * "sample-conv" functions. It expects a converter name used |
| 8021 | * in the haproxy configuration file, and an LUA function. |
| 8022 | */ |
| 8023 | __LJMP static int hlua_register_service(lua_State *L) |
| 8024 | { |
| 8025 | struct action_kw_list *akl; |
| 8026 | const char *name; |
| 8027 | const char *env; |
| 8028 | int ref; |
| 8029 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8030 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8031 | struct buffer *trash; |
| 8032 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8033 | |
| 8034 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 8035 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8036 | if (hlua_gethlua(L)) { |
| 8037 | /* runtime processing */ |
| 8038 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 8039 | } |
| 8040 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8041 | /* First argument : converter name. */ |
| 8042 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8043 | |
| 8044 | /* Second argument : environment. */ |
| 8045 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8046 | |
| 8047 | /* Third argument : lua function. */ |
| 8048 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8049 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8050 | /* Check for service already registered */ |
| 8051 | trash = get_trash_chunk(); |
| 8052 | chunk_printf(trash, "lua.%s", name); |
| 8053 | akw = service_find(trash->area); |
| 8054 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8055 | fcn = akw->private; |
| 8056 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8057 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 8058 | "This will become a hard error in version 2.5.\n", name); |
| 8059 | } |
| 8060 | fcn->function_ref[hlua_state_id] = ref; |
| 8061 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8062 | } |
| 8063 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8064 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8065 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 8066 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8067 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8068 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8069 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8070 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8071 | |
| 8072 | /* Fill fcn. */ |
| 8073 | len = strlen("<lua.>") + strlen(name) + 1; |
| 8074 | fcn->name = calloc(1, len); |
| 8075 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8076 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8077 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8078 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8079 | |
| 8080 | /* List head */ |
| 8081 | akl->list.n = akl->list.p = NULL; |
| 8082 | |
| 8083 | /* converter keyword. */ |
| 8084 | len = strlen("lua.") + strlen(name) + 1; |
| 8085 | akl->kw[0].kw = calloc(1, len); |
| 8086 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8087 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8088 | |
| 8089 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8090 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 8091 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8092 | if (strcmp(env, "tcp") == 0) |
| 8093 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8094 | else if (strcmp(env, "http") == 0) |
| 8095 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8096 | else { |
| 8097 | release_hlua_function(fcn); |
| 8098 | if (akl) |
| 8099 | ha_free((char **)&(akl->kw[0].kw)); |
| 8100 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8101 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 8102 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8103 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8104 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8105 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8106 | akl->kw[0].private = fcn; |
| 8107 | |
| 8108 | /* End of array. */ |
| 8109 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8110 | |
| 8111 | /* Register this new converter */ |
| 8112 | service_keywords_register(akl); |
| 8113 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8114 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8115 | |
| 8116 | alloc_error: |
| 8117 | release_hlua_function(fcn); |
| 8118 | ha_free(&akl); |
| 8119 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8120 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8121 | } |
| 8122 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8123 | /* This function initialises Lua cli handler. It copies the |
| 8124 | * arguments in the Lua stack and create channel IO objects. |
| 8125 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8126 | 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] | 8127 | { |
| 8128 | struct hlua *hlua; |
| 8129 | struct hlua_function *fcn; |
| 8130 | int i; |
| 8131 | const char *error; |
| 8132 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8133 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8134 | appctx->ctx.hlua_cli.fcn = private; |
| 8135 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8136 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8137 | if (!hlua) { |
| 8138 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8139 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8140 | } |
| 8141 | HLUA_INIT(hlua); |
| 8142 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8143 | |
| 8144 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8145 | * 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] | 8146 | * applet_http. It is absolutely compatible. |
| 8147 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8148 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8149 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8150 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8151 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8152 | } |
| 8153 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8154 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8155 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8156 | |
| 8157 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8158 | 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] | 8159 | 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] | 8160 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8161 | } |
| 8162 | |
| 8163 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8164 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8165 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8166 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8167 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8168 | else |
| 8169 | error = "critical error"; |
| 8170 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8171 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8172 | goto error; |
| 8173 | } |
| 8174 | |
| 8175 | /* Check stack available size. */ |
| 8176 | if (!lua_checkstack(hlua->T, 2)) { |
| 8177 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8178 | goto error; |
| 8179 | } |
| 8180 | |
| 8181 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8182 | 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] | 8183 | |
| 8184 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8185 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8186 | */ |
| 8187 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8188 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8189 | goto error; |
| 8190 | } |
| 8191 | hlua->nargs = 1; |
| 8192 | |
| 8193 | /* push keywords in the stack. */ |
| 8194 | for (i = 0; *args[i]; i++) { |
| 8195 | /* Check stack available size. */ |
| 8196 | if (!lua_checkstack(hlua->T, 1)) { |
| 8197 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8198 | goto error; |
| 8199 | } |
| 8200 | lua_pushstring(hlua->T, args[i]); |
| 8201 | hlua->nargs++; |
| 8202 | } |
| 8203 | |
| 8204 | /* We must initialize the execution timeouts. */ |
| 8205 | hlua->max_time = hlua_timeout_session; |
| 8206 | |
| 8207 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8208 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8209 | |
| 8210 | /* It's ok */ |
| 8211 | return 0; |
| 8212 | |
| 8213 | /* It's not ok. */ |
| 8214 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8215 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8216 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8217 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8218 | return 1; |
| 8219 | } |
| 8220 | |
| 8221 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8222 | { |
| 8223 | struct hlua *hlua; |
| 8224 | struct stream_interface *si; |
| 8225 | struct hlua_function *fcn; |
| 8226 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8227 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8228 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8229 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8230 | |
| 8231 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8232 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8233 | return 1; |
| 8234 | |
| 8235 | /* Execute the function. */ |
| 8236 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8237 | |
| 8238 | /* finished. */ |
| 8239 | case HLUA_E_OK: |
| 8240 | return 1; |
| 8241 | |
| 8242 | /* yield. */ |
| 8243 | case HLUA_E_AGAIN: |
| 8244 | /* We want write. */ |
| 8245 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8246 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8247 | /* Set the timeout. */ |
| 8248 | if (hlua->wake_time != TICK_ETERNITY) |
| 8249 | task_schedule(hlua->task, hlua->wake_time); |
| 8250 | return 0; |
| 8251 | |
| 8252 | /* finished with error. */ |
| 8253 | case HLUA_E_ERRMSG: |
| 8254 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8255 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8256 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8257 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8258 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8259 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8260 | return 1; |
| 8261 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8262 | case HLUA_E_ETMOUT: |
| 8263 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8264 | fcn->name); |
| 8265 | return 1; |
| 8266 | |
| 8267 | case HLUA_E_NOMEM: |
| 8268 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8269 | fcn->name); |
| 8270 | return 1; |
| 8271 | |
| 8272 | case HLUA_E_YIELD: /* unexpected */ |
| 8273 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8274 | fcn->name); |
| 8275 | return 1; |
| 8276 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8277 | case HLUA_E_ERR: |
| 8278 | /* Display log. */ |
| 8279 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8280 | fcn->name); |
| 8281 | return 1; |
| 8282 | |
| 8283 | default: |
| 8284 | return 1; |
| 8285 | } |
| 8286 | |
| 8287 | return 1; |
| 8288 | } |
| 8289 | |
| 8290 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8291 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8292 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8293 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8294 | } |
| 8295 | |
| 8296 | /* This function is an LUA binding used for registering |
| 8297 | * new keywords in the cli. It expects a list of keywords |
| 8298 | * which are the "path". It is limited to 5 keywords. A |
| 8299 | * description of the command, a function to be executed |
| 8300 | * for the parsing and a function for io handlers. |
| 8301 | */ |
| 8302 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8303 | { |
| 8304 | struct cli_kw_list *cli_kws; |
| 8305 | const char *message; |
| 8306 | int ref_io; |
| 8307 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8308 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8309 | int index; |
| 8310 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8311 | struct buffer *trash; |
| 8312 | const char *kw[5]; |
| 8313 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8314 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8315 | |
| 8316 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8317 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8318 | if (hlua_gethlua(L)) { |
| 8319 | /* runtime processing */ |
| 8320 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8321 | } |
| 8322 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8323 | /* First argument : an array of maximum 5 keywords. */ |
| 8324 | if (!lua_istable(L, 1)) |
| 8325 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8326 | |
| 8327 | /* Second argument : string with contextual message. */ |
| 8328 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8329 | |
| 8330 | /* Third and fourth argument : lua function. */ |
| 8331 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8332 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8333 | /* Check for CLI service already registered */ |
| 8334 | trash = get_trash_chunk(); |
| 8335 | index = 0; |
| 8336 | lua_pushnil(L); |
| 8337 | memset(kw, 0, sizeof(kw)); |
| 8338 | while (lua_next(L, 1) != 0) { |
| 8339 | if (index >= CLI_PREFIX_KW_NB) |
| 8340 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8341 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8342 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8343 | kw[index] = lua_tostring(L, -1); |
| 8344 | if (index == 0) |
| 8345 | chunk_printf(trash, "%s", kw[index]); |
| 8346 | else |
| 8347 | chunk_appendf(trash, " %s", kw[index]); |
| 8348 | index++; |
| 8349 | lua_pop(L, 1); |
| 8350 | } |
| 8351 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8352 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8353 | fcn = cli_kw->private; |
| 8354 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8355 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8356 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8357 | } |
| 8358 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8359 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8360 | } |
| 8361 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8362 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8363 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8364 | if (!cli_kws) { |
| 8365 | errmsg = "Lua out of memory error."; |
| 8366 | goto error; |
| 8367 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8368 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8369 | if (!fcn) { |
| 8370 | errmsg = "Lua out of memory error."; |
| 8371 | goto error; |
| 8372 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8373 | |
| 8374 | /* Fill path. */ |
| 8375 | index = 0; |
| 8376 | lua_pushnil(L); |
| 8377 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8378 | if (index >= 5) { |
| 8379 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8380 | goto error; |
| 8381 | } |
| 8382 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8383 | errmsg = "1st argument must be a table filled with strings"; |
| 8384 | goto error; |
| 8385 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8386 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8387 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8388 | errmsg = "Lua out of memory error."; |
| 8389 | goto error; |
| 8390 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8391 | index++; |
| 8392 | lua_pop(L, 1); |
| 8393 | } |
| 8394 | |
| 8395 | /* Copy help message. */ |
| 8396 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8397 | if (!cli_kws->kw[0].usage) { |
| 8398 | errmsg = "Lua out of memory error."; |
| 8399 | goto error; |
| 8400 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8401 | |
| 8402 | /* Fill fcn io handler. */ |
| 8403 | len = strlen("<lua.cli>") + 1; |
| 8404 | for (i = 0; i < index; i++) |
| 8405 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8406 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8407 | if (!fcn->name) { |
| 8408 | errmsg = "Lua out of memory error."; |
| 8409 | goto error; |
| 8410 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8411 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8412 | for (i = 0; i < index; i++) { |
| 8413 | strncat((char *)fcn->name, ".", len); |
| 8414 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8415 | } |
| 8416 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8417 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8418 | |
| 8419 | /* Fill last entries. */ |
| 8420 | cli_kws->kw[0].private = fcn; |
| 8421 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8422 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8423 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8424 | |
| 8425 | /* Register this new converter */ |
| 8426 | cli_register_kw(cli_kws); |
| 8427 | |
| 8428 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8429 | |
| 8430 | error: |
| 8431 | release_hlua_function(fcn); |
| 8432 | if (cli_kws) { |
| 8433 | for (i = 0; i < index; i++) |
| 8434 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8435 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8436 | } |
| 8437 | ha_free(&cli_kws); |
| 8438 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8439 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8440 | } |
| 8441 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8442 | 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] | 8443 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8444 | char **err, unsigned int *timeout) |
| 8445 | { |
| 8446 | const char *error; |
| 8447 | |
| 8448 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8449 | if (error == PARSE_TIME_OVER) { |
| 8450 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8451 | args[1], args[0]); |
| 8452 | return -1; |
| 8453 | } |
| 8454 | else if (error == PARSE_TIME_UNDER) { |
| 8455 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8456 | args[1], args[0]); |
| 8457 | return -1; |
| 8458 | } |
| 8459 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8460 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8461 | return -1; |
| 8462 | } |
| 8463 | return 0; |
| 8464 | } |
| 8465 | |
| 8466 | 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] | 8467 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8468 | char **err) |
| 8469 | { |
| 8470 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8471 | file, line, err, &hlua_timeout_session); |
| 8472 | } |
| 8473 | |
| 8474 | 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] | 8475 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8476 | char **err) |
| 8477 | { |
| 8478 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8479 | file, line, err, &hlua_timeout_task); |
| 8480 | } |
| 8481 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8482 | 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] | 8483 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8484 | char **err) |
| 8485 | { |
| 8486 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8487 | file, line, err, &hlua_timeout_applet); |
| 8488 | } |
| 8489 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8490 | 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] | 8491 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8492 | char **err) |
| 8493 | { |
| 8494 | char *error; |
| 8495 | |
| 8496 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8497 | if (*error != '\0') { |
| 8498 | memprintf(err, "%s: invalid number", args[0]); |
| 8499 | return -1; |
| 8500 | } |
| 8501 | return 0; |
| 8502 | } |
| 8503 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8504 | 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] | 8505 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8506 | char **err) |
| 8507 | { |
| 8508 | char *error; |
| 8509 | |
| 8510 | if (*(args[1]) == 0) { |
| 8511 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8512 | return -1; |
| 8513 | } |
| 8514 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8515 | if (*error != '\0') { |
| 8516 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8517 | return -1; |
| 8518 | } |
| 8519 | return 0; |
| 8520 | } |
| 8521 | |
| 8522 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8523 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8524 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8525 | * the main lua entry point. |
| 8526 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8527 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8528 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8529 | * |
| 8530 | * In some error case, LUA set an error message in top of the stack. This function |
| 8531 | * 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] | 8532 | * |
| 8533 | * This function can fail with an abort() due to an Lua critical error. |
| 8534 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8535 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8536 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8537 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8538 | { |
| 8539 | int error; |
| 8540 | |
| 8541 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8542 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8543 | if (error) { |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8544 | 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] | 8545 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8546 | return -1; |
| 8547 | } |
| 8548 | |
| 8549 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8550 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8551 | switch (error) { |
| 8552 | case LUA_OK: |
| 8553 | break; |
| 8554 | case LUA_ERRRUN: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8555 | memprintf(err, "Lua runtime error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8556 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8557 | return -1; |
| 8558 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8559 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8560 | return -1; |
| 8561 | case LUA_ERRERR: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8562 | 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] | 8563 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8564 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8565 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8566 | case LUA_ERRGCMM: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8567 | 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] | 8568 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8569 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8570 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8571 | default: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8572 | memprintf(err, "Lua unknown error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8573 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8574 | return -1; |
| 8575 | } |
| 8576 | |
| 8577 | return 0; |
| 8578 | } |
| 8579 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8580 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8581 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8582 | char **err) |
| 8583 | { |
| 8584 | if (*(args[1]) == 0) { |
| 8585 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8586 | return -1; |
| 8587 | } |
| 8588 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8589 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8590 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8591 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8592 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8593 | } |
| 8594 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8595 | 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] | 8596 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8597 | char **err) |
| 8598 | { |
| 8599 | int len; |
| 8600 | |
| 8601 | if (*(args[1]) == 0) { |
| 8602 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8603 | return -1; |
| 8604 | } |
| 8605 | |
| 8606 | if (per_thread_load == NULL) { |
| 8607 | /* allocate the first entry large enough to store the final NULL */ |
| 8608 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8609 | if (per_thread_load == NULL) { |
| 8610 | memprintf(err, "out of memory error"); |
| 8611 | return -1; |
| 8612 | } |
| 8613 | } |
| 8614 | |
| 8615 | /* count used entries */ |
| 8616 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8617 | ; |
| 8618 | |
| 8619 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8620 | if (per_thread_load == NULL) { |
| 8621 | memprintf(err, "out of memory error"); |
| 8622 | return -1; |
| 8623 | } |
| 8624 | |
| 8625 | per_thread_load[len] = strdup(args[1]); |
| 8626 | per_thread_load[len + 1] = NULL; |
| 8627 | |
| 8628 | if (per_thread_load[len] == NULL) { |
| 8629 | memprintf(err, "out of memory error"); |
| 8630 | return -1; |
| 8631 | } |
| 8632 | |
| 8633 | /* loading for thread 1 only */ |
| 8634 | hlua_state_id = 1; |
| 8635 | ha_set_tid(0); |
| 8636 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8637 | } |
| 8638 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8639 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8640 | * in the given <ctx>. |
| 8641 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8642 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8643 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8644 | lua_getglobal(L, "package"); /* push package variable */ |
| 8645 | lua_pushstring(L, path); /* push given path */ |
| 8646 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8647 | lua_getfield(L, -3, type); /* push old path */ |
| 8648 | lua_concat(L, 3); /* concatenate to new path */ |
| 8649 | lua_setfield(L, -2, type); /* store new path */ |
| 8650 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8651 | |
| 8652 | return 0; |
| 8653 | } |
| 8654 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8655 | 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] | 8656 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8657 | char **err) |
| 8658 | { |
| 8659 | char *path; |
| 8660 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8661 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8662 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8663 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8664 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8665 | } |
| 8666 | |
| 8667 | if (!(*args[1])) { |
| 8668 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8669 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8670 | } |
| 8671 | path = args[1]; |
| 8672 | |
| 8673 | if (*args[2]) { |
| 8674 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8675 | 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] | 8676 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8677 | } |
| 8678 | type = args[2]; |
| 8679 | } |
| 8680 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8681 | p = calloc(1, sizeof(*p)); |
| 8682 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8683 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8684 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8685 | } |
| 8686 | p->path = strdup(path); |
| 8687 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8688 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8689 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8690 | } |
| 8691 | p->type = strdup(type); |
| 8692 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8693 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8694 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8695 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8696 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8697 | |
| 8698 | hlua_prepend_path(hlua_states[0], type, path); |
| 8699 | hlua_prepend_path(hlua_states[1], type, path); |
| 8700 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8701 | |
| 8702 | err2: |
| 8703 | free(p->type); |
| 8704 | free(p->path); |
| 8705 | err: |
| 8706 | free(p); |
| 8707 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8708 | } |
| 8709 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8710 | /* configuration keywords declaration */ |
| 8711 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8712 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8713 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8714 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8715 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8716 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8717 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8718 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8719 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8720 | { 0, NULL, NULL }, |
| 8721 | }}; |
| 8722 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8723 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8724 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8725 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8726 | /* This function can fail with an abort() due to an Lua critical error. |
| 8727 | * We are in the initialisation process of HAProxy, this abort() is |
| 8728 | * tolerated. |
| 8729 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8730 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8731 | { |
| 8732 | struct hlua_init_function *init; |
| 8733 | const char *msg; |
| 8734 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8735 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8736 | const char *kind; |
| 8737 | const char *trace; |
| 8738 | int return_status = 1; |
| 8739 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8740 | int nres; |
| 8741 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8742 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8743 | /* disable memory limit checks if limit is not set */ |
| 8744 | if (!hlua_global_allocator.limit) |
| 8745 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8746 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8747 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8748 | if (setjmp(safe_ljmp_env) != 0) { |
| 8749 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8750 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8751 | error = hlua_tostring_safe(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8752 | else |
| 8753 | error = "critical error"; |
| 8754 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8755 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8756 | } else { |
| 8757 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8758 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8759 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8760 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8761 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8762 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8763 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 8764 | /* function ref should be released right away since it was pushed |
| 8765 | * on the stack and will not be used anymore |
| 8766 | */ |
| 8767 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8768 | |
| 8769 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8770 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8771 | #else |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8772 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8773 | #endif |
| 8774 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8775 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8776 | |
| 8777 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8778 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8779 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8780 | |
| 8781 | case LUA_ERRERR: |
| 8782 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8783 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8784 | case LUA_ERRRUN: |
| 8785 | if (!kind) |
| 8786 | kind = "runtime error"; |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8787 | msg = hlua_tostring_safe(L, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8788 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8789 | if (msg) |
| 8790 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8791 | else |
| 8792 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 8793 | |
| 8794 | lua_settop(L, 0); /* Empty the stack. */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8795 | return_status = 0; |
| 8796 | break; |
| 8797 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8798 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8799 | /* Unknown error */ |
| 8800 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8801 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8802 | case LUA_YIELD: |
| 8803 | /* yield is not configured at this step, this state doesn't happen */ |
| 8804 | if (!kind) |
| 8805 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8806 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8807 | case LUA_ERRMEM: |
| 8808 | if (!kind) |
| 8809 | kind = "out of memory error"; |
Aurelien DARRAGON | 93f5d8d | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 8810 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8811 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8812 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8813 | return_status = 0; |
| 8814 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8815 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8816 | if (!return_status) |
| 8817 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8818 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8819 | |
| 8820 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8821 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8822 | } |
| 8823 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8824 | int hlua_post_init() |
| 8825 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8826 | int ret; |
| 8827 | int i; |
| 8828 | int errors; |
| 8829 | char *err = NULL; |
| 8830 | struct hlua_function *fcn; |
| 8831 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8832 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8833 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8834 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8835 | int saved_used_backed = global.ssl_used_backend; |
| 8836 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8837 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8838 | global.ssl_used_backend = saved_used_backed; |
| 8839 | } |
| 8840 | #endif |
| 8841 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8842 | /* Perform post init of common thread */ |
| 8843 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8844 | ha_set_tid(0); |
| 8845 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8846 | if (ret == 0) |
| 8847 | return 0; |
| 8848 | |
| 8849 | /* init remaining lua states and load files */ |
| 8850 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8851 | |
| 8852 | /* set thread context */ |
| 8853 | ha_set_tid(hlua_state_id - 1); |
| 8854 | |
| 8855 | /* Init lua state */ |
| 8856 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8857 | |
| 8858 | /* Load lua files */ |
| 8859 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8860 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8861 | if (ret != 0) { |
| 8862 | ha_alert("Lua init: %s\n", err); |
| 8863 | return 0; |
| 8864 | } |
| 8865 | } |
| 8866 | } |
| 8867 | |
| 8868 | /* Reset thread context */ |
| 8869 | ha_set_tid(0); |
| 8870 | |
| 8871 | /* Execute post init for all states */ |
| 8872 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8873 | |
| 8874 | /* set thread context */ |
| 8875 | ha_set_tid(hlua_state_id - 1); |
| 8876 | |
| 8877 | /* run post init */ |
| 8878 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8879 | if (ret == 0) |
| 8880 | return 0; |
| 8881 | } |
| 8882 | |
| 8883 | /* Reset thread context */ |
| 8884 | ha_set_tid(0); |
| 8885 | |
| 8886 | /* control functions registering. Each function must have: |
| 8887 | * - only the function_ref[0] set positive and all other to -1 |
| 8888 | * - only the function_ref[0] set to -1 and all other positive |
| 8889 | * This ensure a same reference is not used both in shared |
| 8890 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8891 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8892 | * complicated to found for the end user. |
| 8893 | */ |
| 8894 | errors = 0; |
| 8895 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8896 | ret = 0; |
| 8897 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8898 | if (fcn->function_ref[i] == -1) |
| 8899 | ret--; |
| 8900 | else |
| 8901 | ret++; |
| 8902 | } |
| 8903 | if (abs(ret) != global.nbthread) { |
| 8904 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8905 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8906 | errors++; |
| 8907 | continue; |
| 8908 | } |
| 8909 | |
| 8910 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8911 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8912 | "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] | 8913 | "exclusive.\n", fcn->name); |
| 8914 | errors++; |
| 8915 | } |
| 8916 | } |
| 8917 | |
| 8918 | if (errors > 0) |
| 8919 | return 0; |
| 8920 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8921 | /* 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] | 8922 | * -1 in order to have probably a segfault if someone use it |
| 8923 | */ |
| 8924 | hlua_state_id = -1; |
| 8925 | |
| 8926 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8927 | } |
| 8928 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8929 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8930 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8931 | * 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] | 8932 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8933 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8934 | * zero. This one verifies that the limits are respected but is optimized |
| 8935 | * 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] | 8936 | * |
| 8937 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8938 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8939 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8940 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8941 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8942 | */ |
| 8943 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8944 | { |
| 8945 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8946 | size_t limit, old, new; |
| 8947 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8948 | if (unlikely(!ptr && !nsize)) |
| 8949 | return NULL; |
| 8950 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8951 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8952 | * for accounting anymore. |
| 8953 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8954 | if (likely(~zone->limit == 0)) { |
| 8955 | if (!nsize) |
| 8956 | ha_free(&ptr); |
| 8957 | else |
| 8958 | ptr = realloc(ptr, nsize); |
| 8959 | return ptr; |
| 8960 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8961 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8962 | if (!ptr) |
| 8963 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8964 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8965 | /* enforce strict limits across all threads */ |
| 8966 | limit = zone->limit; |
| 8967 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8968 | do { |
| 8969 | new = old + nsize - osize; |
| 8970 | if (unlikely(nsize && limit && new > limit)) |
| 8971 | return NULL; |
| 8972 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8973 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8974 | if (!nsize) |
| 8975 | ha_free(&ptr); |
| 8976 | else |
| 8977 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8978 | |
| 8979 | if (unlikely(!ptr && nsize)) // failed |
| 8980 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8981 | |
| 8982 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8983 | return ptr; |
| 8984 | } |
| 8985 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8986 | /* 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] | 8987 | * We are in the initialisation process of HAProxy, this abort() is |
| 8988 | * tolerated. |
| 8989 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8990 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8991 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8992 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8993 | int idx; |
| 8994 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8995 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8996 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8997 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8998 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8999 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9000 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9001 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9002 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9003 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9004 | |
firexinghe | bdd336c | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 9005 | if (!L) { |
| 9006 | fprintf(stderr, |
| 9007 | "Lua init: critical error: lua_newstate() returned NULL." |
| 9008 | " This may possibly be caused by a memory allocation error.\n"); |
| 9009 | exit(1); |
| 9010 | } |
| 9011 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9012 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9013 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9014 | *context = NULL; |
| 9015 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9016 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9017 | * the Lua function can fail with an abort. We are in the initialisation |
| 9018 | * process of HAProxy, this abort() is tolerated. |
| 9019 | */ |
| 9020 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9021 | /* Call post initialisation function in safe environment. */ |
| 9022 | if (setjmp(safe_ljmp_env) != 0) { |
| 9023 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9024 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9025 | error_msg = hlua_tostring_safe(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9026 | else |
| 9027 | error_msg = "critical error"; |
| 9028 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 9029 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9030 | } else { |
| 9031 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9032 | } |
| 9033 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9034 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9035 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9036 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 9037 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 9038 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9039 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9040 | #endif |
| 9041 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9042 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9043 | #endif |
| 9044 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 9045 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9046 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9047 | /* Apply configured prepend path */ |
| 9048 | list_for_each_entry(pp, &prepend_path_list, l) |
| 9049 | hlua_prepend_path(L, pp->type, pp->path); |
| 9050 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9051 | /* |
| 9052 | * |
| 9053 | * Create "core" object. |
| 9054 | * |
| 9055 | */ |
| 9056 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 9057 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9058 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9059 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9060 | /* set the thread id */ |
| 9061 | hlua_class_const_int(L, "thread", thread_num); |
| 9062 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9063 | /* Push the loglevel constants. */ |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 9064 | hlua_class_const_int(L, "silent", -1); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 9065 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9066 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9067 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9068 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9069 | hlua_class_function(L, "register_init", hlua_register_init); |
| 9070 | hlua_class_function(L, "register_task", hlua_register_task); |
| 9071 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 9072 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 9073 | hlua_class_function(L, "register_action", hlua_register_action); |
| 9074 | hlua_class_function(L, "register_service", hlua_register_service); |
| 9075 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 9076 | hlua_class_function(L, "yield", hlua_yield); |
| 9077 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 9078 | hlua_class_function(L, "sleep", hlua_sleep); |
| 9079 | hlua_class_function(L, "msleep", hlua_msleep); |
| 9080 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 9081 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 9082 | hlua_class_function(L, "set_map", hlua_set_map); |
| 9083 | hlua_class_function(L, "del_map", hlua_del_map); |
| 9084 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 9085 | hlua_class_function(L, "log", hlua_log); |
| 9086 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 9087 | hlua_class_function(L, "Info", hlua_log_info); |
| 9088 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 9089 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 9090 | hlua_class_function(L, "done", hlua_done); |
| 9091 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9092 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9093 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9094 | |
| 9095 | /* |
| 9096 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9097 | * Create "act" object. |
| 9098 | * |
| 9099 | */ |
| 9100 | |
| 9101 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9102 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9103 | |
| 9104 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9105 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 9106 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 9107 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 9108 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9109 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9110 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9111 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9112 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9113 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9114 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9115 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9116 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9117 | |
| 9118 | /* |
| 9119 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9120 | * Register class Map |
| 9121 | * |
| 9122 | */ |
| 9123 | |
| 9124 | /* This table entry is the object "Map" base. */ |
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 | |
| 9127 | /* register pattern types. */ |
| 9128 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9129 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9130 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9131 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9132 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9133 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9134 | |
| 9135 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9136 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9137 | |
| 9138 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9139 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9140 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9141 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9142 | lua_pushstring(L, "__index"); |
| 9143 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9144 | |
| 9145 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9146 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9147 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9148 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9149 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9150 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9151 | /* Register previous table in the registry with reference and named entry. |
| 9152 | * The function hlua_register_metatable() pops the stack, so we |
| 9153 | * previously create a copy of the table. |
| 9154 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9155 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9156 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9157 | |
| 9158 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9159 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9160 | |
| 9161 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9162 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9163 | |
| 9164 | /* |
| 9165 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9166 | * Register class Channel |
| 9167 | * |
| 9168 | */ |
| 9169 | |
| 9170 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9171 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9172 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9173 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9174 | lua_pushstring(L, "__index"); |
| 9175 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9176 | |
| 9177 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9178 | hlua_class_function(L, "get", hlua_channel_get); |
| 9179 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9180 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9181 | hlua_class_function(L, "set", hlua_channel_set); |
| 9182 | hlua_class_function(L, "append", hlua_channel_append); |
| 9183 | hlua_class_function(L, "send", hlua_channel_send); |
| 9184 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9185 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9186 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9187 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9188 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9189 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9190 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9191 | |
| 9192 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9193 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9194 | |
| 9195 | /* |
| 9196 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9197 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9198 | * |
| 9199 | */ |
| 9200 | |
| 9201 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9202 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9203 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9204 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9205 | lua_pushstring(L, "__index"); |
| 9206 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9207 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9208 | /* Browse existing fetches and create the associated |
| 9209 | * object method. |
| 9210 | */ |
| 9211 | sf = NULL; |
| 9212 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9213 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9214 | * by an underscore. |
| 9215 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9216 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9217 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9218 | if (*p == '.' || *p == '-' || *p == '+') |
| 9219 | *p = '_'; |
| 9220 | |
| 9221 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9222 | lua_pushstring(L, trash.area); |
| 9223 | lua_pushlightuserdata(L, sf); |
| 9224 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9225 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9226 | } |
| 9227 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9228 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9229 | |
| 9230 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9231 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9232 | |
| 9233 | /* |
| 9234 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9235 | * Register class Converters |
| 9236 | * |
| 9237 | */ |
| 9238 | |
| 9239 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9240 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9241 | |
| 9242 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9243 | lua_pushstring(L, "__index"); |
| 9244 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9245 | |
| 9246 | /* Browse existing converters and create the associated |
| 9247 | * object method. |
| 9248 | */ |
| 9249 | sc = NULL; |
| 9250 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9251 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9252 | * by an underscore. |
| 9253 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9254 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9255 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9256 | if (*p == '.' || *p == '-' || *p == '+') |
| 9257 | *p = '_'; |
| 9258 | |
| 9259 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9260 | lua_pushstring(L, trash.area); |
| 9261 | lua_pushlightuserdata(L, sc); |
| 9262 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9263 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9264 | } |
| 9265 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9266 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9267 | |
| 9268 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9269 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9270 | |
| 9271 | /* |
| 9272 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9273 | * Register class HTTP |
| 9274 | * |
| 9275 | */ |
| 9276 | |
| 9277 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9278 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9279 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9280 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9281 | lua_pushstring(L, "__index"); |
| 9282 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9283 | |
| 9284 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9285 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9286 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9287 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9288 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9289 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9290 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9291 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9292 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9293 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9294 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9295 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9296 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9297 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9298 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9299 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9300 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9301 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9302 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9303 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9304 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9305 | |
| 9306 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9307 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9308 | |
| 9309 | /* |
| 9310 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9311 | * Register class AppletTCP |
| 9312 | * |
| 9313 | */ |
| 9314 | |
| 9315 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9316 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9317 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9318 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9319 | lua_pushstring(L, "__index"); |
| 9320 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9321 | |
| 9322 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9323 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9324 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9325 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9326 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9327 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9328 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9329 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9330 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9331 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9332 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9333 | |
| 9334 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9335 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9336 | |
| 9337 | /* |
| 9338 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9339 | * Register class AppletHTTP |
| 9340 | * |
| 9341 | */ |
| 9342 | |
| 9343 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9344 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9345 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9346 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9347 | lua_pushstring(L, "__index"); |
| 9348 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9349 | |
| 9350 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9351 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9352 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9353 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9354 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9355 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9356 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9357 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9358 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9359 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9360 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9361 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9362 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9363 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9364 | |
| 9365 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9366 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9367 | |
| 9368 | /* |
| 9369 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9370 | * Register class TXN |
| 9371 | * |
| 9372 | */ |
| 9373 | |
| 9374 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9375 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9376 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9377 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9378 | lua_pushstring(L, "__index"); |
| 9379 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9380 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9381 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9382 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9383 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9384 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9385 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9386 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9387 | hlua_class_function(L, "done", hlua_txn_done); |
| 9388 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9389 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9390 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9391 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9392 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9393 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9394 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9395 | hlua_class_function(L, "log", hlua_txn_log); |
| 9396 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9397 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9398 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9399 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9400 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9401 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9402 | |
| 9403 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9404 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9405 | |
| 9406 | /* |
| 9407 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9408 | * Register class reply |
| 9409 | * |
| 9410 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9411 | lua_newtable(L); |
| 9412 | lua_pushstring(L, "__index"); |
| 9413 | lua_newtable(L); |
| 9414 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9415 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9416 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9417 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9418 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9419 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9420 | |
| 9421 | |
| 9422 | /* |
| 9423 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9424 | * Register class Socket |
| 9425 | * |
| 9426 | */ |
| 9427 | |
| 9428 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9429 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9430 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9431 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9432 | lua_pushstring(L, "__index"); |
| 9433 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9434 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9435 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9436 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9437 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9438 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9439 | hlua_class_function(L, "send", hlua_socket_send); |
| 9440 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9441 | hlua_class_function(L, "close", hlua_socket_close); |
| 9442 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9443 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9444 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9445 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9446 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9447 | 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] | 9448 | |
| 9449 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9450 | lua_pushstring(L, "__gc"); |
| 9451 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9452 | 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] | 9453 | |
| 9454 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9455 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9456 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9457 | lua_atpanic(L, hlua_panic_safe); |
| 9458 | |
| 9459 | return L; |
| 9460 | } |
| 9461 | |
| 9462 | void hlua_init(void) { |
| 9463 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9464 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9465 | #ifdef USE_OPENSSL |
| 9466 | struct srv_kw *kw; |
| 9467 | int tmp_error; |
| 9468 | char *error; |
| 9469 | char *args[] = { /* SSL client configuration. */ |
| 9470 | "ssl", |
| 9471 | "verify", |
| 9472 | "none", |
| 9473 | NULL |
| 9474 | }; |
| 9475 | #endif |
| 9476 | |
| 9477 | /* Init post init function list head */ |
| 9478 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9479 | LIST_INIT(&hlua_init_functions[i]); |
| 9480 | |
| 9481 | /* Init state for common/shared lua parts */ |
| 9482 | hlua_state_id = 0; |
| 9483 | ha_set_tid(0); |
| 9484 | hlua_states[0] = hlua_init_state(0); |
| 9485 | |
| 9486 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9487 | hlua_state_id = 1; |
| 9488 | ha_set_tid(0); |
| 9489 | hlua_states[1] = hlua_init_state(1); |
| 9490 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9491 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9492 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9493 | if (!socket_proxy) { |
| 9494 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9495 | exit(1); |
| 9496 | } |
| 9497 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9498 | |
| 9499 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9500 | socket_tcp = new_server(socket_proxy); |
| 9501 | if (!socket_tcp) { |
| 9502 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9503 | exit(1); |
| 9504 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9505 | |
| 9506 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9507 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9508 | socket_ssl = new_server(socket_proxy); |
| 9509 | if (!socket_ssl) { |
| 9510 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9511 | exit(1); |
| 9512 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9513 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9514 | socket_ssl->use_ssl = 1; |
| 9515 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9516 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9517 | for (i = 0; args[i] != NULL; i++) { |
| 9518 | 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] | 9519 | /* |
| 9520 | * |
| 9521 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9522 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9523 | * features like client certificates and ssl_verify. |
| 9524 | * |
| 9525 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9526 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9527 | if (tmp_error != 0) { |
| 9528 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9529 | abort(); /* This must be never arrives because the command line |
| 9530 | not editable by the user. */ |
| 9531 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9532 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9533 | } |
| 9534 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9535 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9536 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9537 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9538 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9539 | static void hlua_deinit() |
| 9540 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9541 | int thr; |
| 9542 | |
| 9543 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9544 | if (hlua_states[thr]) |
| 9545 | lua_close(hlua_states[thr]); |
| 9546 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9547 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9548 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9549 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9550 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9551 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9552 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9553 | |
| 9554 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9555 | } |
| 9556 | |
| 9557 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9558 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9559 | static void hlua_register_build_options(void) |
| 9560 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9561 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9562 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9563 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9564 | hap_register_build_opts(ptr, 1); |
| 9565 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9566 | |
| 9567 | INITCALL0(STG_REGISTER, hlua_register_build_options); |